Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit c905ef4

Browse files
committed
make flag checks reobust against multi-bit flags
1 parent 0876519 commit c905ef4

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -562,17 +562,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
562562
let mut mirror = access_mode;
563563

564564
let o_append = this.eval_libc_i32("O_APPEND")?;
565-
if flag & o_append != 0 {
565+
if flag & o_append == o_append {
566566
options.append(true);
567567
mirror |= o_append;
568568
}
569569
let o_trunc = this.eval_libc_i32("O_TRUNC")?;
570-
if flag & o_trunc != 0 {
570+
if flag & o_trunc == o_trunc {
571571
options.truncate(true);
572572
mirror |= o_trunc;
573573
}
574574
let o_creat = this.eval_libc_i32("O_CREAT")?;
575-
if flag & o_creat != 0 {
575+
if flag & o_creat == o_creat {
576576
// Get the mode. On macOS, the argument type `mode_t` is actually `u16`, but
577577
// C integer promotion rules mean that on the ABI level, it gets passed as `u32`
578578
// (see https://github.com/rust-lang/rust/issues/71915).
@@ -592,22 +592,22 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
592592
mirror |= o_creat;
593593

594594
let o_excl = this.eval_libc_i32("O_EXCL")?;
595-
if flag & o_excl != 0 {
595+
if flag & o_excl == o_excl {
596596
mirror |= o_excl;
597597
options.create_new(true);
598598
} else {
599599
options.create(true);
600600
}
601601
}
602602
let o_cloexec = this.eval_libc_i32("O_CLOEXEC")?;
603-
if flag & o_cloexec != 0 {
603+
if flag & o_cloexec == o_cloexec {
604604
// We do not need to do anything for this flag because `std` already sets it.
605605
// (Technically we do not support *not* setting this flag, but we ignore that.)
606606
mirror |= o_cloexec;
607607
}
608608
if this.tcx.sess.target.os == "linux" {
609609
let o_tmpfile = this.eval_libc_i32("O_TMPFILE")?;
610-
if flag & o_tmpfile != 0 {
610+
if flag & o_tmpfile == o_tmpfile {
611611
// if the flag contains `O_TMPFILE` then we return a graceful error
612612
let eopnotsupp = this.eval_libc("EOPNOTSUPP")?;
613613
this.set_last_error(eopnotsupp)?;
@@ -1020,7 +1020,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
10201020

10211021
let path = this.read_path_from_c_str(pathname_ptr)?.into_owned();
10221022
// See <https://github.com/rust-lang/rust/pull/79196> for a discussion of argument sizes.
1023-
let empty_path_flag = flags & this.eval_libc("AT_EMPTY_PATH")?.to_i32()? != 0;
1023+
let at_ampty_path = this.eval_libc_i32("AT_EMPTY_PATH")?;
1024+
let empty_path_flag = flags & at_ampty_path == at_ampty_path;
10241025
// We only support:
10251026
// * interpreting `path` as an absolute directory,
10261027
// * interpreting `path` as a path relative to `dirfd` when the latter is `AT_FDCWD`, or

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ pub fn futex<'tcx>(
9090
let timeout_time = if this.ptr_is_null(timeout.ptr)? {
9191
None
9292
} else {
93-
if op & futex_realtime != 0 {
93+
let realtime = op & futex_realtime == futex_realtime;
94+
if realtime {
9495
this.check_no_isolation(
9596
"`futex` syscall with `op=FUTEX_WAIT` and non-null timeout with `FUTEX_CLOCK_REALTIME`",
9697
)?;
@@ -106,14 +107,14 @@ pub fn futex<'tcx>(
106107
};
107108
Some(if wait_bitset {
108109
// FUTEX_WAIT_BITSET uses an absolute timestamp.
109-
if op & futex_realtime != 0 {
110+
if realtime {
110111
Time::RealTime(SystemTime::UNIX_EPOCH.checked_add(duration).unwrap())
111112
} else {
112113
Time::Monotonic(this.machine.clock.anchor().checked_add(duration).unwrap())
113114
}
114115
} else {
115116
// FUTEX_WAIT uses a relative timestamp.
116-
if op & futex_realtime != 0 {
117+
if realtime {
117118
Time::RealTime(SystemTime::now().checked_add(duration).unwrap())
118119
} else {
119120
Time::Monotonic(this.machine.clock.now().checked_add(duration).unwrap())

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
7676
this.read_scalar(handle)?.to_machine_isize(this)?;
7777
let flags = this.read_scalar(flags)?.to_u32()?;
7878
let size = this.read_scalar(size)?.to_machine_usize(this)?;
79-
let zero_init = (flags & 0x00000008) != 0; // HEAP_ZERO_MEMORY
79+
let heap_zero_memory = 0x00000008; // HEAP_ZERO_MEMORY
80+
let zero_init = (flags & heap_zero_memory) == heap_zero_memory;
8081
let res = this.malloc(size, zero_init, MiriMemoryKind::WinHeap)?;
8182
this.write_pointer(res, dest)?;
8283
}

0 commit comments

Comments
 (0)