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