Skip to content

Commit 39e1d45

Browse files
committed
Restrict opcodes to berlin hardfork
1 parent cedabe2 commit 39e1d45

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

packages/vm/lib/evm/opFns.ts

+25-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import BN = require('bn.js')
2-
import { keccak256, setLengthRight, TWO_POW256, MAX_INTEGER, KECCAK256_NULL } from 'ethereumjs-util'
2+
import * as utils from 'ethereumjs-util'
33
import { ERROR, VmError } from '../exceptions'
44
import { RunState } from './interpreter'
55

@@ -38,12 +38,12 @@ export const handlers: { [k: string]: OpHandler } = {
3838
},
3939
ADD: function (runState: RunState) {
4040
const [a, b] = runState.stack.popN(2)
41-
const r = a.add(b).mod(TWO_POW256)
41+
const r = a.add(b).mod(utils.TWO_POW256)
4242
runState.stack.push(r)
4343
},
4444
MUL: function (runState: RunState) {
4545
const [a, b] = runState.stack.popN(2)
46-
const r = a.mul(b).mod(TWO_POW256)
46+
const r = a.mul(b).mod(utils.TWO_POW256)
4747
runState.stack.push(r)
4848
},
4949
SUB: function (runState: RunState) {
@@ -137,7 +137,7 @@ export const handlers: { [k: string]: OpHandler } = {
137137
runState.stack.push(new BN(0))
138138
return
139139
}
140-
const m = BN.red(TWO_POW256)
140+
const m = BN.red(utils.TWO_POW256)
141141
const redBase = base.toRed(m)
142142
const r = redBase.redPow(exponent)
143143
runState.stack.push(r.fromRed())
@@ -229,7 +229,7 @@ export const handlers: { [k: string]: OpHandler } = {
229229
return
230230
}
231231

232-
const r = b.shln(a.toNumber()).iand(MAX_INTEGER)
232+
const r = b.shln(a.toNumber()).iand(utils.MAX_INTEGER)
233233
runState.stack.push(r)
234234
},
235235
SHR: function (runState: RunState) {
@@ -255,7 +255,7 @@ export const handlers: { [k: string]: OpHandler } = {
255255
const isSigned = b.testn(255)
256256
if (a.gten(256)) {
257257
if (isSigned) {
258-
r = new BN(MAX_INTEGER)
258+
r = new BN(utils.MAX_INTEGER)
259259
} else {
260260
r = new BN(0)
261261
}
@@ -266,7 +266,7 @@ export const handlers: { [k: string]: OpHandler } = {
266266
const c = b.shrn(a.toNumber())
267267
if (isSigned) {
268268
const shiftedOutWidth = 255 - a.toNumber()
269-
const mask = MAX_INTEGER.shrn(shiftedOutWidth).shln(shiftedOutWidth)
269+
const mask = utils.MAX_INTEGER.shrn(shiftedOutWidth).shln(shiftedOutWidth)
270270
r = c.ior(mask)
271271
} else {
272272
r = c
@@ -285,7 +285,7 @@ export const handlers: { [k: string]: OpHandler } = {
285285
runState.eei.useGas(
286286
new BN(runState._common.param('gasPrices', 'sha3Word')).imul(divCeil(length, new BN(32))),
287287
)
288-
const r = new BN(keccak256(data))
288+
const r = new BN(utils.keccak256(data))
289289
runState.stack.push(r)
290290
},
291291
// 0x30 range - closure state
@@ -317,7 +317,7 @@ export const handlers: { [k: string]: OpHandler } = {
317317
const i = pos.toNumber()
318318
let loaded = runState.eei.getCallData().slice(i, i + 32)
319319
loaded = loaded.length ? loaded : Buffer.from([0])
320-
const r = new BN(setLengthRight(loaded, 32))
320+
const r = new BN(utils.setLengthRight(loaded, 32))
321321

322322
runState.stack.push(r)
323323
},
@@ -394,11 +394,11 @@ export const handlers: { [k: string]: OpHandler } = {
394394

395395
const code = await runState.eei.getExternalCode(address)
396396
if (code.length === 0) {
397-
runState.stack.push(new BN(KECCAK256_NULL))
397+
runState.stack.push(new BN(utils.KECCAK256_NULL))
398398
return
399399
}
400400

401-
runState.stack.push(new BN(keccak256(code)))
401+
runState.stack.push(new BN(utils.keccak256(code)))
402402
},
403403
RETURNDATASIZE: function (runState: RunState) {
404404
runState.stack.push(runState.eei.getReturnDataSize())
@@ -567,10 +567,18 @@ export const handlers: { [k: string]: OpHandler } = {
567567
},
568568
JUMPDEST: function (runState: RunState) {},
569569
BEGINSUB: function (runState: RunState) {
570+
if (!runState._common.gteHardfork('berlin')) {
571+
trap(ERROR.INVALID_OPCODE)
572+
}
573+
570574
trap(ERROR.INVALID_BEGINSUB + ' at ' + describeLocation(runState))
571575
},
572576
JUMPSUB: function (runState: RunState) {
573577
const dest = runState.stack.pop()
578+
if (!runState._common.gteHardfork('berlin')) {
579+
trap(ERROR.INVALID_OPCODE)
580+
}
581+
574582
if (dest.gt(runState.eei.getCodeSize())) {
575583
trap(ERROR.INVALID_JUMPSUB + ' at ' + describeLocation(runState))
576584
}
@@ -585,6 +593,10 @@ export const handlers: { [k: string]: OpHandler } = {
585593
runState.programCounter = destNum + 1
586594
},
587595
RETURNSUB: function (runState: RunState) {
596+
if (!runState._common.gteHardfork('berlin')) {
597+
trap(ERROR.INVALID_OPCODE)
598+
}
599+
588600
if (runState.returnStack.length < 1) {
589601
trap(ERROR.INVALID_RETURNSUB)
590602
}
@@ -852,7 +864,7 @@ export const handlers: { [k: string]: OpHandler } = {
852864
}
853865

854866
function describeLocation(runState: RunState) {
855-
var hash = keccak256(runState.eei.getCode()).toString('hex')
867+
var hash = utils.keccak256(runState.eei.getCode()).toString('hex')
856868
var address = runState.eei.getAddress().toString('hex')
857869
var pc = runState.programCounter - 1
858870
return hash + '/' + address + ':' + pc
@@ -912,7 +924,7 @@ function getDataSlice(data: Buffer, offset: BN, length: BN): Buffer {
912924

913925
data = data.slice(offset.toNumber(), end.toNumber())
914926
// Right-pad with zeros to fill dataLength bytes
915-
data = setLengthRight(data, length.toNumber())
927+
data = utils.setLengthRight(data, length.toNumber())
916928

917929
return data
918930
}

packages/vm/tests/api/berlin/eip-2315.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
const tape = require('tape')
22
const BN = require('bn.js')
33
const VM = require('../../../dist/index').default
4+
const Common = require('@ethereumjs/common').default
5+
46

57
tape('Berlin: EIP 2315 tests', t => {
68
let callArgs;
79
let stepCounter;
810
let vm;
11+
const common = new Common('mainnet', 'berlin')
912

1013
const runTest = async function(test, st){
1114
let i = 0;
12-
vm = new VM();
15+
vm = new VM({ common: common });
1316

1417
vm.on('step', function(step){
1518
if (test.steps.length){

0 commit comments

Comments
 (0)