Skip to content

Commit

Permalink
Restrict opcodes to berlin hardfork
Browse files Browse the repository at this point in the history
  • Loading branch information
cgewecke committed Jun 16, 2020
1 parent cedabe2 commit 39e1d45
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
38 changes: 25 additions & 13 deletions packages/vm/lib/evm/opFns.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import BN = require('bn.js')
import { keccak256, setLengthRight, TWO_POW256, MAX_INTEGER, KECCAK256_NULL } from 'ethereumjs-util'
import * as utils from 'ethereumjs-util'
import { ERROR, VmError } from '../exceptions'
import { RunState } from './interpreter'

Expand Down Expand Up @@ -38,12 +38,12 @@ export const handlers: { [k: string]: OpHandler } = {
},
ADD: function (runState: RunState) {
const [a, b] = runState.stack.popN(2)
const r = a.add(b).mod(TWO_POW256)
const r = a.add(b).mod(utils.TWO_POW256)
runState.stack.push(r)
},
MUL: function (runState: RunState) {
const [a, b] = runState.stack.popN(2)
const r = a.mul(b).mod(TWO_POW256)
const r = a.mul(b).mod(utils.TWO_POW256)
runState.stack.push(r)
},
SUB: function (runState: RunState) {
Expand Down Expand Up @@ -137,7 +137,7 @@ export const handlers: { [k: string]: OpHandler } = {
runState.stack.push(new BN(0))
return
}
const m = BN.red(TWO_POW256)
const m = BN.red(utils.TWO_POW256)
const redBase = base.toRed(m)
const r = redBase.redPow(exponent)
runState.stack.push(r.fromRed())
Expand Down Expand Up @@ -229,7 +229,7 @@ export const handlers: { [k: string]: OpHandler } = {
return
}

const r = b.shln(a.toNumber()).iand(MAX_INTEGER)
const r = b.shln(a.toNumber()).iand(utils.MAX_INTEGER)
runState.stack.push(r)
},
SHR: function (runState: RunState) {
Expand All @@ -255,7 +255,7 @@ export const handlers: { [k: string]: OpHandler } = {
const isSigned = b.testn(255)
if (a.gten(256)) {
if (isSigned) {
r = new BN(MAX_INTEGER)
r = new BN(utils.MAX_INTEGER)
} else {
r = new BN(0)
}
Expand All @@ -266,7 +266,7 @@ export const handlers: { [k: string]: OpHandler } = {
const c = b.shrn(a.toNumber())
if (isSigned) {
const shiftedOutWidth = 255 - a.toNumber()
const mask = MAX_INTEGER.shrn(shiftedOutWidth).shln(shiftedOutWidth)
const mask = utils.MAX_INTEGER.shrn(shiftedOutWidth).shln(shiftedOutWidth)
r = c.ior(mask)
} else {
r = c
Expand All @@ -285,7 +285,7 @@ export const handlers: { [k: string]: OpHandler } = {
runState.eei.useGas(
new BN(runState._common.param('gasPrices', 'sha3Word')).imul(divCeil(length, new BN(32))),
)
const r = new BN(keccak256(data))
const r = new BN(utils.keccak256(data))
runState.stack.push(r)
},
// 0x30 range - closure state
Expand Down Expand Up @@ -317,7 +317,7 @@ export const handlers: { [k: string]: OpHandler } = {
const i = pos.toNumber()
let loaded = runState.eei.getCallData().slice(i, i + 32)
loaded = loaded.length ? loaded : Buffer.from([0])
const r = new BN(setLengthRight(loaded, 32))
const r = new BN(utils.setLengthRight(loaded, 32))

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

const code = await runState.eei.getExternalCode(address)
if (code.length === 0) {
runState.stack.push(new BN(KECCAK256_NULL))
runState.stack.push(new BN(utils.KECCAK256_NULL))
return
}

runState.stack.push(new BN(keccak256(code)))
runState.stack.push(new BN(utils.keccak256(code)))
},
RETURNDATASIZE: function (runState: RunState) {
runState.stack.push(runState.eei.getReturnDataSize())
Expand Down Expand Up @@ -567,10 +567,18 @@ export const handlers: { [k: string]: OpHandler } = {
},
JUMPDEST: function (runState: RunState) {},
BEGINSUB: function (runState: RunState) {
if (!runState._common.gteHardfork('berlin')) {
trap(ERROR.INVALID_OPCODE)
}

trap(ERROR.INVALID_BEGINSUB + ' at ' + describeLocation(runState))
},
JUMPSUB: function (runState: RunState) {
const dest = runState.stack.pop()
if (!runState._common.gteHardfork('berlin')) {
trap(ERROR.INVALID_OPCODE)
}

if (dest.gt(runState.eei.getCodeSize())) {
trap(ERROR.INVALID_JUMPSUB + ' at ' + describeLocation(runState))
}
Expand All @@ -585,6 +593,10 @@ export const handlers: { [k: string]: OpHandler } = {
runState.programCounter = destNum + 1
},
RETURNSUB: function (runState: RunState) {
if (!runState._common.gteHardfork('berlin')) {
trap(ERROR.INVALID_OPCODE)
}

if (runState.returnStack.length < 1) {
trap(ERROR.INVALID_RETURNSUB)
}
Expand Down Expand Up @@ -852,7 +864,7 @@ export const handlers: { [k: string]: OpHandler } = {
}

function describeLocation(runState: RunState) {
var hash = keccak256(runState.eei.getCode()).toString('hex')
var hash = utils.keccak256(runState.eei.getCode()).toString('hex')
var address = runState.eei.getAddress().toString('hex')
var pc = runState.programCounter - 1
return hash + '/' + address + ':' + pc
Expand Down Expand Up @@ -912,7 +924,7 @@ function getDataSlice(data: Buffer, offset: BN, length: BN): Buffer {

data = data.slice(offset.toNumber(), end.toNumber())
// Right-pad with zeros to fill dataLength bytes
data = setLengthRight(data, length.toNumber())
data = utils.setLengthRight(data, length.toNumber())

return data
}
Expand Down
5 changes: 4 additions & 1 deletion packages/vm/tests/api/berlin/eip-2315.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
const tape = require('tape')
const BN = require('bn.js')
const VM = require('../../../dist/index').default
const Common = require('@ethereumjs/common').default


tape('Berlin: EIP 2315 tests', t => {
let callArgs;
let stepCounter;
let vm;
const common = new Common('mainnet', 'berlin')

const runTest = async function(test, st){
let i = 0;
vm = new VM();
vm = new VM({ common: common });

vm.on('step', function(step){
if (test.steps.length){
Expand Down

0 comments on commit 39e1d45

Please sign in to comment.