Skip to content

Commit

Permalink
Correctly align all allocs
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 committed Aug 1, 2019
1 parent 2f0093b commit c4af588
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
4 changes: 0 additions & 4 deletions build_sysroot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,3 @@ rustc-std-workspace-alloc = { path = "./rustc-std-workspace-alloc" }

[profile.release]
debug = true

[profile.dev]
# FIXME correctly align constants, so that copy_nonoverlapping doesn't complain about alignment
debug-assertions = false
15 changes: 15 additions & 0 deletions example/mini_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,21 @@ impl Sub for i16 {
}
}

#[lang = "rem"]
pub trait Rem<RHS = Self> {
type Output;

fn rem(self, rhs: RHS) -> Self::Output;
}

impl Rem for usize {
type Output = Self;

fn rem(self, rhs: Self) -> Self {
self % rhs
}
}

#[lang = "bitor"]
pub trait BitOr<RHS = Self> {
type Output;
Expand Down
4 changes: 4 additions & 0 deletions example/mini_core_hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ fn main() {

call_return_u128_pair();

let slice = &[0, 1] as &[i32];
let slice_ptr = slice as *const [i32] as *const i32;
assert_eq!(slice_ptr as usize % 4, 0);

//return;

unsafe {
Expand Down
12 changes: 6 additions & 6 deletions src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::borrow::Cow;
use rustc::mir::interpret::{
read_target_uint, AllocId, GlobalAlloc, Allocation, ConstValue, InterpResult, GlobalId, Scalar,
};
use rustc::ty::Const;
use rustc::ty::{Const, layout::Align};
use rustc_mir::interpret::{
InterpCx, ImmTy, Machine, Memory, MemoryKind, OpTy, PlaceTy,
StackPopCleanup,
Expand Down Expand Up @@ -170,13 +170,13 @@ fn trans_const_place<'a, 'tcx: 'a>(
//println!("const value: {:?} allocation: {:?}", value, alloc);
let alloc_id = fx.tcx.alloc_map.lock().create_memory_alloc(alloc);
fx.constants.todo.insert(TodoItem::Alloc(alloc_id));
let data_id = data_id_for_alloc_id(fx.module, alloc_id);
let data_id = data_id_for_alloc_id(fx.module, alloc_id, alloc.align);
cplace_for_dataid(fx, const_.ty, data_id)
}

fn data_id_for_alloc_id(module: &mut Module<impl Backend>, alloc_id: AllocId) -> DataId {
fn data_id_for_alloc_id<B: Backend>(module: &mut Module<B>, alloc_id: AllocId, align: Align) -> DataId {
module
.declare_data(&format!("__alloc_{}", alloc_id.0), Linkage::Local, false, None)
.declare_data(&format!("__alloc_{}", alloc_id.0), Linkage::Local, false, Some(align.bytes() as u8))
.unwrap()
}

Expand Down Expand Up @@ -245,8 +245,8 @@ fn define_all_allocs(
let (data_id, alloc) = match todo_item {
TodoItem::Alloc(alloc_id) => {
//println!("alloc_id {}", alloc_id);
let data_id = data_id_for_alloc_id(module, alloc_id);
let alloc = memory.get(alloc_id).unwrap();
let data_id = data_id_for_alloc_id(module, alloc_id, alloc.align);
(data_id, alloc)
}
TodoItem::Static(def_id) => {
Expand Down Expand Up @@ -302,7 +302,7 @@ fn define_all_allocs(
}
GlobalAlloc::Memory(_) => {
cx.todo.insert(TodoItem::Alloc(reloc));
data_id_for_alloc_id(module, reloc)
data_id_for_alloc_id(module, reloc, alloc.align)
}
GlobalAlloc::Static(def_id) => {
cx.todo.insert(TodoItem::Static(def_id));
Expand Down

0 comments on commit c4af588

Please sign in to comment.