diff options
author | Vitaly Buka <vitalybuka@google.com> | 2024-03-26 23:27:17 -0700 |
---|---|---|
committer | Vitaly Buka <vitalybuka@google.com> | 2024-03-26 23:27:17 -0700 |
commit | bd8da30930a14372511c84dcacff865dc0920763 (patch) | |
tree | 2234367b1d3ac674e06ba8323139272f17352837 | |
parent | 16993c793a7d81771ea17a2991f76e87b4b0a6c0 (diff) | |
parent | e603a9fbecfd48cd09111da75126b1a2ff49ef09 (diff) | |
download | llvm-bd8da30930a14372511c84dcacff865dc0920763.zip llvm-bd8da30930a14372511c84dcacff865dc0920763.tar.gz llvm-bd8da30930a14372511c84dcacff865dc0920763.tar.bz2 |
[𝘀𝗽𝗿] initial version
Created using spr 1.3.4
-rw-r--r-- | llvm/include/llvm/IR/GlobalValue.h | 1 | ||||
-rw-r--r-- | llvm/include/llvm/IR/IRBuilder.h | 6 | ||||
-rw-r--r-- | llvm/lib/IR/Globals.cpp | 7 | ||||
-rw-r--r-- | llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp | 15 | ||||
-rw-r--r-- | llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp | 45 | ||||
-rw-r--r-- | llvm/test/Instrumentation/HWAddressSanitizer/globals-access.ll | 44 | ||||
-rw-r--r-- | llvm/test/Instrumentation/HWAddressSanitizer/use-after-scope-setjmp.ll | 1 |
7 files changed, 103 insertions, 16 deletions
diff --git a/llvm/include/llvm/IR/GlobalValue.h b/llvm/include/llvm/IR/GlobalValue.h index aa8188c..c61d502 100644 --- a/llvm/include/llvm/IR/GlobalValue.h +++ b/llvm/include/llvm/IR/GlobalValue.h @@ -360,6 +360,7 @@ public: // storage is shared between `G1` and `G2`. void setSanitizerMetadata(SanitizerMetadata Meta); void removeSanitizerMetadata(); + void setNoSanitizeMetadata(); bool isTagged() const { return hasSanitizerMetadata() && getSanitizerMetadata().Memtag; diff --git a/llvm/include/llvm/IR/IRBuilder.h b/llvm/include/llvm/IR/IRBuilder.h index a6165ef..2a0c1e9 100644 --- a/llvm/include/llvm/IR/IRBuilder.h +++ b/llvm/include/llvm/IR/IRBuilder.h @@ -221,6 +221,12 @@ public: AddOrRemoveMetadataToCopy(LLVMContext::MD_dbg, L.getAsMDNode()); } + /// Set nosanitize metadata. + void SetNoSanitizeMetadata() { + AddOrRemoveMetadataToCopy(llvm::LLVMContext::MD_nosanitize, + llvm::MDNode::get(getContext(), std::nullopt)); + } + /// Collect metadata with IDs \p MetadataKinds from \p Src which should be /// added to all created instructions. Entries present in MedataDataToCopy but /// not on \p Src will be dropped from MetadataToCopy. diff --git a/llvm/lib/IR/Globals.cpp b/llvm/lib/IR/Globals.cpp index 481a1d8..40f854a 100644 --- a/llvm/lib/IR/Globals.cpp +++ b/llvm/lib/IR/Globals.cpp @@ -243,6 +243,13 @@ void GlobalValue::removeSanitizerMetadata() { HasSanitizerMetadata = false; } +void GlobalValue::setNoSanitizeMetadata() { + SanitizerMetadata Meta; + Meta.NoAddress = true; + Meta.NoHWAddress = true; + setSanitizerMetadata(Meta); +} + StringRef GlobalObject::getSectionImpl() const { assert(hasSection()); return getContext().pImpl->GlobalObjectSections[this]; diff --git a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp index 5d366e3..f89a22d 100644 --- a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp +++ b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp @@ -422,6 +422,7 @@ private: bool InstrumentLandingPads; bool InstrumentWithCalls; bool InstrumentStack; + bool InstrumentGlobals; bool DetectUseAfterScope; bool UsePageAliases; bool UseMatchAllCallback; @@ -639,11 +640,13 @@ void HWAddressSanitizer::initializeModule() { // If we don't have personality function support, fall back to landing pads. InstrumentLandingPads = optOr(ClInstrumentLandingPads, !NewRuntime); + InstrumentGlobals = + !CompileKernel && !UsePageAliases && optOr(ClGlobals, NewRuntime); + if (!CompileKernel) { createHwasanCtorComdat(); - bool InstrumentGlobals = optOr(ClGlobals, NewRuntime); - if (InstrumentGlobals && !UsePageAliases) + if (InstrumentGlobals) instrumentGlobals(); bool InstrumentPersonalityFunctions = @@ -787,6 +790,14 @@ bool HWAddressSanitizer::ignoreAccess(Instruction *Inst, Value *Ptr) { if (SSI && SSI->stackAccessIsSafe(*Inst)) return true; } + + GlobalVariable *G = dyn_cast<GlobalVariable>(getUnderlyingObject(Ptr)); + if (G) { + if (!InstrumentGlobals) + return true; + // TODO: Optimize inbound global accesses, like Asan `instrumentMop`. + } + return false; } diff --git a/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp b/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp index c42c53e..7cb0b29 100644 --- a/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp +++ b/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp @@ -188,6 +188,17 @@ static bool profDataReferencedByCode(const Module &M) { return enablesValueProfiling(M); } +class InstrIRBuilder : public IRBuilder<> { +public: + explicit InstrIRBuilder(Instruction *IP) : IRBuilder<>(IP) { + SetNoSanitizeMetadata(); + } + + explicit InstrIRBuilder(BasicBlock *BB) : IRBuilder<>(BB) { + SetNoSanitizeMetadata(); + } +}; + class InstrLowerer final { public: InstrLowerer(Module &M, const InstrProfOptions &Options, @@ -370,7 +381,7 @@ public: Value *LiveInValue = SSA.GetValueInMiddleOfBlock(ExitBlock); Value *Addr = cast<StoreInst>(Store)->getPointerOperand(); Type *Ty = LiveInValue->getType(); - IRBuilder<> Builder(InsertPos); + InstrIRBuilder Builder(InsertPos); if (auto *AddrInst = dyn_cast_or_null<IntToPtrInst>(Addr)) { // If isRuntimeCounterRelocationEnabled() is true then the address of // the store instruction is computed with two instructions in @@ -842,7 +853,7 @@ void InstrLowerer::lowerValueProfileInst(InstrProfValueProfileInst *Ind) { for (uint32_t Kind = IPVK_First; Kind < ValueKind; ++Kind) Index += It->second.NumValueSites[Kind]; - IRBuilder<> Builder(Ind); + InstrIRBuilder Builder(Ind); bool IsMemOpSize = (Ind->getValueKind()->getZExtValue() == llvm::InstrProfValueKind::IPVK_MemOPSize); CallInst *Call = nullptr; @@ -872,7 +883,7 @@ void InstrLowerer::lowerValueProfileInst(InstrProfValueProfileInst *Ind) { Value *InstrLowerer::getCounterAddress(InstrProfCntrInstBase *I) { auto *Counters = getOrCreateRegionCounters(I); - IRBuilder<> Builder(I); + InstrIRBuilder Builder(I); if (isa<InstrProfTimestampInst>(I)) Counters->setAlignment(Align(8)); @@ -887,7 +898,7 @@ Value *InstrLowerer::getCounterAddress(InstrProfCntrInstBase *I) { Function *Fn = I->getParent()->getParent(); LoadInst *&BiasLI = FunctionToProfileBiasMap[Fn]; if (!BiasLI) { - IRBuilder<> EntryBuilder(&Fn->getEntryBlock().front()); + InstrIRBuilder EntryBuilder(&Fn->getEntryBlock().front()); auto *Bias = M.getGlobalVariable(getInstrProfCounterBiasVarName()); if (!Bias) { // Compiler must define this variable when runtime counter relocation @@ -897,6 +908,7 @@ Value *InstrLowerer::getCounterAddress(InstrProfCntrInstBase *I) { M, Int64Ty, false, GlobalValue::LinkOnceODRLinkage, Constant::getNullValue(Int64Ty), getInstrProfCounterBiasVarName()); Bias->setVisibility(GlobalVariable::HiddenVisibility); + Bias->setNoSanitizeMetadata(); // A definition that's weak (linkonce_odr) without being in a COMDAT // section wouldn't lead to link errors, but it would lead to a dead // data word from every TU but one. Putting it in COMDAT ensures there @@ -912,7 +924,7 @@ Value *InstrLowerer::getCounterAddress(InstrProfCntrInstBase *I) { Value *InstrLowerer::getBitmapAddress(InstrProfMCDCTVBitmapUpdate *I) { auto *Bitmaps = getOrCreateRegionBitmaps(I); - IRBuilder<> Builder(I); + InstrIRBuilder Builder(I); auto *Addr = Builder.CreateConstInBoundsGEP2_32( Bitmaps->getValueType(), Bitmaps, 0, I->getBitmapIndex()->getZExtValue()); @@ -931,7 +943,7 @@ Value *InstrLowerer::getBitmapAddress(InstrProfMCDCTVBitmapUpdate *I) { void InstrLowerer::lowerCover(InstrProfCoverInst *CoverInstruction) { auto *Addr = getCounterAddress(CoverInstruction); - IRBuilder<> Builder(CoverInstruction); + InstrIRBuilder Builder(CoverInstruction); // We store zero to represent that this block is covered. Builder.CreateStore(Builder.getInt8(0), Addr); CoverInstruction->eraseFromParent(); @@ -943,7 +955,7 @@ void InstrLowerer::lowerTimestamp( "timestamp probes are always the first probe for a function"); auto &Ctx = M.getContext(); auto *TimestampAddr = getCounterAddress(TimestampInstruction); - IRBuilder<> Builder(TimestampInstruction); + InstrIRBuilder Builder(TimestampInstruction); auto *CalleeTy = FunctionType::get(Type::getVoidTy(Ctx), TimestampAddr->getType(), false); auto Callee = M.getOrInsertFunction( @@ -955,7 +967,7 @@ void InstrLowerer::lowerTimestamp( void InstrLowerer::lowerIncrement(InstrProfIncrementInst *Inc) { auto *Addr = getCounterAddress(Inc); - IRBuilder<> Builder(Inc); + InstrIRBuilder Builder(Inc); if (Options.Atomic || AtomicCounterUpdateAll || (Inc->getIndex()->isZeroValue() && AtomicFirstCounter)) { Builder.CreateAtomicRMW(AtomicRMWInst::Add, Addr, Inc->getStep(), @@ -990,7 +1002,7 @@ void InstrLowerer::lowerCoverageData(GlobalVariable *CoverageNamesVar) { void InstrLowerer::lowerMCDCTestVectorBitmapUpdate( InstrProfMCDCTVBitmapUpdate *Update) { - IRBuilder<> Builder(Update); + InstrIRBuilder Builder(Update); auto *Int8Ty = Type::getInt8Ty(M.getContext()); auto *Int32Ty = Type::getInt32Ty(M.getContext()); auto *MCDCCondBitmapAddr = Update->getMCDCCondBitmapAddr(); @@ -1034,7 +1046,7 @@ void InstrLowerer::lowerMCDCTestVectorBitmapUpdate( void InstrLowerer::lowerMCDCCondBitmapUpdate( InstrProfMCDCCondBitmapUpdate *Update) { - IRBuilder<> Builder(Update); + InstrIRBuilder Builder(Update); auto *Int32Ty = Type::getInt32Ty(M.getContext()); auto *MCDCCondBitmapAddr = Update->getMCDCCondBitmapAddr(); @@ -1300,6 +1312,7 @@ InstrLowerer::createRegionBitmaps(InstrProfMCDCBitmapInstBase *Inc, auto GV = new GlobalVariable(M, BitmapTy, false, Linkage, Constant::getNullValue(BitmapTy), Name); GV->setAlignment(Align(1)); + GV->setNoSanitizeMetadata(); return GV; } @@ -1340,6 +1353,7 @@ InstrLowerer::createRegionCounters(InstrProfCntrInstBase *Inc, StringRef Name, Constant::getNullValue(CounterTy), Name); GV->setAlignment(Align(8)); } + GV->setNoSanitizeMetadata(); return GV; } @@ -1454,6 +1468,7 @@ void InstrLowerer::createDataVariable(InstrProfCntrInstBase *Inc) { ValuesVar->setSection( getInstrProfSectionName(IPSK_vals, TT.getObjectFormat())); ValuesVar->setAlignment(Align(8)); + ValuesVar->setNoSanitizeMetadata(); maybeSetComdat(ValuesVar, Fn, CntsVarName); ValuesPtrExpr = ValuesVar; } @@ -1532,6 +1547,7 @@ void InstrLowerer::createDataVariable(InstrProfCntrInstBase *Inc) { Data->setSection( getInstrProfSectionName(DataSectionKind, TT.getObjectFormat())); Data->setAlignment(Align(INSTR_PROF_DATA_ALIGNMENT)); + Data->setNoSanitizeMetadata(); maybeSetComdat(Data, Fn, CntsVarName); PD.DataVar = Data; @@ -1592,6 +1608,7 @@ void InstrLowerer::emitVNodes() { VNodesVar->setSection( getInstrProfSectionName(IPSK_vnodes, TT.getObjectFormat())); VNodesVar->setAlignment(M.getDataLayout().getABITypeAlign(VNodesTy)); + VNodesVar->setNoSanitizeMetadata(); // VNodesVar is used by runtime but not referenced via relocation by other // sections. Conservatively make it linker retained. UsedVars.push_back(VNodesVar); @@ -1625,6 +1642,7 @@ void InstrLowerer::emitNameData() { // linker from inserting padding before the start of the names section or // between names entries. NamesVar->setAlignment(Align(1)); + NamesVar->setNoSanitizeMetadata(); // NamesVar is used by runtime but not referenced via relocation by other // sections. Conservatively make it linker retained. UsedVars.push_back(NamesVar); @@ -1653,7 +1671,7 @@ void InstrLowerer::emitRegistration() { Function::Create(RuntimeRegisterTy, GlobalVariable::ExternalLinkage, getInstrProfRegFuncName(), M); - IRBuilder<> IRB(BasicBlock::Create(M.getContext(), "", RegisterF)); + InstrIRBuilder IRB(BasicBlock::Create(M.getContext(), "", RegisterF)); for (Value *Data : CompilerUsedVars) if (!isa<Function>(Data)) IRB.CreateCall(RuntimeRegisterF, Data); @@ -1690,6 +1708,7 @@ bool InstrLowerer::emitRuntimeHook() { new GlobalVariable(M, Int32Ty, false, GlobalValue::ExternalLinkage, nullptr, getInstrProfRuntimeHookVarName()); Var->setVisibility(GlobalValue::HiddenVisibility); + Var->setNoSanitizeMetadata(); if (TT.isOSBinFormatELF() && !TT.isPS()) { // Mark the user variable as used so that it isn't stripped out. @@ -1706,7 +1725,7 @@ bool InstrLowerer::emitRuntimeHook() { if (TT.supportsCOMDAT()) User->setComdat(M.getOrInsertComdat(User->getName())); - IRBuilder<> IRB(BasicBlock::Create(M.getContext(), "", User)); + InstrIRBuilder IRB(BasicBlock::Create(M.getContext(), "", User)); auto *Load = IRB.CreateLoad(Int32Ty, Var); IRB.CreateRet(Load); @@ -1760,7 +1779,7 @@ void InstrLowerer::emitInitialization() { F->addFnAttr(Attribute::NoRedZone); // Add the basic block and the necessary calls. - IRBuilder<> IRB(BasicBlock::Create(M.getContext(), "", F)); + InstrIRBuilder IRB(BasicBlock::Create(M.getContext(), "", F)); IRB.CreateCall(RegisterF, {}); IRB.CreateRetVoid(); diff --git a/llvm/test/Instrumentation/HWAddressSanitizer/globals-access.ll b/llvm/test/Instrumentation/HWAddressSanitizer/globals-access.ll new file mode 100644 index 0000000..df1a2af --- /dev/null +++ b/llvm/test/Instrumentation/HWAddressSanitizer/globals-access.ll @@ -0,0 +1,44 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals all --global-value-regex "x" --version 4 +; RUN: opt < %s -S -passes=hwasan -mtriple=aarch64 -hwasan-globals=0 | FileCheck %s --check-prefixes=NOSTACK +; RUN: opt < %s -S -passes=hwasan -mtriple=aarch64 -hwasan-globals=1 | FileCheck %s + +@x = dso_local global i32 0, align 4 + +;. +; NOSTACK: @x = dso_local global i32 0, align 4 +;. +; CHECK: @x = alias i32, inttoptr (i64 add (i64 ptrtoint (ptr @x.hwasan to i64), i64 5260204364768739328) to ptr) +;. +define dso_local noundef i32 @_Z3tmpv() sanitize_hwaddress { +; NOSTACK-LABEL: define dso_local noundef i32 @_Z3tmpv( +; NOSTACK-SAME: ) #[[ATTR0:[0-9]+]] { +; NOSTACK-NEXT: entry: +; NOSTACK-NEXT: [[TMP0:%.*]] = load i32, ptr @x, align 4 +; NOSTACK-NEXT: ret i32 [[TMP0]] +; +; CHECK-LABEL: define dso_local noundef i32 @_Z3tmpv( +; CHECK-SAME: ) #[[ATTR0:[0-9]+]] { +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP12:%.*]] = load i64, ptr @__hwasan_tls, align 8 +; CHECK-NEXT: [[TMP1:%.*]] = or i64 [[TMP12]], 4294967295 +; CHECK-NEXT: [[HWASAN_SHADOW:%.*]] = add i64 [[TMP1]], 1 +; CHECK-NEXT: [[TMP2:%.*]] = inttoptr i64 [[HWASAN_SHADOW]] to ptr +; CHECK-NEXT: [[TMP3:%.*]] = lshr i64 ptrtoint (ptr @x to i64), 56 +; CHECK-NEXT: [[TMP4:%.*]] = trunc i64 [[TMP3]] to i8 +; CHECK-NEXT: [[TMP5:%.*]] = and i64 ptrtoint (ptr @x to i64), 72057594037927935 +; CHECK-NEXT: [[TMP6:%.*]] = lshr i64 [[TMP5]], 4 +; CHECK-NEXT: [[TMP7:%.*]] = getelementptr i8, ptr [[TMP2]], i64 [[TMP6]] +; CHECK-NEXT: [[TMP8:%.*]] = load i8, ptr [[TMP7]], align 1 +; CHECK-NEXT: [[TMP9:%.*]] = icmp ne i8 [[TMP4]], [[TMP8]] +; CHECK-NEXT: br i1 [[TMP9]], label [[TMP10:%.*]], label [[TMP11:%.*]], !prof [[PROF2:![0-9]+]] +; CHECK: 10: +; CHECK-NEXT: call void @llvm.hwasan.check.memaccess.shortgranules(ptr [[TMP2]], ptr @x, i32 2) +; CHECK-NEXT: br label [[TMP11]] +; CHECK: 11: +; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr @x, align 4 +; CHECK-NEXT: ret i32 [[TMP0]] +; +entry: + %0 = load i32, ptr @x, align 4 + ret i32 %0 +} diff --git a/llvm/test/Instrumentation/HWAddressSanitizer/use-after-scope-setjmp.ll b/llvm/test/Instrumentation/HWAddressSanitizer/use-after-scope-setjmp.ll index 079d722..62fd7a1 100644 --- a/llvm/test/Instrumentation/HWAddressSanitizer/use-after-scope-setjmp.ll +++ b/llvm/test/Instrumentation/HWAddressSanitizer/use-after-scope-setjmp.ll @@ -54,7 +54,6 @@ define dso_local noundef i1 @_Z6targetv() sanitize_hwaddress { ; CHECK: sw.bb1: ; CHECK-NEXT: br label [[RETURN]] ; CHECK: while.body: -; CHECK-NEXT: call void @llvm.hwasan.check.memaccess(ptr [[TMP16]], ptr @stackbuf, i32 19) ; CHECK-NEXT: store ptr [[BUF_HWASAN]], ptr @stackbuf, align 8 ; CHECK-NEXT: call void @may_jump() ; CHECK-NEXT: br label [[RETURN]] |