]> git.lizzy.rs Git - rust.git/commitdiff
Shim 'libc::getrandom' in addition to 'libc::syscall(libc::SYS_getrandom)'
authorAaron Hill <aa1ronham@gmail.com>
Sun, 4 Aug 2019 14:50:38 +0000 (10:50 -0400)
committerAaron Hill <aa1ronham@gmail.com>
Sun, 4 Aug 2019 20:12:23 +0000 (16:12 -0400)
src/shims/foreign_items.rs

index 4fc8b1d54cfcb1e2edd915c609ed7003ce00a8a1..6ae99c9bc15cd935fac6f34c030319ada25f9644 100644 (file)
@@ -293,15 +293,9 @@ fn emulate_foreign_item(
                 // is called if a `HashMap` is created the regular way (e.g. HashMap<K, V>).
                 match this.read_scalar(args[0])?.to_usize(this)? {
                     id if id == sys_getrandom => {
-                        let ptr = this.read_scalar(args[1])?.not_undef()?;
-                        let len = this.read_scalar(args[2])?.to_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[3])?.to_i32()?;
-
-                        this.gen_random(ptr, len as usize)?;
-                        this.write_scalar(Scalar::from_uint(len, dest.layout.size), dest)?;
+                        // The first argument is the syscall id,
+                        // so skip over it
+                        linux_getrandom(this, &args[1..], dest)?;
                     }
                     id => {
                         throw_unsup_format!("miri does not support syscall ID {}", id)
@@ -309,6 +303,10 @@ fn emulate_foreign_item(
                 }
             }
 
+            "getrandom" => {
+                linux_getrandom(this, args, dest)?;
+            }
+
             "dlsym" => {
                 let _handle = this.read_scalar(args[0])?;
                 let symbol = this.read_scalar(args[1])?.not_undef()?;
@@ -969,3 +967,20 @@ fn eval_path_scalar(&mut self, path: &[&str]) -> InterpResult<'tcx, Option<Scala
         return Ok(None);
     }
 }
+
+// Shims the linux 'getrandom()' syscall
+fn linux_getrandom<'mir, 'tcx>(this: &mut MiriEvalContext<'mir, '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_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 as usize)?;
+    this.write_scalar(Scalar::from_uint(len, dest.layout.size), dest)?;
+    Ok(())
+}