[SharedCache] Fix some memory leaks in shared cache loading#6124
Closed
bdash wants to merge 2 commits into
Closed
Conversation
`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.
Contributor
Author
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This fixes three sources of leaks:
MMappedFileAccessor::ReadBufferwas returning a heap-allocatedDataBuffer, but no callers were ever deleting it. There does not appear to be any reason to heap allocate theDataBufferas the type is effectively a smart pointer wrapper aroundBNDataBuffer. It is now returned by value instead.MMappedFileAccessor::ReadBufferwas allocating a buffer, copying data into it, and then handing that allocation to theDataBufferconstructor. The constructor copies data into a new allocation it owns so this allocation is unnecessary and was being leaked.SharedCache::Storewas 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:
After this change the leaks are in the 10s of kilobytes and appear to be unrelated to shared cache loading:
Additionally, the memory footprint of
binaryninjaafter loading the macOS shared cache drops from 8.3GB to 6.9GB.