Skip to content

Commit a7818ab

Browse files
committed
minor tweaks and comments
1 parent 7daf0e6 commit a7818ab

File tree

6 files changed

+35
-25
lines changed

6 files changed

+35
-25
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,8 @@ trait EvalContextPrivExt<'tcx>: MiriInterpCxExt<'tcx> {
677677
fn run_on_stack_empty(&mut self) -> InterpResult<'tcx, Poll<()>> {
678678
let this = self.eval_context_mut();
679679
// Inform GenMC that a thread has finished all user code. GenMC needs to know this for scheduling.
680+
// FIXME(GenMC): Thread-local destructors *are* user code, so this is odd. Also now that we
681+
// support pre-main constructors, it can get called there as well.
680682
if let Some(genmc_ctx) = this.machine.data_race.as_genmc_ref() {
681683
let thread_id = this.active_thread();
682684
genmc_ctx.handle_thread_stack_empty(thread_id);

src/tools/miri/src/eval.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_session::config::EntryFnType;
1818
use crate::concurrency::GenmcCtx;
1919
use crate::concurrency::thread::TlsAllocAction;
2020
use crate::diagnostics::report_leaks;
21-
use crate::shims::{ctor, tls};
21+
use crate::shims::{global_ctor, tls};
2222
use crate::*;
2323

2424
#[derive(Copy, Clone, Debug)]
@@ -219,9 +219,11 @@ impl Default for MiriConfig {
219219
#[derive(Debug)]
220220
enum MainThreadState<'tcx> {
221221
GlobalCtors {
222-
ctor_state: ctor::GlobalCtorState<'tcx>,
222+
ctor_state: global_ctor::GlobalCtorState<'tcx>,
223+
/// The main function to call.
223224
entry_id: DefId,
224225
entry_type: MiriEntryFnType,
226+
/// Arguments passed to `main`.
225227
argc: ImmTy<'tcx>,
226228
argv: ImmTy<'tcx>,
227229
},
@@ -324,12 +326,19 @@ pub fn create_ecx<'tcx>(
324326
MiriMachine::new(config, layout_cx, genmc_ctx),
325327
);
326328

327-
// First argument is constructed later, because it's skipped for `miri_start.`
329+
// Make sure we have MIR. We check MIR for some stable monomorphic function in libcore.
330+
let sentinel =
331+
helpers::try_resolve_path(tcx, &["core", "ascii", "escape_default"], Namespace::ValueNS);
332+
if !matches!(sentinel, Some(s) if tcx.is_mir_available(s.def.def_id())) {
333+
tcx.dcx().fatal(
334+
"the current sysroot was built without `-Zalways-encode-mir`, or libcore seems missing. \
335+
Use `cargo miri setup` to prepare a sysroot that is suitable for Miri."
336+
);
337+
}
328338

329-
// Second argument (argc): length of `config.args`.
339+
// Compute argc and argv from `config.args`.
330340
let argc =
331341
ImmTy::from_int(i64::try_from(config.args.len()).unwrap(), ecx.machine.layouts.isize);
332-
// Third argument (`argv`): created from `config.args`.
333342
let argv = {
334343
// Put each argument in memory, collect pointers.
335344
let mut argvs = Vec::<Immediate<Provenance>>::with_capacity(config.args.len());
@@ -354,7 +363,7 @@ pub fn create_ecx<'tcx>(
354363
ecx.write_immediate(arg, &place)?;
355364
}
356365
ecx.mark_immutable(&argvs_place);
357-
// Store `argc` and `argv` for macOS `_NSGetArg{c,v}`.
366+
// Store `argc` and `argv` for macOS `_NSGetArg{c,v}`, and for the GC to see them.
358367
{
359368
let argc_place =
360369
ecx.allocate(ecx.machine.layouts.isize, MiriMemoryKind::Machine.into())?;
@@ -369,7 +378,7 @@ pub fn create_ecx<'tcx>(
369378
ecx.machine.argv = Some(argv_place.ptr());
370379
}
371380
// Store command line as UTF-16 for Windows `GetCommandLineW`.
372-
{
381+
if tcx.sess.target.os == "windows" {
373382
// Construct a command string with all the arguments.
374383
let cmd_utf16: Vec<u16> = args_to_utf16_command_string(config.args.iter());
375384

@@ -392,28 +401,20 @@ pub fn create_ecx<'tcx>(
392401

393402
// Some parts of initialization require a full `InterpCx`.
394403
MiriMachine::late_init(&mut ecx, config, {
395-
let mut state = MainThreadState::GlobalCtors {
404+
let mut main_thread_state = MainThreadState::GlobalCtors {
396405
entry_id,
397406
entry_type,
398407
argc,
399408
argv,
400-
ctor_state: ctor::GlobalCtorState::default(),
409+
ctor_state: global_ctor::GlobalCtorState::default(),
401410
};
402411

403412
// Cannot capture anything GC-relevant here.
404-
Box::new(move |m| state.on_main_stack_empty(m))
413+
// `argc` and `argv` *are* GC_relevant, but they also get stored in `machine.argc` and
414+
// `machine.argv` so we are good.
415+
Box::new(move |m| main_thread_state.on_main_stack_empty(m))
405416
})?;
406417

407-
// Make sure we have MIR. We check MIR for some stable monomorphic function in libcore.
408-
let sentinel =
409-
helpers::try_resolve_path(tcx, &["core", "ascii", "escape_default"], Namespace::ValueNS);
410-
if !matches!(sentinel, Some(s) if tcx.is_mir_available(s.def.def_id())) {
411-
tcx.dcx().fatal(
412-
"the current sysroot was built without `-Zalways-encode-mir`, or libcore seems missing. \
413-
Use `cargo miri setup` to prepare a sysroot that is suitable for Miri."
414-
);
415-
}
416-
417418
interp_ok(ecx)
418419
}
419420

src/tools/miri/src/shims/ctor.rs renamed to src/tools/miri/src/shims/global_ctor.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,12 @@ impl<'tcx> GlobalCtorState<'tcx> {
4545

4646
segment_name == Some("__DATA")
4747
&& section_name == Some("__mod_init_func")
48-
// The `mod_init_funcs` directive ensures that the `S_MOD_INIT_FUNC_POINTERS` flag
49-
// is set on the section, but it is not strictly required.
48+
// The `mod_init_funcs` directive ensures that the
49+
// `S_MOD_INIT_FUNC_POINTERS` flag is set on the section. LLVM
50+
// adds this automatically so we currently do not require it.
51+
// FIXME: is this guaranteed LLVM behavior? If not, we shouldn't
52+
// implicitly add it here. Also see
53+
// <https://github.com/rust-lang/miri/pull/4459#discussion_r2200115629>.
5054
&& matches!(section_type, None | Some("mod_init_funcs"))
5155
})?,
5256

src/tools/miri/src/shims/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ mod wasi;
1111
mod windows;
1212
mod x86;
1313

14-
pub mod ctor;
1514
pub mod env;
1615
pub mod extern_static;
1716
pub mod foreign_items;
17+
pub mod global_ctor;
1818
pub mod io_error;
1919
pub mod os_str;
2020
pub mod panic;

src/tools/miri/tests/pass/alloc-access-tracking.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![no_std]
22
#![no_main]
3-
//@compile-flags: -Zmiri-track-alloc-id=20 -Zmiri-track-alloc-accesses -Cpanic=abort
4-
//@normalize-stderr-test: "id 20" -> "id $$ALLOC"
3+
//@compile-flags: -Zmiri-track-alloc-id=19 -Zmiri-track-alloc-accesses -Cpanic=abort
4+
//@normalize-stderr-test: "id 19" -> "id $$ALLOC"
55
//@only-target: linux # alloc IDs differ between OSes (due to extern static allocations)
66

77
extern "Rust" {

src/tools/miri/tests/pass/shims/ctor.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ unsafe extern "C" fn ctor() {
66
COUNT.fetch_add(1, Ordering::Relaxed);
77
}
88

9+
#[rustfmt::skip]
910
macro_rules! ctor {
1011
($ident:ident = $ctor:ident) => {
1112
#[cfg_attr(
@@ -27,6 +28,8 @@ macro_rules! ctor {
2728
#[cfg_attr(windows, link_section = ".CRT$XCU")]
2829
#[cfg_attr(
2930
any(target_os = "macos", target_os = "ios"),
31+
// We do not set the `mod_init_funcs` flag here since ctor/inventory also do not do
32+
// that. See <https://github.com/rust-lang/miri/pull/4459#discussion_r2200115629>.
3033
link_section = "__DATA,__mod_init_func"
3134
)]
3235
#[used]

0 commit comments

Comments
 (0)