From 28f10f4783bd4365654191740069a7112be03d92 Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 28 Apr 2017 13:09:48 +0200 Subject: Remove error label / invalid jump label. --- libsolidity/codegen/CompilerUtils.cpp | 19 ------------------- 1 file changed, 19 deletions(-) (limited to 'libsolidity/codegen/CompilerUtils.cpp') diff --git a/libsolidity/codegen/CompilerUtils.cpp b/libsolidity/codegen/CompilerUtils.cpp index dc0b340c..4ae9a09a 100644 --- a/libsolidity/codegen/CompilerUtils.cpp +++ b/libsolidity/codegen/CompilerUtils.cpp @@ -299,25 +299,6 @@ void CompilerUtils::zeroInitialiseMemoryArray(ArrayType const& _type) m_context << Instruction::SWAP1 << Instruction::POP; } -void CompilerUtils::memoryCopyPrecompile() -{ - // Stack here: size target source - - m_context.appendInlineAssembly(R"( - { - let words := div(add(len, 31), 32) - let cost := add(15, mul(3, words)) - jumpi(invalidJumpLabel, iszero(call(cost, $identityContractAddress, 0, src, len, dst, len))) - } - )", - { "len", "dst", "src" }, - map { - { "$identityContractAddress", toString(identityContractAddress) } - } - ); - m_context << Instruction::POP << Instruction::POP << Instruction::POP; -} - void CompilerUtils::memoryCopy32() { // Stack here: size target source -- cgit v1.2.3 From 0c8beac35785a3c688c48c7f607832c30509c907 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Wed, 10 May 2017 08:48:00 +0100 Subject: Rename the SHA3 assembly instruction to KECCAK256 --- libsolidity/codegen/CompilerUtils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'libsolidity/codegen/CompilerUtils.cpp') diff --git a/libsolidity/codegen/CompilerUtils.cpp b/libsolidity/codegen/CompilerUtils.cpp index 4ae9a09a..3baaaddf 100644 --- a/libsolidity/codegen/CompilerUtils.cpp +++ b/libsolidity/codegen/CompilerUtils.cpp @@ -934,7 +934,7 @@ unsigned CompilerUtils::sizeOnStack(vector> const& _varia void CompilerUtils::computeHashStatic() { storeInMemory(0); - m_context << u256(32) << u256(0) << Instruction::SHA3; + m_context << u256(32) << u256(0) << Instruction::KECCAK256; } void CompilerUtils::storeStringData(bytesConstRef _data) -- cgit v1.2.3 From 83ea7793d99ddc1f1c5bd897c3a2b5e457d2ce29 Mon Sep 17 00:00:00 2001 From: chriseth Date: Thu, 22 Jun 2017 17:55:06 +0200 Subject: Fix ABI encoding of empty string literal. --- libsolidity/codegen/CompilerUtils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'libsolidity/codegen/CompilerUtils.cpp') diff --git a/libsolidity/codegen/CompilerUtils.cpp b/libsolidity/codegen/CompilerUtils.cpp index 3baaaddf..bfe72961 100644 --- a/libsolidity/codegen/CompilerUtils.cpp +++ b/libsolidity/codegen/CompilerUtils.cpp @@ -128,7 +128,7 @@ void CompilerUtils::storeInMemoryDynamic(Type const& _type, bool _padToWordBound m_context << Instruction::DUP1; storeStringData(bytesConstRef(str->value())); if (_padToWordBoundaries) - m_context << u256(((str->value().size() + 31) / 32) * 32); + m_context << u256(max(32, ((str->value().size() + 31) / 32) * 32)); else m_context << u256(str->value().size()); m_context << Instruction::ADD; -- cgit v1.2.3 From 831ed083875b73faf91f9d3335e2599540366712 Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 24 May 2017 11:47:43 +0200 Subject: Change invalid opcode to revert for input validation. --- libsolidity/codegen/CompilerUtils.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'libsolidity/codegen/CompilerUtils.cpp') diff --git a/libsolidity/codegen/CompilerUtils.cpp b/libsolidity/codegen/CompilerUtils.cpp index bfe72961..7fed1975 100644 --- a/libsolidity/codegen/CompilerUtils.cpp +++ b/libsolidity/codegen/CompilerUtils.cpp @@ -392,7 +392,13 @@ void CompilerUtils::pushCombinedFunctionEntryLabel(Declaration const& _function) Instruction::OR; } -void CompilerUtils::convertType(Type const& _typeOnStack, Type const& _targetType, bool _cleanupNeeded, bool _chopSignBits) +void CompilerUtils::convertType( + Type const& _typeOnStack, + Type const& _targetType, + bool _cleanupNeeded, + bool _chopSignBits, + bool _asPartOfArgumentDecoding +) { // For a type extension, we need to remove all higher-order bits that we might have ignored in // previous operations. @@ -450,7 +456,10 @@ void CompilerUtils::convertType(Type const& _typeOnStack, Type const& _targetTyp EnumType const& enumType = dynamic_cast(_typeOnStack); solAssert(enumType.numberOfMembers() > 0, "empty enum should have caused a parser error."); m_context << u256(enumType.numberOfMembers() - 1) << Instruction::DUP2 << Instruction::GT; - m_context.appendConditionalInvalid(); + if (_asPartOfArgumentDecoding) + m_context.appendConditionalRevert(); + else + m_context.appendConditionalInvalid(); enumOverflowCheckPending = false; } break; @@ -985,7 +994,7 @@ unsigned CompilerUtils::loadFromMemoryHelper(Type const& _type, bool _fromCallda m_context << shiftFactor << Instruction::MUL; } if (_fromCalldata) - convertType(_type, _type, true); + convertType(_type, _type, true, false, true); return numBytes; } -- cgit v1.2.3 From 0aaa6d876d56e450ce907e5a8b1db6cfd220312f Mon Sep 17 00:00:00 2001 From: chriseth Date: Thu, 22 Jun 2017 18:25:22 +0200 Subject: Use for loop for assembly memcopy. --- libsolidity/codegen/CompilerUtils.cpp | 38 ++++++++++++++--------------------- 1 file changed, 15 insertions(+), 23 deletions(-) (limited to 'libsolidity/codegen/CompilerUtils.cpp') diff --git a/libsolidity/codegen/CompilerUtils.cpp b/libsolidity/codegen/CompilerUtils.cpp index 7fed1975..26eadf9d 100644 --- a/libsolidity/codegen/CompilerUtils.cpp +++ b/libsolidity/codegen/CompilerUtils.cpp @@ -305,15 +305,9 @@ void CompilerUtils::memoryCopy32() m_context.appendInlineAssembly(R"( { - jumpi(end, eq(len, 0)) - start: - mstore(dst, mload(src)) - jumpi(end, iszero(gt(len, 32))) - dst := add(dst, 32) - src := add(src, 32) - len := sub(len, 32) - jump(start) - end: + for { let i := 0 } lt(i, len) { i := add(i, 32) } { + mstore(add(dst, i), mload(add(src, i))) + } } )", { "len", "dst", "src" } @@ -327,21 +321,19 @@ void CompilerUtils::memoryCopy() m_context.appendInlineAssembly(R"( { - // copy 32 bytes at once - start32: - jumpi(end32, lt(len, 32)) - mstore(dst, mload(src)) - dst := add(dst, 32) - src := add(src, 32) - len := sub(len, 32) - jump(start32) - end32: + // copy 32 bytes at once + for {} iszero(lt(len, 32)) { + dst := add(dst, 32) + src := add(src, 32) + len := sub(len, 32) + } + { mstore(dst, mload(src)) } - // copy the remainder (0 < len < 32) - let mask := sub(exp(256, sub(32, len)), 1) - let srcpart := and(mload(src), not(mask)) - let dstpart := and(mload(dst), mask) - mstore(dst, or(srcpart, dstpart)) + // copy the remainder (0 < len < 32) + let mask := sub(exp(256, sub(32, len)), 1) + let srcpart := and(mload(src), not(mask)) + let dstpart := and(mload(dst), mask) + mstore(dst, or(srcpart, dstpart)) } )", { "len", "dst", "src" } -- cgit v1.2.3 From d94a12a34c4c3846a40002a4e3a79c2b1f0b6972 Mon Sep 17 00:00:00 2001 From: chriseth Date: Mon, 26 Jun 2017 09:19:11 +0200 Subject: Reformat. --- libsolidity/codegen/CompilerUtils.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'libsolidity/codegen/CompilerUtils.cpp') diff --git a/libsolidity/codegen/CompilerUtils.cpp b/libsolidity/codegen/CompilerUtils.cpp index 26eadf9d..4edec155 100644 --- a/libsolidity/codegen/CompilerUtils.cpp +++ b/libsolidity/codegen/CompilerUtils.cpp @@ -322,12 +322,15 @@ void CompilerUtils::memoryCopy() m_context.appendInlineAssembly(R"( { // copy 32 bytes at once - for {} iszero(lt(len, 32)) { - dst := add(dst, 32) - src := add(src, 32) - len := sub(len, 32) - } - { mstore(dst, mload(src)) } + for + {} + iszero(lt(len, 32)) + { + dst := add(dst, 32) + src := add(src, 32) + len := sub(len, 32) + } + { mstore(dst, mload(src)) } // copy the remainder (0 < len < 32) let mask := sub(exp(256, sub(32, len)), 1) -- cgit v1.2.3 From 71d866cd7ab8013f36ab5b01d849a74b9908aae0 Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 30 Jun 2017 14:09:35 +0200 Subject: Abort early if there is nothing to encode or decode. --- libsolidity/codegen/CompilerUtils.cpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'libsolidity/codegen/CompilerUtils.cpp') diff --git a/libsolidity/codegen/CompilerUtils.cpp b/libsolidity/codegen/CompilerUtils.cpp index 4edec155..a105036f 100644 --- a/libsolidity/codegen/CompilerUtils.cpp +++ b/libsolidity/codegen/CompilerUtils.cpp @@ -180,6 +180,9 @@ void CompilerUtils::encodeToMemory( t = t->mobileType()->interfaceType(_encodeAsLibraryTypes)->encodingType(); } + if (_givenTypes.empty()) + return; + // Stack during operation: // ... ... // The values dyn_head_i are added during the first loop and they point to the head part -- cgit v1.2.3 From 0494fa98c0c973bf9e8b3dfa4873e1b5744e7715 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Mon, 21 Nov 2016 12:23:43 +0000 Subject: Add shift helper to CompilerUtils --- libsolidity/codegen/CompilerUtils.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'libsolidity/codegen/CompilerUtils.cpp') diff --git a/libsolidity/codegen/CompilerUtils.cpp b/libsolidity/codegen/CompilerUtils.cpp index a105036f..1da21607 100644 --- a/libsolidity/codegen/CompilerUtils.cpp +++ b/libsolidity/codegen/CompilerUtils.cpp @@ -1007,6 +1007,16 @@ void CompilerUtils::cleanHigherOrderBits(IntegerType const& _typeOnStack) m_context << ((u256(1) << _typeOnStack.numBits()) - 1) << Instruction::AND; } +void CompilerUtils::leftShiftNumberOnStack(u256 _shiftFactor) +{ + m_context << _shiftFactor << Instruction::MUL; +} + +void CompilerUtils::rightShiftNumberOnStack(u256 _shiftFactor, bool _isSigned) +{ + m_context << _shiftFactor << Instruction::SWAP1 << (_isSigned ? Instruction::SDIV : Instruction::DIV); +} + unsigned CompilerUtils::prepareMemoryStore(Type const& _type, bool _padToWords) { unsigned numBytes = _type.calldataEncodedSize(_padToWords); -- cgit v1.2.3 From c7ae042114e50ceccd2c230abf80ac7b246d0a44 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Mon, 21 Nov 2016 12:23:51 +0000 Subject: Use shift helper --- libsolidity/codegen/CompilerUtils.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'libsolidity/codegen/CompilerUtils.cpp') diff --git a/libsolidity/codegen/CompilerUtils.cpp b/libsolidity/codegen/CompilerUtils.cpp index 1da21607..4d8f32ca 100644 --- a/libsolidity/codegen/CompilerUtils.cpp +++ b/libsolidity/codegen/CompilerUtils.cpp @@ -371,10 +371,10 @@ void CompilerUtils::combineExternalFunctionType(bool _leftAligned) m_context << u256(0xffffffffUL) << Instruction::AND << Instruction::SWAP1; if (!_leftAligned) m_context << ((u256(1) << 160) - 1) << Instruction::AND; - m_context << (u256(1) << 32) << Instruction::MUL; + leftShiftNumberOnStack(u256(1) << 32); m_context << Instruction::OR; if (_leftAligned) - m_context << (u256(1) << 64) << Instruction::MUL; + leftShiftNumberOnStack(u256(1) << 64); } void CompilerUtils::pushCombinedFunctionEntryLabel(Declaration const& _function) @@ -425,7 +425,7 @@ void CompilerUtils::convertType( // conversion from bytes to integer. no need to clean the high bit // only to shift right because of opposite alignment IntegerType const& targetIntegerType = dynamic_cast(_targetType); - m_context << (u256(1) << (256 - typeOnStack.numBytes() * 8)) << Instruction::SWAP1 << Instruction::DIV; + rightShiftNumberOnStack(u256(1) << (256 - typeOnStack.numBytes() * 8), false); if (targetIntegerType.numBits() < typeOnStack.numBytes() * 8) convertType(IntegerType(typeOnStack.numBytes() * 8), _targetType, _cleanupNeeded); } @@ -476,7 +476,7 @@ void CompilerUtils::convertType( if (auto typeOnStack = dynamic_cast(&_typeOnStack)) if (targetBytesType.numBytes() * 8 > typeOnStack->numBits()) cleanHigherOrderBits(*typeOnStack); - m_context << (u256(1) << (256 - targetBytesType.numBytes() * 8)) << Instruction::MUL; + leftShiftNumberOnStack(u256(1) << (256 - targetBytesType.numBytes() * 8)); } else if (targetTypeCategory == Type::Category::Enum) { @@ -987,9 +987,9 @@ unsigned CompilerUtils::loadFromMemoryHelper(Type const& _type, bool _fromCallda bool leftAligned = _type.category() == Type::Category::FixedBytes; // add leading or trailing zeros by dividing/multiplying depending on alignment u256 shiftFactor = u256(1) << ((32 - numBytes) * 8); - m_context << shiftFactor << Instruction::SWAP1 << Instruction::DIV; + rightShiftNumberOnStack(shiftFactor, false); if (leftAligned) - m_context << shiftFactor << Instruction::MUL; + leftShiftNumberOnStack(shiftFactor); } if (_fromCalldata) convertType(_type, _type, true, false, true); -- cgit v1.2.3 From 8fd1d4167d2d05363ecf6af7ada42da940199900 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Wed, 30 Nov 2016 10:03:26 +0000 Subject: Change shiftNumberOnStack to bits --- libsolidity/codegen/CompilerUtils.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'libsolidity/codegen/CompilerUtils.cpp') diff --git a/libsolidity/codegen/CompilerUtils.cpp b/libsolidity/codegen/CompilerUtils.cpp index 4d8f32ca..8ea86684 100644 --- a/libsolidity/codegen/CompilerUtils.cpp +++ b/libsolidity/codegen/CompilerUtils.cpp @@ -371,10 +371,10 @@ void CompilerUtils::combineExternalFunctionType(bool _leftAligned) m_context << u256(0xffffffffUL) << Instruction::AND << Instruction::SWAP1; if (!_leftAligned) m_context << ((u256(1) << 160) - 1) << Instruction::AND; - leftShiftNumberOnStack(u256(1) << 32); + leftShiftNumberOnStack(32); m_context << Instruction::OR; if (_leftAligned) - leftShiftNumberOnStack(u256(1) << 64); + leftShiftNumberOnStack(64); } void CompilerUtils::pushCombinedFunctionEntryLabel(Declaration const& _function) @@ -425,7 +425,7 @@ void CompilerUtils::convertType( // conversion from bytes to integer. no need to clean the high bit // only to shift right because of opposite alignment IntegerType const& targetIntegerType = dynamic_cast(_targetType); - rightShiftNumberOnStack(u256(1) << (256 - typeOnStack.numBytes() * 8), false); + rightShiftNumberOnStack(256 - typeOnStack.numBytes() * 8, false); if (targetIntegerType.numBits() < typeOnStack.numBytes() * 8) convertType(IntegerType(typeOnStack.numBytes() * 8), _targetType, _cleanupNeeded); } @@ -476,7 +476,7 @@ void CompilerUtils::convertType( if (auto typeOnStack = dynamic_cast(&_typeOnStack)) if (targetBytesType.numBytes() * 8 > typeOnStack->numBits()) cleanHigherOrderBits(*typeOnStack); - leftShiftNumberOnStack(u256(1) << (256 - targetBytesType.numBytes() * 8)); + leftShiftNumberOnStack(256 - targetBytesType.numBytes() * 8); } else if (targetTypeCategory == Type::Category::Enum) { @@ -986,7 +986,7 @@ unsigned CompilerUtils::loadFromMemoryHelper(Type const& _type, bool _fromCallda { bool leftAligned = _type.category() == Type::Category::FixedBytes; // add leading or trailing zeros by dividing/multiplying depending on alignment - u256 shiftFactor = u256(1) << ((32 - numBytes) * 8); + int shiftFactor = (32 - numBytes) * 8; rightShiftNumberOnStack(shiftFactor, false); if (leftAligned) leftShiftNumberOnStack(shiftFactor); @@ -1007,14 +1007,14 @@ void CompilerUtils::cleanHigherOrderBits(IntegerType const& _typeOnStack) m_context << ((u256(1) << _typeOnStack.numBits()) - 1) << Instruction::AND; } -void CompilerUtils::leftShiftNumberOnStack(u256 _shiftFactor) +void CompilerUtils::leftShiftNumberOnStack(unsigned _bits) { - m_context << _shiftFactor << Instruction::MUL; + m_context << (u256(1) << _bits) << Instruction::MUL; } -void CompilerUtils::rightShiftNumberOnStack(u256 _shiftFactor, bool _isSigned) +void CompilerUtils::rightShiftNumberOnStack(unsigned _bits, bool _isSigned) { - m_context << _shiftFactor << Instruction::SWAP1 << (_isSigned ? Instruction::SDIV : Instruction::DIV); + m_context << (u256(1) << _bits) << Instruction::SWAP1 << (_isSigned ? Instruction::SDIV : Instruction::DIV); } unsigned CompilerUtils::prepareMemoryStore(Type const& _type, bool _padToWords) -- cgit v1.2.3 From 68b0ac4fdd86b02d9a12c762b5cf57d5873da82a Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Sun, 12 Feb 2017 14:48:46 +0000 Subject: More uses of shift helper --- libsolidity/codegen/CompilerUtils.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'libsolidity/codegen/CompilerUtils.cpp') diff --git a/libsolidity/codegen/CompilerUtils.cpp b/libsolidity/codegen/CompilerUtils.cpp index 8ea86684..94be90ab 100644 --- a/libsolidity/codegen/CompilerUtils.cpp +++ b/libsolidity/codegen/CompilerUtils.cpp @@ -353,13 +353,16 @@ void CompilerUtils::splitExternalFunctionType(bool _leftAligned) // address (right aligned), function identifier (right aligned) if (_leftAligned) { - m_context << Instruction::DUP1 << (u256(1) << (64 + 32)) << Instruction::SWAP1 << Instruction::DIV; + m_context << Instruction::DUP1; + rightShiftNumberOnStack(64 + 32, false); //
- m_context << Instruction::SWAP1 << (u256(1) << 64) << Instruction::SWAP1 << Instruction::DIV; + m_context << Instruction::SWAP1; + rightShiftNumberOnStack(64, false); } else { - m_context << Instruction::DUP1 << (u256(1) << 32) << Instruction::SWAP1 << Instruction::DIV; + m_context << Instruction::DUP1; + rightShiftNumberOnStack(32, false); m_context << ((u256(1) << 160) - 1) << Instruction::AND << Instruction::SWAP1; } m_context << u256(0xffffffffUL) << Instruction::AND; @@ -383,11 +386,12 @@ void CompilerUtils::pushCombinedFunctionEntryLabel(Declaration const& _function) // If there is a runtime context, we have to merge both labels into the same // stack slot in case we store it in storage. if (CompilerContext* rtc = m_context.runtimeContext()) + { + leftShiftNumberOnStack(32); m_context << - (u256(1) << 32) << - Instruction::MUL << rtc->functionEntryLabel(_function).toSubAssemblyTag(m_context.runtimeSub()) << Instruction::OR; + } } void CompilerUtils::convertType( @@ -1029,7 +1033,7 @@ unsigned CompilerUtils::prepareMemoryStore(Type const& _type, bool _padToWords) convertType(_type, _type, true); if (numBytes != 32 && !leftAligned && !_padToWords) // shift the value accordingly before storing - m_context << (u256(1) << ((32 - numBytes) * 8)) << Instruction::MUL; + leftShiftNumberOnStack((32 - numBytes) * 8); } return numBytes; } -- cgit v1.2.3 From 677700d5ac1c5079aa20a6a9e5667c7e8596da27 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Sat, 1 Jul 2017 11:48:12 +0100 Subject: Add assert to shift helper --- libsolidity/codegen/CompilerUtils.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'libsolidity/codegen/CompilerUtils.cpp') diff --git a/libsolidity/codegen/CompilerUtils.cpp b/libsolidity/codegen/CompilerUtils.cpp index 94be90ab..7067ddd5 100644 --- a/libsolidity/codegen/CompilerUtils.cpp +++ b/libsolidity/codegen/CompilerUtils.cpp @@ -1013,11 +1013,13 @@ void CompilerUtils::cleanHigherOrderBits(IntegerType const& _typeOnStack) void CompilerUtils::leftShiftNumberOnStack(unsigned _bits) { + solAssert(_bits < 256, ""); m_context << (u256(1) << _bits) << Instruction::MUL; } void CompilerUtils::rightShiftNumberOnStack(unsigned _bits, bool _isSigned) { + solAssert(_bits < 256, ""); m_context << (u256(1) << _bits) << Instruction::SWAP1 << (_isSigned ? Instruction::SDIV : Instruction::DIV); } -- cgit v1.2.3