Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 1 | // Copyright 2015 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 | |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 5 | #include "testing/test_support.h" |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 6 | |
| 7 | #include <stdio.h> |
| 8 | #include <string.h> |
| 9 | |
Lei Zhang | d145e4b | 2018-10-12 18:54:31 +0000 | [diff] [blame] | 10 | #include "core/fdrm/fx_crypt.h" |
Jane Liu | 4442d45 | 2017-07-19 10:19:42 -0400 | [diff] [blame] | 11 | #include "core/fxcrt/fx_string.h" |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 12 | #include "testing/utils/path_service.h" |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 13 | |
Lei Zhang | 8241df7 | 2015-11-06 14:38:48 -0800 | [diff] [blame] | 14 | #ifdef PDF_ENABLE_V8 |
| 15 | #include "v8/include/libplatform/libplatform.h" |
Lei Zhang | 9a25ede | 2017-06-22 00:05:12 -0700 | [diff] [blame] | 16 | #include "v8/include/v8.h" |
Lei Zhang | 8241df7 | 2015-11-06 14:38:48 -0800 | [diff] [blame] | 17 | #endif |
| 18 | |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 19 | namespace { |
| 20 | |
| 21 | #ifdef PDF_ENABLE_V8 |
| 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; |
Lei Zhang | bcc03a4 | 2016-01-07 00:48:00 -0800 | [diff] [blame] | 51 | std::unique_ptr<char, pdfium::FreeDeleter> data_buffer = |
| 52 | GetFileContents(full_path.c_str(), &data_length); |
| 53 | if (!data_buffer) |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 54 | return false; |
Lei Zhang | bcc03a4 | 2016-01-07 00:48:00 -0800 | [diff] [blame] | 55 | |
| 56 | result_data->data = data_buffer.release(); |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 57 | result_data->raw_size = data_length; |
| 58 | return true; |
| 59 | } |
| 60 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 61 | |
Lei Zhang | b0fb8cc | 2018-02-08 14:01:32 +0000 | [diff] [blame] | 62 | std::unique_ptr<v8::Platform> InitializeV8Common(const std::string& exe_path) { |
| 63 | v8::V8::InitializeICUDefaultLocation(exe_path.c_str()); |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 64 | |
Lei Zhang | b0fb8cc | 2018-02-08 14:01:32 +0000 | [diff] [blame] | 65 | std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform(); |
| 66 | v8::V8::InitializePlatform(platform.get()); |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 67 | |
Tom Sepez | 2a5632a | 2018-11-29 00:12:34 +0000 | [diff] [blame] | 68 | const char* recommended_v8_flags = FPDF_GetRecommendedV8Flags(); |
| 69 | v8::V8::SetFlagsFromString(recommended_v8_flags, |
| 70 | static_cast<int>(strlen(recommended_v8_flags))); |
| 71 | |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 72 | // By enabling predictable mode, V8 won't post any background tasks. |
tsepez | 9882243 | 2017-01-11 05:53:10 -0800 | [diff] [blame] | 73 | // By enabling GC, it makes it easier to chase use-after-free. |
Tom Sepez | 2a5632a | 2018-11-29 00:12:34 +0000 | [diff] [blame] | 74 | static const char kAdditionalV8Flags[] = "--predictable --expose-gc"; |
| 75 | v8::V8::SetFlagsFromString(kAdditionalV8Flags, |
| 76 | static_cast<int>(strlen(kAdditionalV8Flags))); |
| 77 | |
tsepez | 9882243 | 2017-01-11 05:53:10 -0800 | [diff] [blame] | 78 | v8::V8::Initialize(); |
Lei Zhang | b0fb8cc | 2018-02-08 14:01:32 +0000 | [diff] [blame] | 79 | return platform; |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 80 | } |
| 81 | #endif // PDF_ENABLE_V8 |
| 82 | |
| 83 | } // namespace |
| 84 | |
Tom Sepez | 0aa3531 | 2016-01-06 10:16:32 -0800 | [diff] [blame] | 85 | std::unique_ptr<char, pdfium::FreeDeleter> GetFileContents(const char* filename, |
| 86 | size_t* retlen) { |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 87 | FILE* file = fopen(filename, "rb"); |
| 88 | if (!file) { |
| 89 | fprintf(stderr, "Failed to open: %s\n", filename); |
| 90 | return nullptr; |
| 91 | } |
Ryan Harrison | 6cec70a | 2018-06-05 14:06:10 +0000 | [diff] [blame] | 92 | (void)fseek(file, 0, SEEK_END); |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 93 | size_t file_length = ftell(file); |
| 94 | if (!file_length) { |
| 95 | return nullptr; |
| 96 | } |
Ryan Harrison | 6cec70a | 2018-06-05 14:06:10 +0000 | [diff] [blame] | 97 | (void)fseek(file, 0, SEEK_SET); |
Tom Sepez | 0aa3531 | 2016-01-06 10:16:32 -0800 | [diff] [blame] | 98 | std::unique_ptr<char, pdfium::FreeDeleter> buffer( |
| 99 | static_cast<char*>(malloc(file_length))); |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 100 | if (!buffer) { |
| 101 | return nullptr; |
| 102 | } |
Tom Sepez | 0aa3531 | 2016-01-06 10:16:32 -0800 | [diff] [blame] | 103 | size_t bytes_read = fread(buffer.get(), 1, file_length, file); |
Ryan Harrison | 6cec70a | 2018-06-05 14:06:10 +0000 | [diff] [blame] | 104 | (void)fclose(file); |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 105 | if (bytes_read != file_length) { |
| 106 | fprintf(stderr, "Failed to read: %s\n", filename); |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 107 | return nullptr; |
| 108 | } |
| 109 | *retlen = bytes_read; |
| 110 | return buffer; |
| 111 | } |
| 112 | |
Jane Liu | 4442d45 | 2017-07-19 10:19:42 -0400 | [diff] [blame] | 113 | std::string GetPlatformString(FPDF_WIDESTRING wstr) { |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 114 | WideString wide_string = |
| 115 | WideString::FromUTF16LE(wstr, WideString::WStringLength(wstr)); |
Tom Sepez | b4c95fe | 2018-11-27 01:09:44 +0000 | [diff] [blame] | 116 | return std::string(wide_string.ToUTF8().c_str()); |
Jane Liu | 4442d45 | 2017-07-19 10:19:42 -0400 | [diff] [blame] | 117 | } |
| 118 | |
Tom Sepez | 8ab45ea | 2016-01-05 10:17:30 -0800 | [diff] [blame] | 119 | std::wstring GetPlatformWString(FPDF_WIDESTRING wstr) { |
Lei Zhang | 79e893a | 2015-11-04 16:02:47 -0800 | [diff] [blame] | 120 | if (!wstr) |
| 121 | return nullptr; |
| 122 | |
| 123 | size_t characters = 0; |
| 124 | while (wstr[characters]) |
| 125 | ++characters; |
| 126 | |
| 127 | std::wstring platform_string(characters, L'\0'); |
| 128 | for (size_t i = 0; i < characters + 1; ++i) { |
| 129 | const unsigned char* ptr = reinterpret_cast<const unsigned char*>(&wstr[i]); |
| 130 | platform_string[i] = ptr[0] + 256 * ptr[1]; |
| 131 | } |
| 132 | return platform_string; |
| 133 | } |
| 134 | |
tsepez | f09bdfa | 2016-04-18 16:08:26 -0700 | [diff] [blame] | 135 | std::vector<std::string> StringSplit(const std::string& str, char delimiter) { |
| 136 | std::vector<std::string> result; |
| 137 | size_t pos = 0; |
| 138 | while (1) { |
| 139 | size_t found = str.find(delimiter, pos); |
| 140 | if (found == std::string::npos) |
| 141 | break; |
| 142 | |
| 143 | result.push_back(str.substr(pos, found - pos)); |
| 144 | pos = found + 1; |
| 145 | } |
| 146 | result.push_back(str.substr(pos)); |
| 147 | return result; |
| 148 | } |
| 149 | |
Tom Sepez | 0aa3531 | 2016-01-06 10:16:32 -0800 | [diff] [blame] | 150 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> GetFPDFWideString( |
| 151 | const std::wstring& wstr) { |
Tom Sepez | 8ab45ea | 2016-01-05 10:17:30 -0800 | [diff] [blame] | 152 | size_t length = sizeof(uint16_t) * (wstr.length() + 1); |
Tom Sepez | 0aa3531 | 2016-01-06 10:16:32 -0800 | [diff] [blame] | 153 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> result( |
| 154 | static_cast<unsigned short*>(malloc(length))); |
| 155 | char* ptr = reinterpret_cast<char*>(result.get()); |
Tom Sepez | 8ab45ea | 2016-01-05 10:17:30 -0800 | [diff] [blame] | 156 | size_t i = 0; |
| 157 | for (wchar_t w : wstr) { |
| 158 | ptr[i++] = w & 0xff; |
| 159 | ptr[i++] = (w >> 8) & 0xff; |
| 160 | } |
| 161 | ptr[i++] = 0; |
| 162 | ptr[i] = 0; |
Tom Sepez | 0aa3531 | 2016-01-06 10:16:32 -0800 | [diff] [blame] | 163 | return result; |
Tom Sepez | 8ab45ea | 2016-01-05 10:17:30 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Lei Zhang | d9e0e6e | 2017-04-28 16:54:10 -0700 | [diff] [blame] | 166 | std::string CryptToBase16(const uint8_t* digest) { |
| 167 | static char const zEncode[] = "0123456789abcdef"; |
| 168 | std::string ret; |
| 169 | ret.resize(32); |
| 170 | for (int i = 0, j = 0; i < 16; i++, j += 2) { |
| 171 | uint8_t a = digest[i]; |
| 172 | ret[j] = zEncode[(a >> 4) & 0xf]; |
| 173 | ret[j + 1] = zEncode[a & 0xf]; |
| 174 | } |
| 175 | return ret; |
| 176 | } |
| 177 | |
| 178 | std::string GenerateMD5Base16(const uint8_t* data, uint32_t size) { |
| 179 | uint8_t digest[16]; |
| 180 | CRYPT_MD5Generate(data, size, digest); |
| 181 | return CryptToBase16(digest); |
| 182 | } |
| 183 | |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 184 | #ifdef PDF_ENABLE_V8 |
| 185 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
Lei Zhang | b0fb8cc | 2018-02-08 14:01:32 +0000 | [diff] [blame] | 186 | std::unique_ptr<v8::Platform> InitializeV8ForPDFiumWithStartupData( |
| 187 | const std::string& exe_path, |
| 188 | const std::string& bin_dir, |
| 189 | v8::StartupData* natives_blob, |
| 190 | v8::StartupData* snapshot_blob) { |
| 191 | std::unique_ptr<v8::Platform> platform = InitializeV8Common(exe_path); |
| 192 | if (natives_blob && snapshot_blob) { |
| 193 | if (!GetExternalData(exe_path, bin_dir, "natives_blob.bin", natives_blob)) |
| 194 | return nullptr; |
| 195 | if (!GetExternalData(exe_path, bin_dir, "snapshot_blob.bin", snapshot_blob)) |
| 196 | return nullptr; |
| 197 | v8::V8::SetNativesDataBlob(natives_blob); |
| 198 | v8::V8::SetSnapshotDataBlob(snapshot_blob); |
| 199 | } |
| 200 | return platform; |
| 201 | } |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 202 | #else // V8_USE_EXTERNAL_STARTUP_DATA |
Lei Zhang | b0fb8cc | 2018-02-08 14:01:32 +0000 | [diff] [blame] | 203 | std::unique_ptr<v8::Platform> InitializeV8ForPDFium( |
| 204 | const std::string& exe_path) { |
| 205 | return InitializeV8Common(exe_path); |
| 206 | } |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 207 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 208 | #endif // PDF_ENABLE_V8 |
| 209 | |
| 210 | TestLoader::TestLoader(const char* pBuf, size_t len) |
| 211 | : m_pBuf(pBuf), m_Len(len) { |
| 212 | } |
| 213 | |
| 214 | // static |
| 215 | int TestLoader::GetBlock(void* param, |
| 216 | unsigned long pos, |
| 217 | unsigned char* pBuf, |
| 218 | unsigned long size) { |
| 219 | TestLoader* pLoader = static_cast<TestLoader*>(param); |
| 220 | if (pos + size < pos || pos + size > pLoader->m_Len) |
| 221 | return 0; |
| 222 | |
| 223 | memcpy(pBuf, pLoader->m_pBuf + pos, size); |
| 224 | return 1; |
| 225 | } |