2017-08-07 14:43:26 +00:00
|
|
|
/*
|
|
|
|
This file is part of solidity.
|
|
|
|
|
|
|
|
solidity is free software: you can redistribute it and/or modify
|
|
|
|
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.
|
|
|
|
|
|
|
|
solidity is distributed in the hope that it will be useful,
|
|
|
|
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
|
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2017-08-07 14:43:26 +00:00
|
|
|
/**
|
|
|
|
* @author Christian <chris@ethereum.org>
|
|
|
|
* @date 2017
|
2018-06-12 18:28:52 +00:00
|
|
|
* Routines that generate Yul code related to ABI encoding, decoding and type conversions.
|
2017-08-07 14:43:26 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2019-03-05 16:20:28 +00:00
|
|
|
#include <libsolidity/codegen/MultiUseYulFunctionCollector.h>
|
|
|
|
#include <libsolidity/codegen/YulUtilFunctions.h>
|
|
|
|
|
2020-01-22 14:48:56 +00:00
|
|
|
#include <libsolidity/interface/DebugSettings.h>
|
|
|
|
|
2018-12-17 16:06:11 +00:00
|
|
|
#include <liblangutil/EVMVersion.h>
|
2017-08-07 14:43:26 +00:00
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <map>
|
2018-12-17 16:06:11 +00:00
|
|
|
#include <vector>
|
2017-08-07 14:43:26 +00:00
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::frontend
|
2019-03-05 16:20:28 +00:00
|
|
|
{
|
2017-08-07 14:43:26 +00:00
|
|
|
|
|
|
|
class Type;
|
|
|
|
class ArrayType;
|
|
|
|
class StructType;
|
|
|
|
class FunctionType;
|
2019-04-15 13:33:39 +00:00
|
|
|
using TypePointer = Type const*;
|
2017-08-07 14:43:26 +00:00
|
|
|
using TypePointers = std::vector<TypePointer>;
|
|
|
|
|
2019-03-05 16:20:28 +00:00
|
|
|
/**
|
|
|
|
* Class to generate encoding and decoding functions. Also maintains a collection
|
|
|
|
* of "functions to be generated" in order to avoid generating the same function
|
|
|
|
* multiple times.
|
|
|
|
*
|
|
|
|
* Make sure to include the result of ``requestedFunctions()`` to a block that
|
|
|
|
* is visible from the code that was generated here, or use named labels.
|
|
|
|
*/
|
2017-08-07 14:43:26 +00:00
|
|
|
class ABIFunctions
|
|
|
|
{
|
|
|
|
public:
|
2019-03-05 16:20:28 +00:00
|
|
|
explicit ABIFunctions(
|
|
|
|
langutil::EVMVersion _evmVersion,
|
2020-01-22 14:48:56 +00:00
|
|
|
RevertStrings _revertStrings,
|
2020-03-02 17:08:19 +00:00
|
|
|
MultiUseYulFunctionCollector& _functionCollector
|
2019-03-05 16:20:28 +00:00
|
|
|
):
|
|
|
|
m_evmVersion(_evmVersion),
|
2020-01-22 14:48:56 +00:00
|
|
|
m_revertStrings(_revertStrings),
|
2020-03-02 17:08:19 +00:00
|
|
|
m_functionCollector(_functionCollector),
|
2020-01-22 14:48:56 +00:00
|
|
|
m_utils(_evmVersion, m_revertStrings, m_functionCollector)
|
2019-03-05 16:20:28 +00:00
|
|
|
{}
|
2018-04-05 14:56:02 +00:00
|
|
|
|
2017-08-25 16:58:12 +00:00
|
|
|
/// @returns name of an assembly function to ABI-encode values of @a _givenTypes
|
2017-08-07 14:43:26 +00:00
|
|
|
/// into memory, converting the types to @a _targetTypes on the fly.
|
2020-05-06 23:55:30 +00:00
|
|
|
/// Parameters are: <headStart> <value_1> ... <value_n>, i.e.
|
|
|
|
/// the layout on the stack is <value_n> ... <value_1> <headStart> with
|
2017-08-25 16:58:12 +00:00
|
|
|
/// the top of the stack on the right.
|
|
|
|
/// The values represent stack slots. If a type occupies more or less than one
|
|
|
|
/// stack slot, it takes exactly that number of values.
|
|
|
|
/// Returns a pointer to the end of the area written in memory.
|
2019-01-23 10:32:25 +00:00
|
|
|
/// Does not allocate memory (does not change the free memory pointer), but writes
|
2017-08-07 14:43:26 +00:00
|
|
|
/// to memory starting at $headStart and an unrestricted amount after that.
|
2020-05-06 23:55:30 +00:00
|
|
|
/// If @reversed is true, the order of the variables after <headStart> is reversed.
|
2017-08-07 14:43:26 +00:00
|
|
|
std::string tupleEncoder(
|
|
|
|
TypePointers const& _givenTypes,
|
2021-01-11 10:52:22 +00:00
|
|
|
TypePointers _targetTypes,
|
2020-05-06 23:55:30 +00:00
|
|
|
bool _encodeAsLibraryTypes = false,
|
|
|
|
bool _reversed = false
|
2017-08-07 14:43:26 +00:00
|
|
|
);
|
|
|
|
|
2020-05-06 23:55:30 +00:00
|
|
|
/// Specialization of tupleEncoder to _reversed = true
|
|
|
|
std::string tupleEncoderReversed(
|
|
|
|
TypePointers const& _givenTypes,
|
|
|
|
TypePointers const& _targetTypes,
|
|
|
|
bool _encodeAsLibraryTypes = false
|
|
|
|
) {
|
|
|
|
return tupleEncoder(_givenTypes, _targetTypes, _encodeAsLibraryTypes, true);
|
|
|
|
}
|
|
|
|
|
2019-01-23 10:32:25 +00:00
|
|
|
/// @returns name of an assembly function to encode values of @a _givenTypes
|
|
|
|
/// with packed encoding into memory, converting the types to @a _targetTypes on the fly.
|
2020-05-06 23:55:30 +00:00
|
|
|
/// Parameters are: <memPos> <value_1> ... <value_n>, i.e.
|
|
|
|
/// the layout on the stack is <value_n> ... <value_1> <memPos> with
|
2019-01-23 10:32:25 +00:00
|
|
|
/// the top of the stack on the right.
|
|
|
|
/// The values represent stack slots. If a type occupies more or less than one
|
|
|
|
/// stack slot, it takes exactly that number of values.
|
|
|
|
/// Returns a pointer to the end of the area written in memory.
|
|
|
|
/// Does not allocate memory (does not change the free memory pointer), but writes
|
|
|
|
/// to memory starting at memPos and an unrestricted amount after that.
|
2020-05-06 23:55:30 +00:00
|
|
|
/// If @reversed is true, the order of the variables after <headStart> is reversed.
|
|
|
|
std::string tupleEncoderPacked(
|
|
|
|
TypePointers const& _givenTypes,
|
2021-01-11 10:52:22 +00:00
|
|
|
TypePointers _targetTypes,
|
2020-05-06 23:55:30 +00:00
|
|
|
bool _reversed = false
|
|
|
|
);
|
|
|
|
|
|
|
|
/// Specialization of tupleEncoderPacked to _reversed = true
|
|
|
|
std::string tupleEncoderPackedReversed(TypePointers const& _givenTypes, TypePointers const& _targetTypes)
|
|
|
|
{
|
|
|
|
return tupleEncoderPacked(_givenTypes, _targetTypes, true);
|
|
|
|
}
|
2019-01-23 10:32:25 +00:00
|
|
|
|
2017-09-01 11:59:21 +00:00
|
|
|
/// @returns name of an assembly function to ABI-decode values of @a _types
|
|
|
|
/// into memory. If @a _fromMemory is true, decodes from memory instead of
|
|
|
|
/// from calldata.
|
|
|
|
/// Can allocate memory.
|
|
|
|
/// Inputs: <source_offset> <source_end> (layout reversed on stack)
|
|
|
|
/// Outputs: <value0> <value1> ... <valuen>
|
|
|
|
/// The values represent stack slots. If a type occupies more or less than one
|
|
|
|
/// stack slot, it takes exactly that number of values.
|
|
|
|
std::string tupleDecoder(TypePointers const& _types, bool _fromMemory = false);
|
|
|
|
|
2019-01-21 15:43:32 +00:00
|
|
|
struct EncodingOptions
|
|
|
|
{
|
2019-01-22 11:24:54 +00:00
|
|
|
/// Pad/signextend value types and bytes/string to multiples of 32 bytes.
|
|
|
|
/// If false, data is always left-aligned.
|
|
|
|
/// Note that this is always re-set to true for the elements of arrays and structs.
|
2019-01-21 15:43:32 +00:00
|
|
|
bool padded = true;
|
|
|
|
/// Store arrays and structs in place without "data pointer" and do not store the length.
|
|
|
|
bool dynamicInplace = false;
|
|
|
|
/// Only for external function types: The value is a pair of address / function id instead
|
|
|
|
/// of a memory pointer to the compression representation.
|
|
|
|
bool encodeFunctionFromStack = false;
|
|
|
|
/// Encode storage pointers as storage pointers (we are targeting a library call).
|
|
|
|
bool encodeAsLibraryTypes = false;
|
|
|
|
|
|
|
|
/// @returns a string to uniquely identify the encoding options for the encoding
|
|
|
|
/// function name. Skips everything that has its default value.
|
|
|
|
std::string toFunctionNameSuffix() const;
|
|
|
|
};
|
|
|
|
|
2020-10-01 17:42:42 +00:00
|
|
|
/// Internal encoding function that is also used by some copying routines.
|
2017-08-07 14:43:26 +00:00
|
|
|
/// @returns the name of the ABI encoding function with the given type
|
|
|
|
/// and queues the generation of the function to the requested functions.
|
2017-10-04 14:56:54 +00:00
|
|
|
/// @param _fromStack if false, the input value was just loaded from storage
|
2017-08-07 14:43:26 +00:00
|
|
|
/// or memory and thus might be compacted into a single slot (depending on the type).
|
|
|
|
std::string abiEncodingFunction(
|
|
|
|
Type const& _givenType,
|
|
|
|
Type const& _targetType,
|
2019-01-21 15:43:32 +00:00
|
|
|
EncodingOptions const& _options
|
2017-08-07 14:43:26 +00:00
|
|
|
);
|
2020-10-01 17:42:42 +00:00
|
|
|
/// Internal encoding function that is also used by some copying routines.
|
2019-01-22 11:24:54 +00:00
|
|
|
/// @returns the name of a function that internally calls `abiEncodingFunction`
|
|
|
|
/// but always returns the updated encoding position, even if the type is
|
|
|
|
/// statically encoded.
|
|
|
|
std::string abiEncodeAndReturnUpdatedPosFunction(
|
|
|
|
Type const& _givenType,
|
|
|
|
Type const& _targetType,
|
|
|
|
EncodingOptions const& _options
|
|
|
|
);
|
2020-10-01 17:42:42 +00:00
|
|
|
|
2020-11-19 11:04:44 +00:00
|
|
|
/// Decodes array in case of dynamic arrays with offset pointing to
|
|
|
|
/// data and length already on stack
|
|
|
|
/// signature: (dataOffset, length, dataEnd) -> decodedArray
|
|
|
|
std::string abiDecodingFunctionArrayAvailableLength(ArrayType const& _type, bool _fromMemory);
|
|
|
|
|
2020-11-23 14:17:11 +00:00
|
|
|
/// Internal decoding function that is also used by some copying routines.
|
|
|
|
/// @returns the name of a function that decodes structs.
|
|
|
|
/// signature: (dataStart, dataEnd) -> decodedStruct
|
|
|
|
std::string abiDecodingFunctionStruct(StructType const& _type, bool _fromMemory);
|
|
|
|
|
2020-10-01 17:42:42 +00:00
|
|
|
private:
|
2017-08-07 14:43:26 +00:00
|
|
|
/// Part of @a abiEncodingFunction for array target type and given calldata array.
|
2019-03-15 13:08:38 +00:00
|
|
|
/// Uses calldatacopy and does not perform cleanup or validation and can therefore only
|
|
|
|
/// be used for byte arrays and arrays with the base type uint256 or bytes32.
|
|
|
|
std::string abiEncodingFunctionCalldataArrayWithoutCleanup(
|
2017-08-07 14:43:26 +00:00
|
|
|
Type const& _givenType,
|
|
|
|
Type const& _targetType,
|
2019-01-21 15:43:32 +00:00
|
|
|
EncodingOptions const& _options
|
2017-08-07 14:43:26 +00:00
|
|
|
);
|
|
|
|
/// Part of @a abiEncodingFunction for array target type and given memory array or
|
2019-06-19 02:46:05 +00:00
|
|
|
/// a given storage array with every item occupies one or multiple full slots.
|
2017-08-07 14:43:26 +00:00
|
|
|
std::string abiEncodingFunctionSimpleArray(
|
|
|
|
ArrayType const& _givenType,
|
|
|
|
ArrayType const& _targetType,
|
2019-01-21 15:43:32 +00:00
|
|
|
EncodingOptions const& _options
|
2017-08-07 14:43:26 +00:00
|
|
|
);
|
|
|
|
std::string abiEncodingFunctionMemoryByteArray(
|
|
|
|
ArrayType const& _givenType,
|
|
|
|
ArrayType const& _targetType,
|
2019-01-21 15:43:32 +00:00
|
|
|
EncodingOptions const& _options
|
2017-08-07 14:43:26 +00:00
|
|
|
);
|
|
|
|
/// Part of @a abiEncodingFunction for array target type and given storage array
|
|
|
|
/// where multiple items are packed into the same storage slot.
|
|
|
|
std::string abiEncodingFunctionCompactStorageArray(
|
|
|
|
ArrayType const& _givenType,
|
|
|
|
ArrayType const& _targetType,
|
2019-01-21 15:43:32 +00:00
|
|
|
EncodingOptions const& _options
|
2017-08-07 14:43:26 +00:00
|
|
|
);
|
|
|
|
|
2017-08-24 13:20:49 +00:00
|
|
|
/// Part of @a abiEncodingFunction for struct types.
|
|
|
|
std::string abiEncodingFunctionStruct(
|
|
|
|
StructType const& _givenType,
|
|
|
|
StructType const& _targetType,
|
2019-01-21 15:43:32 +00:00
|
|
|
EncodingOptions const& _options
|
2017-08-24 13:20:49 +00:00
|
|
|
);
|
|
|
|
|
2017-08-07 14:43:26 +00:00
|
|
|
// @returns the name of the ABI encoding function with the given type
|
|
|
|
// and queues the generation of the function to the requested functions.
|
|
|
|
// Case for _givenType being a string literal
|
|
|
|
std::string abiEncodingFunctionStringLiteral(
|
|
|
|
Type const& _givenType,
|
|
|
|
Type const& _targetType,
|
2019-01-21 15:43:32 +00:00
|
|
|
EncodingOptions const& _options
|
2017-08-07 14:43:26 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
std::string abiEncodingFunctionFunctionType(
|
|
|
|
FunctionType const& _from,
|
|
|
|
Type const& _to,
|
2019-01-21 15:43:32 +00:00
|
|
|
EncodingOptions const& _options
|
2017-08-07 14:43:26 +00:00
|
|
|
);
|
|
|
|
|
2017-10-13 15:29:09 +00:00
|
|
|
/// @returns the name of the ABI decoding function for the given type
|
2017-09-01 11:59:21 +00:00
|
|
|
/// and queues the generation of the function to the requested functions.
|
|
|
|
/// The caller has to ensure that no out of bounds access (at least to the static
|
|
|
|
/// part) can happen inside this function.
|
|
|
|
/// @param _fromMemory if decoding from memory instead of from calldata
|
|
|
|
/// @param _forUseOnStack if the decoded value is stored on stack or in memory.
|
|
|
|
std::string abiDecodingFunction(
|
2020-02-17 17:44:39 +00:00
|
|
|
Type const& _type,
|
2017-09-01 11:59:21 +00:00
|
|
|
bool _fromMemory,
|
|
|
|
bool _forUseOnStack
|
|
|
|
);
|
|
|
|
|
2017-10-13 15:29:09 +00:00
|
|
|
/// Part of @a abiDecodingFunction for value types.
|
|
|
|
std::string abiDecodingFunctionValueType(Type const& _type, bool _fromMemory);
|
2017-09-01 11:59:21 +00:00
|
|
|
/// Part of @a abiDecodingFunction for "regular" array types.
|
|
|
|
std::string abiDecodingFunctionArray(ArrayType const& _type, bool _fromMemory);
|
|
|
|
/// Part of @a abiDecodingFunction for calldata array types.
|
|
|
|
std::string abiDecodingFunctionCalldataArray(ArrayType const& _type);
|
2020-11-19 11:04:44 +00:00
|
|
|
/// Part of @a abiDecodingFunctionArrayWithAvailableLength
|
|
|
|
std::string abiDecodingFunctionByteArrayAvailableLength(ArrayType const& _type, bool _fromMemory);
|
2019-02-05 19:29:57 +00:00
|
|
|
/// Part of @a abiDecodingFunction for calldata struct types.
|
|
|
|
std::string abiDecodingFunctionCalldataStruct(StructType const& _type);
|
2017-09-01 11:59:21 +00:00
|
|
|
/// Part of @a abiDecodingFunction for array types.
|
|
|
|
std::string abiDecodingFunctionFunctionType(FunctionType const& _type, bool _fromMemory, bool _forUseOnStack);
|
2019-03-15 13:47:17 +00:00
|
|
|
/// @returns the name of a function that retrieves an element from calldata.
|
|
|
|
std::string calldataAccessFunction(Type const& _type);
|
|
|
|
|
2019-02-06 11:26:54 +00:00
|
|
|
/// @returns the name of a function used during encoding that stores the length
|
2019-01-22 11:24:54 +00:00
|
|
|
/// if the array is dynamically sized (and the options do not request in-place encoding).
|
|
|
|
/// It returns the new encoding position.
|
|
|
|
/// If the array is not dynamically sized (or in-place encoding was requested),
|
|
|
|
/// does nothing and just returns the position again.
|
|
|
|
std::string arrayStoreLengthForEncodingFunction(ArrayType const& _type, EncodingOptions const& _options);
|
2019-02-06 11:26:54 +00:00
|
|
|
|
2017-08-07 14:43:26 +00:00
|
|
|
/// Helper function that uses @a _creator to create a function and add it to
|
|
|
|
/// @a m_requestedFunctions if it has not been created yet and returns @a _name in both
|
|
|
|
/// cases.
|
|
|
|
std::string createFunction(std::string const& _name, std::function<std::string()> const& _creator);
|
|
|
|
|
|
|
|
/// @returns the size of the static part of the encoding of the given types.
|
2017-08-21 21:05:24 +00:00
|
|
|
static size_t headSize(TypePointers const& _targetTypes);
|
2017-08-07 14:43:26 +00:00
|
|
|
|
2019-03-15 12:53:26 +00:00
|
|
|
/// @returns the number of variables needed to store a type.
|
|
|
|
/// This is one for almost all types. The exception being dynamically sized calldata arrays or
|
|
|
|
/// external function types (if we are encoding from stack, i.e. _options.encodeFunctionFromStack
|
|
|
|
/// is true), for which it is two.
|
|
|
|
static size_t numVariablesForType(Type const& _type, EncodingOptions const& _options);
|
|
|
|
|
2020-01-22 14:48:56 +00:00
|
|
|
/// @returns code that stores @param _message for revert reason
|
|
|
|
/// if m_revertStrings is debug.
|
|
|
|
std::string revertReasonIfDebug(std::string const& _message = "");
|
|
|
|
|
2019-02-25 14:29:57 +00:00
|
|
|
langutil::EVMVersion m_evmVersion;
|
2020-01-22 14:48:56 +00:00
|
|
|
RevertStrings const m_revertStrings;
|
2020-03-02 17:08:19 +00:00
|
|
|
MultiUseYulFunctionCollector& m_functionCollector;
|
2019-03-05 16:20:28 +00:00
|
|
|
YulUtilFunctions m_utils;
|
2017-08-07 14:43:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|