Skip to content

Commit 845a6f4

Browse files
committed
better error message when the program tries to spawn a thread
1 parent 31996bd commit 845a6f4

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
@@ -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
@@ -328,13 +328,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
328328
let symbol_name = this.memory().get(symbol.alloc_id)?.read_c_str(tcx, symbol)?;
329329
let err = format!("bad c unicode symbol: {:?}", symbol_name);
330330
let symbol_name = ::std::str::from_utf8(symbol_name).unwrap_or(&err);
331-
if let Some(dlsym) = Dlsym::from_str(symbol_name) {
331+
if let Some(dlsym) = Dlsym::from_str(symbol_name)? {
332332
let ptr = this.memory_mut().create_fn_alloc(FnVal::Other(dlsym));
333333
this.write_scalar(Scalar::from(ptr), dest)?;
334334
} else {
335-
return err!(Unimplemented(format!(
336-
"Unsupported dlsym: {}", symbol_name
337-
)));
335+
this.write_null(dest)?;
338336
}
339337
}
340338

@@ -722,24 +720,31 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
722720
this.write_null(dest)?;
723721
}
724722

725-
// Determine stack base address.
726-
"pthread_attr_init" | "pthread_attr_destroy" | "pthread_attr_get_np" |
727-
"pthread_getattr_np" | "pthread_self" | "pthread_get_stacksize_np" => {
723+
// Stack size/address stuff.
724+
"pthread_attr_init" | "pthread_attr_destroy" | "pthread_self" |
725+
"pthread_attr_setstacksize" => {
728726
this.write_null(dest)?;
729727
}
730728
"pthread_attr_getstack" => {
731-
// Second argument is where we are supposed to write the stack size.
732-
let ptr = this.deref_operand(args[1])?;
733-
// Just any address.
734-
let stack_addr = Scalar::from_uint(STACK_ADDR, args[1].layout.size);
735-
this.write_scalar(stack_addr, ptr.into())?;
729+
let addr_place = this.deref_operand(args[1])?;
730+
let size_place = this.deref_operand(args[2])?;
731+
732+
this.write_scalar(
733+
Scalar::from_uint(STACK_ADDR, addr_place.layout.size),
734+
addr_place.into(),
735+
)?;
736+
this.write_scalar(
737+
Scalar::from_uint(STACK_SIZE, size_place.layout.size),
738+
size_place.into(),
739+
)?;
740+
736741
// Return success (`0`).
737742
this.write_null(dest)?;
738743
}
739-
"pthread_get_stackaddr_np" => {
740-
// Just any address.
741-
let stack_addr = Scalar::from_uint(STACK_ADDR, dest.layout.size);
742-
this.write_scalar(stack_addr, dest)?;
744+
745+
// We don't support threading.
746+
"pthread_create" => {
747+
return err!(Unimplemented(format!("Miri does not support threading")));
743748
}
744749

745750
// Stub out calls for condvar, mutex and rwlock, to just return `0`.
@@ -767,6 +772,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
767772
}
768773

769774
// macOS API stubs.
775+
"pthread_attr_get_np" | "pthread_getattr_np" => {
776+
this.write_null(dest)?;
777+
}
778+
"pthread_get_stackaddr_np" => {
779+
let stack_addr = Scalar::from_uint(STACK_ADDR, dest.layout.size);
780+
this.write_scalar(stack_addr, dest)?;
781+
}
782+
"pthread_get_stacksize_np" => {
783+
let stack_size = Scalar::from_uint(STACK_SIZE, dest.layout.size);
784+
this.write_scalar(stack_size, dest)?;
785+
}
770786
"_tlv_atexit" => {
771787
// FIXME: register the destructor.
772788
},

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)