HAL  v4.5.0-124-g47ab54673
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
netlist_graph.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 <functional>
33 #include <igraph/igraph.h>
34 #include <set>
35 #include <unordered_map>
36 
42 namespace hal
43 {
44  class Netlist;
45  class Gate;
46  class Net;
47 
51  namespace graph_algorithm
52  {
60  {
61  public:
66  enum class Direction
67  {
71  NONE,
72 
76  IN,
77 
81  OUT,
82 
86  ALL
87  };
88 
96  NetlistGraph(Netlist* nl, igraph_t&& graph, std::unordered_map<u32, Gate*>&& nodes_to_gates);
97 
101  ~NetlistGraph();
102 
112  NetlistGraph(const NetlistGraph&) = delete;
113 
115 
127  static Result<std::unique_ptr<NetlistGraph>> from_netlist(Netlist* nl, bool create_dummy_vertices = false, const std::function<bool(const Net*)>& filter = nullptr);
128 
138  static Result<std::unique_ptr<NetlistGraph>> from_netlist_no_edges(Netlist* nl, const std::vector<Gate*>& gates = {});
139 
162  from_gates(const std::vector<Gate*>& gates, const std::set<Gate*>& split_gates = {}, const std::function<bool(const Net*)>& filter = nullptr);
163 
170 
176  Netlist* get_netlist() const;
177 
183  igraph_t* get_graph() const;
184 
193  Result<std::vector<Gate*>> get_gates_from_vertices(const std::vector<u32>& vertices) const;
194 
203  Result<std::vector<Gate*>> get_gates_from_vertices(const std::set<u32>& vertices) const;
204 
213  Result<std::vector<Gate*>> get_gates_from_vertices_igraph(const igraph_vector_int_t* vertices) const;
214 
221  Result<std::set<Gate*>> get_gates_set_from_vertices(const std::vector<u32>& vertices) const;
222 
229  Result<std::set<Gate*>> get_gates_set_from_vertices(const std::set<u32>& vertices) const;
230 
237  Result<std::set<Gate*>> get_gates_set_from_vertices_igraph(const igraph_vector_int_t* vertices) const;
238 
245  Result<Gate*> get_gate_from_vertex(const u32 vertex) const;
246 
253  Result<std::vector<u32>> get_vertices_from_gates(const std::vector<Gate*>& gates) const;
254 
261  Result<std::vector<u32>> get_vertices_from_gates(const std::set<Gate*>& gates) const;
262 
269  Result<igraph_vector_int_t> get_vertices_from_gates_igraph(const std::vector<Gate*>& gates) const;
270 
277  Result<igraph_vector_int_t> get_vertices_from_gates_igraph(const std::set<Gate*>& gates) const;
278 
286 
297  bool is_shadow_vertex(const u32 vertex) const;
298 
307 
314  u32 get_num_vertices(bool only_connected = false) const;
315 
321  u32 get_num_edges() const;
322 
329  Result<std::vector<u32>> get_vertices(bool only_connected = false) const;
330 
337 
344 
353  Result<std::monostate> add_edges(const std::vector<std::pair<Gate*, Gate*>>& edges);
354 
363  Result<std::monostate> add_edges(const std::vector<std::pair<u32, u32>>& edges);
364 
373  Result<std::monostate> add_edges(const std::map<Gate*, std::set<Gate*>>& edges);
374 
381  Result<std::monostate> delete_edges(const std::vector<std::pair<Gate*, Gate*>>& edges);
382 
389  Result<std::monostate> delete_edges(const std::vector<std::pair<u32, u32>>& edges);
390 
394  void print() const;
395 
396  private:
397  NetlistGraph() = delete;
398 
404  NetlistGraph(Netlist* nl);
405 
409  Netlist* m_nl;
410 
414  igraph_t m_graph;
415 
425  bool m_graph_initialized = false;
426 
430  igraph_t* m_graph_ptr = nullptr;
431 
435  std::unordered_map<u32, Gate*> m_nodes_to_gates;
436 
440  std::unordered_map<Gate*, u32> m_gates_to_nodes;
441 
448  std::unordered_map<u32, u32> m_shadow_nodes_to_nodes;
449  };
450  } // namespace graph_algorithm
451 
452  template<>
453  std::map<graph_algorithm::NetlistGraph::Direction, std::string> EnumStrings<graph_algorithm::NetlistGraph::Direction>::data;
454 } // namespace hal
Definition: gate.h:58
Definition: net.h:58
A directed graph corresponding to a netlist.
Definition: netlist_graph.h:60
Netlist * get_netlist() const
Get the netlist associated with the netlist graph.
u32 get_num_vertices(bool only_connected=false) const
Get the number of vertices in the netlist graph.
bool is_shadow_vertex(const u32 vertex) const
Check whether the specified vertex is a shadow vertex.
Result< std::vector< Gate * > > get_gates_from_vertices(const std::vector< u32 > &vertices) const
Get the gates corresponding to the specified vertices.
static Result< std::unique_ptr< NetlistGraph > > from_netlist(Netlist *nl, bool create_dummy_vertices=false, const std::function< bool(const Net *)> &filter=nullptr)
Create a directed graph from a netlist.
Result< Gate * > get_gate_from_vertex(const u32 vertex) const
Get the gate corresponding to the specified vertex.
Result< std::unique_ptr< NetlistGraph > > copy() const
Create a deep copy of the netlist graph.
Result< std::set< Gate * > > get_gates_set_from_vertices(const std::vector< u32 > &vertices) const
Get the gates corresponding to the specified vertices.
~NetlistGraph()
Default destructor for NetlistGraph.
Result< std::set< Gate * > > get_gates_set_from_vertices_igraph(const igraph_vector_int_t *vertices) const
Get the gates corresponding to the specified vertices.
Direction
The direction of exploration within the graph.
Definition: netlist_graph.h:67
@ ALL
Explore in both directions, i.e., treat the graph as undirected.
@ NONE
No direction, invalid default setting.
@ IN
Explore through the inputs of the current node, i.e., traverse backwards.
@ OUT
Explore through the outputs of the current node, i.e., traverse forwards.
Result< u32 > get_vertex_from_gate(Gate *g) const
Get the vertex corresponding to the specified gate.
Result< igraph_vector_int_t > get_vertices_from_gates_igraph(const std::vector< Gate * > &gates) const
Get the vertices corresponding to the specified gates.
Result< std::vector< u32 > > get_vertices_from_gates(const std::vector< Gate * > &gates) const
Get the vertices corresponding to the specified gates.
static Result< std::unique_ptr< NetlistGraph > > from_gates(const std::vector< Gate * > &gates, const std::set< Gate * > &split_gates={}, const std::function< bool(const Net *)> &filter=nullptr)
Create a directed graph from a subset of the gates of a netlist.
Result< std::monostate > add_edges(const std::vector< std::pair< Gate *, Gate * >> &edges)
Add edges between the specified pairs of source and destination gates to the netlist graph.
void print() const
Print the edge list of the graph to stdout.
Result< std::vector< Gate * > > get_gates_from_vertices_igraph(const igraph_vector_int_t *vertices) const
Get the gates corresponding to the specified vertices.
Result< std::vector< std::pair< u32, u32 > > > get_edges() const
Get the edges between vertices in the netlist graph.
u32 get_num_edges() const
Get the number of edges in the netlist graph.
Result< std::vector< std::pair< Gate *, Gate * > > > get_edges_in_netlist() const
Get the edges between gates in the netlist corresponding to the netlist graph.
NetlistGraph & operator=(const NetlistGraph &)=delete
NetlistGraph(const NetlistGraph &)=delete
Result< std::monostate > delete_edges(const std::vector< std::pair< Gate *, Gate * >> &edges)
Delete edges between the specified pairs of source and destination gates from the netlist graph.
Result< std::vector< u32 > > get_vertices(bool only_connected=false) const
Get the vertices in the netlist graph.
static Result< std::unique_ptr< NetlistGraph > > from_netlist_no_edges(Netlist *nl, const std::vector< Gate * > &gates={})
Create an empty directed graph from a netlist.
Result< std::vector< u32 > > get_all_vertices_from_gate(Gate *g) const
Get all vertices corresponding to the specified gate, i.e., its primary vertex and,...
igraph_t * get_graph() const
Get the graph object of the netlist graph.
uint32_t u32
Definition: defines.h:41
Definition: defines.h:45