diff options
| author | bors <bors@rust-lang.org> | 2017-11-16 06:10:36 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2017-11-16 06:10:36 +0000 |
| commit | 1410d5604042b739f02f9ec0f2a6c5125c797d52 (patch) | |
| tree | 5e945d090d07c9abff1dbb8ce36193ed5ee32f15 /src/rustllvm/PassWrapper.cpp | |
| parent | 8385fc062dbb57c32d96d16e59d244c168103946 (diff) | |
| parent | ac48348db85d0ce9efdc450ab52837dcfc42a932 (diff) | |
| download | rust-1410d5604042b739f02f9ec0f2a6c5125c797d52.tar.gz rust-1410d5604042b739f02f9ec0f2a6c5125c797d52.zip | |
Auto merge of #45920 - sunfishcode:trap-on-unreachable, r=Zoxc
Enable TrapUnreachable in LLVM. This patch enables 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. This follows up on https://github.com/rust-lang/rust/issues/28728#issuecomment-332581533. For example, for @zackw's testcase [here](https://github.com/rust-lang/rust/issues/42009#issue-228745924), the output function contains a `ud2` instead of no code, so it won't "fall through" into whatever happens to be next in memory. (I'm also working on the problem of LLVM optimizing away infinite loops, but the patch here is useful independently.) I tested this patch on a few different codebases, and the code size increase ranged from 0.0% to 0.1%.
Diffstat (limited to 'src/rustllvm/PassWrapper.cpp')
| -rw-r--r-- | src/rustllvm/PassWrapper.cpp | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/src/rustllvm/PassWrapper.cpp b/src/rustllvm/PassWrapper.cpp index b397ad1e98f..b4116c96ba1 100644 --- a/src/rustllvm/PassWrapper.cpp +++ b/src/rustllvm/PassWrapper.cpp @@ -366,7 +366,7 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine( LLVMRustCodeModel RustCM, LLVMRustRelocMode RustReloc, LLVMRustCodeGenOptLevel RustOptLevel, bool UseSoftFloat, bool PositionIndependentExecutable, bool FunctionSections, - bool DataSections) { + bool DataSections, bool TrapUnreachable) { auto CM = fromRust(RustCM); auto OptLevel = fromRust(RustOptLevel); @@ -398,6 +398,14 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine( Options.DataSections = DataSections; Options.FunctionSections = FunctionSections; + if (TrapUnreachable) { + // Tell LLVM to translate `unreachable` into an explicit trap instruction. + // This limits the extent of possible undefined behavior in some cases, as + // it prevents control flow from "falling through" into whatever code + // happens to be laid out next in memory. + Options.TrapUnreachable = true; + } + TargetMachine *TM = TheTarget->createTargetMachine( Trip.getTriple(), RealCPU, Feature, Options, RM, CM, OptLevel); return wrap(TM); |
