blob: 50905c4f886b752e54e7e73f4c4bd7e248b75f4d [file] [log] [blame]
Calin Juravle87e2cb62017-06-13 21:48:45 -07001/*
2 * Copyright (C) 2017 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 <gtest/gtest.h>
18
19
20#include "class_loader_context.h"
21#include "common_runtime_test.h"
22
23#include "base/dchecked_vector.h"
24#include "base/stl_util.h"
25#include "class_linker.h"
26#include "dex_file.h"
27#include "handle_scope-inl.h"
28#include "mirror/class.h"
29#include "mirror/class_loader.h"
30#include "mirror/object-inl.h"
31#include "oat_file_assistant.h"
32#include "runtime.h"
33#include "scoped_thread_state_change-inl.h"
34#include "thread.h"
35#include "well_known_classes.h"
36
37namespace art {
38
39class ClassLoaderContextTest : public CommonRuntimeTest {
40 public:
41 void VerifyContextSize(ClassLoaderContext* context, size_t expected_size) {
42 ASSERT_TRUE(context != nullptr);
43 ASSERT_EQ(expected_size, context->class_loader_chain_.size());
44 }
45
46 void VerifyClassLoaderPCL(ClassLoaderContext* context,
47 size_t index,
Calin Juravle57d0acc2017-07-11 17:41:30 -070048 const std::string& classpath) {
Calin Juravle87e2cb62017-06-13 21:48:45 -070049 VerifyClassLoaderInfo(
50 context, index, ClassLoaderContext::kPathClassLoader, classpath);
51 }
52
53 void VerifyClassLoaderDLC(ClassLoaderContext* context,
54 size_t index,
Calin Juravle57d0acc2017-07-11 17:41:30 -070055 const std::string& classpath) {
Calin Juravle87e2cb62017-06-13 21:48:45 -070056 VerifyClassLoaderInfo(
57 context, index, ClassLoaderContext::kDelegateLastClassLoader, classpath);
58 }
59
Calin Juravle57d0acc2017-07-11 17:41:30 -070060 void VerifyClassLoaderPCLFromTestDex(ClassLoaderContext* context,
61 size_t index,
62 const std::string& test_name) {
63 VerifyClassLoaderFromTestDex(
64 context, index, ClassLoaderContext::kPathClassLoader, test_name);
65 }
66
67 void VerifyClassLoaderDLCFromTestDex(ClassLoaderContext* context,
68 size_t index,
69 const std::string& test_name) {
70 VerifyClassLoaderFromTestDex(
71 context, index, ClassLoaderContext::kDelegateLastClassLoader, test_name);
72 }
73
Calin Juravle87e2cb62017-06-13 21:48:45 -070074 void VerifyOpenDexFiles(
75 ClassLoaderContext* context,
76 size_t index,
77 std::vector<std::vector<std::unique_ptr<const DexFile>>*>& all_dex_files) {
78 ASSERT_TRUE(context != nullptr);
79 ASSERT_TRUE(context->dex_files_open_attempted_);
80 ASSERT_TRUE(context->dex_files_open_result_);
81 ClassLoaderContext::ClassLoaderInfo& info = context->class_loader_chain_[index];
82 ASSERT_EQ(all_dex_files.size(), info.classpath.size());
83 size_t cur_open_dex_index = 0;
84 for (size_t k = 0; k < all_dex_files.size(); k++) {
85 std::vector<std::unique_ptr<const DexFile>>& dex_files_for_cp_elem = *(all_dex_files[k]);
86 for (size_t i = 0; i < dex_files_for_cp_elem.size(); i++) {
87 ASSERT_LT(cur_open_dex_index, info.opened_dex_files.size());
88
89 std::unique_ptr<const DexFile>& opened_dex_file =
90 info.opened_dex_files[cur_open_dex_index++];
91 std::unique_ptr<const DexFile>& expected_dex_file = dex_files_for_cp_elem[i];
92
93 ASSERT_EQ(expected_dex_file->GetLocation(), opened_dex_file->GetLocation());
94 ASSERT_EQ(expected_dex_file->GetLocationChecksum(), opened_dex_file->GetLocationChecksum());
95 ASSERT_EQ(info.classpath[k], opened_dex_file->GetBaseLocation());
96 }
97 }
98 }
99
Calin Juravle57d0acc2017-07-11 17:41:30 -0700100 std::unique_ptr<ClassLoaderContext> CreateContextForClassLoader(jobject class_loader) {
101 return ClassLoaderContext::CreateContextForClassLoader(class_loader, nullptr);
102 }
103
104 void VerifyContextForClassLoader(ClassLoaderContext* context) {
105 ASSERT_TRUE(context != nullptr);
106 ASSERT_TRUE(context->dex_files_open_attempted_);
107 ASSERT_TRUE(context->dex_files_open_result_);
108 ASSERT_FALSE(context->owns_the_dex_files_);
109 ASSERT_FALSE(context->special_shared_library_);
110 }
111
Calin Juravle87e2cb62017-06-13 21:48:45 -0700112 private:
113 void VerifyClassLoaderInfo(ClassLoaderContext* context,
114 size_t index,
115 ClassLoaderContext::ClassLoaderType type,
Calin Juravle57d0acc2017-07-11 17:41:30 -0700116 const std::string& classpath) {
Calin Juravle87e2cb62017-06-13 21:48:45 -0700117 ASSERT_TRUE(context != nullptr);
118 ASSERT_GT(context->class_loader_chain_.size(), index);
119 ClassLoaderContext::ClassLoaderInfo& info = context->class_loader_chain_[index];
120 ASSERT_EQ(type, info.type);
121 std::vector<std::string> expected_classpath;
122 Split(classpath, ':', &expected_classpath);
123 ASSERT_EQ(expected_classpath, info.classpath);
124 }
Calin Juravle57d0acc2017-07-11 17:41:30 -0700125
126 void VerifyClassLoaderFromTestDex(ClassLoaderContext* context,
127 size_t index,
128 ClassLoaderContext::ClassLoaderType type,
129 const std::string& test_name) {
130 std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles(test_name.c_str());
131 std::vector<std::vector<std::unique_ptr<const DexFile>>*> all_dex_files;
132 all_dex_files.push_back(&dex_files);
133
134 VerifyClassLoaderInfo(context, index, type, GetTestDexFileName(test_name.c_str()));
135 VerifyOpenDexFiles(context, index, all_dex_files);
136 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700137};
138
139TEST_F(ClassLoaderContextTest, ParseValidContextPCL) {
140 std::unique_ptr<ClassLoaderContext> context =
141 ClassLoaderContext::Create("PCL[a.dex]");
142 VerifyContextSize(context.get(), 1);
143 VerifyClassLoaderPCL(context.get(), 0, "a.dex");
144}
145
146TEST_F(ClassLoaderContextTest, ParseValidContextDLC) {
147 std::unique_ptr<ClassLoaderContext> context =
148 ClassLoaderContext::Create("DLC[a.dex]");
149 VerifyContextSize(context.get(), 1);
150 VerifyClassLoaderDLC(context.get(), 0, "a.dex");
151}
152
153TEST_F(ClassLoaderContextTest, ParseValidContextChain) {
154 std::unique_ptr<ClassLoaderContext> context =
155 ClassLoaderContext::Create("PCL[a.dex:b.dex];DLC[c.dex:d.dex];PCL[e.dex]");
156 VerifyContextSize(context.get(), 3);
157 VerifyClassLoaderPCL(context.get(), 0, "a.dex:b.dex");
158 VerifyClassLoaderDLC(context.get(), 1, "c.dex:d.dex");
159 VerifyClassLoaderPCL(context.get(), 2, "e.dex");
160}
161
162TEST_F(ClassLoaderContextTest, ParseValidEmptyContextDLC) {
163 std::unique_ptr<ClassLoaderContext> context =
164 ClassLoaderContext::Create("DLC[]");
165 VerifyContextSize(context.get(), 1);
166 VerifyClassLoaderDLC(context.get(), 0, "");
167}
168
169TEST_F(ClassLoaderContextTest, ParseValidContextSpecialSymbol) {
170 std::unique_ptr<ClassLoaderContext> context =
171 ClassLoaderContext::Create(OatFile::kSpecialSharedLibrary);
172 VerifyContextSize(context.get(), 0);
173}
174
175TEST_F(ClassLoaderContextTest, ParseInvalidValidContexts) {
176 ASSERT_TRUE(nullptr == ClassLoaderContext::Create("ABC[a.dex]"));
177 ASSERT_TRUE(nullptr == ClassLoaderContext::Create("PCL"));
178 ASSERT_TRUE(nullptr == ClassLoaderContext::Create("PCL[a.dex"));
179 ASSERT_TRUE(nullptr == ClassLoaderContext::Create("PCLa.dex]"));
180 ASSERT_TRUE(nullptr == ClassLoaderContext::Create("PCL{a.dex}"));
181 ASSERT_TRUE(nullptr == ClassLoaderContext::Create("PCL[a.dex];DLC[b.dex"));
182}
183
184TEST_F(ClassLoaderContextTest, OpenInvalidDexFiles) {
185 std::unique_ptr<ClassLoaderContext> context =
186 ClassLoaderContext::Create("PCL[does_not_exist.dex]");
187 VerifyContextSize(context.get(), 1);
188 ASSERT_FALSE(context->OpenDexFiles(InstructionSet::kArm, "."));
189}
190
191TEST_F(ClassLoaderContextTest, OpenValidDexFiles) {
192 std::string multidex_name = GetTestDexFileName("MultiDex");
193 std::vector<std::unique_ptr<const DexFile>> multidex_files = OpenTestDexFiles("MultiDex");
194 std::string myclass_dex_name = GetTestDexFileName("MyClass");
195 std::vector<std::unique_ptr<const DexFile>> myclass_dex_files = OpenTestDexFiles("MyClass");
196 std::string dex_name = GetTestDexFileName("Main");
197 std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("Main");
198
199
200 std::unique_ptr<ClassLoaderContext> context =
201 ClassLoaderContext::Create(
202 "PCL[" + multidex_name + ":" + myclass_dex_name + "];" +
203 "DLC[" + dex_name + "]");
204
205 ASSERT_TRUE(context->OpenDexFiles(InstructionSet::kArm, /*classpath_dir*/ ""));
206
207 VerifyContextSize(context.get(), 2);
208 std::vector<std::vector<std::unique_ptr<const DexFile>>*> all_dex_files0;
209 all_dex_files0.push_back(&multidex_files);
210 all_dex_files0.push_back(&myclass_dex_files);
211 std::vector<std::vector<std::unique_ptr<const DexFile>>*> all_dex_files1;
212 all_dex_files1.push_back(&dex_files);
213
214 VerifyOpenDexFiles(context.get(), 0, all_dex_files0);
215 VerifyOpenDexFiles(context.get(), 1, all_dex_files1);
216}
217
218TEST_F(ClassLoaderContextTest, OpenInvalidDexFilesMix) {
219 std::string dex_name = GetTestDexFileName("Main");
220 std::unique_ptr<ClassLoaderContext> context =
221 ClassLoaderContext::Create("PCL[does_not_exist.dex];DLC[" + dex_name + "]");
222 ASSERT_FALSE(context->OpenDexFiles(InstructionSet::kArm, ""));
223}
224
225TEST_F(ClassLoaderContextTest, CreateClassLoader) {
226 std::string dex_name = GetTestDexFileName("Main");
227 std::unique_ptr<ClassLoaderContext> context =
228 ClassLoaderContext::Create("PCL[" + dex_name + "]");
229 ASSERT_TRUE(context->OpenDexFiles(InstructionSet::kArm, ""));
230
231 std::vector<std::unique_ptr<const DexFile>> classpath_dex = OpenTestDexFiles("Main");
232 std::vector<std::unique_ptr<const DexFile>> compilation_sources = OpenTestDexFiles("MultiDex");
233
234 std::vector<const DexFile*> compilation_sources_raw =
235 MakeNonOwningPointerVector(compilation_sources);
236 jobject jclass_loader = context->CreateClassLoader(compilation_sources_raw);
237 ASSERT_TRUE(jclass_loader != nullptr);
238
239 ScopedObjectAccess soa(Thread::Current());
240
241 StackHandleScope<2> hs(soa.Self());
242 Handle<mirror::ClassLoader> class_loader = hs.NewHandle(
243 soa.Decode<mirror::ClassLoader>(jclass_loader));
244
245 ASSERT_TRUE(class_loader->GetClass() ==
246 soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_PathClassLoader));
247 ASSERT_TRUE(class_loader->GetParent()->GetClass() ==
248 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_BootClassLoader));
249
250
251 std::vector<const DexFile*> class_loader_dex_files = GetDexFiles(jclass_loader);
252 ASSERT_EQ(classpath_dex.size() + compilation_sources.size(), class_loader_dex_files.size());
253
254 // The classpath dex files must come first.
255 for (size_t i = 0; i < classpath_dex.size(); i++) {
256 ASSERT_EQ(classpath_dex[i]->GetLocation(),
257 class_loader_dex_files[i]->GetLocation());
258 ASSERT_EQ(classpath_dex[i]->GetLocationChecksum(),
259 class_loader_dex_files[i]->GetLocationChecksum());
260 }
261
262 // The compilation dex files must come second.
263 for (size_t i = 0, k = classpath_dex.size(); i < compilation_sources.size(); i++, k++) {
264 ASSERT_EQ(compilation_sources[i]->GetLocation(),
265 class_loader_dex_files[k]->GetLocation());
266 ASSERT_EQ(compilation_sources[i]->GetLocationChecksum(),
267 class_loader_dex_files[k]->GetLocationChecksum());
268 }
269}
270
Calin Juravle7b0648a2017-07-07 18:40:50 -0700271TEST_F(ClassLoaderContextTest, CreateClassLoaderWithEmptyContext) {
272 std::unique_ptr<ClassLoaderContext> context =
273 ClassLoaderContext::Create("");
274 ASSERT_TRUE(context->OpenDexFiles(InstructionSet::kArm, ""));
275
276 std::vector<std::unique_ptr<const DexFile>> compilation_sources = OpenTestDexFiles("MultiDex");
277
278 std::vector<const DexFile*> compilation_sources_raw =
279 MakeNonOwningPointerVector(compilation_sources);
280 jobject jclass_loader = context->CreateClassLoader(compilation_sources_raw);
281 ASSERT_TRUE(jclass_loader != nullptr);
282
283 ScopedObjectAccess soa(Thread::Current());
284
285 StackHandleScope<2> hs(soa.Self());
286 Handle<mirror::ClassLoader> class_loader = hs.NewHandle(
287 soa.Decode<mirror::ClassLoader>(jclass_loader));
288
289 ASSERT_TRUE(class_loader->GetClass() ==
290 soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_PathClassLoader));
291 ASSERT_TRUE(class_loader->GetParent()->GetClass() ==
292 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_BootClassLoader));
293
294
295 std::vector<const DexFile*> class_loader_dex_files = GetDexFiles(jclass_loader);
296
297 // The compilation sources should be the only files present in the class loader
298 ASSERT_EQ(compilation_sources.size(), class_loader_dex_files.size());
299 for (size_t i = 0; i < compilation_sources.size(); i++) {
300 ASSERT_EQ(compilation_sources[i]->GetLocation(),
301 class_loader_dex_files[i]->GetLocation());
302 ASSERT_EQ(compilation_sources[i]->GetLocationChecksum(),
303 class_loader_dex_files[i]->GetLocationChecksum());
304 }
305}
306
Calin Juravle87e2cb62017-06-13 21:48:45 -0700307TEST_F(ClassLoaderContextTest, RemoveSourceLocations) {
308 std::unique_ptr<ClassLoaderContext> context =
309 ClassLoaderContext::Create("PCL[a.dex]");
310 dchecked_vector<std::string> classpath_dex;
311 classpath_dex.push_back("a.dex");
312 dchecked_vector<std::string> compilation_sources;
313 compilation_sources.push_back("src.dex");
314
315 // Nothing should be removed.
316 ASSERT_FALSE(context->RemoveLocationsFromClassPaths(compilation_sources));
317 VerifyClassLoaderPCL(context.get(), 0, "a.dex");
318 // Classes should be removed.
319 ASSERT_TRUE(context->RemoveLocationsFromClassPaths(classpath_dex));
320 VerifyClassLoaderPCL(context.get(), 0, "");
321}
322
323TEST_F(ClassLoaderContextTest, EncodeInOatFile) {
324 std::string dex1_name = GetTestDexFileName("Main");
325 std::string dex2_name = GetTestDexFileName("MyClass");
326 std::unique_ptr<ClassLoaderContext> context =
327 ClassLoaderContext::Create("PCL[" + dex1_name + ":" + dex2_name + "]");
328 ASSERT_TRUE(context->OpenDexFiles(InstructionSet::kArm, ""));
329
330 std::vector<std::unique_ptr<const DexFile>> dex1 = OpenTestDexFiles("Main");
331 std::vector<std::unique_ptr<const DexFile>> dex2 = OpenTestDexFiles("MyClass");
332 std::string encoding = context->EncodeContextForOatFile("");
Calin Juravle7b0648a2017-07-07 18:40:50 -0700333 std::string expected_encoding = "PCL[" +
334 dex1[0]->GetLocation() + "*" + std::to_string(dex1[0]->GetLocationChecksum()) + ":" +
335 dex2[0]->GetLocation() + "*" + std::to_string(dex2[0]->GetLocationChecksum()) + "]";
Calin Juravle87e2cb62017-06-13 21:48:45 -0700336 ASSERT_EQ(expected_encoding, context->EncodeContextForOatFile(""));
337}
338
Calin Juravle7b0648a2017-07-07 18:40:50 -0700339TEST_F(ClassLoaderContextTest, DecodeOatFileKey) {
340 std::string oat_file_encoding = "PCL[a.dex*123:b.dex*456]";
341 std::vector<std::string> classpath;
342 std::vector<uint32_t> checksums;
343 bool is_special_shared_library;
344 bool result = ClassLoaderContext::DecodePathClassLoaderContextFromOatFileKey(
345 oat_file_encoding,
346 &classpath,
347 &checksums,
348 &is_special_shared_library);
349 ASSERT_TRUE(result);
350 ASSERT_FALSE(is_special_shared_library);
351 ASSERT_EQ(2u, classpath.size());
352 ASSERT_EQ(2u, checksums.size());
353 ASSERT_EQ("a.dex", classpath[0]);
354 ASSERT_EQ(123u, checksums[0]);
355 ASSERT_EQ("b.dex", classpath[1]);
356 ASSERT_EQ(456u, checksums[1]);
357}
358
359TEST_F(ClassLoaderContextTest, DecodeOatFileKeySpecialLibrary) {
360 std::string oat_file_encoding = "&";
361 std::vector<std::string> classpath;
362 std::vector<uint32_t> checksums;
363 bool is_special_shared_library;
364 bool result = ClassLoaderContext::DecodePathClassLoaderContextFromOatFileKey(
365 oat_file_encoding,
366 &classpath,
367 &checksums,
368 &is_special_shared_library);
369 ASSERT_TRUE(result);
370 ASSERT_TRUE(is_special_shared_library);
371 ASSERT_TRUE(classpath.empty());
372 ASSERT_TRUE(checksums.empty());
373}
374
Calin Juravle57d0acc2017-07-11 17:41:30 -0700375// TODO(calin) add a test which creates the context for a class loader together with dex_elements.
376TEST_F(ClassLoaderContextTest, CreateContextForClassLoader) {
377 // The chain is
378 // ClassLoaderA (PathClassLoader)
379 // ^
380 // |
381 // ClassLoaderB (DelegateLastClassLoader)
382 // ^
383 // |
384 // ClassLoaderC (PathClassLoader)
385 // ^
386 // |
387 // ClassLoaderD (DelegateLastClassLoader)
388
389 jobject class_loader_a = LoadDexInPathClassLoader("ForClassLoaderA", nullptr);
390 jobject class_loader_b = LoadDexInDelegateLastClassLoader("ForClassLoaderB", class_loader_a);
391 jobject class_loader_c = LoadDexInPathClassLoader("ForClassLoaderC", class_loader_b);
392 jobject class_loader_d = LoadDexInDelegateLastClassLoader("ForClassLoaderD", class_loader_c);
393
394 std::unique_ptr<ClassLoaderContext> context = CreateContextForClassLoader(class_loader_d);
395
396 VerifyContextForClassLoader(context.get());
397 VerifyContextSize(context.get(), 4);
398
399 VerifyClassLoaderDLCFromTestDex(context.get(), 0, "ForClassLoaderD");
400 VerifyClassLoaderPCLFromTestDex(context.get(), 1, "ForClassLoaderC");
401 VerifyClassLoaderDLCFromTestDex(context.get(), 2, "ForClassLoaderB");
402 VerifyClassLoaderPCLFromTestDex(context.get(), 3, "ForClassLoaderA");
403}
404
Calin Juravle87e2cb62017-06-13 21:48:45 -0700405} // namespace art