Solidity Scratch Pad – Understanding Its Purpose in EVM and Assembly

assemblyevmmemorysolidity

I'm reading up on upgradable proxies, came across this in openzeppelin.

    //solium-disable-next-line
    assembly {
      // Copy msg.data. We take full control of memory in this inline assembly
      // block because it will not return to Solidity code. We overwrite the
      // Solidity scratch pad at memory position 0.
      calldatacopy(0, 0, calldatasize())

      // Call the implementation.
      // out and outsize are 0 because we don't know the size yet.
      let result := delegatecall(gas(), implementation, 0, calldatasize(), 0, 0)

      // Copy the returned data.
      returndatacopy(0, 0, returndatasize())

      switch result
        // delegatecall returns 0 on error.
        case 0 {
          revert(0, returndatasize())
        }
        default {
          return(0, returndatasize())
        }
    }
  }

Particularly I'm curious about this line " Solidity scratch pad at memory position 0.".

I understand the gist of this statement that we are writing the 0th position in our memory data type using assembly. But what is the "solidity scratch pad"? Why is it called that and what exactly does that mean. Does it have any significance?

Best Answer

A solidity scratch pad is a memory location for short term memory allocation. It is called that because it is used by proxy to write an arbitrary amount of data starting at position 0 because proxy doesn't care about overwriting allocating memory or respecting solidity memory model.

See:

https://forum.openzeppelin.com/t/upgradable-contract-assembler/9723