Florian Mayer | ca4749a | 2024-02-14 12:55:46 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2024 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 <android/crash_detail.h> |
| 30 | |
| 31 | #include <async_safe/log.h> |
| 32 | #include <bionic/crash_detail_internal.h> |
| 33 | |
| 34 | #include <bits/stdatomic.h> |
| 35 | #include <pthread.h> |
| 36 | #include <stddef.h> |
| 37 | #include <stdint.h> |
| 38 | #include <string.h> |
| 39 | #include <sys/mman.h> |
| 40 | #include <sys/prctl.h> |
| 41 | |
| 42 | #include "private/ScopedPthreadMutexLocker.h" |
| 43 | #include "private/bionic_defs.h" |
| 44 | #include "private/bionic_globals.h" |
| 45 | |
| 46 | static _Atomic(crash_detail_t*) free_head = nullptr; |
| 47 | |
| 48 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE |
| 49 | crash_detail_t* android_crash_detail_register(const void* name, size_t name_size, const void* data, |
| 50 | size_t data_size) { |
| 51 | auto populate_crash_detail = [&](crash_detail_t* result) { |
| 52 | result->name = reinterpret_cast<const char*>(name); |
| 53 | result->name_size = name_size; |
| 54 | result->data = reinterpret_cast<const char*>(data); |
| 55 | result->data_size = data_size; |
| 56 | }; |
| 57 | // This is a atomic fast-path for RAII use-cases where the app keeps creating and deleting |
| 58 | // crash details for short periods of time to capture detailed scopes. |
| 59 | if (crash_detail_t* head = atomic_load(&free_head)) { |
| 60 | while (head != nullptr && !atomic_compare_exchange_strong(&free_head, &head, head->prev_free)) { |
| 61 | // intentionally left blank. |
| 62 | } |
| 63 | if (head) { |
| 64 | head->prev_free = nullptr; |
| 65 | populate_crash_detail(head); |
| 66 | return head; |
| 67 | } |
| 68 | } |
| 69 | ScopedPthreadMutexLocker locker(&__libc_shared_globals()->crash_detail_page_lock); |
| 70 | struct crash_detail_page_t* prev = nullptr; |
| 71 | struct crash_detail_page_t* page = __libc_shared_globals()->crash_detail_page; |
| 72 | if (page != nullptr && page->used == kNumCrashDetails) { |
| 73 | prev = page; |
| 74 | page = nullptr; |
| 75 | } |
| 76 | if (page == nullptr) { |
| 77 | size_t size = sizeof(crash_detail_page_t); |
| 78 | void* map = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); |
| 79 | if (map == MAP_FAILED) { |
| 80 | async_safe_format_log(ANDROID_LOG_ERROR, "libc", "failed to allocate crash_detail_page: %m"); |
| 81 | return nullptr; |
| 82 | } |
| 83 | prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map, size, "crash details"); |
| 84 | page = reinterpret_cast<struct crash_detail_page_t*>(map); |
| 85 | page->prev = prev; |
| 86 | __libc_shared_globals()->crash_detail_page = page; |
| 87 | } |
| 88 | crash_detail_t* result = &page->crash_details[page->used]; |
| 89 | populate_crash_detail(result); |
| 90 | page->used++; |
| 91 | return result; |
| 92 | } |
| 93 | |
| 94 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE |
| 95 | void android_crash_detail_unregister(crash_detail_t* crash_detail) { |
| 96 | if (crash_detail) { |
| 97 | if (crash_detail->prev_free) { |
| 98 | // removing already removed would mess up the free-list by creating a circle. |
| 99 | return; |
| 100 | } |
| 101 | crash_detail->data = nullptr; |
| 102 | crash_detail->name = nullptr; |
| 103 | crash_detail_t* prev = atomic_load(&free_head); |
| 104 | do { |
| 105 | crash_detail->prev_free = prev; |
| 106 | } while (!atomic_compare_exchange_strong(&free_head, &prev, crash_detail)); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE |
| 111 | void android_crash_detail_replace_data(crash_detail_t* crash_detail, const void* data, |
| 112 | size_t data_size) { |
| 113 | crash_detail->data = reinterpret_cast<const char*>(data); |
| 114 | crash_detail->data_size = data_size; |
| 115 | } |
| 116 | |
| 117 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE |
| 118 | void android_crash_detail_replace_name(crash_detail_t* crash_detail, const void* name, |
| 119 | size_t name_size) { |
| 120 | crash_detail->name = reinterpret_cast<const char*>(name); |
| 121 | crash_detail->name_size = name_size; |
| 122 | } |