llvm-project/clang/test/CodeGenCXX/sanitize-trap-loop.cpp
Peter Collingbourne a544d3792c
CodeGen, Driver: Add -fsanitize-trap-loop option.
This option may be used to opt into infinite loops for failed UBSan and
CFI checks. It causes Clang to generate an llvm.cond.loop intrinsic call
instead of a conditional branch to a trap instruction when generating
code for a conditional trap.

Part of this RFC:
https://discourse.llvm.org/t/rfc-optimizing-conditional-traps/89456

Reviewers: fmayer, vitalybuka

Reviewed By: vitalybuka, fmayer

Pull Request: https://github.com/llvm/llvm-project/pull/177688
2026-02-06 17:12:53 -08:00

21 lines
813 B
C++

// RUN: %clang_cc1 -flto -flto-unit -triple x86_64-unknown-linux -fvisibility=hidden -fsanitize=cfi-vcall,signed-integer-overflow -fsanitize-trap=cfi-vcall,signed-integer-overflow -fsanitize-trap-loop -emit-llvm -o - %s | FileCheck %s
struct A {
virtual void f();
};
void vcall(A *a) {
// CHECK: [[TEST:%.*]] = call i1 @llvm.type.test
// CHECK-NEXT: [[NOT:%.*]] = xor i1 [[TEST]], true
// CHECK-NEXT: call void @llvm.cond.loop(i1 [[NOT]])
a->f();
}
int overflow(int a, int b) {
// CHECK: [[OVERFLOW:%.*]] = extractvalue { i32, i1 } %2, 1, !nosanitize
// CHECK-NEXT: [[NOTOVERFLOW:%.*]] = xor i1 [[OVERFLOW]], true, !nosanitize
// CHECK-NEXT: [[NOTNOTOVERFLOW:%.*]] = xor i1 [[NOTOVERFLOW]], true, !nosanitize
// CHECK-NEXT: call void @llvm.cond.loop(i1 [[NOTNOTOVERFLOW]])
return a + b;
}