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
|
|
|
|
2017-08-02 16:59:01 +02:00
|
|
|
#[macro_export]
|
2019-08-01 09:49:01 +05:30
|
|
|
macro_rules! err_unsup {
|
2019-07-29 13:06:42 +05:30
|
|
|
($($tt:tt)*) => {
|
2019-08-01 09:49:01 +05:30
|
|
|
$crate::mir::interpret::InterpError::Unsupported(
|
|
|
|
$crate::mir::interpret::UnsupportedOpInfo::$($tt)*
|
|
|
|
)
|
2019-07-29 13:06:42 +05:30
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-08-03 20:35:52 +02:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! err_unsup_format {
|
|
|
|
($($tt:tt)*) => { err_unsup!(Unsupported(format!($($tt)*))) };
|
|
|
|
}
|
|
|
|
|
2019-07-30 15:25:12 +05:30
|
|
|
#[macro_export]
|
|
|
|
macro_rules! err_inval {
|
|
|
|
($($tt:tt)*) => {
|
|
|
|
$crate::mir::interpret::InterpError::InvalidProgram(
|
|
|
|
$crate::mir::interpret::InvalidProgramInfo::$($tt)*
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
2019-08-01 09:49:01 +05:30
|
|
|
macro_rules! err_ub {
|
2019-07-30 15:25:12 +05:30
|
|
|
($($tt:tt)*) => {
|
2019-08-02 23:24:27 +02:00
|
|
|
$crate::mir::interpret::InterpError::UndefinedBehavior(
|
|
|
|
$crate::mir::interpret::UndefinedBehaviorInfo::$($tt)*
|
2019-08-01 09:49:01 +05:30
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-08-03 20:35:52 +02:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! err_ub_format {
|
|
|
|
($($tt:tt)*) => { err_ub!(Ub(format!($($tt)*))) };
|
|
|
|
}
|
|
|
|
|
2019-07-31 12:48:54 +05:30
|
|
|
#[macro_export]
|
|
|
|
macro_rules! err_exhaust {
|
|
|
|
($($tt:tt)*) => {
|
|
|
|
$crate::mir::interpret::InterpError::ResourceExhaustion(
|
|
|
|
$crate::mir::interpret::ResourceExhaustionInfo::$($tt)*
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
2019-08-01 09:49:01 +05:30
|
|
|
macro_rules! throw_unsup {
|
|
|
|
($($tt:tt)*) => { return Err(err_unsup!($($tt)*).into()) };
|
|
|
|
}
|
|
|
|
|
2019-08-02 23:41:24 +02:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! throw_unsup_format {
|
|
|
|
($($tt:tt)*) => { throw_unsup!(Unsupported(format!($($tt)*))) };
|
|
|
|
}
|
|
|
|
|
2019-08-01 09:49:01 +05:30
|
|
|
#[macro_export]
|
|
|
|
macro_rules! throw_inval {
|
|
|
|
($($tt:tt)*) => { return Err(err_inval!($($tt)*).into()) };
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! throw_ub {
|
2019-08-01 12:13:49 +05:30
|
|
|
($($tt:tt)*) => { return Err(err_ub!($($tt)*).into()) };
|
2019-07-31 12:48:54 +05:30
|
|
|
}
|
|
|
|
|
2019-08-02 23:41:24 +02:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! throw_ub_format {
|
|
|
|
($($tt:tt)*) => { throw_ub!(Ub(format!($($tt)*))) };
|
|
|
|
}
|
|
|
|
|
2019-08-01 09:49:01 +05:30
|
|
|
#[macro_export]
|
|
|
|
macro_rules! throw_exhaust {
|
|
|
|
($($tt:tt)*) => { return Err(err_exhaust!($($tt)*).into()) };
|
|
|
|
}
|
|
|
|
|
2019-12-02 10:59:06 +01:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! throw_machine_stop {
|
|
|
|
($($tt:tt)*) => {
|
|
|
|
return Err($crate::mir::interpret::InterpError::MachineStop(Box::new($($tt)*)).into())
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-10-23 18:05:32 +02:00
|
|
|
mod allocation;
|
2019-12-22 17:42:04 -05:00
|
|
|
mod error;
|
2018-10-25 18:23:09 +02:00
|
|
|
mod pointer;
|
2019-11-30 08:42:56 +13:00
|
|
|
mod queries;
|
2019-12-22 17:42:04 -05:00
|
|
|
mod value;
|
2016-12-07 20:30:37 -08:00
|
|
|
|
2018-06-25 18:46:02 +02:00
|
|
|
pub use self::error::{
|
2020-02-10 20:53:01 +01:00
|
|
|
struct_error, ConstEvalErr, ConstEvalRawResult, ConstEvalResult, ErrorHandled, FrameInfo,
|
|
|
|
InterpError, InterpErrorInfo, InterpResult, InvalidProgramInfo, ResourceExhaustionInfo,
|
|
|
|
UndefinedBehaviorInfo, UnsupportedOpInfo,
|
2018-06-25 18:46:02 +02:00
|
|
|
};
|
2016-06-10 13:01:51 +02:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
pub use self::value::{get_slice_bytes, ConstValue, RawConst, Scalar, ScalarMaybeUndef};
|
2016-06-10 13:01:51 +02:00
|
|
|
|
2019-06-23 16:42:51 +02:00
|
|
|
pub use self::allocation::{Allocation, AllocationExtra, Relocations, UndefMask};
|
2018-10-23 18:05:32 +02:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
pub use self::pointer::{CheckInAllocMsg, Pointer, PointerArithmetic};
|
2018-10-25 18:23:09 +02:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
use crate::mir;
|
2019-09-06 03:57:44 +01:00
|
|
|
use crate::ty::codec::TyDecoder;
|
2019-02-05 11:20:45 -06:00
|
|
|
use crate::ty::layout::{self, Size};
|
2019-11-30 08:42:56 +13:00
|
|
|
use crate::ty::subst::GenericArgKind;
|
2020-01-11 15:22:36 +13:00
|
|
|
use crate::ty::{self, Instance, Ty, TyCtxt};
|
2019-12-22 17:42:04 -05:00
|
|
|
use byteorder::{BigEndian, LittleEndian, ReadBytesExt, WriteBytesExt};
|
2020-02-29 20:37:32 +03:00
|
|
|
use rustc_ast::ast::LitKind;
|
2018-05-02 06:03:06 +02:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2019-12-22 17:42:04 -05:00
|
|
|
use rustc_data_structures::sync::{HashMapExt, Lock};
|
2018-05-25 17:19:31 +02:00
|
|
|
use rustc_data_structures::tiny_list::TinyList;
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir::def_id::DefId;
|
2018-12-03 01:14:35 +01:00
|
|
|
use rustc_macros::HashStable;
|
2019-12-22 17:42:04 -05:00
|
|
|
use rustc_serialize::{Decodable, Encodable, Encoder};
|
|
|
|
use std::fmt;
|
|
|
|
use std::io;
|
|
|
|
use std::num::NonZeroU32;
|
|
|
|
use std::sync::atomic::{AtomicU32, Ordering};
|
2016-12-07 20:58:48 -08:00
|
|
|
|
2019-11-11 14:32:36 +00:00
|
|
|
/// Uniquely identifies one of the following:
|
|
|
|
/// - A constant
|
|
|
|
/// - A static
|
|
|
|
/// - A const fn where all arguments (if any) are zero-sized types
|
2019-11-15 18:19:52 +01:00
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, RustcEncodable, RustcDecodable)]
|
|
|
|
#[derive(HashStable, Lift)]
|
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>,
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
/// This is used for graceful error handling (`delay_span_bug`) in
|
|
|
|
/// type checking (`AstConv::ast_const_to_const`).
|
|
|
|
TypeError,
|
2020-01-11 15:22:36 +13:00
|
|
|
UnparseableFloat,
|
|
|
|
Reported,
|
|
|
|
}
|
|
|
|
|
2019-12-23 17:41:06 +01:00
|
|
|
#[derive(Copy, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
2017-12-12 17:14:49 +01:00
|
|
|
pub struct AllocId(pub u64);
|
|
|
|
|
2019-12-23 17:41:06 +01:00
|
|
|
impl fmt::Debug for AllocId {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(fmt, "alloc{}", self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-23 18:50:47 +03:00
|
|
|
impl rustc_serialize::UseSpecializedEncodable for AllocId {}
|
|
|
|
impl rustc_serialize::UseSpecializedDecodable for AllocId {}
|
2018-01-16 09:12:54 +01:00
|
|
|
|
2018-04-10 09:58:46 +02:00
|
|
|
#[derive(RustcDecodable, RustcEncodable)]
|
2018-12-03 16:30:43 +01:00
|
|
|
enum AllocDiscriminant {
|
2018-04-10 09:58:46 +02:00
|
|
|
Alloc,
|
|
|
|
Fn,
|
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
|
|
|
|
2019-06-11 23:35:39 +03:00
|
|
|
pub fn specialized_encode_alloc_id<'tcx, E: Encoder>(
|
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,
|
|
|
|
) -> Result<(), E::Error> {
|
2019-12-22 17:42:04 -05:00
|
|
|
let alloc: GlobalAlloc<'tcx> =
|
|
|
|
tcx.alloc_map.lock().get(alloc_id).expect("no value for given alloc ID");
|
2019-05-30 13:05:05 +02:00
|
|
|
match alloc {
|
|
|
|
GlobalAlloc::Memory(alloc) => {
|
2018-05-02 06:03:06 +02:00
|
|
|
trace!("encoding {:?} with {:#?}", alloc_id, alloc);
|
2018-12-03 16:30:43 +01:00
|
|
|
AllocDiscriminant::Alloc.encode(encoder)?;
|
2018-05-02 06:03:06 +02:00
|
|
|
alloc.encode(encoder)?;
|
|
|
|
}
|
2019-05-30 13:05:05 +02:00
|
|
|
GlobalAlloc::Function(fn_instance) => {
|
2018-05-02 06:03:06 +02:00
|
|
|
trace!("encoding {:?} with {:#?}", alloc_id, fn_instance);
|
2018-12-03 16:30:43 +01:00
|
|
|
AllocDiscriminant::Fn.encode(encoder)?;
|
2018-05-02 06:03:06 +02:00
|
|
|
fn_instance.encode(encoder)?;
|
|
|
|
}
|
2019-05-30 13:05:05 +02:00
|
|
|
GlobalAlloc::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`.
|
2018-12-03 16:30:43 +01:00
|
|
|
AllocDiscriminant::Static.encode(encoder)?;
|
2018-05-02 06:03:06 +02:00
|
|
|
did.encode(encoder)?;
|
|
|
|
}
|
2018-03-16 09:59:42 +01:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-05-25 17:19:31 +02:00
|
|
|
// Used to avoid infinite recursion when decoding cyclic allocations.
|
|
|
|
type DecodingSessionId = NonZeroU32;
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
enum State {
|
|
|
|
Empty,
|
|
|
|
InProgressNonAlloc(TinyList<DecodingSessionId>),
|
|
|
|
InProgress(TinyList<DecodingSessionId>, AllocId),
|
|
|
|
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.
|
|
|
|
data_offsets: Vec<u32>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AllocDecodingState {
|
2018-08-29 22:02:42 -07:00
|
|
|
pub fn new_decoding_session(&self) -> AllocDecodingSession<'_> {
|
2018-05-25 17:19:31 +02:00
|
|
|
static DECODER_SESSION_ID: AtomicU32 = AtomicU32::new(0);
|
|
|
|
let counter = DECODER_SESSION_ID.fetch_add(1, Ordering::SeqCst);
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
// Make sure this is never zero.
|
2018-05-25 17:19:31 +02:00
|
|
|
let session_id = DecodingSessionId::new((counter & 0x7FFFFFFF) + 1).unwrap();
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
AllocDecodingSession { state: self, session_id }
|
2018-05-25 17:19:31 +02:00
|
|
|
}
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
pub fn new(data_offsets: Vec<u32>) -> Self {
|
|
|
|
let decoding_state = vec![Lock::new(State::Empty); data_offsets.len()];
|
2018-05-25 17:19:31 +02:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
Self { decoding_state, data_offsets }
|
2018-05-25 17:19:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct AllocDecodingSession<'s> {
|
|
|
|
state: &'s AllocDecodingState,
|
|
|
|
session_id: DecodingSessionId,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'s> AllocDecodingSession<'s> {
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Decodes an `AllocId` in a thread-safe way.
|
2019-06-12 00:11:55 +03:00
|
|
|
pub fn decode_alloc_id<D>(&self, decoder: &mut D) -> Result<AllocId, D::Error>
|
|
|
|
where
|
|
|
|
D: TyDecoder<'tcx>,
|
2018-05-25 17:19:31 +02:00
|
|
|
{
|
2019-09-06 03:57:44 +01:00
|
|
|
// Read the index of the allocation.
|
2018-05-25 17:19:31 +02:00
|
|
|
let idx = decoder.read_u32()? as usize;
|
|
|
|
let pos = self.state.data_offsets[idx] as usize;
|
|
|
|
|
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| {
|
2018-12-03 16:30:43 +01:00
|
|
|
let alloc_kind = AllocDiscriminant::decode(decoder)?;
|
2018-05-25 17:19:31 +02:00
|
|
|
Ok((alloc_kind, decoder.position()))
|
|
|
|
})?;
|
|
|
|
|
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.
|
|
|
|
let alloc_id = {
|
|
|
|
let mut entry = self.state.decoding_state[idx].lock();
|
|
|
|
|
|
|
|
match *entry {
|
|
|
|
State::Done(alloc_id) => {
|
|
|
|
return Ok(alloc_id);
|
|
|
|
}
|
|
|
|
ref mut entry @ State::Empty => {
|
2019-09-06 03:57:44 +01:00
|
|
|
// We are allowed to decode.
|
2018-05-25 17:19:31 +02:00
|
|
|
match alloc_kind {
|
2018-12-03 16:30:43 +01:00
|
|
|
AllocDiscriminant::Alloc => {
|
2018-05-25 17:19:31 +02:00
|
|
|
// If this is an allocation, we need to reserve an
|
2019-09-06 03:57:44 +01:00
|
|
|
// `AllocId` so we can decode cyclic graphs.
|
2018-05-25 17:19:31 +02:00
|
|
|
let alloc_id = decoder.tcx().alloc_map.lock().reserve();
|
2019-12-22 17:42:04 -05:00
|
|
|
*entry =
|
|
|
|
State::InProgress(TinyList::new_single(self.session_id), alloc_id);
|
2018-05-25 17:19:31 +02:00
|
|
|
Some(alloc_id)
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2018-12-03 16:30:43 +01:00
|
|
|
AllocDiscriminant::Fn | AllocDiscriminant::Static => {
|
2019-09-06 03:57:44 +01:00
|
|
|
// Fns and statics cannot be cyclic, and their `AllocId`
|
|
|
|
// is determined later by interning.
|
2019-12-22 17:42:04 -05:00
|
|
|
*entry =
|
|
|
|
State::InProgressNonAlloc(TinyList::new_single(self.session_id));
|
2018-05-25 17:19:31 +02:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
State::InProgressNonAlloc(ref mut sessions) => {
|
|
|
|
if sessions.contains(&self.session_id) {
|
2019-09-06 03:57:44 +01:00
|
|
|
bug!("this should be unreachable");
|
2018-05-25 17:19:31 +02:00
|
|
|
} else {
|
2019-09-06 03:57:44 +01:00
|
|
|
// Start decoding concurrently.
|
2018-05-25 17:19:31 +02:00
|
|
|
sessions.insert(self.session_id);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
State::InProgress(ref mut sessions, alloc_id) => {
|
|
|
|
if sessions.contains(&self.session_id) {
|
|
|
|
// Don't recurse.
|
2019-12-22 17:42:04 -05:00
|
|
|
return Ok(alloc_id);
|
2018-05-25 17:19:31 +02:00
|
|
|
} else {
|
2019-09-06 03:57:44 +01:00
|
|
|
// Start decoding concurrently.
|
2018-05-25 17:19:31 +02:00
|
|
|
sessions.insert(self.session_id);
|
|
|
|
Some(alloc_id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
// Now decode the actual data.
|
2018-05-25 17:19:31 +02:00
|
|
|
let alloc_id = decoder.with_position(pos, |decoder| {
|
|
|
|
match alloc_kind {
|
2018-12-03 16:30:43 +01:00
|
|
|
AllocDiscriminant::Alloc => {
|
2019-09-06 03:57:44 +01:00
|
|
|
let alloc = <&'tcx Allocation as Decodable>::decode(decoder)?;
|
|
|
|
// We already have a reserved `AllocId`.
|
2018-05-25 17:19:31 +02:00
|
|
|
let alloc_id = alloc_id.unwrap();
|
2019-09-06 03:57:44 +01:00
|
|
|
trace!("decoded alloc {:?}: {:#?}", alloc_id, alloc);
|
|
|
|
decoder.tcx().alloc_map.lock().set_alloc_id_same_memory(alloc_id, alloc);
|
2018-05-25 17:19:31 +02:00
|
|
|
Ok(alloc_id)
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2018-12-03 16:30:43 +01:00
|
|
|
AllocDiscriminant::Fn => {
|
2018-05-25 17:19:31 +02:00
|
|
|
assert!(alloc_id.is_none());
|
2019-09-06 03:57:44 +01:00
|
|
|
trace!("creating fn alloc ID");
|
2018-05-25 17:19:31 +02:00
|
|
|
let instance = ty::Instance::decode(decoder)?;
|
|
|
|
trace!("decoded fn alloc instance: {:?}", instance);
|
|
|
|
let alloc_id = decoder.tcx().alloc_map.lock().create_fn_alloc(instance);
|
|
|
|
Ok(alloc_id)
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2018-12-03 16:30:43 +01:00
|
|
|
AllocDiscriminant::Static => {
|
2018-05-25 17:19:31 +02:00
|
|
|
assert!(alloc_id.is_none());
|
2019-09-06 03:57:44 +01:00
|
|
|
trace!("creating extern static alloc ID");
|
2018-05-25 17:19:31 +02:00
|
|
|
let did = DefId::decode(decoder)?;
|
2019-09-06 03:57:44 +01:00
|
|
|
trace!("decoded static def-ID: {:?}", did);
|
2019-05-30 13:05:05 +02:00
|
|
|
let alloc_id = decoder.tcx().alloc_map.lock().create_static_alloc(did);
|
2018-05-25 17:19:31 +02:00
|
|
|
Ok(alloc_id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})?;
|
|
|
|
|
|
|
|
self.state.decoding_state[idx].with_lock(|entry| {
|
|
|
|
*entry = State::Done(alloc_id);
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(alloc_id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-12 17:14:49 +01:00
|
|
|
impl fmt::Display for AllocId {
|
2018-08-29 22:02:42 -07:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2017-12-12 17:14:49 +01:00
|
|
|
write!(f, "{}", self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2018-12-03 01:14:35 +01:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq, Hash, RustcDecodable, RustcEncodable, 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.
|
2018-05-02 06:03:06 +02:00
|
|
|
Function(Instance<'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.
|
2018-12-03 14:57:41 +01:00
|
|
|
Memory(&'tcx Allocation),
|
2018-05-02 06:03:06 +02:00
|
|
|
}
|
|
|
|
|
2018-12-03 14:54:58 +01:00
|
|
|
pub 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
|
|
|
|
2019-05-30 13:05:05 +02:00
|
|
|
/// Used to ensure that statics and functions only get one associated `AllocId`.
|
|
|
|
/// Should never contain a `GlobalAlloc::Memory`!
|
2019-09-06 03:57:44 +01:00
|
|
|
//
|
|
|
|
// FIXME: Should we just have two separate dedup maps for statics and functions each?
|
2019-05-30 13:05:05 +02:00
|
|
|
dedup: FxHashMap<GlobalAlloc<'tcx>, 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> {
|
2018-05-02 06:03:06 +02:00
|
|
|
pub fn new() -> Self {
|
2019-12-22 17:42:04 -05:00
|
|
|
AllocMap { alloc_map: Default::default(), dedup: Default::default(), next_id: AllocId(0) }
|
2018-05-02 06:03:06 +02:00
|
|
|
}
|
|
|
|
|
2018-12-03 16:18:31 +01:00
|
|
|
/// Obtains a new allocation ID that can be referenced but does not
|
2018-05-02 06:03:06 +02:00
|
|
|
/// yet have an allocation backing it.
|
2018-12-03 16:18:31 +01:00
|
|
|
///
|
2018-12-04 10:01:58 +01:00
|
|
|
/// Make sure to call `set_alloc_id_memory` or `set_alloc_id_same_memory` before returning such
|
|
|
|
/// an `AllocId` from a query.
|
2019-12-22 17:42:04 -05:00
|
|
|
pub fn reserve(&mut self) -> AllocId {
|
2018-05-02 06:03:06 +02:00
|
|
|
let next = self.next_id;
|
2019-12-22 17:42:04 -05:00
|
|
|
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?!",
|
2019-12-22 17:42:04 -05:00
|
|
|
);
|
2018-05-02 06:03:06 +02:00
|
|
|
next
|
|
|
|
}
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Reserves a new ID *if* this allocation has not been dedup-reserved before.
|
2019-05-30 13:05:05 +02:00
|
|
|
/// Should only be used for function pointers and statics, we don't want
|
|
|
|
/// to dedup IDs for "real" memory!
|
|
|
|
fn reserve_and_set_dedup(&mut self, alloc: GlobalAlloc<'tcx>) -> AllocId {
|
|
|
|
match alloc {
|
2019-12-22 17:42:04 -05:00
|
|
|
GlobalAlloc::Function(..) | GlobalAlloc::Static(..) => {}
|
2019-05-30 13:05:05 +02:00
|
|
|
GlobalAlloc::Memory(..) => bug!("Trying to dedup-reserve memory with real data!"),
|
|
|
|
}
|
|
|
|
if let Some(&alloc_id) = self.dedup.get(&alloc) {
|
2018-05-02 06:03:06 +02:00
|
|
|
return alloc_id;
|
|
|
|
}
|
|
|
|
let id = self.reserve();
|
2019-05-30 13:05:05 +02:00
|
|
|
debug!("creating alloc {:?} with id {}", alloc, id);
|
|
|
|
self.alloc_map.insert(id, alloc.clone());
|
|
|
|
self.dedup.insert(alloc, id);
|
2018-05-02 06:03:06 +02:00
|
|
|
id
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
pub fn create_static_alloc(&mut self, static_id: DefId) -> AllocId {
|
|
|
|
self.reserve_and_set_dedup(GlobalAlloc::Static(static_id))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Generates an `AllocId` for a function. Depending on the function type,
|
|
|
|
/// this might get deduplicated or assigned a new ID each time.
|
2018-05-02 06:03:06 +02:00
|
|
|
pub fn create_fn_alloc(&mut self, instance: Instance<'tcx>) -> AllocId {
|
2019-02-09 15:44:54 +01:00
|
|
|
// Functions cannot be identified by pointers, as asm-equal functions can get deduplicated
|
|
|
|
// by the linker (we set the "unnamed_addr" attribute for LLVM) and functions can be
|
|
|
|
// duplicated across crates.
|
|
|
|
// We thus generate a new `AllocId` for every mention of a function. This means that
|
|
|
|
// `main as fn() == main as fn()` is false, while `let x = main as fn(); x == x` is true.
|
|
|
|
// However, formatting code relies on function identity (see #58320), so we only do
|
|
|
|
// this for generic functions. Lifetime parameters are ignored.
|
2019-12-22 17:42:04 -05:00
|
|
|
let is_generic = instance.substs.into_iter().any(|kind| match kind.unpack() {
|
|
|
|
GenericArgKind::Lifetime(_) => false,
|
|
|
|
_ => true,
|
2019-02-09 15:44:54 +01:00
|
|
|
});
|
|
|
|
if is_generic {
|
2019-09-06 03:57:44 +01:00
|
|
|
// Get a fresh ID.
|
2019-02-09 15:44:54 +01:00
|
|
|
let id = self.reserve();
|
2019-05-30 13:05:05 +02:00
|
|
|
self.alloc_map.insert(id, GlobalAlloc::Function(instance));
|
2019-02-09 15:44:54 +01:00
|
|
|
id
|
|
|
|
} else {
|
2019-09-06 03:57:44 +01:00
|
|
|
// Deduplicate.
|
2019-05-30 13:05:05 +02:00
|
|
|
self.reserve_and_set_dedup(GlobalAlloc::Function(instance))
|
2019-02-09 15:44:54 +01:00
|
|
|
}
|
2018-05-02 06:03:06 +02: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.
|
|
|
|
pub fn create_memory_alloc(&mut self, mem: &'tcx Allocation) -> AllocId {
|
|
|
|
let id = self.reserve();
|
|
|
|
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]
|
2019-05-30 13:05:05 +02:00
|
|
|
pub fn get(&self, id: AllocId) -> Option<GlobalAlloc<'tcx>> {
|
|
|
|
self.alloc_map.get(&id).cloned()
|
2018-05-02 06:03:06 +02:00
|
|
|
}
|
|
|
|
|
2018-12-03 16:18:31 +01:00
|
|
|
/// Panics if the `AllocId` does not refer to an `Allocation`
|
2018-12-03 14:54:58 +01:00
|
|
|
pub fn unwrap_memory(&self, id: AllocId) -> &'tcx Allocation {
|
2018-05-02 06:03:06 +02:00
|
|
|
match self.get(id) {
|
2019-05-30 13:05:05 +02:00
|
|
|
Some(GlobalAlloc::Memory(mem)) => mem,
|
2019-09-06 03:57:44 +01:00
|
|
|
_ => bug!("expected allocation ID {} to point to memory", id),
|
2018-05-02 06:03:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-02 20:29:16 +13:00
|
|
|
/// Panics if the `AllocId` does not refer to a function
|
|
|
|
pub fn unwrap_fn(&self, id: AllocId) -> Instance<'tcx> {
|
|
|
|
match self.get(id) {
|
|
|
|
Some(GlobalAlloc::Function(instance)) => instance,
|
|
|
|
_ => bug!("expected allocation ID {} to point to a function", id),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2018-12-04 10:01:58 +01:00
|
|
|
pub fn set_alloc_id_memory(&mut self, id: AllocId, mem: &'tcx Allocation) {
|
2019-05-30 13:05:05 +02:00
|
|
|
if let Some(old) = self.alloc_map.insert(id, GlobalAlloc::Memory(mem)) {
|
2019-09-06 03:57:44 +01:00
|
|
|
bug!("tried to set allocation ID {}, but it was already existing as {:#?}", id, old);
|
2018-05-02 06:03:06 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-25 17:19:31 +02:00
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Freezes an `AllocId` created with `reserve` by pointing it at an `Allocation`. May be called
|
2018-12-03 16:18:31 +01:00
|
|
|
/// twice for the same `(AllocId, Allocation)` pair.
|
2018-12-04 10:03:21 +01:00
|
|
|
fn set_alloc_id_same_memory(&mut self, id: AllocId, mem: &'tcx Allocation) {
|
2019-05-30 13:05:05 +02:00
|
|
|
self.alloc_map.insert_same(id, GlobalAlloc::Memory(mem));
|
2018-05-25 17:19:31 +02:00
|
|
|
}
|
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(
|
|
|
|
endianness: layout::Endian,
|
|
|
|
mut target: &mut [u8],
|
|
|
|
data: u128,
|
|
|
|
) -> Result<(), io::Error> {
|
|
|
|
let len = target.len();
|
|
|
|
match endianness {
|
|
|
|
layout::Endian::Little => target.write_uint128::<LittleEndian>(data, len),
|
|
|
|
layout::Endian::Big => target.write_uint128::<BigEndian>(data, len),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-27 09:40:13 +02:00
|
|
|
#[inline]
|
2018-04-26 09:18:19 +02:00
|
|
|
pub fn read_target_uint(endianness: layout::Endian, mut source: &[u8]) -> Result<u128, io::Error> {
|
|
|
|
match endianness {
|
|
|
|
layout::Endian::Little => source.read_uint128::<LittleEndian>(source.len()),
|
|
|
|
layout::Endian::Big => source.read_uint128::<BigEndian>(source.len()),
|
|
|
|
}
|
2017-12-12 17:14:49 +01:00
|
|
|
}
|
|
|
|
|
2018-08-16 11:38:16 +02:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2018-10-22 18:21:55 +02:00
|
|
|
// Methods to facilitate working with signed integers stored in a u128
|
2018-08-16 11:38:16 +02:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Truncates `value` to `size` bits and then sign-extend it to 128 bits
|
2019-05-27 09:40:13 +02:00
|
|
|
/// (i.e., if it is negative, fill with 1's on the left).
|
|
|
|
#[inline]
|
2018-08-16 11:38:16 +02:00
|
|
|
pub fn sign_extend(value: u128, size: Size) -> u128 {
|
|
|
|
let size = size.bits();
|
2019-05-27 09:40:13 +02:00
|
|
|
if size == 0 {
|
|
|
|
// Truncated until nothing is left.
|
|
|
|
return 0;
|
|
|
|
}
|
2019-09-06 03:57:44 +01:00
|
|
|
// Sign-extend it.
|
2018-08-16 11:38:16 +02:00
|
|
|
let shift = 128 - size;
|
2019-09-06 03:57:44 +01:00
|
|
|
// Shift the unsigned value to the left, then shift back to the right as signed
|
|
|
|
// (essentially fills with FF on the left).
|
2018-08-16 11:38:16 +02:00
|
|
|
(((value << shift) as i128) >> shift) as u128
|
|
|
|
}
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Truncates `value` to `size` bits.
|
2019-05-27 09:40:13 +02:00
|
|
|
#[inline]
|
2018-08-16 11:38:16 +02:00
|
|
|
pub fn truncate(value: u128, size: Size) -> u128 {
|
|
|
|
let size = size.bits();
|
2019-05-27 09:40:13 +02:00
|
|
|
if size == 0 {
|
|
|
|
// Truncated until nothing is left.
|
|
|
|
return 0;
|
|
|
|
}
|
2018-08-16 11:38:16 +02:00
|
|
|
let shift = 128 - size;
|
2019-09-06 03:57:44 +01:00
|
|
|
// Truncate (shift left to drop out leftover values, shift right to fill with zeroes).
|
2018-08-16 11:38:16 +02:00
|
|
|
(value << shift) >> shift
|
|
|
|
}
|