Skip to content

Commit e815550

Browse files
committed
minimal ptrace setup
Apply suggestions from code review Co-authored-by: Oli Scherer <github35764891676564198441@oli-obk.de> review comments fix possible hang
1 parent 7e51e9c commit e815550

File tree

16 files changed

+938
-39
lines changed

16 files changed

+938
-39
lines changed

Cargo.lock

Lines changed: 171 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ libc = "0.2"
4040
libffi = "4.0.0"
4141
libloading = "0.8"
4242

43+
[target.'cfg(target_os = "linux")'.dependencies]
44+
nix = { version = "0.30.1", features = ["mman", "ptrace", "signal"] }
45+
ipc-channel = "0.19.0"
46+
serde = { version = "1.0.219", features = ["derive"] }
47+
4348
[dev-dependencies]
4449
ui_test = "0.29.1"
4550
colored = "2"

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,11 @@ to Miri failing to detect cases of undefined behavior in a program.
419419
Finally, the flag is **unsound** in the sense that Miri stops tracking details such as
420420
initialization and provenance on memory shared with native code, so it is easily possible to write
421421
code that has UB which is missed by Miri.
422+
* `-Zmiri-force-old-native-lib-mode` disables the WIP improved native code access tracking. If for
423+
whatever reason enabling native calls leads to odd behaviours or causes Miri to panic, disabling
424+
the tracer *might* fix this. This will likely be removed once the tracer has been adequately
425+
battle-tested. Note that this flag is only meaningful on Linux systems; other Unixes (currently)
426+
exclusively use the old native-lib code.
422427
* `-Zmiri-measureme=<name>` enables `measureme` profiling for the interpreted program.
423428
This can be used to find which parts of your program are executing slowly under Miri.
424429
The profile is written out to a file inside a directory called `<name>`, and can be processed

src/alloc/isolated_alloc.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,18 @@ impl IsolatedAlloc {
266266
alloc::dealloc(ptr, layout);
267267
}
268268
}
269+
270+
/// Returns a vector of page addresses managed by the allocator.
271+
pub fn pages(&self) -> Vec<usize> {
272+
let mut pages: Vec<_> =
273+
self.page_ptrs.clone().into_iter().map(|p| p.expose_provenance()).collect();
274+
for (ptr, size) in &self.huge_ptrs {
275+
for i in 0..size / self.page_size {
276+
pages.push(ptr.expose_provenance().strict_add(i * self.page_size));
277+
}
278+
}
279+
pages
280+
}
269281
}
270282

271283
#[cfg(test)]

src/bin/miri.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,11 @@ impl rustc_driver::Callbacks for MiriCompilerCalls {
227227
} else {
228228
let return_code = miri::eval_entry(tcx, entry_def_id, entry_type, &config, None)
229229
.unwrap_or_else(|| {
230+
#[cfg(target_os = "linux")]
231+
miri::register_retcode_sv(rustc_driver::EXIT_FAILURE);
230232
tcx.dcx().abort_if_errors();
231233
rustc_driver::EXIT_FAILURE
232234
});
233-
234235
std::process::exit(return_code);
235236
}
236237

@@ -722,6 +723,8 @@ fn main() {
722723
} else {
723724
show_error!("-Zmiri-native-lib `{}` does not exist", filename);
724725
}
726+
} else if arg == "-Zmiri-force-old-native-lib-mode" {
727+
miri_config.force_old_native_lib = true;
725728
} else if let Some(param) = arg.strip_prefix("-Zmiri-num-cpus=") {
726729
let num_cpus = param
727730
.parse::<u32>()
@@ -792,6 +795,16 @@ fn main() {
792795

793796
debug!("rustc arguments: {:?}", rustc_args);
794797
debug!("crate arguments: {:?}", miri_config.args);
798+
#[cfg(target_os = "linux")]
799+
if !miri_config.native_lib.is_empty() && !miri_config.force_old_native_lib {
800+
// FIXME: This should display a diagnostic / warning on error
801+
// SAFETY: If any other threads exist at this point (namely for the ctrlc
802+
// handler), they will not interact with anything on the main rustc/Miri
803+
// thread in an async-signal-unsafe way such as by accessing shared
804+
// semaphores, etc.; the handler only calls `sleep()` and `exit()`, which
805+
// are async-signal-safe, as is accessing atomics
806+
let _ = unsafe { miri::init_sv() };
807+
}
795808
run_compiler_and_exit(
796809
&rustc_args,
797810
&mut MiriCompilerCalls::new(miri_config, many_seeds, genmc_config),

0 commit comments

Comments
 (0)