Skip to content

Commit e857c65

Browse files
authored
Merge pull request #4401 from nia-e/barebones-ptrace
trace: add barebones ptrace setup
2 parents e8fccab + 2701dbb commit e857c65

File tree

16 files changed

+938
-39
lines changed

16 files changed

+938
-39
lines changed

src/tools/miri/Cargo.lock

Lines changed: 171 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ dependencies = [
8080
"rustc-demangle",
8181
]
8282

83+
[[package]]
84+
name = "bincode"
85+
version = "1.3.3"
86+
source = "registry+https://github.com/rust-lang/crates.io-index"
87+
checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad"
88+
dependencies = [
89+
"serde",
90+
]
91+
8392
[[package]]
8493
name = "bitflags"
8594
version = "2.9.0"
@@ -150,6 +159,12 @@ version = "1.0.0"
150159
source = "registry+https://github.com/rust-lang/crates.io-index"
151160
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
152161

162+
[[package]]
163+
name = "cfg_aliases"
164+
version = "0.2.1"
165+
source = "registry+https://github.com/rust-lang/crates.io-index"
166+
checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"
167+
153168
[[package]]
154169
name = "chrono"
155170
version = "0.4.40"
@@ -224,7 +239,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
224239
checksum = "117725a109d387c937a1533ce01b450cbde6b88abceea8473c4d7a85853cda3c"
225240
dependencies = [
226241
"lazy_static",
227-
"windows-sys",
242+
"windows-sys 0.59.0",
228243
]
229244

230245
[[package]]
@@ -243,7 +258,7 @@ dependencies = [
243258
"libc",
244259
"once_cell",
245260
"unicode-width 0.2.0",
246-
"windows-sys",
261+
"windows-sys 0.59.0",
247262
]
248263

249264
[[package]]
@@ -298,7 +313,7 @@ dependencies = [
298313
"libc",
299314
"option-ext",
300315
"redox_users",
301-
"windows-sys",
316+
"windows-sys 0.59.0",
302317
]
303318

304319
[[package]]
@@ -314,7 +329,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
314329
checksum = "976dd42dc7e85965fe702eb8164f21f450704bdde31faefd6471dba214cb594e"
315330
dependencies = [
316331
"libc",
317-
"windows-sys",
332+
"windows-sys 0.59.0",
318333
]
319334

320335
[[package]]
@@ -333,6 +348,12 @@ version = "2.3.0"
333348
source = "registry+https://github.com/rust-lang/crates.io-index"
334349
checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be"
335350

351+
[[package]]
352+
name = "fnv"
353+
version = "1.0.7"
354+
source = "registry+https://github.com/rust-lang/crates.io-index"
355+
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
356+
336357
[[package]]
337358
name = "generic-array"
338359
version = "0.14.7"
@@ -400,6 +421,25 @@ dependencies = [
400421
"generic-array",
401422
]
402423

424+
[[package]]
425+
name = "ipc-channel"
426+
version = "0.19.0"
427+
source = "registry+https://github.com/rust-lang/crates.io-index"
428+
checksum = "6fb8251fb7bcd9ccd3725ed8deae9fe7db8e586495c9eb5b0c52e6233e5e75ea"
429+
dependencies = [
430+
"bincode",
431+
"crossbeam-channel",
432+
"fnv",
433+
"lazy_static",
434+
"libc",
435+
"mio",
436+
"rand 0.8.5",
437+
"serde",
438+
"tempfile",
439+
"uuid",
440+
"windows",
441+
]
442+
403443
[[package]]
404444
name = "itoa"
405445
version = "1.0.15"
@@ -533,6 +573,18 @@ dependencies = [
533573
"adler",
534574
]
535575

576+
[[package]]
577+
name = "mio"
578+
version = "1.0.3"
579+
source = "registry+https://github.com/rust-lang/crates.io-index"
580+
checksum = "2886843bf800fba2e3377cff24abf6379b4c4d5c6681eaf9ea5b0d15090450bd"
581+
dependencies = [
582+
"libc",
583+
"log",
584+
"wasi 0.11.0+wasi-snapshot-preview1",
585+
"windows-sys 0.52.0",
586+
]
587+
536588
[[package]]
537589
name = "miri"
538590
version = "0.1.0"
@@ -544,19 +596,34 @@ dependencies = [
544596
"colored",
545597
"directories",
546598
"getrandom 0.3.2",
599+
"ipc-channel",
547600
"libc",
548601
"libffi",
549602
"libloading",
550603
"measureme",
604+
"nix",
551605
"rand 0.9.0",
552606
"regex",
553607
"rustc_version",
608+
"serde",
554609
"smallvec",
555610
"tempfile",
556611
"tikv-jemalloc-sys",
557612
"ui_test",
558613
]
559614

615+
[[package]]
616+
name = "nix"
617+
version = "0.30.1"
618+
source = "registry+https://github.com/rust-lang/crates.io-index"
619+
checksum = "74523f3a35e05aba87a1d978330aef40f67b0304ac79c1c00b294c9830543db6"
620+
dependencies = [
621+
"bitflags",
622+
"cfg-if",
623+
"cfg_aliases",
624+
"libc",
625+
]
626+
560627
[[package]]
561628
name = "num-traits"
562629
version = "0.2.19"
@@ -748,6 +815,8 @@ version = "0.8.5"
748815
source = "registry+https://github.com/rust-lang/crates.io-index"
749816
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
750817
dependencies = [
818+
"libc",
819+
"rand_chacha 0.3.1",
751820
"rand_core 0.6.4",
752821
]
753822

@@ -757,11 +826,21 @@ version = "0.9.0"
757826
source = "registry+https://github.com/rust-lang/crates.io-index"
758827
checksum = "3779b94aeb87e8bd4e834cee3650289ee9e0d5677f976ecdb6d219e5f4f6cd94"
759828
dependencies = [
760-
"rand_chacha",
829+
"rand_chacha 0.9.0",
761830
"rand_core 0.9.3",
762831
"zerocopy",
763832
]
764833

834+
[[package]]
835+
name = "rand_chacha"
836+
version = "0.3.1"
837+
source = "registry+https://github.com/rust-lang/crates.io-index"
838+
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
839+
dependencies = [
840+
"ppv-lite86",
841+
"rand_core 0.6.4",
842+
]
843+
765844
[[package]]
766845
name = "rand_chacha"
767846
version = "0.9.0"
@@ -777,6 +856,9 @@ name = "rand_core"
777856
version = "0.6.4"
778857
source = "registry+https://github.com/rust-lang/crates.io-index"
779858
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
859+
dependencies = [
860+
"getrandom 0.2.15",
861+
]
780862

781863
[[package]]
782864
name = "rand_core"
@@ -879,7 +961,7 @@ dependencies = [
879961
"errno",
880962
"libc",
881963
"linux-raw-sys",
882-
"windows-sys",
964+
"windows-sys 0.59.0",
883965
]
884966

885967
[[package]]
@@ -993,7 +1075,7 @@ dependencies = [
9931075
"getrandom 0.3.2",
9941076
"once_cell",
9951077
"rustix",
996-
"windows-sys",
1078+
"windows-sys 0.59.0",
9971079
]
9981080

9991081
[[package]]
@@ -1147,6 +1229,15 @@ version = "0.2.0"
11471229
source = "registry+https://github.com/rust-lang/crates.io-index"
11481230
checksum = "1fc81956842c57dac11422a97c3b8195a1ff727f06e85c84ed2e8aa277c9a0fd"
11491231

1232+
[[package]]
1233+
name = "uuid"
1234+
version = "1.16.0"
1235+
source = "registry+https://github.com/rust-lang/crates.io-index"
1236+
checksum = "458f7a779bf54acc9f347480ac654f68407d3aab21269a6e3c9f922acd9e2da9"
1237+
dependencies = [
1238+
"getrandom 0.3.2",
1239+
]
1240+
11501241
[[package]]
11511242
name = "valuable"
11521243
version = "0.1.1"
@@ -1241,6 +1332,79 @@ dependencies = [
12411332
"wasm-bindgen",
12421333
]
12431334

1335+
[[package]]
1336+
name = "windows"
1337+
version = "0.58.0"
1338+
source = "registry+https://github.com/rust-lang/crates.io-index"
1339+
checksum = "dd04d41d93c4992d421894c18c8b43496aa748dd4c081bac0dc93eb0489272b6"
1340+
dependencies = [
1341+
"windows-core",
1342+
"windows-targets",
1343+
]
1344+
1345+
[[package]]
1346+
name = "windows-core"
1347+
version = "0.58.0"
1348+
source = "registry+https://github.com/rust-lang/crates.io-index"
1349+
checksum = "6ba6d44ec8c2591c134257ce647b7ea6b20335bf6379a27dac5f1641fcf59f99"
1350+
dependencies = [
1351+
"windows-implement",
1352+
"windows-interface",
1353+
"windows-result",
1354+
"windows-strings",
1355+
"windows-targets",
1356+
]
1357+
1358+
[[package]]
1359+
name = "windows-implement"
1360+
version = "0.58.0"
1361+
source = "registry+https://github.com/rust-lang/crates.io-index"
1362+
checksum = "2bbd5b46c938e506ecbce286b6628a02171d56153ba733b6c741fc627ec9579b"
1363+
dependencies = [
1364+
"proc-macro2",
1365+
"quote",
1366+
"syn",
1367+
]
1368+
1369+
[[package]]
1370+
name = "windows-interface"
1371+
version = "0.58.0"
1372+
source = "registry+https://github.com/rust-lang/crates.io-index"
1373+
checksum = "053c4c462dc91d3b1504c6fe5a726dd15e216ba718e84a0e46a88fbe5ded3515"
1374+
dependencies = [
1375+
"proc-macro2",
1376+
"quote",
1377+
"syn",
1378+
]
1379+
1380+
[[package]]
1381+
name = "windows-result"
1382+
version = "0.2.0"
1383+
source = "registry+https://github.com/rust-lang/crates.io-index"
1384+
checksum = "1d1043d8214f791817bab27572aaa8af63732e11bf84aa21a45a78d6c317ae0e"
1385+
dependencies = [
1386+
"windows-targets",
1387+
]
1388+
1389+
[[package]]
1390+
name = "windows-strings"
1391+
version = "0.1.0"
1392+
source = "registry+https://github.com/rust-lang/crates.io-index"
1393+
checksum = "4cd9b125c486025df0eabcb585e62173c6c9eddcec5d117d3b6e8c30e2ee4d10"
1394+
dependencies = [
1395+
"windows-result",
1396+
"windows-targets",
1397+
]
1398+
1399+
[[package]]
1400+
name = "windows-sys"
1401+
version = "0.52.0"
1402+
source = "registry+https://github.com/rust-lang/crates.io-index"
1403+
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
1404+
dependencies = [
1405+
"windows-targets",
1406+
]
1407+
12441408
[[package]]
12451409
name = "windows-sys"
12461410
version = "0.59.0"

src/tools/miri/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"

src/tools/miri/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/tools/miri/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/tools/miri/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)