Skip to content

Commit

Permalink
Auto merge of rust-lang#3098 - BlackHoleFox:apple-entropy, r=RalfJung
Browse files Browse the repository at this point in the history
Support getentropy on macOS as a foreign item

Prior this was always assumed to be accessed via `dlsym` shim, but in `std` I'm attempting to start [unconditionally linking](rust-lang#116319) to `getentropy` on macOS now that Rust's platform version support allows it.

This just moves the main logic of the previous `dlsym` handler into an eval context extension so it can be used via both call paths. The `dlsym` handler is still needed as `getrandom` uses it.
  • Loading branch information
bors committed Oct 6, 2023
2 parents 1a6ab01 + dff7d5a commit f9003c0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/tools/miri/src/shims/unix/macos/dlsym.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use rustc_middle::mir;

use log::trace;

use super::foreign_items::EvalContextExt as _;
use crate::*;
use helpers::check_arg_count;

Expand Down Expand Up @@ -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)?;
}
}

Expand Down
23 changes: 23 additions & 0 deletions src/tools/miri/src/shims/unix/macos/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down Expand Up @@ -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<Provenance>> {
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
}
}

0 comments on commit f9003c0

Please sign in to comment.