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

Store stack and heap separately #697

Merged
merged 26 commits into from
Apr 5, 2024
Merged

Store stack and heap separately #697

merged 26 commits into from
Apr 5, 2024

Conversation

xgreenx
Copy link
Collaborator

@xgreenx xgreenx commented Mar 12, 2024

Closes #452

Spec PR: FuelLabs/fuel-specs#566

@xgreenx xgreenx requested a review from Dentosal March 12, 2024 11:57
@xgreenx xgreenx added the no changelog Skips the CI changelog check label Mar 12, 2024
Comment on lines 52 to 58
// It is how `Vec::resize` calculates a new capacity.
let cap = core::cmp::max(len * 2, new_len);
let cap = core::cmp::min(cap, MEM_SIZE);
let diff = cap - len;
let mut new_vec = vec![value; cap];
new_vec[diff..].copy_from_slice(self.as_slice());
*self = new_vec;
Copy link
Member

Choose a reason for hiding this comment

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

This at least just copies the bytes just once, but it also forces a new allocation every time. I guess that cannot be avoided if we want to use a single Vec. We could maybe be a bit smarter than just doubling the size every time, e.g. starting with a kilobyte-sized allocation and not allowing the last doubling to MEM_SIZE, instead allocating with reserve_exact.

In any case, ALOC is currently constant cost and will allow copying the amount of entire memory, in parts. What used to be a cheap subtract and compare operation, is now a memcpy.

@Dentosal Dentosal self-assigned this Apr 2, 2024
@Dentosal
Copy link
Member

Dentosal commented Apr 3, 2024

There's a subtle bug in the implementation here: It's possible to extend the stack, then shrink the stack, and then extend the heap to be where the stack once was. Then reading from that memory area would read from the memory that was in the stack, and not from the heap that's on that region now. Fortunately we never shrink the heap, so just truncating the `stack´ field every time we grow heap over it fixes that.

Fixed in 10fed83

@Dentosal
Copy link
Member

Dentosal commented Apr 3, 2024

Another breaking change: Allocating new heap space with ALOC now zeroes memory.

@Dentosal Dentosal marked this pull request as ready for review April 3, 2024 19:27
@Dentosal Dentosal requested a review from a team April 3, 2024 19:28
@Dentosal Dentosal removed the no changelog Skips the CI changelog check label Apr 3, 2024
@Dentosal Dentosal added breaking A breaking api change fuel-vm Related to the `fuel-vm` crate. fuel-asm Related to the `fuel-asm` crate. labels Apr 3, 2024
fuel-vm/src/interpreter/memory.rs Show resolved Hide resolved
fuel-vm/src/constraints.rs Outdated Show resolved Hide resolved
fuel-vm/src/interpreter/flow.rs Show resolved Hide resolved
fuel-vm/src/interpreter/memory.rs Outdated Show resolved Hide resolved
fuel-vm/src/interpreter/memory.rs Outdated Show resolved Hide resolved
fuel-vm/src/interpreter/memory.rs Outdated Show resolved Hide resolved
fuel-vm/src/interpreter/memory.rs Show resolved Hide resolved
try_update_stack_pointer(sp, ssp, hp, stack_range.words().end)?;
let write_size = count * (core::mem::size_of::<Word>() as u64);
let write_at = *sp;
try_update_stack_pointer(sp, ssp, hp, write_at + write_size, memory)?;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I know that it shouldn't be possible to overflow here, but it would be nice to add #![deny(clippy::arithmetic_side_effects)] for fuel-vm. But it is definitely not a part of the current PR=)

Copy link
Member

Choose a reason for hiding this comment

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

Agreed. It's definitely not too easy to see that this cannot overflow.

Copy link
Collaborator Author

@xgreenx xgreenx left a comment

Choose a reason for hiding this comment

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

Looks super good to me=) Approved! (I can't approve since I created the draft PR)

@Dentosal Dentosal enabled auto-merge April 5, 2024 10:05
Dentosal added a commit to FuelLabs/fuel-specs that referenced this pull request Apr 5, 2024
@Dentosal Dentosal added this pull request to the merge queue Apr 5, 2024
Merged via the queue into master with commit fbdaa4b Apr 5, 2024
38 checks passed
@Dentosal Dentosal deleted the feature/growing-memory branch April 5, 2024 10:14
@xgreenx xgreenx restored the feature/growing-memory branch April 15, 2024 11:16
@xgreenx xgreenx mentioned this pull request Apr 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking A breaking api change fuel-asm Related to the `fuel-asm` crate. fuel-vm Related to the `fuel-vm` crate.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add memory pages
2 participants