Skip to content

Commit ea4232c

Browse files
committed
Move TLS data to machine data
There is no good reason to let the machine store stuff in the machine *and* in memory. I plan to get rid of the latter.
1 parent 384c2be commit ea4232c

File tree

4 files changed

+94
-116
lines changed

4 files changed

+94
-116
lines changed

src/fn_call.rs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ use std::mem;
88

99
use super::*;
1010

11-
use tls::MemoryExt;
12-
13-
use super::memory::MemoryKind;
14-
1511
pub trait EvalContextExt<'tcx, 'mir> {
1612
/// Emulate calling a foreign item, fail if the item is not supported.
1713
/// This function will handle `goto_block` if needed.
@@ -129,7 +125,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx, 'mir> for EvalContext<'a, '
129125
self.write_null(dest)?;
130126
} else {
131127
let align = self.tcx.data_layout.pointer_align;
132-
let ptr = self.memory.allocate(Size::from_bytes(size), align, MemoryKind::C.into())?;
128+
let ptr = self.memory.allocate(Size::from_bytes(size), align, MiriMemoryKind::C.into())?;
133129
self.write_scalar(Scalar::Ptr(ptr), dest)?;
134130
}
135131
}
@@ -140,7 +136,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx, 'mir> for EvalContext<'a, '
140136
self.memory.deallocate(
141137
ptr.to_ptr()?,
142138
None,
143-
MemoryKind::C.into(),
139+
MiriMemoryKind::C.into(),
144140
)?;
145141
}
146142
}
@@ -156,7 +152,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx, 'mir> for EvalContext<'a, '
156152
}
157153
let ptr = self.memory.allocate(Size::from_bytes(size),
158154
Align::from_bytes(align, align).unwrap(),
159-
MemoryKind::Rust.into())?;
155+
MiriMemoryKind::Rust.into())?;
160156
self.write_scalar(Scalar::Ptr(ptr), dest)?;
161157
}
162158
"__rust_alloc_zeroed" => {
@@ -170,7 +166,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx, 'mir> for EvalContext<'a, '
170166
}
171167
let ptr = self.memory.allocate(Size::from_bytes(size),
172168
Align::from_bytes(align, align).unwrap(),
173-
MemoryKind::Rust.into())?;
169+
MiriMemoryKind::Rust.into())?;
174170
self.memory.write_repeat(ptr.into(), 0, Size::from_bytes(size))?;
175171
self.write_scalar(Scalar::Ptr(ptr), dest)?;
176172
}
@@ -187,7 +183,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx, 'mir> for EvalContext<'a, '
187183
self.memory.deallocate(
188184
ptr,
189185
Some((Size::from_bytes(old_size), Align::from_bytes(align, align).unwrap())),
190-
MemoryKind::Rust.into(),
186+
MiriMemoryKind::Rust.into(),
191187
)?;
192188
}
193189
"__rust_realloc" => {
@@ -207,7 +203,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx, 'mir> for EvalContext<'a, '
207203
Align::from_bytes(align, align).unwrap(),
208204
Size::from_bytes(new_size),
209205
Align::from_bytes(align, align).unwrap(),
210-
MemoryKind::Rust.into(),
206+
MiriMemoryKind::Rust.into(),
211207
)?;
212208
self.write_scalar(Scalar::Ptr(new_ptr), dest)?;
213209
}
@@ -365,7 +361,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx, 'mir> for EvalContext<'a, '
365361
}
366362
if let Some(old) = success {
367363
if let Some(var) = old {
368-
self.memory.deallocate(var, None, MemoryKind::Env.into())?;
364+
self.memory.deallocate(var, None, MiriMemoryKind::Env.into())?;
369365
}
370366
self.write_null(dest)?;
371367
} else {
@@ -391,7 +387,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx, 'mir> for EvalContext<'a, '
391387
let value_copy = self.memory.allocate(
392388
Size::from_bytes((value.len() + 1) as u64),
393389
Align::from_bytes(1, 1).unwrap(),
394-
MemoryKind::Env.into(),
390+
MiriMemoryKind::Env.into(),
395391
)?;
396392
self.memory.write_bytes(value_copy.into(), &value)?;
397393
let trailing_zero_ptr = value_copy.offset(Size::from_bytes(value.len() as u64), &self)?.into();
@@ -401,7 +397,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx, 'mir> for EvalContext<'a, '
401397
value_copy,
402398
)
403399
{
404-
self.memory.deallocate(var, None, MemoryKind::Env.into())?;
400+
self.memory.deallocate(var, None, MiriMemoryKind::Env.into())?;
405401
}
406402
self.write_null(dest)?;
407403
} else {
@@ -504,7 +500,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx, 'mir> for EvalContext<'a, '
504500
let key_layout = self.layout_of(key_type)?;
505501

506502
// Create key and write it into the memory where key_ptr wants it
507-
let key = self.memory.create_tls_key(dtor) as u128;
503+
let key = self.machine.tls.create_tls_key(dtor, *self.tcx) as u128;
508504
if key_layout.size.bits() < 128 && key >= (1u128 << key_layout.size.bits() as u128) {
509505
return err!(OutOfTls);
510506
}
@@ -520,19 +516,19 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx, 'mir> for EvalContext<'a, '
520516
}
521517
"pthread_key_delete" => {
522518
let key = self.read_scalar(args[0])?.to_bytes()?;
523-
self.memory.delete_tls_key(key)?;
519+
self.machine.tls.delete_tls_key(key)?;
524520
// Return success (0)
525521
self.write_null(dest)?;
526522
}
527523
"pthread_getspecific" => {
528524
let key = self.read_scalar(args[0])?.to_bytes()?;
529-
let ptr = self.memory.load_tls(key)?;
525+
let ptr = self.machine.tls.load_tls(key)?;
530526
self.write_scalar(ptr, dest)?;
531527
}
532528
"pthread_setspecific" => {
533529
let key = self.read_scalar(args[0])?.to_bytes()?;
534530
let new_ptr = self.read_scalar(args[1])?.not_undef()?;
535-
self.memory.store_tls(key, new_ptr)?;
531+
self.machine.tls.store_tls(key, new_ptr)?;
536532

537533
// Return success (0)
538534
self.write_null(dest)?;
@@ -607,7 +603,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx, 'mir> for EvalContext<'a, '
607603
// This just creates a key; Windows does not natively support TLS dtors.
608604

609605
// Create key and return it
610-
let key = self.memory.create_tls_key(None) as u128;
606+
let key = self.machine.tls.create_tls_key(None, *self.tcx) as u128;
611607

612608
// Figure out how large a TLS key actually is. This is c::DWORD.
613609
if dest.layout.size.bits() < 128 && key >= (1u128 << dest.layout.size.bits() as u128) {
@@ -617,13 +613,13 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx, 'mir> for EvalContext<'a, '
617613
}
618614
"TlsGetValue" => {
619615
let key = self.read_scalar(args[0])?.to_bytes()?;
620-
let ptr = self.memory.load_tls(key)?;
616+
let ptr = self.machine.tls.load_tls(key)?;
621617
self.write_scalar(ptr, dest)?;
622618
}
623619
"TlsSetValue" => {
624620
let key = self.read_scalar(args[0])?.to_bytes()?;
625621
let new_ptr = self.read_scalar(args[1])?.not_undef()?;
626-
self.memory.store_tls(key, new_ptr)?;
622+
self.machine.tls.store_tls(key, new_ptr)?;
627623

628624
// Return success (1)
629625
self.write_scalar(Scalar::from_int(1, dest.layout.size), dest)?;

src/lib.rs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,24 @@ use rustc::mir;
2121
use syntax::ast::Mutability;
2222
use syntax::attr;
2323

24-
use std::marker::PhantomData;
2524
use std::collections::HashMap;
2625

2726
pub use rustc::mir::interpret::*;
2827
pub use rustc_mir::interpret::*;
28+
pub use rustc_mir::interpret;
2929

3030
mod fn_call;
3131
mod operator;
3232
mod intrinsic;
3333
mod helpers;
34-
mod memory;
3534
mod tls;
3635
mod locks;
3736
mod range_map;
3837

3938
use fn_call::EvalContextExt as MissingFnsEvalContextExt;
4039
use operator::EvalContextExt as OperatorEvalContextExt;
4140
use intrinsic::EvalContextExt as IntrinsicEvalContextExt;
42-
use tls::EvalContextExt as TlsEvalContextExt;
43-
use memory::{MemoryKind as MiriMemoryKind, TlsKey, TlsEntry, MemoryData};
44-
use locks::LockInfo;
41+
use tls::{EvalContextExt as TlsEvalContextExt, TlsData};
4542
use range_map::RangeMap;
4643
use helpers::FalibleScalarExt;
4744

@@ -54,7 +51,7 @@ pub fn create_ecx<'a, 'mir: 'a, 'tcx: 'mir>(
5451
tcx.at(syntax::source_map::DUMMY_SP),
5552
ty::ParamEnv::reveal_all(),
5653
Default::default(),
57-
MemoryData::new()
54+
Default::default(),
5855
);
5956

6057
let main_instance = ty::Instance::mono(ecx.tcx.tcx, main_id);
@@ -201,21 +198,41 @@ pub fn eval_main<'a, 'tcx: 'a>(
201198
}
202199
}
203200

201+
202+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
203+
pub enum MiriMemoryKind {
204+
/// `__rust_alloc` memory
205+
Rust,
206+
/// `malloc` memory
207+
C,
208+
/// Part of env var emulation
209+
Env,
210+
/// mutable statics
211+
MutStatic,
212+
}
213+
214+
impl Into<MemoryKind<MiriMemoryKind>> for MiriMemoryKind {
215+
fn into(self) -> MemoryKind<MiriMemoryKind> {
216+
MemoryKind::Machine(self)
217+
}
218+
}
219+
220+
204221
#[derive(Clone, Default, PartialEq, Eq)]
205222
pub struct Evaluator<'tcx> {
206223
/// Environment variables set by `setenv`
207224
/// Miri does not expose env vars from the host to the emulated program
208225
pub(crate) env_vars: HashMap<Vec<u8>, Pointer>,
209226

210-
/// Use the lifetime
211-
_dummy : PhantomData<&'tcx ()>,
227+
/// TLS state
228+
pub(crate) tls: TlsData<'tcx>,
212229
}
213230

214231
impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
215-
type MemoryData = memory::MemoryData<'tcx>;
216-
type MemoryKinds = memory::MemoryKind;
232+
type MemoryData = ();
233+
type MemoryKinds = MiriMemoryKind;
217234

218-
const MUT_STATIC_KIND: Option<memory::MemoryKind> = Some(memory::MemoryKind::MutStatic);
235+
const MUT_STATIC_KIND: Option<MiriMemoryKind> = Some(MiriMemoryKind::MutStatic);
219236
const DETECT_LOOPS: bool = false;
220237

221238
/// Returns Ok() when the function was handled, fail otherwise

src/memory.rs

Lines changed: 0 additions & 57 deletions
This file was deleted.

0 commit comments

Comments
 (0)