Skip to content

Commit 9a4d264

Browse files
authored
Add custom platforms (#132)
1 parent de8a6c9 commit 9a4d264

6 files changed

Lines changed: 601 additions & 95 deletions

File tree

‎Function/BNCreateCustomPlatform.cs‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,9 @@ internal static partial class NativeMethods
1717
EntryPoint = "BNCreateCustomPlatform"
1818
)]
1919
internal static extern IntPtr BNCreateCustomPlatform(
20-
21-
// BNArchitecture* arch
22-
IntPtr arch ,
23-
24-
// const char* name
25-
[MarshalAs(UnmanagedType.LPUTF8Str)] string name ,
26-
27-
// BNCustomPlatform* impl
28-
IntPtr impl
20+
IntPtr arch,
21+
[MarshalAs(UnmanagedType.LPUTF8Str)] string name,
22+
in BNCustomPlatform implementation
2923
);
3024
}
31-
}
25+
}

‎Function/BNCreateCustomPlatformWithTypes.cs‎

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,12 @@ internal static partial class NativeMethods
1717
EntryPoint = "BNCreateCustomPlatformWithTypes"
1818
)]
1919
internal static extern IntPtr BNCreateCustomPlatformWithTypes(
20-
21-
// BNArchitecture* arch
22-
IntPtr arch ,
23-
24-
// const char* name
25-
[MarshalAs(UnmanagedType.LPUTF8Str)] string name ,
26-
27-
// BNCustomPlatform* impl
28-
IntPtr impl ,
29-
30-
// const char* typeFile
31-
[MarshalAs(UnmanagedType.LPUTF8Str)] string typeFile ,
32-
33-
// const char** includeDirs
34-
string[] includeDirs ,
35-
36-
// uint64_t includeDirCount
37-
ulong includeDirCount
20+
IntPtr arch,
21+
[MarshalAs(UnmanagedType.LPUTF8Str)] string name,
22+
in BNCustomPlatform implementation,
23+
[MarshalAs(UnmanagedType.LPUTF8Str)] string typeFile,
24+
IntPtr includeDirs,
25+
ulong includeDirCount
3826
);
3927
}
40-
}
28+
}

‎Handle/BNPlatform.cs‎

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,27 @@
55

66
namespace BinaryNinja
77
{
8-
public sealed class Platform : AbstractSafeHandle<Platform>
8+
public partial class Platform : AbstractSafeHandle<Platform>
99
{
1010
public Platform(Architecture arch , string name)
11-
:this( NativeMethods.BNCreatePlatform(arch.DangerousGetHandle() , name) , true)
11+
: base(true)
1212
{
13-
13+
this.custom = true;
14+
this.InitializeCustomPlatform(arch, name, null, Array.Empty<string>());
15+
}
16+
17+
protected Platform(
18+
Architecture arch,
19+
string name,
20+
string typeFile,
21+
string[] includeDirs
22+
)
23+
: base(true)
24+
{
25+
this.custom = true;
26+
this.InitializeCustomPlatform(
27+
arch, name, typeFile, includeDirs ?? Array.Empty<string>()
28+
);
1429
}
1530

1631
internal Platform(IntPtr handle , bool owner)
@@ -87,7 +102,7 @@ internal static Platform MustBorrowHandle(IntPtr handle)
87102

88103
protected override bool ReleaseHandle()
89104
{
90-
if ( !this.IsInvalid )
105+
if (!this.IsInvalid && !this.registered)
91106
{
92107
NativeMethods.BNFreePlatform(this.handle);
93108
this.SetHandleAsInvalid();
@@ -144,6 +159,7 @@ public static string[] GetOSList()
144159
public void RegisterPlatform(string os)
145160
{
146161
NativeMethods.BNRegisterPlatform(os , this.handle);
162+
Platform.RootForRegistration(this);
147163
}
148164

149165
public string Name
@@ -280,10 +296,15 @@ public void SetSystemCallConvention(BinaryNinja.CallingConvention callConvention
280296
);
281297
}
282298

283-
public uint[] GlobalRegisters
299+
public virtual uint[] GlobalRegisters
284300
{
285301
get
286302
{
303+
if (this.custom)
304+
{
305+
return this.GetDefaultGlobalRegisters();
306+
}
307+
287308
IntPtr arrayPointer = NativeMethods.BNGetPlatformGlobalRegisters(
288309
this.handle,
289310
out ulong arrayLength
@@ -297,10 +318,15 @@ out ulong arrayLength
297318
}
298319
}
299320

300-
public ulong AddressSize
321+
public virtual ulong AddressSize
301322
{
302323
get
303324
{
325+
if (this.custom)
326+
{
327+
return this.Architecture.AddressSize;
328+
}
329+
304330
return NativeMethods.BNGetPlatformAddressSize(this.handle);
305331
}
306332
}
@@ -551,8 +577,13 @@ public static unsafe Platform[] GetListByOSAndArchitecture(string os, Architectu
551577
/// <summary>
552578
/// Gets the type of a global register on this platform.
553579
/// </summary>
554-
public BinaryNinja.Type? GetGlobalRegisterType(uint reg)
580+
public virtual BinaryNinja.Type? GetGlobalRegisterType(uint reg)
555581
{
582+
if (this.custom)
583+
{
584+
return null;
585+
}
586+
556587
return BinaryNinja.Type.TakeHandle(
557588
NativeMethods.BNGetPlatformGlobalRegisterType(this.handle , reg)
558589
);
@@ -836,4 +867,4 @@ string[] includeDirs
836867
}
837868
}
838869

839-
}
870+
}

0 commit comments

Comments
 (0)