pcode-graph 4.0.0__py3-none-any.whl → 4.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,8 @@
1
1
  from enum import IntFlag, auto
2
- from typing import NamedTuple
3
- import networkx as nx
4
-
5
-
2
+ from typing import Callable, NamedTuple
3
+ import igraph
6
4
  from pcode_graph.nx_exporter import export_to_networkx
7
- from pcode_graph.graph import CDG, EdgeKinds, NodeKinds
5
+ from pcode_graph.graph import CDG, Edge, EdgeKinds, NodeKinds
8
6
  from pcode_graph.pcode import OpCodes
9
7
 
10
8
 
@@ -29,51 +27,38 @@ def count_edges(cdg: CDG) -> GraphCharacteristics:
29
27
  return GraphCharacteristics(data_only, len(cdg.edges) - data_only, len(cdg.edges))
30
28
 
31
29
 
32
- def get_graph_diameters(cdg: CDG) -> GraphCharacteristics:
33
- """Computes the diameter of the given graph."""
34
-
35
- n = len(cdg.nodes)
36
-
37
- def build_adjacency(edges):
38
- adj = [[] for _ in range(n)]
39
- for e in edges:
40
- adj[e.source_node].append(e.destination_node)
41
- return adj
42
-
43
- data_edges = []
44
- ctrl_edges = []
45
- for e in cdg.edges:
46
- if e.kind == EdgeKinds.Data:
47
- data_edges.append(e)
48
- else:
49
- ctrl_edges.append(e)
50
-
51
- def diameter(adjacency) -> int:
52
- # Compute graph diameter with BFS
53
- d = 0
54
- for src in range(n):
55
- dist = [-1] * n
56
- dist[src] = 0
57
- front = deque((src,))
58
- while front:
59
- current = front.popleft()
60
- for neigbor in adjacency[current]:
61
- if dist[neigbor] == -1:
62
- dist[neigbor] = dist[current] + 1
63
- front.append(neigbor)
64
- d = max(d, max(dist))
65
- return d
30
+ def get_graph_diameter(
31
+ cdg: CDG,
32
+ edge_filter: Callable[[Edge], bool] = lambda e: True,
33
+ ) -> int:
34
+ """Computes the undirected diameter of the given graph."""
35
+
36
+ undirected = igraph.Graph(
37
+ n=len(cdg.nodes),
38
+ edges=[
39
+ (edge.source_node, edge.destination_node)
40
+ for edge in cdg.edges
41
+ if edge_filter(edge)
42
+ ],
43
+ directed=True,
44
+ )
45
+ undirected.simplify()
46
+ return undirected.diameter(directed=True, unconn=True)
47
+
66
48
 
49
+ def get_graph_diameters(
50
+ cdg: CDG,
51
+ ) -> GraphCharacteristics:
67
52
  return GraphCharacteristics(
68
- diameter(build_adjacency(data_edges)),
69
- diameter(build_adjacency(ctrl_edges)),
70
- diameter(build_adjacency(chain(data_edges, ctrl_edges))),
53
+ data_only=get_graph_diameter(cdg, lambda e: e.kind == EdgeKinds.Data),
54
+ control_only=get_graph_diameter(cdg, lambda e: e.kind != EdgeKinds.Data),
55
+ both=get_graph_diameter(cdg),
71
56
  )
72
57
 
73
58
 
74
59
  class FunctionCharacteristics(IntFlag):
75
60
  """Helps evaluating the level of difficulty of a function,
76
- regarding miscalleneous ML tasks."""
61
+ regarding miscellaneous ML tasks."""
77
62
 
78
63
  data = auto()
79
64
  condition = auto()
@@ -83,18 +68,25 @@ class FunctionCharacteristics(IntFlag):
83
68
  loop = auto()
84
69
 
85
70
 
71
+ def has_loops(cdg: CDG) -> bool:
72
+ """Returns True if the given graph has loops."""
73
+ edges = []
74
+ for edge in cdg.edges:
75
+ if edge.kind == EdgeKinds.Control:
76
+ edges.append((edge.source_node, edge.destination_node))
77
+ elif edge.kind == EdgeKinds.BasicBlockMembership:
78
+ # Reverse membership to create cycles
79
+ edges.append((edge.destination_node, edge.source_node))
80
+ return not igraph.Graph(n=len(cdg.nodes), edges=edges, directed=True).is_dag()
81
+
82
+
86
83
  def get_function_characteristics(graph: CDG) -> FunctionCharacteristics:
87
84
  """Characterizes a function regarding control flow features."""
88
85
 
89
86
  # Every function contains data-flow easiest level
90
87
  function_characteristics = FunctionCharacteristics.data
91
88
 
92
- ng = export_to_networkx(graph, False)
93
- try:
94
- nx.find_cycle(ng)
95
- except nx.NetworkXNoCycle:
96
- pass
97
- else:
89
+ if has_loops(graph):
98
90
  function_characteristics |= FunctionCharacteristics.loop
99
91
 
100
92
  for n in graph.nodes:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: pcode_graph
3
- Version: 4.0.0
3
+ Version: 4.1.0
4
4
  Summary: Build, display and vectorize semantic graphs from binary code
5
5
  Project-URL: Homepage, https://github.com/quarkslab/pcode_graph
6
6
  Project-URL: Issues, https://github.com/quarkslab/pcode_graph/issues
@@ -12,6 +12,7 @@ Classifier: Programming Language :: Python :: 3
12
12
  Classifier: Topic :: Security
13
13
  Requires-Python: >=3.12
14
14
  Requires-Dist: docopt
15
+ Requires-Dist: igraph~=1.0.0
15
16
  Requires-Dist: keystone-engine~=0.9.2
16
17
  Requires-Dist: lief~=1.0.0
17
18
  Requires-Dist: loguru
@@ -4,7 +4,7 @@ pcode_graph/arch.py,sha256=5_nRqA2sgz7Ne_8M7_NT9YDUftB88EeOSt1o0ARhksg,324
4
4
  pcode_graph/asm.py,sha256=qXG8hkVbhD9AicjOMEjAWUt6UxpHZJkYhijaWgmou6A,1752
5
5
  pcode_graph/basic_blocks.py,sha256=bgVkXS2lNWsCxsVM8L7vQQdgZuhW_s61XBQ0iliknXQ,7289
6
6
  pcode_graph/bit_utils.py,sha256=rEEfiJevPc-QMYVryZVwWPBOufC3MyA3bZZ8y9Q4aLU,405
7
- pcode_graph/characterization.py,sha256=xcJFhgzDoanuysOlw5WbJJLNYU4AwxJcT3rKcJe1PEs,3229
7
+ pcode_graph/characterization.py,sha256=iOiwJ8PHbCWRHrv9bLI92mfgqrxjHZSiH696oAdaS8A,3213
8
8
  pcode_graph/checker.py,sha256=OieaYXDnk0QHhYVO12ckzCNPH16DQ3vb0Q9pOO7TR-0,9293
9
9
  pcode_graph/cli.py,sha256=eVGxXeg4QUf8MRfs7df3sqTf9iV5w501HQNWHdZ8F1o,7143
10
10
  pcode_graph/emulator.py,sha256=7r1aMoTKDc6j_cT8V2vCcts4Mg2FMmvdoP8Y3H12RDY,13366
@@ -30,8 +30,8 @@ pcode_graph/analysis/pass_base.py,sha256=IeG5NiEz_Mez-ftjPA2GmC_oT7j7gPAQ6fRClGh
30
30
  pcode_graph/analysis/passes.py,sha256=aRzbEqP5uoXZxPcwQppMOZQencOhI8A6xUbyEQoeEKQ,429
31
31
  pcode_graph/analysis/program.py,sha256=ENg3nWOiR-niu-1N5V0x6gbq8Iy1rnWozKmxRgL27lk,9013
32
32
  pcode_graph/analysis/unreachable.py,sha256=Poza5XjP5Nf9fAR_5srGLU2E0Jsl7pmk3m3QEYBgDgo,505
33
- pcode_graph-4.0.0.dist-info/METADATA,sha256=XybvcFBKCPJHEiFzI0J6PDTzW82FJjtTUvGC1RUB2bo,6646
34
- pcode_graph-4.0.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
35
- pcode_graph-4.0.0.dist-info/entry_points.txt,sha256=7pTTgqjYSRYwmBdA0GwJYXPfOmdk8I_mGsJOHHx-PXQ,45
36
- pcode_graph-4.0.0.dist-info/licenses/LICENSE,sha256=iVkUrjpGBX_59jOK6a7mKkvjHUCGfiCFQRgrziEh-nY,1066
37
- pcode_graph-4.0.0.dist-info/RECORD,,
33
+ pcode_graph-4.1.0.dist-info/METADATA,sha256=_gMJkQ_J5gDCgtIih1yOrRWYDui3v3f14xRYk8CODv4,6675
34
+ pcode_graph-4.1.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
35
+ pcode_graph-4.1.0.dist-info/entry_points.txt,sha256=7pTTgqjYSRYwmBdA0GwJYXPfOmdk8I_mGsJOHHx-PXQ,45
36
+ pcode_graph-4.1.0.dist-info/licenses/LICENSE,sha256=iVkUrjpGBX_59jOK6a7mKkvjHUCGfiCFQRgrziEh-nY,1066
37
+ pcode_graph-4.1.0.dist-info/RECORD,,