Solidity Fallback Function – How Does a Solidity Fallback Function Work with the Raw CALL Opcode of the EVM

evmfallback-functionsolidity

Since a fallback function is a Solidity feature, how does it work if the EVM opcode CALL is used?

If msg.data is empty, how can Solidity run the fallback function? Shouldn't the EVM just transfer the Ether, since there's no function invocation involved?

In Solidity, does syntax like contractAddress.call.value(amountWei)() invoke a raw CALL opcode, or does Solidity still do something additional?

Best Answer

Any message sent to a contract, even if it is empty or just an ether transfer will invoke the code of the contract. The fallback function is run if the data sent does not start with a known function identifier (e.g. if a non-existent function is called or if the message data is empty as is the case for a simple ether transfer).

So if a contract has a fallback function, there is no way to prevent it being called (except by calling an actually existing function or not sending enough gas but no ether).

In Solidity, both .call and .send compile to the CALL opcode, just their expected arguments are different.