This is an archive of the discontinued LLVM Phabricator instance.

Add delete to fix resource leak in llc.cpp
ClosedPublic

Authored by XinWang10 on Apr 10 2023, 10:23 PM.

Details

Summary

From line 693 in file llc.cpp, it uses new operator to creates a ModulePass
and assigned to MMIWP. If the condition after take the true branch, it has
chance to go in to line 702 or line 709, the function will return without
cleaning the memory.
The second issue existed for the same reason, the ref TPC get the pointer
created from LLVMTM.createPassConfig, and will leak memory if goes into
line 709.
This patch uses delete in the issued branch.

Diff Detail

Event Timeline

XinWang10 created this revision.Apr 10 2023, 10:23 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 10 2023, 10:23 PM
XinWang10 requested review of this revision.Apr 10 2023, 10:23 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 10 2023, 10:23 PM
XinWang10 retitled this revision from draft for fix resource leak in llc to Add delete to fix resource leak in llc.cpp.Apr 10 2023, 11:36 PM
XinWang10 edited the summary of this revision. (Show Details)
craig.topper added inline comments.
llvm/tools/llc/llc.cpp
702

Why is it only a leak on these paths? The are other return 1 in this function.

Can we use std::unique_ptr where these are created?

XinWang10 added inline comments.Apr 11 2023, 6:46 PM
llvm/tools/llc/llc.cpp
702
  1. MMIWP and TPC will be maintained by PM here at line 717, 718. I remember that PM's destructor will handle them.
  2. MMIWP and TPC will be used as parameters for function like PM.add or arget->addPassesToEmitFile() in line 727, if we use unique_ptr, we need to handle more complicated issues.
craig.topper added inline comments.Apr 11 2023, 6:51 PM
llvm/tools/llc/llc.cpp
702

Can we release the unique_ptr when we hand it over to PM?

XinWang10 added inline comments.Apr 11 2023, 7:25 PM
llvm/tools/llc/llc.cpp
702

Yes we can. If it its preferred, will take a try.

XinWang10 added inline comments.Apr 11 2023, 8:06 PM
llvm/tools/llc/llc.cpp
702

I took a try and find if we use unique_ptr instead and release the unique_ptr when we hand it over to PM, we can not handle it well after PM.add(MMIWP), at line 734 and 737 we still use it.
So I think it's not proper here to use unique_ptr.

This revision is now accepted and ready to land.Apr 11 2023, 8:11 PM
This revision was automatically updated to reflect the committed changes.