From b81949358540fd4da759b943f8a2d7382fc2a0e3 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Tue, 11 Feb 2020 18:48:38 -0500 Subject: [PATCH] move remaining shims --- src/shims/foreign_items.rs | 63 +------------------------- src/shims/foreign_items/posix.rs | 56 +++++++++++++++++++++++ src/shims/foreign_items/posix/linux.rs | 5 ++ 3 files changed, 63 insertions(+), 61 deletions(-) diff --git a/src/shims/foreign_items.rs b/src/shims/foreign_items.rs index 9ad4a6dbaf6..59f9a8c63a7 100644 --- a/src/shims/foreign_items.rs +++ b/src/shims/foreign_items.rs @@ -193,6 +193,8 @@ fn emulate_foreign_item_by_name( ) -> InterpResult<'tcx> { let this = self.eval_context_mut(); + // Here we dispatch all the shims for foreign functions. If you have a platform specific + // shim, add it to the corresponding submodule. match link_name { "malloc" => { let size = this.read_scalar(args[0])?.to_machine_usize(this)?; @@ -439,67 +441,6 @@ fn emulate_foreign_item_by_name( this.write_scalar(Scalar::from_f64(res), dest)?; } - // Some things needed for `sys::thread` initialization to go through. - | "signal" - | "sigaction" - | "sigaltstack" - => { - this.write_scalar(Scalar::from_int(0, dest.layout.size), dest)?; - } - - "sysconf" => { - let name = this.read_scalar(args[0])?.to_i32()?; - - trace!("sysconf() called with name {}", name); - // TODO: Cache the sysconf integers via Miri's global cache. - let paths = &[ - (&["libc", "_SC_PAGESIZE"], Scalar::from_int(PAGE_SIZE, dest.layout.size)), - (&["libc", "_SC_GETPW_R_SIZE_MAX"], Scalar::from_int(-1, dest.layout.size)), - ( - &["libc", "_SC_NPROCESSORS_ONLN"], - Scalar::from_int(NUM_CPUS, dest.layout.size), - ), - ]; - let mut result = None; - for &(path, path_value) in paths { - if let Some(val) = this.eval_path_scalar(path)? { - let val = val.to_i32()?; - if val == name { - result = Some(path_value); - break; - } - } - } - if let Some(result) = result { - this.write_scalar(result, dest)?; - } else { - throw_unsup_format!("Unimplemented sysconf name: {}", name) - } - } - - "sched_getaffinity" => { - // Return an error; `num_cpus` then falls back to `sysconf`. - this.write_scalar(Scalar::from_int(-1, dest.layout.size), dest)?; - } - - "isatty" => { - this.write_null(dest)?; - } - - "posix_fadvise" => { - // fadvise is only informational, we can ignore it. - this.write_null(dest)?; - } - - "mmap" => { - // 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. - let addr = this.read_scalar(args[0])?.not_undef()?; - this.write_scalar(addr, dest)?; - } - "mprotect" => { - this.write_null(dest)?; - } - _ => match this.tcx.sess.target.target.target_os.to_lowercase().as_str() { "linux" | "macos" => posix::EvalContextExt::emulate_foreign_item_by_name(this, link_name, args, dest)?, "windows" => windows::EvalContextExt::emulate_foreign_item_by_name(this, link_name, args, dest)?, diff --git a/src/shims/foreign_items/posix.rs b/src/shims/foreign_items/posix.rs index 2ec8b9d7a60..f524e4df291 100644 --- a/src/shims/foreign_items/posix.rs +++ b/src/shims/foreign_items/posix.rs @@ -266,6 +266,62 @@ fn emulate_foreign_item_by_name( this.write_null(dest)?; } + // Some things needed for `sys::thread` initialization to go through. + | "signal" + | "sigaction" + | "sigaltstack" + => { + this.write_scalar(Scalar::from_int(0, dest.layout.size), dest)?; + } + + "sysconf" => { + let name = this.read_scalar(args[0])?.to_i32()?; + + trace!("sysconf() called with name {}", name); + // TODO: Cache the sysconf integers via Miri's global cache. + let paths = &[ + (&["libc", "_SC_PAGESIZE"], Scalar::from_int(PAGE_SIZE, dest.layout.size)), + (&["libc", "_SC_GETPW_R_SIZE_MAX"], Scalar::from_int(-1, dest.layout.size)), + ( + &["libc", "_SC_NPROCESSORS_ONLN"], + Scalar::from_int(NUM_CPUS, dest.layout.size), + ), + ]; + let mut result = None; + for &(path, path_value) in paths { + if let Some(val) = this.eval_path_scalar(path)? { + let val = val.to_i32()?; + if val == name { + result = Some(path_value); + break; + } + } + } + if let Some(result) = result { + this.write_scalar(result, dest)?; + } else { + throw_unsup_format!("Unimplemented sysconf name: {}", name) + } + } + + "isatty" => { + this.write_null(dest)?; + } + + "posix_fadvise" => { + // fadvise is only informational, we can ignore it. + this.write_null(dest)?; + } + + "mmap" => { + // 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. + let addr = this.read_scalar(args[0])?.not_undef()?; + this.write_scalar(addr, dest)?; + } + + "mprotect" => { + this.write_null(dest)?; + } _ => { match this.tcx.sess.target.target.target_os.to_lowercase().as_str() { diff --git a/src/shims/foreign_items/posix/linux.rs b/src/shims/foreign_items/posix/linux.rs index 8d928b60ecb..7267cc1af8e 100644 --- a/src/shims/foreign_items/posix/linux.rs +++ b/src/shims/foreign_items/posix/linux.rs @@ -75,6 +75,11 @@ fn emulate_foreign_item_by_name( super::getrandom(this, args, dest)?; } + "sched_getaffinity" => { + // Return an error; `num_cpus` then falls back to `sysconf`. + this.write_scalar(Scalar::from_int(-1, dest.layout.size), dest)?; + } + _ => throw_unsup_format!("can't call foreign function: {}", link_name), }; -- 2.44.0