]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/src/shims/unix/linux/foreign_items.rs
Rollup merge of #99460 - JanBeh:PR_asref_asmut_docs, r=joshtriplett
[rust.git] / src / tools / miri / src / shims / unix / linux / foreign_items.rs
1 use rustc_span::Symbol;
2 use rustc_target::spec::abi::Abi;
3
4 use crate::*;
5 use shims::foreign_items::EmulateByNameResult;
6 use shims::unix::fs::EvalContextExt as _;
7 use shims::unix::linux::sync::futex;
8 use shims::unix::sync::EvalContextExt as _;
9 use shims::unix::thread::EvalContextExt as _;
10
11 impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriInterpCx<'mir, 'tcx> {}
12 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
13     fn emulate_foreign_item_by_name(
14         &mut self,
15         link_name: Symbol,
16         abi: Abi,
17         args: &[OpTy<'tcx, Provenance>],
18         dest: &PlaceTy<'tcx, Provenance>,
19     ) -> InterpResult<'tcx, EmulateByNameResult<'mir, 'tcx>> {
20         let this = self.eval_context_mut();
21
22         // See `fn emulate_foreign_item_by_name` in `shims/foreign_items.rs` for the general pattern.
23
24         match link_name.as_str() {
25             // errno
26             "__errno_location" => {
27                 let [] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
28                 let errno_place = this.last_error_place()?;
29                 this.write_scalar(errno_place.to_ref(this).to_scalar(), dest)?;
30             }
31
32             // File related shims (but also see "syscall" below for statx)
33             "readdir64" => {
34                 let [dirp] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
35                 let result = this.linux_readdir64(dirp)?;
36                 this.write_scalar(result, dest)?;
37             }
38             // Linux-only
39             "sync_file_range" => {
40                 let [fd, offset, nbytes, flags] =
41                     this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
42                 let result = this.sync_file_range(fd, offset, nbytes, flags)?;
43                 this.write_scalar(result, dest)?;
44             }
45
46             // Time related shims
47             "clock_gettime" => {
48                 // This is a POSIX function but it has only been tested on linux.
49                 let [clk_id, tp] =
50                     this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
51                 let result = this.clock_gettime(clk_id, tp)?;
52                 this.write_scalar(result, dest)?;
53             }
54
55             // Threading
56             "pthread_condattr_setclock" => {
57                 let [attr, clock_id] =
58                     this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
59                 let result = this.pthread_condattr_setclock(attr, clock_id)?;
60                 this.write_scalar(result, dest)?;
61             }
62             "pthread_condattr_getclock" => {
63                 let [attr, clock_id] =
64                     this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
65                 let result = this.pthread_condattr_getclock(attr, clock_id)?;
66                 this.write_scalar(result, dest)?;
67             }
68             "pthread_setname_np" => {
69                 let [thread, name] =
70                     this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
71                 let res =
72                     this.pthread_setname_np(this.read_scalar(thread)?, this.read_scalar(name)?)?;
73                 this.write_scalar(res, dest)?;
74             }
75
76             // Dynamically invoked syscalls
77             "syscall" => {
78                 // We do not use `check_shim` here because `syscall` is variadic. The argument
79                 // count is checked bellow.
80                 this.check_abi_and_shim_symbol_clash(abi, Abi::C { unwind: false }, link_name)?;
81                 // The syscall variadic function is legal to call with more arguments than needed,
82                 // extra arguments are simply ignored. The important check is that when we use an
83                 // argument, we have to also check all arguments *before* it to ensure that they
84                 // have the right type.
85
86                 let sys_getrandom = this.eval_libc("SYS_getrandom")?.to_machine_usize(this)?;
87
88                 let sys_statx = this.eval_libc("SYS_statx")?.to_machine_usize(this)?;
89
90                 let sys_futex = this.eval_libc("SYS_futex")?.to_machine_usize(this)?;
91
92                 if args.is_empty() {
93                     throw_ub_format!(
94                         "incorrect number of arguments for syscall: got 0, expected at least 1"
95                     );
96                 }
97                 match this.read_scalar(&args[0])?.to_machine_usize(this)? {
98                     // `libc::syscall(NR_GETRANDOM, buf.as_mut_ptr(), buf.len(), GRND_NONBLOCK)`
99                     // is called if a `HashMap` is created the regular way (e.g. HashMap<K, V>).
100                     id if id == sys_getrandom => {
101                         // The first argument is the syscall id, so skip over it.
102                         if args.len() < 4 {
103                             throw_ub_format!(
104                                 "incorrect number of arguments for `getrandom` syscall: got {}, expected at least 4",
105                                 args.len()
106                             );
107                         }
108                         getrandom(this, &args[1], &args[2], &args[3], dest)?;
109                     }
110                     // `statx` is used by `libstd` to retrieve metadata information on `linux`
111                     // instead of using `stat`,`lstat` or `fstat` as on `macos`.
112                     id if id == sys_statx => {
113                         // The first argument is the syscall id, so skip over it.
114                         if args.len() < 6 {
115                             throw_ub_format!(
116                                 "incorrect number of arguments for `statx` syscall: got {}, expected at least 6",
117                                 args.len()
118                             );
119                         }
120                         let result =
121                             this.linux_statx(&args[1], &args[2], &args[3], &args[4], &args[5])?;
122                         this.write_scalar(Scalar::from_machine_isize(result.into(), this), dest)?;
123                     }
124                     // `futex` is used by some synchonization primitives.
125                     id if id == sys_futex => {
126                         futex(this, &args[1..], dest)?;
127                     }
128                     id => {
129                         this.handle_unsupported(format!("can't execute syscall with ID {}", id))?;
130                         return Ok(EmulateByNameResult::AlreadyJumped);
131                     }
132                 }
133             }
134
135             // Miscelanneous
136             "getrandom" => {
137                 let [ptr, len, flags] =
138                     this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
139                 getrandom(this, ptr, len, flags, dest)?;
140             }
141             "sched_getaffinity" => {
142                 let [pid, cpusetsize, mask] =
143                     this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
144                 this.read_scalar(pid)?.to_i32()?;
145                 this.read_scalar(cpusetsize)?.to_machine_usize(this)?;
146                 this.deref_operand(mask)?;
147                 // FIXME: we just return an error; `num_cpus` then falls back to `sysconf`.
148                 let einval = this.eval_libc("EINVAL")?;
149                 this.set_last_error(einval)?;
150                 this.write_scalar(Scalar::from_i32(-1), dest)?;
151             }
152
153             // Incomplete shims that we "stub out" just to get pre-main initialization code to work.
154             // These shims are enabled only when the caller is in the standard library.
155             "pthread_getattr_np" if this.frame_in_std() => {
156                 let [_thread, _attr] =
157                     this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
158                 this.write_null(dest)?;
159             }
160
161             _ => return Ok(EmulateByNameResult::NotSupported),
162         };
163
164         Ok(EmulateByNameResult::NeedsJumping)
165     }
166 }
167
168 // Shims the linux `getrandom` syscall.
169 fn getrandom<'tcx>(
170     this: &mut MiriInterpCx<'_, 'tcx>,
171     ptr: &OpTy<'tcx, Provenance>,
172     len: &OpTy<'tcx, Provenance>,
173     flags: &OpTy<'tcx, Provenance>,
174     dest: &PlaceTy<'tcx, Provenance>,
175 ) -> InterpResult<'tcx> {
176     let ptr = this.read_pointer(ptr)?;
177     let len = this.read_scalar(len)?.to_machine_usize(this)?;
178
179     // The only supported flags are GRND_RANDOM and GRND_NONBLOCK,
180     // neither of which have any effect on our current PRNG.
181     // See <https://github.com/rust-lang/rust/pull/79196> for a discussion of argument sizes.
182     let _flags = this.read_scalar(flags)?.to_i32();
183
184     this.gen_random(ptr, len)?;
185     this.write_scalar(Scalar::from_machine_usize(len, this), dest)?;
186     Ok(())
187 }