Skip to content

Commit

Permalink
fix: [internal-r5] make sure even there are some gaps between offseta…
Browse files Browse the repository at this point in the history
…nd length in the abi-encoding of extsload#slots the function will still work (#120)

* fix: [internal-r5] make sure even there are some gaps between offset and length in the abi-encoding of extsload#slots the function will still work

* fix: [internal-r4] make sure all assembly block are memory safe
  • Loading branch information
chefburger authored Jul 25, 2024
1 parent 870eb15 commit 2aa42c4
Show file tree
Hide file tree
Showing 22 changed files with 55 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testBurnSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
178819
178871
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testMintSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
328598
328650
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerBytecodeSize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
23620
23691
Original file line number Diff line number Diff line change
@@ -1 +1 @@
133834
133876
Original file line number Diff line number Diff line change
@@ -1 +1 @@
142745
142797
Original file line number Diff line number Diff line change
@@ -1 +1 @@
288502
288876
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
127005
127046
Original file line number Diff line number Diff line change
@@ -1 +1 @@
967714
968182
Original file line number Diff line number Diff line change
@@ -1 +1 @@
325931
326399
Original file line number Diff line number Diff line change
@@ -1 +1 @@
337317
337369
Original file line number Diff line number Diff line change
@@ -1 +1 @@
139719
139771
Original file line number Diff line number Diff line change
@@ -1 +1 @@
304287
304339
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerBytecodeSize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
22712
22811
Original file line number Diff line number Diff line change
@@ -1 +1 @@
352371
352438
Original file line number Diff line number Diff line change
@@ -1 +1 @@
167812
167879
Original file line number Diff line number Diff line change
@@ -1 +1 @@
239219
239286
Original file line number Diff line number Diff line change
@@ -1 +1 @@
115846
115913
2 changes: 1 addition & 1 deletion .forge-snapshots/ExtsloadTest#extsloadInBatch.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
11050
11109
21 changes: 11 additions & 10 deletions src/Extsload.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,25 @@ abstract contract Extsload is IExtsload {

/// @inheritdoc IExtsload
function extsload(bytes32[] calldata slots) external view returns (bytes32[] memory) {
// since the function is external and enters a new call context and exits right
// after execution, Solidity's memory management convention can be disregarded
// and a direct slice of memory can be returned
assembly ("memory-safe") {
// Copy the abi offset of dynamic array and the length of the array to memory.
calldatacopy(0, 0x04, 0x40)
let memptr := mload(0x40)
let start := memptr
// for abi encoding the response - the array will be found at 0x20
mstore(memptr, 0x20)
// next we store the length of the return array
mstore(add(memptr, 0x20), slots.length)
// update memptr to the first location to hold an array entry
memptr := add(memptr, 0x40)
// A left bit-shift of 5 is equivalent to multiplying by 32 but costs less gas.
let end := add(0x40, shl(5, slots.length))
let end := add(memptr, shl(5, slots.length))
let calldataptr := slots.offset
// Return values will start at 64 while calldata offset is 68.
for { let memptr := 0x40 } 1 {} {
for {} 1 {} {
mstore(memptr, sload(calldataload(calldataptr)))
memptr := add(memptr, 0x20)
calldataptr := add(calldataptr, 0x20)
if iszero(lt(memptr, end)) { break }
}
// The end offset is also the length of the returndata.
return(0, end)
return(start, sub(end, start))
}
}
}
17 changes: 10 additions & 7 deletions src/pool-bin/libraries/BinPosition.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@ library BinPosition {
// ref: https://github.com/Vectorized/solady/blob/main/src/tokens/ERC20.sol#L95
// memory will be 12 bytes of zeros, the 20 bytes of address, 3 bytes for uint24
assembly ("memory-safe") {
mstore(0x23, salt)
mstore(0x03, binId)
mstore(0x00, owner)
key := keccak256(0x0c, 0x37)
// 0x00 - 0x3f is scratch space
// 0x40 ~ 0x46 should be clear to avoid polluting free pointer
mstore(0x23, 0)
let fmp := mload(0x40)
mstore(add(fmp, 0x23), salt) // [0x23, 0x43)
mstore(add(fmp, 0x03), binId) // [0x03, 0x23)
mstore(fmp, owner) // [0x0c, 0x20)
key := keccak256(add(fmp, 0x0c), 0x37) // len is 55 bytes

// now clean the memory we used
mstore(add(fmp, 0x40), 0) // fmp+0x40 held salt
mstore(add(fmp, 0x20), 0) // fmp+0x20 held binId, salt
mstore(fmp, 0) // fmp held owner
}
position = self[key];
}
Expand Down
19 changes: 11 additions & 8 deletions src/pool-cl/libraries/CLPosition.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,17 @@ library CLPosition {
// make use of memory scratch space
// ref: https://github.com/Vectorized/solady/blob/main/src/tokens/ERC20.sol#L95
assembly ("memory-safe") {
mstore(0x26, salt)
mstore(0x06, tickUpper)
mstore(0x03, tickLower)
mstore(0x00, owner)
key := keccak256(0x0c, 0x3a)
// 0x00 - 0x3f is scratch space
// 0x40 ~ 0x46 should be clear to avoid polluting free pointer
mstore(0x26, 0)
let fmp := mload(0x40)
mstore(add(fmp, 0x26), salt) // [0x26, 0x46)
mstore(add(fmp, 0x06), tickUpper) // [0x23, 0x26)
mstore(add(fmp, 0x03), tickLower) // [0x20, 0x23)
mstore(fmp, owner) // [0x0c, 0x20)
key := keccak256(add(fmp, 0x0c), 0x3a) // len is 58 bytes

// now clean the memory we used
mstore(add(fmp, 0x40), 0) // fmp+0x40 held salt
mstore(add(fmp, 0x20), 0) // fmp+0x20 held tickLower, tickUpper, salt
mstore(fmp, 0) // fmp held owner
}
position = self[key];
}
Expand Down
9 changes: 5 additions & 4 deletions src/pool-cl/libraries/TickBitmap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ library TickBitmap {
assembly ("memory-safe") {
// ensure that the tick is spaced
if smod(tick, tickSpacing) {
mstore(0, 0xd4d8f3e6) // selector for TickMisaligned(int24,int24)
mstore(0x20, tick)
mstore(0x40, tickSpacing)
revert(0x1c, 0x44)
let fmp := mload(0x40)
mstore(fmp, 0xd4d8f3e6) // selector for TickMisaligned(int24,int24)
mstore(add(fmp, 0x20), tick)
mstore(add(fmp, 0x40), tickSpacing)
revert(add(fmp, 0x1c), 0x44)
}
tick := sdiv(tick, tickSpacing)
// calculate the storage slot corresponding to the tick
Expand Down

0 comments on commit 2aa42c4

Please sign in to comment.