@@ -85,6 +85,98 @@ enum VariableTypeDescriptorKind : uint16_t {
85
85
// Miscellaneous Helper Methods
86
86
// ===--------------------------------------------------------------------===//
87
87
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
+
88
180
// / CreateTempAlloca - This creates a alloca and inserts it into the entry
89
181
// / block.
90
182
RawAddress
@@ -4041,7 +4133,8 @@ void CodeGenFunction::EmitUnreachable(SourceLocation Loc) {
4041
4133
4042
4134
void CodeGenFunction::EmitTrapCheck (llvm::Value *Checked,
4043
4135
SanitizerHandler CheckHandlerID,
4044
- bool NoMerge) {
4136
+ bool NoMerge, StringRef Annotation,
4137
+ StringRef TrapMessage) {
4045
4138
llvm::BasicBlock *Cont = createBasicBlock (" cont" );
4046
4139
4047
4140
// If we're optimizing, collapse all calls to trap down to just one per
@@ -4051,6 +4144,14 @@ void CodeGenFunction::EmitTrapCheck(llvm::Value *Checked,
4051
4144
4052
4145
llvm::BasicBlock *&TrapBB = TrapBBs[CheckHandlerID];
4053
4146
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
+
4054
4155
NoMerge = NoMerge || !CGM.getCodeGenOpts ().OptimizationLevel ||
4055
4156
(CurCodeDecl && CurCodeDecl->hasAttr <OptimizeNoneAttr>());
4056
4157
@@ -4059,8 +4160,16 @@ void CodeGenFunction::EmitTrapCheck(llvm::Value *Checked,
4059
4160
auto Call = TrapBB->begin ();
4060
4161
assert (isa<llvm::CallInst>(Call) && " Expected call in trap BB" );
4061
4162
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
+
4064
4173
Builder.CreateCondBr (Checked, Cont, TrapBB,
4065
4174
MDHelper.createLikelyBranchWeights ());
4066
4175
} else {
@@ -4069,6 +4178,8 @@ void CodeGenFunction::EmitTrapCheck(llvm::Value *Checked,
4069
4178
MDHelper.createLikelyBranchWeights ());
4070
4179
EmitBlock (TrapBB);
4071
4180
4181
+ ApplyDebugLocation applyTrapDI (*this , TrapLocation);
4182
+
4072
4183
llvm::CallInst *TrapCall =
4073
4184
Builder.CreateCall (CGM.getIntrinsic (llvm::Intrinsic::ubsantrap),
4074
4185
llvm::ConstantInt::get (CGM.Int8Ty , CheckHandlerID));
0 commit comments