Skip to content

Commit 678cd8e

Browse files
committed
Add support for builtin_verbose_trap
The builtin causes the program to stop its execution abnormally and shows a human-readable description of the reason for the termination when a debugger is attached or in a symbolicated crash log. The motivation for the builtin is explained in the following RFC: https://discourse.llvm.org/t/rfc-adding-builtin-verbose-trap-string-literal/75845
1 parent e99edf6 commit 678cd8e

File tree

11 files changed

+249
-3
lines changed

11 files changed

+249
-3
lines changed

clang/docs/LanguageExtensions.rst

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3379,6 +3379,54 @@ Query for this feature with ``__has_builtin(__builtin_debugtrap)``.
33793379
33803380
Query for this feature with ``__has_builtin(__builtin_trap)``.
33813381
3382+
``__builtin_verbose_trap``
3383+
------------------
3384+
3385+
``__builtin_verbose_trap`` causes the program to stop its execution abnormally
3386+
and shows a human-readable description of the reason for the termination when a
3387+
debugger is attached or in a symbolicated crash log.
3388+
3389+
**Syntax**:
3390+
3391+
.. code-block:: c++
3392+
3393+
__builtin_verbose_trap(const char *reason)
3394+
3395+
**Description**
3396+
3397+
``__builtin_verbose_trap`` is lowered to the ` ``llvm.trap`` <https://llvm.org/docs/LangRef.html#llvm-trap-intrinsic>`_ builtin.
3398+
Additionally, clang emits debug metadata that represents an artificial inline
3399+
frame whose name encodes the string passed to the builtin, prefixed by a "magic"
3400+
prefix.
3401+
3402+
For example, consider the following code:
3403+
3404+
.. code-block:: c++
3405+
3406+
void foo(int* p) {
3407+
if (p == nullptr)
3408+
__builtin_verbose_trap("Argument_must_not_be_null");
3409+
}
3410+
3411+
The debug metadata would look as if it were produced for the following code:
3412+
3413+
.. code-block:: c++
3414+
3415+
__attribute__((always_inline))
3416+
inline void "__llvm_verbose_trap: Argument_must_not_be_null"() {
3417+
__builtin_trap();
3418+
}
3419+
3420+
void foo(int* p) {
3421+
if (p == nullptr)
3422+
"__llvm_verbose_trap: Argument_must_not_be_null"();
3423+
}
3424+
3425+
However, the LLVM IR would not actually contain a call to the artificial
3426+
function — it only exists in the debug metadata.
3427+
3428+
Query for this feature with ``__has_builtin(__builtin_verbose_trap)``.
3429+
33823430
``__builtin_nondeterministic_value``
33833431
------------------------------------
33843432

clang/include/clang/AST/Expr.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,11 @@ class Expr : public ValueStmt {
775775
const Expr *PtrExpression, ASTContext &Ctx,
776776
EvalResult &Status) const;
777777

778+
/// If the current Expr can be evaluated to a pointer to a null-terminated
779+
/// constant string, return the constant string (without the terminating null)
780+
/// in Result. Return true if it succeeds.
781+
bool tryEvaluateString(std::string &Result, ASTContext &Ctx) const;
782+
778783
/// Enumeration used to describe the kind of Null pointer constant
779784
/// returned from \c isNullPointerConstant().
780785
enum NullPointerConstantKind {

clang/include/clang/Basic/Builtins.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,12 @@ def Trap : Builtin {
10961096
let Prototype = "void()";
10971097
}
10981098

1099+
def VerboseTrap : Builtin {
1100+
let Spellings = ["__builtin_verbose_trap"];
1101+
let Attributes = [NoThrow, NoReturn];
1102+
let Prototype = "void(char const*)";
1103+
}
1104+
10991105
def Debugtrap : Builtin {
11001106
let Spellings = ["__builtin_debugtrap"];
11011107
let Attributes = [NoThrow];

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8794,6 +8794,8 @@ def err_expected_callable_argument : Error<
87948794
"expected a callable expression as %ordinal0 argument to %1, found %2">;
87958795
def note_building_builtin_dump_struct_call : Note<
87968796
"in call to printing function with arguments '(%0)' while dumping struct">;
8797+
def err_builtin_verbose_trap_arg : Error<
8798+
"argument to __builtin_verbose_trap must be a non-empty string literal">;
87978799

87988800
def err_atomic_load_store_uses_lib : Error<
87998801
"atomic %select{load|store}0 requires runtime support that is not "

clang/lib/AST/ExprConstant.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1880,7 +1880,8 @@ static bool EvaluateAtomic(const Expr *E, const LValue *This, APValue &Result,
18801880
EvalInfo &Info);
18811881
static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result);
18821882
static bool EvaluateBuiltinStrLen(const Expr *E, uint64_t &Result,
1883-
EvalInfo &Info);
1883+
EvalInfo &Info,
1884+
std::string *StringResult = nullptr);
18841885

18851886
/// Evaluate an integer or fixed point expression into an APResult.
18861887
static bool EvaluateFixedPointOrInteger(const Expr *E, APFixedPoint &Result,
@@ -16625,7 +16626,7 @@ bool Expr::tryEvaluateObjectSize(uint64_t &Result, ASTContext &Ctx,
1662516626
}
1662616627

1662716628
static bool EvaluateBuiltinStrLen(const Expr *E, uint64_t &Result,
16628-
EvalInfo &Info) {
16629+
EvalInfo &Info, std::string *StringResult) {
1662916630
if (!E->getType()->hasPointerRepresentation() || !E->isPRValue())
1663016631
return false;
1663116632

@@ -16652,6 +16653,8 @@ static bool EvaluateBuiltinStrLen(const Expr *E, uint64_t &Result,
1665216653
Str = Str.substr(0, Pos);
1665316654

1665416655
Result = Str.size();
16656+
if (StringResult)
16657+
*StringResult = Str;
1665516658
return true;
1665616659
}
1665716660

@@ -16667,12 +16670,21 @@ static bool EvaluateBuiltinStrLen(const Expr *E, uint64_t &Result,
1666716670
if (!Char.getInt()) {
1666816671
Result = Strlen;
1666916672
return true;
16670-
}
16673+
} else if (StringResult)
16674+
StringResult->push_back(Char.getInt().getExtValue());
1667116675
if (!HandleLValueArrayAdjustment(Info, E, String, CharTy, 1))
1667216676
return false;
1667316677
}
1667416678
}
1667516679

16680+
bool Expr::tryEvaluateString(std::string &StringResult, ASTContext &Ctx) const {
16681+
Expr::EvalStatus Status;
16682+
EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantFold);
16683+
uint64_t Result;
16684+
16685+
return EvaluateBuiltinStrLen(this, Result, Info, &StringResult);
16686+
}
16687+
1667616688
bool Expr::EvaluateCharRangeAsString(std::string &Result,
1667716689
const Expr *SizeExpression,
1667816690
const Expr *PtrExpression, ASTContext &Ctx,

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3452,6 +3452,18 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
34523452
case Builtin::BI__builtin_trap:
34533453
EmitTrapCall(Intrinsic::trap);
34543454
return RValue::get(nullptr);
3455+
case Builtin::BI__builtin_verbose_trap: {
3456+
llvm::DILocation *TrapLocation = Builder.getCurrentDebugLocation();
3457+
if (getDebugInfo()) {
3458+
std::string Str;
3459+
E->getArg(0)->tryEvaluateString(Str, getContext());
3460+
TrapLocation = getDebugInfo()->CreateTrapFailureMessageFor(
3461+
TrapLocation, "__llvm_verbose_trap", Str);
3462+
}
3463+
ApplyDebugLocation ApplyTrapDI(*this, TrapLocation);
3464+
EmitTrapCall(Intrinsic::trap);
3465+
return RValue::get(nullptr);
3466+
}
34553467
case Builtin::BI__debugbreak:
34563468
EmitTrapCall(Intrinsic::debugtrap);
34573469
return RValue::get(nullptr);

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,6 +1628,27 @@ llvm::DIType *CGDebugInfo::createFieldType(
16281628
offsetInBits, flags, debugType, Annotations);
16291629
}
16301630

1631+
llvm::DISubprogram *
1632+
CGDebugInfo::getFakeFuncSubprogram(const std::string &FakeFuncName) {
1633+
llvm::DISubprogram *&SP = FakeFuncMap[FakeFuncName];
1634+
1635+
if (!SP) {
1636+
auto FileScope = TheCU->getFile();
1637+
llvm::DISubroutineType *DIFnTy = DBuilder.createSubroutineType(nullptr);
1638+
// Note: We use `FileScope` rather than `TheCU` as the scope because that's
1639+
// what LLVM's inliner seems to do.
1640+
SP = DBuilder.createFunction(
1641+
/*Scope=*/FileScope, /*Name=*/FakeFuncName, /*LinkageName=*/StringRef(),
1642+
/*File=*/FileScope, /*LineNo=*/0, /*Ty=*/DIFnTy,
1643+
/*ScopeLine=*/0,
1644+
/*Flags=*/llvm::DINode::FlagArtificial,
1645+
/*SPFlags=*/llvm::DISubprogram::SPFlagDefinition,
1646+
/*TParams=*/nullptr, /*ThrownTypes=*/nullptr, /*Annotations=*/nullptr);
1647+
}
1648+
1649+
return SP;
1650+
}
1651+
16311652
void CGDebugInfo::CollectRecordLambdaFields(
16321653
const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
16331654
llvm::DIType *RecordTy) {
@@ -3424,6 +3445,27 @@ llvm::DIMacroFile *CGDebugInfo::CreateTempMacroFile(llvm::DIMacroFile *Parent,
34243445
return DBuilder.createTempMacroFile(Parent, Line, FName);
34253446
}
34263447

3448+
llvm::DILocation *CGDebugInfo::CreateTrapFailureMessageFor(
3449+
llvm::DebugLoc TrapLocation, StringRef Prefix, StringRef FailureMsg) {
3450+
// Create debug info that describes a fake function whose name is the failure
3451+
// message.
3452+
std::string FuncName(Prefix);
3453+
if (!FailureMsg.empty()) {
3454+
// A space in the function name identifies this as not being a real function
3455+
// because it's not a valid symbol name.
3456+
FuncName += ": ";
3457+
FuncName += FailureMsg;
3458+
}
3459+
3460+
assert(FuncName.size() > 0);
3461+
assert(FuncName.find(' ') != std::string::npos &&
3462+
"Fake function name must contain a space");
3463+
3464+
llvm::DISubprogram *TrapSP = getFakeFuncSubprogram(FuncName);
3465+
return llvm::DILocation::get(CGM.getLLVMContext(), /*Line=*/0, /*Column=*/0,
3466+
/*Scope=*/TrapSP, /*InlinedAt=*/TrapLocation);
3467+
}
3468+
34273469
static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
34283470
Qualifiers Quals;
34293471
do {

clang/lib/CodeGen/CGDebugInfo.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,14 @@ class CGDebugInfo {
346346
const FieldDecl *BitFieldDecl, const llvm::DIDerivedType *BitFieldDI,
347347
llvm::ArrayRef<llvm::Metadata *> PreviousFieldsDI, const RecordDecl *RD);
348348

349+
// A cache that maps fake function names used for __builtin_verbose_trap to
350+
// subprograms.
351+
std::map<std::string, llvm::DISubprogram *> FakeFuncMap;
352+
353+
// A function that returns the subprogram corresponding to the fake function
354+
// name.
355+
llvm::DISubprogram *getFakeFuncSubprogram(const std::string &FakeFuncName);
356+
349357
/// Helpers for collecting fields of a record.
350358
/// @{
351359
void CollectRecordLambdaFields(const CXXRecordDecl *CXXDecl,
@@ -602,6 +610,18 @@ class CGDebugInfo {
602610
return CoroutineParameterMappings;
603611
}
604612

613+
// Create a debug location from `TrapLocation` that adds a fake
614+
// inline frame where the frame name is
615+
//
616+
// * `<Prefix>: <FailureMsg>` if `<FailureMsg>` is not empty.
617+
// * `<Prefix>` if `<FailureMsg>` is empty. Note `<Prefix>` must
618+
// contain a space.
619+
//
620+
// This is used to store failure reasons for traps.
621+
llvm::DILocation *CreateTrapFailureMessageFor(llvm::DebugLoc TrapLocation,
622+
StringRef Prefix,
623+
StringRef FailureMsg);
624+
605625
private:
606626
/// Emit call to llvm.dbg.declare for a variable declaration.
607627
/// Returns a pointer to the DILocalVariable associated with the

clang/lib/Sema/SemaChecking.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,23 @@ static bool checkArgCount(Sema &S, CallExpr *Call, unsigned DesiredArgCount) {
171171
<< /*is non object*/ 0 << Call->getArg(1)->getSourceRange();
172172
}
173173

174+
static bool checkBuiltinVerboseTrap(CallExpr *Call, Sema &S) {
175+
Expr *Arg = Call->getArg(0);
176+
177+
if (Arg->isValueDependent())
178+
return true;
179+
180+
// FIXME: Add more checks and reject strings that can't be handled by
181+
// debuggers.
182+
std::string Result;
183+
if (!Arg->tryEvaluateString(Result, S.Context) || Result.empty()) {
184+
S.Diag(Arg->getBeginLoc(), diag::err_builtin_verbose_trap_arg)
185+
<< Arg->getSourceRange();
186+
return false;
187+
}
188+
return true;
189+
}
190+
174191
static bool convertArgumentToType(Sema &S, Expr *&Value, QualType Ty) {
175192
if (Value->isTypeDependent())
176193
return false;
@@ -2882,6 +2899,11 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID,
28822899
case Builtin::BI__builtin_matrix_column_major_store:
28832900
return SemaBuiltinMatrixColumnMajorStore(TheCall, TheCallResult);
28842901

2902+
case Builtin::BI__builtin_verbose_trap:
2903+
if (!checkBuiltinVerboseTrap(TheCall, *this))
2904+
return ExprError();
2905+
break;
2906+
28852907
case Builtin::BI__builtin_get_device_side_mangled_name: {
28862908
auto Check = [](CallExpr *TheCall) {
28872909
if (TheCall->getNumArgs() != 1)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// RUN: %clang_cc1 -std=c++20 -emit-llvm -debug-info-kind=limited %s -o - | FileCheck %s
2+
3+
// CHECK-LABEL: define void @_Z2f0v()
4+
// CHECK: call void @llvm.trap(), !dbg ![[LOC17:.*]]
5+
6+
// CHECK-LABEL: define void @_Z2f1v()
7+
// CHECK: call void @llvm.trap(), !dbg ![[LOC23:.*]]
8+
// CHECK: call void @llvm.trap(), !dbg ![[LOC25:.*]]
9+
10+
// CHECK-LABEL: define void @_Z2f3v()
11+
// CHECK: call void @_Z2f2IXadsoKcL_ZL8constMsgEEEEvv()
12+
13+
// CHECK-LABEL: define internal void @_Z2f2IXadsoKcL_ZL8constMsgEEEEvv()
14+
// CHECK: call void @llvm.trap(), !dbg ![[LOC36:.*]]
15+
16+
// CHECK: ![[FILESCOPE:.*]] = !DIFile(filename:
17+
// CHECK: ![[SUBPROG14:.*]] = distinct !DISubprogram(name: "f0", linkageName: "_Z2f0v",
18+
// CHECK: ![[LOC17]] = !DILocation(line: 0, scope: ![[SUBPROG18:.*]], inlinedAt: ![[LOC20:.*]])
19+
// CHECK: ![[SUBPROG18]] = distinct !DISubprogram(name: "__llvm_verbose_trap: Argument_must_not_be_null", scope: ![[FILESCOPE]], file: ![[FILESCOPE]], type: !{{.*}}, flags: DIFlagArtificial, spFlags: DISPFlagDefinition, unit: !{{.*}})
20+
// CHECK: ![[LOC20]] = !DILocation(line: 34, column: 3, scope: ![[SUBPROG14]])
21+
// CHECK: ![[SUBPROG22:.*]] = distinct !DISubprogram(name: "f1", linkageName: "_Z2f1v",
22+
// CHECK: ![[LOC23]] = !DILocation(line: 0, scope: ![[SUBPROG18]], inlinedAt: ![[LOC24:.*]])
23+
// CHECK: ![[LOC24]] = !DILocation(line: 38, column: 3, scope: ![[SUBPROG22]])
24+
// CHECK: ![[LOC25]] = !DILocation(line: 0, scope: ![[SUBPROG26:.*]], inlinedAt: ![[LOC27:.*]])
25+
// CHECK: ![[SUBPROG26]] = distinct !DISubprogram(name: "__llvm_verbose_trap: hello", scope: ![[FILESCOPE]], file: ![[FILESCOPE]], type: !{{.*}}, flags: DIFlagArtificial, spFlags: DISPFlagDefinition, unit: !{{.*}})
26+
// CHECK: ![[LOC27]] = !DILocation(line: 39, column: 3, scope: ![[SUBPROG22]])
27+
// CHECK: ![[SUBPROG32:.*]] = distinct !DISubprogram(name: "f2<&constMsg[0]>", linkageName: "_Z2f2IXadsoKcL_ZL8constMsgEEEEvv",
28+
// CHECK: ![[LOC36]] = !DILocation(line: 0, scope: ![[SUBPROG26]], inlinedAt: ![[LOC37:.*]])
29+
// CHECK: ![[LOC37]] = !DILocation(line: 44, column: 3, scope: ![[SUBPROG32]])
30+
31+
char const constMsg[] = "hello";
32+
33+
void f0() {
34+
__builtin_verbose_trap("Argument_must_not_be_null");
35+
}
36+
37+
void f1() {
38+
__builtin_verbose_trap("Argument_must_not_be_null");
39+
__builtin_verbose_trap("hello");
40+
}
41+
42+
template <const char * const str>
43+
void f2() {
44+
__builtin_verbose_trap(str);
45+
}
46+
47+
void f3() {
48+
f2<constMsg>();
49+
}

clang/test/SemaCXX/verbose-trap.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// RUN: %clang_cc1 -std=c++11 -fsyntax-only -fcxx-exceptions -verify %s
2+
3+
#if !__has_builtin(__builtin_verbose_trap)
4+
#error
5+
#endif
6+
7+
constexpr char const* constMsg1 = "hello";
8+
char const* const constMsg2 = "hello";
9+
char const constMsg3[] = "hello";
10+
11+
template <const char * const str>
12+
void f(const char * arg) {
13+
__builtin_verbose_trap("Argument_must_not_be_null");
14+
__builtin_verbose_trap("hello" "world");
15+
__builtin_verbose_trap(constMsg1);
16+
__builtin_verbose_trap(constMsg2);
17+
__builtin_verbose_trap(""); // expected-error {{argument to __builtin_verbose_trap must be a non-empty string literal}}
18+
__builtin_verbose_trap(); // expected-error {{too few arguments}}
19+
__builtin_verbose_trap(0); // expected-error {{argument to __builtin_verbose_trap must be a non-empty string literal}}
20+
__builtin_verbose_trap(1); // expected-error {{cannot initialize a parameter of type 'const char *' with}}
21+
__builtin_verbose_trap(arg); // expected-error {{argument to __builtin_verbose_trap must be a non-empty string literal}}
22+
__builtin_verbose_trap(str);
23+
__builtin_verbose_trap(u8"hello");
24+
}
25+
26+
void test() {
27+
f<constMsg3>(nullptr);
28+
}

0 commit comments

Comments
 (0)