Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 07eb96d

Browse files
committed
[asan] Move instrumented null-terminated strings to a special section, LLVM part
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. Differential Revision: https://reviews.llvm.org/D25026 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@285619 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 383790d commit 07eb96d

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

lib/Transforms/Instrumentation/AddressSanitizer.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,6 +1509,14 @@ bool AddressSanitizerModule::InstrumentGlobals(IRBuilder<> &IRB, Module &M) {
15091509
NewGlobal->copyAttributesFrom(G);
15101510
NewGlobal->setAlignment(MinRZ);
15111511

1512+
// Move null-terminated C strings to "__asan_cstring" section on Darwin.
1513+
if (TargetTriple.isOSBinFormatMachO() && !G->hasSection() &&
1514+
G->isConstant()) {
1515+
auto Seq = dyn_cast<ConstantDataSequential>(G->getInitializer());
1516+
if (Seq && Seq->isCString())
1517+
NewGlobal->setSection("__TEXT,__asan_cstring,regular");
1518+
}
1519+
15121520
// Transfer the debug info. The payload starts at offset zero so we can
15131521
// copy the debug info over as is.
15141522
SmallVector<DIGlobalVariable *, 1> GVs;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
; This test checks that instrumented global C (null terminated) strings are put into a special section on Darwin.
2+
; RUN: opt < %s -asan -asan-module -S | FileCheck %s
3+
4+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
5+
target triple = "x86_64-apple-macosx10.10.0"
6+
7+
; Should be put into __asan_cstring section:
8+
@.str.1 = private unnamed_addr constant [13 x i8] c"Hello world.\00", align 1
9+
@.str.2 = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1
10+
11+
; 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
12+
; 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
13+
14+
; Shouldn't be put into special section:
15+
@.str.3 = private unnamed_addr constant [4 x i8] c"\00\01\02\03", align 1
16+
@.str.4 = private unnamed_addr global [7 x i8] c"Hello.\00", align 1
17+
@.str.5 = private unnamed_addr constant [8 x i8] c"Hello.\00\00", align 1
18+
19+
; 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
20+
; 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
21+
; 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

0 commit comments

Comments
 (0)