2015-04-29 16:16:05 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2015-04-29 16:16:05 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2015-04-29 16:16:05 +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-04-29 16:16:05 +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-04-29 16:16:05 +00:00
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2015-04-29 16:16:05 +00:00
|
|
|
/**
|
|
|
|
* @file KnownState.h
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2015
|
|
|
|
* Contains knowledge about the state of the virtual machine at a specific instruction.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-04-01 03:04:29 +00:00
|
|
|
#include <utility>
|
2015-04-29 16:16:05 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
#include <tuple>
|
2015-05-04 08:15:41 +00:00
|
|
|
#include <memory>
|
2015-04-29 16:16:05 +00:00
|
|
|
#include <ostream>
|
2018-07-21 22:08:47 +00:00
|
|
|
|
|
|
|
#if defined(__clang__)
|
|
|
|
#pragma clang diagnostic push
|
2015-08-06 13:56:00 +00:00
|
|
|
#pragma clang diagnostic ignored "-Wredeclared-class-member"
|
2018-07-21 22:08:47 +00:00
|
|
|
#endif // defined(__clang__)
|
|
|
|
|
2015-05-12 19:27:04 +00:00
|
|
|
#include <boost/bimap.hpp>
|
2018-07-21 22:08:47 +00:00
|
|
|
|
|
|
|
#if defined(__clang__)
|
|
|
|
#pragma clang diagnostic pop
|
|
|
|
#endif // defined(__clang__)
|
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/CommonIO.h>
|
|
|
|
#include <libsolutil/Exceptions.h>
|
2015-04-29 16:16:05 +00:00
|
|
|
#include <libevmasm/ExpressionClasses.h>
|
|
|
|
#include <libevmasm/SemanticInformation.h>
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::langutil
|
2018-11-14 16:11:55 +00:00
|
|
|
{
|
|
|
|
struct SourceLocation;
|
|
|
|
}
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::evmasm
|
2015-04-29 16:16:05 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
class AssemblyItem;
|
|
|
|
using AssemblyItems = std::vector<AssemblyItem>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class to infer and store knowledge about the state of the virtual machine at a specific
|
|
|
|
* instruction.
|
|
|
|
*
|
|
|
|
* The general workings are that for each assembly item that is fed, an equivalence class is
|
|
|
|
* derived from the operation and the equivalence class of its arguments. DUPi, SWAPi and some
|
|
|
|
* arithmetic instructions are used to infer equivalences while these classes are determined.
|
|
|
|
*/
|
|
|
|
class KnownState
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using Id = ExpressionClasses::Id;
|
|
|
|
struct StoreOperation
|
|
|
|
{
|
|
|
|
enum Target { Invalid, Memory, Storage };
|
2018-12-12 13:51:22 +00:00
|
|
|
|
2015-04-29 16:16:05 +00:00
|
|
|
bool isValid() const { return target != Invalid; }
|
2018-12-12 13:51:22 +00:00
|
|
|
|
|
|
|
Target target{Invalid};
|
|
|
|
Id slot{std::numeric_limits<Id>::max()};
|
|
|
|
unsigned sequenceNumber{std::numeric_limits<unsigned>::max()};
|
|
|
|
Id expression{std::numeric_limits<Id>::max()};
|
2015-04-29 16:16:05 +00:00
|
|
|
};
|
|
|
|
|
2015-05-04 08:15:41 +00:00
|
|
|
explicit KnownState(
|
|
|
|
std::shared_ptr<ExpressionClasses> _expressionClasses = std::make_shared<ExpressionClasses>()
|
2020-04-01 03:04:29 +00:00
|
|
|
): m_expressionClasses(std::move(_expressionClasses))
|
2015-05-04 08:15:41 +00:00
|
|
|
{
|
|
|
|
}
|
2015-04-29 16:16:05 +00:00
|
|
|
|
|
|
|
/// Streams debugging information to @a _out.
|
2015-05-04 08:15:41 +00:00
|
|
|
std::ostream& stream(std::ostream& _out) const;
|
2015-04-29 16:16:05 +00:00
|
|
|
|
|
|
|
/// Feeds the item into the system for analysis.
|
|
|
|
/// @returns a possible store operation
|
|
|
|
StoreOperation feedItem(AssemblyItem const& _item, bool _copyItem = false);
|
|
|
|
|
|
|
|
/// Resets any knowledge about storage.
|
|
|
|
void resetStorage() { m_storageContent.clear(); }
|
|
|
|
/// Resets any knowledge about storage.
|
|
|
|
void resetMemory() { m_memoryContent.clear(); }
|
|
|
|
/// Resets any knowledge about the current stack.
|
|
|
|
void resetStack() { m_stackElements.clear(); m_stackHeight = 0; }
|
|
|
|
/// Resets any knowledge.
|
|
|
|
void reset() { resetStorage(); resetMemory(); resetStack(); }
|
|
|
|
|
2015-06-06 13:31:22 +00:00
|
|
|
unsigned sequenceNumber() const { return m_sequenceNumber; }
|
2015-05-04 08:15:41 +00:00
|
|
|
|
|
|
|
/// Replaces the state by the intersection with _other, i.e. only equal knowledge is retained.
|
|
|
|
/// If the stack heighht is different, the smaller one is used and the stack is compared
|
|
|
|
/// relatively.
|
2016-01-15 15:26:12 +00:00
|
|
|
/// @param _combineSequenceNumbers if true, sets the sequence number to the maximum of both
|
|
|
|
void reduceToCommonKnowledge(KnownState const& _other, bool _combineSequenceNumbers);
|
2015-05-04 08:15:41 +00:00
|
|
|
|
|
|
|
/// @returns a shared pointer to a copy of this state.
|
|
|
|
std::shared_ptr<KnownState> copy() const { return std::make_shared<KnownState>(*this); }
|
|
|
|
|
|
|
|
/// @returns true if the knowledge about the state of both objects is (known to be) equal.
|
|
|
|
bool operator==(KnownState const& _other) const;
|
|
|
|
|
2015-04-29 16:16:05 +00:00
|
|
|
/// Retrieves the current equivalence class fo the given stack element (or generates a new
|
|
|
|
/// one if it does not exist yet).
|
2018-11-14 16:11:55 +00:00
|
|
|
Id stackElement(int _stackHeight, langutil::SourceLocation const& _location);
|
2015-05-19 22:27:07 +00:00
|
|
|
/// @returns the stackElement relative to the current stack height.
|
2018-11-14 16:11:55 +00:00
|
|
|
Id relativeStackElement(int _stackOffset, langutil::SourceLocation const& _location = {});
|
2015-05-12 19:27:04 +00:00
|
|
|
|
|
|
|
/// @returns its set of tags if the given expression class is a known tag union; returns a set
|
|
|
|
/// containing the tag if it is a PushTag expression and the empty set otherwise.
|
|
|
|
std::set<u256> tagsInExpression(Id _expressionId);
|
|
|
|
/// During analysis, different tags on the stack are partially treated as the same class.
|
|
|
|
/// This removes such classes not to confuse later analyzers.
|
|
|
|
void clearTagUnions();
|
2015-04-29 16:16:05 +00:00
|
|
|
|
|
|
|
int stackHeight() const { return m_stackHeight; }
|
|
|
|
std::map<int, Id> const& stackElements() const { return m_stackElements; }
|
|
|
|
ExpressionClasses& expressionClasses() const { return *m_expressionClasses; }
|
|
|
|
|
2015-05-19 22:27:07 +00:00
|
|
|
std::map<Id, Id> const& storageContent() const { return m_storageContent; }
|
|
|
|
|
2015-04-29 16:16:05 +00:00
|
|
|
private:
|
|
|
|
/// Assigns a new equivalence class to the next sequence number of the given stack element.
|
|
|
|
void setStackElement(int _stackHeight, Id _class);
|
|
|
|
/// Swaps the given stack elements in their next sequence number.
|
2018-11-14 16:11:55 +00:00
|
|
|
void swapStackElements(int _stackHeightA, int _stackHeightB, langutil::SourceLocation const& _location);
|
2015-04-29 16:16:05 +00:00
|
|
|
|
|
|
|
/// Increments the sequence number, deletes all storage information that might be overwritten
|
|
|
|
/// and stores the new value at the given slot.
|
|
|
|
/// @returns the store operation, which might be invalid if storage was not modified
|
2018-11-14 16:11:55 +00:00
|
|
|
StoreOperation storeInStorage(Id _slot, Id _value, langutil::SourceLocation const& _location);
|
2015-04-29 16:16:05 +00:00
|
|
|
/// Retrieves the current value at the given slot in storage or creates a new special sload class.
|
2018-11-14 16:11:55 +00:00
|
|
|
Id loadFromStorage(Id _slot, langutil::SourceLocation const& _location);
|
2015-04-29 16:16:05 +00:00
|
|
|
/// Increments the sequence number, deletes all memory information that might be overwritten
|
|
|
|
/// and stores the new value at the given slot.
|
|
|
|
/// @returns the store operation, which might be invalid if memory was not modified
|
2018-11-14 16:11:55 +00:00
|
|
|
StoreOperation storeInMemory(Id _slot, Id _value, langutil::SourceLocation const& _location);
|
2015-04-29 16:16:05 +00:00
|
|
|
/// Retrieves the current value at the given slot in memory or creates a new special mload class.
|
2018-11-14 16:11:55 +00:00
|
|
|
Id loadFromMemory(Id _slot, langutil::SourceLocation const& _location);
|
2017-05-10 07:48:00 +00:00
|
|
|
/// Finds or creates a new expression that applies the Keccak-256 hash function to the contents in memory.
|
2018-11-14 16:11:55 +00:00
|
|
|
Id applyKeccak256(Id _start, Id _length, langutil::SourceLocation const& _location);
|
2015-04-29 16:16:05 +00:00
|
|
|
|
2015-05-12 19:27:04 +00:00
|
|
|
/// @returns a new or already used Id representing the given set of tags.
|
|
|
|
Id tagUnion(std::set<u256> _tags);
|
|
|
|
|
2015-04-29 16:16:05 +00:00
|
|
|
/// Current stack height, can be negative.
|
|
|
|
int m_stackHeight = 0;
|
|
|
|
/// Current stack layout, mapping stack height -> equivalence class
|
|
|
|
std::map<int, Id> m_stackElements;
|
|
|
|
/// Current sequence number, this is incremented with each modification to storage or memory.
|
|
|
|
unsigned m_sequenceNumber = 1;
|
|
|
|
/// Knowledge about storage content.
|
|
|
|
std::map<Id, Id> m_storageContent;
|
|
|
|
/// Knowledge about memory content. Keys are memory addresses, note that the values overlap
|
|
|
|
/// and are not contained here if they are not completely known.
|
|
|
|
std::map<Id, Id> m_memoryContent;
|
2017-05-10 07:48:00 +00:00
|
|
|
/// Keeps record of all Keccak-256 hashes that are computed.
|
|
|
|
std::map<std::vector<Id>, Id> m_knownKeccak256Hashes;
|
2015-04-29 16:16:05 +00:00
|
|
|
/// Structure containing the classes of equivalent expressions.
|
|
|
|
std::shared_ptr<ExpressionClasses> m_expressionClasses;
|
2015-05-12 19:27:04 +00:00
|
|
|
/// Container for unions of tags stored on the stack.
|
|
|
|
boost::bimap<Id, std::set<u256>> m_tagUnions;
|
2015-04-29 16:16:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|