Skip to content

Commit 83c7e45

Browse files
Amxxernestognw
andauthored
Fix dirty bits in upper bits in implementation address in Clones.sol (#5069)
Co-authored-by: ernestognw <[email protected]>
1 parent 8a990e6 commit 83c7e45

File tree

2 files changed

+52
-12
lines changed

2 files changed

+52
-12
lines changed

contracts/proxy/Clones.sol

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,11 @@ library Clones {
3939
}
4040
/// @solidity memory-safe-assembly
4141
assembly {
42-
// Stores the bytecode after address
43-
mstore(0x20, 0x5af43d82803e903d91602b57fd5bf3)
44-
// implementation address
45-
mstore(0x11, implementation)
46-
// Packs the first 3 bytes of the `implementation` address with the bytecode before the address.
47-
mstore(0x00, or(shr(0x88, implementation), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000))
42+
// Cleans the upper 96 bits of the `implementation` word, then packs the first 3 bytes
43+
// of the `implementation` address with the bytecode before the address.
44+
mstore(0x00, or(shr(0xe8, shl(0x60, implementation)), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000))
45+
// Packs the remaining 17 bytes of `implementation` with the bytecode after the address.
46+
mstore(0x20, or(shl(0x78, implementation), 0x5af43d82803e903d91602b57fd5bf3))
4847
instance := create(value, 0x09, 0x37)
4948
}
5049
if (instance == address(0)) {
@@ -80,12 +79,11 @@ library Clones {
8079
}
8180
/// @solidity memory-safe-assembly
8281
assembly {
83-
// Stores the bytecode after address
84-
mstore(0x20, 0x5af43d82803e903d91602b57fd5bf3)
85-
// implementation address
86-
mstore(0x11, implementation)
87-
// Packs the first 3 bytes of the `implementation` address with the bytecode before the address.
88-
mstore(0x00, or(shr(0x88, implementation), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000))
82+
// Cleans the upper 96 bits of the `implementation` word, then packs the first 3 bytes
83+
// of the `implementation` address with the bytecode before the address.
84+
mstore(0x00, or(shr(0xe8, shl(0x60, implementation)), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000))
85+
// Packs the remaining 17 bytes of `implementation` with the bytecode after the address.
86+
mstore(0x20, or(shl(0x78, implementation), 0x5af43d82803e903d91602b57fd5bf3))
8987
instance := create2(value, 0x09, 0x37, salt)
9088
}
9189
if (instance == address(0)) {

test/proxy/Clones.t.sol

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import {Test} from "forge-std/Test.sol";
66
import {Clones} from "@openzeppelin/contracts/proxy/Clones.sol";
77

88
contract ClonesTest is Test {
9+
function getNumber() external pure returns (uint256) {
10+
return 42;
11+
}
12+
913
function testSymbolicPredictDeterministicAddressSpillage(address implementation, bytes32 salt) public {
1014
address predicted = Clones.predictDeterministicAddress(implementation, salt);
1115
bytes32 spillage;
@@ -15,4 +19,42 @@ contract ClonesTest is Test {
1519
}
1620
assertEq(spillage, bytes32(0));
1721
}
22+
23+
function testCloneDirty() external {
24+
address cloneClean = Clones.clone(address(this));
25+
address cloneDirty = Clones.clone(_dirty(address(this)));
26+
27+
// both clones have the same code
28+
assertEq(keccak256(cloneClean.code), keccak256(cloneDirty.code));
29+
30+
// both clones behave as expected
31+
assertEq(ClonesTest(cloneClean).getNumber(), this.getNumber());
32+
assertEq(ClonesTest(cloneDirty).getNumber(), this.getNumber());
33+
}
34+
35+
function testCloneDeterministicDirty(bytes32 salt) external {
36+
address cloneClean = Clones.cloneDeterministic(address(this), salt);
37+
address cloneDirty = Clones.cloneDeterministic(_dirty(address(this)), ~salt);
38+
39+
// both clones have the same code
40+
assertEq(keccak256(cloneClean.code), keccak256(cloneDirty.code));
41+
42+
// both clones behave as expected
43+
assertEq(ClonesTest(cloneClean).getNumber(), this.getNumber());
44+
assertEq(ClonesTest(cloneDirty).getNumber(), this.getNumber());
45+
}
46+
47+
function testPredictDeterministicAddressDirty(bytes32 salt) external {
48+
address predictClean = Clones.predictDeterministicAddress(address(this), salt);
49+
address predictDirty = Clones.predictDeterministicAddress(_dirty(address(this)), salt);
50+
51+
//prediction should be similar
52+
assertEq(predictClean, predictDirty);
53+
}
54+
55+
function _dirty(address input) private pure returns (address output) {
56+
assembly ("memory-safe") {
57+
output := or(input, shl(160, not(0)))
58+
}
59+
}
1860
}

0 commit comments

Comments
 (0)