mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Use .slot and .offest to access storage items.
This commit is contained in:
@@ -2,38 +2,38 @@ contract c {
|
||||
bytes data;
|
||||
function test_short() public returns (uint256 r) {
|
||||
assembly {
|
||||
sstore(data_slot, 0)
|
||||
sstore(data.slot, 0)
|
||||
}
|
||||
for (uint8 i = 0; i < 15; i++) {
|
||||
data.push(bytes1(i));
|
||||
}
|
||||
assembly {
|
||||
r := sload(data_slot)
|
||||
r := sload(data.slot)
|
||||
}
|
||||
}
|
||||
|
||||
function test_long() public returns (uint256 r) {
|
||||
assembly {
|
||||
sstore(data_slot, 0)
|
||||
sstore(data.slot, 0)
|
||||
}
|
||||
for (uint8 i = 0; i < 33; i++) {
|
||||
data.push(bytes1(i));
|
||||
}
|
||||
assembly {
|
||||
r := sload(data_slot)
|
||||
r := sload(data.slot)
|
||||
}
|
||||
}
|
||||
|
||||
function test_pop() public returns (uint256 r) {
|
||||
assembly {
|
||||
sstore(data_slot, 0)
|
||||
sstore(data.slot, 0)
|
||||
}
|
||||
for (uint8 i = 0; i < 32; i++) {
|
||||
data.push(bytes1(i));
|
||||
}
|
||||
data.pop();
|
||||
assembly {
|
||||
r := sload(data_slot)
|
||||
r := sload(data.slot)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ contract C {
|
||||
data.push(123);
|
||||
delete data;
|
||||
assembly {
|
||||
ret := sload(data_slot)
|
||||
ret := sload(data.slot)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ contract C {
|
||||
}
|
||||
x = m;
|
||||
assembly {
|
||||
r := sload(x_slot)
|
||||
r := sload(x.slot)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,9 +7,9 @@ contract C {
|
||||
uint256 off1;
|
||||
uint256 off2;
|
||||
assembly {
|
||||
sstore(z_slot, 7)
|
||||
off1 := z_offset
|
||||
off2 := y_offset
|
||||
sstore(z.slot, 7)
|
||||
off1 := z.offset
|
||||
off2 := y.offset
|
||||
}
|
||||
assert(off1 == 0);
|
||||
assert(off2 == 2);
|
||||
|
||||
+2
-2
@@ -8,8 +8,8 @@ contract C {
|
||||
uint256 off2;
|
||||
assembly {
|
||||
function f() -> o1 {
|
||||
sstore(z_slot, 7)
|
||||
o1 := y_offset
|
||||
sstore(z.slot, 7)
|
||||
o1 := y.offset
|
||||
}
|
||||
off2 := f()
|
||||
}
|
||||
|
||||
+2
-2
@@ -10,8 +10,8 @@ contract C {
|
||||
Data storage x = a;
|
||||
uint256 off;
|
||||
assembly {
|
||||
sstore(x_slot, 7)
|
||||
off := x_offset
|
||||
sstore(x.slot, 7)
|
||||
off := x.offset
|
||||
}
|
||||
assert(off == 0);
|
||||
return true;
|
||||
|
||||
@@ -13,7 +13,7 @@ contract C {
|
||||
|
||||
bytes32 slot = keccak256(abi.encode(uint(1), uint(0)));
|
||||
assembly {
|
||||
_data_slot := slot
|
||||
_data.slot := slot
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,13 +12,13 @@ contract C {
|
||||
}
|
||||
function g() public returns (bool, uint256) {
|
||||
uint256 ys;
|
||||
assembly { ys := y_slot }
|
||||
assembly { ys := y.slot }
|
||||
(bool success, bytes memory data) = address(L).delegatecall(abi.encodeWithSelector(L.f.selector, ys));
|
||||
return (success, success ? abi.decode(data,(uint256)) : 0);
|
||||
}
|
||||
function h() public returns (bool, uint256) {
|
||||
uint256 ys;
|
||||
assembly { ys := y_slot }
|
||||
assembly { ys := y.slot }
|
||||
(bool success, bytes memory data) = address(L).call(abi.encodeWithSelector(L.f.selector, ys));
|
||||
return (success, success ? abi.decode(data,(uint256)) : 0);
|
||||
}
|
||||
|
||||
@@ -12,13 +12,13 @@ contract C {
|
||||
}
|
||||
function g() public returns (bool, uint256) {
|
||||
uint256 ys;
|
||||
assembly { ys := y_slot }
|
||||
assembly { ys := y.slot }
|
||||
(bool success, bytes memory data) = address(L).delegatecall(abi.encodeWithSelector(L.f.selector, ys));
|
||||
return (success, success ? abi.decode(data,(uint256)) : 0);
|
||||
}
|
||||
function h() public returns (bool, uint256) {
|
||||
uint256 ys;
|
||||
assembly { ys := y_slot }
|
||||
assembly { ys := y.slot }
|
||||
(bool success, bytes memory data) = address(L).call(abi.encodeWithSelector(L.f.selector, ys));
|
||||
return (success, success ? abi.decode(data,(uint256)) : 0);
|
||||
}
|
||||
|
||||
+2
-2
@@ -11,13 +11,13 @@ contract C {
|
||||
}
|
||||
function g() public returns (bool, uint256) {
|
||||
uint256 ys;
|
||||
assembly { ys := y_slot }
|
||||
assembly { ys := y.slot }
|
||||
(bool success, bytes memory data) = address(L).delegatecall(abi.encodeWithSelector(L.f.selector, ys));
|
||||
return (success, success ? abi.decode(data,(uint256)) : 0);
|
||||
}
|
||||
function h() public returns (bool, uint256) {
|
||||
uint256 ys;
|
||||
assembly { ys := y_slot }
|
||||
assembly { ys := y.slot }
|
||||
(bool success, bytes memory data) = address(L).call(abi.encodeWithSelector(L.f.selector, ys));
|
||||
return (success, success ? abi.decode(data,(uint256)) : 0);
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ contract C {
|
||||
}
|
||||
function g() public returns (bool, bool, uint256) {
|
||||
uint256 s_ptr;
|
||||
assembly { s_ptr := s_slot }
|
||||
assembly { s_ptr := s.slot }
|
||||
(bool success, bytes memory data) = address(L).delegatecall(abi.encodeWithSelector(L.g.selector, s_ptr));
|
||||
return (L.g.selector == bytes4(keccak256("g(uint256[] storage)")), success, abi.decode(data, (uint256)));
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ contract C {
|
||||
|
||||
function f() public returns (bool, bool, uint256) {
|
||||
uint256 s_ptr;
|
||||
assembly { s_ptr := s_slot }
|
||||
assembly { s_ptr := s.slot }
|
||||
(bool success, bytes memory data) = address(L).delegatecall(abi.encodeWithSelector(L.f.selector, s_ptr));
|
||||
return (L.f.selector == bytes4(keccak256("f(L.S storage)")), success, abi.decode(data, (uint256)));
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ contract C {
|
||||
for (uint i = 3; i < len; i++)
|
||||
{
|
||||
assembly {
|
||||
mstore(0, storageArray_slot)
|
||||
mstore(0, storageArray.slot)
|
||||
let pos := add(keccak256(0, 0x20), i)
|
||||
|
||||
if iszero(eq(sload(pos), 0)) {
|
||||
|
||||
@@ -2,7 +2,7 @@ contract C {
|
||||
bytes b;
|
||||
function f() public returns (bool correct) {
|
||||
assembly {
|
||||
sstore(b_slot, or("deadbeef", 0x08))
|
||||
sstore(b.slot, or("deadbeef", 0x08))
|
||||
}
|
||||
byte s = b[3];
|
||||
uint r;
|
||||
|
||||
@@ -2,8 +2,8 @@ contract C {
|
||||
bytes b;
|
||||
function f() public returns (bool correct) {
|
||||
assembly {
|
||||
sstore(b_slot, 0x41)
|
||||
mstore(0, b_slot)
|
||||
sstore(b.slot, 0x41)
|
||||
mstore(0, b.slot)
|
||||
sstore(keccak256(0, 0x20), "deadbeefdeadbeefdeadbeefdeadbeef")
|
||||
}
|
||||
byte s = b[31];
|
||||
|
||||
@@ -3,7 +3,7 @@ contract C {
|
||||
function f() public returns (bool correct) {
|
||||
s.push();
|
||||
assembly {
|
||||
mstore(0, s_slot)
|
||||
mstore(0, s.slot)
|
||||
sstore(keccak256(0, 0x20), 257)
|
||||
}
|
||||
uint8 x = s[0];
|
||||
|
||||
@@ -2,7 +2,7 @@ contract C {
|
||||
uint8[1] s;
|
||||
function f() public returns (bool correct) {
|
||||
assembly {
|
||||
sstore(s_slot, 257)
|
||||
sstore(s.slot, 257)
|
||||
}
|
||||
uint8 x = s[0];
|
||||
uint r;
|
||||
|
||||
@@ -6,7 +6,7 @@ contract C {
|
||||
function f() public returns (bool correct) {
|
||||
s.m.push();
|
||||
assembly {
|
||||
mstore(0, s_slot)
|
||||
mstore(0, s.slot)
|
||||
sstore(keccak256(0, 0x20), 257)
|
||||
}
|
||||
uint8 x = s.m[0];
|
||||
|
||||
Reference in New Issue
Block a user