From 2af6ccdb9ad32bc0a608e78ad46301f2f53e88e6 Mon Sep 17 00:00:00 2001 From: Steffen Larsen Date: Sat, 20 Nov 2021 08:34:22 +0100 Subject: [PATCH] [SYCL] Fix memory leak in online compiler (#4963) The experimental online compiler may leak memory in compileToSPIRV. These changes address this leak by storing the SPIR-V binary information directly in the vector that will later be returned. Signed-off-by: Steffen Larsen --- .../detail/online_compiler/online_compiler.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/sycl/source/detail/online_compiler/online_compiler.cpp b/sycl/source/detail/online_compiler/online_compiler.cpp index 96a1641750bc3..78f2c12098d19 100644 --- a/sycl/source/detail/online_compiler/online_compiler.cpp +++ b/sycl/source/detail/online_compiler/online_compiler.cpp @@ -160,16 +160,14 @@ compileToSPIRV(const std::string &Source, sycl::info::device_type DeviceType, &SourceName, 0, nullptr, nullptr, nullptr, &NumOutputs, &Outputs, &OutputLengths, &OutputNames); - byte *SpirV = nullptr; + std::vector SpirV; std::string CompileLog; - size_t SpirVSize = 0; for (uint32_t I = 0; I < NumOutputs; I++) { size_t NameLen = strlen(OutputNames[I]); if (NameLen >= 4 && strstr(OutputNames[I], ".spv") != nullptr && Outputs[I] != nullptr) { - SpirVSize = OutputLengths[I]; - SpirV = new byte[SpirVSize]; - std::memcpy(SpirV, Outputs[I], SpirVSize); + assert(SpirV.size() == 0 && "More than one SPIR-V output found."); + SpirV = std::vector(Outputs[I], Outputs[I] + OutputLengths[I]); } else if (!strcmp(OutputNames[I], "stdout.log")) { CompileLog = std::string(reinterpret_cast(Outputs[I])); } @@ -184,13 +182,13 @@ compileToSPIRV(const std::string &Source, sycl::info::device_type DeviceType, if (CompileError) throw online_compile_error("ocloc reported compilation errors: {\n" + CompileLog + "\n}"); - if (!SpirV) + if (SpirV.empty()) throw online_compile_error( "Unexpected output: ocloc did not return SPIR-V"); if (MemFreeError) throw online_compile_error("ocloc cannot safely free resources"); - return std::vector(SpirV, SpirV + SpirVSize); + return SpirV; } } // namespace detail