@@ -27,25 +27,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
2727 }
2828 // There are some more lang items we want to hook that CTFE does not hook (yet).
2929 if this. tcx . lang_items ( ) . align_offset_fn ( ) == Some ( instance. def . def_id ( ) ) {
30-
31- let n = {
32- let ptr = this. force_ptr ( this. read_scalar ( args[ 0 ] ) ?. not_undef ( ) ?) ?;
33- let align = this. force_bits (
34- this. read_scalar ( args[ 1 ] ) ?. not_undef ( ) ?,
35- this. pointer_size ( )
36- ) ? as usize ;
37-
38- let stride = this. memory ( ) . get ( ptr. alloc_id ) ?. align . bytes ( ) as usize ;
39- // if the allocation alignment is at least the required alignment, we use the
40- // libcore implementation
41- if stride >= align {
42- ( ( stride + ptr. offset . bytes ( ) as usize ) as * const ( ) )
43- . align_offset ( align) as u128
44- } else {
45- u128:: max_value ( )
46- }
47- } ;
48-
30+ let n = this. align_offset ( args[ 0 ] , args[ 1 ] ) ?;
4931 let dest = dest. unwrap ( ) ;
5032 let n = this. truncate ( n, dest. layout ) ;
5133 this. write_scalar ( Scalar :: from_uint ( n, dest. layout . size ) , dest) ?;
@@ -65,4 +47,33 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
6547 // Otherwise, load the MIR.
6648 Ok ( Some ( this. load_mir ( instance. def , None ) ?) )
6749 }
50+
51+ fn align_offset (
52+ & mut self ,
53+ ptr_op : OpTy < ' tcx , Tag > ,
54+ align_op : OpTy < ' tcx , Tag >
55+ ) -> InterpResult < ' tcx , u128 > {
56+ let this = self . eval_context_mut ( ) ;
57+
58+ let req_align = this. force_bits (
59+ this. read_scalar ( align_op) ?. not_undef ( ) ?,
60+ this. pointer_size ( )
61+ ) ? as usize ;
62+
63+ let ptr_scalar = this. read_scalar ( ptr_op) ?. not_undef ( ) ?;
64+
65+ if let Scalar :: Ptr ( ptr) = ptr_scalar {
66+ let cur_align = this. memory ( ) . get ( ptr. alloc_id ) ?. align . bytes ( ) as usize ;
67+ if cur_align < req_align {
68+ return Ok ( u128:: max_value ( ) ) ;
69+ }
70+ }
71+
72+ // if the allocation alignment is at least the required alignment or if the pointer is an
73+ // integer, we use the libcore implementation
74+ Ok (
75+ ( this. force_bits ( ptr_scalar, this. pointer_size ( ) ) ? as * const i8 )
76+ . align_offset ( req_align) as u128
77+ )
78+ }
6879}
0 commit comments