Skip to content

Commit 73d58aa

Browse files
committed
Auto merge of #3937 - FrankReh:syscall-eventfd2, r=RalfJung
syscall eventfd2 Add plumbing so syscall of SYS_eventfd2 can take advantage of the eventfd support already built into Miri.
2 parents e0bd116 + 3e04fd4 commit 73d58aa

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/shims/unix/linux/foreign_items.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
122122

123123
let sys_getrandom = this.eval_libc("SYS_getrandom").to_target_usize(this)?;
124124
let sys_futex = this.eval_libc("SYS_futex").to_target_usize(this)?;
125+
let sys_eventfd2 = this.eval_libc("SYS_eventfd2").to_target_usize(this)?;
125126

126127
if args.is_empty() {
127128
throw_ub_format!(
@@ -155,6 +156,17 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
155156
id if id == sys_futex => {
156157
futex(this, &args[1..], dest)?;
157158
}
159+
id if id == sys_eventfd2 => {
160+
let [_, initval, flags, ..] = args else {
161+
throw_ub_format!(
162+
"incorrect number of arguments for `eventfd2` syscall: got {}, expected at least 3",
163+
args.len()
164+
);
165+
};
166+
167+
let result = this.eventfd(initval, flags)?;
168+
this.write_int(result.to_i32()?, dest)?;
169+
}
158170
id => {
159171
this.handle_unsupported_foreign_item(format!(
160172
"can't execute syscall with ID {id}"

tests/pass-dep/libc/libc-eventfd.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::thread;
1010
fn main() {
1111
test_read_write();
1212
test_race();
13+
test_syscall();
1314
}
1415

1516
fn read_bytes<const N: usize>(fd: i32, buf: &mut [u8; N]) -> i32 {
@@ -109,3 +110,11 @@ fn test_race() {
109110
thread::yield_now();
110111
thread1.join().unwrap();
111112
}
113+
114+
// This is a test for calling eventfd2 through a syscall.
115+
fn test_syscall() {
116+
let initval = 0 as libc::c_uint;
117+
let flags = (libc::EFD_CLOEXEC | libc::EFD_NONBLOCK) as libc::c_int;
118+
let fd = unsafe { libc::syscall(libc::SYS_eventfd2, initval, flags) };
119+
assert_ne!(fd, -1);
120+
}

0 commit comments

Comments
 (0)