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 <malloc.h>
18 #include <stdint.h>
19
20 #include <string>
21
22 #include <android-base/file.h>
23 #include <gtest/gtest.h>
24
25 #include <memory_trace/MemoryTrace.h>
26
27 #include "File.h"
28
GetTestDirectory()29 static std::string GetTestDirectory() {
30 return android::base::GetExecutableDirectory() + "/tests";
31 }
32
GetTestZip()33 static std::string GetTestZip() {
34 return GetTestDirectory() + "/test.zip";
35 }
36
TEST(FileTest,zip_get_contents)37 TEST(FileTest, zip_get_contents) {
38 EXPECT_EQ("12345: malloc 0x1000 16\n12345: free 0x1000\n", ZipGetContents(GetTestZip().c_str()));
39 }
40
TEST(FileTest,zip_get_contents_bad_file)41 TEST(FileTest, zip_get_contents_bad_file) {
42 EXPECT_EQ("", ZipGetContents("/does/not/exist.zip"));
43 }
44
TEST(FileTest,get_unwind_info_from_zip_file)45 TEST(FileTest, get_unwind_info_from_zip_file) {
46 // This will allocate, so do it before getting mallinfo.
47 std::string file_name = GetTestZip();
48
49 size_t mallinfo_before = mallinfo().uordblks;
50 memory_trace::Entry* entries;
51 size_t num_entries;
52 GetUnwindInfo(file_name.c_str(), &entries, &num_entries);
53 size_t mallinfo_after = mallinfo().uordblks;
54
55 // Verify no memory is allocated.
56 EXPECT_EQ(mallinfo_after, mallinfo_before);
57
58 ASSERT_EQ(2U, num_entries);
59 EXPECT_EQ(12345, entries[0].tid);
60 EXPECT_EQ(memory_trace::MALLOC, entries[0].type);
61 EXPECT_EQ(0x1000U, entries[0].ptr);
62 EXPECT_EQ(16U, entries[0].size);
63 EXPECT_EQ(0U, entries[0].u.old_ptr);
64
65 EXPECT_EQ(12345, entries[1].tid);
66 EXPECT_EQ(memory_trace::FREE, entries[1].type);
67 EXPECT_EQ(0x1000U, entries[1].ptr);
68 EXPECT_EQ(0U, entries[1].size);
69 EXPECT_EQ(0U, entries[1].u.old_ptr);
70
71 FreeEntries(entries, num_entries);
72 }
73
TEST(FileTest,get_unwind_info_bad_zip_file)74 TEST(FileTest, get_unwind_info_bad_zip_file) {
75 memory_trace::Entry* entries;
76 size_t num_entries;
77 EXPECT_DEATH(GetUnwindInfo("/does/not/exist.zip", &entries, &num_entries), "");
78 }
79
TEST(FileTest,get_unwind_info_from_text_file)80 TEST(FileTest, get_unwind_info_from_text_file) {
81 // This will allocate, so do it before getting mallinfo.
82 std::string file_name = GetTestDirectory() + "/test.txt";
83
84 size_t mallinfo_before = mallinfo().uordblks;
85 memory_trace::Entry* entries;
86 size_t num_entries;
87 GetUnwindInfo(file_name.c_str(), &entries, &num_entries);
88 size_t mallinfo_after = mallinfo().uordblks;
89
90 // Verify no memory is allocated.
91 EXPECT_EQ(mallinfo_after, mallinfo_before);
92
93 ASSERT_EQ(2U, num_entries);
94 EXPECT_EQ(98765, entries[0].tid);
95 EXPECT_EQ(memory_trace::MEMALIGN, entries[0].type);
96 EXPECT_EQ(0xa000U, entries[0].ptr);
97 EXPECT_EQ(124U, entries[0].size);
98 EXPECT_EQ(16U, entries[0].u.align);
99
100 EXPECT_EQ(98765, entries[1].tid);
101 EXPECT_EQ(memory_trace::FREE, entries[1].type);
102 EXPECT_EQ(0xa000U, entries[1].ptr);
103 EXPECT_EQ(0U, entries[1].size);
104 EXPECT_EQ(0U, entries[1].u.old_ptr);
105
106 FreeEntries(entries, num_entries);
107 }
108
TEST(FileTest,get_unwind_info_bad_file)109 TEST(FileTest, get_unwind_info_bad_file) {
110 memory_trace::Entry* entries;
111 size_t num_entries;
112 EXPECT_DEATH(GetUnwindInfo("/does/not/exist", &entries, &num_entries), "");
113 }
114
TEST(FileTest,present_bytes_updated)115 TEST(FileTest, present_bytes_updated) {
116 TemporaryFile tf;
117 ASSERT_NE(-1, tf.fd);
118 // Entry 0
119 ASSERT_TRUE(memory_trace::WriteEntryToFd(
120 tf.fd, memory_trace::Entry{.type = memory_trace::MALLOC, .ptr = 0x100, .size = 100}));
121 // Entry 1
122 ASSERT_TRUE(memory_trace::WriteEntryToFd(
123 tf.fd, memory_trace::Entry{.type = memory_trace::MALLOC, .ptr = 0x200, .size = 10}));
124 // Entry 2
125 ASSERT_TRUE(memory_trace::WriteEntryToFd(
126 tf.fd, memory_trace::Entry{
127 .type = memory_trace::MEMALIGN, .ptr = 0x300, .size = 300, .u.align = 16}));
128 // Entry 3
129 ASSERT_TRUE(memory_trace::WriteEntryToFd(
130 tf.fd, memory_trace::Entry{
131 .type = memory_trace::CALLOC, .ptr = 0x400, .size = 400, .u.n_elements = 100}));
132 // Entry 4
133 ASSERT_TRUE(memory_trace::WriteEntryToFd(
134 tf.fd, memory_trace::Entry{.type = memory_trace::FREE, .ptr = 0x400, .present_bytes = 400}));
135 // Entry 5
136 ASSERT_TRUE(memory_trace::WriteEntryToFd(
137 tf.fd, memory_trace::Entry{.type = memory_trace::FREE, .ptr = 0x100, .present_bytes = 100}));
138 // Entry 6
139 ASSERT_TRUE(memory_trace::WriteEntryToFd(
140 tf.fd, memory_trace::Entry{.type = memory_trace::MALLOC, .ptr = 0x100, .size = 101}));
141 // Entry 7
142 ASSERT_TRUE(memory_trace::WriteEntryToFd(
143 tf.fd, memory_trace::Entry{.type = memory_trace::FREE, .ptr = 0x300, .present_bytes = 300}));
144 // Entry 8
145 ASSERT_TRUE(memory_trace::WriteEntryToFd(
146 tf.fd, memory_trace::Entry{.type = memory_trace::FREE, .ptr = 0x100, .present_bytes = 101}));
147 // Entry 9
148 ASSERT_TRUE(memory_trace::WriteEntryToFd(
149 tf.fd, memory_trace::Entry{.type = memory_trace::MALLOC, .ptr = 0x700, .size = 1000}));
150 // Enrty 10
151 ASSERT_TRUE(memory_trace::WriteEntryToFd(tf.fd, memory_trace::Entry{.type = memory_trace::REALLOC,
152 .ptr = 0x800,
153 .size = 800,
154 .u.old_ptr = 0x700,
155 .present_bytes = 700}));
156 // Entry 11
157 ASSERT_TRUE(memory_trace::WriteEntryToFd(
158 tf.fd, memory_trace::Entry{.type = memory_trace::FREE, .ptr = 0x800, .present_bytes = 800}));
159 close(tf.fd);
160 tf.fd = -1;
161
162 memory_trace::Entry* entries;
163 size_t num_entries;
164 GetUnwindInfo(tf.path, &entries, &num_entries);
165 EXPECT_EQ(12U, num_entries);
166
167 // Only verify the present bytes values.
168 EXPECT_EQ(100, entries[0].present_bytes);
169 // No free for this allocation.
170 EXPECT_EQ(-1, entries[1].present_bytes);
171 EXPECT_EQ(300, entries[2].present_bytes);
172 EXPECT_EQ(400, entries[3].present_bytes);
173 EXPECT_EQ(400, entries[4].present_bytes);
174 EXPECT_EQ(100, entries[5].present_bytes);
175 EXPECT_EQ(101, entries[6].present_bytes);
176 EXPECT_EQ(300, entries[7].present_bytes);
177 EXPECT_EQ(101, entries[8].present_bytes);
178 EXPECT_EQ(700, entries[9].present_bytes);
179 EXPECT_EQ(800, entries[10].present_bytes);
180 EXPECT_EQ(800, entries[11].present_bytes);
181
182 FreeEntries(entries, num_entries);
183 }
184
TEST(FileTest,present_bytes_reset_realloc)185 TEST(FileTest, present_bytes_reset_realloc) {
186 TemporaryFile tf;
187 ASSERT_NE(-1, tf.fd);
188 ASSERT_TRUE(memory_trace::WriteEntryToFd(
189 tf.fd, memory_trace::Entry{.type = memory_trace::MALLOC, .ptr = 0x100, .size = 200}));
190 ASSERT_TRUE(memory_trace::WriteEntryToFd(tf.fd, memory_trace::Entry{.type = memory_trace::REALLOC,
191 .ptr = 0x200,
192 .size = 400,
193 .u.old_ptr = 0x100,
194 .present_bytes = 200}));
195
196 memory_trace::Entry* entries;
197 size_t num_entries;
198 GetUnwindInfo(tf.path, &entries, &num_entries);
199 EXPECT_EQ(2U, num_entries);
200
201 // Verify that the present bytes is -1 for the actual realloc since it was
202 // never freed.
203 EXPECT_EQ(200, entries[0].present_bytes);
204 EXPECT_EQ(-1, entries[1].present_bytes);
205
206 FreeEntries(entries, num_entries);
207 }
208
TEST(FileTest,present_bytes_adjusted)209 TEST(FileTest, present_bytes_adjusted) {
210 TemporaryFile tf;
211 ASSERT_NE(-1, tf.fd);
212 // Entry 0
213 ASSERT_TRUE(memory_trace::WriteEntryToFd(
214 tf.fd, memory_trace::Entry{.type = memory_trace::MALLOC, .ptr = 0x100, .size = 10}));
215 // Entry 1
216 ASSERT_TRUE(memory_trace::WriteEntryToFd(
217 tf.fd, memory_trace::Entry{.type = memory_trace::FREE, .ptr = 0x100, .present_bytes = 30}));
218 // Entry 2
219 ASSERT_TRUE(memory_trace::WriteEntryToFd(
220 tf.fd, memory_trace::Entry{.type = memory_trace::MALLOC, .ptr = 0x200, .size = 100}));
221 // Entry 3
222 ASSERT_TRUE(memory_trace::WriteEntryToFd(tf.fd, memory_trace::Entry{.type = memory_trace::REALLOC,
223 .ptr = 0x300,
224 .size = 700,
225 .u.old_ptr = 0x200,
226 .present_bytes = 200}));
227 // Entry 4
228 ASSERT_TRUE(memory_trace::WriteEntryToFd(
229 tf.fd, memory_trace::Entry{.type = memory_trace::FREE, .ptr = 0x300, .present_bytes = 1000}));
230 close(tf.fd);
231 tf.fd = -1;
232
233 memory_trace::Entry* entries;
234 size_t num_entries;
235 GetUnwindInfo(tf.path, &entries, &num_entries);
236 EXPECT_EQ(5U, num_entries);
237
238 // Only verify the present bytes values.
239 EXPECT_EQ(10, entries[0].present_bytes);
240 EXPECT_EQ(30, entries[1].present_bytes);
241 EXPECT_EQ(100, entries[2].present_bytes);
242 EXPECT_EQ(700, entries[3].present_bytes);
243 EXPECT_EQ(1000, entries[4].present_bytes);
244
245 FreeEntries(entries, num_entries);
246 }
247