Skip to content

Commit 296aa96

Browse files
author
Robin Kruppe
committed
[rustllvm] Use report_fatal_error over llvm_unreachable
This makes it more robust when assertions are disabled, crashing instead of causing UB. Also introduces a tidy check to enforce this rule, which in turn necessitated making tidy run on src/rustllvm. Fixes #44020
1 parent 26e881d commit 296aa96

File tree

5 files changed

+37
-30
lines changed

5 files changed

+37
-30
lines changed

src/rustllvm/ArchiveWrapper.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static Archive::Kind fromRust(LLVMRustArchiveKind Kind) {
6666
case LLVMRustArchiveKind::COFF:
6767
return Archive::K_COFF;
6868
default:
69-
llvm_unreachable("Bad ArchiveKind.");
69+
report_fatal_error("Bad ArchiveKind.");
7070
}
7171
}
7272

src/rustllvm/PassWrapper.cpp

+16-16
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ static CodeModel::Model fromRust(LLVMRustCodeModel Model) {
235235
case LLVMRustCodeModel::Large:
236236
return CodeModel::Large;
237237
default:
238-
llvm_unreachable("Bad CodeModel.");
238+
report_fatal_error("Bad CodeModel.");
239239
}
240240
}
241241

@@ -258,7 +258,7 @@ static CodeGenOpt::Level fromRust(LLVMRustCodeGenOptLevel Level) {
258258
case LLVMRustCodeGenOptLevel::Aggressive:
259259
return CodeGenOpt::Aggressive;
260260
default:
261-
llvm_unreachable("Bad CodeGenOptLevel.");
261+
report_fatal_error("Bad CodeGenOptLevel.");
262262
}
263263
}
264264

@@ -302,7 +302,7 @@ static Optional<Reloc::Model> fromRust(LLVMRustRelocMode RustReloc) {
302302
break;
303303
#endif
304304
}
305-
llvm_unreachable("Bad RelocModel.");
305+
report_fatal_error("Bad RelocModel.");
306306
}
307307

308308
#if LLVM_RUSTLLVM
@@ -511,7 +511,7 @@ static TargetMachine::CodeGenFileType fromRust(LLVMRustFileType Type) {
511511
case LLVMRustFileType::ObjectFile:
512512
return TargetMachine::CGFT_ObjectFile;
513513
default:
514-
llvm_unreachable("Bad FileType.");
514+
report_fatal_error("Bad FileType.");
515515
}
516516
}
517517

@@ -1197,7 +1197,7 @@ extern "C" bool
11971197
LLVMRustWriteThinBitcodeToFile(LLVMPassManagerRef PMR,
11981198
LLVMModuleRef M,
11991199
const char *BcFile) {
1200-
llvm_unreachable("ThinLTO not available");
1200+
report_fatal_error("ThinLTO not available");
12011201
}
12021202

12031203
struct LLVMRustThinLTOData {
@@ -1211,62 +1211,62 @@ LLVMRustCreateThinLTOData(LLVMRustThinLTOModule *modules,
12111211
int num_modules,
12121212
const char **preserved_symbols,
12131213
int num_symbols) {
1214-
llvm_unreachable("ThinLTO not available");
1214+
report_fatal_error("ThinLTO not available");
12151215
}
12161216

12171217
extern "C" bool
12181218
LLVMRustPrepareThinLTORename(const LLVMRustThinLTOData *Data, LLVMModuleRef M) {
1219-
llvm_unreachable("ThinLTO not available");
1219+
report_fatal_error("ThinLTO not available");
12201220
}
12211221

12221222
extern "C" bool
12231223
LLVMRustPrepareThinLTOResolveWeak(const LLVMRustThinLTOData *Data, LLVMModuleRef M) {
1224-
llvm_unreachable("ThinLTO not available");
1224+
report_fatal_error("ThinLTO not available");
12251225
}
12261226

12271227
extern "C" bool
12281228
LLVMRustPrepareThinLTOInternalize(const LLVMRustThinLTOData *Data, LLVMModuleRef M) {
1229-
llvm_unreachable("ThinLTO not available");
1229+
report_fatal_error("ThinLTO not available");
12301230
}
12311231

12321232
extern "C" bool
12331233
LLVMRustPrepareThinLTOImport(const LLVMRustThinLTOData *Data, LLVMModuleRef M) {
1234-
llvm_unreachable("ThinLTO not available");
1234+
report_fatal_error("ThinLTO not available");
12351235
}
12361236

12371237
extern "C" void
12381238
LLVMRustFreeThinLTOData(LLVMRustThinLTOData *Data) {
1239-
llvm_unreachable("ThinLTO not available");
1239+
report_fatal_error("ThinLTO not available");
12401240
}
12411241

12421242
struct LLVMRustThinLTOBuffer {
12431243
};
12441244

12451245
extern "C" LLVMRustThinLTOBuffer*
12461246
LLVMRustThinLTOBufferCreate(LLVMModuleRef M) {
1247-
llvm_unreachable("ThinLTO not available");
1247+
report_fatal_error("ThinLTO not available");
12481248
}
12491249

12501250
extern "C" void
12511251
LLVMRustThinLTOBufferFree(LLVMRustThinLTOBuffer *Buffer) {
1252-
llvm_unreachable("ThinLTO not available");
1252+
report_fatal_error("ThinLTO not available");
12531253
}
12541254

12551255
extern "C" const void*
12561256
LLVMRustThinLTOBufferPtr(const LLVMRustThinLTOBuffer *Buffer) {
1257-
llvm_unreachable("ThinLTO not available");
1257+
report_fatal_error("ThinLTO not available");
12581258
}
12591259

12601260
extern "C" size_t
12611261
LLVMRustThinLTOBufferLen(const LLVMRustThinLTOBuffer *Buffer) {
1262-
llvm_unreachable("ThinLTO not available");
1262+
report_fatal_error("ThinLTO not available");
12631263
}
12641264

12651265
extern "C" LLVMModuleRef
12661266
LLVMRustParseBitcodeForThinLTO(LLVMContextRef Context,
12671267
const char *data,
12681268
size_t len,
12691269
const char *identifier) {
1270-
llvm_unreachable("ThinLTO not available");
1270+
report_fatal_error("ThinLTO not available");
12711271
}
12721272
#endif // LLVM_VERSION_GE(4, 0)

src/rustllvm/RustWrapper.cpp

+11-11
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ static AtomicOrdering fromRust(LLVMAtomicOrdering Ordering) {
5454
return AtomicOrdering::SequentiallyConsistent;
5555
}
5656

57-
llvm_unreachable("Invalid LLVMAtomicOrdering value!");
57+
report_fatal_error("Invalid LLVMAtomicOrdering value!");
5858
}
5959

6060
static LLVM_THREAD_LOCAL char *LastError;
@@ -161,7 +161,7 @@ static Attribute::AttrKind fromRust(LLVMRustAttribute Kind) {
161161
case SanitizeMemory:
162162
return Attribute::SanitizeMemory;
163163
}
164-
llvm_unreachable("bad AttributeKind");
164+
report_fatal_error("bad AttributeKind");
165165
}
166166

167167
extern "C" void LLVMRustAddCallSiteAttribute(LLVMValueRef Instr, unsigned Index,
@@ -356,7 +356,7 @@ static SyncScope::ID fromRust(LLVMRustSynchronizationScope Scope) {
356356
case LLVMRustSynchronizationScope::CrossThread:
357357
return SyncScope::System;
358358
default:
359-
llvm_unreachable("bad SynchronizationScope.");
359+
report_fatal_error("bad SynchronizationScope.");
360360
}
361361
}
362362
#else
@@ -367,7 +367,7 @@ static SynchronizationScope fromRust(LLVMRustSynchronizationScope Scope) {
367367
case LLVMRustSynchronizationScope::CrossThread:
368368
return CrossThread;
369369
default:
370-
llvm_unreachable("bad SynchronizationScope.");
370+
report_fatal_error("bad SynchronizationScope.");
371371
}
372372
}
373373
#endif
@@ -397,7 +397,7 @@ static InlineAsm::AsmDialect fromRust(LLVMRustAsmDialect Dialect) {
397397
case LLVMRustAsmDialect::Intel:
398398
return InlineAsm::AD_Intel;
399399
default:
400-
llvm_unreachable("bad AsmDialect.");
400+
report_fatal_error("bad AsmDialect.");
401401
}
402402
}
403403

@@ -748,7 +748,7 @@ extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateVariable(
748748
unwrapDI<DIType>(Ty), AlwaysPreserve, fromRust(Flags)
749749
#if LLVM_VERSION_GE(4, 0)
750750
,
751-
AlignInBits
751+
AlignInBits
752752
#endif
753753
));
754754
} else {
@@ -1149,7 +1149,7 @@ extern "C" LLVMTypeKind LLVMRustGetTypeKind(LLVMTypeRef Ty) {
11491149
return LLVMTokenTypeKind;
11501150
#endif
11511151
}
1152-
llvm_unreachable("Unhandled TypeID.");
1152+
report_fatal_error("Unhandled TypeID.");
11531153
}
11541154

11551155
extern "C" void LLVMRustWriteDebugLocToString(LLVMContextRef C,
@@ -1370,7 +1370,7 @@ static LLVMRustLinkage toRust(LLVMLinkage Linkage) {
13701370
case LLVMCommonLinkage:
13711371
return LLVMRustLinkage::CommonLinkage;
13721372
default:
1373-
llvm_unreachable("Invalid LLVMRustLinkage value!");
1373+
report_fatal_error("Invalid LLVMRustLinkage value!");
13741374
}
13751375
}
13761376

@@ -1399,7 +1399,7 @@ static LLVMLinkage fromRust(LLVMRustLinkage Linkage) {
13991399
case LLVMRustLinkage::CommonLinkage:
14001400
return LLVMCommonLinkage;
14011401
}
1402-
llvm_unreachable("Invalid LLVMRustLinkage value!");
1402+
report_fatal_error("Invalid LLVMRustLinkage value!");
14031403
}
14041404

14051405
extern "C" LLVMRustLinkage LLVMRustGetLinkage(LLVMValueRef V) {
@@ -1447,7 +1447,7 @@ static LLVMRustVisibility toRust(LLVMVisibility Vis) {
14471447
case LLVMProtectedVisibility:
14481448
return LLVMRustVisibility::Protected;
14491449
}
1450-
llvm_unreachable("Invalid LLVMRustVisibility value!");
1450+
report_fatal_error("Invalid LLVMRustVisibility value!");
14511451
}
14521452

14531453
static LLVMVisibility fromRust(LLVMRustVisibility Vis) {
@@ -1459,7 +1459,7 @@ static LLVMVisibility fromRust(LLVMRustVisibility Vis) {
14591459
case LLVMRustVisibility::Protected:
14601460
return LLVMProtectedVisibility;
14611461
}
1462-
llvm_unreachable("Invalid LLVMRustVisibility value!");
1462+
report_fatal_error("Invalid LLVMRustVisibility value!");
14631463
}
14641464

14651465
extern "C" LLVMRustVisibility LLVMRustGetVisibility(LLVMValueRef V) {

src/tools/tidy/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ fn filter_dirs(path: &Path) -> bool {
5757
"src/libbacktrace",
5858
"src/libcompiler_builtins",
5959
"src/compiler-rt",
60-
"src/rustllvm",
6160
"src/liblibc",
6261
"src/vendor",
6362
"src/rt/hoedown",

src/tools/tidy/src/style.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ const UNEXPLAINED_IGNORE_DOCTEST_INFO: &str = r#"unexplained "```ignore" doctest
5050
5151
"#;
5252

53+
const LLVM_UNREACHABLE_INFO: &str = r"\
54+
C++ code used llvm_unreachable, which triggers undefined behavior
55+
when executed when assertions are disabled.
56+
Use llvm::report_fatal_error for increased robustness.";
57+
5358
/// Parser states for line_is_url.
5459
#[derive(PartialEq)]
5560
#[allow(non_camel_case_types)]
@@ -108,7 +113,7 @@ pub fn check(path: &Path, bad: &mut bool) {
108113
let mut contents = String::new();
109114
super::walk(path, &mut super::filter_dirs, &mut |file| {
110115
let filename = file.file_name().unwrap().to_string_lossy();
111-
let extensions = [".rs", ".py", ".js", ".sh", ".c", ".h"];
116+
let extensions = [".rs", ".py", ".js", ".sh", ".c", ".cpp", ".h"];
112117
if extensions.iter().all(|e| !filename.ends_with(e)) ||
113118
filename.starts_with(".#") {
114119
return
@@ -153,6 +158,9 @@ pub fn check(path: &Path, bad: &mut bool) {
153158
if line.ends_with("```ignore") || line.ends_with("```rust,ignore") {
154159
err(UNEXPLAINED_IGNORE_DOCTEST_INFO);
155160
}
161+
if filename.ends_with(".cpp") && line.contains("llvm_unreachable") {
162+
err(LLVM_UNREACHABLE_INFO);
163+
}
156164
}
157165
if !licenseck(file, &contents) {
158166
tidy_error!(bad, "{}: incorrect license", file.display());

0 commit comments

Comments
 (0)