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 internal::Calloc(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.
71   abort();
72 }
73 
74 namespace internal {
75 
Alloc(size_t num_members,size_t member_size)76 void* Alloc(size_t num_members, size_t member_size) {
77   FX_SAFE_SIZE_T total = member_size;
78   total *= num_members;
79   if (!total.IsValid())
80     return nullptr;
81 
82   constexpr int kFlags = pdfium::base::PartitionAllocReturnNull;
83   return pdfium::base::PartitionAllocGenericFlags(
84       GetGeneralPartitionAllocator().root(), kFlags, total.ValueOrDie(),
85       "GeneralPartition");
86 }
87 
AllocOrDie(size_t num_members,size_t member_size)88 void* AllocOrDie(size_t num_members, size_t member_size) {
89   void* result = Alloc(num_members, member_size);
90   if (!result)
91     FX_OutOfMemoryTerminate();  // Never returns.
92 
93   return result;
94 }
95 
AllocOrDie2D(size_t w,size_t h,size_t member_size)96 void* AllocOrDie2D(size_t w, size_t h, size_t member_size) {
97   if (w >= std::numeric_limits<size_t>::max() / h)
98     FX_OutOfMemoryTerminate();  // Never returns.
99 
100   return AllocOrDie(w * h, member_size);
101 }
102 
Calloc(size_t num_members,size_t member_size)103 void* Calloc(size_t num_members, size_t member_size) {
104   FX_SAFE_SIZE_T total = member_size;
105   total *= num_members;
106   if (!total.IsValid())
107     return nullptr;
108 
109   constexpr int kFlags = pdfium::base::PartitionAllocReturnNull |
110                          pdfium::base::PartitionAllocZeroFill;
111   return pdfium::base::PartitionAllocGenericFlags(
112       GetGeneralPartitionAllocator().root(), kFlags, total.ValueOrDie(),
113       "GeneralPartition");
114 }
115 
Realloc(void * ptr,size_t num_members,size_t member_size)116 void* Realloc(void* ptr, size_t num_members, size_t member_size) {
117   FX_SAFE_SIZE_T size = num_members;
118   size *= member_size;
119   if (!size.IsValid())
120     return nullptr;
121 
122   return pdfium::base::PartitionReallocGenericFlags(
123       GetGeneralPartitionAllocator().root(),
124       pdfium::base::PartitionAllocReturnNull, ptr, size.ValueOrDie(),
125       "GeneralPartition");
126 }
127 
CallocOrDie(size_t num_members,size_t member_size)128 void* CallocOrDie(size_t num_members, size_t member_size) {
129   void* result = Calloc(num_members, member_size);
130   if (!result)
131     FX_OutOfMemoryTerminate();  // Never returns.
132 
133   return result;
134 }
135 
CallocOrDie2D(size_t w,size_t h,size_t member_size)136 void* CallocOrDie2D(size_t w, size_t h, size_t member_size) {
137   if (w >= std::numeric_limits<size_t>::max() / h)
138     FX_OutOfMemoryTerminate();  // Never returns.
139 
140   return CallocOrDie(w * h, member_size);
141 }
142 
ReallocOrDie(void * ptr,size_t num_members,size_t member_size)143 void* ReallocOrDie(void* ptr, size_t num_members, size_t member_size) {
144   void* result = Realloc(ptr, num_members, member_size);
145   if (!result)
146     FX_OutOfMemoryTerminate();  // Never returns.
147 
148   return result;
149 }
150 
151 }  // namespace internal
152 
FX_Free(void * ptr)153 void FX_Free(void* ptr) {
154   // TODO(palmer): Removing this check exposes crashes when PDFium callers
155   // attempt to free |nullptr|. Although libc's |free| allows freeing |NULL|, no
156   // other Partition Alloc callers need this tolerant behavior. Additionally,
157   // checking for |nullptr| adds a branch to |PartitionFree|, and it's nice to
158   // not have to have that.
159   //
160   // So this check is hiding (what I consider to be) bugs, and we should try to
161   // fix them. https://bugs.chromium.org/p/pdfium/issues/detail?id=690
162   if (ptr)
163     pdfium::base::PartitionFree(ptr);
164 }
165