2015-02-25 19:27:55 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2015-02-25 19:27:55 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2015-02-25 19:27:55 +00:00
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is distributed in the hope that it will be useful,
|
2015-02-25 19:27:55 +00:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2016-11-18 23:13:20 +00:00
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
2015-02-25 19:27:55 +00:00
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2015-02-25 19:27:55 +00:00
|
|
|
/**
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2015
|
|
|
|
* Code generation utils that handle arrays.
|
|
|
|
*/
|
|
|
|
|
2015-10-20 22:21:52 +00:00
|
|
|
#include <libsolidity/codegen/ArrayUtils.h>
|
2018-12-17 16:06:11 +00:00
|
|
|
|
|
|
|
#include <libsolidity/ast/Types.h>
|
2019-04-15 13:33:39 +00:00
|
|
|
#include <libsolidity/ast/TypeProvider.h>
|
2015-10-20 22:21:52 +00:00
|
|
|
#include <libsolidity/codegen/CompilerContext.h>
|
|
|
|
#include <libsolidity/codegen/CompilerUtils.h>
|
|
|
|
#include <libsolidity/codegen/LValue.h>
|
2015-02-25 19:27:55 +00:00
|
|
|
|
2020-10-12 14:01:45 +00:00
|
|
|
#include <libsolutil/FunctionSelector.h>
|
|
|
|
#include <libsolutil/Whiskers.h>
|
|
|
|
|
2018-12-17 16:06:11 +00:00
|
|
|
#include <libevmasm/Instruction.h>
|
|
|
|
#include <liblangutil/Exceptions.h>
|
|
|
|
|
2015-02-25 19:27:55 +00:00
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::evmasm;
|
|
|
|
using namespace solidity::frontend;
|
|
|
|
using namespace solidity::langutil;
|
2015-02-25 19:27:55 +00:00
|
|
|
|
|
|
|
void ArrayUtils::copyArrayToStorage(ArrayType const& _targetType, ArrayType const& _sourceType) const
|
|
|
|
{
|
2015-03-11 17:09:35 +00:00
|
|
|
// this copies source to target and also clears target if it was larger
|
|
|
|
// need to leave "target_ref target_byte_off" on the stack at the end
|
|
|
|
|
2015-07-14 09:58:16 +00:00
|
|
|
// stack layout: [source_ref] [source length] target_ref (top)
|
2015-06-17 10:01:39 +00:00
|
|
|
solAssert(_targetType.location() == DataLocation::Storage, "");
|
2015-02-27 21:52:02 +00:00
|
|
|
|
2021-03-22 16:12:05 +00:00
|
|
|
Type const* uint256 = TypeProvider::uint256();
|
2022-01-28 17:57:05 +00:00
|
|
|
Type const* targetBaseType = _targetType.isByteArrayOrString() ? uint256 : _targetType.baseType();
|
|
|
|
Type const* sourceBaseType = _sourceType.isByteArrayOrString() ? uint256 : _sourceType.baseType();
|
2015-02-25 19:27:55 +00:00
|
|
|
|
2015-03-05 17:22:17 +00:00
|
|
|
// TODO unroll loop for small sizes
|
2015-02-25 19:27:55 +00:00
|
|
|
|
2015-06-17 10:01:39 +00:00
|
|
|
bool sourceIsStorage = _sourceType.location() == DataLocation::Storage;
|
2015-06-22 10:50:09 +00:00
|
|
|
bool fromCalldata = _sourceType.location() == DataLocation::CallData;
|
2015-03-16 16:52:19 +00:00
|
|
|
bool directCopy = sourceIsStorage && sourceBaseType->isValueType() && *sourceBaseType == *targetBaseType;
|
2015-08-31 16:44:29 +00:00
|
|
|
bool haveByteOffsetSource = !directCopy && sourceIsStorage && sourceBaseType->storageBytes() <= 16;
|
|
|
|
bool haveByteOffsetTarget = !directCopy && targetBaseType->storageBytes() <= 16;
|
2020-06-02 13:42:46 +00:00
|
|
|
unsigned byteOffsetSize = (haveByteOffsetSource ? 1u : 0u) + (haveByteOffsetTarget ? 1u : 0u);
|
2015-03-11 17:09:35 +00:00
|
|
|
|
2015-07-14 09:58:16 +00:00
|
|
|
// stack: source_ref [source_length] target_ref
|
2015-03-05 17:22:17 +00:00
|
|
|
// store target_ref
|
2015-08-31 16:44:29 +00:00
|
|
|
for (unsigned i = _sourceType.sizeOnStack(); i > 0; --i)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << swapInstruction(i);
|
2015-07-14 09:58:16 +00:00
|
|
|
// stack: target_ref source_ref [source_length]
|
2015-03-11 17:09:35 +00:00
|
|
|
// stack: target_ref source_ref [source_length]
|
|
|
|
// retrieve source length
|
2015-06-17 10:01:39 +00:00
|
|
|
if (_sourceType.location() != DataLocation::CallData || !_sourceType.isDynamicallySized())
|
2015-03-05 17:22:17 +00:00
|
|
|
retrieveLength(_sourceType); // otherwise, length is already there
|
2015-06-22 10:50:09 +00:00
|
|
|
if (_sourceType.location() == DataLocation::Memory && _sourceType.isDynamicallySized())
|
|
|
|
{
|
|
|
|
// increment source pointer to point to data
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP1 << u256(0x20);
|
|
|
|
m_context << Instruction::ADD << Instruction::SWAP1;
|
2015-06-22 10:50:09 +00:00
|
|
|
}
|
|
|
|
|
2015-03-05 17:22:17 +00:00
|
|
|
// stack: target_ref source_ref source_length
|
2021-03-22 16:12:05 +00:00
|
|
|
Type const* targetType = &_targetType;
|
|
|
|
Type const* sourceType = &_sourceType;
|
2017-01-20 13:19:25 +00:00
|
|
|
m_context.callLowLevelFunction(
|
|
|
|
"$copyArrayToStorage_" + sourceType->identifier() + "_to_" + targetType->identifier(),
|
|
|
|
3,
|
|
|
|
1,
|
|
|
|
[=](CompilerContext& _context)
|
2015-09-25 15:13:29 +00:00
|
|
|
{
|
2017-01-20 13:19:25 +00:00
|
|
|
ArrayUtils utils(_context);
|
|
|
|
ArrayType const& _sourceType = dynamic_cast<ArrayType const&>(*sourceType);
|
|
|
|
ArrayType const& _targetType = dynamic_cast<ArrayType const&>(*targetType);
|
|
|
|
// stack: target_ref source_ref source_length
|
|
|
|
_context << Instruction::DUP3;
|
|
|
|
// stack: target_ref source_ref source_length target_ref
|
|
|
|
utils.retrieveLength(_targetType);
|
|
|
|
// stack: target_ref source_ref source_length target_ref target_length
|
|
|
|
if (_targetType.isDynamicallySized())
|
|
|
|
// store new target length
|
2022-01-28 17:57:05 +00:00
|
|
|
if (!_targetType.isByteArrayOrString())
|
2017-01-20 13:19:25 +00:00
|
|
|
// Otherwise, length will be stored below.
|
|
|
|
_context << Instruction::DUP3 << Instruction::DUP3 << Instruction::SSTORE;
|
|
|
|
if (sourceBaseType->category() == Type::Category::Mapping)
|
|
|
|
{
|
|
|
|
solAssert(targetBaseType->category() == Type::Category::Mapping, "");
|
|
|
|
solAssert(_sourceType.location() == DataLocation::Storage, "");
|
|
|
|
// nothing to copy
|
|
|
|
_context
|
|
|
|
<< Instruction::POP << Instruction::POP
|
|
|
|
<< Instruction::POP << Instruction::POP;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// stack: target_ref source_ref source_length target_ref target_length
|
|
|
|
// compute hashes (data positions)
|
|
|
|
_context << Instruction::SWAP1;
|
|
|
|
if (_targetType.isDynamicallySized())
|
|
|
|
CompilerUtils(_context).computeHashStatic();
|
|
|
|
// stack: target_ref source_ref source_length target_length target_data_pos
|
|
|
|
_context << Instruction::SWAP1;
|
|
|
|
utils.convertLengthToSize(_targetType);
|
|
|
|
_context << Instruction::DUP2 << Instruction::ADD;
|
|
|
|
// stack: target_ref source_ref source_length target_data_pos target_data_end
|
|
|
|
_context << Instruction::SWAP3;
|
|
|
|
// stack: target_ref target_data_end source_length target_data_pos source_ref
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem copyLoopEndWithoutByteOffset = _context.newTag();
|
2017-01-20 13:19:25 +00:00
|
|
|
|
|
|
|
// special case for short byte arrays: Store them together with their length.
|
2022-01-28 17:57:05 +00:00
|
|
|
if (_targetType.isByteArrayOrString())
|
2017-01-20 13:19:25 +00:00
|
|
|
{
|
2020-10-15 09:44:53 +00:00
|
|
|
// stack: target_ref target_data_end source_length target_data_pos source_ref
|
|
|
|
_context << Instruction::DUP3;
|
|
|
|
evmasm::AssemblyItem nonEmptyByteArray = _context.appendConditionalJump();
|
|
|
|
// Empty source, just zero out the main slot.
|
|
|
|
_context << u256(0) << Instruction::DUP6 << Instruction::SSTORE;
|
|
|
|
_context.appendJumpTo(copyLoopEndWithoutByteOffset);
|
|
|
|
|
|
|
|
_context << nonEmptyByteArray;
|
|
|
|
// Non-empty source.
|
2017-01-20 13:19:25 +00:00
|
|
|
// stack: target_ref target_data_end source_length target_data_pos source_ref
|
|
|
|
_context << Instruction::DUP3 << u256(31) << Instruction::LT;
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem longByteArray = _context.appendConditionalJump();
|
2017-01-20 13:19:25 +00:00
|
|
|
// store the short byte array
|
2022-01-28 17:57:05 +00:00
|
|
|
solAssert(_sourceType.isByteArrayOrString(), "");
|
2017-01-20 13:19:25 +00:00
|
|
|
if (_sourceType.location() == DataLocation::Storage)
|
|
|
|
{
|
|
|
|
// just copy the slot, it contains length and data
|
|
|
|
_context << Instruction::DUP1 << Instruction::SLOAD;
|
|
|
|
_context << Instruction::DUP6 << Instruction::SSTORE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_context << Instruction::DUP1;
|
|
|
|
CompilerUtils(_context).loadFromMemoryDynamic(*sourceBaseType, fromCalldata, true, false);
|
|
|
|
// stack: target_ref target_data_end source_length target_data_pos source_ref value
|
|
|
|
// clear the lower-order byte - which will hold the length
|
|
|
|
_context << u256(0xff) << Instruction::NOT << Instruction::AND;
|
|
|
|
// fetch the length and shift it left by one
|
|
|
|
_context << Instruction::DUP4 << Instruction::DUP1 << Instruction::ADD;
|
|
|
|
// combine value and length and store them
|
|
|
|
_context << Instruction::OR << Instruction::DUP6 << Instruction::SSTORE;
|
|
|
|
}
|
|
|
|
// end of special case, jump right into cleaning target data area
|
|
|
|
_context.appendJumpTo(copyLoopEndWithoutByteOffset);
|
|
|
|
_context << longByteArray;
|
|
|
|
// Store length (2*length+1)
|
|
|
|
_context << Instruction::DUP3 << Instruction::DUP1 << Instruction::ADD;
|
|
|
|
_context << u256(1) << Instruction::ADD;
|
|
|
|
_context << Instruction::DUP6 << Instruction::SSTORE;
|
|
|
|
}
|
2015-09-25 15:13:29 +00:00
|
|
|
|
2017-01-20 13:19:25 +00:00
|
|
|
// skip copying if source length is zero
|
|
|
|
_context << Instruction::DUP3 << Instruction::ISZERO;
|
|
|
|
_context.appendConditionalJumpTo(copyLoopEndWithoutByteOffset);
|
|
|
|
|
|
|
|
if (_sourceType.location() == DataLocation::Storage && _sourceType.isDynamicallySized())
|
|
|
|
CompilerUtils(_context).computeHashStatic();
|
|
|
|
// stack: target_ref target_data_end source_length target_data_pos source_data_pos
|
|
|
|
_context << Instruction::SWAP2;
|
|
|
|
utils.convertLengthToSize(_sourceType);
|
|
|
|
_context << Instruction::DUP3 << Instruction::ADD;
|
|
|
|
// stack: target_ref target_data_end source_data_pos target_data_pos source_data_end
|
|
|
|
if (haveByteOffsetTarget)
|
|
|
|
_context << u256(0);
|
2015-03-16 16:52:19 +00:00
|
|
|
if (haveByteOffsetSource)
|
2017-01-20 13:19:25 +00:00
|
|
|
_context << u256(0);
|
|
|
|
// stack: target_ref target_data_end source_data_pos target_data_pos source_data_end [target_byte_offset] [source_byte_offset]
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem copyLoopStart = _context.newTag();
|
2017-01-20 13:19:25 +00:00
|
|
|
_context << copyLoopStart;
|
|
|
|
// check for loop condition
|
|
|
|
_context
|
|
|
|
<< dupInstruction(3 + byteOffsetSize) << dupInstruction(2 + byteOffsetSize)
|
|
|
|
<< Instruction::GT << Instruction::ISZERO;
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem copyLoopEnd = _context.appendConditionalJump();
|
2017-01-20 13:19:25 +00:00
|
|
|
// stack: target_ref target_data_end source_data_pos target_data_pos source_data_end [target_byte_offset] [source_byte_offset]
|
|
|
|
// copy
|
|
|
|
if (sourceBaseType->category() == Type::Category::Array)
|
|
|
|
{
|
|
|
|
solAssert(byteOffsetSize == 0, "Byte offset for array as base type.");
|
|
|
|
auto const& sourceBaseArrayType = dynamic_cast<ArrayType const&>(*sourceBaseType);
|
2020-05-19 11:12:07 +00:00
|
|
|
|
|
|
|
solUnimplementedAssert(
|
|
|
|
_sourceType.location() != DataLocation::CallData ||
|
|
|
|
!_sourceType.isDynamicallyEncoded() ||
|
|
|
|
!sourceBaseArrayType.isDynamicallySized(),
|
|
|
|
"Copying nested calldata dynamic arrays to storage is not implemented in the old code generator."
|
|
|
|
);
|
2017-01-20 13:19:25 +00:00
|
|
|
_context << Instruction::DUP3;
|
|
|
|
if (sourceBaseArrayType.location() == DataLocation::Memory)
|
|
|
|
_context << Instruction::MLOAD;
|
|
|
|
_context << Instruction::DUP3;
|
|
|
|
utils.copyArrayToStorage(dynamic_cast<ArrayType const&>(*targetBaseType), sourceBaseArrayType);
|
|
|
|
_context << Instruction::POP;
|
|
|
|
}
|
|
|
|
else if (directCopy)
|
|
|
|
{
|
|
|
|
solAssert(byteOffsetSize == 0, "Byte offset for direct copy.");
|
|
|
|
_context
|
|
|
|
<< Instruction::DUP3 << Instruction::SLOAD
|
|
|
|
<< Instruction::DUP3 << Instruction::SSTORE;
|
|
|
|
}
|
2015-03-16 16:52:19 +00:00
|
|
|
else
|
2017-01-20 13:19:25 +00:00
|
|
|
{
|
|
|
|
// Note that we have to copy each element on its own in case conversion is involved.
|
|
|
|
// We might copy too much if there is padding at the last element, but this way end
|
|
|
|
// checking is easier.
|
|
|
|
// stack: target_ref target_data_end source_data_pos target_data_pos source_data_end [target_byte_offset] [source_byte_offset]
|
|
|
|
_context << dupInstruction(3 + byteOffsetSize);
|
|
|
|
if (_sourceType.location() == DataLocation::Storage)
|
|
|
|
{
|
|
|
|
if (haveByteOffsetSource)
|
|
|
|
_context << Instruction::DUP2;
|
|
|
|
else
|
|
|
|
_context << u256(0);
|
|
|
|
StorageItem(_context, *sourceBaseType).retrieveValue(SourceLocation(), true);
|
|
|
|
}
|
|
|
|
else if (sourceBaseType->isValueType())
|
|
|
|
CompilerUtils(_context).loadFromMemoryDynamic(*sourceBaseType, fromCalldata, true, false);
|
|
|
|
else
|
|
|
|
solUnimplemented("Copying of type " + _sourceType.toString(false) + " to storage not yet supported.");
|
|
|
|
// stack: target_ref target_data_end source_data_pos target_data_pos source_data_end [target_byte_offset] [source_byte_offset] <source_value>...
|
2020-07-01 07:46:06 +00:00
|
|
|
assertThrow(
|
2017-01-20 13:19:25 +00:00
|
|
|
2 + byteOffsetSize + sourceBaseType->sizeOnStack() <= 16,
|
2020-07-01 07:46:06 +00:00
|
|
|
StackTooDeepError,
|
2017-01-20 13:19:25 +00:00
|
|
|
"Stack too deep, try removing local variables."
|
|
|
|
);
|
|
|
|
// fetch target storage reference
|
|
|
|
_context << dupInstruction(2 + byteOffsetSize + sourceBaseType->sizeOnStack());
|
|
|
|
if (haveByteOffsetTarget)
|
|
|
|
_context << dupInstruction(1 + byteOffsetSize + sourceBaseType->sizeOnStack());
|
|
|
|
else
|
|
|
|
_context << u256(0);
|
|
|
|
StorageItem(_context, *targetBaseType).storeValue(*sourceBaseType, SourceLocation(), true);
|
|
|
|
}
|
|
|
|
// stack: target_ref target_data_end source_data_pos target_data_pos source_data_end [target_byte_offset] [source_byte_offset]
|
|
|
|
// increment source
|
|
|
|
if (haveByteOffsetSource)
|
|
|
|
utils.incrementByteOffset(sourceBaseType->storageBytes(), 1, haveByteOffsetTarget ? 5 : 4);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_context << swapInstruction(2 + byteOffsetSize);
|
|
|
|
if (sourceIsStorage)
|
|
|
|
_context << sourceBaseType->storageSize();
|
|
|
|
else if (_sourceType.location() == DataLocation::Memory)
|
|
|
|
_context << sourceBaseType->memoryHeadSize();
|
|
|
|
else
|
2019-07-09 14:10:38 +00:00
|
|
|
_context << sourceBaseType->calldataHeadSize();
|
2017-01-20 13:19:25 +00:00
|
|
|
_context
|
|
|
|
<< Instruction::ADD
|
|
|
|
<< swapInstruction(2 + byteOffsetSize);
|
|
|
|
}
|
|
|
|
// increment target
|
|
|
|
if (haveByteOffsetTarget)
|
|
|
|
utils.incrementByteOffset(targetBaseType->storageBytes(), byteOffsetSize, byteOffsetSize + 2);
|
|
|
|
else
|
|
|
|
_context
|
|
|
|
<< swapInstruction(1 + byteOffsetSize)
|
|
|
|
<< targetBaseType->storageSize()
|
|
|
|
<< Instruction::ADD
|
|
|
|
<< swapInstruction(1 + byteOffsetSize);
|
|
|
|
_context.appendJumpTo(copyLoopStart);
|
|
|
|
_context << copyLoopEnd;
|
|
|
|
if (haveByteOffsetTarget)
|
|
|
|
{
|
|
|
|
// clear elements that might be left over in the current slot in target
|
|
|
|
// stack: target_ref target_data_end source_data_pos target_data_pos source_data_end target_byte_offset [source_byte_offset]
|
|
|
|
_context << dupInstruction(byteOffsetSize) << Instruction::ISZERO;
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem copyCleanupLoopEnd = _context.appendConditionalJump();
|
2017-01-20 13:19:25 +00:00
|
|
|
_context << dupInstruction(2 + byteOffsetSize) << dupInstruction(1 + byteOffsetSize);
|
|
|
|
StorageItem(_context, *targetBaseType).setToZero(SourceLocation(), true);
|
|
|
|
utils.incrementByteOffset(targetBaseType->storageBytes(), byteOffsetSize, byteOffsetSize + 2);
|
|
|
|
_context.appendJumpTo(copyLoopEnd);
|
|
|
|
|
|
|
|
_context << copyCleanupLoopEnd;
|
|
|
|
_context << Instruction::POP; // might pop the source, but then target is popped next
|
|
|
|
}
|
|
|
|
if (haveByteOffsetSource)
|
|
|
|
_context << Instruction::POP;
|
|
|
|
_context << copyLoopEndWithoutByteOffset;
|
|
|
|
|
|
|
|
// zero-out leftovers in target
|
|
|
|
// stack: target_ref target_data_end source_data_pos target_data_pos_updated source_data_end
|
|
|
|
_context << Instruction::POP << Instruction::SWAP1 << Instruction::POP;
|
|
|
|
// stack: target_ref target_data_end target_data_pos_updated
|
2020-10-01 17:07:56 +00:00
|
|
|
if (targetBaseType->storageBytes() < 32)
|
|
|
|
utils.clearStorageLoop(TypeProvider::uint256());
|
|
|
|
else
|
|
|
|
utils.clearStorageLoop(targetBaseType);
|
2017-01-20 13:19:25 +00:00
|
|
|
_context << Instruction::POP;
|
2015-03-11 17:09:35 +00:00
|
|
|
}
|
2017-01-20 13:19:25 +00:00
|
|
|
);
|
2015-02-25 19:27:55 +00:00
|
|
|
}
|
|
|
|
|
2015-09-25 15:13:29 +00:00
|
|
|
void ArrayUtils::copyArrayToMemory(ArrayType const& _sourceType, bool _padToWordBoundaries) const
|
2015-06-23 12:55:33 +00:00
|
|
|
{
|
2016-11-14 20:41:58 +00:00
|
|
|
solUnimplementedAssert(
|
2015-11-24 13:54:37 +00:00
|
|
|
!_sourceType.baseType()->isDynamicallySized(),
|
2015-06-25 15:51:01 +00:00
|
|
|
"Nested dynamic arrays not implemented here."
|
2015-06-23 12:55:33 +00:00
|
|
|
);
|
2015-06-25 15:51:01 +00:00
|
|
|
CompilerUtils utils(m_context);
|
2015-06-23 12:55:33 +00:00
|
|
|
|
|
|
|
if (_sourceType.location() == DataLocation::CallData)
|
|
|
|
{
|
|
|
|
if (!_sourceType.isDynamicallySized())
|
2015-08-31 16:44:29 +00:00
|
|
|
m_context << _sourceType.length();
|
2022-01-28 17:57:05 +00:00
|
|
|
if (!_sourceType.isByteArrayOrString())
|
2019-07-09 14:10:38 +00:00
|
|
|
convertLengthToSize(_sourceType);
|
2018-05-07 14:23:39 +00:00
|
|
|
|
|
|
|
string routine = "calldatacopy(target, source, len)\n";
|
|
|
|
if (_padToWordBoundaries)
|
|
|
|
routine += R"(
|
|
|
|
// Set padding suffix to zero
|
|
|
|
mstore(add(target, len), 0)
|
|
|
|
len := and(add(len, 0x1f), not(0x1f))
|
|
|
|
)";
|
|
|
|
routine += "target := add(target, len)\n";
|
|
|
|
m_context.appendInlineAssembly("{" + routine + "}", {"target", "source", "len"});
|
|
|
|
m_context << Instruction::POP << Instruction::POP;
|
2015-06-23 12:55:33 +00:00
|
|
|
}
|
|
|
|
else if (_sourceType.location() == DataLocation::Memory)
|
|
|
|
{
|
|
|
|
retrieveLength(_sourceType);
|
2015-06-25 15:51:01 +00:00
|
|
|
// stack: target source length
|
2015-08-31 16:44:29 +00:00
|
|
|
if (!_sourceType.baseType()->isValueType())
|
2015-06-25 15:51:01 +00:00
|
|
|
{
|
|
|
|
// copy using a loop
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << u256(0) << Instruction::SWAP3;
|
2015-06-25 15:51:01 +00:00
|
|
|
// stack: counter source length target
|
|
|
|
auto repeat = m_context.newTag();
|
|
|
|
m_context << repeat;
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP2 << Instruction::DUP5;
|
|
|
|
m_context << Instruction::LT << Instruction::ISZERO;
|
2015-06-25 15:51:01 +00:00
|
|
|
auto loopEnd = m_context.appendConditionalJump();
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP3 << Instruction::DUP5;
|
2015-06-25 15:51:01 +00:00
|
|
|
accessIndex(_sourceType, false);
|
2015-08-31 16:44:29 +00:00
|
|
|
MemoryItem(m_context, *_sourceType.baseType(), true).retrieveValue(SourceLocation(), true);
|
2019-04-15 13:33:39 +00:00
|
|
|
if (auto baseArray = dynamic_cast<ArrayType const*>(_sourceType.baseType()))
|
2015-06-25 15:51:01 +00:00
|
|
|
copyArrayToMemory(*baseArray, _padToWordBoundaries);
|
|
|
|
else
|
2015-08-31 16:44:29 +00:00
|
|
|
utils.storeInMemoryDynamic(*_sourceType.baseType());
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP3 << u256(1) << Instruction::ADD;
|
|
|
|
m_context << Instruction::SWAP3;
|
2015-06-25 15:51:01 +00:00
|
|
|
m_context.appendJumpTo(repeat);
|
|
|
|
m_context << loopEnd;
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP3;
|
2015-06-25 15:51:01 +00:00
|
|
|
utils.popStackSlots(3);
|
|
|
|
// stack: updated_target_pos
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// memcpy using the built-in contract
|
2015-06-23 12:55:33 +00:00
|
|
|
if (_sourceType.isDynamicallySized())
|
|
|
|
{
|
|
|
|
// change pointer to data part
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP1 << u256(32) << Instruction::ADD;
|
|
|
|
m_context << Instruction::SWAP1;
|
2015-06-23 12:55:33 +00:00
|
|
|
}
|
2022-01-28 17:57:05 +00:00
|
|
|
if (!_sourceType.isByteArrayOrString())
|
2019-07-09 14:10:38 +00:00
|
|
|
convertLengthToSize(_sourceType);
|
2015-06-23 12:55:33 +00:00
|
|
|
// stack: <target> <source> <size>
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP1 << Instruction::DUP4 << Instruction::DUP4;
|
2016-12-11 16:51:17 +00:00
|
|
|
// We can resort to copying full 32 bytes only if
|
|
|
|
// - the length is known to be a multiple of 32 or
|
|
|
|
// - we will pad to full 32 bytes later anyway.
|
2022-01-28 17:57:05 +00:00
|
|
|
if (!_sourceType.isByteArrayOrString() || _padToWordBoundaries)
|
2016-12-11 16:51:17 +00:00
|
|
|
utils.memoryCopy32();
|
|
|
|
else
|
|
|
|
utils.memoryCopy();
|
2015-06-23 12:55:33 +00:00
|
|
|
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP1 << Instruction::POP;
|
2015-06-23 12:55:33 +00:00
|
|
|
// stack: <target> <size>
|
|
|
|
|
2022-01-28 17:57:05 +00:00
|
|
|
bool paddingNeeded = _padToWordBoundaries && _sourceType.isByteArrayOrString();
|
2019-07-09 14:10:38 +00:00
|
|
|
|
2015-06-23 12:55:33 +00:00
|
|
|
if (paddingNeeded)
|
|
|
|
{
|
|
|
|
// stack: <target> <size>
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP1 << Instruction::DUP2 << Instruction::ADD;
|
2015-06-23 12:55:33 +00:00
|
|
|
// stack: <length> <target + size>
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP1 << u256(31) << Instruction::AND;
|
2015-06-23 12:55:33 +00:00
|
|
|
// stack: <target + size> <remainder = size % 32>
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem skip = m_context.newTag();
|
2015-06-23 12:55:33 +00:00
|
|
|
if (_sourceType.isDynamicallySized())
|
|
|
|
{
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP1 << Instruction::ISZERO;
|
2015-06-23 12:55:33 +00:00
|
|
|
m_context.appendConditionalJumpTo(skip);
|
|
|
|
}
|
|
|
|
// round off, load from there.
|
|
|
|
// stack <target + size> <remainder = size % 32>
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP1 << Instruction::DUP3;
|
|
|
|
m_context << Instruction::SUB;
|
2015-06-23 12:55:33 +00:00
|
|
|
// stack: target+size remainder <target + size - remainder>
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP1 << Instruction::MLOAD;
|
2015-06-23 12:55:33 +00:00
|
|
|
// Now we AND it with ~(2**(8 * (32 - remainder)) - 1)
|
|
|
|
m_context << u256(1);
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP4 << u256(32) << Instruction::SUB;
|
2015-06-23 12:55:33 +00:00
|
|
|
// stack: ...<v> 1 <32 - remainder>
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << u256(0x100) << Instruction::EXP << Instruction::SUB;
|
|
|
|
m_context << Instruction::NOT << Instruction::AND;
|
2015-06-23 12:55:33 +00:00
|
|
|
// stack: target+size remainder target+size-remainder <v & ...>
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP2 << Instruction::MSTORE;
|
2015-06-23 12:55:33 +00:00
|
|
|
// stack: target+size remainder target+size-remainder
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << u256(32) << Instruction::ADD;
|
2015-06-23 12:55:33 +00:00
|
|
|
// stack: target+size remainder <new_padded_end>
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP2 << Instruction::POP;
|
2015-06-23 12:55:33 +00:00
|
|
|
|
|
|
|
if (_sourceType.isDynamicallySized())
|
|
|
|
m_context << skip.tag();
|
|
|
|
// stack <target + "size"> <remainder = size % 32>
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::POP;
|
2015-06-23 12:55:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
// stack: <target> <size>
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::ADD;
|
2015-06-23 12:55:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
solAssert(_sourceType.location() == DataLocation::Storage, "");
|
2015-08-31 16:44:29 +00:00
|
|
|
unsigned storageBytes = _sourceType.baseType()->storageBytes();
|
|
|
|
u256 storageSize = _sourceType.baseType()->storageSize();
|
2015-06-23 12:55:33 +00:00
|
|
|
solAssert(storageSize > 1 || (storageSize == 1 && storageBytes > 0), "");
|
|
|
|
|
|
|
|
retrieveLength(_sourceType);
|
|
|
|
// stack here: memory_offset storage_offset length
|
|
|
|
// jump to end if length is zero
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP1 << Instruction::ISZERO;
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem loopEnd = m_context.appendConditionalJump();
|
2015-09-25 15:13:29 +00:00
|
|
|
// Special case for tightly-stored byte arrays
|
2022-01-28 17:57:05 +00:00
|
|
|
if (_sourceType.isByteArrayOrString())
|
2015-09-25 15:13:29 +00:00
|
|
|
{
|
|
|
|
// stack here: memory_offset storage_offset length
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP1 << u256(31) << Instruction::LT;
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem longByteArray = m_context.appendConditionalJump();
|
2015-09-25 15:13:29 +00:00
|
|
|
// store the short byte array (discard lower-order byte)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << u256(0x100) << Instruction::DUP1;
|
|
|
|
m_context << Instruction::DUP4 << Instruction::SLOAD;
|
|
|
|
m_context << Instruction::DIV << Instruction::MUL;
|
|
|
|
m_context << Instruction::DUP4 << Instruction::MSTORE;
|
2015-09-25 15:13:29 +00:00
|
|
|
// stack here: memory_offset storage_offset length
|
|
|
|
// add 32 or length to memory offset
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP2;
|
2015-09-25 15:13:29 +00:00
|
|
|
if (_padToWordBoundaries)
|
|
|
|
m_context << u256(32);
|
|
|
|
else
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP3;
|
|
|
|
m_context << Instruction::ADD;
|
|
|
|
m_context << Instruction::SWAP2;
|
2015-09-25 15:13:29 +00:00
|
|
|
m_context.appendJumpTo(loopEnd);
|
|
|
|
m_context << longByteArray;
|
|
|
|
}
|
2019-07-09 14:10:38 +00:00
|
|
|
else
|
2015-06-23 12:55:33 +00:00
|
|
|
// convert length to memory size
|
2019-07-09 14:10:38 +00:00
|
|
|
m_context << _sourceType.baseType()->memoryHeadSize() << Instruction::MUL;
|
|
|
|
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP3 << Instruction::ADD << Instruction::SWAP2;
|
2015-06-23 12:55:33 +00:00
|
|
|
if (_sourceType.isDynamicallySized())
|
|
|
|
{
|
2017-05-10 07:48:00 +00:00
|
|
|
// actual array data is stored at KECCAK256(storage_offset)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP1;
|
2015-06-25 15:51:01 +00:00
|
|
|
utils.computeHashStatic();
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP1;
|
2015-06-23 12:55:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// stack here: memory_end_offset storage_data_offset memory_offset
|
2022-01-28 17:57:05 +00:00
|
|
|
bool haveByteOffset = !_sourceType.isByteArrayOrString() && storageBytes <= 16;
|
2015-06-23 12:55:33 +00:00
|
|
|
if (haveByteOffset)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << u256(0) << Instruction::SWAP1;
|
2015-06-23 12:55:33 +00:00
|
|
|
// stack here: memory_end_offset storage_data_offset [storage_byte_offset] memory_offset
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem loopStart = m_context.newTag();
|
2015-06-23 12:55:33 +00:00
|
|
|
m_context << loopStart;
|
|
|
|
// load and store
|
2022-01-28 17:57:05 +00:00
|
|
|
if (_sourceType.isByteArrayOrString())
|
2015-06-23 12:55:33 +00:00
|
|
|
{
|
|
|
|
// Packed both in storage and memory.
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP2 << Instruction::SLOAD;
|
|
|
|
m_context << Instruction::DUP2 << Instruction::MSTORE;
|
2015-06-23 12:55:33 +00:00
|
|
|
// increment storage_data_offset by 1
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP1 << u256(1) << Instruction::ADD;
|
2015-06-23 12:55:33 +00:00
|
|
|
// increment memory offset by 32
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP1 << u256(32) << Instruction::ADD;
|
2015-06-23 12:55:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// stack here: memory_end_offset storage_data_offset [storage_byte_offset] memory_offset
|
|
|
|
if (haveByteOffset)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP3 << Instruction::DUP3;
|
2015-06-23 12:55:33 +00:00
|
|
|
else
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP2 << u256(0);
|
2015-08-31 16:44:29 +00:00
|
|
|
StorageItem(m_context, *_sourceType.baseType()).retrieveValue(SourceLocation(), true);
|
2019-04-15 13:33:39 +00:00
|
|
|
if (auto baseArray = dynamic_cast<ArrayType const*>(_sourceType.baseType()))
|
2015-06-25 15:51:01 +00:00
|
|
|
copyArrayToMemory(*baseArray, _padToWordBoundaries);
|
|
|
|
else
|
2015-08-31 16:44:29 +00:00
|
|
|
utils.storeInMemoryDynamic(*_sourceType.baseType());
|
2015-06-23 12:55:33 +00:00
|
|
|
// increment storage_data_offset and byte offset
|
|
|
|
if (haveByteOffset)
|
|
|
|
incrementByteOffset(storageBytes, 2, 3);
|
|
|
|
else
|
|
|
|
{
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP1;
|
|
|
|
m_context << storageSize << Instruction::ADD;
|
|
|
|
m_context << Instruction::SWAP1;
|
2015-06-23 12:55:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// check for loop condition
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP1 << dupInstruction(haveByteOffset ? 5 : 4);
|
|
|
|
m_context << Instruction::GT;
|
2015-06-23 12:55:33 +00:00
|
|
|
m_context.appendConditionalJumpTo(loopStart);
|
|
|
|
// stack here: memory_end_offset storage_data_offset [storage_byte_offset] memory_offset
|
|
|
|
if (haveByteOffset)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP1 << Instruction::POP;
|
2022-01-28 17:57:05 +00:00
|
|
|
if (!_sourceType.isByteArrayOrString())
|
2019-07-09 14:10:38 +00:00
|
|
|
{
|
|
|
|
solAssert(_sourceType.calldataStride() % 32 == 0, "");
|
|
|
|
solAssert(_sourceType.memoryStride() % 32 == 0, "");
|
|
|
|
}
|
2022-01-28 17:57:05 +00:00
|
|
|
if (_padToWordBoundaries && _sourceType.isByteArrayOrString())
|
2015-06-23 12:55:33 +00:00
|
|
|
{
|
|
|
|
// memory_end_offset - start is the actual length (we want to compute the ceil of).
|
|
|
|
// memory_offset - start is its next multiple of 32, but it might be off by 32.
|
|
|
|
// so we compute: memory_end_offset += (memory_offset - memory_end_offest) & 31
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP3 << Instruction::SWAP1 << Instruction::SUB;
|
|
|
|
m_context << u256(31) << Instruction::AND;
|
|
|
|
m_context << Instruction::DUP3 << Instruction::ADD;
|
|
|
|
m_context << Instruction::SWAP2;
|
2015-06-23 12:55:33 +00:00
|
|
|
}
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << loopEnd << Instruction::POP << Instruction::POP;
|
2015-06-23 12:55:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-19 16:21:55 +00:00
|
|
|
void ArrayUtils::clearArray(ArrayType const& _typeIn) const
|
2015-02-25 19:27:55 +00:00
|
|
|
{
|
2021-03-22 16:12:05 +00:00
|
|
|
Type const* type = &_typeIn;
|
2017-01-19 16:21:55 +00:00
|
|
|
m_context.callLowLevelFunction(
|
|
|
|
"$clearArray_" + _typeIn.identifier(),
|
|
|
|
2,
|
|
|
|
0,
|
|
|
|
[type](CompilerContext& _context)
|
2015-02-25 19:27:55 +00:00
|
|
|
{
|
2017-01-19 16:21:55 +00:00
|
|
|
ArrayType const& _type = dynamic_cast<ArrayType const&>(*type);
|
|
|
|
unsigned stackHeightStart = _context.stackHeight();
|
|
|
|
solAssert(_type.location() == DataLocation::Storage, "");
|
|
|
|
if (_type.baseType()->storageBytes() < 32)
|
|
|
|
{
|
|
|
|
solAssert(_type.baseType()->isValueType(), "Invalid storage size for non-value type.");
|
|
|
|
solAssert(_type.baseType()->storageSize() <= 1, "Invalid storage size for type.");
|
|
|
|
}
|
|
|
|
if (_type.baseType()->isValueType())
|
|
|
|
solAssert(_type.baseType()->storageSize() <= 1, "Invalid size for value type.");
|
|
|
|
|
|
|
|
_context << Instruction::POP; // remove byte offset
|
|
|
|
if (_type.isDynamicallySized())
|
|
|
|
ArrayUtils(_context).clearDynamicArray(_type);
|
|
|
|
else if (_type.length() == 0 || _type.baseType()->category() == Type::Category::Mapping)
|
|
|
|
_context << Instruction::POP;
|
|
|
|
else if (_type.baseType()->isValueType() && _type.storageSize() <= 5)
|
|
|
|
{
|
|
|
|
// unroll loop for small arrays @todo choose a good value
|
|
|
|
// Note that we loop over storage slots here, not elements.
|
|
|
|
for (unsigned i = 1; i < _type.storageSize(); ++i)
|
|
|
|
_context
|
|
|
|
<< u256(0) << Instruction::DUP2 << Instruction::SSTORE
|
|
|
|
<< u256(1) << Instruction::ADD;
|
|
|
|
_context << u256(0) << Instruction::SWAP1 << Instruction::SSTORE;
|
|
|
|
}
|
|
|
|
else if (!_type.baseType()->isValueType() && _type.length() <= 4)
|
|
|
|
{
|
|
|
|
// unroll loop for small arrays @todo choose a good value
|
|
|
|
solAssert(_type.baseType()->storageBytes() >= 32, "Invalid storage size.");
|
|
|
|
for (unsigned i = 1; i < _type.length(); ++i)
|
|
|
|
{
|
|
|
|
_context << u256(0);
|
|
|
|
StorageItem(_context, *_type.baseType()).setToZero(SourceLocation(), false);
|
|
|
|
_context
|
|
|
|
<< Instruction::POP
|
|
|
|
<< u256(_type.baseType()->storageSize()) << Instruction::ADD;
|
|
|
|
}
|
|
|
|
_context << u256(0);
|
|
|
|
StorageItem(_context, *_type.baseType()).setToZero(SourceLocation(), true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_context << Instruction::DUP1 << _type.length();
|
|
|
|
ArrayUtils(_context).convertLengthToSize(_type);
|
|
|
|
_context << Instruction::ADD << Instruction::SWAP1;
|
|
|
|
if (_type.baseType()->storageBytes() < 32)
|
2019-04-15 16:10:43 +00:00
|
|
|
ArrayUtils(_context).clearStorageLoop(TypeProvider::uint256());
|
2017-01-19 16:21:55 +00:00
|
|
|
else
|
2017-01-24 11:06:11 +00:00
|
|
|
ArrayUtils(_context).clearStorageLoop(_type.baseType());
|
2017-01-19 16:21:55 +00:00
|
|
|
_context << Instruction::POP;
|
|
|
|
}
|
|
|
|
solAssert(_context.stackHeight() == stackHeightStart - 2, "");
|
2015-02-25 19:27:55 +00:00
|
|
|
}
|
2017-01-19 16:21:55 +00:00
|
|
|
);
|
2015-02-25 19:27:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArrayUtils::clearDynamicArray(ArrayType const& _type) const
|
|
|
|
{
|
2015-06-17 10:01:39 +00:00
|
|
|
solAssert(_type.location() == DataLocation::Storage, "");
|
2015-02-25 19:27:55 +00:00
|
|
|
solAssert(_type.isDynamicallySized(), "");
|
|
|
|
|
|
|
|
// fetch length
|
2015-09-25 15:13:29 +00:00
|
|
|
retrieveLength(_type);
|
2015-02-25 19:27:55 +00:00
|
|
|
// set length to zero
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << u256(0) << Instruction::DUP3 << Instruction::SSTORE;
|
2015-09-25 15:13:29 +00:00
|
|
|
// Special case: short byte arrays are stored togeher with their length
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem endTag = m_context.newTag();
|
2022-01-28 17:57:05 +00:00
|
|
|
if (_type.isByteArrayOrString())
|
2015-09-25 15:13:29 +00:00
|
|
|
{
|
|
|
|
// stack: ref old_length
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP1 << u256(31) << Instruction::LT;
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem longByteArray = m_context.appendConditionalJump();
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::POP;
|
2015-09-25 15:13:29 +00:00
|
|
|
m_context.appendJumpTo(endTag);
|
|
|
|
m_context.adjustStackOffset(1); // needed because of jump
|
|
|
|
m_context << longByteArray;
|
|
|
|
}
|
2015-02-25 19:27:55 +00:00
|
|
|
// stack: ref old_length
|
|
|
|
convertLengthToSize(_type);
|
|
|
|
// compute data positions
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP1;
|
2015-02-25 19:27:55 +00:00
|
|
|
CompilerUtils(m_context).computeHashStatic();
|
2015-03-11 17:09:35 +00:00
|
|
|
// stack: len data_pos
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP1 << Instruction::DUP2 << Instruction::ADD
|
|
|
|
<< Instruction::SWAP1;
|
2015-02-25 19:27:55 +00:00
|
|
|
// stack: data_pos_end data_pos
|
2019-02-14 09:50:13 +00:00
|
|
|
if (_type.storageStride() < 32)
|
2019-04-15 16:10:43 +00:00
|
|
|
clearStorageLoop(TypeProvider::uint256());
|
2015-02-25 19:27:55 +00:00
|
|
|
else
|
2017-01-24 11:06:11 +00:00
|
|
|
clearStorageLoop(_type.baseType());
|
2015-02-25 19:27:55 +00:00
|
|
|
// cleanup
|
2015-09-25 15:13:29 +00:00
|
|
|
m_context << endTag;
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::POP;
|
2015-02-25 19:27:55 +00:00
|
|
|
}
|
|
|
|
|
2017-01-19 16:21:55 +00:00
|
|
|
void ArrayUtils::resizeDynamicArray(ArrayType const& _typeIn) const
|
2015-02-25 19:27:55 +00:00
|
|
|
{
|
2021-03-22 16:12:05 +00:00
|
|
|
Type const* type = &_typeIn;
|
2017-01-19 16:21:55 +00:00
|
|
|
m_context.callLowLevelFunction(
|
|
|
|
"$resizeDynamicArray_" + _typeIn.identifier(),
|
|
|
|
2,
|
|
|
|
0,
|
|
|
|
[type](CompilerContext& _context)
|
|
|
|
{
|
|
|
|
ArrayType const& _type = dynamic_cast<ArrayType const&>(*type);
|
|
|
|
solAssert(_type.location() == DataLocation::Storage, "");
|
|
|
|
solAssert(_type.isDynamicallySized(), "");
|
2022-01-28 17:57:05 +00:00
|
|
|
if (!_type.isByteArrayOrString() && _type.baseType()->storageBytes() < 32)
|
2017-01-19 16:21:55 +00:00
|
|
|
solAssert(_type.baseType()->isValueType(), "Invalid storage size for non-value type.");
|
|
|
|
|
|
|
|
unsigned stackHeightStart = _context.stackHeight();
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem resizeEnd = _context.newTag();
|
2017-01-19 16:21:55 +00:00
|
|
|
|
|
|
|
// stack: ref new_length
|
|
|
|
// fetch old length
|
|
|
|
ArrayUtils(_context).retrieveLength(_type, 1);
|
|
|
|
// stack: ref new_length old_length
|
|
|
|
solAssert(_context.stackHeight() - stackHeightStart == 3 - 2, "2");
|
|
|
|
|
|
|
|
// Special case for short byte arrays, they are stored together with their length
|
2022-01-28 17:57:05 +00:00
|
|
|
if (_type.isByteArrayOrString())
|
2017-01-19 16:21:55 +00:00
|
|
|
{
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem regularPath = _context.newTag();
|
2017-01-19 16:21:55 +00:00
|
|
|
// We start by a large case-distinction about the old and new length of the byte array.
|
|
|
|
|
|
|
|
_context << Instruction::DUP3 << Instruction::SLOAD;
|
|
|
|
// stack: ref new_length current_length ref_value
|
|
|
|
|
|
|
|
solAssert(_context.stackHeight() - stackHeightStart == 4 - 2, "3");
|
|
|
|
_context << Instruction::DUP2 << u256(31) << Instruction::LT;
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem currentIsLong = _context.appendConditionalJump();
|
2017-01-19 16:21:55 +00:00
|
|
|
_context << Instruction::DUP3 << u256(31) << Instruction::LT;
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem newIsLong = _context.appendConditionalJump();
|
2017-01-19 16:21:55 +00:00
|
|
|
|
|
|
|
// Here: short -> short
|
|
|
|
|
|
|
|
// Compute 1 << (256 - 8 * new_size)
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem shortToShort = _context.newTag();
|
2017-01-19 16:21:55 +00:00
|
|
|
_context << shortToShort;
|
|
|
|
_context << Instruction::DUP3 << u256(8) << Instruction::MUL;
|
|
|
|
_context << u256(0x100) << Instruction::SUB;
|
|
|
|
_context << u256(2) << Instruction::EXP;
|
|
|
|
// Divide and multiply by that value, clearing bits.
|
|
|
|
_context << Instruction::DUP1 << Instruction::SWAP2;
|
|
|
|
_context << Instruction::DIV << Instruction::MUL;
|
|
|
|
// Insert 2*length.
|
|
|
|
_context << Instruction::DUP3 << Instruction::DUP1 << Instruction::ADD;
|
|
|
|
_context << Instruction::OR;
|
|
|
|
// Store.
|
|
|
|
_context << Instruction::DUP4 << Instruction::SSTORE;
|
|
|
|
solAssert(_context.stackHeight() - stackHeightStart == 3 - 2, "3");
|
|
|
|
_context.appendJumpTo(resizeEnd);
|
|
|
|
|
|
|
|
_context.adjustStackOffset(1); // we have to do that because of the jumps
|
|
|
|
// Here: short -> long
|
|
|
|
|
|
|
|
_context << newIsLong;
|
|
|
|
// stack: ref new_length current_length ref_value
|
|
|
|
solAssert(_context.stackHeight() - stackHeightStart == 4 - 2, "3");
|
|
|
|
// Zero out lower-order byte.
|
|
|
|
_context << u256(0xff) << Instruction::NOT << Instruction::AND;
|
|
|
|
// Store at data location.
|
|
|
|
_context << Instruction::DUP4;
|
|
|
|
CompilerUtils(_context).computeHashStatic();
|
|
|
|
_context << Instruction::SSTORE;
|
|
|
|
// stack: ref new_length current_length
|
|
|
|
// Store new length: Compule 2*length + 1 and store it.
|
|
|
|
_context << Instruction::DUP2 << Instruction::DUP1 << Instruction::ADD;
|
|
|
|
_context << u256(1) << Instruction::ADD;
|
|
|
|
// stack: ref new_length current_length 2*new_length+1
|
|
|
|
_context << Instruction::DUP4 << Instruction::SSTORE;
|
|
|
|
solAssert(_context.stackHeight() - stackHeightStart == 3 - 2, "3");
|
|
|
|
_context.appendJumpTo(resizeEnd);
|
|
|
|
|
|
|
|
_context.adjustStackOffset(1); // we have to do that because of the jumps
|
|
|
|
|
|
|
|
_context << currentIsLong;
|
|
|
|
_context << Instruction::DUP3 << u256(31) << Instruction::LT;
|
|
|
|
_context.appendConditionalJumpTo(regularPath);
|
|
|
|
|
|
|
|
// Here: long -> short
|
|
|
|
// Read the first word of the data and store it on the stack. Clear the data location and
|
|
|
|
// then jump to the short -> short case.
|
|
|
|
|
|
|
|
// stack: ref new_length current_length ref_value
|
|
|
|
solAssert(_context.stackHeight() - stackHeightStart == 4 - 2, "3");
|
|
|
|
_context << Instruction::POP << Instruction::DUP3;
|
|
|
|
CompilerUtils(_context).computeHashStatic();
|
|
|
|
_context << Instruction::DUP1 << Instruction::SLOAD << Instruction::SWAP1;
|
|
|
|
// stack: ref new_length current_length first_word data_location
|
|
|
|
_context << Instruction::DUP3;
|
|
|
|
ArrayUtils(_context).convertLengthToSize(_type);
|
|
|
|
_context << Instruction::DUP2 << Instruction::ADD << Instruction::SWAP1;
|
|
|
|
// stack: ref new_length current_length first_word data_location_end data_location
|
2019-04-15 16:10:43 +00:00
|
|
|
ArrayUtils(_context).clearStorageLoop(TypeProvider::uint256());
|
2017-01-19 16:21:55 +00:00
|
|
|
_context << Instruction::POP;
|
|
|
|
// stack: ref new_length current_length first_word
|
|
|
|
solAssert(_context.stackHeight() - stackHeightStart == 4 - 2, "3");
|
|
|
|
_context.appendJumpTo(shortToShort);
|
|
|
|
|
|
|
|
_context << regularPath;
|
|
|
|
// stack: ref new_length current_length ref_value
|
|
|
|
_context << Instruction::POP;
|
|
|
|
}
|
2015-09-25 15:13:29 +00:00
|
|
|
|
2017-05-10 07:48:00 +00:00
|
|
|
// Change of length for a regular array (i.e. length at location, data at KECCAK256(location)).
|
2017-01-19 16:21:55 +00:00
|
|
|
// stack: ref new_length old_length
|
|
|
|
// store new length
|
|
|
|
_context << Instruction::DUP2;
|
2022-01-28 17:57:05 +00:00
|
|
|
if (_type.isByteArrayOrString())
|
2017-01-19 16:21:55 +00:00
|
|
|
// For a "long" byte array, store length as 2*length+1
|
|
|
|
_context << Instruction::DUP1 << Instruction::ADD << u256(1) << Instruction::ADD;
|
2018-03-09 13:06:54 +00:00
|
|
|
_context << Instruction::DUP4 << Instruction::SSTORE;
|
2017-01-19 16:21:55 +00:00
|
|
|
// skip if size is not reduced
|
|
|
|
_context << Instruction::DUP2 << Instruction::DUP2
|
2018-03-09 13:06:54 +00:00
|
|
|
<< Instruction::GT << Instruction::ISZERO;
|
2017-01-19 16:21:55 +00:00
|
|
|
_context.appendConditionalJumpTo(resizeEnd);
|
|
|
|
|
|
|
|
// size reduced, clear the end of the array
|
|
|
|
// stack: ref new_length old_length
|
|
|
|
ArrayUtils(_context).convertLengthToSize(_type);
|
|
|
|
_context << Instruction::DUP2;
|
|
|
|
ArrayUtils(_context).convertLengthToSize(_type);
|
|
|
|
// stack: ref new_length old_size new_size
|
|
|
|
// compute data positions
|
|
|
|
_context << Instruction::DUP4;
|
|
|
|
CompilerUtils(_context).computeHashStatic();
|
|
|
|
// stack: ref new_length old_size new_size data_pos
|
|
|
|
_context << Instruction::SWAP2 << Instruction::DUP3 << Instruction::ADD;
|
|
|
|
// stack: ref new_length data_pos new_size delete_end
|
|
|
|
_context << Instruction::SWAP2 << Instruction::ADD;
|
|
|
|
// stack: ref new_length delete_end delete_start
|
2019-02-14 09:50:13 +00:00
|
|
|
if (_type.storageStride() < 32)
|
2019-04-15 16:10:43 +00:00
|
|
|
ArrayUtils(_context).clearStorageLoop(TypeProvider::uint256());
|
2017-01-19 16:21:55 +00:00
|
|
|
else
|
2017-01-24 11:06:11 +00:00
|
|
|
ArrayUtils(_context).clearStorageLoop(_type.baseType());
|
2015-02-25 19:27:55 +00:00
|
|
|
|
2017-01-19 16:21:55 +00:00
|
|
|
_context << resizeEnd;
|
|
|
|
// cleanup
|
|
|
|
_context << Instruction::POP << Instruction::POP << Instruction::POP;
|
|
|
|
solAssert(_context.stackHeight() == stackHeightStart - 2, "");
|
|
|
|
}
|
|
|
|
);
|
2015-02-25 19:27:55 +00:00
|
|
|
}
|
|
|
|
|
2018-03-08 14:38:14 +00:00
|
|
|
void ArrayUtils::incrementDynamicArraySize(ArrayType const& _type) const
|
|
|
|
{
|
|
|
|
solAssert(_type.location() == DataLocation::Storage, "");
|
|
|
|
solAssert(_type.isDynamicallySized(), "");
|
2022-01-28 17:57:05 +00:00
|
|
|
if (!_type.isByteArrayOrString() && _type.baseType()->storageBytes() < 32)
|
2018-03-08 14:38:14 +00:00
|
|
|
solAssert(_type.baseType()->isValueType(), "Invalid storage size for non-value type.");
|
|
|
|
|
2022-01-28 17:57:05 +00:00
|
|
|
if (_type.isByteArrayOrString())
|
2018-03-08 14:38:14 +00:00
|
|
|
{
|
|
|
|
// We almost always just add 2 (length of byte arrays is shifted left by one)
|
|
|
|
// except for the case where we transition from a short byte array
|
|
|
|
// to a long byte array, there we have to copy.
|
|
|
|
// This happens if the length is exactly 31, which means that the
|
|
|
|
// lowest-order byte (we actually use a mask with fewer bits) must
|
|
|
|
// be (31*2+0) = 62
|
|
|
|
|
2020-11-09 14:43:57 +00:00
|
|
|
m_context << Instruction::DUP1 << Instruction::SLOAD << Instruction::DUP1;
|
|
|
|
m_context.callYulFunction(m_context.utilFunctions().extractByteArrayLengthFunction(), 1, 1);
|
2018-03-08 14:38:14 +00:00
|
|
|
m_context.appendInlineAssembly(R"({
|
|
|
|
// We have to copy if length is exactly 31, because that marks
|
|
|
|
// the transition between in-place and out-of-place storage.
|
2020-11-09 14:43:57 +00:00
|
|
|
switch length
|
|
|
|
case 31
|
2018-03-08 14:38:14 +00:00
|
|
|
{
|
|
|
|
mstore(0, ref)
|
|
|
|
let data_area := keccak256(0, 0x20)
|
|
|
|
sstore(data_area, and(data, not(0xff)))
|
2020-11-09 14:43:57 +00:00
|
|
|
// Set old length in new format (31 * 2 + 1)
|
|
|
|
data := 63
|
2018-03-08 14:38:14 +00:00
|
|
|
}
|
2020-11-09 14:43:57 +00:00
|
|
|
sstore(ref, add(data, 2))
|
|
|
|
// return new length in ref
|
|
|
|
ref := add(length, 1)
|
|
|
|
})", {"ref", "data", "length"});
|
|
|
|
m_context << Instruction::POP << Instruction::POP;
|
2018-03-08 14:38:14 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
m_context.appendInlineAssembly(R"({
|
|
|
|
let new_length := add(sload(ref), 1)
|
|
|
|
sstore(ref, new_length)
|
|
|
|
ref := new_length
|
|
|
|
})", {"ref"});
|
|
|
|
}
|
|
|
|
|
2018-03-09 16:46:24 +00:00
|
|
|
void ArrayUtils::popStorageArrayElement(ArrayType const& _type) const
|
|
|
|
{
|
|
|
|
solAssert(_type.location() == DataLocation::Storage, "");
|
|
|
|
solAssert(_type.isDynamicallySized(), "");
|
2022-01-28 17:57:05 +00:00
|
|
|
if (!_type.isByteArrayOrString() && _type.baseType()->storageBytes() < 32)
|
2018-03-09 16:46:24 +00:00
|
|
|
solAssert(_type.baseType()->isValueType(), "Invalid storage size for non-value type.");
|
|
|
|
|
2022-01-28 17:57:05 +00:00
|
|
|
if (_type.isByteArrayOrString())
|
2018-03-09 16:46:24 +00:00
|
|
|
{
|
2020-11-09 14:43:57 +00:00
|
|
|
m_context << Instruction::DUP1 << Instruction::SLOAD << Instruction::DUP1;
|
|
|
|
m_context.callYulFunction(m_context.utilFunctions().extractByteArrayLengthFunction(), 1, 1);
|
2020-10-12 14:01:45 +00:00
|
|
|
util::Whiskers code(R"({
|
2020-11-09 14:43:57 +00:00
|
|
|
if iszero(length) {
|
|
|
|
mstore(0, <panicSelector>)
|
|
|
|
mstore(4, <emptyArrayPop>)
|
|
|
|
revert(0, 0x24)
|
|
|
|
}
|
|
|
|
switch gt(length, 31)
|
2018-03-16 16:06:38 +00:00
|
|
|
case 0 {
|
|
|
|
// short byte array
|
2018-05-16 15:18:30 +00:00
|
|
|
// Zero-out the suffix including the least significant byte.
|
2018-04-04 16:21:06 +00:00
|
|
|
let mask := sub(exp(0x100, sub(33, length)), 1)
|
|
|
|
length := sub(length, 1)
|
|
|
|
slot_value := or(and(not(mask), slot_value), mul(length, 2))
|
2018-03-16 16:06:38 +00:00
|
|
|
}
|
|
|
|
case 1 {
|
|
|
|
// long byte array
|
2018-05-29 09:25:13 +00:00
|
|
|
mstore(0, ref)
|
2018-04-04 16:21:06 +00:00
|
|
|
let slot := keccak256(0, 0x20)
|
2018-03-16 16:06:38 +00:00
|
|
|
switch length
|
|
|
|
case 32
|
|
|
|
{
|
|
|
|
let data := sload(slot)
|
|
|
|
sstore(slot, 0)
|
|
|
|
data := and(data, not(0xff))
|
2020-11-09 14:43:57 +00:00
|
|
|
slot_value := or(data, 62)
|
2018-03-16 16:06:38 +00:00
|
|
|
}
|
|
|
|
default
|
|
|
|
{
|
2018-05-29 09:25:13 +00:00
|
|
|
let offset_inside_slot := and(sub(length, 1), 0x1f)
|
|
|
|
slot := add(slot, div(sub(length, 1), 32))
|
2018-03-16 16:06:38 +00:00
|
|
|
let data := sload(slot)
|
|
|
|
|
|
|
|
// Zero-out the suffix of the byte array by masking it.
|
|
|
|
// ((1<<(8 * (32 - offset))) - 1)
|
2018-05-29 09:25:13 +00:00
|
|
|
let mask := sub(exp(0x100, sub(32, offset_inside_slot)), 1)
|
2018-03-16 16:06:38 +00:00
|
|
|
data := and(not(mask), data)
|
|
|
|
sstore(slot, data)
|
|
|
|
|
|
|
|
// Reduce the length by 1
|
|
|
|
slot_value := sub(slot_value, 2)
|
|
|
|
}
|
|
|
|
}
|
2020-11-09 14:43:57 +00:00
|
|
|
sstore(ref, slot_value)
|
2020-10-12 14:01:45 +00:00
|
|
|
})");
|
|
|
|
code("panicSelector", util::selectorFromSignature("Panic(uint256)").str());
|
|
|
|
code("emptyArrayPop", to_string(unsigned(util::PanicCode::EmptyArrayPop)));
|
2020-11-09 14:43:57 +00:00
|
|
|
m_context.appendInlineAssembly(code.render(), {"ref", "slot_value", "length"});
|
|
|
|
m_context << Instruction::POP << Instruction::POP << Instruction::POP;
|
2018-03-09 16:46:24 +00:00
|
|
|
}
|
|
|
|
else
|
2018-04-04 16:21:06 +00:00
|
|
|
{
|
2018-03-16 16:06:38 +00:00
|
|
|
// stack: ArrayReference
|
|
|
|
retrieveLength(_type);
|
|
|
|
// stack: ArrayReference oldLength
|
|
|
|
m_context << Instruction::DUP1;
|
|
|
|
// stack: ArrayReference oldLength oldLength
|
|
|
|
m_context << Instruction::ISZERO;
|
2020-10-12 14:01:45 +00:00
|
|
|
m_context.appendConditionalPanic(util::PanicCode::EmptyArrayPop);
|
2018-03-16 16:06:38 +00:00
|
|
|
|
2018-03-09 16:46:24 +00:00
|
|
|
// Stack: ArrayReference oldLength
|
|
|
|
m_context << u256(1) << Instruction::SWAP1 << Instruction::SUB;
|
|
|
|
// Stack ArrayReference newLength
|
2019-09-14 00:11:09 +00:00
|
|
|
|
|
|
|
if (_type.baseType()->category() != Type::Category::Mapping)
|
|
|
|
{
|
|
|
|
m_context << Instruction::DUP2 << Instruction::DUP2;
|
|
|
|
// Stack ArrayReference newLength ArrayReference newLength;
|
|
|
|
accessIndex(_type, false);
|
|
|
|
// Stack: ArrayReference newLength storage_slot byte_offset
|
|
|
|
StorageItem(m_context, *_type.baseType()).setToZero(SourceLocation(), true);
|
|
|
|
}
|
|
|
|
|
2018-03-09 16:46:24 +00:00
|
|
|
// Stack: ArrayReference newLength
|
2018-03-16 16:06:38 +00:00
|
|
|
m_context << Instruction::SWAP1 << Instruction::SSTORE;
|
2018-03-09 16:46:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-22 16:12:05 +00:00
|
|
|
void ArrayUtils::clearStorageLoop(Type const* _type) const
|
2015-02-25 19:27:55 +00:00
|
|
|
{
|
2020-10-01 17:07:56 +00:00
|
|
|
solAssert(_type->storageBytes() >= 32, "");
|
2017-01-20 13:19:25 +00:00
|
|
|
m_context.callLowLevelFunction(
|
2017-01-24 11:06:11 +00:00
|
|
|
"$clearStorageLoop_" + _type->identifier(),
|
2017-01-20 13:19:25 +00:00
|
|
|
2,
|
|
|
|
1,
|
2017-01-24 11:06:11 +00:00
|
|
|
[_type](CompilerContext& _context)
|
2017-01-20 13:19:25 +00:00
|
|
|
{
|
|
|
|
unsigned stackHeightStart = _context.stackHeight();
|
2017-01-24 11:06:11 +00:00
|
|
|
if (_type->category() == Type::Category::Mapping)
|
2017-01-20 13:19:25 +00:00
|
|
|
{
|
|
|
|
_context << Instruction::POP;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// stack: end_pos pos
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem loopStart = _context.appendJumpToNew();
|
2017-01-20 13:19:25 +00:00
|
|
|
_context << loopStart;
|
|
|
|
// check for loop condition
|
2019-02-13 15:56:46 +00:00
|
|
|
_context <<
|
|
|
|
Instruction::DUP1 <<
|
|
|
|
Instruction::DUP3 <<
|
|
|
|
Instruction::GT <<
|
|
|
|
Instruction::ISZERO;
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem zeroLoopEnd = _context.newTag();
|
2017-01-20 13:19:25 +00:00
|
|
|
_context.appendConditionalJumpTo(zeroLoopEnd);
|
|
|
|
// delete
|
|
|
|
_context << u256(0);
|
2017-01-24 11:06:11 +00:00
|
|
|
StorageItem(_context, *_type).setToZero(SourceLocation(), false);
|
2017-01-20 13:19:25 +00:00
|
|
|
_context << Instruction::POP;
|
|
|
|
// increment
|
2017-01-24 11:06:11 +00:00
|
|
|
_context << _type->storageSize() << Instruction::ADD;
|
2017-01-20 13:19:25 +00:00
|
|
|
_context.appendJumpTo(loopStart);
|
|
|
|
// cleanup
|
|
|
|
_context << zeroLoopEnd;
|
2020-07-06 16:14:51 +00:00
|
|
|
_context << Instruction::POP;
|
2015-05-28 12:43:46 +00:00
|
|
|
|
2017-01-20 13:19:25 +00:00
|
|
|
solAssert(_context.stackHeight() == stackHeightStart - 1, "");
|
|
|
|
}
|
|
|
|
);
|
2015-02-25 19:27:55 +00:00
|
|
|
}
|
|
|
|
|
2015-03-05 17:22:17 +00:00
|
|
|
void ArrayUtils::convertLengthToSize(ArrayType const& _arrayType, bool _pad) const
|
2015-02-25 19:27:55 +00:00
|
|
|
{
|
2015-06-17 10:01:39 +00:00
|
|
|
if (_arrayType.location() == DataLocation::Storage)
|
2015-03-05 17:22:17 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
if (_arrayType.baseType()->storageSize() <= 1)
|
2015-03-16 16:52:19 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
unsigned baseBytes = _arrayType.baseType()->storageBytes();
|
2015-03-16 16:52:19 +00:00
|
|
|
if (baseBytes == 0)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::POP << u256(1);
|
2015-03-16 16:52:19 +00:00
|
|
|
else if (baseBytes <= 16)
|
|
|
|
{
|
|
|
|
unsigned itemsPerSlot = 32 / baseBytes;
|
|
|
|
m_context
|
2016-04-04 11:41:35 +00:00
|
|
|
<< u256(itemsPerSlot - 1) << Instruction::ADD
|
|
|
|
<< u256(itemsPerSlot) << Instruction::SWAP1 << Instruction::DIV;
|
2015-03-16 16:52:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << _arrayType.baseType()->storageSize() << Instruction::MUL;
|
2015-03-05 17:22:17 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-01-28 17:57:05 +00:00
|
|
|
if (!_arrayType.isByteArrayOrString())
|
2015-06-25 15:51:01 +00:00
|
|
|
{
|
|
|
|
if (_arrayType.location() == DataLocation::Memory)
|
2019-07-09 14:10:38 +00:00
|
|
|
m_context << _arrayType.memoryStride();
|
2015-06-25 15:51:01 +00:00
|
|
|
else
|
2019-07-09 14:10:38 +00:00
|
|
|
m_context << _arrayType.calldataStride();
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::MUL;
|
2015-06-25 15:51:01 +00:00
|
|
|
}
|
2015-03-05 17:22:17 +00:00
|
|
|
else if (_pad)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << u256(31) << Instruction::ADD
|
|
|
|
<< u256(32) << Instruction::DUP1
|
|
|
|
<< Instruction::SWAP2 << Instruction::DIV << Instruction::MUL;
|
2015-03-05 17:22:17 +00:00
|
|
|
}
|
2015-02-25 19:27:55 +00:00
|
|
|
}
|
|
|
|
|
2015-09-25 15:13:29 +00:00
|
|
|
void ArrayUtils::retrieveLength(ArrayType const& _arrayType, unsigned _stackDepth) const
|
2015-02-27 21:52:02 +00:00
|
|
|
{
|
2015-03-03 16:55:28 +00:00
|
|
|
if (!_arrayType.isDynamicallySized())
|
2015-08-31 16:44:29 +00:00
|
|
|
m_context << _arrayType.length();
|
2015-03-03 16:55:28 +00:00
|
|
|
else
|
|
|
|
{
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << dupInstruction(1 + _stackDepth);
|
2015-06-05 09:07:50 +00:00
|
|
|
switch (_arrayType.location())
|
2015-03-03 16:55:28 +00:00
|
|
|
{
|
2015-06-17 10:01:39 +00:00
|
|
|
case DataLocation::CallData:
|
2015-03-03 16:55:28 +00:00
|
|
|
// length is stored on the stack
|
|
|
|
break;
|
2015-06-17 10:01:39 +00:00
|
|
|
case DataLocation::Memory:
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::MLOAD;
|
2015-03-03 16:55:28 +00:00
|
|
|
break;
|
2015-06-17 10:01:39 +00:00
|
|
|
case DataLocation::Storage:
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SLOAD;
|
2022-01-28 17:57:05 +00:00
|
|
|
if (_arrayType.isByteArrayOrString())
|
2020-11-09 14:43:57 +00:00
|
|
|
m_context.callYulFunction(m_context.utilFunctions().extractByteArrayLengthFunction(), 1, 1);
|
2015-03-03 16:55:28 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-02-27 21:52:02 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 08:22:45 +00:00
|
|
|
void ArrayUtils::accessIndex(ArrayType const& _arrayType, bool _doBoundsCheck, bool _keepReference) const
|
2015-03-30 17:31:57 +00:00
|
|
|
{
|
2015-09-25 15:13:29 +00:00
|
|
|
/// Stack: reference [length] index
|
2015-06-17 10:01:39 +00:00
|
|
|
DataLocation location = _arrayType.location();
|
2015-03-30 17:31:57 +00:00
|
|
|
|
2015-06-25 15:51:01 +00:00
|
|
|
if (_doBoundsCheck)
|
|
|
|
{
|
|
|
|
// retrieve length
|
2015-09-25 15:13:29 +00:00
|
|
|
ArrayUtils::retrieveLength(_arrayType, 1);
|
|
|
|
// Stack: ref [length] index length
|
2015-06-25 15:51:01 +00:00
|
|
|
// check out-of-bounds access
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP2 << Instruction::LT << Instruction::ISZERO;
|
2015-06-25 15:51:01 +00:00
|
|
|
// out-of-bounds access throws exception
|
2020-10-12 14:01:45 +00:00
|
|
|
m_context.appendConditionalPanic(util::PanicCode::ArrayOutOfBounds);
|
2015-06-25 15:51:01 +00:00
|
|
|
}
|
2015-09-25 15:13:29 +00:00
|
|
|
if (location == DataLocation::CallData && _arrayType.isDynamicallySized())
|
2015-06-26 18:27:56 +00:00
|
|
|
// remove length if present
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP1 << Instruction::POP;
|
2015-03-30 17:31:57 +00:00
|
|
|
|
|
|
|
// stack: <base_ref> <index>
|
2015-03-31 12:59:38 +00:00
|
|
|
switch (location)
|
|
|
|
{
|
2015-06-22 10:50:09 +00:00
|
|
|
case DataLocation::Memory:
|
2019-06-03 10:01:11 +00:00
|
|
|
// stack: <base_ref> <index>
|
2022-01-28 17:57:05 +00:00
|
|
|
if (!_arrayType.isByteArrayOrString())
|
2019-06-03 10:01:11 +00:00
|
|
|
m_context << u256(_arrayType.memoryHeadSize()) << Instruction::MUL;
|
|
|
|
if (_arrayType.isDynamicallySized())
|
|
|
|
m_context << u256(32) << Instruction::ADD;
|
|
|
|
if (_keepReference)
|
|
|
|
m_context << Instruction::DUP2;
|
|
|
|
m_context << Instruction::ADD;
|
|
|
|
break;
|
2015-09-25 15:13:29 +00:00
|
|
|
case DataLocation::CallData:
|
2022-01-28 17:57:05 +00:00
|
|
|
if (!_arrayType.isByteArrayOrString())
|
2015-06-17 10:01:39 +00:00
|
|
|
{
|
2019-07-09 14:10:38 +00:00
|
|
|
m_context << _arrayType.calldataStride();
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::MUL;
|
2015-06-17 10:01:39 +00:00
|
|
|
}
|
2019-02-13 08:22:45 +00:00
|
|
|
// stack: <base_ref> <index * size>
|
|
|
|
if (_keepReference)
|
|
|
|
m_context << Instruction::DUP2;
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::ADD;
|
2015-03-31 12:59:38 +00:00
|
|
|
break;
|
2015-06-17 10:01:39 +00:00
|
|
|
case DataLocation::Storage:
|
2015-09-25 15:13:29 +00:00
|
|
|
{
|
2019-02-13 08:22:45 +00:00
|
|
|
if (_keepReference)
|
|
|
|
m_context << Instruction::DUP2;
|
|
|
|
else
|
|
|
|
m_context << Instruction::SWAP1;
|
|
|
|
// stack: [<base_ref>] <index> <base_ref>
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem endTag = m_context.newTag();
|
2022-01-28 17:57:05 +00:00
|
|
|
if (_arrayType.isByteArrayOrString())
|
2015-09-25 15:13:29 +00:00
|
|
|
{
|
|
|
|
// Special case of short byte arrays.
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP1;
|
|
|
|
m_context << Instruction::DUP2 << Instruction::SLOAD;
|
|
|
|
m_context << u256(1) << Instruction::AND << Instruction::ISZERO;
|
2015-09-25 15:13:29 +00:00
|
|
|
// No action needed for short byte arrays.
|
|
|
|
m_context.appendConditionalJumpTo(endTag);
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP1;
|
2015-09-25 15:13:29 +00:00
|
|
|
}
|
|
|
|
if (_arrayType.isDynamicallySized())
|
|
|
|
CompilerUtils(m_context).computeHashStatic();
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP1;
|
2015-08-31 16:44:29 +00:00
|
|
|
if (_arrayType.baseType()->storageBytes() <= 16)
|
2015-03-31 12:59:38 +00:00
|
|
|
{
|
|
|
|
// stack: <data_ref> <index>
|
|
|
|
// goal:
|
|
|
|
// <ref> <byte_number> = <base_ref + index / itemsPerSlot> <(index % itemsPerSlot) * byteSize>
|
2015-08-31 16:44:29 +00:00
|
|
|
unsigned byteSize = _arrayType.baseType()->storageBytes();
|
2015-03-31 12:59:38 +00:00
|
|
|
solAssert(byteSize != 0, "");
|
|
|
|
unsigned itemsPerSlot = 32 / byteSize;
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << u256(itemsPerSlot) << Instruction::SWAP2;
|
2015-03-31 12:59:38 +00:00
|
|
|
// stack: itemsPerSlot index data_ref
|
2015-03-30 17:31:57 +00:00
|
|
|
m_context
|
2016-04-04 11:41:35 +00:00
|
|
|
<< Instruction::DUP3 << Instruction::DUP3
|
|
|
|
<< Instruction::DIV << Instruction::ADD
|
2015-03-31 12:59:38 +00:00
|
|
|
// stack: itemsPerSlot index (data_ref + index / itemsPerSlot)
|
2016-04-04 11:41:35 +00:00
|
|
|
<< Instruction::SWAP2 << Instruction::SWAP1
|
|
|
|
<< Instruction::MOD;
|
2015-03-31 12:59:38 +00:00
|
|
|
if (byteSize != 1)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << u256(byteSize) << Instruction::MUL;
|
2015-03-30 17:31:57 +00:00
|
|
|
}
|
2015-03-31 12:59:38 +00:00
|
|
|
else
|
2015-03-30 17:31:57 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
if (_arrayType.baseType()->storageSize() != 1)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << _arrayType.baseType()->storageSize() << Instruction::MUL;
|
|
|
|
m_context << Instruction::ADD << u256(0);
|
2015-03-30 17:31:57 +00:00
|
|
|
}
|
2015-09-25 15:13:29 +00:00
|
|
|
m_context << endTag;
|
2015-03-31 12:59:38 +00:00
|
|
|
break;
|
2015-03-30 17:31:57 +00:00
|
|
|
}
|
2015-09-25 15:13:29 +00:00
|
|
|
}
|
2015-03-30 17:31:57 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 09:22:11 +00:00
|
|
|
void ArrayUtils::accessCallDataArrayElement(ArrayType const& _arrayType, bool _doBoundsCheck) const
|
|
|
|
{
|
|
|
|
solAssert(_arrayType.location() == DataLocation::CallData, "");
|
|
|
|
if (_arrayType.baseType()->isDynamicallyEncoded())
|
|
|
|
{
|
|
|
|
// stack layout: <base_ref> <length> <index>
|
|
|
|
ArrayUtils(m_context).accessIndex(_arrayType, _doBoundsCheck, true);
|
|
|
|
// stack layout: <base_ref> <ptr_to_tail>
|
|
|
|
|
|
|
|
CompilerUtils(m_context).accessCalldataTail(*_arrayType.baseType());
|
|
|
|
// stack layout: <tail_ref> [length]
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ArrayUtils(m_context).accessIndex(_arrayType, _doBoundsCheck);
|
|
|
|
if (_arrayType.baseType()->isValueType())
|
|
|
|
{
|
|
|
|
solAssert(_arrayType.baseType()->storageBytes() <= 32, "");
|
|
|
|
if (
|
2022-01-28 17:57:05 +00:00
|
|
|
!_arrayType.isByteArrayOrString() &&
|
2019-06-19 09:22:11 +00:00
|
|
|
_arrayType.baseType()->storageBytes() < 32 &&
|
2020-10-22 17:19:14 +00:00
|
|
|
m_context.useABICoderV2()
|
2019-06-19 09:22:11 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
m_context << u256(32);
|
|
|
|
CompilerUtils(m_context).abiDecodeV2({_arrayType.baseType()}, false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
CompilerUtils(m_context).loadFromMemoryDynamic(
|
|
|
|
*_arrayType.baseType(),
|
|
|
|
true,
|
2022-01-28 17:57:05 +00:00
|
|
|
!_arrayType.isByteArrayOrString(),
|
2019-06-19 09:22:11 +00:00
|
|
|
false
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
solAssert(
|
|
|
|
_arrayType.baseType()->category() == Type::Category::Struct ||
|
|
|
|
_arrayType.baseType()->category() == Type::Category::Array,
|
|
|
|
"Invalid statically sized non-value base type on array access."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 17:15:16 +00:00
|
|
|
void ArrayUtils::incrementByteOffset(unsigned _byteSize, unsigned _byteOffsetPosition, unsigned _storageOffsetPosition) const
|
2015-03-16 16:52:19 +00:00
|
|
|
{
|
2015-03-19 17:15:16 +00:00
|
|
|
solAssert(_byteSize < 32, "");
|
|
|
|
solAssert(_byteSize != 0, "");
|
2015-03-16 16:52:19 +00:00
|
|
|
// We do the following, but avoiding jumps:
|
|
|
|
// byteOffset += byteSize
|
|
|
|
// if (byteOffset + byteSize > 32)
|
|
|
|
// {
|
|
|
|
// storageOffset++;
|
|
|
|
// byteOffset = 0;
|
|
|
|
// }
|
2015-03-19 17:15:16 +00:00
|
|
|
if (_byteOffsetPosition > 1)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << swapInstruction(_byteOffsetPosition - 1);
|
|
|
|
m_context << u256(_byteSize) << Instruction::ADD;
|
2015-03-19 17:15:16 +00:00
|
|
|
if (_byteOffsetPosition > 1)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << swapInstruction(_byteOffsetPosition - 1);
|
2015-03-16 16:52:19 +00:00
|
|
|
// compute, X := (byteOffset + byteSize - 1) / 32, should be 1 iff byteOffset + bytesize > 32
|
|
|
|
m_context
|
2016-04-04 11:41:35 +00:00
|
|
|
<< u256(32) << dupInstruction(1 + _byteOffsetPosition) << u256(_byteSize - 1)
|
|
|
|
<< Instruction::ADD << Instruction::DIV;
|
2015-03-16 16:52:19 +00:00
|
|
|
// increment storage offset if X == 1 (just add X to it)
|
|
|
|
// stack: X
|
|
|
|
m_context
|
2016-04-04 11:41:35 +00:00
|
|
|
<< swapInstruction(_storageOffsetPosition) << dupInstruction(_storageOffsetPosition + 1)
|
|
|
|
<< Instruction::ADD << swapInstruction(_storageOffsetPosition);
|
2015-03-16 16:52:19 +00:00
|
|
|
// stack: X
|
|
|
|
// set source_byte_offset to zero if X == 1 (using source_byte_offset *= 1 - X)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << u256(1) << Instruction::SUB;
|
2015-03-16 16:52:19 +00:00
|
|
|
// stack: 1 - X
|
2015-03-19 17:15:16 +00:00
|
|
|
if (_byteOffsetPosition == 1)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::MUL;
|
2015-03-16 16:52:19 +00:00
|
|
|
else
|
|
|
|
m_context
|
2016-04-04 11:41:35 +00:00
|
|
|
<< dupInstruction(_byteOffsetPosition + 1) << Instruction::MUL
|
|
|
|
<< swapInstruction(_byteOffsetPosition) << Instruction::POP;
|
2015-03-16 16:52:19 +00:00
|
|
|
}
|