1 /*
2  * Copyright (C) 2020 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 <stddef.h>
20 #include <stdint.h>
21 
22 #include <log/log.h>
23 #include <unwindstack/Memory.h>
24 
25 #include "gwp_asan/common.h"
26 #include "types.h"
27 #include "utility.h"
28 
29 class Cause;
30 class Tombstone;
31 
32 class GwpAsanCrashData {
33  public:
34   GwpAsanCrashData() = delete;
35   ~GwpAsanCrashData() = default;
36 
37   // Construct the crash data object. Takes a handle to the object that can
38   // supply the memory of the dead process, and pointers to the GWP-ASan state
39   // and metadata regions within that process. Also takes the thread information
40   // of the crashed process. If the process didn't crash via SEGV, GWP-ASan may
41   // still be responsible, as it terminates when it detects an internal error
42   // (double free, invalid free). In these cases, we will retrieve the fault
43   // address from the GWP-ASan allocator's state.
44   GwpAsanCrashData(unwindstack::Memory* process_memory, const ProcessInfo& process_info,
45                    const ThreadInfo& thread_info);
46 
47   // Is GWP-ASan responsible for this crash.
48   bool CrashIsMine() const;
49 
50   // Returns the fault address. The fault address may be the same as provided
51   // during construction, or it may have been retrieved from GWP-ASan's internal
52   // allocator crash state.
53   uintptr_t GetFaultAddress() const;
54 
55   // Dump the GWP-ASan stringified cause of this crash. May only be called if
56   // CrashIsMine() returns true.
57   void DumpCause(log_t* log) const;
58 
59   // Returns whether this crash has a deallocation trace. May only be called if
60   // CrashIsMine() returns true.
61   bool HasDeallocationTrace() const;
62 
63   // Dump the GWP-ASan deallocation trace for this crash. May only be called if
64   // HasDeallocationTrace() returns true.
65   void DumpDeallocationTrace(log_t* log, unwindstack::Unwinder* unwinder) const;
66 
67   // Returns whether this crash has a allocation trace. May only be called if
68   // CrashIsMine() returns true.
69   bool HasAllocationTrace() const;
70 
71   // Dump the GWP-ASan allocation trace for this crash. May only be called if
72   // HasAllocationTrace() returns true.
73   void DumpAllocationTrace(log_t* log, unwindstack::Unwinder* unwinder) const;
74 
75   void AddCauseProtos(Tombstone* tombstone, unwindstack::Unwinder* unwinder) const;
76 
77  protected:
78   // Is GWP-ASan responsible for this crash.
79   bool is_gwp_asan_responsible_ = false;
80 
81   // Thread ID of the crash.
82   size_t thread_id_;
83 
84   // The type of error that GWP-ASan caused (and the stringified version),
85   // Undefined if GWP-ASan isn't responsible for the crash.
86   gwp_asan::Error error_;
87   const char* error_string_;
88 
89   // Pointer to the crash address. Holds the internal crash address if it
90   // exists, otherwise the address provided at construction.
91   uintptr_t crash_address_ = 0u;
92 
93   // Pointer to the metadata for the responsible allocation, nullptr if it
94   // doesn't exist.
95   const gwp_asan::AllocationMetadata* responsible_allocation_ = nullptr;
96 
97   // Internal state.
98   gwp_asan::AllocatorState state_;
99   std::unique_ptr<const gwp_asan::AllocationMetadata> metadata_;
100 };
101