blob: 930160f2b4b303abe14c91bb56287667a601daca [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
Andreas Gampe72527382017-09-02 16:53:03 -070017#include "class_loader_context.h"
18
Calin Juravle87e2cb62017-06-13 21:48:45 -070019#include <gtest/gtest.h>
Calin Juravle821a2592017-08-11 14:33:38 -070020#include <stdlib.h>
Calin Juravle87e2cb62017-06-13 21:48:45 -070021
Andreas Gampe72527382017-09-02 16:53:03 -070022#include "android-base/strings.h"
Calin Juravle87e2cb62017-06-13 21:48:45 -070023
24#include "base/dchecked_vector.h"
25#include "base/stl_util.h"
Calin Juravle821a2592017-08-11 14:33:38 -070026#include "class_loader_context.h"
Calin Juravle87e2cb62017-06-13 21:48:45 -070027#include "class_linker.h"
Calin Juravle821a2592017-08-11 14:33:38 -070028#include "common_runtime_test.h"
Calin Juravle87e2cb62017-06-13 21:48:45 -070029#include "dex_file.h"
Calin Juravle821a2592017-08-11 14:33:38 -070030#include "dex2oat_environment_test.h"
Calin Juravle87e2cb62017-06-13 21:48:45 -070031#include "handle_scope-inl.h"
32#include "mirror/class.h"
33#include "mirror/class_loader.h"
34#include "mirror/object-inl.h"
35#include "oat_file_assistant.h"
36#include "runtime.h"
37#include "scoped_thread_state_change-inl.h"
38#include "thread.h"
39#include "well_known_classes.h"
40
41namespace art {
42
43class ClassLoaderContextTest : public CommonRuntimeTest {
44 public:
45 void VerifyContextSize(ClassLoaderContext* context, size_t expected_size) {
46 ASSERT_TRUE(context != nullptr);
47 ASSERT_EQ(expected_size, context->class_loader_chain_.size());
48 }
49
50 void VerifyClassLoaderPCL(ClassLoaderContext* context,
51 size_t index,
Calin Juravle57d0acc2017-07-11 17:41:30 -070052 const std::string& classpath) {
Calin Juravle87e2cb62017-06-13 21:48:45 -070053 VerifyClassLoaderInfo(
54 context, index, ClassLoaderContext::kPathClassLoader, classpath);
55 }
56
57 void VerifyClassLoaderDLC(ClassLoaderContext* context,
58 size_t index,
Calin Juravle57d0acc2017-07-11 17:41:30 -070059 const std::string& classpath) {
Calin Juravle87e2cb62017-06-13 21:48:45 -070060 VerifyClassLoaderInfo(
61 context, index, ClassLoaderContext::kDelegateLastClassLoader, classpath);
62 }
63
Calin Juravle57d0acc2017-07-11 17:41:30 -070064 void VerifyClassLoaderPCLFromTestDex(ClassLoaderContext* context,
65 size_t index,
66 const std::string& test_name) {
67 VerifyClassLoaderFromTestDex(
68 context, index, ClassLoaderContext::kPathClassLoader, test_name);
69 }
70
71 void VerifyClassLoaderDLCFromTestDex(ClassLoaderContext* context,
72 size_t index,
73 const std::string& test_name) {
74 VerifyClassLoaderFromTestDex(
75 context, index, ClassLoaderContext::kDelegateLastClassLoader, test_name);
76 }
77
Andreas Gampe72527382017-09-02 16:53:03 -070078 enum class LocationCheck {
79 kEquals,
80 kEndsWith
81 };
82 enum class BaseLocationCheck {
83 kEquals,
84 kEndsWith
85 };
86
Calin Juravle87e2cb62017-06-13 21:48:45 -070087 void VerifyOpenDexFiles(
88 ClassLoaderContext* context,
89 size_t index,
Andreas Gampe72527382017-09-02 16:53:03 -070090 std::vector<std::vector<std::unique_ptr<const DexFile>>*>& all_dex_files,
91 LocationCheck mode = LocationCheck::kEquals,
92 BaseLocationCheck base_mode = BaseLocationCheck::kEquals) {
Calin Juravle87e2cb62017-06-13 21:48:45 -070093 ASSERT_TRUE(context != nullptr);
94 ASSERT_TRUE(context->dex_files_open_attempted_);
95 ASSERT_TRUE(context->dex_files_open_result_);
96 ClassLoaderContext::ClassLoaderInfo& info = context->class_loader_chain_[index];
97 ASSERT_EQ(all_dex_files.size(), info.classpath.size());
98 size_t cur_open_dex_index = 0;
99 for (size_t k = 0; k < all_dex_files.size(); k++) {
100 std::vector<std::unique_ptr<const DexFile>>& dex_files_for_cp_elem = *(all_dex_files[k]);
101 for (size_t i = 0; i < dex_files_for_cp_elem.size(); i++) {
102 ASSERT_LT(cur_open_dex_index, info.opened_dex_files.size());
103
104 std::unique_ptr<const DexFile>& opened_dex_file =
105 info.opened_dex_files[cur_open_dex_index++];
106 std::unique_ptr<const DexFile>& expected_dex_file = dex_files_for_cp_elem[i];
107
Calin Juravle821a2592017-08-11 14:33:38 -0700108 std::string expected_location = expected_dex_file->GetBaseLocation();
109 UniqueCPtr<const char[]> expected_real_location(
110 realpath(expected_location.c_str(), nullptr));
111 ASSERT_TRUE(expected_real_location != nullptr) << expected_location;
112 expected_location.assign(expected_real_location.get());
113 expected_location += DexFile::GetMultiDexSuffix(expected_dex_file->GetLocation());
114
Andreas Gampe72527382017-09-02 16:53:03 -0700115 switch (mode) {
116 case LocationCheck::kEquals:
117 ASSERT_EQ(expected_dex_file->GetLocation(), opened_dex_file->GetLocation());
118 break;
119 case LocationCheck::kEndsWith:
120 ASSERT_TRUE(android::base::EndsWith(expected_dex_file->GetLocation(),
121 opened_dex_file->GetLocation().c_str()))
122 << opened_dex_file->GetLocation() << " vs " << expected_dex_file->GetLocation();
123 break;
124 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700125 ASSERT_EQ(expected_dex_file->GetLocationChecksum(), opened_dex_file->GetLocationChecksum());
Calin Juravle821a2592017-08-11 14:33:38 -0700126
127 std::string class_path_location = info.classpath[k];
128 UniqueCPtr<const char[]> class_path_location_real(
129 realpath(class_path_location.c_str(), nullptr));
130 ASSERT_TRUE(class_path_location_real != nullptr);
131 class_path_location.assign(class_path_location_real.get());
Andreas Gampe72527382017-09-02 16:53:03 -0700132 switch (base_mode) {
133 case BaseLocationCheck::kEquals:
134 ASSERT_EQ(class_path_location, opened_dex_file->GetBaseLocation());
135 break;
136
137 case BaseLocationCheck::kEndsWith:
138 ASSERT_TRUE(android::base::EndsWith(opened_dex_file->GetBaseLocation(),
139 class_path_location.c_str()))
140 << info.classpath[k] << " vs " << opened_dex_file->GetBaseLocation();
141 break;
142 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700143 }
144 }
145 }
146
Calin Juravle57d0acc2017-07-11 17:41:30 -0700147 std::unique_ptr<ClassLoaderContext> CreateContextForClassLoader(jobject class_loader) {
148 return ClassLoaderContext::CreateContextForClassLoader(class_loader, nullptr);
149 }
150
Calin Juravle3f918642017-07-11 19:04:20 -0700151 std::unique_ptr<ClassLoaderContext> ParseContextWithChecksums(const std::string& context_spec) {
152 std::unique_ptr<ClassLoaderContext> context(new ClassLoaderContext());
153 if (!context->Parse(context_spec, /*parse_checksums*/ true)) {
154 return nullptr;
155 }
156 return context;
157 }
158
Calin Juravle57d0acc2017-07-11 17:41:30 -0700159 void VerifyContextForClassLoader(ClassLoaderContext* context) {
160 ASSERT_TRUE(context != nullptr);
161 ASSERT_TRUE(context->dex_files_open_attempted_);
162 ASSERT_TRUE(context->dex_files_open_result_);
163 ASSERT_FALSE(context->owns_the_dex_files_);
164 ASSERT_FALSE(context->special_shared_library_);
165 }
166
Calin Juravlec79470d2017-07-12 17:37:42 -0700167 void VerifyClassLoaderDexFiles(ScopedObjectAccess& soa,
168 Handle<mirror::ClassLoader> class_loader,
169 jclass type,
170 std::vector<const DexFile*>& expected_dex_files)
171 REQUIRES_SHARED(Locks::mutator_lock_) {
172 ASSERT_TRUE(class_loader->GetClass() == soa.Decode<mirror::Class>(type));
173
174 std::vector<const DexFile*> class_loader_dex_files = GetDexFiles(soa, class_loader);
175 ASSERT_EQ(expected_dex_files.size(), class_loader_dex_files.size());
176
177 for (size_t i = 0; i < expected_dex_files.size(); i++) {
178 ASSERT_EQ(expected_dex_files[i]->GetLocation(),
179 class_loader_dex_files[i]->GetLocation());
180 ASSERT_EQ(expected_dex_files[i]->GetLocationChecksum(),
181 class_loader_dex_files[i]->GetLocationChecksum());
182 }
183 }
184
Calin Juravle87e2cb62017-06-13 21:48:45 -0700185 private:
186 void VerifyClassLoaderInfo(ClassLoaderContext* context,
187 size_t index,
188 ClassLoaderContext::ClassLoaderType type,
Calin Juravle57d0acc2017-07-11 17:41:30 -0700189 const std::string& classpath) {
Calin Juravle87e2cb62017-06-13 21:48:45 -0700190 ASSERT_TRUE(context != nullptr);
191 ASSERT_GT(context->class_loader_chain_.size(), index);
192 ClassLoaderContext::ClassLoaderInfo& info = context->class_loader_chain_[index];
193 ASSERT_EQ(type, info.type);
194 std::vector<std::string> expected_classpath;
195 Split(classpath, ':', &expected_classpath);
196 ASSERT_EQ(expected_classpath, info.classpath);
197 }
Calin Juravle57d0acc2017-07-11 17:41:30 -0700198
199 void VerifyClassLoaderFromTestDex(ClassLoaderContext* context,
200 size_t index,
201 ClassLoaderContext::ClassLoaderType type,
202 const std::string& test_name) {
203 std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles(test_name.c_str());
204 std::vector<std::vector<std::unique_ptr<const DexFile>>*> all_dex_files;
205 all_dex_files.push_back(&dex_files);
206
207 VerifyClassLoaderInfo(context, index, type, GetTestDexFileName(test_name.c_str()));
208 VerifyOpenDexFiles(context, index, all_dex_files);
209 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700210};
211
Calin Juravle1a509c82017-07-24 16:51:21 -0700212TEST_F(ClassLoaderContextTest, ParseValidEmptyContext) {
213 std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::Create("");
214 // An empty context should create a single empty PathClassLoader.
215 VerifyContextSize(context.get(), 1);
216 VerifyClassLoaderPCL(context.get(), 0, "");
217}
218
219TEST_F(ClassLoaderContextTest, ParseValidSharedLibraryContext) {
220 std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::Create("&");
221 // An shared library context should have no class loader in the chain.
222 VerifyContextSize(context.get(), 0);
223}
224
Calin Juravle87e2cb62017-06-13 21:48:45 -0700225TEST_F(ClassLoaderContextTest, ParseValidContextPCL) {
226 std::unique_ptr<ClassLoaderContext> context =
227 ClassLoaderContext::Create("PCL[a.dex]");
228 VerifyContextSize(context.get(), 1);
229 VerifyClassLoaderPCL(context.get(), 0, "a.dex");
230}
231
232TEST_F(ClassLoaderContextTest, ParseValidContextDLC) {
233 std::unique_ptr<ClassLoaderContext> context =
234 ClassLoaderContext::Create("DLC[a.dex]");
235 VerifyContextSize(context.get(), 1);
236 VerifyClassLoaderDLC(context.get(), 0, "a.dex");
237}
238
239TEST_F(ClassLoaderContextTest, ParseValidContextChain) {
240 std::unique_ptr<ClassLoaderContext> context =
241 ClassLoaderContext::Create("PCL[a.dex:b.dex];DLC[c.dex:d.dex];PCL[e.dex]");
242 VerifyContextSize(context.get(), 3);
243 VerifyClassLoaderPCL(context.get(), 0, "a.dex:b.dex");
244 VerifyClassLoaderDLC(context.get(), 1, "c.dex:d.dex");
245 VerifyClassLoaderPCL(context.get(), 2, "e.dex");
246}
247
248TEST_F(ClassLoaderContextTest, ParseValidEmptyContextDLC) {
249 std::unique_ptr<ClassLoaderContext> context =
250 ClassLoaderContext::Create("DLC[]");
251 VerifyContextSize(context.get(), 1);
252 VerifyClassLoaderDLC(context.get(), 0, "");
253}
254
255TEST_F(ClassLoaderContextTest, ParseValidContextSpecialSymbol) {
256 std::unique_ptr<ClassLoaderContext> context =
257 ClassLoaderContext::Create(OatFile::kSpecialSharedLibrary);
258 VerifyContextSize(context.get(), 0);
259}
260
261TEST_F(ClassLoaderContextTest, ParseInvalidValidContexts) {
262 ASSERT_TRUE(nullptr == ClassLoaderContext::Create("ABC[a.dex]"));
263 ASSERT_TRUE(nullptr == ClassLoaderContext::Create("PCL"));
264 ASSERT_TRUE(nullptr == ClassLoaderContext::Create("PCL[a.dex"));
265 ASSERT_TRUE(nullptr == ClassLoaderContext::Create("PCLa.dex]"));
266 ASSERT_TRUE(nullptr == ClassLoaderContext::Create("PCL{a.dex}"));
267 ASSERT_TRUE(nullptr == ClassLoaderContext::Create("PCL[a.dex];DLC[b.dex"));
268}
269
270TEST_F(ClassLoaderContextTest, OpenInvalidDexFiles) {
271 std::unique_ptr<ClassLoaderContext> context =
272 ClassLoaderContext::Create("PCL[does_not_exist.dex]");
273 VerifyContextSize(context.get(), 1);
274 ASSERT_FALSE(context->OpenDexFiles(InstructionSet::kArm, "."));
275}
276
277TEST_F(ClassLoaderContextTest, OpenValidDexFiles) {
278 std::string multidex_name = GetTestDexFileName("MultiDex");
279 std::vector<std::unique_ptr<const DexFile>> multidex_files = OpenTestDexFiles("MultiDex");
280 std::string myclass_dex_name = GetTestDexFileName("MyClass");
281 std::vector<std::unique_ptr<const DexFile>> myclass_dex_files = OpenTestDexFiles("MyClass");
282 std::string dex_name = GetTestDexFileName("Main");
283 std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("Main");
284
Calin Juravle87e2cb62017-06-13 21:48:45 -0700285 std::unique_ptr<ClassLoaderContext> context =
286 ClassLoaderContext::Create(
287 "PCL[" + multidex_name + ":" + myclass_dex_name + "];" +
288 "DLC[" + dex_name + "]");
289
290 ASSERT_TRUE(context->OpenDexFiles(InstructionSet::kArm, /*classpath_dir*/ ""));
291
292 VerifyContextSize(context.get(), 2);
293 std::vector<std::vector<std::unique_ptr<const DexFile>>*> all_dex_files0;
294 all_dex_files0.push_back(&multidex_files);
295 all_dex_files0.push_back(&myclass_dex_files);
296 std::vector<std::vector<std::unique_ptr<const DexFile>>*> all_dex_files1;
297 all_dex_files1.push_back(&dex_files);
298
299 VerifyOpenDexFiles(context.get(), 0, all_dex_files0);
300 VerifyOpenDexFiles(context.get(), 1, all_dex_files1);
301}
302
Calin Juravle821a2592017-08-11 14:33:38 -0700303class ScratchSymLink {
304 public:
305 explicit ScratchSymLink(const std::string& file) {
306 // Use a temporary scratch file to get a unique name for the link.
307 ScratchFile scratchFile;
308 scratch_link_name_ = scratchFile.GetFilename() + ".link.jar";
309 CHECK_EQ(0, symlink(file.c_str(), scratch_link_name_.c_str()));
310 }
311
312 ~ScratchSymLink() {
313 CHECK_EQ(0, unlink(scratch_link_name_.c_str()));
314 }
315
316 const std::string& GetFilename() { return scratch_link_name_; }
317
318 private:
319 std::string scratch_link_name_;
320};
321
322TEST_F(ClassLoaderContextTest, OpenValidDexFilesSymLink) {
323 std::string myclass_dex_name = GetTestDexFileName("MyClass");
324 // Now replace the dex location with a symlink.
325 ScratchSymLink link(myclass_dex_name);
326
327 std::unique_ptr<ClassLoaderContext> context =
328 ClassLoaderContext::Create("PCL[" + link.GetFilename() + "]");
329
330 ASSERT_TRUE(context->OpenDexFiles(InstructionSet::kArm, /*classpath_dir*/ ""));
331
332 VerifyContextSize(context.get(), 1);
333 std::vector<std::vector<std::unique_ptr<const DexFile>>*> all_dex_files0;
334 std::vector<std::unique_ptr<const DexFile>> myclass_dex_files = OpenTestDexFiles("MyClass");
335 all_dex_files0.push_back(&myclass_dex_files);
336
337 VerifyOpenDexFiles(context.get(), 0, all_dex_files0);
338}
339
Andreas Gampe72527382017-09-02 16:53:03 -0700340static std::string CreateRelativeString(const std::string& in, const char* cwd) {
341 if (!android::base::StartsWith(in, cwd)) {
342 LOG(FATAL) << in << " " << cwd;
343 }
344 return in.substr(strlen(cwd) + 1);
345}
346
347TEST_F(ClassLoaderContextTest, OpenValidDexFilesRelative) {
348 char cwd_buf[4096];
349 if (getcwd(cwd_buf, arraysize(cwd_buf)) == nullptr) {
350 PLOG(FATAL) << "Could not get working directory";
351 }
352 std::string multidex_name = CreateRelativeString(GetTestDexFileName("MultiDex"), cwd_buf);
353 std::vector<std::unique_ptr<const DexFile>> multidex_files = OpenTestDexFiles("MultiDex");
354 std::string myclass_dex_name = CreateRelativeString(GetTestDexFileName("MyClass"), cwd_buf);
355 std::vector<std::unique_ptr<const DexFile>> myclass_dex_files = OpenTestDexFiles("MyClass");
356 std::string dex_name = CreateRelativeString(GetTestDexFileName("Main"), cwd_buf);
357 std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("Main");
358
359
360 std::unique_ptr<ClassLoaderContext> context =
361 ClassLoaderContext::Create(
362 "PCL[" + multidex_name + ":" + myclass_dex_name + "];" +
363 "DLC[" + dex_name + "]");
364
365 ASSERT_TRUE(context->OpenDexFiles(InstructionSet::kArm, /*classpath_dir*/ ""));
366
367 VerifyContextSize(context.get(), 2);
368 std::vector<std::vector<std::unique_ptr<const DexFile>>*> all_dex_files0;
369 all_dex_files0.push_back(&multidex_files);
370 all_dex_files0.push_back(&myclass_dex_files);
371 std::vector<std::vector<std::unique_ptr<const DexFile>>*> all_dex_files1;
372 all_dex_files1.push_back(&dex_files);
373
374 VerifyOpenDexFiles(context.get(), 0, all_dex_files0, LocationCheck::kEndsWith);
375 VerifyOpenDexFiles(context.get(), 1, all_dex_files1, LocationCheck::kEndsWith);
376}
377
378TEST_F(ClassLoaderContextTest, OpenValidDexFilesClasspathDir) {
379 char cwd_buf[4096];
380 if (getcwd(cwd_buf, arraysize(cwd_buf)) == nullptr) {
381 PLOG(FATAL) << "Could not get working directory";
382 }
383 std::string multidex_name = CreateRelativeString(GetTestDexFileName("MultiDex"), cwd_buf);
384 std::vector<std::unique_ptr<const DexFile>> multidex_files = OpenTestDexFiles("MultiDex");
385 std::string myclass_dex_name = CreateRelativeString(GetTestDexFileName("MyClass"), cwd_buf);
386 std::vector<std::unique_ptr<const DexFile>> myclass_dex_files = OpenTestDexFiles("MyClass");
387 std::string dex_name = CreateRelativeString(GetTestDexFileName("Main"), cwd_buf);
388 std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("Main");
389
390
391 std::unique_ptr<ClassLoaderContext> context =
392 ClassLoaderContext::Create(
393 "PCL[" + multidex_name + ":" + myclass_dex_name + "];" +
394 "DLC[" + dex_name + "]");
395
396 ASSERT_TRUE(context->OpenDexFiles(InstructionSet::kArm, cwd_buf));
397
398 VerifyContextSize(context.get(), 2);
399 std::vector<std::vector<std::unique_ptr<const DexFile>>*> all_dex_files0;
400 all_dex_files0.push_back(&multidex_files);
401 all_dex_files0.push_back(&myclass_dex_files);
402 std::vector<std::vector<std::unique_ptr<const DexFile>>*> all_dex_files1;
403 all_dex_files1.push_back(&dex_files);
404
405 VerifyOpenDexFiles(
406 context.get(), 0, all_dex_files0, LocationCheck::kEquals, BaseLocationCheck::kEndsWith);
407 VerifyOpenDexFiles(
408 context.get(), 1, all_dex_files1, LocationCheck::kEquals, BaseLocationCheck::kEndsWith);
409}
410
Calin Juravle87e2cb62017-06-13 21:48:45 -0700411TEST_F(ClassLoaderContextTest, OpenInvalidDexFilesMix) {
412 std::string dex_name = GetTestDexFileName("Main");
413 std::unique_ptr<ClassLoaderContext> context =
414 ClassLoaderContext::Create("PCL[does_not_exist.dex];DLC[" + dex_name + "]");
415 ASSERT_FALSE(context->OpenDexFiles(InstructionSet::kArm, ""));
416}
417
418TEST_F(ClassLoaderContextTest, CreateClassLoader) {
419 std::string dex_name = GetTestDexFileName("Main");
420 std::unique_ptr<ClassLoaderContext> context =
421 ClassLoaderContext::Create("PCL[" + dex_name + "]");
422 ASSERT_TRUE(context->OpenDexFiles(InstructionSet::kArm, ""));
423
424 std::vector<std::unique_ptr<const DexFile>> classpath_dex = OpenTestDexFiles("Main");
425 std::vector<std::unique_ptr<const DexFile>> compilation_sources = OpenTestDexFiles("MultiDex");
426
427 std::vector<const DexFile*> compilation_sources_raw =
428 MakeNonOwningPointerVector(compilation_sources);
429 jobject jclass_loader = context->CreateClassLoader(compilation_sources_raw);
430 ASSERT_TRUE(jclass_loader != nullptr);
431
432 ScopedObjectAccess soa(Thread::Current());
433
Calin Juravlec79470d2017-07-12 17:37:42 -0700434 StackHandleScope<1> hs(soa.Self());
Calin Juravle87e2cb62017-06-13 21:48:45 -0700435 Handle<mirror::ClassLoader> class_loader = hs.NewHandle(
436 soa.Decode<mirror::ClassLoader>(jclass_loader));
437
438 ASSERT_TRUE(class_loader->GetClass() ==
439 soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_PathClassLoader));
440 ASSERT_TRUE(class_loader->GetParent()->GetClass() ==
441 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_BootClassLoader));
442
Calin Juravlec79470d2017-07-12 17:37:42 -0700443 // For the first class loader the class path dex files must come first and then the
444 // compilation sources.
445 std::vector<const DexFile*> expected_classpath = MakeNonOwningPointerVector(classpath_dex);
446 for (auto& dex : compilation_sources_raw) {
447 expected_classpath.push_back(dex);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700448 }
449
Calin Juravlec79470d2017-07-12 17:37:42 -0700450 VerifyClassLoaderDexFiles(soa,
451 class_loader,
452 WellKnownClasses::dalvik_system_PathClassLoader,
453 expected_classpath);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700454}
455
Calin Juravle7b0648a2017-07-07 18:40:50 -0700456TEST_F(ClassLoaderContextTest, CreateClassLoaderWithEmptyContext) {
457 std::unique_ptr<ClassLoaderContext> context =
458 ClassLoaderContext::Create("");
459 ASSERT_TRUE(context->OpenDexFiles(InstructionSet::kArm, ""));
460
461 std::vector<std::unique_ptr<const DexFile>> compilation_sources = OpenTestDexFiles("MultiDex");
462
463 std::vector<const DexFile*> compilation_sources_raw =
464 MakeNonOwningPointerVector(compilation_sources);
465 jobject jclass_loader = context->CreateClassLoader(compilation_sources_raw);
466 ASSERT_TRUE(jclass_loader != nullptr);
467
468 ScopedObjectAccess soa(Thread::Current());
469
Calin Juravlec79470d2017-07-12 17:37:42 -0700470 StackHandleScope<1> hs(soa.Self());
Calin Juravle7b0648a2017-07-07 18:40:50 -0700471 Handle<mirror::ClassLoader> class_loader = hs.NewHandle(
472 soa.Decode<mirror::ClassLoader>(jclass_loader));
473
Calin Juravlec79470d2017-07-12 17:37:42 -0700474 // An empty context should create a single PathClassLoader with only the compilation sources.
475 VerifyClassLoaderDexFiles(soa,
476 class_loader,
477 WellKnownClasses::dalvik_system_PathClassLoader,
478 compilation_sources_raw);
Calin Juravle7b0648a2017-07-07 18:40:50 -0700479 ASSERT_TRUE(class_loader->GetParent()->GetClass() ==
480 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_BootClassLoader));
Calin Juravle7b0648a2017-07-07 18:40:50 -0700481}
482
Calin Juravle1a509c82017-07-24 16:51:21 -0700483TEST_F(ClassLoaderContextTest, CreateClassLoaderWithSharedLibraryContext) {
484 std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::Create("&");
485
486 ASSERT_TRUE(context->OpenDexFiles(InstructionSet::kArm, ""));
487
488 std::vector<std::unique_ptr<const DexFile>> compilation_sources = OpenTestDexFiles("MultiDex");
489
490 std::vector<const DexFile*> compilation_sources_raw =
491 MakeNonOwningPointerVector(compilation_sources);
492 jobject jclass_loader = context->CreateClassLoader(compilation_sources_raw);
493 ASSERT_TRUE(jclass_loader != nullptr);
494
495 ScopedObjectAccess soa(Thread::Current());
496
497 StackHandleScope<1> hs(soa.Self());
498 Handle<mirror::ClassLoader> class_loader = hs.NewHandle(
499 soa.Decode<mirror::ClassLoader>(jclass_loader));
500
501 // A shared library context should create a single PathClassLoader with only the compilation
502 // sources.
503 VerifyClassLoaderDexFiles(soa,
504 class_loader,
505 WellKnownClasses::dalvik_system_PathClassLoader,
506 compilation_sources_raw);
507 ASSERT_TRUE(class_loader->GetParent()->GetClass() ==
508 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_BootClassLoader));
509}
510
Calin Juravlec79470d2017-07-12 17:37:42 -0700511TEST_F(ClassLoaderContextTest, CreateClassLoaderWithComplexChain) {
512 // Setup the context.
513 std::vector<std::unique_ptr<const DexFile>> classpath_dex_a = OpenTestDexFiles("ForClassLoaderA");
514 std::vector<std::unique_ptr<const DexFile>> classpath_dex_b = OpenTestDexFiles("ForClassLoaderB");
515 std::vector<std::unique_ptr<const DexFile>> classpath_dex_c = OpenTestDexFiles("ForClassLoaderC");
516 std::vector<std::unique_ptr<const DexFile>> classpath_dex_d = OpenTestDexFiles("ForClassLoaderD");
517
518 std::string context_spec =
519 "PCL[" + CreateClassPath(classpath_dex_a) + ":" + CreateClassPath(classpath_dex_b) + "];" +
520 "DLC[" + CreateClassPath(classpath_dex_c) + "];" +
521 "PCL[" + CreateClassPath(classpath_dex_d) + "]";
522
523 std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::Create(context_spec);
524 ASSERT_TRUE(context->OpenDexFiles(InstructionSet::kArm, ""));
525
526 // Setup the compilation sources.
527 std::vector<std::unique_ptr<const DexFile>> compilation_sources = OpenTestDexFiles("MultiDex");
528 std::vector<const DexFile*> compilation_sources_raw =
529 MakeNonOwningPointerVector(compilation_sources);
530
531 // Create the class loader.
532 jobject jclass_loader = context->CreateClassLoader(compilation_sources_raw);
533 ASSERT_TRUE(jclass_loader != nullptr);
534
535 // Verify the class loader.
536 ScopedObjectAccess soa(Thread::Current());
537
538 StackHandleScope<3> hs(soa.Self());
539 Handle<mirror::ClassLoader> class_loader_1 = hs.NewHandle(
540 soa.Decode<mirror::ClassLoader>(jclass_loader));
541
542 // Verify the first class loader
543
544 // For the first class loader the class path dex files must come first and then the
545 // compilation sources.
546 std::vector<const DexFile*> class_loader_1_dex_files =
547 MakeNonOwningPointerVector(classpath_dex_a);
548 for (auto& dex : classpath_dex_b) {
549 class_loader_1_dex_files.push_back(dex.get());
550 }
551 for (auto& dex : compilation_sources_raw) {
552 class_loader_1_dex_files.push_back(dex);
553 }
554 VerifyClassLoaderDexFiles(soa,
555 class_loader_1,
556 WellKnownClasses::dalvik_system_PathClassLoader,
557 class_loader_1_dex_files);
558
559 // Verify the second class loader
560 Handle<mirror::ClassLoader> class_loader_2 = hs.NewHandle(class_loader_1->GetParent());
561 std::vector<const DexFile*> class_loader_2_dex_files =
562 MakeNonOwningPointerVector(classpath_dex_c);
563 VerifyClassLoaderDexFiles(soa,
564 class_loader_2,
565 WellKnownClasses::dalvik_system_DelegateLastClassLoader,
566 class_loader_2_dex_files);
567
568 // Verify the third class loader
569 Handle<mirror::ClassLoader> class_loader_3 = hs.NewHandle(class_loader_2->GetParent());
570 std::vector<const DexFile*> class_loader_3_dex_files =
571 MakeNonOwningPointerVector(classpath_dex_d);
572 VerifyClassLoaderDexFiles(soa,
573 class_loader_3,
574 WellKnownClasses::dalvik_system_PathClassLoader,
575 class_loader_3_dex_files);
576 // The last class loader should have the BootClassLoader as a parent.
577 ASSERT_TRUE(class_loader_3->GetParent()->GetClass() ==
578 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_BootClassLoader));
579}
580
581
Calin Juravle87e2cb62017-06-13 21:48:45 -0700582TEST_F(ClassLoaderContextTest, RemoveSourceLocations) {
583 std::unique_ptr<ClassLoaderContext> context =
584 ClassLoaderContext::Create("PCL[a.dex]");
585 dchecked_vector<std::string> classpath_dex;
586 classpath_dex.push_back("a.dex");
587 dchecked_vector<std::string> compilation_sources;
588 compilation_sources.push_back("src.dex");
589
590 // Nothing should be removed.
591 ASSERT_FALSE(context->RemoveLocationsFromClassPaths(compilation_sources));
592 VerifyClassLoaderPCL(context.get(), 0, "a.dex");
593 // Classes should be removed.
594 ASSERT_TRUE(context->RemoveLocationsFromClassPaths(classpath_dex));
595 VerifyClassLoaderPCL(context.get(), 0, "");
596}
597
598TEST_F(ClassLoaderContextTest, EncodeInOatFile) {
599 std::string dex1_name = GetTestDexFileName("Main");
600 std::string dex2_name = GetTestDexFileName("MyClass");
601 std::unique_ptr<ClassLoaderContext> context =
602 ClassLoaderContext::Create("PCL[" + dex1_name + ":" + dex2_name + "]");
603 ASSERT_TRUE(context->OpenDexFiles(InstructionSet::kArm, ""));
604
605 std::vector<std::unique_ptr<const DexFile>> dex1 = OpenTestDexFiles("Main");
606 std::vector<std::unique_ptr<const DexFile>> dex2 = OpenTestDexFiles("MyClass");
607 std::string encoding = context->EncodeContextForOatFile("");
Calin Juravlec79470d2017-07-12 17:37:42 -0700608 std::string expected_encoding = "PCL[" + CreateClassPathWithChecksums(dex1) + ":" +
609 CreateClassPathWithChecksums(dex2) + "]";
Calin Juravle87e2cb62017-06-13 21:48:45 -0700610 ASSERT_EQ(expected_encoding, context->EncodeContextForOatFile(""));
611}
612
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700613TEST_F(ClassLoaderContextTest, EncodeForDex2oat) {
614 std::string dex1_name = GetTestDexFileName("Main");
615 std::string dex2_name = GetTestDexFileName("MultiDex");
616 std::unique_ptr<ClassLoaderContext> context =
617 ClassLoaderContext::Create("PCL[" + dex1_name + ":" + dex2_name + "]");
618 ASSERT_TRUE(context->OpenDexFiles(InstructionSet::kArm, ""));
619
620 std::vector<std::unique_ptr<const DexFile>> dex1 = OpenTestDexFiles("Main");
621 std::vector<std::unique_ptr<const DexFile>> dex2 = OpenTestDexFiles("MultiDex");
622 std::string encoding = context->EncodeContextForDex2oat("");
623 std::string expected_encoding = "PCL[" + dex1_name + ":" + dex2_name + "]";
624 ASSERT_EQ(expected_encoding, context->EncodeContextForDex2oat(""));
625}
626
Calin Juravle57d0acc2017-07-11 17:41:30 -0700627// TODO(calin) add a test which creates the context for a class loader together with dex_elements.
628TEST_F(ClassLoaderContextTest, CreateContextForClassLoader) {
629 // The chain is
630 // ClassLoaderA (PathClassLoader)
631 // ^
632 // |
633 // ClassLoaderB (DelegateLastClassLoader)
634 // ^
635 // |
636 // ClassLoaderC (PathClassLoader)
637 // ^
638 // |
639 // ClassLoaderD (DelegateLastClassLoader)
640
641 jobject class_loader_a = LoadDexInPathClassLoader("ForClassLoaderA", nullptr);
642 jobject class_loader_b = LoadDexInDelegateLastClassLoader("ForClassLoaderB", class_loader_a);
643 jobject class_loader_c = LoadDexInPathClassLoader("ForClassLoaderC", class_loader_b);
644 jobject class_loader_d = LoadDexInDelegateLastClassLoader("ForClassLoaderD", class_loader_c);
645
646 std::unique_ptr<ClassLoaderContext> context = CreateContextForClassLoader(class_loader_d);
647
648 VerifyContextForClassLoader(context.get());
649 VerifyContextSize(context.get(), 4);
650
651 VerifyClassLoaderDLCFromTestDex(context.get(), 0, "ForClassLoaderD");
652 VerifyClassLoaderPCLFromTestDex(context.get(), 1, "ForClassLoaderC");
653 VerifyClassLoaderDLCFromTestDex(context.get(), 2, "ForClassLoaderB");
654 VerifyClassLoaderPCLFromTestDex(context.get(), 3, "ForClassLoaderA");
655}
656
Calin Juravle3f918642017-07-11 19:04:20 -0700657TEST_F(ClassLoaderContextTest, VerifyClassLoaderContextMatch) {
658 std::string context_spec = "PCL[a.dex*123:b.dex*456];DLC[c.dex*890]";
659 std::unique_ptr<ClassLoaderContext> context = ParseContextWithChecksums(context_spec);
660
661 VerifyContextSize(context.get(), 2);
662 VerifyClassLoaderPCL(context.get(), 0, "a.dex:b.dex");
663 VerifyClassLoaderDLC(context.get(), 1, "c.dex");
664
665 ASSERT_TRUE(context->VerifyClassLoaderContextMatch(context_spec));
666
667 std::string wrong_class_loader_type = "PCL[a.dex*123:b.dex*456];PCL[c.dex*890]";
668 ASSERT_FALSE(context->VerifyClassLoaderContextMatch(wrong_class_loader_type));
669
670 std::string wrong_class_loader_order = "DLC[c.dex*890];PCL[a.dex*123:b.dex*456]";
671 ASSERT_FALSE(context->VerifyClassLoaderContextMatch(wrong_class_loader_order));
672
673 std::string wrong_classpath_order = "PCL[b.dex*456:a.dex*123];DLC[c.dex*890]";
674 ASSERT_FALSE(context->VerifyClassLoaderContextMatch(wrong_classpath_order));
675
676 std::string wrong_checksum = "PCL[a.dex*999:b.dex*456];DLC[c.dex*890]";
677 ASSERT_FALSE(context->VerifyClassLoaderContextMatch(wrong_checksum));
678
679 std::string wrong_extra_class_loader = "PCL[a.dex*123:b.dex*456];DLC[c.dex*890];PCL[d.dex*321]";
680 ASSERT_FALSE(context->VerifyClassLoaderContextMatch(wrong_extra_class_loader));
681
682 std::string wrong_extra_classpath = "PCL[a.dex*123:b.dex*456];DLC[c.dex*890:d.dex*321]";
683 ASSERT_FALSE(context->VerifyClassLoaderContextMatch(wrong_extra_classpath));
684
685 std::string wrong_spec = "PCL[a.dex*999:b.dex*456];DLC[";
686 ASSERT_FALSE(context->VerifyClassLoaderContextMatch(wrong_spec));
687}
688
689TEST_F(ClassLoaderContextTest, VerifyClassLoaderContextMatchAfterEncoding) {
690 jobject class_loader_a = LoadDexInPathClassLoader("ForClassLoaderA", nullptr);
691 jobject class_loader_b = LoadDexInDelegateLastClassLoader("ForClassLoaderB", class_loader_a);
692 jobject class_loader_c = LoadDexInPathClassLoader("ForClassLoaderC", class_loader_b);
693 jobject class_loader_d = LoadDexInDelegateLastClassLoader("ForClassLoaderD", class_loader_c);
694
695 std::unique_ptr<ClassLoaderContext> context = CreateContextForClassLoader(class_loader_d);
696
697 ASSERT_TRUE(context->VerifyClassLoaderContextMatch(context->EncodeContextForOatFile("")));
698}
699
Calin Juravlea308a322017-07-18 16:51:51 -0700700TEST_F(ClassLoaderContextTest, VerifyClassLoaderContextMatchAfterEncodingMultidex) {
701 jobject class_loader = LoadDexInPathClassLoader("MultiDex", nullptr);
702
703 std::unique_ptr<ClassLoaderContext> context = CreateContextForClassLoader(class_loader);
704
705 ASSERT_TRUE(context->VerifyClassLoaderContextMatch(context->EncodeContextForOatFile("")));
706}
707
Calin Juravle87e2cb62017-06-13 21:48:45 -0700708} // namespace art