-
Notifications
You must be signed in to change notification settings - Fork 0
/
NativeInstruction.cs
188 lines (168 loc) · 7 KB
/
NativeInstruction.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System;
using System.Runtime.InteropServices;
using Gee.External.Capstone.Arm;
using Gee.External.Capstone.Arm64;
using Gee.External.Capstone.X86;
namespace Gee.External.Capstone {
/// <summary>
/// Native Instruction.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public unsafe struct NativeInstruction {
/// <summary>
/// Instruction's Unique Identifier.
/// </summary>
public uint Id;
/// <summary>
/// Instruction's Address (EIP).
/// </summary>
public ulong Address;
/// <summary>
/// Instruction's Size.
/// </summary>
public ushort Size;
/// <summary>
/// Instruction's Machine Bytes.
/// </summary>
public fixed byte Bytes [16];
/// <summary>
/// Instruction's Mnemonic ASCII Text.
/// </summary>
public fixed byte Mnemonic [32];
/// <summary>
/// Instruction's Operand ASCII Text.
/// </summary>
public fixed byte Operand [160];
/// <summary>
/// Instruction's Architecture Independent Detail.
/// </summary>
public IntPtr IndependentDetail;
/// <summary>
/// Get Instruction's Managed Machine Bytes.
/// </summary>
/// <value>
/// Convenient property to retrieve the instruction's machine bytes as a managed collection. The size of
/// the managed collection will always be equal to the value represented by <c>NativeInstruction.Size</c>.
/// This property allocates managed memory for a new managed collection and uses direct memory copying to
/// copy the collection from unmanaged memory to managed memory every time it is invoked.
/// </value>
public byte[] ManagedBytes {
get {
fixed (byte* pBytes = this.Bytes) {
var pPBytes = new IntPtr(pBytes);
var managedBytes = new byte[this.Size];
Marshal.Copy(pPBytes, managedBytes, 0, this.Size);
return managedBytes;
}
}
}
/// <summary>
/// Get Instruction's Managed Mnemonic Text.
/// </summary>
/// <value>
/// Convenient property to retrieve the instruction's mnemonic ASCII text as a managed string. This
/// property allocates managed memory for a new managed string every time it is invoked.
/// </value>
/// <exception cref="System.ArgumentException">
/// Thrown if a managed string could not be initialized.
/// </exception>
/// <exception cref="System.ArgumentOutOfRangeException">
/// Thrown if the unmanaged string is too large to allocate in managed memory.
/// </exception>
/// <exception cref="System.AccessViolationException">
/// Thrown if the unmanaged string is inaccessible.
/// </exception>
public string ManagedMnemonic {
get {
fixed (byte* pMnemonic = this.Mnemonic) {
return new string((sbyte*) pMnemonic);
}
}
}
/// <summary>
/// Get Instruction's Managed Operand Text.
/// </summary>
/// <value>
/// Convenient property to retrieve the instruction's operand ASCII text as a managed string. This
/// property allocates managed memory for a new managed string every time it is invoked.
/// </value>
/// <exception cref="System.ArgumentException">
/// Thrown if a managed string could not be initialized.
/// </exception>
/// <exception cref="System.ArgumentOutOfRangeException">
/// Thrown if the unmanaged string is too large to allocate in managed memory.
/// </exception>
/// <exception cref="System.AccessViolationException">
/// Thrown if the unmanaged string is inaccessible.
/// </exception>
public string ManagedOperand {
get {
fixed (byte* pOperand = this.Operand) {
// Create Managed String.
//
// Throws an exception if the operation fails.
return new string((sbyte*) pOperand);
}
}
}
/// <summary>
/// Get Instruction's Managed Architecture Independent Detail.
/// </summary>
/// <value>
/// Convenient property to retrieve the instruction's architecture independent detail as a managed
/// structure. This property allocates managed memory for a new managed structure every time it is
/// invoked. If <c>NativeInstruction.IndependentDetail</c> is equal to <c>IntPtr.Zero</c>, a null
/// reference is returned. If the unmanaged structure is freed or is inaccessible, the return value is
/// undefined.
/// </value>
public NativeIndependentInstructionDetail? ManagedIndependentDetail {
get {
NativeIndependentInstructionDetail? managedDetail = null;
if (this.IndependentDetail != IntPtr.Zero) {
managedDetail = MarshalExtension.PtrToStructure<NativeIndependentInstructionDetail>(this.IndependentDetail);
}
return managedDetail;
}
}
/// <summary>
/// Get Instruction's ARM64 Detail.
/// </summary>
public NativeArm64InstructionDetail NativeArm64Detail {
get {
var pDetail = CapstoneProxyImport.Arm64Detail(this.IndependentDetail);
var detail = MarshalExtension.PtrToStructure<NativeArm64InstructionDetail>(pDetail);
return detail;
}
}
/// <summary>
/// Get Instruction's ARM Detail.
/// </summary>
public NativeArmInstructionDetail NativeArmDetail {
get {
var pDetail = CapstoneProxyImport.ArmDetail(this.IndependentDetail);
var detail = MarshalExtension.PtrToStructure<NativeArmInstructionDetail>(pDetail);
return detail;
}
}
/// <summary>
/// Get Instruction's X86 Detail.
/// </summary>
public NativeX86InstructionDetail NativeX86Detail {
get {
var pDetail = CapstoneProxyImport.X86Detail(this.IndependentDetail);
var detail = MarshalExtension.PtrToStructure<NativeX86InstructionDetail>(pDetail);
return detail;
}
}
/// <summary>
/// Get Object's String Representation.
/// </summary>
/// <returns>
/// The object's string representation.
/// </returns>
public override string ToString() {
var @string = String.Format("{0} {1}", this.ManagedMnemonic, this.ManagedOperand);
return @string;
}
}
}