Skip to content

Limit shims to libstd where possible #1263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx

/// Helper function to get a `libc` constant as an `i32`.
fn eval_libc_i32(&mut self, name: &str) -> InterpResult<'tcx, i32> {
// TODO: Cache the result.
self.eval_libc(name)?.to_i32()
}

Expand Down
17 changes: 9 additions & 8 deletions src/shims/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
// 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 {
// Standard C allocation
"malloc" => {
let size = this.read_scalar(args[0])?.to_machine_usize(this)?;
let res = this.malloc(size, /*zero_init:*/ false, MiriMemoryKind::C);
Expand All @@ -220,6 +221,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
this.write_scalar(res, dest)?;
}

// Rust allocation
// (Usually these would be forwarded to to `#[global_allocator]`; we instead implement a generic
// allocation that also checks that all conditions are met, such as not permitting zero-sized allocations.)
"__rust_alloc" => {
let size = this.read_scalar(args[0])?.to_machine_usize(this)?;
let align = this.read_scalar(args[1])?.to_machine_usize(this)?;
Expand Down Expand Up @@ -274,6 +278,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
this.write_scalar(new_ptr, dest)?;
}

// C memory handling functions
"memcmp" => {
let left = this.read_scalar(args[0])?.not_undef()?;
let right = this.read_scalar(args[1])?.not_undef()?;
Expand All @@ -293,7 +298,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx

this.write_scalar(Scalar::from_int(result, Size::from_bits(32)), dest)?;
}

"memrchr" => {
let ptr = this.read_scalar(args[0])?.not_undef()?;
let val = this.read_scalar(args[1])?.to_i32()? as u8;
Expand All @@ -311,7 +315,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
this.write_null(dest)?;
}
}

"memchr" => {
let ptr = this.read_scalar(args[0])?.not_undef()?;
let val = this.read_scalar(args[1])?.to_i32()? as u8;
Expand All @@ -328,7 +331,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
this.write_null(dest)?;
}
}

"strlen" => {
let ptr = this.read_scalar(args[0])?.not_undef()?;
let n = this.memory.read_c_str(ptr)?.len();
Expand Down Expand Up @@ -358,11 +360,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
};
this.write_scalar(Scalar::from_u32(f.to_bits()), dest)?;
}
// underscore case for windows
| "_hypotf"
| "hypotf"
| "atan2f"
=> {
// underscore case for windows, here and below
// (see https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/floating-point-primitives?view=vs-2019)
// FIXME: Using host floats.
let f1 = f32::from_bits(this.read_scalar(args[0])?.to_u32()?);
let f2 = f32::from_bits(this.read_scalar(args[1])?.to_u32()?);
Expand All @@ -373,7 +376,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
};
this.write_scalar(Scalar::from_u32(n.to_bits()), dest)?;
}

| "cbrt"
| "cosh"
| "sinh"
Expand All @@ -396,8 +398,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
};
this.write_scalar(Scalar::from_u64(f.to_bits()), dest)?;
}
// underscore case for windows, here and below
// (see https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/floating-point-primitives?view=vs-2019)
| "_hypot"
| "hypot"
| "atan2"
Expand All @@ -412,11 +412,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
};
this.write_scalar(Scalar::from_u64(n.to_bits()), dest)?;
}
// For radix-2 (binary) systems, `ldexp` and `scalbn` are the same.
| "_ldexp"
| "ldexp"
| "scalbn"
=> {
// For radix-2 (binary) systems, `ldexp` and `scalbn` are the same.
let x = this.read_scalar(args[0])?.to_f64()?;
let exp = this.read_scalar(args[1])?.to_i32()?;

Expand All @@ -434,6 +434,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
this.write_scalar(Scalar::from_f64(res), dest)?;
}

// Target-specific shims
_ => match this.tcx.sess.target.target.target_os.as_str() {
"linux" | "macos" => return posix::EvalContextExt::emulate_foreign_item_by_name(this, link_name, args, dest, ret),
"windows" => return windows::EvalContextExt::emulate_foreign_item_by_name(this, link_name, args, dest, ret),
Expand Down
145 changes: 53 additions & 92 deletions src/shims/foreign_items/posix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,18 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
let result = this.getenv(args[0])?;
this.write_scalar(result, dest)?;
}

"unsetenv" => {
let result = this.unsetenv(args[0])?;
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
}

"setenv" => {
let result = this.setenv(args[0], args[1])?;
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
}

"getcwd" => {
let result = this.getcwd(args[0], args[1])?;
this.write_scalar(result, dest)?;
}

"chdir" => {
let result = this.chdir(args[0])?;
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
Expand All @@ -50,17 +46,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
let result = this.open(args[0], args[1])?;
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
}

"fcntl" => {
let result = this.fcntl(args[0], args[1], args.get(2).cloned())?;
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
}

"read" => {
let result = this.read(args[0], args[1], args[2])?;
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
}

"write" => {
let fd = this.read_scalar(args[0])?.to_i32()?;
let buf = this.read_scalar(args[1])?.not_undef()?;
Expand Down Expand Up @@ -94,43 +87,36 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
// Now, `result` is the value we return back to the program.
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
}

"unlink" => {
let result = this.unlink(args[0])?;
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
}

"symlink" => {
let result = this.symlink(args[0], args[1])?;
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
}

"rename" => {
let result = this.rename(args[0], args[1])?;
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
}

"mkdir" => {
let result = this.mkdir(args[0], args[1])?;
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
}

"rmdir" => {
let result = this.rmdir(args[0])?;
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
}

"closedir" => {
let result = this.closedir(args[0])?;
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
}

"lseek" | "lseek64" => {
let result = this.lseek64(args[0], args[1], args[2])?;
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
}

// Other shims
// Allocation
"posix_memalign" => {
let ret = this.deref_operand(args[0])?;
let align = this.read_scalar(args[1])?.to_machine_usize(this)?;
Expand Down Expand Up @@ -159,6 +145,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
this.write_null(dest)?;
}

// Dynamic symbol loading
"dlsym" => {
let _handle = this.read_scalar(args[0])?;
let symbol = this.read_scalar(args[1])?.not_undef()?;
Expand All @@ -173,7 +160,30 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
}
}

// Hook pthread calls that go to the thread-local storage memory subsystem.
// Querying system information
"sysconf" => {
let name = this.read_scalar(args[0])?.to_i32()?;

let sysconfs = &[
("_SC_PAGESIZE", Scalar::from_int(PAGE_SIZE, dest.layout.size)),
("_SC_NPROCESSORS_ONLN", Scalar::from_int(NUM_CPUS, dest.layout.size)),
];
let mut result = None;
for &(sysconf_name, value) in sysconfs {
let sysconf_name = this.eval_libc_i32(sysconf_name)?;
if sysconf_name == name {
result = Some(value);
break;
}
}
if let Some(result) = result {
this.write_scalar(result, dest)?;
} else {
throw_unsup_format!("unimplemented sysconf name: {}", name)
}
}

// Thread-local storage
"pthread_key_create" => {
let key_place = this.deref_operand(args[0])?;

Expand Down Expand Up @@ -220,36 +230,36 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
this.write_null(dest)?;
}

// Stack size/address stuff.
| "pthread_attr_init"
| "pthread_attr_destroy"
| "pthread_self"
| "pthread_attr_setstacksize" => {
this.write_null(dest)?;
// Better error for attempts to create a thread
"pthread_create" => {
throw_unsup_format!("Miri does not support threading");
}
"pthread_attr_getstack" => {
let addr_place = this.deref_operand(args[1])?;
let size_place = this.deref_operand(args[2])?;

this.write_scalar(
Scalar::from_uint(STACK_ADDR, addr_place.layout.size),
addr_place.into(),
)?;
this.write_scalar(
Scalar::from_uint(STACK_SIZE, size_place.layout.size),
size_place.into(),
)?;

// Return success (`0`).
// Miscellaneous
"isatty" => {
let _fd = this.read_scalar(args[0])?.to_i32()?;
// "returns 1 if fd is an open file descriptor referring to a terminal; otherwise 0 is returned, and errno is set to indicate the error"
// FIXME: we just say nothing is a terminal.
let enotty = this.eval_libc("ENOTTY")?;
this.set_last_error(enotty)?;
this.write_null(dest)?;
}

// We don't support threading.
"pthread_create" => {
throw_unsup_format!("Miri does not support threading");
"pthread_atfork" => {
let _prepare = this.read_scalar(args[0])?.not_undef()?;
let _parent = this.read_scalar(args[1])?.not_undef()?;
let _child = this.read_scalar(args[1])?.not_undef()?;
// We do not support forking, so there is nothing to do here.
this.write_null(dest)?;
}

// Stub out calls for condvar, mutex and rwlock, to just return `0`.
// Incomplete shims that we "stub out" just to get pre-main initialziation code to work.
// These shims are enabled only when the caller is in the standard library.
| "pthread_attr_init"
| "pthread_attr_destroy"
| "pthread_self"
| "pthread_attr_setstacksize" if this.frame().instance.to_string().starts_with("std::sys::unix::") => {
this.write_null(dest)?;
}
| "pthread_mutexattr_init"
| "pthread_mutexattr_settype"
| "pthread_mutex_init"
Expand All @@ -265,68 +275,19 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
| "pthread_condattr_setclock"
| "pthread_cond_init"
| "pthread_condattr_destroy"
| "pthread_cond_destroy"
| "pthread_cond_destroy" if this.frame().instance.to_string().starts_with("std::sys::unix::")
=> {
this.write_null(dest)?;
}

// We don't support fork so we don't have to do anything for atfork.
"pthread_atfork" => {
this.write_null(dest)?;
}

// Some things needed for `sys::thread` initialization to go through.
| "signal"
| "sigaction"
| "sigaltstack"
| "mprotect" if this.frame().instance.to_string().starts_with("std::sys::unix::")
=> {
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 sysconfs = &[
("_SC_PAGESIZE", Scalar::from_int(PAGE_SIZE, dest.layout.size)),
("_SC_GETPW_R_SIZE_MAX", Scalar::from_int(-1, dest.layout.size)),
("_SC_NPROCESSORS_ONLN", Scalar::from_int(NUM_CPUS, dest.layout.size)),
];
let mut result = None;
for &(sysconf_name, value) in sysconfs {
let sysconf_name = this.eval_libc_i32(sysconf_name)?;
if sysconf_name == name {
result = Some(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)?;
}

// Platform-specific shims
_ => {
match this.tcx.sess.target.target.target_os.as_str() {
"linux" => return linux::EvalContextExt::emulate_foreign_item_by_name(this, link_name, args, dest, ret),
Expand Down
Loading