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