Skip to content

Commit dd7e34c

Browse files
committed
separate bounds-check from alignment check
1 parent 6a5731b commit dd7e34c

File tree

7 files changed

+13
-24
lines changed

7 files changed

+13
-24
lines changed

src/borrow_tracker/stacked_borrows/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use log::trace;
1414
use rustc_data_structures::fx::FxHashSet;
1515
use rustc_middle::mir::{Mutability, RetagKind};
1616
use rustc_middle::ty::{self, layout::HasParamEnv, Ty};
17-
use rustc_target::abi::{Abi, Align, Size};
17+
use rustc_target::abi::{Abi, Size};
1818

1919
use crate::borrow_tracker::{
2020
stacked_borrows::diagnostics::{AllocHistory, DiagnosticCx, DiagnosticCxBuilder},
@@ -616,7 +616,7 @@ trait EvalContextPrivExt<'mir: 'ecx, 'tcx: 'mir, 'ecx>: crate::MiriInterpCxExt<'
616616
) -> InterpResult<'tcx, Option<Provenance>> {
617617
let this = self.eval_context_mut();
618618
// Ensure we bail out if the pointer goes out-of-bounds (see miri#1050).
619-
this.check_ptr_access_align(place.ptr(), size, Align::ONE, CheckInAllocMsg::InboundsTest)?;
619+
this.check_ptr_access(place.ptr(), size, CheckInAllocMsg::InboundsTest)?;
620620

621621
// It is crucial that this gets called on all code paths, to ensure we track tag creation.
622622
let log_creation = |this: &MiriInterpCx<'mir, 'tcx>,

src/borrow_tracker/tree_borrows/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use log::trace;
22

3-
use rustc_target::abi::{Abi, Align, Size};
3+
use rustc_target::abi::{Abi, Size};
44

55
use crate::borrow_tracker::{
66
AccessKind, GlobalState, GlobalStateInner, ProtectorKind, RetagFields,
@@ -206,10 +206,9 @@ trait EvalContextPrivExt<'mir: 'ecx, 'tcx: 'mir, 'ecx>: crate::MiriInterpCxExt<'
206206
// Make sure the new permission makes sense as the initial permission of a fresh tag.
207207
assert!(new_perm.initial_state.is_initial());
208208
// Ensure we bail out if the pointer goes out-of-bounds (see miri#1050).
209-
this.check_ptr_access_align(
209+
this.check_ptr_access(
210210
place.ptr(),
211211
ptr_size,
212-
Align::ONE,
213212
CheckInAllocMsg::InboundsTest,
214213
)?;
215214

src/concurrency/data_race.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1017,11 +1017,9 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: MiriInterpCxExt<'mir, 'tcx> {
10171017
// even if the type they wrap would be less aligned (e.g. AtomicU64 on 32bit must
10181018
// be 8-aligned).
10191019
let align = Align::from_bytes(place.layout.size.bytes()).unwrap();
1020-
this.check_ptr_access_align(
1020+
this.check_ptr_align(
10211021
place.ptr(),
1022-
place.layout.size,
10231022
align,
1024-
CheckInAllocMsg::MemoryAccessTest,
10251023
)?;
10261024
// Ensure the allocation is mutable. Even failing (read-only) compare_exchange need mutable
10271025
// memory on many targets (i.e., they segfault if taht memory is mapped read-only), and

src/helpers.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
785785
loop {
786786
// FIXME: We are re-getting the allocation each time around the loop.
787787
// Would be nice if we could somehow "extend" an existing AllocRange.
788-
let alloc = this.get_ptr_alloc(ptr.offset(len, this)?, size1, Align::ONE)?.unwrap(); // not a ZST, so we will get a result
788+
let alloc = this.get_ptr_alloc(ptr.offset(len, this)?, size1)?.unwrap(); // not a ZST, so we will get a result
789789
let byte = alloc.read_integer(alloc_range(Size::ZERO, size1))?.to_u8()?;
790790
if byte == 0 {
791791
break;
@@ -825,13 +825,13 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
825825
fn read_wide_str(&self, mut ptr: Pointer<Option<Provenance>>) -> InterpResult<'tcx, Vec<u16>> {
826826
let this = self.eval_context_ref();
827827
let size2 = Size::from_bytes(2);
828-
let align2 = Align::from_bytes(2).unwrap();
828+
this.check_ptr_align(ptr, Align::from_bytes(2).unwrap())?;
829829

830830
let mut wchars = Vec::new();
831831
loop {
832832
// FIXME: We are re-getting the allocation each time around the loop.
833833
// Would be nice if we could somehow "extend" an existing AllocRange.
834-
let alloc = this.get_ptr_alloc(ptr, size2, align2)?.unwrap(); // not a ZST, so we will get a result
834+
let alloc = this.get_ptr_alloc(ptr, size2)?.unwrap(); // not a ZST, so we will get a result
835835
let wchar = alloc.read_integer(alloc_range(Size::ZERO, size2))?.to_u16()?;
836836
if wchar == 0 {
837837
break;
@@ -867,8 +867,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
867867
// Store the UTF-16 string.
868868
let size2 = Size::from_bytes(2);
869869
let this = self.eval_context_mut();
870+
this.check_ptr_align(ptr, Align::from_bytes(2).unwrap())?;
870871
let mut alloc = this
871-
.get_ptr_alloc_mut(ptr, size2 * string_length, Align::from_bytes(2).unwrap())?
872+
.get_ptr_alloc_mut(ptr, size2 * string_length)?
872873
.unwrap(); // not a ZST, so we will get a result
873874
for (offset, wchar) in wide_str.iter().copied().chain(iter::once(0x0000)).enumerate() {
874875
let offset = u64::try_from(offset).unwrap();

src/shims/foreign_items.rs

-4
Original file line numberDiff line numberDiff line change
@@ -807,9 +807,7 @@ trait EvalContextExtPriv<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
807807

808808
this.mem_copy(
809809
ptr_src,
810-
Align::ONE,
811810
ptr_dest,
812-
Align::ONE,
813811
Size::from_bytes(n),
814812
true,
815813
)?;
@@ -830,9 +828,7 @@ trait EvalContextExtPriv<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
830828
let n = this.read_c_str(ptr_src)?.len().checked_add(1).unwrap();
831829
this.mem_copy(
832830
ptr_src,
833-
Align::ONE,
834831
ptr_dest,
835-
Align::ONE,
836832
Size::from_bytes(n),
837833
true,
838834
)?;

src/shims/unix/fs.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use log::trace;
1313

1414
use rustc_data_structures::fx::FxHashMap;
1515
use rustc_middle::ty::TyCtxt;
16-
use rustc_target::abi::{Align, Size};
16+
use rustc_target::abi::Size;
1717

1818
use crate::shims::os_str::bytes_to_os_str;
1919
use crate::*;
@@ -756,10 +756,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
756756
trace!("Reading from FD {}, size {}", fd, count);
757757

758758
// Check that the *entire* buffer is actually valid memory.
759-
this.check_ptr_access_align(
759+
this.check_ptr_access(
760760
buf,
761761
Size::from_bytes(count),
762-
Align::ONE,
763762
CheckInAllocMsg::MemoryAccessTest,
764763
)?;
765764

@@ -810,10 +809,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
810809
// Isolation check is done via `FileDescriptor` trait.
811810

812811
// Check that the *entire* buffer is actually valid memory.
813-
this.check_ptr_access_align(
812+
this.check_ptr_access(
814813
buf,
815814
Size::from_bytes(count),
816-
Align::ONE,
817815
CheckInAllocMsg::MemoryAccessTest,
818816
)?;
819817

src/shims/x86/sse3.rs

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use rustc_middle::mir;
22
use rustc_span::Symbol;
3-
use rustc_target::abi::Align;
43
use rustc_target::spec::abi::Abi;
54

65
use super::horizontal_bin_op;
@@ -76,9 +75,7 @@ pub(super) trait EvalContextExt<'mir, 'tcx: 'mir>:
7675

7776
this.mem_copy(
7877
src_ptr,
79-
Align::ONE,
8078
dest.ptr(),
81-
Align::ONE,
8279
dest.layout.size,
8380
/*nonoverlapping*/ true,
8481
)?;

0 commit comments

Comments
 (0)