Skip to content

Commit

Permalink
Memory-Safe Proxy Code Without Allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
nlordell committed Dec 1, 2023
1 parent ce2b76f commit cdbd55d
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions accounts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,33 +61,26 @@ An example implementation of such a `fallback` function is:
bytes32 slot = SAFE_CORE_PROTOCOL_MANAGER_SLOT;
/// @solidity memory-safe-assembly
assembly {
function allocate(length) -> pos {
pos := mload(0x40)
mstore(0x40, add(pos, length))
}
let protocol := sload(slot)
if iszero(protocol) {
return(0, 0)
}
let calldataPtr := allocate(calldatasize())
calldatacopy(calldataPtr, 0, calldatasize())
let ptr := mload(0x40)
calldatacopy(ptr, 0, calldatasize())
// The msg.sender address is shifted to the left by 12 bytes to remove the padding
// Then the address without padding is stored right after the calldata
let senderPtr := allocate(20)
mstore(senderPtr, shl(96, caller()))
mstore(add(ptr, calldatasize()), shl(96, caller()))
// Add 20 bytes for the address appended add the end
let success := call(gas(), protocol, 0, calldataPtr, add(calldatasize(), 20), 0, 0)
let success := call(gas(), protocol, 0, ptr, add(calldatasize(), 20), 0, 0)
let returnDataPtr := allocate(returndatasize())
returndatacopy(returnDataPtr, 0, returndatasize())
returndatacopy(ptr, 0, returndatasize())
if iszero(success) {
revert(returnDataPtr, returndatasize())
revert(ptr, returndatasize())
}
return(returnDataPtr, returndatasize())
return(ptr, returndatasize())
}
}
```
Expand Down

0 comments on commit cdbd55d

Please sign in to comment.