Skip to content

Commit

Permalink
Update declaration of memory safe assembly blocks (#5172)
Browse files Browse the repository at this point in the history
  • Loading branch information
Amxx committed Aug 29, 2024
1 parent 1e7ca3e commit d8bbd34
Show file tree
Hide file tree
Showing 32 changed files with 91 additions and 182 deletions.
6 changes: 2 additions & 4 deletions contracts/metatx/ERC2771Forwarder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,7 @@ contract ERC2771Forwarder is EIP712, Nonces {
bool success;
uint256 returnSize;
uint256 returnValue;
/// @solidity memory-safe-assembly
assembly {
assembly ("memory-safe") {
// Perform the staticcall and save the result in the scratch space.
// | Location | Content | Content (Hex) |
// |-----------|----------|--------------------------------------------------------------------|
Expand Down Expand Up @@ -362,8 +361,7 @@ contract ERC2771Forwarder is EIP712, Nonces {
// We explicitly trigger invalid opcode to consume all gas and bubble-up the effects, since
// neither revert or assert consume all gas since Solidity 0.8.20
// https://docs.soliditylang.org/en/v0.8.20/control-structures.html#panic-via-assert-and-error-via-require
/// @solidity memory-safe-assembly
assembly {
assembly ("memory-safe") {
invalid()
}
}
Expand Down
9 changes: 3 additions & 6 deletions contracts/proxy/Clones.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ library Clones {
if (address(this).balance < value) {
revert Errors.InsufficientBalance(address(this).balance, value);
}
/// @solidity memory-safe-assembly
assembly {
assembly ("memory-safe") {
// Cleans the upper 96 bits of the `implementation` word, then packs the first 3 bytes
// of the `implementation` address with the bytecode before the address.
mstore(0x00, or(shr(0xe8, shl(0x60, implementation)), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000))
Expand Down Expand Up @@ -77,8 +76,7 @@ library Clones {
if (address(this).balance < value) {
revert Errors.InsufficientBalance(address(this).balance, value);
}
/// @solidity memory-safe-assembly
assembly {
assembly ("memory-safe") {
// Cleans the upper 96 bits of the `implementation` word, then packs the first 3 bytes
// of the `implementation` address with the bytecode before the address.
mstore(0x00, or(shr(0xe8, shl(0x60, implementation)), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000))
Expand All @@ -99,8 +97,7 @@ library Clones {
bytes32 salt,
address deployer
) internal pure returns (address predicted) {
/// @solidity memory-safe-assembly
assembly {
assembly ("memory-safe") {
let ptr := mload(0x40)
mstore(add(ptr, 0x38), deployer)
mstore(add(ptr, 0x24), 0x5af43d82803e903d91602b57fd5bf3ff)
Expand Down
3 changes: 1 addition & 2 deletions contracts/token/ERC1155/ERC1155.sol
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,7 @@ abstract contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI, IER
uint256 element1,
uint256 element2
) private pure returns (uint256[] memory array1, uint256[] memory array2) {
/// @solidity memory-safe-assembly
assembly {
assembly ("memory-safe") {
// Load the free memory pointer
array1 := mload(0x40)
// Set array length to 1
Expand Down
6 changes: 2 additions & 4 deletions contracts/token/ERC1155/utils/ERC1155Utils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ library ERC1155Utils {
// non-IERC1155Receiver implementer
revert IERC1155Errors.ERC1155InvalidReceiver(to);
} else {
/// @solidity memory-safe-assembly
assembly {
assembly ("memory-safe") {
revert(add(32, reason), mload(reason))
}
}
Expand Down Expand Up @@ -76,8 +75,7 @@ library ERC1155Utils {
// non-IERC1155Receiver implementer
revert IERC1155Errors.ERC1155InvalidReceiver(to);
} else {
/// @solidity memory-safe-assembly
assembly {
assembly ("memory-safe") {
revert(add(32, reason), mload(reason))
}
}
Expand Down
6 changes: 2 additions & 4 deletions contracts/token/ERC20/utils/ERC1363Utils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ library ERC1363Utils {
if (reason.length == 0) {
revert ERC1363InvalidReceiver(to);
} else {
/// @solidity memory-safe-assembly
assembly {
assembly ("memory-safe") {
revert(add(32, reason), mload(reason))
}
}
Expand Down Expand Up @@ -86,8 +85,7 @@ library ERC1363Utils {
if (reason.length == 0) {
revert ERC1363InvalidSpender(spender);
} else {
/// @solidity memory-safe-assembly
assembly {
assembly ("memory-safe") {
revert(add(32, reason), mload(reason))
}
}
Expand Down
3 changes: 1 addition & 2 deletions contracts/token/ERC721/utils/ERC721Utils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ library ERC721Utils {
// non-IERC721Receiver implementer
revert IERC721Errors.ERC721InvalidReceiver(to);
} else {
/// @solidity memory-safe-assembly
assembly {
assembly ("memory-safe") {
revert(add(32, reason), mload(reason))
}
}
Expand Down
3 changes: 1 addition & 2 deletions contracts/utils/Address.sol
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ library Address {
// Look for revert reason and bubble it up if present
if (returndata.length > 0) {
// The easiest way to bubble the revert reason is using memory via assembly
/// @solidity memory-safe-assembly
assembly {
assembly ("memory-safe") {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
Expand Down
21 changes: 7 additions & 14 deletions contracts/utils/Arrays.sol
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ library Arrays {
* @dev Pointer to the memory location of the first element of `array`.
*/
function _begin(uint256[] memory array) private pure returns (uint256 ptr) {
/// @solidity memory-safe-assembly
assembly {
assembly ("memory-safe") {
ptr := add(array, 0x20)
}
}
Expand Down Expand Up @@ -377,8 +376,7 @@ library Arrays {
*/
function unsafeAccess(address[] storage arr, uint256 pos) internal pure returns (StorageSlot.AddressSlot storage) {
bytes32 slot;
/// @solidity memory-safe-assembly
assembly {
assembly ("memory-safe") {
slot := arr.slot
}
return slot.deriveArray().offset(pos).getAddressSlot();
Expand All @@ -391,8 +389,7 @@ library Arrays {
*/
function unsafeAccess(bytes32[] storage arr, uint256 pos) internal pure returns (StorageSlot.Bytes32Slot storage) {
bytes32 slot;
/// @solidity memory-safe-assembly
assembly {
assembly ("memory-safe") {
slot := arr.slot
}
return slot.deriveArray().offset(pos).getBytes32Slot();
Expand All @@ -405,8 +402,7 @@ library Arrays {
*/
function unsafeAccess(uint256[] storage arr, uint256 pos) internal pure returns (StorageSlot.Uint256Slot storage) {
bytes32 slot;
/// @solidity memory-safe-assembly
assembly {
assembly ("memory-safe") {
slot := arr.slot
}
return slot.deriveArray().offset(pos).getUint256Slot();
Expand Down Expand Up @@ -451,8 +447,7 @@ library Arrays {
* WARNING: this does not clear elements if length is reduced, of initialize elements if length is increased.
*/
function unsafeSetLength(address[] storage array, uint256 len) internal {
/// @solidity memory-safe-assembly
assembly {
assembly ("memory-safe") {
sstore(array.slot, len)
}
}
Expand All @@ -463,8 +458,7 @@ library Arrays {
* WARNING: this does not clear elements if length is reduced, of initialize elements if length is increased.
*/
function unsafeSetLength(bytes32[] storage array, uint256 len) internal {
/// @solidity memory-safe-assembly
assembly {
assembly ("memory-safe") {
sstore(array.slot, len)
}
}
Expand All @@ -475,8 +469,7 @@ library Arrays {
* WARNING: this does not clear elements if length is reduced, of initialize elements if length is increased.
*/
function unsafeSetLength(uint256[] storage array, uint256 len) internal {
/// @solidity memory-safe-assembly
assembly {
assembly ("memory-safe") {
sstore(array.slot, len)
}
}
Expand Down
3 changes: 1 addition & 2 deletions contracts/utils/Base64.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ library Base64 {

string memory result = new string(resultLength);

/// @solidity memory-safe-assembly
assembly {
assembly ("memory-safe") {
// Prepare the lookup table (skip the first "length" byte)
let tablePtr := add(table, 1)

Expand Down
6 changes: 2 additions & 4 deletions contracts/utils/Create2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ library Create2 {
if (bytecode.length == 0) {
revert Create2EmptyBytecode();
}
/// @solidity memory-safe-assembly
assembly {
assembly ("memory-safe") {
addr := create2(amount, add(bytecode, 0x20), mload(bytecode), salt)
// if no address was created, and returndata is not empty, bubble revert
if and(iszero(addr), not(iszero(returndatasize()))) {
Expand All @@ -69,8 +68,7 @@ library Create2 {
* `deployer`. If `deployer` is this contract's address, returns the same value as {computeAddress}.
*/
function computeAddress(bytes32 salt, bytes32 bytecodeHash, address deployer) internal pure returns (address addr) {
/// @solidity memory-safe-assembly
assembly {
assembly ("memory-safe") {
let ptr := mload(0x40) // Get free memory pointer

// | | ↓ ptr ... ↓ ptr + 0x0B (start) ... ↓ ptr + 0x20 ... ↓ ptr + 0x40 ... |
Expand Down
3 changes: 1 addition & 2 deletions contracts/utils/Panic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ library Panic {
/// @dev Reverts with a panic code. Recommended to use with
/// the internal constants with predefined codes.
function panic(uint256 code) internal pure {
/// @solidity memory-safe-assembly
assembly {
assembly ("memory-safe") {
mstore(0x00, 0x4e487b71)
mstore(0x20, code)
revert(0x1c, 0x24)
Expand Down
3 changes: 1 addition & 2 deletions contracts/utils/ShortStrings.sol
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ library ShortStrings {
uint256 len = byteLength(sstr);
// using `new string(len)` would work locally but is not memory safe.
string memory str = new string(32);
/// @solidity memory-safe-assembly
assembly {
assembly ("memory-safe") {
mstore(str, len)
mstore(add(str, 0x20), sstr)
}
Expand Down
27 changes: 9 additions & 18 deletions contracts/utils/SlotDerivation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ library SlotDerivation {
* @dev Derive an ERC-7201 slot from a string (namespace).
*/
function erc7201Slot(string memory namespace) internal pure returns (bytes32 slot) {
/// @solidity memory-safe-assembly
assembly {
assembly ("memory-safe") {
mstore(0x00, sub(keccak256(add(namespace, 0x20), mload(namespace)), 1))
slot := and(keccak256(0x00, 0x20), not(0xff))
}
Expand All @@ -60,8 +59,7 @@ library SlotDerivation {
* @dev Derive the location of the first element in an array from the slot where the length is stored.
*/
function deriveArray(bytes32 slot) internal pure returns (bytes32 result) {
/// @solidity memory-safe-assembly
assembly {
assembly ("memory-safe") {
mstore(0x00, slot)
result := keccak256(0x00, 0x20)
}
Expand All @@ -71,8 +69,7 @@ library SlotDerivation {
* @dev Derive the location of a mapping element from the key.
*/
function deriveMapping(bytes32 slot, address key) internal pure returns (bytes32 result) {
/// @solidity memory-safe-assembly
assembly {
assembly ("memory-safe") {
mstore(0x00, key)
mstore(0x20, slot)
result := keccak256(0x00, 0x40)
Expand All @@ -83,8 +80,7 @@ library SlotDerivation {
* @dev Derive the location of a mapping element from the key.
*/
function deriveMapping(bytes32 slot, bool key) internal pure returns (bytes32 result) {
/// @solidity memory-safe-assembly
assembly {
assembly ("memory-safe") {
mstore(0x00, key)
mstore(0x20, slot)
result := keccak256(0x00, 0x40)
Expand All @@ -95,8 +91,7 @@ library SlotDerivation {
* @dev Derive the location of a mapping element from the key.
*/
function deriveMapping(bytes32 slot, bytes32 key) internal pure returns (bytes32 result) {
/// @solidity memory-safe-assembly
assembly {
assembly ("memory-safe") {
mstore(0x00, key)
mstore(0x20, slot)
result := keccak256(0x00, 0x40)
Expand All @@ -107,8 +102,7 @@ library SlotDerivation {
* @dev Derive the location of a mapping element from the key.
*/
function deriveMapping(bytes32 slot, uint256 key) internal pure returns (bytes32 result) {
/// @solidity memory-safe-assembly
assembly {
assembly ("memory-safe") {
mstore(0x00, key)
mstore(0x20, slot)
result := keccak256(0x00, 0x40)
Expand All @@ -119,8 +113,7 @@ library SlotDerivation {
* @dev Derive the location of a mapping element from the key.
*/
function deriveMapping(bytes32 slot, int256 key) internal pure returns (bytes32 result) {
/// @solidity memory-safe-assembly
assembly {
assembly ("memory-safe") {
mstore(0x00, key)
mstore(0x20, slot)
result := keccak256(0x00, 0x40)
Expand All @@ -131,8 +124,7 @@ library SlotDerivation {
* @dev Derive the location of a mapping element from the key.
*/
function deriveMapping(bytes32 slot, string memory key) internal pure returns (bytes32 result) {
/// @solidity memory-safe-assembly
assembly {
assembly ("memory-safe") {
let length := mload(key)
let begin := add(key, 0x20)
let end := add(begin, length)
Expand All @@ -147,8 +139,7 @@ library SlotDerivation {
* @dev Derive the location of a mapping element from the key.
*/
function deriveMapping(bytes32 slot, bytes memory key) internal pure returns (bytes32 result) {
/// @solidity memory-safe-assembly
assembly {
assembly ("memory-safe") {
let length := mload(key)
let begin := add(key, 0x20)
let end := add(begin, length)
Expand Down
Loading

0 comments on commit d8bbd34

Please sign in to comment.