Skip to content

Commit

Permalink
♻️ ReentrancyGuard: avoid using magic numbers and initialize slot ins…
Browse files Browse the repository at this point in the history
…ide of constructor (#785)
  • Loading branch information
zerosnacks authored Jan 9, 2024
1 parent b2a5a8f commit 45ca1e6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
10 changes: 5 additions & 5 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -938,11 +938,11 @@ RedBlackTreeLibTest:testRedBlackTreeValues(uint256) (runs: 256, μ: 266948, ~: 2
RedBlackTreeLibTest:test__codesize() (gas: 14795)
ReentrancyGuardTest:testRecursiveDirectUnguardedCall() (gas: 34256)
ReentrancyGuardTest:testRecursiveIndirectUnguardedCall() (gas: 47773)
ReentrancyGuardTest:testRevertGuardLocked() (gas: 53925)
ReentrancyGuardTest:testRevertReadGuardLocked() (gas: 53626)
ReentrancyGuardTest:testRevertRecursiveDirectGuardedCall() (gas: 55173)
ReentrancyGuardTest:testRevertRecursiveIndirectGuardedCall() (gas: 56579)
ReentrancyGuardTest:testRevertRemoteCallback() (gas: 56213)
ReentrancyGuardTest:testRevertGuardLocked() (gas: 36825)
ReentrancyGuardTest:testRevertReadGuardLocked() (gas: 36526)
ReentrancyGuardTest:testRevertRecursiveDirectGuardedCall() (gas: 38073)
ReentrancyGuardTest:testRevertRecursiveIndirectGuardedCall() (gas: 39479)
ReentrancyGuardTest:testRevertRemoteCallback() (gas: 39113)
ReentrancyGuardTest:test__codesize() (gas: 5563)
SSTORE2Test:testReadInvalidPointerCustomBoundsReverts() (gas: 3242)
SSTORE2Test:testReadInvalidPointerCustomBoundsReverts(address,uint256,uint256) (runs: 256, μ: 832570, ~: 629512)
Expand Down
29 changes: 25 additions & 4 deletions src/utils/ReentrancyGuard.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ abstract contract ReentrancyGuard {
/// @dev Unauthorized reentrant call.
error Reentrancy();

/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CONSTANTS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

/// @dev The non-reentrancy flag.
uint256 private constant _NOT_ENTERED = 1;

/// @dev The reentrancy flag.
uint256 private constant _ENTERED = 2;

/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* STORAGE */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
Expand All @@ -20,6 +30,17 @@ abstract contract ReentrancyGuard {
/// but not too large to result in excessive bytecode bloat.
uint256 private constant _REENTRANCY_GUARD_SLOT = 0x929eee149b4bd21268;

/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CONSTRUCTOR */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

constructor() {
/// @solidity memory-safe-assembly
assembly {
sstore(_REENTRANCY_GUARD_SLOT, _NOT_ENTERED)
}
}

/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* REENTRANCY GUARD */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
Expand All @@ -28,24 +49,24 @@ abstract contract ReentrancyGuard {
modifier nonReentrant() virtual {
/// @solidity memory-safe-assembly
assembly {
if eq(sload(_REENTRANCY_GUARD_SLOT), 2) {
if eq(sload(_REENTRANCY_GUARD_SLOT), _ENTERED) {
mstore(0x00, 0xab143c06) // `Reentrancy()`.
revert(0x1c, 0x04)
}
sstore(_REENTRANCY_GUARD_SLOT, 2)
sstore(_REENTRANCY_GUARD_SLOT, _ENTERED)
}
_;
/// @solidity memory-safe-assembly
assembly {
sstore(_REENTRANCY_GUARD_SLOT, 1)
sstore(_REENTRANCY_GUARD_SLOT, _NOT_ENTERED)
}
}

/// @dev Guards a view function from read-only reentrancy.
modifier nonReadReentrant() virtual {
/// @solidity memory-safe-assembly
assembly {
if eq(sload(_REENTRANCY_GUARD_SLOT), 2) {
if eq(sload(_REENTRANCY_GUARD_SLOT), _ENTERED) {
mstore(0x00, 0xab143c06) // `Reentrancy()`.
revert(0x1c, 0x04)
}
Expand Down

0 comments on commit 45ca1e6

Please sign in to comment.