HAL  v4.5.0-259-g577210b12
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
branches.cpp
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 
28 
29 #include <algorithm>
30 #include <igraph/igraph.h>
31 #include <utility>
32 #include <variant>
33 
34 namespace hal::cte
35 {
36  namespace
37  {
38  Result<std::monostate> recover_branches_for_root( const ClockTree &clock_tree,
39  const std::pair<const void *, PtrType> node,
40  Branch &branch,
41  std::vector<Branch> &branches )
42  {
43  branch.push_back( node );
44 
45  const auto children = clock_tree.get_neighbors( node.first, IGRAPH_OUT );
46  if( children.is_error() )
47  {
48  return ERR( children.get_error().get() );
49  }
50 
51  if( children.get().empty() )
52  {
53  branches.push_back( branch );
54  }
55  else
56  {
57  for( const auto &child : children.get() )
58  {
59  // prevent loop-caused runtime explosion (e.g., ring-oscillator)
60  if( std::find( branch.begin(), branch.end(), child ) != branch.end() )
61  {
62  continue;
63  }
64 
65  const auto res = recover_branches_for_root( clock_tree, child, branch, branches );
66  if( res.is_error() )
67  {
68  return ERR( res.get_error().get() );
69  }
70  }
71  }
72 
73  branch.pop_back();
74  return OK( {} );
75  }
76  } // namespace
77 
79  {
80  Branch branch;
81  std::vector<Branch> branches;
82 
83  for( const igraph_int_t root : clock_tree.get_roots() )
84  {
85  const auto root_ptr = clock_tree.get_ptr_from_vertex( root );
86  if( root_ptr.is_error() )
87  {
88  return ERR( root_ptr.get_error().get() );
89  }
90 
91  const auto result = recover_branches_for_root( clock_tree, root_ptr.get(), branch, branches );
92  if( result.is_error() )
93  {
94  return ERR( result.get_error().get() );
95  }
96  }
97 
98  return OK( std::move( branches ) );
99  }
100 } // namespace hal::cte
const std::unordered_set< igraph_int_t > & get_roots() const
Definition: clock_tree.cpp:724
Result< std::pair< const void *, PtrType > > get_ptr_from_vertex(igraph_int_t vertex) const
Definition: clock_tree.cpp:631
#define ERR(message)
Definition: result.h:60
#define OK(...)
Definition: result.h:56
Result< std::vector< Branch > > recover_branches(const ClockTree &clock_tree)
Definition: branches.cpp:78
std::vector< std::pair< const void *, PtrType > > Branch
Definition: clock_tree.h:71