@@ -690,6 +690,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
690
690
let right = this. read_pointer ( right) ?;
691
691
let n = Size :: from_bytes ( this. read_target_usize ( n) ?) ;
692
692
693
+ // C requires that this must always be a valid pointer (C18 §7.1.4).
694
+ this. ptr_get_alloc_id ( left) ?;
695
+ this. ptr_get_alloc_id ( right) ?;
696
+
693
697
let result = {
694
698
let left_bytes = this. read_bytes_ptr_strip_provenance ( left, n) ?;
695
699
let right_bytes = this. read_bytes_ptr_strip_provenance ( right, n) ?;
@@ -714,6 +718,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
714
718
#[ allow( clippy:: cast_sign_loss, clippy:: cast_possible_truncation) ]
715
719
let val = val as u8 ;
716
720
721
+ // C requires that this must always be a valid pointer (C18 §7.1.4).
722
+ this. ptr_get_alloc_id ( ptr) ?;
723
+
717
724
if let Some ( idx) = this
718
725
. read_bytes_ptr_strip_provenance ( ptr, Size :: from_bytes ( num) ) ?
719
726
. iter ( )
@@ -738,6 +745,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
738
745
#[ allow( clippy:: cast_sign_loss, clippy:: cast_possible_truncation) ]
739
746
let val = val as u8 ;
740
747
748
+ // C requires that this must always be a valid pointer (C18 §7.1.4).
749
+ this. ptr_get_alloc_id ( ptr) ?;
750
+
741
751
let idx = this
742
752
. read_bytes_ptr_strip_provenance ( ptr, Size :: from_bytes ( num) ) ?
743
753
. iter ( )
@@ -752,6 +762,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
752
762
"strlen" => {
753
763
let [ ptr] = this. check_shim ( abi, Abi :: C { unwind : false } , link_name, args) ?;
754
764
let ptr = this. read_pointer ( ptr) ?;
765
+ // This reads at least 1 byte, so we are already enforcing that this is a valid pointer.
755
766
let n = this. read_c_str ( ptr) ?. len ( ) ;
756
767
this. write_scalar (
757
768
Scalar :: from_target_usize ( u64:: try_from ( n) . unwrap ( ) , this) ,
@@ -791,6 +802,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
791
802
// pointer provenance is preserved by this implementation of `strcpy`.
792
803
// That is probably overly cautious, but there also is no fundamental
793
804
// reason to have `strcpy` destroy pointer provenance.
805
+ // This reads at least 1 byte, so we are already enforcing that this is a valid pointer.
794
806
let n = this. read_c_str ( ptr_src) ?. len ( ) . checked_add ( 1 ) . unwrap ( ) ;
795
807
this. mem_copy (
796
808
ptr_src,
0 commit comments