Move getentropy handling to a shared location for foreign item implementation

This commit is contained in:
BlackHoleFox 2023-10-01 12:19:41 -05:00
parent 6687c075df
commit dff7d5aa2f
2 changed files with 26 additions and 4 deletions

View File

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

View File

@ -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<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
}
}