Skip to content

Commit

Permalink
Rollup merge of rust-lang#74103 - ajpaverd:cfguard-msvc-only, r=nikom…
Browse files Browse the repository at this point in the history
…atsakis

Only add CFGuard on `windows-msvc` targets

As @ollie27 pointed out in rust-lang#73893, the `cfguard` module flag causes incorrect behavior on `windows-gnu` targets. This patch restricts rustc to only add this flag for `windows-msvc` targets (this may need to be changed if other linkers gain support for CFGuard).
  • Loading branch information
Manishearth authored Jul 11, 2020
2 parents 5f495d5 + 1ca7bfe commit 34e083f
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 19 deletions.
19 changes: 12 additions & 7 deletions src/librustc_codegen_llvm/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,19 @@ pub unsafe fn create_module(
llvm::LLVMRustAddModuleFlag(llmod, avoid_plt, 1);
}

// Set module flags to enable Windows Control Flow Guard (/guard:cf) metadata
// only (`cfguard=1`) or metadata and checks (`cfguard=2`).
match sess.opts.debugging_opts.control_flow_guard {
CFGuard::Disabled => {}
CFGuard::NoChecks => {
llvm::LLVMRustAddModuleFlag(llmod, "cfguard\0".as_ptr() as *const _, 1)
// Control Flow Guard is currently only supported by the MSVC linker on Windows.
if sess.target.target.options.is_like_msvc {
match sess.opts.debugging_opts.control_flow_guard {
CFGuard::Disabled => {}
CFGuard::NoChecks => {
// Set `cfguard=1` module flag to emit metadata only.
llvm::LLVMRustAddModuleFlag(llmod, "cfguard\0".as_ptr() as *const _, 1)
}
CFGuard::Checks => {
// Set `cfguard=2` module flag to emit metadata and checks.
llvm::LLVMRustAddModuleFlag(llmod, "cfguard\0".as_ptr() as *const _, 2)
}
}
CFGuard::Checks => llvm::LLVMRustAddModuleFlag(llmod, "cfguard\0".as_ptr() as *const _, 2),
}

llmod
Expand Down
16 changes: 4 additions & 12 deletions src/librustc_codegen_ssa/back/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,7 @@ impl<'a> Linker for GccLinker<'a> {
self.cmd.arg("__llvm_profile_runtime");
}

fn control_flow_guard(&mut self) {
self.sess.warn("Windows Control Flow Guard is not supported by this linker.");
}
fn control_flow_guard(&mut self) {}

fn debuginfo(&mut self, strip: Strip) {
match strip {
Expand Down Expand Up @@ -959,9 +957,7 @@ impl<'a> Linker for EmLinker<'a> {
// noop, but maybe we need something like the gnu linker?
}

fn control_flow_guard(&mut self) {
self.sess.warn("Windows Control Flow Guard is not supported by this linker.");
}
fn control_flow_guard(&mut self) {}

fn debuginfo(&mut self, _strip: Strip) {
// Preserve names or generate source maps depending on debug info
Expand Down Expand Up @@ -1163,9 +1159,7 @@ impl<'a> Linker for WasmLd<'a> {
}
}

fn control_flow_guard(&mut self) {
self.sess.warn("Windows Control Flow Guard is not supported by this linker.");
}
fn control_flow_guard(&mut self) {}

fn no_crt_objects(&mut self) {}

Expand Down Expand Up @@ -1330,9 +1324,7 @@ impl<'a> Linker for PtxLinker<'a> {

fn no_default_libraries(&mut self) {}

fn control_flow_guard(&mut self) {
self.sess.warn("Windows Control Flow Guard is not supported by this linker.");
}
fn control_flow_guard(&mut self) {}

fn export_symbols(&mut self, _tmpdir: &Path, _crate_type: CrateType) {}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// compile-flags: -Z control-flow-guard=checks
// only-msvc

#![crate_type = "lib"]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// compile-flags: -Z control-flow-guard=no
// only-msvc

#![crate_type = "lib"]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// compile-flags: -Z control-flow-guard=nochecks
// only-msvc

#![crate_type = "lib"]

Expand Down
11 changes: 11 additions & 0 deletions src/test/codegen/cfguard-non-msvc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// compile-flags: -Z control-flow-guard
// ignore-msvc

#![crate_type = "lib"]

// A basic test function.
pub fn test() {
}

// Ensure the cfguard module flag is not added for non-MSVC targets.
// CHECK-NOT: !"cfguard"
6 changes: 6 additions & 0 deletions src/test/ui/cfguard-run.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// run-pass
// compile-flags: -Z control-flow-guard

pub fn main() {
println!("hello, world");
}

0 comments on commit 34e083f

Please sign in to comment.