Skip to content

Commit e048504

Browse files
authored
Rollup merge of rust-lang#74103 - ajpaverd:cfguard-msvc-only, r=nikomatsakis
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).
2 parents cd699b9 + 1ca7bfe commit e048504

File tree

7 files changed

+36
-19
lines changed

7 files changed

+36
-19
lines changed

src/librustc_codegen_llvm/context.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,19 @@ pub unsafe fn create_module(
188188
llvm::LLVMRustAddModuleFlag(llmod, avoid_plt, 1);
189189
}
190190

191-
// Set module flags to enable Windows Control Flow Guard (/guard:cf) metadata
192-
// only (`cfguard=1`) or metadata and checks (`cfguard=2`).
193-
match sess.opts.debugging_opts.control_flow_guard {
194-
CFGuard::Disabled => {}
195-
CFGuard::NoChecks => {
196-
llvm::LLVMRustAddModuleFlag(llmod, "cfguard\0".as_ptr() as *const _, 1)
191+
// Control Flow Guard is currently only supported by the MSVC linker on Windows.
192+
if sess.target.target.options.is_like_msvc {
193+
match sess.opts.debugging_opts.control_flow_guard {
194+
CFGuard::Disabled => {}
195+
CFGuard::NoChecks => {
196+
// Set `cfguard=1` module flag to emit metadata only.
197+
llvm::LLVMRustAddModuleFlag(llmod, "cfguard\0".as_ptr() as *const _, 1)
198+
}
199+
CFGuard::Checks => {
200+
// Set `cfguard=2` module flag to emit metadata and checks.
201+
llvm::LLVMRustAddModuleFlag(llmod, "cfguard\0".as_ptr() as *const _, 2)
202+
}
197203
}
198-
CFGuard::Checks => llvm::LLVMRustAddModuleFlag(llmod, "cfguard\0".as_ptr() as *const _, 2),
199204
}
200205

201206
llmod

src/librustc_codegen_ssa/back/linker.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -475,9 +475,7 @@ impl<'a> Linker for GccLinker<'a> {
475475
self.cmd.arg("__llvm_profile_runtime");
476476
}
477477

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

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

962-
fn control_flow_guard(&mut self) {
963-
self.sess.warn("Windows Control Flow Guard is not supported by this linker.");
964-
}
960+
fn control_flow_guard(&mut self) {}
965961

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

1166-
fn control_flow_guard(&mut self) {
1167-
self.sess.warn("Windows Control Flow Guard is not supported by this linker.");
1168-
}
1162+
fn control_flow_guard(&mut self) {}
11691163

11701164
fn no_crt_objects(&mut self) {}
11711165

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

13311325
fn no_default_libraries(&mut self) {}
13321326

1333-
fn control_flow_guard(&mut self) {
1334-
self.sess.warn("Windows Control Flow Guard is not supported by this linker.");
1335-
}
1327+
fn control_flow_guard(&mut self) {}
13361328

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

src/test/codegen/cfguard_checks.rs src/test/codegen/cfguard-checks.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// compile-flags: -Z control-flow-guard=checks
2+
// only-msvc
23

34
#![crate_type = "lib"]
45

src/test/codegen/cfguard_disabled.rs src/test/codegen/cfguard-disabled.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// compile-flags: -Z control-flow-guard=no
2+
// only-msvc
23

34
#![crate_type = "lib"]
45

src/test/codegen/cfguard_nochecks.rs src/test/codegen/cfguard-nochecks.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// compile-flags: -Z control-flow-guard=nochecks
2+
// only-msvc
23

34
#![crate_type = "lib"]
45

src/test/codegen/cfguard-non-msvc.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// compile-flags: -Z control-flow-guard
2+
// ignore-msvc
3+
4+
#![crate_type = "lib"]
5+
6+
// A basic test function.
7+
pub fn test() {
8+
}
9+
10+
// Ensure the cfguard module flag is not added for non-MSVC targets.
11+
// CHECK-NOT: !"cfguard"

src/test/ui/cfguard-run.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// run-pass
2+
// compile-flags: -Z control-flow-guard
3+
4+
pub fn main() {
5+
println!("hello, world");
6+
}

0 commit comments

Comments
 (0)