• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2013 The Chromium 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 #ifndef BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_ALLOC_H
6 #define BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_ALLOC_H
7 
8 // DESCRIPTION
9 // partitionAlloc() / PartitionAllocGeneric() and PartitionFree() /
10 // PartitionFreeGeneric() are approximately analagous to malloc() and free().
11 //
12 // The main difference is that a PartitionRoot / PartitionRootGeneric object
13 // must be supplied to these functions, representing a specific "heap partition"
14 // that will be used to satisfy the allocation. Different partitions are
15 // guaranteed to exist in separate address spaces, including being separate from
16 // the main system heap. If the contained objects are all freed, physical memory
17 // is returned to the system but the address space remains reserved.
18 // See PartitionAlloc.md for other security properties PartitionAlloc provides.
19 //
20 // THE ONLY LEGITIMATE WAY TO OBTAIN A PartitionRoot IS THROUGH THE
21 // SizeSpecificPartitionAllocator / PartitionAllocatorGeneric classes. To
22 // minimize the instruction count to the fullest extent possible, the
23 // PartitionRoot is really just a header adjacent to other data areas provided
24 // by the allocator class.
25 //
26 // The partitionAlloc() variant of the API has the following caveats:
27 // - Allocations and frees against a single partition must be single threaded.
28 // - Allocations must not exceed a max size, chosen at compile-time via a
29 // templated parameter to PartitionAllocator.
30 // - Allocation sizes must be aligned to the system pointer size.
31 // - Allocations are bucketed exactly according to size.
32 //
33 // And for PartitionAllocGeneric():
34 // - Multi-threaded use against a single partition is ok; locking is handled.
35 // - Allocations of any arbitrary size can be handled (subject to a limit of
36 // INT_MAX bytes for security reasons).
37 // - Bucketing is by approximate size, for example an allocation of 4000 bytes
38 // might be placed into a 4096-byte bucket. Bucket sizes are chosen to try and
39 // keep worst-case waste to ~10%.
40 //
41 // The allocators are designed to be extremely fast, thanks to the following
42 // properties and design:
43 // - Just two single (reasonably predicatable) branches in the hot / fast path
44 //   for both allocating and (significantly) freeing.
45 // - A minimal number of operations in the hot / fast path, with the slow paths
46 //   in separate functions, leading to the possibility of inlining.
47 // - Each partition page (which is usually multiple physical pages) has a
48 //   metadata structure which allows fast mapping of free() address to an
49 //   underlying bucket.
50 // - Supports a lock-free API for fast performance in single-threaded cases.
51 // - The freelist for a given bucket is split across a number of partition
52 //   pages, enabling various simple tricks to try and minimize fragmentation.
53 // - Fine-grained bucket sizes leading to less waste and better packing.
54 //
55 // The following security properties could be investigated in the future:
56 // - Per-object bucketing (instead of per-size) is mostly available at the API,
57 // but not used yet.
58 // - No randomness of freelist entries or bucket position.
59 // - Better checking for wild pointers in free().
60 // - Better freelist masking function to guarantee fault on 32-bit.
61 
62 #include <limits.h>
63 #include <string.h>
64 
65 #include "third_party/base/allocator/partition_allocator/page_allocator.h"
66 #include "third_party/base/allocator/partition_allocator/spin_lock.h"
67 #include "third_party/base/bits.h"
68 #include "third_party/base/compiler_specific.h"
69 #include "third_party/base/logging.h"
70 #include "third_party/base/sys_byteorder.h"
71 #include "third_party/build/build_config.h"
72 
73 #if defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
74 #include <stdlib.h>
75 #endif
76 
77 namespace pdfium {
78 namespace base {
79 
80 // Allocation granularity of sizeof(void*) bytes.
81 static const size_t kAllocationGranularity = sizeof(void*);
82 static const size_t kAllocationGranularityMask = kAllocationGranularity - 1;
83 static const size_t kBucketShift = (kAllocationGranularity == 8) ? 3 : 2;
84 
85 // Underlying partition storage pages are a power-of-two size. It is typical
86 // for a partition page to be based on multiple system pages. Most references to
87 // "page" refer to partition pages.
88 // We also have the concept of "super pages" -- these are the underlying system
89 // allocations we make. Super pages contain multiple partition pages inside them
90 // and include space for a small amount of metadata per partition page.
91 // Inside super pages, we store "slot spans". A slot span is a continguous range
92 // of one or more partition pages that stores allocations of the same size.
93 // Slot span sizes are adjusted depending on the allocation size, to make sure
94 // the packing does not lead to unused (wasted) space at the end of the last
95 // system page of the span. For our current max slot span size of 64k and other
96 // constant values, we pack _all_ PartitionAllocGeneric() sizes perfectly up
97 // against the end of a system page.
98 #if defined(_MIPS_ARCH_LOONGSON)
99 static const size_t kPartitionPageShift = 16;  // 64KB
100 #else
101 static const size_t kPartitionPageShift = 14;  // 16KB
102 #endif
103 static const size_t kPartitionPageSize = 1 << kPartitionPageShift;
104 static const size_t kPartitionPageOffsetMask = kPartitionPageSize - 1;
105 static const size_t kPartitionPageBaseMask = ~kPartitionPageOffsetMask;
106 static const size_t kMaxPartitionPagesPerSlotSpan = 4;
107 
108 // To avoid fragmentation via never-used freelist entries, we hand out partition
109 // freelist sections gradually, in units of the dominant system page size.
110 // What we're actually doing is avoiding filling the full partition page (16 KB)
111 // with freelist pointers right away. Writing freelist pointers will fault and
112 // dirty a private page, which is very wasteful if we never actually store
113 // objects there.
114 static const size_t kNumSystemPagesPerPartitionPage =
115     kPartitionPageSize / kSystemPageSize;
116 static const size_t kMaxSystemPagesPerSlotSpan =
117     kNumSystemPagesPerPartitionPage * kMaxPartitionPagesPerSlotSpan;
118 
119 // We reserve virtual address space in 2MB chunks (aligned to 2MB as well).
120 // These chunks are called "super pages". We do this so that we can store
121 // metadata in the first few pages of each 2MB aligned section. This leads to
122 // a very fast free(). We specifically choose 2MB because this virtual address
123 // block represents a full but single PTE allocation on ARM, ia32 and x64.
124 //
125 // The layout of the super page is as follows. The sizes below are the same
126 // for 32 bit and 64 bit.
127 //
128 //   | Guard page (4KB)    |
129 //   | Metadata page (4KB) |
130 //   | Guard pages (8KB)   |
131 //   | Slot span           |
132 //   | Slot span           |
133 //   | ...                 |
134 //   | Slot span           |
135 //   | Guard page (4KB)    |
136 //
137 //   - Each slot span is a contiguous range of one or more PartitionPages.
138 //   - The metadata page has the following format. Note that the PartitionPage
139 //     that is not at the head of a slot span is "unused". In other words,
140 //     the metadata for the slot span is stored only in the first PartitionPage
141 //     of the slot span. Metadata accesses to other PartitionPages are
142 //     redirected to the first PartitionPage.
143 //
144 //     | SuperPageExtentEntry (32B)                 |
145 //     | PartitionPage of slot span 1 (32B, used)   |
146 //     | PartitionPage of slot span 1 (32B, unused) |
147 //     | PartitionPage of slot span 1 (32B, unused) |
148 //     | PartitionPage of slot span 2 (32B, used)   |
149 //     | PartitionPage of slot span 3 (32B, used)   |
150 //     | ...                                        |
151 //     | PartitionPage of slot span N (32B, unused) |
152 //
153 // A direct mapped page has a similar layout to fake it looking like a super
154 // page:
155 //
156 //     | Guard page (4KB)     |
157 //     | Metadata page (4KB)  |
158 //     | Guard pages (8KB)    |
159 //     | Direct mapped object |
160 //     | Guard page (4KB)     |
161 //
162 //    - The metadata page has the following layout:
163 //
164 //     | SuperPageExtentEntry (32B)    |
165 //     | PartitionPage (32B)           |
166 //     | PartitionBucket (32B)         |
167 //     | PartitionDirectMapExtent (8B) |
168 static const size_t kSuperPageShift = 21;  // 2MB
169 static const size_t kSuperPageSize = 1 << kSuperPageShift;
170 static const size_t kSuperPageOffsetMask = kSuperPageSize - 1;
171 static const size_t kSuperPageBaseMask = ~kSuperPageOffsetMask;
172 static const size_t kNumPartitionPagesPerSuperPage =
173     kSuperPageSize / kPartitionPageSize;
174 
175 static const size_t kPageMetadataShift = 5;  // 32 bytes per partition page.
176 static const size_t kPageMetadataSize = 1 << kPageMetadataShift;
177 
178 // The following kGeneric* constants apply to the generic variants of the API.
179 // The "order" of an allocation is closely related to the power-of-two size of
180 // the allocation. More precisely, the order is the bit index of the
181 // most-significant-bit in the allocation size, where the bit numbers starts
182 // at index 1 for the least-significant-bit.
183 // In terms of allocation sizes, order 0 covers 0, order 1 covers 1, order 2
184 // covers 2->3, order 3 covers 4->7, order 4 covers 8->15.
185 static const size_t kGenericMinBucketedOrder = 4;  // 8 bytes.
186 static const size_t kGenericMaxBucketedOrder =
187     20;  // Largest bucketed order is 1<<(20-1) (storing 512KB -> almost 1MB)
188 static const size_t kGenericNumBucketedOrders =
189     (kGenericMaxBucketedOrder - kGenericMinBucketedOrder) + 1;
190 // Eight buckets per order (for the higher orders), e.g. order 8 is 128, 144,
191 // 160, ..., 240:
192 static const size_t kGenericNumBucketsPerOrderBits = 3;
193 static const size_t kGenericNumBucketsPerOrder =
194     1 << kGenericNumBucketsPerOrderBits;
195 static const size_t kGenericNumBuckets =
196     kGenericNumBucketedOrders * kGenericNumBucketsPerOrder;
197 static const size_t kGenericSmallestBucket = 1
198                                              << (kGenericMinBucketedOrder - 1);
199 static const size_t kGenericMaxBucketSpacing =
200     1 << ((kGenericMaxBucketedOrder - 1) - kGenericNumBucketsPerOrderBits);
201 static const size_t kGenericMaxBucketed =
202     (1 << (kGenericMaxBucketedOrder - 1)) +
203     ((kGenericNumBucketsPerOrder - 1) * kGenericMaxBucketSpacing);
204 static const size_t kGenericMinDirectMappedDownsize =
205     kGenericMaxBucketed +
206     1;  // Limit when downsizing a direct mapping using realloc().
207 static const size_t kGenericMaxDirectMapped = INT_MAX - kSystemPageSize;
208 static const size_t kBitsPerSizeT = sizeof(void*) * CHAR_BIT;
209 
210 // Constants for the memory reclaim logic.
211 static const size_t kMaxFreeableSpans = 16;
212 
213 // If the total size in bytes of allocated but not committed pages exceeds this
214 // value (probably it is a "out of virtual address space" crash),
215 // a special crash stack trace is generated at |partitionOutOfMemory|.
216 // This is to distinguish "out of virtual address space" from
217 // "out of physical memory" in crash reports.
218 static const size_t kReasonableSizeOfUnusedPages = 1024 * 1024 * 1024;  // 1GiB
219 
220 #if DCHECK_IS_ON()
221 // These two byte values match tcmalloc.
222 static const unsigned char kUninitializedByte = 0xAB;
223 static const unsigned char kFreedByte = 0xCD;
224 static const size_t kCookieSize =
225     16;  // Handles alignment up to XMM instructions on Intel.
226 static const unsigned char kCookieValue[kCookieSize] = {
227     0xDE, 0xAD, 0xBE, 0xEF, 0xCA, 0xFE, 0xD0, 0x0D,
228     0x13, 0x37, 0xF0, 0x05, 0xBA, 0x11, 0xAB, 0x1E};
229 #endif
230 
231 struct PartitionBucket;
232 struct PartitionRootBase;
233 
234 struct PartitionFreelistEntry {
235   PartitionFreelistEntry* next;
236 };
237 
238 // Some notes on page states. A page can be in one of four major states:
239 // 1) Active.
240 // 2) Full.
241 // 3) Empty.
242 // 4) Decommitted.
243 // An active page has available free slots. A full page has no free slots. An
244 // empty page has no free slots, and a decommitted page is an empty page that
245 // had its backing memory released back to the system.
246 // There are two linked lists tracking the pages. The "active page" list is an
247 // approximation of a list of active pages. It is an approximation because
248 // full, empty and decommitted pages may briefly be present in the list until
249 // we next do a scan over it.
250 // The "empty page" list is an accurate list of pages which are either empty
251 // or decommitted.
252 //
253 // The significant page transitions are:
254 // - free() will detect when a full page has a slot free()'d and immediately
255 // return the page to the head of the active list.
256 // - free() will detect when a page is fully emptied. It _may_ add it to the
257 // empty list or it _may_ leave it on the active list until a future list scan.
258 // - malloc() _may_ scan the active page list in order to fulfil the request.
259 // If it does this, full, empty and decommitted pages encountered will be
260 // booted out of the active list. If there are no suitable active pages found,
261 // an empty or decommitted page (if one exists) will be pulled from the empty
262 // list on to the active list.
263 struct PartitionPage {
264   PartitionFreelistEntry* freelist_head;
265   PartitionPage* next_page;
266   PartitionBucket* bucket;
267   // Deliberately signed, 0 for empty or decommitted page, -n for full pages:
268   int16_t num_allocated_slots;
269   uint16_t num_unprovisioned_slots;
270   uint16_t page_offset;
271   int16_t empty_cache_index;  // -1 if not in the empty cache.
272 };
273 
274 struct PartitionBucket {
275   PartitionPage* active_pages_head;  // Accessed most in hot path => goes first.
276   PartitionPage* empty_pages_head;
277   PartitionPage* decommitted_pages_head;
278   uint32_t slot_size;
279   unsigned num_system_pages_per_slot_span : 8;
280   unsigned num_full_pages : 24;
281 };
282 
283 // An "extent" is a span of consecutive superpages. We link to the partition's
284 // next extent (if there is one) at the very start of a superpage's metadata
285 // area.
286 struct PartitionSuperPageExtentEntry {
287   PartitionRootBase* root;
288   char* super_page_base;
289   char* super_pages_end;
290   PartitionSuperPageExtentEntry* next;
291 };
292 
293 struct PartitionDirectMapExtent {
294   PartitionDirectMapExtent* next_extent;
295   PartitionDirectMapExtent* prev_extent;
296   PartitionBucket* bucket;
297   size_t map_size;  // Mapped size, not including guard pages and meta-data.
298 };
299 
300 struct BASE_EXPORT PartitionRootBase {
301   size_t total_size_of_committed_pages;
302   size_t total_size_of_super_pages;
303   size_t total_size_of_direct_mapped_pages;
304   // Invariant: total_size_of_committed_pages <=
305   //                total_size_of_super_pages +
306   //                total_size_of_direct_mapped_pages.
307   unsigned num_buckets;
308   unsigned max_allocation;
309   bool initialized;
310   char* next_super_page;
311   char* next_partition_page;
312   char* next_partition_page_end;
313   PartitionSuperPageExtentEntry* current_extent;
314   PartitionSuperPageExtentEntry* first_extent;
315   PartitionDirectMapExtent* direct_map_list;
316   PartitionPage* global_empty_page_ring[kMaxFreeableSpans];
317   int16_t global_empty_page_ring_index;
318   uintptr_t inverted_self;
319 
320   static subtle::SpinLock gInitializedLock;
321   static bool gInitialized;
322   // gSeedPage is used as a sentinel to indicate that there is no page
323   // in the active page list. We can use nullptr, but in that case we need
324   // to add a null-check branch to the hot allocation path. We want to avoid
325   // that.
326   static PartitionPage gSeedPage;
327   static PartitionBucket gPagedBucket;
328   // gOomHandlingFunction is invoked when ParitionAlloc hits OutOfMemory.
329   static void (*gOomHandlingFunction)();
330 };
331 
332 // Never instantiate a PartitionRoot directly, instead use PartitionAlloc.
333 struct PartitionRoot : public PartitionRootBase {
334   // The PartitionAlloc templated class ensures the following is correct.
bucketsPartitionRoot335   ALWAYS_INLINE PartitionBucket* buckets() {
336     return reinterpret_cast<PartitionBucket*>(this + 1);
337   }
bucketsPartitionRoot338   ALWAYS_INLINE const PartitionBucket* buckets() const {
339     return reinterpret_cast<const PartitionBucket*>(this + 1);
340   }
341 };
342 
343 // Never instantiate a PartitionRootGeneric directly, instead use
344 // PartitionAllocatorGeneric.
345 struct PartitionRootGeneric : public PartitionRootBase {
346   subtle::SpinLock lock;
347   // Some pre-computed constants.
348   size_t order_index_shifts[kBitsPerSizeT + 1];
349   size_t order_sub_index_masks[kBitsPerSizeT + 1];
350   // The bucket lookup table lets us map a size_t to a bucket quickly.
351   // The trailing +1 caters for the overflow case for very large allocation
352   // sizes.  It is one flat array instead of a 2D array because in the 2D
353   // world, we'd need to index array[blah][max+1] which risks undefined
354   // behavior.
355   PartitionBucket*
356       bucket_lookups[((kBitsPerSizeT + 1) * kGenericNumBucketsPerOrder) + 1];
357   PartitionBucket buckets[kGenericNumBuckets];
358 };
359 
360 // Flags for PartitionAllocGenericFlags.
361 enum PartitionAllocFlags {
362   PartitionAllocReturnNull = 1 << 0,
363 };
364 
365 // Struct used to retrieve total memory usage of a partition. Used by
366 // PartitionStatsDumper implementation.
367 struct PartitionMemoryStats {
368   size_t total_mmapped_bytes;    // Total bytes mmaped from the system.
369   size_t total_committed_bytes;  // Total size of commmitted pages.
370   size_t total_resident_bytes;   // Total bytes provisioned by the partition.
371   size_t total_active_bytes;     // Total active bytes in the partition.
372   size_t total_decommittable_bytes;  // Total bytes that could be decommitted.
373   size_t total_discardable_bytes;    // Total bytes that could be discarded.
374 };
375 
376 // Struct used to retrieve memory statistics about a partition bucket. Used by
377 // PartitionStatsDumper implementation.
378 struct PartitionBucketMemoryStats {
379   bool is_valid;       // Used to check if the stats is valid.
380   bool is_direct_map;  // True if this is a direct mapping; size will not be
381                        // unique.
382   uint32_t bucket_slot_size;     // The size of the slot in bytes.
383   uint32_t allocated_page_size;  // Total size the partition page allocated from
384                                  // the system.
385   uint32_t active_bytes;         // Total active bytes used in the bucket.
386   uint32_t resident_bytes;       // Total bytes provisioned in the bucket.
387   uint32_t decommittable_bytes;  // Total bytes that could be decommitted.
388   uint32_t discardable_bytes;    // Total bytes that could be discarded.
389   uint32_t num_full_pages;       // Number of pages with all slots allocated.
390   uint32_t num_active_pages;     // Number of pages that have at least one
391                                  // provisioned slot.
392   uint32_t num_empty_pages;      // Number of pages that are empty
393                                  // but not decommitted.
394   uint32_t num_decommitted_pages;  // Number of pages that are empty
395                                    // and decommitted.
396 };
397 
398 // Interface that is passed to PartitionDumpStats and
399 // PartitionDumpStatsGeneric for using the memory statistics.
400 class BASE_EXPORT PartitionStatsDumper {
401  public:
402   // Called to dump total memory used by partition, once per partition.
403   virtual void PartitionDumpTotals(const char* partition_name,
404                                    const PartitionMemoryStats*) = 0;
405 
406   // Called to dump stats about buckets, for each bucket.
407   virtual void PartitionsDumpBucketStats(const char* partition_name,
408                                          const PartitionBucketMemoryStats*) = 0;
409 };
410 
411 BASE_EXPORT void PartitionAllocGlobalInit(void (*oom_handling_function)());
412 BASE_EXPORT void PartitionAllocInit(PartitionRoot*,
413                                     size_t num_buckets,
414                                     size_t max_allocation);
415 BASE_EXPORT void PartitionAllocGenericInit(PartitionRootGeneric*);
416 
417 enum PartitionPurgeFlags {
418   // Decommitting the ring list of empty pages is reasonably fast.
419   PartitionPurgeDecommitEmptyPages = 1 << 0,
420   // Discarding unused system pages is slower, because it involves walking all
421   // freelists in all active partition pages of all buckets >= system page
422   // size. It often frees a similar amount of memory to decommitting the empty
423   // pages, though.
424   PartitionPurgeDiscardUnusedSystemPages = 1 << 1,
425 };
426 
427 BASE_EXPORT void PartitionPurgeMemory(PartitionRoot*, int);
428 BASE_EXPORT void PartitionPurgeMemoryGeneric(PartitionRootGeneric*, int);
429 
430 BASE_EXPORT NOINLINE void* PartitionAllocSlowPath(PartitionRootBase*,
431                                                   int,
432                                                   size_t,
433                                                   PartitionBucket*);
434 BASE_EXPORT NOINLINE void PartitionFreeSlowPath(PartitionPage*);
435 BASE_EXPORT NOINLINE void* PartitionReallocGeneric(PartitionRootGeneric*,
436                                                    void*,
437                                                    size_t,
438                                                    const char* type_name);
439 
440 BASE_EXPORT void PartitionDumpStats(PartitionRoot*,
441                                     const char* partition_name,
442                                     bool is_light_dump,
443                                     PartitionStatsDumper*);
444 BASE_EXPORT void PartitionDumpStatsGeneric(PartitionRootGeneric*,
445                                            const char* partition_name,
446                                            bool is_light_dump,
447                                            PartitionStatsDumper*);
448 
449 class BASE_EXPORT PartitionAllocHooks {
450  public:
451   typedef void AllocationHook(void* address, size_t, const char* type_name);
452   typedef void FreeHook(void* address);
453 
SetAllocationHook(AllocationHook * hook)454   static void SetAllocationHook(AllocationHook* hook) {
455     allocation_hook_ = hook;
456   }
SetFreeHook(FreeHook * hook)457   static void SetFreeHook(FreeHook* hook) { free_hook_ = hook; }
458 
AllocationHookIfEnabled(void * address,size_t size,const char * type_name)459   static void AllocationHookIfEnabled(void* address,
460                                       size_t size,
461                                       const char* type_name) {
462     AllocationHook* hook = allocation_hook_;
463     if (UNLIKELY(hook != nullptr))
464       hook(address, size, type_name);
465   }
466 
FreeHookIfEnabled(void * address)467   static void FreeHookIfEnabled(void* address) {
468     FreeHook* hook = free_hook_;
469     if (UNLIKELY(hook != nullptr))
470       hook(address);
471   }
472 
ReallocHookIfEnabled(void * old_address,void * new_address,size_t size,const char * type_name)473   static void ReallocHookIfEnabled(void* old_address,
474                                    void* new_address,
475                                    size_t size,
476                                    const char* type_name) {
477     // Report a reallocation as a free followed by an allocation.
478     AllocationHook* allocation_hook = allocation_hook_;
479     FreeHook* free_hook = free_hook_;
480     if (UNLIKELY(allocation_hook && free_hook)) {
481       free_hook(old_address);
482       allocation_hook(new_address, size, type_name);
483     }
484   }
485 
486  private:
487   // Pointers to hook functions that PartitionAlloc will call on allocation and
488   // free if the pointers are non-null.
489   static AllocationHook* allocation_hook_;
490   static FreeHook* free_hook_;
491 };
492 
PartitionFreelistMask(PartitionFreelistEntry * ptr)493 ALWAYS_INLINE PartitionFreelistEntry* PartitionFreelistMask(
494     PartitionFreelistEntry* ptr) {
495 // We use bswap on little endian as a fast mask for two reasons:
496 // 1) If an object is freed and its vtable used where the attacker doesn't
497 // get the chance to run allocations between the free and use, the vtable
498 // dereference is likely to fault.
499 // 2) If the attacker has a linear buffer overflow and elects to try and
500 // corrupt a freelist pointer, partial pointer overwrite attacks are
501 // thwarted.
502 // For big endian, similar guarantees are arrived at with a negation.
503 #if defined(ARCH_CPU_BIG_ENDIAN)
504   uintptr_t masked = ~reinterpret_cast<uintptr_t>(ptr);
505 #else
506   uintptr_t masked = ByteSwapUintPtrT(reinterpret_cast<uintptr_t>(ptr));
507 #endif
508   return reinterpret_cast<PartitionFreelistEntry*>(masked);
509 }
510 
PartitionCookieSizeAdjustAdd(size_t size)511 ALWAYS_INLINE size_t PartitionCookieSizeAdjustAdd(size_t size) {
512 #if DCHECK_IS_ON()
513   // Add space for cookies, checking for integer overflow. TODO(palmer):
514   // Investigate the performance and code size implications of using
515   // CheckedNumeric throughout PA.
516   DCHECK(size + (2 * kCookieSize) > size);
517   size += 2 * kCookieSize;
518 #endif
519   return size;
520 }
521 
PartitionCookieSizeAdjustSubtract(size_t size)522 ALWAYS_INLINE size_t PartitionCookieSizeAdjustSubtract(size_t size) {
523 #if DCHECK_IS_ON()
524   // Remove space for cookies.
525   DCHECK(size >= 2 * kCookieSize);
526   size -= 2 * kCookieSize;
527 #endif
528   return size;
529 }
530 
PartitionCookieFreePointerAdjust(void * ptr)531 ALWAYS_INLINE void* PartitionCookieFreePointerAdjust(void* ptr) {
532 #if DCHECK_IS_ON()
533   // The value given to the application is actually just after the cookie.
534   ptr = static_cast<char*>(ptr) - kCookieSize;
535 #endif
536   return ptr;
537 }
538 
PartitionCookieWriteValue(void * ptr)539 ALWAYS_INLINE void PartitionCookieWriteValue(void* ptr) {
540 #if DCHECK_IS_ON()
541   auto* cookie_ptr = reinterpret_cast<unsigned char*>(ptr);
542   for (size_t i = 0; i < kCookieSize; ++i, ++cookie_ptr)
543     *cookie_ptr = kCookieValue[i];
544 #endif
545 }
546 
PartitionCookieCheckValue(void * ptr)547 ALWAYS_INLINE void PartitionCookieCheckValue(void* ptr) {
548 #if DCHECK_IS_ON()
549   auto* cookie_ptr = reinterpret_cast<unsigned char*>(ptr);
550   for (size_t i = 0; i < kCookieSize; ++i, ++cookie_ptr)
551     DCHECK(*cookie_ptr == kCookieValue[i]);
552 #endif
553 }
554 
PartitionSuperPageToMetadataArea(char * ptr)555 ALWAYS_INLINE char* PartitionSuperPageToMetadataArea(char* ptr) {
556   auto pointer_as_uint = reinterpret_cast<uintptr_t>(ptr);
557   DCHECK(!(pointer_as_uint & kSuperPageOffsetMask));
558   // The metadata area is exactly one system page (the guard page) into the
559   // super page.
560   return reinterpret_cast<char*>(pointer_as_uint + kSystemPageSize);
561 }
562 
PartitionPointerToPageNoAlignmentCheck(void * ptr)563 ALWAYS_INLINE PartitionPage* PartitionPointerToPageNoAlignmentCheck(void* ptr) {
564   auto pointer_as_uint = reinterpret_cast<uintptr_t>(ptr);
565   auto* super_page_ptr =
566       reinterpret_cast<char*>(pointer_as_uint & kSuperPageBaseMask);
567   uintptr_t partition_page_index =
568       (pointer_as_uint & kSuperPageOffsetMask) >> kPartitionPageShift;
569   // Index 0 is invalid because it is the metadata and guard area and
570   // the last index is invalid because it is a guard page.
571   DCHECK(partition_page_index);
572   DCHECK(partition_page_index < kNumPartitionPagesPerSuperPage - 1);
573   auto* page = reinterpret_cast<PartitionPage*>(
574       PartitionSuperPageToMetadataArea(super_page_ptr) +
575       (partition_page_index << kPageMetadataShift));
576   // Partition pages in the same slot span can share the same page object.
577   // Adjust for that.
578   size_t delta = page->page_offset << kPageMetadataShift;
579   page =
580       reinterpret_cast<PartitionPage*>(reinterpret_cast<char*>(page) - delta);
581   return page;
582 }
583 
PartitionPageToPointer(const PartitionPage * page)584 ALWAYS_INLINE void* PartitionPageToPointer(const PartitionPage* page) {
585   auto pointer_as_uint = reinterpret_cast<uintptr_t>(page);
586   uintptr_t super_page_offset = (pointer_as_uint & kSuperPageOffsetMask);
587   DCHECK(super_page_offset > kSystemPageSize);
588   DCHECK(super_page_offset < kSystemPageSize + (kNumPartitionPagesPerSuperPage *
589                                                 kPageMetadataSize));
590   uintptr_t partition_page_index =
591       (super_page_offset - kSystemPageSize) >> kPageMetadataShift;
592   // Index 0 is invalid because it is the metadata area and the last index is
593   // invalid because it is a guard page.
594   DCHECK(partition_page_index);
595   DCHECK(partition_page_index < kNumPartitionPagesPerSuperPage - 1);
596   uintptr_t super_page_base = (pointer_as_uint & kSuperPageBaseMask);
597   auto* ret = reinterpret_cast<void*>(
598       super_page_base + (partition_page_index << kPartitionPageShift));
599   return ret;
600 }
601 
PartitionPointerToPage(void * ptr)602 ALWAYS_INLINE PartitionPage* PartitionPointerToPage(void* ptr) {
603   PartitionPage* page = PartitionPointerToPageNoAlignmentCheck(ptr);
604   // Checks that the pointer is a multiple of bucket size.
605   DCHECK(!((reinterpret_cast<uintptr_t>(ptr) -
606             reinterpret_cast<uintptr_t>(PartitionPageToPointer(page))) %
607            page->bucket->slot_size));
608   return page;
609 }
610 
PartitionBucketIsDirectMapped(const PartitionBucket * bucket)611 ALWAYS_INLINE bool PartitionBucketIsDirectMapped(
612     const PartitionBucket* bucket) {
613   return !bucket->num_system_pages_per_slot_span;
614 }
615 
PartitionBucketBytes(const PartitionBucket * bucket)616 ALWAYS_INLINE size_t PartitionBucketBytes(const PartitionBucket* bucket) {
617   return bucket->num_system_pages_per_slot_span * kSystemPageSize;
618 }
619 
PartitionBucketSlots(const PartitionBucket * bucket)620 ALWAYS_INLINE uint16_t PartitionBucketSlots(const PartitionBucket* bucket) {
621   return static_cast<uint16_t>(PartitionBucketBytes(bucket) /
622                                bucket->slot_size);
623 }
624 
PartitionPageGetRawSizePtr(PartitionPage * page)625 ALWAYS_INLINE size_t* PartitionPageGetRawSizePtr(PartitionPage* page) {
626   // For single-slot buckets which span more than one partition page, we
627   // have some spare metadata space to store the raw allocation size. We
628   // can use this to report better statistics.
629   PartitionBucket* bucket = page->bucket;
630   if (bucket->slot_size <= kMaxSystemPagesPerSlotSpan * kSystemPageSize)
631     return nullptr;
632 
633   DCHECK((bucket->slot_size % kSystemPageSize) == 0);
634   DCHECK(PartitionBucketIsDirectMapped(bucket) ||
635          PartitionBucketSlots(bucket) == 1);
636   page++;
637   return reinterpret_cast<size_t*>(&page->freelist_head);
638 }
639 
PartitionPageGetRawSize(PartitionPage * page)640 ALWAYS_INLINE size_t PartitionPageGetRawSize(PartitionPage* page) {
641   size_t* raw_size_ptr = PartitionPageGetRawSizePtr(page);
642   if (UNLIKELY(raw_size_ptr != nullptr))
643     return *raw_size_ptr;
644   return 0;
645 }
646 
PartitionPageToRoot(PartitionPage * page)647 ALWAYS_INLINE PartitionRootBase* PartitionPageToRoot(PartitionPage* page) {
648   auto* extent_entry = reinterpret_cast<PartitionSuperPageExtentEntry*>(
649       reinterpret_cast<uintptr_t>(page) & kSystemPageBaseMask);
650   return extent_entry->root;
651 }
652 
PartitionPointerIsValid(void * ptr)653 ALWAYS_INLINE bool PartitionPointerIsValid(void* ptr) {
654   PartitionPage* page = PartitionPointerToPage(ptr);
655   PartitionRootBase* root = PartitionPageToRoot(page);
656   return root->inverted_self == ~reinterpret_cast<uintptr_t>(root);
657 }
658 
PartitionBucketAlloc(PartitionRootBase * root,int flags,size_t size,PartitionBucket * bucket)659 ALWAYS_INLINE void* PartitionBucketAlloc(PartitionRootBase* root,
660                                          int flags,
661                                          size_t size,
662                                          PartitionBucket* bucket) {
663   PartitionPage* page = bucket->active_pages_head;
664   // Check that this page is neither full nor freed.
665   DCHECK(page->num_allocated_slots >= 0);
666   void* ret = page->freelist_head;
667   if (LIKELY(ret)) {
668     // If these asserts fire, you probably corrupted memory.
669     DCHECK(PartitionPointerIsValid(ret));
670     // All large allocations must go through the slow path to correctly
671     // update the size metadata.
672     DCHECK(PartitionPageGetRawSize(page) == 0);
673     PartitionFreelistEntry* new_head =
674         PartitionFreelistMask(static_cast<PartitionFreelistEntry*>(ret)->next);
675     page->freelist_head = new_head;
676     page->num_allocated_slots++;
677   } else {
678     ret = PartitionAllocSlowPath(root, flags, size, bucket);
679     DCHECK(!ret || PartitionPointerIsValid(ret));
680   }
681 #if DCHECK_IS_ON()
682   if (!ret)
683     return nullptr;
684   // Fill the uninitialized pattern, and write the cookies.
685   page = PartitionPointerToPage(ret);
686   size_t slot_size = page->bucket->slot_size;
687   size_t raw_size = PartitionPageGetRawSize(page);
688   if (raw_size) {
689     DCHECK(raw_size == size);
690     slot_size = raw_size;
691   }
692   size_t no_cookie_size = PartitionCookieSizeAdjustSubtract(slot_size);
693   auto* char_ret = static_cast<char*>(ret);
694   // The value given to the application is actually just after the cookie.
695   ret = char_ret + kCookieSize;
696   memset(ret, kUninitializedByte, no_cookie_size);
697   PartitionCookieWriteValue(char_ret);
698   PartitionCookieWriteValue(char_ret + kCookieSize + no_cookie_size);
699 #endif
700   return ret;
701 }
702 
PartitionAlloc(PartitionRoot * root,size_t size,const char * type_name)703 ALWAYS_INLINE void* PartitionAlloc(PartitionRoot* root,
704                                    size_t size,
705                                    const char* type_name) {
706 #if defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
707   void* result = malloc(size);
708   CHECK(result);
709   return result;
710 #else
711   size_t requested_size = size;
712   size = PartitionCookieSizeAdjustAdd(size);
713   DCHECK(root->initialized);
714   size_t index = size >> kBucketShift;
715   DCHECK(index < root->num_buckets);
716   DCHECK(size == index << kBucketShift);
717   PartitionBucket* bucket = &root->buckets()[index];
718   void* result = PartitionBucketAlloc(root, 0, size, bucket);
719   PartitionAllocHooks::AllocationHookIfEnabled(result, requested_size,
720                                                type_name);
721   return result;
722 #endif  // defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
723 }
724 
PartitionFreeWithPage(void * ptr,PartitionPage * page)725 ALWAYS_INLINE void PartitionFreeWithPage(void* ptr, PartitionPage* page) {
726 // If these asserts fire, you probably corrupted memory.
727 #if DCHECK_IS_ON()
728   size_t slot_size = page->bucket->slot_size;
729   size_t raw_size = PartitionPageGetRawSize(page);
730   if (raw_size)
731     slot_size = raw_size;
732   PartitionCookieCheckValue(ptr);
733   PartitionCookieCheckValue(reinterpret_cast<char*>(ptr) + slot_size -
734                             kCookieSize);
735   memset(ptr, kFreedByte, slot_size);
736 #endif
737   DCHECK(page->num_allocated_slots);
738   PartitionFreelistEntry* freelist_head = page->freelist_head;
739   DCHECK(!freelist_head || PartitionPointerIsValid(freelist_head));
740   CHECK(ptr != freelist_head);  // Catches an immediate double free.
741   // Look for double free one level deeper in debug.
742   DCHECK(!freelist_head || ptr != PartitionFreelistMask(freelist_head->next));
743   auto* entry = static_cast<PartitionFreelistEntry*>(ptr);
744   entry->next = PartitionFreelistMask(freelist_head);
745   page->freelist_head = entry;
746   --page->num_allocated_slots;
747   if (UNLIKELY(page->num_allocated_slots <= 0)) {
748     PartitionFreeSlowPath(page);
749   } else {
750     // All single-slot allocations must go through the slow path to
751     // correctly update the size metadata.
752     DCHECK(PartitionPageGetRawSize(page) == 0);
753   }
754 }
755 
PartitionFree(void * ptr)756 ALWAYS_INLINE void PartitionFree(void* ptr) {
757 #if defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
758   free(ptr);
759 #else
760   PartitionAllocHooks::FreeHookIfEnabled(ptr);
761   ptr = PartitionCookieFreePointerAdjust(ptr);
762   DCHECK(PartitionPointerIsValid(ptr));
763   PartitionPage* page = PartitionPointerToPage(ptr);
764   PartitionFreeWithPage(ptr, page);
765 #endif
766 }
767 
PartitionGenericSizeToBucket(PartitionRootGeneric * root,size_t size)768 ALWAYS_INLINE PartitionBucket* PartitionGenericSizeToBucket(
769     PartitionRootGeneric* root,
770     size_t size) {
771   size_t order = kBitsPerSizeT - bits::CountLeadingZeroBitsSizeT(size);
772   // The order index is simply the next few bits after the most significant bit.
773   size_t order_index = (size >> root->order_index_shifts[order]) &
774                        (kGenericNumBucketsPerOrder - 1);
775   // And if the remaining bits are non-zero we must bump the bucket up.
776   size_t sub_order_index = size & root->order_sub_index_masks[order];
777   PartitionBucket* bucket =
778       root->bucket_lookups[(order << kGenericNumBucketsPerOrderBits) +
779                            order_index + !!sub_order_index];
780   DCHECK(!bucket->slot_size || bucket->slot_size >= size);
781   DCHECK(!(bucket->slot_size % kGenericSmallestBucket));
782   return bucket;
783 }
784 
PartitionAllocGenericFlags(PartitionRootGeneric * root,int flags,size_t size,const char * type_name)785 ALWAYS_INLINE void* PartitionAllocGenericFlags(PartitionRootGeneric* root,
786                                                int flags,
787                                                size_t size,
788                                                const char* type_name) {
789 #if defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
790   void* result = malloc(size);
791   CHECK(result || flags & PartitionAllocReturnNull);
792   return result;
793 #else
794   DCHECK(root->initialized);
795   size_t requested_size = size;
796   size = PartitionCookieSizeAdjustAdd(size);
797   PartitionBucket* bucket = PartitionGenericSizeToBucket(root, size);
798   void* ret = nullptr;
799   {
800     subtle::SpinLock::Guard guard(root->lock);
801     ret = PartitionBucketAlloc(root, flags, size, bucket);
802   }
803   PartitionAllocHooks::AllocationHookIfEnabled(ret, requested_size, type_name);
804   return ret;
805 #endif
806 }
807 
PartitionAllocGeneric(PartitionRootGeneric * root,size_t size,const char * type_name)808 ALWAYS_INLINE void* PartitionAllocGeneric(PartitionRootGeneric* root,
809                                           size_t size,
810                                           const char* type_name) {
811   return PartitionAllocGenericFlags(root, 0, size, type_name);
812 }
813 
PartitionFreeGeneric(PartitionRootGeneric * root,void * ptr)814 ALWAYS_INLINE void PartitionFreeGeneric(PartitionRootGeneric* root, void* ptr) {
815 #if defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
816   free(ptr);
817 #else
818   DCHECK(root->initialized);
819 
820   if (UNLIKELY(!ptr))
821     return;
822 
823   PartitionAllocHooks::FreeHookIfEnabled(ptr);
824   ptr = PartitionCookieFreePointerAdjust(ptr);
825   DCHECK(PartitionPointerIsValid(ptr));
826   PartitionPage* page = PartitionPointerToPage(ptr);
827   {
828     subtle::SpinLock::Guard guard(root->lock);
829     PartitionFreeWithPage(ptr, page);
830   }
831 #endif
832 }
833 
PartitionDirectMapSize(size_t size)834 ALWAYS_INLINE size_t PartitionDirectMapSize(size_t size) {
835   // Caller must check that the size is not above the kGenericMaxDirectMapped
836   // limit before calling. This also guards against integer overflow in the
837   // calculation here.
838   DCHECK(size <= kGenericMaxDirectMapped);
839   return (size + kSystemPageOffsetMask) & kSystemPageBaseMask;
840 }
841 
PartitionAllocActualSize(PartitionRootGeneric * root,size_t size)842 ALWAYS_INLINE size_t PartitionAllocActualSize(PartitionRootGeneric* root,
843                                               size_t size) {
844 #if defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
845   return size;
846 #else
847   DCHECK(root->initialized);
848   size = PartitionCookieSizeAdjustAdd(size);
849   PartitionBucket* bucket = PartitionGenericSizeToBucket(root, size);
850   if (LIKELY(!PartitionBucketIsDirectMapped(bucket))) {
851     size = bucket->slot_size;
852   } else if (size > kGenericMaxDirectMapped) {
853     // Too large to allocate => return the size unchanged.
854   } else {
855     DCHECK(bucket == &PartitionRootBase::gPagedBucket);
856     size = PartitionDirectMapSize(size);
857   }
858   return PartitionCookieSizeAdjustSubtract(size);
859 #endif
860 }
861 
PartitionAllocSupportsGetSize()862 ALWAYS_INLINE bool PartitionAllocSupportsGetSize() {
863 #if defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
864   return false;
865 #else
866   return true;
867 #endif
868 }
869 
PartitionAllocGetSize(void * ptr)870 ALWAYS_INLINE size_t PartitionAllocGetSize(void* ptr) {
871   // No need to lock here. Only |ptr| being freed by another thread could
872   // cause trouble, and the caller is responsible for that not happening.
873   DCHECK(PartitionAllocSupportsGetSize());
874   ptr = PartitionCookieFreePointerAdjust(ptr);
875   DCHECK(PartitionPointerIsValid(ptr));
876   PartitionPage* page = PartitionPointerToPage(ptr);
877   size_t size = page->bucket->slot_size;
878   return PartitionCookieSizeAdjustSubtract(size);
879 }
880 
881 // N (or more accurately, N - sizeof(void*)) represents the largest size in
882 // bytes that will be handled by a SizeSpecificPartitionAllocator.
883 // Attempts to partitionAlloc() more than this amount will fail.
884 template <size_t N>
885 class SizeSpecificPartitionAllocator {
886  public:
887   static const size_t kMaxAllocation = N - kAllocationGranularity;
888   static const size_t kNumBuckets = N / kAllocationGranularity;
init()889   void init() {
890     PartitionAllocInit(&partition_root_, kNumBuckets, kMaxAllocation);
891   }
root()892   ALWAYS_INLINE PartitionRoot* root() { return &partition_root_; }
893 
894  private:
895   PartitionRoot partition_root_;
896   PartitionBucket actual_buckets_[kNumBuckets];
897 };
898 
899 class PartitionAllocatorGeneric {
900  public:
init()901   void init() { PartitionAllocGenericInit(&partition_root_); }
root()902   ALWAYS_INLINE PartitionRootGeneric* root() { return &partition_root_; }
903 
904  private:
905   PartitionRootGeneric partition_root_;
906 };
907 
908 }  // namespace base
909 }  // namespace pdfium
910 
911 #endif  // BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_ALLOC_H
912