Skip to content

Commit

Permalink
Add memory safe assembly annotations (OpenZeppelin#3384)
Browse files Browse the repository at this point in the history
Co-authored-by: Nate <nate@Nates-MacBook-Pro.local>
  • Loading branch information
nathan-lapinski and Nate authored May 23, 2022
1 parent c019e7c commit 65b4572
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions contracts/metatx/ERC2771Context.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ abstract contract ERC2771Context is Context {
function _msgSender() internal view virtual override returns (address sender) {
if (isTrustedForwarder(msg.sender)) {
// The assembly code is more direct than the Solidity version using `abi.decode`.
/// @solidity memory-safe-assembly
assembly {
sender := shr(96, calldataload(sub(calldatasize(), 20)))
}
Expand Down
1 change: 1 addition & 0 deletions contracts/metatx/MinimalForwarder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ contract MinimalForwarder is EIP712 {
// 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.0
// https://docs.soliditylang.org/en/v0.8.0/control-structures.html#panic-via-assert-and-error-via-require
/// @solidity memory-safe-assembly
assembly {
invalid()
}
Expand Down
3 changes: 3 additions & 0 deletions contracts/proxy/Clones.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ library Clones {
* This function uses the create opcode, which should never revert.
*/
function clone(address implementation) internal returns (address instance) {
/// @solidity memory-safe-assembly
assembly {
let ptr := mload(0x40)
mstore(ptr, 0x602d8060093d393df3363d3d373d3d3d363d7300000000000000000000000000)
Expand All @@ -41,6 +42,7 @@ library Clones {
* the clones cannot be deployed twice at the same address.
*/
function cloneDeterministic(address implementation, bytes32 salt) internal returns (address instance) {
/// @solidity memory-safe-assembly
assembly {
let ptr := mload(0x40)
mstore(ptr, 0x602d8060093d393df3363d3d373d3d3d363d7300000000000000000000000000)
Expand All @@ -59,6 +61,7 @@ library Clones {
bytes32 salt,
address deployer
) internal pure returns (address predicted) {
/// @solidity memory-safe-assembly
assembly {
let ptr := mload(0x40)
mstore(ptr, 0x602d8060093d393df3363d3d373d3d3d363d7300000000000000000000000000)
Expand Down
1 change: 1 addition & 0 deletions contracts/token/ERC721/ERC721.sol
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ contract ERC721 is Context, ERC165, IERC721, IERC721Metadata {
if (reason.length == 0) {
revert("ERC721: transfer to non ERC721Receiver implementer");
} else {
/// @solidity memory-safe-assembly
assembly {
revert(add(32, reason), mload(reason))
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/utils/Address.sol
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,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 {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
Expand Down
1 change: 1 addition & 0 deletions contracts/utils/Base64.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ library Base64 {
// - `4 *` -> 4 characters for each chunk
string memory result = new string(4 * ((data.length + 2) / 3));

/// @solidity memory-safe-assembly
assembly {
// Prepare the lookup table (skip the first "length" byte)
let tablePtr := add(table, 1)
Expand Down
4 changes: 4 additions & 0 deletions contracts/utils/StorageSlot.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ library StorageSlot {
* @dev Returns an `AddressSlot` with member `value` located at `slot`.
*/
function getAddressSlot(bytes32 slot) internal pure returns (AddressSlot storage r) {
/// @solidity memory-safe-assembly
assembly {
r.slot := slot
}
Expand All @@ -59,6 +60,7 @@ library StorageSlot {
* @dev Returns an `BooleanSlot` with member `value` located at `slot`.
*/
function getBooleanSlot(bytes32 slot) internal pure returns (BooleanSlot storage r) {
/// @solidity memory-safe-assembly
assembly {
r.slot := slot
}
Expand All @@ -68,6 +70,7 @@ library StorageSlot {
* @dev Returns an `Bytes32Slot` with member `value` located at `slot`.
*/
function getBytes32Slot(bytes32 slot) internal pure returns (Bytes32Slot storage r) {
/// @solidity memory-safe-assembly
assembly {
r.slot := slot
}
Expand All @@ -77,6 +80,7 @@ library StorageSlot {
* @dev Returns an `Uint256Slot` with member `value` located at `slot`.
*/
function getUint256Slot(bytes32 slot) internal pure returns (Uint256Slot storage r) {
/// @solidity memory-safe-assembly
assembly {
r.slot := slot
}
Expand Down
2 changes: 2 additions & 0 deletions contracts/utils/structs/EnumerableSet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ library EnumerableSet {
bytes32[] memory store = _values(set._inner);
address[] memory result;

/// @solidity memory-safe-assembly
assembly {
result := store
}
Expand Down Expand Up @@ -356,6 +357,7 @@ library EnumerableSet {
bytes32[] memory store = _values(set._inner);
uint256[] memory result;

/// @solidity memory-safe-assembly
assembly {
result := store
}
Expand Down

0 comments on commit 65b4572

Please sign in to comment.