git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 2a922dd52511e08d2503efb3f1e3e8b06b291cc4

Merge pull request #70 from ewasm/tables

use tables for callbacks instead of exports
wanderer authored on 1/6/2017, 3:15:18 AM
GitHub committed on 1/6/2017, 3:15:18 AM
Parent: 26787afda66ed5f43ebf8e40d4edefbbcc133e01
Parent: a51f04b0499d6c309d071a010595bc54b0086074

Files changed

EVMimports.jschanged
tests/interface/balance.wasmchanged
tests/interface/balance.wastchanged
tests/interface/call.wasmchanged
tests/interface/call.wastchanged
tests/interface/sstore.wasmchanged
tests/interface/sstore.wastchanged
tests/interfaceRunner.jschanged
vm.jschanged
EVMimports.jsView
@@ -187,9 +187,9 @@
187187 */
188188 callDataCopy (offset, dataOffset, length) {
189189 this.takeGas(3 + Math.ceil(length / 32) * 3)
190190
191- if (length) {
191+ if (length > 0 && offset >= 0 && dataOffset >= 0) {
192192 const callData = this.kernel.environment.callData.slice(dataOffset, dataOffset + length)
193193 this.setMemory(offset, length, callData)
194194 }
195195 }
@@ -201,8 +201,9 @@
201201 * @param {integer} dataOffset the offset in the input data
202202 */
203203 callDataCopy256 (offset, dataOffset) {
204204 this.takeGas(3)
205+
205206 const callData = this.kernel.environment.callData.slice(dataOffset, dataOffset + 32)
206207 this.setMemory(offset, U256_SIZE_BYTES, callData)
207208 }
208209
@@ -336,9 +337,9 @@
336337 */
337338 getBlockCoinbase (offset) {
338339 this.takeGas(2)
339340
340- this.setMemory(offset, ADDRESS_SIZE_BYTES, this.kernel.environment.coinbase.toMemory())
341+ this.setMemory(offset, ADDRESS_SIZE_BYTES, this.kernel.environment.block.header.coinbase)
341342 }
342343
343344 /**
344345 * Get the block’s timestamp.
@@ -603,11 +604,9 @@
603604 * @param {integer} offset the offset of the output data.
604605 * @param {integer} length the length of the output data.
605606 */
606607 return (offset, length) {
607- if (length) {
608- this.kernel.environment.returnValue = this.getMemory(offset, length).slice(0)
609- }
608+ this.kernel.environment.returnValue = this.getMemory(offset, length).slice(0)
610609 }
611610
612611 /**
613612 * Halt execution and register account for later deletion giving the remaining
@@ -620,14 +619,20 @@
620619 this.kernel.environment.gasRefund += 24000
621620 }
622621
623622 getMemory (offset, length) {
624- return new Uint8Array(this.kernel.memory, offset, length)
623+ if (offset >= 0 && length > 0) {
624+ return new Uint8Array(this.kernel.memory, offset, length)
625+ } else {
626+ return new Uint8Array([])
627+ }
625628 }
626629
627630 setMemory (offset, length, value) {
628- const memory = new Uint8Array(this.kernel.memory, offset, length)
629- memory.set(value)
631+ if (offset >= 0 && length > 0) {
632+ const memory = new Uint8Array(this.kernel.memory, offset, length)
633+ memory.set(value)
634+ }
630635 }
631636
632637 /*
633638 * Takes gas from the tank. Only needs to check if there's gas left to be taken,
tests/interface/balance.wasmView
@@ -1,5 +1,5 @@
11 asm 
22 ``ethereum
3-getBalancememorymain1
3+getBalancepmemorymaincallback A 
44 '
5-AAA @A)B��������Q@ A ]H���r�h)���oJ-�{
5+AAA @A)B��������Q@ A ]H���r�h)���oJ-�{
tests/interface/balance.wastView
@@ -4,12 +4,19 @@
44 (memory 1 )
55 (data (i32.const 0) "\5d\48\c1\01\89\04\a1\72\88\68\29\bb\bd\9c\6f\4a\2d\06\c4\7b")
66 (export "memory" (memory 0))
77 (export "main" (func $main))
8- (export "1" (func $callback))
98
9+ (table
10+ (export "callback")
11+ anyfunc
12+ (elem
13+ $callback
14+ )
15+ )
16+
1017 (func $main
11- (call $balance (i32.const 0) (i32.const 0) (i32.const 1))
18+ (call $balance (i32.const 0) (i32.const 0) (i32.const 0))
1219 )
1320
1421 (func $callback
1522 (block
tests/interface/call.wasmView
@@ -1,2 +1,2 @@
1-asm `~``ethereumcallmemorymain1
2-:+@AA6A4A����6B�AAA4AA8AA A F@
1+asm `~``ethereumcallpmemorymaincallback A 
2+:+@AA6A4A����6B�AAA4AA8AA A F@
tests/interface/call.wastView
@@ -3,8 +3,17 @@
33 (import "ethereum" "call" (func $call (param i64 i32 i32 i32 i32 i32 i32 i32) (result i32)))
44 (memory 1)
55 (export "memory" (memory 0))
66 (export "main" (func $main))
7+
8+ (table
9+ (export "callback")
10+ anyfunc
11+ (elem
12+ $callback
13+ )
14+ )
15+
716 (func $main
817 (block
918 ;; Memory layout:
1019 ;; 0 - 20 bytes: address (4)
@@ -12,14 +21,13 @@
1221 ;; 52 - 56 bytes: data (0x42004200)
1322 ;; 56 - 60 bytes: result
1423 (i32.store (i32.const 0) (i32.const 0x4))
1524 (i32.store (i32.const 52) (i32.const 0x42004200))
16- (call $call (i64.const 2000) (i32.const 0) (i32.const 20) (i32.const 52) (i32.const 4) (i32.const 56) (i32.const 4) (i32.const 1))
25+ (call $call (i64.const 2000) (i32.const 0) (i32.const 20) (i32.const 52) (i32.const 4) (i32.const 56) (i32.const 4) (i32.const 0))
1726 drop
1827 )
1928 )
2029
21- (export "1" (func $callback))
2230 (func $callback (param $result i32)
2331 (if (i32.eq (i32.const 1) (get_local $result))
2432 (return)
2533 )
tests/interface/sstore.wasmView
@@ -1,3 +1,3 @@
11 asm 
2-``0ethereum storageStoreethereum storageLoadmainmemory12
3-L~@AB���݄ƥ�7A�AA @A�A�A @A�)B���݄ƥ�R@
2+``0ethereum storageStoreethereum storageLoadpmemorycallbackmain A 
3+L~@AB���݄ƥ�7A�AA @A�A�A @A�)B���݄ƥ�R@
tests/interface/sstore.wastView
@@ -3,27 +3,35 @@
33 (import "ethereum" "storageStore" (func $sstore (param i32 i32 i32)))
44 (import "ethereum" "storageLoad" (func $sload (param i32 i32 i32)))
55
66 (memory 1)
7- (export "main" (func $main))
87 (export "memory" (memory 0))
8+
9+ (table
10+ (export "callback")
11+ anyfunc
12+ (elem
13+ $callback
14+ $callback2
15+ )
16+ )
17+
918 (func $main
19+ (export "main")
1020 (local $temp i64)
1121 (block
1222 ;; should roundtrip store and load a value from storage
1323 (i64.store (i32.const 0) (i64.const 173553719826446289))
14- (call $sstore (i32.const 64) (i32.const 0) (i32.const 1))
24+ (call $sstore (i32.const 64) (i32.const 0) (i32.const 0))
1525 )
1626 )
1727
18- (export "1" (func $callback))
1928 (func $callback
2029 (block
21- (call $sload (i32.const 64) (i32.const 64) (i32.const 2))
30+ (call $sload (i32.const 64) (i32.const 64) (i32.const 1))
2231 )
2332 )
2433
25- (export "2" (func $callback2))
2634 (func $callback2
2735 (block
2836 (if (i64.ne (i64.load (i32.const 64)) (i64.const 173553719826446289))
2937 (unreachable))
tests/interfaceRunner.jsView
@@ -3,16 +3,16 @@
33 const path = require('path')
44 const Vertex = require('merkle-trie')
55 const Address = require('../deps/address')
66 const U256 = require('../deps/u256')
7+const Block = require('../deps/block.js')
78
89 const Kernel = require('../index.js')
910 const Environment = require('../testEnvironment.js')
1011
1112 const dir = path.join(__dirname, '/interface')
1213 // get the test names
1314 let tests = fs.readdirSync(dir).filter((file) => file.endsWith('.wast'))
14-// tests = ['callDataSize.wast']
1515
1616 runTests(tests)
1717
1818 function runTests (tests) {
@@ -23,11 +23,12 @@
2323 const rootVertex = new Vertex()
2424 const code = fs.readFileSync(`${dir}/${testName}.wasm`)
2525 const envData = JSON.parse(fs.readFileSync(`${dir}/${testName}.json`).toString())
2626
27+ envData.block = new Block()
2728 envData.caller = new Address(envData.caller)
2829 envData.address = new Address(envData.address)
29- envData.coinbase = new Address(envData.coinbase)
30+ envData.block.header.coinbase = new Address(envData.coinbase)
3031 envData.origin = new Address(envData.origin)
3132 envData.callData = new Buffer(envData.callData.slice(2), 'hex')
3233 envData.callValue = new U256(envData.callValue)
3334
vm.jsView
@@ -36,9 +36,9 @@
3636 */
3737 pushOpsQueue (promise, callbackIndex, intefaceCallback) {
3838 this._opsQueue = Promise.all([this._opsQueue, promise]).then(values => {
3939 const result = intefaceCallback(values.pop())
40- this._instance.exports[callbackIndex.toString()](result)
40+ this._instance.exports.callback.get(callbackIndex)(result)
4141 })
4242 }
4343
4444 sendMessage (message) {

Built with git-ssb-web