mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
test: some tests for push0
1. `push0_disallowed.yul`: checks if `push0()` is a valid builtin in strict Yul 2. `push0_disallowed.sol`: checks if `push0()` is a valid builtin in inline assembly 3. `push0.sol`: simple semantic test that returns 0 4. `evmone_support.sol`: tests if push0 works properly in evmone 5. Updated some bytecode too large tests to use `shanghai` as version 6. Updated various tests where `push1 0` was hardcoded in different forms / expectations on bytecode size (`Assembler.cpp`, `GasCosts.cpp`, `SolidityCompiler.cpp`, `SolidityExpressionCompiler.cpp`)
This commit is contained in:
@@ -112,15 +112,21 @@ BOOST_AUTO_TEST_CASE(string_storage)
|
||||
// Costs with 0 are cases which cannot be triggered in tests.
|
||||
if (evmVersion < EVMVersion::istanbul())
|
||||
CHECK_DEPLOY_GAS(0, 109241, evmVersion);
|
||||
else
|
||||
else if (evmVersion < EVMVersion::shanghai())
|
||||
CHECK_DEPLOY_GAS(0, 97697, evmVersion);
|
||||
// Shanghai is cheaper due to `push0`
|
||||
else
|
||||
CHECK_DEPLOY_GAS(0, 97071, evmVersion);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (evmVersion < EVMVersion::istanbul())
|
||||
CHECK_DEPLOY_GAS(139013, 123969, evmVersion);
|
||||
else
|
||||
else if (evmVersion < EVMVersion::shanghai())
|
||||
CHECK_DEPLOY_GAS(123361, 110969, evmVersion);
|
||||
// Shanghai is cheaper due to `push0`
|
||||
else
|
||||
CHECK_DEPLOY_GAS(121493, 110969, evmVersion);
|
||||
}
|
||||
}
|
||||
else if (evmVersion < EVMVersion::istanbul())
|
||||
@@ -198,7 +204,11 @@ BOOST_AUTO_TEST_CASE(single_callvaluecheck)
|
||||
size_t bytecodeSizeNonpayable = m_compiler.object("Nonpayable").bytecode.size();
|
||||
size_t bytecodeSizePayable = m_compiler.object("Payable").bytecode.size();
|
||||
|
||||
BOOST_CHECK_EQUAL(bytecodeSizePayable - bytecodeSizeNonpayable, 26);
|
||||
auto evmVersion = solidity::test::CommonOptions::get().evmVersion();
|
||||
if (evmVersion < EVMVersion::shanghai())
|
||||
BOOST_CHECK_EQUAL(bytecodeSizePayable - bytecodeSizeNonpayable, 26);
|
||||
else
|
||||
BOOST_CHECK_EQUAL(bytecodeSizePayable - bytecodeSizeNonpayable, 24);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
@@ -47,7 +47,9 @@ BOOST_AUTO_TEST_CASE(does_not_include_creation_time_only_internal_functions)
|
||||
bytes const& runtimeBytecode = solidity::test::bytecodeSansMetadata(compiler().runtimeObject("C").bytecode);
|
||||
BOOST_CHECK(creationBytecode.size() >= 90);
|
||||
BOOST_CHECK(creationBytecode.size() <= 120);
|
||||
BOOST_CHECK(runtimeBytecode.size() >= 10);
|
||||
auto evmVersion = solidity::test::CommonOptions::get().evmVersion();
|
||||
unsigned threshold = evmVersion.hasPush0() ? 9 : 10;
|
||||
BOOST_CHECK(runtimeBytecode.size() >= threshold);
|
||||
BOOST_CHECK(runtimeBytecode.size() <= 30);
|
||||
}
|
||||
|
||||
|
||||
@@ -210,7 +210,10 @@ BOOST_AUTO_TEST_CASE(literal_false)
|
||||
)";
|
||||
bytes code = compileFirstExpression(sourceCode);
|
||||
|
||||
bytes expectation({uint8_t(Instruction::PUSH1), 0x0});
|
||||
bytes expectation = solidity::test::CommonOptions::get().evmVersion().hasPush0() ?
|
||||
bytes{uint8_t(Instruction::PUSH0)} :
|
||||
bytes{uint8_t(Instruction::PUSH1), 0x0};
|
||||
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
|
||||
}
|
||||
|
||||
@@ -344,21 +347,27 @@ BOOST_AUTO_TEST_CASE(arithmetic)
|
||||
}
|
||||
)";
|
||||
bytes code = compileFirstExpression(sourceCode, {}, {{"test", "f", "y"}});
|
||||
|
||||
bool hasPush0 = solidity::test::CommonOptions::get().evmVersion().hasPush0();
|
||||
bytes push0Bytes = hasPush0 ?
|
||||
bytes{uint8_t(Instruction::PUSH0)} :
|
||||
bytes{uint8_t(Instruction::PUSH1), 0x0};
|
||||
uint8_t size = hasPush0 ? 0x65: 0x67;
|
||||
bytes panic =
|
||||
bytes{
|
||||
uint8_t(Instruction::JUMPDEST),
|
||||
uint8_t(Instruction::PUSH32)
|
||||
} +
|
||||
util::fromHex("4E487B7100000000000000000000000000000000000000000000000000000000") +
|
||||
push0Bytes +
|
||||
bytes{
|
||||
uint8_t(Instruction::PUSH1), 0x0,
|
||||
uint8_t(Instruction::MSTORE),
|
||||
uint8_t(Instruction::PUSH1), 0x12,
|
||||
uint8_t(Instruction::PUSH1), 0x4,
|
||||
uint8_t(Instruction::MSTORE),
|
||||
uint8_t(Instruction::PUSH1), 0x24,
|
||||
uint8_t(Instruction::PUSH1), 0x0,
|
||||
uint8_t(Instruction::PUSH1), 0x24
|
||||
} +
|
||||
push0Bytes +
|
||||
bytes{
|
||||
uint8_t(Instruction::REVERT),
|
||||
uint8_t(Instruction::JUMPDEST),
|
||||
uint8_t(Instruction::JUMP),
|
||||
@@ -405,7 +414,7 @@ BOOST_AUTO_TEST_CASE(arithmetic)
|
||||
uint8_t(Instruction::DIV),
|
||||
uint8_t(Instruction::PUSH1), 0x1,
|
||||
uint8_t(Instruction::MUL),
|
||||
uint8_t(Instruction::PUSH1), 0x67,
|
||||
uint8_t(Instruction::PUSH1), size,
|
||||
uint8_t(Instruction::JUMP)
|
||||
} + panic;
|
||||
else
|
||||
@@ -447,7 +456,7 @@ BOOST_AUTO_TEST_CASE(arithmetic)
|
||||
uint8_t(Instruction::JUMPDEST),
|
||||
uint8_t(Instruction::DIV),
|
||||
uint8_t(Instruction::MUL),
|
||||
uint8_t(Instruction::PUSH1), 0x67,
|
||||
uint8_t(Instruction::PUSH1), size,
|
||||
uint8_t(Instruction::JUMP)
|
||||
} + panic;
|
||||
|
||||
@@ -463,11 +472,17 @@ BOOST_AUTO_TEST_CASE(unary_operators)
|
||||
)";
|
||||
bytes code = compileFirstExpression(sourceCode, {}, {{"test", "f", "y"}});
|
||||
|
||||
bytes push0Bytes = solidity::test::CommonOptions::get().evmVersion().hasPush0() ?
|
||||
bytes{uint8_t(Instruction::PUSH0)} :
|
||||
bytes{uint8_t(Instruction::PUSH1), 0x0};
|
||||
|
||||
bytes expectation;
|
||||
if (solidity::test::CommonOptions::get().optimize)
|
||||
expectation = {
|
||||
expectation = bytes{
|
||||
uint8_t(Instruction::DUP1),
|
||||
uint8_t(Instruction::PUSH1), 0x0,
|
||||
} +
|
||||
push0Bytes +
|
||||
bytes{
|
||||
uint8_t(Instruction::SUB),
|
||||
uint8_t(Instruction::NOT),
|
||||
uint8_t(Instruction::PUSH1), 0x2,
|
||||
@@ -475,10 +490,12 @@ BOOST_AUTO_TEST_CASE(unary_operators)
|
||||
uint8_t(Instruction::ISZERO)
|
||||
};
|
||||
else
|
||||
expectation = {
|
||||
expectation = bytes{
|
||||
uint8_t(Instruction::PUSH1), 0x2,
|
||||
uint8_t(Instruction::DUP2),
|
||||
uint8_t(Instruction::PUSH1), 0x0,
|
||||
} +
|
||||
push0Bytes +
|
||||
bytes{
|
||||
uint8_t(Instruction::SUB),
|
||||
uint8_t(Instruction::NOT),
|
||||
uint8_t(Instruction::EQ),
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
contract ShortReturn {
|
||||
constructor() {
|
||||
assembly {
|
||||
// return(0, 32)
|
||||
// PUSH1 0x20 PUSH0 RETURN
|
||||
mstore(0, hex"60205ff3")
|
||||
return(0, 4)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
interface DoesItReturnZero {
|
||||
function foo() external pure returns (uint256);
|
||||
}
|
||||
|
||||
contract Test {
|
||||
ShortReturn immutable shortReturn = new ShortReturn();
|
||||
function bytecode() external view returns(bytes memory) {
|
||||
return address(shortReturn).code;
|
||||
}
|
||||
function isPush0Supported() external view returns (bool) {
|
||||
assert(DoesItReturnZero(address(shortReturn)).foo() == 0);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// compileViaYul: also
|
||||
// EVMVersion: >=shanghai
|
||||
// ----
|
||||
// bytecode() -> 0x20, 4, 0x60205ff300000000000000000000000000000000000000000000000000000000
|
||||
// isPush0Supported() -> true
|
||||
@@ -0,0 +1,11 @@
|
||||
contract C {
|
||||
function zero() external returns (uint) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
// ====
|
||||
// compileViaYul: also
|
||||
// EVMVersion: >=shanghai
|
||||
// ----
|
||||
// zero() -> 0
|
||||
@@ -7,6 +7,6 @@ contract test {
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// EVMVersion: >byzantium
|
||||
// EVMVersion: >=shanghai
|
||||
// ----
|
||||
// Warning 5574: (21-27154): Contract code size is 27192 bytes and exceeds 24576 bytes (a limit introduced in Spurious Dragon). This contract may not be deployable on Mainnet. Consider enabling the optimizer (with a low "runs" value!), turning off revert strings, or using libraries.
|
||||
// Warning 5574: (21-27154): Contract code size is 27186 bytes and exceeds 24576 bytes (a limit introduced in Spurious Dragon). This contract may not be deployable on Mainnet. Consider enabling the optimizer (with a low "runs" value!), turning off revert strings, or using libraries.
|
||||
|
||||
@@ -7,6 +7,6 @@ contract test {
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// EVMVersion: >byzantium
|
||||
// EVMVersion: >=shanghai
|
||||
// ----
|
||||
// Warning 5574: (21-27154): Contract code size is 27209 bytes and exceeds 24576 bytes (a limit introduced in Spurious Dragon). This contract may not be deployable on Mainnet. Consider enabling the optimizer (with a low "runs" value!), turning off revert strings, or using libraries.
|
||||
// Warning 5574: (21-27154): Contract code size is 27205 bytes and exceeds 24576 bytes (a limit introduced in Spurious Dragon). This contract may not be deployable on Mainnet. Consider enabling the optimizer (with a low "runs" value!), turning off revert strings, or using libraries.
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
// Ok
|
||||
contract push0 {}
|
||||
|
||||
contract A {
|
||||
// Ok, warning about shadowing
|
||||
function push0() external {}
|
||||
}
|
||||
|
||||
contract C {
|
||||
function f() external {
|
||||
assembly {
|
||||
// Not okay
|
||||
push0()
|
||||
}
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// Warning 2519: (77-105): This declaration shadows an existing declaration.
|
||||
// DeclarationError 4619: (205-210): Function "push0" not found.
|
||||
Reference in New Issue
Block a user