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

Kuba Brecka via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 28 08:11:39 PDT 2016


kubabrecka created this revision.
kubabrecka added reviewers: dvyukov, kcc, filcab, zaks.anna, eugenis, m.ostapenko.
kubabrecka added a subscriber: llvm-commits.
kubabrecka added a project: Sanitizers.

On Darwin, simple C null-terminated constant strings normally end up in the `__TEXT,__cstring` section of the resulting Mach-O binary.  When instrumented with ASan, these strings are transformed in a way that they cannot be in `__cstring` (the linker unifies the content of this section and strips extra NUL bytes, which would break instrumentation), and are put into a generic `__const` section.  This breaks some of the tools that we have:  Some tools need to scan all C null-terminated strings in Mach-O binaries, and scanning all the contents of `__const` has a large performance penalty.  This patch instead introduces a special section, `__asan_cstring` which will now hold the instrumented null-terminated strings.

https://reviews.llvm.org/D25026

Files:
  lib/Transforms/Instrumentation/AddressSanitizer.cpp
  test/Instrumentation/AddressSanitizer/global_cstring_darwin.ll

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
@@ -1508,6 +1508,13 @@
     NewGlobal->copyAttributesFrom(G);
     NewGlobal->setAlignment(MinRZ);
 
+    // Move null-terminated C strings to "__asan_cstring" section on Darwin.
+    if (TargetTriple.isOSBinFormatMachO())
+      if (!G->hasSection() && G->isConstant())
+        if (auto Seq = dyn_cast<ConstantDataSequential>(G->getInitializer()))
+          if (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.72829.patch
Type: text/x-patch
Size: 2671 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160928/e1dcef22/attachment.bin>


More information about the llvm-commits mailing list