Merge pull request #10953 from ethereum/modifier-function-return

[Sol2Yul] IR bug on Modifier with argument being a function return variable
This commit is contained in:
chriseth 2021-02-11 17:32:40 +01:00 committed by GitHub
commit 003701f6bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 1 deletions

View File

@ -323,7 +323,7 @@ string IRGenerator::generateModifier(
t("functionName", functionName);
vector<string> retParamsIn;
for (auto const& varDecl: _function.returnParameters())
retParamsIn += IRVariable(*varDecl).stackSlots();
retParamsIn += m_context.addLocalVariable(*varDecl).stackSlots();
vector<string> params = retParamsIn;
for (auto const& varDecl: _function.parameters())
params += m_context.addLocalVariable(*varDecl).stackSlots();

View File

@ -0,0 +1,10 @@
// The IR of this contract used to throw
contract B {
function f(uint8 a) mod1(a, true) mod2(r) pure public returns (bytes7 r) { }
modifier mod1(uint a, bool b) { if (b) _; }
modifier mod2(bytes7 a) { while (a == "1234567") _; }
}
// ====
// compileViaYul: also
// ----
// f(uint8): 5 -> 0x00

View File

@ -0,0 +1,38 @@
// Test to see if the function return parameter, when forwarded to the modifier actually has value
// zero.
contract A {
uint public x = 0;
modifier alwaysZeros(uint256 a, uint256 b) {
x++;
_;
require(a == 0, "a is not zero");
require(b == 0, "b is not zero");
}
function f() public alwaysZeros(r1, r3) returns(uint r1, uint r2, uint r3) {
r1 = 16;
r2 = 32;
r3 = 64;
}
function shouldFail(uint i1) public alwaysZeros(i1, r + 20) returns (uint r) {
r = 0;
}
// The value of x would be 1 before calling this. It gets incremented four times in total during
// the modifier calls
function g() alwaysZeros(r, r) alwaysZeros(r, r) alwaysZeros(r + r, r - r) alwaysZeros(r * r, r & r) public returns (uint r) {
r = x;
}
}
// ====
// compileViaYul: also
// ----
// f() -> 0x10, 0x20, 0x40
// x() -> 1
// shouldFail(uint256): 1 -> FAILURE, hex"08c379a0", 0x20, 13, "a is not zero"
// shouldFail(uint256): 0 -> FAILURE, hex"08c379a0", 0x20, 13, "b is not zero"
// x() -> 1
// g() -> 5
// x() -> 5