• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,OpenZipMultiDex)60 TEST_F(ArtDexFileLoaderTest, OpenZipMultiDex) {
61   std::string zip_file = GetTestDexFileName("MultiDex");
62   File file(zip_file, O_RDONLY, /*check_usage=*/false);
63   ASSERT_GE(file.Fd(), 0);
64   std::vector<std::unique_ptr<const DexFile>> dex_files;
65   std::string error_msg;
66   ArtDexFileLoader dex_file_loader(file.Release(), zip_file);
67   ASSERT_TRUE(dex_file_loader.Open(/*verify=*/false,
68                                    /*verify_checksum=*/true,
69                                    /*allow_no_dex_files=*/true,
70                                    &error_msg,
71                                    &dex_files))
72       << error_msg;
73   EXPECT_GT(dex_files.size(), 1);
74 }
75 
TEST_F(ArtDexFileLoaderTest,OpenZipEmpty)76 TEST_F(ArtDexFileLoaderTest, OpenZipEmpty) {
77   std::string zip_file = GetTestDexFileName("MainEmptyUncompressed");
78   File file(zip_file, O_RDONLY, /*check_usage=*/false);
79   ASSERT_GE(file.Fd(), 0);
80   std::vector<std::unique_ptr<const DexFile>> dex_files;
81   std::string error_msg;
82   ArtDexFileLoader dex_file_loader(file.Release(), zip_file);
83   ASSERT_TRUE(dex_file_loader.Open(/*verify=*/false,
84                                    /*verify_checksum=*/true,
85                                    /*allow_no_dex_files=*/true,
86                                    &error_msg,
87                                    &dex_files))
88       << error_msg;
89   EXPECT_EQ(dex_files.size(), 0);
90 }
91 
TEST_F(ArtDexFileLoaderTest,GetLocationChecksum)92 TEST_F(ArtDexFileLoaderTest, GetLocationChecksum) {
93   std::unique_ptr<const DexFile> raw(OpenTestDexFile("Main"));
94   EXPECT_NE(raw->GetHeader().checksum_, raw->GetLocationChecksum());
95 }
96 
TEST_F(ArtDexFileLoaderTest,GetChecksum)97 TEST_F(ArtDexFileLoaderTest, GetChecksum) {
98   std::vector<uint32_t> checksums;
99   std::vector<std::string> dex_locations;
100   std::string error_msg;
101   EXPECT_TRUE(ArtDexFileLoader::GetMultiDexChecksums(
102       GetLibCoreDexFileNames()[0].c_str(), &checksums, &dex_locations, &error_msg))
103       << error_msg;
104   ASSERT_EQ(1U, checksums.size());
105   ASSERT_EQ(1U, dex_locations.size());
106   EXPECT_EQ(java_lang_dex_file_->GetLocationChecksum(), checksums[0]);
107   EXPECT_EQ(java_lang_dex_file_->GetLocation(), dex_locations[0]);
108 }
109 
TEST_F(ArtDexFileLoaderTest,GetMultiDexChecksums)110 TEST_F(ArtDexFileLoaderTest, GetMultiDexChecksums) {
111   std::string error_msg;
112   std::vector<uint32_t> checksums;
113   std::vector<std::string> dex_locations;
114   std::string multidex_file = GetTestDexFileName("MultiDex");
115   EXPECT_TRUE(ArtDexFileLoader::GetMultiDexChecksums(
116       multidex_file.c_str(), &checksums, &dex_locations, &error_msg))
117       << error_msg;
118 
119   std::vector<std::unique_ptr<const DexFile>> dexes = OpenTestDexFiles("MultiDex");
120   ASSERT_EQ(2U, dexes.size());
121   ASSERT_EQ(2U, checksums.size());
122   ASSERT_EQ(2U, dex_locations.size());
123 
124   EXPECT_EQ(dexes[0]->GetLocation(), DexFileLoader::GetMultiDexLocation(0, multidex_file.c_str()));
125   EXPECT_EQ(dexes[0]->GetLocation(), dex_locations[0]);
126   EXPECT_EQ(dexes[0]->GetLocationChecksum(), checksums[0]);
127 
128   EXPECT_EQ(dexes[1]->GetLocation(), DexFileLoader::GetMultiDexLocation(1, multidex_file.c_str()));
129   EXPECT_EQ(dexes[1]->GetLocation(), dex_locations[1]);
130   EXPECT_EQ(dexes[1]->GetLocationChecksum(), checksums[1]);
131 }
132 
TEST_F(ArtDexFileLoaderTest,GetMultiDexChecksumsEmptyZip)133 TEST_F(ArtDexFileLoaderTest, GetMultiDexChecksumsEmptyZip) {
134   std::string error_msg;
135   std::vector<uint32_t> checksums;
136   std::vector<std::string> dex_locations;
137   std::string multidex_file = GetTestDexFileName("MainEmptyUncompressed");
138   EXPECT_TRUE(ArtDexFileLoader::GetMultiDexChecksums(
139       multidex_file.c_str(), &checksums, &dex_locations, &error_msg))
140       << error_msg;
141 
142   EXPECT_EQ(dex_locations.size(), 0);
143   EXPECT_EQ(checksums.size(), 0);
144 }
145 
TEST_F(ArtDexFileLoaderTest,GetMultiDexChecksumsDexFile)146 TEST_F(ArtDexFileLoaderTest, GetMultiDexChecksumsDexFile) {
147   std::string error_msg;
148   std::vector<uint32_t> checksums;
149   std::vector<std::string> dex_locations;
150   std::string multidex_file = GetTestDexFileName("VerifierDeps");  // This is a .dex file.
151   EXPECT_TRUE(ArtDexFileLoader::GetMultiDexChecksums(
152       multidex_file.c_str(), &checksums, &dex_locations, &error_msg))
153       << error_msg;
154 
155   EXPECT_EQ(dex_locations.size(), 1);
156   EXPECT_EQ(checksums.size(), 1);
157 }
158 
TEST_F(ArtDexFileLoaderTest,ClassDefs)159 TEST_F(ArtDexFileLoaderTest, ClassDefs) {
160   std::unique_ptr<const DexFile> raw(OpenTestDexFile("Nested"));
161   ASSERT_TRUE(raw.get() != nullptr);
162   EXPECT_EQ(3U, raw->NumClassDefs());
163 
164   const dex::ClassDef& c0 = raw->GetClassDef(0);
165   EXPECT_STREQ("LNested$1;", raw->GetClassDescriptor(c0));
166 
167   const dex::ClassDef& c1 = raw->GetClassDef(1);
168   EXPECT_STREQ("LNested$Inner;", raw->GetClassDescriptor(c1));
169 
170   const dex::ClassDef& c2 = raw->GetClassDef(2);
171   EXPECT_STREQ("LNested;", raw->GetClassDescriptor(c2));
172 }
173 
TEST_F(ArtDexFileLoaderTest,GetMethodSignature)174 TEST_F(ArtDexFileLoaderTest, GetMethodSignature) {
175   std::unique_ptr<const DexFile> raw(OpenTestDexFile("GetMethodSignature"));
176   ASSERT_TRUE(raw.get() != nullptr);
177   EXPECT_EQ(1U, raw->NumClassDefs());
178 
179   const dex::ClassDef& class_def = raw->GetClassDef(0);
180   ASSERT_STREQ("LGetMethodSignature;", raw->GetClassDescriptor(class_def));
181 
182   ClassAccessor accessor(*raw, class_def);
183   ASSERT_TRUE(accessor.HasClassData());
184   auto methods = accessor.GetMethods();
185   auto cur_method = methods.begin();
186 
187   // Check the signature for the static initializer.
188   {
189     ASSERT_EQ(1U, accessor.NumDirectMethods());
190     const dex::MethodId& method_id = raw->GetMethodId(cur_method->GetIndex());
191     const char* name = raw->StringDataByIdx(method_id.name_idx_);
192     ASSERT_STREQ("<init>", name);
193     std::string signature(raw->GetMethodSignature(method_id).ToString());
194     ASSERT_EQ("()V", signature);
195   }
196 
197   // Check all virtual methods.
198   struct Result {
199     const char* name;
200     const char* signature;
201     const char* pretty_method;
202   };
203   static const Result results[] = {
204       {
205           "m1",
206           "(IDJLjava/lang/Object;)Ljava/lang/Float;",
207           "java.lang.Float GetMethodSignature.m1(int, double, long, java.lang.Object)"
208       },
209       {
210           "m2",
211           "(ZSC)LGetMethodSignature;",
212           "GetMethodSignature GetMethodSignature.m2(boolean, short, char)"
213       },
214       {
215           "m3",
216           "()V",
217           "void GetMethodSignature.m3()"
218       },
219       {
220           "m4",
221           "(I)V",
222           "void GetMethodSignature.m4(int)"
223       },
224       {
225           "m5",
226           "(II)V",
227           "void GetMethodSignature.m5(int, int)"
228       },
229       {
230           "m6",
231           "(II[[I)V",
232           "void GetMethodSignature.m6(int, int, int[][])"
233       },
234       {
235           "m7",
236           "(II[[ILjava/lang/Object;)V",
237           "void GetMethodSignature.m7(int, int, int[][], java.lang.Object)"
238       },
239       {
240           "m8",
241           "(II[[ILjava/lang/Object;[[Ljava/lang/Object;)V",
242           "void GetMethodSignature.m8(int, int, int[][], java.lang.Object, java.lang.Object[][])"
243       },
244       {
245           "m9",
246           "()I",
247           "int GetMethodSignature.m9()"
248       },
249       {
250           "mA",
251           "()[[I",
252           "int[][] GetMethodSignature.mA()"
253       },
254       {
255           "mB",
256           "()[[Ljava/lang/Object;",
257           "java.lang.Object[][] GetMethodSignature.mB()"
258       },
259   };
260   ASSERT_EQ(arraysize(results), accessor.NumVirtualMethods());
261   for (const Result& r : results) {
262     ++cur_method;
263     ASSERT_TRUE(cur_method != methods.end());
264     const dex::MethodId& method_id = raw->GetMethodId(cur_method->GetIndex());
265 
266     const char* name = raw->StringDataByIdx(method_id.name_idx_);
267     ASSERT_STREQ(r.name, name);
268 
269     std::string signature(raw->GetMethodSignature(method_id).ToString());
270     ASSERT_EQ(r.signature, signature);
271 
272     std::string plain_method = std::string("GetMethodSignature.") + r.name;
273     ASSERT_EQ(plain_method,
274               raw->PrettyMethod(cur_method->GetIndex(), /* with_signature= */ false));
275     ASSERT_EQ(r.pretty_method,
276               raw->PrettyMethod(cur_method->GetIndex(), /* with_signature= */ true));
277   }
278 }
279 
TEST_F(ArtDexFileLoaderTest,FindStringId)280 TEST_F(ArtDexFileLoaderTest, FindStringId) {
281   std::unique_ptr<const DexFile> raw(OpenTestDexFile("GetMethodSignature"));
282   ASSERT_TRUE(raw.get() != nullptr);
283   EXPECT_EQ(1U, raw->NumClassDefs());
284 
285   const char* strings[] = { "LGetMethodSignature;", "Ljava/lang/Float;", "Ljava/lang/Object;",
286       "D", "I", "J", nullptr };
287   for (size_t i = 0; strings[i] != nullptr; i++) {
288     const char* str = strings[i];
289     const dex::StringId* str_id = raw->FindStringId(str);
290     const char* dex_str = raw->GetStringData(*str_id);
291     EXPECT_STREQ(dex_str, str);
292   }
293 }
294 
TEST_F(ArtDexFileLoaderTest,FindTypeId)295 TEST_F(ArtDexFileLoaderTest, FindTypeId) {
296   for (size_t i = 0; i < java_lang_dex_file_->NumTypeIds(); i++) {
297     const char* type_str = java_lang_dex_file_->StringByTypeIdx(dex::TypeIndex(i));
298     const dex::StringId* type_str_id = java_lang_dex_file_->FindStringId(type_str);
299     ASSERT_TRUE(type_str_id != nullptr);
300     dex::StringIndex type_str_idx = java_lang_dex_file_->GetIndexForStringId(*type_str_id);
301     const dex::TypeId* type_id = java_lang_dex_file_->FindTypeId(type_str_idx);
302     ASSERT_EQ(type_id, java_lang_dex_file_->FindTypeId(type_str));
303     ASSERT_TRUE(type_id != nullptr);
304     EXPECT_EQ(java_lang_dex_file_->GetIndexForTypeId(*type_id).index_, i);
305   }
306 }
307 
TEST_F(ArtDexFileLoaderTest,FindProtoId)308 TEST_F(ArtDexFileLoaderTest, FindProtoId) {
309   for (size_t i = 0; i < java_lang_dex_file_->NumProtoIds(); i++) {
310     const dex::ProtoId& to_find = java_lang_dex_file_->GetProtoId(dex::ProtoIndex(i));
311     const dex::TypeList* to_find_tl = java_lang_dex_file_->GetProtoParameters(to_find);
312     std::vector<dex::TypeIndex> to_find_types;
313     if (to_find_tl != nullptr) {
314       for (size_t j = 0; j < to_find_tl->Size(); j++) {
315         to_find_types.push_back(to_find_tl->GetTypeItem(j).type_idx_);
316       }
317     }
318     const dex::ProtoId* found =
319         java_lang_dex_file_->FindProtoId(to_find.return_type_idx_, to_find_types);
320     ASSERT_TRUE(found != nullptr);
321     EXPECT_EQ(java_lang_dex_file_->GetIndexForProtoId(*found), dex::ProtoIndex(i));
322   }
323 }
324 
TEST_F(ArtDexFileLoaderTest,FindMethodId)325 TEST_F(ArtDexFileLoaderTest, FindMethodId) {
326   for (size_t i = 0; i < java_lang_dex_file_->NumMethodIds(); i++) {
327     const dex::MethodId& to_find = java_lang_dex_file_->GetMethodId(i);
328     const dex::TypeId& klass = java_lang_dex_file_->GetTypeId(to_find.class_idx_);
329     const dex::StringId& name = java_lang_dex_file_->GetStringId(to_find.name_idx_);
330     const dex::ProtoId& signature = java_lang_dex_file_->GetProtoId(to_find.proto_idx_);
331     const dex::MethodId* found = java_lang_dex_file_->FindMethodId(klass, name, signature);
332     ASSERT_TRUE(found != nullptr) << "Didn't find method " << i << ": "
333         << java_lang_dex_file_->StringByTypeIdx(to_find.class_idx_) << "."
334         << java_lang_dex_file_->GetStringData(name)
335         << java_lang_dex_file_->GetMethodSignature(to_find);
336     EXPECT_EQ(java_lang_dex_file_->GetIndexForMethodId(*found), i);
337   }
338 }
339 
TEST_F(ArtDexFileLoaderTest,FindFieldId)340 TEST_F(ArtDexFileLoaderTest, FindFieldId) {
341   for (size_t i = 0; i < java_lang_dex_file_->NumFieldIds(); i++) {
342     const dex::FieldId& to_find = java_lang_dex_file_->GetFieldId(i);
343     const dex::TypeId& klass = java_lang_dex_file_->GetTypeId(to_find.class_idx_);
344     const dex::StringId& name = java_lang_dex_file_->GetStringId(to_find.name_idx_);
345     const dex::TypeId& type = java_lang_dex_file_->GetTypeId(to_find.type_idx_);
346     const dex::FieldId* found = java_lang_dex_file_->FindFieldId(klass, name, type);
347     ASSERT_TRUE(found != nullptr) << "Didn't find field " << i << ": "
348         << java_lang_dex_file_->StringByTypeIdx(to_find.type_idx_) << " "
349         << java_lang_dex_file_->StringByTypeIdx(to_find.class_idx_) << "."
350         << java_lang_dex_file_->GetStringData(name);
351     EXPECT_EQ(java_lang_dex_file_->GetIndexForFieldId(*found), i);
352   }
353 }
354 
TEST_F(ArtDexFileLoaderTest,GetDexCanonicalLocation)355 TEST_F(ArtDexFileLoaderTest, GetDexCanonicalLocation) {
356   ScratchFile file;
357   UniqueCPtr<const char[]> dex_location_real(realpath(file.GetFilename().c_str(), nullptr));
358   std::string dex_location(dex_location_real.get());
359 
360   ASSERT_EQ(dex_location, DexFileLoader::GetDexCanonicalLocation(dex_location.c_str()));
361   std::string multidex_location = DexFileLoader::GetMultiDexLocation(1, dex_location.c_str());
362   ASSERT_EQ(multidex_location, DexFileLoader::GetDexCanonicalLocation(multidex_location.c_str()));
363 
364   std::string dex_location_sym = dex_location + "symlink";
365   ASSERT_EQ(0, symlink(dex_location.c_str(), dex_location_sym.c_str()));
366 
367   ASSERT_EQ(dex_location, DexFileLoader::GetDexCanonicalLocation(dex_location_sym.c_str()));
368 
369   std::string multidex_location_sym = DexFileLoader::GetMultiDexLocation(
370       1, dex_location_sym.c_str());
371   ASSERT_EQ(multidex_location,
372             DexFileLoader::GetDexCanonicalLocation(multidex_location_sym.c_str()));
373 
374   ASSERT_EQ(0, unlink(dex_location_sym.c_str()));
375 }
376 
377 }  // namespace art
378