blob: 58f24cfa8ca66094378a22276064bc5a6068cd7a [file] [log] [blame]
Josh Gao10ec9282017-04-03 15:13:29 -07001/*
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 Chavez6d93c602018-08-22 07:39:26 -070032#include <stdint.h>
33#include <stddef.h>
Dan Albert1c78cb02017-10-11 11:25:25 -070034#include <string.h>
Josh Gao10ec9282017-04-03 15:13:29 -070035#include <sys/mman.h>
36
dimitryb6d2b872017-10-18 15:15:54 +020037#include "private/bionic_defs.h"
Josh Gao10ec9282017-04-03 15:13:29 -070038#include "private/ScopedPthreadMutexLocker.h"
39
40static pthread_mutex_t g_abort_msg_lock = PTHREAD_MUTEX_INITIALIZER;
41
42struct abort_msg_t {
43 size_t size;
44 char msg[0];
45};
Luis Hector Chavez6d93c602018-08-22 07:39:26 -070046static_assert(
47 offsetof(abort_msg_t, msg) == sizeof(size_t),
48 "The in-memory layout of abort_msg_t is not consistent with what libdebuggerd expects.");
49
50struct magic_abort_msg_t {
51 uint64_t magic1;
52 uint64_t magic2;
53 abort_msg_t msg;
54};
55static_assert(offsetof(magic_abort_msg_t, msg) == 2 * sizeof(uint64_t),
56 "The in-memory layout of magic_abort_msg_t is not consistent with what automated "
57 "tools expect.");
Josh Gao10ec9282017-04-03 15:13:29 -070058
59abort_msg_t** __abort_message_ptr; // Accessible to __libc_init_common.
60
Luis Hector Chavez6d93c602018-08-22 07:39:26 -070061[[clang::optnone]]
62static void fill_abort_message_magic(magic_abort_msg_t* new_magic_abort_message) {
63 // 128-bit magic for the abort message. Chosen by fair dice roll.
64 // This function is intentionally deoptimized to avoid the magic to be present
65 // in the final binary. This causes clang to only use instructions where parts
66 // of the magic are encoded into immediate arguments for the instructions in
67 // all supported architectures.
68 new_magic_abort_message->magic1 = 0xb18e40886ac388f0ULL;
69 new_magic_abort_message->magic2 = 0xc6dfba755a1de0b5ULL;
70}
71
dimitryb6d2b872017-10-18 15:15:54 +020072__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Josh Gao10ec9282017-04-03 15:13:29 -070073void android_set_abort_message(const char* msg) {
74 ScopedPthreadMutexLocker locker(&g_abort_msg_lock);
75
76 if (__abort_message_ptr == nullptr) {
77 // We must have crashed _very_ early.
78 return;
79 }
80
81 if (*__abort_message_ptr != nullptr) {
82 // We already have an abort message.
83 // Assume that the first crash is the one most worth reporting.
84 return;
85 }
86
Luis Hector Chavez6d93c602018-08-22 07:39:26 -070087 size_t size = sizeof(magic_abort_msg_t) + strlen(msg) + 1;
Josh Gao10ec9282017-04-03 15:13:29 -070088 void* map = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
89 if (map == MAP_FAILED) {
90 return;
91 }
92
Luis Hector Chavez6d93c602018-08-22 07:39:26 -070093 magic_abort_msg_t* new_magic_abort_message = reinterpret_cast<magic_abort_msg_t*>(map);
94 fill_abort_message_magic(new_magic_abort_message);
95 new_magic_abort_message->msg.size = size;
96 strcpy(new_magic_abort_message->msg.msg, msg);
97 *__abort_message_ptr = &new_magic_abort_message->msg;
Josh Gao10ec9282017-04-03 15:13:29 -070098}