Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grow_stack and empty $hp range write fixes #788

Merged
merged 4 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
#### Breaking
- [#783](https://github.com/FuelLabs/fuel-vm/pull/783): Remove unnecessary look up for old values by adding new methods to the `StorageMutate` trait. The old `insert` and `remove` are now `replace` and `take`. The new `insert` and `remove` don't return a value.
- [#783](https://github.com/FuelLabs/fuel-vm/pull/783): Renamed methods of `StorageWrite` trait from `write`, `replace`, `take` to `write_bytes`, `replace_bytes`, `take_bytes`.
- [#788](https://github.com/FuelLabs/fuel-vm/pull/788): Fix truncating `sp` to `MEM_SIZE` in `grow_stack`, and allow empty writes to zero-length ranges at `$hp`.

### Fixed

Expand Down
16 changes: 10 additions & 6 deletions fuel-vm/src/interpreter/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,12 @@ impl MemoryInstance {
/// Grows the stack to be at least `new_sp` bytes.
pub fn grow_stack(&mut self, new_sp: Word) -> Result<(), PanicReason> {
#[allow(clippy::cast_possible_truncation)] // Safety: MEM_SIZE is usize
let new_sp = new_sp.min(MEM_SIZE as Word) as usize;
if new_sp > VM_MAX_RAM {
return Err(PanicReason::MemoryOverflow);
}
#[allow(clippy::cast_possible_truncation)] // Safety: VM_MAX_RAM is usize
let new_sp = new_sp as usize;

if new_sp > self.stack.len() {
if new_sp > self.hp {
return Err(PanicReason::MemoryGrowthOverlap)
Expand Down Expand Up @@ -972,11 +977,10 @@ impl OwnershipRegisters {

/// Empty range is owned iff the range.start is owned
pub(crate) fn has_ownership_heap(&self, range: &Range<Word>) -> bool {
// TODO implement fp->hp and (addr, size) validations
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These were already implemented, so this comment was obsolete.

// fp->hp
// it means $hp from the previous context, i.e. what's saved in the
// "Saved registers from previous context" of the call frame at
// $fp`
if range.is_empty() && range.start == self.hp {
return true
}

if range.start < self.hp {
return false
}
Expand Down
8 changes: 8 additions & 0 deletions fuel-vm/src/interpreter/memory/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@ fn stack_alloc_ownership() {
OwnershipRegisters::test(0..20, 40..50),
20..41 => false; "start exclusive and end inclusive"
)]
#[test_case(
OwnershipRegisters::test(0..0, VM_MAX_RAM..VM_MAX_RAM),
MEM_SIZE..MEM_SIZE => true; "empty range at $hp (VM_MAX_RAM) should be allowed"
)]
#[test_case(
OwnershipRegisters::test(0..0, 100..VM_MAX_RAM),
100..100 => true; "empty range at $hp (not VM_MAX_RAM) should be allowed"
)]
fn test_ownership(reg: OwnershipRegisters, range: Range<usize>) -> bool {
reg.verify_ownership(&MemoryRange::new(range.start, range.len()))
.is_ok()
Expand Down
Loading