• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include "private/bionic_allocator.h"
30 
31 #include <stdlib.h>
32 #include <sys/cdefs.h>
33 #include <unistd.h>
34 
35 #include <atomic>
36 
37 #include <async_safe/log.h>
38 
39 static BionicAllocator g_bionic_allocator;
40 static std::atomic<pid_t> fallback_tid(0);
41 
42 // Used by libdebuggerd_handler to switch allocators during a crash dump, in
43 // case the linker heap is corrupted. Do not use this function.
__linker_enable_fallback_allocator()44 extern "C" bool __linker_enable_fallback_allocator() {
45   pid_t expected = 0;
46   return fallback_tid.compare_exchange_strong(expected, gettid());
47 }
48 
__linker_disable_fallback_allocator()49 extern "C" void __linker_disable_fallback_allocator() {
50   pid_t previous = fallback_tid.exchange(0);
51   if (previous == 0) {
52     async_safe_fatal("attempted to disable unused fallback allocator");
53   } else if (previous != gettid()) {
54     async_safe_fatal("attempted to disable fallback allocator in use by another thread (%d)",
55                      previous);
56   }
57 }
58 
get_fallback_allocator()59 static BionicAllocator& get_fallback_allocator() {
60   static BionicAllocator fallback_allocator;
61   return fallback_allocator;
62 }
63 
get_allocator()64 static BionicAllocator& get_allocator() {
65   if (__predict_false(fallback_tid) && __predict_false(gettid() == fallback_tid)) {
66     return get_fallback_allocator();
67   }
68   return g_bionic_allocator;
69 }
70 
malloc(size_t byte_count)71 void* malloc(size_t byte_count) {
72   return get_allocator().alloc(byte_count);
73 }
74 
calloc(size_t item_count,size_t item_size)75 void* calloc(size_t item_count, size_t item_size) {
76   return get_allocator().alloc(item_count*item_size);
77 }
78 
realloc(void * p,size_t byte_count)79 void* realloc(void* p, size_t byte_count) {
80   return get_allocator().realloc(p, byte_count);
81 }
82 
reallocarray(void * p,size_t item_count,size_t item_size)83 void* reallocarray(void* p, size_t item_count, size_t item_size) {
84   size_t byte_count;
85   if (__builtin_mul_overflow(item_count, item_size, &byte_count)) {
86     errno = ENOMEM;
87     return nullptr;
88   }
89   return get_allocator().realloc(p, byte_count);
90 }
91 
free(void * ptr)92 void free(void* ptr) {
93   get_allocator().free(ptr);
94 }
95