Skip to content

[SharedCache] Fix some memory leaks in shared cache loading#6124

Closed
bdash wants to merge 2 commits into
Vector35:devfrom
bdash:dsc-memory
Closed

[SharedCache] Fix some memory leaks in shared cache loading#6124
bdash wants to merge 2 commits into
Vector35:devfrom
bdash:dsc-memory

Conversation

@bdash
Copy link
Copy Markdown
Contributor

@bdash bdash commented Nov 13, 2024

This fixes three sources of leaks:

  1. MMappedFileAccessor::ReadBuffer was returning a heap-allocated DataBuffer, but no callers were ever deleting it. There does not appear to be any reason to heap allocate the DataBuffer as the type is effectively a smart pointer wrapper around BNDataBuffer. It is now returned by value instead.
  2. MMappedFileAccessor::ReadBuffer was allocating a buffer, copying data into it, and then handing that allocation to the DataBuffer constructor. The constructor copies data into a new allocation it owns so this allocation is unnecessary and was being leaked.
  3. SharedCache::Store was appending a JSON array to itself rather than to a similarly named variable. This cyclic data structure was not correctly freed.

Before this change loading a macOS shared cache would leak over 1GB of memory:

leaks Report Version: 4.0, multi-line stacks
Process 34177: 30805047 nodes malloced for 11333150 KB
Process 34177: 2512405 leaks for 1087361648 total leaked bytes.

After this change the leaks are in the 10s of kilobytes and appear to be unrelated to shared cache loading:

leaks Report Version: 4.0, multi-line stacks
Process 69005: 25357627 nodes malloced for 9319529 KB
Process 69005: 240 leaks for 18400 total leaked bytes.

Additionally, the memory footprint of binaryninja after loading the macOS shared cache drops from 8.3GB to 6.9GB.

`MMappedFileAccessor::ReadBuffer` was returning a heap-allocated
`DataBuffer`, but no callers were ever deleting it. There does not
appear to be any reason to heap allocate the `DataBuffer` as the type is
effectively a smart pointer wrapper around `BNDataBuffer`. Switch to
returning it by value instead.

Additionally, `MMappedFileAccessor::ReadBuffer` was allocating a buffer,
copying data into it, and then handing that allocation to the `DataBuffer`
constructor. The constructor copies data into a new allocation it
owns so this allocation is unnecessary and was being leaked.
This is both a correctness fix and avoids leaking a massive amount of memory.
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Nov 13, 2024

CLA assistant check
All committers have signed the CLA.

@bdash
Copy link
Copy Markdown
Contributor Author

bdash commented Nov 13, 2024

@0cyn

@bdash
Copy link
Copy Markdown
Contributor Author

bdash commented Nov 14, 2024

Looks like these changes were cherry-picked into dev in 0d27f74 and d553396.

@bdash bdash closed this Nov 14, 2024
@bdash bdash deleted the dsc-memory branch November 14, 2024 01:47
@emesare emesare added this to the Gallifrey milestone Apr 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants