1
- use std:: convert:: TryFrom ;
2
-
3
1
use log:: trace;
4
2
5
3
use rustc_middle:: { mir, ty:: Ty } ;
6
- use rustc_target:: abi:: { LayoutOf , Size } ;
7
4
8
5
use crate :: * ;
9
6
@@ -16,13 +13,6 @@ pub trait EvalContextExt<'tcx> {
16
13
) -> InterpResult < ' tcx , ( Scalar < Tag > , bool , Ty < ' tcx > ) > ;
17
14
18
15
fn ptr_eq ( & self , left : Scalar < Tag > , right : Scalar < Tag > ) -> InterpResult < ' tcx , bool > ;
19
-
20
- fn pointer_offset_inbounds (
21
- & self ,
22
- ptr : Scalar < Tag > ,
23
- pointee_ty : Ty < ' tcx > ,
24
- offset : i64 ,
25
- ) -> InterpResult < ' tcx , Scalar < Tag > > ;
26
16
}
27
17
28
18
impl < ' mir , ' tcx > EvalContextExt < ' tcx > for super :: MiriEvalContext < ' mir , ' tcx > {
@@ -71,7 +61,7 @@ impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {
71
61
Offset => {
72
62
let pointee_ty =
73
63
left. layout . ty . builtin_deref ( true ) . expect ( "Offset called on non-ptr type" ) . ty ;
74
- let ptr = self . pointer_offset_inbounds (
64
+ let ptr = self . ptr_offset_inbounds (
75
65
left. to_scalar ( ) ?,
76
66
pointee_ty,
77
67
right. to_scalar ( ) ?. to_machine_isize ( self ) ?,
@@ -91,38 +81,4 @@ impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {
91
81
let right = self . force_bits ( right, size) ?;
92
82
Ok ( left == right)
93
83
}
94
-
95
- /// Raises an error if the offset moves the pointer outside of its allocation.
96
- /// For integers, we consider each of them their own tiny allocation of size 0,
97
- /// so offset-by-0 is okay for them -- except for NULL, which we rule out entirely.
98
- fn pointer_offset_inbounds (
99
- & self ,
100
- ptr : Scalar < Tag > ,
101
- pointee_ty : Ty < ' tcx > ,
102
- offset : i64 ,
103
- ) -> InterpResult < ' tcx , Scalar < Tag > > {
104
- let pointee_size = i64:: try_from ( self . layout_of ( pointee_ty) ?. size . bytes ( ) ) . unwrap ( ) ;
105
- let offset = offset. checked_mul ( pointee_size) . ok_or_else ( || {
106
- err_ub_format ! ( "overflow during offset comutation for inbounds pointer arithmetic" )
107
- } ) ?;
108
- // We do this first, to rule out overflows.
109
- let offset_ptr = ptr. ptr_signed_offset ( offset, self ) ?;
110
- // What we need to check is that starting at `min(ptr, offset_ptr)`,
111
- // we could do an access of size `abs(offset)`. Alignment does not matter.
112
- let ( min_ptr, abs_offset) = if offset >= 0 {
113
- ( ptr, u64:: try_from ( offset) . unwrap ( ) )
114
- } else {
115
- // Negative offset.
116
- // If the negation overflows, the result will be negative so the try_from will fail.
117
- ( offset_ptr, u64:: try_from ( -offset) . unwrap ( ) )
118
- } ;
119
- self . memory . check_ptr_access_align (
120
- min_ptr,
121
- Size :: from_bytes ( abs_offset) ,
122
- None ,
123
- CheckInAllocMsg :: InboundsTest ,
124
- ) ?;
125
- // That's it!
126
- Ok ( offset_ptr)
127
- }
128
84
}
0 commit comments