Skip to content
This repository was archived by the owner on Jan 5, 2019. It is now read-only.

Commit 709684a

Browse files
lrettigaxic
authored andcommitted
Add basic/untested support for Byzantium opcodes
1 parent eb40b00 commit 709684a

File tree

5 files changed

+35
-2
lines changed

5 files changed

+35
-2
lines changed

include/evm2wasm.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ enum class opcodeEnum
4747
GASPRICE,
4848
EXTCODESIZE,
4949
EXTCODECOPY,
50+
RETURNDATASIZE,
51+
RETURNDATACOPY,
5052
BLOCKHASH,
5153
COINBASE,
5254
TIMESTAMP,
@@ -75,8 +77,9 @@ enum class opcodeEnum
7577
RETURN,
7678
DELEGATECALL,
7779
STATICCALL,
78-
SELFDESTRUCT,
80+
REVERT,
7981
INVALID,
82+
SELFDESTRUCT,
8083
bswap_i32,
8184
bswap_i64,
8285
bswap_m128,
@@ -164,6 +167,8 @@ static std::map<int, std::tuple<opcodeEnum, int, int, int>> codes = {
164167
{0x3a, Opcode{opcodeEnum::GASPRICE, 0, 0, 1}},
165168
{0x3b, Opcode{opcodeEnum::EXTCODESIZE, 0, 1, 1}},
166169
{0x3c, Opcode{opcodeEnum::EXTCODECOPY, 0, 4, 0}},
170+
{0x3d, Opcode{opcodeEnum::RETURNDATASIZE, 0, 0, 1}},
171+
{0x3e, Opcode{opcodeEnum::RETURNDATACOPY, 0, 3, 0}},
167172

168173
// "0x40" range - block operations
169174
{0x40, Opcode{opcodeEnum::BLOCKHASH, 0, 1, 1}},
@@ -270,6 +275,8 @@ static std::map<int, std::tuple<opcodeEnum, int, int, int>> codes = {
270275
{0xfa, Opcode{opcodeEnum::STATICCALL, 0, 6, 1}},
271276

272277
// "0x70", range - other
278+
{0xfd, Opcode{opcodeEnum::REVERT, 0, 2, 0}},
279+
{0xfe, Opcode{opcodeEnum::INVALID, 0, 0, 0}},
273280
{0xff, Opcode{opcodeEnum::SELFDESTRUCT, 0, 1, 0}}};
274281

275282
static std::map<opcodeEnum, std::vector<opcodeEnum>> depMap = {
@@ -306,6 +313,7 @@ static std::map<opcodeEnum, std::vector<opcodeEnum>> depMap = {
306313
{opcodeEnum::EXTCODECOPY, {opcodeEnum::bswap_m256, opcodeEnum::callback, opcodeEnum::memusegas,
307314
opcodeEnum::check_overflow, opcodeEnum::memset}},
308315
{opcodeEnum::EXTCODESIZE, {opcodeEnum::callback_32, opcodeEnum::bswap_m256}},
316+
{opcodeEnum::RETURNDATACOPY, {opcodeEnum::memusegas, opcodeEnum::check_overflow, opcodeEnum::memset}},
309317
{opcodeEnum::LOG, {opcodeEnum::memusegas, opcodeEnum::check_overflow}},
310318
{opcodeEnum::BLOCKHASH, {opcodeEnum::check_overflow, opcodeEnum::callback_256}},
311319
{opcodeEnum::SHA3, {opcodeEnum::memusegas, opcodeEnum::bswap_m256, opcodeEnum::check_overflow,
@@ -326,6 +334,7 @@ static std::map<opcodeEnum, std::vector<opcodeEnum>> depMap = {
326334
{opcodeEnum::CREATE, {opcodeEnum::bswap_m256, opcodeEnum::bswap_m160, opcodeEnum::callback_160,
327335
opcodeEnum::memusegas, opcodeEnum::check_overflow}},
328336
{opcodeEnum::RETURN, {opcodeEnum::memusegas, opcodeEnum::check_overflow}},
337+
{opcodeEnum::REVERT, {opcodeEnum::memusegas, opcodeEnum::check_overflow}},
329338
{opcodeEnum::BALANCE, {opcodeEnum::bswap_m256, opcodeEnum::callback_128}},
330339
{opcodeEnum::SELFDESTRUCT, {opcodeEnum::bswap_m256}},
331340
{opcodeEnum::SSTORE, {opcodeEnum::bswap_m256, opcodeEnum::callback}},

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const depMap = new Map([
3434
['CALLVALUE', ['bswap_m128']],
3535
['EXTCODECOPY', ['bswap_m256', 'callback', 'memusegas', 'check_overflow', 'memset']],
3636
['EXTCODESIZE', ['callback_32', 'bswap_m256']],
37+
['RETURNDATACOPY', ['memusegas', 'check_overflow', 'memset']],
3738
['LOG', ['memusegas', 'check_overflow']],
3839
['BLOCKHASH', ['check_overflow', 'callback_256']],
3940
['SHA3', ['memusegas', 'bswap_m256', 'check_overflow', 'keccak']],
@@ -43,6 +44,7 @@ const depMap = new Map([
4344
['CALLCODE', ['bswap_m256', 'callback', 'memusegas', 'check_overflow_i64', 'check_overflow', 'check_overflow_i64', 'memset', 'callback_32']],
4445
['CREATE', ['bswap_m256', 'bswap_m160', 'callback_160', 'memusegas', 'check_overflow']],
4546
['RETURN', ['memusegas', 'check_overflow']],
47+
['REVERT', ['memusegas', 'check_overflow']],
4648
['BALANCE', ['bswap_m256', 'callback_128']],
4749
['SELFDESTRUCT', ['bswap_m256']],
4850
['SSTORE', ['bswap_m256', 'callback']],
@@ -299,6 +301,7 @@ exports.evm2wast = function (evmCode, opts = {
299301
break
300302
case 'SELFDESTRUCT':
301303
case 'RETURN':
304+
case 'REVERT':
302305
segment += `(call $${op.name}) (br $done)\n`
303306
if (jumpFound) {
304307
pc = findNextJumpDest(evmCode, pc)

libs/evm2wasm/evm2wasm.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ string evm2wast(const vector<uint8_t>& evmCode, bool stackTrace, bool useAsyncAP
318318
break;
319319
case opcodeEnum::SELFDESTRUCT:
320320
case opcodeEnum::RETURN:
321+
case opcodeEnum::REVERT:
321322
segment << "(call $" << opcodeToString(op.name) << ") (br $done)\n";
322323
if (jumpFound)
323324
{

opcodes.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ const codes = {
4444
0x3a: ['GASPRICE', 0, 0, 1],
4545
0x3b: ['EXTCODESIZE', 0, 1, 1],
4646
0x3c: ['EXTCODECOPY', 0, 4, 0],
47+
0x3d: ['RETURNDATASIZE', 0, 0, 1],
48+
0x3e: ['RETURNDATACOPY', 0, 3, 0],
4749

4850
// '0x40' range - block operations
4951
0x40: ['BLOCKHASH', 0, 1, 1],
@@ -150,11 +152,14 @@ const codes = {
150152
0xfa: ['STATICCALL', 0, 6, 1],
151153

152154
// '0x70', range - other
155+
0xfd: ['REVERT', 0, 2, 0],
156+
0xfe: ['INVALID', 0, 0, 0],
153157
0xff: ['SELFDESTRUCT', 0, 1, 0]
154158
}
155159

156160
module.exports = function (op) {
157-
const code = codes[op] ? codes[op] : ['INVALID', 0, 0, 0]
161+
// Map invalid opcodes to the INVALID opcode
162+
const code = codes[op] ? codes[op] : codes[0xfe]
158163
let opcode = code[0]
159164
let number
160165

wasm/generateInterface.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,16 @@ const interfaceManifest = {
161161
input: ['gasLimit', 'address', 'readOffset', 'length'],
162162
output: ['i32']
163163
},
164+
RETURNDATACOPY: {
165+
name: 'returnDataCopy',
166+
input: ['writeOffset', 'i32', 'length'],
167+
output: []
168+
},
169+
RETURNDATASIZE: {
170+
name: 'getReturnDataSize',
171+
input: [],
172+
output: ['i32']
173+
},
164174
SSTORE: {
165175
name: 'storageStore',
166176
async: true,
@@ -182,6 +192,11 @@ const interfaceManifest = {
182192
name: 'return',
183193
input: ['readOffset', 'length'],
184194
output: []
195+
},
196+
REVERT: {
197+
name: 'revert',
198+
input: ['readOffset', 'length'],
199+
output: []
185200
}
186201
}
187202

0 commit comments

Comments
 (0)