Skip to content

Commit

Permalink
Move getentropy handling to a shared location for foreign item implem…
Browse files Browse the repository at this point in the history
…entation
  • Loading branch information
BlackHoleFox committed Oct 1, 2023
1 parent 7555cbb commit a24506e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/shims/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
19 changes: 19 additions & 0 deletions src/shims/rand.rs
Original file line number Diff line number Diff line change
@@ -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<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
}
}
8 changes: 3 additions & 5 deletions src/shims/unix/macos/dlsym.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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)?;
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/shims/unix/macos/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 _;

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

0 comments on commit a24506e

Please sign in to comment.