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