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 <async_safe/log.h>
32
33 #include <bits/stdatomic.h>
34 #include <pthread.h>
35 #include <stddef.h>
36 #include <stdint.h>
37 #include <string.h>
38 #include <sys/mman.h>
39 #include <sys/prctl.h>
40
41 #include "private/bionic_defs.h"
42 #include "private/bionic_globals.h"
43 #include "private/ScopedPthreadMutexLocker.h"
44
45 struct abort_msg_t {
46 size_t size;
47 char msg[0];
48 };
49 static_assert(
50 offsetof(abort_msg_t, msg) == sizeof(size_t),
51 "The in-memory layout of abort_msg_t is not consistent with what libdebuggerd expects.");
52
53 struct magic_abort_msg_t {
54 uint64_t magic1;
55 uint64_t magic2;
56 abort_msg_t msg;
57 };
58 static_assert(offsetof(magic_abort_msg_t, msg) == 2 * sizeof(uint64_t),
59 "The in-memory layout of magic_abort_msg_t is not consistent with what automated "
60 "tools expect.");
61
62 [[clang::optnone]]
fill_abort_message_magic(magic_abort_msg_t * new_magic_abort_message)63 static void fill_abort_message_magic(magic_abort_msg_t* new_magic_abort_message) {
64 // 128-bit magic for the abort message. Chosen by fair dice roll.
65 // This function is intentionally deoptimized to avoid the magic to be present
66 // in the final binary. This causes clang to only use instructions where parts
67 // of the magic are encoded into immediate arguments for the instructions in
68 // all supported architectures.
69 new_magic_abort_message->magic1 = 0xb18e40886ac388f0ULL;
70 new_magic_abort_message->magic2 = 0xc6dfba755a1de0b5ULL;
71 }
72
73 __BIONIC_WEAK_FOR_NATIVE_BRIDGE
android_set_abort_message(const char * msg)74 void android_set_abort_message(const char* msg) {
75 ScopedPthreadMutexLocker locker(&__libc_shared_globals()->abort_msg_lock);
76
77 if (__libc_shared_globals()->abort_msg != nullptr) {
78 // We already have an abort message.
79 // Assume that the first crash is the one most worth reporting.
80 return;
81 }
82
83 if (msg == nullptr) {
84 msg = "(null)";
85 }
86
87 size_t size = sizeof(magic_abort_msg_t) + strlen(msg) + 1;
88 void* map = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
89 if (map == MAP_FAILED) {
90 return;
91 }
92
93 // Name the abort message mapping to make it easier for tools to find the
94 // mapping.
95 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map, size, "abort message");
96
97 magic_abort_msg_t* new_magic_abort_message = reinterpret_cast<magic_abort_msg_t*>(map);
98 fill_abort_message_magic(new_magic_abort_message);
99 new_magic_abort_message->msg.size = size;
100 strcpy(new_magic_abort_message->msg.msg, msg);
101 __libc_shared_globals()->abort_msg = &new_magic_abort_message->msg;
102 }
103