Recommit of r223513 and r223514.

Reviewed at http://reviews.llvm.org/D6488



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223532 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Kuba Brecka 2014-12-05 22:19:18 +00:00
parent d2f12fd27a
commit 0a12d8211e
2 changed files with 49 additions and 34 deletions

View File

@ -37,6 +37,7 @@
#include "llvm/IR/MDBuilder.h" #include "llvm/IR/MDBuilder.h"
#include "llvm/IR/Module.h" #include "llvm/IR/Module.h"
#include "llvm/IR/Type.h" #include "llvm/IR/Type.h"
#include "llvm/MC/MCSectionMachO.h"
#include "llvm/Support/CommandLine.h" #include "llvm/Support/CommandLine.h"
#include "llvm/Support/DataTypes.h" #include "llvm/Support/DataTypes.h"
#include "llvm/Support/Debug.h" #include "llvm/Support/Debug.h"
@ -288,8 +289,7 @@ struct ShadowMapping {
bool OrShadowOffset; bool OrShadowOffset;
}; };
static ShadowMapping getShadowMapping(const Module &M, int LongSize) { static ShadowMapping getShadowMapping(Triple &TargetTriple, int LongSize) {
llvm::Triple TargetTriple(M.getTargetTriple());
bool IsAndroid = TargetTriple.getEnvironment() == llvm::Triple::Android; bool IsAndroid = TargetTriple.getEnvironment() == llvm::Triple::Android;
bool IsIOS = TargetTriple.isiOS(); bool IsIOS = TargetTriple.isiOS();
bool IsFreeBSD = TargetTriple.isOSFreeBSD(); bool IsFreeBSD = TargetTriple.isOSFreeBSD();
@ -385,6 +385,7 @@ struct AddressSanitizer : public FunctionPass {
LLVMContext *C; LLVMContext *C;
const DataLayout *DL; const DataLayout *DL;
Triple TargetTriple;
int LongSize; int LongSize;
Type *IntptrTy; Type *IntptrTy;
ShadowMapping Mapping; ShadowMapping Mapping;
@ -430,6 +431,7 @@ class AddressSanitizerModule : public ModulePass {
Type *IntptrTy; Type *IntptrTy;
LLVMContext *C; LLVMContext *C;
const DataLayout *DL; const DataLayout *DL;
Triple TargetTriple;
ShadowMapping Mapping; ShadowMapping Mapping;
Function *AsanPoisonGlobals; Function *AsanPoisonGlobals;
Function *AsanUnpoisonGlobals; Function *AsanUnpoisonGlobals;
@ -1042,11 +1044,25 @@ bool AddressSanitizerModule::ShouldInstrumentGlobal(GlobalVariable *G) {
if (G->hasSection()) { if (G->hasSection()) {
StringRef Section(G->getSection()); StringRef Section(G->getSection());
if (TargetTriple.isOSBinFormatMachO()) {
StringRef ParsedSegment, ParsedSection;
unsigned TAA = 0, StubSize = 0;
bool TAAParsed;
std::string ErrorCode =
MCSectionMachO::ParseSectionSpecifier(Section, ParsedSegment,
ParsedSection, TAA, TAAParsed,
StubSize);
if (!ErrorCode.empty()) {
report_fatal_error("Invalid section specifier '" + ParsedSection +
"': " + ErrorCode + ".");
}
// Ignore the globals from the __OBJC section. The ObjC runtime assumes // Ignore the globals from the __OBJC section. The ObjC runtime assumes
// those conform to /usr/lib/objc/runtime.h, so we can't add redzones to // those conform to /usr/lib/objc/runtime.h, so we can't add redzones to
// them. // them.
if (Section.startswith("__OBJC,") || if (ParsedSegment == "__OBJC" ||
Section.startswith("__DATA, __objc_")) { (ParsedSegment == "__DATA" && ParsedSection.startswith("__objc_"))) {
DEBUG(dbgs() << "Ignoring ObjC runtime global: " << *G << "\n"); DEBUG(dbgs() << "Ignoring ObjC runtime global: " << *G << "\n");
return false; return false;
} }
@ -1058,22 +1074,18 @@ bool AddressSanitizerModule::ShouldInstrumentGlobal(GlobalVariable *G) {
// is placed into __DATA,__cfstring // is placed into __DATA,__cfstring
// Therefore there's no point in placing redzones into __DATA,__cfstring. // Therefore there's no point in placing redzones into __DATA,__cfstring.
// Moreover, it causes the linker to crash on OS X 10.7 // Moreover, it causes the linker to crash on OS X 10.7
if (Section.startswith("__DATA,__cfstring")) { if (ParsedSegment == "__DATA" && ParsedSection == "__cfstring") {
DEBUG(dbgs() << "Ignoring CFString: " << *G << "\n"); DEBUG(dbgs() << "Ignoring CFString: " << *G << "\n");
return false; return false;
} }
// The linker merges the contents of cstring_literals and removes the // The linker merges the contents of cstring_literals and removes the
// trailing zeroes. // trailing zeroes.
if (Section.startswith("__TEXT,__cstring,cstring_literals")) { if (ParsedSegment == "__TEXT" && (TAA & MachO::S_CSTRING_LITERALS)) {
DEBUG(dbgs() << "Ignoring a cstring literal: " << *G << "\n"); DEBUG(dbgs() << "Ignoring a cstring literal: " << *G << "\n");
return false; return false;
} }
if (Section.startswith("__TEXT,__objc_methname,cstring_literals")) {
DEBUG(dbgs() << "Ignoring objc_methname cstring global: " << *G << "\n");
return false;
} }
// Callbacks put into the CRT initializer/terminator sections // Callbacks put into the CRT initializer/terminator sections
// should not be instrumented. // should not be instrumented.
// See https://code.google.com/p/address-sanitizer/issues/detail?id=305 // See https://code.google.com/p/address-sanitizer/issues/detail?id=305
@ -1256,7 +1268,8 @@ bool AddressSanitizerModule::runOnModule(Module &M) {
C = &(M.getContext()); C = &(M.getContext());
int LongSize = DL->getPointerSizeInBits(); int LongSize = DL->getPointerSizeInBits();
IntptrTy = Type::getIntNTy(*C, LongSize); IntptrTy = Type::getIntNTy(*C, LongSize);
Mapping = getShadowMapping(M, LongSize); TargetTriple = Triple(M.getTargetTriple());
Mapping = getShadowMapping(TargetTriple, LongSize);
initializeCallbacks(M); initializeCallbacks(M);
bool Changed = false; bool Changed = false;
@ -1338,6 +1351,7 @@ bool AddressSanitizer::doInitialization(Module &M) {
C = &(M.getContext()); C = &(M.getContext());
LongSize = DL->getPointerSizeInBits(); LongSize = DL->getPointerSizeInBits();
IntptrTy = Type::getIntNTy(*C, LongSize); IntptrTy = Type::getIntNTy(*C, LongSize);
TargetTriple = Triple(M.getTargetTriple());
AsanCtorFunction = Function::Create( AsanCtorFunction = Function::Create(
FunctionType::get(Type::getVoidTy(*C), false), FunctionType::get(Type::getVoidTy(*C), false),
@ -1350,7 +1364,7 @@ bool AddressSanitizer::doInitialization(Module &M) {
AsanInitFunction->setLinkage(Function::ExternalLinkage); AsanInitFunction->setLinkage(Function::ExternalLinkage);
IRB.CreateCall(AsanInitFunction); IRB.CreateCall(AsanInitFunction);
Mapping = getShadowMapping(M, LongSize); Mapping = getShadowMapping(TargetTriple, LongSize);
appendToGlobalCtors(M, AsanCtorFunction, kAsanCtorAndDtorPriority); appendToGlobalCtors(M, AsanCtorFunction, kAsanCtorAndDtorPriority);
return true; return true;

View File

@ -1,6 +1,7 @@
; RUN: opt < %s -asan -asan-module -S | FileCheck %s ; RUN: opt < %s -asan -asan-module -S | FileCheck %s
target datalayout = "e" target datalayout = "e"
target triple = "x86_64-apple-darwin10.0.0"
@foo = private global [19 x i8] c"scannerWithString:\00", section "__TEXT,__objc_methname,cstring_literals" @foo = private global [19 x i8] c"scannerWithString:\00", section "__TEXT,__objc_methname,cstring_literals"