You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(To be able to resume a function where it ran out of gas or create a merkle tree of intermediate execution states, as in truebit)
I've tried to serialize a Wasm instance but then noticed the stack isn't accessible:
constfs=require('fs')constmetering=require('.')constsha256=require('simple-sha256')constwasm=fs.readFileSync('example/main.wasm')constpersist=require('wasm-persist')meteredWasm=persist.prepare(meteredWasm)varmeteredWasm=metering.meterWASM(wasm,{meterType: 'i32'})console.log(wasm)console.log(meteredWasm)//console.log(meteredWasm.keys())constmod=WebAssembly.Module(meteredWasm)letgasUsed=0varstep=100000varlimit=10000000vartotal=0instance=WebAssembly.Instance(mod,{'metering': {'usegas': (gas)=>{gasUsed+=gasif(total>limit){thrownewError('out of gas!')}elseif(gasUsed>=step){state=persist.hibernate(instance)varhsh=sha256.sync(JSON.stringify({instance,state}))console.log("step",hsh)total+=gasUsedgasUsed=0}}}})varresult=instance.exports.fac(1000)//console.log(sha256.sync(JSON.stringify(result)))console.log(`result:${result}, gas used ${total*1e-9}s`)// result:720, gas used 0.4177
My knowledge of Wasm semantics is limited. Would it be possible to inject the following:
keep a stack depth counter along with the instruction cost
grow the memory by this depth
store the entire stack in memory
call the metering function as usual
within the metering function, serialize the stack and the rest of the module that can already be accessed (code, memory etc.)
recover the stack from memory
continue execution
One problem with this might be that the value type on the stack is not known, I'm not sure if a module that uses two times i32.store instead of one i64.store is still valid.
Also, this doesn't save the call frames either.
Edit: I'm reading through the specs again now, one crucial limitation is:
The fact that the nested instruction sequence instr∗ must have type []→[t?] implies that it cannot access operands that have been pushed on the stack before the block was entered. This may be generalized in future versions of WebAssembly.
This seems to imply that the stack must have been mirrored to memory before any block or function call in order to recover it within it, which will be quite inefficient.
The text was updated successfully, but these errors were encountered:
After starting to modify warpy (an RPython Wasm JIT interpreter), I switched to extending https://github.com/xtuc/webassemblyjs/ instead. Here's a preview:
(To be able to resume a function where it ran out of gas or create a merkle tree of intermediate execution states, as in truebit)
I've tried to serialize a Wasm instance but then noticed the stack isn't accessible:
My knowledge of Wasm semantics is limited. Would it be possible to inject the following:
One problem with this might be that the value type on the stack is not known, I'm not sure if a module that uses two times i32.store instead of one i64.store is still valid.
Also, this doesn't save the call frames either.
Edit: I'm reading through the specs again now, one crucial limitation is:
The fact that the nested instruction sequence instr∗ must have type []→[t?] implies that it cannot access operands that have been pushed on the stack before the block was entered. This may be generalized in future versions of WebAssembly.
This seems to imply that the stack must have been mirrored to memory before any block or function call in order to recover it within it, which will be quite inefficient.
The text was updated successfully, but these errors were encountered: