From a24506e81ce5fae8e647b425f924a4302efa03f6 Mon Sep 17 00:00:00 2001 From: BlackHoleFox Date: Sun, 1 Oct 2023 12:19:41 -0500 Subject: [PATCH] Move getentropy handling to a shared location for foreign item implementation --- src/shims/mod.rs | 1 + src/shims/rand.rs | 19 +++++++++++++++++++ src/shims/unix/macos/dlsym.rs | 8 +++----- src/shims/unix/macos/foreign_items.rs | 9 +++++++++ 4 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 src/shims/rand.rs diff --git a/src/shims/mod.rs b/src/shims/mod.rs index 5a9574766f..4a11ca7a3e 100644 --- a/src/shims/mod.rs +++ b/src/shims/mod.rs @@ -13,6 +13,7 @@ pub mod dlsym; pub mod env; pub mod os_str; pub mod panic; +pub mod rand; pub mod time; pub mod tls; diff --git a/src/shims/rand.rs b/src/shims/rand.rs new file mode 100644 index 0000000000..85bcacc08c --- /dev/null +++ b/src/shims/rand.rs @@ -0,0 +1,19 @@ +use crate::*; + +impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriInterpCx<'mir, 'tcx> {} +pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { + fn getentropy( + &mut self, + buffer_op: &OpTy<'tcx, Provenance>, + length_op: &OpTy<'tcx, Provenance>, + ) -> InterpResult<'tcx, Scalar> { + let this = self.eval_context_mut(); + this.assert_target_os("macos", "getentropy"); + + let ptr = this.read_pointer(buffer_op)?; + let len = this.read_target_usize(length_op)?; + this.gen_random(ptr, len)?; + + Ok(Scalar::from_i32(0)) // KERN_SUCCESS + } +} diff --git a/src/shims/unix/macos/dlsym.rs b/src/shims/unix/macos/dlsym.rs index 63ad680de6..95237e8084 100644 --- a/src/shims/unix/macos/dlsym.rs +++ b/src/shims/unix/macos/dlsym.rs @@ -2,7 +2,7 @@ use rustc_middle::mir; use log::trace; -use crate::*; +use crate::{shims::rand::EvalContextExt as _, *}; use helpers::check_arg_count; #[derive(Debug, Copy, Clone)] @@ -38,10 +38,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { match dlsym { Dlsym::getentropy => { let [ptr, len] = check_arg_count(args)?; - let ptr = this.read_pointer(ptr)?; - let len = this.read_target_usize(len)?; - this.gen_random(ptr, len)?; - this.write_null(dest)?; + let result = this.getentropy(ptr, len)?; + this.write_scalar(result, dest)?; } } diff --git a/src/shims/unix/macos/foreign_items.rs b/src/shims/unix/macos/foreign_items.rs index 0ee3cea05d..006475c376 100644 --- a/src/shims/unix/macos/foreign_items.rs +++ b/src/shims/unix/macos/foreign_items.rs @@ -3,6 +3,7 @@ use rustc_target::spec::abi::Abi; use crate::*; use shims::foreign_items::EmulateByNameResult; +use shims::rand::EvalContextExt as _; use shims::unix::fs::EvalContextExt as _; use shims::unix::thread::EvalContextExt as _; @@ -109,6 +110,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { this.write_scalar(result, dest)?; } + // Random generation related shims + "getentropy" => { + let [buf, bufsize] = + this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?; + let result = this.getentropy(buf, bufsize)?; + this.write_scalar(result, dest)?; + } + // Access to command-line arguments "_NSGetArgc" => { let [] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;