Hi!

The c-c++-common/asan/global-overflow-1.c test fails because
we don't instrument local common vars (those that go into .lcomm).
Of course we can't instrument them if they are emitted using .lcomm,
because the linker can arbitrarily reorder the .lcomm vars for best packing,
but we can instrument them by forcing them into .bss instead.
Public common variables obviously still can't be instrumented, because
they have to be emitted as .comm, users can rely on common var merging,
provide non-zero definition somewhere else, etc.

The following patch implements that, ok for trunk?

2012-12-05  Jakub Jelinek  <ja...@redhat.com>

        * varasm.c (get_variable_section): Don't return lcomm_section
        for asan_protect_global decls.
        * asan.c (asan_protect_global): Only avoid public common variables.
        Don't call get_variable_section here.

--- gcc/varasm.c.jj     2012-12-03 12:41:32.000000000 +0100
+++ gcc/varasm.c        2012-12-05 08:24:59.728816975 +0100
@@ -1034,7 +1034,8 @@ get_variable_section (tree decl, bool pr
       && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
       && bss_initializer_p (decl))
     {
-      if (!TREE_PUBLIC (decl))
+      if (!TREE_PUBLIC (decl)
+         && !(flag_asan && asan_protect_global (decl)))
        return lcomm_section;
       if (bss_noswitch_section)
        return bss_noswitch_section;
--- gcc/asan.c.jj       2012-12-04 14:19:36.000000000 +0100
+++ gcc/asan.c  2012-12-05 08:25:50.417507197 +0100
@@ -428,7 +428,6 @@ bool
 asan_protect_global (tree decl)
 {
   rtx rtl, symbol;
-  section *sect;
 
   if (TREE_CODE (decl) != VAR_DECL
       /* TLS vars aren't statically protectable.  */
@@ -442,7 +441,7 @@ asan_protect_global (tree decl)
         padding or not.  */
       || DECL_ONE_ONLY (decl)
       /* Similarly for common vars.  People can use -fno-common.  */
-      || DECL_COMMON (decl)
+      || (DECL_COMMON (decl) && TREE_PUBLIC (decl))
       /* Don't protect if using user section, often vars placed
         into user section from multiple TUs are then assumed
         to be an array of such vars, putting padding in there
@@ -464,10 +463,6 @@ asan_protect_global (tree decl)
       || TREE_CONSTANT_POOL_ADDRESS_P (symbol))
     return false;
 
-  sect = get_variable_section (decl, false);
-  if (sect->common.flags & SECTION_COMMON)
-    return false;
-
   if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
     return false;
 

        Jakub

Reply via email to