]> git.lizzy.rs Git - rust.git/blobdiff - src/shims/foreign_items/posix/linux.rs
avoid using unchecked casts or arithmetic
[rust.git] / src / shims / foreign_items / posix / linux.rs
index 31a0a815891eca39cc58c8ab84088147280e51e9..023fee4ca7b1eef68bc37bed701bb41680e52751 100644 (file)
@@ -1,4 +1,5 @@
 use crate::*;
+use rustc::mir;
 
 impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
@@ -7,7 +8,8 @@ fn emulate_foreign_item_by_name(
         link_name: &str,
         args: &[OpTy<'tcx, Tag>],
         dest: PlaceTy<'tcx, Tag>,
-    ) -> InterpResult<'tcx> {
+        _ret: mir::BasicBlock,
+    ) -> InterpResult<'tcx, bool> {
         let this = self.eval_context_mut();
 
         match link_name {
@@ -16,34 +18,103 @@ fn emulate_foreign_item_by_name(
                 this.write_scalar(errno_place.to_ref().to_scalar()?, dest)?;
             }
 
-            // File related shims
-            "open64" => {
-                let result = this.open(args[0], args[1])?;
-                this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
-            }
+            // File related shims (but also see "syscall" below for statx)
 
+            // The only reason this is not in the `posix` module is because the `macos` item has a
+            // different name.
             "close" => {
                 let result = this.close(args[0])?;
                 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
             }
 
-            "lseek64" => {
-                let result = this.lseek64(args[0], args[1], args[2])?;
+            // The only reason this is not in the `posix` module is because the `macos` item has a
+            // different name.
+            "opendir" => {
+                let result = this.opendir(args[0])?;
+                this.write_scalar(result, dest)?;
+            }
+
+            // The `macos` module has a parallel foreign item, `readdir_r`, which uses a different
+            // struct layout.
+            "readdir64_r" => {
+                let result = this.linux_readdir64_r(args[0], args[1], args[2])?;
                 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
             }
 
             // Time related shims
+
+            // This is a POSIX function but it has only been tested on linux.
             "clock_gettime" => {
                 let result = this.clock_gettime(args[0], args[1])?;
                 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
             }
 
+            // Other shims
             "pthread_getattr_np" => {
                 this.write_null(dest)?;
             }
+
+            "syscall" => {
+                let sys_getrandom = this
+                    .eval_path_scalar(&["libc", "SYS_getrandom"])?
+                    .expect("Failed to get libc::SYS_getrandom")
+                    .to_machine_usize(this)?;
+
+                let sys_statx = this
+                    .eval_path_scalar(&["libc", "SYS_statx"])?
+                    .expect("Failed to get libc::SYS_statx")
+                    .to_machine_usize(this)?;
+
+                match this.read_scalar(args[0])?.to_machine_usize(this)? {
+                    // `libc::syscall(NR_GETRANDOM, buf.as_mut_ptr(), buf.len(), GRND_NONBLOCK)`
+                    // is called if a `HashMap` is created the regular way (e.g. HashMap<K, V>).
+                    id if id == sys_getrandom => {
+                        // The first argument is the syscall id,
+                        // so skip over it.
+                        getrandom(this, &args[1..], dest)?;
+                    }
+                    // `statx` is used by `libstd` to retrieve metadata information on `linux`
+                    // instead of using `stat`,`lstat` or `fstat` as on `macos`.
+                    id if id == sys_statx => {
+                        // The first argument is the syscall id,
+                        // so skip over it.
+                        let result = this.linux_statx(args[1], args[2], args[3], args[4], args[5])?;
+                        this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
+                    }
+                    id => throw_unsup_format!("miri does not support syscall ID {}", id),
+                }
+            }
+
+            "getrandom" => {
+                getrandom(this, args, dest)?;
+            }
+
+            "sched_getaffinity" => {
+                // Return an error; `num_cpus` then falls back to `sysconf`.
+                this.write_scalar(Scalar::from_int(-1, dest.layout.size), dest)?;
+            }
+
             _ => throw_unsup_format!("can't call foreign function: {}", link_name),
         };
 
-        Ok(())
+        Ok(true)
     }
 }
+
+// Shims the linux `getrandom` syscall.
+fn getrandom<'tcx>(
+    this: &mut MiriEvalContext<'_, 'tcx>,
+    args: &[OpTy<'tcx, Tag>],
+    dest: PlaceTy<'tcx, Tag>,
+) -> InterpResult<'tcx> {
+    let ptr = this.read_scalar(args[0])?.not_undef()?;
+    let len = this.read_scalar(args[1])?.to_machine_usize(this)?;
+
+    // The only supported flags are GRND_RANDOM and GRND_NONBLOCK,
+    // neither of which have any effect on our current PRNG.
+    let _flags = this.read_scalar(args[2])?.to_i32()?;
+
+    this.gen_random(ptr, len)?;
+    this.write_scalar(Scalar::from_uint(len, dest.layout.size), dest)?;
+    Ok(())
+}