From d0feaee4f4c230e00daedcaea2c3f1ecd957cc3f Mon Sep 17 00:00:00 2001 From: Chris Kitching Date: Sun, 15 Oct 2017 11:51:35 +0100 Subject: [PATCH] Avoid a double-free of HipifyPPCallbacks instance This bug was present all along, but something changed in the order of de-initialisation performed by llvm that makes it actually crash now. The constructor of HipifyPPCallbacks gives: ``` std::unique_ptr(this) ``` to the LLVM Preprocessor instance. The Preprocessor instance subsequently frees the HipifyPPCallbacks, which is then freed again when we leave the stack frame at line 4340. So: let's leak the HipifyPPCallbacks onto the heap, and leave the LLVM Preprocessor object responsible for tidying it up. [ROCm/clr commit: 893ee6d6ca12f353f6b16846c39411b587e552dc] --- projects/clr/hipamd/hipify-clang/src/Cuda2Hip.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/projects/clr/hipamd/hipify-clang/src/Cuda2Hip.cpp b/projects/clr/hipamd/hipify-clang/src/Cuda2Hip.cpp index 8b16e75db0..0f73859893 100644 --- a/projects/clr/hipamd/hipify-clang/src/Cuda2Hip.cpp +++ b/projects/clr/hipamd/hipify-clang/src/Cuda2Hip.cpp @@ -4302,12 +4302,12 @@ int main(int argc, const char **argv) { replacementsToUse = &Tool.getReplacements(); #endif - HipifyPPCallbacks PPCallbacks(replacementsToUse, tmpFile); - Cuda2HipCallback Callback(replacementsToUse, &Finder, &PPCallbacks, tmpFile); + HipifyPPCallbacks* PPCallbacks = new HipifyPPCallbacks(replacementsToUse, tmpFile); + Cuda2HipCallback Callback(replacementsToUse, &Finder, PPCallbacks, tmpFile); addAllMatchers(Finder, &Callback); - auto action = newFrontendActionFactory(&Finder, &PPCallbacks); + auto action = newFrontendActionFactory(&Finder, PPCallbacks); Tool.appendArgumentsAdjuster(getInsertArgumentAdjuster("--cuda-host-only", ArgumentInsertPosition::BEGIN)); @@ -4365,13 +4365,13 @@ int main(int argc, const char **argv) { } std::remove(csv.c_str()); } - if (0 == printStats(csv, src, PPCallbacks, Callback, repBytes, bytes, lines, start)) { + if (0 == printStats(csv, src, *PPCallbacks, Callback, repBytes, bytes, lines, start)) { filesTranslated--; } start = std::chrono::steady_clock::now(); repBytesTotal += repBytes; bytesTotal += bytes; - changedLinesTotal += PPCallbacks.LOCs.size() + Callback.LOCs.size(); + changedLinesTotal += PPCallbacks->LOCs.size() + Callback.LOCs.size(); linesTotal += lines; } dst.clear();