HAL  v4.5.0-124-g47ab54673
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
clock_tree.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 // Copyright (c) 2025-2026 Sascha Tommasone. All rights reserved.
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 //
16 // The above copyright notice and this permission notice shall be included in all
17 // copies or substantial portions of the Software.
18 //
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 // SOFTWARE.
26 
27 #pragma once
28 
30 #include "hal_core/defines.h"
32 
33 #include <igraph/igraph.h>
34 #include <memory>
35 #include <string>
36 #include <unordered_map>
37 #include <unordered_set>
38 #include <variant>
39 #include <vector>
40 
41 namespace hal
42 {
43  class Netlist;
44 }
45 
46 namespace hal
47 {
48  class Gate;
49 }
50 
51 namespace hal
52 {
53  class Net;
54 }
55 
56 namespace hal
57 {
58  namespace cte
59  {
60  enum PtrType { UNKNOWN, GATE, NET };
61 
62  struct VoidPtrHash
63  {
64  std::size_t operator()( const std::pair<const void *, const void *> &pair ) const noexcept
65  {
66  return std::hash<const void *>()( pair.first ) ^ ( std::hash<const void *>()( pair.second ) << 1 );
67  }
68  };
69 
70  struct PairPtrEq
71  {
72  bool operator()( const std::pair<const void *, const void *> &p1,
73  const std::pair<const void *, const void *> &p2 ) const noexcept
74  {
75  return p1.first == p2.first && p1.second == p2.second;
76  }
77  };
78 
79  class ClockTree
80  {
81  public:
82  ClockTree( const Netlist *netlist,
83  igraph_t &&graph,
84  std::unordered_set<igraph_integer_t> &&roots,
85  std::unordered_map<igraph_integer_t, const void *> &&m_vertices_to_ptrs,
86  std::unordered_map<const void *, PtrType> &&m_ptrs_to_types );
87 
88  ~ClockTree();
89 
90  static Result<std::unique_ptr<ClockTree>> from_netlist( const Netlist *netlist );
91 
92  Result<std::monostate> export_dot( const std::string &pathname ) const;
93 
95  igraph_neimode_t direction ) const;
96 
97  Result<std::unique_ptr<ClockTree>> get_subtree( const void *ptr, const bool parent ) const;
98 
99  Result<igraph_integer_t> get_vertex_from_ptr( const void *ptr ) const;
100 
101  Result<std::pair<const void *, PtrType>> get_ptr_from_vertex( const igraph_integer_t vertex ) const;
102 
103  Result<std::vector<igraph_integer_t>> get_vertices_from_ptrs( const std::vector<const void *> &ptrs ) const;
104 
106  get_ptrs_from_vertices( const std::vector<igraph_integer_t> &vertices ) const;
107 
108  const std::vector<const Gate *> get_gates() const;
109 
110  const std::vector<const Net *> get_nets() const;
111 
112  const std::unordered_map<const void *, PtrType> get_all() const;
113 
114  const Netlist *get_netlist() const;
115 
116  const igraph_t *get_igraph() const;
117 
118  private:
119  ClockTree() = delete;
120 
121  ClockTree( const Netlist *netlist );
122 
123  const Netlist *m_netlist;
124 
125  igraph_t m_igraph;
126 
127  igraph_t *m_igraph_ptr;
128 
129  std::unordered_set<igraph_integer_t> m_roots;
130 
131  std::unordered_map<igraph_integer_t, const void *> m_vertices_to_ptrs;
132 
133  std::unordered_map<const void *, igraph_integer_t> m_ptrs_to_vertices;
134 
135  std::unordered_map<const void *, PtrType> m_ptrs_to_types;
136  };
137  } // namespace cte
138 } // namespace hal
Result< std::vector< std::pair< const void *, PtrType > > > get_neighbors(const void *ptr, igraph_neimode_t direction) const
Definition: clock_tree.cpp:803
Result< igraph_integer_t > get_vertex_from_ptr(const void *ptr) const
Definition: clock_tree.cpp:697
const Netlist * get_netlist() const
Definition: clock_tree.cpp:792
const igraph_t * get_igraph() const
Definition: clock_tree.cpp:797
Result< std::vector< igraph_integer_t > > get_vertices_from_ptrs(const std::vector< const void * > &ptrs) const
Definition: clock_tree.cpp:720
const std::vector< const Gate * > get_gates() const
Definition: clock_tree.cpp:757
Result< std::pair< const void *, PtrType > > get_ptr_from_vertex(const igraph_integer_t vertex) const
Definition: clock_tree.cpp:708
const std::vector< const Net * > get_nets() const
Definition: clock_tree.cpp:772
Result< std::monostate > export_dot(const std::string &pathname) const
Definition: clock_tree.cpp:423
static Result< std::unique_ptr< ClockTree > > from_netlist(const Netlist *netlist)
Definition: clock_tree.cpp:160
Result< std::unique_ptr< ClockTree > > get_subtree(const void *ptr, const bool parent) const
Definition: clock_tree.cpp:561
Result< std::vector< std::pair< const void *, PtrType > > > get_ptrs_from_vertices(const std::vector< igraph_integer_t > &vertices) const
Definition: clock_tree.cpp:739
const std::unordered_map< const void *, PtrType > get_all() const
Definition: clock_tree.cpp:787
Definition: defines.h:45
PinDirection direction
This file contains the class that holds a netlist graph.
bool operator()(const std::pair< const void *, const void * > &p1, const std::pair< const void *, const void * > &p2) const noexcept
Definition: clock_tree.h:72
std::size_t operator()(const std::pair< const void *, const void * > &pair) const noexcept
Definition: clock_tree.h:64