1 /*
2 * Copyright (C) 2011 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 "art_dex_file_loader.h"
18
19 #include <sys/mman.h>
20
21 #include <memory>
22
23 #include "base/common_art_test.h"
24 #include "base/mem_map.h"
25 #include "base/os.h"
26 #include "base/stl_util.h"
27 #include "base/unix_file/fd_file.h"
28 #include "dex/base64_test_util.h"
29 #include "dex/class_accessor-inl.h"
30 #include "dex/code_item_accessors-inl.h"
31 #include "dex/descriptors_names.h"
32 #include "dex/dex_file.h"
33 #include "dex/dex_file-inl.h"
34 #include "dex/dex_file_loader.h"
35
36 namespace art {
37
38 class ArtDexFileLoaderTest : public CommonArtTest {
SetUp()39 void SetUp() override {
40 CommonArtTest::SetUp();
41 // Open a jar file from the boot classpath for use in basic tests of dex accessors.
42 std::vector<std::string> lib_core_dex_file_names = GetLibCoreDexFileNames();
43 CHECK_NE(lib_core_dex_file_names.size(), 0U);
44 dex_files_ = OpenDexFiles(lib_core_dex_file_names[0].c_str());
45 CHECK_NE(dex_files_.size(), 0U);
46 // Save a dex file for use by tests.
47 java_lang_dex_file_ = dex_files_[0].get();
48 }
49
50 protected:
51 std::vector<std::unique_ptr<const DexFile>> dex_files_;
52 const DexFile* java_lang_dex_file_;
53 };
54
TEST_F(ArtDexFileLoaderTest,Open)55 TEST_F(ArtDexFileLoaderTest, Open) {
56 std::unique_ptr<const DexFile> dex(OpenTestDexFile("Nested"));
57 ASSERT_TRUE(dex.get() != nullptr);
58 }
59
TEST_F(ArtDexFileLoaderTest,GetLocationChecksum)60 TEST_F(ArtDexFileLoaderTest, GetLocationChecksum) {
61 std::unique_ptr<const DexFile> raw(OpenTestDexFile("Main"));
62 EXPECT_NE(raw->GetHeader().checksum_, raw->GetLocationChecksum());
63 }
64
TEST_F(ArtDexFileLoaderTest,GetChecksum)65 TEST_F(ArtDexFileLoaderTest, GetChecksum) {
66 std::vector<uint32_t> checksums;
67 std::vector<std::string> dex_locations;
68 std::string error_msg;
69 const ArtDexFileLoader dex_file_loader;
70 EXPECT_TRUE(dex_file_loader.GetMultiDexChecksums(GetLibCoreDexFileNames()[0].c_str(),
71 &checksums,
72 &dex_locations,
73 &error_msg))
74 << error_msg;
75 ASSERT_EQ(1U, checksums.size());
76 ASSERT_EQ(1U, dex_locations.size());
77 EXPECT_EQ(java_lang_dex_file_->GetLocationChecksum(), checksums[0]);
78 EXPECT_EQ(java_lang_dex_file_->GetLocation(), dex_locations[0]);
79 }
80
TEST_F(ArtDexFileLoaderTest,GetMultiDexChecksums)81 TEST_F(ArtDexFileLoaderTest, GetMultiDexChecksums) {
82 std::string error_msg;
83 std::vector<uint32_t> checksums;
84 std::vector<std::string> dex_locations;
85 std::string multidex_file = GetTestDexFileName("MultiDex");
86 const ArtDexFileLoader dex_file_loader;
87 EXPECT_TRUE(dex_file_loader.GetMultiDexChecksums(multidex_file.c_str(),
88 &checksums,
89 &dex_locations,
90 &error_msg)) << error_msg;
91
92 std::vector<std::unique_ptr<const DexFile>> dexes = OpenTestDexFiles("MultiDex");
93 ASSERT_EQ(2U, dexes.size());
94 ASSERT_EQ(2U, checksums.size());
95 ASSERT_EQ(2U, dex_locations.size());
96
97 EXPECT_EQ(dexes[0]->GetLocation(), DexFileLoader::GetMultiDexLocation(0, multidex_file.c_str()));
98 EXPECT_EQ(dexes[0]->GetLocation(), dex_locations[0]);
99 EXPECT_EQ(dexes[0]->GetLocationChecksum(), checksums[0]);
100
101 EXPECT_EQ(dexes[1]->GetLocation(), DexFileLoader::GetMultiDexLocation(1, multidex_file.c_str()));
102 EXPECT_EQ(dexes[1]->GetLocation(), dex_locations[1]);
103 EXPECT_EQ(dexes[1]->GetLocationChecksum(), checksums[1]);
104 }
105
TEST_F(ArtDexFileLoaderTest,ClassDefs)106 TEST_F(ArtDexFileLoaderTest, ClassDefs) {
107 std::unique_ptr<const DexFile> raw(OpenTestDexFile("Nested"));
108 ASSERT_TRUE(raw.get() != nullptr);
109 EXPECT_EQ(3U, raw->NumClassDefs());
110
111 const dex::ClassDef& c0 = raw->GetClassDef(0);
112 EXPECT_STREQ("LNested$1;", raw->GetClassDescriptor(c0));
113
114 const dex::ClassDef& c1 = raw->GetClassDef(1);
115 EXPECT_STREQ("LNested$Inner;", raw->GetClassDescriptor(c1));
116
117 const dex::ClassDef& c2 = raw->GetClassDef(2);
118 EXPECT_STREQ("LNested;", raw->GetClassDescriptor(c2));
119 }
120
TEST_F(ArtDexFileLoaderTest,GetMethodSignature)121 TEST_F(ArtDexFileLoaderTest, GetMethodSignature) {
122 std::unique_ptr<const DexFile> raw(OpenTestDexFile("GetMethodSignature"));
123 ASSERT_TRUE(raw.get() != nullptr);
124 EXPECT_EQ(1U, raw->NumClassDefs());
125
126 const dex::ClassDef& class_def = raw->GetClassDef(0);
127 ASSERT_STREQ("LGetMethodSignature;", raw->GetClassDescriptor(class_def));
128
129 ClassAccessor accessor(*raw, class_def);
130 ASSERT_TRUE(accessor.HasClassData());
131 auto methods = accessor.GetMethods();
132 auto cur_method = methods.begin();
133
134 // Check the signature for the static initializer.
135 {
136 ASSERT_EQ(1U, accessor.NumDirectMethods());
137 const dex::MethodId& method_id = raw->GetMethodId(cur_method->GetIndex());
138 const char* name = raw->StringDataByIdx(method_id.name_idx_);
139 ASSERT_STREQ("<init>", name);
140 std::string signature(raw->GetMethodSignature(method_id).ToString());
141 ASSERT_EQ("()V", signature);
142 }
143
144 // Check all virtual methods.
145 struct Result {
146 const char* name;
147 const char* signature;
148 const char* pretty_method;
149 };
150 static const Result results[] = {
151 {
152 "m1",
153 "(IDJLjava/lang/Object;)Ljava/lang/Float;",
154 "java.lang.Float GetMethodSignature.m1(int, double, long, java.lang.Object)"
155 },
156 {
157 "m2",
158 "(ZSC)LGetMethodSignature;",
159 "GetMethodSignature GetMethodSignature.m2(boolean, short, char)"
160 },
161 {
162 "m3",
163 "()V",
164 "void GetMethodSignature.m3()"
165 },
166 {
167 "m4",
168 "(I)V",
169 "void GetMethodSignature.m4(int)"
170 },
171 {
172 "m5",
173 "(II)V",
174 "void GetMethodSignature.m5(int, int)"
175 },
176 {
177 "m6",
178 "(II[[I)V",
179 "void GetMethodSignature.m6(int, int, int[][])"
180 },
181 {
182 "m7",
183 "(II[[ILjava/lang/Object;)V",
184 "void GetMethodSignature.m7(int, int, int[][], java.lang.Object)"
185 },
186 {
187 "m8",
188 "(II[[ILjava/lang/Object;[[Ljava/lang/Object;)V",
189 "void GetMethodSignature.m8(int, int, int[][], java.lang.Object, java.lang.Object[][])"
190 },
191 {
192 "m9",
193 "()I",
194 "int GetMethodSignature.m9()"
195 },
196 {
197 "mA",
198 "()[[I",
199 "int[][] GetMethodSignature.mA()"
200 },
201 {
202 "mB",
203 "()[[Ljava/lang/Object;",
204 "java.lang.Object[][] GetMethodSignature.mB()"
205 },
206 };
207 ASSERT_EQ(arraysize(results), accessor.NumVirtualMethods());
208 for (const Result& r : results) {
209 ++cur_method;
210 ASSERT_TRUE(cur_method != methods.end());
211 const dex::MethodId& method_id = raw->GetMethodId(cur_method->GetIndex());
212
213 const char* name = raw->StringDataByIdx(method_id.name_idx_);
214 ASSERT_STREQ(r.name, name);
215
216 std::string signature(raw->GetMethodSignature(method_id).ToString());
217 ASSERT_EQ(r.signature, signature);
218
219 std::string plain_method = std::string("GetMethodSignature.") + r.name;
220 ASSERT_EQ(plain_method,
221 raw->PrettyMethod(cur_method->GetIndex(), /* with_signature= */ false));
222 ASSERT_EQ(r.pretty_method,
223 raw->PrettyMethod(cur_method->GetIndex(), /* with_signature= */ true));
224 }
225 }
226
TEST_F(ArtDexFileLoaderTest,FindStringId)227 TEST_F(ArtDexFileLoaderTest, FindStringId) {
228 std::unique_ptr<const DexFile> raw(OpenTestDexFile("GetMethodSignature"));
229 ASSERT_TRUE(raw.get() != nullptr);
230 EXPECT_EQ(1U, raw->NumClassDefs());
231
232 const char* strings[] = { "LGetMethodSignature;", "Ljava/lang/Float;", "Ljava/lang/Object;",
233 "D", "I", "J", nullptr };
234 for (size_t i = 0; strings[i] != nullptr; i++) {
235 const char* str = strings[i];
236 const dex::StringId* str_id = raw->FindStringId(str);
237 const char* dex_str = raw->GetStringData(*str_id);
238 EXPECT_STREQ(dex_str, str);
239 }
240 }
241
TEST_F(ArtDexFileLoaderTest,FindTypeId)242 TEST_F(ArtDexFileLoaderTest, FindTypeId) {
243 for (size_t i = 0; i < java_lang_dex_file_->NumTypeIds(); i++) {
244 const char* type_str = java_lang_dex_file_->StringByTypeIdx(dex::TypeIndex(i));
245 const dex::StringId* type_str_id = java_lang_dex_file_->FindStringId(type_str);
246 ASSERT_TRUE(type_str_id != nullptr);
247 dex::StringIndex type_str_idx = java_lang_dex_file_->GetIndexForStringId(*type_str_id);
248 const dex::TypeId* type_id = java_lang_dex_file_->FindTypeId(type_str_idx);
249 ASSERT_EQ(type_id, java_lang_dex_file_->FindTypeId(type_str));
250 ASSERT_TRUE(type_id != nullptr);
251 EXPECT_EQ(java_lang_dex_file_->GetIndexForTypeId(*type_id).index_, i);
252 }
253 }
254
TEST_F(ArtDexFileLoaderTest,FindProtoId)255 TEST_F(ArtDexFileLoaderTest, FindProtoId) {
256 for (size_t i = 0; i < java_lang_dex_file_->NumProtoIds(); i++) {
257 const dex::ProtoId& to_find = java_lang_dex_file_->GetProtoId(dex::ProtoIndex(i));
258 const dex::TypeList* to_find_tl = java_lang_dex_file_->GetProtoParameters(to_find);
259 std::vector<dex::TypeIndex> to_find_types;
260 if (to_find_tl != nullptr) {
261 for (size_t j = 0; j < to_find_tl->Size(); j++) {
262 to_find_types.push_back(to_find_tl->GetTypeItem(j).type_idx_);
263 }
264 }
265 const dex::ProtoId* found =
266 java_lang_dex_file_->FindProtoId(to_find.return_type_idx_, to_find_types);
267 ASSERT_TRUE(found != nullptr);
268 EXPECT_EQ(java_lang_dex_file_->GetIndexForProtoId(*found), dex::ProtoIndex(i));
269 }
270 }
271
TEST_F(ArtDexFileLoaderTest,FindMethodId)272 TEST_F(ArtDexFileLoaderTest, FindMethodId) {
273 for (size_t i = 0; i < java_lang_dex_file_->NumMethodIds(); i++) {
274 const dex::MethodId& to_find = java_lang_dex_file_->GetMethodId(i);
275 const dex::TypeId& klass = java_lang_dex_file_->GetTypeId(to_find.class_idx_);
276 const dex::StringId& name = java_lang_dex_file_->GetStringId(to_find.name_idx_);
277 const dex::ProtoId& signature = java_lang_dex_file_->GetProtoId(to_find.proto_idx_);
278 const dex::MethodId* found = java_lang_dex_file_->FindMethodId(klass, name, signature);
279 ASSERT_TRUE(found != nullptr) << "Didn't find method " << i << ": "
280 << java_lang_dex_file_->StringByTypeIdx(to_find.class_idx_) << "."
281 << java_lang_dex_file_->GetStringData(name)
282 << java_lang_dex_file_->GetMethodSignature(to_find);
283 EXPECT_EQ(java_lang_dex_file_->GetIndexForMethodId(*found), i);
284 }
285 }
286
TEST_F(ArtDexFileLoaderTest,FindFieldId)287 TEST_F(ArtDexFileLoaderTest, FindFieldId) {
288 for (size_t i = 0; i < java_lang_dex_file_->NumFieldIds(); i++) {
289 const dex::FieldId& to_find = java_lang_dex_file_->GetFieldId(i);
290 const dex::TypeId& klass = java_lang_dex_file_->GetTypeId(to_find.class_idx_);
291 const dex::StringId& name = java_lang_dex_file_->GetStringId(to_find.name_idx_);
292 const dex::TypeId& type = java_lang_dex_file_->GetTypeId(to_find.type_idx_);
293 const dex::FieldId* found = java_lang_dex_file_->FindFieldId(klass, name, type);
294 ASSERT_TRUE(found != nullptr) << "Didn't find field " << i << ": "
295 << java_lang_dex_file_->StringByTypeIdx(to_find.type_idx_) << " "
296 << java_lang_dex_file_->StringByTypeIdx(to_find.class_idx_) << "."
297 << java_lang_dex_file_->GetStringData(name);
298 EXPECT_EQ(java_lang_dex_file_->GetIndexForFieldId(*found), i);
299 }
300 }
301
TEST_F(ArtDexFileLoaderTest,GetDexCanonicalLocation)302 TEST_F(ArtDexFileLoaderTest, GetDexCanonicalLocation) {
303 ScratchFile file;
304 UniqueCPtr<const char[]> dex_location_real(realpath(file.GetFilename().c_str(), nullptr));
305 std::string dex_location(dex_location_real.get());
306
307 ASSERT_EQ(dex_location, DexFileLoader::GetDexCanonicalLocation(dex_location.c_str()));
308 std::string multidex_location = DexFileLoader::GetMultiDexLocation(1, dex_location.c_str());
309 ASSERT_EQ(multidex_location, DexFileLoader::GetDexCanonicalLocation(multidex_location.c_str()));
310
311 std::string dex_location_sym = dex_location + "symlink";
312 ASSERT_EQ(0, symlink(dex_location.c_str(), dex_location_sym.c_str()));
313
314 ASSERT_EQ(dex_location, DexFileLoader::GetDexCanonicalLocation(dex_location_sym.c_str()));
315
316 std::string multidex_location_sym = DexFileLoader::GetMultiDexLocation(
317 1, dex_location_sym.c_str());
318 ASSERT_EQ(multidex_location,
319 DexFileLoader::GetDexCanonicalLocation(multidex_location_sym.c_str()));
320
321 ASSERT_EQ(0, unlink(dex_location_sym.c_str()));
322 }
323
324 } // namespace art
325