From 6fddf3b75bdc57784e2fe0ff7217a51f498b754c Mon Sep 17 00:00:00 2001 From: Matt Hammerly Date: Fri, 25 Feb 2022 20:22:31 -0800 Subject: [PATCH 1/2] create `-Z force-allocator-shim` codegen option --- compiler/rustc_codegen_ssa/src/back/link.rs | 13 ++++++------- compiler/rustc_metadata/src/creader.rs | 4 +++- compiler/rustc_session/src/options.rs | 2 ++ 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index 587453fd8e8d6..0671981c56f0a 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -299,13 +299,12 @@ fn link_rlib<'a, B: ArchiveBuilder<'a>>( } } - match flavor { - RlibFlavor::Normal => {} - RlibFlavor::StaticlibBase => { - let obj = codegen_results.allocator_module.as_ref().and_then(|m| m.object.as_ref()); - if let Some(obj) = obj { - ab.add_file(obj); - } + if flavor == RlibFlavor::StaticlibBase + || sess.opts.debugging_opts.force_allocator_shim.unwrap_or(false) + { + let obj = codegen_results.allocator_module.as_ref().and_then(|m| m.object.as_ref()); + if let Some(obj) = obj { + ab.add_file(obj); } } diff --git a/compiler/rustc_metadata/src/creader.rs b/compiler/rustc_metadata/src/creader.rs index 7343f1465f603..208a6b7f483b7 100644 --- a/compiler/rustc_metadata/src/creader.rs +++ b/compiler/rustc_metadata/src/creader.rs @@ -809,7 +809,9 @@ impl<'a> CrateLoader<'a> { // if our compilation session actually needs an allocator based on what // we're emitting. let all_rlib = self.sess.crate_types().iter().all(|ct| matches!(*ct, CrateType::Rlib)); - if all_rlib { + let force_allocator_shim = + self.sess.opts.debugging_opts.force_allocator_shim.unwrap_or(false); + if all_rlib && !force_allocator_shim { return; } diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index 359e00718bf13..672abe6b569a7 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -1222,6 +1222,8 @@ options! { fewer_names: Option = (None, parse_opt_bool, [TRACKED], "reduce memory use by retaining fewer names within compilation artifacts (LLVM-IR) \ (default: no)"), + force_allocator_shim: Option = (None, parse_opt_bool, [TRACKED], + "force generation of an allocator shim"), force_unstable_if_unmarked: bool = (false, parse_bool, [TRACKED], "force all crates to be `rustc_private` unstable (default: no)"), fuel: Option<(String, u64)> = (None, parse_optimization_fuel, [TRACKED], From dea9e8f80b9f015f268e45f389fd222b98bbadf4 Mon Sep 17 00:00:00 2001 From: Matt Hammerly Date: Mon, 28 Feb 2022 09:36:23 -0800 Subject: [PATCH 2/2] tests --- compiler/rustc_interface/src/tests.rs | 1 + src/test/run-make/force-allocator-shim/Makefile | 12 ++++++++++++ src/test/run-make/force-allocator-shim/foo.rs | 1 + 3 files changed, 14 insertions(+) create mode 100644 src/test/run-make/force-allocator-shim/Makefile create mode 100644 src/test/run-make/force-allocator-shim/foo.rs diff --git a/compiler/rustc_interface/src/tests.rs b/compiler/rustc_interface/src/tests.rs index def5c30411b84..858d67173f3e5 100644 --- a/compiler/rustc_interface/src/tests.rs +++ b/compiler/rustc_interface/src/tests.rs @@ -732,6 +732,7 @@ fn test_debugging_options_tracking_hash() { tracked!(drop_tracking, true); tracked!(dual_proc_macros, true); tracked!(fewer_names, Some(true)); + tracked!(force_allocator_shim, Some(false)); tracked!(force_unstable_if_unmarked, true); tracked!(fuel, Some(("abc".to_string(), 99))); tracked!(function_sections, Some(false)); diff --git a/src/test/run-make/force-allocator-shim/Makefile b/src/test/run-make/force-allocator-shim/Makefile new file mode 100644 index 0000000000000..a232254b853e4 --- /dev/null +++ b/src/test/run-make/force-allocator-shim/Makefile @@ -0,0 +1,12 @@ +-include ../../run-make-fulldeps/tools.mk + +# Tests that the `-Z force-allocator-shim` flag causes an allocator shim to be +# generated when expected. + +all: + $(RUSTC) foo.rs --crate-type rlib -Z force-allocator-shim + nm $(TMPDIR)/libfoo.rlib | $(CGREP) "__rust_alloc" && \ + nm $(TMPDIR)/libfoo.rlib | $(CGREP) "__rust_alloc_error_handler" && \ + nm $(TMPDIR)/libfoo.rlib | $(CGREP) "__rust_alloc_zeroed" && \ + nm $(TMPDIR)/libfoo.rlib | $(CGREP) "__rust_dealloc" && \ + nm $(TMPDIR)/libfoo.rlib | $(CGREP) "__rust_realloc" diff --git a/src/test/run-make/force-allocator-shim/foo.rs b/src/test/run-make/force-allocator-shim/foo.rs new file mode 100644 index 0000000000000..b76b4321d62aa --- /dev/null +++ b/src/test/run-make/force-allocator-shim/foo.rs @@ -0,0 +1 @@ +pub fn foo() {}