about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2024-06-25 12:02:55 +0200
committerRalf Jung <post@ralfj.de>2024-06-25 14:00:35 +0200
commitd9a34232888629efb8226a9c791b40a384986ef8 (patch)
tree4133e5de21e7100733414a80d7d4dba9d3403306
parentfc555cd832ee743ff5410c35de2b0dd59ec4322e (diff)
downloadrust-d9a34232888629efb8226a9c791b40a384986ef8.tar.gz
rust-d9a34232888629efb8226a9c791b40a384986ef8.zip
miri: make sure we can find link_section statics even for the local crate
-rw-r--r--compiler/rustc_passes/src/reachable.rs12
-rw-r--r--src/tools/miri/tests/pass/tls/win_tls_callback.rs16
-rw-r--r--src/tools/miri/tests/pass/tls/win_tls_callback.stderr1
3 files changed, 19 insertions, 10 deletions
diff --git a/compiler/rustc_passes/src/reachable.rs b/compiler/rustc_passes/src/reachable.rs
index 6dd8eaf7e67..da4435ebebe 100644
--- a/compiler/rustc_passes/src/reachable.rs
+++ b/compiler/rustc_passes/src/reachable.rs
@@ -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;
@@ -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 {
diff --git a/src/tools/miri/tests/pass/tls/win_tls_callback.rs b/src/tools/miri/tests/pass/tls/win_tls_callback.rs
new file mode 100644
index 00000000000..99a8de29e91
--- /dev/null
+++ b/src/tools/miri/tests/pass/tls/win_tls_callback.rs
@@ -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() {}
diff --git a/src/tools/miri/tests/pass/tls/win_tls_callback.stderr b/src/tools/miri/tests/pass/tls/win_tls_callback.stderr
new file mode 100644
index 00000000000..84795589544
--- /dev/null
+++ b/src/tools/miri/tests/pass/tls/win_tls_callback.stderr
@@ -0,0 +1 @@
+in tls_callback