Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tighten CTFE safety net for accesses to globals #70947

Merged
merged 2 commits into from
Apr 15, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions src/librustc_mir/const_eval/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,15 +350,30 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter {
static_def_id: Option<DefId>,
is_write: bool,
) -> InterpResult<'tcx> {
if is_write && allocation.mutability == Mutability::Not {
Err(err_ub!(WriteToReadOnly(alloc_id)).into())
} else if is_write {
Err(ConstEvalErrKind::ModifiedGlobal.into())
} else if memory_extra.can_access_statics || static_def_id.is_none() {
// `static_def_id.is_none()` indicates this is not a static, but a const or so.
Ok(())
if is_write {
// Write access. These are never allowed, but we give a targeted error message.
if allocation.mutability == Mutability::Not {
Err(err_ub!(WriteToReadOnly(alloc_id)).into())
} else {
Err(ConstEvalErrKind::ModifiedGlobal.into())
}
} else {
Err(ConstEvalErrKind::ConstAccessesStatic.into())
// Read access. These are usually allowed, with some exceptions.
if memory_extra.can_access_statics {
// This is allowed to read from anything.
Centril marked this conversation as resolved.
Show resolved Hide resolved
Ok(())
} else if allocation.mutability == Mutability::Mut || static_def_id.is_some() {
RalfJung marked this conversation as resolved.
Show resolved Hide resolved
// This is a potentially dangerous read.
// We *must* error on any access to a mutable global here, as the content of
// this allocation may be different now and at run-time, so if we permit reading
// now we might return the wrong value.
// We conservatively also reject all statics here, but that could be relaxed
// in the future.
Err(ConstEvalErrKind::ConstAccessesStatic.into())
} else {
// Immutable global, this read is fine.
Ok(())
}
}
}
}
Expand Down