Skip to content

Commit 758adf6

Browse files
Rollup merge of #113011 - Nilstrieb:can_access_statics, r=oli-obk
Add enum for `can_access_statics` boolean `/*can_access_statics:*/ false` is one of the ways to do this, but not the one I like. r? oli-obk
2 parents 691580f + 70b6a74 commit 758adf6

File tree

5 files changed

+36
-15
lines changed

5 files changed

+36
-15
lines changed

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_middle::ty::{self, TyCtxt};
1414
use rustc_span::source_map::Span;
1515
use rustc_target::abi::{self, Abi};
1616

17-
use super::{CompileTimeEvalContext, CompileTimeInterpreter};
17+
use super::{CanAccessStatics, CompileTimeEvalContext, CompileTimeInterpreter};
1818
use crate::errors;
1919
use crate::interpret::eval_nullary_intrinsic;
2020
use crate::interpret::{
@@ -93,7 +93,7 @@ pub(super) fn mk_eval_cx<'mir, 'tcx>(
9393
tcx: TyCtxt<'tcx>,
9494
root_span: Span,
9595
param_env: ty::ParamEnv<'tcx>,
96-
can_access_statics: bool,
96+
can_access_statics: CanAccessStatics,
9797
) -> CompileTimeEvalContext<'mir, 'tcx> {
9898
debug!("mk_eval_cx: {:?}", param_env);
9999
InterpCx::new(
@@ -207,7 +207,7 @@ pub(crate) fn turn_into_const_value<'tcx>(
207207
tcx,
208208
tcx.def_span(key.value.instance.def_id()),
209209
key.param_env,
210-
/*can_access_statics:*/ is_static,
210+
CanAccessStatics::from(is_static),
211211
);
212212

213213
let mplace = ecx.raw_const_to_mplace(constant).expect(
@@ -309,7 +309,7 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
309309
// Statics (and promoteds inside statics) may access other statics, because unlike consts
310310
// they do not have to behave "as if" they were evaluated at runtime.
311311
CompileTimeInterpreter::new(
312-
/*can_access_statics:*/ is_static,
312+
CanAccessStatics::from(is_static),
313313
if tcx.sess.opts.unstable_opts.extra_const_ub_checks {
314314
CheckAlignment::Error
315315
} else {

compiler/rustc_const_eval/src/const_eval/machine.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub struct CompileTimeInterpreter<'mir, 'tcx> {
5757
/// * Interning makes everything outside of statics immutable.
5858
/// * Pointers to allocations inside of statics can never leak outside, to a non-static global.
5959
/// This boolean here controls the second part.
60-
pub(super) can_access_statics: bool,
60+
pub(super) can_access_statics: CanAccessStatics,
6161

6262
/// Whether to check alignment during evaluation.
6363
pub(super) check_alignment: CheckAlignment,
@@ -83,8 +83,23 @@ impl CheckAlignment {
8383
}
8484
}
8585

86+
#[derive(Copy, Clone, PartialEq)]
87+
pub(crate) enum CanAccessStatics {
88+
No,
89+
Yes,
90+
}
91+
92+
impl From<bool> for CanAccessStatics {
93+
fn from(value: bool) -> Self {
94+
if value { Self::Yes } else { Self::No }
95+
}
96+
}
97+
8698
impl<'mir, 'tcx> CompileTimeInterpreter<'mir, 'tcx> {
87-
pub(crate) fn new(can_access_statics: bool, check_alignment: CheckAlignment) -> Self {
99+
pub(crate) fn new(
100+
can_access_statics: CanAccessStatics,
101+
check_alignment: CheckAlignment,
102+
) -> Self {
88103
CompileTimeInterpreter {
89104
num_evaluated_steps: 0,
90105
stack: Vec::new(),
@@ -699,7 +714,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
699714
}
700715
} else {
701716
// Read access. These are usually allowed, with some exceptions.
702-
if machine.can_access_statics {
717+
if machine.can_access_statics == CanAccessStatics::Yes {
703718
// Machine configuration allows us read from anything (e.g., `static` initializer).
704719
Ok(())
705720
} else if static_def_id.is_some() {

compiler/rustc_const_eval/src/const_eval/mod.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub(crate) fn const_caller_location(
2626
(file, line, col): (Symbol, u32, u32),
2727
) -> ConstValue<'_> {
2828
trace!("const_caller_location: {}:{}:{}", file, line, col);
29-
let mut ecx = mk_eval_cx(tcx, DUMMY_SP, ty::ParamEnv::reveal_all(), false);
29+
let mut ecx = mk_eval_cx(tcx, DUMMY_SP, ty::ParamEnv::reveal_all(), CanAccessStatics::No);
3030

3131
let loc_place = ecx.alloc_caller_location(file, line, col);
3232
if intern_const_alloc_recursive(&mut ecx, InternKind::Constant, &loc_place).is_err() {
@@ -55,10 +55,12 @@ pub(crate) fn eval_to_valtree<'tcx>(
5555

5656
// FIXME Need to provide a span to `eval_to_valtree`
5757
let ecx = mk_eval_cx(
58-
tcx, DUMMY_SP, param_env,
58+
tcx,
59+
DUMMY_SP,
60+
param_env,
5961
// It is absolutely crucial for soundness that
6062
// we do not read from static items or other mutable memory.
61-
false,
63+
CanAccessStatics::No,
6264
);
6365
let place = ecx.raw_const_to_mplace(const_alloc).unwrap();
6466
debug!(?place);
@@ -91,7 +93,7 @@ pub(crate) fn try_destructure_mir_constant<'tcx>(
9193
val: mir::ConstantKind<'tcx>,
9294
) -> InterpResult<'tcx, mir::DestructuredConstant<'tcx>> {
9395
trace!("destructure_mir_constant: {:?}", val);
94-
let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false);
96+
let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, CanAccessStatics::No);
9597
let op = ecx.eval_mir_constant(&val, None, None)?;
9698

9799
// We go to `usize` as we cannot allocate anything bigger anyway.

compiler/rustc_const_eval/src/const_eval/valtrees.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::eval_queries::{mk_eval_cx, op_to_const};
22
use super::machine::CompileTimeEvalContext;
33
use super::{ValTreeCreationError, ValTreeCreationResult, VALTREE_MAX_NODES};
4+
use crate::const_eval::CanAccessStatics;
45
use crate::interpret::{
56
intern_const_alloc_recursive, ConstValue, ImmTy, Immediate, InternKind, MemPlaceMeta,
67
MemoryKind, PlaceTy, Scalar,
@@ -263,7 +264,11 @@ pub fn valtree_to_const_value<'tcx>(
263264
// FIXME Does this need an example?
264265

265266
let (param_env, ty) = param_env_ty.into_parts();
266-
let mut ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false);
267+
let mut ecx: crate::interpret::InterpCx<
268+
'_,
269+
'_,
270+
crate::const_eval::CompileTimeInterpreter<'_, '_>,
271+
> = mk_eval_cx(tcx, DUMMY_SP, param_env, CanAccessStatics::No);
267272

268273
match ty.kind() {
269274
ty::FnDef(..) => {

compiler/rustc_const_eval/src/util/check_validity_requirement.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_middle::ty::layout::{LayoutCx, LayoutError, LayoutOf, TyAndLayout, Val
22
use rustc_middle::ty::{ParamEnv, ParamEnvAnd, Ty, TyCtxt};
33
use rustc_target::abi::{Abi, FieldsShape, Scalar, Variants};
44

5-
use crate::const_eval::{CheckAlignment, CompileTimeInterpreter};
5+
use crate::const_eval::{CanAccessStatics, CheckAlignment, CompileTimeInterpreter};
66
use crate::interpret::{InterpCx, MemoryKind, OpTy};
77

88
/// Determines if this type permits "raw" initialization by just transmuting some memory into an
@@ -44,8 +44,7 @@ fn might_permit_raw_init_strict<'tcx>(
4444
tcx: TyCtxt<'tcx>,
4545
kind: ValidityRequirement,
4646
) -> Result<bool, LayoutError<'tcx>> {
47-
let machine =
48-
CompileTimeInterpreter::new(/*can_access_statics:*/ false, CheckAlignment::Error);
47+
let machine = CompileTimeInterpreter::new(CanAccessStatics::No, CheckAlignment::Error);
4948

5049
let mut cx = InterpCx::new(tcx, rustc_span::DUMMY_SP, ParamEnv::reveal_all(), machine);
5150

0 commit comments

Comments
 (0)