Skip to content

Commit a24506e

Browse files
committed
Move getentropy handling to a shared location for foreign item implementation
1 parent 7555cbb commit a24506e

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

src/shims/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub mod dlsym;
1313
pub mod env;
1414
pub mod os_str;
1515
pub mod panic;
16+
pub mod rand;
1617
pub mod time;
1718
pub mod tls;
1819

src/shims/rand.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use crate::*;
2+
3+
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriInterpCx<'mir, 'tcx> {}
4+
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
5+
fn getentropy(
6+
&mut self,
7+
buffer_op: &OpTy<'tcx, Provenance>,
8+
length_op: &OpTy<'tcx, Provenance>,
9+
) -> InterpResult<'tcx, Scalar<Provenance>> {
10+
let this = self.eval_context_mut();
11+
this.assert_target_os("macos", "getentropy");
12+
13+
let ptr = this.read_pointer(buffer_op)?;
14+
let len = this.read_target_usize(length_op)?;
15+
this.gen_random(ptr, len)?;
16+
17+
Ok(Scalar::from_i32(0)) // KERN_SUCCESS
18+
}
19+
}

src/shims/unix/macos/dlsym.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_middle::mir;
22

33
use log::trace;
44

5-
use crate::*;
5+
use crate::{shims::rand::EvalContextExt as _, *};
66
use helpers::check_arg_count;
77

88
#[derive(Debug, Copy, Clone)]
@@ -38,10 +38,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
3838
match dlsym {
3939
Dlsym::getentropy => {
4040
let [ptr, len] = check_arg_count(args)?;
41-
let ptr = this.read_pointer(ptr)?;
42-
let len = this.read_target_usize(len)?;
43-
this.gen_random(ptr, len)?;
44-
this.write_null(dest)?;
41+
let result = this.getentropy(ptr, len)?;
42+
this.write_scalar(result, dest)?;
4543
}
4644
}
4745

src/shims/unix/macos/foreign_items.rs

+9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use rustc_target::spec::abi::Abi;
33

44
use crate::*;
55
use shims::foreign_items::EmulateByNameResult;
6+
use shims::rand::EvalContextExt as _;
67
use shims::unix::fs::EvalContextExt as _;
78
use shims::unix::thread::EvalContextExt as _;
89

@@ -109,6 +110,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
109110
this.write_scalar(result, dest)?;
110111
}
111112

113+
// Random generation related shims
114+
"getentropy" => {
115+
let [buf, bufsize] =
116+
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
117+
let result = this.getentropy(buf, bufsize)?;
118+
this.write_scalar(result, dest)?;
119+
}
120+
112121
// Access to command-line arguments
113122
"_NSGetArgc" => {
114123
let [] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;

0 commit comments

Comments
 (0)