Skip to content

Commit 4f6a56f

Browse files
committed
better error message when the program tries to spawn a thread
1 parent 2ca1b94 commit 4f6a56f

File tree

5 files changed

+53
-23
lines changed

5 files changed

+53
-23
lines changed

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub use crate::helpers::{EvalContextExt as HelpersEvalContextExt};
3838
pub use crate::mono_hash_map::MonoHashMap;
3939
pub use crate::stacked_borrows::{EvalContextExt as StackedBorEvalContextExt, Tag, Permission, Stack, Stacks, Item};
4040
pub use crate::machine::{
41-
PAGE_SIZE, STACK_ADDR, NUM_CPUS,
41+
PAGE_SIZE, STACK_ADDR, STACK_SIZE, NUM_CPUS,
4242
MemoryExtra, AllocExtra, MiriMemoryKind, Evaluator, MiriEvalContext, MiriEvalContextExt,
4343
};
4444
pub use crate::eval::{eval_main, create_ecx, MiriConfig};

src/machine.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ use crate::*;
1818

1919
// Some global facts about the emulated machine.
2020
pub const PAGE_SIZE: u64 = 4*1024; // FIXME: adjust to target architecture
21-
pub const STACK_ADDR: u64 = 16*PAGE_SIZE; // not really about the "stack", but where we start assigning integer addresses to allocations
21+
pub const STACK_ADDR: u64 = 32*PAGE_SIZE; // not really about the "stack", but where we start assigning integer addresses to allocations
22+
pub const STACK_SIZE: u64 = 16*PAGE_SIZE; // whatever
2223
pub const NUM_CPUS: u64 = 1;
2324

2425
/// Extra memory kinds

src/shims/dlsym.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,17 @@ pub enum Dlsym {
88
}
99

1010
impl Dlsym {
11-
pub fn from_str(name: &str) -> Option<Dlsym> {
11+
// Returns an error for unsupported symbols, and None if this symbol
12+
// should become a NULL pointer (pretend it does not exist).
13+
pub fn from_str(name: &str) -> InterpResult<'static, Option<Dlsym>> {
1214
use self::Dlsym::*;
13-
Some(match name {
14-
"getentropy" => GetEntropy,
15-
_ => return None,
15+
Ok(match name {
16+
"getentropy" => Some(GetEntropy),
17+
"__pthread_get_minstack" => None,
18+
_ =>
19+
return err!(Unimplemented(format!(
20+
"Unsupported dlsym: {}", name
21+
))),
1622
})
1723
}
1824
}
@@ -32,7 +38,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
3238

3339
let dest = dest.expect("we don't support any diverging dlsym");
3440
let ret = ret.expect("dest is `Some` but ret is `None`");
35-
41+
3642
match dlsym {
3743
GetEntropy => {
3844
let ptr = this.read_scalar(args[0])?.not_undef()?;

src/shims/foreign_items.rs

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -324,13 +324,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
324324
let symbol_name = this.memory().get(symbol.alloc_id)?.read_c_str(tcx, symbol)?;
325325
let err = format!("bad c unicode symbol: {:?}", symbol_name);
326326
let symbol_name = ::std::str::from_utf8(symbol_name).unwrap_or(&err);
327-
if let Some(dlsym) = Dlsym::from_str(symbol_name) {
327+
if let Some(dlsym) = Dlsym::from_str(symbol_name)? {
328328
let ptr = this.memory_mut().create_fn_alloc(FnVal::Other(dlsym));
329329
this.write_scalar(Scalar::from(ptr), dest)?;
330330
} else {
331-
return err!(Unimplemented(format!(
332-
"Unsupported dlsym: {}", symbol_name
333-
)));
331+
this.write_null(dest)?;
334332
}
335333
}
336334

@@ -713,24 +711,31 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
713711
this.write_null(dest)?;
714712
}
715713

716-
// Determine stack base address.
717-
"pthread_attr_init" | "pthread_attr_destroy" | "pthread_attr_get_np" |
718-
"pthread_getattr_np" | "pthread_self" | "pthread_get_stacksize_np" => {
714+
// Stack size/address stuff.
715+
"pthread_attr_init" | "pthread_attr_destroy" | "pthread_self" |
716+
"pthread_attr_setstacksize" => {
719717
this.write_null(dest)?;
720718
}
721719
"pthread_attr_getstack" => {
722-
// Second argument is where we are supposed to write the stack size.
723-
let ptr = this.deref_operand(args[1])?;
724-
// Just any address.
725-
let stack_addr = Scalar::from_uint(STACK_ADDR, args[1].layout.size);
726-
this.write_scalar(stack_addr, ptr.into())?;
720+
let addr_place = this.deref_operand(args[1])?;
721+
let size_place = this.deref_operand(args[2])?;
722+
723+
this.write_scalar(
724+
Scalar::from_uint(STACK_ADDR, addr_place.layout.size),
725+
addr_place.into(),
726+
)?;
727+
this.write_scalar(
728+
Scalar::from_uint(STACK_SIZE, size_place.layout.size),
729+
size_place.into(),
730+
)?;
731+
727732
// Return success (`0`).
728733
this.write_null(dest)?;
729734
}
730-
"pthread_get_stackaddr_np" => {
731-
// Just any address.
732-
let stack_addr = Scalar::from_uint(STACK_ADDR, dest.layout.size);
733-
this.write_scalar(stack_addr, dest)?;
735+
736+
// We don't support threading.
737+
"pthread_create" => {
738+
return err!(Unimplemented(format!("Miri does not support threading")));
734739
}
735740

736741
// Stub out calls for condvar, mutex and rwlock, to just return `0`.
@@ -758,6 +763,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
758763
}
759764

760765
// macOS API stubs.
766+
"pthread_attr_get_np" | "pthread_getattr_np" => {
767+
this.write_null(dest)?;
768+
}
769+
"pthread_get_stackaddr_np" => {
770+
let stack_addr = Scalar::from_uint(STACK_ADDR, dest.layout.size);
771+
this.write_scalar(stack_addr, dest)?;
772+
}
773+
"pthread_get_stacksize_np" => {
774+
let stack_size = Scalar::from_uint(STACK_SIZE, dest.layout.size);
775+
this.write_scalar(stack_size, dest)?;
776+
}
761777
"_tlv_atexit" => {
762778
// FIXME: register the destructor.
763779
},

tests/compile-fail/thread-spawn.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use std::thread;
2+
3+
// error-pattern: Miri does not support threading
4+
5+
fn main() {
6+
thread::spawn(|| {});
7+
}

0 commit comments

Comments
 (0)