@@ -51,13 +51,10 @@ pub struct CompileTimeInterpreter<'mir, 'tcx> {
51
51
/// The virtual call stack.
52
52
pub ( super ) stack : Vec < Frame < ' mir , ' tcx > > ,
53
53
54
- /// We need to make sure consts never point to anything mutable, even recursively. That is
55
- /// relied on for pattern matching on consts with references.
56
- /// To achieve this, two pieces have to work together:
57
- /// * Interning makes everything outside of statics immutable.
58
- /// * Pointers to allocations inside of statics can never leak outside, to a non-static global.
59
- /// This boolean here controls the second part.
60
- pub ( super ) can_access_statics : CanAccessStatics ,
54
+ /// Pattern matching on consts with references would be unsound if those references
55
+ /// could point to anything mutable. Therefore, when evaluating consts and when constructing valtrees,
56
+ /// we ensure that only immutable global memory can be accessed.
57
+ pub ( super ) can_access_mut_global : CanAccessMutGlobal ,
61
58
62
59
/// Whether to check alignment during evaluation.
63
60
pub ( super ) check_alignment : CheckAlignment ,
@@ -73,26 +70,26 @@ pub enum CheckAlignment {
73
70
}
74
71
75
72
#[ derive( Copy , Clone , PartialEq ) ]
76
- pub ( crate ) enum CanAccessStatics {
73
+ pub ( crate ) enum CanAccessMutGlobal {
77
74
No ,
78
75
Yes ,
79
76
}
80
77
81
- impl From < bool > for CanAccessStatics {
78
+ impl From < bool > for CanAccessMutGlobal {
82
79
fn from ( value : bool ) -> Self {
83
80
if value { Self :: Yes } else { Self :: No }
84
81
}
85
82
}
86
83
87
84
impl < ' mir , ' tcx > CompileTimeInterpreter < ' mir , ' tcx > {
88
85
pub ( crate ) fn new (
89
- can_access_statics : CanAccessStatics ,
86
+ can_access_mut_global : CanAccessMutGlobal ,
90
87
check_alignment : CheckAlignment ,
91
88
) -> Self {
92
89
CompileTimeInterpreter {
93
90
num_evaluated_steps : 0 ,
94
91
stack : Vec :: new ( ) ,
95
- can_access_statics ,
92
+ can_access_mut_global ,
96
93
check_alignment,
97
94
}
98
95
}
@@ -675,7 +672,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
675
672
machine : & Self ,
676
673
alloc_id : AllocId ,
677
674
alloc : ConstAllocation < ' tcx > ,
678
- static_def_id : Option < DefId > ,
675
+ _static_def_id : Option < DefId > ,
679
676
is_write : bool ,
680
677
) -> InterpResult < ' tcx > {
681
678
let alloc = alloc. inner ( ) ;
@@ -687,22 +684,15 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
687
684
}
688
685
} else {
689
686
// Read access. These are usually allowed, with some exceptions.
690
- if machine. can_access_statics == CanAccessStatics :: Yes {
687
+ if machine. can_access_mut_global == CanAccessMutGlobal :: Yes {
691
688
// Machine configuration allows us read from anything (e.g., `static` initializer).
692
689
Ok ( ( ) )
693
- } else if static_def_id. is_some ( ) {
694
- // Machine configuration does not allow us to read statics
695
- // (e.g., `const` initializer).
696
- // See const_eval::machine::MemoryExtra::can_access_statics for why
697
- // this check is so important: if we could read statics, we could read pointers
698
- // to mutable allocations *inside* statics. These allocations are not themselves
699
- // statics, so pointers to them can get around the check in `validity.rs`.
700
- Err ( ConstEvalErrKind :: ConstAccessesStatic . into ( ) )
690
+ } else if alloc. mutability == Mutability :: Mut {
691
+ // Machine configuration does not allow us to read statics (e.g., `const`
692
+ // initializer).
693
+ Err ( ConstEvalErrKind :: ConstAccessesMutGlobal . into ( ) )
701
694
} else {
702
695
// Immutable global, this read is fine.
703
- // But make sure we never accept a read from something mutable, that would be
704
- // unsound. The reason is that as the content of this allocation may be different
705
- // now and at run-time, so if we permit reading now we might return the wrong value.
706
696
assert_eq ! ( alloc. mutability, Mutability :: Not ) ;
707
697
Ok ( ( ) )
708
698
}
0 commit comments