mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-01-19 20:34:38 +00:00
ASan: add optional 'zero-based shadow' option to ASan passes. Always tell the values of shadow scale and offset to the runtime
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@172709 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
6c327f92a5
commit
11af9a873f
@ -39,9 +39,11 @@ ModulePass *createGCOVProfilerPass(bool EmitNotes = true, bool EmitData = true,
|
||||
// Insert AddressSanitizer (address sanity checking) instrumentation
|
||||
FunctionPass *createAddressSanitizerFunctionPass(
|
||||
bool CheckInitOrder = false, bool CheckUseAfterReturn = false,
|
||||
bool CheckLifetime = false, StringRef BlacklistFile = StringRef());
|
||||
bool CheckLifetime = false, StringRef BlacklistFile = StringRef(),
|
||||
bool ZeroBaseShadow = false);
|
||||
ModulePass *createAddressSanitizerModulePass(
|
||||
bool CheckInitOrder = false, StringRef BlacklistFile = StringRef());
|
||||
bool CheckInitOrder = false, StringRef BlacklistFile = StringRef(),
|
||||
bool ZeroBaseShadow = false);
|
||||
|
||||
// Insert MemorySanitizer instrumentation (detection of uninitialized reads)
|
||||
FunctionPass *createMemorySanitizerPass(bool TrackOrigins = false,
|
||||
|
@ -53,7 +53,6 @@ using namespace llvm;
|
||||
static const uint64_t kDefaultShadowScale = 3;
|
||||
static const uint64_t kDefaultShadowOffset32 = 1ULL << 29;
|
||||
static const uint64_t kDefaultShadowOffset64 = 1ULL << 44;
|
||||
static const uint64_t kDefaultShadowOffsetPie = 0;
|
||||
|
||||
static const size_t kMaxStackMallocSize = 1 << 16; // 64K
|
||||
static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3;
|
||||
@ -193,13 +192,14 @@ struct ShadowMapping {
|
||||
uint64_t Offset;
|
||||
};
|
||||
|
||||
static ShadowMapping getShadowMapping(const Module &M, int LongSize) {
|
||||
llvm::Triple targetTriple(M.getTargetTriple());
|
||||
bool isAndroid = targetTriple.getEnvironment() == llvm::Triple::Android;
|
||||
static ShadowMapping getShadowMapping(const Module &M, int LongSize,
|
||||
bool ZeroBaseShadow) {
|
||||
llvm::Triple TargetTriple(M.getTargetTriple());
|
||||
bool IsAndroid = TargetTriple.getEnvironment() == llvm::Triple::Android;
|
||||
|
||||
ShadowMapping Mapping;
|
||||
|
||||
Mapping.Offset = isAndroid ? kDefaultShadowOffsetPie :
|
||||
Mapping.Offset = (IsAndroid || ZeroBaseShadow) ? 0 :
|
||||
(LongSize == 32 ? kDefaultShadowOffset32 : kDefaultShadowOffset64);
|
||||
if (ClMappingOffsetLog >= 0) {
|
||||
// Zero offset log is the special case.
|
||||
@ -225,13 +225,15 @@ struct AddressSanitizer : public FunctionPass {
|
||||
AddressSanitizer(bool CheckInitOrder = false,
|
||||
bool CheckUseAfterReturn = false,
|
||||
bool CheckLifetime = false,
|
||||
StringRef BlacklistFile = StringRef())
|
||||
StringRef BlacklistFile = StringRef(),
|
||||
bool ZeroBaseShadow = false)
|
||||
: FunctionPass(ID),
|
||||
CheckInitOrder(CheckInitOrder || ClInitializers),
|
||||
CheckUseAfterReturn(CheckUseAfterReturn || ClUseAfterReturn),
|
||||
CheckLifetime(CheckLifetime || ClCheckLifetime),
|
||||
BlacklistFile(BlacklistFile.empty() ? ClBlacklistFile
|
||||
: BlacklistFile) {}
|
||||
: BlacklistFile),
|
||||
ZeroBaseShadow(ZeroBaseShadow) {}
|
||||
virtual const char *getPassName() const {
|
||||
return "AddressSanitizerFunctionPass";
|
||||
}
|
||||
@ -265,6 +267,9 @@ struct AddressSanitizer : public FunctionPass {
|
||||
bool CheckInitOrder;
|
||||
bool CheckUseAfterReturn;
|
||||
bool CheckLifetime;
|
||||
SmallString<64> BlacklistFile;
|
||||
bool ZeroBaseShadow;
|
||||
|
||||
LLVMContext *C;
|
||||
DataLayout *TD;
|
||||
int LongSize;
|
||||
@ -273,7 +278,6 @@ struct AddressSanitizer : public FunctionPass {
|
||||
Function *AsanCtorFunction;
|
||||
Function *AsanInitFunction;
|
||||
Function *AsanHandleNoReturnFunc;
|
||||
SmallString<64> BlacklistFile;
|
||||
OwningPtr<BlackList> BL;
|
||||
// This array is indexed by AccessIsWrite and log2(AccessSize).
|
||||
Function *AsanErrorCallback[2][kNumberOfAccessSizes];
|
||||
@ -286,11 +290,13 @@ struct AddressSanitizer : public FunctionPass {
|
||||
class AddressSanitizerModule : public ModulePass {
|
||||
public:
|
||||
AddressSanitizerModule(bool CheckInitOrder = false,
|
||||
StringRef BlacklistFile = StringRef())
|
||||
StringRef BlacklistFile = StringRef(),
|
||||
bool ZeroBaseShadow = false)
|
||||
: ModulePass(ID),
|
||||
CheckInitOrder(CheckInitOrder || ClInitializers),
|
||||
BlacklistFile(BlacklistFile.empty() ? ClBlacklistFile
|
||||
: BlacklistFile) {}
|
||||
: BlacklistFile),
|
||||
ZeroBaseShadow(ZeroBaseShadow) {}
|
||||
bool runOnModule(Module &M);
|
||||
static char ID; // Pass identification, replacement for typeid
|
||||
virtual const char *getPassName() const {
|
||||
@ -309,6 +315,8 @@ class AddressSanitizerModule : public ModulePass {
|
||||
|
||||
bool CheckInitOrder;
|
||||
SmallString<64> BlacklistFile;
|
||||
bool ZeroBaseShadow;
|
||||
|
||||
OwningPtr<BlackList> BL;
|
||||
SetOfDynamicallyInitializedGlobals DynamicallyInitializedGlobals;
|
||||
Type *IntptrTy;
|
||||
@ -473,9 +481,9 @@ INITIALIZE_PASS(AddressSanitizer, "asan",
|
||||
false, false)
|
||||
FunctionPass *llvm::createAddressSanitizerFunctionPass(
|
||||
bool CheckInitOrder, bool CheckUseAfterReturn, bool CheckLifetime,
|
||||
StringRef BlacklistFile) {
|
||||
StringRef BlacklistFile, bool ZeroBaseShadow) {
|
||||
return new AddressSanitizer(CheckInitOrder, CheckUseAfterReturn,
|
||||
CheckLifetime, BlacklistFile);
|
||||
CheckLifetime, BlacklistFile, ZeroBaseShadow);
|
||||
}
|
||||
|
||||
char AddressSanitizerModule::ID = 0;
|
||||
@ -483,8 +491,9 @@ INITIALIZE_PASS(AddressSanitizerModule, "asan-module",
|
||||
"AddressSanitizer: detects use-after-free and out-of-bounds bugs."
|
||||
"ModulePass", false, false)
|
||||
ModulePass *llvm::createAddressSanitizerModulePass(
|
||||
bool CheckInitOrder, StringRef BlacklistFile) {
|
||||
return new AddressSanitizerModule(CheckInitOrder, BlacklistFile);
|
||||
bool CheckInitOrder, StringRef BlacklistFile, bool ZeroBaseShadow) {
|
||||
return new AddressSanitizerModule(CheckInitOrder, BlacklistFile,
|
||||
ZeroBaseShadow);
|
||||
}
|
||||
|
||||
static size_t TypeSizeToSizeIndex(uint32_t TypeSize) {
|
||||
@ -818,7 +827,7 @@ bool AddressSanitizerModule::runOnModule(Module &M) {
|
||||
C = &(M.getContext());
|
||||
int LongSize = TD->getPointerSizeInBits();
|
||||
IntptrTy = Type::getIntNTy(*C, LongSize);
|
||||
Mapping = getShadowMapping(M, LongSize);
|
||||
Mapping = getShadowMapping(M, LongSize, ZeroBaseShadow);
|
||||
initializeCallbacks(M);
|
||||
DynamicallyInitializedGlobals.Init(M);
|
||||
|
||||
@ -967,25 +976,20 @@ void AddressSanitizer::initializeCallbacks(Module &M) {
|
||||
}
|
||||
|
||||
void AddressSanitizer::emitShadowMapping(Module &M, IRBuilder<> &IRB) const {
|
||||
// Tell the values of mapping offset and scale to the run-time if they are
|
||||
// specified by command-line flags.
|
||||
if (ClMappingOffsetLog >= 0) {
|
||||
GlobalValue *asan_mapping_offset =
|
||||
new GlobalVariable(M, IntptrTy, true, GlobalValue::LinkOnceODRLinkage,
|
||||
ConstantInt::get(IntptrTy, Mapping.Offset),
|
||||
kAsanMappingOffsetName);
|
||||
// Read the global, otherwise it may be optimized away.
|
||||
IRB.CreateLoad(asan_mapping_offset, true);
|
||||
}
|
||||
// Tell the values of mapping offset and scale to the run-time.
|
||||
GlobalValue *asan_mapping_offset =
|
||||
new GlobalVariable(M, IntptrTy, true, GlobalValue::LinkOnceODRLinkage,
|
||||
ConstantInt::get(IntptrTy, Mapping.Offset),
|
||||
kAsanMappingOffsetName);
|
||||
// Read the global, otherwise it may be optimized away.
|
||||
IRB.CreateLoad(asan_mapping_offset, true);
|
||||
|
||||
if (ClMappingScale) {
|
||||
GlobalValue *asan_mapping_scale =
|
||||
new GlobalVariable(M, IntptrTy, true, GlobalValue::LinkOnceODRLinkage,
|
||||
ConstantInt::get(IntptrTy, Mapping.Scale),
|
||||
kAsanMappingScaleName);
|
||||
// Read the global, otherwise it may be optimized away.
|
||||
IRB.CreateLoad(asan_mapping_scale, true);
|
||||
}
|
||||
GlobalValue *asan_mapping_scale =
|
||||
new GlobalVariable(M, IntptrTy, true, GlobalValue::LinkOnceODRLinkage,
|
||||
ConstantInt::get(IntptrTy, Mapping.Scale),
|
||||
kAsanMappingScaleName);
|
||||
// Read the global, otherwise it may be optimized away.
|
||||
IRB.CreateLoad(asan_mapping_scale, true);
|
||||
}
|
||||
|
||||
// virtual
|
||||
@ -1013,7 +1017,7 @@ bool AddressSanitizer::doInitialization(Module &M) {
|
||||
AsanInitFunction->setLinkage(Function::ExternalLinkage);
|
||||
IRB.CreateCall(AsanInitFunction);
|
||||
|
||||
Mapping = getShadowMapping(M, LongSize);
|
||||
Mapping = getShadowMapping(M, LongSize, ZeroBaseShadow);
|
||||
emitShadowMapping(M, IRB);
|
||||
|
||||
appendToGlobalCtors(M, AsanCtorFunction, kAsanCtorAndCtorPriority);
|
||||
|
Loading…
x
Reference in New Issue
Block a user