Improve FunctionSelector helpers

This commit is contained in:
Alex Beregszaszi
2022-09-27 17:58:32 +02:00
parent 311b2054af
commit 725253551e
23 changed files with 106 additions and 50 deletions
+1 -1
View File
@@ -411,7 +411,7 @@ struct ReservedErrorSelector: public PostTypeChecker::Checker
);
else
{
uint32_t selector = util::selectorFromSignature32(_error.functionType(true)->externalSignature());
uint32_t selector = util::selectorFromSignatureU32(_error.functionType(true)->externalSignature());
if (selector == 0 || ~selector == 0)
m_errorReporter.syntaxError(
2855_error,
@@ -52,7 +52,7 @@ bool PostTypeContractLevelChecker::check(ContractDefinition const& _contract)
for (ErrorDefinition const* error: _contract.interfaceErrors())
{
string signature = error->functionType(true)->externalSignature();
uint32_t hash = util::selectorFromSignature32(signature);
uint32_t hash = util::selectorFromSignatureU32(signature);
// Fail if there is a different signature for the same hash.
if (!errorHashes[hash].empty() && !errorHashes[hash].count(signature))
{
+2 -2
View File
@@ -27,6 +27,7 @@
#include <libsolidity/ast/ASTVisitor.h>
#include <libsolidity/ast/AST_accept.h>
#include <libsolidity/ast/TypeProvider.h>
#include <libsolutil/FunctionSelector.h>
#include <libsolutil/Keccak256.h>
#include <range/v3/view/tail.hpp>
@@ -281,8 +282,7 @@ vector<pair<util::FixedHash<4>, FunctionTypePointer>> const& ContractDefinition:
if (signaturesSeen.count(functionSignature) == 0)
{
signaturesSeen.insert(functionSignature);
util::FixedHash<4> hash(util::keccak256(functionSignature));
interfaceFunctionList.emplace_back(hash, fun);
interfaceFunctionList.emplace_back(util::selectorFromSignatureH32(functionSignature), fun);
}
}
}
+2 -2
View File
@@ -3583,12 +3583,12 @@ string FunctionType::externalSignature() const
u256 FunctionType::externalIdentifier() const
{
return util::selectorFromSignature32(externalSignature());
return util::selectorFromSignatureU32(externalSignature());
}
string FunctionType::externalIdentifierHex() const
{
return util::FixedHash<4>(util::keccak256(externalSignature())).hex();
return util::selectorFromSignatureH32(externalSignature()).hex();
}
bool FunctionType::isPure() const
+1 -1
View File
@@ -889,7 +889,7 @@ void ArrayUtils::popStorageArrayElement(ArrayType const& _type) const
}
sstore(ref, slot_value)
})");
code("panicSelector", util::selectorFromSignature("Panic(uint256)").str());
code("panicSelector", util::selectorFromSignatureU256("Panic(uint256)").str());
code("emptyArrayPop", to_string(unsigned(util::PanicCode::EmptyArrayPop)));
m_context.appendInlineAssembly(code.render(), {"ref", "slot_value", "length"});
m_context << Instruction::POP << Instruction::POP << Instruction::POP;
+2 -2
View File
@@ -95,7 +95,7 @@ void CompilerUtils::revertWithStringData(Type const& _argumentType)
{
solAssert(_argumentType.isImplicitlyConvertibleTo(*TypeProvider::fromElementaryTypeName("string memory")));
fetchFreeMemoryPointer();
m_context << util::selectorFromSignature("Error(string)");
m_context << util::selectorFromSignatureU256("Error(string)");
m_context << Instruction::DUP2 << Instruction::MSTORE;
m_context << u256(4) << Instruction::ADD;
// Stack: <string data> <mem pos of encoding start>
@@ -111,7 +111,7 @@ void CompilerUtils::revertWithError(
)
{
fetchFreeMemoryPointer();
m_context << util::selectorFromSignature(_signature);
m_context << util::selectorFromSignatureU256(_signature);
m_context << Instruction::DUP2 << Instruction::MSTORE;
m_context << u256(4) << Instruction::ADD;
// Stack: <arguments...> <mem pos of encoding start>
+3 -3
View File
@@ -253,7 +253,7 @@ size_t ContractCompiler::deployLibrary(ContractDefinition const& _contract)
return(codepos, subSize)
}
)")
("panicSelector", util::selectorFromSignature("Panic(uint256)").str())
("panicSelector", util::selectorFromSignatureU256("Panic(uint256)").str())
("panicCode", "0")
.render(),
{"subSize", "subOffset"}
@@ -1046,7 +1046,7 @@ void ContractCompiler::handleCatch(vector<ASTPointer<TryCatchClause>> const& _ca
solAssert(m_context.evmVersion().supportsReturndata(), "");
// stack: <selector>
m_context << Instruction::DUP1 << util::selectorFromSignature32("Error(string)") << Instruction::EQ;
m_context << Instruction::DUP1 << util::selectorFromSignatureU32("Error(string)") << Instruction::EQ;
m_context << Instruction::ISZERO;
m_context.appendConditionalJumpTo(panicTag);
m_context << Instruction::POP; // remove selector
@@ -1078,7 +1078,7 @@ void ContractCompiler::handleCatch(vector<ASTPointer<TryCatchClause>> const& _ca
solAssert(m_context.evmVersion().supportsReturndata(), "");
// stack: <selector>
m_context << util::selectorFromSignature32("Panic(uint256)") << Instruction::EQ;
m_context << util::selectorFromSignatureU32("Panic(uint256)") << Instruction::EQ;
m_context << Instruction::ISZERO;
m_context.appendConditionalJumpTo(fallbackTag);
+1 -1
View File
@@ -1329,7 +1329,7 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
// hash the signature
if (auto const* stringType = dynamic_cast<StringLiteralType const*>(selectorType))
{
m_context << util::selectorFromSignature(stringType->value());
m_context << util::selectorFromSignatureU256(stringType->value());
dataOnStack = TypeProvider::fixedBytes(4);
}
else
+3 -3
View File
@@ -233,7 +233,7 @@ string YulUtilFunctions::requireOrAssertFunction(bool _assert, Type const* _mess
.render();
int const hashHeaderSize = 4;
u256 const errorHash = util::selectorFromSignature("Error(string)");
u256 const errorHash = util::selectorFromSignatureU256("Error(string)");
string const encodeFunc = ABIFunctions(m_evmVersion, m_revertStrings, m_functionCollector)
.tupleEncoder(
@@ -4426,7 +4426,7 @@ string YulUtilFunctions::revertReasonIfDebugBody(
revert(start, <overallLength>)
)");
templ("allocate", _allocation);
templ("sig", util::selectorFromSignature("Error(string)").str());
templ("sig", util::selectorFromSignatureU256("Error(string)").str());
templ("length", to_string(_message.length()));
size_t words = (_message.length() + 31) / 32;
@@ -4454,7 +4454,7 @@ string YulUtilFunctions::panicFunction(util::PanicCode _code)
}
)")
("functionName", functionName)
("selector", util::selectorFromSignature("Panic(uint256)").str())
("selector", util::selectorFromSignatureU256("Panic(uint256)").str())
("code", toCompactHexWithPrefix(static_cast<unsigned>(_code)))
.render();
});
@@ -1196,7 +1196,7 @@ void IRGeneratorForStatements::endVisit(FunctionCall const& _functionCall)
// hash the signature
Type const& selectorType = type(*arguments.front());
if (auto const* stringType = dynamic_cast<StringLiteralType const*>(&selectorType))
selector = formatNumber(util::selectorFromSignature(stringType->value()));
selector = formatNumber(util::selectorFromSignatureU256(stringType->value()));
else
{
// Used to reset the free memory pointer later.
@@ -1785,7 +1785,7 @@ void IRGeneratorForStatements::endVisit(MemberAccess const& _memberAccess)
""
);
define(IRVariable{_memberAccess}) << formatNumber(
util::selectorFromSignature(functionType.externalSignature())
util::selectorFromSignatureU256(functionType.externalSignature())
) << "\n";
}
else if (functionType.kind() == FunctionType::Kind::Event)
@@ -3234,7 +3234,7 @@ void IRGeneratorForStatements::handleCatch(TryStatement const& _tryStatement)
if (TryCatchClause const* errorClause = _tryStatement.errorClause())
{
appendCode() << "case " << selectorFromSignature32("Error(string)") << " {\n";
appendCode() << "case " << selectorFromSignatureU32("Error(string)") << " {\n";
setLocation(*errorClause);
string const dataVariable = m_context.newYulVariable();
appendCode() << "let " << dataVariable << " := " << m_utils.tryDecodeErrorMessageFunction() << "()\n";
@@ -3254,7 +3254,7 @@ void IRGeneratorForStatements::handleCatch(TryStatement const& _tryStatement)
}
if (TryCatchClause const* panicClause = _tryStatement.panicClause())
{
appendCode() << "case " << selectorFromSignature32("Panic(uint256)") << " {\n";
appendCode() << "case " << selectorFromSignatureU32("Panic(uint256)") << " {\n";
setLocation(*panicClause);
string const success = m_context.newYulVariable();
string const code = m_context.newYulVariable();
@@ -3317,7 +3317,7 @@ void IRGeneratorForStatements::revertWithError(
})");
templ("pos", m_context.newYulVariable());
templ("end", m_context.newYulVariable());
templ("hash", util::selectorFromSignature(_signature).str());
templ("hash", util::selectorFromSignatureU256(_signature).str());
templ("allocateUnbounded", m_utils.allocateUnboundedFunction());
vector<string> errorArgumentVars;
+1 -1
View File
@@ -1034,7 +1034,7 @@ Json::Value CompilerStack::interfaceSymbols(string const& _contractName) const
for (ErrorDefinition const* error: contractDefinition(_contractName).interfaceErrors())
{
string signature = error->functionType(true)->externalSignature();
interfaceSymbols["errors"][signature] = util::toHex(toCompactBigEndian(util::selectorFromSignature32(signature), 4));
interfaceSymbols["errors"][signature] = util::toHex(toCompactBigEndian(util::selectorFromSignatureU32(signature), 4));
}
for (EventDefinition const* event: ranges::concat_view(
+2 -1
View File
@@ -30,6 +30,7 @@
#include <libevmasm/ControlFlowGraph.h>
#include <libevmasm/KnownState.h>
#include <libevmasm/PathGasMeter.h>
#include <libsolutil/FunctionSelector.h>
#include <libsolutil/Keccak256.h>
#include <functional>
@@ -54,7 +55,7 @@ GasEstimator::GasConsumption GasEstimator::functionalEstimation(
ExpressionClasses& classes = state->expressionClasses();
using Id = ExpressionClasses::Id;
using Ids = vector<Id>;
Id hashValue = classes.find(u256(util::FixedHash<4>::Arith(util::FixedHash<4>(util::keccak256(_signature)))));
Id hashValue = classes.find(u256(util::selectorFromSignatureU32(_signature)));
Id calldata = classes.find(Instruction::CALLDATALOAD, Ids{classes.find(u256(0))});
if (!m_evmVersion.hasBitwiseShifting())
// div(calldataload(0), 1 << 224) equals to hashValue