Solidity Compilation – Why Compiled Code Checks Arguments

compilationevmsolidity

I have compiled the following Solidity code:

pragma solidity ^0.4.9;
import "./Callee.sol";

contract Caller{
  function call(address callee_address){
    Callee callee = Callee(callee_address);
    callee.callee_function();
  }
}

This becomes the following EVM:
http://pastebin.com/BeUPghsT

I do not understand the following code snippet. Only the code after address 0x1c is copied into the bytearray of the contract upon execution. So all addresses should be 0x1c lower than what is stated here.

Address 0x1c
  PUSH1 0x60
  PUSH1 0x40
  MSTORE
  PUSH1 0x00
  CALLDATALOAD
Address 0x24
  PUSH29 0x0100000000000000000000000000000000000000000000000000000000
Address 0x42
  SWAP1
  DIV
  PUSH4 0xf55332ab
  EQ
  PUSH1 0x3a
  JUMPI
  JUMPDEST
  UNKNOWN OPCODE "0xFE"

Does the CALLDATALOAD put the address argument (callee_address) onto the stack? If so, it seems that the unknown opcode FE (a way of stopping all execution?) is called if the address does not have 0xf55332ab as its first four bytes. Is that correctly understood?

Why should the address start with 0xf55332ab? There are no checksums in Ethereum addresses, right?

Am I correct to understand that this is where a call to function lands or is this dead code?

Best Answer

Code execution in EVM always starts with PC=0. So the method you have called is jumped to in a switch-like code segment in the beginning of the bytecode of the EVM.

CALLDATALOAD is not the argument given to the function "call". CALLDATALOAD is the function signature which is calculated as web3.sha3('call(address)').substr(0,10) where "call" is the name of the function in this example, and "address" is the type of the argument (or the type of the parameter, if you will) that the function takes. And lo and behold! web3.sha3('initNumbers()').substr(0,10)=0xf55332ab.

Therefore 0xFE (the halt) is called if you call anything but the method "call" with the correct set of arguments on this deployed contract. In other words: The start of the contract EVM acts as a switch statement that sends you to the correct method inside the contract. This sort of structure is called a jump table.

Related Topic