Skip to content

Commit

Permalink
Validate stack items after operations
Browse files Browse the repository at this point in the history
  • Loading branch information
axic committed Oct 13, 2017
1 parent 7c357fd commit 462d5ad
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ exports.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'
}
31 changes: 28 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()
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,22 @@ 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
runState.vmError = ERROR.INTERNAL_ERROR
cb()
return
}
// 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
runState.vmError = ERROR.INTERNAL_ERROR
cb()
return
}

cb()
Expand All @@ -199,11 +213,22 @@ module.exports = function (opts, cb) {
}

// save result to the stack
if (result) {
if (result !== undefined) {
if (retNum != 1) {
// opcode post-stack mismatch
runState.vmError = ERROR.INTERNAL_ERROR
cb()
return
}
// 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
runState.vmError = ERROR.INTERNAL_ERROR
cb()
return
}

// call the callback if opFn was sync
Expand Down

0 comments on commit 462d5ad

Please sign in to comment.