From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 1386 invoked by alias); 22 Apr 2015 09:26:54 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 1350 invoked by uid 89); 22 Apr 2015 09:26:54 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=0.3 required=5.0 tests=AWL,BAYES_50,KAM_LAZY_DOMAIN_SECURITY,SPF_HELO_PASS,T_RP_MATCHES_RCVD autolearn=no version=3.3.2 X-HELO: mailout1.w1.samsung.com Received: from mailout1.w1.samsung.com (HELO mailout1.w1.samsung.com) (210.118.77.11) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Wed, 22 Apr 2015 09:26:52 +0000 Received: from eucpsbgm1.samsung.com (unknown [203.254.199.244]) by mailout1.w1.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTP id <0NN700HYABKOEL40@mailout1.w1.samsung.com> for gcc-patches@gcc.gnu.org; Wed, 22 Apr 2015 10:26:48 +0100 (BST) Received: from eusync4.samsung.com ( [203.254.199.214]) by eucpsbgm1.samsung.com (EUCPMTA) with SMTP id 2E.D0.04846.85967355; Wed, 22 Apr 2015 10:26:48 +0100 (BST) Received: from [106.109.9.145] by eusync4.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTPA id <0NN700ACFBKNHQ30@eusync4.samsung.com>; Wed, 22 Apr 2015 10:26:48 +0100 (BST) Message-id: <55376961.609@samsung.com> Date: Wed, 22 Apr 2015 09:26:00 -0000 From: Yury Gribov User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 MIME-version: 1.0 To: Jakub Jelinek Cc: GCC Patches , Andi Kleen , Andrey Ryabinin , Dmitry Vyukov Subject: Re: [PATCH] Optionally sanitize globals in user-defined sections References: <5530B739.5000306@samsung.com> <5530B84E.7030709@samsung.com> <87vbgu1x4d.fsf@tassilo.jf.intel.com> <55335F51.3080900@samsung.com> <20150419151142.GZ1725@tucnak.redhat.com> <55375C7C.40804@samsung.com> <55375F49.6010701@samsung.com> <20150422090024.GC1725@tucnak.redhat.com> In-reply-to: <20150422090024.GC1725@tucnak.redhat.com> Content-type: multipart/mixed; boundary=------------010207070901060909010301 X-IsSubscribed: yes X-SW-Source: 2015-04/txt/msg01278.txt.bz2 This is a multi-part message in MIME format. --------------010207070901060909010301 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Content-length: 1893 On 04/22/2015 12:00 PM, Jakub Jelinek wrote: > On Wed, Apr 22, 2015 at 11:43:53AM +0300, Yury Gribov wrote: >> @@ -272,7 +273,7 @@ along with GCC; see the file COPYING3. If not see >> >> static unsigned HOST_WIDE_INT asan_shadow_offset_value; >> static bool asan_shadow_offset_computed; >> -static const char *sanitized_sections; >> +static vec *sanitized_sections; > > Why don't you use static vec sanitized_section instead? Fixed. I thought we try to avoid creating unnecessary vectors but probably not that important. >> -set_sanitized_sections (const char *secs) >> +set_sanitized_sections (const char *sections) >> { >> - sanitized_sections = secs; >> + char *pat; >> + for (unsigned i = 0; >> + sanitized_sections && sanitized_sections->iterate (i, &pat); >> + ++i) > > This really should be FOR_EACH_VEC_SAFE_ELT (if you keep using va_gc > vec *) or FOR_EACH_VEC_ELT. Done. >> + { >> + free (pat); >> + } > > No {}s around single line body. Done. >> @@ -308,16 +325,13 @@ set_sanitized_sections (const char *secs) >> static bool >> section_sanitized_p (const char *sec) >> { >> - if (!sanitized_sections) >> - return false; >> - size_t len = strlen (sec); >> - const char *p = sanitized_sections; >> - while ((p = strstr (p, sec))) >> + char *pat; >> + for (unsigned i = 0; >> + sanitized_sections && sanitized_sections->iterate (i, &pat); >> + ++i) > > Similarly. Ok. > Also, wonder if won't be too expensive if people use too long > list of sections. Perhaps we could cache positive as well as negative > answers in a hash table? Though, perhaps it is worth that only if this > shows up to be a bottleneck. Yeah, I thought about throwing in a hashtable but wasn't sure that added complexity would be justified. So I'd rather wait and see whether this causes a noticeable slowdown. -Y --------------010207070901060909010301 Content-Type: text/x-patch; name="user-section-3.diff" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="user-section-3.diff" Content-length: 5104 commit bc33a73d9406abf5209d98aba79eee33b14aadc6 Author: Yury Gribov Date: Tue Apr 21 20:47:04 2015 +0300 2015-04-22 Yury Gribov gcc/ * asan.c (set_sanitized_sections): Parse incoming arg. (section_sanitized_p): Support wildcards. * doc/invoke.texi (-fsanitize-sections): Update description. gcc/testsuite/ * c-c++-common/asan/user-section-1.c: New test. * c-c++-common/asan/user-section-2.c: New test. * c-c++-common/asan/user-section-3.c: New test. diff --git a/gcc/asan.c b/gcc/asan.c index cd6ccdc..479301a 100644 --- a/gcc/asan.c +++ b/gcc/asan.c @@ -88,6 +88,7 @@ along with GCC; see the file COPYING3. If not see #include "ubsan.h" #include "params.h" #include "builtins.h" +#include "fnmatch.h" /* AddressSanitizer finds out-of-bounds and use-after-free bugs with <2x slowdown on average. @@ -272,7 +273,7 @@ along with GCC; see the file COPYING3. If not see static unsigned HOST_WIDE_INT asan_shadow_offset_value; static bool asan_shadow_offset_computed; -static const char *sanitized_sections; +static vec sanitized_sections; /* Sets shadow offset to value in string VAL. */ @@ -298,9 +299,22 @@ set_asan_shadow_offset (const char *val) /* Set list of user-defined sections that need to be sanitized. */ void -set_sanitized_sections (const char *secs) +set_sanitized_sections (const char *sections) { - sanitized_sections = secs; + char *pat; + unsigned i; + FOR_EACH_VEC_ELT (sanitized_sections, i, pat) + free (pat); + sanitized_sections.truncate (0); + + for (const char *s = sections; *s; ) + { + const char *end; + for (end = s; *end && *end != ','; ++end); + size_t len = end - s; + sanitized_sections.safe_push (xstrndup (s, len)); + s = *end ? end + 1 : end; + } } /* Checks whether section SEC should be sanitized. */ @@ -308,17 +322,11 @@ set_sanitized_sections (const char *secs) static bool section_sanitized_p (const char *sec) { - if (!sanitized_sections) - return false; - size_t len = strlen (sec); - const char *p = sanitized_sections; - while ((p = strstr (p, sec))) - { - if ((p == sanitized_sections || p[-1] == ',') - && (p[len] == 0 || p[len] == ',')) - return true; - ++p; - } + char *pat; + unsigned i; + FOR_EACH_VEC_ELT (sanitized_sections, i, pat) + if (fnmatch (pat, sec, FNM_PERIOD) == 0) + return true; return false; } diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index c20dd4d..a939ff7 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -5806,7 +5806,8 @@ Kernel AddressSanitizer. @item -fsanitize-sections=@var{s1,s2,...} @opindex fsanitize-sections -Sanitize global variables in selected user-defined sections. +Sanitize global variables in selected user-defined sections. @var{si} may +contain wildcards. @item -fsanitize-recover@r{[}=@var{opts}@r{]} @opindex fsanitize-recover diff --git a/gcc/testsuite/c-c++-common/asan/user-section-1.c b/gcc/testsuite/c-c++-common/asan/user-section-1.c new file mode 100644 index 0000000..51e2b99 --- /dev/null +++ b/gcc/testsuite/c-c++-common/asan/user-section-1.c @@ -0,0 +1,11 @@ +/* { dg-do compile } */ +/* { dg-options "-fsanitize=address -fsanitize-sections=.xxx,.yyy -fdump-tree-sanopt" } */ +/* { dg-skip-if "" { *-*-* } { "-flto" } { "" } } */ + +int x __attribute__((section(".xxx"))) = 1; +int y __attribute__((section(".yyy"))) = 1; +int z __attribute__((section(".zzz"))) = 1; + +/* { dg-final { scan-tree-dump "__builtin___asan_unregister_globals \\(.*, 2\\);" "sanopt" } } */ +/* { dg-final { cleanup-tree-dump "sanopt" } } */ + diff --git a/gcc/testsuite/c-c++-common/asan/user-section-2.c b/gcc/testsuite/c-c++-common/asan/user-section-2.c new file mode 100644 index 0000000..f602116 --- /dev/null +++ b/gcc/testsuite/c-c++-common/asan/user-section-2.c @@ -0,0 +1,11 @@ +/* { dg-do compile } */ +/* { dg-options "-fsanitize=address -fsanitize-sections=.x* -fdump-tree-sanopt" } */ +/* { dg-skip-if "" { *-*-* } { "-flto" } { "" } } */ + +int x __attribute__((section(".x1"))) = 1; +int y __attribute__((section(".x2"))) = 1; +int z __attribute__((section(".x3"))) = 1; + +/* { dg-final { scan-tree-dump "__builtin___asan_unregister_globals \\(.*, 3\\);" "sanopt" } } */ +/* { dg-final { cleanup-tree-dump "sanopt" } } */ + diff --git a/gcc/testsuite/c-c++-common/asan/user-section-3.c b/gcc/testsuite/c-c++-common/asan/user-section-3.c new file mode 100644 index 0000000..66e5f9a --- /dev/null +++ b/gcc/testsuite/c-c++-common/asan/user-section-3.c @@ -0,0 +1,11 @@ +/* { dg-do compile } */ +/* { dg-options "-fsanitize=address -fsanitize-sections=.x* -fsanitize-sections=.y* -fdump-tree-sanopt" } */ +/* { dg-skip-if "" { *-*-* } { "-flto" } { "" } } */ + +int x __attribute__((section(".x1"))) = 1; +int y __attribute__((section(".x2"))) = 1; +int z __attribute__((section(".y1"))) = 1; + +/* { dg-final { scan-tree-dump "__builtin___asan_unregister_globals \\(.*, 1\\);" "sanopt" } } */ +/* { dg-final { cleanup-tree-dump "sanopt" } } */ + --------------010207070901060909010301--