Skip to content

Commit

Permalink
Merge pull request #222 from ethereumjs/stack-length-checks
Browse files Browse the repository at this point in the history
Validate stack items after operations
  • Loading branch information
jwasinger authored Dec 18, 2017
2 parents 9e63cd3 + 6033239 commit 9fd6a1d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/exceptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const ERROR = {
INVALID_JUMP: 'invalid JUMP',
INVALID_OPCODE: 'invalid opcode',
REVERT: 'revert',
STATIC_STATE_CHANGE: 'static state change'
STATIC_STATE_CHANGE: 'static state change',
INTERNAL_ERROR: 'internal error'
}

function VmError (error) {
Expand Down
27 changes: 24 additions & 3 deletions lib/runCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,14 @@ module.exports = function (opts, cb) {
cb(new VmError(ERROR.OUT_OF_GAS))
return
}

// advance program counter
runState.programCounter++
var argsNum = opInfo.in
var retNum = opInfo.out
// pop the stack
var args = argsNum ? runState.stack.splice(-opInfo.in) : []
var args = argsNum ? runState.stack.splice(-argsNum) : []

args.reverse()
args.push(runState)
// create a callback for async opFunc
Expand All @@ -178,11 +181,20 @@ module.exports = function (opts, cb) {
if (err) return cb(err)

// save result to the stack
if (result) {
if (result !== undefined) {
if (retNum !== 1) {
// opcode post-stack mismatch
return cb(new VmError(ERROR.INTERNAL_ERROR))
}
// NOTE: Ensure that every stack item is padded to 256 bits.
// This should be done at every opcode in the future.
result = utils.setLengthLeft(result, 32)
runState.stack.push(result)
} else {
if (retNum !== 0) {
// opcode post-stack mismatch
return cb(new VmError(ERROR.INTERNAL_ERROR))
}
}

cb()
Expand All @@ -198,11 +210,20 @@ module.exports = function (opts, cb) {
}

// save result to the stack
if (result) {
if (result !== undefined) {
if (retNum !== 1) {
// opcode post-stack mismatch
return cb(VmError(ERROR.INTERNAL_ERROR))
}
// NOTE: Ensure that every stack item is padded to 256 bits.
// This should be done at every opcode in the future.
result = utils.setLengthLeft(result, 32)
runState.stack.push(result)
} else {
if (!opInfo.async && retNum !== 0) {
// opcode post-stack mismatch
return cb(VmError(ERROR.INTERNAL_ERROR))
}
}

// call the callback if opFn was sync
Expand Down

0 comments on commit 9fd6a1d

Please sign in to comment.