From: Yury Gribov Date: Wed, 22 Apr 2015 11:44:26 +0000 (+0000) Subject: Allow wildcards in -fsanitize-sections. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=860503d8cc91ca9a5d8bf3db0b88281f31257603;p=gcc.git Allow wildcards in -fsanitize-sections. 2015-04-22 Yury Gribov Allow wildcards in -fsanitize-sections. 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. From-SVN: r222322 --- diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 59eb8f8c1ec..27620dd3761 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2015-04-22 Yury Gribov + + * doc/invoke.texi (-fsanitize-sections): Update description. + * asan.c (set_sanitized_sections): Parse incoming arg. + (section_sanitized_p): Support wildcards. + 2015-04-22 Tom de Vries PR tree-optimization/65823 diff --git a/gcc/asan.c b/gcc/asan.c index cd6ccdc9070..479301ad2f0 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 c20dd4d58cb..a939ff74670 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/ChangeLog b/gcc/testsuite/ChangeLog index fd3f7006a89..53667bc3c4a 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2015-04-22 Yury Gribov + + * 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. + 2015-04-22 Hale Wang Terry Guo 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 00000000000..51e2b9966d6 --- /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 00000000000..f602116fe2d --- /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 00000000000..66e5f9ac770 --- /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" } } */ +