HAL  v4.5.0-133-g64838ea8d
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 namespace hal
84 {
85  namespace py = pybind11;
86 
92  template<class T>
93  using RawPtrWrapper = std::unique_ptr<T, py::nodelete>;
94 
129  struct borrowed
130  {
131  };
132 
133  namespace python_bindings_detail
134  {
140  template<typename T>
141  py::handle existing_wrapper(const T* object)
142  {
143  if (object == nullptr)
144  {
145  return py::handle();
146  }
147 
148  const auto* type = py::detail::get_type_info(typeid(T));
149  if (type == nullptr)
150  {
151  return py::handle();
152  }
153 
154  // returns a borrowed reference and does not create a wrapper
155  return py::detail::get_object_handle(object, type);
156  }
157 
163  inline py::handle owner_of(py::handle object)
164  {
165  if (py::isinstance<Gate>(object))
166  {
167  return existing_wrapper(py::cast<const Gate*>(object)->get_netlist());
168  }
169  if (py::isinstance<Net>(object))
170  {
171  return existing_wrapper(py::cast<const Net*>(object)->get_netlist());
172  }
173  if (py::isinstance<Module>(object))
174  {
175  return existing_wrapper(py::cast<const Module*>(object)->get_netlist());
176  }
177  if (py::isinstance<Grouping>(object))
178  {
179  return existing_wrapper(py::cast<const Grouping*>(object)->get_netlist());
180  }
181  if (py::isinstance<Endpoint>(object))
182  {
183  const auto* gate = py::cast<const Endpoint*>(object)->get_gate();
184  return gate == nullptr ? py::handle() : existing_wrapper(gate->get_netlist());
185  }
186  if (py::isinstance<GateType>(object))
187  {
188  return existing_wrapper(py::cast<const GateType*>(object)->get_gate_library());
189  }
190 
191  // A pin, a pin group and a gate type component do not know what they belong to, so
192  // there is nothing to look up and the caller falls back to the receiver.
193  return py::handle();
194  }
195 
203  inline void keep_owner_alive(py::detail::function_call& call, py::handle value, py::handle receiver)
204  {
205  if (!value || value.is_none())
206  {
207  return;
208  }
209 
210  if (py::isinstance<py::list>(value) || py::isinstance<py::tuple>(value) || py::isinstance<py::set>(value))
211  {
212  for (py::handle element : py::reinterpret_borrow<py::object>(value))
213  {
214  keep_owner_alive(call, element, receiver);
215  }
216  return;
217  }
218 
219  if (py::isinstance<py::dict>(value))
220  {
221  // Keys as well as values: a dict from net to influence, say, borrows through its keys.
222  for (auto item : py::reinterpret_borrow<py::dict>(value))
223  {
224  keep_owner_alive(call, item.first, receiver);
225  keep_owner_alive(call, item.second, receiver);
226  }
227  return;
228  }
229 
230  // Only a pybind11 instance can hold a reference to its owner. Anything else, such as the
231  // index paired with the pin group returned by GatePin.get_group, would send keep_alive
232  // down its weak reference fallback, which fails outright on an int or a string.
233  if (py::detail::all_type_info(Py_TYPE(value.ptr())).empty())
234  {
235  return;
236  }
237 
238  const py::handle owner = owner_of(value);
239  const py::handle patient = owner ? owner : receiver;
240 
241  if (!patient || patient.is_none() || patient.ptr() == value.ptr())
242  {
243  return;
244  }
245 
246  py::detail::keep_alive_impl(value, patient);
247  }
248  } // namespace python_bindings_detail
249 } // namespace hal
250 
251 namespace pybind11
252 {
253  namespace detail
254  {
256  template<>
257  struct process_attribute<hal::borrowed> : public process_attribute_default<hal::borrowed>
258  {
259  static void init(const hal::borrowed&, function_record* record)
260  {
261  // A non-owning wrapper that links nothing; every link is added in postcall below.
262  record->policy = return_value_policy::reference;
263  }
264 
265  static void postcall(function_call& call, handle ret)
266  {
267  // Since pybind11 3.1 the hook also runs for an overload whose arguments did not load,
268  // and is then handed the try-next-overload sentinel rather than an object.
269  if (!ret || ret.ptr() == PYBIND11_TRY_NEXT_OVERLOAD)
270  {
271  return;
272  }
273 
274  // Only a method has a receiver worth falling back to. The first argument of a free
275  // function is just an argument and need not own anything that is returned.
276  // size(), not empty(): pybind11 changed args from a std::vector to a small_vector that has no empty()
277  const handle receiver = (call.func.is_method && call.args.size() > 0) ? call.args[0] : handle();
279  }
280  };
281  } // namespace detail
282 } // namespace pybind11
283 
284 namespace hal
285 {
286 
287  // TODO move into own namespace
300 
306  void core_utils_init(py::module& m);
307 
313  void gate_type_init(py::module& m);
314 
321 
328 
335 
341  void gate_library_init(py::module& m);
342 
349 
355  void endpoint_init(py::module& m);
356 
362  void netlist_init(py::module& m);
363 
370 
377 
384 
390  void base_pin_init(py::module& m);
391 
397  void gate_pin_init(py::module& m);
398 
405 
411  void module_pin_init(py::module& m);
412 
419 
425  void gate_init(py::module& m);
426 
432  void net_init(py::module& m);
433 
439  void module_init(py::module& m);
440 
446  void grouping_init(py::module& m);
447 
454 
461 
468 
470 
477 
484 
490  void smt_init(py::module& m);
491 
498 
505 
512 
519 
526 
532  void log_init(py::module& m);
536 } // namespace hal
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
std::unique_ptr< T, py::nodelete > RawPtrWrapper
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)
py::handle existing_wrapper(const T *object)
void keep_owner_alive(py::detail::function_call &call, py::handle value, py::handle receiver)
py::handle owner_of(py::handle object)
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)