@@ -64,7 +64,10 @@ pub enum MiriMemoryKind {
64
64
Global ,
65
65
/// Memory for extern statics.
66
66
/// This memory may leak.
67
- ExternGlobal ,
67
+ ExternStatic ,
68
+ /// Memory for thread-local statics.
69
+ /// This memory may leak.
70
+ Tls ,
68
71
}
69
72
70
73
impl Into < MemoryKind < MiriMemoryKind > > for MiriMemoryKind {
@@ -80,7 +83,7 @@ impl MayLeak for MiriMemoryKind {
80
83
use self :: MiriMemoryKind :: * ;
81
84
match self {
82
85
Rust | C | WinHeap | Env => false ,
83
- Machine | Global | ExternGlobal => true ,
86
+ Machine | Global | ExternStatic | Tls => true ,
84
87
}
85
88
}
86
89
}
@@ -94,8 +97,9 @@ impl fmt::Display for MiriMemoryKind {
94
97
WinHeap => write ! ( f, "Windows heap" ) ,
95
98
Machine => write ! ( f, "machine-managed memory" ) ,
96
99
Env => write ! ( f, "environment variable" ) ,
97
- Global => write ! ( f, "global" ) ,
98
- ExternGlobal => write ! ( f, "extern global" ) ,
100
+ Global => write ! ( f, "global (static or const)" ) ,
101
+ ExternStatic => write ! ( f, "extern static" ) ,
102
+ Tls => write ! ( f, "thread-local static" ) ,
99
103
}
100
104
}
101
105
}
@@ -175,7 +179,7 @@ impl MemoryExtra {
175
179
// "__cxa_thread_atexit_impl"
176
180
// This should be all-zero, pointer-sized.
177
181
let layout = this. machine . layouts . usize ;
178
- let place = this. allocate ( layout, MiriMemoryKind :: ExternGlobal . into ( ) ) ;
182
+ let place = this. allocate ( layout, MiriMemoryKind :: ExternStatic . into ( ) ) ;
179
183
this. write_scalar ( Scalar :: from_machine_usize ( 0 , this) , place. into ( ) ) ?;
180
184
Self :: add_extern_static ( this, "__cxa_thread_atexit_impl" , place. ptr ) ;
181
185
// "environ"
@@ -185,7 +189,7 @@ impl MemoryExtra {
185
189
// "_tls_used"
186
190
// This is some obscure hack that is part of the Windows TLS story. It's a `u8`.
187
191
let layout = this. machine . layouts . u8 ;
188
- let place = this. allocate ( layout, MiriMemoryKind :: ExternGlobal . into ( ) ) ;
192
+ let place = this. allocate ( layout, MiriMemoryKind :: ExternStatic . into ( ) ) ;
189
193
this. write_scalar ( Scalar :: from_u8 ( 0 ) , place. into ( ) ) ?;
190
194
Self :: add_extern_static ( this, "_tls_used" , place. ptr ) ;
191
195
}
@@ -426,44 +430,26 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
426
430
Ok ( ( ) )
427
431
}
428
432
429
- fn thread_local_alloc_id (
433
+ fn thread_local_static_alloc_id (
430
434
ecx : & mut InterpCx < ' mir , ' tcx , Self > ,
431
435
def_id : DefId ,
432
436
) -> InterpResult < ' tcx , AllocId > {
433
437
ecx. get_or_create_thread_local_alloc_id ( def_id)
434
438
}
435
439
436
- fn adjust_global_const (
437
- ecx : & InterpCx < ' mir , ' tcx , Self > ,
438
- mut val : mir:: interpret:: ConstValue < ' tcx > ,
439
- ) -> InterpResult < ' tcx , mir:: interpret:: ConstValue < ' tcx > > {
440
- // FIXME: Remove this, do The Right Thing in `thread_local_alloc_id` instead.
441
- ecx. remap_thread_local_alloc_ids ( & mut val) ?;
442
- Ok ( val)
443
- }
444
-
445
- fn canonical_alloc_id ( mem : & Memory < ' mir , ' tcx , Self > , id : AllocId ) -> AllocId {
446
- let tcx = mem. tcx ;
447
- // Figure out if this is an extern static, and if yes, which one.
448
- let def_id = match tcx. get_global_alloc ( id) {
449
- Some ( GlobalAlloc :: Static ( def_id) ) if tcx. is_foreign_item ( def_id) => def_id,
450
- _ => {
451
- // No need to canonicalize anything.
452
- return id;
453
- }
454
- } ;
455
- let attrs = tcx. get_attrs ( def_id) ;
440
+ fn extern_static_alloc_id (
441
+ memory : & Memory < ' mir , ' tcx , Self > ,
442
+ def_id : DefId ,
443
+ ) -> InterpResult < ' tcx , AllocId > {
444
+ let attrs = memory. tcx . get_attrs ( def_id) ;
456
445
let link_name = match attr:: first_attr_value_str_by_name ( & attrs, sym:: link_name) {
457
446
Some ( name) => name,
458
- None => tcx. item_name ( def_id) ,
447
+ None => memory . tcx . item_name ( def_id) ,
459
448
} ;
460
- // Check if we know this one.
461
- if let Some ( canonical_id) = mem. extra . extern_statics . get ( & link_name) {
462
- trace ! ( "canonical_alloc_id: {:?} ({}) -> {:?}" , id, link_name, canonical_id) ;
463
- * canonical_id
449
+ if let Some ( & id) = memory. extra . extern_statics . get ( & link_name) {
450
+ Ok ( id)
464
451
} else {
465
- // Return original id; `Memory::get_static_alloc` will throw an error.
466
- id
452
+ throw_unsup_format ! ( "`extern` static {:?} is not supported by Miri" , def_id)
467
453
}
468
454
}
469
455
0 commit comments