1 /*
2 * Copyright (C) 2018 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 #include <sys/types.h>
20 #include <unistd.h>
21
22 #include <memory>
23 #include <unordered_map>
24
25 #include <MemoryLocal.h>
26 #include <android-base/file.h>
27 #include <gtest/gtest.h>
28 #include <unwindstack/MapInfo.h>
29 #include <unwindstack/Memory.h>
30
31 #include "DexFile.h"
32 #include "DexFileData.h"
33 #include "utils/MemoryFake.h"
34
35 namespace unwindstack {
36
37 static constexpr size_t kNumLeakLoops = 5000;
38 static constexpr size_t kMaxAllowedLeakBytes = 4 * 1024;
39
CheckForLeak(size_t loop,size_t * first_allocated_bytes,size_t * last_allocated_bytes)40 static void CheckForLeak(size_t loop, size_t* first_allocated_bytes, size_t* last_allocated_bytes) {
41 size_t allocated_bytes = mallinfo().uordblks;
42 if (*first_allocated_bytes == 0) {
43 *first_allocated_bytes = allocated_bytes;
44 } else if (*last_allocated_bytes > *first_allocated_bytes) {
45 // Check that the total memory did not increase too much over the first loop.
46 ASSERT_LE(*last_allocated_bytes - *first_allocated_bytes, kMaxAllowedLeakBytes)
47 << "Failed in loop " << loop << " first_allocated_bytes " << *first_allocated_bytes
48 << " last_allocated_bytes " << *last_allocated_bytes;
49 }
50 *last_allocated_bytes = allocated_bytes;
51 }
52
TEST(DexFileTest,from_file_no_leak)53 TEST(DexFileTest, from_file_no_leak) {
54 TemporaryFile tf;
55 ASSERT_TRUE(tf.fd != -1);
56
57 ASSERT_EQ(sizeof(kDexData),
58 static_cast<size_t>(TEMP_FAILURE_RETRY(write(tf.fd, kDexData, sizeof(kDexData)))));
59
60 size_t first_allocated_bytes = 0;
61 size_t last_allocated_bytes = 0;
62 for (size_t i = 0; i < kNumLeakLoops; i++) {
63 MemoryFake memory;
64 auto info = MapInfo::Create(0, 0x10000, 0, 0x5, tf.path);
65 EXPECT_TRUE(DexFile::Create(0, sizeof(kDexData), &memory, info.get()) != nullptr);
66 ASSERT_NO_FATAL_FAILURE(CheckForLeak(i, &first_allocated_bytes, &last_allocated_bytes));
67 }
68 }
69
TEST(DexFileTest,from_memory_no_leak)70 TEST(DexFileTest, from_memory_no_leak) {
71 MemoryFake memory;
72
73 memory.SetMemory(0x1000, kDexData, sizeof(kDexData));
74
75 size_t first_allocated_bytes = 0;
76 size_t last_allocated_bytes = 0;
77 for (size_t i = 0; i < kNumLeakLoops; i++) {
78 EXPECT_TRUE(DexFile::Create(0x1000, sizeof(kDexData), &memory, nullptr) != nullptr);
79 ASSERT_NO_FATAL_FAILURE(CheckForLeak(i, &first_allocated_bytes, &last_allocated_bytes));
80 }
81 }
82
TEST(DexFileTest,create_using_file)83 TEST(DexFileTest, create_using_file) {
84 TemporaryFile tf;
85 ASSERT_TRUE(tf.fd != -1);
86
87 ASSERT_EQ(0x500, lseek(tf.fd, 0x500, SEEK_SET));
88 ASSERT_EQ(sizeof(kDexData),
89 static_cast<size_t>(TEMP_FAILURE_RETRY(write(tf.fd, kDexData, sizeof(kDexData)))));
90
91 MemoryFake memory;
92 auto info = MapInfo::Create(0, 0x10000, 0, 0x5, tf.path);
93 EXPECT_TRUE(DexFile::Create(0x500, sizeof(kDexData), &memory, info.get()) != nullptr);
94 }
95
TEST(DexFileTest,create_using_file_non_zero_start)96 TEST(DexFileTest, create_using_file_non_zero_start) {
97 TemporaryFile tf;
98 ASSERT_TRUE(tf.fd != -1);
99
100 ASSERT_EQ(0x500, lseek(tf.fd, 0x500, SEEK_SET));
101 ASSERT_EQ(sizeof(kDexData),
102 static_cast<size_t>(TEMP_FAILURE_RETRY(write(tf.fd, kDexData, sizeof(kDexData)))));
103
104 MemoryFake memory;
105 auto info = MapInfo::Create(0x100, 0x10000, 0, 0x5, tf.path);
106 EXPECT_TRUE(DexFile::Create(0x600, sizeof(kDexData), &memory, info.get()) != nullptr);
107 }
108
TEST(DexFileTest,create_using_file_non_zero_offset)109 TEST(DexFileTest, create_using_file_non_zero_offset) {
110 TemporaryFile tf;
111 ASSERT_TRUE(tf.fd != -1);
112
113 ASSERT_EQ(0x500, lseek(tf.fd, 0x500, SEEK_SET));
114 ASSERT_EQ(sizeof(kDexData),
115 static_cast<size_t>(TEMP_FAILURE_RETRY(write(tf.fd, kDexData, sizeof(kDexData)))));
116
117 MemoryFake memory;
118 auto info = MapInfo::Create(0x100, 0x10000, 0x200, 0x5, tf.path);
119 EXPECT_TRUE(DexFile::Create(0x400, sizeof(kDexData), &memory, info.get()) != nullptr);
120 }
121
TEST(DexFileTest,create_using_memory_empty_file)122 TEST(DexFileTest, create_using_memory_empty_file) {
123 MemoryFake memory;
124 memory.SetMemory(0x4000, kDexData, sizeof(kDexData));
125 auto info = MapInfo::Create(0x100, 0x10000, 0x200, 0x5, "");
126 EXPECT_TRUE(DexFile::Create(0x4000, sizeof(kDexData), &memory, info.get()) != nullptr);
127 }
128
TEST(DexFileTest,create_using_memory_file_does_not_exist)129 TEST(DexFileTest, create_using_memory_file_does_not_exist) {
130 MemoryFake memory;
131 memory.SetMemory(0x4000, kDexData, sizeof(kDexData));
132 auto info = MapInfo::Create(0x100, 0x10000, 0x200, 0x5, "/does/not/exist");
133 EXPECT_TRUE(DexFile::Create(0x4000, sizeof(kDexData), &memory, info.get()) != nullptr);
134 }
135
TEST(DexFileTest,create_using_memory_file_is_malformed)136 TEST(DexFileTest, create_using_memory_file_is_malformed) {
137 TemporaryFile tf;
138 ASSERT_TRUE(tf.fd != -1);
139
140 ASSERT_EQ(sizeof(kDexData) - 10,
141 static_cast<size_t>(TEMP_FAILURE_RETRY(write(tf.fd, kDexData, sizeof(kDexData) - 10))));
142
143 MemoryFake memory;
144 memory.SetMemory(0x4000, kDexData, sizeof(kDexData));
145 auto info = MapInfo::Create(0x4000, 0x10000, 0x200, 0x5, "/does/not/exist");
146 std::shared_ptr<DexFile> dex_file =
147 DexFile::Create(0x4000, sizeof(kDexData), &memory, info.get());
148 ASSERT_TRUE(dex_file != nullptr);
149
150 // Check it came from memory by clearing memory and verifying it fails.
151 memory.Clear();
152 dex_file = DexFile::Create(0x4000, sizeof(kDexData), &memory, info.get());
153 EXPECT_TRUE(dex_file == nullptr);
154 }
155
TEST(DexFileTest,create_using_memory_header_too_small)156 TEST(DexFileTest, create_using_memory_header_too_small) {
157 MemoryFake memory;
158 size_t size = 10;
159 memory.SetMemory(0x4000, kDexData, size);
160 EXPECT_TRUE(DexFile::Create(0x4000, size, &memory, nullptr) == nullptr);
161 }
162
TEST(DexFileTest,create_using_memory_size_too_small)163 TEST(DexFileTest, create_using_memory_size_too_small) {
164 MemoryFake memory;
165 size_t size = sizeof(kDexData) - 1;
166 memory.SetMemory(0x4000, kDexData, size);
167 EXPECT_TRUE(DexFile::Create(0x4000, size, &memory, nullptr) == nullptr);
168 }
169
TEST(DexFileTest,get_method)170 TEST(DexFileTest, get_method) {
171 MemoryFake memory;
172 memory.SetMemory(0x4000, kDexData, sizeof(kDexData));
173 auto info = MapInfo::Create(0x100, 0x10000, 0x200, 0x5, "");
174 std::shared_ptr<DexFile> dex_file(DexFile::Create(0x4000, sizeof(kDexData), &memory, info.get()));
175 ASSERT_TRUE(dex_file != nullptr);
176
177 SharedString method;
178 uint64_t method_offset;
179 ASSERT_TRUE(dex_file->GetFunctionName(0x4102, &method, &method_offset));
180 EXPECT_EQ("Main.<init>", method);
181 EXPECT_EQ(2U, method_offset);
182
183 ASSERT_TRUE(dex_file->GetFunctionName(0x4118, &method, &method_offset));
184 EXPECT_EQ("Main.main", method);
185 EXPECT_EQ(0U, method_offset);
186 }
187
TEST(DexFileTest,get_method_empty)188 TEST(DexFileTest, get_method_empty) {
189 MemoryFake memory;
190 memory.SetMemory(0x4000, kDexData, sizeof(kDexData));
191 auto info = MapInfo::Create(0x100, 0x10000, 0x200, 0x5, "");
192 std::shared_ptr<DexFile> dex_file(DexFile::Create(0x4000, sizeof(kDexData), &memory, info.get()));
193 ASSERT_TRUE(dex_file != nullptr);
194
195 SharedString method;
196 uint64_t method_offset;
197 EXPECT_FALSE(dex_file->GetFunctionName(0x100000, &method, &method_offset));
198
199 EXPECT_FALSE(dex_file->GetFunctionName(0x98, &method, &method_offset));
200 }
201
TEST(DexFileTest,get_method_from_cache)202 TEST(DexFileTest, get_method_from_cache) {
203 TemporaryFile tf;
204 ASSERT_TRUE(tf.fd != -1);
205 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET));
206 ASSERT_EQ(sizeof(kDexData),
207 static_cast<size_t>(TEMP_FAILURE_RETRY(write(tf.fd, kDexData, sizeof(kDexData)))));
208
209 MemoryFake memory;
210 auto info = MapInfo::Create(0x4000, 0x10000, 0, 0x5, tf.path);
211 std::shared_ptr<DexFile> dex_file =
212 DexFile::Create(0x4000, sizeof(kDexData), &memory, info.get());
213 EXPECT_TRUE(dex_file != nullptr);
214
215 SharedString method;
216 uint64_t method_offset;
217 ASSERT_TRUE(dex_file->GetFunctionName(0x4118, &method, &method_offset));
218 EXPECT_EQ("Main.main", method);
219 EXPECT_EQ(0U, method_offset);
220
221 // Corrupt the dex file: change the name of the class.
222 int main = std::string(reinterpret_cast<const char*>(kDexData), sizeof(kDexData)).find("Main");
223 ASSERT_EQ(main, lseek(tf.fd, main, SEEK_SET));
224 ASSERT_EQ(4u, static_cast<size_t>(TEMP_FAILURE_RETRY(write(tf.fd, "MAIN", 4))));
225
226 // Check that we see the *old* cached value.
227 ASSERT_TRUE(dex_file->GetFunctionName(0x4118, &method, &method_offset));
228 EXPECT_EQ("Main.main", method);
229 EXPECT_EQ(0U, method_offset);
230
231 // Check that for other methods we see the *new* updated value.
232 ASSERT_TRUE(dex_file->GetFunctionName(0x4102, &method, &method_offset));
233 EXPECT_EQ("MAIN.<init>", method);
234 EXPECT_EQ(2U, method_offset);
235 }
236
237 } // namespace unwindstack
238