HAL
types.h
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2019 Ruhr University Bochum, Chair for Embedded Security. All Rights reserved.
4 // Copyright (c) 2019 Marc Fyrbiak, Sebastian Wallat, Max Hoffmann ("ORIGINAL AUTHORS"). All rights reserved.
5 // Copyright (c) 2021 Max Planck Institute for Security and Privacy. All Rights reserved.
6 // Copyright (c) 2021 Jörn Langheinrich, Julian Speith, Nils Albartus, René Walendy, Simon Klix ("ORIGINAL AUTHORS"). All Rights reserved.
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
25 
26 #pragma once
27 
28 #include "hal_core/defines.h"
31 
32 #include <map>
33 #include <optional>
34 #include <string>
35 
36 namespace hal
37 {
38  namespace SMT
39  {
43  enum class SolverType : int
44  {
45  Z3 = 0,
46  Boolector,
47  Bitwuzla,
48  Unknown,
49  };
50 
51  enum class SolverCall : int
52  {
53  Binary,
54  Library,
55  };
56 
60  struct QueryConfig final
61  {
63  // Member
65 
71  bool local = true;
73  bool generate_model = false;
76 
78  // Interface
80 
88 
96 
103 
110 
117 
124 
131  QueryConfig& with_timeout(u64 seconds);
132 
140  friend std::ostream& operator<<(std::ostream& out, const QueryConfig& config);
141  };
142 
147  struct Constraint final
148  {
150  // Member
152 
154  std::variant<BooleanFunction, std::pair<BooleanFunction, BooleanFunction>> constraint;
155 
157  // Constructors, Destructors, Operators
159 
166 
173  explicit Constraint(BooleanFunction&& lhs, BooleanFunction&& rhs);
174 
182  friend std::ostream& operator<<(std::ostream& out, const Constraint& constraint);
183 
189  std::string to_string() const;
190 
196  bool is_assignment() const;
197 
204 
211  };
212 
216  enum class SolverResultType
217  {
218  Sat,
219  UnSat,
220  Unknown,
221  };
222 
226  struct Model final
227  {
229  // Member
231 
233  std::map<std::string, std::tuple<u64, u16>> model;
234 
236  // Constructors, Destructors, Operators
238 
244  Model(const std::map<std::string, std::tuple<u64, u16>>& model = {});
245 
252  bool operator==(const Model& other) const;
253 
260  bool operator!=(const Model& other) const;
261 
269  friend std::ostream& operator<<(std::ostream& out, const Model& model);
270 
272  // Interface
274 
282  static Result<Model> parse(const std::string& model_str, const SolverType& solver);
283 
291  };
292 
296  struct SolverResult final
297  {
299  // Member
301 
305  std::optional<Model> model;
306 
308  // Constructors, Destructors, Operators
310 
313  {
314  }
315 
322  static SolverResult Sat(const std::optional<Model>& model = {});
323 
329  static SolverResult UnSat();
330 
336  static SolverResult Unknown();
337 
344  bool is(const SolverResultType& type) const;
345 
351  bool is_sat() const;
352 
358  bool is_unsat() const;
359 
365  bool is_unknown() const;
366 
374  friend std::ostream& operator<<(std::ostream& out, const SolverResult& result);
375 
376  private:
378  SolverResult(SolverResultType _type, std::optional<Model> _model);
379  };
380 
381  } // namespace SMT
382 
383  template<>
384  std::map<SMT::SolverType, std::string> EnumStrings<SMT::SolverType>::data;
385 
386  template<>
387  std::map<SMT::SolverResultType, std::string> EnumStrings<SMT::SolverResultType>::data;
388 } // namespace hal
uint64_t u64
Definition: defines.h:42
SolverCall
Definition: types.h:52
SolverType
Definition: types.h:44
SolverResultType
Definition: types.h:217
friend std::ostream & operator<<(std::ostream &out, const Constraint &constraint)
Definition: types.cpp:151
std::string to_string() const
Definition: types.cpp:166
bool is_assignment() const
Definition: types.cpp:173
std::variant< BooleanFunction, std::pair< BooleanFunction, BooleanFunction > > constraint
A constraint that is either an assignment of two Boolean functions or a single Boolean function,...
Definition: types.h:154
Constraint(BooleanFunction &&constraint)
Definition: types.cpp:143
Result< const BooleanFunction * > get_function() const
Definition: types.cpp:187
Result< const std::pair< BooleanFunction, BooleanFunction > * > get_assignment() const
Definition: types.cpp:178
Model(const std::map< std::string, std::tuple< u64, u16 >> &model={})
Definition: types.cpp:196
friend std::ostream & operator<<(std::ostream &out, const Model &model)
Definition: types.cpp:210
static Result< Model > parse(const std::string &model_str, const SolverType &solver)
Definition: types.cpp:220
bool operator!=(const Model &other) const
Definition: types.cpp:205
std::map< std::string, std::tuple< u64, u16 > > model
maps variable identifiers to a (1) value and (2) its bit-size.
Definition: types.h:233
Result< BooleanFunction > evaluate(const BooleanFunction &bf) const
Definition: types.cpp:261
bool operator==(const Model &other) const
Definition: types.cpp:200
bool generate_model
Controls whether the SMT solver should generate a model in case formula is satisfiable.
Definition: types.h:73
QueryConfig & with_remote_solver()
Definition: types.cpp:107
SolverType solver
The SMT solver identifier.
Definition: types.h:67
u64 timeout_in_seconds
The timeout after which the SMT solver is killed in seconds.
Definition: types.h:75
QueryConfig & without_model_generation()
Definition: types.cpp:119
QueryConfig & with_model_generation()
Definition: types.cpp:113
SolverCall call
The calling format for the SMT solver.
Definition: types.h:69
QueryConfig & with_local_solver()
Definition: types.cpp:101
QueryConfig & with_timeout(u64 seconds)
Definition: types.cpp:125
QueryConfig & with_solver(SolverType solver)
Definition: types.cpp:89
friend std::ostream & operator<<(std::ostream &out, const QueryConfig &config)
Definition: types.cpp:131
QueryConfig & with_call(SolverCall call)
Definition: types.cpp:95
static SolverResult Unknown()
Definition: types.cpp:309
SolverResult()
Default constructor (required for Result<T> initialization)
Definition: types.h:312
friend std::ostream & operator<<(std::ostream &out, const SolverResult &result)
Definition: types.cpp:334
std::optional< Model > model
The (optional) model that is only available if type == SMT::ResultType::Sat and model generation is e...
Definition: types.h:305
static SolverResult Sat(const std::optional< Model > &model={})
Definition: types.cpp:299
bool is_sat() const
Definition: types.cpp:319
bool is(const SolverResultType &type) const
Definition: types.cpp:314
bool is_unsat() const
Definition: types.cpp:324
static SolverResult UnSat()
Definition: types.cpp:304
SolverResultType type
Result type of the SMT query.
Definition: types.h:303
bool is_unknown() const
Definition: types.cpp:329