Skip to content

Commit ea806e7

Browse files
author
Alexander Regueiro
committed
Removed uninitialized_statics field from Memory struct in miri.
Refactored code accordingly.
1 parent c697a56 commit ea806e7

File tree

4 files changed

+64
-93
lines changed

4 files changed

+64
-93
lines changed

src/librustc_mir/interpret/const_eval.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use std::fmt;
2+
use std::error::Error;
3+
14
use rustc::hir;
25
use rustc::mir::interpret::{ConstEvalErr};
36
use rustc::mir;
@@ -15,9 +18,6 @@ use rustc::mir::interpret::{
1518
};
1619
use super::{Place, EvalContext, StackPopCleanup, ValTy, PlaceExtra, Memory, MemoryKind};
1720

18-
use std::fmt;
19-
use std::error::Error;
20-
2121
pub fn mk_borrowck_eval_cx<'a, 'mir, 'tcx>(
2222
tcx: TyCtxt<'a, 'tcx, 'tcx>,
2323
instance: Instance<'tcx>,
@@ -152,7 +152,7 @@ fn eval_body_using_ecx<'a, 'mir, 'tcx>(
152152
let ptr = ecx.memory.allocate(
153153
layout.size,
154154
layout.align,
155-
None,
155+
MemoryKind::Stack,
156156
)?;
157157
let internally_mutable = !layout.ty.is_freeze(tcx, param_env, mir.span);
158158
let is_static = tcx.is_static(cid.instance.def_id());
@@ -486,7 +486,7 @@ pub fn const_variant_index<'a, 'tcx>(
486486
let (ptr, align) = match value {
487487
Value::ScalarPair(..) | Value::Scalar(_) => {
488488
let layout = ecx.layout_of(val.ty)?;
489-
let ptr = ecx.memory.allocate(layout.size, layout.align, Some(MemoryKind::Stack))?.into();
489+
let ptr = ecx.memory.allocate(layout.size, layout.align, MemoryKind::Stack)?.into();
490490
ecx.write_value_to_ptr(value, ptr, layout.align, val.ty)?;
491491
(ptr, layout.align)
492492
},
@@ -515,7 +515,7 @@ pub fn const_value_to_allocation_provider<'a, 'tcx>(
515515
());
516516
let value = ecx.const_to_value(val.val)?;
517517
let layout = ecx.layout_of(val.ty)?;
518-
let ptr = ecx.memory.allocate(layout.size, layout.align, Some(MemoryKind::Stack))?;
518+
let ptr = ecx.memory.allocate(layout.size, layout.align, MemoryKind::Stack)?;
519519
ecx.write_value_to_ptr(value, ptr.into(), layout.align, val.ty)?;
520520
let alloc = ecx.memory.get(ptr.alloc_id)?;
521521
Ok(tcx.intern_const_alloc(alloc.clone()))

src/librustc_mir/interpret/eval_context.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::fmt::Write;
2+
use std::mem;
23

34
use rustc::hir::def_id::DefId;
45
use rustc::hir::def::Def;
@@ -9,14 +10,13 @@ use rustc::ty::subst::{Subst, Substs};
910
use rustc::ty::{self, Ty, TyCtxt, TypeAndMut};
1011
use rustc::ty::query::TyCtxtAt;
1112
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
12-
use rustc::mir::interpret::FrameInfo;
13-
use syntax::codemap::{self, Span};
14-
use syntax::ast::Mutability;
1513
use rustc::mir::interpret::{
16-
GlobalId, Value, Scalar,
14+
FrameInfo, GlobalId, Value, Scalar,
1715
EvalResult, EvalErrorKind, Pointer, ConstValue,
1816
};
19-
use std::mem;
17+
18+
use syntax::codemap::{self, Span};
19+
use syntax::ast::Mutability;
2020

2121
use super::{Place, PlaceExtra, Memory,
2222
HasMemory, MemoryKind,
@@ -206,7 +206,7 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
206206
let layout = self.layout_of(ty)?;
207207
assert!(!layout.is_unsized(), "cannot alloc memory for unsized type");
208208

209-
self.memory.allocate(layout.size, layout.align, Some(MemoryKind::Stack))
209+
self.memory.allocate(layout.size, layout.align, MemoryKind::Stack)
210210
}
211211

212212
pub fn memory(&self) -> &Memory<'a, 'mir, 'tcx, M> {
@@ -246,7 +246,7 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
246246
}
247247
ConstValue::ByRef(alloc, offset) => {
248248
// FIXME: Allocate new AllocId for all constants inside
249-
let id = self.memory.allocate_value(alloc.clone(), Some(MemoryKind::Stack))?;
249+
let id = self.memory.allocate_value(alloc.clone(), MemoryKind::Stack)?;
250250
Ok(Value::ByRef(Pointer::new(id, offset).into(), alloc.align))
251251
},
252252
ConstValue::ScalarPair(a, b) => Ok(Value::ScalarPair(a, b)),

src/librustc_mir/interpret/memory.rs

+47-77
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ use rustc::ty::Instance;
66
use rustc::ty::ParamEnv;
77
use rustc::ty::query::TyCtxtAt;
88
use rustc::ty::layout::{self, Align, TargetDataLayout, Size};
9-
use syntax::ast::Mutability;
10-
11-
use rustc_data_structures::fx::{FxHashSet, FxHashMap};
129
use rustc::mir::interpret::{Pointer, AllocId, Allocation, AccessKind, Value,
1310
EvalResult, Scalar, EvalErrorKind, GlobalId, AllocType};
1411
pub use rustc::mir::interpret::{write_target_uint, write_target_int, read_target_uint};
12+
use rustc_data_structures::fx::{FxHashSet, FxHashMap};
13+
14+
use syntax::ast::Mutability;
1515

1616
use super::{EvalContext, Machine};
1717

@@ -41,11 +41,6 @@ pub struct Memory<'a, 'mir, 'tcx: 'a + 'mir, M: Machine<'mir, 'tcx>> {
4141
/// Actual memory allocations (arbitrary bytes, may contain pointers into other allocations).
4242
alloc_map: FxHashMap<AllocId, Allocation>,
4343

44-
/// Actual memory allocations (arbitrary bytes, may contain pointers into other allocations).
45-
///
46-
/// Stores statics while they are being processed, before they are interned and thus frozen
47-
uninitialized_statics: FxHashMap<AllocId, Allocation>,
48-
4944
/// The current stack frame. Used to check accesses against locks.
5045
pub cur_frame: usize,
5146

@@ -58,7 +53,6 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
5853
data,
5954
alloc_kind: FxHashMap::default(),
6055
alloc_map: FxHashMap::default(),
61-
uninitialized_statics: FxHashMap::default(),
6256
tcx,
6357
cur_frame: usize::max_value(),
6458
}
@@ -82,20 +76,12 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
8276
pub fn allocate_value(
8377
&mut self,
8478
alloc: Allocation,
85-
kind: Option<MemoryKind<M::MemoryKinds>>,
79+
kind: MemoryKind<M::MemoryKinds>,
8680
) -> EvalResult<'tcx, AllocId> {
8781
let id = self.tcx.alloc_map.lock().reserve();
8882
M::add_lock(self, id);
89-
match kind {
90-
Some(kind @ MemoryKind::Stack) |
91-
Some(kind @ MemoryKind::Machine(_)) => {
92-
self.alloc_map.insert(id, alloc);
93-
self.alloc_kind.insert(id, kind);
94-
},
95-
None => {
96-
self.uninitialized_statics.insert(id, alloc);
97-
},
98-
}
83+
self.alloc_map.insert(id, alloc);
84+
self.alloc_kind.insert(id, kind);
9985
Ok(id)
10086
}
10187

@@ -104,7 +90,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
10490
&mut self,
10591
size: Size,
10692
align: Align,
107-
kind: Option<MemoryKind<M::MemoryKinds>>,
93+
kind: MemoryKind<M::MemoryKinds>,
10894
) -> EvalResult<'tcx, Pointer> {
10995
self.allocate_value(Allocation::undef(size, align), kind).map(Pointer::from)
11096
}
@@ -132,7 +118,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
132118
}
133119

134120
// For simplicities' sake, we implement reallocate as "alloc, copy, dealloc"
135-
let new_ptr = self.allocate(new_size, new_align, Some(kind))?;
121+
let new_ptr = self.allocate(new_size, new_align, kind)?;
136122
self.copy(
137123
ptr.into(),
138124
old_align,
@@ -168,12 +154,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
168154

169155
let alloc = match self.alloc_map.remove(&ptr.alloc_id) {
170156
Some(alloc) => alloc,
171-
None => if self.uninitialized_statics.contains_key(&ptr.alloc_id) {
172-
return err!(DeallocatedWrongMemoryKind(
173-
"uninitializedstatic".to_string(),
174-
format!("{:?}", kind),
175-
))
176-
} else {
157+
None => {
177158
return match self.tcx.alloc_map.lock().get(ptr.alloc_id) {
178159
Some(AllocType::Function(..)) => err!(DeallocatedWrongMemoryKind(
179160
"function".to_string(),
@@ -299,24 +280,21 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
299280
pub fn get(&self, id: AllocId) -> EvalResult<'tcx, &Allocation> {
300281
// normal alloc?
301282
match self.alloc_map.get(&id) {
302-
Some(alloc) => Ok(alloc),
283+
Some(alloc) => Ok(alloc),
303284
// uninitialized static alloc?
304-
None => match self.uninitialized_statics.get(&id) {
305-
Some(alloc) => Ok(alloc),
306-
None => {
307-
// static alloc?
308-
let alloc = self.tcx.alloc_map.lock().get(id);
309-
match alloc {
310-
Some(AllocType::Memory(mem)) => Ok(mem),
311-
Some(AllocType::Function(..)) => {
312-
Err(EvalErrorKind::DerefFunctionPointer.into())
313-
}
314-
Some(AllocType::Static(did)) => {
315-
self.const_eval_static(did)
316-
}
317-
None => Err(EvalErrorKind::DanglingPointerDeref.into()),
285+
None => {
286+
// static alloc?
287+
let alloc = self.tcx.alloc_map.lock().get(id);
288+
match alloc {
289+
Some(AllocType::Memory(mem)) => Ok(mem),
290+
Some(AllocType::Function(..)) => {
291+
Err(EvalErrorKind::DerefFunctionPointer.into())
292+
}
293+
Some(AllocType::Static(did)) => {
294+
self.const_eval_static(did)
318295
}
319-
},
296+
None => Err(EvalErrorKind::DanglingPointerDeref.into()),
297+
}
320298
},
321299
}
322300
}
@@ -329,17 +307,14 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
329307
match self.alloc_map.get_mut(&id) {
330308
Some(alloc) => Ok(alloc),
331309
// uninitialized static alloc?
332-
None => match self.uninitialized_statics.get_mut(&id) {
333-
Some(alloc) => Ok(alloc),
334-
None => {
335-
// no alloc or immutable alloc? produce an error
336-
match self.tcx.alloc_map.lock().get(id) {
337-
Some(AllocType::Memory(..)) |
338-
Some(AllocType::Static(..)) => err!(ModifiedConstantMemory),
339-
Some(AllocType::Function(..)) => err!(DerefFunctionPointer),
340-
None => err!(DanglingPointerDeref),
341-
}
342-
},
310+
None => {
311+
// no alloc or immutable alloc? produce an error
312+
match self.tcx.alloc_map.lock().get(id) {
313+
Some(AllocType::Memory(..)) |
314+
Some(AllocType::Static(..)) => err!(ModifiedConstantMemory),
315+
Some(AllocType::Function(..)) => err!(DerefFunctionPointer),
316+
None => err!(DanglingPointerDeref),
317+
}
343318
},
344319
}
345320
}
@@ -390,27 +365,23 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
390365
MemoryKind::Stack => " (stack)".to_owned(),
391366
MemoryKind::Machine(m) => format!(" ({:?})", m),
392367
}),
393-
// uninitialized static alloc?
394-
None => match self.uninitialized_statics.get(&id) {
395-
Some(a) => (a, " (static in the process of initialization)".to_owned()),
396-
None => {
397-
// static alloc?
398-
match self.tcx.alloc_map.lock().get(id) {
399-
Some(AllocType::Memory(a)) => (a, "(immutable)".to_owned()),
400-
Some(AllocType::Function(func)) => {
401-
trace!("{} {}", msg, func);
402-
continue;
403-
}
404-
Some(AllocType::Static(did)) => {
405-
trace!("{} {:?}", msg, did);
406-
continue;
407-
}
408-
None => {
409-
trace!("{} (deallocated)", msg);
410-
continue;
411-
}
368+
None => {
369+
// static alloc?
370+
match self.tcx.alloc_map.lock().get(id) {
371+
Some(AllocType::Memory(a)) => (a, "(immutable)".to_owned()),
372+
Some(AllocType::Function(func)) => {
373+
trace!("{} {}", msg, func);
374+
continue;
412375
}
413-
},
376+
Some(AllocType::Static(did)) => {
377+
trace!("{} {:?}", msg, did);
378+
continue;
379+
}
380+
None => {
381+
trace!("{} (deallocated)", msg);
382+
continue;
383+
}
384+
}
414385
},
415386
};
416387

@@ -569,8 +540,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
569540
Some(MemoryKind::Machine(_)) => bug!("machine didn't handle machine alloc"),
570541
Some(MemoryKind::Stack) => {},
571542
}
572-
let uninit = self.uninitialized_statics.remove(&alloc_id);
573-
if let Some(mut alloc) = alloc.or(uninit) {
543+
if let Some(mut alloc) = alloc {
574544
// ensure llvm knows not to put this into immutable memroy
575545
alloc.runtime_mutability = mutability;
576546
let alloc = self.tcx.intern_const_alloc(alloc);

src/librustc_mir/interpret/traits.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use rustc::ty::{self, Ty};
22
use rustc::ty::layout::{Size, Align, LayoutOf};
3+
use rustc::mir::interpret::{Scalar, Value, Pointer, EvalResult};
4+
35
use syntax::ast::Mutability;
46

5-
use rustc::mir::interpret::{Scalar, Value, Pointer, EvalResult};
6-
use super::{EvalContext, Machine};
7+
use super::{EvalContext, Machine, MemoryKind};
78

89
impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
910
/// Creates a dynamic vtable for the given type and vtable origin. This is used only for
@@ -30,7 +31,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
3031
let vtable = self.memory.allocate(
3132
ptr_size * (3 + methods.len() as u64),
3233
ptr_align,
33-
None,
34+
MemoryKind::Stack,
3435
)?;
3536

3637
let drop = ::monomorphize::resolve_drop_in_place(*self.tcx, ty);

0 commit comments

Comments
 (0)