Skip to content

Commit c233c4a

Browse files
committed
migrate more functions
1 parent f2f8fb2 commit c233c4a

File tree

4 files changed

+136
-111
lines changed

4 files changed

+136
-111
lines changed

src/shims/foreign_items.rs

Lines changed: 0 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -208,33 +208,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
208208
let res = this.malloc(size, /*zero_init:*/ true, MiriMemoryKind::C);
209209
this.write_scalar(res, dest)?;
210210
}
211-
"posix_memalign" => {
212-
let ret = this.deref_operand(args[0])?;
213-
let align = this.read_scalar(args[1])?.to_machine_usize(this)?;
214-
let size = this.read_scalar(args[2])?.to_machine_usize(this)?;
215-
// Align must be power of 2, and also at least ptr-sized (POSIX rules).
216-
if !align.is_power_of_two() {
217-
throw_unsup!(HeapAllocNonPowerOfTwoAlignment(align));
218-
}
219-
if align < this.pointer_size().bytes() {
220-
throw_ub_format!(
221-
"posix_memalign: alignment must be at least the size of a pointer, but is {}",
222-
align,
223-
);
224-
}
225-
226-
if size == 0 {
227-
this.write_null(ret.into())?;
228-
} else {
229-
let ptr = this.memory.allocate(
230-
Size::from_bytes(size),
231-
Align::from_bytes(align).unwrap(),
232-
MiriMemoryKind::C.into(),
233-
);
234-
this.write_scalar(ptr, ret.into())?;
235-
}
236-
this.write_null(dest)?;
237-
}
238211
"free" => {
239212
let ptr = this.read_scalar(args[0])?.not_undef()?;
240213
this.free(ptr, MiriMemoryKind::C)?;
@@ -319,53 +292,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
319292
this.write_scalar(new_ptr, dest)?;
320293
}
321294

322-
"syscall" => {
323-
let sys_getrandom = this
324-
.eval_path_scalar(&["libc", "SYS_getrandom"])?
325-
.expect("Failed to get libc::SYS_getrandom")
326-
.to_machine_usize(this)?;
327-
328-
let sys_statx = this
329-
.eval_path_scalar(&["libc", "SYS_statx"])?
330-
.expect("Failed to get libc::SYS_statx")
331-
.to_machine_usize(this)?;
332-
333-
match this.read_scalar(args[0])?.to_machine_usize(this)? {
334-
// `libc::syscall(NR_GETRANDOM, buf.as_mut_ptr(), buf.len(), GRND_NONBLOCK)`
335-
// is called if a `HashMap` is created the regular way (e.g. HashMap<K, V>).
336-
id if id == sys_getrandom => {
337-
// The first argument is the syscall id,
338-
// so skip over it.
339-
linux_getrandom(this, &args[1..], dest)?;
340-
}
341-
id if id == sys_statx => {
342-
// The first argument is the syscall id,
343-
// so skip over it.
344-
let result = this.statx(args[1], args[2], args[3], args[4], args[5])?;
345-
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
346-
}
347-
id => throw_unsup_format!("miri does not support syscall ID {}", id),
348-
}
349-
}
350-
351-
"getrandom" => {
352-
linux_getrandom(this, args, dest)?;
353-
}
354-
355-
"dlsym" => {
356-
let _handle = this.read_scalar(args[0])?;
357-
let symbol = this.read_scalar(args[1])?.not_undef()?;
358-
let symbol_name = this.memory.read_c_str(symbol)?;
359-
let err = format!("bad c unicode symbol: {:?}", symbol_name);
360-
let symbol_name = ::std::str::from_utf8(symbol_name).unwrap_or(&err);
361-
if let Some(dlsym) = Dlsym::from_str(symbol_name)? {
362-
let ptr = this.memory.create_fn_alloc(FnVal::Other(dlsym));
363-
this.write_scalar(Scalar::from(ptr), dest)?;
364-
} else {
365-
this.write_null(dest)?;
366-
}
367-
}
368-
369295
"memcmp" => {
370296
let left = this.read_scalar(args[0])?.not_undef()?;
371297
let right = this.read_scalar(args[1])?.not_undef()?;
@@ -386,24 +312,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
386312
this.write_scalar(Scalar::from_int(result, Size::from_bits(32)), dest)?;
387313
}
388314

389-
"memrchr" => {
390-
let ptr = this.read_scalar(args[0])?.not_undef()?;
391-
let val = this.read_scalar(args[1])?.to_i32()? as u8;
392-
let num = this.read_scalar(args[2])?.to_machine_usize(this)?;
393-
if let Some(idx) = this
394-
.memory
395-
.read_bytes(ptr, Size::from_bytes(num))?
396-
.iter()
397-
.rev()
398-
.position(|&c| c == val)
399-
{
400-
let new_ptr = ptr.ptr_offset(Size::from_bytes(num - idx as u64 - 1), this)?;
401-
this.write_scalar(new_ptr, dest)?;
402-
} else {
403-
this.write_null(dest)?;
404-
}
405-
}
406-
407315
"memchr" => {
408316
let ptr = this.read_scalar(args[0])?.not_undef()?;
409317
let val = this.read_scalar(args[1])?.to_i32()? as u8;
@@ -728,21 +636,3 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
728636
return Ok(None);
729637
}
730638
}
731-
732-
// Shims the linux 'getrandom()' syscall.
733-
fn linux_getrandom<'tcx>(
734-
this: &mut MiriEvalContext<'_, 'tcx>,
735-
args: &[OpTy<'tcx, Tag>],
736-
dest: PlaceTy<'tcx, Tag>,
737-
) -> InterpResult<'tcx> {
738-
let ptr = this.read_scalar(args[0])?.not_undef()?;
739-
let len = this.read_scalar(args[1])?.to_machine_usize(this)?;
740-
741-
// The only supported flags are GRND_RANDOM and GRND_NONBLOCK,
742-
// neither of which have any effect on our current PRNG.
743-
let _flags = this.read_scalar(args[2])?.to_i32()?;
744-
745-
this.gen_random(ptr, len as usize)?;
746-
this.write_scalar(Scalar::from_uint(len, dest.layout.size), dest)?;
747-
Ok(())
748-
}

src/shims/foreign_items/posix.rs

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ mod linux;
22
mod macos;
33

44
use crate::*;
5-
use rustc::ty::layout::Size;
5+
use rustc::ty::layout::{Align, Size};
66

77
impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
88
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
@@ -97,6 +97,66 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
9797
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
9898
}
9999

100+
"posix_memalign" => {
101+
let ret = this.deref_operand(args[0])?;
102+
let align = this.read_scalar(args[1])?.to_machine_usize(this)?;
103+
let size = this.read_scalar(args[2])?.to_machine_usize(this)?;
104+
// Align must be power of 2, and also at least ptr-sized (POSIX rules).
105+
if !align.is_power_of_two() {
106+
throw_unsup!(HeapAllocNonPowerOfTwoAlignment(align));
107+
}
108+
if align < this.pointer_size().bytes() {
109+
throw_ub_format!(
110+
"posix_memalign: alignment must be at least the size of a pointer, but is {}",
111+
align,
112+
);
113+
}
114+
115+
if size == 0 {
116+
this.write_null(ret.into())?;
117+
} else {
118+
let ptr = this.memory.allocate(
119+
Size::from_bytes(size),
120+
Align::from_bytes(align).unwrap(),
121+
MiriMemoryKind::C.into(),
122+
);
123+
this.write_scalar(ptr, ret.into())?;
124+
}
125+
this.write_null(dest)?;
126+
}
127+
128+
"dlsym" => {
129+
let _handle = this.read_scalar(args[0])?;
130+
let symbol = this.read_scalar(args[1])?.not_undef()?;
131+
let symbol_name = this.memory.read_c_str(symbol)?;
132+
let err = format!("bad c unicode symbol: {:?}", symbol_name);
133+
let symbol_name = ::std::str::from_utf8(symbol_name).unwrap_or(&err);
134+
if let Some(dlsym) = Dlsym::from_str(symbol_name)? {
135+
let ptr = this.memory.create_fn_alloc(FnVal::Other(dlsym));
136+
this.write_scalar(Scalar::from(ptr), dest)?;
137+
} else {
138+
this.write_null(dest)?;
139+
}
140+
}
141+
142+
"memrchr" => {
143+
let ptr = this.read_scalar(args[0])?.not_undef()?;
144+
let val = this.read_scalar(args[1])?.to_i32()? as u8;
145+
let num = this.read_scalar(args[2])?.to_machine_usize(this)?;
146+
if let Some(idx) = this
147+
.memory
148+
.read_bytes(ptr, Size::from_bytes(num))?
149+
.iter()
150+
.rev()
151+
.position(|&c| c == val)
152+
{
153+
let new_ptr = ptr.ptr_offset(Size::from_bytes(num - idx as u64 - 1), this)?;
154+
this.write_scalar(new_ptr, dest)?;
155+
} else {
156+
this.write_null(dest)?;
157+
}
158+
}
159+
100160
_ => {
101161
match this.tcx.sess.target.target.target_os.to_lowercase().as_str() {
102162
"linux" => linux::EvalContextExt::emulate_foreign_item_by_name(this, link_name, args, dest)?,
@@ -109,3 +169,21 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
109169
Ok(())
110170
}
111171
}
172+
173+
// Shims the posix 'getrandom()' syscall.
174+
fn getrandom<'tcx>(
175+
this: &mut MiriEvalContext<'_, 'tcx>,
176+
args: &[OpTy<'tcx, Tag>],
177+
dest: PlaceTy<'tcx, Tag>,
178+
) -> InterpResult<'tcx> {
179+
let ptr = this.read_scalar(args[0])?.not_undef()?;
180+
let len = this.read_scalar(args[1])?.to_machine_usize(this)?;
181+
182+
// The only supported flags are GRND_RANDOM and GRND_NONBLOCK,
183+
// neither of which have any effect on our current PRNG.
184+
let _flags = this.read_scalar(args[2])?.to_i32()?;
185+
186+
this.gen_random(ptr, len as usize)?;
187+
this.write_scalar(Scalar::from_uint(len, dest.layout.size), dest)?;
188+
Ok(())
189+
}

src/shims/foreign_items/posix/linux.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,40 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
4141
"pthread_getattr_np" => {
4242
this.write_null(dest)?;
4343
}
44+
45+
"syscall" => {
46+
let sys_getrandom = this
47+
.eval_path_scalar(&["libc", "SYS_getrandom"])?
48+
.expect("Failed to get libc::SYS_getrandom")
49+
.to_machine_usize(this)?;
50+
51+
let sys_statx = this
52+
.eval_path_scalar(&["libc", "SYS_statx"])?
53+
.expect("Failed to get libc::SYS_statx")
54+
.to_machine_usize(this)?;
55+
56+
match this.read_scalar(args[0])?.to_machine_usize(this)? {
57+
// `libc::syscall(NR_GETRANDOM, buf.as_mut_ptr(), buf.len(), GRND_NONBLOCK)`
58+
// is called if a `HashMap` is created the regular way (e.g. HashMap<K, V>).
59+
id if id == sys_getrandom => {
60+
// The first argument is the syscall id,
61+
// so skip over it.
62+
super::getrandom(this, &args[1..], dest)?;
63+
}
64+
id if id == sys_statx => {
65+
// The first argument is the syscall id,
66+
// so skip over it.
67+
let result = this.statx(args[1], args[2], args[3], args[4], args[5])?;
68+
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
69+
}
70+
id => throw_unsup_format!("miri does not support syscall ID {}", id),
71+
}
72+
}
73+
74+
"getrandom" => {
75+
super::getrandom(this, args, dest)?;
76+
}
77+
4478
_ => throw_unsup_format!("can't call foreign function: {}", link_name),
4579
};
4680

src/shims/foreign_items/posix/macos.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,30 +57,53 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
5757
"pthread_attr_get_np" => {
5858
this.write_null(dest)?;
5959
}
60+
6061
"pthread_get_stackaddr_np" => {
6162
let stack_addr = Scalar::from_uint(STACK_ADDR, dest.layout.size);
6263
this.write_scalar(stack_addr, dest)?;
6364
}
65+
6466
"pthread_get_stacksize_np" => {
6567
let stack_size = Scalar::from_uint(STACK_SIZE, dest.layout.size);
6668
this.write_scalar(stack_size, dest)?;
6769
}
70+
6871
"_tlv_atexit" => {
6972
// FIXME: register the destructor.
7073
}
74+
7175
"_NSGetArgc" => {
7276
this.write_scalar(this.machine.argc.expect("machine must be initialized"), dest)?;
7377
}
78+
7479
"_NSGetArgv" => {
7580
this.write_scalar(this.machine.argv.expect("machine must be initialized"), dest)?;
7681
}
82+
7783
"SecRandomCopyBytes" => {
7884
let len = this.read_scalar(args[1])?.to_machine_usize(this)?;
7985
let ptr = this.read_scalar(args[2])?.not_undef()?;
8086
this.gen_random(ptr, len as usize)?;
8187
this.write_null(dest)?;
8288
}
8389

90+
"syscall" => {
91+
let sys_getrandom = this
92+
.eval_path_scalar(&["libc", "SYS_getrandom"])?
93+
.expect("Failed to get libc::SYS_getrandom")
94+
.to_machine_usize(this)?;
95+
96+
match this.read_scalar(args[0])?.to_machine_usize(this)? {
97+
// `libc::syscall(NR_GETRANDOM, buf.as_mut_ptr(), buf.len(), GRND_NONBLOCK)`
98+
// is called if a `HashMap` is created the regular way (e.g. HashMap<K, V>).
99+
id if id == sys_getrandom => {
100+
// The first argument is the syscall id,
101+
// so skip over it.
102+
super::getrandom(this, &args[1..], dest)?;
103+
}
104+
id => throw_unsup_format!("miri does not support syscall ID {}", id),
105+
}
106+
}
84107

85108
_ => throw_unsup_format!("can't call foreign function: {}", link_name),
86109
};

0 commit comments

Comments
 (0)