Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory-Safe Proxy Code Without Allocation #67

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading