1
- use std:: collections:: { btree_map , VecDeque } ;
1
+ use std:: collections:: VecDeque ;
2
2
use std:: ptr;
3
3
4
4
use rustc:: hir:: def_id:: DefId ;
@@ -515,7 +515,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
515
515
516
516
fn get_bytes ( & self , ptr : MemoryPointer , size : u64 , align : Align ) -> EvalResult < ' tcx , & [ u8 ] > {
517
517
assert_ne ! ( size, 0 ) ;
518
- if self . relocations ( ptr, size) ?. count ( ) != 0 {
518
+ if self . relocations ( ptr, size) ?. len ( ) != 0 {
519
519
return err ! ( ReadPointerAsBytes ) ;
520
520
}
521
521
self . check_defined ( ptr, size) ?;
@@ -610,9 +610,9 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
610
610
// first copy the relocations to a temporary buffer, because
611
611
// `get_bytes_mut` will clear the relocations, which is correct,
612
612
// since we don't want to keep any relocations at the target.
613
-
614
613
let relocations: Vec < _ > = self . relocations ( src, size) ?
615
- . map ( |( & offset, & alloc_id) | {
614
+ . iter ( )
615
+ . map ( |& ( offset, alloc_id) | {
616
616
// Update relocation offsets for the new positions in the destination allocation.
617
617
( offset + dest. offset - src. offset , alloc_id)
618
618
} )
@@ -644,7 +644,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
644
644
645
645
self . copy_undef_mask ( src, dest, size) ?;
646
646
// copy back the relocations
647
- self . get_mut ( dest. alloc_id ) ?. relocations . extend ( relocations) ;
647
+ self . get_mut ( dest. alloc_id ) ?. relocations . insert_presorted ( relocations) ;
648
648
649
649
Ok ( ( ) )
650
650
}
@@ -655,7 +655,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
655
655
let offset = ptr. offset as usize ;
656
656
match alloc. bytes [ offset..] . iter ( ) . position ( |& c| c == 0 ) {
657
657
Some ( size) => {
658
- if self . relocations ( ptr, ( size + 1 ) as u64 ) ?. count ( ) != 0 {
658
+ if self . relocations ( ptr, ( size + 1 ) as u64 ) ?. len ( ) != 0 {
659
659
return err ! ( ReadPointerAsBytes ) ;
660
660
}
661
661
self . check_defined ( ptr, ( size + 1 ) as u64 ) ?;
@@ -715,7 +715,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
715
715
let bytes = read_target_uint ( endianness, bytes) . unwrap ( ) ;
716
716
// See if we got a pointer
717
717
if size != self . pointer_size ( ) {
718
- if self . relocations ( ptr, size) ?. count ( ) != 0 {
718
+ if self . relocations ( ptr, size) ?. len ( ) != 0 {
719
719
return err ! ( ReadPointerAsBytes ) ;
720
720
}
721
721
} else {
@@ -803,24 +803,26 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
803
803
& self ,
804
804
ptr : MemoryPointer ,
805
805
size : u64 ,
806
- ) -> EvalResult < ' tcx , btree_map :: Range < u64 , AllocId > > {
806
+ ) -> EvalResult < ' tcx , & [ ( u64 , AllocId ) ] > {
807
807
let start = ptr. offset . saturating_sub ( self . pointer_size ( ) - 1 ) ;
808
808
let end = ptr. offset + size;
809
809
Ok ( self . get ( ptr. alloc_id ) ?. relocations . range ( start..end) )
810
810
}
811
811
812
812
fn clear_relocations ( & mut self , ptr : MemoryPointer , size : u64 ) -> EvalResult < ' tcx > {
813
- // Find all relocations overlapping the given range.
814
- let keys: Vec < _ > = self . relocations ( ptr, size) ?. map ( |( & k, _) | k) . collect ( ) ;
815
- if keys. is_empty ( ) {
816
- return Ok ( ( ) ) ;
817
- }
818
-
819
813
// Find the start and end of the given range and its outermost relocations.
814
+ let ( first, last) = {
815
+ // Find all relocations overlapping the given range.
816
+ let relocations = self . relocations ( ptr, size) ?;
817
+ if relocations. is_empty ( ) {
818
+ return Ok ( ( ) ) ;
819
+ }
820
+
821
+ ( relocations. first ( ) . unwrap ( ) . 0 ,
822
+ relocations. last ( ) . unwrap ( ) . 0 + self . pointer_size ( ) )
823
+ } ;
820
824
let start = ptr. offset ;
821
825
let end = start + size;
822
- let first = * keys. first ( ) . unwrap ( ) ;
823
- let last = * keys. last ( ) . unwrap ( ) + self . pointer_size ( ) ;
824
826
825
827
let alloc = self . get_mut ( ptr. alloc_id ) ?;
826
828
@@ -834,16 +836,14 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
834
836
}
835
837
836
838
// Forget all the relocations.
837
- for k in keys {
838
- alloc. relocations . remove ( & k) ;
839
- }
839
+ alloc. relocations . remove_range ( first ..= last) ;
840
840
841
841
Ok ( ( ) )
842
842
}
843
843
844
844
fn check_relocation_edges ( & self , ptr : MemoryPointer , size : u64 ) -> EvalResult < ' tcx > {
845
- let overlapping_start = self . relocations ( ptr, 0 ) ?. count ( ) ;
846
- let overlapping_end = self . relocations ( ptr. offset ( size, self ) ?, 0 ) ?. count ( ) ;
845
+ let overlapping_start = self . relocations ( ptr, 0 ) ?. len ( ) ;
846
+ let overlapping_end = self . relocations ( ptr. offset ( size, self ) ?, 0 ) ?. len ( ) ;
847
847
if overlapping_start + overlapping_end != 0 {
848
848
return err ! ( ReadPointerAsBytes ) ;
849
849
}
0 commit comments