From 330a462737e5ed73934151fcef8d7fdca7573664 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/tools/miri/src/shims/unix/macos/dlsym.rs | 7 +++--- .../src/shims/unix/macos/foreign_items.rs | 23 +++++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/tools/miri/src/shims/unix/macos/dlsym.rs b/src/tools/miri/src/shims/unix/macos/dlsym.rs index 63ad680de60fa..fa8094528757e 100644 --- a/src/tools/miri/src/shims/unix/macos/dlsym.rs +++ b/src/tools/miri/src/shims/unix/macos/dlsym.rs @@ -2,6 +2,7 @@ use rustc_middle::mir; use log::trace; +use super::foreign_items::EvalContextExt as _; use crate::*; use helpers::check_arg_count; @@ -38,10 +39,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/tools/miri/src/shims/unix/macos/foreign_items.rs b/src/tools/miri/src/shims/unix/macos/foreign_items.rs index 0ee3cea05d508..b514097c5df08 100644 --- a/src/tools/miri/src/shims/unix/macos/foreign_items.rs +++ b/src/tools/miri/src/shims/unix/macos/foreign_items.rs @@ -109,6 +109,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)?; @@ -198,4 +206,19 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { Ok(EmulateByNameResult::NeedsJumping) } + + 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 + } }