Skip to content
This repository has been archived by the owner on May 13, 2022. It is now read-only.

Commit

Permalink
Merge pull request #757 from smblucker/invalid-opcode-issue601
Browse files Browse the repository at this point in the history
Implementing INVALID opcode
  • Loading branch information
Silas Davis authored May 8, 2018
2 parents 7c08d93 + a647541 commit 8cf2080
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
2 changes: 2 additions & 0 deletions execution/evm/asm/opcodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ const (
// 0x70 range - other
STATICCALL = 0xfa
REVERT = 0xfd
INVALID = 0xfe
SELFDESTRUCT = 0xff
)

Expand Down Expand Up @@ -347,6 +348,7 @@ var opCodeNames = map[OpCode]string{
STATICCALL: "STATICCALL",
// 0x70 range - other
REVERT: "REVERT",
INVALID: "INVALID",
SELFDESTRUCT: "SELFDESTRUCT",
}

Expand Down
8 changes: 6 additions & 2 deletions execution/evm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var (
ErrDataStackUnderflow = errors.New("Data stack underflow")
ErrInvalidContract = errors.New("Invalid contract")
ErrNativeContractCodeCopy = errors.New("Tried to copy native contract code")
ErrExecutionAborted = errors.New("Execution aborted")
ErrExecutionReverted = errors.New("Execution reverted")
)

Expand Down Expand Up @@ -1039,6 +1040,9 @@ func (vm *VM) call(caller acm.Account, callee acm.MutableAccount, code, input []
vm.Debugf(" => [%v, %v] (%d) 0x%X\n", offset, size, len(output), output)
return output, ErrExecutionReverted

case INVALID: //0xFE
return nil, ErrExecutionAborted

case SELFDESTRUCT: // 0xFF
addr := stack.Pop()
if useGasNegative(gas, GasGetAccount, &err) {
Expand Down Expand Up @@ -1079,8 +1083,8 @@ func (vm *VM) call(caller acm.Account, callee acm.MutableAccount, code, input []
case STATICCALL, SHL, SHR, SAR, RETURNDATASIZE, RETURNDATACOPY:
return nil, fmt.Errorf("%s not yet implemented", op.Name())
default:
vm.Debugf("(pc) %-3v Invalid opcode %X\n", pc, op)
return nil, fmt.Errorf("invalid opcode %X", op)
vm.Debugf("(pc) %-3v Unknown opcode %X\n", pc, op)
return nil, fmt.Errorf("unknown opcode %X", op)
}
pc++
}
Expand Down
20 changes: 20 additions & 0 deletions execution/evm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,26 @@ func TestMsgSender(t *testing.T) {

}

func TestInvalid(t *testing.T) {
ourVm := NewVM(newAppState(), newParams(), acm.ZeroAddress, nil, logger)

// Create accounts
account1 := newAccount(1)
account2 := newAccount(1, 0, 1)

var gas uint64 = 100000

bytecode := MustSplice(PUSH32, 0x72, 0x65, 0x76, 0x65, 0x72, 0x74, 0x20, 0x6D, 0x65, 0x73, 0x73, 0x61,
0x67, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, PUSH1, 0x00, MSTORE, PUSH1, 0x0E, PUSH1, 0x00, INVALID)

output, err := ourVm.Call(account1, account2, bytecode, []byte{}, 0, &gas)
expected := "call error: " + ErrExecutionAborted.Error()
assert.EqualError(t, err, expected)
t.Logf("Output: %v Error: %v\n", output, err)

}

// These code segment helpers exercise the MSTORE MLOAD MSTORE cycle to test
// both of the memory operations. Each MSTORE is done on the memory boundary
// (at MSIZE) which Solidity uses to find guaranteed unallocated memory.
Expand Down

0 comments on commit 8cf2080

Please sign in to comment.