Merge pull request #9843 from ethereum/deleteStructSol2Yul

[Sol->Yul] Implementing delete struct
This commit is contained in:
chriseth 2020-10-08 19:08:40 +02:00 committed by GitHub
commit f8d5c4db36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 205 additions and 30 deletions

View File

@ -33,8 +33,11 @@ string MultiUseYulFunctionCollector::requestedFunctions()
{
string result;
for (auto const& f: m_requestedFunctions)
{
solAssert(f.second != "<<STUB<<", "");
// std::map guarantees ascending order when iterating through its keys.
result += f.second;
}
m_requestedFunctions.clear();
return result;
}
@ -43,9 +46,10 @@ string MultiUseYulFunctionCollector::createFunction(string const& _name, functio
{
if (!m_requestedFunctions.count(_name))
{
m_requestedFunctions[_name] = "<<STUB<<";
string fun = _creator();
solAssert(!fun.empty(), "");
solAssert(fun.find("function " + _name) != string::npos, "Function not properly named.");
solAssert(fun.find("function " + _name + "(") != string::npos, "Function not properly named.");
m_requestedFunctions[_name] = std::move(fun);
}
return _name;

View File

@ -851,7 +851,6 @@ std::string YulUtilFunctions::resizeDynamicArrayFunction(ArrayType const& _type)
solAssert(_type.isDynamicallySized(), "");
solUnimplementedAssert(!_type.isByteArray(), "Byte Arrays not yet implemented!");
solUnimplementedAssert(_type.baseType()->storageBytes() <= 32, "...");
solUnimplementedAssert(_type.baseType()->storageSize() == 1, "");
string functionName = "resize_array_" + _type.identifier();
return m_functionCollector.createFunction(functionName, [&]() {
@ -1147,6 +1146,45 @@ string YulUtilFunctions::clearStorageArrayFunction(ArrayType const& _type)
});
}
string YulUtilFunctions::clearStorageStructFunction(StructType const& _type)
{
solAssert(_type.location() == DataLocation::Storage, "");
string functionName = "clear_struct_storage_" + _type.identifier();
return m_functionCollector.createFunction(functionName, [&] {
MemberList::MemberMap structMembers = _type.nativeMembers(nullptr);
vector<map<string, string>> memberSetValues;
for (auto const& member: structMembers)
{
auto const& [memberSlotDiff, memberStorageOffset] = _type.storageOffsetsOfMember(member.name);
memberSetValues.emplace_back().emplace("clearMember", Whiskers(R"(
<setZero>(add(slot, <memberSlotDiff>), <memberStorageOffset>)
)")
("setZero", storageSetToZeroFunction(*member.type))
("memberSlotDiff", memberSlotDiff.str())
("memberStorageOffset", to_string(memberStorageOffset))
.render()
);
}
return Whiskers(R"(
function <functionName>(slot) {
<#member>
<clearMember>
</member>
}
)")
("functionName", functionName)
("allocStruct", allocateMemoryStructFunction(_type))
("storageSize", _type.storageSize().str())
("member", memberSetValues)
.render();
});
}
string YulUtilFunctions::arrayConvertLengthToSize(ArrayType const& _type)
{
string functionName = "array_convert_length_to_size_" + _type.identifier();
@ -2346,27 +2384,32 @@ string YulUtilFunctions::conversionFunction(Type const& _from, Type const& _to)
auto const& toStructType = dynamic_cast<StructType const &>(_to);
solAssert(fromStructType.structDefinition() == toStructType.structDefinition(), "");
solUnimplementedAssert(!fromStructType.isDynamicallyEncoded(), "");
solUnimplementedAssert(toStructType.location() == DataLocation::Memory, "");
solUnimplementedAssert(fromStructType.location() != DataLocation::Memory, "");
if (fromStructType.location() == DataLocation::CallData)
{
body = Whiskers(R"(
converted := <abiDecode>(value, calldatasize())
)")("abiDecode", ABIFunctions(m_evmVersion, m_revertStrings, m_functionCollector).tupleDecoder(
{&toStructType}
)).render();
}
if (fromStructType.location() == toStructType.location() && toStructType.isPointer())
body = "converted := value";
else
{
solAssert(fromStructType.location() == DataLocation::Storage, "");
solUnimplementedAssert(toStructType.location() == DataLocation::Memory, "");
solUnimplementedAssert(fromStructType.location() != DataLocation::Memory, "");
body = Whiskers(R"(
converted := <readFromStorage>(value)
)")
("readFromStorage", readFromStorage(toStructType, 0, true))
.render();
if (fromStructType.location() == DataLocation::CallData)
{
solUnimplementedAssert(!fromStructType.isDynamicallyEncoded(), "");
body = Whiskers(R"(
converted := <abiDecode>(value, calldatasize())
)")("abiDecode", ABIFunctions(m_evmVersion, m_revertStrings, m_functionCollector).tupleDecoder(
{&toStructType}
)).render();
}
else
{
solAssert(fromStructType.location() == DataLocation::Storage, "");
body = Whiskers(R"(
converted := <readFromStorage>(value)
)")
("readFromStorage", readFromStorage(toStructType, 0, true))
.render();
}
}
break;
@ -2809,11 +2852,24 @@ string YulUtilFunctions::storageSetToZeroFunction(Type const& _type)
else if (_type.category() == Type::Category::Array)
return Whiskers(R"(
function <functionName>(slot, offset) {
if iszero(eq(offset, 0)) { <panic>() }
<clearArray>(slot)
}
)")
("functionName", functionName)
("clearArray", clearStorageArrayFunction(dynamic_cast<ArrayType const&>(_type)))
("panic", panicFunction())
.render();
else if (_type.category() == Type::Category::Struct)
return Whiskers(R"(
function <functionName>(slot, offset) {
if iszero(eq(offset, 0)) { <panic>() }
<clearStruct>(slot)
}
)")
("functionName", functionName)
("clearStruct", clearStorageStructFunction(dynamic_cast<StructType const&>(_type)))
("panic", panicFunction())
.render();
else
solUnimplemented("setToZero for type " + _type.identifier() + " not yet implemented!");

View File

@ -431,6 +431,10 @@ private:
/// signature: (slot, offset)
std::string partialClearStorageSlotFunction();
/// @returns the name of a function that will clear the given storage struct
/// signature: (slot) ->
std::string clearStorageStructFunction(StructType const& _type);
langutil::EVMVersion m_evmVersion;
RevertStrings m_revertStrings;
MultiUseYulFunctionCollector& m_functionCollector;

View File

@ -1,5 +1,5 @@
Error (1834): Unimplemented feature error: setToZero for type t_struct$_S_$4_storage not yet implemented! in FILENAME REMOVED
--> yul_unimplemented/input.sol:9:9:
Error (1834): Unimplemented feature error: Byte Arrays not yet implemented! in FILENAME REMOVED
--> yul_unimplemented/input.sol:6:9:
|
9 | delete str;
| ^^^^^^^^^^
6 | delete a;
| ^^^^^^^^

View File

@ -1,11 +1,8 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.0;
contract test {
struct S {
uint x;
}
S str;
constructor() {
delete str;
bytes a;
function f() public {
delete a;
}
}

View File

@ -16,5 +16,7 @@ contract C {
}
}
// ====
// compileViaYul: also
// ----
// f() -> true # This code interprets x as an array length and thus will go out of gas. neither of the two should throw due to out-of-bounds access #

View File

@ -31,6 +31,8 @@ contract C {
}
}
// ====
// compileViaYul: also
// ----
// l() -> 0
// f(uint256): 42 ->

View File

@ -18,6 +18,8 @@ contract C {
}
}
// ====
// compileViaYul: also
// ----
// f() -> true
// a() -> 7

View File

@ -21,5 +21,7 @@ contract C {
return (s.f(), h(s));
}
}
// ====
// compileViaYul: also
// ----
// g() -> 7, 7

View File

@ -0,0 +1,26 @@
contract C {
struct S {
uint16 v;
S[] x;
}
uint8[77] padding;
S s;
constructor() {
s.v = 21;
s.x.push(); s.x.push(); s.x.push();
s.x[0].v = 101; s.x[1].v = 102; s.x[2].v = 103;
}
function f() public returns (uint256 a, uint256 b, uint256 c, uint256 d) {
S storage sptr1 = s.x[0];
S storage sptr2 = s.x[1];
S storage sptr3 = s.x[2];
uint256 slot1; uint256 slot2; uint256 slot3;
assembly { slot1 := sptr1.slot slot2 := sptr2.slot slot3 := sptr3.slot }
delete s;
assembly { a := sload(s.slot) b := sload(slot1) c := sload(slot2) d := sload(slot3) }
}
}
// ====
// compileViaYul: also
// ----
// f() -> 0, 0, 0, 0

View File

@ -15,5 +15,7 @@ contract C {
}
}
// ====
// compileViaYul: also
// ----
// f() -> 1

View File

@ -16,5 +16,7 @@ contract test {
}
}
// ====
// compileViaYul: also
// ----
// deleteMember() -> 0

View File

@ -0,0 +1,23 @@
contract C {
struct S {
uint256 x;
uint128 y;
uint32 z;
}
uint8 b = 23;
S s;
uint8 a = 17;
function f() public {
s.x = 42; s.y = 42; s.y = 42;
delete s;
assert(s.x == 0);
assert(s.y == 0);
assert(s.z == 0);
assert(b == 23);
assert(a == 17);
}
}
// ====
// compileViaYul: also
// ----
// f() ->

View File

@ -0,0 +1,47 @@
pragma experimental ABIEncoderV2;
contract C {
struct S {
uint128 a;
uint256[] x;
uint240 b;
}
uint8 b = 23;
S s;
uint8 a = 17;
function f() public {
delete s;
s.x.push(42); s.x.push(42); s.x.push(42);
delete s;
assert(s.x.length == 0);
uint256[] storage x = s.x;
assembly { sstore(x.slot, 3) }
assert(s.x[0] == 0);
assert(s.x[1] == 0);
assert(s.x[2] == 0);
assert(b == 23);
assert(a == 17);
}
function g() public {
delete s;
s.x.push(42); s.x.push(42); s.x.push(42);
s.a = 1; s.b = 2;
delete s.x;
assert(s.x.length == 0);
uint256[] storage x = s.x;
assembly { sstore(x.slot, 3) }
assert(s.x[0] == 0);
assert(s.x[1] == 0);
assert(s.x[2] == 0);
assert(b == 23);
assert(a == 17);
assert(s.a == 1);
assert(s.b == 2);
}
}
// ====
// compileViaYul: also
// ----
// f() ->
// g() ->

View File

@ -14,5 +14,7 @@ contract test {
}
}
// ====
// compileViaYul: also
// ----
// deleteIt() -> 0

View File

@ -18,6 +18,8 @@ contract test {
inner.recursive[0].z = inner.recursive[1].z + 1;
}
}
// ====
// compileViaYul: also
// ----
// check() -> false
// set() ->

View File

@ -22,5 +22,7 @@ contract C {
}
}
// ====
// compileViaYul: also
// ----
// f() -> true