HAL  v4.5.0-124-g47ab54673
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  for (auto item : py::reinterpret_borrow<py::dict>(value))
222  {
223  keep_owner_alive(call, item.second, receiver);
224  }
225  return;
226  }
227 
228  // Only a pybind11 instance can hold a reference to its owner. Anything else, such as the
229  // index paired with the pin group returned by GatePin.get_group, would send keep_alive
230  // down its weak reference fallback, which fails outright on an int or a string.
231  if (py::detail::all_type_info(Py_TYPE(value.ptr())).empty())
232  {
233  return;
234  }
235 
236  const py::handle owner = owner_of(value);
237  const py::handle patient = owner ? owner : receiver;
238 
239  if (!patient || patient.is_none() || patient.ptr() == value.ptr())
240  {
241  return;
242  }
243 
244  py::detail::keep_alive_impl(value, patient);
245  }
246  } // namespace python_bindings_detail
247 } // namespace hal
248 
249 namespace pybind11
250 {
251  namespace detail
252  {
254  template<>
255  struct process_attribute<hal::borrowed> : public process_attribute_default<hal::borrowed>
256  {
257  static void init(const hal::borrowed&, function_record* record)
258  {
259  // A non-owning wrapper that links nothing; every link is added in postcall below.
260  record->policy = return_value_policy::reference;
261  }
262 
263  static void postcall(function_call& call, handle ret)
264  {
265  // Only a method has a receiver worth falling back to. The first argument of a free
266  // function is just an argument and need not own anything that is returned.
267  // size(), not empty(): pybind11 changed args from a std::vector to a small_vector that has no empty()
268  const handle receiver = (call.func.is_method && call.args.size() > 0) ? call.args[0] : handle();
270  }
271  };
272  } // namespace detail
273 } // namespace pybind11
274 
275 namespace hal
276 {
277 
278  // TODO move into own namespace
291 
297  void core_utils_init(py::module& m);
298 
304  void gate_type_init(py::module& m);
305 
312 
319 
326 
332  void gate_library_init(py::module& m);
333 
340 
346  void endpoint_init(py::module& m);
347 
353  void netlist_init(py::module& m);
354 
361 
368 
375 
381  void base_pin_init(py::module& m);
382 
388  void gate_pin_init(py::module& m);
389 
396 
402  void module_pin_init(py::module& m);
403 
410 
416  void gate_init(py::module& m);
417 
423  void net_init(py::module& m);
424 
430  void module_init(py::module& m);
431 
437  void grouping_init(py::module& m);
438 
445 
452 
459 
461 
468 
475 
481  void smt_init(py::module& m);
482 
489 
496 
503 
510 
517 
523  void log_init(py::module& m);
527 } // 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)