@@ -41,25 +41,31 @@ impl Into<MemoryKind<MiriMemoryKind>> for MiriMemoryKind {
41
41
/// Extra per-allocation data
42
42
#[ derive( Debug , Clone ) ]
43
43
pub struct AllocExtra {
44
- pub stacked_borrows : stacked_borrows:: AllocExtra ,
44
+ /// Stacked Borrows state is only added if validation is enabled.
45
+ pub stacked_borrows : Option < stacked_borrows:: AllocExtra > ,
45
46
}
46
47
47
48
/// Extra global memory data
48
49
#[ derive( Default , Clone , Debug ) ]
49
50
pub struct MemoryExtra {
50
51
pub stacked_borrows : stacked_borrows:: MemoryExtra ,
51
52
pub intptrcast : intptrcast:: MemoryExtra ,
53
+
52
54
/// The random number generator to use if Miri is running in non-deterministic mode and to
53
55
/// enable intptrcast
54
- pub ( crate ) rng : Option < RefCell < StdRng > >
56
+ pub ( crate ) rng : Option < RefCell < StdRng > > ,
57
+
58
+ /// Whether to enforce the validity invariant.
59
+ pub ( crate ) validate : bool ,
55
60
}
56
61
57
62
impl MemoryExtra {
58
- pub fn with_rng ( rng : Option < StdRng > ) -> Self {
63
+ pub fn new ( rng : Option < StdRng > , validate : bool ) -> Self {
59
64
MemoryExtra {
60
65
stacked_borrows : Default :: default ( ) ,
61
66
intptrcast : Default :: default ( ) ,
62
67
rng : rng. map ( RefCell :: new) ,
68
+ validate,
63
69
}
64
70
}
65
71
}
@@ -82,21 +88,17 @@ pub struct Evaluator<'tcx> {
82
88
83
89
/// TLS state.
84
90
pub ( crate ) tls : TlsData < ' tcx > ,
85
-
86
- /// Whether to enforce the validity invariant.
87
- pub ( crate ) validate : bool ,
88
91
}
89
92
90
93
impl < ' tcx > Evaluator < ' tcx > {
91
- pub ( crate ) fn new ( validate : bool ) -> Self {
94
+ pub ( crate ) fn new ( ) -> Self {
92
95
Evaluator {
93
96
env_vars : HashMap :: default ( ) ,
94
97
argc : None ,
95
98
argv : None ,
96
99
cmd_line : None ,
97
100
last_error : 0 ,
98
101
tls : TlsData :: default ( ) ,
99
- validate,
100
102
}
101
103
}
102
104
}
@@ -135,7 +137,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
135
137
136
138
#[ inline( always) ]
137
139
fn enforce_validity ( ecx : & InterpretCx < ' mir , ' tcx , Self > ) -> bool {
138
- ecx. machine . validate
140
+ ecx. memory ( ) . extra . validate
139
141
}
140
142
141
143
/// Returns `Ok()` when the function was handled; fail otherwise.
@@ -251,12 +253,17 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
251
253
) -> ( Cow < ' b , Allocation < Self :: PointerTag , Self :: AllocExtra > > , Self :: PointerTag ) {
252
254
let kind = kind. expect ( "we set our STATIC_KIND so this cannot be None" ) ;
253
255
let alloc = alloc. into_owned ( ) ;
254
- let ( stacks, base_tag) = Stacks :: new_allocation (
255
- id,
256
- Size :: from_bytes ( alloc. bytes . len ( ) as u64 ) ,
257
- Rc :: clone ( & memory. extra . stacked_borrows ) ,
258
- kind,
259
- ) ;
256
+ let ( stacks, base_tag) = if !memory. extra . validate {
257
+ ( None , Tag :: Untagged )
258
+ } else {
259
+ let ( stacks, base_tag) = Stacks :: new_allocation (
260
+ id,
261
+ Size :: from_bytes ( alloc. bytes . len ( ) as u64 ) ,
262
+ Rc :: clone ( & memory. extra . stacked_borrows ) ,
263
+ kind,
264
+ ) ;
265
+ ( Some ( stacks) , base_tag)
266
+ } ;
260
267
if kind != MiriMemoryKind :: Static . into ( ) {
261
268
assert ! ( alloc. relocations. is_empty( ) , "Only statics can come initialized with inner pointers" ) ;
262
269
// Now we can rely on the inner pointers being static, too.
@@ -268,7 +275,14 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
268
275
alloc. relocations . iter ( )
269
276
// The allocations in the relocations (pointers stored *inside* this allocation)
270
277
// all get the base pointer tag.
271
- . map ( |& ( offset, ( ( ) , alloc) ) | ( offset, ( memory_extra. static_base_ptr ( alloc) , alloc) ) )
278
+ . map ( |& ( offset, ( ( ) , alloc) ) | {
279
+ let tag = if !memory. extra . validate {
280
+ Tag :: Untagged
281
+ } else {
282
+ memory_extra. static_base_ptr ( alloc)
283
+ } ;
284
+ ( offset, ( tag, alloc) )
285
+ } )
272
286
. collect ( )
273
287
) ,
274
288
undef_mask : alloc. undef_mask ,
@@ -286,7 +300,11 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
286
300
id : AllocId ,
287
301
memory : & Memory < ' mir , ' tcx , Self > ,
288
302
) -> Self :: PointerTag {
289
- memory. extra . stacked_borrows . borrow_mut ( ) . static_base_ptr ( id)
303
+ if !memory. extra . validate {
304
+ Tag :: Untagged
305
+ } else {
306
+ memory. extra . stacked_borrows . borrow_mut ( ) . static_base_ptr ( id)
307
+ }
290
308
}
291
309
292
310
#[ inline( always) ]
@@ -295,12 +313,8 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
295
313
kind : mir:: RetagKind ,
296
314
place : PlaceTy < ' tcx , Tag > ,
297
315
) -> InterpResult < ' tcx > {
298
- if !ecx. tcx . sess . opts . debugging_opts . mir_emit_retag || !Self :: enforce_validity ( ecx) {
299
- // No tracking, or no retagging. The latter is possible because a dependency of ours
300
- // might be called with different flags than we are, so there are `Retag`
301
- // statements but we do not want to execute them.
302
- // Also, honor the whitelist in `enforce_validity` because otherwise we might retag
303
- // uninitialized data.
316
+ if !Self :: enforce_validity ( ecx) {
317
+ // No tracking.
304
318
Ok ( ( ) )
305
319
} else {
306
320
ecx. retag ( kind, place)
@@ -354,7 +368,11 @@ impl AllocationExtra<Tag> for AllocExtra {
354
368
ptr : Pointer < Tag > ,
355
369
size : Size ,
356
370
) -> InterpResult < ' tcx > {
357
- alloc. extra . stacked_borrows . memory_read ( ptr, size)
371
+ if let Some ( ref stacked_borrows) = alloc. extra . stacked_borrows {
372
+ stacked_borrows. memory_read ( ptr, size)
373
+ } else {
374
+ Ok ( ( ) )
375
+ }
358
376
}
359
377
360
378
#[ inline( always) ]
@@ -363,7 +381,11 @@ impl AllocationExtra<Tag> for AllocExtra {
363
381
ptr : Pointer < Tag > ,
364
382
size : Size ,
365
383
) -> InterpResult < ' tcx > {
366
- alloc. extra . stacked_borrows . memory_written ( ptr, size)
384
+ if let Some ( ref mut stacked_borrows) = alloc. extra . stacked_borrows {
385
+ stacked_borrows. memory_written ( ptr, size)
386
+ } else {
387
+ Ok ( ( ) )
388
+ }
367
389
}
368
390
369
391
#[ inline( always) ]
@@ -372,7 +394,11 @@ impl AllocationExtra<Tag> for AllocExtra {
372
394
ptr : Pointer < Tag > ,
373
395
size : Size ,
374
396
) -> InterpResult < ' tcx > {
375
- alloc. extra . stacked_borrows . memory_deallocated ( ptr, size)
397
+ if let Some ( ref mut stacked_borrows) = alloc. extra . stacked_borrows {
398
+ stacked_borrows. memory_deallocated ( ptr, size)
399
+ } else {
400
+ Ok ( ( ) )
401
+ }
376
402
}
377
403
}
378
404
0 commit comments