1 /*
2 * Copyright (C) 2024 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 #include <fcntl.h>
18 #include <getopt.h>
19 #include <inttypes.h>
20 #include <stdio.h>
21 #include <unistd.h>
22
23 #include <string>
24 #include <unordered_map>
25 #include <utility>
26
27 #include <android-base/file.h>
28
29 #include <memory_trace/MemoryTrace.h>
30
31 #include "File.h"
32
Usage()33 static void Usage() {
34 fprintf(stderr, "Usage: %s [--attempt_repair] TRACE_FILE1 TRACE_FILE2 ...\n",
35 android::base::Basename(android::base::GetExecutablePath()).c_str());
36 fprintf(stderr, " --attempt_repair\n");
37 fprintf(stderr, " If a trace file has some errors, try to fix them. The new\n");
38 fprintf(stderr, " file will be named TRACE_FILE.repair\n");
39 fprintf(stderr, " TRACE_FILE1 TRACE_FILE2 ...\n");
40 fprintf(stderr, " The trace files to verify\n");
41 fprintf(stderr, "\n Verify trace are valid.\n");
42 exit(1);
43 }
44
WriteRepairEntries(const std::string & repair_file,memory_trace::Entry * entries,size_t num_entries)45 static bool WriteRepairEntries(const std::string& repair_file, memory_trace::Entry* entries,
46 size_t num_entries) {
47 int fd = open(repair_file.c_str(), O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0644);
48 if (fd == -1) {
49 printf(" Failed to create repair file %s: %s\n", repair_file.c_str(), strerror(errno));
50 return false;
51 }
52 bool valid = true;
53 for (size_t i = 0; i < num_entries; i++) {
54 if (!memory_trace::WriteEntryToFd(fd, entries[i])) {
55 printf(" Failed to write entry to file:\n");
56 valid = false;
57 break;
58 }
59 }
60 close(fd);
61 if (!valid) {
62 unlink(repair_file.c_str());
63 }
64 return valid;
65 }
66
VerifyTrace(const char * trace_file,bool attempt_repair)67 static void VerifyTrace(const char* trace_file, bool attempt_repair) {
68 printf("Checking %s\n", trace_file);
69
70 memory_trace::Entry* entries;
71 size_t num_entries;
72 GetUnwindInfo(trace_file, &entries, &num_entries);
73
74 size_t errors_found = 0;
75 size_t errors_repaired = 0;
76 std::unordered_map<uint64_t, std::pair<memory_trace::Entry*, size_t>> live_ptrs;
77 std::pair<memory_trace::Entry*, size_t> erased(nullptr, 0);
78 for (size_t i = 0; i < num_entries; i++) {
79 memory_trace::Entry* entry = &entries[i];
80
81 uint64_t ptr = 0;
82 switch (entry->type) {
83 case memory_trace::MALLOC:
84 case memory_trace::MEMALIGN:
85 ptr = entry->ptr;
86 break;
87 case memory_trace::CALLOC:
88 ptr = entry->ptr;
89 break;
90 case memory_trace::REALLOC:
91 if (entry->ptr != 0) {
92 ptr = entry->ptr;
93 }
94 if (entry->u.old_ptr != 0) {
95 // Verify old pointer
96 auto entry_iter = live_ptrs.find(entry->u.old_ptr);
97 if (entry_iter == live_ptrs.end()) {
98 // Verify the pointer didn't get realloc'd to itself.
99 if (entry->u.old_ptr != entry->ptr) {
100 printf(" Line %zu: freeing of unknown ptr 0x%" PRIx64 "\n", i + 1, entry->u.old_ptr);
101 printf(" %s\n", memory_trace::CreateStringFromEntry(*entry).c_str());
102 errors_found++;
103 if (attempt_repair) {
104 printf(" Unable to repair this failure.\n");
105 }
106 }
107 } else {
108 if (attempt_repair) {
109 erased = entry_iter->second;
110 }
111 live_ptrs.erase(entry_iter);
112 }
113 }
114 break;
115 case memory_trace::FREE:
116 if (entry->ptr != 0) {
117 // Verify pointer is present.
118 auto entry_iter = live_ptrs.find(entry->ptr);
119 if (entry_iter == live_ptrs.end()) {
120 printf(" Line %zu: freeing of unknown ptr 0x%" PRIx64 "\n", i + 1, entry->ptr);
121 printf(" %s\n", memory_trace::CreateStringFromEntry(*entry).c_str());
122 errors_found++;
123 if (attempt_repair) {
124 printf(" Unable to repair this failure.\n");
125 }
126 } else {
127 live_ptrs.erase(entry_iter);
128 }
129 }
130 break;
131 case memory_trace::THREAD_DONE:
132 break;
133 }
134
135 if (ptr != 0) {
136 auto old_entry = live_ptrs.find(ptr);
137 if (old_entry != live_ptrs.end()) {
138 printf(" Line %zu: duplicate ptr 0x%" PRIx64 "\n", i + 1, ptr);
139 printf(" Original entry at line %zu:\n", old_entry->second.second);
140 printf(" %s\n", memory_trace::CreateStringFromEntry(*old_entry->second.first).c_str());
141 printf(" Duplicate entry at line %zu:\n", i + 1);
142 printf(" %s\n", memory_trace::CreateStringFromEntry(*entry).c_str());
143 errors_found++;
144 if (attempt_repair) {
145 // There is a small chance of a race where the same pointer is returned
146 // in two different threads before the free is recorded. If this occurs,
147 // the way to repair is to search forward for the free of the pointer and
148 // swap the two entries.
149 bool fixed = false;
150 for (size_t j = i + 1; j < num_entries; j++) {
151 if ((entries[j].type == memory_trace::FREE && entries[j].ptr == ptr) ||
152 (entries[j].type == memory_trace::REALLOC && entries[j].u.old_ptr == ptr)) {
153 memory_trace::Entry tmp_entry = *entry;
154 *entry = entries[j];
155 entries[j] = tmp_entry;
156 errors_repaired++;
157
158 live_ptrs.erase(old_entry);
159 if (entry->type == memory_trace::REALLOC) {
160 if (entry->ptr != 0) {
161 // Need to add the newly allocated pointer.
162 live_ptrs[entry->ptr] = std::make_pair(entry, i + 1);
163 }
164 if (erased.first != nullptr) {
165 // Need to put the erased old ptr back.
166 live_ptrs[tmp_entry.u.old_ptr] = erased;
167 }
168 }
169 fixed = true;
170 break;
171 }
172 }
173 if (!fixed) {
174 printf(" Unable to fix error.\n");
175 }
176 }
177 } else {
178 live_ptrs[ptr] = std::make_pair(entry, i + 1);
179 }
180 }
181 }
182
183 if (errors_found != 0) {
184 printf("Trace %s is not valid.\n", trace_file);
185 if (attempt_repair) {
186 // Save the repaired data out to a file.
187 std::string repair_file(std::string(trace_file) + ".repair");
188 printf("Creating repaired trace file %s...\n", repair_file.c_str());
189 if (!WriteRepairEntries(repair_file, entries, num_entries)) {
190 printf("Failed trying to write repaired entries to file.\n");
191 } else if (errors_repaired == errors_found) {
192 printf("Repaired file is complete, no more errors.\n");
193 } else {
194 printf("Repaired file is still not valid.\n");
195 }
196 }
197 } else if (attempt_repair) {
198 printf("Trace %s is valid, no repair needed.\n", trace_file);
199 } else {
200 printf("Trace %s is valid.\n", trace_file);
201 }
202
203 FreeEntries(entries, num_entries);
204 }
205
main(int argc,char ** argv)206 int main(int argc, char** argv) {
207 option options[] = {
208 {"attempt_repair", no_argument, nullptr, 'a'},
209 {nullptr, 0, nullptr, 0},
210 };
211 int option_index = 0;
212 int opt = getopt_long(argc, argv, "", options, &option_index);
213 if (argc == 1 || (argc == 2 && opt != -1)) {
214 fprintf(stderr, "Requires at least one TRACE_FILE\n");
215 Usage();
216 }
217
218 bool attempt_repair = false;
219 if (opt == 'a') {
220 attempt_repair = true;
221 } else if (opt != -1) {
222 Usage();
223 }
224
225 for (int i = 1; i < argc; i++) {
226 if (i + 1 == optind) {
227 continue;
228 }
229 VerifyTrace(argv[i], attempt_repair);
230 }
231
232 return 0;
233 }
234