2

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

2

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.

  • It's probably not because of debugging in the middle of some bytecode, exact same opcodes are produced by the compiler as well, I tried a few other contracts from the etherscan websites, they all seem to have some invalid opcodes in there. I'm just not sure about the reason of having invalid code there. Are they there on purpose to help throw exception? Or are they bugs in the solidity compiler? – Huadong Feng Apr 30 at 21:23
  • Do you have an example address? I've seen some faulty deployments produce invalid contracts because they send the wrong bytecode. Also I saw experiments that used contracts a some kind of read only storage. In my experience I saw similar problem with a corrupted opcode view because an invalid jump in assembly code. – Ismael May 1 at 5:02
  • I mean if you have an example address of deployed contract that has the opcode sequence you showed? ie the sequence INVALID/GASPRICE/INVALID/GASPRICE/INVALID/PUSH17 doesn't look like produced by a compiler and doesn't make sense by itself. – Ismael May 2 at 16:03
  • github.com/JackHFeng/SampleCode/blob/master/Bank.sol Copy and paste the code into remix, and compile using 0.5.7+commit.6da8b019, after deployment, call – Huadong Feng May 2 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.