2019-05-21 13:52:15 +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
|
2019-05-21 13:52:15 +00:00
|
|
|
/**
|
|
|
|
* Class that can answer questions about values of variables and their relations.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-10-29 14:00:27 +00:00
|
|
|
#include <libyul/ASTForward.h>
|
2019-05-21 13:52:15 +00:00
|
|
|
#include <libyul/YulString.h>
|
2020-10-29 14:00:27 +00:00
|
|
|
|
2021-05-05 16:02:35 +00:00
|
|
|
#include <libsolutil/Common.h>
|
2021-09-16 14:33:28 +00:00
|
|
|
#include <libsolutil/Numeric.h>
|
2021-05-05 16:02:35 +00:00
|
|
|
|
2019-05-21 13:52:15 +00:00
|
|
|
#include <map>
|
2022-03-10 17:25:00 +00:00
|
|
|
#include <functional>
|
2019-05-21 13:52:15 +00:00
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::yul
|
2019-05-21 13:52:15 +00:00
|
|
|
{
|
|
|
|
|
2019-05-21 16:38:05 +00:00
|
|
|
struct Dialect;
|
2019-11-28 13:22:17 +00:00
|
|
|
struct AssignedValue;
|
2019-05-21 13:52:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class that can answer questions about values of variables and their relations.
|
2022-11-10 10:49:40 +00:00
|
|
|
*
|
|
|
|
* Requires a callback that returns the current value of the variable.
|
|
|
|
* The value can change any time during the lifetime of the KnowledgeBase,
|
|
|
|
* it will update its internal data structure accordingly.
|
|
|
|
*
|
|
|
|
* This means that the code the KnowledgeBase is used on does not need to be in SSA
|
|
|
|
* form.
|
|
|
|
* The only requirement is that the assigned values are movable expressions.
|
|
|
|
*
|
2022-11-10 13:22:40 +00:00
|
|
|
* There is a constructor to provide all SSA values right at the beginning.
|
|
|
|
* If you use this, the KnowledgeBase will be slightly more efficient.
|
|
|
|
*
|
2022-11-10 10:49:40 +00:00
|
|
|
* Internally, tries to find groups of variables that have a mutual constant
|
|
|
|
* difference and stores these differences always relative to a specific
|
|
|
|
* representative variable of the group.
|
|
|
|
*
|
|
|
|
* There is a special group which is the constant values. Those use the
|
|
|
|
* empty YulString as representative "variable".
|
2019-05-21 13:52:15 +00:00
|
|
|
*/
|
|
|
|
class KnowledgeBase
|
|
|
|
{
|
|
|
|
public:
|
2022-11-10 13:22:40 +00:00
|
|
|
/// Constructor for arbitrary value callback that allows for variable values
|
|
|
|
/// to change in between calls to functions of this class.
|
2022-11-10 10:49:40 +00:00
|
|
|
KnowledgeBase(std::function<AssignedValue const*(YulString)> _variableValues):
|
2022-03-10 17:25:00 +00:00
|
|
|
m_variableValues(std::move(_variableValues))
|
2019-05-21 13:52:15 +00:00
|
|
|
{}
|
2022-11-10 13:22:40 +00:00
|
|
|
/// Constructor to use if source code is in SSA form and values are constant.
|
|
|
|
KnowledgeBase(std::map<YulString, AssignedValue> const& _ssaValues);
|
2019-05-21 13:52:15 +00:00
|
|
|
|
2019-05-21 16:38:05 +00:00
|
|
|
bool knownToBeDifferent(YulString _a, YulString _b);
|
2021-05-05 16:02:35 +00:00
|
|
|
std::optional<u256> differenceIfKnownConstant(YulString _a, YulString _b);
|
2019-05-21 16:38:05 +00:00
|
|
|
bool knownToBeDifferentByAtLeast32(YulString _a, YulString _b);
|
2021-05-05 16:02:35 +00:00
|
|
|
bool knownToBeZero(YulString _a);
|
|
|
|
std::optional<u256> valueIfKnownConstant(YulString _a);
|
2022-11-10 10:49:40 +00:00
|
|
|
std::optional<u256> valueIfKnownConstant(Expression const& _expression);
|
2019-05-21 13:52:15 +00:00
|
|
|
|
|
|
|
private:
|
2022-11-10 10:49:40 +00:00
|
|
|
/**
|
|
|
|
* Constant offset relative to a reference variable, or absolute constant if the
|
|
|
|
* reference variable is the empty YulString.
|
|
|
|
*/
|
|
|
|
struct VariableOffset
|
|
|
|
{
|
|
|
|
YulString reference;
|
|
|
|
u256 offset;
|
2023-02-09 10:21:37 +00:00
|
|
|
|
|
|
|
bool isAbsolute() const
|
|
|
|
{
|
|
|
|
return reference.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<u256> absoluteValue() const
|
|
|
|
{
|
|
|
|
if (isAbsolute())
|
|
|
|
return offset;
|
|
|
|
else
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2022-11-10 10:49:40 +00:00
|
|
|
};
|
2019-05-21 16:38:05 +00:00
|
|
|
|
2022-11-10 10:49:40 +00:00
|
|
|
VariableOffset explore(YulString _var);
|
|
|
|
std::optional<VariableOffset> explore(Expression const& _value);
|
|
|
|
|
|
|
|
/// Retrieves the current value of a variable and potentially resets the variable if it is not up to date.
|
|
|
|
Expression const* valueOf(YulString _var);
|
|
|
|
|
|
|
|
/// Resets all information about the variable and removes it from its group,
|
|
|
|
/// potentially finding a new representative.
|
|
|
|
void reset(YulString _var);
|
|
|
|
|
|
|
|
VariableOffset setOffset(YulString _variable, VariableOffset _value);
|
|
|
|
|
2022-11-10 13:22:40 +00:00
|
|
|
/// If true, we can assume that variable values never change and skip some steps.
|
|
|
|
bool m_valuesAreSSA = false;
|
2022-11-10 10:49:40 +00:00
|
|
|
/// Callback to retrieve the current value of a variable.
|
2022-03-10 17:25:00 +00:00
|
|
|
std::function<AssignedValue const*(YulString)> m_variableValues;
|
2022-11-10 10:49:40 +00:00
|
|
|
|
|
|
|
/// Offsets for each variable to one representative per group.
|
|
|
|
/// The empty string is the representative of the constant value zero.
|
|
|
|
std::map<YulString, VariableOffset> m_offsets;
|
|
|
|
/// Last known value of each variable we queried.
|
|
|
|
std::map<YulString, Expression const*> m_lastKnownValue;
|
|
|
|
/// For each representative, variables that use it to offset from.
|
|
|
|
std::map<YulString, std::set<YulString>> m_groupMembers;
|
2019-05-21 13:52:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|