Skip to content

Commit c4af588

Browse files
committed
Correctly align all allocs
Fixes rust-lang#348
1 parent 2f0093b commit c4af588

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

build_sysroot/Cargo.toml

-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,3 @@ rustc-std-workspace-alloc = { path = "./rustc-std-workspace-alloc" }
1717

1818
[profile.release]
1919
debug = true
20-
21-
[profile.dev]
22-
# FIXME correctly align constants, so that copy_nonoverlapping doesn't complain about alignment
23-
debug-assertions = false

example/mini_core.rs

+15
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,21 @@ impl Sub for i16 {
186186
}
187187
}
188188

189+
#[lang = "rem"]
190+
pub trait Rem<RHS = Self> {
191+
type Output;
192+
193+
fn rem(self, rhs: RHS) -> Self::Output;
194+
}
195+
196+
impl Rem for usize {
197+
type Output = Self;
198+
199+
fn rem(self, rhs: Self) -> Self {
200+
self % rhs
201+
}
202+
}
203+
189204
#[lang = "bitor"]
190205
pub trait BitOr<RHS = Self> {
191206
type Output;

example/mini_core_hello_world.rs

+4
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ fn main() {
134134

135135
call_return_u128_pair();
136136

137+
let slice = &[0, 1] as &[i32];
138+
let slice_ptr = slice as *const [i32] as *const i32;
139+
assert_eq!(slice_ptr as usize % 4, 0);
140+
137141
//return;
138142

139143
unsafe {

src/constant.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::borrow::Cow;
33
use rustc::mir::interpret::{
44
read_target_uint, AllocId, GlobalAlloc, Allocation, ConstValue, InterpResult, GlobalId, Scalar,
55
};
6-
use rustc::ty::Const;
6+
use rustc::ty::{Const, layout::Align};
77
use rustc_mir::interpret::{
88
InterpCx, ImmTy, Machine, Memory, MemoryKind, OpTy, PlaceTy,
99
StackPopCleanup,
@@ -170,13 +170,13 @@ fn trans_const_place<'a, 'tcx: 'a>(
170170
//println!("const value: {:?} allocation: {:?}", value, alloc);
171171
let alloc_id = fx.tcx.alloc_map.lock().create_memory_alloc(alloc);
172172
fx.constants.todo.insert(TodoItem::Alloc(alloc_id));
173-
let data_id = data_id_for_alloc_id(fx.module, alloc_id);
173+
let data_id = data_id_for_alloc_id(fx.module, alloc_id, alloc.align);
174174
cplace_for_dataid(fx, const_.ty, data_id)
175175
}
176176

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

@@ -245,8 +245,8 @@ fn define_all_allocs(
245245
let (data_id, alloc) = match todo_item {
246246
TodoItem::Alloc(alloc_id) => {
247247
//println!("alloc_id {}", alloc_id);
248-
let data_id = data_id_for_alloc_id(module, alloc_id);
249248
let alloc = memory.get(alloc_id).unwrap();
249+
let data_id = data_id_for_alloc_id(module, alloc_id, alloc.align);
250250
(data_id, alloc)
251251
}
252252
TodoItem::Static(def_id) => {
@@ -302,7 +302,7 @@ fn define_all_allocs(
302302
}
303303
GlobalAlloc::Memory(_) => {
304304
cx.todo.insert(TodoItem::Alloc(reloc));
305-
data_id_for_alloc_id(module, reloc)
305+
data_id_for_alloc_id(module, reloc, alloc.align)
306306
}
307307
GlobalAlloc::Static(def_id) => {
308308
cx.todo.insert(TodoItem::Static(def_id));

0 commit comments

Comments
 (0)