]> git.lizzy.rs Git - rust.git/blobdiff - src/stacked_borrows.rs
Stacked Borrows: print affected memory location on errors
[rust.git] / src / stacked_borrows.rs
index 257208056d76486a4f8cb426b541b9bc4b275af3..cf5b31597daa73e818d29a56cdd1948e585835e9 100644 (file)
@@ -309,14 +309,14 @@ fn check_protector(item: &Item, tag: Option<Tag>, global: &GlobalState) -> Inter
 
     /// Test if a memory `access` using pointer tagged `tag` is granted.
     /// If yes, return the index of the item that granted it.
-    fn access(&mut self, access: AccessKind, tag: Tag, global: &GlobalState) -> InterpResult<'tcx> {
+    fn access(&mut self, access: AccessKind, ptr: Pointer<Tag>, global: &GlobalState) -> InterpResult<'tcx> {
         // Two main steps: Find granting item, remove incompatible items above.
 
         // Step 1: Find granting item.
-        let granting_idx = self.find_granting(access, tag).ok_or_else(|| {
+        let granting_idx = self.find_granting(access, ptr.tag).ok_or_else(|| {
             err_sb_ub(format!(
-                "no item granting {} to tag {:?} found in borrow stack.",
-                access, tag
+                "no item granting {} to tag {:?} at {} found in borrow stack.",
+                access, ptr.tag, ptr.erase_tag(),
             ))
         })?;
 
@@ -328,7 +328,7 @@ fn access(&mut self, access: AccessKind, tag: Tag, global: &GlobalState) -> Inte
             let first_incompatible_idx = self.find_first_write_incompatible(granting_idx);
             for item in self.borrows.drain(first_incompatible_idx..).rev() {
                 trace!("access: popping item {:?}", item);
-                Stack::check_protector(&item, Some(tag), global)?;
+                Stack::check_protector(&item, Some(ptr.tag), global)?;
             }
         } else {
             // On a read, *disable* all `Unique` above the granting item.  This ensures U2 for read accesses.
@@ -343,7 +343,7 @@ fn access(&mut self, access: AccessKind, tag: Tag, global: &GlobalState) -> Inte
                 let item = &mut self.borrows[idx];
                 if item.perm == Permission::Unique {
                     trace!("access: disabling item {:?}", item);
-                    Stack::check_protector(item, Some(tag), global)?;
+                    Stack::check_protector(item, Some(ptr.tag), global)?;
                     item.perm = Permission::Disabled;
                 }
             }
@@ -355,12 +355,12 @@ fn access(&mut self, access: AccessKind, tag: Tag, global: &GlobalState) -> Inte
 
     /// Deallocate a location: Like a write access, but also there must be no
     /// active protectors at all because we will remove all items.
-    fn dealloc(&mut self, tag: Tag, global: &GlobalState) -> InterpResult<'tcx> {
+    fn dealloc(&mut self, ptr: Pointer<Tag>, global: &GlobalState) -> InterpResult<'tcx> {
         // Step 1: Find granting item.
-        self.find_granting(AccessKind::Write, tag).ok_or_else(|| {
+        self.find_granting(AccessKind::Write, ptr.tag).ok_or_else(|| {
             err_sb_ub(format!(
-                "no item granting write access for deallocation to tag {:?} found in borrow stack",
-                tag,
+                "no item granting write access for deallocation to tag {:?} at {} found in borrow stack",
+                ptr.tag, ptr.erase_tag(),
             ))
         })?;
 
@@ -372,20 +372,20 @@ fn dealloc(&mut self, tag: Tag, global: &GlobalState) -> InterpResult<'tcx> {
         Ok(())
     }
 
-    /// Derived a new pointer from one with the given tag.
+    /// Derive a new pointer from one with the given tag.
     /// `weak` controls whether this operation is weak or strong: weak granting does not act as
     /// an access, and they add the new item directly on top of the one it is derived
     /// from instead of all the way at the top of the stack.
-    fn grant(&mut self, derived_from: Tag, new: Item, global: &GlobalState) -> InterpResult<'tcx> {
+    fn grant(&mut self, derived_from: Pointer<Tag>, new: Item, global: &GlobalState) -> InterpResult<'tcx> {
         // Figure out which access `perm` corresponds to.
         let access =
             if new.perm.grants(AccessKind::Write) { AccessKind::Write } else { AccessKind::Read };
         // Now we figure out which item grants our parent (`derived_from`) this kind of access.
         // We use that to determine where to put the new item.
-        let granting_idx = self.find_granting(access, derived_from)
+        let granting_idx = self.find_granting(access, derived_from.tag)
             .ok_or_else(|| err_sb_ub(format!(
-                "trying to reborrow for {:?}, but parent tag {:?} does not have an appropriate item in the borrow stack",
-                new.perm, derived_from,
+                "trying to reborrow for {:?} at {}, but parent tag {:?} does not have an appropriate item in the borrow stack",
+                new.perm, derived_from.erase_tag(), derived_from.tag,
             )))?;
 
         // Compute where to put the new item.
@@ -443,12 +443,14 @@ fn for_each(
         &self,
         ptr: Pointer<Tag>,
         size: Size,
-        f: impl Fn(&mut Stack, &GlobalState) -> InterpResult<'tcx>,
+        f: impl Fn(Pointer<Tag>, &mut Stack, &GlobalState) -> InterpResult<'tcx>,
     ) -> InterpResult<'tcx> {
         let global = self.global.borrow();
         let mut stacks = self.stacks.borrow_mut();
-        for stack in stacks.iter_mut(ptr.offset, size) {
-            f(stack, &*global)?;
+        for (offset, stack) in stacks.iter_mut(ptr.offset, size) {
+            let mut cur_ptr = ptr;
+            cur_ptr.offset = offset;
+            f(cur_ptr, stack, &*global)?;
         }
         Ok(())
     }
@@ -487,19 +489,13 @@ pub fn new_allocation(
     #[inline(always)]
     pub fn memory_read<'tcx>(&self, ptr: Pointer<Tag>, size: Size) -> InterpResult<'tcx> {
         trace!("read access with tag {:?}: {:?}, size {}", ptr.tag, ptr.erase_tag(), size.bytes());
-        self.for_each(ptr, size, |stack, global| {
-            stack.access(AccessKind::Read, ptr.tag, global)?;
-            Ok(())
-        })
+        self.for_each(ptr, size, |ptr, stack, global| stack.access(AccessKind::Read, ptr, global))
     }
 
     #[inline(always)]
     pub fn memory_written<'tcx>(&mut self, ptr: Pointer<Tag>, size: Size) -> InterpResult<'tcx> {
         trace!("write access with tag {:?}: {:?}, size {}", ptr.tag, ptr.erase_tag(), size.bytes());
-        self.for_each(ptr, size, |stack, global| {
-            stack.access(AccessKind::Write, ptr.tag, global)?;
-            Ok(())
-        })
+        self.for_each(ptr, size, |ptr, stack, global| stack.access(AccessKind::Write, ptr, global))
     }
 
     #[inline(always)]
@@ -509,7 +505,7 @@ pub fn memory_deallocated<'tcx>(
         size: Size,
     ) -> InterpResult<'tcx> {
         trace!("deallocation with tag {:?}: {:?}, size {}", ptr.tag, ptr.erase_tag(), size.bytes());
-        self.for_each(ptr, size, |stack, global| stack.dealloc(ptr.tag, global))
+        self.for_each(ptr, size, |ptr, stack, global| stack.dealloc(ptr, global))
     }
 }
 
@@ -561,14 +557,14 @@ fn reborrow(
                         Permission::SharedReadWrite
                     };
                     let item = Item { perm, tag: new_tag, protector };
-                    stacked_borrows.for_each(cur_ptr, size, |stack, global| {
-                        stack.grant(cur_ptr.tag, item, global)
+                    stacked_borrows.for_each(cur_ptr, size, |cur_ptr, stack, global| {
+                        stack.grant(cur_ptr, item, global)
                     })
                 });
             }
         };
         let item = Item { perm, tag: new_tag, protector };
-        stacked_borrows.for_each(ptr, size, |stack, global| stack.grant(ptr.tag, item, global))
+        stacked_borrows.for_each(ptr, size, |ptr, stack, global| stack.grant(ptr, item, global))
     }
 
     /// Retags an indidual pointer, returning the retagged version.