From patchwork Fri Dec 6 09:53:37 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 297661 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id B9CD62C00A1 for ; Fri, 6 Dec 2013 20:53:58 +1100 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:reply-to:references:mime-version :content-type:in-reply-to; q=dns; s=default; b=HsgUyOE+ycE8Kmyh1 FeMRNYtls5dCrcynLHm4vqoEWCqmSU7CL8oEDgfEtZZs4oICG3xtStwiSp/I7wnr jvZptFec2Al0Njn2kHM746ltPqN8yk2iCFX0exl2edan7GBiDu0CVqhSaeDVneaV FknypVVNs6uKyWkVc4ywIE1Byc= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:reply-to:references:mime-version :content-type:in-reply-to; s=default; bh=mKkJg3ZdzEP1+PnZ/GVRP5Y Poh0=; b=BIjDlq0DqD/CBgo+oIui3Zx90iUEsbsMZttowVlfAebTkt2gwiTaxfn 1YK6o9SZNWiZm3v4xGQoQDpmfdMo1pl5XNqYbPIkcT0Op+SaJAxRN6EyzIevBieC zb+bIEj5TEE+zBBksCO5s5Cm6wJAz9eQ+z1zF+qJ253L9svXwpXw= Received: (qmail 7939 invoked by alias); 6 Dec 2013 09:53:52 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 7875 invoked by uid 89); 6 Dec 2013 09:53:51 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.8 required=5.0 tests=AWL, BAYES_00, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.2 X-HELO: mx1.redhat.com Received: from Unknown (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 06 Dec 2013 09:53:51 +0000 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id rB69rhoB025184 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 6 Dec 2013 04:53:43 -0500 Received: from tucnak.zalov.cz (vpn1-5-16.ams2.redhat.com [10.36.5.16]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id rB69rfKW016357 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Fri, 6 Dec 2013 04:53:43 -0500 Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.14.7/8.14.7) with ESMTP id rB69rff5004040; Fri, 6 Dec 2013 10:53:41 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.14.7/8.14.7/Submit) id rB69rb91004039; Fri, 6 Dec 2013 10:53:37 +0100 Date: Fri, 6 Dec 2013 10:53:37 +0100 From: Jakub Jelinek To: Alexey Samsonov Cc: Dmitry Vyukov , Ian Lance Taylor , Konstantin Serebryany , GCC Patches Subject: Re: [PATCH] Handle PIEs in libbacktrace Message-ID: <20131206095337.GH892@tucnak.redhat.com> Reply-To: Jakub Jelinek References: <20131206075033.GE892@tucnak.redhat.com> <20131206082507.GF892@tucnak.redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) X-IsSubscribed: yes On Fri, Dec 06, 2013 at 12:53:02PM +0400, Alexey Samsonov wrote: > ASan calls this function only for global variables (not functions). It > is needed because > we use a different machinery to pass global names to ASan runtime - we > register globals > by a call from instrumented code, which also tells runtime the > (mangled) global name. Well, given that it demangled just fine even function names, I guess it calls it always and just it would do nothing for function names demangled already by llvm-symbolizer if using the external symbolizer. > OTOH, in TSan we rely on symbolization to demangle names of global variables. Then we need something like following, with which all tests pass. I'm not using Demangle method from the symbolizer, because getting it requires a mutex that is already held. The alternative would be to just (perhaps under #ifdef SANITIZER_CP_DEMANGLE) compile in libiberty/cp-demangle.c (similarly how libstdc++ compiles it in) as part of libsanitizer/libiberty/ or even libsanitizer/libbacktrace/, and tweak it, so that like libsanitizer/libbacktrace it uses internal_memcpy etc. and uses InternalAlloc/InternalFree. The problem is that cp-demangle.c uses only realloc and free, and doesn't provide any hint on how large the previously allocated memory chunk is. So, either there is some easy way how to query the size of InternalAlloc returned allocation, or we would need to allocate uptr extra and store there number of bytes allocated and emulate realloc/free that way. Jakub --- libsanitizer/sanitizer_common/sanitizer_symbolizer_libbacktrace.cc.jj 2013-12-05 12:04:28.000000000 +0100 +++ libsanitizer/sanitizer_common/sanitizer_symbolizer_libbacktrace.cc 2013-12-06 10:36:02.285059188 +0100 @@ -25,6 +25,15 @@ # endif #endif +// C++ demangling function, as required by Itanium C++ ABI. This is weak, +// because we do not require a C++ ABI library to be linked to a program +// using sanitizers; if it's not present, we'll just use the mangled name. +namespace __cxxabiv1 { + extern "C" SANITIZER_WEAK_ATTRIBUTE + char *__cxa_demangle(const char *mangled, char *buffer, + size_t *length, int *status); +} + namespace __sanitizer { #if SANITIZER_LIBBACKTRACE @@ -39,6 +48,19 @@ struct SymbolizeCodeData { uptr module_offset; }; +char *MaybeDemangleAndDup(const char *symname) { + const char *demangled = symname; + if (symname[0] == '_' && symname[1] == 'Z' && __cxxabiv1::__cxa_demangle) { + demangled = __cxxabiv1::__cxa_demangle(symname, 0, 0, 0); + if (demangled == NULL) + demangled = symname; + } + char *ret = internal_strdup(demangled); + if (demangled != symname) + __builtin_free ((void *) demangled); + return ret; +} + extern "C" { static int SymbolizeCodePCInfoCallback(void *vdata, uintptr_t addr, const char *filename, int lineno, @@ -49,7 +71,7 @@ static int SymbolizeCodePCInfoCallback(v info->Clear(); info->FillAddressAndModuleInfo(addr, cdata->module_name, cdata->module_offset); - info->function = internal_strdup(function); + info->function = MaybeDemangleAndDup(function); if (filename) info->file = internal_strdup(filename); info->line = lineno; @@ -67,7 +89,7 @@ static void SymbolizeCodeCallback(void * info->Clear(); info->FillAddressAndModuleInfo(addr, cdata->module_name, cdata->module_offset); - info->function = internal_strdup(symname); + info->function = MaybeDemangleAndDup(symname); cdata->n_frames = 1; } } @@ -76,7 +98,7 @@ static void SymbolizeDataCallback(void * uintptr_t symval, uintptr_t symsize) { DataInfo *info = (DataInfo *)vdata; if (symname && symval) { - info->name = internal_strdup(symname); + info->name = MaybeDemangleAndDup(symname); info->start = symval; info->size = symsize; }