]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Darwin: Handle string constants specially when asan is enabled.
authorIain Sandoe <iain@sandoe.co.uk>
Sat, 9 Aug 2025 07:19:08 +0000 (08:19 +0100)
committerIain Sandoe <iain@sandoe.co.uk>
Wed, 13 Aug 2025 17:23:12 +0000 (18:23 +0100)
The Darwin ABI uses a different section for string constants when
address sanitizing is enabled.  This adds defintions of the asan-
specific sections and switches string constants to the correct
section.

It also makes the string constant symbols linker-visible when
asan is enabled, but not otherwise.

gcc/ChangeLog:

* config/darwin-sections.def (asan_string_section,
asan_globals_section, asan_liveness_section): New.
* config/darwin.cc (objc_method_decl): Use asan sections
when asan is enabled.
(darwin_encode_section_info): Alter string constant
linker visibility depending on asan.
(machopic_select_section): Use the asan sections when
asan is enabled.

gcc/testsuite/ChangeLog:

* gcc.dg/torture/darwin-cfstring-3.c: Adjust for amended
string labels.
* g++.dg/torture/darwin-cfstring-3.C: Likewise.

Signed-off-by: Iain Sandoe <iain@sandoe.co.uk>
gcc/config/darwin-sections.def
gcc/config/darwin.cc
gcc/testsuite/g++.dg/torture/darwin-cfstring-3.C
gcc/testsuite/gcc.dg/torture/darwin-cfstring-3.c

index 44adcc6062dba1d0d7b56c6c21d0724b978f1eeb..76587c268cefbe69cfaf37a34c312cda97b2bce6 100644 (file)
@@ -215,3 +215,10 @@ DEF_SECTION (objc2_method_names_section, 0,
 
 DEF_SECTION (objc2_method_types_section, 0,
             ".section __TEXT, __objc_methtype, cstring_literals", 1)
+
+/* ASAN sections.  */
+
+DEF_SECTION (asan_string_section, 0, ".section __TEXT, __asan_cstring", 0)
+DEF_SECTION (asan_globals_section, 0, ".section __DATA, __asan_globals", 0)
+DEF_SECTION (asan_liveness_section, 0,
+            ".section __DATA,__asan_liveness,regular,live_support", 0)
index 1724084b628734b963c7dc8ecb904289c6dd8660..75ac3560954b442a4cfd25635277fc5f256c6407 100644 (file)
@@ -49,6 +49,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "optabs.h"
 #include "flags.h"
 #include "opts.h"
+#include "asan.h"
 
 /* Fix and Continue.
 
@@ -1303,8 +1304,9 @@ darwin_encode_section_info (tree decl, rtx rtl, int first)
       bool is_str = TREE_CODE (decl) == STRING_CST;
       rtx sym_ref = XEXP (rtl, 0);
 
-      /* If this is a string cst or not anchored we have nothing to do.  */
-      if (is_str || !SYMBOL_REF_HAS_BLOCK_INFO_P (sym_ref))
+      /* Unless this is a string cst or we are in an anchored section we have
+        nothing more to do here.  */
+      if (!is_str && !SYMBOL_REF_HAS_BLOCK_INFO_P (sym_ref))
        return;
 
       tree sym_decl = SYMBOL_REF_DECL (sym_ref);
@@ -1312,9 +1314,18 @@ darwin_encode_section_info (tree decl, rtx rtl, int first)
       gcc_checking_assert (strncmp ("*lC", name, 3) == 0);
 
       char *buf;
-      /* Lets identify anchored constants with a different prefix, for the
-         sake of inspection only.  */
-      buf = xasprintf ("*LaC%s", &name[3]);
+      if (is_str)
+       {
+         bool for_asan = (flag_sanitize & SANITIZE_ADDRESS)
+                          && asan_protect_global (CONST_CAST_TREE (decl));
+         /* When we are generating code for sanitized strings, the string
+            internal symbols are made visible in the object.  */
+         buf = xasprintf ("*%c.str.%s", for_asan ? 'l' : 'L', &name[3]);
+       }
+      else
+       /* Lets identify anchored constants with a different prefix, for the
+          sake of inspection only.  */
+       buf = xasprintf ("*LaC%s", &name[3]);
       if (sym_decl)
        DECL_NAME (sym_decl) = get_identifier (buf);
       XSTR (sym_ref, 0) = ggc_strdup (buf);
@@ -1706,6 +1717,17 @@ machopic_select_section (tree decl,
 
   ro = TREE_READONLY (decl) || TREE_CONSTANT (decl) ;
 
+  /* Trump categorize_decl_for_section () for ASAN stuff - the Darwin
+     categorisations are special.  */
+  if (flag_sanitize & SANITIZE_ADDRESS)
+    {
+      if (TREE_CODE (decl) == STRING_CST
+         && asan_protect_global (CONST_CAST_TREE (decl)))
+       {
+         return darwin_sections[asan_string_section];
+       }
+    }
+
   switch (categorize_decl_for_section (decl, reloc))
     {
     case SECCAT_TEXT:
@@ -1722,7 +1744,12 @@ machopic_select_section (tree decl,
       break;
 
     case SECCAT_RODATA_MERGE_STR_INIT:
-      base_section = darwin_mergeable_string_section (DECL_INITIAL (decl), align);
+      if ((flag_sanitize & SANITIZE_ADDRESS)
+          && asan_protect_global (CONST_CAST_TREE (decl)))
+       /* or !flag_merge_constants */
+       return darwin_sections[asan_string_section];
+      else
+       return darwin_mergeable_string_section (DECL_INITIAL (decl), align);
       break;
 
     case SECCAT_RODATA_MERGE_CONST:
index ee4b385b17fad6cf20257df2d6caaed6fc958c7d..4be3a2526c3eed901c6ba69105feb5e7ca4d29ca 100644 (file)
@@ -24,7 +24,7 @@ void foo(void) {
   s0 = s1;
 }
 
-/* { dg-final { scan-assembler "\\.long\[ \\t\]+___CFConstantStringClassReference\n\[ \\t\]*\\.long\[ \\t\]+1992\n\[ \\t\]*\\.long\[ \\t\]+\[lL\]C.*\n\[ \\t\]*\\.long\[ \\t\]+4\n" { target { *-*-darwin* && { ! lp64 } } } } } */
-/* { dg-final { scan-assembler "\\.long\[ \\t\]+___CFConstantStringClassReference\n\[ \\t\]*\\.long\[ \\t\]+1992\n\[ \\t\]*\\.long\[ \\t\]+\[lL\]C.*\n\[ \\t\]*\\.long\[ \\t\]+10\n" { target { *-*-darwin* && { ! lp64 } } } } } */
-/* { dg-final { scan-assembler ".quad\t___CFConstantStringClassReference\n\t.long\t1992\n\t.space 4\n\t.quad\t.*\n\t.quad\t4\n" { target { *-*-darwin* && {  lp64 } } } } } */
-/* { dg-final { scan-assembler ".quad\t___CFConstantStringClassReference\n\t.long\t1992\n\t.space 4\n\t.quad\t.*\n\t.quad\t10\n" { target { *-*-darwin* && {  lp64 } } } } } */
+/* { dg-final { scan-assembler "\\.long\[ \\t\]+___CFConstantStringClassReference\n\[ \\t\]*\\.long\[ \\t\]+1992\n\[ \\t\]*\\.long\[ \\t\]+L\.str\..*\n\[ \\t\]*\\.long\[ \\t\]+4\n" { target { *-*-darwin* && { ! lp64 } } } } } */
+/* { dg-final { scan-assembler "\\.long\[ \\t\]+___CFConstantStringClassReference\n\[ \\t\]*\\.long\[ \\t\]+1992\n\[ \\t\]*\\.long\[ \\t\]+L\.str\..*\n\[ \\t\]*\\.long\[ \\t\]+10\n" { target { *-*-darwin* && { ! lp64 } } } } } */
+/* { dg-final { scan-assembler ".quad\t___CFConstantStringClassReference\n\t.long\t1992\n\t.space 4\n\t.quad\tL\.str\..*\n\t.quad\t4\n" { target { *-*-darwin* && {  lp64 } } } } } */
+/* { dg-final { scan-assembler ".quad\t___CFConstantStringClassReference\n\t.long\t1992\n\t.space 4\n\t.quad\tL\.str\..*\n\t.quad\t10\n" { target { *-*-darwin* && {  lp64 } } } } } */
index ee4b385b17fad6cf20257df2d6caaed6fc958c7d..4be3a2526c3eed901c6ba69105feb5e7ca4d29ca 100644 (file)
@@ -24,7 +24,7 @@ void foo(void) {
   s0 = s1;
 }
 
-/* { dg-final { scan-assembler "\\.long\[ \\t\]+___CFConstantStringClassReference\n\[ \\t\]*\\.long\[ \\t\]+1992\n\[ \\t\]*\\.long\[ \\t\]+\[lL\]C.*\n\[ \\t\]*\\.long\[ \\t\]+4\n" { target { *-*-darwin* && { ! lp64 } } } } } */
-/* { dg-final { scan-assembler "\\.long\[ \\t\]+___CFConstantStringClassReference\n\[ \\t\]*\\.long\[ \\t\]+1992\n\[ \\t\]*\\.long\[ \\t\]+\[lL\]C.*\n\[ \\t\]*\\.long\[ \\t\]+10\n" { target { *-*-darwin* && { ! lp64 } } } } } */
-/* { dg-final { scan-assembler ".quad\t___CFConstantStringClassReference\n\t.long\t1992\n\t.space 4\n\t.quad\t.*\n\t.quad\t4\n" { target { *-*-darwin* && {  lp64 } } } } } */
-/* { dg-final { scan-assembler ".quad\t___CFConstantStringClassReference\n\t.long\t1992\n\t.space 4\n\t.quad\t.*\n\t.quad\t10\n" { target { *-*-darwin* && {  lp64 } } } } } */
+/* { dg-final { scan-assembler "\\.long\[ \\t\]+___CFConstantStringClassReference\n\[ \\t\]*\\.long\[ \\t\]+1992\n\[ \\t\]*\\.long\[ \\t\]+L\.str\..*\n\[ \\t\]*\\.long\[ \\t\]+4\n" { target { *-*-darwin* && { ! lp64 } } } } } */
+/* { dg-final { scan-assembler "\\.long\[ \\t\]+___CFConstantStringClassReference\n\[ \\t\]*\\.long\[ \\t\]+1992\n\[ \\t\]*\\.long\[ \\t\]+L\.str\..*\n\[ \\t\]*\\.long\[ \\t\]+10\n" { target { *-*-darwin* && { ! lp64 } } } } } */
+/* { dg-final { scan-assembler ".quad\t___CFConstantStringClassReference\n\t.long\t1992\n\t.space 4\n\t.quad\tL\.str\..*\n\t.quad\t4\n" { target { *-*-darwin* && {  lp64 } } } } } */
+/* { dg-final { scan-assembler ".quad\t___CFConstantStringClassReference\n\t.long\t1992\n\t.space 4\n\t.quad\tL\.str\..*\n\t.quad\t10\n" { target { *-*-darwin* && {  lp64 } } } } } */