Skip to content

Commit 0230848

Browse files
committed
Auto merge of #123937 - RalfJung:miri-link-section, r=oli-obk
Miri on Windows: run .CRT$XLB linker section on thread-end Hopefully fixes #123583 First commit is originally by `@bjorn3` r? `@oli-obk` Cc `@ChrisDenton`
2 parents 84e729a + 5934aaa commit 0230848

File tree

7 files changed

+205
-146
lines changed

7 files changed

+205
-146
lines changed

library/std/src/sys/pal/windows/thread_local_key.rs

+1
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ unsafe fn register_dtor(key: &'static StaticKey) {
276276
// the address of the symbol to ensure it sticks around.
277277

278278
#[link_section = ".CRT$XLB"]
279+
#[cfg_attr(miri, used)] // Miri only considers explicitly `#[used]` statics for `lookup_link_section`
279280
pub static p_thread_callback: unsafe extern "system" fn(c::LPVOID, c::DWORD, c::LPVOID) =
280281
on_tls_callback;
281282

src/tools/miri/src/bin/miri.rs

+26-15
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ use rustc_driver::Compilation;
3232
use rustc_hir::{self as hir, Node};
3333
use rustc_interface::interface::Config;
3434
use rustc_middle::{
35-
middle::exported_symbols::{
36-
ExportedSymbol, SymbolExportInfo, SymbolExportKind, SymbolExportLevel,
35+
middle::{
36+
codegen_fn_attrs::CodegenFnAttrFlags,
37+
exported_symbols::{ExportedSymbol, SymbolExportInfo, SymbolExportKind, SymbolExportLevel},
3738
},
3839
query::LocalCrate,
3940
ty::TyCtxt,
@@ -136,6 +137,7 @@ impl rustc_driver::Callbacks for MiriBeRustCompilerCalls {
136137
config.override_queries = Some(|_, local_providers| {
137138
// `exported_symbols` and `reachable_non_generics` provided by rustc always returns
138139
// an empty result if `tcx.sess.opts.output_types.should_codegen()` is false.
140+
// In addition we need to add #[used] symbols to exported_symbols for `lookup_link_section`.
139141
local_providers.exported_symbols = |tcx, LocalCrate| {
140142
let reachable_set = tcx.with_stable_hashing_context(|hcx| {
141143
tcx.reachable_set(()).to_sorted(&hcx, true)
@@ -160,19 +162,28 @@ impl rustc_driver::Callbacks for MiriBeRustCompilerCalls {
160162
})
161163
if !tcx.generics_of(local_def_id).requires_monomorphization(tcx)
162164
);
163-
(is_reachable_non_generic
164-
&& tcx.codegen_fn_attrs(local_def_id).contains_extern_indicator())
165-
.then_some((
166-
ExportedSymbol::NonGeneric(local_def_id.to_def_id()),
167-
// Some dummy `SymbolExportInfo` here. We only use
168-
// `exported_symbols` in shims/foreign_items.rs and the export info
169-
// is ignored.
170-
SymbolExportInfo {
171-
level: SymbolExportLevel::C,
172-
kind: SymbolExportKind::Text,
173-
used: false,
174-
},
175-
))
165+
if !is_reachable_non_generic {
166+
return None;
167+
}
168+
let codegen_fn_attrs = tcx.codegen_fn_attrs(local_def_id);
169+
if codegen_fn_attrs.contains_extern_indicator()
170+
|| codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::USED)
171+
|| codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER)
172+
{
173+
Some((
174+
ExportedSymbol::NonGeneric(local_def_id.to_def_id()),
175+
// Some dummy `SymbolExportInfo` here. We only use
176+
// `exported_symbols` in shims/foreign_items.rs and the export info
177+
// is ignored.
178+
SymbolExportInfo {
179+
level: SymbolExportLevel::C,
180+
kind: SymbolExportKind::Text,
181+
used: false,
182+
},
183+
))
184+
} else {
185+
None
186+
}
176187
}),
177188
)
178189
}

src/tools/miri/src/concurrency/thread.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ pub struct Thread<'mir, 'tcx> {
158158
}
159159

160160
pub type StackEmptyCallback<'mir, 'tcx> =
161-
Box<dyn FnMut(&mut MiriInterpCx<'mir, 'tcx>) -> InterpResult<'tcx, Poll<()>>>;
161+
Box<dyn FnMut(&mut MiriInterpCx<'mir, 'tcx>) -> InterpResult<'tcx, Poll<()>> + 'tcx>;
162162

163163
impl<'mir, 'tcx> Thread<'mir, 'tcx> {
164164
/// Get the name of the current thread if it was set.

src/tools/miri/src/eval.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -192,18 +192,18 @@ impl Default for MiriConfig {
192192

193193
/// The state of the main thread. Implementation detail of `on_main_stack_empty`.
194194
#[derive(Default, Debug)]
195-
enum MainThreadState {
195+
enum MainThreadState<'tcx> {
196196
#[default]
197197
Running,
198-
TlsDtors(tls::TlsDtorsState),
198+
TlsDtors(tls::TlsDtorsState<'tcx>),
199199
Yield {
200200
remaining: u32,
201201
},
202202
Done,
203203
}
204204

205-
impl MainThreadState {
206-
fn on_main_stack_empty<'tcx>(
205+
impl<'tcx> MainThreadState<'tcx> {
206+
fn on_main_stack_empty(
207207
&mut self,
208208
this: &mut MiriInterpCx<'_, 'tcx>,
209209
) -> InterpResult<'tcx, Poll<()>> {

src/tools/miri/src/helpers.rs

+71-3
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,21 @@ use rand::RngCore;
99

1010
use rustc_apfloat::ieee::{Double, Single};
1111
use rustc_apfloat::Float;
12-
use rustc_hir::def::{DefKind, Namespace};
13-
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX};
12+
use rustc_hir::{
13+
def::{DefKind, Namespace},
14+
def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE},
15+
};
1416
use rustc_index::IndexVec;
17+
use rustc_middle::middle::dependency_format::Linkage;
18+
use rustc_middle::middle::exported_symbols::ExportedSymbol;
1519
use rustc_middle::mir;
1620
use rustc_middle::ty::{
1721
self,
1822
layout::{LayoutOf, TyAndLayout},
1923
FloatTy, IntTy, Ty, TyCtxt, UintTy,
2024
};
21-
use rustc_span::{def_id::CrateNum, sym, Span, Symbol};
25+
use rustc_session::config::CrateType;
26+
use rustc_span::{sym, Span, Symbol};
2227
use rustc_target::abi::{Align, FieldIdx, FieldsShape, Size, Variants};
2328
use rustc_target::spec::abi::Abi;
2429

@@ -142,6 +147,38 @@ fn try_resolve_did(tcx: TyCtxt<'_>, path: &[&str], namespace: Option<Namespace>)
142147
None
143148
}
144149

150+
/// Call `f` for each exported symbol.
151+
pub fn iter_exported_symbols<'tcx>(
152+
tcx: TyCtxt<'tcx>,
153+
mut f: impl FnMut(CrateNum, DefId) -> InterpResult<'tcx>,
154+
) -> InterpResult<'tcx> {
155+
// `dependency_formats` includes all the transitive informations needed to link a crate,
156+
// which is what we need here since we need to dig out `exported_symbols` from all transitive
157+
// dependencies.
158+
let dependency_formats = tcx.dependency_formats(());
159+
let dependency_format = dependency_formats
160+
.iter()
161+
.find(|(crate_type, _)| *crate_type == CrateType::Executable)
162+
.expect("interpreting a non-executable crate");
163+
for cnum in iter::once(LOCAL_CRATE).chain(dependency_format.1.iter().enumerate().filter_map(
164+
|(num, &linkage)| {
165+
// We add 1 to the number because that's what rustc also does everywhere it
166+
// calls `CrateNum::new`...
167+
#[allow(clippy::arithmetic_side_effects)]
168+
(linkage != Linkage::NotLinked).then_some(CrateNum::new(num + 1))
169+
},
170+
)) {
171+
// We can ignore `_export_info` here: we are a Rust crate, and everything is exported
172+
// from a Rust crate.
173+
for &(symbol, _export_info) in tcx.exported_symbols(cnum) {
174+
if let ExportedSymbol::NonGeneric(def_id) = symbol {
175+
f(cnum, def_id)?;
176+
}
177+
}
178+
}
179+
Ok(())
180+
}
181+
145182
/// Convert a softfloat type to its corresponding hostfloat type.
146183
pub trait ToHost {
147184
type HostFloat;
@@ -1180,6 +1217,37 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
11801217
}
11811218
Ok(())
11821219
}
1220+
1221+
/// Lookup an array of immediates stored as a linker section of name `name`.
1222+
fn lookup_link_section(
1223+
&mut self,
1224+
name: &str,
1225+
) -> InterpResult<'tcx, Vec<ImmTy<'tcx, Provenance>>> {
1226+
let this = self.eval_context_mut();
1227+
let tcx = this.tcx.tcx;
1228+
1229+
let mut array = vec![];
1230+
1231+
iter_exported_symbols(tcx, |_cnum, def_id| {
1232+
let attrs = tcx.codegen_fn_attrs(def_id);
1233+
let Some(link_section) = attrs.link_section else {
1234+
return Ok(());
1235+
};
1236+
if link_section.as_str() == name {
1237+
let instance = ty::Instance::mono(tcx, def_id);
1238+
let const_val = this.eval_global(instance).unwrap_or_else(|err| {
1239+
panic!(
1240+
"failed to evaluate static in required link_section: {def_id:?}\n{err:?}"
1241+
)
1242+
});
1243+
let val = this.read_immediate(&const_val)?;
1244+
array.push(val);
1245+
}
1246+
Ok(())
1247+
})?;
1248+
1249+
Ok(array)
1250+
}
11831251
}
11841252

11851253
impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {

src/tools/miri/src/shims/foreign_items.rs

+40-73
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,10 @@ use std::{collections::hash_map::Entry, io::Write, iter, path::Path};
22

33
use rustc_apfloat::Float;
44
use rustc_ast::expand::allocator::AllocatorKind;
5-
use rustc_hir::{
6-
def::DefKind,
7-
def_id::{CrateNum, LOCAL_CRATE},
8-
};
9-
use rustc_middle::middle::{
10-
codegen_fn_attrs::CodegenFnAttrFlags, dependency_format::Linkage,
11-
exported_symbols::ExportedSymbol,
12-
};
5+
use rustc_hir::{def::DefKind, def_id::CrateNum};
6+
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
137
use rustc_middle::mir;
148
use rustc_middle::ty;
15-
use rustc_session::config::CrateType;
169
use rustc_span::Symbol;
1710
use rustc_target::{
1811
abi::{Align, Size},
@@ -174,74 +167,48 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
174167
Entry::Vacant(e) => {
175168
// Find it if it was not cached.
176169
let mut instance_and_crate: Option<(ty::Instance<'_>, CrateNum)> = None;
177-
// `dependency_formats` includes all the transitive informations needed to link a crate,
178-
// which is what we need here since we need to dig out `exported_symbols` from all transitive
179-
// dependencies.
180-
let dependency_formats = tcx.dependency_formats(());
181-
let dependency_format = dependency_formats
182-
.iter()
183-
.find(|(crate_type, _)| *crate_type == CrateType::Executable)
184-
.expect("interpreting a non-executable crate");
185-
for cnum in iter::once(LOCAL_CRATE).chain(
186-
dependency_format.1.iter().enumerate().filter_map(|(num, &linkage)| {
187-
// We add 1 to the number because that's what rustc also does everywhere it
188-
// calls `CrateNum::new`...
189-
#[allow(clippy::arithmetic_side_effects)]
190-
(linkage != Linkage::NotLinked).then_some(CrateNum::new(num + 1))
191-
}),
192-
) {
193-
// We can ignore `_export_info` here: we are a Rust crate, and everything is exported
194-
// from a Rust crate.
195-
for &(symbol, _export_info) in tcx.exported_symbols(cnum) {
196-
if let ExportedSymbol::NonGeneric(def_id) = symbol {
197-
let attrs = tcx.codegen_fn_attrs(def_id);
198-
let symbol_name = if let Some(export_name) = attrs.export_name {
199-
export_name
200-
} else if attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE) {
201-
tcx.item_name(def_id)
170+
helpers::iter_exported_symbols(tcx, |cnum, def_id| {
171+
let attrs = tcx.codegen_fn_attrs(def_id);
172+
let symbol_name = if let Some(export_name) = attrs.export_name {
173+
export_name
174+
} else if attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE) {
175+
tcx.item_name(def_id)
176+
} else {
177+
// Skip over items without an explicitly defined symbol name.
178+
return Ok(());
179+
};
180+
if symbol_name == link_name {
181+
if let Some((original_instance, original_cnum)) = instance_and_crate {
182+
// Make sure we are consistent wrt what is 'first' and 'second'.
183+
let original_span = tcx.def_span(original_instance.def_id()).data();
184+
let span = tcx.def_span(def_id).data();
185+
if original_span < span {
186+
throw_machine_stop!(TerminationInfo::MultipleSymbolDefinitions {
187+
link_name,
188+
first: original_span,
189+
first_crate: tcx.crate_name(original_cnum),
190+
second: span,
191+
second_crate: tcx.crate_name(cnum),
192+
});
202193
} else {
203-
// Skip over items without an explicitly defined symbol name.
204-
continue;
205-
};
206-
if symbol_name == link_name {
207-
if let Some((original_instance, original_cnum)) = instance_and_crate
208-
{
209-
// Make sure we are consistent wrt what is 'first' and 'second'.
210-
let original_span =
211-
tcx.def_span(original_instance.def_id()).data();
212-
let span = tcx.def_span(def_id).data();
213-
if original_span < span {
214-
throw_machine_stop!(
215-
TerminationInfo::MultipleSymbolDefinitions {
216-
link_name,
217-
first: original_span,
218-
first_crate: tcx.crate_name(original_cnum),
219-
second: span,
220-
second_crate: tcx.crate_name(cnum),
221-
}
222-
);
223-
} else {
224-
throw_machine_stop!(
225-
TerminationInfo::MultipleSymbolDefinitions {
226-
link_name,
227-
first: span,
228-
first_crate: tcx.crate_name(cnum),
229-
second: original_span,
230-
second_crate: tcx.crate_name(original_cnum),
231-
}
232-
);
233-
}
234-
}
235-
if !matches!(tcx.def_kind(def_id), DefKind::Fn | DefKind::AssocFn) {
236-
throw_ub_format!(
237-
"attempt to call an exported symbol that is not defined as a function"
238-
);
239-
}
240-
instance_and_crate = Some((ty::Instance::mono(tcx, def_id), cnum));
194+
throw_machine_stop!(TerminationInfo::MultipleSymbolDefinitions {
195+
link_name,
196+
first: span,
197+
first_crate: tcx.crate_name(cnum),
198+
second: original_span,
199+
second_crate: tcx.crate_name(original_cnum),
200+
});
241201
}
242202
}
203+
if !matches!(tcx.def_kind(def_id), DefKind::Fn | DefKind::AssocFn) {
204+
throw_ub_format!(
205+
"attempt to call an exported symbol that is not defined as a function"
206+
);
207+
}
208+
instance_and_crate = Some((ty::Instance::mono(tcx, def_id), cnum));
243209
}
244-
}
210+
Ok(())
211+
})?;
245212

246213
e.insert(instance_and_crate.map(|ic| ic.0))
247214
}

0 commit comments

Comments
 (0)