Member-only story
Solidity — All About Errors
Today’s article starts a new sub-series within the All About Solidity article series, focused on errors in Solidity and EVM-based execution environments.
In Smart Contract land, errors can be lethal. If not handled properly, they can cause bugs and vulnerabilities. If badly handled, they can cause smart contracts to be stuck and unusable.
Before jumping straight into Solidity, we will have a gentle premiere on how the EVM handle things when bad or wrong things happen and what are the different type of errors the EVM has built-in.
“If debugging is the process of removing software bugs, then programming must be the process of putting them in.”
― Edsger W. Dijkstra
Table of Content
- The Program Counter
- EVM Errors & Halting Exceptions
- Types of Errors (Compile vs Runtime Errors)
- Compile Time Errors
- Contract Runtime Errors
- EVM Halting Exceptions
- Out of Gas
- Stack Under/OverFlow
- Invalid JUMP Destination
- Bad Instruction
The Program Counter
When the EVM runs the bytecode of a smart contract, it runs each opcode one after the other using a Program Counter (PC).
The Program Counter (PC) encodes which instruction (stored in the contract code) should be read next by the EVM (and therefore run). it is incremented by one byte after each next instruction (with the exceptions of the PUSHN
instructions and JUMP
/ JUMPI
where the PC is modified to the JUMPDEST
destination in the bytecode)
Think of the EVM as a driver on the road. As the driver encounters some traffic signs with directions, signals, and restrictions, it knows what it should do next or not.
The EVM will keep running opcodes in the current execution environment of a contract as long as the PC encounters valid opcodes.
But some opcodes can instruct to stop execution. When the execution stops, we say the EVM halts (see Yellow Paper).
But the execution can be halted successfully or with an error. If the execution halts successfully, the state of the blockchain is updated. Otherwise, any state change is reverted…