@@ -85,6 +85,98 @@ enum VariableTypeDescriptorKind : uint16_t {
8585// Miscellaneous Helper Methods
8686// ===--------------------------------------------------------------------===//
8787
88+ static llvm::StringRef GetTrapMessageForHandler (SanitizerHandler ID) {
89+ switch (ID) {
90+ case SanitizerHandler::AddOverflow:
91+ return " The addition of two signed integers resulted in overflow." ;
92+
93+ case SanitizerHandler::BuiltinUnreachable:
94+ return " _builtin_unreachable encountered." ;
95+
96+ case SanitizerHandler::CFICheckFail:
97+ return " Control flow integrity check failed." ;
98+
99+ case SanitizerHandler::DivremOverflow: // Unsure
100+ return " stub" ;
101+
102+ case SanitizerHandler::DynamicTypeCacheMiss: // Unsure
103+ return " Data requested for dynamic type not found in cache memory." ;
104+
105+ case SanitizerHandler::FloatCastOverflow: // Pasted from LLVM docs, maybe
106+ // something better to put here.
107+ return " Conversion to, from, or between floating-point types which would "
108+ " overflow the destination." ;
109+
110+ case SanitizerHandler::FunctionTypeMismatch:
111+ return " Function called with arguments of a different data type than "
112+ " expected" ;
113+
114+ case SanitizerHandler::ImplicitConversion:
115+ return " Implicit conversion occurred." ;
116+
117+ case SanitizerHandler::InvalidBuiltin:
118+ return " Built-in function or keyword not recognized." ;
119+
120+ case SanitizerHandler::InvalidObjCCast:
121+ return " Invalid Objective-C cast." ;
122+
123+ case SanitizerHandler::LoadInvalidValue:
124+ return " stub" ;
125+
126+ case SanitizerHandler::MissingReturn:
127+ return " Function is missing a return." ;
128+
129+ case SanitizerHandler::MulOverflow:
130+ return " The multiplication of two signed integers resulted in overflow." ;
131+
132+ case SanitizerHandler::NegateOverflow:
133+ return " Underflow/negative overflow occurred." ;
134+
135+ case SanitizerHandler::
136+ NullabilityArg: // Next 4 pasted from
137+ // https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html
138+ return " Passing null as a function parameter which is annotated with "
139+ " _Nonnull" ;
140+
141+ case SanitizerHandler::NullabilityReturn:
142+ return " Returning null from a function with a return type annotated with "
143+ " _Nonnull" ;
144+
145+ case SanitizerHandler::NonnullArg:
146+ return " Passing null as a function parameter which is declared to never be "
147+ " null" ;
148+
149+ case SanitizerHandler::NonnullReturn:
150+ return " Returning null pointer from a function which is declared to never "
151+ " be null" ;
152+
153+ case SanitizerHandler::OutOfBounds:
154+ return " Out of bounds -- memory accessed outside of expected boundaries." ;
155+
156+ case SanitizerHandler::PointerOverflow:
157+ return " stub" ;
158+
159+ case SanitizerHandler::ShiftOutOfBounds:
160+ return " Bit shift attempted to move bits beyond boundaries of data type's "
161+ " bit size." ;
162+
163+ case SanitizerHandler::SubOverflow:
164+ return " The subtraction of two signed integers resulted in overflow." ;
165+
166+ case SanitizerHandler::TypeMismatch:
167+ return " Type mismatch -- value type used does not match type expected." ;
168+
169+ case SanitizerHandler::AlignmentAssumption: // Help on bottom 2
170+ return " stub" ;
171+
172+ case SanitizerHandler::VLABoundNotPositive:
173+ return " stub" ;
174+
175+ default :
176+ return " " ;
177+ }
178+ }
179+
88180// / CreateTempAlloca - This creates a alloca and inserts it into the entry
89181// / block.
90182RawAddress
@@ -4041,7 +4133,8 @@ void CodeGenFunction::EmitUnreachable(SourceLocation Loc) {
40414133
40424134void CodeGenFunction::EmitTrapCheck (llvm::Value *Checked,
40434135 SanitizerHandler CheckHandlerID,
4044- bool NoMerge) {
4136+ bool NoMerge, StringRef Annotation,
4137+ StringRef TrapMessage) {
40454138 llvm::BasicBlock *Cont = createBasicBlock (" cont" );
40464139
40474140 // If we're optimizing, collapse all calls to trap down to just one per
@@ -4051,6 +4144,14 @@ void CodeGenFunction::EmitTrapCheck(llvm::Value *Checked,
40514144
40524145 llvm::BasicBlock *&TrapBB = TrapBBs[CheckHandlerID];
40534146
4147+ llvm::DILocation *TrapLocation = Builder.getCurrentDebugLocation ();
4148+ llvm::StringRef Category = GetTrapMessageForHandler (CheckHandlerID);
4149+
4150+ if (getDebugInfo () && !Category.empty ()) {
4151+ TrapLocation = getDebugInfo ()->CreateTrapFailureMessageFor (
4152+ TrapLocation, Category, TrapMessage);
4153+ }
4154+
40544155 NoMerge = NoMerge || !CGM.getCodeGenOpts ().OptimizationLevel ||
40554156 (CurCodeDecl && CurCodeDecl->hasAttr <OptimizeNoneAttr>());
40564157
@@ -4059,8 +4160,16 @@ void CodeGenFunction::EmitTrapCheck(llvm::Value *Checked,
40594160 auto Call = TrapBB->begin ();
40604161 assert (isa<llvm::CallInst>(Call) && " Expected call in trap BB" );
40614162
4062- Call->applyMergedLocation (Call->getDebugLoc (),
4063- Builder.getCurrentDebugLocation ());
4163+ // Call->applyMergedLocation(Call->getDebugLoc(),
4164+ // Builder.getCurrentDebugLocation());
4165+ Call->applyMergedLocation (Call->getDebugLoc (), TrapLocation);
4166+
4167+ auto Unreachable = ++TrapBB->begin ();
4168+ if (isa<llvm::UnreachableInst>(Unreachable)) {
4169+ Unreachable->applyMergedLocation (Unreachable->getDebugLoc (),
4170+ TrapLocation);
4171+ }
4172+
40644173 Builder.CreateCondBr (Checked, Cont, TrapBB,
40654174 MDHelper.createLikelyBranchWeights ());
40664175 } else {
@@ -4069,6 +4178,8 @@ void CodeGenFunction::EmitTrapCheck(llvm::Value *Checked,
40694178 MDHelper.createLikelyBranchWeights ());
40704179 EmitBlock (TrapBB);
40714180
4181+ ApplyDebugLocation applyTrapDI (*this , TrapLocation);
4182+
40724183 llvm::CallInst *TrapCall =
40734184 Builder.CreateCall (CGM.getIntrinsic (llvm::Intrinsic::ubsantrap),
40744185 llvm::ConstantInt::get (CGM.Int8Ty , CheckHandlerID));
0 commit comments