@@ -6,12 +6,12 @@ use rustc::ty::Instance;
66use rustc:: ty:: ParamEnv ;
77use rustc:: ty:: query:: TyCtxtAt ;
88use rustc:: ty:: layout:: { self , Align , TargetDataLayout , Size } ;
9- use syntax:: ast:: Mutability ;
10-
11- use rustc_data_structures:: fx:: { FxHashSet , FxHashMap } ;
129use rustc:: mir:: interpret:: { Pointer , AllocId , Allocation , AccessKind , Value ,
1310 EvalResult , Scalar , EvalErrorKind , GlobalId , AllocType } ;
1411pub 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
1616use 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) ;
0 commit comments