Skip to content

Commit 38748fa

Browse files
committed
refactor away IntegerPtr
1 parent 875a454 commit 38748fa

File tree

10 files changed

+24
-44
lines changed

10 files changed

+24
-44
lines changed

src/error.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use syntax::codemap::Span;
1010
pub enum EvalError<'tcx> {
1111
FunctionPointerTyMismatch(&'tcx BareFnTy<'tcx>, &'tcx BareFnTy<'tcx>),
1212
DanglingPointerDeref,
13-
ZstAllocAccess,
13+
InvalidMemoryAccess,
1414
InvalidFunctionPointer,
1515
InvalidBool,
1616
InvalidDiscriminant,
@@ -20,7 +20,6 @@ pub enum EvalError<'tcx> {
2020
allocation_size: usize,
2121
},
2222
ReadPointerAsBytes,
23-
ReadBytesAsPointer,
2423
InvalidPointerMath,
2524
ReadUndefBytes,
2625
InvalidBoolOp(mir::BinOp),
@@ -54,8 +53,8 @@ impl<'tcx> Error for EvalError<'tcx> {
5453
match *self {
5554
EvalError::FunctionPointerTyMismatch(..) =>
5655
"tried to call a function through a function pointer of a different type",
57-
EvalError::ZstAllocAccess =>
58-
"tried to access the ZST allocation",
56+
EvalError::InvalidMemoryAccess =>
57+
"tried to access memory through an invalid pointer",
5958
EvalError::DanglingPointerDeref =>
6059
"dangling pointer was dereferenced",
6160
EvalError::InvalidFunctionPointer =>
@@ -68,8 +67,6 @@ impl<'tcx> Error for EvalError<'tcx> {
6867
"pointer offset outside bounds of allocation",
6968
EvalError::ReadPointerAsBytes =>
7069
"a raw memory access tried to access part of a pointer value as raw bytes",
71-
EvalError::ReadBytesAsPointer =>
72-
"attempted to interpret some raw bytes as a pointer address",
7370
EvalError::InvalidPointerMath =>
7471
"attempted to do math or a comparison on pointers into different allocations",
7572
EvalError::ReadUndefBytes =>

src/interpreter/cast.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
2525
U16(u) => self.cast_const_int(u as u64, ty, false),
2626
U32(u) => self.cast_const_int(u as u64, ty, false),
2727
Char(c) => self.cast_const_int(c as u64, ty, false),
28-
U64(u) |
29-
IntegerPtr(u) => self.cast_const_int(u, ty, false),
28+
U64(u) => self.cast_const_int(u, ty, false),
3029
FnPtr(ptr) |
3130
Ptr(ptr) => self.cast_ptr(ptr, ty),
3231
}
@@ -74,7 +73,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
7473
ty::TyFloat(ast::FloatTy::F64) => Ok(F64(v as f64)),
7574
ty::TyFloat(ast::FloatTy::F32) if negative => Ok(F32(v as i64 as f32)),
7675
ty::TyFloat(ast::FloatTy::F32) => Ok(F32(v as f32)),
77-
ty::TyRawPtr(_) => Ok(IntegerPtr(v)),
76+
ty::TyRawPtr(_) => Ok(Ptr(Pointer::from_int(v as usize))),
7877
ty::TyChar if v as u8 as u64 == v => Ok(Char(v as u8 as char)),
7978
ty::TyChar => Err(EvalError::InvalidChar(v)),
8079
_ => Err(EvalError::Unimplemented(format!("int to {:?} cast", ty))),

src/interpreter/mod.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -1064,13 +1064,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
10641064
&ty::TyRef(_, ty::TypeAndMut { ty, .. }) |
10651065
&ty::TyRawPtr(ty::TypeAndMut { ty, .. }) => {
10661066
if self.type_is_sized(ty) {
1067-
match self.memory.read_ptr(ptr) {
1068-
Ok(p) => PrimVal::Ptr(p),
1069-
Err(EvalError::ReadBytesAsPointer) => {
1070-
PrimVal::IntegerPtr(self.memory.read_usize(ptr)?)
1071-
}
1072-
Err(e) => return Err(e),
1073-
}
1067+
PrimVal::Ptr(self.memory.read_ptr(ptr)?)
10741068
} else {
10751069
bug!("primitive read of fat pointer type: {:?}", ty);
10761070
}

src/interpreter/terminator/intrinsics.rs

+3-12
Original file line numberDiff line numberDiff line change
@@ -132,18 +132,9 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
132132
let ptr_arg = args_ptrs[0];
133133
let offset = self.memory.read_isize(args_ptrs[1])?;
134134

135-
match self.memory.read_ptr(ptr_arg) {
136-
Ok(ptr) => {
137-
let result_ptr = ptr.offset(offset as isize * pointee_size);
138-
self.memory.write_ptr(dest, result_ptr)?;
139-
}
140-
Err(EvalError::ReadBytesAsPointer) => {
141-
let addr = self.memory.read_isize(ptr_arg)?;
142-
let result_addr = addr + offset * pointee_size as i64;
143-
self.memory.write_isize(dest, result_addr)?;
144-
}
145-
Err(e) => return Err(e),
146-
}
135+
let ptr = self.memory.read_ptr(ptr_arg)?;
136+
let result_ptr = ptr.offset(offset as isize * pointee_size);
137+
self.memory.write_ptr(dest, result_ptr)?;
147138
}
148139

149140
"overflowing_sub" => {

src/memory.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ impl Pointer {
5555
pub fn points_to_zst(&self) -> bool {
5656
self.alloc_id == ZST_ALLOC_ID
5757
}
58+
pub fn from_int(i: usize) -> Self {
59+
Pointer {
60+
alloc_id: ZST_ALLOC_ID,
61+
offset: i,
62+
}
63+
}
5864
fn zst_ptr() -> Self {
5965
Pointer {
6066
alloc_id: ZST_ALLOC_ID,
@@ -279,7 +285,7 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
279285
Some(alloc) => Ok(alloc),
280286
None => match self.functions.get(&id) {
281287
Some(_) => Err(EvalError::DerefFunctionPointer),
282-
None if id == ZST_ALLOC_ID => Err(EvalError::ZstAllocAccess),
288+
None if id == ZST_ALLOC_ID => Err(EvalError::InvalidMemoryAccess),
283289
None => Err(EvalError::DanglingPointerDeref),
284290
}
285291
}
@@ -291,7 +297,7 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
291297
Some(alloc) => Ok(alloc),
292298
None => match self.functions.get(&id) {
293299
Some(_) => Err(EvalError::DerefFunctionPointer),
294-
None if id == ZST_ALLOC_ID => Err(EvalError::ZstAllocAccess),
300+
None if id == ZST_ALLOC_ID => Err(EvalError::InvalidMemoryAccess),
295301
None => Err(EvalError::DanglingPointerDeref),
296302
}
297303
}
@@ -511,7 +517,7 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
511517
let alloc = self.get(ptr.alloc_id)?;
512518
match alloc.relocations.get(&ptr.offset) {
513519
Some(&alloc_id) => Ok(Pointer { alloc_id: alloc_id, offset: offset }),
514-
None => Err(EvalError::ReadBytesAsPointer),
520+
None => Ok(Pointer::from_int(offset)),
515521
}
516522
}
517523

@@ -522,7 +528,6 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
522528
}
523529

524530
pub fn write_primval(&mut self, ptr: Pointer, val: PrimVal) -> EvalResult<'tcx, ()> {
525-
let pointer_size = self.pointer_size();
526531
match val {
527532
PrimVal::Bool(b) => self.write_bool(ptr, b),
528533
PrimVal::I8(n) => self.write_int(ptr, n as i64, 1),
@@ -534,7 +539,6 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
534539
PrimVal::U32(n) => self.write_uint(ptr, n as u64, 4),
535540
PrimVal::U64(n) => self.write_uint(ptr, n as u64, 8),
536541
PrimVal::Char(c) => self.write_uint(ptr, c as u64, 4),
537-
PrimVal::IntegerPtr(n) => self.write_uint(ptr, n as u64, pointer_size),
538542
PrimVal::F32(f) => self.write_f32(ptr, f),
539543
PrimVal::F64(f) => self.write_f64(ptr, f),
540544
PrimVal::FnPtr(p) |

src/primval.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ pub enum PrimVal {
1414

1515
Ptr(Pointer),
1616
FnPtr(Pointer),
17-
IntegerPtr(u64),
1817
Char(char),
1918

2019
F32(f32), F64(f64),
@@ -209,14 +208,8 @@ pub fn binary_op<'tcx>(bin_op: mir::BinOp, left: PrimVal, right: PrimVal) -> Eva
209208
})
210209
}
211210

212-
(IntegerPtr(l), IntegerPtr(r)) => int_binops!(IntegerPtr, l, r),
213-
214-
(Ptr(_), IntegerPtr(_)) |
215-
(IntegerPtr(_), Ptr(_)) |
216211
(FnPtr(_), Ptr(_)) |
217-
(Ptr(_), FnPtr(_)) |
218-
(FnPtr(_), IntegerPtr(_)) |
219-
(IntegerPtr(_), FnPtr(_)) =>
212+
(Ptr(_), FnPtr(_)) =>
220213
unrelated_ptr_ops(bin_op)?,
221214

222215
(FnPtr(l_ptr), FnPtr(r_ptr)) => match bin_op {
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
fn main() {
2-
let x: i32 = unsafe { *std::ptr::null() }; //~ ERROR: attempted to interpret some raw bytes as a pointer address
2+
let x: i32 = unsafe { *std::ptr::null() }; //~ ERROR: tried to access memory through an invalid pointer
33
panic!("this should never print: {}", x);
44
}
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
fn main() {
22
let p = 42 as *const i32;
3-
let x = unsafe { *p }; //~ ERROR: attempted to interpret some raw bytes as a pointer address
3+
let x = unsafe { *p }; //~ ERROR: tried to access memory through an invalid pointer
44
panic!("this should never print: {}", x);
55
}

tests/compile-fail/zst.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
fn main() {
22
let x = &() as *const () as *const i32;
3-
let _ = unsafe { *x }; //~ ERROR: tried to access the ZST allocation
3+
let _ = unsafe { *x }; //~ ERROR: tried to access memory through an invalid pointer
44
}

tests/run-pass/zst.rs

+2
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@ fn main() {
2121
assert_eq!(use_zst(), A);
2222
assert_eq!(&A as *const A as *const (), &() as *const _);
2323
assert_eq!(&A as *const A, &A as *const A);
24+
let x = 42 as *mut ();
25+
unsafe { *x = (); }
2426
}

0 commit comments

Comments
 (0)