Skip to content

Commit 957ffd0

Browse files
Copilotxusheng6
andcommitted
Fix TTD Calls data corruption and create new icon
Co-authored-by: xusheng6 <[email protected]>
1 parent a6ef378 commit 957ffd0

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

core/ffi.cpp

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,15 @@ limitations under the License.
2121
#include "debuggercontroller.h"
2222
#include "debuggercommon.h"
2323
#include "../api/ffi.h"
24+
#include <map>
25+
#include <mutex>
2426

2527
using namespace BinaryNinjaDebugger;
2628

29+
// Global map to track TTD call event allocations and their counts
30+
static std::map<BNDebuggerTTDCallEvent*, size_t> g_ttdCallEventCounts;
31+
static std::mutex g_ttdCallEventMutex;
32+
2733

2834
char* BNDebuggerAllocString(const char* contents)
2935
{
@@ -1148,6 +1154,12 @@ BNDebuggerTTDCallEvent* BNDebuggerGetTTDCallsForSymbols(BNDebuggerController* co
11481154
*count = events.size();
11491155
auto result = new BNDebuggerTTDCallEvent[events.size()];
11501156

1157+
// Store the count for proper cleanup later
1158+
{
1159+
std::lock_guard<std::mutex> lock(g_ttdCallEventMutex);
1160+
g_ttdCallEventCounts[result] = events.size();
1161+
}
1162+
11511163
for (size_t i = 0; i < events.size(); ++i)
11521164
{
11531165
// Copy string fields
@@ -1190,13 +1202,52 @@ BNDebuggerTTDCallEvent* BNDebuggerGetTTDCallsForSymbols(BNDebuggerController* co
11901202

11911203
void BNDebuggerFreeTTDCallEvents(BNDebuggerTTDCallEvent* events)
11921204
{
1193-
// Note: This implementation has the same limitation as TTD memory events -
1194-
// we need to know the count to properly free strings, but the API doesn't provide it.
1195-
// In practice, the caller should manage this or we need to modify the API.
1196-
if (events)
1205+
if (!events)
1206+
return;
1207+
1208+
size_t eventCount = 0;
1209+
1210+
// Retrieve the count from our tracking map
11971211
{
1198-
delete[] events;
1212+
std::lock_guard<std::mutex> lock(g_ttdCallEventMutex);
1213+
auto it = g_ttdCallEventCounts.find(events);
1214+
if (it != g_ttdCallEventCounts.end())
1215+
{
1216+
eventCount = it->second;
1217+
g_ttdCallEventCounts.erase(it);
1218+
}
1219+
}
1220+
1221+
// If we found the count, properly free all strings
1222+
if (eventCount > 0)
1223+
{
1224+
for (size_t i = 0; i < eventCount; ++i)
1225+
{
1226+
if (events[i].eventType)
1227+
{
1228+
BNFreeString(events[i].eventType);
1229+
}
1230+
if (events[i].function)
1231+
{
1232+
BNFreeString(events[i].function);
1233+
}
1234+
1235+
// Free parameter strings
1236+
if (events[i].parameters && events[i].parameterCount > 0)
1237+
{
1238+
for (size_t j = 0; j < events[i].parameterCount; ++j)
1239+
{
1240+
if (events[i].parameters[j])
1241+
{
1242+
BNFreeString(events[i].parameters[j]);
1243+
}
1244+
}
1245+
delete[] events[i].parameters;
1246+
}
1247+
}
11991248
}
1249+
1250+
delete[] events;
12001251
}
12011252

12021253

icons/ttd-calls.png

54 Bytes
Loading

0 commit comments

Comments
 (0)