Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
Dimitry Ivanov | bcc4da9 | 2017-02-15 15:31:13 -0800 | [diff] [blame] | 3 | * All rights reserved. |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 4 | * |
Dimitry Ivanov | bcc4da9 | 2017-02-15 15:31:13 -0800 | [diff] [blame] | 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. |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 14 | * |
Dimitry Ivanov | bcc4da9 | 2017-02-15 15:31:13 -0800 | [diff] [blame] | 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. |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 27 | */ |
| 28 | |
Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 29 | #include "private/bionic_allocator.h" |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 30 | |
| 31 | #include <stdlib.h> |
Josh Gao | 9ccccc1 | 2017-02-09 10:54:44 -0800 | [diff] [blame] | 32 | #include <sys/cdefs.h> |
| 33 | #include <unistd.h> |
| 34 | |
Josh Gao | 72282ad | 2018-01-24 14:45:58 -0800 | [diff] [blame] | 35 | #include <atomic> |
| 36 | |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 37 | #include <async_safe/log.h> |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 38 | |
Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 39 | static BionicAllocator g_bionic_allocator; |
Josh Gao | 72282ad | 2018-01-24 14:45:58 -0800 | [diff] [blame] | 40 | static std::atomic<pid_t> fallback_tid(0); |
Josh Gao | 9ccccc1 | 2017-02-09 10:54:44 -0800 | [diff] [blame] | 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. |
Josh Gao | 72282ad | 2018-01-24 14:45:58 -0800 | [diff] [blame] | 44 | extern "C" bool __linker_enable_fallback_allocator() { |
| 45 | pid_t expected = 0; |
| 46 | return fallback_tid.compare_exchange_strong(expected, gettid()); |
Josh Gao | 9ccccc1 | 2017-02-09 10:54:44 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Josh Gao | 222272e | 2017-03-06 17:46:47 -0800 | [diff] [blame] | 49 | extern "C" void __linker_disable_fallback_allocator() { |
Josh Gao | 72282ad | 2018-01-24 14:45:58 -0800 | [diff] [blame] | 50 | pid_t previous = fallback_tid.exchange(0); |
| 51 | if (previous == 0) { |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 52 | async_safe_fatal("attempted to disable unused fallback allocator"); |
Josh Gao | 72282ad | 2018-01-24 14:45:58 -0800 | [diff] [blame] | 53 | } else if (previous != gettid()) { |
| 54 | async_safe_fatal("attempted to disable fallback allocator in use by another thread (%d)", |
| 55 | previous); |
Josh Gao | 222272e | 2017-03-06 17:46:47 -0800 | [diff] [blame] | 56 | } |
Josh Gao | 222272e | 2017-03-06 17:46:47 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 59 | static BionicAllocator& get_fallback_allocator() { |
| 60 | static BionicAllocator fallback_allocator; |
Josh Gao | 9ccccc1 | 2017-02-09 10:54:44 -0800 | [diff] [blame] | 61 | return fallback_allocator; |
| 62 | } |
| 63 | |
Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 64 | static BionicAllocator& get_allocator() { |
Josh Gao | 9ccccc1 | 2017-02-09 10:54:44 -0800 | [diff] [blame] | 65 | if (__predict_false(fallback_tid) && __predict_false(gettid() == fallback_tid)) { |
| 66 | return get_fallback_allocator(); |
| 67 | } |
Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 68 | return g_bionic_allocator; |
Josh Gao | 9ccccc1 | 2017-02-09 10:54:44 -0800 | [diff] [blame] | 69 | } |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 70 | |
| 71 | void* malloc(size_t byte_count) { |
Josh Gao | 9ccccc1 | 2017-02-09 10:54:44 -0800 | [diff] [blame] | 72 | return get_allocator().alloc(byte_count); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Matthew Maurer | 2411a5e | 2021-06-24 16:12:45 -0700 | [diff] [blame] | 75 | void* memalign(size_t alignment, size_t byte_count) { |
| 76 | return get_allocator().memalign(alignment, byte_count); |
| 77 | } |
| 78 | |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 79 | void* calloc(size_t item_count, size_t item_size) { |
Josh Gao | 9ccccc1 | 2017-02-09 10:54:44 -0800 | [diff] [blame] | 80 | return get_allocator().alloc(item_count*item_size); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void* realloc(void* p, size_t byte_count) { |
Josh Gao | 9ccccc1 | 2017-02-09 10:54:44 -0800 | [diff] [blame] | 84 | return get_allocator().realloc(p, byte_count); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Elliott Hughes | b177085 | 2018-09-18 12:52:42 -0700 | [diff] [blame] | 87 | void* reallocarray(void* p, size_t item_count, size_t item_size) { |
| 88 | size_t byte_count; |
| 89 | if (__builtin_mul_overflow(item_count, item_size, &byte_count)) { |
| 90 | errno = ENOMEM; |
| 91 | return nullptr; |
| 92 | } |
| 93 | return get_allocator().realloc(p, byte_count); |
| 94 | } |
| 95 | |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 96 | void free(void* ptr) { |
Josh Gao | 9ccccc1 | 2017-02-09 10:54:44 -0800 | [diff] [blame] | 97 | get_allocator().free(ptr); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 98 | } |