Skip to content

Commit b819493

Browse files
committed
move remaining shims
1 parent 63160c6 commit b819493

File tree

3 files changed

+63
-61
lines changed

3 files changed

+63
-61
lines changed

src/shims/foreign_items.rs

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
193193
) -> InterpResult<'tcx> {
194194
let this = self.eval_context_mut();
195195

196+
// Here we dispatch all the shims for foreign functions. If you have a platform specific
197+
// shim, add it to the corresponding submodule.
196198
match link_name {
197199
"malloc" => {
198200
let size = this.read_scalar(args[0])?.to_machine_usize(this)?;
@@ -439,67 +441,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
439441
this.write_scalar(Scalar::from_f64(res), dest)?;
440442
}
441443

442-
// Some things needed for `sys::thread` initialization to go through.
443-
| "signal"
444-
| "sigaction"
445-
| "sigaltstack"
446-
=> {
447-
this.write_scalar(Scalar::from_int(0, dest.layout.size), dest)?;
448-
}
449-
450-
"sysconf" => {
451-
let name = this.read_scalar(args[0])?.to_i32()?;
452-
453-
trace!("sysconf() called with name {}", name);
454-
// TODO: Cache the sysconf integers via Miri's global cache.
455-
let paths = &[
456-
(&["libc", "_SC_PAGESIZE"], Scalar::from_int(PAGE_SIZE, dest.layout.size)),
457-
(&["libc", "_SC_GETPW_R_SIZE_MAX"], Scalar::from_int(-1, dest.layout.size)),
458-
(
459-
&["libc", "_SC_NPROCESSORS_ONLN"],
460-
Scalar::from_int(NUM_CPUS, dest.layout.size),
461-
),
462-
];
463-
let mut result = None;
464-
for &(path, path_value) in paths {
465-
if let Some(val) = this.eval_path_scalar(path)? {
466-
let val = val.to_i32()?;
467-
if val == name {
468-
result = Some(path_value);
469-
break;
470-
}
471-
}
472-
}
473-
if let Some(result) = result {
474-
this.write_scalar(result, dest)?;
475-
} else {
476-
throw_unsup_format!("Unimplemented sysconf name: {}", name)
477-
}
478-
}
479-
480-
"sched_getaffinity" => {
481-
// Return an error; `num_cpus` then falls back to `sysconf`.
482-
this.write_scalar(Scalar::from_int(-1, dest.layout.size), dest)?;
483-
}
484-
485-
"isatty" => {
486-
this.write_null(dest)?;
487-
}
488-
489-
"posix_fadvise" => {
490-
// fadvise is only informational, we can ignore it.
491-
this.write_null(dest)?;
492-
}
493-
494-
"mmap" => {
495-
// This is a horrible hack, but since the guard page mechanism calls mmap and expects a particular return value, we just give it that value.
496-
let addr = this.read_scalar(args[0])?.not_undef()?;
497-
this.write_scalar(addr, dest)?;
498-
}
499-
"mprotect" => {
500-
this.write_null(dest)?;
501-
}
502-
503444
_ => match this.tcx.sess.target.target.target_os.to_lowercase().as_str() {
504445
"linux" | "macos" => posix::EvalContextExt::emulate_foreign_item_by_name(this, link_name, args, dest)?,
505446
"windows" => windows::EvalContextExt::emulate_foreign_item_by_name(this, link_name, args, dest)?,

src/shims/foreign_items/posix.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,62 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
266266
this.write_null(dest)?;
267267
}
268268

269+
// Some things needed for `sys::thread` initialization to go through.
270+
| "signal"
271+
| "sigaction"
272+
| "sigaltstack"
273+
=> {
274+
this.write_scalar(Scalar::from_int(0, dest.layout.size), dest)?;
275+
}
276+
277+
"sysconf" => {
278+
let name = this.read_scalar(args[0])?.to_i32()?;
279+
280+
trace!("sysconf() called with name {}", name);
281+
// TODO: Cache the sysconf integers via Miri's global cache.
282+
let paths = &[
283+
(&["libc", "_SC_PAGESIZE"], Scalar::from_int(PAGE_SIZE, dest.layout.size)),
284+
(&["libc", "_SC_GETPW_R_SIZE_MAX"], Scalar::from_int(-1, dest.layout.size)),
285+
(
286+
&["libc", "_SC_NPROCESSORS_ONLN"],
287+
Scalar::from_int(NUM_CPUS, dest.layout.size),
288+
),
289+
];
290+
let mut result = None;
291+
for &(path, path_value) in paths {
292+
if let Some(val) = this.eval_path_scalar(path)? {
293+
let val = val.to_i32()?;
294+
if val == name {
295+
result = Some(path_value);
296+
break;
297+
}
298+
}
299+
}
300+
if let Some(result) = result {
301+
this.write_scalar(result, dest)?;
302+
} else {
303+
throw_unsup_format!("Unimplemented sysconf name: {}", name)
304+
}
305+
}
306+
307+
"isatty" => {
308+
this.write_null(dest)?;
309+
}
310+
311+
"posix_fadvise" => {
312+
// fadvise is only informational, we can ignore it.
313+
this.write_null(dest)?;
314+
}
315+
316+
"mmap" => {
317+
// This is a horrible hack, but since the guard page mechanism calls mmap and expects a particular return value, we just give it that value.
318+
let addr = this.read_scalar(args[0])?.not_undef()?;
319+
this.write_scalar(addr, dest)?;
320+
}
321+
322+
"mprotect" => {
323+
this.write_null(dest)?;
324+
}
269325

270326
_ => {
271327
match this.tcx.sess.target.target.target_os.to_lowercase().as_str() {

src/shims/foreign_items/posix/linux.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
7575
super::getrandom(this, args, dest)?;
7676
}
7777

78+
"sched_getaffinity" => {
79+
// Return an error; `num_cpus` then falls back to `sysconf`.
80+
this.write_scalar(Scalar::from_int(-1, dest.layout.size), dest)?;
81+
}
82+
7883
_ => throw_unsup_format!("can't call foreign function: {}", link_name),
7984
};
8085

0 commit comments

Comments
 (0)