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/allocator/partition_allocator/partition_alloc.h"
16 #include "third_party/base/debug/alias.h"
17 
GetArrayBufferPartitionAllocator()18 pdfium::base::PartitionAllocatorGeneric& GetArrayBufferPartitionAllocator() {
19   static pdfium::base::PartitionAllocatorGeneric s_array_buffer_allocator;
20   return s_array_buffer_allocator;
21 }
22 
GetGeneralPartitionAllocator()23 pdfium::base::PartitionAllocatorGeneric& GetGeneralPartitionAllocator() {
24   static pdfium::base::PartitionAllocatorGeneric s_general_allocator;
25   return s_general_allocator;
26 }
27 
GetStringPartitionAllocator()28 pdfium::base::PartitionAllocatorGeneric& GetStringPartitionAllocator() {
29   static pdfium::base::PartitionAllocatorGeneric s_string_allocator;
30   return s_string_allocator;
31 }
32 
FXMEM_InitializePartitionAlloc()33 void FXMEM_InitializePartitionAlloc() {
34   static bool s_partition_allocators_initialized = false;
35   if (!s_partition_allocators_initialized) {
36     pdfium::base::PartitionAllocGlobalInit(FX_OutOfMemoryTerminate);
37     GetArrayBufferPartitionAllocator().init();
38     GetGeneralPartitionAllocator().init();
39     GetStringPartitionAllocator().init();
40     s_partition_allocators_initialized = true;
41   }
42 }
43 
FXMEM_DefaultAlloc(size_t byte_size)44 void* FXMEM_DefaultAlloc(size_t byte_size) {
45   return pdfium::base::PartitionAllocGenericFlags(
46       GetGeneralPartitionAllocator().root(),
47       pdfium::base::PartitionAllocReturnNull, byte_size, "GeneralPartition");
48 }
49 
FXMEM_DefaultCalloc(size_t num_elems,size_t byte_size)50 void* FXMEM_DefaultCalloc(size_t num_elems, size_t byte_size) {
51   return pdfium::internal::Calloc(num_elems, byte_size);
52 }
53 
FXMEM_DefaultRealloc(void * pointer,size_t new_size)54 void* FXMEM_DefaultRealloc(void* pointer, size_t new_size) {
55   return pdfium::base::PartitionReallocGenericFlags(
56       GetGeneralPartitionAllocator().root(),
57       pdfium::base::PartitionAllocReturnNull, pointer, new_size,
58       "GeneralPartition");
59 }
60 
FXMEM_DefaultFree(void * pointer)61 void FXMEM_DefaultFree(void* pointer) {
62   pdfium::base::PartitionFree(pointer);
63 }
64 
FX_OutOfMemoryTerminate(size_t size)65 NOINLINE void FX_OutOfMemoryTerminate(size_t size) {
66   // Convince the linker this should not be folded with similar functions using
67   // Identical Code Folding.
68   static int make_this_function_aliased = 0xbd;
69   pdfium::base::debug::Alias(&make_this_function_aliased);
70 
71   // Termimate cleanly.
72   abort();
73 }
74 
75 namespace pdfium {
76 namespace internal {
77 
Alloc(size_t num_members,size_t member_size)78 void* Alloc(size_t num_members, size_t member_size) {
79   FX_SAFE_SIZE_T total = member_size;
80   total *= num_members;
81   if (!total.IsValid())
82     return nullptr;
83 
84   constexpr int kFlags = pdfium::base::PartitionAllocReturnNull;
85   return pdfium::base::PartitionAllocGenericFlags(
86       GetGeneralPartitionAllocator().root(), kFlags, total.ValueOrDie(),
87       "GeneralPartition");
88 }
89 
AllocOrDie(size_t num_members,size_t member_size)90 void* AllocOrDie(size_t num_members, size_t member_size) {
91   void* result = Alloc(num_members, member_size);
92   if (!result)
93     FX_OutOfMemoryTerminate(0);  // Never returns.
94 
95   return result;
96 }
97 
AllocOrDie2D(size_t w,size_t h,size_t member_size)98 void* AllocOrDie2D(size_t w, size_t h, size_t member_size) {
99   if (w >= std::numeric_limits<size_t>::max() / h)
100     FX_OutOfMemoryTerminate(0);  // Never returns.
101 
102   return AllocOrDie(w * h, member_size);
103 }
104 
Calloc(size_t num_members,size_t member_size)105 void* Calloc(size_t num_members, size_t member_size) {
106   FX_SAFE_SIZE_T total = member_size;
107   total *= num_members;
108   if (!total.IsValid())
109     return nullptr;
110 
111   constexpr int kFlags = pdfium::base::PartitionAllocReturnNull |
112                          pdfium::base::PartitionAllocZeroFill;
113   return pdfium::base::PartitionAllocGenericFlags(
114       GetGeneralPartitionAllocator().root(), kFlags, total.ValueOrDie(),
115       "GeneralPartition");
116 }
117 
Realloc(void * ptr,size_t num_members,size_t member_size)118 void* Realloc(void* ptr, size_t num_members, size_t member_size) {
119   FX_SAFE_SIZE_T size = num_members;
120   size *= member_size;
121   if (!size.IsValid())
122     return nullptr;
123 
124   return pdfium::base::PartitionReallocGenericFlags(
125       GetGeneralPartitionAllocator().root(),
126       pdfium::base::PartitionAllocReturnNull, ptr, size.ValueOrDie(),
127       "GeneralPartition");
128 }
129 
CallocOrDie(size_t num_members,size_t member_size)130 void* CallocOrDie(size_t num_members, size_t member_size) {
131   void* result = Calloc(num_members, member_size);
132   if (!result)
133     FX_OutOfMemoryTerminate(0);  // Never returns.
134 
135   return result;
136 }
137 
CallocOrDie2D(size_t w,size_t h,size_t member_size)138 void* CallocOrDie2D(size_t w, size_t h, size_t member_size) {
139   if (w >= std::numeric_limits<size_t>::max() / h)
140     FX_OutOfMemoryTerminate(0);  // Never returns.
141 
142   return CallocOrDie(w * h, member_size);
143 }
144 
ReallocOrDie(void * ptr,size_t num_members,size_t member_size)145 void* ReallocOrDie(void* ptr, size_t num_members, size_t member_size) {
146   void* result = Realloc(ptr, num_members, member_size);
147   if (!result)
148     FX_OutOfMemoryTerminate(0);  // Never returns.
149 
150   return result;
151 }
152 
153 }  // namespace internal
154 }  // namespace pdfium
155 
FX_Free(void * ptr)156 void FX_Free(void* ptr) {
157   // TODO(palmer): Removing this check exposes crashes when PDFium callers
158   // attempt to free |nullptr|. Although libc's |free| allows freeing |NULL|, no
159   // other Partition Alloc callers need this tolerant behavior. Additionally,
160   // checking for |nullptr| adds a branch to |PartitionFree|, and it's nice to
161   // not have to have that.
162   //
163   // So this check is hiding (what I consider to be) bugs, and we should try to
164   // fix them. https://bugs.chromium.org/p/pdfium/issues/detail?id=690
165   if (ptr)
166     pdfium::base::PartitionFree(ptr);
167 }
168