Skip to content
Open
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
16 changes: 10 additions & 6 deletions arbos/programs/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package programs

import (
"errors"
"fmt"
"unsafe"

"github.com/ethereum/go-ethereum/arbitrum/multigas"
Expand Down Expand Up @@ -124,10 +125,9 @@ func programRequiresPrepare(

//go:wasmimport programs program_prepare
func ProgramPrepare(
statedbPtr unsafe.Pointer,
wasmPtr unsafe.Pointer,
moduleHashPtr unsafe.Pointer,
addressForLoggingPtr unsafe.Pointer,
codePtr unsafe.Pointer,
codeSize uint64,
Copy link
Contributor

Choose a reason for hiding this comment

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

we need wasmSize instea of codeSize, and right after wasmPtr

codehashPtr unsafe.Pointer,
maxWasmSize uint32,
Expand Down Expand Up @@ -156,21 +156,25 @@ func startProgram(module uint32) uint32
//go:wasmimport programs send_response
func sendResponse(req_id uint32) uint32

func handleProgramPrepare(statedb vm.StateDB, moduleHash common.Hash, addressForLogging common.Address, code []byte, codehash common.Hash, params *StylusParams, time uint64, debugMode bool, program Program, runCtx *core.MessageRunContext) []byte {
func handleProgramPrepare(statedb vm.StateDB, moduleHash common.Hash, programAddress common.Address, code []byte, codehash common.Hash, params *StylusParams, time uint64, debugMode bool, program Program, runCtx *core.MessageRunContext) []byte {
requiresPrepare := programRequiresPrepare(unsafe.Pointer(&moduleHash[0]))
if requiresPrepare != 0 {
var debugInt uint32
if debugMode {
debugInt = 1
}

wasm, err := getWasm(statedb, programAddress, params)
Copy link
Contributor Author

@bragaigor bragaigor Jan 28, 2026

Choose a reason for hiding this comment

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

we could use getWasmFromContractCode which is called by getWasm. My concern comes from this comment:

// addressForLogging may be empty or may not correspond to the code, so we need to be careful to use the code passed in separately
wasm, err := getWasmFromContractCode(statedb, code, params, false)

Copy link
Member

@KolbyML KolbyML Jan 28, 2026

Choose a reason for hiding this comment

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

If we used getWasmFromContractCode make sure the isActivation flag is set to true, because handle is only used in ethCall? so it is simulating contract activation?, other than that for the potential edgecase you are presenting, I trust your judgement

Copy link
Contributor

Choose a reason for hiding this comment

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

the comment is exactly why you want to use getWasmFromContractCode and not getWasm. There are weird edgeCases like delegateCall and reading code from programAddressForLogging might not be correct, but the code you have here is correct and can be passed to getWasmFromCode

if err != nil {
panic(fmt.Sprintf("failed to get wasm for program, program address: %v, err: %v", programAddress.Hex(), err))
}

codeSize := uint64(len(code))

ProgramPrepare(
unsafe.Pointer(&statedb),
unsafe.Pointer(&wasm),
unsafe.Pointer(&moduleHash),
unsafe.Pointer(&addressForLogging),
unsafe.Pointer(&code),
unsafe.Pointer(&programAddress),
codeSize,
unsafe.Pointer(&codehash),
params.MaxWasmSize,
Expand Down
2 changes: 2 additions & 0 deletions changelog/bragaigor-nit-4407.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
### Changed
- update ProgramPrepare to accept wasm instead of statedb and code
3 changes: 1 addition & 2 deletions crates/jit/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,9 @@ pub fn program_requires_prepare(
/// program_prepare
pub fn program_prepare(
mut _env: WasmEnvMut,
_state_ptr: GuestPtr,
_wasm_ptr: GuestPtr,
_module_hash_ptr: GuestPtr,
_address_for_logging_ptr: GuestPtr,
_code_ptr: GuestPtr,
_code_size: u64,
_code_hash_ptr: GuestPtr,
_max_wasm_size: u32,
Expand Down
3 changes: 1 addition & 2 deletions crates/wasm-libraries/user-host/src/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,9 @@ pub unsafe extern "C" fn programs__program_requires_prepare(_module_hash_ptr: Gu
/// consumes activated program module hash and wasm code
#[no_mangle]
pub unsafe extern "C" fn programs__program_prepare(
_state_ptr: GuestPtr,
_wasm_ptr: GuestPtr,
_module_hash_ptr: GuestPtr,
_address_for_logging_ptr: GuestPtr,
_code_ptr: GuestPtr,
_code_size: u64,
_code_hash_ptr: GuestPtr,
_max_wasm_size: u32,
Expand Down
Loading