Skip to content
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

prepare Dlsym system for dynamic symbols on Windows #1424

Merged
merged 4 commits into from
May 21, 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
23 changes: 18 additions & 5 deletions src/shims/dlsym.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,25 @@ pub enum Dlsym {
impl Dlsym {
// Returns an error for unsupported symbols, and None if this symbol
// should become a NULL pointer (pretend it does not exist).
pub fn from_str(name: &str) -> InterpResult<'static, Option<Dlsym>> {
pub fn from_str(name: &[u8], target_os: &str) -> InterpResult<'static, Option<Dlsym>> {
use self::Dlsym::*;
Ok(match name {
"getentropy" => Some(GetEntropy),
"__pthread_get_minstack" => None,
_ => throw_unsup_format!("unsupported dlsym: {}", name),
let name = String::from_utf8_lossy(name);
Ok(match target_os {
"linux" => match &*name {
"__pthread_get_minstack" => None,
_ => throw_unsup_format!("unsupported Linux dlsym: {}", name),
}
"macos" => match &*name {
"getentropy" => Some(GetEntropy),
_ => throw_unsup_format!("unsupported macOS dlsym: {}", name),
}
"windows" => match &*name {
"SetThreadStackGuarantee" => None,
"AcquireSRWLockExclusive" => None,
"GetSystemTimePreciseAsFileTime" => None,
_ => throw_unsup_format!("unsupported Windows dlsym: {}", name),
}
os => bug!("dlsym not implemented for target_os {}", os),
})
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/shims/foreign_items/posix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
this.read_scalar(handle)?.not_undef()?;
let symbol = this.read_scalar(symbol)?.not_undef()?;
let symbol_name = this.memory.read_c_str(symbol)?;
let err = format!("bad c unicode symbol: {:?}", symbol_name);
let symbol_name = ::std::str::from_utf8(symbol_name).unwrap_or(&err);
if let Some(dlsym) = Dlsym::from_str(symbol_name)? {
if let Some(dlsym) = Dlsym::from_str(symbol_name, &this.tcx.sess.target.target.target_os)? {
let ptr = this.memory.create_fn_alloc(FnVal::Other(dlsym));
this.write_scalar(Scalar::from(ptr), dest)?;
} else {
Expand Down
20 changes: 14 additions & 6 deletions src/shims/foreign_items/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,20 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
this.write_scalar(Scalar::from_i32(result), dest)?;
}

// Dynamic symbol loading
"GetProcAddress" => {
#[allow(non_snake_case)]
let &[hModule, lpProcName] = check_arg_count(args)?;
this.read_scalar(hModule)?.not_undef()?;
let name = this.memory.read_c_str(this.read_scalar(lpProcName)?.not_undef()?)?;
if let Some(dlsym) = Dlsym::from_str(name, &this.tcx.sess.target.target.target_os)? {
let ptr = this.memory.create_fn_alloc(FnVal::Other(dlsym));
this.write_scalar(Scalar::from(ptr), dest)?;
} else {
this.write_null(dest)?;
}
}

// Miscellaneous
"SystemFunction036" => {
// The actual name of 'RtlGenRandom'
Expand Down Expand Up @@ -258,12 +272,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
// Pretend this does not exist / nothing happened, by returning zero.
this.write_null(dest)?;
}
"GetProcAddress" if this.frame().instance.to_string().starts_with("std::sys::windows::") => {
#[allow(non_snake_case)]
let &[_hModule, _lpProcName] = check_arg_count(args)?;
// Pretend this does not exist / nothing happened, by returning zero.
this.write_null(dest)?;
}
"SetConsoleTextAttribute" if this.frame().instance.to_string().starts_with("std::sys::windows::") => {
#[allow(non_snake_case)]
let &[_hConsoleOutput, _wAttribute] = check_arg_count(args)?;
Expand Down