|
| 1 | +using System; |
| 2 | +using System.Runtime.InteropServices; |
| 3 | + |
| 4 | +namespace BinaryNinja |
| 5 | +{ |
| 6 | + internal static partial class NativeDelegates |
| 7 | + { |
| 8 | + // void (*typeAdded)(void* ctxt, BNTypeArchive* archive, const char* id, BNType* definition) |
| 9 | + public delegate void TypeArchiveTypeAddedDelegate( |
| 10 | + IntPtr context, IntPtr archive, IntPtr id, IntPtr definition); |
| 11 | + |
| 12 | + // void (*typeUpdated)(void* ctxt, BNTypeArchive* archive, const char* id, BNType* oldDefinition, BNType* newDefinition) |
| 13 | + public delegate void TypeArchiveTypeUpdatedDelegate( |
| 14 | + IntPtr context, IntPtr archive, IntPtr id, IntPtr oldDefinition, IntPtr newDefinition); |
| 15 | + |
| 16 | + // void (*typeRenamed)(void* ctxt, BNTypeArchive* archive, const char* id, BNQualifiedName* oldName, BNQualifiedName* newName) |
| 17 | + public delegate void TypeArchiveTypeRenamedDelegate( |
| 18 | + IntPtr context, IntPtr archive, IntPtr id, IntPtr oldName, IntPtr newName); |
| 19 | + |
| 20 | + // void (*typeDeleted)(void* ctxt, BNTypeArchive* archive, const char* id, BNType* definition) |
| 21 | + public delegate void TypeArchiveTypeDeletedDelegate( |
| 22 | + IntPtr context, IntPtr archive, IntPtr id, IntPtr definition); |
| 23 | + } |
| 24 | + |
| 25 | + internal static partial class UnsafeUtils |
| 26 | + { |
| 27 | + // Adapts a TypeArchiveNotification into the four native TypeArchive callback shapes. Each |
| 28 | + // method marshals the native arguments into managed objects and forwards to the user's |
| 29 | + // virtual override. The core invokes the function pointers stored on the |
| 30 | + // BNTypeArchiveNotification struct; this context is rooted by those delegates (see |
| 31 | + // TypeArchiveNotificationContext.BuildNative), so it -- and the notification it holds -- |
| 32 | + // stays alive for the registration lifetime. |
| 33 | + internal sealed class TypeArchiveNotificationContext |
| 34 | + { |
| 35 | + internal readonly TypeArchiveNotification Notification; |
| 36 | + |
| 37 | + // The allocated BNTypeArchiveNotification struct (IntPtr to AllocHGlobal memory), kept |
| 38 | + // alive until FreeNative so the core-held registration never sees freed memory. |
| 39 | + internal IntPtr NativeStruct = IntPtr.Zero; |
| 40 | + |
| 41 | + // Rooting the four wrapper delegates on this context prevents the GC from reclaiming |
| 42 | + // their function pointers while the core holds the registration. |
| 43 | + private NativeDelegates.TypeArchiveTypeAddedDelegate? m_added; |
| 44 | + private NativeDelegates.TypeArchiveTypeUpdatedDelegate? m_updated; |
| 45 | + private NativeDelegates.TypeArchiveTypeRenamedDelegate? m_renamed; |
| 46 | + private NativeDelegates.TypeArchiveTypeDeletedDelegate? m_deleted; |
| 47 | + |
| 48 | + // The GCHandle stored as the struct's context field; freed on unregistration. The |
| 49 | + // delegates already root this context, but the handle makes the rooting explicit and |
| 50 | + // gives the core a stable context value to pass back. |
| 51 | + private GCHandle m_selfHandle; |
| 52 | + |
| 53 | + internal TypeArchiveNotificationContext(TypeArchiveNotification notification) |
| 54 | + { |
| 55 | + this.Notification = notification; |
| 56 | + } |
| 57 | + |
| 58 | + // Marshals the native arguments and forwards to the user override. A throwing override |
| 59 | + // must not abort the core's notification dispatch, so exceptions are swallowed (Python |
| 60 | + // logs and continues). |
| 61 | + internal void OnTypeAdded(IntPtr context, IntPtr archive, IntPtr id, IntPtr definition) |
| 62 | + { |
| 63 | + try |
| 64 | + { |
| 65 | + TypeArchive? archiveObject = TypeArchive.BorrowHandle(archive); |
| 66 | + if (null == archiveObject) |
| 67 | + { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + string idString = PtrToAnsiString(id); |
| 72 | + Type? definitionType = Type.NewFromHandle(definition); |
| 73 | + |
| 74 | + this.Notification.OnTypeAdded(archiveObject, idString, definitionType); |
| 75 | + } |
| 76 | + catch (Exception) |
| 77 | + { |
| 78 | + // Swallowed: a failing callback must not crash the core dispatch. |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + internal void OnTypeUpdated( |
| 83 | + IntPtr context, IntPtr archive, IntPtr id, IntPtr oldDefinition, IntPtr newDefinition) |
| 84 | + { |
| 85 | + try |
| 86 | + { |
| 87 | + TypeArchive? archiveObject = TypeArchive.BorrowHandle(archive); |
| 88 | + if (null == archiveObject) |
| 89 | + { |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + string idString = PtrToAnsiString(id); |
| 94 | + Type? oldType = Type.NewFromHandle(oldDefinition); |
| 95 | + Type? newType = Type.NewFromHandle(newDefinition); |
| 96 | + |
| 97 | + this.Notification.OnTypeUpdated(archiveObject, idString, oldType, newType); |
| 98 | + } |
| 99 | + catch (Exception) |
| 100 | + { |
| 101 | + // Swallowed: a failing callback must not crash the core dispatch. |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + internal void OnTypeRenamed( |
| 106 | + IntPtr context, IntPtr archive, IntPtr id, IntPtr oldName, IntPtr newName) |
| 107 | + { |
| 108 | + try |
| 109 | + { |
| 110 | + TypeArchive? archiveObject = TypeArchive.BorrowHandle(archive); |
| 111 | + if (null == archiveObject) |
| 112 | + { |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + string idString = PtrToAnsiString(id); |
| 117 | + QualifiedName oldNameValue = QualifiedNameFromPtr(oldName); |
| 118 | + QualifiedName newNameValue = QualifiedNameFromPtr(newName); |
| 119 | + |
| 120 | + this.Notification.OnTypeRenamed(archiveObject, idString, oldNameValue, newNameValue); |
| 121 | + } |
| 122 | + catch (Exception) |
| 123 | + { |
| 124 | + // Swallowed: a failing callback must not crash the core dispatch. |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + internal void OnTypeDeleted(IntPtr context, IntPtr archive, IntPtr id, IntPtr definition) |
| 129 | + { |
| 130 | + try |
| 131 | + { |
| 132 | + TypeArchive? archiveObject = TypeArchive.BorrowHandle(archive); |
| 133 | + if (null == archiveObject) |
| 134 | + { |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + string idString = PtrToAnsiString(id); |
| 139 | + Type? definitionType = Type.NewFromHandle(definition); |
| 140 | + |
| 141 | + this.Notification.OnTypeDeleted(archiveObject, idString, definitionType); |
| 142 | + } |
| 143 | + catch (Exception) |
| 144 | + { |
| 145 | + // Swallowed: a failing callback must not crash the core dispatch. |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + // Allocates the BNTypeArchiveNotification struct, sets its context and the four function |
| 150 | + // pointers (taken from the rooted wrapper delegates), and returns the struct pointer. |
| 151 | + // Idempotent: a second call returns the existing struct. |
| 152 | + internal IntPtr BuildNative() |
| 153 | + { |
| 154 | + if (IntPtr.Zero != this.NativeStruct) |
| 155 | + { |
| 156 | + return this.NativeStruct; |
| 157 | + } |
| 158 | + |
| 159 | + // 1. Create the four wrapper delegates bound to this context and root them as fields. |
| 160 | + this.m_added = new NativeDelegates.TypeArchiveTypeAddedDelegate(this.OnTypeAdded); |
| 161 | + this.m_updated = new NativeDelegates.TypeArchiveTypeUpdatedDelegate(this.OnTypeUpdated); |
| 162 | + this.m_renamed = new NativeDelegates.TypeArchiveTypeRenamedDelegate(this.OnTypeRenamed); |
| 163 | + this.m_deleted = new NativeDelegates.TypeArchiveTypeDeletedDelegate(this.OnTypeDeleted); |
| 164 | + |
| 165 | + // 2. Allocate a stable context handle so the core has a value to pass back, and so |
| 166 | + // this context is explicitly rooted. |
| 167 | + this.m_selfHandle = GCHandle.Alloc(this, GCHandleType.Normal); |
| 168 | + |
| 169 | + // 3. Build the struct: context + four function pointers. |
| 170 | + BNTypeArchiveNotification native = new BNTypeArchiveNotification(); |
| 171 | + native.context = GCHandle.ToIntPtr(this.m_selfHandle); |
| 172 | + native.typeAdded = Marshal.GetFunctionPointerForDelegate(this.m_added); |
| 173 | + native.typeUpdated = Marshal.GetFunctionPointerForDelegate(this.m_updated); |
| 174 | + native.typeRenamed = Marshal.GetFunctionPointerForDelegate(this.m_renamed); |
| 175 | + native.typeDeleted = Marshal.GetFunctionPointerForDelegate(this.m_deleted); |
| 176 | + |
| 177 | + this.NativeStruct = Marshal.AllocHGlobal(Marshal.SizeOf<BNTypeArchiveNotification>()); |
| 178 | + Marshal.StructureToPtr<BNTypeArchiveNotification>(native, this.NativeStruct, false); |
| 179 | + |
| 180 | + return this.NativeStruct; |
| 181 | + } |
| 182 | + |
| 183 | + // Frees the struct and the context handle. Called after the core has unregistered. |
| 184 | + internal void FreeNative() |
| 185 | + { |
| 186 | + if (IntPtr.Zero != this.NativeStruct) |
| 187 | + { |
| 188 | + Marshal.DestroyStructure<BNTypeArchiveNotification>(this.NativeStruct); |
| 189 | + Marshal.FreeHGlobal(this.NativeStruct); |
| 190 | + this.NativeStruct = IntPtr.Zero; |
| 191 | + } |
| 192 | + |
| 193 | + if (this.m_selfHandle.IsAllocated) |
| 194 | + { |
| 195 | + this.m_selfHandle.Free(); |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + private static string PtrToAnsiString(IntPtr pointer) |
| 200 | + { |
| 201 | + if (IntPtr.Zero == pointer) |
| 202 | + { |
| 203 | + return string.Empty; |
| 204 | + } |
| 205 | + |
| 206 | + return Marshal.PtrToStringAnsi(pointer) ?? string.Empty; |
| 207 | + } |
| 208 | + |
| 209 | + // Reads a BNQualifiedName out of a pointer the core passes (the archive owns the name |
| 210 | + // storage, so it is read without freeing, matching Python's _from_core_struct). |
| 211 | + private static QualifiedName QualifiedNameFromPtr(IntPtr pointer) |
| 212 | + { |
| 213 | + if (IntPtr.Zero == pointer) |
| 214 | + { |
| 215 | + return new QualifiedName(string.Empty); |
| 216 | + } |
| 217 | + |
| 218 | + BNQualifiedName native = Marshal.PtrToStructure<BNQualifiedName>(pointer); |
| 219 | + |
| 220 | + return QualifiedName.FromNative(native); |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | +} |
0 commit comments