Skip to content

Commit 330e81c

Browse files
authored
Merge pull request #1567 from N1ark/consts-rework-1
Handle nested statics + remove unknown provenance
2 parents 84f44c3 + 8cdd615 commit 330e81c

22 files changed

Lines changed: 77 additions & 124 deletions

File tree

‎charon-ml/src/CharonVersion.ml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
(* This is an automatically generated file, generated from `charon/Cargo.toml`. *)
22
(* To re-generate this file, rune `make` in the root directory *)
3-
let supported_charon_version = "0.1.299"
3+
let supported_charon_version = "0.1.300"

‎charon-ml/src/PrintFmt.ml‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,6 @@ and pp_provenance (env : fmt_env) (fmt : Format.formatter)
477477
| ProvGlobal gref -> Format.fprintf fmt "&%a" (pp_global_decl_ref env) gref
478478
| ProvFunction (fn_ptr, _) -> Format.fprintf fmt "&%a" (pp_fn_ptr env) fn_ptr
479479
| ProvTypeId ty -> Format.fprintf fmt "&TypeId(%a)" (pp_ty env) ty
480-
| ProvUnknown -> pp_string fmt "&?"
481480

482481
and pp_byte (env : fmt_env) (fmt : Format.formatter) (cv : byte) : unit =
483482
match cv with
@@ -651,7 +650,6 @@ and pp_global_place (env : fmt_env) (fmt : Format.formatter) (p : global_place)
651650
| GlobalPlaceGlobal (ProvFunction (fn_ptr, _)) -> pp_fn_ptr env fmt fn_ptr
652651
| GlobalPlaceGlobal (ProvTypeId ty) ->
653652
Format.fprintf fmt "TypeId(%a)" (pp_ty env) ty
654-
| GlobalPlaceGlobal ProvUnknown -> pp_string fmt "?"
655653
| GlobalPlaceValue c -> pp_constant_expr env fmt c
656654
| GlobalPlaceProjection (subplace, proj) -> (
657655
let sub = pp_to_string (fun fmt -> pp_global_place env fmt subplace) in

‎charon-ml/src/generated/Generated_OfJson.ml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,6 @@ and global_provenance_of_json (ctx : of_json_ctx) (js : json) :
10111011
| `Assoc [ ("TypeId", _0) ] ->
10121012
let* _0 = ty_of_json ctx _0 in
10131013
Ok (ProvTypeId _0)
1014-
| `String "Unknown" -> Ok ProvUnknown
10151014
| _ -> Error "")
10161015

10171016
and hash_consed_of_json :

‎charon-ml/src/generated/Generated_OfPostcard.ml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,6 @@ and global_provenance_of_postcard (ctx : of_postcard_ctx) (st : postcard_state)
901901
| 2 ->
902902
let* _0 = ty_of_postcard ctx st in
903903
Ok (ProvTypeId _0)
904-
| 3 -> Ok ProvUnknown
905904
| _ -> Error ("unknown enum variant tag: " ^ string_of_int __tag))
906905

907906
and hash_consed_of_postcard :

‎charon-ml/src/generated/Generated_Types.ml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,6 @@ and global_provenance =
527527
pointer size, so [[Byte::Provenance]]'s byte index can go up to 16! In
528528
other words, the bytes of a full [TypeId] are
529529
[Provenance(TypeId(ty), 0, 0..16)]. *)
530-
| ProvUnknown
531530

532531
(** Hash-consed data structure: a reference-counted wrapper that guarantees that
533532
two equal value will be stored at the same address. This makes it possible

‎charon/Cargo.lock‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎charon/Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ tracing = { version = "0.1", features = ["max_level_trace"] }
2727

2828
[package]
2929
name = "charon"
30-
version = "0.1.299"
30+
version = "0.1.300"
3131
authors.workspace = true
3232
edition.workspace = true
3333
license.workspace = true

‎charon/rustc_const_uneval/src/eval.rs‎

Lines changed: 53 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,44 @@ enum Evaluated<'tcx> {
1414
Value(mir::ConstValue, Ty<'tcx>),
1515
}
1616

17+
impl GlobalRef {
18+
/// The mutability of this global.
19+
pub fn mutability(self, tcx: TyCtxt<'_>) -> mir::Mutability {
20+
match self {
21+
GlobalRef::Static(def_id) => tcx.static_mutability(def_id).unwrap(),
22+
GlobalRef::Alloc(_) => self.memory(tcx).unwrap().inner().mutability,
23+
}
24+
}
25+
26+
/// The memory of this global. Returns `None` for a static whose evaluation fails, or that has
27+
/// no initializer (in an `extern` block).
28+
pub fn memory<'tcx>(self, tcx: TyCtxt<'tcx>) -> Option<interpret::ConstAllocation<'tcx>> {
29+
let def_id = match self {
30+
GlobalRef::Static(def_id) if tcx.is_foreign_item(def_id) => return None,
31+
GlobalRef::Static(def_id) => def_id,
32+
GlobalRef::Alloc(alloc_id) => match tcx.global_alloc(alloc_id) {
33+
interpret::GlobalAlloc::Memory(alloc) => return Some(alloc),
34+
// A nested static.
35+
interpret::GlobalAlloc::Static(def_id) => def_id,
36+
alloc => unreachable!("unexpected GlobalRef::Alloc: {alloc:?}"),
37+
},
38+
};
39+
tcx.eval_static_initializer(def_id).ok()
40+
}
41+
}
42+
43+
/// The type at which we view an allocation without a corresponding item: `MaybeUninit<[u8; N]>`.
44+
pub fn alloc_as_untyped_ty<'tcx>(tcx: TyCtxt<'tcx>, alloc_id: interpret::AllocId) -> Ty<'tcx> {
45+
let size = GlobalRef::Alloc(alloc_id)
46+
.memory(tcx)
47+
.unwrap()
48+
.inner()
49+
.size();
50+
let bytes = Ty::new_array(tcx, tcx.types.u8, size.bytes());
51+
let maybe_uninit = tcx.require_lang_item(LangItem::MaybeUninit, rustc_span::DUMMY_SP);
52+
Ty::new_adt(tcx, tcx.adt_def(maybe_uninit), tcx.mk_args(&[bytes.into()]))
53+
}
54+
1755
/// Call `f` on the MIR of the given promoted constant.
1856
pub fn promoted_body<'tcx, R>(
1957
tcx: TyCtxt<'tcx>,
@@ -131,35 +169,22 @@ impl<'tcx> ConstReader<'tcx> {
131169
Evaluated::Value(val, instantiate(ty, args).skip_normalization())
132170
}
133171
ConstSource::Global(global) => {
134-
let alloc_id = match global {
135-
// Statics in `extern` blocks have no initializer.
136-
GlobalRef::Static(def_id) if tcx.is_foreign_item(def_id) => {
137-
return Err(ReadError::NotEvaluable);
138-
}
139-
GlobalRef::Static(def_id) => {
140-
let alloc = tcx
141-
.eval_static_initializer(def_id)
142-
.map_err(|_| ReadError::NotEvaluable)?;
143-
// A static whose type has interior mutability gets a mutable allocation,
144-
// which const-eval refuses to read. We don't care though, so we reintern
145-
// it as immutable.
146-
let alloc = if alloc.inner().mutability.is_mut() {
147-
let mut alloc = alloc.inner().clone();
148-
alloc.mutability = mir::Mutability::Not;
149-
tcx.mk_const_alloc(alloc)
150-
} else {
151-
alloc
152-
};
153-
// `eval_static_initializer` returns an interned allocation without an
154-
// `AllocId`; give it one so we can read it like any other allocation.
155-
// This creates a fresh `AllocId` each time we read the static.
156-
// FIXME: this breaks sharing.
157-
tcx.reserve_and_set_memory_alloc(alloc)
158-
}
159-
GlobalRef::Alloc(alloc_id) => alloc_id,
172+
let alloc = global.memory(tcx).ok_or(ReadError::NotEvaluable)?;
173+
// A mutable allocation (e.g. of a `static mut`, or of a static whose type has
174+
// interior mutability) can't be read by const-eval. We don't care though, so we
175+
// reintern it as immutable.
176+
let alloc = if alloc.inner().mutability.is_mut() {
177+
let mut alloc = alloc.inner().clone();
178+
alloc.mutability = mir::Mutability::Not;
179+
tcx.mk_const_alloc(alloc)
180+
} else {
181+
alloc
160182
};
183+
// Give the allocation a fresh `AllocId` so we can read it like any other
184+
// allocation. This creates a fresh `AllocId` each time we read the global. This
185+
// doesn't break sharing.
161186
let val = mir::ConstValue::Indirect {
162-
alloc_id,
187+
alloc_id: tcx.reserve_and_set_memory_alloc(alloc),
163188
offset: rustc_abi::Size::ZERO,
164189
};
165190
Evaluated::Value(val, self.global_ty(global))
@@ -207,15 +232,7 @@ impl<'tcx> ConstReader<'tcx> {
207232
.instantiate_identity()
208233
.skip_normalization(),
209234
),
210-
GlobalRef::Alloc(alloc_id) => {
211-
let (size, _) = tcx
212-
.global_alloc(alloc_id)
213-
.size_and_align(tcx, self.typing_env);
214-
let bytes = Ty::new_array(tcx, tcx.types.u8, size.bytes());
215-
let maybe_uninit =
216-
tcx.require_lang_item(LangItem::MaybeUninit, rustc_span::DUMMY_SP);
217-
Ty::new_adt(tcx, tcx.adt_def(maybe_uninit), tcx.mk_args(&[bytes.into()]))
218-
}
235+
GlobalRef::Alloc(alloc_id) => alloc_as_untyped_ty(tcx, alloc_id),
219236
}
220237
}
221238

‎charon/rustc_const_uneval/src/lib.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ mod memory;
1616
mod pointer;
1717
mod valtree;
1818

19-
pub use eval::promoted_body;
19+
pub use eval::{alloc_as_untyped_ty, promoted_body};
2020

2121
use rustc_abi::{ExternAbi, FieldIdx, Size, VariantIdx};
2222
use rustc_hir::def_id::DefId;

‎charon/rustc_const_uneval/src/memory.rs‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,6 @@ impl<'tcx> ConstReader<'tcx> {
2828
}
2929
}
3030

31-
/// Whether we refer to `global` by name in pointers to it, rather than inlining its contents.
32-
pub fn is_named_global(&self, global: GlobalRef) -> bool {
33-
match global {
34-
GlobalRef::Static(_) => true,
35-
GlobalRef::Alloc(alloc_id) => {
36-
self.config.anon_allocs_as_globals
37-
// TODO: nested statics are synthetic items that make the rest of the machinery
38-
// ICE, so we don't name them yet.
39-
&& matches!(self.tcx.global_alloc(alloc_id), GlobalAlloc::Memory(_))
40-
}
41-
}
42-
}
43-
4431
/// Read the raw bytes of an evaluated operand, keeping track of uninitialized bytes and pointer
4532
/// provenance.
4633
pub(crate) fn read_raw_bytes(

0 commit comments

Comments
 (0)