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