Contract Debugging – Is It Normal to Have Invalid Opcode in a Contract?

contract-debugginginvalid-opcode

I was debugging my smart contract, while I did not encounter an invalid opcode error, I saw a lot of invalid opcodes in my deployed contract. Screenshot is from remix debugger. Is it normal to have these invalid opcodes in your contract? I'm thinking it is probably because my contract didn't consider some edge cases that may cause a failure.

So, if in a perfect world, a perfectly written contract, does it mean that contract would not have any invalid opcodes in there?

enter image description here

Best Answer

Some opcodes like PUSH have variale length so it is possible the debugger has started to decode in the middle of some opcode and is reading data instead of a valid opcode.

Also if you jump outside of your contract valid range it can show invalid opcodes.

Answer: 0xfe is an invalid opcode that is used for throwing internal exceptions. Other invalid opcodes, likely at the end of the bytecode, are related to metadata.