OpenZeppelin / openzeppelin-labs

A space for the community to interact and exchange ideas on the OpenZeppelin platform. Do not use in production!
https://openzeppelin.com/
MIT License
376 stars 116 forks source link

Why we need calldatacopy(ptr, 0, calldatasize) #108

Closed thcrnk closed 6 years ago

thcrnk commented 6 years ago

From few months we are using the same pattern for upgradability with proxy, only difference is we do not have this line: calldatacopy(ptr, 0, calldatasize) Here is the code we are using and currently is working fine, how calldatacopy is improving it:

      assembly {
            switch extcodesize(_dst) case 0 { revert(0, 0) }

            let result := delegatecall(sub(gas, 10000), _dst, add(_calldata, 0x20), mload(_calldata), 0, 0)
            let size := returndatasize

            let ptr := mload(0x40)
            returndatacopy(ptr, 0, size)

            switch result 
            case 0 { revert(ptr, size) }
            default { return(ptr, size) }
        }

Just asking to be sure and if we need to implement it.

frangio commented 6 years ago

What is the _calldata variable your assembly code is using? I suspect it refers to an array that you created via bytes _calldata = msg.data. This compiles down to a calldatacopy. We're using the opcode directly as an optimization. See https://github.com/zeppelinos/zos-lib/pull/13 where we introduced this change.

thcrnk commented 6 years ago

Thanks, now is all clear!