Skip to content

Commit 2404351

Browse files
committed
better error message when the program tries to spawn a thread
1 parent 954eddf commit 2404351

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
@@ -39,7 +39,7 @@ pub use crate::helpers::{EvalContextExt as HelpersEvalContextExt};
3939
pub use crate::mono_hash_map::MonoHashMap;
4040
pub use crate::stacked_borrows::{EvalContextExt as StackedBorEvalContextExt, Tag, Permission, Stack, Stacks, Item};
4141
pub use crate::machine::{
42-
PAGE_SIZE, STACK_ADDR, NUM_CPUS,
42+
PAGE_SIZE, STACK_ADDR, STACK_SIZE, NUM_CPUS,
4343
MemoryExtra, AllocExtra, MiriMemoryKind, Evaluator, MiriEvalContext, MiriEvalContextExt,
4444
};
4545
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
@@ -15,7 +15,8 @@ use crate::*;
1515

1616
// Some global facts about the emulated machine.
1717
pub const PAGE_SIZE: u64 = 4*1024; // FIXME: adjust to target architecture
18-
pub const STACK_ADDR: u64 = 16*PAGE_SIZE; // not really about the "stack", but where we start assigning integer addresses to allocations
18+
pub const STACK_ADDR: u64 = 32*PAGE_SIZE; // not really about the "stack", but where we start assigning integer addresses to allocations
19+
pub const STACK_SIZE: u64 = 16*PAGE_SIZE; // whatever
1920
pub const NUM_CPUS: u64 = 1;
2021

2122
/// 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
@@ -303,13 +303,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
303303
let symbol_name = this.memory().get(symbol.alloc_id)?.read_c_str(tcx, symbol)?;
304304
let err = format!("bad c unicode symbol: {:?}", symbol_name);
305305
let symbol_name = ::std::str::from_utf8(symbol_name).unwrap_or(&err);
306-
if let Some(dlsym) = Dlsym::from_str(symbol_name) {
306+
if let Some(dlsym) = Dlsym::from_str(symbol_name)? {
307307
let ptr = this.memory_mut().create_fn_alloc(FnVal::Other(dlsym));
308308
this.write_scalar(Scalar::from(ptr), dest)?;
309309
} else {
310-
return err!(Unimplemented(format!(
311-
"Unsupported dlsym: {}", symbol_name
312-
)));
310+
this.write_null(dest)?;
313311
}
314312
}
315313

@@ -697,24 +695,31 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
697695
this.write_null(dest)?;
698696
}
699697

700-
// Determine stack base address.
701-
"pthread_attr_init" | "pthread_attr_destroy" | "pthread_attr_get_np" |
702-
"pthread_getattr_np" | "pthread_self" | "pthread_get_stacksize_np" => {
698+
// Stack size/address stuff.
699+
"pthread_attr_init" | "pthread_attr_destroy" | "pthread_self" |
700+
"pthread_attr_setstacksize" => {
703701
this.write_null(dest)?;
704702
}
705703
"pthread_attr_getstack" => {
706-
// Second argument is where we are supposed to write the stack size.
707-
let ptr = this.deref_operand(args[1])?;
708-
// Just any address.
709-
let stack_addr = Scalar::from_uint(STACK_ADDR, args[1].layout.size);
710-
this.write_scalar(stack_addr, ptr.into())?;
704+
let addr_place = this.deref_operand(args[1])?;
705+
let size_place = this.deref_operand(args[2])?;
706+
707+
this.write_scalar(
708+
Scalar::from_uint(STACK_ADDR, addr_place.layout.size),
709+
addr_place.into(),
710+
)?;
711+
this.write_scalar(
712+
Scalar::from_uint(STACK_SIZE, size_place.layout.size),
713+
size_place.into(),
714+
)?;
715+
711716
// Return success (`0`).
712717
this.write_null(dest)?;
713718
}
714-
"pthread_get_stackaddr_np" => {
715-
// Just any address.
716-
let stack_addr = Scalar::from_uint(STACK_ADDR, dest.layout.size);
717-
this.write_scalar(stack_addr, dest)?;
719+
720+
// We don't support threading.
721+
"pthread_create" => {
722+
return err!(Unimplemented(format!("Miri does not support threading")));
718723
}
719724

720725
// Stub out calls for condvar, mutex and rwlock, to just return `0`.
@@ -742,6 +747,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
742747
}
743748

744749
// macOS API stubs.
750+
"pthread_attr_get_np" | "pthread_getattr_np" => {
751+
this.write_null(dest)?;
752+
}
753+
"pthread_get_stackaddr_np" => {
754+
let stack_addr = Scalar::from_uint(STACK_ADDR, dest.layout.size);
755+
this.write_scalar(stack_addr, dest)?;
756+
}
757+
"pthread_get_stacksize_np" => {
758+
let stack_size = Scalar::from_uint(STACK_SIZE, dest.layout.size);
759+
this.write_scalar(stack_size, dest)?;
760+
}
745761
"_tlv_atexit" => {
746762
// FIXME: register the destructor.
747763
},

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)