• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
5 #include "testing/test_support.h"
6 
7 #include <stdio.h>
8 #include <string.h>
9 
10 #include <string>
11 
12 #include "testing/utils/path_service.h"
13 
14 #ifdef PDF_ENABLE_V8
15 #include "v8/include/libplatform/libplatform.h"
16 #endif
17 
18 namespace {
19 
20 #ifdef PDF_ENABLE_V8
21 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
22 // Returns the full path for an external V8 data file based on either
23 // the currect exectuable path or an explicit override.
GetFullPathForSnapshotFile(const std::string & exe_path,const std::string & bin_dir,const std::string & filename)24 std::string GetFullPathForSnapshotFile(const std::string& exe_path,
25                                        const std::string& bin_dir,
26                                        const std::string& filename) {
27   std::string result;
28   if (!bin_dir.empty()) {
29     result = bin_dir;
30     if (*bin_dir.rbegin() != PATH_SEPARATOR) {
31       result += PATH_SEPARATOR;
32     }
33   } else if (!exe_path.empty()) {
34     size_t last_separator = exe_path.rfind(PATH_SEPARATOR);
35     if (last_separator != std::string::npos) {
36       result = exe_path.substr(0, last_separator + 1);
37     }
38   }
39   result += filename;
40   return result;
41 }
42 
GetExternalData(const std::string & exe_path,const std::string & bin_dir,const std::string & filename,v8::StartupData * result_data)43 bool GetExternalData(const std::string& exe_path,
44                      const std::string& bin_dir,
45                      const std::string& filename,
46                      v8::StartupData* result_data) {
47   std::string full_path =
48       GetFullPathForSnapshotFile(exe_path, bin_dir, filename);
49   size_t data_length = 0;
50   std::unique_ptr<char, pdfium::FreeDeleter> data_buffer =
51       GetFileContents(full_path.c_str(), &data_length);
52   if (!data_buffer)
53     return false;
54 
55   result_data->data = data_buffer.release();
56   result_data->raw_size = data_length;
57   return true;
58 }
59 #endif  // V8_USE_EXTERNAL_STARTUP_DATA
60 
InitializeV8Common(const char * exe_path,v8::Platform ** platform)61 void InitializeV8Common(const char* exe_path, v8::Platform** platform) {
62   v8::V8::InitializeICUDefaultLocation(exe_path);
63 
64   *platform = v8::platform::CreateDefaultPlatform();
65   v8::V8::InitializePlatform(*platform);
66 
67   // By enabling predictable mode, V8 won't post any background tasks.
68   // By enabling GC, it makes it easier to chase use-after-free.
69   const char v8_flags[] = "--predictable --expose-gc";
70   v8::V8::SetFlagsFromString(v8_flags, static_cast<int>(strlen(v8_flags)));
71   v8::V8::Initialize();
72 }
73 #endif  // PDF_ENABLE_V8
74 
75 }  // namespace
76 
GetFileContents(const char * filename,size_t * retlen)77 std::unique_ptr<char, pdfium::FreeDeleter> GetFileContents(const char* filename,
78                                                            size_t* retlen) {
79   FILE* file = fopen(filename, "rb");
80   if (!file) {
81     fprintf(stderr, "Failed to open: %s\n", filename);
82     return nullptr;
83   }
84   (void)fseek(file, 0, SEEK_END);
85   size_t file_length = ftell(file);
86   if (!file_length) {
87     return nullptr;
88   }
89   (void)fseek(file, 0, SEEK_SET);
90   std::unique_ptr<char, pdfium::FreeDeleter> buffer(
91       static_cast<char*>(malloc(file_length)));
92   if (!buffer) {
93     return nullptr;
94   }
95   size_t bytes_read = fread(buffer.get(), 1, file_length, file);
96   (void)fclose(file);
97   if (bytes_read != file_length) {
98     fprintf(stderr, "Failed to read: %s\n", filename);
99     return nullptr;
100   }
101   *retlen = bytes_read;
102   return buffer;
103 }
104 
GetPlatformWString(FPDF_WIDESTRING wstr)105 std::wstring GetPlatformWString(FPDF_WIDESTRING wstr) {
106   if (!wstr)
107     return nullptr;
108 
109   size_t characters = 0;
110   while (wstr[characters])
111     ++characters;
112 
113   std::wstring platform_string(characters, L'\0');
114   for (size_t i = 0; i < characters + 1; ++i) {
115     const unsigned char* ptr = reinterpret_cast<const unsigned char*>(&wstr[i]);
116     platform_string[i] = ptr[0] + 256 * ptr[1];
117   }
118   return platform_string;
119 }
120 
StringSplit(const std::string & str,char delimiter)121 std::vector<std::string> StringSplit(const std::string& str, char delimiter) {
122   std::vector<std::string> result;
123   size_t pos = 0;
124   while (1) {
125     size_t found = str.find(delimiter, pos);
126     if (found == std::string::npos)
127       break;
128 
129     result.push_back(str.substr(pos, found - pos));
130     pos = found + 1;
131   }
132   result.push_back(str.substr(pos));
133   return result;
134 }
135 
GetFPDFWideString(const std::wstring & wstr)136 std::unique_ptr<unsigned short, pdfium::FreeDeleter> GetFPDFWideString(
137     const std::wstring& wstr) {
138   size_t length = sizeof(uint16_t) * (wstr.length() + 1);
139   std::unique_ptr<unsigned short, pdfium::FreeDeleter> result(
140       static_cast<unsigned short*>(malloc(length)));
141   char* ptr = reinterpret_cast<char*>(result.get());
142   size_t i = 0;
143   for (wchar_t w : wstr) {
144     ptr[i++] = w & 0xff;
145     ptr[i++] = (w >> 8) & 0xff;
146   }
147   ptr[i++] = 0;
148   ptr[i] = 0;
149   return result;
150 }
151 
152 #ifdef PDF_ENABLE_V8
153 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
InitializeV8ForPDFium(const std::string & exe_path,const std::string & bin_dir,v8::StartupData * natives_blob,v8::StartupData * snapshot_blob,v8::Platform ** platform)154 bool InitializeV8ForPDFium(const std::string& exe_path,
155                            const std::string& bin_dir,
156                            v8::StartupData* natives_blob,
157                            v8::StartupData* snapshot_blob,
158                            v8::Platform** platform) {
159   InitializeV8Common(exe_path.c_str(), platform);
160   if (natives_blob && snapshot_blob) {
161     if (!GetExternalData(exe_path, bin_dir, "natives_blob.bin", natives_blob))
162       return false;
163     if (!GetExternalData(exe_path, bin_dir, "snapshot_blob.bin", snapshot_blob))
164       return false;
165     v8::V8::SetNativesDataBlob(natives_blob);
166     v8::V8::SetSnapshotDataBlob(snapshot_blob);
167   }
168   return true;
169 }
170 #else   // V8_USE_EXTERNAL_STARTUP_DATA
InitializeV8ForPDFium(const std::string & exe_path,v8::Platform ** platform)171 bool InitializeV8ForPDFium(const std::string& exe_path,
172                            v8::Platform** platform) {
173   InitializeV8Common(exe_path.c_str(), platform);
174   return true;
175 }
176 #endif  // V8_USE_EXTERNAL_STARTUP_DATA
177 #endif  // PDF_ENABLE_V8
178 
TestLoader(const char * pBuf,size_t len)179 TestLoader::TestLoader(const char* pBuf, size_t len)
180     : m_pBuf(pBuf), m_Len(len) {
181 }
182 
183 // static
GetBlock(void * param,unsigned long pos,unsigned char * pBuf,unsigned long size)184 int TestLoader::GetBlock(void* param,
185                          unsigned long pos,
186                          unsigned char* pBuf,
187                          unsigned long size) {
188   TestLoader* pLoader = static_cast<TestLoader*>(param);
189   if (pos + size < pos || pos + size > pLoader->m_Len)
190     return 0;
191 
192   memcpy(pBuf, pLoader->m_pBuf + pos, size);
193   return 1;
194 }
195 
TestSaver()196 TestSaver::TestSaver() {
197   FPDF_FILEWRITE::version = 1;
198   FPDF_FILEWRITE::WriteBlock = WriteBlockCallback;
199 }
200 
ClearString()201 void TestSaver::ClearString() {
202   m_String.clear();
203 }
204 
205 // static
WriteBlockCallback(FPDF_FILEWRITE * pFileWrite,const void * data,unsigned long size)206 int TestSaver::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
207                                   const void* data,
208                                   unsigned long size) {
209   TestSaver* pThis = static_cast<TestSaver*>(pFileWrite);
210   pThis->m_String.append(static_cast<const char*>(data), size);
211   return 1;
212 }
213