Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust storage layout to consider explicit storage base location #15668

Draft
wants to merge 2 commits into
base: storageLocationsTypeChecking
Choose a base branch
from
Draft
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
7 changes: 4 additions & 3 deletions libsolidity/ast/Types.cpp
Original file line number Diff line number Diff line change
@@ -138,9 +138,9 @@ void Type::clearCache() const
m_stackSize.reset();
}

void StorageOffsets::computeOffsets(TypePointers const& _types)
void StorageOffsets::computeOffsets(TypePointers const& _types, u256 _storageBaseLocation)
{
bigint slotOffset = 0;
bigint slotOffset = bigint(_storageBaseLocation);
unsigned byteOffset = 0;
std::map<size_t, std::pair<u256, unsigned>> offsets;
for (size_t i = 0; i < _types.size(); ++i)
@@ -2160,11 +2160,12 @@ std::vector<std::tuple<VariableDeclaration const*, u256, unsigned>> ContractType
for (VariableDeclaration const* variable: contract->stateVariables())
if (!(variable->isConstant() || variable->immutable()) && variable->referenceLocation() == location)
variables.push_back(variable);

TypePointers types;
for (auto variable: variables)
types.push_back(variable->annotation().type);
StorageOffsets offsets;
offsets.computeOffsets(types);
offsets.computeOffsets(types, m_contract.storageBaseLocationValue());

std::vector<std::tuple<VariableDeclaration const*, u256, unsigned>> variablesAndOffsets;
for (size_t index = 0; index < variables.size(); ++index)
2 changes: 1 addition & 1 deletion libsolidity/ast/Types.h
Original file line number Diff line number Diff line change
@@ -81,7 +81,7 @@ class StorageOffsets
public:
/// Resets the StorageOffsets objects and determines the position in storage for each
/// of the elements of @a _types.
void computeOffsets(TypePointers const& _types);
void computeOffsets(TypePointers const& _types, u256 _storageBaseLocation = 0);
/// @returns the offset of the given member, might be null if the member is not part of storage.
std::pair<u256, unsigned> const* offset(size_t _index) const;
/// @returns the total number of slots occupied by all members.
11 changes: 11 additions & 0 deletions test/cmdlineTests/storage_layout_specified_location/in.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.27;

contract A layout at 42 {
uint x;
uint y;
int8 z;
bool b;
address addr;
uint[2] array;
}
16 changes: 16 additions & 0 deletions test/cmdlineTests/storage_layout_specified_location/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"language": "Solidity",
"sources": {
"fileA": {
"urls": ["storage_layout_specified_location/in.sol"]
}
},
"settings": {
"outputSelection": {
"fileA": {
"A": [ "storageLayout" ],
"": [ "storageLayout" ]
}
}
}
}
93 changes: 93 additions & 0 deletions test/cmdlineTests/storage_layout_specified_location/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
{
"contracts": {
"fileA": {
"A": {
"storageLayout": {
"storage": [
{
"astId": 4,
"contract": "fileA:A",
"label": "x",
"offset": 0,
"slot": "42",
"type": "t_uint256"
},
{
"astId": 6,
"contract": "fileA:A",
"label": "y",
"offset": 0,
"slot": "43",
"type": "t_uint256"
},
{
"astId": 8,
"contract": "fileA:A",
"label": "z",
"offset": 0,
"slot": "44",
"type": "t_int8"
},
{
"astId": 10,
"contract": "fileA:A",
"label": "b",
"offset": 1,
"slot": "44",
"type": "t_bool"
},
{
"astId": 12,
"contract": "fileA:A",
"label": "addr",
"offset": 2,
"slot": "44",
"type": "t_address"
},
{
"astId": 16,
"contract": "fileA:A",
"label": "array",
"offset": 0,
"slot": "45",
"type": "t_array(t_uint256)2_storage"
}
],
"types": {
"t_address": {
"encoding": "inplace",
"label": "address",
"numberOfBytes": "20"
},
"t_array(t_uint256)2_storage": {
"base": "t_uint256",
"encoding": "inplace",
"label": "uint256[2]",
"numberOfBytes": "64"
},
"t_bool": {
"encoding": "inplace",
"label": "bool",
"numberOfBytes": "1"
},
"t_int8": {
"encoding": "inplace",
"label": "int8",
"numberOfBytes": "1"
},
"t_uint256": {
"encoding": "inplace",
"label": "uint256",
"numberOfBytes": "32"
}
}
}
}
}
},
"sources": {
"fileA": {
"id": 0
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.27;

contract A {
uint x;
uint y;
int8 z;
}

contract C layout at 42 {
bool b;
address addr;
uint[2] array;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"language": "Solidity",
"sources": {
"fileA": {
"urls": ["storage_layout_specified_location_inheritance/in.sol"]
}
},
"settings": {
"outputSelection": {
"fileA": {
"A": [ "storageLayout" ],
"": [ "storageLayout" ]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"contracts": {
"fileA": {
"A": {
"storageLayout": {
"storage": [
{
"astId": 3,
"contract": "fileA:A",
"label": "x",
"offset": 0,
"slot": "0",
"type": "t_uint256"
},
{
"astId": 5,
"contract": "fileA:A",
"label": "y",
"offset": 0,
"slot": "1",
"type": "t_uint256"
},
{
"astId": 7,
"contract": "fileA:A",
"label": "z",
"offset": 0,
"slot": "2",
"type": "t_int8"
}
],
"types": {
"t_int8": {
"encoding": "inplace",
"label": "int8",
"numberOfBytes": "1"
},
"t_uint256": {
"encoding": "inplace",
"label": "uint256",
"numberOfBytes": "32"
}
}
}
}
}
},
"sources": {
"fileA": {
"id": 0
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
contract A {
uint32 x;
int y;
bool b;
address a;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
contract B {
bool b;
address a;
uint x;
int32 y;
int64 z;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"language": "Solidity",
"sources": {
"fileA": {
"urls": ["storage_layout_specified_location_two_contracts/contractA.sol"]
},
"fileB": {
"urls": ["storage_layout_specified_location_two_contracts/contractB.sol"]
}
},
"settings": {
"outputSelection": {
"fileA": {
"A": [ "storageLayout" ],
"": [ "storageLayout" ]
}
}
}
}
Loading