Skip to content

Commit 08ddbd6

Browse files
committed
prepare Dlsym system for dynamic symbols on Windows
1 parent 5d2423d commit 08ddbd6

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

src/shims/dlsym.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,21 @@ pub enum Dlsym {
1111
impl Dlsym {
1212
// Returns an error for unsupported symbols, and None if this symbol
1313
// should become a NULL pointer (pretend it does not exist).
14-
pub fn from_str(name: &str) -> InterpResult<'static, Option<Dlsym>> {
14+
pub fn from_str(name: &[u8], target_os: &str) -> InterpResult<'static, Option<Dlsym>> {
1515
use self::Dlsym::*;
16-
Ok(match name {
17-
"getentropy" => Some(GetEntropy),
18-
"__pthread_get_minstack" => None,
19-
_ => throw_unsup_format!("unsupported dlsym: {}", name),
16+
let name = String::from_utf8_lossy(name);
17+
Ok(match target_os {
18+
"linux" | "macos" => match &*name {
19+
"getentropy" => Some(GetEntropy),
20+
"__pthread_get_minstack" => None,
21+
_ => throw_unsup_format!("unsupported dlsym: {}", name),
22+
}
23+
"windows" => match &*name {
24+
"SetThreadStackGuarantee" => None,
25+
"AcquireSRWLockExclusive" => None,
26+
_ => throw_unsup_format!("unsupported dlsym: {}", name),
27+
}
28+
os => bug!("dlsym not implemented for target_os {}", os),
2029
})
2130
}
2231
}

src/shims/foreign_items/posix.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
173173
this.read_scalar(handle)?.not_undef()?;
174174
let symbol = this.read_scalar(symbol)?.not_undef()?;
175175
let symbol_name = this.memory.read_c_str(symbol)?;
176-
let err = format!("bad c unicode symbol: {:?}", symbol_name);
177-
let symbol_name = ::std::str::from_utf8(symbol_name).unwrap_or(&err);
178-
if let Some(dlsym) = Dlsym::from_str(symbol_name)? {
176+
if let Some(dlsym) = Dlsym::from_str(symbol_name, &this.tcx.sess.target.target.target_os)? {
179177
let ptr = this.memory.create_fn_alloc(FnVal::Other(dlsym));
180178
this.write_scalar(Scalar::from(ptr), dest)?;
181179
} else {

src/shims/foreign_items/windows.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
260260
}
261261
"GetProcAddress" if this.frame().instance.to_string().starts_with("std::sys::windows::") => {
262262
#[allow(non_snake_case)]
263-
let &[_hModule, _lpProcName] = check_arg_count(args)?;
264-
// Pretend this does not exist / nothing happened, by returning zero.
265-
this.write_null(dest)?;
263+
let &[_hModule, lpProcName] = check_arg_count(args)?;
264+
let name = this.memory.read_c_str(this.read_scalar(lpProcName)?.not_undef()?)?;
265+
if let Some(dlsym) = Dlsym::from_str(name, &this.tcx.sess.target.target.target_os)? {
266+
let ptr = this.memory.create_fn_alloc(FnVal::Other(dlsym));
267+
this.write_scalar(Scalar::from(ptr), dest)?;
268+
} else {
269+
this.write_null(dest)?;
270+
}
266271
}
267272
"SetConsoleTextAttribute" if this.frame().instance.to_string().starts_with("std::sys::windows::") => {
268273
#[allow(non_snake_case)]

0 commit comments

Comments
 (0)