4 impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
5 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
6 fn emulate_foreign_item_by_name(
9 args: &[OpTy<'tcx, Tag>],
10 dest: PlaceTy<'tcx, Tag>,
11 _ret: mir::BasicBlock,
12 ) -> InterpResult<'tcx, bool> {
13 let this = self.eval_context_mut();
16 "__errno_location" => {
17 let errno_place = this.machine.last_error.unwrap();
18 this.write_scalar(errno_place.to_ref().to_scalar()?, dest)?;
23 let result = this.close(args[0])?;
24 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
29 // This is a POSIX function but it has only been tested on linux.
31 let result = this.clock_gettime(args[0], args[1])?;
32 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
36 "pthread_getattr_np" => {
37 this.write_null(dest)?;
41 let sys_getrandom = this
42 .eval_path_scalar(&["libc", "SYS_getrandom"])?
43 .expect("Failed to get libc::SYS_getrandom")
44 .to_machine_usize(this)?;
47 .eval_path_scalar(&["libc", "SYS_statx"])?
48 .expect("Failed to get libc::SYS_statx")
49 .to_machine_usize(this)?;
51 match this.read_scalar(args[0])?.to_machine_usize(this)? {
52 // `libc::syscall(NR_GETRANDOM, buf.as_mut_ptr(), buf.len(), GRND_NONBLOCK)`
53 // is called if a `HashMap` is created the regular way (e.g. HashMap<K, V>).
54 id if id == sys_getrandom => {
55 // The first argument is the syscall id,
57 getrandom(this, &args[1..], dest)?;
59 id if id == sys_statx => {
60 // The first argument is the syscall id,
62 let result = this.linux_statx(args[1], args[2], args[3], args[4], args[5])?;
63 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
65 id => throw_unsup_format!("miri does not support syscall ID {}", id),
70 getrandom(this, args, dest)?;
73 "sched_getaffinity" => {
74 // Return an error; `num_cpus` then falls back to `sysconf`.
75 this.write_scalar(Scalar::from_int(-1, dest.layout.size), dest)?;
78 _ => throw_unsup_format!("can't call foreign function: {}", link_name),
85 // Shims the linux 'getrandom()' syscall.
87 this: &mut MiriEvalContext<'_, 'tcx>,
88 args: &[OpTy<'tcx, Tag>],
89 dest: PlaceTy<'tcx, Tag>,
90 ) -> InterpResult<'tcx> {
91 let ptr = this.read_scalar(args[0])?.not_undef()?;
92 let len = this.read_scalar(args[1])?.to_machine_usize(this)?;
94 // The only supported flags are GRND_RANDOM and GRND_NONBLOCK,
95 // neither of which have any effect on our current PRNG.
96 let _flags = this.read_scalar(args[2])?.to_i32()?;
98 this.gen_random(ptr, len as usize)?;
99 this.write_scalar(Scalar::from_uint(len, dest.layout.size), dest)?;