This repository has been archived by the owner on Jan 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathindex.js
527 lines (483 loc) · 16.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
const BN = require('bn.js')
const wast2wasm = require('wast2wasm')
const ethUtil = require('ethereumjs-util')
const opcodes = require('./opcodes.js')
const wastSyncInterface = require('./wasm/wast.json')
const wastAsyncInterface = require('./wasm/wast-async.json')
const wabt = require('./wabt.js')
// map to track dependent WASM functions
const depMap = new Map([
['callback_256', ['bswap_m256']],
['callback_160', ['bswap_m160']],
['callback_128', ['bswap_m128']],
['bswap_m256', ['bswap_i64']],
['bswap_m128', ['bswap_i64']],
['bswap_m160', ['bswap_i64', 'bswap_i32']],
['keccak', ['memcpy', 'memset']],
['mod_320', ['iszero_320', 'gte_320']],
['mod_512', ['iszero_512', 'gte_512']],
['MOD', ['iszero_256', 'gte_256']],
['ADDMOD', ['MOD', 'ADD', 'mod_320']],
['MULMOD', ['mod_512']],
['SDIV', ['iszero_256', 'gte_256']],
['SMOD', ['iszero_256', 'gte_256']],
['DIV', ['iszero_256', 'gte_256']],
['EXP', ['iszero_256', 'mul_256']],
['MUL', ['mul_256']],
['ISZERO', ['iszero_256']],
['MSTORE', ['memusegas', 'bswap_m256', 'check_overflow']],
['MLOAD', ['memusegas', 'bswap_m256', 'check_overflow']],
['MSTORE8', ['memusegas', 'check_overflow']],
['CODECOPY', ['callback', 'memusegas', 'check_overflow', 'memset']],
['CALLDATALOAD', ['bswap_m256', 'bswap_i64', 'check_overflow']],
['CALLDATACOPY', ['memusegas', 'check_overflow', 'memset']],
['CALLVALUE', ['bswap_m128']],
['EXTCODECOPY', ['bswap_m256', 'callback', 'memusegas', 'check_overflow', 'memset']],
['EXTCODESIZE', ['callback_32', 'bswap_m256']],
['LOG', ['memusegas', 'check_overflow']],
['BLOCKHASH', ['check_overflow', 'callback_256']],
['SHA3', ['memusegas', 'bswap_m256', 'check_overflow', 'keccak']],
['CALL', ['bswap_m256', 'memusegas', 'check_overflow_i64', 'check_overflow', 'memset', 'callback_32']],
['DELEGATECALL', ['callback', 'memusegas', 'check_overflow', 'memset']],
['CALLCODE', ['bswap_m256', 'callback', 'memusegas', 'check_overflow', 'memset', 'callback_32']],
['CREATE', ['bswap_m256', 'bswap_m160', 'callback_160', 'memusegas', 'check_overflow']],
['RETURN', ['memusegas', 'check_overflow']],
['BALANCE', ['bswap_m256', 'callback_128']],
['SELFDESTRUCT', ['bswap_m256']],
['SSTORE', ['bswap_m256', 'callback']],
['SLOAD', ['callback_256']],
['CODESIZE', ['callback_32']],
['DIFFICULTY', ['bswap_m256']],
['COINBASE', ['bswap_m160']],
['ORIGIN', ['bswap_m160']],
['ADDRESS', ['bswap_m160']],
['CALLER', ['bswap_m160']]
])
// maps the async ops to their call back function
const callbackFuncs = new Map([
['SSTORE', '$callback'],
['SLOAD', '$callback_256'],
['CREATE', '$callback_160'],
['CALL', '$callback_32'],
['DELEGATECALL', '$callback'],
['CALLCODE', '$callback_32'],
['EXTCODECOPY', '$callback'],
['EXTCODESIZE', '$callback_32'],
['CODECOPY', '$callback'],
['CODESIZE', '$callback_32'],
['BALANCE', '$callback_128'],
['BLOCKHASH', '$callback_256']
])
/**
* compiles evmCode to wasm in the binary format
* @param {Array} evmCode
* @param {Object} opts
* @param {boolean} opts.stackTrace if `true` generates an runtime EVM stack trace (default: false)
* @param {boolean} opts.inlineOps if `true` inlines the EVM1 operations (default: true)
* @param {boolean} opts.wabt use wabt to compile wast to wasm instad of the built in JS module (default: false)
* @param {String} opts.testName is the name used for the wast file (default: 'temp')
* @return {string}
*/
exports.evm2wasm = function (evmCode, opts = {
'stackTrace': false,
'useAsyncAPI': false,
'inlineOps': true,
'testName': 'temp'
}) {
const wast = exports.evm2wast(evmCode, opts)
const testName = opts.testName
if (opts.wabt) {
return wabt.compile(wast, testName)
} else {
return wast2wasm(wast)
}
}
/**
* Transcompiles EVM code to ewasm in the sexpression text format. The EVM code
* is broken into segments and each instruction in those segments is replaced
* with a `call` to wasm function that does the equivalent operation. Each
* opcode function takes in and returns the stack pointer.
*
* Segments are sections of EVM code in between flow control
* opcodes (JUMPI. JUMP).
* All segments start at
* * the beginning for EVM code
* * a GAS opcode
* * a JUMPDEST opcode
* * After a JUMPI opcode
* @param {Integer} evmCode the evm byte code
* @param {Object} opts
* @param {boolean} opts.stackTrace if `true` generates a stack trace (default: false)
* @param {boolean} opts.inlineOps if `true` inlines the EVM1 operations (default: true)
* @return {string}
*/
exports.evm2wast = function (evmCode, opts = {
'stackTrace': false,
'useAsyncAPI': false,
'inlineOps': true
}) {
// adds stack height checks to the beginning of a segment
function addStackCheck () {
let check = ''
if (segmentStackHigh !== 0) {
check = `(if (i32.gt_s (get_global $sp) (i32.const ${(1023 - segmentStackHigh) * 32}))
(then (unreachable)))`
}
if (segmentStackLow !== 0) {
check += `(if (i32.lt_s (get_global $sp) (i32.const ${-segmentStackLow * 32 - 32}))
(then (unreachable)))`
}
segment = check + segment
segmentStackHigh = 0
segmentStackLow = 0
segmentStackDeta = 0
}
// add a metering statment at the beginning of a segment
function addMetering () {
wast += `(call $useGas (i64.const ${gasCount})) ` + segment
segment = ''
gasCount = 0
}
// finishes off a segment
function endSegment () {
segment += ')'
addStackCheck()
addMetering()
}
// this keep track of the opcode we have found so far. This will be used to
// to figure out what .wast files to include
const opcodesUsed = new Set()
const ignoredOps = new Set(['JUMP', 'JUMPI', 'JUMPDEST', 'POP', 'STOP', 'INVALID'])
let callbackTable = []
// an array of found segments
const jumpSegments = []
// the transcompiled EVM code
let wast = ''
let segment = ''
// keeps track of the gas that each section uses
let gasCount = 0
// used for pruning dead code
let jumpFound = false
// the accumlitive stack difference for the current segmnet
let segmentStackDeta = 0
let segmentStackHigh = 0
let segmentStackLow = 0
for (let pc = 0; pc < evmCode.length; pc++) {
const opint = evmCode[pc]
const op = opcodes(opint)
let bytes
gasCount += op.fee
segmentStackDeta += op.on
if (segmentStackDeta > segmentStackHigh) {
segmentStackHigh = segmentStackDeta
}
segmentStackDeta -= op.off
if (segmentStackDeta < segmentStackLow) {
segmentStackLow = segmentStackDeta
}
switch (op.name) {
case 'JUMP':
jumpFound = true
segment += `;; jump
(set_local $jump_dest (call $check_overflow
(i64.load (get_global $sp))
(i64.load (i32.add (get_global $sp) (i32.const 8)))
(i64.load (i32.add (get_global $sp) (i32.const 16)))
(i64.load (i32.add (get_global $sp) (i32.const 24)))))
(set_global $sp (i32.sub (get_global $sp) (i32.const 32)))
(br $loop)`
opcodesUsed.add('check_overflow')
pc = findNextJumpDest(evmCode, pc)
break
case 'JUMPI':
jumpFound = true
segment += `(set_local $jump_dest (call $check_overflow
(i64.load (get_global $sp))
(i64.load (i32.add (get_global $sp) (i32.const 8)))
(i64.load (i32.add (get_global $sp) (i32.const 16)))
(i64.load (i32.add (get_global $sp) (i32.const 24)))))
(set_global $sp (i32.sub (get_global $sp) (i32.const 64)))
(br_if $loop (i32.eqz (i64.eqz (i64.or
(i64.load (i32.add (get_global $sp) (i32.const 32)))
(i64.or
(i64.load (i32.add (get_global $sp) (i32.const 40)))
(i64.or
(i64.load (i32.add (get_global $sp) (i32.const 48)))
(i64.load (i32.add (get_global $sp) (i32.const 56)))
)
)
))))\n`
opcodesUsed.add('check_overflow')
addStackCheck()
addMetering()
break
case 'JUMPDEST':
endSegment()
jumpSegments.push({
number: pc,
type: 'jump_dest'
})
gasCount = 1
break
case 'GAS':
segment += `(call $GAS)\n`
addMetering()
break
case 'LOG':
segment += `(call $LOG (i32.const ${op.number}))\n`
break
case 'DUP':
case 'SWAP':
// adds the number on the stack to SWAP
segment += `(call $${op.name} (i32.const ${op.number - 1}))\n`
break
case 'PC':
segment += `(call $PC (i32.const ${pc}))\n`
break
case 'PUSH':
pc++
bytes = ethUtil.setLength(evmCode.slice(pc, pc += op.number), 32)
const bytesRounded = Math.ceil(op.number / 8)
let push = ''
let q = 0
// pad the remaining of the word with 0
for (; q < 4 - bytesRounded; q++) {
push = '(i64.const 0)' + push
}
for (; q < 4; q++) {
const int64 = bytes2int64(bytes.slice(q * 8, q * 8 + 8))
push = push + `(i64.const ${int64})`
}
segment += `(call $PUSH ${push})`
pc--
break
case 'POP':
// do nothing
break
case 'STOP':
segment += '(br $done)'
if (jumpFound) {
pc = findNextJumpDest(evmCode, pc)
} else {
// the rest is dead code
pc = evmCode.length
}
break
case 'SELFDESTRUCT':
case 'RETURN':
segment += `(call $${op.name}) (br $done)\n`
if (jumpFound) {
pc = findNextJumpDest(evmCode, pc)
} else {
// the rest is dead code
pc = evmCode.length
}
break
case 'INVALID':
segment = '(unreachable)'
pc = findNextJumpDest(evmCode, pc)
break
default:
if (opts.useAsyncAPI && callbackFuncs.has(op.name)) {
const cbFunc = callbackFuncs.get(op.name)
let index = callbackTable.indexOf(cbFunc)
if (index === -1) {
index = callbackTable.push(cbFunc) - 1
}
segment += `(call $${op.name} (i32.const ${index}))\n`
} else {
// use synchronous API
segment += `(call $${op.name})\n`
}
}
if (!ignoredOps.has(op.name)) {
opcodesUsed.add(op.name)
}
const stackDeta = op.on - op.off
// update the stack pointer
if (stackDeta !== 0) {
segment += `(set_global $sp (i32.add (get_global $sp) (i32.const ${stackDeta * 32})))\n`
}
// creates a stack trace
if (opts.stackTrace) {
segment += `(call $stackTrace (i32.const ${pc}) (i32.const ${opint}) (i32.const ${gasCount}) (get_global $sp))\n`
}
// adds the logic to save the stack pointer before exiting to wiat to for a callback
// note, this must be done before the sp is updated above^
if (opts.useAsyncAPI && callbackFuncs.has(op.name)) {
segment += `(set_global $cb_dest (i32.const ${jumpSegments.length + 1}))
(br $done))`
jumpSegments.push({
type: 'cb_dest'
})
}
}
endSegment()
wast = assembleSegments(jumpSegments) + wast + '))'
let wastFiles = wastSyncInterface // default to synchronous interface
if (opts.useAsyncAPI) {
wastFiles = wastAsyncInterface
}
let imports = []
let funcs = []
// inline EVM opcode implemention
if (opts.inlineOps) {
[funcs, imports] = exports.resolveFunctions(opcodesUsed, wastFiles)
}
// import stack trace function
if (opts.stackTrace) {
imports.push('(import "debug" "printMemHex" (func $printMem (param i32 i32)))')
imports.push('(import "debug" "print" (func $print (param i32)))')
imports.push('(import "debug" "evmTrace" (func $stackTrace (param i32 i32 i32 i32)))')
}
imports.push('(import "ethereum" "useGas" (func $useGas (param i64)))')
funcs.push(wast)
wast = exports.buildModule(funcs, imports, callbackTable)
return wast
}
// given an array for segments builds a wasm module from those segments
// @param {Array} segments
// @return {String}
function assembleSegments (segments) {
let wasm = buildJumpMap(segments)
segments.forEach((seg, index) => {
wasm = `(block $${index + 1} ${wasm}`
})
return `
(func $main
(export "main")
(local $jump_dest i32)
(set_local $jump_dest (i32.const -1))
(block $done
(loop $loop
${wasm}`
}
// Builds the Jump map, which maps EVM jump location to a block label
// @param {Array} segments
// @return {String}
function buildJumpMap (segments) {
let wasm = '(unreachable)'
let brTable = ''
segments.forEach((seg, index) => {
brTable += ' $' + (index + 1)
if (seg.type === 'jump_dest') {
wasm = `(if (i32.eq (get_local $jump_dest) (i32.const ${seg.number}))
(then (br $${index + 1}))
(else ${wasm}))`
}
})
wasm = `
(block $0
(if
(i32.eqz (get_global $init))
(then
(set_global $init (i32.const 1))
(br $0))
(else
;; the callback dest can never be in the first block
(if (i32.eq (get_global $cb_dest) (i32.const 0))
(then
${wasm}
)
(else
;; return callback destination and zero out $cb_dest
get_global $cb_dest
(set_global $cb_dest (i32.const 0))
(br_table $0 ${brTable})
)))))`
return wasm
}
// returns the index of the next jump destination opcode in given EVM code in an
// array and a starting index
// @param {Array} evmCode
// @param {Integer} index
// @return {Integer}
function findNextJumpDest (evmCode, i) {
for (; i < evmCode.length; i++) {
const opint = evmCode[i]
const op = opcodes(opint)
switch (op.name) {
case 'PUSH':
// skip add how many bytes where pushed
i += op.number
break
case 'JUMPDEST':
return --i
}
}
return --i
}
// converts 8 bytes into a int 64
// @param {Integer}
// @return {String}
function bytes2int64 (bytes) {
return new BN(bytes).fromTwos(64).toString()
}
// Ensure that dependencies are only imported once (use the Set)
// @param {Set} funcSet a set of wasm function that need to be linked to their dependencies
// @return {Set}
function resolveFunctionDeps (funcSet) {
let funcs = funcSet
for (let func of funcSet) {
const deps = depMap.get(func)
if (deps) {
for (const dep of deps) {
funcs.add(dep)
}
}
}
return funcs
}
/**
* given a Set of wasm function this return an array for wasm equivalents
* @param {Set} funcSet
* @return {Array}
*/
exports.resolveFunctions = function (funcSet, wastFiles) {
let funcs = []
let imports = []
for (let func of resolveFunctionDeps(funcSet)) {
funcs.push(wastFiles[func].wast)
imports.push(wastFiles[func].imports)
}
return [funcs, imports]
}
/**
* builds a wasm module
* @param {Array} funcs the function to include in the module
* @param {Array} imports the imports for the module's import table
* @return {string}
*/
exports.buildModule = function (funcs, imports = [], callbacks = []) {
let funcStr = ''
for (let func of funcs) {
funcStr += func
}
let callbackTableStr = ''
if (callbacks.length) {
callbackTableStr = `
(table
(export "callback") ;; name of table
anyfunc
(elem ${callbacks.join(' ')}) ;; elements will have indexes in order
)`
}
return `
(module
${imports.join('\n')}
(global $cb_dest (mut i32) (i32.const 0))
(global $sp (mut i32) (i32.const -32))
(global $init (mut i32) (i32.const 0))
;; memory related global
(global $memstart i32 (i32.const 33832))
;; the number of 256 words stored in memory
(global $wordCount (mut i64) (i64.const 0))
;; what was charged for the last memory allocation
(global $prevMemCost (mut i64) (i64.const 0))
;; TODO: memory should only be 1, but can't resize right now
(memory 500)
(export "memory" (memory 0))
${callbackTableStr}
${funcStr}
)`
}