Skip to content

Commit d9f0e88

Browse files
committed
Enable TrapUnreachable in LLVM.
Enable LLVM's TrapUnreachable flag, which tells it to translate `unreachable` instructions into hardware trap instructions, rather than allowing control flow to "fall through" into whatever code happens to follow it in memory.
1 parent 75d25ac commit d9f0e88

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/rustllvm/PassWrapper.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,12 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
398398
Options.DataSections = DataSections;
399399
Options.FunctionSections = FunctionSections;
400400

401+
// Tell LLVM to translate `unreachable` into an explicit trap instruction.
402+
// This limits the extent of possible undefined behavior in some cases, as it
403+
// prevents control flow from "falling through" into whatever code happens to
404+
// be layed out next in memory.
405+
Options.TrapUnreachable = true;
406+
401407
TargetMachine *TM = TheTarget->createTargetMachine(
402408
Trip.getTriple(), RealCPU, Feature, Options, RM, CM, OptLevel);
403409
return wrap(TM);

0 commit comments

Comments
 (0)