Skip to content

Commit 2405d94

Browse files
committed
[NFC][BoundsSafety] Adopt TrapReason in CodeGenFunction::EmitBoundsSafetyTrapCheck
This is the first step in adopting the new trap reasons infrastructure. This patch introduces the `trap_bs_fallback` diagnostic and this just uses the existing infrastructure for computing trap reason reasons. Thus there is no user visible change in behavior with this patch. Future patches will start introducing new diagnostics so more specific trap reason messages can be created. rdar://158623471 (cherry picked from commit 7159768)
1 parent 9b3edeb commit 2405d94

File tree

4 files changed

+51
-22
lines changed

4 files changed

+51
-22
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//==--- DiagnosticBoundsSafetyTrapKinds.td --------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
// These are the trap diagnostics used by -fbounds-safety
9+
//===----------------------------------------------------------------------===//
10+
11+
let Component = "Trap" in {
12+
let CategoryName = "Bounds check failed" in {
13+
14+
// Used for falling back to the legacy infrastructure for constructing
15+
// trap reason strings.
16+
def trap_bs_fallback : Trap<"%0">;
17+
18+
}
19+
}

clang/include/clang/Basic/DiagnosticTrapKinds.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@ def trap_ubsan_arith_overflow : Trap<
2828

2929
}
3030
}
31+
32+
// TO_UPSTREAM(BoundsSafety) ON
33+
include "DiagnosticBoundsSafetyTrapKinds.td"
34+
// TO_UPSTREAM(BoundsSafety) OFF

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "clang/AST/StmtVisitor.h"
3535
#include "clang/Basic/Builtins.h"
3636
#include "clang/Basic/CodeGenOptions.h"
37+
#include "clang/Basic/DiagnosticTrap.h" // TO_UPSTREAM(BoundsSafety)
3738
#include "clang/Basic/Module.h"
3839
#include "clang/Basic/SourceManager.h"
3940
#include "llvm/ADT/STLExtras.h"
@@ -4577,8 +4578,7 @@ void CodeGenFunction::EmitUnreachable(SourceLocation Loc) {
45774578
void CodeGenFunction::EmitTrapCheck(llvm::Value *Checked,
45784579
SanitizerHandler CheckHandlerID,
45794580
bool NoMerge, const TrapReason *TR,
4580-
StringRef Annotation,
4581-
StringRef BoundsSafetyTrapMessage) {
4581+
StringRef Annotation) {
45824582
llvm::BasicBlock *Cont = createBasicBlock("cont");
45834583

45844584
// If we're optimizing, collapse all calls to trap down to just one per
@@ -4592,26 +4592,24 @@ void CodeGenFunction::EmitTrapCheck(llvm::Value *Checked,
45924592
llvm::StringRef TrapMessage;
45934593
llvm::StringRef TrapCategory;
45944594
auto DebugTrapReasonKind = CGM.getCodeGenOpts().getSanitizeDebugTrapReasons();
4595+
4596+
/* TO_UPSTREAM(BoundsSafety) ON*/
45954597
if (TR && !TR->isEmpty() &&
4596-
DebugTrapReasonKind ==
4597-
CodeGenOptions::SanitizeDebugTrapReasonKind::Detailed) {
4598+
(DebugTrapReasonKind ==
4599+
CodeGenOptions::SanitizeDebugTrapReasonKind::Detailed ||
4600+
CheckHandlerID == SanitizerHandler::BoundsSafety)) {
45984601
TrapMessage = TR->getMessage();
45994602
TrapCategory = TR->getCategory();
46004603
} else {
4601-
/* TO_UPSTREAM(BoundsSafety) ON*/
4602-
// FIXME: Move to using `TrapReason` (rdar://158623471).
4603-
if (CheckHandlerID == SanitizerHandler::BoundsSafety) {
4604-
TrapMessage = BoundsSafetyTrapMessage;
4605-
TrapCategory = GetBoundsSafetyTrapMessagePrefix();
4606-
} else {
4607-
TrapMessage = GetUBSanTrapForHandler(CheckHandlerID);
4608-
TrapCategory = "Undefined Behavior Sanitizer";
4609-
}
4604+
assert(CheckHandlerID != SanitizerHandler::BoundsSafety);
4605+
TrapMessage = GetUBSanTrapForHandler(CheckHandlerID);
4606+
TrapCategory = "Undefined Behavior Sanitizer";
46104607
}
46114608

46124609
if (getDebugInfo() && !(TrapMessage.empty() && TrapCategory.empty()) &&
4613-
DebugTrapReasonKind !=
4614-
CodeGenOptions::SanitizeDebugTrapReasonKind::None &&
4610+
(DebugTrapReasonKind !=
4611+
CodeGenOptions::SanitizeDebugTrapReasonKind::None ||
4612+
CheckHandlerID == SanitizerHandler::BoundsSafety) &&
46154613
TrapLocation) {
46164614
TrapLocation = getDebugInfo()->CreateTrapFailureMessageFor(
46174615
TrapLocation, TrapCategory, TrapMessage);
@@ -4701,19 +4699,26 @@ void CodeGenFunction::EmitTrapCheck(llvm::Value *Checked,
47014699
EmitBlock(Cont);
47024700
}
47034701

4704-
void CodeGenFunction::EmitBoundsSafetyTrapCheck(llvm::Value *Checked,
4705-
BoundsSafetyTrapKind kind,
4706-
BoundsSafetyTrapCtx::Kind TrapCtx) {
4702+
void CodeGenFunction::EmitBoundsSafetyTrapCheck(
4703+
llvm::Value *Checked, BoundsSafetyTrapKind kind,
4704+
BoundsSafetyTrapCtx::Kind TrapCtx, TrapReason *TR) {
47074705
auto OptRemark = GetBoundsSafetyOptRemarkForTrap(kind);
47084706
assert(BoundsSafetyOptRemarkScope::InScope(this, OptRemark));
47094707

4708+
// Fallback: If a TrapReason object isn't provided use the legacy approach
4709+
// for constructing
4710+
TrapReason TempTR;
4711+
if (!TR) {
4712+
CGM.BuildTrapReason(diag::trap_bs_fallback, TempTR)
4713+
<< GetBoundsSafetyTrapMessageSuffix(kind, TrapCtx);
4714+
}
4715+
47104716
// We still need to pass `OptRemark` because not all emitted instructions
47114717
// can be covered by BoundsSafetyOptRemarkScope. This is because EmitTrapCheck
47124718
// caches basic blocks that contain instructions that need annotating.
47134719
EmitTrapCheck(Checked, SanitizerHandler::BoundsSafety,
47144720
/*NoMerge=*/CGM.getCodeGenOpts().BoundsSafetyUniqueTraps,
4715-
/*TR=*/nullptr, GetBoundsSafetyOptRemarkString(OptRemark),
4716-
GetBoundsSafetyTrapMessageSuffix(kind, TrapCtx));
4721+
TR ? TR : &TempTR, GetBoundsSafetyOptRemarkString(OptRemark));
47174722
}
47184723

47194724
llvm::CallInst *CodeGenFunction::EmitTrapCall(llvm::Intrinsic::ID IntrID) {

clang/lib/CodeGen/CodeGenFunction.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5408,7 +5408,7 @@ class CodeGenFunction : public CodeGenTypeCache {
54085408
/// conditional branch to it, for the -ftrapv checks.
54095409
void EmitTrapCheck(llvm::Value *Checked, SanitizerHandler CheckHandlerID,
54105410
bool NoMerge = false, const TrapReason *TR = nullptr,
5411-
StringRef Annotation = "", StringRef TrapMessage = "");
5411+
StringRef Annotation = "");
54125412

54135413
/* TO_UPSTREAM(BoundsSafety) ON*/
54145414
/// Create a basic block that will call the trap intrinsic for -fbounds-safety, and
@@ -5418,7 +5418,8 @@ class CodeGenFunction : public CodeGenTypeCache {
54185418
/// must be in scope when this method is called.
54195419
void EmitBoundsSafetyTrapCheck(
54205420
llvm::Value *Checked, BoundsSafetyTrapKind kind,
5421-
BoundsSafetyTrapCtx::Kind TrapCtx = BoundsSafetyTrapCtx::UNKNOWN);
5421+
BoundsSafetyTrapCtx::Kind TrapCtx = BoundsSafetyTrapCtx::UNKNOWN,
5422+
TrapReason *TR = nullptr);
54225423
/* TO_UPSTREAM(BoundsSafety) OFF*/
54235424

54245425
/// Emit a call to trap or debugtrap and attach function attribute

0 commit comments

Comments
 (0)