Skip to content
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion docs/utils/safetransferlib.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Suggested gas stipend for contract receiving ETH that disallows any storage writ
### GAS_STIPEND_NO_GRIEF

```solidity
uint256 internal constant GAS_STIPEND_NO_GRIEF = 100000
uint256 internal constant GAS_STIPEND_NO_GRIEF = 500000
```

Suggested gas stipend for contract receiving ETH to perform a few
Expand Down
6 changes: 3 additions & 3 deletions src/utils/SafeTransferLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ library SafeTransferLib {

/// @dev Suggested gas stipend for contract receiving ETH to perform a few
/// storage reads and writes, but low enough to prevent griefing.
uint256 internal constant GAS_STIPEND_NO_GRIEF = 100000;
uint256 internal constant GAS_STIPEND_NO_GRIEF = 500000;

/// @dev The unique EIP-712 domain separator for the DAI token contract.
bytes32 internal constant DAI_DOMAIN_SEPARATOR =
Expand Down Expand Up @@ -550,8 +550,8 @@ library SafeTransferLib {
and( // The arguments of `and` are evaluated from right to left.
lt(iszero(mload(0x00)), eq(returndatasize(), 0x20)), // Returns 1 non-zero word.
// Gas stipend to limit gas burn for tokens that don't refund gas when
// an non-existing function is called. 5K should be enough for a SLOAD.
staticcall(5000, token, 0x1c, 0x04, 0x00, 0x20)
// an non-existing function is called. 50K covers cold proxy SLOADs.
staticcall(50000, token, 0x1c, 0x04, 0x00, 0x20)
)
) { break }
// After here, we can be sure that token is a contract.
Expand Down
54 changes: 54 additions & 0 deletions test/SafeTransferLib.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,33 @@ interface IPermit2 {
returns (uint160 amount, uint48 expiration, uint48 nonce);
}

/// @dev Burns 200K gas upon receiving ETH, without touching storage.
/// More than the old 100K `GAS_STIPEND_NO_GRIEF`, same cost on every fork.
contract GasBurningETHRecipient {
event Received();

receive() external payable {
uint256 g = gasleft();
while (g - gasleft() < 200000) {}
emit Received();
}
}

/// @dev Burns 20K gas in `DOMAIN_SEPARATOR()`, like a token behind a beacon proxy.
/// More than the old 5K probe stipend, same cost on every fork.
contract MockERC20WithHeavyDomainSeparator is MockERC20 {
constructor() MockERC20("Heavy", "HEAVY", 18) {}

function DOMAIN_SEPARATOR() public view virtual override returns (bytes32 result) {
result = super.DOMAIN_SEPARATOR();
uint256 g = gasleft();
while (g - gasleft() < 20000) {}
}
}

contract SafeTransferLibTest is SoladyTest {
event Received();

uint256 internal constant _SUCCESS = 1;
uint256 internal constant _REVERTS_WITH_SELECTOR = 2;
uint256 internal constant _REVERTS_WITH_ANY = 3;
Expand Down Expand Up @@ -327,6 +353,17 @@ contract SafeTransferLibTest is SoladyTest {
testForceTransferETHToGriever(1 ether, 2);
}

function testForceTransferETHToGasHeavyRecipient() public {
address recipient = address(new GasBurningETHRecipient());
vm.deal(address(this), 1 ether);
// The stipend is enough, so `receive()` runs to the end and emits its event.
// If it runs out of gas, the `SELFDESTRUCT` fallback sends the ETH with no event.
vm.expectEmit(recipient);
emit Received();
this.forceSafeTransferETH(recipient, 1 ether);
assertEq(recipient.balance, 1 ether);
}

function testTransferWithReturnsFalseReverts() public {
verifySafeTransfer(address(returnsFalse), address(0xBEEF), 1e18, _REVERTS_WITH_SELECTOR);
}
Expand Down Expand Up @@ -898,6 +935,23 @@ contract SafeTransferLibTest is SoladyTest {
this.permit2(t);
}

function testPermit2WithHeavyDomainSeparator() public {
MockERC20 heavy = new MockERC20WithHeavyDomainSeparator();
_TestTemps memory t;
t.token = address(heavy);
t.deadline = block.timestamp;
(t.signer, t.privateKey) = _randomSigner();
t.spender = _randomNonZeroAddress();
t.amount = _bound(_random(), 0, type(uint160).max);
t.hash =
keccak256(abi.encode(_PERMIT_TYPEHASH, t.signer, t.spender, t.amount, 0, t.deadline));
t.hash = keccak256(abi.encodePacked("\x19\x01", heavy.DOMAIN_SEPARATOR(), t.hash));
(t.v, t.r, t.s) = vm.sign(t.privateKey, t.hash);
this.permit2(t);
// The probe succeeds, so the native permit is used.
assertEq(heavy.allowance(t.signer, t.spender), t.amount);
}

function testPermit2OnDAI() public {
_TestTemps memory t;
t.token = _DAI;
Expand Down
80 changes: 80 additions & 0 deletions test/SoladyTest.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

import "./utils/SoladyTest.sol";

/// @dev Differential tests of the bound helpers against forge-std v1.17.0 `StdUtils`.
contract SoladyTestTest is SoladyTest {
uint256 private constant _UINT256_MAX = type(uint256).max;
uint256 private constant _INT256_MIN_ABS = 1 << 255;

function testBoundMatchesForgeStd(uint256 x, uint256 a, uint256 b) public {
if (_randomChance(2)) x = _random();
if (_randomChance(2)) a = _random();
if (_randomChance(2)) b = _random();
(uint256 min, uint256 max) = a <= b ? (a, b) : (b, a);
assertEq(bound(x, min, max), _forgeStdBound(x, min, max));
}

function testBoundSignedMatchesForgeStd(int256 x, int256 a, int256 b) public {
if (_randomChance(2)) x = int256(_random());
if (_randomChance(2)) a = int256(_random());
if (_randomChance(2)) b = int256(_random());
(int256 min, int256 max) = a <= b ? (a, b) : (b, a);
assertEq(bound(x, min, max), _forgeStdBound(x, min, max));
}

function testBoundSignedEndpoints() public {
assertEq(bound(int256(0), -5, 5), 0);
assertEq(bound(type(int256).min, -5, 5), -5);
assertEq(bound(type(int256).max, -5, 5), 5);
assertEq(bound(type(int256).min, type(int256).min, type(int256).max), type(int256).min);
}

function testBoundPrivateKey(uint256 k) public {
uint256 r = boundPrivateKey(k);
assertGe(r, 1);
assertLe(r, 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140);
}

// Verbatim from forge-std v1.17.0 `StdUtils._bound(uint256,uint256,uint256)`.
function _forgeStdBound(uint256 x, uint256 min, uint256 max)
internal
pure
returns (uint256 result)
{
require(min <= max);
if (x >= min && x <= max) return x;
uint256 size = max - min + 1;
if (x <= 3 && size > x) return min + x;
if (x >= _UINT256_MAX - 3 && size > _UINT256_MAX - x) return max - (_UINT256_MAX - x);
if (x > max) {
uint256 diff = x - max;
uint256 rem = diff % size;
if (rem == 0) return max;
result = min + rem - 1;
} else if (x < min) {
uint256 diff = min - x;
uint256 rem = diff % size;
if (rem == 0) return min;
result = max - rem + 1;
}
}

// Verbatim from forge-std v1.17.0 `StdUtils._bound(int256,int256,int256)`.
function _forgeStdBound(int256 x, int256 min, int256 max)
internal
pure
returns (int256 result)
{
require(min <= max);
uint256 _x = x < 0 ? (_INT256_MIN_ABS - ~uint256(x) - 1) : (uint256(x) + _INT256_MIN_ABS);
uint256 _min =
min < 0 ? (_INT256_MIN_ABS - ~uint256(min) - 1) : (uint256(min) + _INT256_MIN_ABS);
uint256 _max =
max < 0 ? (_INT256_MIN_ABS - ~uint256(max) - 1) : (uint256(max) + _INT256_MIN_ABS);
uint256 y = _forgeStdBound(_x, _min, _max);
result =
y < _INT256_MIN_ABS ? int256(~(_INT256_MIN_ABS - y) + 1) : int256(y - _INT256_MIN_ABS);
}
}
27 changes: 27 additions & 0 deletions test/utils/SoladyTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,31 @@ contract SoladyTest is Test, TestPlus {
function _bound(uint256 x, uint256 min, uint256 max) internal pure virtual returns (uint256) {
return _hem(x, min, max);
}

/// @dev Signed version of `_bound`, matching forge-std's `_bound(int256,int256,int256)`.
/// Shifts the signed range into uint256 by flipping the sign bit, bounds it,
/// and flips it back. This keeps the ordering, so the endpoint bias is preserved.
function _bound(int256 x, int256 min, int256 max) internal pure virtual returns (int256) {
require(min <= max, "Max is less than min.");
uint256 b = 1 << 255;
return int256(_hem(uint256(x) ^ b, uint256(min) ^ b, uint256(max) ^ b) ^ b);
}

/// @dev Alias for `_bound`, for tests written against forge-std.
function bound(uint256 x, uint256 min, uint256 max) internal pure virtual returns (uint256) {
return _bound(x, min, max);
}

/// @dev Alias for `_bound`, for tests written against forge-std.
function bound(int256 x, int256 min, int256 max) internal pure virtual returns (int256) {
return _bound(x, min, max);
}

/// @dev Bounds `privateKey` to a valid secp256k1 private key, like forge-std.
function boundPrivateKey(uint256 privateKey) internal pure virtual returns (uint256) {
return
_bound(
privateKey, 1, 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140
);
}
}
3 changes: 2 additions & 1 deletion test/utils/mocks/MockETHRecipient.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ contract MockETHRecipient {
mstore(0x00, timestamp())
mstore(0x20, 0)

for { let i := 0 } lt(i, 10) { i := add(i, 1) } {
// Writes enough new slots to exceed `GAS_STIPEND_NO_GRIEF` on any fork.
for { let i := 0 } lt(i, 100) { i := add(i, 1) } {
let h := keccak256(0x00, 0x40)
mstore(0x00, sload(h))
mstore(0x20, i)
Expand Down
Loading