-
Notifications
You must be signed in to change notification settings - Fork 248
Memory leak fix in SPIRVFunction::decodeBB #2268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -153,6 +153,7 @@ bool SPIRVFunction::decodeBB(SPIRVDecoder &Decoder) { | |
| std::to_string(Entry->getOpCode()))) { | ||
| // Bail out if the opcode is not implemented. | ||
| Module->setInvalid(); | ||
| delete Entry; | ||
| return false; | ||
| } | ||
|
|
||
|
|
@@ -164,6 +165,7 @@ bool SPIRVFunction::decodeBB(SPIRVDecoder &Decoder) { | |
| SPIRVDebug::DebugNoLine) || | ||
| Inst->isExtInst(SPIRVEIS_NonSemantic_Shader_DebugInfo_200, | ||
| SPIRVDebug::DebugNoLine)) { | ||
| delete Entry; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as above |
||
| continue; | ||
| } else if (Inst->isExtInst(SPIRVEIS_NonSemantic_Shader_DebugInfo_100, | ||
| SPIRVDebug::DebugLine) || | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @bwlodarcz
Can you please recheck this? I see that we eventually call into some code that returns an entry from a SPIRVFactory. Not sure if we can delete such pointers here.
Thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move delete to Decoder d'tor eliminating all Entries from the map (or whatever container we use underneath getDecoder), or am I missing something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First @asudarsa:
Allocation of SPIRVEntry starts in
template <typename T> SPIRVEntry *create() { return new T(); }on lib/SPIRV/libSPIRV/SPIRVEntry.cpp:62 - raw new/malloc.As we can see in the reviewed function above we have
Decoder.getEntry()call which points out toSPIRVEntry *SPIRVDecoder::getEntrymethod located in lib/SPIRV/libSPIRV/SPIRVStream.cpp:239.Overall we can see that it's some kind of factory method which initializes Entry.
During this initialization we have call to
Entry->setModule(&M)where Entry points out to Module - but not the other way.So right only the entry is aware of Module not vice versa.
And after that we are in our
SPIRVFunction::decodeBB. From that point If the SPIRVEntry pointer is lost by some condition, before it get's assigned toSPIRVModuleinstance which happens later, we are unable to recover it by any means. That's why the delete/free is a necessity in such branches.I rechecked and when the deletes are not in place than it leaks again.
Second @MrSidims:
It seems that, in theory, in Decoder/Encoder iostream architecture
SPIRVModuleownsSPIRVEntriesand it's responsible for the destruction - look onSPIRVModuleImpldestructor. In reality,SPIRVEntriesare created outside of lifetime scope ofSPIRVModuleand eventually passed to it when we are coming through happy path. There are paths/branches whereSPIRVEntryis created but doesn't fit (like in this PR) - in these casesSPIRVModuleisn't an owner of the resource and it needs to be manually freed.Changing an architecture of Decoder/Encoder is much more work and outside of scope of fixing direct ASan errors. I think that we, as community, should definitely think about such architectural change in future.