mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #6248 from ethereum/shiftopt-fix-overflow
Fixes u256 overflow in logical shift optimization rule and adds tests.
This commit is contained in:
@@ -238,6 +238,36 @@ BOOST_AUTO_TEST_CASE(cse_associativity2)
|
||||
checkCSE(input, {Instruction::DUP2, Instruction::DUP2, Instruction::ADD, u256(5), Instruction::ADD});
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(cse_double_shift_right_overflow)
|
||||
{
|
||||
if (dev::test::Options::get().evmVersion().hasBitwiseShifting())
|
||||
{
|
||||
AssemblyItems input{
|
||||
Instruction::CALLVALUE,
|
||||
u256(2),
|
||||
Instruction::SHR,
|
||||
u256(-1),
|
||||
Instruction::SHR
|
||||
};
|
||||
checkCSE(input, {u256(0)});
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(cse_double_shift_left_overflow)
|
||||
{
|
||||
if (dev::test::Options::get().evmVersion().hasBitwiseShifting())
|
||||
{
|
||||
AssemblyItems input{
|
||||
Instruction::DUP1,
|
||||
u256(2),
|
||||
Instruction::SHL,
|
||||
u256(-1),
|
||||
Instruction::SHL
|
||||
};
|
||||
checkCSE(input, {u256(0)});
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(cse_storage)
|
||||
{
|
||||
AssemblyItems input{
|
||||
|
||||
@@ -15009,11 +15009,21 @@ BOOST_AUTO_TEST_CASE(bitwise_shifting_constantinople_combined)
|
||||
c := shl(0xd0, shl(0x40, a))
|
||||
}
|
||||
}
|
||||
function shl_combined_overflow(uint a) public returns (uint c) {
|
||||
assembly {
|
||||
c := shl(0x01, shl(not(0x00), a))
|
||||
}
|
||||
}
|
||||
function shr_combined_large(uint a) public returns (uint c) {
|
||||
assembly {
|
||||
c := shr(0xd0, shr(0x40, a))
|
||||
}
|
||||
}
|
||||
function shr_combined_overflow(uint a) public returns (uint c) {
|
||||
assembly {
|
||||
c := shr(0x01, shr(not(0x00), a))
|
||||
}
|
||||
}
|
||||
function sar_combined_large(uint a) public returns (uint c) {
|
||||
assembly {
|
||||
c := sar(0xd0, sar(0x40, a))
|
||||
@@ -15052,8 +15062,10 @@ BOOST_AUTO_TEST_CASE(bitwise_shifting_constantinople_combined)
|
||||
BOOST_CHECK(callContractFunction("shl_combined_large(uint256)", u256(0)) == encodeArgs(u256(0)));
|
||||
BOOST_CHECK(callContractFunction("shl_combined_large(uint256)", u256("0xffff")) == encodeArgs(u256(0)));
|
||||
BOOST_CHECK(callContractFunction("shl_combined_large(uint256)", u256("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")) == encodeArgs(u256(0)));
|
||||
BOOST_CHECK(callContractFunction("shl_combined_overflow(uint256)", u256(2)) == encodeArgs(u256(0)));
|
||||
BOOST_CHECK(callContractFunction("shr_combined_large(uint256)", u256(0)) == encodeArgs(u256(0)));
|
||||
BOOST_CHECK(callContractFunction("shr_combined_large(uint256)", u256("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")) == encodeArgs(u256(0)));
|
||||
BOOST_CHECK(callContractFunction("shr_combined_overflow(uint256)", u256(2)) == encodeArgs(u256(0)));
|
||||
BOOST_CHECK(callContractFunction("sar_combined_large(uint256)", u256(0)) == encodeArgs(u256(0)));
|
||||
BOOST_CHECK(callContractFunction("sar_combined_large(uint256)", u256("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")) == encodeArgs(u256(0)));
|
||||
BOOST_CHECK(callContractFunction("sar_combined_large(uint256)", u256("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")) == encodeArgs(u256("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")));
|
||||
|
||||
Reference in New Issue
Block a user