mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Yul interpreter: Limit nesting level
This commit is contained in:
@@ -247,6 +247,7 @@ void Interpreter::incrementStep()
|
||||
|
||||
void ExpressionEvaluator::operator()(Literal const& _literal)
|
||||
{
|
||||
incrementStep();
|
||||
static YulString const trueString("true");
|
||||
static YulString const falseString("false");
|
||||
|
||||
@@ -256,6 +257,7 @@ void ExpressionEvaluator::operator()(Literal const& _literal)
|
||||
void ExpressionEvaluator::operator()(Identifier const& _identifier)
|
||||
{
|
||||
solAssert(m_variables.count(_identifier.name), "");
|
||||
incrementStep();
|
||||
setValue(m_variables.at(_identifier.name));
|
||||
}
|
||||
|
||||
@@ -326,6 +328,7 @@ void ExpressionEvaluator::evaluateArgs(
|
||||
vector<optional<LiteralKind>> const* _literalArguments
|
||||
)
|
||||
{
|
||||
incrementStep();
|
||||
vector<u256> values;
|
||||
size_t i = 0;
|
||||
/// Function arguments are evaluated in reverse.
|
||||
@@ -341,3 +344,13 @@ void ExpressionEvaluator::evaluateArgs(
|
||||
m_values = std::move(values);
|
||||
std::reverse(m_values.begin(), m_values.end());
|
||||
}
|
||||
|
||||
void ExpressionEvaluator::incrementStep()
|
||||
{
|
||||
m_nestingLevel++;
|
||||
if (m_state.maxExprNesting > 0 && m_nestingLevel > m_state.maxExprNesting)
|
||||
{
|
||||
m_state.trace.emplace_back("Maximum expression nesting level reached.");
|
||||
throw ExpressionNestingLimitReached();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,6 +55,10 @@ class TraceLimitReached: public InterpreterTerminatedGeneric
|
||||
{
|
||||
};
|
||||
|
||||
class ExpressionNestingLimitReached: public InterpreterTerminatedGeneric
|
||||
{
|
||||
};
|
||||
|
||||
enum class ControlFlowState
|
||||
{
|
||||
Default,
|
||||
@@ -92,6 +96,7 @@ struct InterpreterState
|
||||
size_t maxTraceSize = 0;
|
||||
size_t maxSteps = 0;
|
||||
size_t numSteps = 0;
|
||||
size_t maxExprNesting = 0;
|
||||
ControlFlowState controlFlowState = ControlFlowState::Default;
|
||||
|
||||
void dumpTraceAndState(std::ostream& _out) const;
|
||||
@@ -202,6 +207,11 @@ private:
|
||||
std::vector<std::optional<LiteralKind>> const* _literalArguments
|
||||
);
|
||||
|
||||
/// Increment evaluation count, throwing exception if the
|
||||
/// nesting level is beyond the upper bound configured in
|
||||
/// the interpreter state.
|
||||
void incrementStep();
|
||||
|
||||
InterpreterState& m_state;
|
||||
Dialect const& m_dialect;
|
||||
/// Values of variables.
|
||||
@@ -209,6 +219,8 @@ private:
|
||||
Scope& m_scope;
|
||||
/// Current value of the expression
|
||||
std::vector<u256> m_values;
|
||||
/// Current expression nesting level
|
||||
unsigned m_nestingLevel = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user