Skip to content
This repository has been archived by the owner on Nov 26, 2024. It is now read-only.

Commit

Permalink
Rename memory_grow hostio to pay_for_memory_grow
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuacolvin0 committed Dec 13, 2023
1 parent fc96536 commit 745a078
Show file tree
Hide file tree
Showing 25 changed files with 62 additions and 57 deletions.
2 changes: 1 addition & 1 deletion arbitrator/langs/bf
Submodule bf updated 1 files
+1 −1 Cargo.lock
2 changes: 1 addition & 1 deletion arbitrator/langs/c
25 changes: 15 additions & 10 deletions arbitrator/prover/src/programs/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct HeapBound {
/// Upper bounds the amount of heap memory a module may use
limit: Pages,
/// Import called when allocating new pages
memory_grow: RwLock<Option<FunctionIndex>>,
pay_for_memory_grow: RwLock<Option<FunctionIndex>>,
/// Scratch global shared among middlewares
scratch: RwLock<Option<GlobalIndex>>,
}
Expand All @@ -26,7 +26,7 @@ impl HeapBound {
pub fn new(bounds: CompileMemoryParams) -> Self {
Self {
limit: bounds.heap_bound,
memory_grow: RwLock::default(),
pay_for_memory_grow: RwLock::default(),
scratch: RwLock::default(),
}
}
Expand All @@ -51,23 +51,28 @@ impl<M: ModuleMod> Middleware<M> for HeapBound {
return Ok(());
}

let ImportIndex::Function(import) = module.get_import("vm_hooks", "memory_grow")? else {
bail!("wrong import kind for {}", "memory_grow".red());
let ImportIndex::Function(import) = module.get_import("vm_hooks", "pay_for_memory_grow")?
else {
bail!("wrong import kind for {}", "pay_for_memory_grow".red());
};

let ty = module.get_function(import)?;
if ty != FunctionType::new(vec![ArbValueType::I32], vec![]) {
bail!("wrong type for {}: {}", "memory_grow".red(), ty.red());
bail!(
"wrong type for {}: {}",
"pay_for_memory_grow".red(),
ty.red()
);
}

*self.memory_grow.write() = Some(import);
*self.pay_for_memory_grow.write() = Some(import);
Ok(())
}

fn instrument<'a>(&self, _: LocalFunctionIndex) -> Result<Self::FM<'a>> {
Ok(FuncHeapBound {
scratch: self.scratch.read().expect("no scratch global"),
memory_grow: *self.memory_grow.read(),
pay_for_memory_grow: *self.pay_for_memory_grow.read(),
})
}

Expand All @@ -78,7 +83,7 @@ impl<M: ModuleMod> Middleware<M> for HeapBound {

#[derive(Debug)]
pub struct FuncHeapBound {
memory_grow: Option<FunctionIndex>,
pay_for_memory_grow: Option<FunctionIndex>,
scratch: GlobalIndex,
}

Expand All @@ -89,13 +94,13 @@ impl<'a> FuncMiddleware<'a> for FuncHeapBound {
{
use Operator::*;

let Some(memory_grow) = self.memory_grow else {
let Some(pay_for_memory_grow) = self.pay_for_memory_grow else {
out.extend([op]);
return Ok(());
};

let global_index = self.scratch.as_u32();
let function_index = memory_grow.as_u32();
let function_index = pay_for_memory_grow.as_u32();

if let MemoryGrow { .. } = op {
out.extend([
Expand Down
4 changes: 2 additions & 2 deletions arbitrator/stylus/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,8 @@ pub(crate) fn tx_origin<E: EvmApi>(mut env: WasmEnvMut<E>, ptr: u32) -> MaybeEsc
hostio!(env, tx_origin(ptr))
}

pub(crate) fn memory_grow<E: EvmApi>(mut env: WasmEnvMut<E>, pages: u16) -> MaybeEscape {
hostio!(env, memory_grow(pages))
pub(crate) fn pay_for_memory_grow<E: EvmApi>(mut env: WasmEnvMut<E>, pages: u16) -> MaybeEscape {
hostio!(env, pay_for_memory_grow(pages))
}

pub(crate) fn console_log_text<E: EvmApi>(
Expand Down
4 changes: 2 additions & 2 deletions arbitrator/stylus/src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl<E: EvmApi> NativeInstance<E> {
"tx_gas_price" => func!(host::tx_gas_price),
"tx_ink_price" => func!(host::tx_ink_price),
"tx_origin" => func!(host::tx_origin),
"memory_grow" => func!(host::memory_grow),
"pay_for_memory_grow" => func!(host::pay_for_memory_grow),
"native_keccak256" => func!(host::native_keccak256),
},
};
Expand Down Expand Up @@ -357,7 +357,7 @@ pub fn module(wasm: &[u8], compile: CompileConfig) -> Result<Vec<u8>> {
"tx_gas_price" => stub!(|_: u32|),
"tx_ink_price" => stub!(u32 <- ||),
"tx_origin" => stub!(|_: u32|),
"memory_grow" => stub!(|_: u16|),
"pay_for_memory_grow" => stub!(|_: u16|),
"native_keccak256" => stub!(|_: u32, _: u32, _: u32|),
},
};
Expand Down
2 changes: 1 addition & 1 deletion arbitrator/stylus/src/test/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ fn test_heap() -> Result<()> {
}

// in memory2.wat
// the user program calls memory_grow directly with malicious arguments
// the user program calls pay_for_memory_grow directly with malicious arguments
// the cost should exceed a maximum u32, consuming more gas than can ever be bought

let (mut native, _) = TestInstance::new_with_evm("tests/memory2.wat", &compile, config)?;
Expand Down
4 changes: 2 additions & 2 deletions arbitrator/stylus/tests/create/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions arbitrator/stylus/tests/erc20/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions arbitrator/stylus/tests/evm-data/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions arbitrator/stylus/tests/fallible/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions arbitrator/stylus/tests/grow-and-call.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
;; For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE

(module
(import "vm_hooks" "memory_grow" (func (param i32)))
(import "vm_hooks" "read_args" (func $read_args (param i32)))
(import "vm_hooks" "write_result" (func $write_result (param i32 i32)))
(import "vm_hooks" "call_contract" (func $call_contract (param i32 i32 i32 i32 i64 i32) (result i32)))
(import "console" "tee_i32" (func $tee (param i32) (result i32)))
(import "vm_hooks" "pay_for_memory_grow" (func (param i32)))
(import "vm_hooks" "read_args" (func $read_args (param i32)))
(import "vm_hooks" "write_result" (func $write_result (param i32 i32)))
(import "vm_hooks" "call_contract" (func $call_contract (param i32 i32 i32 i32 i64 i32) (result i32)))
(import "console" "tee_i32" (func $tee (param i32) (result i32)))
(func (export "user_entrypoint") (param $args_len i32) (result i32)

;; store the target size argument at offset 0
Expand Down
4 changes: 2 additions & 2 deletions arbitrator/stylus/tests/keccak-100/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions arbitrator/stylus/tests/keccak/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions arbitrator/stylus/tests/log/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions arbitrator/stylus/tests/memory.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
;; For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE

(module
(import "vm_hooks" "memory_grow" (func (param i32)))
(import "vm_hooks" "read_args" (func $read_args (param i32)))
(import "vm_hooks" "write_result" (func $write_result (param i32 i32)))
(import "vm_hooks" "pay_for_memory_grow" (func (param i32)))
(import "vm_hooks" "read_args" (func $read_args (param i32)))
(import "vm_hooks" "write_result" (func $write_result (param i32 i32)))
(func (export "user_entrypoint") (param $args_len i32) (result i32)
(local $size i32) (local $step i32)

Expand Down
6 changes: 3 additions & 3 deletions arbitrator/stylus/tests/memory2.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
;; For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE

(module
(import "vm_hooks" "memory_grow" (func $memory_grow (param i32)))
(import "vm_hooks" "pay_for_memory_grow" (func $pay_for_memory_grow (param i32)))
(func (export "user_entrypoint") (param $args_len i32) (result i32)
(call $memory_grow (i32.const 0))
(call $memory_grow (i32.sub (i32.const 0) (i32.const 1)))
(call $pay_for_memory_grow (i32.const 0))
(call $pay_for_memory_grow (i32.sub (i32.const 0) (i32.const 1)))
i32.const 0
)
(memory (export "memory") 0)
Expand Down
4 changes: 2 additions & 2 deletions arbitrator/stylus/tests/multicall/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions arbitrator/stylus/tests/read-return-data/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions arbitrator/stylus/tests/sdk-storage/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions arbitrator/stylus/tests/storage/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion arbitrator/wasm-libraries/forward/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const HOSTIOS: [[&str; 3]; 31] = [
["tx_gas_price", "i32", ""],
["tx_ink_price", "", "i32"],
["tx_origin", "i32", ""],
["memory_grow", "i32", ""],
["pay_for_memory_grow", "i32", ""],
];

#[derive(StructOpt)]
Expand Down
4 changes: 2 additions & 2 deletions arbitrator/wasm-libraries/user-host-trait/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,14 +653,14 @@ pub trait UserHost: GasMeteredMachine {
}

/// Pays for new pages as needed before the memory.grow opcode is invoked
fn memory_grow(&mut self, pages: u16) -> Result<(), Self::Err> {
fn pay_for_memory_grow(&mut self, pages: u16) -> Result<(), Self::Err> {
if pages == 0 {
self.buy_ink(HOSTIO_INK)?;
return Ok(());
}
let gas_cost = self.evm_api().add_pages(pages);
self.buy_gas(gas_cost)?;
trace!("memory_grow", self, be!(pages), &[])
trace!("pay_for_memory_grow", self, be!(pages), &[])
}

/// Prints a UTF-8 encoded string to the console. Only available in debug mode.
Expand Down
Loading

0 comments on commit 745a078

Please sign in to comment.