fix types in env shim to avoid ptr-int transmutes

This commit is contained in:
Ralf Jung 2022-03-26 14:29:58 -04:00
parent ede470e1fc
commit 552b77e3b9
3 changed files with 20 additions and 15 deletions

View File

@ -10,13 +10,14 @@ use std::time::Instant;
use rand::rngs::StdRng;
use rand::SeedableRng;
use rustc_ast::ast::Mutability;
use rustc_data_structures::fx::FxHashMap;
use rustc_middle::{
mir,
ty::{
self,
layout::{LayoutCx, LayoutError, LayoutOf, TyAndLayout},
Instance, TyCtxt,
Instance, TyCtxt, TypeAndMut,
},
};
use rustc_span::def_id::{CrateNum, DefId};
@ -269,19 +270,23 @@ pub struct PrimitiveLayouts<'tcx> {
pub u32: TyAndLayout<'tcx>,
pub usize: TyAndLayout<'tcx>,
pub bool: TyAndLayout<'tcx>,
pub mut_raw_ptr: TyAndLayout<'tcx>,
}
impl<'mir, 'tcx: 'mir> PrimitiveLayouts<'tcx> {
fn new(layout_cx: LayoutCx<'tcx, TyCtxt<'tcx>>) -> Result<Self, LayoutError<'tcx>> {
let tcx = layout_cx.tcx;
let mut_raw_ptr = tcx.mk_ptr(TypeAndMut { ty: tcx.types.unit, mutbl: Mutability::Mut });
Ok(Self {
unit: layout_cx.layout_of(layout_cx.tcx.mk_unit())?,
i8: layout_cx.layout_of(layout_cx.tcx.types.i8)?,
i32: layout_cx.layout_of(layout_cx.tcx.types.i32)?,
isize: layout_cx.layout_of(layout_cx.tcx.types.isize)?,
u8: layout_cx.layout_of(layout_cx.tcx.types.u8)?,
u32: layout_cx.layout_of(layout_cx.tcx.types.u32)?,
usize: layout_cx.layout_of(layout_cx.tcx.types.usize)?,
bool: layout_cx.layout_of(layout_cx.tcx.types.bool)?,
unit: layout_cx.layout_of(tcx.mk_unit())?,
i8: layout_cx.layout_of(tcx.types.i8)?,
i32: layout_cx.layout_of(tcx.types.i32)?,
isize: layout_cx.layout_of(tcx.types.isize)?,
u8: layout_cx.layout_of(tcx.types.u8)?,
u32: layout_cx.layout_of(tcx.types.u32)?,
usize: layout_cx.layout_of(tcx.types.usize)?,
bool: layout_cx.layout_of(tcx.types.bool)?,
mut_raw_ptr: layout_cx.layout_of(mut_raw_ptr)?,
})
}
}

View File

@ -1,7 +1,7 @@
use crate::*;
use rustc_ast::ast::Mutability;
use rustc_middle::ty::layout::LayoutOf as _;
use rustc_middle::ty::{self, Instance, TypeAndMut};
use rustc_middle::ty::{self, Instance};
use rustc_span::{BytePos, Loc, Symbol};
use rustc_target::{abi::Size, spec::abi::Abi};
use std::convert::TryInto as _;
@ -71,8 +71,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
let len: u64 = ptrs.len().try_into().unwrap();
let ptr_ty = tcx.mk_ptr(TypeAndMut { ty: tcx.types.unit, mutbl: Mutability::Mut });
let ptr_ty = this.machine.layouts.mut_raw_ptr.ty;
let array_layout = this.layout_of(tcx.mk_array(ptr_ty, len)).unwrap();
match flags {

View File

@ -440,7 +440,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
} else {
// No `environ` allocated yet, let's do that.
// This is memory backing an extern static, hence `ExternStatic`, not `Env`.
let layout = this.machine.layouts.usize;
let layout = this.machine.layouts.mut_raw_ptr;
let place = this.allocate(layout, MiriMemoryKind::ExternStatic.into())?;
this.machine.env_vars.environ = Some(place);
}
@ -452,8 +452,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
vars.push(Pointer::null());
// Make an array with all these pointers inside Miri.
let tcx = this.tcx;
let vars_layout =
this.layout_of(tcx.mk_array(tcx.types.usize, u64::try_from(vars.len()).unwrap()))?;
let vars_layout = this.layout_of(
tcx.mk_array(this.machine.layouts.mut_raw_ptr.ty, u64::try_from(vars.len()).unwrap()),
)?;
let vars_place = this.allocate(vars_layout, MiriMemoryKind::Runtime.into())?;
for (idx, var) in vars.into_iter().enumerate() {
let place = this.mplace_field(&vars_place, idx)?;