llvm.org GIT mirror llvm / f94a258
HWASan exception support. Summary: Adds a call to __hwasan_handle_vfork(SP) at each landingpad entry. Reusing __hwasan_handle_vfork instead of introducing a new runtime call in order to be ABI-compatible with old runtime library. Reviewers: pcc Subscribers: kubamracek, hiraditya, #sanitizers, llvm-commits Tags: #sanitizers, #llvm Differential Revision: https://reviews.llvm.org/D61968 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@360959 91177308-0d34-0410-b5e6-96231b3b80d8 Evgeniy Stepanov 5 months ago
2 changed file(s) with 73 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
156156 cl::desc("instrument memory intrinsics"),
157157 cl::Hidden, cl::init(true));
158158
159 static cl::opt
160 ClInstrumentLandingPads("hwasan-instrument-landing-pads",
161 cl::desc("instrument landing pads"), cl::Hidden,
162 cl::init(true));
163
159164 static cl::opt ClInlineAllChecks("hwasan-inline-all-checks",
160165 cl::desc("inline all checks"),
161166 cl::Hidden, cl::init(false));
201206 Value *untagPointer(IRBuilder<> &IRB, Value *PtrLong);
202207 bool instrumentStack(SmallVectorImpl &Allocas,
203208 SmallVectorImpl &RetVec, Value *StackTag);
209 bool instrumentLandingPads(SmallVectorImpl &RetVec);
204210 Value *getNextTagWithCall(IRBuilder<> &IRB);
205211 Value *getStackBaseTag(IRBuilder<> &IRB);
206212 Value *getAllocaTag(IRBuilder<> &IRB, Value *StackTag, AllocaInst *AI,
215221 std::string CurModuleUniqueId;
216222 Triple TargetTriple;
217223 FunctionCallee HWAsanMemmove, HWAsanMemcpy, HWAsanMemset;
224 FunctionCallee HWAsanHandleVfork;
218225
219226 // Frame description is a way to pass names/sizes of local variables
220227 // to the run-time w/o adding extra executable code in every function.
438445 HWAsanMemset = M.getOrInsertFunction(MemIntrinCallbackPrefix + "memset",
439446 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
440447 IRB.getInt32Ty(), IntptrTy);
448
449 HWAsanHandleVfork =
450 M.getOrInsertFunction("__hwasan_handle_vfork", IRB.getVoidTy(), IntptrTy);
441451
442452 HwasanThreadEnterFunc =
443453 M.getOrInsertFunction("__hwasan_thread_enter", IRB.getVoidTy());
954964 return ShadowBase;
955965 }
956966
967 bool HWAddressSanitizer::instrumentLandingPads(
968 SmallVectorImpl &LandingPadVec) {
969 Module *M = LandingPadVec[0]->getModule();
970 Function *ReadRegister =
971 Intrinsic::getDeclaration(M, Intrinsic::read_register, IntptrTy);
972 const char *RegName =
973 (TargetTriple.getArch() == Triple::x86_64) ? "rsp" : "sp";
974 MDNode *MD = MDNode::get(*C, {MDString::get(*C, RegName)});
975 Value *Args[] = {MetadataAsValue::get(*C, MD)};
976
977 for (auto *LP : LandingPadVec) {
978 IRBuilder<> IRB(LP->getNextNode());
979 IRB.CreateCall(HWAsanHandleVfork, {IRB.CreateCall(ReadRegister, Args)});
980 }
981 return true;
982 }
983
957984 bool HWAddressSanitizer::instrumentStack(
958985 SmallVectorImpl &Allocas,
959986 SmallVectorImpl &RetVec, Value *StackTag) {
10221049 SmallVector ToInstrument;
10231050 SmallVector AllocasToInstrument;
10241051 SmallVector RetVec;
1052 SmallVector LandingPadVec;
10251053 for (auto &BB : F) {
10261054 for (auto &Inst : BB) {
10271055 if (ClInstrumentStack)
10401068 isa(Inst))
10411069 RetVec.push_back(&Inst);
10421070
1071 if (ClInstrumentLandingPads && isa(Inst))
1072 LandingPadVec.push_back(&Inst);
1073
10431074 Value *MaybeMask = nullptr;
10441075 bool IsWrite;
10451076 unsigned Alignment;
10511082 }
10521083 }
10531084
1085 initializeCallbacks(*F.getParent());
1086
1087 if (!LandingPadVec.empty())
1088 instrumentLandingPads(LandingPadVec);
1089
10541090 if (AllocasToInstrument.empty() && ToInstrument.empty())
10551091 return false;
10561092
10571093 if (ClCreateFrameDescriptions && !AllocasToInstrument.empty())
10581094 createFrameGlobal(F, createFrameString(AllocasToInstrument));
10591095
1060 initializeCallbacks(*F.getParent());
10611096
10621097 assert(!LocalDynamicShadow);
10631098
0 ; RUN: opt < %s -mtriple aarch64-linux-android -hwasan -S | FileCheck %s --check-prefixes=COMMON,ARM
1 ; RUN: opt < %s -mtriple x86_64-linux -hwasan -S | FileCheck %s --check-prefixes=COMMON,X86
2
3 target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
4 target triple = "aarch64-unknown-linux-android"
5
6 define i32 @f() local_unnamed_addr sanitize_hwaddress personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
7 entry:
8 invoke void @g()
9 to label %return unwind label %lpad
10
11 lpad:
12 ; COMMON: landingpad { i8*, i32 }
13 ; COMMON-NEXT: catch i8* null
14 %0 = landingpad { i8*, i32 }
15 catch i8* null
16
17 ; COMMON-NEXT: %[[X:[^ ]*]] = call i64 @llvm.read_register.i64(metadata ![[META:[^ ]*]])
18 ; COMMON-NEXT: call void @__hwasan_handle_vfork(i64 %[[X]])
19
20 %1 = extractvalue { i8*, i32 } %0, 0
21 %2 = tail call i8* @__cxa_begin_catch(i8* %1)
22 tail call void @__cxa_end_catch()
23 br label %return
24 return:
25 %retval.0 = phi i32 [ 1, %lpad ], [ 0, %entry ]
26 ret i32 %retval.0
27 }
28
29 declare void @g() local_unnamed_addr
30
31 declare i32 @__gxx_personality_v0(...)
32 declare i8* @__cxa_begin_catch(i8*) local_unnamed_addr
33 declare void @__cxa_end_catch() local_unnamed_addr
34
35 ; ARM: ![[META]] = !{!"sp"}
36 ; X86: ![[META]] = !{!"rsp"}