Skip to content

Commit fd8beaf

Browse files
committed
add option to disable alignment checks
1 parent fbbd442 commit fd8beaf

File tree

5 files changed

+36
-3
lines changed

5 files changed

+36
-3
lines changed

src/bin/miri.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ fn main() {
128128
// Parse our arguments and split them across `rustc` and `miri`.
129129
let mut validate = true;
130130
let mut stacked_borrows = true;
131+
let mut check_alignment = true;
131132
let mut communicate = false;
132133
let mut ignore_leaks = false;
133134
let mut seed: Option<u64> = None;
@@ -152,6 +153,9 @@ fn main() {
152153
"-Zmiri-disable-stacked-borrows" => {
153154
stacked_borrows = false;
154155
}
156+
"-Zmiri-disable-alignment-check" => {
157+
check_alignment = false;
158+
}
155159
"-Zmiri-disable-isolation" => {
156160
communicate = true;
157161
}
@@ -243,6 +247,7 @@ fn main() {
243247
let miri_config = miri::MiriConfig {
244248
validate,
245249
stacked_borrows,
250+
check_alignment,
246251
communicate,
247252
ignore_leaks,
248253
excluded_env_vars,

src/eval.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ pub struct MiriConfig {
1919
pub validate: bool,
2020
/// Determines if Stacked Borrows is enabled.
2121
pub stacked_borrows: bool,
22+
/// Determines if alignment checking is enabled.
23+
pub check_alignment: bool,
2224
/// Determines if communication with the host environment is enabled.
2325
pub communicate: bool,
2426
/// Determines if memory leaks should be ignored.
@@ -40,6 +42,7 @@ impl Default for MiriConfig {
4042
MiriConfig {
4143
validate: true,
4244
stacked_borrows: true,
45+
check_alignment: true,
4346
communicate: false,
4447
ignore_leaks: false,
4548
excluded_env_vars: vec![],
@@ -72,6 +75,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
7275
config.stacked_borrows,
7376
config.tracked_pointer_tag,
7477
config.tracked_alloc_id,
78+
config.check_alignment,
7579
),
7680
);
7781
// Complete initialization.

src/machine.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,19 @@ pub struct MemoryExtra {
118118
/// An allocation ID to report when it is being allocated
119119
/// (helps for debugging memory leaks).
120120
tracked_alloc_id: Option<AllocId>,
121+
122+
/// Controls whether alignment of memory accesses is being checked.
123+
check_alignment: bool,
121124
}
122125

123126
impl MemoryExtra {
124-
pub fn new(rng: StdRng, stacked_borrows: bool, tracked_pointer_tag: Option<PtrId>, tracked_alloc_id: Option<AllocId>) -> Self {
127+
pub fn new(
128+
rng: StdRng,
129+
stacked_borrows: bool,
130+
tracked_pointer_tag: Option<PtrId>,
131+
tracked_alloc_id: Option<AllocId>,
132+
check_alignment: bool,
133+
) -> Self {
125134
let stacked_borrows = if stacked_borrows {
126135
Some(Rc::new(RefCell::new(stacked_borrows::GlobalState::new(tracked_pointer_tag))))
127136
} else {
@@ -133,6 +142,7 @@ impl MemoryExtra {
133142
extern_statics: FxHashMap::default(),
134143
rng: RefCell::new(rng),
135144
tracked_alloc_id,
145+
check_alignment,
136146
}
137147
}
138148

@@ -299,7 +309,10 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
299309

300310
const GLOBAL_KIND: Option<MiriMemoryKind> = Some(MiriMemoryKind::Global);
301311

302-
const CHECK_ALIGN: bool = true;
312+
#[inline(always)]
313+
fn enforce_alignment(memory_extra: &MemoryExtra) -> bool {
314+
memory_extra.check_alignment
315+
}
303316

304317
#[inline(always)]
305318
fn enforce_validity(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool {

tests/compile-fail/unaligned_pointers/alignment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ fn main() {
22
// miri always gives allocations the worst possible alignment, so a `u8` array is guaranteed
33
// to be at the virtual location 1 (so one byte offset from the ultimate alignemnt location 0)
44
let mut x = [0u8; 20];
5-
let x_ptr: *mut u8 = &mut x[0];
5+
let x_ptr: *mut u8 = x.as_mut_ptr();
66
let y_ptr = x_ptr as *mut u64;
77
unsafe {
88
*y_ptr = 42; //~ ERROR accessing memory with alignment 1, but alignment
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// compile-flags: -Zmiri-disable-alignment-check
2+
3+
fn main() {
4+
let mut x = [0u8; 20];
5+
let x_ptr: *mut u8 = x.as_mut_ptr();
6+
// At least one of these is definitely unaligned.
7+
unsafe {
8+
*(x_ptr as *mut u64) = 42;
9+
*(x_ptr.add(1) as *mut u64) = 42;
10+
}
11+
}

0 commit comments

Comments
 (0)