2019-09-06 03:57:44 +01:00
|
|
|
//! An interpreter for MIR used in CTFE and by miri.
|
2017-07-21 17:25:30 +02:00
|
|
|
|
2024-05-13 08:06:37 +02:00
|
|
|
#[macro_use]
|
|
|
|
mod error;
|
2023-05-17 10:30:14 +00:00
|
|
|
|
2018-10-23 18:05:32 +02:00
|
|
|
mod allocation;
|
2018-10-25 18:23:09 +02:00
|
|
|
mod pointer;
|
2019-11-30 08:42:56 +13:00
|
|
|
mod queries;
|
2016-12-07 20:30:37 -08:00
|
|
|
mod value;
|
|
|
|
|
2020-08-20 02:37:00 -07:00
|
|
|
use std::io::{Read, Write};
|
2024-01-29 23:59:09 +01:00
|
|
|
use std::num::NonZero;
|
2024-05-22 15:20:21 +10:00
|
|
|
use std::{fmt, io};
|
2024-05-07 08:05:02 +10:00
|
|
|
|
2024-11-02 19:33:00 -07:00
|
|
|
use rustc_abi::{AddressSpace, Endian, HasDataLayout};
|
2020-04-27 23:26:11 +05:30
|
|
|
use rustc_ast::LitKind;
|
2020-03-21 13:49:02 +01:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2024-07-06 20:41:52 -04:00
|
|
|
use rustc_data_structures::sync::Lock;
|
2022-11-10 05:39:06 +00:00
|
|
|
use rustc_errors::ErrorGuaranteed;
|
2024-02-26 18:03:06 +00:00
|
|
|
use rustc_hir::def_id::{DefId, LocalDefId};
|
2024-04-29 11:14:55 +10:00
|
|
|
use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable};
|
2020-09-02 10:40:56 +03:00
|
|
|
use rustc_middle::ty::print::with_no_trimmed_paths;
|
2020-06-11 15:49:57 +01:00
|
|
|
use rustc_serialize::{Decodable, Encodable};
|
2022-05-27 20:03:57 -07:00
|
|
|
use tracing::{debug, trace};
|
2024-05-13 08:06:37 +02:00
|
|
|
// Also make the error macros available from this module.
|
|
|
|
pub use {
|
|
|
|
err_exhaust, err_inval, err_machine_stop, err_ub, err_ub_custom, err_ub_format, err_unsup,
|
|
|
|
err_unsup_format, throw_exhaust, throw_inval, throw_machine_stop, throw_ub, throw_ub_custom,
|
|
|
|
throw_ub_format, throw_unsup, throw_unsup_format,
|
|
|
|
};
|
2016-06-10 13:01:51 +02:00
|
|
|
|
2021-03-31 00:06:01 -04:00
|
|
|
pub use self::allocation::{
|
2023-02-14 15:26:47 -05:00
|
|
|
AllocBytes, AllocError, AllocRange, AllocResult, Allocation, ConstAllocation, InitChunk,
|
|
|
|
InitChunkIter, alloc_range,
|
2021-03-31 00:06:01 -04:00
|
|
|
};
|
2018-06-25 18:46:02 +02:00
|
|
|
pub use self::error::{
|
2024-09-29 11:53:23 +02:00
|
|
|
BadBytesAccess, CheckAlignMsg, CheckInAllocMsg, ErrorHandled, EvalStaticInitializerRawResult,
|
|
|
|
EvalToAllocationRawResult, EvalToConstValueResult, EvalToValTreeResult, ExpectedKind,
|
2024-10-19 08:49:13 +02:00
|
|
|
InterpErrorInfo, InterpErrorKind, InterpResult, InvalidMetaKind, InvalidProgramInfo,
|
2024-09-29 11:53:23 +02:00
|
|
|
MachineStopType, Misalignment, PointerKind, ReportedErrorInfo, ResourceExhaustionInfo,
|
|
|
|
ScalarSizeMismatch, UndefinedBehaviorInfo, UnsupportedOpInfo, ValidationErrorInfo,
|
|
|
|
ValidationErrorKind, interp_ok,
|
2024-07-29 08:13:50 +10:00
|
|
|
};
|
2023-11-25 18:41:53 +01:00
|
|
|
pub use self::pointer::{CtfeProvenance, Pointer, PointerArithmetic, Provenance};
|
2023-09-16 09:36:22 +02:00
|
|
|
pub use self::value::Scalar;
|
2020-03-21 13:49:02 +01:00
|
|
|
use crate::mir;
|
2020-06-11 15:49:57 +01:00
|
|
|
use crate::ty::codec::{TyDecoder, TyEncoder};
|
2024-08-06 19:02:01 +02:00
|
|
|
use crate::ty::{self, Instance, Ty, TyCtxt};
|
2018-10-25 18:23:09 +02:00
|
|
|
|
2019-11-11 14:32:36 +00:00
|
|
|
/// Uniquely identifies one of the following:
|
|
|
|
/// - A constant
|
|
|
|
/// - A static
|
2020-06-11 15:49:57 +01:00
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, TyEncodable, TyDecodable)]
|
2023-09-14 09:46:18 +10:00
|
|
|
#[derive(HashStable, TypeFoldable, TypeVisitable)]
|
2017-12-12 17:14:49 +01:00
|
|
|
pub struct GlobalId<'tcx> {
|
|
|
|
/// For a constant or static, the `Instance` of the item itself.
|
|
|
|
/// For a promoted global, the `Instance` of the function they belong to.
|
|
|
|
pub instance: ty::Instance<'tcx>,
|
|
|
|
|
2019-05-29 00:26:56 +03:00
|
|
|
/// The index for promoted globals within their function's `mir::Body`.
|
2017-12-12 17:14:49 +01:00
|
|
|
pub promoted: Option<mir::Promoted>,
|
|
|
|
}
|
|
|
|
|
2021-12-15 19:32:30 -05:00
|
|
|
impl<'tcx> GlobalId<'tcx> {
|
2020-08-19 12:05:32 +02:00
|
|
|
pub fn display(self, tcx: TyCtxt<'tcx>) -> String {
|
2022-02-16 13:04:48 -05:00
|
|
|
let instance_name = with_no_trimmed_paths!(tcx.def_path_str(self.instance.def.def_id()));
|
2020-08-19 12:05:32 +02:00
|
|
|
if let Some(promoted) = self.promoted {
|
2023-07-25 22:00:13 +02:00
|
|
|
format!("{instance_name}::{promoted:?}")
|
2020-08-19 12:05:32 +02:00
|
|
|
} else {
|
|
|
|
instance_name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-11 20:57:38 +13:00
|
|
|
/// Input argument for `tcx.lit_to_const`.
|
2020-01-11 15:22:36 +13:00
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, HashStable)]
|
|
|
|
pub struct LitToConstInput<'tcx> {
|
2020-01-11 20:57:38 +13:00
|
|
|
/// The absolute value of the resultant constant.
|
2020-01-11 15:22:36 +13:00
|
|
|
pub lit: &'tcx LitKind,
|
2020-01-11 20:57:38 +13:00
|
|
|
/// The type of the constant.
|
2020-01-11 15:22:36 +13:00
|
|
|
pub ty: Ty<'tcx>,
|
2020-01-11 20:57:38 +13:00
|
|
|
/// If the constant is negative.
|
2020-01-11 15:22:36 +13:00
|
|
|
pub neg: bool,
|
|
|
|
}
|
|
|
|
|
2020-01-11 20:57:38 +13:00
|
|
|
/// Error type for `tcx.lit_to_const`.
|
2020-01-11 15:22:36 +13:00
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq, HashStable)]
|
|
|
|
pub enum LitToConstError {
|
2020-02-20 23:23:22 +01:00
|
|
|
/// The literal's inferred type did not match the expected `ty` in the input.
|
2023-11-30 15:01:11 +11:00
|
|
|
/// This is used for graceful error handling (`span_delayed_bug`) in
|
2020-03-23 22:24:31 +01:00
|
|
|
/// type checking (`Const::from_anon_const`).
|
2020-02-20 23:23:22 +01:00
|
|
|
TypeError,
|
2022-11-10 05:39:06 +00:00
|
|
|
Reported(ErrorGuaranteed),
|
2020-01-11 15:22:36 +13:00
|
|
|
}
|
|
|
|
|
2019-12-23 17:41:06 +01:00
|
|
|
#[derive(Copy, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
2024-01-29 23:59:09 +01:00
|
|
|
pub struct AllocId(pub NonZero<u64>);
|
2017-12-12 17:14:49 +01:00
|
|
|
|
2020-04-26 18:59:20 +02:00
|
|
|
// We want the `Debug` output to be readable as it is used by `derive(Debug)` for
|
|
|
|
// all the Miri types.
|
|
|
|
impl fmt::Debug for AllocId {
|
2020-04-26 18:14:16 +02:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2020-04-26 19:33:16 +02:00
|
|
|
if f.alternate() { write!(f, "a{}", self.0) } else { write!(f, "alloc{}", self.0) }
|
2020-03-08 18:52:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-02 10:53:34 -04:00
|
|
|
// No "Display" since AllocIds are not usually user-visible.
|
2019-12-23 17:41:06 +01:00
|
|
|
|
2020-06-11 15:49:57 +01:00
|
|
|
#[derive(TyDecodable, TyEncodable)]
|
2018-12-03 16:30:43 +01:00
|
|
|
enum AllocDiscriminant {
|
2018-04-10 09:58:46 +02:00
|
|
|
Alloc,
|
|
|
|
Fn,
|
2022-07-19 19:57:44 -04:00
|
|
|
VTable,
|
2018-04-10 16:25:10 +02:00
|
|
|
Static,
|
2018-04-10 09:58:46 +02:00
|
|
|
}
|
2018-03-16 09:59:42 +01:00
|
|
|
|
2022-05-27 20:03:57 -07:00
|
|
|
pub fn specialized_encode_alloc_id<'tcx, E: TyEncoder<I = TyCtxt<'tcx>>>(
|
2018-03-16 09:59:42 +01:00
|
|
|
encoder: &mut E,
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2018-03-16 09:59:42 +01:00
|
|
|
alloc_id: AllocId,
|
Use delayed error handling for `Encodable` and `Encoder` infallible.
There are two impls of the `Encoder` trait: `opaque::Encoder` and
`opaque::FileEncoder`. The former encodes into memory and is infallible, the
latter writes to file and is fallible.
Currently, standard `Result`/`?`/`unwrap` error handling is used, but this is a
bit verbose and has non-trivial cost, which is annoying given how rare failures
are (especially in the infallible `opaque::Encoder` case).
This commit changes how `Encoder` fallibility is handled. All the `emit_*`
methods are now infallible. `opaque::Encoder` requires no great changes for
this. `opaque::FileEncoder` now implements a delayed error handling strategy.
If a failure occurs, it records this via the `res` field, and all subsequent
encoding operations are skipped if `res` indicates an error has occurred. Once
encoding is complete, the new `finish` method is called, which returns a
`Result`. In other words, there is now a single `Result`-producing method
instead of many of them.
This has very little effect on how any file errors are reported if
`opaque::FileEncoder` has any failures.
Much of this commit is boring mechanical changes, removing `Result` return
values and `?` or `unwrap` from expressions. The more interesting parts are as
follows.
- serialize.rs: The `Encoder` trait gains an `Ok` associated type. The
`into_inner` method is changed into `finish`, which returns
`Result<Vec<u8>, !>`.
- opaque.rs: The `FileEncoder` adopts the delayed error handling
strategy. Its `Ok` type is a `usize`, returning the number of bytes
written, replacing previous uses of `FileEncoder::position`.
- Various methods that take an encoder now consume it, rather than being
passed a mutable reference, e.g. `serialize_query_result_cache`.
2022-06-07 13:30:45 +10:00
|
|
|
) {
|
2020-05-08 10:58:53 +02:00
|
|
|
match tcx.global_alloc(alloc_id) {
|
2019-05-30 13:05:05 +02:00
|
|
|
GlobalAlloc::Memory(alloc) => {
|
2018-05-02 06:03:06 +02:00
|
|
|
trace!("encoding {:?} with {:#?}", alloc_id, alloc);
|
Use delayed error handling for `Encodable` and `Encoder` infallible.
There are two impls of the `Encoder` trait: `opaque::Encoder` and
`opaque::FileEncoder`. The former encodes into memory and is infallible, the
latter writes to file and is fallible.
Currently, standard `Result`/`?`/`unwrap` error handling is used, but this is a
bit verbose and has non-trivial cost, which is annoying given how rare failures
are (especially in the infallible `opaque::Encoder` case).
This commit changes how `Encoder` fallibility is handled. All the `emit_*`
methods are now infallible. `opaque::Encoder` requires no great changes for
this. `opaque::FileEncoder` now implements a delayed error handling strategy.
If a failure occurs, it records this via the `res` field, and all subsequent
encoding operations are skipped if `res` indicates an error has occurred. Once
encoding is complete, the new `finish` method is called, which returns a
`Result`. In other words, there is now a single `Result`-producing method
instead of many of them.
This has very little effect on how any file errors are reported if
`opaque::FileEncoder` has any failures.
Much of this commit is boring mechanical changes, removing `Result` return
values and `?` or `unwrap` from expressions. The more interesting parts are as
follows.
- serialize.rs: The `Encoder` trait gains an `Ok` associated type. The
`into_inner` method is changed into `finish`, which returns
`Result<Vec<u8>, !>`.
- opaque.rs: The `FileEncoder` adopts the delayed error handling
strategy. Its `Ok` type is a `usize`, returning the number of bytes
written, replacing previous uses of `FileEncoder::position`.
- Various methods that take an encoder now consume it, rather than being
passed a mutable reference, e.g. `serialize_query_result_cache`.
2022-06-07 13:30:45 +10:00
|
|
|
AllocDiscriminant::Alloc.encode(encoder);
|
|
|
|
alloc.encode(encoder);
|
2018-05-02 06:03:06 +02:00
|
|
|
}
|
2024-08-06 19:02:01 +02:00
|
|
|
GlobalAlloc::Function { instance } => {
|
2024-07-02 21:05:22 +02:00
|
|
|
trace!("encoding {:?} with {:#?}", alloc_id, instance);
|
Use delayed error handling for `Encodable` and `Encoder` infallible.
There are two impls of the `Encoder` trait: `opaque::Encoder` and
`opaque::FileEncoder`. The former encodes into memory and is infallible, the
latter writes to file and is fallible.
Currently, standard `Result`/`?`/`unwrap` error handling is used, but this is a
bit verbose and has non-trivial cost, which is annoying given how rare failures
are (especially in the infallible `opaque::Encoder` case).
This commit changes how `Encoder` fallibility is handled. All the `emit_*`
methods are now infallible. `opaque::Encoder` requires no great changes for
this. `opaque::FileEncoder` now implements a delayed error handling strategy.
If a failure occurs, it records this via the `res` field, and all subsequent
encoding operations are skipped if `res` indicates an error has occurred. Once
encoding is complete, the new `finish` method is called, which returns a
`Result`. In other words, there is now a single `Result`-producing method
instead of many of them.
This has very little effect on how any file errors are reported if
`opaque::FileEncoder` has any failures.
Much of this commit is boring mechanical changes, removing `Result` return
values and `?` or `unwrap` from expressions. The more interesting parts are as
follows.
- serialize.rs: The `Encoder` trait gains an `Ok` associated type. The
`into_inner` method is changed into `finish`, which returns
`Result<Vec<u8>, !>`.
- opaque.rs: The `FileEncoder` adopts the delayed error handling
strategy. Its `Ok` type is a `usize`, returning the number of bytes
written, replacing previous uses of `FileEncoder::position`.
- Various methods that take an encoder now consume it, rather than being
passed a mutable reference, e.g. `serialize_query_result_cache`.
2022-06-07 13:30:45 +10:00
|
|
|
AllocDiscriminant::Fn.encode(encoder);
|
2024-07-02 21:05:22 +02:00
|
|
|
instance.encode(encoder);
|
2018-05-02 06:03:06 +02:00
|
|
|
}
|
2022-07-19 19:57:44 -04:00
|
|
|
GlobalAlloc::VTable(ty, poly_trait_ref) => {
|
2022-07-17 11:36:37 -04:00
|
|
|
trace!("encoding {:?} with {ty:#?}, {poly_trait_ref:#?}", alloc_id);
|
2022-07-19 19:57:44 -04:00
|
|
|
AllocDiscriminant::VTable.encode(encoder);
|
2022-07-17 11:36:37 -04:00
|
|
|
ty.encode(encoder);
|
|
|
|
poly_trait_ref.encode(encoder);
|
|
|
|
}
|
2019-05-30 13:05:05 +02:00
|
|
|
GlobalAlloc::Static(did) => {
|
2020-05-02 21:44:25 +02:00
|
|
|
assert!(!tcx.is_thread_local_static(did));
|
2019-09-06 03:57:44 +01:00
|
|
|
// References to statics doesn't need to know about their allocations,
|
|
|
|
// just about its `DefId`.
|
Use delayed error handling for `Encodable` and `Encoder` infallible.
There are two impls of the `Encoder` trait: `opaque::Encoder` and
`opaque::FileEncoder`. The former encodes into memory and is infallible, the
latter writes to file and is fallible.
Currently, standard `Result`/`?`/`unwrap` error handling is used, but this is a
bit verbose and has non-trivial cost, which is annoying given how rare failures
are (especially in the infallible `opaque::Encoder` case).
This commit changes how `Encoder` fallibility is handled. All the `emit_*`
methods are now infallible. `opaque::Encoder` requires no great changes for
this. `opaque::FileEncoder` now implements a delayed error handling strategy.
If a failure occurs, it records this via the `res` field, and all subsequent
encoding operations are skipped if `res` indicates an error has occurred. Once
encoding is complete, the new `finish` method is called, which returns a
`Result`. In other words, there is now a single `Result`-producing method
instead of many of them.
This has very little effect on how any file errors are reported if
`opaque::FileEncoder` has any failures.
Much of this commit is boring mechanical changes, removing `Result` return
values and `?` or `unwrap` from expressions. The more interesting parts are as
follows.
- serialize.rs: The `Encoder` trait gains an `Ok` associated type. The
`into_inner` method is changed into `finish`, which returns
`Result<Vec<u8>, !>`.
- opaque.rs: The `FileEncoder` adopts the delayed error handling
strategy. Its `Ok` type is a `usize`, returning the number of bytes
written, replacing previous uses of `FileEncoder::position`.
- Various methods that take an encoder now consume it, rather than being
passed a mutable reference, e.g. `serialize_query_result_cache`.
2022-06-07 13:30:45 +10:00
|
|
|
AllocDiscriminant::Static.encode(encoder);
|
2023-03-25 09:46:19 +01:00
|
|
|
// Cannot use `did.encode(encoder)` because of a bug around
|
|
|
|
// specializations and method calls.
|
|
|
|
Encodable::<E>::encode(&did, encoder);
|
2018-05-02 06:03:06 +02:00
|
|
|
}
|
2018-03-16 09:59:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-25 17:19:31 +02:00
|
|
|
#[derive(Clone)]
|
|
|
|
enum State {
|
|
|
|
Empty,
|
|
|
|
Done(AllocId),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct AllocDecodingState {
|
2019-09-06 03:57:44 +01:00
|
|
|
// For each `AllocId`, we keep track of which decoding state it's currently in.
|
|
|
|
decoding_state: Vec<Lock<State>>,
|
2018-05-25 17:19:31 +02:00
|
|
|
// The offsets of each allocation in the data stream.
|
2023-07-10 21:39:28 -04:00
|
|
|
data_offsets: Vec<u64>,
|
2018-05-25 17:19:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AllocDecodingState {
|
2021-06-01 00:00:00 +00:00
|
|
|
#[inline]
|
2018-08-29 22:02:42 -07:00
|
|
|
pub fn new_decoding_session(&self) -> AllocDecodingSession<'_> {
|
2024-07-06 20:41:52 -04:00
|
|
|
AllocDecodingSession { state: self }
|
2018-05-25 17:19:31 +02:00
|
|
|
}
|
|
|
|
|
2023-07-10 21:39:28 -04:00
|
|
|
pub fn new(data_offsets: Vec<u64>) -> Self {
|
2023-03-14 11:30:23 +00:00
|
|
|
let decoding_state =
|
|
|
|
std::iter::repeat_with(|| Lock::new(State::Empty)).take(data_offsets.len()).collect();
|
2018-05-25 17:19:31 +02:00
|
|
|
|
2018-10-30 14:37:26 +01:00
|
|
|
Self { decoding_state, data_offsets }
|
2018-05-25 17:19:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct AllocDecodingSession<'s> {
|
|
|
|
state: &'s AllocDecodingState,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'s> AllocDecodingSession<'s> {
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Decodes an `AllocId` in a thread-safe way.
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
pub fn decode_alloc_id<'tcx, D>(&self, decoder: &mut D) -> AllocId
|
2019-06-12 00:11:55 +03:00
|
|
|
where
|
2022-05-27 20:03:57 -07:00
|
|
|
D: TyDecoder<I = TyCtxt<'tcx>>,
|
2018-05-25 17:19:31 +02:00
|
|
|
{
|
2019-09-06 03:57:44 +01:00
|
|
|
// Read the index of the allocation.
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
let idx = usize::try_from(decoder.read_u32()).unwrap();
|
2020-03-21 13:49:02 +01:00
|
|
|
let pos = usize::try_from(self.state.data_offsets[idx]).unwrap();
|
2018-05-25 17:19:31 +02:00
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
// Decode the `AllocDiscriminant` now so that we know if we have to reserve an
|
|
|
|
// `AllocId`.
|
2018-05-25 17:19:31 +02:00
|
|
|
let (alloc_kind, pos) = decoder.with_position(pos, |decoder| {
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
let alloc_kind = AllocDiscriminant::decode(decoder);
|
|
|
|
(alloc_kind, decoder.position())
|
|
|
|
});
|
2018-05-25 17:19:31 +02:00
|
|
|
|
2024-07-19 17:43:33 -04:00
|
|
|
// We are going to hold this lock during the entire decoding of this allocation, which may
|
|
|
|
// require that we decode other allocations. This cannot deadlock for two reasons:
|
|
|
|
//
|
|
|
|
// At the time of writing, it is only possible to create an allocation that contains a pointer
|
|
|
|
// to itself using the const_allocate intrinsic (which is for testing only), and even attempting
|
|
|
|
// to evaluate such consts blows the stack. If we ever grow a mechanism for producing
|
|
|
|
// cyclic allocations, we will need a new strategy for decoding that doesn't bring back
|
|
|
|
// https://github.com/rust-lang/rust/issues/126741.
|
|
|
|
//
|
|
|
|
// It is also impossible to create two allocations (call them A and B) where A is a pointer to B, and B
|
|
|
|
// is a pointer to A, because attempting to evaluate either of those consts will produce a
|
|
|
|
// query cycle, failing compilation.
|
|
|
|
let mut entry = self.state.decoding_state[idx].lock();
|
2019-09-06 03:57:44 +01:00
|
|
|
// Check the decoding state to see if it's already decoded or if we should
|
2018-05-25 17:19:31 +02:00
|
|
|
// decode it here.
|
2024-07-06 20:41:52 -04:00
|
|
|
if let State::Done(alloc_id) = *entry {
|
|
|
|
return alloc_id;
|
|
|
|
}
|
2018-05-25 17:19:31 +02:00
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
// Now decode the actual data.
|
2024-08-06 19:02:01 +02:00
|
|
|
let alloc_id = decoder.with_position(pos, |decoder| match alloc_kind {
|
|
|
|
AllocDiscriminant::Alloc => {
|
|
|
|
trace!("creating memory alloc ID");
|
|
|
|
let alloc = <ConstAllocation<'tcx> as Decodable<_>>::decode(decoder);
|
|
|
|
trace!("decoded alloc {:?}", alloc);
|
|
|
|
decoder.interner().reserve_and_set_memory_alloc(alloc)
|
|
|
|
}
|
|
|
|
AllocDiscriminant::Fn => {
|
|
|
|
trace!("creating fn alloc ID");
|
|
|
|
let instance = ty::Instance::decode(decoder);
|
|
|
|
trace!("decoded fn alloc instance: {:?}", instance);
|
|
|
|
decoder.interner().reserve_and_set_fn_alloc(instance, CTFE_ALLOC_SALT)
|
|
|
|
}
|
|
|
|
AllocDiscriminant::VTable => {
|
|
|
|
trace!("creating vtable alloc ID");
|
2024-09-22 20:00:01 -04:00
|
|
|
let ty = Decodable::decode(decoder);
|
|
|
|
let poly_trait_ref = Decodable::decode(decoder);
|
2024-08-06 19:02:01 +02:00
|
|
|
trace!("decoded vtable alloc instance: {ty:?}, {poly_trait_ref:?}");
|
|
|
|
decoder.interner().reserve_and_set_vtable_alloc(ty, poly_trait_ref, CTFE_ALLOC_SALT)
|
|
|
|
}
|
|
|
|
AllocDiscriminant::Static => {
|
|
|
|
trace!("creating extern static alloc ID");
|
|
|
|
let did = <DefId as Decodable<D>>::decode(decoder);
|
|
|
|
trace!("decoded static def-ID: {:?}", did);
|
|
|
|
decoder.interner().reserve_and_set_static_alloc(did)
|
2018-05-25 17:19:31 +02:00
|
|
|
}
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
});
|
2018-05-25 17:19:31 +02:00
|
|
|
|
2024-07-06 20:41:52 -04:00
|
|
|
*entry = State::Done(alloc_id);
|
2018-05-25 17:19:31 +02:00
|
|
|
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
alloc_id
|
2018-05-25 17:19:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-30 13:05:05 +02:00
|
|
|
/// An allocation in the global (tcx-managed) memory can be either a function pointer,
|
|
|
|
/// a static, or a "real" allocation with some data in it.
|
2020-06-11 15:49:57 +01:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq, Hash, TyDecodable, TyEncodable, HashStable)]
|
2019-05-30 13:05:05 +02:00
|
|
|
pub enum GlobalAlloc<'tcx> {
|
2019-09-06 03:57:44 +01:00
|
|
|
/// The alloc ID is used as a function pointer.
|
2024-08-06 19:02:01 +02:00
|
|
|
Function { instance: Instance<'tcx> },
|
2022-07-17 11:36:37 -04:00
|
|
|
/// This alloc ID points to a symbolic (not-reified) vtable.
|
2024-09-22 20:00:01 -04:00
|
|
|
/// We remember the full dyn type, not just the principal trait, so that
|
|
|
|
/// const-eval and Miri can detect UB due to invalid transmutes of
|
|
|
|
/// `dyn Trait` types.
|
|
|
|
VTable(Ty<'tcx>, &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>),
|
2019-02-08 14:53:55 +01:00
|
|
|
/// The alloc ID points to a "lazy" static variable that did not get computed (yet).
|
2018-08-23 19:04:33 +02:00
|
|
|
/// This is also used to break the cycle in recursive statics.
|
2018-05-02 06:03:06 +02:00
|
|
|
Static(DefId),
|
2019-02-08 14:53:55 +01:00
|
|
|
/// The alloc ID points to memory.
|
Introduce `ConstAllocation`.
Currently some `Allocation`s are interned, some are not, and it's very
hard to tell at a use point which is which.
This commit introduces `ConstAllocation` for the known-interned ones,
which makes the division much clearer. `ConstAllocation::inner()` is
used to get the underlying `Allocation`.
In some places it's natural to use an `Allocation`, in some it's natural
to use a `ConstAllocation`, and in some places there's no clear choice.
I've tried to make things look as nice as possible, while generally
favouring `ConstAllocation`, which is the type that embodies more
information. This does require quite a few calls to `inner()`.
The commit also tweaks how `PartialOrd` works for `Interned`. The
previous code was too clever by half, building on `T: Ord` to make the
code shorter. That caused problems with deriving `PartialOrd` and `Ord`
for `ConstAllocation`, so I changed it to build on `T: PartialOrd`,
which is slightly more verbose but much more standard and avoided the
problems.
2022-03-02 07:15:04 +11:00
|
|
|
Memory(ConstAllocation<'tcx>),
|
2018-05-02 06:03:06 +02:00
|
|
|
}
|
|
|
|
|
2021-12-15 19:32:30 -05:00
|
|
|
impl<'tcx> GlobalAlloc<'tcx> {
|
2020-04-24 17:33:25 +02:00
|
|
|
/// Panics if the `GlobalAlloc` does not refer to an `GlobalAlloc::Memory`
|
|
|
|
#[track_caller]
|
|
|
|
#[inline]
|
Introduce `ConstAllocation`.
Currently some `Allocation`s are interned, some are not, and it's very
hard to tell at a use point which is which.
This commit introduces `ConstAllocation` for the known-interned ones,
which makes the division much clearer. `ConstAllocation::inner()` is
used to get the underlying `Allocation`.
In some places it's natural to use an `Allocation`, in some it's natural
to use a `ConstAllocation`, and in some places there's no clear choice.
I've tried to make things look as nice as possible, while generally
favouring `ConstAllocation`, which is the type that embodies more
information. This does require quite a few calls to `inner()`.
The commit also tweaks how `PartialOrd` works for `Interned`. The
previous code was too clever by half, building on `T: Ord` to make the
code shorter. That caused problems with deriving `PartialOrd` and `Ord`
for `ConstAllocation`, so I changed it to build on `T: PartialOrd`,
which is slightly more verbose but much more standard and avoided the
problems.
2022-03-02 07:15:04 +11:00
|
|
|
pub fn unwrap_memory(&self) -> ConstAllocation<'tcx> {
|
2020-04-24 17:33:25 +02:00
|
|
|
match *self {
|
|
|
|
GlobalAlloc::Memory(mem) => mem,
|
|
|
|
_ => bug!("expected memory, got {:?}", self),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Panics if the `GlobalAlloc` is not `GlobalAlloc::Function`
|
|
|
|
#[track_caller]
|
|
|
|
#[inline]
|
|
|
|
pub fn unwrap_fn(&self) -> Instance<'tcx> {
|
|
|
|
match *self {
|
2024-07-02 21:05:22 +02:00
|
|
|
GlobalAlloc::Function { instance, .. } => instance,
|
2020-04-24 17:33:25 +02:00
|
|
|
_ => bug!("expected function, got {:?}", self),
|
|
|
|
}
|
|
|
|
}
|
2022-07-17 11:36:37 -04:00
|
|
|
|
2022-07-19 19:57:44 -04:00
|
|
|
/// Panics if the `GlobalAlloc` is not `GlobalAlloc::VTable`
|
2022-07-17 11:36:37 -04:00
|
|
|
#[track_caller]
|
|
|
|
#[inline]
|
|
|
|
pub fn unwrap_vtable(&self) -> (Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>) {
|
|
|
|
match *self {
|
2024-09-22 20:00:01 -04:00
|
|
|
GlobalAlloc::VTable(ty, dyn_ty) => (ty, dyn_ty.principal()),
|
2022-07-17 11:36:37 -04:00
|
|
|
_ => bug!("expected vtable, got {:?}", self),
|
|
|
|
}
|
|
|
|
}
|
2023-01-25 01:46:19 -05:00
|
|
|
|
|
|
|
/// The address space that this `GlobalAlloc` should be placed in.
|
|
|
|
#[inline]
|
|
|
|
pub fn address_space(&self, cx: &impl HasDataLayout) -> AddressSpace {
|
|
|
|
match self {
|
2024-07-02 21:05:22 +02:00
|
|
|
GlobalAlloc::Function { .. } => cx.data_layout().instruction_address_space,
|
2023-01-25 01:46:19 -05:00
|
|
|
GlobalAlloc::Static(..) | GlobalAlloc::Memory(..) | GlobalAlloc::VTable(..) => {
|
|
|
|
AddressSpace::DATA
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-24 17:33:25 +02:00
|
|
|
}
|
|
|
|
|
2024-08-06 19:02:01 +02:00
|
|
|
pub const CTFE_ALLOC_SALT: usize = 0;
|
|
|
|
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) struct AllocMap<'tcx> {
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Maps `AllocId`s to their corresponding allocations.
|
2019-05-30 13:05:05 +02:00
|
|
|
alloc_map: FxHashMap<AllocId, GlobalAlloc<'tcx>>,
|
2018-05-02 06:03:06 +02:00
|
|
|
|
2024-08-06 19:02:01 +02:00
|
|
|
/// Used to deduplicate global allocations: functions, vtables, string literals, ...
|
|
|
|
///
|
|
|
|
/// The `usize` is a "salt" used by Miri to make deduplication imperfect, thus better emulating
|
|
|
|
/// the actual guarantees.
|
|
|
|
dedup: FxHashMap<(GlobalAlloc<'tcx>, usize), AllocId>,
|
2018-05-02 06:03:06 +02:00
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// The `AllocId` to assign to the next requested ID.
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Always incremented; never gets smaller.
|
2018-05-02 06:03:06 +02:00
|
|
|
next_id: AllocId,
|
|
|
|
}
|
|
|
|
|
2018-12-03 14:54:58 +01:00
|
|
|
impl<'tcx> AllocMap<'tcx> {
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) fn new() -> Self {
|
2021-07-14 09:56:54 +02:00
|
|
|
AllocMap {
|
|
|
|
alloc_map: Default::default(),
|
|
|
|
dedup: Default::default(),
|
2024-02-08 23:03:25 +01:00
|
|
|
next_id: AllocId(NonZero::new(1).unwrap()),
|
2021-07-14 09:56:54 +02:00
|
|
|
}
|
2018-05-02 06:03:06 +02:00
|
|
|
}
|
2020-04-24 17:34:56 +02:00
|
|
|
fn reserve(&mut self) -> AllocId {
|
2018-05-02 06:03:06 +02:00
|
|
|
let next = self.next_id;
|
|
|
|
self.next_id.0 = self.next_id.0.checked_add(1).expect(
|
|
|
|
"You overflowed a u64 by incrementing by 1... \
|
2020-01-03 13:31:56 +01:00
|
|
|
You've just earned yourself a free drink if we ever meet. \
|
|
|
|
Seriously, how did you do that?!",
|
2018-05-02 06:03:06 +02:00
|
|
|
);
|
|
|
|
next
|
|
|
|
}
|
2020-04-24 12:53:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> TyCtxt<'tcx> {
|
|
|
|
/// Obtains a new allocation ID that can be referenced but does not
|
|
|
|
/// yet have an allocation backing it.
|
|
|
|
///
|
|
|
|
/// Make sure to call `set_alloc_id_memory` or `set_alloc_id_same_memory` before returning such
|
|
|
|
/// an `AllocId` from a query.
|
2020-09-18 20:49:25 +02:00
|
|
|
pub fn reserve_alloc_id(self) -> AllocId {
|
2020-04-24 12:53:18 +02:00
|
|
|
self.alloc_map.lock().reserve()
|
|
|
|
}
|
2018-05-02 06:03:06 +02:00
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Reserves a new ID *if* this allocation has not been dedup-reserved before.
|
2024-07-12 13:08:05 +08:00
|
|
|
/// Should not be used for mutable memory.
|
2024-08-06 19:02:01 +02:00
|
|
|
fn reserve_and_set_dedup(self, alloc: GlobalAlloc<'tcx>, salt: usize) -> AllocId {
|
2020-04-24 12:53:18 +02:00
|
|
|
let mut alloc_map = self.alloc_map.lock();
|
2024-07-12 13:08:05 +08:00
|
|
|
if let GlobalAlloc::Memory(mem) = alloc {
|
|
|
|
if mem.inner().mutability.is_mut() {
|
|
|
|
bug!("trying to dedup-reserve mutable memory");
|
|
|
|
}
|
2019-05-30 13:05:05 +02:00
|
|
|
}
|
2024-08-06 19:02:01 +02:00
|
|
|
let alloc_salt = (alloc, salt);
|
|
|
|
if let Some(&alloc_id) = alloc_map.dedup.get(&alloc_salt) {
|
2018-05-02 06:03:06 +02:00
|
|
|
return alloc_id;
|
|
|
|
}
|
2020-04-24 12:53:18 +02:00
|
|
|
let id = alloc_map.reserve();
|
2024-08-06 19:02:01 +02:00
|
|
|
debug!("creating alloc {:?} with id {id:?}", alloc_salt.0);
|
|
|
|
alloc_map.alloc_map.insert(id, alloc_salt.0.clone());
|
|
|
|
alloc_map.dedup.insert(alloc_salt, id);
|
2018-05-02 06:03:06 +02:00
|
|
|
id
|
|
|
|
}
|
|
|
|
|
2024-07-12 13:08:05 +08:00
|
|
|
/// Generates an `AllocId` for a memory allocation. If the exact same memory has been
|
|
|
|
/// allocated before, this will return the same `AllocId`.
|
2024-08-06 19:02:01 +02:00
|
|
|
pub fn reserve_and_set_memory_dedup(self, mem: ConstAllocation<'tcx>, salt: usize) -> AllocId {
|
|
|
|
self.reserve_and_set_dedup(GlobalAlloc::Memory(mem), salt)
|
2024-07-12 13:08:05 +08:00
|
|
|
}
|
|
|
|
|
2019-05-30 13:05:05 +02:00
|
|
|
/// Generates an `AllocId` for a static or return a cached one in case this function has been
|
|
|
|
/// called on the same static before.
|
2023-09-12 08:42:36 +02:00
|
|
|
pub fn reserve_and_set_static_alloc(self, static_id: DefId) -> AllocId {
|
2024-08-06 19:02:01 +02:00
|
|
|
let salt = 0; // Statics have a guaranteed unique address, no salt added.
|
|
|
|
self.reserve_and_set_dedup(GlobalAlloc::Static(static_id), salt)
|
2024-07-02 21:05:22 +02:00
|
|
|
}
|
|
|
|
|
2024-08-06 19:02:01 +02:00
|
|
|
/// Generates an `AllocId` for a function. Will get deduplicated.
|
|
|
|
pub fn reserve_and_set_fn_alloc(self, instance: Instance<'tcx>, salt: usize) -> AllocId {
|
|
|
|
self.reserve_and_set_dedup(GlobalAlloc::Function { instance }, salt)
|
2018-05-02 06:03:06 +02:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:34:16 +00:00
|
|
|
/// Generates an `AllocId` for a (symbolic, not-reified) vtable. Will get deduplicated.
|
2023-09-12 08:42:36 +02:00
|
|
|
pub fn reserve_and_set_vtable_alloc(
|
2022-07-17 11:40:34 -04:00
|
|
|
self,
|
|
|
|
ty: Ty<'tcx>,
|
2024-09-22 20:00:01 -04:00
|
|
|
dyn_ty: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
|
2024-08-06 19:02:01 +02:00
|
|
|
salt: usize,
|
2022-07-17 11:40:34 -04:00
|
|
|
) -> AllocId {
|
2024-09-22 20:00:01 -04:00
|
|
|
self.reserve_and_set_dedup(GlobalAlloc::VTable(ty, dyn_ty), salt)
|
2022-07-17 11:36:37 -04:00
|
|
|
}
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Interns the `Allocation` and return a new `AllocId`, even if there's already an identical
|
2019-05-30 13:05:05 +02:00
|
|
|
/// `Allocation` with a different `AllocId`.
|
|
|
|
/// Statics with identical content will still point to the same `Allocation`, i.e.,
|
|
|
|
/// their data will be deduplicated through `Allocation` interning -- but they
|
|
|
|
/// are different places in memory and as such need different IDs.
|
2023-09-12 08:42:36 +02:00
|
|
|
pub fn reserve_and_set_memory_alloc(self, mem: ConstAllocation<'tcx>) -> AllocId {
|
2020-04-24 12:53:18 +02:00
|
|
|
let id = self.reserve_alloc_id();
|
2019-05-30 13:05:05 +02:00
|
|
|
self.set_alloc_id_memory(id, mem);
|
|
|
|
id
|
|
|
|
}
|
|
|
|
|
2019-03-27 10:57:03 +09:00
|
|
|
/// Returns `None` in case the `AllocId` is dangling. An `InterpretCx` can still have a
|
2018-12-04 09:58:36 +01:00
|
|
|
/// local `Allocation` for that `AllocId`, but having such an `AllocId` in a constant is
|
|
|
|
/// illegal and will likely ICE.
|
2018-12-03 16:18:31 +01:00
|
|
|
/// This function exists to allow const eval to detect the difference between evaluation-
|
|
|
|
/// local dangling pointers and allocations in constants/statics.
|
2019-05-27 09:40:13 +02:00
|
|
|
#[inline]
|
2022-07-17 11:40:34 -04:00
|
|
|
pub fn try_get_global_alloc(self, id: AllocId) -> Option<GlobalAlloc<'tcx>> {
|
2020-04-24 12:53:18 +02:00
|
|
|
self.alloc_map.lock().alloc_map.get(&id).cloned()
|
2018-05-02 06:03:06 +02:00
|
|
|
}
|
|
|
|
|
2020-05-08 10:58:53 +02:00
|
|
|
#[inline]
|
|
|
|
#[track_caller]
|
2020-05-08 21:59:17 +02:00
|
|
|
/// Panics in case the `AllocId` is dangling. Since that is impossible for `AllocId`s in
|
|
|
|
/// constants (as all constants must pass interning and validation that check for dangling
|
|
|
|
/// ids), this function is frequently used throughout rustc, but should not be used within
|
2023-08-02 11:27:00 +02:00
|
|
|
/// the interpreter.
|
2020-09-18 20:49:25 +02:00
|
|
|
pub fn global_alloc(self, id: AllocId) -> GlobalAlloc<'tcx> {
|
2022-07-17 11:40:34 -04:00
|
|
|
match self.try_get_global_alloc(id) {
|
2020-05-08 10:58:53 +02:00
|
|
|
Some(alloc) => alloc,
|
2022-07-02 10:53:34 -04:00
|
|
|
None => bug!("could not find allocation for {id:?}"),
|
2019-10-02 20:29:16 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Freezes an `AllocId` created with `reserve` by pointing it at an `Allocation`. Trying to
|
2018-12-03 16:18:31 +01:00
|
|
|
/// call this function twice, even with the same `Allocation` will ICE the compiler.
|
Introduce `ConstAllocation`.
Currently some `Allocation`s are interned, some are not, and it's very
hard to tell at a use point which is which.
This commit introduces `ConstAllocation` for the known-interned ones,
which makes the division much clearer. `ConstAllocation::inner()` is
used to get the underlying `Allocation`.
In some places it's natural to use an `Allocation`, in some it's natural
to use a `ConstAllocation`, and in some places there's no clear choice.
I've tried to make things look as nice as possible, while generally
favouring `ConstAllocation`, which is the type that embodies more
information. This does require quite a few calls to `inner()`.
The commit also tweaks how `PartialOrd` works for `Interned`. The
previous code was too clever by half, building on `T: Ord` to make the
code shorter. That caused problems with deriving `PartialOrd` and `Ord`
for `ConstAllocation`, so I changed it to build on `T: PartialOrd`,
which is slightly more verbose but much more standard and avoided the
problems.
2022-03-02 07:15:04 +11:00
|
|
|
pub fn set_alloc_id_memory(self, id: AllocId, mem: ConstAllocation<'tcx>) {
|
2020-04-24 12:53:18 +02:00
|
|
|
if let Some(old) = self.alloc_map.lock().alloc_map.insert(id, GlobalAlloc::Memory(mem)) {
|
2022-07-02 10:53:34 -04:00
|
|
|
bug!("tried to set allocation ID {id:?}, but it was already existing as {old:#?}");
|
2018-05-02 06:03:06 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-25 17:19:31 +02:00
|
|
|
|
2024-02-26 18:03:06 +00:00
|
|
|
/// Freezes an `AllocId` created with `reserve` by pointing it at a static item. Trying to
|
|
|
|
/// call this function twice, even with the same `DefId` will ICE the compiler.
|
|
|
|
pub fn set_nested_alloc_id_static(self, id: AllocId, def_id: LocalDefId) {
|
|
|
|
if let Some(old) =
|
|
|
|
self.alloc_map.lock().alloc_map.insert(id, GlobalAlloc::Static(def_id.to_def_id()))
|
|
|
|
{
|
|
|
|
bug!("tried to set allocation ID {id:?}, but it was already existing as {old:#?}");
|
|
|
|
}
|
|
|
|
}
|
2018-05-02 06:03:06 +02:00
|
|
|
}
|
|
|
|
|
2018-04-26 09:18:19 +02:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Methods to access integers in the target endianness
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-05-27 09:40:13 +02:00
|
|
|
#[inline]
|
2018-04-26 09:18:19 +02:00
|
|
|
pub fn write_target_uint(
|
2020-03-31 18:16:47 +02:00
|
|
|
endianness: Endian,
|
2018-04-26 09:18:19 +02:00
|
|
|
mut target: &mut [u8],
|
|
|
|
data: u128,
|
|
|
|
) -> Result<(), io::Error> {
|
2020-08-21 00:55:33 -07:00
|
|
|
// This u128 holds an "any-size uint" (since smaller uints can fits in it)
|
|
|
|
// So we do not write all bytes of the u128, just the "payload".
|
2018-04-26 09:18:19 +02:00
|
|
|
match endianness {
|
2020-08-20 02:37:00 -07:00
|
|
|
Endian::Little => target.write(&data.to_le_bytes())?,
|
2020-08-22 03:54:15 -07:00
|
|
|
Endian::Big => target.write(&data.to_be_bytes()[16 - target.len()..])?,
|
2020-08-20 02:37:00 -07:00
|
|
|
};
|
2020-08-21 00:55:33 -07:00
|
|
|
debug_assert!(target.len() == 0); // We should have filled the target buffer.
|
2020-08-20 02:37:00 -07:00
|
|
|
Ok(())
|
2018-04-26 09:18:19 +02:00
|
|
|
}
|
|
|
|
|
2019-05-27 09:40:13 +02:00
|
|
|
#[inline]
|
2020-03-31 18:16:47 +02:00
|
|
|
pub fn read_target_uint(endianness: Endian, mut source: &[u8]) -> Result<u128, io::Error> {
|
2020-08-21 00:55:33 -07:00
|
|
|
// This u128 holds an "any-size uint" (since smaller uints can fits in it)
|
2020-08-20 15:55:02 -07:00
|
|
|
let mut buf = [0u8; std::mem::size_of::<u128>()];
|
2020-08-21 00:55:33 -07:00
|
|
|
// So we do not read exactly 16 bytes into the u128, just the "payload".
|
2020-08-22 03:54:15 -07:00
|
|
|
let uint = match endianness {
|
|
|
|
Endian::Little => {
|
2024-02-25 01:11:09 +03:00
|
|
|
source.read_exact(&mut buf[..source.len()])?;
|
2020-08-22 03:54:15 -07:00
|
|
|
Ok(u128::from_le_bytes(buf))
|
|
|
|
}
|
|
|
|
Endian::Big => {
|
2024-02-25 01:11:09 +03:00
|
|
|
source.read_exact(&mut buf[16 - source.len()..])?;
|
2020-08-22 03:54:15 -07:00
|
|
|
Ok(u128::from_be_bytes(buf))
|
|
|
|
}
|
|
|
|
};
|
2020-08-21 00:55:33 -07:00
|
|
|
debug_assert!(source.len() == 0); // We should have consumed the source buffer.
|
2020-08-22 03:54:15 -07:00
|
|
|
uint
|
2017-12-12 17:14:49 +01:00
|
|
|
}
|