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