Skip to content

Commit

Permalink
Rollup merge of rust-lang#126938 - RalfJung:link_section, r=compiler-…
Browse files Browse the repository at this point in the history
…errors

miri: make sure we can find link_section statics even for the local crate

Miri needs some way to iterate all the exported functions and "used" statics of all crates. For dependency crates, this already works fine since we can overwrite the query resonsible for computing `exported_symbols`, but it turns out for local binary crates this does not work: for binaries, `reachable_set` skips a lot of its logic and only checks `contains_extern_indicator()` and `RUSTC_STD_INTERNAL_SYMBOL`. Other flags like `CodegenFnAttrFlags::USED` are entirely ignored.

This PR proposes to use the same check, `has_custom_linkage`, in binaries that we already use to drive the main workqueue of the reachability recursive traversal. I have no idea why binaries used a slightly different check that ignores `USED` -- was that deliberate or does it just not matter most of the time?
  • Loading branch information
matthiaskrgr committed Jun 26, 2024
2 parents 95332b8 + d9a3423 commit 5c4ede8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
12 changes: 2 additions & 10 deletions compiler/rustc_passes/src/reachable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::Node;
use rustc_middle::bug;
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc_middle::middle::privacy::{self, Level};
use rustc_middle::mir::interpret::{ConstAllocation, ErrorHandled, GlobalAlloc};
use rustc_middle::query::Providers;
Expand Down Expand Up @@ -178,15 +178,7 @@ impl<'tcx> ReachableContext<'tcx> {
if !self.any_library {
// If we are building an executable, only explicitly extern
// types need to be exported.
let codegen_attrs = if self.tcx.def_kind(search_item).has_codegen_attrs() {
self.tcx.codegen_fn_attrs(search_item)
} else {
CodegenFnAttrs::EMPTY
};
let is_extern = codegen_attrs.contains_extern_indicator();
let std_internal =
codegen_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL);
if is_extern || std_internal {
if has_custom_linkage(self.tcx, search_item) {
self.reachable_symbols.insert(search_item);
}
} else {
Expand Down
16 changes: 16 additions & 0 deletions src/tools/miri/tests/pass/tls/win_tls_callback.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//! Ensure that we call Windows TLS callbacks in the local crate.
//@only-target-windows
// Calling eprintln in the callback seems to (re-)initialize some thread-local storage
// and then leak the memory allocated for that. Let's just ignore these leaks,
// that's not what this test is about.
//@compile-flags: -Zmiri-ignore-leaks

#[link_section = ".CRT$XLB"]
#[used] // Miri only considers explicitly `#[used]` statics for `lookup_link_section`
pub static CALLBACK: unsafe extern "system" fn(*const (), u32, *const ()) = tls_callback;

unsafe extern "system" fn tls_callback(_h: *const (), _dw_reason: u32, _pv: *const ()) {
eprintln!("in tls_callback");
}

fn main() {}
1 change: 1 addition & 0 deletions src/tools/miri/tests/pass/tls/win_tls_callback.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
in tls_callback

0 comments on commit 5c4ede8

Please sign in to comment.