Created
September 2, 2015 23:08
-
-
Save struct/0b1a96f7fdfddca91cf0 to your computer and use it in GitHub Desktop.
A patch to add support for PartitionAlloc in PDFIUM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/core/include/fpdfapi/fpdf_objects.h b/core/include/fpdfapi/fpdf_objects.h | |
index c2d838d..099eb62 100644 | |
--- a/core/include/fpdfapi/fpdf_objects.h | |
+++ b/core/include/fpdfapi/fpdf_objects.h | |
@@ -39,6 +39,22 @@ class IFX_FileRead; | |
class CPDF_Object { | |
public: | |
+ | |
+#ifdef PDFIUM_PARTITIONALLOC | |
+ void *operator new(size_t size) { | |
+ void *p = partitionAllocGeneric(g_pdfium_genericAllocator.root(), size); | |
+ // See comment in fx_memory.h | |
+ memset(p, 0x0, size); | |
+ printf("[PDFIUM_PARTITIONALLOC DEBUG] new CPDF_Object(%d) = %p\n", size, p); | |
+ return p; | |
+ } | |
+ | |
+ void operator delete(void *pointer) throw() { | |
+ printf("[PDFIUM_PARTITIONALLOC DEBUG] delete CPDF_Object %p\n", pointer); | |
+ partitionFreeGeneric(g_pdfium_genericAllocator.root(), pointer); | |
+ } | |
+#endif | |
+ | |
int GetType() const { return m_Type; } | |
FX_DWORD GetObjNum() const { return m_ObjNum; } | |
diff --git a/core/include/fxcrt/fx_memory.h b/core/include/fxcrt/fx_memory.h | |
index c607de2..0b295c5 100644 | |
--- a/core/include/fxcrt/fx_memory.h | |
+++ b/core/include/fxcrt/fx_memory.h | |
@@ -10,6 +10,7 @@ | |
#include "fx_system.h" | |
#ifdef __cplusplus | |
+ | |
extern "C" { | |
#endif | |
// For external C libraries to malloc through PDFium. These may return NULL. | |
@@ -25,18 +26,41 @@ void FXMEM_DefaultFree(void* pointer, int flags); | |
NEVER_INLINE void FX_OutOfMemoryTerminate(); | |
+#ifdef PDFIUM_PARTITIONALLOC | |
+extern PartitionAllocatorGeneric g_pdfium_genericAllocator; | |
+#endif | |
+ | |
inline void* FX_SafeRealloc(void* ptr, size_t num_members, size_t member_size) { | |
+#ifdef PDFIUM_PARTITIONALLOC | |
+ if (num_members < std::numeric_limits<size_t>::max() / member_size) { | |
+ printf("[PDFIUM_PARTITIONALLOC DEBUG] FX_SafeRealloc(%d)\n", num_members * member_size); | |
+ return partitionReallocGeneric(g_pdfium_genericAllocator.root(), ptr, num_members * member_size); | |
+ } | |
+#else | |
if (num_members < std::numeric_limits<size_t>::max() / member_size) { | |
return realloc(ptr, num_members * member_size); | |
} | |
+#endif | |
return nullptr; | |
} | |
inline void* FX_AllocOrDie(size_t num_members, size_t member_size) { | |
+#ifdef PDFIUM_PARTITIONALLOC | |
+ if (num_members < std::numeric_limits<size_t>::max() / member_size) { | |
+ void *p = partitionAllocGeneric(g_pdfium_genericAllocator.root(), num_members * member_size); | |
+ // Many pdfium allocations assume underlying FX_Alloc() implementation | |
+ // is calloc() and use the memory uninitialized. This is not good, but | |
+ // but without zeroing the buffer here we crash all over | |
+ memset(p, 0x0, num_members * member_size); | |
+ printf("[PDFIUM_PARTITIONALLOC DEBUG] FX_AllocOrDie(%d) = %p\n", num_members * member_size, p); | |
+ return p; | |
+ } | |
+#else | |
// TODO(tsepez): See if we can avoid the implicit memset(0). | |
if (void* result = calloc(num_members, member_size)) { | |
return result; | |
} | |
+#endif | |
FX_OutOfMemoryTerminate(); // Never returns. | |
return nullptr; // Suppress compiler warning. | |
} | |
@@ -66,11 +90,23 @@ inline void* FX_ReallocOrDie(void* ptr, | |
(type*) FX_ReallocOrDie(ptr, size, sizeof(type)) | |
// May return NULL. | |
+#ifdef PDFIUM_PARTITIONALLOC | |
+#define FX_TryAlloc(type, size) (type *) FX_AllocOrDie(size, sizeof(type)) | |
+#else | |
#define FX_TryAlloc(type, size) (type*) calloc(size, sizeof(type)) | |
+#endif | |
+ | |
#define FX_TryRealloc(type, ptr, size) \ | |
(type*) FX_SafeRealloc(ptr, size, sizeof(type)) | |
-#define FX_Free(ptr) free(ptr) | |
+#ifdef PDFIUM_PARTITIONALLOC | |
+#define FX_Free(ptr) {\ | |
+ printf("[PDFIUM_PARTITIONALLOC DEBUG] FX_Free(%p)\n", ptr); \ | |
+ partitionFreeGeneric(g_pdfium_genericAllocator.root(), ptr); \ | |
+ } | |
+#else | |
+#define FX_Free(ptr) free(ptr); | |
+#endif | |
class CFX_DestructObject { | |
public: | |
diff --git a/core/include/fxcrt/fx_system.h b/core/include/fxcrt/fx_system.h | |
index ad63d56..a1682a4 100644 | |
--- a/core/include/fxcrt/fx_system.h | |
+++ b/core/include/fxcrt/fx_system.h | |
@@ -17,6 +17,16 @@ | |
#include <string.h> | |
#include <wchar.h> | |
+// Use PartitionAlloc | |
+// This global partition is initialized in FPDFInitLibrary() | |
+#ifdef __cplusplus | |
+#include "config.h" | |
+#include "PartitionAlloc.h" | |
+#define PDFIUM_PARTITIONALLOC 1 | |
+#else | |
+#undef PDFIUM_PARTITIONALLOC | |
+#endif | |
+ | |
// _FX_OS_ values: | |
#define _FX_WIN32_DESKTOP_ 1 | |
#define _FX_WIN64_DESKTOP_ 2 | |
@@ -97,13 +107,13 @@ typedef int FX_STRSIZE; | |
#endif | |
#define FXSYS_assert assert | |
-#ifndef ASSERT | |
+/*#ifndef ASSERT | |
#ifdef _DEBUG | |
#define ASSERT FXSYS_assert | |
#else | |
#define ASSERT(a) | |
#endif | |
-#endif | |
+#endif*/ | |
#define FX_MAX(a, b) (((a) > (b)) ? (a) : (b)) | |
#define FX_MIN(a, b) (((a) < (b)) ? (a) : (b)) | |
diff --git a/core/src/fpdfapi/fpdf_edit/fpdf_edit_doc.cpp b/core/src/fpdfapi/fpdf_edit/fpdf_edit_doc.cpp | |
index fa21ac1..745bd58 100644 | |
--- a/core/src/fpdfapi/fpdf_edit/fpdf_edit_doc.cpp | |
+++ b/core/src/fpdfapi/fpdf_edit/fpdf_edit_doc.cpp | |
@@ -486,7 +486,8 @@ static void _CFString2CFXByteString(CFStringRef src, CFX_ByteString& dest) { | |
SInt32 len = CFStringGetLength(src); | |
CFRange range = CFRangeMake(0, len); | |
CFIndex used = 0; | |
- UInt8* pBuffer = (UInt8*)calloc(len + 1, sizeof(UInt8)); | |
+ //UInt8* pBuffer = (UInt8*)calloc(len + 1, sizeof(UInt8)); | |
+ UInt8* pBuffer = (UInt8*)FX_Alloc(len + 1, sizeof(UInt8)); | |
CFStringGetBytes(src, range, kCFStringEncodingASCII, 0, false, pBuffer, len, | |
&used); | |
dest = (FX_CHAR*)pBuffer; | |
diff --git a/core/src/fxcrt/fx_basic_memmgr.cpp b/core/src/fxcrt/fx_basic_memmgr.cpp | |
index ebc2585..72b5ee2 100644 | |
--- a/core/src/fxcrt/fx_basic_memmgr.cpp | |
+++ b/core/src/fxcrt/fx_basic_memmgr.cpp | |
@@ -8,13 +8,31 @@ | |
#include "../../include/fxcrt/fx_memory.h" | |
void* FXMEM_DefaultAlloc(size_t byte_size, int flags) { | |
+#ifdef PDFIUM_PARTITIONALLOC | |
+ void *p = partitionAllocGeneric(g_pdfium_genericAllocator.root(), byte_size); | |
+ // See comment in fx_memory.h | |
+ memset(p, 0x0, byte_size); | |
+ printf("[PDFIUM_PARTITIONALLOC DEBUG] FXMEM_DefaultAlloc(%d) = %p\n", byte_size); | |
+ return p; | |
+#else | |
return (void*)malloc(byte_size); | |
+#endif | |
} | |
void* FXMEM_DefaultRealloc(void* pointer, size_t new_size, int flags) { | |
+#ifdef PDFIUM_PARTITIONALLOC | |
+ printf("[PDFIUM_PARTITIONALLOC DEBUG] FXMEM_DefaultRealloc(%d)\n", new_size); | |
+ return partitionReallocGeneric(g_pdfium_genericAllocator.root(), pointer, new_size); | |
+#else | |
return realloc(pointer, new_size); | |
+#endif | |
} | |
void FXMEM_DefaultFree(void* pointer, int flags) { | |
+#ifdef PDFIUM_PARTITIONALLOC | |
+ printf("[PDFIUM_PARTITIONALLOC DEBUG] FXMEM_DefaultFree(%p)\n", pointer); | |
+ partitionFreeGeneric(g_pdfium_genericAllocator.root(), pointer); | |
+#else | |
free(pointer); | |
+#endif | |
} | |
NEVER_INLINE void FX_OutOfMemoryTerminate() { | |
diff --git a/fpdfsdk/include/javascript/JS_Object.h b/fpdfsdk/include/javascript/JS_Object.h | |
index 9fd7bff..1d6d5aa 100644 | |
--- a/fpdfsdk/include/javascript/JS_Object.h | |
+++ b/fpdfsdk/include/javascript/JS_Object.h | |
@@ -60,6 +60,21 @@ class CJS_Object { | |
virtual FX_BOOL InitInstance(IFXJS_Context* cc) { return TRUE; } | |
virtual FX_BOOL ExitInstance() { return TRUE; } | |
+#ifdef PDFIUM_PARTITIONALLOC | |
+ void *operator new(size_t size) { | |
+ void *p = partitionAllocGeneric(g_pdfium_genericAllocator.root(), size); | |
+ // See comment in fx_memory.h | |
+ memset(p, 0x0, size); | |
+ printf("[PDFIUM_PARTITIONALLOC DEBUG] new CJS_Object(%d) = %p\n", size, p); | |
+ return p; | |
+ } | |
+ | |
+ void operator delete(void *pointer) throw() { | |
+ printf("[PDFIUM_PARTITIONALLOC DEBUG] delete CJS_Object %p\n", pointer); | |
+ partitionFreeGeneric(g_pdfium_genericAllocator.root(), pointer); | |
+ } | |
+#endif | |
+ | |
operator JSFXObject() { | |
return v8::Local<v8::Object>::New(m_pIsolate, m_pObject); | |
} | |
diff --git a/fpdfsdk/src/fpdfview.cpp b/fpdfsdk/src/fpdfview.cpp | |
index a1d5689..dee1734 100644 | |
--- a/fpdfsdk/src/fpdfview.cpp | |
+++ b/fpdfsdk/src/fpdfview.cpp | |
@@ -4,6 +4,7 @@ | |
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | |
+#include "../../core/include/fxcrt/fx_system.h" | |
#include "../../core/include/fxcodec/fx_codec.h" | |
#include "../../core/include/fxcrt/fx_safe_types.h" | |
#include "../../public/fpdf_ext.h" | |
@@ -15,6 +16,11 @@ | |
#include "../include/fsdk_mgr.h" | |
#include "../include/fsdk_rendercontext.h" | |
+// TODO: This is dirty. The extern is declared in core/fxcrt/fx_memory.h | |
+#ifdef PDFIUM_PARTITIONALLOC | |
+PartitionAllocatorGeneric g_pdfium_genericAllocator; | |
+#endif | |
+ | |
CPDF_CustomAccess::CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess) { | |
if (pFileAccess) | |
m_FileAccess = *pFileAccess; | |
@@ -97,6 +103,10 @@ DLLEXPORT void STDCALL FPDF_InitLibrary() { | |
DLLEXPORT void STDCALL FPDF_InitLibraryWithConfig( | |
const FPDF_LIBRARY_CONFIG* cfg) { | |
+#ifdef PDFIUM_PARTITIONALLOC | |
+ g_pdfium_genericAllocator.init(); | |
+ printf("[PDFIUM_PARTITIONALLOC DEBUG] Initialized g_pdfium_genericAllocator %p\n", &g_pdfium_genericAllocator); | |
+#endif | |
g_pCodecModule = new CCodec_ModuleMgr(); | |
CFX_GEModule::Create(cfg ? cfg->m_pUserFontPaths : nullptr); | |
@@ -124,6 +134,10 @@ DLLEXPORT void STDCALL FPDF_DestroyLibrary() { | |
CFX_GEModule::Destroy(); | |
delete g_pCodecModule; | |
g_pCodecModule = nullptr; | |
+ | |
+#ifdef PDFIUM_PARTITIONALLOC | |
+ g_pdfium_genericAllocator.shutdown(); | |
+#endif | |
} | |
#ifndef _WIN32 | |
diff --git a/pdfium.gyp b/pdfium.gyp | |
index c3d591e..4d56961 100644 | |
--- a/pdfium.gyp | |
+++ b/pdfium.gyp | |
@@ -15,6 +15,7 @@ | |
'OPJ_STATIC', | |
'V8_DEPRECATION_WARNINGS', | |
'_CRT_SECURE_NO_WARNINGS', | |
+ 'PDFIUM_PARTITIONALLOC', | |
], | |
'include_dirs': [ | |
'third_party/freetype/include', | |
@@ -403,6 +404,21 @@ | |
'core/include/fxcrt/fx_system.h', | |
'core/include/fxcrt/fx_ucd.h', | |
'core/include/fxcrt/fx_xml.h', | |
+ 'core/include/fxcrt/AddressSpaceRandomization.cpp', | |
+ 'core/include/fxcrt/PageAllocator.cpp', | |
+ 'core/include/fxcrt/Assertions.cpp', | |
+ 'core/include/fxcrt/PartitionAlloc.cpp', | |
+ 'core/include/fxcrt/AddressSpaceRandomization.h', | |
+ 'core/include/fxcrt/Assertions.h', | |
+ 'core/include/fxcrt/Atomics.h', | |
+ 'core/include/fxcrt/BitwiseOperations.h', | |
+ 'core/include/fxcrt/ByteSwap.h', | |
+ 'core/include/fxcrt/Compiler.h', | |
+ 'core/include/fxcrt/CPU.h', | |
+ 'core/include/fxcrt/PageAllocator.h', | |
+ 'core/include/fxcrt/PartitionAlloc.h', | |
+ 'core/include/fxcrt/SpinLock.h', | |
+ 'core/include/fxcrt/WTFExport.h', | |
'core/src/fxcrt/extension.h', | |
'core/src/fxcrt/fxcrt_platforms.cpp', | |
'core/src/fxcrt/fxcrt_platforms.h', | |
user@ubuntu:~/chromium/src/third_party/pdfium$ | |
user@ubuntu:~/chromium/src/third_party/pdfium$ | |
user@ubuntu:~/chromium/src/third_party/pdfium$ | |
user@ubuntu:~/chromium/src/third_party/pdfium$ nano core/include/fxcrt/fx_memory.h fpdfsdk/include/javascript/JS_Object.h fpdfsdk/include/javascript/JS_Object.h fpdfsdk/src/fpdfview.cpp core/src/fpdfapi/fpdf_edit/fpdf_edit_doc.cpp core/src/fxcrt/fx_basic_memmgr.cpp core/include/fpdfapi/fpdf_objects.h^C | |
user@ubuntu:~/chromium/src/third_party/pdfium$ nano core/include/fxcrt/fx_memory.h | |
#include "../include/fsdk_mgr.h" | |
#include "../include/fsdk_rendercontext.h" | |
+// TODO: This is dirty. The extern is declared in core/fxcrt/fx_memory.h | |
+#ifdef PDFIUM_PARTITIONALLOC | |
+PartitionAllocatorGeneric g_pdfium_genericAllocator; | |
+#endif | |
+ | |
CPDF_CustomAccess::CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess) { | |
if (pFileAccess) | |
m_FileAccess = *pFileAccess; | |
@@ -97,6 +103,10 @@ DLLEXPORT void STDCALL FPDF_InitLibrary() { | |
DLLEXPORT void STDCALL FPDF_InitLibraryWithConfig( | |
const FPDF_LIBRARY_CONFIG* cfg) { | |
+#ifdef PDFIUM_PARTITIONALLOC | |
+ g_pdfium_genericAllocator.init(); | |
+ printf("[PDFIUM_PARTITIONALLOC DEBUG] Initialized g_pdfium_genericAllocator %p\n", &g_pdfium_genericAllocator); | |
+#endif | |
g_pCodecModule = new CCodec_ModuleMgr(); | |
CFX_GEModule::Create(cfg ? cfg->m_pUserFontPaths : nullptr); | |
@@ -124,6 +134,10 @@ DLLEXPORT void STDCALL FPDF_DestroyLibrary() { | |
CFX_GEModule::Destroy(); | |
delete g_pCodecModule; | |
g_pCodecModule = nullptr; | |
+ | |
+#ifdef PDFIUM_PARTITIONALLOC | |
+ g_pdfium_genericAllocator.shutdown(); | |
+#endif | |
} | |
#ifndef _WIN32 | |
diff --git a/pdfium.gyp b/pdfium.gyp | |
index c3d591e..4d56961 100644 | |
--- a/pdfium.gyp | |
+++ b/pdfium.gyp | |
@@ -15,6 +15,7 @@ | |
'OPJ_STATIC', | |
'V8_DEPRECATION_WARNINGS', | |
'_CRT_SECURE_NO_WARNINGS', | |
+ 'PDFIUM_PARTITIONALLOC', | |
], | |
'include_dirs': [ | |
'third_party/freetype/include', | |
@@ -403,6 +404,21 @@ | |
'core/include/fxcrt/fx_system.h', | |
'core/include/fxcrt/fx_ucd.h', | |
'core/include/fxcrt/fx_xml.h', | |
+ 'core/include/fxcrt/AddressSpaceRandomization.cpp', | |
+ 'core/include/fxcrt/PageAllocator.cpp', | |
+ 'core/include/fxcrt/Assertions.cpp', | |
+ 'core/include/fxcrt/PartitionAlloc.cpp', | |
+ 'core/include/fxcrt/AddressSpaceRandomization.h', | |
+ 'core/include/fxcrt/Assertions.h', | |
+ 'core/include/fxcrt/Atomics.h', | |
+ 'core/include/fxcrt/BitwiseOperations.h', | |
+ 'core/include/fxcrt/ByteSwap.h', | |
+ 'core/include/fxcrt/Compiler.h', | |
+ 'core/include/fxcrt/CPU.h', | |
+ 'core/include/fxcrt/PageAllocator.h', | |
+ 'core/include/fxcrt/PartitionAlloc.h', | |
+ 'core/include/fxcrt/SpinLock.h', | |
+ 'core/include/fxcrt/WTFExport.h', | |
'core/src/fxcrt/extension.h', | |
'core/src/fxcrt/fxcrt_platforms.cpp', | |
'core/src/fxcrt/fxcrt_platforms.h', |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment