blob: fc3446c3f92c6fdb67008065b73a474578503274 [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>
20
Andreas Gampe72527382017-09-02 16:53:03 -070021#include "android-base/strings.h"
Calin Juravle87e2cb62017-06-13 21:48:45 -070022#include "base/dchecked_vector.h"
23#include "base/stl_util.h"
24#include "class_linker.h"
Calin Juravle821a2592017-08-11 14:33:38 -070025#include "common_runtime_test.h"
Calin Juravle87e2cb62017-06-13 21:48:45 -070026#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
Andreas Gampe72527382017-09-02 16:53:03 -070074 enum class LocationCheck {
75 kEquals,
76 kEndsWith
77 };
78 enum class BaseLocationCheck {
79 kEquals,
80 kEndsWith
81 };
82
Calin Juravle92003fe2017-09-06 02:22:57 +000083 static bool IsAbsoluteLocation(const std::string& location) {
84 return !location.empty() && location[0] == '/';
85 }
86
Calin Juravle87e2cb62017-06-13 21:48:45 -070087 void VerifyOpenDexFiles(
88 ClassLoaderContext* context,
89 size_t index,
Calin Juravlec5b215f2017-09-12 14:49:37 -070090 std::vector<std::unique_ptr<const DexFile>>* all_dex_files) {
Calin Juravle87e2cb62017-06-13 21:48:45 -070091 ASSERT_TRUE(context != nullptr);
92 ASSERT_TRUE(context->dex_files_open_attempted_);
93 ASSERT_TRUE(context->dex_files_open_result_);
94 ClassLoaderContext::ClassLoaderInfo& info = context->class_loader_chain_[index];
Calin Juravlec5b215f2017-09-12 14:49:37 -070095 ASSERT_EQ(all_dex_files->size(), info.classpath.size());
96 ASSERT_EQ(all_dex_files->size(), info.opened_dex_files.size());
Calin Juravle87e2cb62017-06-13 21:48:45 -070097 size_t cur_open_dex_index = 0;
Calin Juravlec5b215f2017-09-12 14:49:37 -070098 for (size_t k = 0; k < all_dex_files->size(); k++) {
99 std::unique_ptr<const DexFile>& opened_dex_file =
Calin Juravle87e2cb62017-06-13 21:48:45 -0700100 info.opened_dex_files[cur_open_dex_index++];
Calin Juravlec5b215f2017-09-12 14:49:37 -0700101 std::unique_ptr<const DexFile>& expected_dex_file = (*all_dex_files)[k];
Calin Juravle87e2cb62017-06-13 21:48:45 -0700102
Calin Juravle92003fe2017-09-06 02:22:57 +0000103 std::string expected_location = expected_dex_file->GetLocation();
Calin Juravle821a2592017-08-11 14:33:38 -0700104
Calin Juravle92003fe2017-09-06 02:22:57 +0000105 const std::string& opened_location = opened_dex_file->GetLocation();
106 if (!IsAbsoluteLocation(opened_location)) {
107 // If the opened location is relative (it was open from a relative path without a
108 // classpath_dir) it might not match the expected location which is absolute in tests).
109 // So we compare the endings (the checksum will validate it's actually the same file).
110 ASSERT_EQ(0, expected_location.compare(
111 expected_location.length() - opened_location.length(),
112 opened_location.length(),
113 opened_location));
114 } else {
115 ASSERT_EQ(expected_location, opened_location);
116 }
Calin Juravlec5b215f2017-09-12 14:49:37 -0700117 ASSERT_EQ(expected_dex_file->GetLocationChecksum(), opened_dex_file->GetLocationChecksum());
Calin Juravle92003fe2017-09-06 02:22:57 +0000118 ASSERT_EQ(info.classpath[k], opened_location);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700119 }
120 }
121
Calin Juravle57d0acc2017-07-11 17:41:30 -0700122 std::unique_ptr<ClassLoaderContext> CreateContextForClassLoader(jobject class_loader) {
123 return ClassLoaderContext::CreateContextForClassLoader(class_loader, nullptr);
124 }
125
Calin Juravle3f918642017-07-11 19:04:20 -0700126 std::unique_ptr<ClassLoaderContext> ParseContextWithChecksums(const std::string& context_spec) {
127 std::unique_ptr<ClassLoaderContext> context(new ClassLoaderContext());
128 if (!context->Parse(context_spec, /*parse_checksums*/ true)) {
129 return nullptr;
130 }
131 return context;
132 }
133
Calin Juravle57d0acc2017-07-11 17:41:30 -0700134 void VerifyContextForClassLoader(ClassLoaderContext* context) {
135 ASSERT_TRUE(context != nullptr);
136 ASSERT_TRUE(context->dex_files_open_attempted_);
137 ASSERT_TRUE(context->dex_files_open_result_);
138 ASSERT_FALSE(context->owns_the_dex_files_);
139 ASSERT_FALSE(context->special_shared_library_);
140 }
141
Calin Juravlec79470d2017-07-12 17:37:42 -0700142 void VerifyClassLoaderDexFiles(ScopedObjectAccess& soa,
143 Handle<mirror::ClassLoader> class_loader,
144 jclass type,
145 std::vector<const DexFile*>& expected_dex_files)
146 REQUIRES_SHARED(Locks::mutator_lock_) {
147 ASSERT_TRUE(class_loader->GetClass() == soa.Decode<mirror::Class>(type));
148
149 std::vector<const DexFile*> class_loader_dex_files = GetDexFiles(soa, class_loader);
150 ASSERT_EQ(expected_dex_files.size(), class_loader_dex_files.size());
151
152 for (size_t i = 0; i < expected_dex_files.size(); i++) {
153 ASSERT_EQ(expected_dex_files[i]->GetLocation(),
154 class_loader_dex_files[i]->GetLocation());
155 ASSERT_EQ(expected_dex_files[i]->GetLocationChecksum(),
156 class_loader_dex_files[i]->GetLocationChecksum());
157 }
158 }
159
Calin Juravlec5b215f2017-09-12 14:49:37 -0700160 void PretendContextOpenedDexFiles(ClassLoaderContext* context) {
161 context->dex_files_open_attempted_ = true;
162 context->dex_files_open_result_ = true;
163 }
164
Calin Juravle87e2cb62017-06-13 21:48:45 -0700165 private:
166 void VerifyClassLoaderInfo(ClassLoaderContext* context,
167 size_t index,
168 ClassLoaderContext::ClassLoaderType type,
Calin Juravle57d0acc2017-07-11 17:41:30 -0700169 const std::string& classpath) {
Calin Juravle87e2cb62017-06-13 21:48:45 -0700170 ASSERT_TRUE(context != nullptr);
171 ASSERT_GT(context->class_loader_chain_.size(), index);
172 ClassLoaderContext::ClassLoaderInfo& info = context->class_loader_chain_[index];
173 ASSERT_EQ(type, info.type);
174 std::vector<std::string> expected_classpath;
175 Split(classpath, ':', &expected_classpath);
176 ASSERT_EQ(expected_classpath, info.classpath);
177 }
Calin Juravle57d0acc2017-07-11 17:41:30 -0700178
179 void VerifyClassLoaderFromTestDex(ClassLoaderContext* context,
180 size_t index,
181 ClassLoaderContext::ClassLoaderType type,
182 const std::string& test_name) {
183 std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles(test_name.c_str());
Calin Juravle57d0acc2017-07-11 17:41:30 -0700184
185 VerifyClassLoaderInfo(context, index, type, GetTestDexFileName(test_name.c_str()));
Calin Juravlec5b215f2017-09-12 14:49:37 -0700186 VerifyOpenDexFiles(context, index, &dex_files);
Calin Juravle57d0acc2017-07-11 17:41:30 -0700187 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700188};
189
Calin Juravle1a509c82017-07-24 16:51:21 -0700190TEST_F(ClassLoaderContextTest, ParseValidEmptyContext) {
191 std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::Create("");
192 // An empty context should create a single empty PathClassLoader.
193 VerifyContextSize(context.get(), 1);
194 VerifyClassLoaderPCL(context.get(), 0, "");
195}
196
197TEST_F(ClassLoaderContextTest, ParseValidSharedLibraryContext) {
198 std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::Create("&");
199 // An shared library context should have no class loader in the chain.
200 VerifyContextSize(context.get(), 0);
201}
202
Calin Juravle87e2cb62017-06-13 21:48:45 -0700203TEST_F(ClassLoaderContextTest, ParseValidContextPCL) {
204 std::unique_ptr<ClassLoaderContext> context =
205 ClassLoaderContext::Create("PCL[a.dex]");
206 VerifyContextSize(context.get(), 1);
207 VerifyClassLoaderPCL(context.get(), 0, "a.dex");
208}
209
210TEST_F(ClassLoaderContextTest, ParseValidContextDLC) {
211 std::unique_ptr<ClassLoaderContext> context =
212 ClassLoaderContext::Create("DLC[a.dex]");
213 VerifyContextSize(context.get(), 1);
214 VerifyClassLoaderDLC(context.get(), 0, "a.dex");
215}
216
217TEST_F(ClassLoaderContextTest, ParseValidContextChain) {
218 std::unique_ptr<ClassLoaderContext> context =
219 ClassLoaderContext::Create("PCL[a.dex:b.dex];DLC[c.dex:d.dex];PCL[e.dex]");
220 VerifyContextSize(context.get(), 3);
221 VerifyClassLoaderPCL(context.get(), 0, "a.dex:b.dex");
222 VerifyClassLoaderDLC(context.get(), 1, "c.dex:d.dex");
223 VerifyClassLoaderPCL(context.get(), 2, "e.dex");
224}
225
226TEST_F(ClassLoaderContextTest, ParseValidEmptyContextDLC) {
227 std::unique_ptr<ClassLoaderContext> context =
228 ClassLoaderContext::Create("DLC[]");
229 VerifyContextSize(context.get(), 1);
230 VerifyClassLoaderDLC(context.get(), 0, "");
231}
232
233TEST_F(ClassLoaderContextTest, ParseValidContextSpecialSymbol) {
234 std::unique_ptr<ClassLoaderContext> context =
235 ClassLoaderContext::Create(OatFile::kSpecialSharedLibrary);
236 VerifyContextSize(context.get(), 0);
237}
238
239TEST_F(ClassLoaderContextTest, ParseInvalidValidContexts) {
240 ASSERT_TRUE(nullptr == ClassLoaderContext::Create("ABC[a.dex]"));
241 ASSERT_TRUE(nullptr == ClassLoaderContext::Create("PCL"));
242 ASSERT_TRUE(nullptr == ClassLoaderContext::Create("PCL[a.dex"));
243 ASSERT_TRUE(nullptr == ClassLoaderContext::Create("PCLa.dex]"));
244 ASSERT_TRUE(nullptr == ClassLoaderContext::Create("PCL{a.dex}"));
245 ASSERT_TRUE(nullptr == ClassLoaderContext::Create("PCL[a.dex];DLC[b.dex"));
246}
247
248TEST_F(ClassLoaderContextTest, OpenInvalidDexFiles) {
249 std::unique_ptr<ClassLoaderContext> context =
250 ClassLoaderContext::Create("PCL[does_not_exist.dex]");
251 VerifyContextSize(context.get(), 1);
252 ASSERT_FALSE(context->OpenDexFiles(InstructionSet::kArm, "."));
253}
254
255TEST_F(ClassLoaderContextTest, OpenValidDexFiles) {
256 std::string multidex_name = GetTestDexFileName("MultiDex");
Calin Juravle87e2cb62017-06-13 21:48:45 -0700257 std::string myclass_dex_name = GetTestDexFileName("MyClass");
Calin Juravle87e2cb62017-06-13 21:48:45 -0700258 std::string dex_name = GetTestDexFileName("Main");
Calin Juravle87e2cb62017-06-13 21:48:45 -0700259
Calin Juravle92003fe2017-09-06 02:22:57 +0000260
Calin Juravle87e2cb62017-06-13 21:48:45 -0700261 std::unique_ptr<ClassLoaderContext> context =
262 ClassLoaderContext::Create(
263 "PCL[" + multidex_name + ":" + myclass_dex_name + "];" +
264 "DLC[" + dex_name + "]");
265
266 ASSERT_TRUE(context->OpenDexFiles(InstructionSet::kArm, /*classpath_dir*/ ""));
267
268 VerifyContextSize(context.get(), 2);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700269
Calin Juravlec5b215f2017-09-12 14:49:37 -0700270 std::vector<std::unique_ptr<const DexFile>> all_dex_files0 = OpenTestDexFiles("MultiDex");
271 std::vector<std::unique_ptr<const DexFile>> myclass_dex_files = OpenTestDexFiles("MyClass");
272 for (size_t i = 0; i < myclass_dex_files.size(); i++) {
273 all_dex_files0.emplace_back(myclass_dex_files[i].release());
274 }
275 VerifyOpenDexFiles(context.get(), 0, &all_dex_files0);
276
277 std::vector<std::unique_ptr<const DexFile>> all_dex_files1 = OpenTestDexFiles("Main");
278 VerifyOpenDexFiles(context.get(), 1, &all_dex_files1);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700279}
280
Andreas Gampe72527382017-09-02 16:53:03 -0700281static std::string CreateRelativeString(const std::string& in, const char* cwd) {
Nicolas Geoffrayc4be2c82017-09-05 12:40:06 +0100282 int cwd_len = strlen(cwd);
283 if (!android::base::StartsWith(in, cwd) || (cwd_len < 1)) {
Andreas Gampe72527382017-09-02 16:53:03 -0700284 LOG(FATAL) << in << " " << cwd;
285 }
Nicolas Geoffrayc4be2c82017-09-05 12:40:06 +0100286 bool contains_trailing_slash = (cwd[cwd_len - 1] == '/');
287 int start_position = cwd_len + (contains_trailing_slash ? 0 : 1);
288 return in.substr(start_position);
Andreas Gampe72527382017-09-02 16:53:03 -0700289}
290
291TEST_F(ClassLoaderContextTest, OpenValidDexFilesRelative) {
292 char cwd_buf[4096];
293 if (getcwd(cwd_buf, arraysize(cwd_buf)) == nullptr) {
294 PLOG(FATAL) << "Could not get working directory";
295 }
296 std::string multidex_name = CreateRelativeString(GetTestDexFileName("MultiDex"), cwd_buf);
Andreas Gampe72527382017-09-02 16:53:03 -0700297 std::string myclass_dex_name = CreateRelativeString(GetTestDexFileName("MyClass"), cwd_buf);
Andreas Gampe72527382017-09-02 16:53:03 -0700298 std::string dex_name = CreateRelativeString(GetTestDexFileName("Main"), cwd_buf);
Andreas Gampe72527382017-09-02 16:53:03 -0700299
300
301 std::unique_ptr<ClassLoaderContext> context =
302 ClassLoaderContext::Create(
303 "PCL[" + multidex_name + ":" + myclass_dex_name + "];" +
304 "DLC[" + dex_name + "]");
305
306 ASSERT_TRUE(context->OpenDexFiles(InstructionSet::kArm, /*classpath_dir*/ ""));
307
Calin Juravlec5b215f2017-09-12 14:49:37 -0700308 std::vector<std::unique_ptr<const DexFile>> all_dex_files0 = OpenTestDexFiles("MultiDex");
309 std::vector<std::unique_ptr<const DexFile>> myclass_dex_files = OpenTestDexFiles("MyClass");
310 for (size_t i = 0; i < myclass_dex_files.size(); i++) {
311 all_dex_files0.emplace_back(myclass_dex_files[i].release());
312 }
313 VerifyOpenDexFiles(context.get(), 0, &all_dex_files0);
Andreas Gampe72527382017-09-02 16:53:03 -0700314
Calin Juravlec5b215f2017-09-12 14:49:37 -0700315 std::vector<std::unique_ptr<const DexFile>> all_dex_files1 = OpenTestDexFiles("Main");
316 VerifyOpenDexFiles(context.get(), 1, &all_dex_files1);
Andreas Gampe72527382017-09-02 16:53:03 -0700317}
318
319TEST_F(ClassLoaderContextTest, OpenValidDexFilesClasspathDir) {
320 char cwd_buf[4096];
321 if (getcwd(cwd_buf, arraysize(cwd_buf)) == nullptr) {
322 PLOG(FATAL) << "Could not get working directory";
323 }
324 std::string multidex_name = CreateRelativeString(GetTestDexFileName("MultiDex"), cwd_buf);
Andreas Gampe72527382017-09-02 16:53:03 -0700325 std::string myclass_dex_name = CreateRelativeString(GetTestDexFileName("MyClass"), cwd_buf);
Andreas Gampe72527382017-09-02 16:53:03 -0700326 std::string dex_name = CreateRelativeString(GetTestDexFileName("Main"), cwd_buf);
Andreas Gampe72527382017-09-02 16:53:03 -0700327
328 std::unique_ptr<ClassLoaderContext> context =
329 ClassLoaderContext::Create(
330 "PCL[" + multidex_name + ":" + myclass_dex_name + "];" +
331 "DLC[" + dex_name + "]");
332
333 ASSERT_TRUE(context->OpenDexFiles(InstructionSet::kArm, cwd_buf));
334
335 VerifyContextSize(context.get(), 2);
Calin Juravlec5b215f2017-09-12 14:49:37 -0700336 std::vector<std::unique_ptr<const DexFile>> all_dex_files0 = OpenTestDexFiles("MultiDex");
337 std::vector<std::unique_ptr<const DexFile>> myclass_dex_files = OpenTestDexFiles("MyClass");
338 for (size_t i = 0; i < myclass_dex_files.size(); i++) {
339 all_dex_files0.emplace_back(myclass_dex_files[i].release());
340 }
341 VerifyOpenDexFiles(context.get(), 0, &all_dex_files0);
Andreas Gampe72527382017-09-02 16:53:03 -0700342
Calin Juravlec5b215f2017-09-12 14:49:37 -0700343 std::vector<std::unique_ptr<const DexFile>> all_dex_files1 = OpenTestDexFiles("Main");
344 VerifyOpenDexFiles(context.get(), 1, &all_dex_files1);
Andreas Gampe72527382017-09-02 16:53:03 -0700345}
346
Calin Juravle87e2cb62017-06-13 21:48:45 -0700347TEST_F(ClassLoaderContextTest, OpenInvalidDexFilesMix) {
348 std::string dex_name = GetTestDexFileName("Main");
349 std::unique_ptr<ClassLoaderContext> context =
350 ClassLoaderContext::Create("PCL[does_not_exist.dex];DLC[" + dex_name + "]");
351 ASSERT_FALSE(context->OpenDexFiles(InstructionSet::kArm, ""));
352}
353
354TEST_F(ClassLoaderContextTest, CreateClassLoader) {
355 std::string dex_name = GetTestDexFileName("Main");
356 std::unique_ptr<ClassLoaderContext> context =
357 ClassLoaderContext::Create("PCL[" + dex_name + "]");
358 ASSERT_TRUE(context->OpenDexFiles(InstructionSet::kArm, ""));
359
360 std::vector<std::unique_ptr<const DexFile>> classpath_dex = OpenTestDexFiles("Main");
361 std::vector<std::unique_ptr<const DexFile>> compilation_sources = OpenTestDexFiles("MultiDex");
362
363 std::vector<const DexFile*> compilation_sources_raw =
364 MakeNonOwningPointerVector(compilation_sources);
365 jobject jclass_loader = context->CreateClassLoader(compilation_sources_raw);
366 ASSERT_TRUE(jclass_loader != nullptr);
367
368 ScopedObjectAccess soa(Thread::Current());
369
Calin Juravlec79470d2017-07-12 17:37:42 -0700370 StackHandleScope<1> hs(soa.Self());
Calin Juravle87e2cb62017-06-13 21:48:45 -0700371 Handle<mirror::ClassLoader> class_loader = hs.NewHandle(
372 soa.Decode<mirror::ClassLoader>(jclass_loader));
373
374 ASSERT_TRUE(class_loader->GetClass() ==
375 soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_PathClassLoader));
376 ASSERT_TRUE(class_loader->GetParent()->GetClass() ==
377 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_BootClassLoader));
378
Calin Juravlec79470d2017-07-12 17:37:42 -0700379 // For the first class loader the class path dex files must come first and then the
380 // compilation sources.
381 std::vector<const DexFile*> expected_classpath = MakeNonOwningPointerVector(classpath_dex);
382 for (auto& dex : compilation_sources_raw) {
383 expected_classpath.push_back(dex);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700384 }
385
Calin Juravlec79470d2017-07-12 17:37:42 -0700386 VerifyClassLoaderDexFiles(soa,
387 class_loader,
388 WellKnownClasses::dalvik_system_PathClassLoader,
389 expected_classpath);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700390}
391
Calin Juravle7b0648a2017-07-07 18:40:50 -0700392TEST_F(ClassLoaderContextTest, CreateClassLoaderWithEmptyContext) {
393 std::unique_ptr<ClassLoaderContext> context =
394 ClassLoaderContext::Create("");
395 ASSERT_TRUE(context->OpenDexFiles(InstructionSet::kArm, ""));
396
397 std::vector<std::unique_ptr<const DexFile>> compilation_sources = OpenTestDexFiles("MultiDex");
398
399 std::vector<const DexFile*> compilation_sources_raw =
400 MakeNonOwningPointerVector(compilation_sources);
401 jobject jclass_loader = context->CreateClassLoader(compilation_sources_raw);
402 ASSERT_TRUE(jclass_loader != nullptr);
403
404 ScopedObjectAccess soa(Thread::Current());
405
Calin Juravlec79470d2017-07-12 17:37:42 -0700406 StackHandleScope<1> hs(soa.Self());
Calin Juravle7b0648a2017-07-07 18:40:50 -0700407 Handle<mirror::ClassLoader> class_loader = hs.NewHandle(
408 soa.Decode<mirror::ClassLoader>(jclass_loader));
409
Calin Juravlec79470d2017-07-12 17:37:42 -0700410 // An empty context should create a single PathClassLoader with only the compilation sources.
411 VerifyClassLoaderDexFiles(soa,
412 class_loader,
413 WellKnownClasses::dalvik_system_PathClassLoader,
414 compilation_sources_raw);
Calin Juravle7b0648a2017-07-07 18:40:50 -0700415 ASSERT_TRUE(class_loader->GetParent()->GetClass() ==
416 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_BootClassLoader));
Calin Juravle7b0648a2017-07-07 18:40:50 -0700417}
418
Calin Juravle1a509c82017-07-24 16:51:21 -0700419TEST_F(ClassLoaderContextTest, CreateClassLoaderWithSharedLibraryContext) {
420 std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::Create("&");
421
422 ASSERT_TRUE(context->OpenDexFiles(InstructionSet::kArm, ""));
423
424 std::vector<std::unique_ptr<const DexFile>> compilation_sources = OpenTestDexFiles("MultiDex");
425
426 std::vector<const DexFile*> compilation_sources_raw =
427 MakeNonOwningPointerVector(compilation_sources);
428 jobject jclass_loader = context->CreateClassLoader(compilation_sources_raw);
429 ASSERT_TRUE(jclass_loader != nullptr);
430
431 ScopedObjectAccess soa(Thread::Current());
432
433 StackHandleScope<1> hs(soa.Self());
434 Handle<mirror::ClassLoader> class_loader = hs.NewHandle(
435 soa.Decode<mirror::ClassLoader>(jclass_loader));
436
437 // A shared library context should create a single PathClassLoader with only the compilation
438 // sources.
439 VerifyClassLoaderDexFiles(soa,
440 class_loader,
441 WellKnownClasses::dalvik_system_PathClassLoader,
442 compilation_sources_raw);
443 ASSERT_TRUE(class_loader->GetParent()->GetClass() ==
444 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_BootClassLoader));
445}
446
Calin Juravlec79470d2017-07-12 17:37:42 -0700447TEST_F(ClassLoaderContextTest, CreateClassLoaderWithComplexChain) {
448 // Setup the context.
449 std::vector<std::unique_ptr<const DexFile>> classpath_dex_a = OpenTestDexFiles("ForClassLoaderA");
450 std::vector<std::unique_ptr<const DexFile>> classpath_dex_b = OpenTestDexFiles("ForClassLoaderB");
451 std::vector<std::unique_ptr<const DexFile>> classpath_dex_c = OpenTestDexFiles("ForClassLoaderC");
452 std::vector<std::unique_ptr<const DexFile>> classpath_dex_d = OpenTestDexFiles("ForClassLoaderD");
453
454 std::string context_spec =
455 "PCL[" + CreateClassPath(classpath_dex_a) + ":" + CreateClassPath(classpath_dex_b) + "];" +
456 "DLC[" + CreateClassPath(classpath_dex_c) + "];" +
457 "PCL[" + CreateClassPath(classpath_dex_d) + "]";
458
459 std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::Create(context_spec);
460 ASSERT_TRUE(context->OpenDexFiles(InstructionSet::kArm, ""));
461
462 // Setup the compilation sources.
463 std::vector<std::unique_ptr<const DexFile>> compilation_sources = OpenTestDexFiles("MultiDex");
464 std::vector<const DexFile*> compilation_sources_raw =
465 MakeNonOwningPointerVector(compilation_sources);
466
467 // Create the class loader.
468 jobject jclass_loader = context->CreateClassLoader(compilation_sources_raw);
469 ASSERT_TRUE(jclass_loader != nullptr);
470
471 // Verify the class loader.
472 ScopedObjectAccess soa(Thread::Current());
473
474 StackHandleScope<3> hs(soa.Self());
475 Handle<mirror::ClassLoader> class_loader_1 = hs.NewHandle(
476 soa.Decode<mirror::ClassLoader>(jclass_loader));
477
478 // Verify the first class loader
479
480 // For the first class loader the class path dex files must come first and then the
481 // compilation sources.
482 std::vector<const DexFile*> class_loader_1_dex_files =
483 MakeNonOwningPointerVector(classpath_dex_a);
484 for (auto& dex : classpath_dex_b) {
485 class_loader_1_dex_files.push_back(dex.get());
486 }
487 for (auto& dex : compilation_sources_raw) {
488 class_loader_1_dex_files.push_back(dex);
489 }
490 VerifyClassLoaderDexFiles(soa,
491 class_loader_1,
492 WellKnownClasses::dalvik_system_PathClassLoader,
493 class_loader_1_dex_files);
494
495 // Verify the second class loader
496 Handle<mirror::ClassLoader> class_loader_2 = hs.NewHandle(class_loader_1->GetParent());
497 std::vector<const DexFile*> class_loader_2_dex_files =
498 MakeNonOwningPointerVector(classpath_dex_c);
499 VerifyClassLoaderDexFiles(soa,
500 class_loader_2,
501 WellKnownClasses::dalvik_system_DelegateLastClassLoader,
502 class_loader_2_dex_files);
503
504 // Verify the third class loader
505 Handle<mirror::ClassLoader> class_loader_3 = hs.NewHandle(class_loader_2->GetParent());
506 std::vector<const DexFile*> class_loader_3_dex_files =
507 MakeNonOwningPointerVector(classpath_dex_d);
508 VerifyClassLoaderDexFiles(soa,
509 class_loader_3,
510 WellKnownClasses::dalvik_system_PathClassLoader,
511 class_loader_3_dex_files);
512 // The last class loader should have the BootClassLoader as a parent.
513 ASSERT_TRUE(class_loader_3->GetParent()->GetClass() ==
514 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_BootClassLoader));
515}
516
517
Calin Juravle87e2cb62017-06-13 21:48:45 -0700518TEST_F(ClassLoaderContextTest, RemoveSourceLocations) {
519 std::unique_ptr<ClassLoaderContext> context =
520 ClassLoaderContext::Create("PCL[a.dex]");
521 dchecked_vector<std::string> classpath_dex;
522 classpath_dex.push_back("a.dex");
523 dchecked_vector<std::string> compilation_sources;
524 compilation_sources.push_back("src.dex");
525
526 // Nothing should be removed.
527 ASSERT_FALSE(context->RemoveLocationsFromClassPaths(compilation_sources));
528 VerifyClassLoaderPCL(context.get(), 0, "a.dex");
529 // Classes should be removed.
530 ASSERT_TRUE(context->RemoveLocationsFromClassPaths(classpath_dex));
531 VerifyClassLoaderPCL(context.get(), 0, "");
532}
533
534TEST_F(ClassLoaderContextTest, EncodeInOatFile) {
535 std::string dex1_name = GetTestDexFileName("Main");
536 std::string dex2_name = GetTestDexFileName("MyClass");
537 std::unique_ptr<ClassLoaderContext> context =
538 ClassLoaderContext::Create("PCL[" + dex1_name + ":" + dex2_name + "]");
539 ASSERT_TRUE(context->OpenDexFiles(InstructionSet::kArm, ""));
540
541 std::vector<std::unique_ptr<const DexFile>> dex1 = OpenTestDexFiles("Main");
542 std::vector<std::unique_ptr<const DexFile>> dex2 = OpenTestDexFiles("MyClass");
543 std::string encoding = context->EncodeContextForOatFile("");
Calin Juravlec79470d2017-07-12 17:37:42 -0700544 std::string expected_encoding = "PCL[" + CreateClassPathWithChecksums(dex1) + ":" +
545 CreateClassPathWithChecksums(dex2) + "]";
Calin Juravle87e2cb62017-06-13 21:48:45 -0700546 ASSERT_EQ(expected_encoding, context->EncodeContextForOatFile(""));
547}
548
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700549TEST_F(ClassLoaderContextTest, EncodeForDex2oat) {
550 std::string dex1_name = GetTestDexFileName("Main");
551 std::string dex2_name = GetTestDexFileName("MultiDex");
552 std::unique_ptr<ClassLoaderContext> context =
553 ClassLoaderContext::Create("PCL[" + dex1_name + ":" + dex2_name + "]");
554 ASSERT_TRUE(context->OpenDexFiles(InstructionSet::kArm, ""));
555
556 std::vector<std::unique_ptr<const DexFile>> dex1 = OpenTestDexFiles("Main");
557 std::vector<std::unique_ptr<const DexFile>> dex2 = OpenTestDexFiles("MultiDex");
558 std::string encoding = context->EncodeContextForDex2oat("");
559 std::string expected_encoding = "PCL[" + dex1_name + ":" + dex2_name + "]";
560 ASSERT_EQ(expected_encoding, context->EncodeContextForDex2oat(""));
561}
562
Calin Juravle57d0acc2017-07-11 17:41:30 -0700563// TODO(calin) add a test which creates the context for a class loader together with dex_elements.
564TEST_F(ClassLoaderContextTest, CreateContextForClassLoader) {
565 // The chain is
566 // ClassLoaderA (PathClassLoader)
567 // ^
568 // |
569 // ClassLoaderB (DelegateLastClassLoader)
570 // ^
571 // |
572 // ClassLoaderC (PathClassLoader)
573 // ^
574 // |
575 // ClassLoaderD (DelegateLastClassLoader)
576
577 jobject class_loader_a = LoadDexInPathClassLoader("ForClassLoaderA", nullptr);
578 jobject class_loader_b = LoadDexInDelegateLastClassLoader("ForClassLoaderB", class_loader_a);
579 jobject class_loader_c = LoadDexInPathClassLoader("ForClassLoaderC", class_loader_b);
580 jobject class_loader_d = LoadDexInDelegateLastClassLoader("ForClassLoaderD", class_loader_c);
581
582 std::unique_ptr<ClassLoaderContext> context = CreateContextForClassLoader(class_loader_d);
583
584 VerifyContextForClassLoader(context.get());
585 VerifyContextSize(context.get(), 4);
586
587 VerifyClassLoaderDLCFromTestDex(context.get(), 0, "ForClassLoaderD");
588 VerifyClassLoaderPCLFromTestDex(context.get(), 1, "ForClassLoaderC");
589 VerifyClassLoaderDLCFromTestDex(context.get(), 2, "ForClassLoaderB");
590 VerifyClassLoaderPCLFromTestDex(context.get(), 3, "ForClassLoaderA");
591}
592
Calin Juravle3f918642017-07-11 19:04:20 -0700593TEST_F(ClassLoaderContextTest, VerifyClassLoaderContextMatch) {
594 std::string context_spec = "PCL[a.dex*123:b.dex*456];DLC[c.dex*890]";
595 std::unique_ptr<ClassLoaderContext> context = ParseContextWithChecksums(context_spec);
Calin Juravlec5b215f2017-09-12 14:49:37 -0700596 // Pretend that we successfully open the dex files to pass the DCHECKS.
597 // (as it's much easier to test all the corner cases without relying on actual dex files).
598 PretendContextOpenedDexFiles(context.get());
Calin Juravle3f918642017-07-11 19:04:20 -0700599
600 VerifyContextSize(context.get(), 2);
601 VerifyClassLoaderPCL(context.get(), 0, "a.dex:b.dex");
602 VerifyClassLoaderDLC(context.get(), 1, "c.dex");
603
604 ASSERT_TRUE(context->VerifyClassLoaderContextMatch(context_spec));
605
606 std::string wrong_class_loader_type = "PCL[a.dex*123:b.dex*456];PCL[c.dex*890]";
607 ASSERT_FALSE(context->VerifyClassLoaderContextMatch(wrong_class_loader_type));
608
609 std::string wrong_class_loader_order = "DLC[c.dex*890];PCL[a.dex*123:b.dex*456]";
610 ASSERT_FALSE(context->VerifyClassLoaderContextMatch(wrong_class_loader_order));
611
612 std::string wrong_classpath_order = "PCL[b.dex*456:a.dex*123];DLC[c.dex*890]";
613 ASSERT_FALSE(context->VerifyClassLoaderContextMatch(wrong_classpath_order));
614
615 std::string wrong_checksum = "PCL[a.dex*999:b.dex*456];DLC[c.dex*890]";
616 ASSERT_FALSE(context->VerifyClassLoaderContextMatch(wrong_checksum));
617
618 std::string wrong_extra_class_loader = "PCL[a.dex*123:b.dex*456];DLC[c.dex*890];PCL[d.dex*321]";
619 ASSERT_FALSE(context->VerifyClassLoaderContextMatch(wrong_extra_class_loader));
620
621 std::string wrong_extra_classpath = "PCL[a.dex*123:b.dex*456];DLC[c.dex*890:d.dex*321]";
622 ASSERT_FALSE(context->VerifyClassLoaderContextMatch(wrong_extra_classpath));
623
624 std::string wrong_spec = "PCL[a.dex*999:b.dex*456];DLC[";
625 ASSERT_FALSE(context->VerifyClassLoaderContextMatch(wrong_spec));
626}
627
628TEST_F(ClassLoaderContextTest, VerifyClassLoaderContextMatchAfterEncoding) {
629 jobject class_loader_a = LoadDexInPathClassLoader("ForClassLoaderA", nullptr);
630 jobject class_loader_b = LoadDexInDelegateLastClassLoader("ForClassLoaderB", class_loader_a);
631 jobject class_loader_c = LoadDexInPathClassLoader("ForClassLoaderC", class_loader_b);
632 jobject class_loader_d = LoadDexInDelegateLastClassLoader("ForClassLoaderD", class_loader_c);
633
634 std::unique_ptr<ClassLoaderContext> context = CreateContextForClassLoader(class_loader_d);
635
Calin Juravle1e96a5d2017-09-05 17:10:48 -0700636 std::string context_with_no_base_dir = context->EncodeContextForOatFile("");
637 ASSERT_TRUE(context->VerifyClassLoaderContextMatch(context_with_no_base_dir));
638
639 std::string dex_location = GetTestDexFileName("ForClassLoaderA");
640 size_t pos = dex_location.rfind('/');
641 ASSERT_NE(std::string::npos, pos);
642 std::string parent = dex_location.substr(0, pos);
643
644 std::string context_with_base_dir = context->EncodeContextForOatFile(parent);
645 ASSERT_NE(context_with_base_dir, context_with_no_base_dir);
646 ASSERT_TRUE(context->VerifyClassLoaderContextMatch(context_with_base_dir));
Calin Juravle3f918642017-07-11 19:04:20 -0700647}
648
Calin Juravlea308a322017-07-18 16:51:51 -0700649TEST_F(ClassLoaderContextTest, VerifyClassLoaderContextMatchAfterEncodingMultidex) {
650 jobject class_loader = LoadDexInPathClassLoader("MultiDex", nullptr);
651
652 std::unique_ptr<ClassLoaderContext> context = CreateContextForClassLoader(class_loader);
653
654 ASSERT_TRUE(context->VerifyClassLoaderContextMatch(context->EncodeContextForOatFile("")));
655}
656
Calin Juravle87e2cb62017-06-13 21:48:45 -0700657} // namespace art