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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <libsolidity/formal/SolverInterface.h>
|
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>
|
2018-01-17 20:02:23 +00:00
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace dev
|
|
|
|
{
|
|
|
|
namespace solidity
|
|
|
|
{
|
2019-05-09 10:16:52 +00:00
|
|
|
namespace smt
|
|
|
|
{
|
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(
|
2019-05-09 10:16:52 +00:00
|
|
|
solidity::TypePointer _type,
|
2019-08-07 08:48:09 +00:00
|
|
|
solidity::TypePointer _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(
|
2019-05-09 10:16:52 +00:00
|
|
|
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
|
|
|
|
2018-05-02 11:29:16 +00:00
|
|
|
virtual ~SymbolicVariable() = default;
|
2018-01-17 20:02:23 +00:00
|
|
|
|
2019-08-07 08:48:09 +00:00
|
|
|
virtual Expression currentValue(solidity::TypePointer const& _targetType = TypePointer{}) const;
|
2018-10-25 14:00:09 +00:00
|
|
|
std::string currentName() const;
|
2019-05-09 10:16:52 +00:00
|
|
|
virtual Expression valueAtIndex(int _index) const;
|
2019-07-03 14:16:42 +00:00
|
|
|
virtual std::string nameAtIndex(int _index) const;
|
2019-07-25 12:10:09 +00:00
|
|
|
virtual Expression resetIndex();
|
2019-05-09 10:16:52 +00:00
|
|
|
virtual Expression increaseIndex();
|
|
|
|
virtual Expression operator()(std::vector<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
|
|
|
|
2019-07-03 14:16:42 +00:00
|
|
|
SortPointer const& sort() const { return m_sort; }
|
2019-05-09 10:16:52 +00:00
|
|
|
solidity::TypePointer const& type() const { return m_type; }
|
2019-08-07 08:48:09 +00:00
|
|
|
solidity::TypePointer 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.
|
2019-05-09 10:16:52 +00:00
|
|
|
SortPointer m_sort;
|
2019-04-12 12:44:18 +00:00
|
|
|
/// Solidity type, used for size and range in number types.
|
2019-05-09 10:16:52 +00:00
|
|
|
solidity::TypePointer m_type;
|
2019-08-07 08:48:09 +00:00
|
|
|
/// Solidity original type, used for type conversion if necessary.
|
|
|
|
solidity::TypePointer 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(
|
2019-05-09 10:16:52 +00:00
|
|
|
solidity::TypePointer _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(
|
2019-05-09 10:16:52 +00:00
|
|
|
solidity::TypePointer _type,
|
2019-08-07 08:48:09 +00:00
|
|
|
solidity::TypePointer _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(
|
2019-08-07 08:48:09 +00:00
|
|
|
solidity::TypePointer _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(
|
2019-05-09 10:16:52 +00:00
|
|
|
solidity::TypePointer _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(
|
|
|
|
SortPointer _sort,
|
|
|
|
std::string _uniqueName,
|
|
|
|
EncodingContext& _context
|
|
|
|
);
|
2018-12-10 10:34:29 +00:00
|
|
|
|
2019-11-10 17:58:29 +00:00
|
|
|
Expression currentValue(solidity::TypePointer const& _targetType = TypePointer{}) const override;
|
|
|
|
|
|
|
|
// Explicit request the function declaration.
|
|
|
|
Expression currentFunctionValue() const;
|
|
|
|
|
|
|
|
Expression valueAtIndex(int _index) const override;
|
|
|
|
|
|
|
|
// Explicit request the function declaration.
|
|
|
|
Expression functionValueAtIndex(int _index) const;
|
|
|
|
|
|
|
|
Expression resetIndex() override;
|
|
|
|
Expression increaseIndex() override;
|
|
|
|
|
|
|
|
Expression operator()(std::vector<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.
|
2019-05-09 10:16:52 +00:00
|
|
|
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
|
|
|
/**
|
|
|
|
* Specialization of SymbolicVariable for Mapping
|
|
|
|
*/
|
|
|
|
class SymbolicMappingVariable: public SymbolicVariable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SymbolicMappingVariable(
|
2019-05-09 10:16:52 +00:00
|
|
|
solidity::TypePointer _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
|
|
|
/**
|
|
|
|
* Specialization of SymbolicVariable for Array
|
|
|
|
*/
|
|
|
|
class SymbolicArrayVariable: public SymbolicVariable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SymbolicArrayVariable(
|
2019-05-09 10:16:52 +00:00
|
|
|
solidity::TypePointer _type,
|
2019-08-07 08:48:09 +00:00
|
|
|
solidity::TypePointer _originalTtype,
|
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
|
|
|
);
|
2019-08-07 08:48:09 +00:00
|
|
|
|
|
|
|
Expression currentValue(solidity::TypePointer const& _targetType = TypePointer{}) const override;
|
2019-02-20 11:34:52 +00:00
|
|
|
};
|
|
|
|
|
2019-03-06 00:10:43 +00:00
|
|
|
/**
|
|
|
|
* Specialization of SymbolicVariable for Enum
|
|
|
|
*/
|
|
|
|
class SymbolicEnumVariable: public SymbolicVariable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SymbolicEnumVariable(
|
2019-05-09 10:16:52 +00:00
|
|
|
solidity::TypePointer _type,
|
2019-04-12 12:44:18 +00:00
|
|
|
std::string _uniqueName,
|
2019-07-03 14:05:56 +00:00
|
|
|
EncodingContext& _context
|
2019-03-06 00:10:43 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-04-26 12:57:29 +00:00
|
|
|
/**
|
|
|
|
* Specialization of SymbolicVariable for Tuple
|
|
|
|
*/
|
|
|
|
class SymbolicTupleVariable: public SymbolicVariable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SymbolicTupleVariable(
|
2019-05-09 10:16:52 +00:00
|
|
|
solidity::TypePointer _type,
|
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
|
|
|
);
|
|
|
|
|
|
|
|
std::vector<std::shared_ptr<SymbolicVariable>> const& components()
|
|
|
|
{
|
|
|
|
return m_components;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<std::shared_ptr<SymbolicVariable>> m_components;
|
|
|
|
};
|
|
|
|
|
2018-01-17 20:02:23 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-09 10:16:52 +00:00
|
|
|
}
|