Index: llvm/trunk/lib/Transforms/Instrumentation/AddressSanitizer.cpp =================================================================== --- llvm/trunk/lib/Transforms/Instrumentation/AddressSanitizer.cpp +++ llvm/trunk/lib/Transforms/Instrumentation/AddressSanitizer.cpp @@ -1535,6 +1535,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(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 GVs; Index: llvm/trunk/test/Instrumentation/AddressSanitizer/global_cstring_darwin.ll =================================================================== --- llvm/trunk/test/Instrumentation/AddressSanitizer/global_cstring_darwin.ll +++ llvm/trunk/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: +@.str.1 = private unnamed_addr constant [13 x i8] c"Hello world.\00", align 1 +@.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: +@.str.3 = private unnamed_addr constant [4 x i8] c"\00\01\02\03", align 1 +@.str.4 = private unnamed_addr global [7 x i8] c"Hello.\00", align 1 +@.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