Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Dentosal committed Dec 12, 2024
1 parent bb0d554 commit 3fa2ebe
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions fuel-vm/src/interpreter/memory/diff_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
use proptest::prelude::*;

use crate::{
consts::{MEM_SIZE, VM_MAX_RAM},
consts::{
MEM_SIZE,
VM_MAX_RAM,
},
interpreter::{
memory::OwnershipRegisters,
MemorySliceChange,
Expand Down Expand Up @@ -156,28 +159,33 @@ fn overlapping_overwrite_produces_patch() {
);
}


#[test]
fn stack_retraction_overwrite_produces_correct_patch() {
let split_change = 10;
let split_a = VM_MAX_RAM / 2 + split_change;
let split_b = VM_MAX_RAM / 2;

let split_a_owner =
OwnershipRegisters::test(0..split_a, split_a..VM_MAX_RAM);
let split_b_owner =
OwnershipRegisters::test(0..split_b, split_b..VM_MAX_RAM);
let split_a_owner = OwnershipRegisters::test(0..split_a, split_a..VM_MAX_RAM);
let split_b_owner = OwnershipRegisters::test(0..split_b, split_b..VM_MAX_RAM);

// Allocate regions
let mut memory1 = MemoryInstance::new();
let mut hp = VM_MAX_RAM;
memory1.grow_stack(split_a).unwrap();
memory1.grow_heap_by(Reg::new(&split_a), RegMut::new(&mut hp), VM_MAX_RAM - split_a)
memory1
.grow_heap_by(
Reg::new(&split_a),
RegMut::new(&mut hp),
VM_MAX_RAM - split_a,
)
.unwrap();

// Fill stack with 0x01 and heap with 0x02
memory1.write(split_a_owner, 0, split_a).unwrap().fill(0x01);
memory1.write(split_a_owner, split_a, VM_MAX_RAM - split_a).unwrap().fill(0x02);
memory1
.write(split_a_owner, split_a, VM_MAX_RAM - split_a)
.unwrap()
.fill(0x02);

// Check that we generate the correct patch from empty state to this
let diff = MemoryInstance::new().diff_patches(&memory1);
Expand All @@ -190,20 +198,27 @@ fn stack_retraction_overwrite_produces_correct_patch() {
// Now we will implicity shrink the stack by explictly expanding the heap over it.
// This keeps the underlying stack allocation, which is subtle and could cause bugs.
let mut memory2 = memory1.clone();
memory2.grow_heap_by(Reg::new(&split_b), RegMut::new(&mut hp), split_change).unwrap();
memory2
.grow_heap_by(Reg::new(&split_b), RegMut::new(&mut hp), split_change)
.unwrap();

// Check that we generate the correct patch for this change,
// as the newly allocated heap memory should be is zeroed.
let diff = memory1.diff_patches(&memory2);
assert_eq!(diff, vec![
MemorySliceChange {
assert_eq!(
diff,
vec![MemorySliceChange {
global_start: split_b as usize,
data: vec![0; split_change as usize],
}
]);
}]
);

// And now we overwrite the whole heap with 0x03 to check that we generate the correct patch.
memory2.write(split_b_owner, split_b, VM_MAX_RAM - split_b).unwrap().fill(0x03);
// And now we overwrite the whole heap with 0x03 to check that we generate the correct
// patch.
memory2
.write(split_b_owner, split_b, VM_MAX_RAM - split_b)
.unwrap()
.fill(0x03);
let diff = memory1.diff_patches(&memory2);
assert_eq!(diff.len(), 1);
assert_eq!(diff[0].global_start, split_b as usize);
Expand Down

0 comments on commit 3fa2ebe

Please sign in to comment.