1 // Copyright 2014 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 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #include "core/fxcrt/fx_memory.h"
8 
9 #include <stdlib.h>  // For abort().
10 
11 #include <limits>
12 
13 #include "build/build_config.h"
14 #include "core/fxcrt/fx_safe_types.h"
15 #include "third_party/base/debug/alias.h"
16 
GetArrayBufferPartitionAllocator()17 pdfium::base::PartitionAllocatorGeneric& GetArrayBufferPartitionAllocator() {
18   static pdfium::base::PartitionAllocatorGeneric s_array_buffer_allocator;
19   return s_array_buffer_allocator;
20 }
21 
GetGeneralPartitionAllocator()22 pdfium::base::PartitionAllocatorGeneric& GetGeneralPartitionAllocator() {
23   static pdfium::base::PartitionAllocatorGeneric s_general_allocator;
24   return s_general_allocator;
25 }
26 
GetStringPartitionAllocator()27 pdfium::base::PartitionAllocatorGeneric& GetStringPartitionAllocator() {
28   static pdfium::base::PartitionAllocatorGeneric s_string_allocator;
29   return s_string_allocator;
30 }
31 
FXMEM_InitializePartitionAlloc()32 void FXMEM_InitializePartitionAlloc() {
33   static bool s_partition_allocators_initialized = false;
34   if (!s_partition_allocators_initialized) {
35     pdfium::base::PartitionAllocGlobalInit(FX_OutOfMemoryTerminate);
36     GetArrayBufferPartitionAllocator().init();
37     GetGeneralPartitionAllocator().init();
38     GetStringPartitionAllocator().init();
39     s_partition_allocators_initialized = true;
40   }
41 }
42 
FXMEM_DefaultAlloc(size_t byte_size)43 void* FXMEM_DefaultAlloc(size_t byte_size) {
44   return pdfium::base::PartitionAllocGenericFlags(
45       GetGeneralPartitionAllocator().root(),
46       pdfium::base::PartitionAllocReturnNull, byte_size, "GeneralPartition");
47 }
48 
FXMEM_DefaultCalloc(size_t num_elems,size_t byte_size)49 void* FXMEM_DefaultCalloc(size_t num_elems, size_t byte_size) {
50   return FX_SafeAlloc(num_elems, byte_size);
51 }
52 
FXMEM_DefaultRealloc(void * pointer,size_t new_size)53 void* FXMEM_DefaultRealloc(void* pointer, size_t new_size) {
54   return pdfium::base::PartitionReallocGenericFlags(
55       GetGeneralPartitionAllocator().root(),
56       pdfium::base::PartitionAllocReturnNull, pointer, new_size,
57       "GeneralPartition");
58 }
59 
FXMEM_DefaultFree(void * pointer)60 void FXMEM_DefaultFree(void* pointer) {
61   pdfium::base::PartitionFree(pointer);
62 }
63 
FX_OutOfMemoryTerminate()64 NOINLINE void FX_OutOfMemoryTerminate() {
65   // Convince the linker this should not be folded with similar functions using
66   // Identical Code Folding.
67   static int make_this_function_aliased = 0xbd;
68   pdfium::base::debug::Alias(&make_this_function_aliased);
69 
70   // Termimate cleanly if we can, else crash at a specific address (0xbd).
71   abort();
72 #if !defined(OS_WIN)
73   reinterpret_cast<void (*)()>(0xbd)();
74 #endif
75 }
76 
FX_SafeAlloc(size_t num_members,size_t member_size)77 void* FX_SafeAlloc(size_t num_members, size_t member_size) {
78   FX_SAFE_SIZE_T total = member_size;
79   total *= num_members;
80   if (!total.IsValid())
81     return nullptr;
82 
83   constexpr int kFlags = pdfium::base::PartitionAllocReturnNull |
84                          pdfium::base::PartitionAllocZeroFill;
85   return pdfium::base::PartitionAllocGenericFlags(
86       GetGeneralPartitionAllocator().root(), kFlags, total.ValueOrDie(),
87       "GeneralPartition");
88 }
89 
FX_SafeRealloc(void * ptr,size_t num_members,size_t member_size)90 void* FX_SafeRealloc(void* ptr, size_t num_members, size_t member_size) {
91   FX_SAFE_SIZE_T size = num_members;
92   size *= member_size;
93   if (!size.IsValid())
94     return nullptr;
95 
96   return pdfium::base::PartitionReallocGenericFlags(
97       GetGeneralPartitionAllocator().root(),
98       pdfium::base::PartitionAllocReturnNull, ptr, size.ValueOrDie(),
99       "GeneralPartition");
100 }
101 
FX_AllocOrDie(size_t num_members,size_t member_size)102 void* FX_AllocOrDie(size_t num_members, size_t member_size) {
103   // TODO(tsepez): See if we can avoid the implicit memset(0).
104   void* result = FX_SafeAlloc(num_members, member_size);
105   if (!result)
106     FX_OutOfMemoryTerminate();  // Never returns.
107 
108   return result;
109 }
110 
FX_AllocOrDie2D(size_t w,size_t h,size_t member_size)111 void* FX_AllocOrDie2D(size_t w, size_t h, size_t member_size) {
112   if (w >= std::numeric_limits<size_t>::max() / h)
113     FX_OutOfMemoryTerminate();  // Never returns.
114 
115   return FX_AllocOrDie(w * h, member_size);
116 }
117 
FX_ReallocOrDie(void * ptr,size_t num_members,size_t member_size)118 void* FX_ReallocOrDie(void* ptr, size_t num_members, size_t member_size) {
119   void* result = FX_SafeRealloc(ptr, num_members, member_size);
120   if (!result)
121     FX_OutOfMemoryTerminate();  // Never returns.
122 
123   return result;
124 }
125 
FX_Free(void * ptr)126 void FX_Free(void* ptr) {
127   // TODO(palmer): Removing this check exposes crashes when PDFium callers
128   // attempt to free |nullptr|. Although libc's |free| allows freeing |NULL|, no
129   // other Partition Alloc callers need this tolerant behavior. Additionally,
130   // checking for |nullptr| adds a branch to |PartitionFree|, and it's nice to
131   // not have to have that.
132   //
133   // So this check is hiding (what I consider to be) bugs, and we should try to
134   // fix them. https://bugs.chromium.org/p/pdfium/issues/detail?id=690
135   if (ptr)
136     pdfium::base::PartitionFree(ptr);
137 }
138