Add test for extcodesize check.

This commit is contained in:
chriseth 2021-10-28 12:35:06 +02:00
parent a1aa9d2d90
commit 31c504c5ba
4 changed files with 113 additions and 1 deletions

View File

@ -0,0 +1,28 @@
pragma solidity >= 0.6.0;
// This tests skipping the extcodesize check.
contract T {
constructor() { this.f(); }
function f() external {}
}
contract U {
constructor() { this.f(); }
function f() external returns (uint) {}
}
contract C {
function f(uint c) external returns (uint) {
if (c == 0) new T();
else if (c == 1) new U();
return 1 + c;
}
}
// ====
// EVMVersion: >=byzantium
// compileViaYul: also
// ----
// f(uint256): 0 -> FAILURE
// f(uint256): 1 -> FAILURE
// f(uint256): 2 -> 3

View File

@ -0,0 +1,36 @@
// This tests skipping the extcodesize check.
interface I {
function a() external pure;
function b() external;
function c() external payable;
function x() external returns (uint);
function y() external returns (string memory);
}
contract C {
I i = I(address(0xcafecafe));
constructor() payable {}
function f(uint c) external returns (uint) {
if (c == 0) i.a();
else if (c == 1) i.b();
else if (c == 2) i.c();
else if (c == 3) i.c{value: 1}();
else if (c == 4) i.x();
else if (c == 5) i.y();
return 1 + c;
}
}
// ====
// compileViaYul: also
// ----
// constructor(), 1 ether ->
// gas legacy: 465314
// gas legacyOptimized: 510004
// f(uint256): 0 -> FAILURE
// f(uint256): 1 -> FAILURE
// f(uint256): 2 -> FAILURE
// f(uint256): 3 -> FAILURE
// f(uint256): 4 -> FAILURE
// f(uint256): 5 -> FAILURE
// f(uint256): 6 -> 7

View File

@ -0,0 +1,39 @@
pragma solidity >= 0.6.0;
// This tests skipping the extcodesize check.
interface I {
function a() external pure;
function b() external;
function c() external payable;
function x() external returns (uint);
function y() external returns (string memory);
}
contract C {
I i = I(address(0xcafecafe));
constructor() payable {}
function f(uint c) external returns (uint) {
if (c == 0) i.a();
else if (c == 1) i.b();
else if (c == 2) i.c();
else if (c == 3) i.c{value: 1}();
else if (c == 4) i.x();
else if (c == 5) i.y();
return 1 + c;
}
}
// ====
// EVMVersion: >=byzantium
// compileViaYul: also
// revertStrings: debug
// ----
// constructor(), 1 ether ->
// gas legacyOptimized: 510004
// f(uint256): 0 -> FAILURE, hex"08c379a0", 0x20, 37, "Target contract does not contain", " code"
// f(uint256): 1 -> FAILURE, hex"08c379a0", 0x20, 37, "Target contract does not contain", " code"
// f(uint256): 2 -> FAILURE, hex"08c379a0", 0x20, 37, "Target contract does not contain", " code"
// f(uint256): 3 -> FAILURE, hex"08c379a0", 0x20, 37, "Target contract does not contain", " code"
// f(uint256): 4 -> FAILURE, hex"08c379a0", 0x20, 37, "Target contract does not contain", " code"
// f(uint256): 5 -> FAILURE, hex"08c379a0", 0x20, 37, "Target contract does not contain", " code"
// f(uint256): 6 -> 7

View File

@ -1,13 +1,21 @@
interface Identity {
function selectorAndAppendValue(uint value) external pure returns (uint);
}
interface ReturnMoreData {
function f(uint value) external pure returns (uint, uint, uint);
}
contract C {
Identity constant i = Identity(address(0x0004));
function testHighLevel() external pure returns (bool) {
// Should fail because `extcodesize(4) = 0`
// Works because the extcodesize check is skipped
// and the precompiled contract returns actual data.
i.selectorAndAppendValue(5);
return true;
}
function testHighLevel2() external pure returns (uint, uint, uint) {
// Fails because the identity contract does not return enough data.
return ReturnMoreData(address(4)).f(2);
}
function testLowLevel() external view returns (uint value) {
(bool success, bytes memory ret) =
address(4).staticcall(
@ -23,3 +31,4 @@ contract C {
// ----
// testHighLevel() -> FAILURE
// testLowLevel() -> 0xc76596d400000000000000000000000000000000000000000000000000000000
// testHighLevel2() -> FAILURE