@@ -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)
@@ -1164,7 +1147,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
1164
1147
//
1165
1148
// Thus, it would be an error to replace the `ByRef` with a `ByVal`, unless we
1166
1149
// knew for certain that there were no outstanding pointers to this allocation.
1167
- self . write_value_to_ptr ( src_val, PrimVal :: Ptr ( dest_ptr) , dest_ty) ?;
1150
+ self . write_value_to_ptr ( src_val, dest_ptr, dest_ty) ?;
1168
1151
1169
1152
} else if let Value :: ByRef ( src_ptr) = src_val {
1170
1153
// If the value is not `ByRef`, then we know there are no pointers to it
@@ -1182,8 +1165,8 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
1182
1165
write_dest ( self , src_val) ?;
1183
1166
} else {
1184
1167
let dest_ptr = self . alloc_ptr ( dest_ty) ?;
1185
- self . copy ( PrimVal :: Ptr ( src_ptr) , PrimVal :: Ptr ( dest_ptr) , dest_ty) ?;
1186
- write_dest ( self , Value :: ByRef ( dest_ptr) ) ?;
1168
+ self . copy ( src_ptr, PrimVal :: Ptr ( dest_ptr) , dest_ty) ?;
1169
+ write_dest ( self , Value :: ByRef ( PrimVal :: Ptr ( dest_ptr) ) ) ?;
1187
1170
}
1188
1171
1189
1172
} else {
@@ -1201,7 +1184,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
1201
1184
dest_ty : Ty < ' tcx > ,
1202
1185
) -> EvalResult < ' tcx > {
1203
1186
match value {
1204
- Value :: ByRef ( ptr) => self . copy ( PrimVal :: Ptr ( ptr) , dest, dest_ty) ,
1187
+ Value :: ByRef ( ptr) => self . copy ( ptr, dest, dest_ty) ,
1205
1188
Value :: ByVal ( primval) => {
1206
1189
let size = self . type_size ( dest_ty) ?. expect ( "dest type must be sized" ) ;
1207
1190
self . memory . write_primval ( dest, primval, size)
@@ -1331,7 +1314,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
1331
1314
}
1332
1315
}
1333
1316
1334
- pub ( super ) fn read_value ( & mut self , ptr : Pointer , ty : Ty < ' tcx > ) -> EvalResult < ' tcx , Value > {
1317
+ pub ( super ) fn read_value ( & mut self , ptr : PrimVal , ty : Ty < ' tcx > ) -> EvalResult < ' tcx , Value > {
1335
1318
if let Some ( val) = self . try_read_value ( ptr, ty) ? {
1336
1319
Ok ( val)
1337
1320
} else {
@@ -1356,13 +1339,13 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
1356
1339
}
1357
1340
}
1358
1341
1359
- fn try_read_value ( & mut self , ptr : Pointer , ty : Ty < ' tcx > ) -> EvalResult < ' tcx , Option < Value > > {
1342
+ fn try_read_value ( & mut self , ptr : PrimVal , ty : Ty < ' tcx > ) -> EvalResult < ' tcx , Option < Value > > {
1360
1343
use syntax:: ast:: FloatTy ;
1361
1344
1362
1345
let val = match ty. sty {
1363
- ty:: TyBool => PrimVal :: from_bool ( self . memory . read_bool ( ptr) ?) ,
1346
+ ty:: TyBool => PrimVal :: from_bool ( self . memory . read_bool ( ptr. to_ptr ( ) ? ) ?) ,
1364
1347
ty:: TyChar => {
1365
- let c = self . memory . read_uint ( ptr, 4 ) ? as u32 ;
1348
+ let c = self . memory . read_uint ( ptr. to_ptr ( ) ? , 4 ) ? as u32 ;
1366
1349
match :: std:: char:: from_u32 ( c) {
1367
1350
Some ( ch) => PrimVal :: from_char ( ch) ,
1368
1351
None => return Err ( EvalError :: InvalidChar ( c as u128 ) ) ,
@@ -1381,8 +1364,8 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
1381
1364
} ;
1382
1365
// if we transmute a ptr to an isize, reading it back into a primval shouldn't panic
1383
1366
// Due to read_ptr ignoring the sign, we need to jump around some hoops
1384
- match self . memory . read_int ( ptr, size) {
1385
- Err ( EvalError :: ReadPointerAsBytes ) if size == self . memory . pointer_size ( ) => self . memory . read_ptr ( ptr) ?,
1367
+ match self . memory . read_int ( ptr. to_ptr ( ) ? , size) {
1368
+ Err ( EvalError :: ReadPointerAsBytes ) if size == self . memory . pointer_size ( ) => self . memory . read_ptr ( ptr. to_ptr ( ) ? ) ?,
1386
1369
other => PrimVal :: from_i128 ( other?) ,
1387
1370
}
1388
1371
}
@@ -1399,30 +1382,30 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
1399
1382
} ;
1400
1383
if size == self . memory . pointer_size ( ) {
1401
1384
// if we transmute a ptr to an usize, reading it back into a primval shouldn't panic
1402
- self . memory . read_ptr ( ptr) ?
1385
+ self . memory . read_ptr ( ptr. to_ptr ( ) ? ) ?
1403
1386
} else {
1404
- PrimVal :: from_u128 ( self . memory . read_uint ( ptr, size) ?)
1387
+ PrimVal :: from_u128 ( self . memory . read_uint ( ptr. to_ptr ( ) ? , size) ?)
1405
1388
}
1406
1389
}
1407
1390
1408
- ty:: TyFloat ( FloatTy :: F32 ) => PrimVal :: from_f32 ( self . memory . read_f32 ( ptr) ?) ,
1409
- ty:: TyFloat ( FloatTy :: F64 ) => PrimVal :: from_f64 ( self . memory . read_f64 ( ptr) ?) ,
1391
+ ty:: TyFloat ( FloatTy :: F32 ) => PrimVal :: from_f32 ( self . memory . read_f32 ( ptr. to_ptr ( ) ? ) ?) ,
1392
+ ty:: TyFloat ( FloatTy :: F64 ) => PrimVal :: from_f64 ( self . memory . read_f64 ( ptr. to_ptr ( ) ? ) ?) ,
1410
1393
1411
- ty:: TyFnPtr ( _) => self . memory . read_ptr ( ptr) ?,
1394
+ ty:: TyFnPtr ( _) => self . memory . read_ptr ( ptr. to_ptr ( ) ? ) ?,
1412
1395
ty:: TyRef ( _, ref tam) |
1413
- ty:: TyRawPtr ( ref tam) => return self . read_ptr ( ptr, tam. ty ) . map ( Some ) ,
1396
+ ty:: TyRawPtr ( ref tam) => return self . read_ptr ( ptr. to_ptr ( ) ? , tam. ty ) . map ( Some ) ,
1414
1397
1415
1398
ty:: TyAdt ( def, _) => {
1416
1399
if def. is_box ( ) {
1417
- return self . read_ptr ( ptr, ty. boxed_ty ( ) ) . map ( Some ) ;
1400
+ return self . read_ptr ( ptr. to_ptr ( ) ? , ty. boxed_ty ( ) ) . map ( Some ) ;
1418
1401
}
1419
1402
use rustc:: ty:: layout:: Layout :: * ;
1420
1403
if let CEnum { discr, signed, .. } = * self . type_layout ( ty) ? {
1421
1404
let size = discr. size ( ) . bytes ( ) ;
1422
1405
if signed {
1423
- PrimVal :: from_i128 ( self . memory . read_int ( ptr, size) ?)
1406
+ PrimVal :: from_i128 ( self . memory . read_int ( ptr. to_ptr ( ) ? , size) ?)
1424
1407
} else {
1425
- PrimVal :: from_u128 ( self . memory . read_uint ( ptr, size) ?)
1408
+ PrimVal :: from_u128 ( self . memory . read_uint ( ptr. to_ptr ( ) ? , size) ?)
1426
1409
}
1427
1410
} else {
1428
1411
return Ok ( None ) ;
@@ -1541,7 +1524,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
1541
1524
let src_f_ptr = src_ptr. offset ( src_field_offset, self . memory . layout ) ?;
1542
1525
let dst_f_ptr = dest. offset ( dst_field_offset, self . memory . layout ) ?;
1543
1526
if src_fty == dst_fty {
1544
- self . copy ( PrimVal :: Ptr ( src_f_ptr) , PrimVal :: Ptr ( dst_f_ptr) , src_fty) ?;
1527
+ self . copy ( src_f_ptr, PrimVal :: Ptr ( dst_f_ptr) , src_fty) ?;
1545
1528
} else {
1546
1529
self . unsize_into ( Value :: ByRef ( src_f_ptr) , src_fty, Lvalue :: from_ptr ( dst_f_ptr) , dst_fty) ?;
1547
1530
}
@@ -1570,10 +1553,13 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
1570
1553
Err ( err) => {
1571
1554
panic ! ( "Failed to access local: {:?}" , err) ;
1572
1555
}
1573
- Ok ( Value :: ByRef ( ptr) ) => {
1556
+ Ok ( Value :: ByRef ( PrimVal :: Ptr ( ptr) ) ) => {
1574
1557
write ! ( msg, " by ref:" ) . unwrap ( ) ;
1575
1558
allocs. push ( ptr. alloc_id ) ;
1576
1559
}
1560
+ Ok ( Value :: ByRef ( ptr) ) => {
1561
+ write ! ( msg, " integral by ref: {:?}" , ptr) . unwrap ( ) ;
1562
+ }
1577
1563
Ok ( Value :: ByVal ( val) ) => {
1578
1564
write ! ( msg, " {:?}" , val) . unwrap ( ) ;
1579
1565
if let PrimVal :: Ptr ( ptr) = val { allocs. push ( ptr. alloc_id ) ; }
0 commit comments