2018-01-17 20:02:23 +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
|
2018-01-17 20:02:23 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2018-12-17 17:26:10 +00:00
|
|
|
#include <libsolidity/formal/SSAVariable.h>
|
2018-10-12 13:44:46 +00:00
|
|
|
#include <libsolidity/ast/Types.h>
|
2019-11-10 17:58:29 +00:00
|
|
|
#include <libsolidity/ast/TypeProvider.h>
|
2020-05-18 15:42:24 +00:00
|
|
|
|
|
|
|
#include <libsmtutil/SolverInterface.h>
|
2020-08-25 14:58:09 +00:00
|
|
|
|
|
|
|
#include <map>
|
2018-01-17 20:02:23 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::frontend::smt
|
2019-05-09 10:16:52 +00:00
|
|
|
{
|
2018-01-17 20:02:23 +00:00
|
|
|
|
2019-07-03 14:05:56 +00:00
|
|
|
class EncodingContext;
|
2018-10-12 13:44:46 +00:00
|
|
|
class Type;
|
2018-01-17 20:02:23 +00:00
|
|
|
|
|
|
|
/**
|
2018-10-22 08:29:03 +00:00
|
|
|
* This abstract class represents the symbolic version of a program variable.
|
2018-01-17 20:02:23 +00:00
|
|
|
*/
|
|
|
|
class SymbolicVariable
|
|
|
|
{
|
|
|
|
public:
|
2018-02-28 17:00:13 +00:00
|
|
|
SymbolicVariable(
|
2021-03-22 16:12:05 +00:00
|
|
|
frontend::Type const* _type,
|
|
|
|
frontend::Type const* _originalType,
|
2019-04-12 12:44:18 +00:00
|
|
|
std::string _uniqueName,
|
2019-07-03 14:05:56 +00:00
|
|
|
EncodingContext& _context
|
2019-04-12 12:44:18 +00:00
|
|
|
);
|
|
|
|
SymbolicVariable(
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::SortPointer _sort,
|
2019-04-12 12:44:18 +00:00
|
|
|
std::string _uniqueName,
|
2019-07-03 14:05:56 +00:00
|
|
|
EncodingContext& _context
|
2018-02-28 17:00:13 +00:00
|
|
|
);
|
2018-10-18 13:03:52 +00:00
|
|
|
|
2020-04-06 08:50:00 +00:00
|
|
|
SymbolicVariable(SymbolicVariable&&) = default;
|
|
|
|
|
2018-05-02 11:29:16 +00:00
|
|
|
virtual ~SymbolicVariable() = default;
|
2018-01-17 20:02:23 +00:00
|
|
|
|
2021-03-22 16:12:05 +00:00
|
|
|
virtual smtutil::Expression currentValue(frontend::Type const* _targetType = nullptr) const;
|
2018-10-25 14:00:09 +00:00
|
|
|
std::string currentName() const;
|
2020-06-02 13:42:46 +00:00
|
|
|
virtual smtutil::Expression valueAtIndex(unsigned _index) const;
|
|
|
|
virtual std::string nameAtIndex(unsigned _index) const;
|
2020-05-19 12:14:46 +00:00
|
|
|
virtual smtutil::Expression resetIndex();
|
|
|
|
virtual smtutil::Expression setIndex(unsigned _index);
|
|
|
|
virtual smtutil::Expression increaseIndex();
|
|
|
|
virtual smtutil::Expression operator()(std::vector<smtutil::Expression> /*_arguments*/) const
|
2018-12-10 10:34:29 +00:00
|
|
|
{
|
|
|
|
solAssert(false, "Function application to non-function.");
|
|
|
|
}
|
2018-10-15 15:32:17 +00:00
|
|
|
|
2018-10-22 16:19:11 +00:00
|
|
|
unsigned index() const { return m_ssa->index(); }
|
|
|
|
unsigned& index() { return m_ssa->index(); }
|
2018-01-17 20:02:23 +00:00
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::SortPointer const& sort() const { return m_sort; }
|
2021-03-22 16:12:05 +00:00
|
|
|
frontend::Type const* type() const { return m_type; }
|
|
|
|
frontend::Type const* originalType() const { return m_originalType; }
|
2018-01-17 20:02:23 +00:00
|
|
|
|
|
|
|
protected:
|
2018-10-22 16:19:11 +00:00
|
|
|
std::string uniqueSymbol(unsigned _index) const;
|
2018-01-17 20:02:23 +00:00
|
|
|
|
2019-04-12 12:44:18 +00:00
|
|
|
/// SMT sort.
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::SortPointer m_sort;
|
2019-04-12 12:44:18 +00:00
|
|
|
/// Solidity type, used for size and range in number types.
|
2021-03-22 16:12:05 +00:00
|
|
|
frontend::Type const* m_type;
|
2019-08-07 08:48:09 +00:00
|
|
|
/// Solidity original type, used for type conversion if necessary.
|
2021-03-22 16:12:05 +00:00
|
|
|
frontend::Type const* m_originalType;
|
2018-10-12 13:44:46 +00:00
|
|
|
std::string m_uniqueName;
|
2019-07-03 14:05:56 +00:00
|
|
|
EncodingContext& m_context;
|
2019-05-09 08:56:58 +00:00
|
|
|
std::unique_ptr<SSAVariable> m_ssa;
|
2018-01-17 20:02:23 +00:00
|
|
|
};
|
|
|
|
|
2018-10-22 08:29:03 +00:00
|
|
|
/**
|
|
|
|
* Specialization of SymbolicVariable for Bool
|
|
|
|
*/
|
|
|
|
class SymbolicBoolVariable: public SymbolicVariable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SymbolicBoolVariable(
|
2021-03-22 16:12:05 +00:00
|
|
|
frontend::Type const* _type,
|
2019-04-12 12:44:18 +00:00
|
|
|
std::string _uniqueName,
|
2019-07-03 14:05:56 +00:00
|
|
|
EncodingContext& _context
|
2018-10-22 08:29:03 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specialization of SymbolicVariable for Integers
|
|
|
|
*/
|
|
|
|
class SymbolicIntVariable: public SymbolicVariable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SymbolicIntVariable(
|
2021-03-22 16:12:05 +00:00
|
|
|
frontend::Type const* _type,
|
|
|
|
frontend::Type const* _originalType,
|
2019-04-12 12:44:18 +00:00
|
|
|
std::string _uniqueName,
|
2019-07-03 14:05:56 +00:00
|
|
|
EncodingContext& _context
|
2018-10-22 08:29:03 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specialization of SymbolicVariable for Address
|
|
|
|
*/
|
|
|
|
class SymbolicAddressVariable: public SymbolicIntVariable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SymbolicAddressVariable(
|
2019-04-12 12:44:18 +00:00
|
|
|
std::string _uniqueName,
|
2019-07-03 14:05:56 +00:00
|
|
|
EncodingContext& _context
|
2018-10-22 08:29:03 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specialization of SymbolicVariable for FixedBytes
|
|
|
|
*/
|
|
|
|
class SymbolicFixedBytesVariable: public SymbolicIntVariable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SymbolicFixedBytesVariable(
|
2021-03-22 16:12:05 +00:00
|
|
|
frontend::Type const* _originalType,
|
2018-10-22 08:29:03 +00:00
|
|
|
unsigned _numBytes,
|
2019-04-12 12:44:18 +00:00
|
|
|
std::string _uniqueName,
|
2019-07-03 14:05:56 +00:00
|
|
|
EncodingContext& _context
|
2018-10-22 08:29:03 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2018-12-10 10:34:29 +00:00
|
|
|
/**
|
2019-11-10 17:58:29 +00:00
|
|
|
* Specialization of SymbolicVariable for FunctionType.
|
|
|
|
* Besides containing a symbolic function declaration,
|
|
|
|
* it also has an integer used as abstraction.
|
|
|
|
* By default, the abstract representation is used when
|
|
|
|
* values are requested, and the function declaration is
|
|
|
|
* used when operator() is applied over arguments.
|
2018-12-10 10:34:29 +00:00
|
|
|
*/
|
|
|
|
class SymbolicFunctionVariable: public SymbolicVariable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SymbolicFunctionVariable(
|
2021-03-22 16:12:05 +00:00
|
|
|
frontend::Type const* _type,
|
2019-04-12 12:44:18 +00:00
|
|
|
std::string _uniqueName,
|
2019-07-03 14:05:56 +00:00
|
|
|
EncodingContext& _context
|
2018-12-10 10:34:29 +00:00
|
|
|
);
|
2019-07-03 14:16:42 +00:00
|
|
|
SymbolicFunctionVariable(
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::SortPointer _sort,
|
2019-07-03 14:16:42 +00:00
|
|
|
std::string _uniqueName,
|
|
|
|
EncodingContext& _context
|
|
|
|
);
|
2018-12-10 10:34:29 +00:00
|
|
|
|
2021-03-22 16:12:05 +00:00
|
|
|
smtutil::Expression currentValue(frontend::Type const* _targetType = nullptr) const override;
|
2019-11-10 17:58:29 +00:00
|
|
|
|
|
|
|
// Explicit request the function declaration.
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::Expression currentFunctionValue() const;
|
2019-11-10 17:58:29 +00:00
|
|
|
|
2020-06-02 13:42:46 +00:00
|
|
|
smtutil::Expression valueAtIndex(unsigned _index) const override;
|
2019-11-10 17:58:29 +00:00
|
|
|
|
|
|
|
// Explicit request the function declaration.
|
2020-06-02 13:42:46 +00:00
|
|
|
smtutil::Expression functionValueAtIndex(unsigned _index) const;
|
2019-11-10 17:58:29 +00:00
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::Expression resetIndex() override;
|
|
|
|
smtutil::Expression setIndex(unsigned _index) override;
|
|
|
|
smtutil::Expression increaseIndex() override;
|
2019-11-10 17:58:29 +00:00
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::Expression operator()(std::vector<smtutil::Expression> _arguments) const override;
|
2018-12-10 10:34:29 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
/// Creates a new function declaration.
|
|
|
|
void resetDeclaration();
|
|
|
|
|
|
|
|
/// Stores the current function declaration.
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::Expression m_declaration;
|
2019-11-10 17:58:29 +00:00
|
|
|
|
|
|
|
/// Abstract representation.
|
|
|
|
SymbolicIntVariable m_abstract{
|
|
|
|
TypeProvider::uint256(),
|
|
|
|
TypeProvider::uint256(),
|
|
|
|
m_uniqueName + "_abstract",
|
|
|
|
m_context
|
|
|
|
};
|
2018-12-10 10:34:29 +00:00
|
|
|
};
|
|
|
|
|
2018-11-09 16:06:30 +00:00
|
|
|
/**
|
2020-04-14 09:09:38 +00:00
|
|
|
* Specialization of SymbolicVariable for Enum
|
2018-11-09 16:06:30 +00:00
|
|
|
*/
|
2020-04-14 09:09:38 +00:00
|
|
|
class SymbolicEnumVariable: public SymbolicVariable
|
2018-11-09 16:06:30 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-04-14 09:09:38 +00:00
|
|
|
SymbolicEnumVariable(
|
2021-03-22 16:12:05 +00:00
|
|
|
frontend::Type const* _type,
|
2019-04-12 12:44:18 +00:00
|
|
|
std::string _uniqueName,
|
2019-07-03 14:05:56 +00:00
|
|
|
EncodingContext& _context
|
2018-11-09 16:06:30 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-02-20 11:34:52 +00:00
|
|
|
/**
|
2020-04-14 09:09:38 +00:00
|
|
|
* Specialization of SymbolicVariable for Tuple
|
2019-02-20 11:34:52 +00:00
|
|
|
*/
|
2020-04-14 09:09:38 +00:00
|
|
|
class SymbolicTupleVariable: public SymbolicVariable
|
2019-02-20 11:34:52 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-04-14 09:09:38 +00:00
|
|
|
SymbolicTupleVariable(
|
2021-03-22 16:12:05 +00:00
|
|
|
frontend::Type const* _type,
|
2019-04-12 12:44:18 +00:00
|
|
|
std::string _uniqueName,
|
2019-07-03 14:05:56 +00:00
|
|
|
EncodingContext& _context
|
2019-02-20 11:34:52 +00:00
|
|
|
);
|
2020-04-14 09:09:38 +00:00
|
|
|
SymbolicTupleVariable(
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::SortPointer _sort,
|
2020-04-06 08:50:00 +00:00
|
|
|
std::string _uniqueName,
|
|
|
|
EncodingContext& _context
|
|
|
|
);
|
|
|
|
|
2021-03-22 16:12:05 +00:00
|
|
|
smtutil::Expression currentValue(frontend::Type const* _targetType = nullptr) const override;
|
2020-10-21 21:04:34 +00:00
|
|
|
|
|
|
|
std::vector<smtutil::SortPointer> const& components() const;
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::Expression component(
|
2020-04-14 09:09:38 +00:00
|
|
|
size_t _index,
|
2021-03-22 16:12:05 +00:00
|
|
|
frontend::Type const* _fromType = nullptr,
|
|
|
|
frontend::Type const* _toType = nullptr
|
2020-10-21 21:04:34 +00:00
|
|
|
) const;
|
2019-03-06 00:10:43 +00:00
|
|
|
};
|
|
|
|
|
2019-04-26 12:57:29 +00:00
|
|
|
/**
|
2020-04-14 09:09:38 +00:00
|
|
|
* Specialization of SymbolicVariable for Array
|
2019-04-26 12:57:29 +00:00
|
|
|
*/
|
2020-04-14 09:09:38 +00:00
|
|
|
class SymbolicArrayVariable: public SymbolicVariable
|
2019-04-26 12:57:29 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-04-14 09:09:38 +00:00
|
|
|
SymbolicArrayVariable(
|
2021-03-22 16:12:05 +00:00
|
|
|
frontend::Type const* _type,
|
|
|
|
frontend::Type const* _originalTtype,
|
2019-04-26 12:57:29 +00:00
|
|
|
std::string _uniqueName,
|
2019-07-03 14:05:56 +00:00
|
|
|
EncodingContext& _context
|
2019-04-26 12:57:29 +00:00
|
|
|
);
|
2020-04-14 09:09:38 +00:00
|
|
|
SymbolicArrayVariable(
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::SortPointer _sort,
|
2020-04-03 13:10:16 +00:00
|
|
|
std::string _uniqueName,
|
|
|
|
EncodingContext& _context
|
|
|
|
);
|
2019-04-26 12:57:29 +00:00
|
|
|
|
2020-04-14 09:09:38 +00:00
|
|
|
SymbolicArrayVariable(SymbolicArrayVariable&&) = default;
|
|
|
|
|
2021-03-22 16:12:05 +00:00
|
|
|
smtutil::Expression currentValue(frontend::Type const* _targetType = nullptr) const override;
|
2020-06-02 13:42:46 +00:00
|
|
|
smtutil::Expression valueAtIndex(unsigned _index) const override;
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::Expression resetIndex() override { SymbolicVariable::resetIndex(); return m_pair.resetIndex(); }
|
|
|
|
smtutil::Expression setIndex(unsigned _index) override { SymbolicVariable::setIndex(_index); return m_pair.setIndex(_index); }
|
|
|
|
smtutil::Expression increaseIndex() override { SymbolicVariable::increaseIndex(); return m_pair.increaseIndex(); }
|
2020-10-26 16:16:27 +00:00
|
|
|
smtutil::Expression elements() const;
|
|
|
|
smtutil::Expression length() const;
|
2020-04-14 09:09:38 +00:00
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::SortPointer tupleSort() { return m_pair.sort(); }
|
2020-04-14 09:09:38 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
SymbolicTupleVariable m_pair;
|
2019-04-26 12:57:29 +00:00
|
|
|
};
|
|
|
|
|
2020-08-25 14:58:09 +00:00
|
|
|
/**
|
|
|
|
* Specialization of SymbolicVariable for Struct.
|
|
|
|
*/
|
|
|
|
class SymbolicStructVariable: public SymbolicVariable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SymbolicStructVariable(
|
2021-03-22 16:12:05 +00:00
|
|
|
frontend::Type const* _type,
|
2020-08-25 14:58:09 +00:00
|
|
|
std::string _uniqueName,
|
|
|
|
EncodingContext& _context
|
|
|
|
);
|
|
|
|
|
|
|
|
/// @returns the symbolic expression representing _member.
|
2020-11-12 09:09:43 +00:00
|
|
|
smtutil::Expression member(std::string const& _member) const;
|
2020-08-25 14:58:09 +00:00
|
|
|
|
|
|
|
/// @returns the symbolic expression representing this struct
|
|
|
|
/// with field _member updated.
|
|
|
|
smtutil::Expression assignMember(std::string const& _member, smtutil::Expression const& _memberValue);
|
|
|
|
|
2020-11-12 09:09:43 +00:00
|
|
|
/// @returns the symbolic expression representing this struct
|
|
|
|
/// with all fields updated with the given values.
|
|
|
|
smtutil::Expression assignAllMembers(std::vector<smtutil::Expression> const& _memberValues);
|
|
|
|
|
2020-08-25 14:58:09 +00:00
|
|
|
private:
|
|
|
|
std::map<std::string, unsigned> m_memberIndices;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-01-17 20:02:23 +00:00
|
|
|
}
|