2021-01-19 11:56:22 +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/>.
|
|
|
|
*/
|
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <libsmtutil/SolverInterface.h>
|
|
|
|
|
|
|
|
#include <optional>
|
|
|
|
#include <set>
|
|
|
|
|
|
|
|
namespace solidity::frontend
|
|
|
|
{
|
|
|
|
|
2021-04-15 10:31:45 +00:00
|
|
|
struct ModelCheckerContracts
|
|
|
|
{
|
|
|
|
/// By default all contracts are analyzed.
|
|
|
|
static ModelCheckerContracts Default() { return {}; }
|
|
|
|
|
|
|
|
/// Parses a string of the form <path>:<contract>,<path>:contract,...
|
|
|
|
/// and returns nullopt if a path or contract name is empty.
|
|
|
|
static std::optional<ModelCheckerContracts> fromString(std::string const& _contracts);
|
|
|
|
|
|
|
|
/// @returns true if all contracts should be analyzed.
|
|
|
|
bool isDefault() const { return contracts.empty(); }
|
|
|
|
|
|
|
|
bool has(std::string const& _source) const { return contracts.count(_source); }
|
|
|
|
bool has(std::string const& _source, std::string const& _contract) const
|
|
|
|
{
|
|
|
|
return has(_source) && contracts.at(_source).count(_contract);
|
|
|
|
}
|
|
|
|
|
2021-06-07 16:57:35 +00:00
|
|
|
bool operator!=(ModelCheckerContracts const& _other) const noexcept { return !(*this == _other); }
|
|
|
|
bool operator==(ModelCheckerContracts const& _other) const noexcept { return contracts == _other.contracts; }
|
|
|
|
|
2021-04-15 10:31:45 +00:00
|
|
|
/// Represents which contracts should be analyzed by the SMTChecker
|
|
|
|
/// as the most derived.
|
|
|
|
/// The key is the source file. If the map is empty, all sources must be analyzed.
|
|
|
|
/// For each source, contracts[source] represents the contracts in that source
|
|
|
|
/// that should be analyzed.
|
|
|
|
/// If the set of contracts is empty, all contracts in that source should be analyzed.
|
|
|
|
std::map<std::string, std::set<std::string>> contracts;
|
|
|
|
};
|
|
|
|
|
2021-01-19 11:56:22 +00:00
|
|
|
struct ModelCheckerEngine
|
|
|
|
{
|
|
|
|
bool bmc = false;
|
|
|
|
bool chc = false;
|
|
|
|
|
|
|
|
static constexpr ModelCheckerEngine All() { return {true, true}; }
|
|
|
|
static constexpr ModelCheckerEngine BMC() { return {true, false}; }
|
|
|
|
static constexpr ModelCheckerEngine CHC() { return {false, true}; }
|
|
|
|
static constexpr ModelCheckerEngine None() { return {false, false}; }
|
|
|
|
|
|
|
|
bool none() const { return !any(); }
|
|
|
|
bool any() const { return bmc || chc; }
|
|
|
|
bool all() const { return bmc && chc; }
|
|
|
|
|
|
|
|
static std::optional<ModelCheckerEngine> fromString(std::string const& _engine)
|
|
|
|
{
|
|
|
|
static std::map<std::string, ModelCheckerEngine> engineMap{
|
|
|
|
{"all", All()},
|
|
|
|
{"bmc", BMC()},
|
|
|
|
{"chc", CHC()},
|
|
|
|
{"none", None()}
|
|
|
|
};
|
|
|
|
if (engineMap.count(_engine))
|
|
|
|
return engineMap.at(_engine);
|
|
|
|
return {};
|
|
|
|
}
|
2021-06-07 16:57:35 +00:00
|
|
|
|
|
|
|
bool operator!=(ModelCheckerEngine const& _other) const noexcept { return !(*this == _other); }
|
|
|
|
bool operator==(ModelCheckerEngine const& _other) const noexcept { return bmc == _other.bmc && chc == _other.chc; }
|
2021-01-19 11:56:22 +00:00
|
|
|
};
|
|
|
|
|
2021-10-06 09:50:00 +00:00
|
|
|
enum class InvariantType { Contract, Reentrancy };
|
|
|
|
|
|
|
|
struct ModelCheckerInvariants
|
|
|
|
{
|
|
|
|
/// Adds the default targets, that is, all except underflow and overflow.
|
|
|
|
static ModelCheckerInvariants Default() { return *fromString("default"); }
|
|
|
|
/// Adds all targets, including underflow and overflow.
|
|
|
|
static ModelCheckerInvariants All() { return *fromString("all"); }
|
2021-10-06 09:51:55 +00:00
|
|
|
static ModelCheckerInvariants None() { return {{}}; }
|
2021-10-06 09:50:00 +00:00
|
|
|
|
|
|
|
static std::optional<ModelCheckerInvariants> fromString(std::string const& _invs);
|
|
|
|
|
|
|
|
bool has(InvariantType _inv) const { return invariants.count(_inv); }
|
|
|
|
|
|
|
|
/// @returns true if the @p _target is valid,
|
|
|
|
/// and false otherwise.
|
|
|
|
bool setFromString(std::string const& _target);
|
|
|
|
|
|
|
|
static std::map<std::string, InvariantType> const validInvariants;
|
|
|
|
|
|
|
|
bool operator!=(ModelCheckerInvariants const& _other) const noexcept { return !(*this == _other); }
|
|
|
|
bool operator==(ModelCheckerInvariants const& _other) const noexcept { return invariants == _other.invariants; }
|
|
|
|
|
|
|
|
std::set<InvariantType> invariants;
|
|
|
|
};
|
|
|
|
|
2021-03-23 18:15:14 +00:00
|
|
|
enum class VerificationTargetType { ConstantCondition, Underflow, Overflow, UnderOverflow, DivByZero, Balance, Assert, PopEmptyArray, OutOfBounds };
|
2021-01-19 11:56:22 +00:00
|
|
|
|
|
|
|
struct ModelCheckerTargets
|
|
|
|
{
|
2021-07-07 10:31:09 +00:00
|
|
|
/// Adds the default targets, that is, all except underflow and overflow.
|
2021-04-15 10:31:45 +00:00
|
|
|
static ModelCheckerTargets Default() { return *fromString("default"); }
|
2021-07-07 10:31:09 +00:00
|
|
|
/// Adds all targets, including underflow and overflow.
|
|
|
|
static ModelCheckerTargets All() { return *fromString("all"); }
|
2021-01-19 11:56:22 +00:00
|
|
|
|
|
|
|
static std::optional<ModelCheckerTargets> fromString(std::string const& _targets);
|
|
|
|
|
|
|
|
bool has(VerificationTargetType _type) const { return targets.count(_type); }
|
2021-04-14 10:56:43 +00:00
|
|
|
|
|
|
|
/// @returns true if the @p _target is valid,
|
|
|
|
/// and false otherwise.
|
|
|
|
bool setFromString(std::string const& _target);
|
|
|
|
|
|
|
|
static std::map<std::string, VerificationTargetType> const targetStrings;
|
|
|
|
|
2021-10-13 14:21:12 +00:00
|
|
|
static std::map<VerificationTargetType, std::string> const targetTypeToString;
|
|
|
|
|
2021-06-07 16:57:35 +00:00
|
|
|
bool operator!=(ModelCheckerTargets const& _other) const noexcept { return !(*this == _other); }
|
|
|
|
bool operator==(ModelCheckerTargets const& _other) const noexcept { return targets == _other.targets; }
|
|
|
|
|
2021-01-19 11:56:22 +00:00
|
|
|
std::set<VerificationTargetType> targets;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ModelCheckerSettings
|
|
|
|
{
|
2021-04-15 10:31:45 +00:00
|
|
|
ModelCheckerContracts contracts = ModelCheckerContracts::Default();
|
2021-07-15 15:39:01 +00:00
|
|
|
/// Currently division and modulo are replaced by multiplication with slack vars, such that
|
|
|
|
/// a / b <=> a = b * k + m
|
|
|
|
/// where k and m are slack variables.
|
|
|
|
/// This is the default because Spacer prefers that over precise / and mod.
|
|
|
|
/// This option allows disabling this mechanism since other solvers
|
|
|
|
/// might prefer the precise encoding.
|
|
|
|
bool divModNoSlacks = false;
|
2021-03-31 15:09:37 +00:00
|
|
|
ModelCheckerEngine engine = ModelCheckerEngine::None();
|
2021-10-06 09:50:00 +00:00
|
|
|
ModelCheckerInvariants invariants = ModelCheckerInvariants::Default();
|
2021-07-02 12:42:53 +00:00
|
|
|
bool showUnproved = false;
|
2022-05-15 15:45:43 +00:00
|
|
|
smtutil::SMTSolverChoice solvers = smtutil::SMTSolverChoice::Z3();
|
2021-04-15 10:31:45 +00:00
|
|
|
ModelCheckerTargets targets = ModelCheckerTargets::Default();
|
2021-01-19 11:56:22 +00:00
|
|
|
std::optional<unsigned> timeout;
|
2021-06-07 16:57:35 +00:00
|
|
|
|
|
|
|
bool operator!=(ModelCheckerSettings const& _other) const noexcept { return !(*this == _other); }
|
|
|
|
bool operator==(ModelCheckerSettings const& _other) const noexcept
|
|
|
|
{
|
|
|
|
return
|
|
|
|
contracts == _other.contracts &&
|
2021-07-15 15:39:01 +00:00
|
|
|
divModNoSlacks == _other.divModNoSlacks &&
|
2021-06-07 16:57:35 +00:00
|
|
|
engine == _other.engine &&
|
2021-10-06 09:50:00 +00:00
|
|
|
invariants == _other.invariants &&
|
2021-07-02 12:42:53 +00:00
|
|
|
showUnproved == _other.showUnproved &&
|
2021-05-19 15:35:19 +00:00
|
|
|
solvers == _other.solvers &&
|
2021-06-07 16:57:35 +00:00
|
|
|
targets == _other.targets &&
|
|
|
|
timeout == _other.timeout;
|
|
|
|
}
|
2021-01-19 11:56:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|