Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate stack items after operations #222

Merged
merged 1 commit into from
Dec 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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