Skip to content

Commit f05c681

Browse files
author
Vytautas Astrauskas
committed
Implement basic support for concurrency (Linux only).
1 parent 0e8a1a4 commit f05c681

23 files changed

+602
-104
lines changed

src/diagnostics.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub enum NonHaltingDiagnostic {
4343

4444
/// Emit a custom diagnostic without going through the miri-engine machinery
4545
pub fn report_error<'tcx, 'mir>(
46-
ecx: &InterpCx<'mir, 'tcx, Evaluator<'tcx>>,
46+
ecx: &InterpCx<'mir, 'tcx, Evaluator<'mir, 'tcx>>,
4747
mut e: InterpErrorInfo<'tcx>,
4848
) -> Option<i64> {
4949
use InterpError::*;
@@ -108,7 +108,7 @@ pub fn report_error<'tcx, 'mir>(
108108
/// Report an error or note (depending on the `error` argument) at the current frame's current statement.
109109
/// Also emits a full stacktrace of the interpreter stack.
110110
fn report_msg<'tcx, 'mir>(
111-
ecx: &InterpCx<'mir, 'tcx, Evaluator<'tcx>>,
111+
ecx: &InterpCx<'mir, 'tcx, Evaluator<'mir, 'tcx>>,
112112
title: &str,
113113
span_msg: String,
114114
helps: &[String],
@@ -164,7 +164,7 @@ pub fn register_diagnostic(e: NonHaltingDiagnostic) {
164164
DIAGNOSTICS.with(|diagnostics| diagnostics.borrow_mut().push(e));
165165
}
166166

167-
impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
167+
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
168168
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
169169
/// Emit all diagnostics that were registed with `register_diagnostics`
170170
fn process_diagnostics(&self) {

src/eval.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
5959
tcx: TyCtxt<'tcx>,
6060
main_id: DefId,
6161
config: MiriConfig,
62-
) -> InterpResult<'tcx, (InterpCx<'mir, 'tcx, Evaluator<'tcx>>, MPlaceTy<'tcx, Tag>)> {
62+
) -> InterpResult<'tcx, (InterpCx<'mir, 'tcx, Evaluator<'mir, 'tcx>>, MPlaceTy<'tcx, Tag>)> {
6363
let mut ecx = InterpCx::new(
6464
tcx.at(rustc_span::source_map::DUMMY_SP),
6565
ty::ParamEnv::reveal_all(),
@@ -198,7 +198,8 @@ pub fn eval_main<'tcx>(tcx: TyCtxt<'tcx>, main_id: DefId, config: MiriConfig) ->
198198
// Perform the main execution.
199199
let res: InterpResult<'_, i64> = (|| {
200200
// Main loop.
201-
while ecx.step()? {
201+
while ecx.schedule()? {
202+
assert!(ecx.step()?);
202203
ecx.process_diagnostics();
203204
}
204205
// Read the return code pointer *before* we run TLS destructors, to assert

src/helpers.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rand::RngCore;
1212

1313
use crate::*;
1414

15-
impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
15+
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
1616

1717
/// Gets an instance for a path.
1818
fn try_resolve_did<'mir, 'tcx>(tcx: TyCtxt<'tcx>, path: &[&str]) -> Option<DefId> {
@@ -264,7 +264,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
264264
unsafe_cell_action: F,
265265
}
266266

267-
impl<'ecx, 'mir, 'tcx, F> ValueVisitor<'mir, 'tcx, Evaluator<'tcx>>
267+
impl<'ecx, 'mir, 'tcx: 'mir, F> ValueVisitor<'mir, 'tcx, Evaluator<'mir, 'tcx>>
268268
for UnsafeCellVisitor<'ecx, 'mir, 'tcx, F>
269269
where
270270
F: FnMut(MPlaceTy<'tcx, Tag>) -> InterpResult<'tcx>,

src/intptrcast.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl Default for GlobalState {
4141
impl<'mir, 'tcx> GlobalState {
4242
pub fn int_to_ptr(
4343
int: u64,
44-
memory: &Memory<'mir, 'tcx, Evaluator<'tcx>>,
44+
memory: &Memory<'mir, 'tcx, Evaluator<'mir, 'tcx>>,
4545
) -> InterpResult<'tcx, Pointer<Tag>> {
4646
let global_state = memory.extra.intptrcast.borrow();
4747
let pos = global_state.int_to_ptr_map.binary_search_by_key(&int, |(addr, _)| *addr);
@@ -73,7 +73,7 @@ impl<'mir, 'tcx> GlobalState {
7373

7474
pub fn ptr_to_int(
7575
ptr: Pointer<Tag>,
76-
memory: &Memory<'mir, 'tcx, Evaluator<'tcx>>,
76+
memory: &Memory<'mir, 'tcx, Evaluator<'mir, 'tcx>>,
7777
) -> InterpResult<'tcx, u64> {
7878
let mut global_state = memory.extra.intptrcast.borrow_mut();
7979
let global_state = &mut *global_state;

src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ extern crate rustc_ast;
1212
#[macro_use] extern crate rustc_middle;
1313
extern crate rustc_data_structures;
1414
extern crate rustc_hir;
15+
extern crate rustc_index;
1516
extern crate rustc_mir;
1617
extern crate rustc_span;
1718
extern crate rustc_target;
@@ -26,6 +27,7 @@ mod operator;
2627
mod range_map;
2728
mod shims;
2829
mod stacked_borrows;
30+
mod threads;
2931

3032
// Make all those symbols available in the same place as our own.
3133
pub use rustc_mir::interpret::*;
@@ -59,6 +61,7 @@ pub use crate::range_map::RangeMap;
5961
pub use crate::stacked_borrows::{
6062
EvalContextExt as StackedBorEvalContextExt, Item, Permission, PtrId, Stack, Stacks, Tag,
6163
};
64+
pub use crate::threads::EvalContextExt as ThreadsEvalContextExt;
6265

6366
/// Insert rustc arguments at the beginning of the argument list that Miri wants to be
6467
/// set per default, for maximal validation power.

src/machine.rs

+51-21
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ use rustc_span::symbol::{sym, Symbol};
1818

1919
use crate::*;
2020

21+
pub use crate::threads::{ThreadId, ThreadSet, ThreadLocalStorage};
22+
2123
// Some global facts about the emulated machine.
2224
pub const PAGE_SIZE: u64 = 4 * 1024; // FIXME: adjust to target architecture
2325
pub const STACK_ADDR: u64 = 32 * PAGE_SIZE; // not really about the "stack", but where we start assigning integer addresses to allocations
@@ -74,6 +76,7 @@ pub struct AllocExtra {
7476
pub struct MemoryExtra {
7577
pub stacked_borrows: Option<stacked_borrows::MemoryExtra>,
7678
pub intptrcast: intptrcast::MemoryExtra,
79+
pub tls: ThreadLocalStorage,
7780

7881
/// Mapping extern static names to their canonical allocation.
7982
extern_statics: FxHashMap<Symbol, AllocId>,
@@ -100,6 +103,7 @@ impl MemoryExtra {
100103
extern_statics: FxHashMap::default(),
101104
rng: RefCell::new(rng),
102105
tracked_alloc_id,
106+
tls: Default::default(),
103107
}
104108
}
105109

@@ -147,7 +151,7 @@ impl MemoryExtra {
147151
}
148152

149153
/// The machine itself.
150-
pub struct Evaluator<'tcx> {
154+
pub struct Evaluator<'mir, 'tcx> {
151155
/// Environment variables set by `setenv`.
152156
/// Miri does not expose env vars from the host to the emulated program.
153157
pub(crate) env_vars: EnvVars<'tcx>,
@@ -182,9 +186,11 @@ pub struct Evaluator<'tcx> {
182186

183187
/// The "time anchor" for this machine's monotone clock (for `Instant` simulation).
184188
pub(crate) time_anchor: Instant,
189+
190+
pub(crate) threads: ThreadSet<'mir, 'tcx>,
185191
}
186192

187-
impl<'tcx> Evaluator<'tcx> {
193+
impl<'mir, 'tcx> Evaluator<'mir, 'tcx> {
188194
pub(crate) fn new(communicate: bool, validate: bool) -> Self {
189195
Evaluator {
190196
// `env_vars` could be initialized properly here if `Memory` were available before
@@ -201,12 +207,13 @@ impl<'tcx> Evaluator<'tcx> {
201207
dir_handler: Default::default(),
202208
panic_payload: None,
203209
time_anchor: Instant::now(),
210+
threads: Default::default(),
204211
}
205212
}
206213
}
207214

208215
/// A rustc InterpCx for Miri.
209-
pub type MiriEvalContext<'mir, 'tcx> = InterpCx<'mir, 'tcx, Evaluator<'tcx>>;
216+
pub type MiriEvalContext<'mir, 'tcx> = InterpCx<'mir, 'tcx, Evaluator<'mir, 'tcx>>;
210217

211218
/// A little trait that's useful to be inherited by extension traits.
212219
pub trait MiriEvalContextExt<'mir, 'tcx> {
@@ -225,7 +232,7 @@ impl<'mir, 'tcx> MiriEvalContextExt<'mir, 'tcx> for MiriEvalContext<'mir, 'tcx>
225232
}
226233

227234
/// Machine hook implementations.
228-
impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
235+
impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
229236
type MemoryKind = MiriMemoryKind;
230237

231238
type FrameExtra = FrameData<'tcx>;
@@ -241,6 +248,19 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
241248

242249
const CHECK_ALIGN: bool = true;
243250

251+
#[inline(always)]
252+
fn stack<'a>(
253+
ecx: &'a InterpCx<'mir, 'tcx, Self>
254+
) -> &'a [Frame<'mir, 'tcx, Self::PointerTag, Self::FrameExtra>] {
255+
ecx.active_thread_stack()
256+
}
257+
258+
fn stack_mut<'a>(
259+
ecx: &'a mut InterpCx<'mir, 'tcx, Self>
260+
) -> &'a mut Vec<Frame<'mir, 'tcx, Self::PointerTag, Self::FrameExtra>> {
261+
ecx.active_thread_stack_mut()
262+
}
263+
244264
#[inline(always)]
245265
fn enforce_validity(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool {
246266
ecx.machine.validate
@@ -333,29 +353,39 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
333353

334354
fn canonical_alloc_id(mem: &Memory<'mir, 'tcx, Self>, id: AllocId) -> AllocId {
335355
let tcx = mem.tcx;
336-
// Figure out if this is an extern static, and if yes, which one.
337-
let def_id = match tcx.alloc_map.lock().get(id) {
338-
Some(GlobalAlloc::Static(def_id)) if tcx.is_foreign_item(def_id) => def_id,
356+
let alloc = tcx.alloc_map.lock().get(id);
357+
match alloc {
358+
Some(GlobalAlloc::Static(def_id)) if tcx.is_foreign_item(def_id) => {
359+
// Figure out if this is an extern static, and if yes, which one.
360+
let attrs = tcx.get_attrs(def_id);
361+
let link_name = match attr::first_attr_value_str_by_name(&attrs, sym::link_name) {
362+
Some(name) => name,
363+
None => tcx.item_name(def_id),
364+
};
365+
// Check if we know this one.
366+
if let Some(canonical_id) = mem.extra.extern_statics.get(&link_name) {
367+
trace!("canonical_alloc_id: {:?} ({}) -> {:?}", id, link_name, canonical_id);
368+
*canonical_id
369+
} else {
370+
// Return original id; `Memory::get_static_alloc` will throw an error.
371+
id
372+
}
373+
},
374+
Some(GlobalAlloc::Static(def_id)) if tcx.has_attr(def_id, sym::thread_local) => {
375+
// We have a thread local, so we need to get a unique allocation id for it.
376+
mem.extra.tls.get_or_register_allocation(*tcx, id)
377+
},
339378
_ => {
340379
// No need to canonicalize anything.
341-
return id;
380+
id
342381
}
343-
};
344-
let attrs = tcx.get_attrs(def_id);
345-
let link_name = match attr::first_attr_value_str_by_name(&attrs, sym::link_name) {
346-
Some(name) => name,
347-
None => tcx.item_name(def_id),
348-
};
349-
// Check if we know this one.
350-
if let Some(canonical_id) = mem.extra.extern_statics.get(&link_name) {
351-
trace!("canonical_alloc_id: {:?} ({}) -> {:?}", id, link_name, canonical_id);
352-
*canonical_id
353-
} else {
354-
// Return original id; `Memory::get_static_alloc` will throw an error.
355-
id
356382
}
357383
}
358384

385+
fn resolve_thread_local_allocation_id(extra: &Self::MemoryExtra, id: AllocId) -> AllocId {
386+
extra.tls.resolve_allocation(id)
387+
}
388+
359389
fn init_allocation_extra<'b>(
360390
memory_extra: &MemoryExtra,
361391
id: AllocId,

src/shims/dlsym.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl Dlsym {
2020
}
2121
}
2222

23-
impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
23+
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
2424
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
2525
fn call_dlsym(
2626
&mut self,

src/shims/env.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub struct EnvVars<'tcx> {
3535

3636
impl<'tcx> EnvVars<'tcx> {
3737
pub(crate) fn init<'mir>(
38-
ecx: &mut InterpCx<'mir, 'tcx, Evaluator<'tcx>>,
38+
ecx: &mut InterpCx<'mir, 'tcx, Evaluator<'mir, 'tcx>>,
3939
mut excluded_env_vars: Vec<String>,
4040
) -> InterpResult<'tcx> {
4141
let target_os = ecx.tcx.sess.target.target.target_os.as_str();
@@ -61,7 +61,7 @@ impl<'tcx> EnvVars<'tcx> {
6161
}
6262

6363
pub(crate) fn cleanup<'mir>(
64-
ecx: &mut InterpCx<'mir, 'tcx, Evaluator<'tcx>>,
64+
ecx: &mut InterpCx<'mir, 'tcx, Evaluator<'mir, 'tcx>>,
6565
) -> InterpResult<'tcx> {
6666
// Deallocate individual env vars.
6767
for (_name, ptr) in ecx.machine.env_vars.map.drain() {
@@ -78,7 +78,7 @@ impl<'tcx> EnvVars<'tcx> {
7878
fn alloc_env_var_as_c_str<'mir, 'tcx>(
7979
name: &OsStr,
8080
value: &OsStr,
81-
ecx: &mut InterpCx<'mir, 'tcx, Evaluator<'tcx>>,
81+
ecx: &mut InterpCx<'mir, 'tcx, Evaluator<'mir, 'tcx>>,
8282
) -> InterpResult<'tcx, Pointer<Tag>> {
8383
let mut name_osstring = name.to_os_string();
8484
name_osstring.push("=");
@@ -89,15 +89,15 @@ fn alloc_env_var_as_c_str<'mir, 'tcx>(
8989
fn alloc_env_var_as_wide_str<'mir, 'tcx>(
9090
name: &OsStr,
9191
value: &OsStr,
92-
ecx: &mut InterpCx<'mir, 'tcx, Evaluator<'tcx>>,
92+
ecx: &mut InterpCx<'mir, 'tcx, Evaluator<'mir, 'tcx>>,
9393
) -> InterpResult<'tcx, Pointer<Tag>> {
9494
let mut name_osstring = name.to_os_string();
9595
name_osstring.push("=");
9696
name_osstring.push(value);
9797
Ok(ecx.alloc_os_str_as_wide_str(name_osstring.as_os_str(), MiriMemoryKind::Env.into()))
9898
}
9999

100-
impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
100+
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
101101
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
102102
fn getenv(&mut self, name_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, Scalar<Tag>> {
103103
let this = self.eval_context_mut();

src/shims/foreign_items.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_ast::attr;
1212

1313
use crate::*;
1414

15-
impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
15+
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
1616
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
1717
/// Returns the minimum alignment for the target architecture for allocations of the given size.
1818
fn min_align(&self, size: u64, kind: MiriMemoryKind) -> Align {

0 commit comments

Comments
 (0)