Skip to content
This repository was archived by the owner on Oct 28, 2021. It is now read-only.

Commit 5b0accd

Browse files
committed
interpreter: Inline throws
1 parent 6848f24 commit 5b0accd

File tree

3 files changed

+39
-84
lines changed

3 files changed

+39
-84
lines changed

libaleth-interpreter/VM.cpp

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ uint64_t VM::gasForMem(intx::uint512 const& _size)
148148
void VM::updateIOGas()
149149
{
150150
if (m_io_gas < m_runGas)
151-
throwOutOfGas();
151+
throw EVMC_OUT_OF_GAS;
152152
m_io_gas -= m_runGas;
153153
}
154154

@@ -158,7 +158,7 @@ void VM::updateGas()
158158
m_runGas += toInt63(gasForMem(m_newMemSize) - gasForMem(m_mem.size()));
159159
m_runGas += (VMSchedule::copyGas * ((m_copyMemSize + 31) / 32));
160160
if (m_io_gas < m_runGas)
161-
throwOutOfGas();
161+
throw EVMC_OUT_OF_GAS;
162162
}
163163

164164
void VM::updateMem(uint64_t _newMem)
@@ -239,9 +239,9 @@ void VM::interpretCases()
239239
{
240240
ON_OP();
241241
if (m_rev < EVMC_CONSTANTINOPLE)
242-
throwBadInstruction();
242+
throw EVMC_BAD_JUMP_DESTINATION;
243243
if (m_message->flags & EVMC_STATIC)
244-
throwDisallowedStateChange();
244+
throw EVMC_STATIC_MODE_VIOLATION;
245245

246246
m_bounce = &VM::caseCreate;
247247
}
@@ -251,7 +251,7 @@ void VM::interpretCases()
251251
{
252252
ON_OP();
253253
if (m_message->flags & EVMC_STATIC)
254-
throwDisallowedStateChange();
254+
throw EVMC_STATIC_MODE_VIOLATION;
255255

256256
m_bounce = &VM::caseCreate;
257257
}
@@ -264,11 +264,11 @@ void VM::interpretCases()
264264
{
265265
ON_OP();
266266
if (m_OP == Instruction::DELEGATECALL && m_rev < EVMC_HOMESTEAD)
267-
throwBadInstruction();
267+
throw EVMC_UNDEFINED_INSTRUCTION;
268268
if (m_OP == Instruction::STATICCALL && m_rev < EVMC_BYZANTIUM)
269-
throwBadInstruction();
269+
throw EVMC_UNDEFINED_INSTRUCTION;
270270
if (m_OP == Instruction::CALL && m_message->flags & EVMC_STATIC && m_SP[2] != 0)
271-
throwDisallowedStateChange();
271+
throw EVMC_STATIC_MODE_VIOLATION;
272272
m_bounce = &VM::caseCall;
273273
}
274274
BREAK
@@ -291,22 +291,24 @@ void VM::interpretCases()
291291
{
292292
// Pre-byzantium
293293
if (m_rev < EVMC_BYZANTIUM)
294-
throwBadInstruction();
294+
throw EVMC_UNDEFINED_INSTRUCTION;
295295

296296
ON_OP();
297297
m_copyMemSize = 0;
298298
updateMem(memNeed(m_SP[0], m_SP[1]));
299299
updateIOGas();
300300

301-
throwRevertInstruction(static_cast<uint64_t>(m_SP[0]), static_cast<uint64_t>(m_SP[1]));
301+
m_output = owning_bytes_ref{
302+
std::move(m_mem), static_cast<uint64_t>(m_SP[0]), static_cast<uint64_t>(m_SP[1])};
303+
throw EVMC_REVERT;
302304
}
303305
BREAK;
304306

305307
CASE(SELFDESTRUCT)
306308
{
307309
ON_OP();
308310
if (m_message->flags & EVMC_STATIC)
309-
throwDisallowedStateChange();
311+
throw EVMC_STATIC_MODE_VIOLATION;
310312

311313
auto const destination = intx::be::trunc<evmc::address>(m_SP[0]);
312314

@@ -393,7 +395,7 @@ void VM::interpretCases()
393395
{
394396
ON_OP();
395397
if (m_message->flags & EVMC_STATIC)
396-
throwDisallowedStateChange();
398+
throw EVMC_STATIC_MODE_VIOLATION;
397399

398400
logGasMem();
399401
updateIOGas();
@@ -410,7 +412,7 @@ void VM::interpretCases()
410412
{
411413
ON_OP();
412414
if (m_message->flags & EVMC_STATIC)
413-
throwDisallowedStateChange();
415+
throw EVMC_STATIC_MODE_VIOLATION;
414416

415417
logGasMem();
416418
updateIOGas();
@@ -430,7 +432,7 @@ void VM::interpretCases()
430432
{
431433
ON_OP();
432434
if (m_message->flags & EVMC_STATIC)
433-
throwDisallowedStateChange();
435+
throw EVMC_STATIC_MODE_VIOLATION;
434436

435437
logGasMem();
436438
updateIOGas();
@@ -451,7 +453,7 @@ void VM::interpretCases()
451453
{
452454
ON_OP();
453455
if (m_message->flags & EVMC_STATIC)
454-
throwDisallowedStateChange();
456+
throw EVMC_STATIC_MODE_VIOLATION;
455457

456458
logGasMem();
457459
updateIOGas();
@@ -472,7 +474,7 @@ void VM::interpretCases()
472474
{
473475
ON_OP();
474476
if (m_message->flags & EVMC_STATIC)
475-
throwDisallowedStateChange();
477+
throw EVMC_STATIC_MODE_VIOLATION;
476478

477479
logGasMem();
478480
updateIOGas();
@@ -681,7 +683,7 @@ void VM::interpretCases()
681683
{
682684
// Pre-constantinople
683685
if (m_rev < EVMC_CONSTANTINOPLE)
684-
throwBadInstruction();
686+
throw EVMC_UNDEFINED_INSTRUCTION;
685687

686688
ON_OP();
687689
updateIOGas();
@@ -697,7 +699,7 @@ void VM::interpretCases()
697699
{
698700
// Pre-constantinople
699701
if (m_rev < EVMC_CONSTANTINOPLE)
700-
throwBadInstruction();
702+
throw EVMC_UNDEFINED_INSTRUCTION;
701703

702704
ON_OP();
703705
updateIOGas();
@@ -713,7 +715,7 @@ void VM::interpretCases()
713715
{
714716
// Pre-constantinople
715717
if (m_rev < EVMC_CONSTANTINOPLE)
716-
throwBadInstruction();
718+
throw EVMC_UNDEFINED_INSTRUCTION;
717719

718720
ON_OP();
719721
updateIOGas();
@@ -781,7 +783,7 @@ void VM::interpretCases()
781783
CASE(JUMPTO) CASE(JUMPIF) CASE(JUMPV) CASE(JUMPSUB) CASE(JUMPSUBV) CASE(RETURNSUB)
782784
CASE(BEGINSUB) CASE(BEGINDATA) CASE(GETLOCAL) CASE(PUTLOCAL)
783785
{
784-
throwBadInstruction();
786+
throw EVMC_UNDEFINED_INSTRUCTION;
785787
}
786788
CONTINUE
787789

@@ -817,7 +819,7 @@ void VM::interpretCases()
817819
CASE(XPUT)
818820
CASE(XGET)
819821
CASE(XSWIZZLE)
820-
CASE(XSHUFFLE) { throwBadInstruction(); }
822+
CASE(XSHUFFLE) { throw EVMC_UNDEFINED_INSTRUCTION; }
821823
CONTINUE
822824

823825
CASE(ADDRESS)
@@ -905,7 +907,7 @@ void VM::interpretCases()
905907
CASE(RETURNDATASIZE)
906908
{
907909
if (m_rev < EVMC_BYZANTIUM)
908-
throwBadInstruction();
910+
throw EVMC_UNDEFINED_INSTRUCTION;
909911

910912
ON_OP();
911913
updateIOGas();
@@ -950,10 +952,10 @@ void VM::interpretCases()
950952
{
951953
ON_OP();
952954
if (m_rev < EVMC_BYZANTIUM)
953-
throwBadInstruction();
955+
throw EVMC_UNDEFINED_INSTRUCTION;
954956
intx::uint512 const endOfAccess = intx::uint512(m_SP[1]) + intx::uint512(m_SP[2]);
955957
if (m_returnData.size() < endOfAccess)
956-
throwBufferOverrun();
958+
throw EVMC_INVALID_MEMORY_ACCESS;
957959

958960
m_copyMemSize = toInt63(m_SP[2]);
959961
updateMem(memNeed(m_SP[0], m_SP[2]));
@@ -967,7 +969,7 @@ void VM::interpretCases()
967969
{
968970
ON_OP();
969971
if (m_rev < EVMC_CONSTANTINOPLE)
970-
throwBadInstruction();
972+
throw EVMC_UNDEFINED_INSTRUCTION;
971973

972974
updateIOGas();
973975

@@ -1091,7 +1093,7 @@ void VM::interpretCases()
10911093
ON_OP();
10921094

10931095
if (m_rev < EVMC_ISTANBUL)
1094-
throwBadInstruction();
1096+
throw EVMC_UNDEFINED_INSTRUCTION;
10951097

10961098
updateIOGas();
10971099

@@ -1104,7 +1106,7 @@ void VM::interpretCases()
11041106
ON_OP();
11051107

11061108
if (m_rev < EVMC_ISTANBUL)
1107-
throwBadInstruction();
1109+
throw EVMC_UNDEFINED_INSTRUCTION;
11081110

11091111
updateIOGas();
11101112

@@ -1138,7 +1140,7 @@ void VM::interpretCases()
11381140
m_SPP[0] = m_pool[off];
11391141
TRACE_VAL(2, "Retrieved pooled const", m_SPP[0]);
11401142
#else
1141-
throwBadInstruction();
1143+
throw EVMC_UNDEFINED_INSTRUCTION;
11421144
#endif
11431145
}
11441146
CONTINUE
@@ -1225,7 +1227,7 @@ void VM::interpretCases()
12251227

12261228
m_PC = uint64_t(m_SP[0]);
12271229
#else
1228-
throwBadInstruction();
1230+
throw EVMC_UNDEFINED_INSTRUCTION;
12291231
#endif
12301232
}
12311233
CONTINUE
@@ -1241,7 +1243,7 @@ void VM::interpretCases()
12411243
else
12421244
++m_PC;
12431245
#else
1244-
throwBadInstruction();
1246+
throw EVMC_UNDEFINED_INSTRUCTION;
12451247
#endif
12461248
}
12471249
CONTINUE
@@ -1304,10 +1306,10 @@ void VM::interpretCases()
13041306
{
13051307
ON_OP();
13061308
if (m_message->flags & EVMC_STATIC)
1307-
throwDisallowedStateChange();
1309+
throw EVMC_STATIC_MODE_VIOLATION;
13081310

13091311
if (m_rev >= EVMC_ISTANBUL && m_io_gas <= VMSchedule::callStipend)
1310-
throwOutOfGas();
1312+
throw EVMC_OUT_OF_GAS;
13111313

13121314
auto const key = intx::be::store<evmc_uint256be>(m_SP[0]);
13131315
auto const value = intx::be::store<evmc_uint256be>(m_SP[1]);
@@ -1373,9 +1375,9 @@ void VM::interpretCases()
13731375
CASE(INVALID) DEFAULT
13741376
{
13751377
if (m_OP == Instruction::INVALID)
1376-
throwInvalidInstruction();
1378+
throw EVMC_INVALID_INSTRUCTION;
13771379
else
1378-
throwBadInstruction();
1380+
throw EVMC_UNDEFINED_INSTRUCTION;
13791381
}
13801382
}
13811383
WHILE_CASES

libaleth-interpreter/VM.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,6 @@ class VM
118118

119119
const evmc_tx_context& getTxContext();
120120

121-
static void throwOutOfGas();
122-
static void throwInvalidInstruction();
123-
static void throwBadInstruction();
124-
static void throwBadJumpDestination();
125-
void throwRevertInstruction(uint64_t _offset, uint64_t _size);
126-
static void throwDisallowedStateChange();
127-
static void throwBufferOverrun();
128-
129121
std::vector<uint64_t> m_beginSubs;
130122
std::vector<uint64_t> m_jumpDests;
131123
int64_t verifyJumpDest(intx::uint256 const& _dest, bool _throw = true);
@@ -146,7 +138,7 @@ class VM
146138
{
147139
// check for overflow
148140
if (v > 0x7FFFFFFFFFFFFFFF)
149-
throwOutOfGas();
141+
throw EVMC_OUT_OF_GAS;
150142
uint64_t w = uint64_t(v);
151143
return w;
152144
}
@@ -155,7 +147,7 @@ class VM
155147
{
156148
// check for overflow
157149
if (v > 0x7FFF)
158-
throwOutOfGas();
150+
throw EVMC_OUT_OF_GAS;
159151
uint64_t w = uint64_t(v);
160152
return w;
161153
}

libaleth-interpreter/VMCalls.cpp

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,45 +22,6 @@ void VM::copyDataToMemory(bytesConstRef _data, intx::uint256*_sp)
2222
std::memset(m_mem.data() + offset + sizeToBeCopied, 0, size - sizeToBeCopied);
2323
}
2424

25-
26-
// consolidate exception throws to avoid spraying boost code all over interpreter
27-
28-
void VM::throwOutOfGas()
29-
{
30-
throw EVMC_OUT_OF_GAS;
31-
}
32-
33-
void VM::throwInvalidInstruction()
34-
{
35-
throw EVMC_INVALID_INSTRUCTION;
36-
}
37-
38-
void VM::throwBadInstruction()
39-
{
40-
throw EVMC_UNDEFINED_INSTRUCTION;
41-
}
42-
43-
void VM::throwBadJumpDestination()
44-
{
45-
throw EVMC_BAD_JUMP_DESTINATION;
46-
}
47-
48-
void VM::throwDisallowedStateChange()
49-
{
50-
throw EVMC_STATIC_MODE_VIOLATION;
51-
}
52-
53-
void VM::throwRevertInstruction(uint64_t _offset, uint64_t _size)
54-
{
55-
m_output = owning_bytes_ref{std::move(m_mem), _offset, _size};
56-
throw EVMC_REVERT;
57-
}
58-
59-
void VM::throwBufferOverrun()
60-
{
61-
throw EVMC_INVALID_MEMORY_ACCESS;
62-
}
63-
6425
int64_t VM::verifyJumpDest(intx::uint256 const& _dest, bool _throw)
6526
{
6627
// check for overflow
@@ -73,7 +34,7 @@ int64_t VM::verifyJumpDest(intx::uint256 const& _dest, bool _throw)
7334
return pc;
7435
}
7536
if (_throw)
76-
throwBadJumpDestination();
37+
throw EVMC_BAD_JUMP_DESTINATION;
7738
return -1;
7839
}
7940

0 commit comments

Comments
 (0)