Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable TrapUnreachable in LLVM. #45920

Merged
merged 6 commits into from
Nov 16, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/rustllvm/PassWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,12 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
Options.DataSections = DataSections;
Options.FunctionSections = FunctionSections;

// 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);
Expand Down
4 changes: 3 additions & 1 deletion src/test/run-make/intrinsic-unreachable/exit-ret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
#![feature(asm)]
#![crate_type="lib"]

pub fn exit(n: usize) {
#[deny(unreachable_code)]
pub fn exit(n: usize) -> i32 {
unsafe {
// Pretend this asm is an exit() syscall.
asm!("" :: "r"(n) :: "volatile");
// Can't actually reach this point, but rustc doesn't know that.
}
42
}
4 changes: 3 additions & 1 deletion src/test/run-make/intrinsic-unreachable/exit-unreachable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@

use std::intrinsics;

pub fn exit(n: usize) -> ! {
#[allow(unreachable_code)]
pub fn exit(n: usize) -> i32 {
unsafe {
// Pretend this asm is an exit() syscall.
asm!("" :: "r"(n) :: "volatile");
intrinsics::unreachable()
}
42
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment that the 42 is there just so there's some code for LLVM to remove, making the test that the assembly output of exit-unreachable.rs is shorter than exit-ret.rs succeed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment added.

}