miri/shims/unix/linux_like/
syscall.rs

1use rustc_abi::CanonAbi;
2use rustc_middle::ty::Ty;
3use rustc_span::Symbol;
4use rustc_target::callconv::FnAbi;
5
6use crate::helpers::check_min_vararg_count;
7use crate::shims::unix::linux_like::eventfd::EvalContextExt as _;
8use crate::shims::unix::linux_like::sync::futex;
9use crate::*;
10
11pub fn syscall<'tcx>(
12    ecx: &mut MiriInterpCx<'tcx>,
13    link_name: Symbol,
14    abi: &FnAbi<'tcx, Ty<'tcx>>,
15    args: &[OpTy<'tcx>],
16    dest: &MPlaceTy<'tcx>,
17) -> InterpResult<'tcx> {
18    let ([op], varargs) = ecx.check_shim_variadic(abi, CanonAbi::C, link_name, args)?;
19    // The syscall variadic function is legal to call with more arguments than needed,
20    // extra arguments are simply ignored. The important check is that when we use an
21    // argument, we have to also check all arguments *before* it to ensure that they
22    // have the right type.
23
24    let sys_getrandom = ecx.eval_libc("SYS_getrandom").to_target_usize(ecx)?;
25    let sys_futex = ecx.eval_libc("SYS_futex").to_target_usize(ecx)?;
26    let sys_eventfd2 = ecx.eval_libc("SYS_eventfd2").to_target_usize(ecx)?;
27
28    match ecx.read_target_usize(op)? {
29        // `libc::syscall(NR_GETRANDOM, buf.as_mut_ptr(), buf.len(), GRND_NONBLOCK)`
30        // is called if a `HashMap` is created the regular way (e.g. HashMap<K, V>).
31        num if num == sys_getrandom => {
32            // Used by getrandom 0.1
33            // The first argument is the syscall id, so skip over it.
34            let [ptr, len, flags] = check_min_vararg_count("syscall(SYS_getrandom, ...)", varargs)?;
35
36            let ptr = ecx.read_pointer(ptr)?;
37            let len = ecx.read_target_usize(len)?;
38            // The only supported flags are GRND_RANDOM and GRND_NONBLOCK,
39            // neither of which have any effect on our current PRNG.
40            // See <https://github.com/rust-lang/rust/pull/79196> for a discussion of argument sizes.
41            let _flags = ecx.read_scalar(flags)?.to_i32()?;
42
43            ecx.gen_random(ptr, len)?;
44            ecx.write_scalar(Scalar::from_target_usize(len, ecx), dest)?;
45        }
46        // `futex` is used by some synchronization primitives.
47        num if num == sys_futex => {
48            futex(ecx, varargs, dest)?;
49        }
50        num if num == sys_eventfd2 => {
51            let [initval, flags] = check_min_vararg_count("syscall(SYS_evetfd2, ...)", varargs)?;
52
53            let result = ecx.eventfd(initval, flags)?;
54            ecx.write_int(result.to_i32()?, dest)?;
55        }
56        num => {
57            throw_unsup_format!("syscall: unsupported syscall number {num}");
58        }
59    };
60
61    interp_ok(())
62}