https://github.com/anthonyhatran created https://github.com/llvm/llvm-project/pull/145967
None >From 7bdb672ef70687575b93d099ccfe261eec66d006 Mon Sep 17 00:00:00 2001 From: Anthony Tran <anthonyt...@anthonys-air.lan> Date: Thu, 26 Jun 2025 12:40:05 -0700 Subject: [PATCH 1/2] Addressed most of Dan's comments and added remaining test cases --- clang/lib/CodeGen/CGExpr.cpp | 107 ++++++++++++++++++ .../CodeGen/ubsan-trap-reason-add-overflow.c | 10 ++ .../ubsan-trap-reason-alignment-assumption.c | 14 +++ .../ubsan-trap-reason-builtin-unreachable.c | 11 ++ .../ubsan-trap-reason-cfi-check-fail.c | 27 +++++ .../ubsan-trap-reason-div-rem-overflow.c | 10 ++ ...an-trap-reason-dynamic-type-cache-miss.cpp | 23 ++++ .../ubsan-trap-reason-float-cast-overflow.c | 10 ++ ...ubsan-trap-reason-function-type-mismatch.c | 16 +++ .../ubsan-trap-reason-implicit-conversion.c | 13 +++ .../ubsan-trap-reason-invalid-builtin.c | 11 ++ .../ubsan-trap-reason-invalid-objc-cast.m | 31 +++++ .../ubsan-trap-reason-load-invalid-value.c | 15 +++ .../ubsan-trap-reason-missing-return.cpp | 12 ++ .../CodeGen/ubsan-trap-reason-mul-overflow.c | 10 ++ .../ubsan-trap-reason-negate-overflow.c | 12 ++ .../CodeGen/ubsan-trap-reason-nonnull-arg.c | 16 +++ .../ubsan-trap-reason-nonnull-return.c | 15 +++ .../ubsan-trap-reason-nullability-arg.c | 18 +++ .../ubsan-trap-reason-nullability-return.c | 18 +++ .../CodeGen/ubsan-trap-reason-out-of-bounds.c | 12 ++ .../ubsan-trap-reason-pointer-overflow.c | 16 +++ .../ubsan-trap-reason-shift-out-of-bounds.c | 12 ++ .../CodeGen/ubsan-trap-reason-sub-overflow.c | 10 ++ .../CodeGen/ubsan-trap-reason-type-mismatch.c | 11 ++ ...ubsan-trap-reason-vla-bound-not-positive.c | 14 +++ 26 files changed, 474 insertions(+) create mode 100644 clang/test/CodeGen/ubsan-trap-reason-add-overflow.c create mode 100644 clang/test/CodeGen/ubsan-trap-reason-alignment-assumption.c create mode 100644 clang/test/CodeGen/ubsan-trap-reason-builtin-unreachable.c create mode 100644 clang/test/CodeGen/ubsan-trap-reason-cfi-check-fail.c create mode 100644 clang/test/CodeGen/ubsan-trap-reason-div-rem-overflow.c create mode 100644 clang/test/CodeGen/ubsan-trap-reason-dynamic-type-cache-miss.cpp create mode 100644 clang/test/CodeGen/ubsan-trap-reason-float-cast-overflow.c create mode 100644 clang/test/CodeGen/ubsan-trap-reason-function-type-mismatch.c create mode 100644 clang/test/CodeGen/ubsan-trap-reason-implicit-conversion.c create mode 100644 clang/test/CodeGen/ubsan-trap-reason-invalid-builtin.c create mode 100644 clang/test/CodeGen/ubsan-trap-reason-invalid-objc-cast.m create mode 100644 clang/test/CodeGen/ubsan-trap-reason-load-invalid-value.c create mode 100644 clang/test/CodeGen/ubsan-trap-reason-missing-return.cpp create mode 100644 clang/test/CodeGen/ubsan-trap-reason-mul-overflow.c create mode 100644 clang/test/CodeGen/ubsan-trap-reason-negate-overflow.c create mode 100644 clang/test/CodeGen/ubsan-trap-reason-nonnull-arg.c create mode 100644 clang/test/CodeGen/ubsan-trap-reason-nonnull-return.c create mode 100644 clang/test/CodeGen/ubsan-trap-reason-nullability-arg.c create mode 100644 clang/test/CodeGen/ubsan-trap-reason-nullability-return.c create mode 100644 clang/test/CodeGen/ubsan-trap-reason-out-of-bounds.c create mode 100644 clang/test/CodeGen/ubsan-trap-reason-pointer-overflow.c create mode 100644 clang/test/CodeGen/ubsan-trap-reason-shift-out-of-bounds.c create mode 100644 clang/test/CodeGen/ubsan-trap-reason-sub-overflow.c create mode 100644 clang/test/CodeGen/ubsan-trap-reason-type-mismatch.c create mode 100644 clang/test/CodeGen/ubsan-trap-reason-vla-bound-not-positive.c diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp index 85c768807572f..34dba66edfac1 100644 --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -85,6 +85,97 @@ enum VariableTypeDescriptorKind : uint16_t { // Miscellaneous Helper Methods //===--------------------------------------------------------------------===// +<<<<<<< HEAD +======= +static llvm::StringRef GetUBSanTrapForHandler(SanitizerHandler ID) { + switch (ID) { + case SanitizerHandler::AddOverflow: + return "Signed integer addition overflowed"; + + case SanitizerHandler::BuiltinUnreachable: + return "_builtin_unreachable(), execution reached an unreachable program " + "point"; + + case SanitizerHandler::CFICheckFail: + return "Control flow integrity check failed"; + + case SanitizerHandler::DivremOverflow: + return "Signed integer divide or remainder overflowed"; + + case SanitizerHandler::DynamicTypeCacheMiss: + return "Dynamic-type cache miss"; + + case SanitizerHandler::FloatCastOverflow: + return "Floating-point to integer conversion overflowed"; + + case SanitizerHandler::FunctionTypeMismatch: + return "Function called with mismatched signature"; + + case SanitizerHandler::ImplicitConversion: + return "Implicit integer conversion overflowed or lost data"; + + case SanitizerHandler::InvalidBuiltin: + return "Invalid use of builtin function"; + + case SanitizerHandler::InvalidObjCCast: + return "Invalid Objective-C cast"; + + case SanitizerHandler::LoadInvalidValue: + return "Loaded an invalid or uninitialized value for the type"; + + case SanitizerHandler::MissingReturn: + return "Execution reached the end of a value-returning function without " + "returning a value"; + + case SanitizerHandler::MulOverflow: + return "Signed integer multiplication overflowed"; + + case SanitizerHandler::NegateOverflow: + return "Signed integer negation overflowed"; + + case SanitizerHandler::NullabilityArg: + return "Passing null as an argument which is annotated with " + "_Nonnull"; + + case SanitizerHandler::NullabilityReturn: + return "Returning null from a function with a return type annotated with " + "_Nonnull"; + + case SanitizerHandler::NonnullArg: + return "Passing null pointer as an argument which is declared to never be " + "null"; + + case SanitizerHandler::NonnullReturn: + return "Returning null pointer from a function which is declared to never " + "return null"; + + case SanitizerHandler::OutOfBounds: + return "Array index out of bounds"; + + case SanitizerHandler::PointerOverflow: + return "Pointer arithmetic overflowed bounds"; + + case SanitizerHandler::ShiftOutOfBounds: + return "Shift exponent is too large for the type"; + + case SanitizerHandler::SubOverflow: + return "Signed integer subtraction overflowed"; + + case SanitizerHandler::TypeMismatch: + return "Type mismatch in operation"; + + case SanitizerHandler::AlignmentAssumption: + return "Alignment assumption violated"; + + case SanitizerHandler::VLABoundNotPositive: + return "Variable length array bound evaluates to non-positive value"; + + case SanitizerHandler::BoundsSafety: + return {}; + } +} + +>>>>>>> 592f9d2f8f85 (Addressed most of Dan's comments and added remaining test cases) /// CreateTempAlloca - This creates a alloca and inserts it into the entry /// block. RawAddress @@ -4051,6 +4142,17 @@ void CodeGenFunction::EmitTrapCheck(llvm::Value *Checked, llvm::BasicBlock *&TrapBB = TrapBBs[CheckHandlerID]; +<<<<<<< HEAD +======= + llvm::DILocation *TrapLocation = Builder.getCurrentDebugLocation(); + llvm::StringRef TrapMessage = GetUBSanTrapForHandler(CheckHandlerID); + + if (getDebugInfo()) { + TrapLocation = getDebugInfo()->CreateTrapFailureMessageFor( + TrapLocation, "Undefined Behavior Sanitizer", TrapMessage); + } + +>>>>>>> 592f9d2f8f85 (Addressed most of Dan's comments and added remaining test cases) NoMerge = NoMerge || !CGM.getCodeGenOpts().OptimizationLevel || (CurCodeDecl && CurCodeDecl->hasAttr<OptimizeNoneAttr>()); @@ -4059,8 +4161,13 @@ void CodeGenFunction::EmitTrapCheck(llvm::Value *Checked, auto Call = TrapBB->begin(); assert(isa<llvm::CallInst>(Call) && "Expected call in trap BB"); +<<<<<<< HEAD Call->applyMergedLocation(Call->getDebugLoc(), Builder.getCurrentDebugLocation()); +======= + Call->applyMergedLocation(Call->getDebugLoc(), TrapLocation); + +>>>>>>> 592f9d2f8f85 (Addressed most of Dan's comments and added remaining test cases) Builder.CreateCondBr(Checked, Cont, TrapBB, MDHelper.createLikelyBranchWeights()); } else { diff --git a/clang/test/CodeGen/ubsan-trap-reason-add-overflow.c b/clang/test/CodeGen/ubsan-trap-reason-add-overflow.c new file mode 100644 index 0000000000000..4b3881ae9c7dc --- /dev/null +++ b/clang/test/CodeGen/ubsan-trap-reason-add-overflow.c @@ -0,0 +1,10 @@ +// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -O0 -debug-info-kind=standalone -dwarf-version=5 \ +// RUN: -fsanitize=signed-integer-overflow -fsanitize-trap=signed-integer-overflow -emit-llvm %s -o - | FileCheck %s + +int add_overflow(int a, int b) { + return a + b; +} + +// CHECK: call void @llvm.ubsantrap(i8 0) {{.*}}!dbg [[LOC:![0-9]+]] +// CHECK: [[LOC]] = !DILocation(line: 0, scope: [[MSG:![0-9]+]], {{.+}}) +// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$Undefined Behavior Sanitizer diff --git a/clang/test/CodeGen/ubsan-trap-reason-alignment-assumption.c b/clang/test/CodeGen/ubsan-trap-reason-alignment-assumption.c new file mode 100644 index 0000000000000..a41a238eaf129 --- /dev/null +++ b/clang/test/CodeGen/ubsan-trap-reason-alignment-assumption.c @@ -0,0 +1,14 @@ +// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -O0 -debug-info-kind=standalone -dwarf-version=5 \ +// RUN: -fsanitize=alignment -fsanitize-trap=alignment -emit-llvm %s -o - | FileCheck %s + +#include <stdint.h> +int32_t* get_int(void) __attribute__((assume_aligned(16))); + +void retrieve_int(void) { + int* i = get_int(); + *i = 7; +} + +// CHECK: call void @llvm.ubsantrap(i8 23) {{.*}}!dbg [[LOC:![0-9]+]] +// CHECK: [[LOC]] = !DILocation(line: 0, scope: [[MSG:![0-9]+]], {{.+}}) +// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$Undefined Behavior Sanitizer \ No newline at end of file diff --git a/clang/test/CodeGen/ubsan-trap-reason-builtin-unreachable.c b/clang/test/CodeGen/ubsan-trap-reason-builtin-unreachable.c new file mode 100644 index 0000000000000..a85d92319cb7b --- /dev/null +++ b/clang/test/CodeGen/ubsan-trap-reason-builtin-unreachable.c @@ -0,0 +1,11 @@ +// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -O0 -debug-info-kind=standalone -dwarf-version=5 \ +// RUN: -fsanitize=unreachable -fsanitize-trap=unreachable -emit-llvm %s -o - | FileCheck %s + +int call_builtin_unreachable() +{ + __builtin_unreachable(); +} + +// CHECK: call void @llvm.ubsantrap(i8 1) {{.*}}!dbg [[LOC:![0-9]+]] +// CHECK: [[LOC]] = !DILocation(line: 0, scope: [[MSG:![0-9]+]], {{.+}}) +// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$Undefined Behavior Sanitizer \ No newline at end of file diff --git a/clang/test/CodeGen/ubsan-trap-reason-cfi-check-fail.c b/clang/test/CodeGen/ubsan-trap-reason-cfi-check-fail.c new file mode 100644 index 0000000000000..da6c9bc7fb2f9 --- /dev/null +++ b/clang/test/CodeGen/ubsan-trap-reason-cfi-check-fail.c @@ -0,0 +1,27 @@ +// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -O0 -debug-info-kind=standalone -dwarf-version=5 \ +// RUN: -fsanitize=cfi-icall -fsanitize-trap=cfi-icall -emit-llvm %s -o - | FileCheck %s + +typedef int (*fp_t)(int); + +int good(int x) { + return x + 1; +} + +int bad(void) { + return 0; +} + +int cfi_trigger(int a) { + fp_t p = good; + int r1 = p(a); + + p = (fp_t)(void*)bad; + int r2 = p(a); + + return r1 + r2; +} + + +// CHECK: call void @llvm.ubsantrap(i8 2) {{.*}}!dbg [[LOC:![0-9]+]] +// CHECK: [[LOC]] = !DILocation(line: 0, scope: [[MSG:![0-9]+]], {{.+}}) +// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$Undefined Behavior Sanitizer \ No newline at end of file diff --git a/clang/test/CodeGen/ubsan-trap-reason-div-rem-overflow.c b/clang/test/CodeGen/ubsan-trap-reason-div-rem-overflow.c new file mode 100644 index 0000000000000..f98927399272f --- /dev/null +++ b/clang/test/CodeGen/ubsan-trap-reason-div-rem-overflow.c @@ -0,0 +1,10 @@ +// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -O0 -debug-info-kind=standalone -dwarf-version=5 \ +// RUN: -fsanitize=signed-integer-overflow -fsanitize-trap=signed-integer-overflow -emit-llvm %s -o - | FileCheck %s + +int div_rem_overflow(int a, int b) { + return a / b; +} + +// CHECK: call void @llvm.ubsantrap(i8 3) {{.*}}!dbg [[LOC:![0-9]+]] +// CHECK: [[LOC]] = !DILocation(line: 0, scope: [[MSG:![0-9]+]], {{.+}}) +// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$Undefined Behavior Sanitizer \ No newline at end of file diff --git a/clang/test/CodeGen/ubsan-trap-reason-dynamic-type-cache-miss.cpp b/clang/test/CodeGen/ubsan-trap-reason-dynamic-type-cache-miss.cpp new file mode 100644 index 0000000000000..e279626f09227 --- /dev/null +++ b/clang/test/CodeGen/ubsan-trap-reason-dynamic-type-cache-miss.cpp @@ -0,0 +1,23 @@ +// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -O0 -debug-info-kind=standalone -dwarf-version=5 \ +// RUN: -fsanitize=vptr -fsanitize-trap=vptr -emit-llvm %s -o - | FileCheck %s + +struct A { + virtual void foo(); +}; +struct B { + virtual void bar(); +}; + +void A::foo() { } +void B::bar() { } + +int dynamic_type_cache_miss() { + B b; + A &a = reinterpret_cast<A&>(b); + a.foo(); + return 0; +} + +// CHECK: call void @llvm.ubsantrap(i8 4) {{.*}}!dbg [[LOC:![0-9]+]] +// CHECK: [[LOC]] = !DILocation(line: 0, scope: [[MSG:![0-9]+]], {{.+}}) +// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$Undefined Behavior Sanitizer diff --git a/clang/test/CodeGen/ubsan-trap-reason-float-cast-overflow.c b/clang/test/CodeGen/ubsan-trap-reason-float-cast-overflow.c new file mode 100644 index 0000000000000..0524d8bbf9373 --- /dev/null +++ b/clang/test/CodeGen/ubsan-trap-reason-float-cast-overflow.c @@ -0,0 +1,10 @@ +// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -O0 -debug-info-kind=standalone -dwarf-version=5 \ +// RUN: -fsanitize=float-cast-overflow -fsanitize-trap=float-cast-overflow -emit-llvm %s -o - | FileCheck %s + +int f(float x) { + return (int)x; +} + +// CHECK: call void @llvm.ubsantrap(i8 5) {{.*}}!dbg [[LOC:![0-9]+]] +// CHECK: [[LOC]] = !DILocation(line: 0, scope: [[MSG:![0-9]+]], {{.+}}) +// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$Undefined Behavior Sanitizer \ No newline at end of file diff --git a/clang/test/CodeGen/ubsan-trap-reason-function-type-mismatch.c b/clang/test/CodeGen/ubsan-trap-reason-function-type-mismatch.c new file mode 100644 index 0000000000000..8811a064a51c0 --- /dev/null +++ b/clang/test/CodeGen/ubsan-trap-reason-function-type-mismatch.c @@ -0,0 +1,16 @@ +// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -O0 -debug-info-kind=standalone -dwarf-version=5 \ +// RUN: -fsanitize=function -fsanitize-trap=function -emit-llvm %s -o - | FileCheck %s + +void target() { } + +int function_type_mismatch() { + int (*fp_int)(int); + + fp_int = (int (*)(int))(void *)target; + + return fp_int(42); +} + +// CHECK: call void @llvm.ubsantrap(i8 6) {{.*}}!dbg [[LOC:![0-9]+]] +// CHECK: [[LOC]] = !DILocation(line: 0, scope: [[MSG:![0-9]+]], {{.+}}) +// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$Undefined Behavior Sanitizer \ No newline at end of file diff --git a/clang/test/CodeGen/ubsan-trap-reason-implicit-conversion.c b/clang/test/CodeGen/ubsan-trap-reason-implicit-conversion.c new file mode 100644 index 0000000000000..6e98aeacb17c9 --- /dev/null +++ b/clang/test/CodeGen/ubsan-trap-reason-implicit-conversion.c @@ -0,0 +1,13 @@ +// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -O0 -debug-info-kind=standalone -dwarf-version=5 \ +// RUN: -fsanitize=implicit-unsigned-integer-truncation -fsanitize-trap=implicit-unsigned-integer-truncation -emit-llvm %s -o - | FileCheck %s + +unsigned long long big; + +unsigned implicit_conversion() +{ + return big; +} + +// CHECK: call void @llvm.ubsantrap(i8 7) {{.*}}!dbg [[LOC:![0-9]+]] +// CHECK: [[LOC]] = !DILocation(line: 0, scope: [[MSG:![0-9]+]], {{.+}}) +// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$Undefined Behavior Sanitizer \ No newline at end of file diff --git a/clang/test/CodeGen/ubsan-trap-reason-invalid-builtin.c b/clang/test/CodeGen/ubsan-trap-reason-invalid-builtin.c new file mode 100644 index 0000000000000..4703518e11e6e --- /dev/null +++ b/clang/test/CodeGen/ubsan-trap-reason-invalid-builtin.c @@ -0,0 +1,11 @@ +// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -O0 -debug-info-kind=standalone -dwarf-version=5 \ +// RUN: -fsanitize=builtin -fsanitize-trap=builtin -emit-llvm %s -o - | FileCheck %s + +unsigned invalid_builtin(unsigned x) +{ + return __builtin_clz(x); +} + +// CHECK: call void @llvm.ubsantrap(i8 8) {{.*}}!dbg [[LOC:![0-9]+]] +// CHECK: [[LOC]] = !DILocation(line: 0, scope: [[MSG:![0-9]+]], {{.+}}) +// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$Undefined Behavior Sanitizer \ No newline at end of file diff --git a/clang/test/CodeGen/ubsan-trap-reason-invalid-objc-cast.m b/clang/test/CodeGen/ubsan-trap-reason-invalid-objc-cast.m new file mode 100644 index 0000000000000..f7460b186b9b3 --- /dev/null +++ b/clang/test/CodeGen/ubsan-trap-reason-invalid-objc-cast.m @@ -0,0 +1,31 @@ +// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -O0 -debug-info-kind=standalone -dwarf-version=5 \ +// RUN: -fsanitize=objc-cast -fsanitize-trap=objc-cast -emit-llvm %s -o - | FileCheck %s + +@interface NSFastEnumerationState +@end + +#define NSUInteger unsigned int + +@interface NSArray ++(NSArray*) arrayWithObjects: (id) first, ...; +- (NSUInteger) countByEnumeratingWithState:(NSFastEnumerationState *) state + objects:(id[]) buffer + count:(NSUInteger) len; +-(unsigned) count; +@end +@interface NSString +-(const char*) cString; +@end + +void receive_NSString(NSString*); + +void t0(void) { + NSArray *array = [NSArray arrayWithObjects: @"0", @"1", (void*)0]; + for (NSString *i in array) { + receive_NSString(i); + } +} + +// CHECK: call void @llvm.ubsantrap(i8 9) {{.*}}!dbg [[LOC:![0-9]+]] +// CHECK: [[LOC]] = !DILocation(line: 0, scope: [[MSG:![0-9]+]], {{.+}}) +// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$Undefined Behavior Sanitizer \ No newline at end of file diff --git a/clang/test/CodeGen/ubsan-trap-reason-load-invalid-value.c b/clang/test/CodeGen/ubsan-trap-reason-load-invalid-value.c new file mode 100644 index 0000000000000..e751d5135a50e --- /dev/null +++ b/clang/test/CodeGen/ubsan-trap-reason-load-invalid-value.c @@ -0,0 +1,15 @@ +// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -O0 -debug-info-kind=standalone -dwarf-version=5 \ +// RUN: -fsanitize=bool -fsanitize-trap=bool -emit-llvm %s -o - | FileCheck %s + +#include <stdbool.h> + +unsigned char bad_byte; + +bool load_invalid_value() +{ + return *((bool *)&bad_byte); +} + +// CHECK: call void @llvm.ubsantrap(i8 10) {{.*}}!dbg [[LOC:![0-9]+]] +// CHECK: [[LOC]] = !DILocation(line: 0, scope: [[MSG:![0-9]+]], {{.+}}) +// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$Undefined Behavior Sanitizer \ No newline at end of file diff --git a/clang/test/CodeGen/ubsan-trap-reason-missing-return.cpp b/clang/test/CodeGen/ubsan-trap-reason-missing-return.cpp new file mode 100644 index 0000000000000..d97523e503eff --- /dev/null +++ b/clang/test/CodeGen/ubsan-trap-reason-missing-return.cpp @@ -0,0 +1,12 @@ +// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -O0 -debug-info-kind=standalone -dwarf-version=5 \ +// RUN: -fsanitize=return -fsanitize-trap=return -emit-llvm %s -o - | FileCheck %s + +int missing_return(int x) +{ + if (x > 0) + return x; +} + +// CHECK: call void @llvm.ubsantrap(i8 11) {{.*}}!dbg [[LOC:![0-9]+]] +// CHECK: [[LOC]] = !DILocation(line: 0, scope: [[MSG:![0-9]+]], {{.+}}) +// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$Undefined Behavior Sanitizer \ No newline at end of file diff --git a/clang/test/CodeGen/ubsan-trap-reason-mul-overflow.c b/clang/test/CodeGen/ubsan-trap-reason-mul-overflow.c new file mode 100644 index 0000000000000..5250e70e61b43 --- /dev/null +++ b/clang/test/CodeGen/ubsan-trap-reason-mul-overflow.c @@ -0,0 +1,10 @@ +// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -O0 -debug-info-kind=standalone -dwarf-version=5 \ +// RUN: -fsanitize=signed-integer-overflow -fsanitize-trap=signed-integer-overflow -emit-llvm %s -o - | FileCheck %s + +int mul_overflow(int a, int b) { + return a * b; +} + +// CHECK: call void @llvm.ubsantrap(i8 12) {{.*}}!dbg [[LOC:![0-9]+]] +// CHECK: [[LOC]] = !DILocation(line: 0, scope: [[MSG:![0-9]+]], {{.+}}) +// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$Undefined Behavior Sanitizer \ No newline at end of file diff --git a/clang/test/CodeGen/ubsan-trap-reason-negate-overflow.c b/clang/test/CodeGen/ubsan-trap-reason-negate-overflow.c new file mode 100644 index 0000000000000..4273efaced40d --- /dev/null +++ b/clang/test/CodeGen/ubsan-trap-reason-negate-overflow.c @@ -0,0 +1,12 @@ +// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -O0 -debug-info-kind=standalone -dwarf-version=5 \ +// RUN: -fsanitize=signed-integer-overflow -fsanitize-trap=signed-integer-overflow -emit-llvm %s -o - | FileCheck %s + +int negate_overflow() +{ + int x; + return -x; +} + +// CHECK: call void @llvm.ubsantrap(i8 13) {{.*}}!dbg [[LOC:![0-9]+]] +// CHECK: [[LOC]] = !DILocation(line: 0, scope: [[MSG:![0-9]+]], {{.+}}) +// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$Undefined Behavior Sanitizer \ No newline at end of file diff --git a/clang/test/CodeGen/ubsan-trap-reason-nonnull-arg.c b/clang/test/CodeGen/ubsan-trap-reason-nonnull-arg.c new file mode 100644 index 0000000000000..e0849c6b81c32 --- /dev/null +++ b/clang/test/CodeGen/ubsan-trap-reason-nonnull-arg.c @@ -0,0 +1,16 @@ +// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -O0 -debug-info-kind=standalone -dwarf-version=5 \ +// RUN: -fsanitize=nonnull-attribute -fsanitize-trap=nonnull-attribute -emit-llvm %s -o - | FileCheck %s + +__attribute__((nonnull)) +void nonnull_arg(int *p) { + (void)p; +} + +void trigger_nonnull_arg() +{ + nonnull_arg(0); +} + +// CHECK: call void @llvm.ubsantrap(i8 16) {{.*}}!dbg [[LOC:![0-9]+]] +// CHECK: [[LOC]] = !DILocation(line: 0, scope: [[MSG:![0-9]+]], {{.+}}) +// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$Undefined Behavior Sanitizer \ No newline at end of file diff --git a/clang/test/CodeGen/ubsan-trap-reason-nonnull-return.c b/clang/test/CodeGen/ubsan-trap-reason-nonnull-return.c new file mode 100644 index 0000000000000..b513957775c86 --- /dev/null +++ b/clang/test/CodeGen/ubsan-trap-reason-nonnull-return.c @@ -0,0 +1,15 @@ +// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -O0 -debug-info-kind=standalone -dwarf-version=5 \ +// RUN: -fsanitize=returns-nonnull-attribute -fsanitize-trap=returns-nonnull-attribute -emit-llvm %s -o - | FileCheck %s + +__attribute__((returns_nonnull)) +int* must_return_nonnull(int bad) +{ + if (bad) + return 0; + static int x = 1; + return &x; +} + +// CHECK: call void @llvm.ubsantrap(i8 17) {{.*}}!dbg [[LOC:![0-9]+]] +// CHECK: [[LOC]] = !DILocation(line: 0, scope: [[MSG:![0-9]+]], {{.+}}) +// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$Undefined Behavior Sanitizer \ No newline at end of file diff --git a/clang/test/CodeGen/ubsan-trap-reason-nullability-arg.c b/clang/test/CodeGen/ubsan-trap-reason-nullability-arg.c new file mode 100644 index 0000000000000..e8012d05e3741 --- /dev/null +++ b/clang/test/CodeGen/ubsan-trap-reason-nullability-arg.c @@ -0,0 +1,18 @@ +// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -O0 -debug-info-kind=standalone -dwarf-version=5 \ +// RUN: -fsanitize=nullability-arg -fsanitize-trap=nullability-arg -emit-llvm %s -o - | FileCheck %s + +#include <stddef.h> + +int nullability_arg(int* _Nonnull p) +{ + return *p; +} + +int trigger_nullability_arg() +{ + return nullability_arg(NULL); +} + +// CHECK: call void @llvm.ubsantrap(i8 14) {{.*}}!dbg [[LOC:![0-9]+]] +// CHECK: [[LOC]] = !DILocation(line: 0, scope: [[MSG:![0-9]+]], {{.+}}) +// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$Undefined Behavior Sanitizer \ No newline at end of file diff --git a/clang/test/CodeGen/ubsan-trap-reason-nullability-return.c b/clang/test/CodeGen/ubsan-trap-reason-nullability-return.c new file mode 100644 index 0000000000000..e5cad805fa968 --- /dev/null +++ b/clang/test/CodeGen/ubsan-trap-reason-nullability-return.c @@ -0,0 +1,18 @@ +// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -O0 -debug-info-kind=standalone -dwarf-version=5 \ +// RUN: -fsanitize=nullability-return -fsanitize-trap=nullability-return -emit-llvm %s -o - | FileCheck %s + +#include <stdbool.h> +#include <stddef.h> + +int* _Nonnull nullability_return(bool fail) +{ + if (fail) + return NULL; + + static int x = 0; + return &x; +} + +// CHECK: call void @llvm.ubsantrap(i8 15) {{.*}}!dbg [[LOC:![0-9]+]] +// CHECK: [[LOC]] = !DILocation(line: 0, scope: [[MSG:![0-9]+]], {{.+}}) +// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$Undefined Behavior Sanitizer \ No newline at end of file diff --git a/clang/test/CodeGen/ubsan-trap-reason-out-of-bounds.c b/clang/test/CodeGen/ubsan-trap-reason-out-of-bounds.c new file mode 100644 index 0000000000000..afaeed4193907 --- /dev/null +++ b/clang/test/CodeGen/ubsan-trap-reason-out-of-bounds.c @@ -0,0 +1,12 @@ +// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -O0 -debug-info-kind=standalone -dwarf-version=5 \ +// RUN: -fsanitize=array-bounds -fsanitize-trap=array-bounds -emit-llvm %s -o - | FileCheck %s + +int out_of_bounds() +{ + int a[1] = {0}; + return a[1]; +} + +// CHECK: call void @llvm.ubsantrap(i8 18) {{.*}}!dbg [[LOC:![0-9]+]] +// CHECK: [[LOC]] = !DILocation(line: 0, scope: [[MSG:![0-9]+]], {{.+}}) +// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$Undefined Behavior Sanitizer \ No newline at end of file diff --git a/clang/test/CodeGen/ubsan-trap-reason-pointer-overflow.c b/clang/test/CodeGen/ubsan-trap-reason-pointer-overflow.c new file mode 100644 index 0000000000000..f219134020eb7 --- /dev/null +++ b/clang/test/CodeGen/ubsan-trap-reason-pointer-overflow.c @@ -0,0 +1,16 @@ +// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -O0 -debug-info-kind=standalone -dwarf-version=5 \ +// RUN: -fsanitize=pointer-overflow -fsanitize-trap=pointer-overflow -emit-llvm %s -o - | FileCheck %s + +#include <stddef.h> +#include <stdint.h> + +int* pointer_overflow(void) +{ + int buf[4]; + volatile size_t n = (SIZE_MAX / sizeof(int)) - 1; + return buf + n; +} + +// CHECK: call void @llvm.ubsantrap(i8 19) {{.*}}!dbg [[LOC:![0-9]+]] +// CHECK: [[LOC]] = !DILocation(line: 0, scope: [[MSG:![0-9]+]], {{.+}}) +// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$Undefined Behavior Sanitizer \ No newline at end of file diff --git a/clang/test/CodeGen/ubsan-trap-reason-shift-out-of-bounds.c b/clang/test/CodeGen/ubsan-trap-reason-shift-out-of-bounds.c new file mode 100644 index 0000000000000..287f4d500922f --- /dev/null +++ b/clang/test/CodeGen/ubsan-trap-reason-shift-out-of-bounds.c @@ -0,0 +1,12 @@ +// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -O0 -debug-info-kind=standalone -dwarf-version=5 \ +// RUN: -fsanitize=shift-base -fsanitize-trap=shift-base -emit-llvm %s -o - | FileCheck %s + +int shift_out_of_bounds() +{ + int sh = 32; + return 1 << sh; +} + +// CHECK: call void @llvm.ubsantrap(i8 20) {{.*}}!dbg [[LOC:![0-9]+]] +// CHECK: [[LOC]] = !DILocation(line: 0, scope: [[MSG:![0-9]+]], {{.+}}) +// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$Undefined Behavior Sanitizer \ No newline at end of file diff --git a/clang/test/CodeGen/ubsan-trap-reason-sub-overflow.c b/clang/test/CodeGen/ubsan-trap-reason-sub-overflow.c new file mode 100644 index 0000000000000..76e59996997f5 --- /dev/null +++ b/clang/test/CodeGen/ubsan-trap-reason-sub-overflow.c @@ -0,0 +1,10 @@ +// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -O0 -debug-info-kind=standalone -dwarf-version=5 \ +// RUN: -fsanitize=signed-integer-overflow -fsanitize-trap=signed-integer-overflow -emit-llvm %s -o - | FileCheck %s + +int sub_overflow(int a, int b) { + return a - b; +} + +// CHECK: call void @llvm.ubsantrap(i8 21) {{.*}}!dbg [[LOC:![0-9]+]] +// CHECK: [[LOC]] = !DILocation(line: 0, scope: [[MSG:![0-9]+]], {{.+}}) +// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$Undefined Behavior Sanitizer \ No newline at end of file diff --git a/clang/test/CodeGen/ubsan-trap-reason-type-mismatch.c b/clang/test/CodeGen/ubsan-trap-reason-type-mismatch.c new file mode 100644 index 0000000000000..6a9f755f4e485 --- /dev/null +++ b/clang/test/CodeGen/ubsan-trap-reason-type-mismatch.c @@ -0,0 +1,11 @@ +// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -O0 -debug-info-kind=standalone -dwarf-version=5 \ +// RUN: -fsanitize=alignment -fsanitize-trap=alignment -emit-llvm %s -o - | FileCheck %s + +int type_mismatch(int *p) +{ + return *p; +} + +// CHECK: call void @llvm.ubsantrap(i8 22) {{.*}}!dbg [[LOC:![0-9]+]] +// CHECK: [[LOC]] = !DILocation(line: 0, scope: [[MSG:![0-9]+]], {{.+}}) +// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$Undefined Behavior Sanitizer \ No newline at end of file diff --git a/clang/test/CodeGen/ubsan-trap-reason-vla-bound-not-positive.c b/clang/test/CodeGen/ubsan-trap-reason-vla-bound-not-positive.c new file mode 100644 index 0000000000000..23fea161feea4 --- /dev/null +++ b/clang/test/CodeGen/ubsan-trap-reason-vla-bound-not-positive.c @@ -0,0 +1,14 @@ +// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -O0 -debug-info-kind=standalone -dwarf-version=5 \ +// RUN: -fsanitize=vla-bound -fsanitize-trap=vla-bound -emit-llvm %s -o - | FileCheck %s + +int n = 0; + +int vla_bound_not_positive() +{ + int a[n]; + return sizeof a; +} + +// CHECK: call void @llvm.ubsantrap(i8 24) {{.*}}!dbg [[LOC:![0-9]+]] +// CHECK: [[LOC]] = !DILocation(line: 0, scope: [[MSG:![0-9]+]], {{.+}}) +// CHECK: distinct !DISubprogram(name: "__clang_trap_msg$Undefined Behavior Sanitizer \ No newline at end of file >From 5a25319074bb345a62d787f81f5a9c7ae5097583 Mon Sep 17 00:00:00 2001 From: Anthony Tran <anthonyt...@anthonys-air.lan> Date: Thu, 26 Jun 2025 13:44:43 -0700 Subject: [PATCH 2/2] Resolve leftover conflict markers --- clang/lib/CodeGen/CGExpr.cpp | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp index 34dba66edfac1..34fd8b4aef0f2 100644 --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -85,8 +85,6 @@ enum VariableTypeDescriptorKind : uint16_t { // Miscellaneous Helper Methods //===--------------------------------------------------------------------===// -<<<<<<< HEAD -======= static llvm::StringRef GetUBSanTrapForHandler(SanitizerHandler ID) { switch (ID) { case SanitizerHandler::AddOverflow: @@ -175,7 +173,6 @@ static llvm::StringRef GetUBSanTrapForHandler(SanitizerHandler ID) { } } ->>>>>>> 592f9d2f8f85 (Addressed most of Dan's comments and added remaining test cases) /// CreateTempAlloca - This creates a alloca and inserts it into the entry /// block. RawAddress @@ -4142,8 +4139,6 @@ void CodeGenFunction::EmitTrapCheck(llvm::Value *Checked, llvm::BasicBlock *&TrapBB = TrapBBs[CheckHandlerID]; -<<<<<<< HEAD -======= llvm::DILocation *TrapLocation = Builder.getCurrentDebugLocation(); llvm::StringRef TrapMessage = GetUBSanTrapForHandler(CheckHandlerID); @@ -4152,7 +4147,6 @@ void CodeGenFunction::EmitTrapCheck(llvm::Value *Checked, TrapLocation, "Undefined Behavior Sanitizer", TrapMessage); } ->>>>>>> 592f9d2f8f85 (Addressed most of Dan's comments and added remaining test cases) NoMerge = NoMerge || !CGM.getCodeGenOpts().OptimizationLevel || (CurCodeDecl && CurCodeDecl->hasAttr<OptimizeNoneAttr>()); @@ -4161,13 +4155,8 @@ void CodeGenFunction::EmitTrapCheck(llvm::Value *Checked, auto Call = TrapBB->begin(); assert(isa<llvm::CallInst>(Call) && "Expected call in trap BB"); -<<<<<<< HEAD - Call->applyMergedLocation(Call->getDebugLoc(), - Builder.getCurrentDebugLocation()); -======= Call->applyMergedLocation(Call->getDebugLoc(), TrapLocation); ->>>>>>> 592f9d2f8f85 (Addressed most of Dan's comments and added remaining test cases) Builder.CreateCondBr(Checked, Cont, TrapBB, MDHelper.createLikelyBranchWeights()); } else { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits