Skip to content

Commit 99f77ac

Browse files
committed
make statx a regular function (so we don't need to support the syscall)
1 parent 8bffd88 commit 99f77ac

File tree

5 files changed

+23
-26
lines changed

5 files changed

+23
-26
lines changed

src/tools/miri/src/shims/extern_static.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
1616

1717
/// Zero-initialized pointer-sized extern statics are pretty common.
1818
/// Most of them are for weak symbols, which we all set to null (indicating that the
19-
/// symbol is not supported, and triggering fallback code which ends up calling a
20-
/// syscall that we do support).
19+
/// symbol is not supported, and triggering fallback code which ends up calling
20+
/// some other shim that we do support).
2121
fn null_ptr_extern_statics(
2222
this: &mut MiriInterpCx<'mir, 'tcx>,
2323
names: &[&str],
@@ -59,8 +59,9 @@ impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
5959
"linux" => {
6060
Self::null_ptr_extern_statics(
6161
this,
62-
&["__cxa_thread_atexit_impl", "getrandom", "statx", "__clock_gettime64"],
62+
&["__cxa_thread_atexit_impl", "__clock_gettime64"],
6363
)?;
64+
Self::weak_symbol_extern_statics(this, &["getrandom", "statx"])?;
6465
// "environ"
6566
let environ = this.machine.env_vars.unix().environ();
6667
Self::add_extern_static(this, "environ", environ);

src/tools/miri/src/shims/unix/fd.rs

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use crate::*;
1717
pub trait FileDescription: std::fmt::Debug + Any {
1818
fn name(&self) -> &'static str;
1919

20+
/// Reads as much as possible into the given buffer, and returns the number of bytes read.
2021
fn read<'tcx>(
2122
&mut self,
2223
_communicate_allowed: bool,
@@ -26,6 +27,7 @@ pub trait FileDescription: std::fmt::Debug + Any {
2627
throw_unsup_format!("cannot read from {}", self.name());
2728
}
2829

30+
/// Writes as much as possible from the given buffer, and returns the number of bytes written.
2931
fn write<'tcx>(
3032
&mut self,
3133
_communicate_allowed: bool,
@@ -35,6 +37,8 @@ pub trait FileDescription: std::fmt::Debug + Any {
3537
throw_unsup_format!("cannot write to {}", self.name());
3638
}
3739

40+
/// Seeks to the given offset (which can be relative to the beginning, end, or current position).
41+
/// Returns the new position from the start of the stream.
3842
fn seek<'tcx>(
3943
&mut self,
4044
_communicate_allowed: bool,

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

+8-17
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use shims::unix::linux::mem::EvalContextExt as _;
1212
use shims::unix::linux::sync::futex;
1313

1414
pub fn is_dyn_sym(name: &str) -> bool {
15-
matches!(name, "getrandom")
15+
matches!(name, "getrandom" | "statx")
1616
}
1717

1818
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriInterpCx<'mir, 'tcx> {}
@@ -29,7 +29,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
2929
// See `fn emulate_foreign_item_inner` in `shims/foreign_items.rs` for the general pattern.
3030

3131
match link_name.as_str() {
32-
// File related shims (but also see "syscall" below for statx)
32+
// File related shims
3333
"readdir64" => {
3434
let [dirp] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
3535
let result = this.linux_readdir64(dirp)?;
@@ -41,6 +41,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
4141
let result = this.sync_file_range(fd, offset, nbytes, flags)?;
4242
this.write_scalar(result, dest)?;
4343
}
44+
"statx" => {
45+
let [dirfd, pathname, flags, mask, statxbuf] =
46+
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
47+
let result = this.linux_statx(dirfd, pathname, flags, mask, statxbuf)?;
48+
this.write_scalar(Scalar::from_i32(result), dest)?;
49+
}
4450

4551
// epoll, eventfd
4652
"epoll_create1" => {
@@ -113,7 +119,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
113119
// have the right type.
114120

115121
let sys_getrandom = this.eval_libc("SYS_getrandom").to_target_usize(this)?;
116-
let sys_statx = this.eval_libc("SYS_statx").to_target_usize(this)?;
117122
let sys_futex = this.eval_libc("SYS_futex").to_target_usize(this)?;
118123

119124
if args.is_empty() {
@@ -134,20 +139,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
134139
}
135140
getrandom(this, &args[1], &args[2], &args[3], dest)?;
136141
}
137-
// `statx` is used by `libstd` to retrieve metadata information on `linux`
138-
// instead of using `stat`,`lstat` or `fstat` as on `macos`.
139-
id if id == sys_statx => {
140-
// The first argument is the syscall id, so skip over it.
141-
if args.len() < 6 {
142-
throw_ub_format!(
143-
"incorrect number of arguments for `statx` syscall: got {}, expected at least 6",
144-
args.len()
145-
);
146-
}
147-
let result =
148-
this.linux_statx(&args[1], &args[2], &args[3], &args[4], &args[5])?;
149-
this.write_scalar(Scalar::from_target_isize(result.into(), this), dest)?;
150-
}
151142
// `futex` is used by some synchronization primitives.
152143
id if id == sys_futex => {
153144
futex(this, &args[1..], dest)?;

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#![feature(start)]
22
#![no_std]
3-
//@compile-flags: -Zmiri-track-alloc-id=18 -Zmiri-track-alloc-accesses -Cpanic=abort
4-
//@only-target-linux: alloc IDs differ between OSes for some reason
3+
//@compile-flags: -Zmiri-track-alloc-id=20 -Zmiri-track-alloc-accesses -Cpanic=abort
4+
//@normalize-stderr-test: "id 20" -> "id $$ALLOC"
5+
//@only-target-linux: alloc IDs differ between OSes (due to extern static allocations)
56

67
extern "Rust" {
78
fn miri_alloc(size: usize, align: usize) -> *mut u8;

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ note: tracking was triggered
22
--> $DIR/alloc-access-tracking.rs:LL:CC
33
|
44
LL | let ptr = miri_alloc(123, 1);
5-
| ^^^^^^^^^^^^^^^^^^ created Miri bare-metal heap allocation of 123 bytes (alignment ALIGN bytes) with id 18
5+
| ^^^^^^^^^^^^^^^^^^ created Miri bare-metal heap allocation of 123 bytes (alignment ALIGN bytes) with id $ALLOC
66
|
77
= note: BACKTRACE:
88
= note: inside `start` at $DIR/alloc-access-tracking.rs:LL:CC
@@ -11,7 +11,7 @@ note: tracking was triggered
1111
--> $DIR/alloc-access-tracking.rs:LL:CC
1212
|
1313
LL | *ptr = 42; // Crucially, only a write is printed here, no read!
14-
| ^^^^^^^^^ write access to allocation with id 18
14+
| ^^^^^^^^^ write access to allocation with id $ALLOC
1515
|
1616
= note: BACKTRACE:
1717
= note: inside `start` at $DIR/alloc-access-tracking.rs:LL:CC
@@ -20,7 +20,7 @@ note: tracking was triggered
2020
--> $DIR/alloc-access-tracking.rs:LL:CC
2121
|
2222
LL | assert_eq!(*ptr, 42);
23-
| ^^^^^^^^^^^^^^^^^^^^ read access to allocation with id 18
23+
| ^^^^^^^^^^^^^^^^^^^^ read access to allocation with id $ALLOC
2424
|
2525
= note: BACKTRACE:
2626
= note: inside `start` at RUSTLIB/core/src/macros/mod.rs:LL:CC
@@ -30,7 +30,7 @@ note: tracking was triggered
3030
--> $DIR/alloc-access-tracking.rs:LL:CC
3131
|
3232
LL | miri_dealloc(ptr, 123, 1);
33-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ freed allocation with id 18
33+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ freed allocation with id $ALLOC
3434
|
3535
= note: BACKTRACE:
3636
= note: inside `start` at $DIR/alloc-access-tracking.rs:LL:CC

0 commit comments

Comments
 (0)