Skip to content

Commit

Permalink
Adjust std::alloc::alloc() to accommodate upcoming VM changes. (FuelL…
Browse files Browse the repository at this point in the history
  • Loading branch information
otrho authored Mar 9, 2023
1 parent 163c649 commit 0848a88
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
15 changes: 13 additions & 2 deletions sway-lib-std/src/alloc.sw
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,19 @@ library alloc;
/// ```
/// For more information, see the Fuel Spec for [VM Initialization](https://fuellabs.github.io/fuel-specs/master/vm#vm-initialization)
/// and the VM Instruction Set for [Memory Allocation](https://fuellabs.github.io/fuel-specs/master/vm/instruction_set.html#aloc-allocate-memory).
///
/// NOTE: See https://github.com/FuelLabs/fuel-specs/pull/464 and related PRs. There is an upcoming
/// breaking change to the VM which 'corrects' the above behaviour to be more intuitive. Instead of
/// `$hp` pointing to the last byte of free memory it will instead point to the bottom of allocated
/// memory. So it will be initialized to `VM_MAX_RAM` and after an `ALOC` it will point directly to
/// the new buffer.
///
/// To avoid the need to synchronize the behaviours between this library and the two allocation
/// modes, i.e., before and after the breaking change, we allocate 1 extra byte here and still
/// return `$hp + 1`. So prior to the VM change every allocation will have an unused byte _after_
/// the buffer and after the change every allocation will have an unused byte _before_ the buffer.
pub fn alloc<T>(count: u64) -> raw_ptr {
asm(size: __size_of::<T>() * count, ptr) {
asm(size: __size_of::<T>() * count + 1, ptr) {
aloc size;
// `$hp` points to unallocated space and heap grows downward so
// our newly allocated space will be right after it.
Expand All @@ -48,7 +59,7 @@ pub fn realloc<T>(ptr: raw_ptr, count: u64, new_count: u64) -> raw_ptr {

/// Allocates zeroed memory on the heap in individual bytes.
pub fn alloc_bytes(count: u64) -> raw_ptr {
asm(size: count, ptr) {
asm(size: count + 1, ptr) {
aloc size;
addi ptr hp i1;
ptr: raw_ptr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ script;
use basic_storage_abi::{BasicStorage, Quad};

fn main() -> u64 {
let addr = abi(BasicStorage, 0x558d7f3f2cc625751596ca69a5c670a02651f1b84852e93adc9675368df26959);
let addr = abi(BasicStorage, 0x89d66ade6874264291ca6416a6e746ac568970c385e19297d0f576ff05463be5);
let key = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
let value = 4242;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
category = "run"
# This test is making assumptions about how `$hp` is used with allocations. It needs to be
# re-enabled after the VM introduces the new `ALOC` behaviour.
category = "disabled"
expected_result = { action = "return", value = 1 }
validate_abi = true

0 comments on commit 0848a88

Please sign in to comment.