K. Moon | 832a694 | 2022-10-31 20:11:31 +0000 | [diff] [blame] | 1 | // Copyright 2019 The PDFium Authors |
Lei Zhang | 72e8b69 | 2019-02-05 19:16:52 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "testing/v8_initializer.h" |
| 6 | |
Felix Kauselmann | 5fe8569 | 2019-02-11 20:55:52 +0000 | [diff] [blame] | 7 | #include <cstring> |
| 8 | |
Lei Zhang | b6992dd | 2019-02-05 23:30:20 +0000 | [diff] [blame] | 9 | #include "public/fpdfview.h" |
| 10 | #include "testing/utils/file_util.h" |
Lei Zhang | 72e8b69 | 2019-02-05 19:16:52 +0000 | [diff] [blame] | 11 | #include "testing/utils/path_service.h" |
Tom Sepez | ea03a7b | 2022-02-24 00:30:14 +0000 | [diff] [blame] | 12 | #include "third_party/base/numerics/safe_conversions.h" |
Lei Zhang | 72e8b69 | 2019-02-05 19:16:52 +0000 | [diff] [blame] | 13 | #include "v8/include/libplatform/libplatform.h" |
Lei Zhang | bfcf156 | 2022-07-07 01:49:26 +0000 | [diff] [blame] | 14 | #include "v8/include/v8-initialization.h" |
| 15 | #include "v8/include/v8-snapshot.h" |
Lei Zhang | 72e8b69 | 2019-02-05 19:16:52 +0000 | [diff] [blame] | 16 | |
Tom Sepez | 30eddfa | 2021-02-16 19:24:00 +0000 | [diff] [blame] | 17 | #ifdef PDF_ENABLE_XFA |
| 18 | #include "v8/include/cppgc/platform.h" |
| 19 | #endif |
| 20 | |
Lei Zhang | 72e8b69 | 2019-02-05 19:16:52 +0000 | [diff] [blame] | 21 | namespace { |
| 22 | |
| 23 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 24 | // Returns the full path for an external V8 data file based on either |
| 25 | // the currect exectuable path or an explicit override. |
| 26 | std::string GetFullPathForSnapshotFile(const std::string& exe_path, |
| 27 | const std::string& bin_dir, |
| 28 | const std::string& filename) { |
| 29 | std::string result; |
| 30 | if (!bin_dir.empty()) { |
| 31 | result = bin_dir; |
| 32 | if (*bin_dir.rbegin() != PATH_SEPARATOR) { |
| 33 | result += PATH_SEPARATOR; |
| 34 | } |
| 35 | } else if (!exe_path.empty()) { |
| 36 | size_t last_separator = exe_path.rfind(PATH_SEPARATOR); |
| 37 | if (last_separator != std::string::npos) { |
| 38 | result = exe_path.substr(0, last_separator + 1); |
| 39 | } |
| 40 | } |
| 41 | result += filename; |
| 42 | return result; |
| 43 | } |
| 44 | |
| 45 | bool GetExternalData(const std::string& exe_path, |
| 46 | const std::string& bin_dir, |
| 47 | const std::string& filename, |
| 48 | v8::StartupData* result_data) { |
| 49 | std::string full_path = |
| 50 | GetFullPathForSnapshotFile(exe_path, bin_dir, filename); |
| 51 | size_t data_length = 0; |
| 52 | std::unique_ptr<char, pdfium::FreeDeleter> data_buffer = |
| 53 | GetFileContents(full_path.c_str(), &data_length); |
| 54 | if (!data_buffer) |
| 55 | return false; |
| 56 | |
| 57 | result_data->data = data_buffer.release(); |
Tom Sepez | ea03a7b | 2022-02-24 00:30:14 +0000 | [diff] [blame] | 58 | result_data->raw_size = pdfium::base::checked_cast<int>(data_length); |
Lei Zhang | 72e8b69 | 2019-02-05 19:16:52 +0000 | [diff] [blame] | 59 | return true; |
| 60 | } |
| 61 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 62 | |
Tom Sepez | e0d3c50 | 2020-08-11 23:51:10 +0000 | [diff] [blame] | 63 | std::unique_ptr<v8::Platform> InitializeV8Common(const std::string& exe_path, |
| 64 | const std::string& js_flags) { |
Lei Zhang | 72e8b69 | 2019-02-05 19:16:52 +0000 | [diff] [blame] | 65 | v8::V8::InitializeICUDefaultLocation(exe_path.c_str()); |
| 66 | |
| 67 | std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform(); |
| 68 | v8::V8::InitializePlatform(platform.get()); |
Tom Sepez | 30eddfa | 2021-02-16 19:24:00 +0000 | [diff] [blame] | 69 | #ifdef PDF_ENABLE_XFA |
| 70 | cppgc::InitializeProcess(platform->GetPageAllocator()); |
| 71 | #endif |
Lei Zhang | 72e8b69 | 2019-02-05 19:16:52 +0000 | [diff] [blame] | 72 | |
| 73 | const char* recommended_v8_flags = FPDF_GetRecommendedV8Flags(); |
Lei Zhang | 06d1acb | 2019-05-04 07:40:40 +0000 | [diff] [blame] | 74 | v8::V8::SetFlagsFromString(recommended_v8_flags); |
Lei Zhang | 72e8b69 | 2019-02-05 19:16:52 +0000 | [diff] [blame] | 75 | |
Tom Sepez | e0d3c50 | 2020-08-11 23:51:10 +0000 | [diff] [blame] | 76 | if (!js_flags.empty()) |
| 77 | v8::V8::SetFlagsFromString(js_flags.c_str()); |
| 78 | |
Lei Zhang | 72e8b69 | 2019-02-05 19:16:52 +0000 | [diff] [blame] | 79 | // By enabling predictable mode, V8 won't post any background tasks. |
| 80 | // By enabling GC, it makes it easier to chase use-after-free. |
| 81 | static const char kAdditionalV8Flags[] = "--predictable --expose-gc"; |
Lei Zhang | 06d1acb | 2019-05-04 07:40:40 +0000 | [diff] [blame] | 82 | v8::V8::SetFlagsFromString(kAdditionalV8Flags); |
Lei Zhang | 72e8b69 | 2019-02-05 19:16:52 +0000 | [diff] [blame] | 83 | |
| 84 | v8::V8::Initialize(); |
| 85 | return platform; |
| 86 | } |
| 87 | |
| 88 | } // namespace |
| 89 | |
| 90 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 91 | std::unique_ptr<v8::Platform> InitializeV8ForPDFiumWithStartupData( |
| 92 | const std::string& exe_path, |
Tom Sepez | e0d3c50 | 2020-08-11 23:51:10 +0000 | [diff] [blame] | 93 | const std::string& js_flags, |
Lei Zhang | 72e8b69 | 2019-02-05 19:16:52 +0000 | [diff] [blame] | 94 | const std::string& bin_dir, |
Lei Zhang | 72e8b69 | 2019-02-05 19:16:52 +0000 | [diff] [blame] | 95 | v8::StartupData* snapshot_blob) { |
Tom Sepez | e0d3c50 | 2020-08-11 23:51:10 +0000 | [diff] [blame] | 96 | std::unique_ptr<v8::Platform> platform = |
| 97 | InitializeV8Common(exe_path, js_flags); |
Lei Zhang | a8e8baf | 2019-10-08 17:53:04 +0000 | [diff] [blame] | 98 | if (snapshot_blob) { |
Lei Zhang | 72e8b69 | 2019-02-05 19:16:52 +0000 | [diff] [blame] | 99 | if (!GetExternalData(exe_path, bin_dir, "snapshot_blob.bin", snapshot_blob)) |
| 100 | return nullptr; |
Lei Zhang | 72e8b69 | 2019-02-05 19:16:52 +0000 | [diff] [blame] | 101 | v8::V8::SetSnapshotDataBlob(snapshot_blob); |
| 102 | } |
| 103 | return platform; |
| 104 | } |
| 105 | #else // V8_USE_EXTERNAL_STARTUP_DATA |
| 106 | std::unique_ptr<v8::Platform> InitializeV8ForPDFium( |
Tom Sepez | e0d3c50 | 2020-08-11 23:51:10 +0000 | [diff] [blame] | 107 | const std::string& exe_path, |
| 108 | const std::string& js_flags) { |
| 109 | return InitializeV8Common(exe_path, js_flags); |
Lei Zhang | 72e8b69 | 2019-02-05 19:16:52 +0000 | [diff] [blame] | 110 | } |
| 111 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
Tom Sepez | 30eddfa | 2021-02-16 19:24:00 +0000 | [diff] [blame] | 112 | |
| 113 | void ShutdownV8ForPDFium() { |
| 114 | #ifdef PDF_ENABLE_XFA |
| 115 | cppgc::ShutdownProcess(); |
| 116 | #endif |
Tom Sepez | e506d56 | 2022-01-24 20:52:53 +0000 | [diff] [blame] | 117 | v8::V8::Dispose(); |
Alan Screen | 5e30ae8 | 2021-12-01 23:08:27 +0000 | [diff] [blame] | 118 | v8::V8::DisposePlatform(); |
Tom Sepez | 30eddfa | 2021-02-16 19:24:00 +0000 | [diff] [blame] | 119 | } |