blob: ce29997eb97a8ee5cdfdf69e3e6fbc04a2c7c849 [file] [log] [blame]
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
Dimitry Ivanovbcc4da92017-02-15 15:31:13 -08003 * All rights reserved.
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -07004 *
Dimitry Ivanovbcc4da92017-02-15 15:31:13 -08005 * 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.
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070014 *
Dimitry Ivanovbcc4da92017-02-15 15:31:13 -080015 * 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.
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070027 */
28
Ryan Prichard083d8502019-01-24 13:47:13 -080029#include "private/bionic_allocator.h"
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070030
31#include <stdlib.h>
Josh Gao9ccccc12017-02-09 10:54:44 -080032#include <sys/cdefs.h>
33#include <unistd.h>
34
Josh Gao72282ad2018-01-24 14:45:58 -080035#include <atomic>
36
Christopher Ferris7a3681e2017-04-24 17:48:32 -070037#include <async_safe/log.h>
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070038
Ryan Prichard083d8502019-01-24 13:47:13 -080039static BionicAllocator g_bionic_allocator;
Josh Gao72282ad2018-01-24 14:45:58 -080040static std::atomic<pid_t> fallback_tid(0);
Josh Gao9ccccc12017-02-09 10:54:44 -080041
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.
Josh Gao72282ad2018-01-24 14:45:58 -080044extern "C" bool __linker_enable_fallback_allocator() {
45 pid_t expected = 0;
46 return fallback_tid.compare_exchange_strong(expected, gettid());
Josh Gao9ccccc12017-02-09 10:54:44 -080047}
48
Josh Gao222272e2017-03-06 17:46:47 -080049extern "C" void __linker_disable_fallback_allocator() {
Josh Gao72282ad2018-01-24 14:45:58 -080050 pid_t previous = fallback_tid.exchange(0);
51 if (previous == 0) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -070052 async_safe_fatal("attempted to disable unused fallback allocator");
Josh Gao72282ad2018-01-24 14:45:58 -080053 } else if (previous != gettid()) {
54 async_safe_fatal("attempted to disable fallback allocator in use by another thread (%d)",
55 previous);
Josh Gao222272e2017-03-06 17:46:47 -080056 }
Josh Gao222272e2017-03-06 17:46:47 -080057}
58
Ryan Prichard083d8502019-01-24 13:47:13 -080059static BionicAllocator& get_fallback_allocator() {
60 static BionicAllocator fallback_allocator;
Josh Gao9ccccc12017-02-09 10:54:44 -080061 return fallback_allocator;
62}
63
Ryan Prichard083d8502019-01-24 13:47:13 -080064static BionicAllocator& get_allocator() {
Josh Gao9ccccc12017-02-09 10:54:44 -080065 if (__predict_false(fallback_tid) && __predict_false(gettid() == fallback_tid)) {
66 return get_fallback_allocator();
67 }
Ryan Prichard083d8502019-01-24 13:47:13 -080068 return g_bionic_allocator;
Josh Gao9ccccc12017-02-09 10:54:44 -080069}
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070070
71void* malloc(size_t byte_count) {
Josh Gao9ccccc12017-02-09 10:54:44 -080072 return get_allocator().alloc(byte_count);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070073}
74
75void* calloc(size_t item_count, size_t item_size) {
Josh Gao9ccccc12017-02-09 10:54:44 -080076 return get_allocator().alloc(item_count*item_size);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070077}
78
79void* realloc(void* p, size_t byte_count) {
Josh Gao9ccccc12017-02-09 10:54:44 -080080 return get_allocator().realloc(p, byte_count);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070081}
82
Elliott Hughesb1770852018-09-18 12:52:42 -070083void* 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
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070092void free(void* ptr) {
Josh Gao9ccccc12017-02-09 10:54:44 -080093 get_allocator().free(ptr);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070094}