Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions Delegate/MatchConstantDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public delegate bool MatchConstantDelegate
ulong address ,
LinearDisassemblyLine line
);

internal static partial class NativeDelegates
{
// bool (*matchCallback)(void* matchCtxt, uint64_t addr, BNLinearDisassemblyLine* line)
Expand All @@ -17,19 +17,40 @@ public delegate bool MatchConstantDelegate(
IntPtr line
);
}

internal static partial class UnsafeUtils
{
// Adapts a public MatchConstantDelegate into the native match-callback shape. The BN core
// hands the callback an OWNED BNLinearDisassemblyLine* per match (the official Python
// binding frees it via _LinearDisassemblyLine_convertor -> BNFreeLinearDisassemblyLines).
// The line is fully copied into managed objects by MustFromNativePointer, so freeing the
// native line afterward is safe; the earlier form never freed it and leaked one line per
// match.
internal sealed class MatchConstantContext
{
private readonly MatchConstantDelegate m_callback;

internal MatchConstantContext(MatchConstantDelegate callback)
{
this.m_callback = callback;
}

internal bool OnMatch(IntPtr matchCtxt, ulong address, IntPtr line)
{
LinearDisassemblyLine copy = LinearDisassemblyLine.MustFromNativePointer(line);

NativeMethods.BNFreeLinearDisassemblyLines(line, 1);

return this.m_callback(address, copy);
}
}

internal static NativeDelegates.MatchConstantDelegate WrapMatchConstantDelegate(
MatchConstantDelegate callback)
{
return bool (matchCtxt , address , line) =>
{
return callback (
address ,
LinearDisassemblyLine.MustFromNativePointer(line)
);
};
MatchConstantContext context = new MatchConstantContext(callback);

return new NativeDelegates.MatchConstantDelegate(context.OnMatch);
}
}
}
37 changes: 28 additions & 9 deletions Delegate/MatchDataDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public delegate bool MatchDataDelegate
ulong address ,
byte[] data
);

internal static partial class NativeDelegates
{
// bool (*matchCallback)(void* matchCtxt, uint64_t addr, BNDataBuffer* match)
Expand All @@ -17,19 +17,38 @@ public delegate bool MatchDataDelegate(
IntPtr match
);
}

internal static partial class UnsafeUtils
{
// Adapts a public MatchDataDelegate into the native match-callback shape. The BN core hands
// the callback an OWNED BNDataBuffer per match (the official Python binding adopts it as
// DataBuffer(handle=match) and frees it in __del__ via BNFreeDataBuffer). The buffer is
// therefore taken (owned) and freed deterministically with `using`, never borrowed; the
// earlier borrow form leaked one buffer per match.
internal sealed class MatchDataContext
{
private readonly MatchDataDelegate m_callback;

internal MatchDataContext(MatchDataDelegate callback)
{
this.m_callback = callback;
}

internal bool OnMatch(IntPtr matchCtxt, ulong address, IntPtr match)
{
using (DataBuffer owned = DataBuffer.MustTakeHandle(match))
{
return this.m_callback(address, owned.Contents);
}
}
}

internal static NativeDelegates.MatchDataDelegate WrapMatchDataDelegate(
MatchDataDelegate callback)
{
return bool (matchCtxt , address , buffer) =>
{
return callback(
address ,
DataBuffer.MustBorrowHandle(buffer).Contents
);
};
MatchDataContext context = new MatchDataContext(callback);

return new NativeDelegates.MatchDataDelegate(context.OnMatch);
}
}
}
42 changes: 33 additions & 9 deletions Delegate/MatchTextDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public delegate bool MatchTextDelegate
string text,
LinearDisassemblyLine line
);

internal static partial class NativeDelegates
{
// bool (*matchCallback)(void* matchCtxt, uint64_t addr, const char* match, BNLinearDisassemblyLine* line)
Expand All @@ -19,20 +19,44 @@ public delegate bool MatchTextDelegate(
IntPtr line
);
}

internal static partial class UnsafeUtils
{
internal static NativeDelegates.MatchTextDelegate WrapMatchTextDelegate(
MatchTextDelegate callback)
// Adapts a public MatchTextDelegate into the native match-callback shape. The BN core hands
// the callback an OWNED BNLinearDisassemblyLine* per match (the official Python binding
// frees it via _LinearDisassemblyLine_convertor -> BNFreeLinearDisassemblyLines). The line
// is fully copied into managed objects by MustFromNativePointer, so freeing the native line
// afterward is safe; the earlier form never freed it and leaked one line per match. The
// matched text is a borrowed const char* (core-owned) and is only read, never freed.
internal sealed class MatchTextContext
{
return bool (matchCtxt , address , text , line) =>
private readonly MatchTextDelegate m_callback;

internal MatchTextContext(MatchTextDelegate callback)
{
this.m_callback = callback;
}

internal bool OnMatch(IntPtr matchCtxt, ulong address, IntPtr text, IntPtr line)
{
return callback (
address ,
LinearDisassemblyLine copy = LinearDisassemblyLine.MustFromNativePointer(line);

NativeMethods.BNFreeLinearDisassemblyLines(line, 1);

return this.m_callback(
address ,
UnsafeUtils.ReadUtf8String(text),
LinearDisassemblyLine.MustFromNativePointer(line)
copy
);
};
}
}

internal static NativeDelegates.MatchTextDelegate WrapMatchTextDelegate(
MatchTextDelegate callback)
{
MatchTextContext context = new MatchTextContext(callback);

return new NativeDelegates.MatchTextDelegate(context.OnMatch);
}
}
}
2 changes: 1 addition & 1 deletion Function/BNCreateFunctionGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal static extern IntPtr BNCreateFunctionGraph(
IntPtr func ,

// BNFunctionViewType type
in BNFunctionViewType type ,
BNFunctionViewType type ,

// BNDisassemblySettings* settings
IntPtr settings
Expand Down
2 changes: 1 addition & 1 deletion Function/BNCreateImmediateFunctionGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal static extern IntPtr BNCreateImmediateFunctionGraph(
IntPtr func ,

// BNFunctionViewType type
in BNFunctionViewType type ,
BNFunctionViewType type ,

// BNDisassemblySettings* settings
IntPtr settings
Expand Down
2 changes: 1 addition & 1 deletion Function/BNFindAllConstantWithProgress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal static extern bool BNFindAllConstantWithProgress(
IntPtr settings ,

// BNFunctionViewType viewType
in BNFunctionViewType viewType ,
BNFunctionViewType viewType ,

// void* ctxt
IntPtr ctxt ,
Expand Down
2 changes: 1 addition & 1 deletion Function/BNFindAllTextWithProgress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal static extern bool BNFindAllTextWithProgress(
FindFlag flags ,

// BNFunctionViewType viewType
in BNFunctionViewType viewType ,
BNFunctionViewType viewType ,

// void* ctxt
IntPtr ctxt ,
Expand Down
2 changes: 1 addition & 1 deletion Function/BNFindNextConstant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal static extern bool BNFindNextConstant(
IntPtr settings ,

// BNFunctionViewType viewType
in BNFunctionViewType viewType
BNFunctionViewType viewType
);
}
}
2 changes: 1 addition & 1 deletion Function/BNFindNextConstantWithProgress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal static extern bool BNFindNextConstantWithProgress(
IntPtr settings ,

// BNFunctionViewType viewType
in BNFunctionViewType viewType ,
BNFunctionViewType viewType ,

// void* ctxt
IntPtr ctxt ,
Expand Down
2 changes: 1 addition & 1 deletion Function/BNFindNextText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal static extern bool BNFindNextText(
FindFlag flags ,

// BNFunctionViewType viewType
in BNFunctionViewType viewType
BNFunctionViewType viewType
);
}
}
2 changes: 1 addition & 1 deletion Function/BNFindNextTextWithProgress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ internal static extern bool BNFindNextTextWithProgress(
FindFlag flags ,

// BNFunctionViewType viewType
in BNFunctionViewType viewType ,
BNFunctionViewType viewType ,

// void* ctxt
IntPtr ctxt ,
Expand Down
Loading
Loading