From c08cd7abb2069b6056ec99c8ec5b87524e45df01 Mon Sep 17 00:00:00 2001 From: thestig Date: Mon, 27 Jun 2016 09:47:59 -0700 Subject: Fix memory leaks with V8 startup data. Review-Url: https://codereview.chromium.org/2068563002 --- samples/pdfium_test.cc | 5 +++++ testing/embedder_test.cpp | 39 +++++++++++++++++++++++++++++++++------ testing/embedder_test.h | 2 -- testing/test_support.cpp | 14 ++++++++------ 4 files changed, 46 insertions(+), 14 deletions(-) diff --git a/samples/pdfium_test.cc b/samples/pdfium_test.cc index 7651205738..f244d8a440 100644 --- a/samples/pdfium_test.cc +++ b/samples/pdfium_test.cc @@ -882,6 +882,11 @@ int main(int argc, const char* argv[]) { #ifdef PDF_ENABLE_V8 v8::V8::ShutdownPlatform(); delete platform; + +#ifdef V8_USE_EXTERNAL_STARTUP_DATA + free(const_cast(natives.data)); + free(const_cast(snapshot.data)); +#endif // V8_USE_EXTERNAL_STARTUP_DATA #endif // PDF_ENABLE_V8 return 0; diff --git a/testing/embedder_test.cpp b/testing/embedder_test.cpp index 9ef1e06dca..b1207fba82 100644 --- a/testing/embedder_test.cpp +++ b/testing/embedder_test.cpp @@ -25,7 +25,16 @@ #endif // PDF_ENABLE_V8 namespace { -const char* g_exe_path_ = nullptr; + +const char* g_exe_path = nullptr; + +#ifdef PDF_ENABLE_V8 +#ifdef V8_USE_EXTERNAL_STARTUP_DATA +v8::StartupData* g_v8_natives = nullptr; +v8::StartupData* g_v8_snapshot = nullptr; +#endif // V8_USE_EXTERNAL_STARTUP_DATA +#endif // PDF_ENABLE_V8 + } // namespace FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) { @@ -50,10 +59,17 @@ EmbedderTest::EmbedderTest() #ifdef PDF_ENABLE_V8 #ifdef V8_USE_EXTERNAL_STARTUP_DATA - InitializeV8ForPDFium(g_exe_path_, std::string(), &natives_, &snapshot_, - &platform_); + if (g_v8_natives && g_v8_snapshot) { + InitializeV8ForPDFium(g_exe_path, std::string(), nullptr, nullptr, + &platform_); + } else { + g_v8_natives = new v8::StartupData; + g_v8_snapshot = new v8::StartupData; + InitializeV8ForPDFium(g_exe_path, std::string(), g_v8_natives, + g_v8_snapshot, &platform_); + } #else - InitializeV8ForPDFium(g_exe_path_, &platform_); + InitializeV8ForPDFium(g_exe_path, &platform_); #endif // V8_USE_EXTERNAL_STARTUP_DATA #endif // FPDF_ENABLE_V8 } @@ -320,8 +336,19 @@ FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info, // Can't use gtest-provided main since we need to stash the path to the // executable in order to find the external V8 binary data files. int main(int argc, char** argv) { - g_exe_path_ = argv[0]; + g_exe_path = argv[0]; testing::InitGoogleTest(&argc, argv); testing::InitGoogleMock(&argc, argv); - return RUN_ALL_TESTS(); + int ret_val = RUN_ALL_TESTS(); + +#ifdef PDF_ENABLE_V8 +#ifdef V8_USE_EXTERNAL_STARTUP_DATA + if (g_v8_natives) + free(const_cast(g_v8_natives->data)); + if (g_v8_snapshot) + free(const_cast(g_v8_snapshot->data)); +#endif // V8_USE_EXTERNAL_STARTUP_DATA +#endif // PDF_ENABLE_V8 + + return ret_val; } diff --git a/testing/embedder_test.h b/testing/embedder_test.h index a176d944c0..6b814a7267 100644 --- a/testing/embedder_test.h +++ b/testing/embedder_test.h @@ -125,8 +125,6 @@ class EmbedderTest : public ::testing::Test, FX_FILEAVAIL file_avail_; #ifdef PDF_ENABLE_V8 v8::Platform* platform_; - v8::StartupData natives_; - v8::StartupData snapshot_; #endif // PDF_ENABLE_V8 void* external_isolate_; TestLoader* loader_; diff --git a/testing/test_support.cpp b/testing/test_support.cpp index 96a18a547a..c4d915e900 100644 --- a/testing/test_support.cpp +++ b/testing/test_support.cpp @@ -157,12 +157,14 @@ bool InitializeV8ForPDFium(const std::string& exe_path, v8::StartupData* snapshot_blob, v8::Platform** platform) { InitializeV8Common(exe_path.c_str(), platform); - if (!GetExternalData(exe_path, bin_dir, "natives_blob.bin", natives_blob)) - return false; - if (!GetExternalData(exe_path, bin_dir, "snapshot_blob.bin", snapshot_blob)) - return false; - v8::V8::SetNativesDataBlob(natives_blob); - v8::V8::SetSnapshotDataBlob(snapshot_blob); + if (natives_blob && snapshot_blob) { + if (!GetExternalData(exe_path, bin_dir, "natives_blob.bin", natives_blob)) + return false; + if (!GetExternalData(exe_path, bin_dir, "snapshot_blob.bin", snapshot_blob)) + return false; + v8::V8::SetNativesDataBlob(natives_blob); + v8::V8::SetSnapshotDataBlob(snapshot_blob); + } return true; } #else // V8_USE_EXTERNAL_STARTUP_DATA -- cgit v1.2.3