From c4af588f72c317a015f6e26bb7d25ae288096b91 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sat, 30 Mar 2019 18:22:43 +0100 Subject: [PATCH] Correctly align all allocs Fixes #348 --- build_sysroot/Cargo.toml | 4 ---- example/mini_core.rs | 15 +++++++++++++++ example/mini_core_hello_world.rs | 4 ++++ src/constant.rs | 12 ++++++------ 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/build_sysroot/Cargo.toml b/build_sysroot/Cargo.toml index 90d245522f6..e7d90b730eb 100644 --- a/build_sysroot/Cargo.toml +++ b/build_sysroot/Cargo.toml @@ -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 diff --git a/example/mini_core.rs b/example/mini_core.rs index 68e056b905f..f57f1ff635d 100644 --- a/example/mini_core.rs +++ b/example/mini_core.rs @@ -186,6 +186,21 @@ impl Sub for i16 { } } +#[lang = "rem"] +pub trait Rem { + 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 { type Output; diff --git a/example/mini_core_hello_world.rs b/example/mini_core_hello_world.rs index 380bc487bcc..48ae80baaad 100644 --- a/example/mini_core_hello_world.rs +++ b/example/mini_core_hello_world.rs @@ -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 { diff --git a/src/constant.rs b/src/constant.rs index c8fb2767f9a..b8f7d039855 100644 --- a/src/constant.rs +++ b/src/constant.rs @@ -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, @@ -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, alloc_id: AllocId) -> DataId { +fn data_id_for_alloc_id(module: &mut Module, 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() } @@ -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) => { @@ -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));