HAL  v4.5.0-147-gff98e36c6
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
python_bindings.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"
39 #include "hal_core/netlist/gate.h"
48 #include "hal_core/netlist/net.h"
61 #include "hal_core/utilities/log.h"
65 
66 #pragma GCC diagnostic push
67 #pragma GCC diagnostic ignored "-Wshadow"
68 #ifdef COMPILER_CLANG
69 #pragma clang diagnostic ignored "-Wnested-anon-types"
70 #pragma clang diagnostic ignored "-Wshadow-field-in-constructor-modified"
71 #pragma clang diagnostic ignored "-Wself-assign-overloaded"
72 #endif
73 
74 #include "pybind11/functional.h"
75 #include "pybind11/operators.h"
76 #include "pybind11/pybind11.h"
77 #include "pybind11/stl.h"
78 #include "pybind11/stl/filesystem.h"
79 #include "pybind11/stl_bind.h"
80 
81 #pragma GCC diagnostic pop
82 
83 // Passed to Python by reference instead of being converted to a dict, so that a cache filled by
84 // SubgraphNetlistDecorator.get_subgraph_function is kept across calls (see subgraph_netlist_decorator.cpp).
86 
87 namespace hal
88 {
89  namespace py = pybind11;
90 
96  template<class T>
97  using RawPtrWrapper = std::unique_ptr<T, py::nodelete>;
98 
133  struct borrowed
134  {
135  };
136 
137  namespace python_bindings_detail
138  {
144  template<typename T>
145  py::handle existing_wrapper(const T* object)
146  {
147  if (object == nullptr)
148  {
149  return py::handle();
150  }
151 
152  const auto* type = py::detail::get_type_info(typeid(T));
153  if (type == nullptr)
154  {
155  return py::handle();
156  }
157 
158  // returns a borrowed reference and does not create a wrapper
159  return py::detail::get_object_handle(object, type);
160  }
161 
167  inline py::handle owner_of(py::handle object)
168  {
169  if (py::isinstance<Gate>(object))
170  {
171  return existing_wrapper(py::cast<const Gate*>(object)->get_netlist());
172  }
173  if (py::isinstance<Net>(object))
174  {
175  return existing_wrapper(py::cast<const Net*>(object)->get_netlist());
176  }
177  if (py::isinstance<Module>(object))
178  {
179  return existing_wrapper(py::cast<const Module*>(object)->get_netlist());
180  }
181  if (py::isinstance<Grouping>(object))
182  {
183  return existing_wrapper(py::cast<const Grouping*>(object)->get_netlist());
184  }
185  if (py::isinstance<Endpoint>(object))
186  {
187  const auto* gate = py::cast<const Endpoint*>(object)->get_gate();
188  return gate == nullptr ? py::handle() : existing_wrapper(gate->get_netlist());
189  }
190  if (py::isinstance<GateType>(object))
191  {
192  return existing_wrapper(py::cast<const GateType*>(object)->get_gate_library());
193  }
194 
195  // A pin, a pin group and a gate type component do not know what they belong to, so
196  // there is nothing to look up and the caller falls back to the receiver.
197  return py::handle();
198  }
199 
207  inline void keep_owner_alive(py::detail::function_call& call, py::handle value, py::handle receiver)
208  {
209  if (!value || value.is_none())
210  {
211  return;
212  }
213 
214  if (py::isinstance<py::list>(value) || py::isinstance<py::tuple>(value) || py::isinstance<py::set>(value))
215  {
216  for (py::handle element : py::reinterpret_borrow<py::object>(value))
217  {
218  keep_owner_alive(call, element, receiver);
219  }
220  return;
221  }
222 
223  if (py::isinstance<py::dict>(value))
224  {
225  // Keys as well as values: a dict from net to influence, say, borrows through its keys.
226  for (auto item : py::reinterpret_borrow<py::dict>(value))
227  {
228  keep_owner_alive(call, item.first, receiver);
229  keep_owner_alive(call, item.second, receiver);
230  }
231  return;
232  }
233 
234  // Only a pybind11 instance can hold a reference to its owner. Anything else, such as the
235  // index paired with the pin group returned by GatePin.get_group, would send keep_alive
236  // down its weak reference fallback, which fails outright on an int or a string.
237  if (py::detail::all_type_info(Py_TYPE(value.ptr())).empty())
238  {
239  return;
240  }
241 
242  const py::handle owner = owner_of(value);
243  const py::handle patient = owner ? owner : receiver;
244 
245  if (!patient || patient.is_none() || patient.ptr() == value.ptr())
246  {
247  return;
248  }
249 
250  py::detail::keep_alive_impl(value, patient);
251  }
252  } // namespace python_bindings_detail
253 } // namespace hal
254 
255 namespace pybind11
256 {
257  namespace detail
258  {
260  template<>
261  struct process_attribute<hal::borrowed> : public process_attribute_default<hal::borrowed>
262  {
263  static void init(const hal::borrowed&, function_record* record)
264  {
265  // A non-owning wrapper that links nothing; every link is added in postcall below.
266  record->policy = return_value_policy::reference;
267  }
268 
269  static void postcall(function_call& call, handle ret)
270  {
271  // Since pybind11 3.1 the hook also runs for an overload whose arguments did not load,
272  // and is then handed the try-next-overload sentinel rather than an object.
273  if (!ret || ret.ptr() == PYBIND11_TRY_NEXT_OVERLOAD)
274  {
275  return;
276  }
277 
278  // Only a method has a receiver worth falling back to. The first argument of a free
279  // function is just an argument and need not own anything that is returned.
280  // size(), not empty(): pybind11 changed args from a std::vector to a small_vector that has no empty()
281  const handle receiver = (call.func.is_method && call.args.size() > 0) ? call.args[0] : handle();
282  hal::python_bindings_detail::keep_owner_alive(call, ret, receiver);
283  }
284  };
285  } // namespace detail
286 } // namespace pybind11
287 
288 namespace hal
289 {
290 
291  // TODO move into own namespace
304 
310  void core_utils_init(py::module& m);
311 
317  void gate_type_init(py::module& m);
318 
325 
332 
339 
345  void gate_library_init(py::module& m);
346 
353 
359  void endpoint_init(py::module& m);
360 
366  void netlist_init(py::module& m);
367 
374 
381 
388 
394  void base_pin_init(py::module& m);
395 
401  void gate_pin_init(py::module& m);
402 
409 
415  void module_pin_init(py::module& m);
416 
423 
429  void gate_init(py::module& m);
430 
436  void net_init(py::module& m);
437 
443  void module_init(py::module& m);
444 
450  void grouping_init(py::module& m);
451 
458 
465 
472 
474 
481 
488 
494  void smt_init(py::module& m);
495 
502 
509 
516 
523 
530 
536  void log_init(py::module& m);
540 } // namespace hal
std::map< std::pair< u32, const GatePin * >, BooleanFunction > SubgraphFunctionCache
void grouping_init(py::module &m)
Definition: grouping.cpp:5
void gate_type_sequential_init(py::module &m)
void module_pin_init(py::module &m)
Definition: module_pin.cpp:5
void program_options_init(py::module &m)
void netlist_writer_manager_init(py::module &m)
void gate_library_init(py::module &m)
Definition: gate_library.cpp:5
void netlist_serializer_init(py::module &m)
void net_init(py::module &m)
Definition: net.cpp:5
void gate_type_components_init(py::module &m)
void gate_pin_group_init(py::module &m)
void gate_pin_init(py::module &m)
Definition: gate_pin.cpp:5
void netlist_utils_init(py::module &m)
void endpoint_init(py::module &m)
Definition: endpoint.cpp:5
void smt_init(py::module &m)
Definition: smt.cpp:5
void base_pin_init(py::module &m)
Definition: base_pin.cpp:5
PYBIND11_MAKE_OPAQUE(hal::SubgraphFunctionCache) namespace hal
void module_pin_group_init(py::module &m)
void plugin_manager_init(py::module &m)
void netlist_init(py::module &m)
Definition: netlist.cpp:5
void gate_init(py::module &m)
Definition: gate.cpp:5
void module_init(py::module &m)
Definition: module.cpp:5
void gate_type_lut_init(py::module &m)
void boolean_function_net_decorator_init(py::module &m)
void project_manager_init(py::module &m)
void subgraph_netlist_decorator_init(py::module &m)
void log_init(py::module &m)
Definition: log.cpp:41
void gate_library_manager_init(py::module &m)
void gate_type_init(py::module &m)
Definition: gate_type.cpp:5
void netlist_modification_decorator_init(py::module &m)
void netlist_traversal_decorator_init(py::module &m)
void data_container_init(py::module &m)
void boolean_function_decorator_init(py::module &m)
void plugin_interfaces_init(py::module &m)
void boolean_function_init(py::module &m)
void core_utils_init(py::module &m)
Definition: core_utils.cpp:5
void netlist_factory_init(py::module &m)
const Module * module(const Gate *g, const NodeBoxes &boxes)
GateLibrary * get_gate_library(const std::string &file_path)
Definition: defines.h:45
PinType type
This file contains various functions to create and load netlists.
static void init(const hal::borrowed &, function_record *record)
static void postcall(function_call &call, handle ret)