Skip to content

Commit a024d5c

Browse files
authored
feat: add compare for high level graph edge (#8)
* feat: add equal for EdgeStyle * feat: low and medium edge * feat: low and medium edge new * fix: ignore Folder.DotSettings.user * fix: hlil block * feat: add compare for HighLevelILFlowGraphEdge
1 parent c58f805 commit a024d5c

15 files changed

+355
-113
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ obj/
44
bin/
55
dist/
66
build/
7-
Folder.DotSettings.user/
7+
Folder.DotSettings.user

Handle/BNFlowGraphNode.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,5 @@ public void SetOutgoingEdgePoints(ulong edgeNum, Point[] points)
155155
(ulong)points.Length
156156
);
157157
}
158-
159-
160-
161158
}
162159
}

HighLevelIL/HighLevelILBasicBlock.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace BinaryNinja
66
{
77
public sealed class HighLevelILBasicBlock : AbstractBasicBlock<HighLevelILBasicBlock>
88
{
9-
internal HighLevelILFunction ILFunction { get; }
9+
public HighLevelILFunction ILFunction { get; }
1010

1111
internal HighLevelILBasicBlock(
1212
HighLevelILFunction function ,
@@ -46,7 +46,6 @@ IntPtr handle
4646
true
4747
);
4848
}
49-
5049

5150
public HighLevelILInstruction this[HighLevelILInstructionIndex index]
5251
{
@@ -290,7 +289,6 @@ public HighLevelILBasicBlock[] PostDominatorTreeChildren
290289
}
291290
}
292291

293-
294292
public HighLevelILBasicBlock[] GetDominanceFrontier(bool post)
295293
{
296294
ulong arrayLength = 0;

HighLevelIL/HighLevelILFlowGraph.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace BinaryNinja
44
{
55
public sealed class HighLevelILFlowGraph : AbstractFlowGraph<HighLevelILFlowGraph>
66
{
7-
internal HighLevelILFunction ILFunction { get; set; }
7+
public HighLevelILFunction ILFunction { get; }
88

99
internal HighLevelILFlowGraph(
1010
HighLevelILFunction ilFunction,
@@ -95,8 +95,6 @@ internal static HighLevelILFlowGraph MustBorrowHandleEx(
9595
return new HighLevelILFlowGraph(ilFunction , handle, false);
9696
}
9797

98-
99-
10098
public HighLevelILFlowGraph? Update()
10199
{
102100
return HighLevelILFlowGraph.TakeHandleEx(
@@ -130,8 +128,7 @@ out ulong arrayLength
130128
NativeMethods.BNGetFlowGraphNode(this.handle, index)
131129
);
132130
}
133-
134-
131+
135132
public HighLevelILFlowGraphNode[] GetNodesInRegion(
136133
int left ,
137134
int top ,
Lines changed: 114 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,128 @@
1+
using System;
2+
13
namespace BinaryNinja
24
{
3-
public sealed class HighLevelILFlowGraphEdge : AbstractFlowGraphEdge<HighLevelILFlowGraphNode>
5+
public sealed class HighLevelILFlowGraphEdge
6+
: AbstractFlowGraphEdge<HighLevelILFlowGraphNode>,
7+
IEquatable<HighLevelILFlowGraphEdge>,
8+
IComparable<HighLevelILFlowGraphEdge>
49
{
5-
internal HighLevelILFunction ILFunction { get; set; }
6-
710
internal HighLevelILFlowGraphEdge(
8-
HighLevelILFunction ilFunction,
9-
BNFlowGraphEdge native)
10-
: base(native , HighLevelILFlowGraphNode.NewFromHandleEx(ilFunction, native.target))
11+
BNFlowGraphEdge native,
12+
HighLevelILFlowGraphNode source,
13+
HighLevelILFlowGraphNode target,
14+
bool outgoing
15+
) : base(native , source, target, outgoing)
1116
{
12-
this.ILFunction = ilFunction;
17+
1318
}
1419

1520
internal static HighLevelILFlowGraphEdge FromNativeEx(
16-
HighLevelILFunction ilFunction,
17-
BNFlowGraphEdge native
21+
BNFlowGraphEdge native,
22+
HighLevelILFlowGraphNode me,
23+
bool outgoing
1824
)
1925
{
20-
return new HighLevelILFlowGraphEdge(
21-
ilFunction ,
22-
native
26+
if (outgoing)
27+
{
28+
return new HighLevelILFlowGraphEdge(
29+
native,
30+
me ,
31+
HighLevelILFlowGraphNode.MustNewFromHandleEx(me.ILFunction, native.target),
32+
outgoing
33+
);
34+
}
35+
else
36+
{
37+
return new HighLevelILFlowGraphEdge(
38+
native,
39+
HighLevelILFlowGraphNode.MustNewFromHandleEx(me.ILFunction,native.target) ,
40+
me,
41+
outgoing
42+
);
43+
}
44+
}
45+
46+
public override bool Equals(object? other)
47+
{
48+
return this.Equals(other as HighLevelILFlowGraphEdge);
49+
}
50+
51+
public bool Equals(HighLevelILFlowGraphEdge? other)
52+
{
53+
if (other is null)
54+
{
55+
return false;
56+
}
57+
58+
if (ReferenceEquals(this , other))
59+
{
60+
return true;
61+
}
62+
63+
if (this.Type != other.Type)
64+
{
65+
return false;
66+
}
67+
68+
if (this.Source != other.Source)
69+
{
70+
return false;
71+
}
72+
73+
if (this.Target != other.Target)
74+
{
75+
return false;
76+
}
77+
78+
return true;
79+
}
80+
81+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "CA2218:OverrideGetHashCodeOnOverridingEquals")]
82+
public override int GetHashCode()
83+
{
84+
return HashCode.Combine<uint,int,int>(
85+
(uint)this.Type,
86+
this.Source.GetHashCode(),
87+
this.Target.GetHashCode()
2388
);
2489
}
90+
91+
public static bool operator ==(HighLevelILFlowGraphEdge? left, HighLevelILFlowGraphEdge? right)
92+
{
93+
if (left is null)
94+
{
95+
return right is null;
96+
}
97+
98+
return left.Equals(right);
99+
}
100+
101+
public static bool operator !=(HighLevelILFlowGraphEdge? left, HighLevelILFlowGraphEdge? right)
102+
{
103+
return !(left == right);
104+
}
105+
106+
public int CompareTo(HighLevelILFlowGraphEdge? other)
107+
{
108+
if (other is null)
109+
{
110+
return 1;
111+
}
112+
113+
int result = this.Type.CompareTo(other.Type);
114+
115+
if (0 == result)
116+
{
117+
result = this.Source.CompareTo(other.Source);
118+
}
119+
120+
if (0 == result)
121+
{
122+
result = this.Target.CompareTo(other.Target);
123+
}
124+
125+
return result;
126+
}
25127
}
26128
}

HighLevelIL/HighLevelILFlowGraphNode.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace BinaryNinja
44
{
55
public sealed class HighLevelILFlowGraphNode : FlowGraphNode
66
{
7-
internal HighLevelILFunction ILFunction { get; set; }
7+
public HighLevelILFunction ILFunction { get; }
88

99
internal HighLevelILFlowGraphNode(
1010
HighLevelILFunction ilFunction,
@@ -107,7 +107,6 @@ internal static HighLevelILFlowGraphNode MustBorrowHandleEx(
107107
false);
108108
}
109109

110-
111110
public HighLevelILBasicBlock? BasicBlock
112111
{
113112
get
@@ -131,17 +130,19 @@ public HighLevelILFlowGraphEdge[] IncomingEdges
131130
{
132131
get
133132
{
134-
ulong arrayLength = 0;
135-
136133
IntPtr arrayPointer = NativeMethods.BNGetFlowGraphNodeIncomingEdges(
137134
this.handle,
138-
out arrayLength
135+
out ulong arrayLength
139136
);
140137

141138
return UnsafeUtils.TakeStructArrayEx<BNFlowGraphEdge,HighLevelILFlowGraphEdge>(
142139
arrayPointer,
143140
arrayLength,
144-
(_native) => HighLevelILFlowGraphEdge.FromNativeEx(this.ILFunction, _native),
141+
(_native) => HighLevelILFlowGraphEdge.FromNativeEx(
142+
_native,
143+
this,
144+
false
145+
),
145146
NativeMethods.BNFreeFlowGraphNodeEdgeList
146147
);
147148
}
@@ -151,17 +152,19 @@ public HighLevelILFlowGraphEdge[] OutgoingEdges
151152
{
152153
get
153154
{
154-
ulong arrayLength = 0;
155-
156155
IntPtr arrayPointer = NativeMethods.BNGetFlowGraphNodeOutgoingEdges(
157156
this.handle,
158-
out arrayLength
157+
out ulong arrayLength
159158
);
160159

161160
return UnsafeUtils.TakeStructArrayEx<BNFlowGraphEdge,HighLevelILFlowGraphEdge>(
162161
arrayPointer,
163162
arrayLength,
164-
(_native) => HighLevelILFlowGraphEdge.FromNativeEx(this.ILFunction, _native),
163+
(_native) => HighLevelILFlowGraphEdge.FromNativeEx(
164+
_native,
165+
this,
166+
true
167+
),
165168
NativeMethods.BNFreeFlowGraphNodeEdgeList
166169
);
167170
}

LowLevelIL/LowLevelILFlowGraphEdge.cs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,39 @@ namespace BinaryNinja
33
public sealed class LowLevelILFlowGraphEdge : AbstractFlowGraphEdge<LowLevelILFlowGraphNode>
44
{
55
public LowLevelILFlowGraphEdge(
6-
LowLevelILFunction ilFunction,
7-
BNFlowGraphEdge native)
8-
: base(native , LowLevelILFlowGraphNode.NewFromHandleEx(ilFunction, native.target))
6+
BNFlowGraphEdge native,
7+
LowLevelILFlowGraphNode source,
8+
LowLevelILFlowGraphNode target,
9+
bool outgoing
10+
) : base(native , source, target, outgoing)
911
{
1012

1113
}
1214

1315
internal static LowLevelILFlowGraphEdge FromNativeEx(
14-
LowLevelILFunction ilFunction,
15-
BNFlowGraphEdge native
16+
BNFlowGraphEdge native,
17+
LowLevelILFlowGraphNode me,
18+
bool outgoing
1619
)
1720
{
18-
return new LowLevelILFlowGraphEdge(
19-
ilFunction ,
20-
native
21-
);
21+
if (outgoing)
22+
{
23+
return new LowLevelILFlowGraphEdge(
24+
native,
25+
me ,
26+
LowLevelILFlowGraphNode.MustNewFromHandleEx(me.ILFunction, native.target),
27+
outgoing
28+
);
29+
}
30+
else
31+
{
32+
return new LowLevelILFlowGraphEdge(
33+
native,
34+
LowLevelILFlowGraphNode.MustNewFromHandleEx(me.ILFunction,native.target) ,
35+
me,
36+
outgoing
37+
);
38+
}
2239
}
2340
}
2441
}

LowLevelIL/LowLevelILFlowGraphNode.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,17 +131,19 @@ public LowLevelILFlowGraphEdge[] IncomingEdges
131131
{
132132
get
133133
{
134-
ulong arrayLength = 0;
135-
136134
IntPtr arrayPointer = NativeMethods.BNGetFlowGraphNodeIncomingEdges(
137135
this.handle,
138-
out arrayLength
136+
out ulong arrayLength
139137
);
140138

141139
return UnsafeUtils.TakeStructArrayEx<BNFlowGraphEdge,LowLevelILFlowGraphEdge>(
142140
arrayPointer,
143141
arrayLength,
144-
(_native) => LowLevelILFlowGraphEdge.FromNativeEx(this.ILFunction, _native),
142+
(_native) => LowLevelILFlowGraphEdge.FromNativeEx(
143+
_native,
144+
this,
145+
false
146+
),
145147
NativeMethods.BNFreeFlowGraphNodeEdgeList
146148
);
147149
}
@@ -151,17 +153,19 @@ public LowLevelILFlowGraphEdge[] OutgoingEdges
151153
{
152154
get
153155
{
154-
ulong arrayLength = 0;
155-
156156
IntPtr arrayPointer = NativeMethods.BNGetFlowGraphNodeOutgoingEdges(
157157
this.handle,
158-
out arrayLength
158+
out ulong arrayLength
159159
);
160160

161161
return UnsafeUtils.TakeStructArrayEx<BNFlowGraphEdge,LowLevelILFlowGraphEdge>(
162162
arrayPointer,
163163
arrayLength,
164-
(_native) => LowLevelILFlowGraphEdge.FromNativeEx(this.ILFunction, _native),
164+
(_native) => LowLevelILFlowGraphEdge.FromNativeEx(
165+
_native,
166+
this,
167+
true
168+
),
165169
NativeMethods.BNFreeFlowGraphNodeEdgeList
166170
);
167171
}

0 commit comments

Comments
 (0)