blob: d1338cba96ba4ca60333acdfe13a8d86026e1a3d [file] [log] [blame]
Christopher Ferris0b06a592018-01-19 10:26:36 -08001/*
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 <stdint.h>
18#include <sys/types.h>
19#include <unistd.h>
20
21#include <unordered_map>
22
23#include <android-base/test_utils.h>
24
25#include <unwindstack/MapInfo.h>
26#include <unwindstack/Memory.h>
27
28#include <dex/code_item_accessors-no_art-inl.h>
29#include <dex/standard_dex_file.h>
30
31#include <gtest/gtest.h>
32
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -080033#include "DexFile.h"
Christopher Ferris0b06a592018-01-19 10:26:36 -080034
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -080035#include "MemoryFake.h"
Christopher Ferris0b06a592018-01-19 10:26:36 -080036
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -080037namespace unwindstack {
Christopher Ferris0b06a592018-01-19 10:26:36 -080038
39// Borrowed from art/dex/dex_file_test.cc.
40static constexpr uint32_t kDexData[] = {
41 0x0a786564, 0x00383330, 0xc98b3ab8, 0xf3749d94, 0xaecca4d8, 0xffc7b09a, 0xdca9ca7f, 0x5be5deab,
42 0x00000220, 0x00000070, 0x12345678, 0x00000000, 0x00000000, 0x0000018c, 0x00000008, 0x00000070,
43 0x00000004, 0x00000090, 0x00000002, 0x000000a0, 0x00000000, 0x00000000, 0x00000003, 0x000000b8,
44 0x00000001, 0x000000d0, 0x00000130, 0x000000f0, 0x00000122, 0x0000012a, 0x00000132, 0x00000146,
45 0x00000151, 0x00000154, 0x00000158, 0x0000016d, 0x00000001, 0x00000002, 0x00000004, 0x00000006,
46 0x00000004, 0x00000002, 0x00000000, 0x00000005, 0x00000002, 0x0000011c, 0x00000000, 0x00000000,
47 0x00010000, 0x00000007, 0x00000001, 0x00000000, 0x00000000, 0x00000001, 0x00000001, 0x00000000,
48 0x00000003, 0x00000000, 0x0000017e, 0x00000000, 0x00010001, 0x00000001, 0x00000173, 0x00000004,
49 0x00021070, 0x000e0000, 0x00010001, 0x00000000, 0x00000178, 0x00000001, 0x0000000e, 0x00000001,
50 0x3c060003, 0x74696e69, 0x4c06003e, 0x6e69614d, 0x4c12003b, 0x6176616a, 0x6e616c2f, 0x624f2f67,
51 0x7463656a, 0x4d09003b, 0x2e6e6961, 0x6176616a, 0x00560100, 0x004c5602, 0x6a4c5b13, 0x2f617661,
52 0x676e616c, 0x7274532f, 0x3b676e69, 0x616d0400, 0x01006e69, 0x000e0700, 0x07000103, 0x0000000e,
53 0x81000002, 0x01f00480, 0x02880901, 0x0000000c, 0x00000000, 0x00000001, 0x00000000, 0x00000001,
54 0x00000008, 0x00000070, 0x00000002, 0x00000004, 0x00000090, 0x00000003, 0x00000002, 0x000000a0,
55 0x00000005, 0x00000003, 0x000000b8, 0x00000006, 0x00000001, 0x000000d0, 0x00002001, 0x00000002,
56 0x000000f0, 0x00001001, 0x00000001, 0x0000011c, 0x00002002, 0x00000008, 0x00000122, 0x00002003,
57 0x00000002, 0x00000173, 0x00002000, 0x00000001, 0x0000017e, 0x00001000, 0x00000001, 0x0000018c,
58};
59
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -080060TEST(DexFileTest, from_file_open_non_exist) {
61 DexFileFromFile dex_file;
Christopher Ferris0b06a592018-01-19 10:26:36 -080062 ASSERT_FALSE(dex_file.Open(0, "/file/does/not/exist"));
63}
64
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -080065TEST(DexFileTest, from_file_open_too_small) {
Christopher Ferris0b06a592018-01-19 10:26:36 -080066 TemporaryFile tf;
67 ASSERT_TRUE(tf.fd != -1);
68
69 ASSERT_EQ(sizeof(art::DexFile::Header) - 2,
70 static_cast<size_t>(
71 TEMP_FAILURE_RETRY(write(tf.fd, kDexData, sizeof(art::DexFile::Header)) - 2)));
72
73 // Header too small.
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -080074 DexFileFromFile dex_file;
Christopher Ferris0b06a592018-01-19 10:26:36 -080075 ASSERT_FALSE(dex_file.Open(0, tf.path));
76
77 // Header correct, file too small.
78 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET));
79 ASSERT_EQ(sizeof(art::DexFile::Header), static_cast<size_t>(TEMP_FAILURE_RETRY(write(
80 tf.fd, kDexData, sizeof(art::DexFile::Header)))));
81 ASSERT_FALSE(dex_file.Open(0, tf.path));
82}
83
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -080084TEST(DexFileTest, from_file_open) {
Christopher Ferris0b06a592018-01-19 10:26:36 -080085 TemporaryFile tf;
86 ASSERT_TRUE(tf.fd != -1);
87
88 ASSERT_EQ(sizeof(kDexData),
89 static_cast<size_t>(TEMP_FAILURE_RETRY(write(tf.fd, kDexData, sizeof(kDexData)))));
90
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -080091 DexFileFromFile dex_file;
Christopher Ferris0b06a592018-01-19 10:26:36 -080092 ASSERT_TRUE(dex_file.Open(0, tf.path));
93}
94
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -080095TEST(DexFileTest, from_file_open_non_zero_offset) {
Christopher Ferris0b06a592018-01-19 10:26:36 -080096 TemporaryFile tf;
97 ASSERT_TRUE(tf.fd != -1);
98
99 ASSERT_EQ(0x100, lseek(tf.fd, 0x100, SEEK_SET));
100 ASSERT_EQ(sizeof(kDexData),
101 static_cast<size_t>(TEMP_FAILURE_RETRY(write(tf.fd, kDexData, sizeof(kDexData)))));
102
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800103 DexFileFromFile dex_file;
Christopher Ferris0b06a592018-01-19 10:26:36 -0800104 ASSERT_TRUE(dex_file.Open(0x100, tf.path));
105}
106
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800107TEST(DexFileTest, from_memory_fail_too_small_for_header) {
Christopher Ferris0b06a592018-01-19 10:26:36 -0800108 MemoryFake memory;
109
110 memory.SetMemory(0x1000, kDexData, sizeof(art::DexFile::Header) - 1);
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800111 DexFileFromMemory dex_file;
Christopher Ferris0b06a592018-01-19 10:26:36 -0800112
113 ASSERT_FALSE(dex_file.Open(0x1000, &memory));
114}
115
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800116TEST(DexFileTest, from_memory_fail_too_small_for_data) {
Christopher Ferris0b06a592018-01-19 10:26:36 -0800117 MemoryFake memory;
118
119 memory.SetMemory(0x1000, kDexData, sizeof(kDexData) - 2);
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800120 DexFileFromMemory dex_file;
Christopher Ferris0b06a592018-01-19 10:26:36 -0800121
122 ASSERT_FALSE(dex_file.Open(0x1000, &memory));
123}
124
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800125TEST(DexFileTest, from_memory_open) {
Christopher Ferris0b06a592018-01-19 10:26:36 -0800126 MemoryFake memory;
127
128 memory.SetMemory(0x1000, kDexData, sizeof(kDexData));
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800129 DexFileFromMemory dex_file;
Christopher Ferris0b06a592018-01-19 10:26:36 -0800130
131 ASSERT_TRUE(dex_file.Open(0x1000, &memory));
132}
133
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800134TEST(DexFileTest, create_using_file) {
Christopher Ferris0b06a592018-01-19 10:26:36 -0800135 TemporaryFile tf;
136 ASSERT_TRUE(tf.fd != -1);
137
138 ASSERT_EQ(0x500, lseek(tf.fd, 0x500, SEEK_SET));
139 ASSERT_EQ(sizeof(kDexData),
140 static_cast<size_t>(TEMP_FAILURE_RETRY(write(tf.fd, kDexData, sizeof(kDexData)))));
141
142 MemoryFake memory;
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800143 MapInfo info(0, 0x10000, 0, 0x5, tf.path);
144 std::unique_ptr<DexFile> dex_file(DexFile::Create(0x500, &memory, &info));
Christopher Ferris0b06a592018-01-19 10:26:36 -0800145 ASSERT_TRUE(dex_file != nullptr);
146}
147
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800148TEST(DexFileTest, create_using_file_non_zero_start) {
Christopher Ferris0b06a592018-01-19 10:26:36 -0800149 TemporaryFile tf;
150 ASSERT_TRUE(tf.fd != -1);
151
152 ASSERT_EQ(0x500, lseek(tf.fd, 0x500, SEEK_SET));
153 ASSERT_EQ(sizeof(kDexData),
154 static_cast<size_t>(TEMP_FAILURE_RETRY(write(tf.fd, kDexData, sizeof(kDexData)))));
155
156 MemoryFake memory;
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800157 MapInfo info(0x100, 0x10000, 0, 0x5, tf.path);
158 std::unique_ptr<DexFile> dex_file(DexFile::Create(0x600, &memory, &info));
Christopher Ferris0b06a592018-01-19 10:26:36 -0800159 ASSERT_TRUE(dex_file != nullptr);
160}
161
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800162TEST(DexFileTest, create_using_file_non_zero_offset) {
Christopher Ferris0b06a592018-01-19 10:26:36 -0800163 TemporaryFile tf;
164 ASSERT_TRUE(tf.fd != -1);
165
166 ASSERT_EQ(0x500, lseek(tf.fd, 0x500, SEEK_SET));
167 ASSERT_EQ(sizeof(kDexData),
168 static_cast<size_t>(TEMP_FAILURE_RETRY(write(tf.fd, kDexData, sizeof(kDexData)))));
169
170 MemoryFake memory;
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800171 MapInfo info(0x100, 0x10000, 0x200, 0x5, tf.path);
172 std::unique_ptr<DexFile> dex_file(DexFile::Create(0x400, &memory, &info));
Christopher Ferris0b06a592018-01-19 10:26:36 -0800173 ASSERT_TRUE(dex_file != nullptr);
174}
175
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800176TEST(DexFileTest, create_using_memory_empty_file) {
Christopher Ferris0b06a592018-01-19 10:26:36 -0800177 MemoryFake memory;
178 memory.SetMemory(0x4000, kDexData, sizeof(kDexData));
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800179 MapInfo info(0x100, 0x10000, 0x200, 0x5, "");
180 std::unique_ptr<DexFile> dex_file(DexFile::Create(0x4000, &memory, &info));
Christopher Ferris0b06a592018-01-19 10:26:36 -0800181 ASSERT_TRUE(dex_file != nullptr);
182}
183
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800184TEST(DexFileTest, create_using_memory_file_does_not_exist) {
Christopher Ferris0b06a592018-01-19 10:26:36 -0800185 MemoryFake memory;
186 memory.SetMemory(0x4000, kDexData, sizeof(kDexData));
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800187 MapInfo info(0x100, 0x10000, 0x200, 0x5, "/does/not/exist");
188 std::unique_ptr<DexFile> dex_file(DexFile::Create(0x4000, &memory, &info));
Christopher Ferris0b06a592018-01-19 10:26:36 -0800189 ASSERT_TRUE(dex_file != nullptr);
190}
191
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800192TEST(DexFileTest, create_using_memory_file_is_malformed) {
Christopher Ferris0b06a592018-01-19 10:26:36 -0800193 TemporaryFile tf;
194 ASSERT_TRUE(tf.fd != -1);
195
196 ASSERT_EQ(sizeof(kDexData) - 10,
197 static_cast<size_t>(TEMP_FAILURE_RETRY(write(tf.fd, kDexData, sizeof(kDexData) - 10))));
198
199 MemoryFake memory;
200 memory.SetMemory(0x4000, kDexData, sizeof(kDexData));
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800201 MapInfo info(0x4000, 0x10000, 0x200, 0x5, "/does/not/exist");
202 std::unique_ptr<DexFile> dex_file(DexFile::Create(0x4000, &memory, &info));
Christopher Ferris0b06a592018-01-19 10:26:36 -0800203 ASSERT_TRUE(dex_file != nullptr);
204
205 // Check it came from memory by clearing memory and verifying it fails.
206 memory.Clear();
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800207 dex_file.reset(DexFile::Create(0x4000, &memory, &info));
Christopher Ferris0b06a592018-01-19 10:26:36 -0800208 ASSERT_TRUE(dex_file == nullptr);
209}
210
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800211TEST(DexFileTest, get_method_not_opened) {
Christopher Ferris0b06a592018-01-19 10:26:36 -0800212 std::string method("something");
213 uint64_t method_offset = 100;
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800214 DexFile dex_file;
Christopher Ferris0b06a592018-01-19 10:26:36 -0800215 dex_file.GetMethodInformation(0x100, &method, &method_offset);
216 EXPECT_EQ("something", method);
217 EXPECT_EQ(100U, method_offset);
218}
219
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800220TEST(DexFileTest, get_method) {
Christopher Ferris0b06a592018-01-19 10:26:36 -0800221 MemoryFake memory;
222 memory.SetMemory(0x4000, kDexData, sizeof(kDexData));
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800223 MapInfo info(0x100, 0x10000, 0x200, 0x5, "");
224 std::unique_ptr<DexFile> dex_file(DexFile::Create(0x4000, &memory, &info));
Christopher Ferris0b06a592018-01-19 10:26:36 -0800225 ASSERT_TRUE(dex_file != nullptr);
226
227 std::string method;
228 uint64_t method_offset;
229 dex_file->GetMethodInformation(0x102, &method, &method_offset);
230 EXPECT_EQ("Main.<init>", method);
231 EXPECT_EQ(2U, method_offset);
232
233 method = "not_in_a_method";
234 method_offset = 0x123;
235 dex_file->GetMethodInformation(0x100000, &method, &method_offset);
236 EXPECT_EQ("not_in_a_method", method);
237 EXPECT_EQ(0x123U, method_offset);
238}
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800239
240} // namespace unwindstack