[PATCH] D25026: [asan] Move instrumented null-terminated strings to a special section

Kuba Brecka via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 11 07:35:51 PDT 2016


kubabrecka updated this revision to Diff 74253.
kubabrecka added a comment.
Herald added a subscriber: mehdi_amini.

Updating patch.  Lowering the number of nested ifs.  Adding a testcase for compiler-rt.  Updating the odr-lto.cc testcase.


https://reviews.llvm.org/D25026

Files:
  lib/Transforms/Instrumentation/AddressSanitizer.cpp
  projects/compiler-rt/test/asan/TestCases/Darwin/cstring_section.c
  projects/compiler-rt/test/asan/TestCases/Darwin/odr-lto.cc
  test/Instrumentation/AddressSanitizer/global_cstring_darwin.ll


Index: projects/compiler-rt/test/asan/TestCases/Darwin/odr-lto.cc
===================================================================
--- projects/compiler-rt/test/asan/TestCases/Darwin/odr-lto.cc
+++ projects/compiler-rt/test/asan/TestCases/Darwin/odr-lto.cc
@@ -3,15 +3,10 @@
 
 // REQUIRES: lto
 
-// RUN: %clangxx_asan -DPART=0 -c %s -o %t-1.o -flto
-// RUN: %clangxx_asan -DPART=1 -c %s -o %t-2.o -flto
-// RUN: %clangxx_asan %t-1.o %t-2.o -o %t -flto
-// RUN: not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-ODR
-
 // RUN: %clangxx_asan -DPART=0 -c %s -o %t-1.o -flto -mllvm -asan-use-private-alias
 // RUN: %clangxx_asan -DPART=1 -c %s -o %t-2.o -flto -mllvm -asan-use-private-alias
 // RUN: %clangxx_asan %t-1.o %t-2.o -o %t -flto
-// RUN: %env_asan_opts=use_odr_indicator=1 %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-NO-ODR
+// RUN: %env_asan_opts=use_odr_indicator=1 %run %t 2>&1 | FileCheck %s
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -36,6 +31,5 @@
 
 #endif // PART == 1
 
-// CHECK-ODR: ERROR: AddressSanitizer: odr-violation
-// CHECK-NO-ODR-NOT: ERROR: AddressSanitizer: odr-violation
-// CHECK-NO-ODR: Done.
+// CHECK-NOT: ERROR: AddressSanitizer: odr-violation
+// CHECK: Done.
Index: projects/compiler-rt/test/asan/TestCases/Darwin/cstring_section.c
===================================================================
--- projects/compiler-rt/test/asan/TestCases/Darwin/cstring_section.c
+++ projects/compiler-rt/test/asan/TestCases/Darwin/cstring_section.c
@@ -0,0 +1,17 @@
+// Test that AddressSanitizer moves constant strings into a separate section.
+
+// RUN: %clang_asan -c -o %t %s
+// RUN: llvm-objdump -s %t | FileCheck %s
+
+// Check that "Hello.\n" is in __asan_cstring and not in __cstring.
+// CHECK: Contents of section __asan_cstring:
+// CHECK: 48656c6c {{.*}} Hello.
+// CHECK: Contents of section __const:
+// CHECK-NOT: 48656c6c {{.*}} Hello.
+// CHECK: Contents of section __cstring:
+// CHECK-NOT: 48656c6c {{.*}} Hello.
+
+int main(int argc, char *argv[]) {
+  argv[0] = "Hello.\n";
+  return 0;
+}
Index: test/Instrumentation/AddressSanitizer/global_cstring_darwin.ll
===================================================================
--- test/Instrumentation/AddressSanitizer/global_cstring_darwin.ll
+++ test/Instrumentation/AddressSanitizer/global_cstring_darwin.ll
@@ -0,0 +1,21 @@
+; This test checks that instrumented global C (null terminated) strings are put into a special section on Darwin.
+; RUN: opt < %s -asan -asan-module -S | FileCheck %s
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-apple-macosx10.10.0"
+
+; Should be put into __asan_cstring section:
+ at .str.1 = private unnamed_addr constant [13 x i8] c"Hello world.\00", align 1
+ at .str.2 = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1
+
+; CHECK: @.str.1 = internal unnamed_addr constant { [13 x i8], [51 x i8] } { [13 x i8] c"Hello world.\00", [51 x i8] zeroinitializer }, section "__TEXT,__asan_cstring,regular", align 32
+; CHECK: @.str.2 = internal unnamed_addr constant { [4 x i8], [60 x i8] } { [4 x i8] c"%s\0A\00", [60 x i8] zeroinitializer }, section "__TEXT,__asan_cstring,regular", align 32
+
+; Shouldn't be put into special section:
+ at .str.3 = private unnamed_addr constant [4 x i8] c"\00\01\02\03", align 1
+ at .str.4 = private unnamed_addr global [7 x i8] c"Hello.\00", align 1
+ at .str.5 = private unnamed_addr constant [8 x i8] c"Hello.\00\00", align 1
+
+; CHECK: @.str.3 = internal unnamed_addr constant { [4 x i8], [60 x i8] } { [4 x i8] c"\00\01\02\03", [60 x i8] zeroinitializer }, align 32
+; CHECK: @.str.4 = private unnamed_addr global { [7 x i8], [57 x i8] } { [7 x i8] c"Hello.\00", [57 x i8] zeroinitializer }, align 32
+; CHECK: @.str.5 = internal unnamed_addr constant { [8 x i8], [56 x i8] } { [8 x i8] c"Hello.\00\00", [56 x i8] zeroinitializer }, align 32
Index: lib/Transforms/Instrumentation/AddressSanitizer.cpp
===================================================================
--- lib/Transforms/Instrumentation/AddressSanitizer.cpp
+++ lib/Transforms/Instrumentation/AddressSanitizer.cpp
@@ -1539,6 +1539,14 @@
     NewGlobal->copyAttributesFrom(G);
     NewGlobal->setAlignment(MinRZ);
 
+    // Move null-terminated C strings to "__asan_cstring" section on Darwin.
+    if (TargetTriple.isOSBinFormatMachO() && !G->hasSection() &&
+        G->isConstant()) {
+      auto Seq = dyn_cast<ConstantDataSequential>(G->getInitializer());
+      if (Seq && Seq->isCString())
+        NewGlobal->setSection("__TEXT,__asan_cstring,regular");
+    }
+
     // Transfer the debug info.  The payload starts at offset zero so we can
     // copy the debug info over as is.
     SmallVector<DIGlobalVariable *, 1> GVs;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D25026.74253.patch
Type: text/x-patch
Size: 4729 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161011/55ec2261/attachment.bin>


More information about the llvm-commits mailing list