-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Properly force-clean for shortening bytesXX conversions. #3868
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -684,19 +684,17 @@ void CompilerUtils::convertType( | |
| // clear for conversion to longer bytes | ||
| solAssert(targetTypeCategory == Type::Category::FixedBytes, "Invalid type conversion requested."); | ||
| FixedBytesType const& targetType = dynamic_cast<FixedBytesType const&>(_targetType); | ||
| if (targetType.numBytes() > typeOnStack.numBytes() || _cleanupNeeded) | ||
| if (typeOnStack.numBytes() == 0 || targetType.numBytes() == 0) | ||
| m_context << Instruction::POP << u256(0); | ||
| else if (targetType.numBytes() > typeOnStack.numBytes() || _cleanupNeeded) | ||
| { | ||
| if (typeOnStack.numBytes() == 0) | ||
| m_context << Instruction::POP << u256(0); | ||
| else | ||
| { | ||
| m_context << ((u256(1) << (256 - typeOnStack.numBytes() * 8)) - 1); | ||
| m_context << Instruction::NOT << Instruction::AND; | ||
| } | ||
| int bytes = min(typeOnStack.numBytes(), targetType.numBytes()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, what case can
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems to be an oversight since there's an assert forcing it to be unsigned: https://github.com/ethereum/solidity/blob/develop/libsolidity/ast/Types.cpp#L1146
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this is a very old line in the code. I guess the idea was that it is easier to subtract bytes or something. |
||
| m_context << ((u256(1) << (256 - bytes * 8)) - 1); | ||
| m_context << Instruction::NOT << Instruction::AND; | ||
| } | ||
| } | ||
| } | ||
| break; | ||
| } | ||
| case Type::Category::Enum: | ||
| solAssert(_targetType == _typeOnStack || targetTypeCategory == Type::Category::Integer, ""); | ||
| if (enumOverflowCheckPending) | ||
|
|
@@ -798,8 +796,9 @@ void CompilerUtils::convertType( | |
| bytesConstRef data(value); | ||
| if (targetTypeCategory == Type::Category::FixedBytes) | ||
| { | ||
| int const numBytes = dynamic_cast<FixedBytesType const&>(_targetType).numBytes(); | ||
| solAssert(data.size() <= 32, ""); | ||
| m_context << h256::Arith(h256(data, h256::AlignLeft)); | ||
| m_context << (h256::Arith(h256(data, h256::AlignLeft)) & (~(u256(-1) >> (8 * numBytes)))); | ||
| } | ||
| else if (targetTypeCategory == Type::Category::Array) | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8807,6 +8807,24 @@ BOOST_AUTO_TEST_CASE(cleanup_bytes_types) | |
| ABI_CHECK(callContractFunction("f(bytes2,uint16)", string("abc"), u256(0x040102)), encodeArgs(0)); | ||
| } | ||
|
|
||
| BOOST_AUTO_TEST_CASE(cleanup_bytes_types_shortening) | ||
| { | ||
| char const* sourceCode = R"( | ||
| contract C { | ||
| function f() pure returns (bytes32 r) { | ||
| bytes4 x = 0xffffffff; | ||
| bytes2 y = bytes2(x); | ||
| assembly { r := y } | ||
| // At this point, r and y both store four bytes, but | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually wonder why shouldn't we properly apply the cleanup before the assembly block?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can actually check how the optimizer copes with cleanup done all the time. |
||
| // y is properly cleaned before the equality check | ||
| require(y == bytes2(0xffff)); | ||
| } | ||
| } | ||
| )"; | ||
| compileAndRun(sourceCode, 0, "C"); | ||
| ABI_CHECK(callContractFunction("f()"), encodeArgs("\xff\xff\xff\xff")); | ||
| } | ||
|
|
||
| BOOST_AUTO_TEST_CASE(skip_dynamic_types) | ||
| { | ||
| // The EVM cannot provide access to dynamically-sized return values, so we have to skip them. | ||
|
|
@@ -11322,13 +11340,18 @@ BOOST_AUTO_TEST_CASE(abi_encode) | |
| y[0] = "e"; | ||
| require(y[0] == "e"); | ||
| } | ||
| function f4() returns (bytes) { | ||
| bytes4 x = "abcd"; | ||
| return abi.encode(bytes2(x)); | ||
| } | ||
| } | ||
| )"; | ||
| compileAndRun(sourceCode, 0, "C"); | ||
| ABI_CHECK(callContractFunction("f0()"), encodeArgs(0x20, 0)); | ||
| ABI_CHECK(callContractFunction("f1()"), encodeArgs(0x20, 0x40, 1, 2)); | ||
| ABI_CHECK(callContractFunction("f2()"), encodeArgs(0x20, 0xa0, 1, 0x60, 2, 3, "abc")); | ||
| ABI_CHECK(callContractFunction("f3()"), encodeArgs(0x20, 0xa0, 1, 0x60, 2, 3, "abc")); | ||
| ABI_CHECK(callContractFunction("f4()"), encodeArgs(0x20, 0x20, "ab")); | ||
| } | ||
|
|
||
| BOOST_AUTO_TEST_CASE(abi_encode_v2) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should have given an example.