HAL  v4.5.0-133-g64838ea8d
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
netlist_traversal_decorator.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 <optional>
33 
34 namespace hal
35 {
41  enum class TraversalDirection
42  {
43  forward,
44  backward,
45  both,
46  };
47 
57  enum class TraversalStop
58  {
64  at_match,
65 
72 
78  never,
79  };
80 
99  {
100  public:
103  TraversalCache(const TraversalCache&) = delete;
105 
106  private:
108 
109  TraversalCache(const Netlist* netlist,
111  std::function<bool(const Gate*)> match,
112  TraversalStop stop,
113  std::function<bool(const Endpoint*)> exit_endpoint_filter,
114  std::function<bool(const Endpoint*)> entry_endpoint_filter)
115  : m_netlist(netlist), m_direction(direction), m_match(std::move(match)), m_stop(stop), m_exit_endpoint_filter(std::move(exit_endpoint_filter)),
116  m_entry_endpoint_filter(std::move(entry_endpoint_filter))
117  {
118  }
119 
120  const Netlist* m_netlist;
121  TraversalDirection m_direction;
122  std::function<bool(const Gate*)> m_match;
123  TraversalStop m_stop;
124  std::function<bool(const Endpoint*)> m_exit_endpoint_filter;
125  std::function<bool(const Endpoint*)> m_entry_endpoint_filter;
126  std::unordered_map<const Net*, std::set<Gate*>> m_store;
127  };
128 
135  {
136  public:
142  NetlistTraversalDecorator(const Netlist& netlist);
143 
160  Result<std::set<Gate*>> get_gates(const Net* net,
162  const std::function<bool(const Gate*)>& match,
163  TraversalStop stop,
164  u32 max_depth = 0,
165  const std::function<bool(const Endpoint*, u32 current_depth)>& exit_endpoint_filter = nullptr,
166  const std::function<bool(const Endpoint*, u32 current_depth)>& entry_endpoint_filter = nullptr) const;
167 
184  Result<std::set<Gate*>> get_gates(const Gate* gate,
186  const std::function<bool(const Gate*)>& match,
187  TraversalStop stop,
188  u32 max_depth = 0,
189  const std::function<bool(const Endpoint*, u32 current_depth)>& exit_endpoint_filter = nullptr,
190  const std::function<bool(const Endpoint*, u32 current_depth)>& entry_endpoint_filter = nullptr) const;
191 
208  Result<std::set<Gate*>> get_next_matching_gates(const Net* net,
209  bool successors,
210  const std::function<bool(const Gate*)>& target_gate_filter,
211  bool continue_on_match = false,
212  const std::function<bool(const Endpoint*, u32 current_depth)>& exit_endpoint_filter = nullptr,
213  const std::function<bool(const Endpoint*, u32 current_depth)>& entry_endpoint_filter = nullptr) const;
214 
229  Result<std::set<Gate*>> get_next_matching_gates(const Gate* gate,
230  bool successors,
231  const std::function<bool(const Gate*)>& target_gate_filter,
232  bool continue_on_match = false,
233  const std::function<bool(const Endpoint*, u32 current_depth)>& exit_endpoint_filter = nullptr,
234  const std::function<bool(const Endpoint*, u32 current_depth)>& entry_endpoint_filter = nullptr) const;
235 
250  Result<std::set<Gate*>> get_next_matching_gates_until(const Net* net,
251  bool successors,
252  const std::function<bool(const Gate*)>& target_gate_filter,
253  bool continue_on_mismatch = false,
254  const std::function<bool(const Endpoint*, u32 current_depth)>& exit_endpoint_filter = nullptr,
255  const std::function<bool(const Endpoint*, u32 current_depth)>& entry_endpoint_filter = nullptr) const;
256 
271  Result<std::set<Gate*>> get_next_matching_gates_until(const Gate* gate,
272  bool successors,
273  const std::function<bool(const Gate*)>& target_gate_filter,
274  bool continue_on_mismatch = false,
275  const std::function<bool(const Endpoint*, u32 current_depth)>& exit_endpoint_filter = nullptr,
276  const std::function<bool(const Endpoint*, u32 current_depth)>& entry_endpoint_filter = nullptr) const;
277 
292  Result<std::set<Gate*>> get_next_matching_gates_until_depth(const Net* net, bool successors, u32 max_depth, const std::function<bool(const Gate*)>& target_gate_filter = nullptr) const;
293 
308  Result<std::set<Gate*>> get_next_matching_gates_until_depth(const Gate* gate, bool successors, u32 max_depth, const std::function<bool(const Gate*)>& target_gate_filter = nullptr) const;
309 
321  Result<std::set<Gate*>> get_next_sequential_gates(const Net* net, bool successors, const std::set<PinType>& forbidden_pins = {}) const;
322 
334  Result<std::set<Gate*>> get_next_sequential_gates(const Gate* gate, bool successors, const std::set<PinType>& forbidden_pins = {}) const;
335 
345  Result<std::map<Gate*, std::set<Gate*>>> get_next_sequential_gates_map(bool successors, const std::set<PinType>& forbidden_pins) const;
346 
359  Result<std::set<Gate*>> get_combinational_cone(const Net* net, bool successors, const std::set<PinType>& forbidden_pins = {}) const;
360 
373  Result<std::set<Gate*>> get_combinational_cone(const Gate* gate, bool successors, const std::set<PinType>& forbidden_pins = {}) const;
374 
387  Result<std::optional<u32>> get_shortest_path_distance(const Gate* start_gate,
388  const Gate* end_gate,
389  const PinDirection& direction,
390  const std::function<bool(const Endpoint*, u32 current_depth)>& exit_endpoint_filter = nullptr,
391  const std::function<bool(const Endpoint*, u32 current_depth)>& entry_endpoint_filter = nullptr) const;
392 
406  const Gate* end_gate,
407  const PinDirection& direction,
408  const std::function<bool(const Endpoint*, u32 current_depth)>& exit_endpoint_filter = nullptr,
409  const std::function<bool(const Endpoint*, u32 current_depth)>& entry_endpoint_filter = nullptr) const;
410 
426  const Module* end_module,
427  const PinDirection& direction,
428  const std::function<bool(const Endpoint*, u32 current_depth)>& exit_endpoint_filter = nullptr,
429  const std::function<bool(const Endpoint*, u32 current_depth)>& entry_endpoint_filter = nullptr) const;
430 
445  const Module* end_module,
446  const PinDirection& direction,
447  const std::function<bool(const Endpoint*, u32 current_depth)>& exit_endpoint_filter = nullptr,
448  const std::function<bool(const Endpoint*, u32 current_depth)>& entry_endpoint_filter = nullptr) const;
449 
459  Result<std::vector<Gate*>> get_gate_chain(Gate* start_gate,
460  const std::vector<const GatePin*>& input_pins = {},
461  const std::vector<const GatePin*>& output_pins = {},
462  const std::function<bool(const Gate*)>& filter = nullptr) const;
463 
474  Result<std::vector<Gate*>> get_complex_gate_chain(Gate* start_gate,
475  const std::vector<GateType*>& chain_types,
476  const std::map<GateType*, std::vector<const GatePin*>>& input_pins = {},
477  const std::map<GateType*, std::vector<const GatePin*>>& output_pins = {},
478  const std::function<bool(const Gate*)>& filter = nullptr) const;
479 
491  Result<std::vector<Net*>> get_common_inputs(const std::vector<Gate*>& gates, u32 threshold = 0) const;
492 
508  TraversalCache make_traversal_cache(TraversalDirection direction,
509  std::function<bool(const Gate*)> match,
510  TraversalStop stop,
511  std::function<bool(const Endpoint*)> exit_endpoint_filter = nullptr,
512  std::function<bool(const Endpoint*)> entry_endpoint_filter = nullptr) const;
513 
524  Result<std::set<Gate*>> get_gates(const Net* net, TraversalCache& cache) const;
525 
536  Result<std::set<Gate*>> get_gates(const Gate* gate, TraversalCache& cache) const;
537 
538  private:
554  Result<std::set<Gate*>> get_gates_memoized(const Net* start,
555  bool successors,
556  const std::function<bool(const Gate*)>& match,
557  TraversalStop stop,
558  const std::function<bool(const Endpoint*)>& exit_endpoint_filter,
559  const std::function<bool(const Endpoint*)>& entry_endpoint_filter,
560  std::unordered_map<const Net*, std::set<Gate*>>& store) const;
561 
567  Result<std::optional<std::vector<Gate*>>> get_shortest_path_to(const Gate* start_gate,
568  const std::function<bool(const Gate*)>& is_target,
569  const PinDirection& direction,
570  const std::function<bool(const Endpoint*, u32 current_depth)>& exit_endpoint_filter,
571  const std::function<bool(const Endpoint*, u32 current_depth)>& entry_endpoint_filter) const;
572 
573  const Netlist& m_netlist;
574  };
575 } // namespace hal
#define NETLIST_API
Definition: arch_linux.h:30
Definition: gate.h:58
Definition: net.h:58
TraversalCache(TraversalCache &&)=default
TraversalCache & operator=(TraversalCache &&)=default
TraversalCache(const TraversalCache &)=delete
TraversalCache & operator=(const TraversalCache &)=delete
uint32_t u32
Definition: defines.h:41
std::vector< Gate * > get_next_sequential_gates(const Gate *gate, bool get_successors, std::unordered_map< u32, std::vector< Gate * >> &cache)
std::vector< Gate * > get_shortest_path(Gate *start_gate, Gate *end_gate, bool search_both_directions=false)
std::vector< Net * > get_common_inputs(const std::vector< Gate * > &gates, u32 threshold=0)
Definition: defines.h:45
PinDirection
Definition: pin_direction.h:36
Net * net
PinDirection direction