1 /*
2 * Copyright (C) 2019 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 <err.h>
18 #include <errno.h>
19 #include <stdint.h>
20 #include <sys/mman.h>
21 #include <sys/types.h>
22 #include <sys/wait.h>
23 #include <unistd.h>
24
25 #include <string>
26
27 #include <android-base/file.h>
28 #include <android-base/strings.h>
29 #include <ziparchive/zip_archive.h>
30
31 #include "Alloc.h"
32 #include "AllocParser.h"
33 #include "File.h"
34
ZipGetContents(const char * filename)35 std::string ZipGetContents(const char* filename) {
36 ZipArchiveHandle archive;
37 if (OpenArchive(filename, &archive) != 0) {
38 return "";
39 }
40
41 // It is assumed that the archive contains only a single entry.
42 void* cookie;
43 std::string contents;
44 if (StartIteration(archive, &cookie) == 0) {
45 ZipEntry entry;
46 std::string name;
47 if (Next(cookie, &entry, &name) == 0) {
48 contents.resize(entry.uncompressed_length);
49 if (ExtractToMemory(archive, &entry, reinterpret_cast<uint8_t*>(contents.data()),
50 entry.uncompressed_length) != 0) {
51 contents = "";
52 }
53 }
54 }
55
56 CloseArchive(archive);
57 return contents;
58 }
59
WaitPid(pid_t pid)60 static void WaitPid(pid_t pid) {
61 int wstatus;
62 pid_t wait_pid = TEMP_FAILURE_RETRY(waitpid(pid, &wstatus, 0));
63 if (wait_pid != pid) {
64 if (wait_pid == -1) {
65 err(1, "waitpid() failed");
66 } else {
67 errx(1, "Unexpected pid from waitpid(): expected %d, returned %d", pid, wait_pid);
68 }
69 }
70 if (!WIFEXITED(wstatus)) {
71 errx(1, "Forked process did not terminate with exit() call");
72 }
73 if (WEXITSTATUS(wstatus) != 0) {
74 errx(1, "Bad exit value from forked process: returned %d", WEXITSTATUS(wstatus));
75 }
76 }
77
78 // This function should not do any memory allocations in the main function.
79 // Any true allocation should happen in fork'd code.
GetUnwindInfo(const char * filename,AllocEntry ** entries,size_t * num_entries)80 void GetUnwindInfo(const char* filename, AllocEntry** entries, size_t* num_entries) {
81 void* mem =
82 mmap(nullptr, sizeof(size_t), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
83 if (mem == MAP_FAILED) {
84 err(1, "Unable to allocate a shared map of size %zu", sizeof(size_t));
85 }
86 *reinterpret_cast<size_t*>(mem) = 0;
87
88 pid_t pid;
89 if ((pid = fork()) == 0) {
90 // First get the number of lines in the trace file. It is assumed
91 // that there are no blank lines, and every line contains a valid
92 // allocation operation.
93 std::string contents;
94 if (android::base::EndsWith(filename, ".zip")) {
95 contents = ZipGetContents(filename);
96 } else if (!android::base::ReadFileToString(filename, &contents)) {
97 errx(1, "Unable to get contents of %s", filename);
98 }
99 if (contents.empty()) {
100 errx(1, "Unable to get contents of %s", filename);
101 }
102
103 size_t lines = 0;
104 size_t index = 0;
105 while (true) {
106 index = contents.find('\n', index);
107 if (index == std::string::npos) {
108 break;
109 }
110 index++;
111 lines++;
112 }
113 if (contents[contents.size() - 1] != '\n') {
114 // Add one since the last line doesn't end in '\n'.
115 lines++;
116 }
117 *reinterpret_cast<size_t*>(mem) = lines;
118 _exit(0);
119 } else if (pid == -1) {
120 err(1, "fork() call failed");
121 }
122 WaitPid(pid);
123 *num_entries = *reinterpret_cast<size_t*>(mem);
124 munmap(mem, sizeof(size_t));
125
126 mem = mmap(nullptr, *num_entries * sizeof(AllocEntry), PROT_READ | PROT_WRITE,
127 MAP_ANONYMOUS | MAP_SHARED, -1, 0);
128 if (mem == MAP_FAILED) {
129 err(1, "Unable to allocate a shared map of size %zu", *num_entries * sizeof(AllocEntry));
130 }
131 *entries = reinterpret_cast<AllocEntry*>(mem);
132
133 if ((pid = fork()) == 0) {
134 std::string contents;
135 if (android::base::EndsWith(filename, ".zip")) {
136 contents = ZipGetContents(filename);
137 } else if (!android::base::ReadFileToString(filename, &contents)) {
138 errx(1, "Unable to get contents of %s", filename);
139 }
140 if (contents.empty()) {
141 errx(1, "Contents of zip file %s is empty.", filename);
142 }
143
144 size_t entry_idx = 0;
145 size_t start_str = 0;
146 size_t end_str = 0;
147 while (true) {
148 end_str = contents.find('\n', start_str);
149 if (end_str == std::string::npos) {
150 break;
151 }
152 if (entry_idx == *num_entries) {
153 errx(1, "Too many entries, stopped at entry %zu", entry_idx);
154 }
155 contents[end_str] = '\0';
156 AllocGetData(&contents[start_str], &(*entries)[entry_idx++]);
157 start_str = end_str + 1;
158 }
159 if (entry_idx != *num_entries) {
160 errx(1, "Mismatched number of entries found: expected %zu, found %zu", *num_entries,
161 entry_idx);
162 }
163 _exit(0);
164 } else if (pid == -1) {
165 err(1, "fork() call failed");
166 }
167 WaitPid(pid);
168 }
169
FreeEntries(AllocEntry * entries,size_t num_entries)170 void FreeEntries(AllocEntry* entries, size_t num_entries) {
171 munmap(entries, num_entries * sizeof(AllocEntry));
172 }
173