From patchwork Thu Aug 12 04:41:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 1516101 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=b3gZu1gx; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4GlYwM6fCvz9t4b for ; Thu, 12 Aug 2021 14:43:15 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 54669385E82F for ; Thu, 12 Aug 2021 04:43:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 54669385E82F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1628743393; bh=8ATakqFbYkLqF/yAMZkPLyXq+SIMuSuaZS1SsVSCP8Q=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=b3gZu1gxSPCBpx9bi/Y0SJ0TH4nsmZnVxXI07P30QtrQIKNJUg3d4cKuSeH9LeU50 efTnmjoypqBzxZrd9wW3Z4eH7F/JkEUPL9xz3XJwF7H6qHzokjf9f0vIQCCpfy6qsU l1Ak5Pc5FvjNYvgoVInHWUNZm+zp1TdIvTzvYHZo= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from black.elm.relay.mailchannels.net (black.elm.relay.mailchannels.net [23.83.212.19]) by sourceware.org (Postfix) with ESMTPS id B12E039AF4F8 for ; Thu, 12 Aug 2021 04:41:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B12E039AF4F8 X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 7F4AD92170B; Thu, 12 Aug 2021 04:41:55 +0000 (UTC) Received: from pdx1-sub0-mail-a1.g.dreamhost.com (100-96-18-119.trex.outbound.svc.cluster.local [100.96.18.119]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 00F419219B1; Thu, 12 Aug 2021 04:41:54 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from pdx1-sub0-mail-a1.g.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384) by 100.96.18.119 (trex/6.3.3); Thu, 12 Aug 2021 04:41:55 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Share-Wipe: 72883a3a642b70f0_1628743315256_1291841341 X-MC-Loop-Signature: 1628743315255:396376706 X-MC-Ingress-Time: 1628743315255 Received: from pdx1-sub0-mail-a1.g.dreamhost.com (localhost [127.0.0.1]) by pdx1-sub0-mail-a1.g.dreamhost.com (Postfix) with ESMTP id B6E6E8017A; Wed, 11 Aug 2021 21:41:54 -0700 (PDT) Received: from rhbox.intra.reserved-bit.com (unknown [1.186.101.110]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a1.g.dreamhost.com (Postfix) with ESMTPSA id 1662D85C0B; Wed, 11 Aug 2021 21:41:51 -0700 (PDT) X-DH-BACKEND: pdx1-sub0-mail-a1 To: libc-alpha@sourceware.org Subject: [PATCH v2] mtrace: Fix output with PIE and ASLR [BZ #22716] Date: Thu, 12 Aug 2021 10:11:36 +0530 Message-Id: <20210812044136.3407827-1-siddhesh@sourceware.org> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Spam-Status: No, score=-3493.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NEUTRAL, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Siddhesh Poyarekar via Libc-alpha From: Siddhesh Poyarekar Reply-To: Siddhesh Poyarekar Cc: John Ogness Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" Record only the relative address of the caller in mtrace file. Use LD_TRACE_PRELINKING to get the executable as well as binary vs executable load offsets so that we may compute a base to add to the relative address in the mtrace file. This allows us to get a valid address to pass to addr2line in all cases. Co-authored-by: John Ogness --- Changes from v1: - Set LD_TRACE_PRELINK to an empty string so that it doesn't match any files and add a comment explaining it. - Formatting change in mtrace-impl.c malloc/mtrace-impl.c | 6 +++--- malloc/mtrace.pl | 15 +++++++-------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/malloc/mtrace-impl.c b/malloc/mtrace-impl.c index 6af1a6e01a..23afc9a1df 100644 --- a/malloc/mtrace-impl.c +++ b/malloc/mtrace-impl.c @@ -67,9 +67,9 @@ tr_where (const void *caller, Dl_info *info) offset); } - fprintf (mallstream, "@ %s%s%s[%p] ", info->dli_fname ? : "", - info->dli_fname ? ":" : "", - buf, caller); + fprintf (mallstream, "@ %s%s%s[0x%" PRIxPTR "] ", + info->dli_fname ? : "", info->dli_fname ? ":" : "", buf, + caller - info->dli_fbase); } else fprintf (mallstream, "@ [%p] ", caller); diff --git a/malloc/mtrace.pl b/malloc/mtrace.pl index 6f49c8338d..bdc407122b 100644 --- a/malloc/mtrace.pl +++ b/malloc/mtrace.pl @@ -75,11 +75,15 @@ if ($#ARGV == 0) { } else { $prog = "./$binary"; } - if (open (LOCS, "env LD_TRACE_LOADED_OBJECTS=1 $prog |")) { + # Set the environment variable LD_TRACE_PRELINKING to an empty string so + # that we trigger tracing but do not match with the executable or any of + # its dependencies. + if (open (LOCS, "env LD_TRACE_PRELINKING=\"\" $prog |")) { while () { chop; - if (/^.*=> (.*) .(0x[0123456789abcdef]*).$/) { + if (/^.*=> (.*) \((0x[0123456789abcdef]*), (0x[0123456789abcdef]*).*/) { $locs{$1} = $2; + $rel{$1} = hex($2) - hex($3); } } close (LOCS); @@ -110,12 +114,7 @@ sub location { my $addr = $2; my $searchaddr; return $cache{$addr} if (exists $cache{$addr}); - if ($locs{$prog} ne "") { - $searchaddr = sprintf "%#x", $addr - $locs{$prog}; - } else { - $searchaddr = $addr; - $prog = $binary; - } + $searchaddr = sprintf "%#x", hex($addr) + $rel{$prog}; if ($binary ne "" && open (ADDR, "addr2line -e $prog $searchaddr|")) { my $line = ; chomp $line;