• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The 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 <string.h>
6 
7 #include <memory>
8 
9 #include "public/fpdf_ext.h"
10 
11 #ifdef PDF_ENABLE_V8
12 #include "testing/free_deleter.h"
13 #include "testing/v8_initializer.h"
14 #include "v8/include/v8-platform.h"
15 #include "v8/include/v8.h"
16 #endif
17 
18 #ifdef _WIN32
19 #include <windows.h>
20 #elif defined(__APPLE__)
21 #include <mach-o/dyld.h>
22 #else  // Linux
23 #include <linux/limits.h>
24 #include <unistd.h>
25 #endif  // _WIN32
26 
27 namespace {
28 
29 #ifdef PDF_ENABLE_V8
ProgramPath()30 std::string ProgramPath() {
31   std::string result;
32 
33 #ifdef _WIN32
34   char path[MAX_PATH];
35   DWORD len = GetModuleFileNameA(nullptr, path, MAX_PATH);
36   if (len != 0)
37     result = std::string(path, len);
38 #elif defined(__APPLE__)
39   char path[PATH_MAX];
40   unsigned int len = PATH_MAX;
41   if (!_NSGetExecutablePath(path, &len)) {
42     std::unique_ptr<char, pdfium::FreeDeleter> resolved_path(
43         realpath(path, nullptr));
44     if (resolved_path.get())
45       result = std::string(resolved_path.get());
46   }
47 #else  // Linux
48   char path[PATH_MAX];
49   ssize_t len = readlink("/proc/self/exe", path, PATH_MAX);
50   if (len > 0)
51     result = std::string(path, len);
52 #endif
53   return result;
54 }
55 #endif  // PDF_ENABLE_V8
56 
57 }  // namespace
58 
59 // Initialize the library once for all runs of the fuzzer.
60 struct TestCase {
TestCaseTestCase61   TestCase() {
62 #ifdef PDF_ENABLE_V8
63 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
64     platform = InitializeV8ForPDFiumWithStartupData(
65         ProgramPath(), std::string(), &snapshot_blob);
66 #else
67     platform = InitializeV8ForPDFium(ProgramPath());
68 #endif  // V8_USE_EXTERNAL_STARTUP_DATA
69 #endif  // PDF_ENABLE_V8
70 
71     memset(&config, '\0', sizeof(config));
72     config.version = 2;
73     config.m_pUserFontPaths = nullptr;
74     config.m_pIsolate = nullptr;
75     config.m_v8EmbedderSlot = 0;
76     FPDF_InitLibraryWithConfig(&config);
77 
78     memset(&unsupport_info, '\0', sizeof(unsupport_info));
79     unsupport_info.version = 1;
80     unsupport_info.FSDK_UnSupport_Handler = [](UNSUPPORT_INFO*, int) {};
81     FSDK_SetUnSpObjProcessHandler(&unsupport_info);
82   }
83 
84 #ifdef PDF_ENABLE_V8
85   std::unique_ptr<v8::Platform> platform;
86   v8::StartupData snapshot_blob;
87 #endif
88 
89   FPDF_LIBRARY_CONFIG config;
90   UNSUPPORT_INFO unsupport_info;
91 };
92 
93 // pdf_fuzzer_init.cc and pdfium_fuzzer_init_public.cc are mutually exclusive
94 // and should not be built together. They deliberately have the same global
95 // variable.
96 static TestCase* g_test_case = new TestCase();
97