Skip to content

Commit 592f9d2

Browse files
author
Anthony Tran
committed
Addressed most of Dan's comments and added remaining test cases
1 parent cd7b3ed commit 592f9d2

26 files changed

+177
-94
lines changed

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,14 @@ enum VariableTypeDescriptorKind : uint16_t {
8585
// Miscellaneous Helper Methods
8686
//===--------------------------------------------------------------------===//
8787

88-
static llvm::StringRef GetTrapMessageForHandler(SanitizerHandler ID) {
88+
static llvm::StringRef GetUBSanTrapForHandler(SanitizerHandler ID) {
8989
switch (ID) {
9090
case SanitizerHandler::AddOverflow:
91-
return "Signed integer addition overflowed.";
91+
return "Signed integer addition overflowed";
9292

9393
case SanitizerHandler::BuiltinUnreachable:
94-
return "_builtin_unreachable() executed.";
94+
return "_builtin_unreachable(), execution reached an unreachable program "
95+
"point";
9596

9697
case SanitizerHandler::CFICheckFail:
9798
return "Control flow integrity check failed";
@@ -115,13 +116,14 @@ static llvm::StringRef GetTrapMessageForHandler(SanitizerHandler ID) {
115116
return "Invalid use of builtin function";
116117

117118
case SanitizerHandler::InvalidObjCCast:
118-
return "Invalid Objective-C cast.";
119+
return "Invalid Objective-C cast";
119120

120121
case SanitizerHandler::LoadInvalidValue:
121-
return "Loaded an invalid or uninitialized value";
122+
return "Loaded an invalid or uninitialized value for the type";
122123

123124
case SanitizerHandler::MissingReturn:
124-
return "Non-void function fell off end without return";
125+
return "Execution reached the end of a value-returning function without "
126+
"returning a value";
125127

126128
case SanitizerHandler::MulOverflow:
127129
return "Signed integer multiplication overflowed";
@@ -130,20 +132,20 @@ static llvm::StringRef GetTrapMessageForHandler(SanitizerHandler ID) {
130132
return "Signed integer negation overflowed";
131133

132134
case SanitizerHandler::NullabilityArg:
133-
return "Passing null as a function parameter which is annotated with "
135+
return "Passing null as an argument which is annotated with "
134136
"_Nonnull";
135137

136138
case SanitizerHandler::NullabilityReturn:
137139
return "Returning null from a function with a return type annotated with "
138140
"_Nonnull";
139141

140142
case SanitizerHandler::NonnullArg:
141-
return "Passing null as a function parameter which is declared to never be "
143+
return "Passing null pointer as an argument which is declared to never be "
142144
"null";
143145

144146
case SanitizerHandler::NonnullReturn:
145147
return "Returning null pointer from a function which is declared to never "
146-
"be null";
148+
"return null";
147149

148150
case SanitizerHandler::OutOfBounds:
149151
return "Array index out of bounds";
@@ -152,22 +154,22 @@ static llvm::StringRef GetTrapMessageForHandler(SanitizerHandler ID) {
152154
return "Pointer arithmetic overflowed bounds";
153155

154156
case SanitizerHandler::ShiftOutOfBounds:
155-
return "Shift amount exceeds bit-width of operand";
157+
return "Shift exponent is too large for the type";
156158

157159
case SanitizerHandler::SubOverflow:
158160
return "Signed integer subtraction overflowed";
159161

160162
case SanitizerHandler::TypeMismatch:
161163
return "Type mismatch in operation";
162164

163-
case SanitizerHandler::AlignmentAssumption: // Help on bottom 2
165+
case SanitizerHandler::AlignmentAssumption:
164166
return "Alignment assumption violated";
165167

166168
case SanitizerHandler::VLABoundNotPositive:
167-
return "Variable-length array bound is not positive";
169+
return "Variable length array bound evaluates to non-positive value";
168170

169-
default:
170-
return "";
171+
case SanitizerHandler::BoundsSafety:
172+
return {};
171173
}
172174
}
173175

@@ -4138,12 +4140,11 @@ void CodeGenFunction::EmitTrapCheck(llvm::Value *Checked,
41384140
llvm::BasicBlock *&TrapBB = TrapBBs[CheckHandlerID];
41394141

41404142
llvm::DILocation *TrapLocation = Builder.getCurrentDebugLocation();
4141-
llvm::StringRef Category = "UBSan Trap Reason";
4142-
llvm::StringRef TrapMessage = GetTrapMessageForHandler(CheckHandlerID);
4143+
llvm::StringRef TrapMessage = GetUBSanTrapForHandler(CheckHandlerID);
41434144

4144-
if (getDebugInfo() && !Category.empty()) {
4145+
if (getDebugInfo()) {
41454146
TrapLocation = getDebugInfo()->CreateTrapFailureMessageFor(
4146-
TrapLocation, Category, TrapMessage);
4147+
TrapLocation, "Undefined Behavior Sanitizer", TrapMessage);
41474148
}
41484149

41494150
NoMerge = NoMerge || !CGM.getCodeGenOpts().OptimizationLevel ||
@@ -4154,16 +4155,8 @@ void CodeGenFunction::EmitTrapCheck(llvm::Value *Checked,
41544155
auto Call = TrapBB->begin();
41554156
assert(isa<llvm::CallInst>(Call) && "Expected call in trap BB");
41564157

4157-
// Call->applyMergedLocation(Call->getDebugLoc(),
4158-
// Builder.getCurrentDebugLocation());
41594158
Call->applyMergedLocation(Call->getDebugLoc(), TrapLocation);
41604159

4161-
auto Unreachable = ++TrapBB->begin();
4162-
if (isa<llvm::UnreachableInst>(Unreachable)) {
4163-
Unreachable->applyMergedLocation(Unreachable->getDebugLoc(),
4164-
TrapLocation);
4165-
}
4166-
41674160
Builder.CreateCondBr(Checked, Cont, TrapBB,
41684161
MDHelper.createLikelyBranchWeights());
41694162
} else {
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
// RUN: %clang -O0 -g -debug-info-kind=standalone -dwarf-version=5 -fsanitize=undefined \
2-
// RUN: -fsanitize-trap=undefined -emit-llvm -S -c %s -o - | FileCheck %s
1+
// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -O0 -debug-info-kind=standalone -dwarf-version=5 \
2+
// RUN: -fsanitize=signed-integer-overflow -fsanitize-trap=signed-integer-overflow -emit-llvm %s -o - | FileCheck %s
33

44
int add_overflow(int a, int b) {
55
return a + b;
66
}
77

88
// CHECK: call void @llvm.ubsantrap(i8 0) {{.*}}!dbg [[LOC:![0-9]+]]
99
// CHECK: [[LOC]] = !DILocation(line: 0, scope: [[MSG:![0-9]+]], {{.+}})
10-
// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$UBSan Trap Reason
10+
// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$Undefined Behavior Sanitizer
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -O0 -debug-info-kind=standalone -dwarf-version=5 \
2+
// RUN: -fsanitize=alignment -fsanitize-trap=alignment -emit-llvm %s -o - | FileCheck %s
3+
4+
#include <stdint.h>
5+
int32_t* get_int(void) __attribute__((assume_aligned(16)));
6+
7+
void retrieve_int(void) {
8+
int* i = get_int();
9+
*i = 7;
10+
}
11+
12+
// CHECK: call void @llvm.ubsantrap(i8 23) {{.*}}!dbg [[LOC:![0-9]+]]
13+
// CHECK: [[LOC]] = !DILocation(line: 0, scope: [[MSG:![0-9]+]], {{.+}})
14+
// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$Undefined Behavior Sanitizer
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
// RUN: %clang -O0 -g -debug-info-kind=standalone -dwarf-version=5 -fsanitize=unreachable \
2-
// RUN: -fsanitize-trap=unreachable -emit-llvm -S -c %s -o - | FileCheck %s
1+
// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -O0 -debug-info-kind=standalone -dwarf-version=5 \
2+
// RUN: -fsanitize=unreachable -fsanitize-trap=unreachable -emit-llvm %s -o - | FileCheck %s
33

44
int call_builtin_unreachable()
55
{
66
__builtin_unreachable();
77
}
88

9-
109
// CHECK: call void @llvm.ubsantrap(i8 1) {{.*}}!dbg [[LOC:![0-9]+]]
1110
// CHECK: [[LOC]] = !DILocation(line: 0, scope: [[MSG:![0-9]+]], {{.+}})
12-
// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$UBSan Trap Reason
11+
// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$Undefined Behavior Sanitizer
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -O0 -debug-info-kind=standalone -dwarf-version=5 \
2+
// RUN: -fsanitize=cfi-icall -fsanitize-trap=cfi-icall -emit-llvm %s -o - | FileCheck %s
3+
4+
typedef int (*fp_t)(int);
5+
6+
int good(int x) {
7+
return x + 1;
8+
}
9+
10+
int bad(void) {
11+
return 0;
12+
}
13+
14+
int cfi_trigger(int a) {
15+
fp_t p = good;
16+
int r1 = p(a);
17+
18+
p = (fp_t)(void*)bad;
19+
int r2 = p(a);
20+
21+
return r1 + r2;
22+
}
23+
24+
25+
// CHECK: call void @llvm.ubsantrap(i8 2) {{.*}}!dbg [[LOC:![0-9]+]]
26+
// CHECK: [[LOC]] = !DILocation(line: 0, scope: [[MSG:![0-9]+]], {{.+}})
27+
// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$Undefined Behavior Sanitizer
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
// RUN: %clang -O0 -g -debug-info-kind=standalone -dwarf-version=5 -fsanitize=undefined \
2-
// RUN: -fsanitize-trap=undefined -emit-llvm -S -c %s -o - | FileCheck %s
1+
// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -O0 -debug-info-kind=standalone -dwarf-version=5 \
2+
// RUN: -fsanitize=signed-integer-overflow -fsanitize-trap=signed-integer-overflow -emit-llvm %s -o - | FileCheck %s
33

44
int div_rem_overflow(int a, int b) {
55
return a / b;
66
}
77

88
// CHECK: call void @llvm.ubsantrap(i8 3) {{.*}}!dbg [[LOC:![0-9]+]]
99
// CHECK: [[LOC]] = !DILocation(line: 0, scope: [[MSG:![0-9]+]], {{.+}})
10-
// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$UBSan Trap Reason
10+
// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$Undefined Behavior Sanitizer
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -O0 -debug-info-kind=standalone -dwarf-version=5 \
2+
// RUN: -fsanitize=vptr -fsanitize-trap=vptr -emit-llvm %s -o - | FileCheck %s
3+
4+
struct A {
5+
virtual void foo();
6+
};
7+
struct B {
8+
virtual void bar();
9+
};
10+
11+
void A::foo() { }
12+
void B::bar() { }
13+
14+
int dynamic_type_cache_miss() {
15+
B b;
16+
A &a = reinterpret_cast<A&>(b);
17+
a.foo();
18+
return 0;
19+
}
20+
21+
// CHECK: call void @llvm.ubsantrap(i8 4) {{.*}}!dbg [[LOC:![0-9]+]]
22+
// CHECK: [[LOC]] = !DILocation(line: 0, scope: [[MSG:![0-9]+]], {{.+}})
23+
// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$Undefined Behavior Sanitizer
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -O0 -debug-info-kind=standalone -dwarf-version=5 -fsanitize=float-cast-overflow \
2-
// RUN: -fsanitize-trap=float-cast-overflow -emit-llvm %s -o - | FileCheck %s
1+
// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -O0 -debug-info-kind=standalone -dwarf-version=5 \
2+
// RUN: -fsanitize=float-cast-overflow -fsanitize-trap=float-cast-overflow -emit-llvm %s -o - | FileCheck %s
33

44
int f(float x) {
55
return (int)x;
66
}
77

88
// CHECK: call void @llvm.ubsantrap(i8 5) {{.*}}!dbg [[LOC:![0-9]+]]
99
// CHECK: [[LOC]] = !DILocation(line: 0, scope: [[MSG:![0-9]+]], {{.+}})
10-
// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$UBSan Trap Reason
10+
// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$Undefined Behavior Sanitizer

clang/test/CodeGen/ubsan-trap-reason-function-type-mismatch.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: %clang -O0 -g -debug-info-kind=standalone -dwarf-version=5 -fsanitize=undefined \
2-
// RUN: -fsanitize-trap=undefined -emit-llvm -S -c %s -o - | FileCheck %s
1+
// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -O0 -debug-info-kind=standalone -dwarf-version=5 \
2+
// RUN: -fsanitize=function -fsanitize-trap=function -emit-llvm %s -o - | FileCheck %s
33

44
void target() { }
55

@@ -13,4 +13,4 @@ int function_type_mismatch() {
1313

1414
// CHECK: call void @llvm.ubsantrap(i8 6) {{.*}}!dbg [[LOC:![0-9]+]]
1515
// CHECK: [[LOC]] = !DILocation(line: 0, scope: [[MSG:![0-9]+]], {{.+}})
16-
// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$UBSan Trap Reason
16+
// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$Undefined Behavior Sanitizer
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: %clang -O0 -g -debug-info-kind=standalone -dwarf-version=5 -fsanitize=implicit-conversion \
2-
// RUN: -fsanitize-trap=implicit-conversion -emit-llvm -S -c %s -o - | FileCheck %s
1+
// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -O0 -debug-info-kind=standalone -dwarf-version=5 \
2+
// RUN: -fsanitize=implicit-unsigned-integer-truncation -fsanitize-trap=implicit-unsigned-integer-truncation -emit-llvm %s -o - | FileCheck %s
33

44
unsigned long long big;
55

@@ -10,4 +10,4 @@ unsigned implicit_conversion()
1010

1111
// CHECK: call void @llvm.ubsantrap(i8 7) {{.*}}!dbg [[LOC:![0-9]+]]
1212
// CHECK: [[LOC]] = !DILocation(line: 0, scope: [[MSG:![0-9]+]], {{.+}})
13-
// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$UBSan Trap Reason
13+
// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$Undefined Behavior Sanitizer

0 commit comments

Comments
 (0)