1 /*
2  * Copyright 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <bionic/reserved_signals.h>
20 #include <signal.h>
21 #include <stdint.h>
22 #include <string.h>
23 #include <sys/cdefs.h>
24 #include <sys/system_properties.h>
25 #include <sys/types.h>
26 
27 __BEGIN_DECLS
28 
29 // Forward declare these classes so not everyone has to include GWP-ASan
30 // headers.
31 namespace gwp_asan {
32 struct AllocatorState;
33 struct AllocationMetadata;
34 };  // namespace gwp_asan
35 
36 struct crash_detail_page_t;
37 
38 // When updating this data structure, CrashInfoDataDynamic and the code in
39 // ReadCrashInfo() must also be updated.
40 struct __attribute__((packed)) debugger_process_info {
41   void* abort_msg;
42   void* fdsan_table;
43   const gwp_asan::AllocatorState* gwp_asan_state;
44   const gwp_asan::AllocationMetadata* gwp_asan_metadata;
45   const char* scudo_stack_depot;
46   const char* scudo_region_info;
47   const char* scudo_ring_buffer;
48   size_t scudo_ring_buffer_size;
49   size_t scudo_stack_depot_size;
50   bool recoverable_crash;
51   struct crash_detail_page_t* crash_detail_page;
52 };
53 
54 // GWP-ASan calbacks to support the recoverable mode. Separate from the
55 // debuggerd_callbacks_t because these values aren't available at debuggerd_init
56 // time, and have to be synthesized on request.
57 typedef struct {
58   bool (*debuggerd_needs_gwp_asan_recovery)(void* fault_addr);
59   void (*debuggerd_gwp_asan_pre_crash_report)(void* fault_addr);
60   void (*debuggerd_gwp_asan_post_crash_report)(void* fault_addr);
61 } gwp_asan_callbacks_t;
62 
63 // These callbacks are called in a signal handler, and thus must be async signal safe.
64 // If null, the callbacks will not be called.
65 typedef struct {
66   debugger_process_info (*get_process_info)();
67   gwp_asan_callbacks_t (*get_gwp_asan_callbacks)();
68   void (*post_dump)();
69 } debuggerd_callbacks_t;
70 
71 void debuggerd_init(debuggerd_callbacks_t* callbacks);
72 bool debuggerd_handle_signal(int signal_number, siginfo_t* info, void* context);
73 
74 // DEBUGGER_ACTION_DUMP_TOMBSTONE and DEBUGGER_ACTION_DUMP_BACKTRACE are both
75 // triggered via BIONIC_SIGNAL_DEBUGGER. The debugger_action_t is sent via si_value
76 // using sigqueue(2) or equivalent. If no si_value is specified (e.g. if the
77 // signal is sent by kill(2)), the default behavior is to print the backtrace
78 // to the log.
79 #define DEBUGGER_SIGNAL BIONIC_SIGNAL_DEBUGGER
80 
debuggerd_register_handlers(struct sigaction * action)81 static void __attribute__((__unused__)) debuggerd_register_handlers(struct sigaction* action) {
82   bool enabled = true;
83 #if ANDROID_DEBUGGABLE
84   char value[PROP_VALUE_MAX] = "";
85   enabled = !(__system_property_get("debug.debuggerd.disable", value) > 0 && !strcmp(value, "1"));
86 #endif
87   if (enabled) {
88     sigaction(SIGABRT, action, nullptr);
89     sigaction(SIGBUS, action, nullptr);
90     sigaction(SIGFPE, action, nullptr);
91     sigaction(SIGILL, action, nullptr);
92     sigaction(SIGSEGV, action, nullptr);
93     sigaction(SIGSTKFLT, action, nullptr);
94     sigaction(SIGSYS, action, nullptr);
95     sigaction(SIGTRAP, action, nullptr);
96   }
97 
98   sigaction(BIONIC_SIGNAL_DEBUGGER, action, nullptr);
99 }
100 
101 __END_DECLS
102