Josh Gao | 10ec928 | 2017-04-03 15:13:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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/set_abort_message.h> |
| 30 | |
| 31 | #include <pthread.h> |
Luis Hector Chavez | 6d93c60 | 2018-08-22 07:39:26 -0700 | [diff] [blame] | 32 | #include <stdint.h> |
| 33 | #include <stddef.h> |
Dan Albert | 1c78cb0 | 2017-10-11 11:25:25 -0700 | [diff] [blame] | 34 | #include <string.h> |
Josh Gao | 10ec928 | 2017-04-03 15:13:29 -0700 | [diff] [blame] | 35 | #include <sys/mman.h> |
| 36 | |
dimitry | b6d2b87 | 2017-10-18 15:15:54 +0200 | [diff] [blame] | 37 | #include "private/bionic_defs.h" |
Ryan Prichard | 7752bcb | 2018-11-22 02:41:04 -0800 | [diff] [blame^] | 38 | #include "private/bionic_globals.h" |
Josh Gao | 10ec928 | 2017-04-03 15:13:29 -0700 | [diff] [blame] | 39 | #include "private/ScopedPthreadMutexLocker.h" |
| 40 | |
Josh Gao | 10ec928 | 2017-04-03 15:13:29 -0700 | [diff] [blame] | 41 | struct abort_msg_t { |
| 42 | size_t size; |
| 43 | char msg[0]; |
| 44 | }; |
Luis Hector Chavez | 6d93c60 | 2018-08-22 07:39:26 -0700 | [diff] [blame] | 45 | static_assert( |
| 46 | offsetof(abort_msg_t, msg) == sizeof(size_t), |
| 47 | "The in-memory layout of abort_msg_t is not consistent with what libdebuggerd expects."); |
| 48 | |
| 49 | struct magic_abort_msg_t { |
| 50 | uint64_t magic1; |
| 51 | uint64_t magic2; |
| 52 | abort_msg_t msg; |
| 53 | }; |
| 54 | static_assert(offsetof(magic_abort_msg_t, msg) == 2 * sizeof(uint64_t), |
| 55 | "The in-memory layout of magic_abort_msg_t is not consistent with what automated " |
| 56 | "tools expect."); |
Josh Gao | 10ec928 | 2017-04-03 15:13:29 -0700 | [diff] [blame] | 57 | |
Luis Hector Chavez | 6d93c60 | 2018-08-22 07:39:26 -0700 | [diff] [blame] | 58 | [[clang::optnone]] |
| 59 | static void fill_abort_message_magic(magic_abort_msg_t* new_magic_abort_message) { |
| 60 | // 128-bit magic for the abort message. Chosen by fair dice roll. |
| 61 | // This function is intentionally deoptimized to avoid the magic to be present |
| 62 | // in the final binary. This causes clang to only use instructions where parts |
| 63 | // of the magic are encoded into immediate arguments for the instructions in |
| 64 | // all supported architectures. |
| 65 | new_magic_abort_message->magic1 = 0xb18e40886ac388f0ULL; |
| 66 | new_magic_abort_message->magic2 = 0xc6dfba755a1de0b5ULL; |
| 67 | } |
| 68 | |
dimitry | b6d2b87 | 2017-10-18 15:15:54 +0200 | [diff] [blame] | 69 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE |
Josh Gao | 10ec928 | 2017-04-03 15:13:29 -0700 | [diff] [blame] | 70 | void android_set_abort_message(const char* msg) { |
Ryan Prichard | 7752bcb | 2018-11-22 02:41:04 -0800 | [diff] [blame^] | 71 | ScopedPthreadMutexLocker locker(&__libc_shared_globals()->abort_msg_lock); |
Josh Gao | 10ec928 | 2017-04-03 15:13:29 -0700 | [diff] [blame] | 72 | |
Ryan Prichard | 7752bcb | 2018-11-22 02:41:04 -0800 | [diff] [blame^] | 73 | if (__libc_shared_globals()->abort_msg != nullptr) { |
Josh Gao | 10ec928 | 2017-04-03 15:13:29 -0700 | [diff] [blame] | 74 | // We already have an abort message. |
| 75 | // Assume that the first crash is the one most worth reporting. |
| 76 | return; |
| 77 | } |
| 78 | |
Luis Hector Chavez | 6d93c60 | 2018-08-22 07:39:26 -0700 | [diff] [blame] | 79 | size_t size = sizeof(magic_abort_msg_t) + strlen(msg) + 1; |
Josh Gao | 10ec928 | 2017-04-03 15:13:29 -0700 | [diff] [blame] | 80 | void* map = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); |
| 81 | if (map == MAP_FAILED) { |
| 82 | return; |
| 83 | } |
| 84 | |
Luis Hector Chavez | 6d93c60 | 2018-08-22 07:39:26 -0700 | [diff] [blame] | 85 | magic_abort_msg_t* new_magic_abort_message = reinterpret_cast<magic_abort_msg_t*>(map); |
| 86 | fill_abort_message_magic(new_magic_abort_message); |
| 87 | new_magic_abort_message->msg.size = size; |
| 88 | strcpy(new_magic_abort_message->msg.msg, msg); |
Ryan Prichard | 7752bcb | 2018-11-22 02:41:04 -0800 | [diff] [blame^] | 89 | __libc_shared_globals()->abort_msg = &new_magic_abort_message->msg; |
Josh Gao | 10ec928 | 2017-04-03 15:13:29 -0700 | [diff] [blame] | 90 | } |