HAL  v4.5.0-299-g9ee015483
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 <vector>
39 
40 namespace hal
41 {
42  class Netlist;
43  class Gate;
44  class Net;
45 
46  namespace cte
47  {
48  enum PtrType { UNKNOWN, GATE, NET };
49 
50  enum class Severity { warning, error };
51 
52  enum class DiagnosticCode {
58  };
59 
60  struct Diagnostic
61  {
65  const void *subject_ptr;
67  std::string message;
68  };
69 
70  // a root-to-sink path through the clock tree
71  using Branch = std::vector<std::pair<const void *, PtrType>>;
72 
73  struct VoidPtrHash
74  {
75  std::size_t operator()( const std::pair<const void *, const void *> &pair ) const noexcept
76  {
77  return std::hash<const void *>()( pair.first ) ^ ( std::hash<const void *>()( pair.second ) << 1 );
78  }
79  };
80 
81  struct PairPtrEq
82  {
83  bool operator()( const std::pair<const void *, const void *> &p1,
84  const std::pair<const void *, const void *> &p2 ) const noexcept
85  {
86  return p1.first == p2.first && p1.second == p2.second;
87  }
88  };
89 
90  class ClockTree
91  {
92  public:
93  ~ClockTree();
94 
95  // owns its igraph graph, which must not be copied implicitly: trees are only handed around in a unique_ptr
96  ClockTree( const ClockTree & ) = delete;
97  ClockTree &operator=( const ClockTree & ) = delete;
98  ClockTree( ClockTree && ) = delete;
99  ClockTree &operator=( ClockTree && ) = delete;
100 
101  static Result<std::unique_ptr<ClockTree>> from_netlist( const Netlist *netlist );
102 
104  igraph_neimode_t direction ) const;
105 
106  Result<std::unique_ptr<ClockTree>> get_subtree( const void *ptr, bool parent ) const;
107 
108  Result<igraph_int_t> get_vertex_from_ptr( const void *ptr ) const;
109 
110  [[nodiscard]] Result<std::pair<const void *, PtrType>> get_ptr_from_vertex( igraph_int_t vertex ) const;
111 
112  [[nodiscard]] Result<std::vector<igraph_int_t>>
113  get_vertices_from_ptrs( const std::vector<const void *> &ptrs ) const;
114 
116  get_ptrs_from_vertices( const std::vector<igraph_int_t> &vertices ) const;
117 
118  [[nodiscard]] std::vector<const Gate *> get_gates() const;
119 
120  [[nodiscard]] std::vector<const Net *> get_nets() const;
121 
122  [[nodiscard]] const std::unordered_map<const void *, PtrType> &get_all() const;
123 
124  [[nodiscard]] const Netlist *get_netlist() const;
125 
126  [[nodiscard]] const igraph_t *get_igraph() const;
127 
128  [[nodiscard]] const std::vector<Diagnostic> &get_diagnostics() const;
129 
130  [[nodiscard]] const std::unordered_set<igraph_int_t> &get_roots() const;
131 
132  private:
133  // takes ownership of graph, which must be initialized; only from_netlist and get_subtree create trees
134  ClockTree( const Netlist *netlist,
135  igraph_t &&graph,
136  std::unordered_set<igraph_int_t> &&roots,
137  std::unordered_map<igraph_int_t, const void *> &&vertices_to_ptrs,
138  std::unordered_map<const void *, PtrType> &&ptrs_to_types );
139 
140  const Netlist *m_netlist;
141 
142  igraph_t m_igraph;
143 
144  std::unordered_set<igraph_int_t> m_roots;
145 
146  std::unordered_map<igraph_int_t, const void *> m_vertices_to_ptrs;
147 
148  std::unordered_map<const void *, igraph_int_t> m_ptrs_to_vertices;
149 
150  std::unordered_map<const void *, PtrType> m_ptrs_to_types;
151 
152  std::vector<Diagnostic> m_diagnostics;
153  };
154  } // namespace cte
155 } // namespace hal
ClockTree(const ClockTree &)=delete
std::vector< const Gate * > get_gates() const
Definition: clock_tree.cpp:679
Result< std::vector< std::pair< const void *, PtrType > > > get_neighbors(const void *ptr, igraph_neimode_t direction) const
Definition: clock_tree.cpp:735
const Netlist * get_netlist() const
Definition: clock_tree.cpp:714
std::vector< const Net * > get_nets() const
Definition: clock_tree.cpp:694
const std::vector< Diagnostic > & get_diagnostics() const
Definition: clock_tree.cpp:719
const igraph_t * get_igraph() const
Definition: clock_tree.cpp:729
Result< std::unique_ptr< ClockTree > > get_subtree(const void *ptr, bool parent) const
Definition: clock_tree.cpp:479
Result< std::vector< std::pair< const void *, PtrType > > > get_ptrs_from_vertices(const std::vector< igraph_int_t > &vertices) const
Definition: clock_tree.cpp:661
const std::unordered_set< igraph_int_t > & get_roots() const
Definition: clock_tree.cpp:724
ClockTree & operator=(const ClockTree &)=delete
const std::unordered_map< const void *, PtrType > & get_all() const
Definition: clock_tree.cpp:709
ClockTree(ClockTree &&)=delete
Result< std::vector< igraph_int_t > > get_vertices_from_ptrs(const std::vector< const void * > &ptrs) const
Definition: clock_tree.cpp:642
ClockTree & operator=(ClockTree &&)=delete
Result< std::pair< const void *, PtrType > > get_ptr_from_vertex(igraph_int_t vertex) const
Definition: clock_tree.cpp:631
static Result< std::unique_ptr< ClockTree > > from_netlist(const Netlist *netlist)
Definition: clock_tree.cpp:158
Result< igraph_int_t > get_vertex_from_ptr(const void *ptr) const
Definition: clock_tree.cpp:620
uint32_t u32
Definition: defines.h:41
DiagnosticCode
Definition: clock_tree.h:52
std::vector< std::pair< const void *, PtrType > > Branch
Definition: clock_tree.h:71
Definition: defines.h:45
PinDirection direction
This file contains the class that holds a netlist graph.
const void * subject_ptr
Definition: clock_tree.h:65
std::string message
Definition: clock_tree.h:67
DiagnosticCode code
Definition: clock_tree.h:63
bool operator()(const std::pair< const void *, const void * > &p1, const std::pair< const void *, const void * > &p2) const noexcept
Definition: clock_tree.h:83
std::size_t operator()(const std::pair< const void *, const void * > &pair) const noexcept
Definition: clock_tree.h:75