HAL  v4.5.0-83-g30c8f0afc
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
python_bindings.cpp
Go to the documentation of this file.
2 
3 #include "pybind11/operators.h"
4 #include "pybind11/pybind11.h"
5 #include "pybind11/stl.h"
6 #include "pybind11/stl_bind.h"
10 
11 namespace py = pybind11;
12 
13 namespace hal
14 {
15 
16  // the name in PYBIND11_MODULE/PYBIND11_PLUGIN *MUST* match the filename of the output library (without extension),
17  // otherwise you will get "ImportError: dynamic module does not define module export function" when importing the module
18 
19 #ifdef PYBIND11_MODULE
20  PYBIND11_MODULE(sequential_symbolic_execution, m)
21  {
22  m.doc() = "hal SequentialSymbolicExecutionPlugin python bindings";
23 #else
24  PYBIND11_PLUGIN(sequential_symbolic_execution)
25  {
26  py::module m("sequential_symbolic_execution", "hal SequentialSymbolicExecutionPlugin python bindings");
27 #endif // ifdef PYBIND11_MODULE
28 
29  py::class_<SequentialSymbolicExecutionPlugin, RawPtrWrapper<SequentialSymbolicExecutionPlugin>, BasePluginInterface> py_sequential_symbolic_execution_plugin(
30  m, "SequentialSymbolicExecutionPlugin", R"(This class provides an interface to integrate the HAWKEYE tool as a plugin within the HAL framework.)");
31 
32  py_sequential_symbolic_execution_plugin.def_property_readonly("name", &SequentialSymbolicExecutionPlugin::get_name, R"(
33  The name of the plugin.
34 
35  :type: str
36  )");
37 
38  py_sequential_symbolic_execution_plugin.def("get_name", &SequentialSymbolicExecutionPlugin::get_name, R"(
39  Get the name of the plugin.
40 
41  :returns: The name of the plugin.
42  :rtype: str
43  )");
44 
45  py_sequential_symbolic_execution_plugin.def_property_readonly("version", &SequentialSymbolicExecutionPlugin::get_version, R"(
46  The version of the plugin.
47 
48  :type: str
49  )");
50 
51  py_sequential_symbolic_execution_plugin.def("get_version", &SequentialSymbolicExecutionPlugin::get_version, R"(
52  Get the version of the plugin.
53 
54  :returns: The version of the plugin.
55  :rtype: str
56  )");
57 
58  py_sequential_symbolic_execution_plugin.def_property_readonly("description", &SequentialSymbolicExecutionPlugin::get_description, R"(
59  The description of the plugin.
60 
61  :type: str
62  )");
63 
64  py_sequential_symbolic_execution_plugin.def("get_description", &SequentialSymbolicExecutionPlugin::get_description, R"(
65  Get the description of the plugin.
66 
67  :returns: The description of the plugin.
68  :rtype: str
69  )");
70 
71  py_sequential_symbolic_execution_plugin.def_property_readonly("dependencies", &SequentialSymbolicExecutionPlugin::get_dependencies, R"(
72  A set of plugin names that this plugin depends on.
73 
74  :type: set[str]
75  )");
76 
77  py_sequential_symbolic_execution_plugin.def("get_dependencies", &SequentialSymbolicExecutionPlugin::get_dependencies, R"(
78  Get a set of plugin names that this plugin depends on.
79 
80  :returns: A set of plugin names that this plugin depends on.
81  :rtype: set[str]
82  )");
83 
84  m.def("get_pg_word_values_at_z3",
85  [](const std::vector<std::pair<Module*, PinGroup<ModulePin>*>>& words,
86  const std::vector<u32>& time_indices,
87  const std::vector<bool>& subgraph_gates_byte_map,
88  const std::vector<std::map<u32, bool>>& known_inputs,
89  const std::map<PinGroup<ModulePin>*, BooleanFunction>& word_level_calculations,
90  const bool substitute_endpoints) {
91  z3::context ctx;
92 
93  const auto word_values_res = sse::get_word_values_at_z3(words, time_indices, subgraph_gates_byte_map, known_inputs, word_level_calculations, substitute_endpoints, ctx);
94 
95  if (word_values_res.is_error())
96  {
97  log_error("sequential_symbolic_execution", "{}", word_values_res.get_error().get());
98  return;
99  }
100 
101  const auto word_values = word_values_res.get();
102 
103  for (u32 idx = 0; idx < word_values.size(); idx++)
104  {
105  const auto& [mod, pg] = words.at(idx);
106  const auto& w = word_values.at(idx);
107  const auto t = time_indices.at(idx);
108 
109  if (idx == 0 || t != time_indices.at(idx - 1))
110  {
111  std::cout << "T[" << t << "]" << std::endl;
112  }
113 
114  //std::cout << "\tWORD: " << w << std::endl;
115  std::cout << "\t" << mod->get_name() << " - " << pg->get_name() << ": " << std::endl;
116  // std::cout << "\t\t" << z3_utils::to_bf(w).get().to_string() << std::endl;
117 
118  const auto w_z3_simplified = z3_utils::simplify_local(w.simplify());
119  if (w_z3_simplified.is_error())
120  {
121  std::cout << w_z3_simplified.get_error().get() << std::endl;
122  }
123 
124  const auto w_bfz_simplified = z3_utils::to_bf(w_z3_simplified.get());
125  if (w_bfz_simplified.is_error())
126  {
127  std::cout << w_bfz_simplified.get_error().get() << std::endl;
128  }
129  else
130  {
131  const std::string s = w_bfz_simplified.get().to_string();
132  std::cout << "\t\t" << s << std::endl;
133  }
134  }
135 
136  return;
137  });
138 
139 #ifndef PYBIND11_MODULE
140  return m.ptr();
141 #endif // PYBIND11_MODULE
142  }
143 } // namespace hal
std::set< std::string > get_dependencies() const override
Get the plugin dependencies.
std::string get_name() const override
Get the name of the plugin.
std::string get_description() const override
Get a short description of the plugin.
std::string get_version() const override
Get the version of the plugin.
uint32_t u32
Definition: defines.h:41
#define log_error(channel,...)
Definition: log.h:78
const Module * module(const Gate *g, const NodeBoxes &boxes)
Result< std::vector< z3::expr > > get_word_values_at_z3(const std::vector< std::vector< Net * >> &words, const std::vector< u32 > &time_indices, const std::vector< bool > &subgraph_gates_byte_map, const std::vector< std::map< u32, bool >> &known_inputs, const std::map< PinGroup< ModulePin > *, BooleanFunction > &word_level_calculations, const bool substitute_endpoints, z3::context &ctx)
Get the Z3 expressions representing the values of multiple nets as words at specific time indices.
Result< z3::expr > simplify_local(const z3::expr &e, std::unordered_map< u32, z3::expr > &cache, const bool check_correctness=false)
Applies hand-crafted simplification rules iteratively until no further simplifications can be made.
Result< BooleanFunction > to_bf(const z3::expr &e)
Definition: z3_utils.cpp:443
Definition: defines.h:45
PYBIND11_PLUGIN(hal_py)
This file contains all functions related to the HAL plugin API.
This file contains the declarations for functions to perform sequential symbolic execution using z3.