@@ -351,7 +351,8 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
351
351
let global_value = self . globals . get_mut ( & id)
352
352
. expect ( "global should have been cached (static)" ) ;
353
353
match global_value. value {
354
- Value :: ByRef ( ptr) => self . memory . mark_static_initalized ( ptr. alloc_id , mutable) ?,
354
+ // FIXME: to_ptr()? might be too extreme here, static zsts might reach this under certain conditions
355
+ Value :: ByRef ( ptr) => self . memory . mark_static_initalized ( ptr. to_ptr ( ) ?. alloc_id , mutable) ?,
355
356
Value :: ByVal ( val) => if let PrimVal :: Ptr ( ptr) = val {
356
357
self . memory . mark_inner_allocation ( ptr. alloc_id , mutable) ?;
357
358
} ,
@@ -409,6 +410,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
409
410
pub fn deallocate_local ( & mut self , local : Option < Value > ) -> EvalResult < ' tcx > {
410
411
if let Some ( Value :: ByRef ( ptr) ) = local {
411
412
trace ! ( "deallocating local" ) ;
413
+ let ptr = ptr. to_ptr ( ) ?;
412
414
self . memory . dump_alloc ( ptr. alloc_id ) ;
413
415
match self . memory . deallocate ( ptr) {
414
416
// We could alternatively check whether the alloc_id is static before calling
@@ -693,26 +695,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
693
695
694
696
// Check alignment and non-NULLness.
695
697
let ( _, align) = self . size_and_align_of_dst ( ty, val) ?;
696
- match ptr {
697
- PrimVal :: Ptr ( ptr) => {
698
- self . memory . check_align ( ptr, align, 0 ) ?;
699
- }
700
- PrimVal :: Bytes ( bytes) => {
701
- let v = ( ( bytes as u128 ) % ( 1 << self . memory . pointer_size ( ) ) ) as u64 ;
702
- if v == 0 {
703
- return Err ( EvalError :: InvalidNullPointerUsage ) ;
704
- }
705
- if v % align != 0 {
706
- return Err ( EvalError :: AlignmentCheckFailed {
707
- has : v % align,
708
- required : align,
709
- } ) ;
710
- }
711
- }
712
- PrimVal :: Undef => {
713
- return Err ( EvalError :: ReadUndefBytes ) ;
714
- }
715
- }
698
+ self . memory . check_align ( ptr, align, 0 ) ?;
716
699
717
700
self . write_value ( val, dest, dest_ty) ?;
718
701
}
@@ -1036,14 +1019,14 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
1036
1019
match self . stack [ frame] . locals [ local. index ( ) - 1 ] {
1037
1020
None => return Err ( EvalError :: DeadLocal ) ,
1038
1021
Some ( Value :: ByRef ( ptr) ) => {
1039
- Lvalue :: from_ptr ( ptr)
1022
+ Lvalue :: from_primval_ptr ( ptr)
1040
1023
} ,
1041
1024
Some ( val) => {
1042
1025
let ty = self . stack [ frame] . mir . local_decls [ local] . ty ;
1043
1026
let ty = self . monomorphize ( ty, self . stack [ frame] . instance . substs ) ;
1044
1027
let substs = self . stack [ frame] . instance . substs ;
1045
1028
let ptr = self . alloc_ptr_with_substs ( ty, substs) ?;
1046
- self . stack [ frame] . locals [ local. index ( ) - 1 ] = Some ( Value :: ByRef ( ptr) ) ; // it stays live
1029
+ self . stack [ frame] . locals [ local. index ( ) - 1 ] = Some ( Value :: ByRef ( PrimVal :: Ptr ( ptr) ) ) ; // it stays live
1047
1030
self . write_value_to_ptr ( val, PrimVal :: Ptr ( ptr) , ty) ?;
1048
1031
Lvalue :: from_ptr ( ptr)
1049
1032
}
@@ -1053,7 +1036,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
1053
1036
Lvalue :: Global ( cid) => {
1054
1037
let global_val = * self . globals . get ( & cid) . expect ( "global not cached" ) ;
1055
1038
match global_val. value {
1056
- Value :: ByRef ( ptr) => Lvalue :: from_ptr ( ptr) ,
1039
+ Value :: ByRef ( ptr) => Lvalue :: from_primval_ptr ( ptr) ,
1057
1040
_ => {
1058
1041
let ptr = self . alloc_ptr_with_substs ( global_val. ty , cid. instance . substs ) ?;
1059
1042
self . memory . mark_static ( ptr. alloc_id ) ;
@@ -1064,7 +1047,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
1064
1047
}
1065
1048
let lval = self . globals . get_mut ( & cid) . expect ( "already checked" ) ;
1066
1049
* lval = Global {
1067
- value : Value :: ByRef ( ptr) ,
1050
+ value : Value :: ByRef ( PrimVal :: Ptr ( ptr) ) ,
1068
1051
.. global_val
1069
1052
} ;
1070
1053
Lvalue :: from_ptr ( ptr)
@@ -1160,7 +1143,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
1160
1143
//
1161
1144
// Thus, it would be an error to replace the `ByRef` with a `ByVal`, unless we
1162
1145
// knew for certain that there were no outstanding pointers to this allocation.
1163
- self . write_value_to_ptr ( src_val, PrimVal :: Ptr ( dest_ptr) , dest_ty) ?;
1146
+ self . write_value_to_ptr ( src_val, dest_ptr, dest_ty) ?;
1164
1147
1165
1148
} else if let Value :: ByRef ( src_ptr) = src_val {
1166
1149
// If the value is not `ByRef`, then we know there are no pointers to it
@@ -1178,8 +1161,8 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
1178
1161
write_dest ( self , src_val) ?;
1179
1162
} else {
1180
1163
let dest_ptr = self . alloc_ptr ( dest_ty) ?;
1181
- self . copy ( PrimVal :: Ptr ( src_ptr) , PrimVal :: Ptr ( dest_ptr) , dest_ty) ?;
1182
- write_dest ( self , Value :: ByRef ( dest_ptr) ) ?;
1164
+ self . copy ( src_ptr, PrimVal :: Ptr ( dest_ptr) , dest_ty) ?;
1165
+ write_dest ( self , Value :: ByRef ( PrimVal :: Ptr ( dest_ptr) ) ) ?;
1183
1166
}
1184
1167
1185
1168
} else {
@@ -1197,7 +1180,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
1197
1180
dest_ty : Ty < ' tcx > ,
1198
1181
) -> EvalResult < ' tcx > {
1199
1182
match value {
1200
- Value :: ByRef ( ptr) => self . copy ( PrimVal :: Ptr ( ptr) , dest, dest_ty) ,
1183
+ Value :: ByRef ( ptr) => self . copy ( ptr, dest, dest_ty) ,
1201
1184
Value :: ByVal ( primval) => {
1202
1185
let size = self . type_size ( dest_ty) ?. expect ( "dest type must be sized" ) ;
1203
1186
self . memory . write_primval ( dest, primval, size)
@@ -1327,7 +1310,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
1327
1310
}
1328
1311
}
1329
1312
1330
- pub ( super ) fn read_value ( & mut self , ptr : Pointer , ty : Ty < ' tcx > ) -> EvalResult < ' tcx , Value > {
1313
+ pub ( super ) fn read_value ( & mut self , ptr : PrimVal , ty : Ty < ' tcx > ) -> EvalResult < ' tcx , Value > {
1331
1314
if let Some ( val) = self . try_read_value ( ptr, ty) ? {
1332
1315
Ok ( val)
1333
1316
} else {
@@ -1352,13 +1335,13 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
1352
1335
}
1353
1336
}
1354
1337
1355
- fn try_read_value ( & mut self , ptr : Pointer , ty : Ty < ' tcx > ) -> EvalResult < ' tcx , Option < Value > > {
1338
+ fn try_read_value ( & mut self , ptr : PrimVal , ty : Ty < ' tcx > ) -> EvalResult < ' tcx , Option < Value > > {
1356
1339
use syntax:: ast:: FloatTy ;
1357
1340
1358
1341
let val = match ty. sty {
1359
- ty:: TyBool => PrimVal :: from_bool ( self . memory . read_bool ( ptr) ?) ,
1342
+ ty:: TyBool => PrimVal :: from_bool ( self . memory . read_bool ( ptr. to_ptr ( ) ? ) ?) ,
1360
1343
ty:: TyChar => {
1361
- let c = self . memory . read_uint ( ptr, 4 ) ? as u32 ;
1344
+ let c = self . memory . read_uint ( ptr. to_ptr ( ) ? , 4 ) ? as u32 ;
1362
1345
match :: std:: char:: from_u32 ( c) {
1363
1346
Some ( ch) => PrimVal :: from_char ( ch) ,
1364
1347
None => return Err ( EvalError :: InvalidChar ( c as u128 ) ) ,
@@ -1377,8 +1360,8 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
1377
1360
} ;
1378
1361
// if we transmute a ptr to an isize, reading it back into a primval shouldn't panic
1379
1362
// Due to read_ptr ignoring the sign, we need to jump around some hoops
1380
- match self . memory . read_int ( ptr, size) {
1381
- Err ( EvalError :: ReadPointerAsBytes ) if size == self . memory . pointer_size ( ) => self . memory . read_ptr ( ptr) ?,
1363
+ match self . memory . read_int ( ptr. to_ptr ( ) ? , size) {
1364
+ Err ( EvalError :: ReadPointerAsBytes ) if size == self . memory . pointer_size ( ) => self . memory . read_ptr ( ptr. to_ptr ( ) ? ) ?,
1382
1365
other => PrimVal :: from_i128 ( other?) ,
1383
1366
}
1384
1367
}
@@ -1395,30 +1378,30 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
1395
1378
} ;
1396
1379
if size == self . memory . pointer_size ( ) {
1397
1380
// if we transmute a ptr to an usize, reading it back into a primval shouldn't panic
1398
- self . memory . read_ptr ( ptr) ?
1381
+ self . memory . read_ptr ( ptr. to_ptr ( ) ? ) ?
1399
1382
} else {
1400
- PrimVal :: from_u128 ( self . memory . read_uint ( ptr, size) ?)
1383
+ PrimVal :: from_u128 ( self . memory . read_uint ( ptr. to_ptr ( ) ? , size) ?)
1401
1384
}
1402
1385
}
1403
1386
1404
- ty:: TyFloat ( FloatTy :: F32 ) => PrimVal :: from_f32 ( self . memory . read_f32 ( ptr) ?) ,
1405
- ty:: TyFloat ( FloatTy :: F64 ) => PrimVal :: from_f64 ( self . memory . read_f64 ( ptr) ?) ,
1387
+ ty:: TyFloat ( FloatTy :: F32 ) => PrimVal :: from_f32 ( self . memory . read_f32 ( ptr. to_ptr ( ) ? ) ?) ,
1388
+ ty:: TyFloat ( FloatTy :: F64 ) => PrimVal :: from_f64 ( self . memory . read_f64 ( ptr. to_ptr ( ) ? ) ?) ,
1406
1389
1407
- ty:: TyFnPtr ( _) => self . memory . read_ptr ( ptr) ?,
1390
+ ty:: TyFnPtr ( _) => self . memory . read_ptr ( ptr. to_ptr ( ) ? ) ?,
1408
1391
ty:: TyRef ( _, ref tam) |
1409
- ty:: TyRawPtr ( ref tam) => return self . read_ptr ( ptr, tam. ty ) . map ( Some ) ,
1392
+ ty:: TyRawPtr ( ref tam) => return self . read_ptr ( ptr. to_ptr ( ) ? , tam. ty ) . map ( Some ) ,
1410
1393
1411
1394
ty:: TyAdt ( def, _) => {
1412
1395
if def. is_box ( ) {
1413
- return self . read_ptr ( ptr, ty. boxed_ty ( ) ) . map ( Some ) ;
1396
+ return self . read_ptr ( ptr. to_ptr ( ) ? , ty. boxed_ty ( ) ) . map ( Some ) ;
1414
1397
}
1415
1398
use rustc:: ty:: layout:: Layout :: * ;
1416
1399
if let CEnum { discr, signed, .. } = * self . type_layout ( ty) ? {
1417
1400
let size = discr. size ( ) . bytes ( ) ;
1418
1401
if signed {
1419
- PrimVal :: from_i128 ( self . memory . read_int ( ptr, size) ?)
1402
+ PrimVal :: from_i128 ( self . memory . read_int ( ptr. to_ptr ( ) ? , size) ?)
1420
1403
} else {
1421
- PrimVal :: from_u128 ( self . memory . read_uint ( ptr, size) ?)
1404
+ PrimVal :: from_u128 ( self . memory . read_uint ( ptr. to_ptr ( ) ? , size) ?)
1422
1405
}
1423
1406
} else {
1424
1407
return Ok ( None ) ;
@@ -1537,7 +1520,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
1537
1520
let src_f_ptr = src_ptr. offset ( src_field_offset, self . memory . layout ) ?;
1538
1521
let dst_f_ptr = dest. offset ( dst_field_offset, self . memory . layout ) ?;
1539
1522
if src_fty == dst_fty {
1540
- self . copy ( PrimVal :: Ptr ( src_f_ptr) , PrimVal :: Ptr ( dst_f_ptr) , src_fty) ?;
1523
+ self . copy ( src_f_ptr, PrimVal :: Ptr ( dst_f_ptr) , src_fty) ?;
1541
1524
} else {
1542
1525
self . unsize_into ( Value :: ByRef ( src_f_ptr) , src_fty, Lvalue :: from_ptr ( dst_f_ptr) , dst_fty) ?;
1543
1526
}
@@ -1566,10 +1549,13 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
1566
1549
Err ( err) => {
1567
1550
panic ! ( "Failed to access local: {:?}" , err) ;
1568
1551
}
1569
- Ok ( Value :: ByRef ( ptr) ) => {
1552
+ Ok ( Value :: ByRef ( PrimVal :: Ptr ( ptr) ) ) => {
1570
1553
write ! ( msg, " by ref:" ) . unwrap ( ) ;
1571
1554
allocs. push ( ptr. alloc_id ) ;
1572
1555
}
1556
+ Ok ( Value :: ByRef ( ptr) ) => {
1557
+ write ! ( msg, " integral by ref: {:?}" , ptr) . unwrap ( ) ;
1558
+ }
1573
1559
Ok ( Value :: ByVal ( val) ) => {
1574
1560
write ! ( msg, " {:?}" , val) . unwrap ( ) ;
1575
1561
if let PrimVal :: Ptr ( ptr) = val { allocs. push ( ptr. alloc_id ) ; }
0 commit comments