1 /*
2 * Copyright (C) 2019 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 "common_compiler_test.h"
18
19 #include "class_linker-inl.h"
20 #include "handle.h"
21 #include "handle_scope-inl.h"
22 #include "mirror/class_loader-inl.h"
23 #include "mirror/dex_cache.h"
24 #include "mirror/object-inl.h"
25 #include "runtime.h"
26 #include "scoped_thread_state_change-inl.h"
27 #include "thread-current-inl.h"
28 #include "well_known_classes.h"
29
30 namespace art {
31
32 class ModuleExclusionTest : public CommonCompilerTest {
33 public:
ModuleExclusionTest(const std::string & module)34 explicit ModuleExclusionTest(const std::string& module)
35 : CommonCompilerTest(),
36 module_(module) {}
37
GetLibCoreModuleNames() const38 std::vector<std::string> GetLibCoreModuleNames() const override {
39 std::vector<std::string> modules = CommonCompilerTest::GetLibCoreModuleNames();
40 // Exclude `module_` from boot class path.
41 auto it = std::find(modules.begin(), modules.end(), module_);
42 if (it != modules.end()) {
43 modules.erase(it);
44 }
45 return modules;
46 }
47
DoTest()48 void DoTest() {
49 Thread* self = Thread::Current();
50 ScopedObjectAccess soa(self);
51 StackHandleScope<2u> hs(self);
52 Runtime* runtime = Runtime::Current();
53 ASSERT_TRUE(runtime->IsAotCompiler());
54 ClassLinker* class_linker = runtime->GetClassLinker();
55 CHECK(loaded_dex_files_.empty());
56 Handle<mirror::ClassLoader> class_loader = hs.NewHandle(LoadModule(soa, class_linker));
57 MutableHandle<mirror::DexCache> dex_cache = hs.NewHandle<mirror::DexCache>(nullptr);
58 CHECK(!loaded_dex_files_.empty());
59
60 // Verify that classes defined in the loaded dex files cannot be resolved.
61 for (const std::unique_ptr<const DexFile>& dex_file : loaded_dex_files_) {
62 dex_cache.Assign(class_linker->RegisterDexFile(*dex_file, class_loader.Get()));
63 for (size_t i = 0u, size = dex_file->NumClassDefs(); i != size; ++i) {
64 const dex::ClassDef& class_def = dex_file->GetClassDef(i);
65 ObjPtr<mirror::Class> resolved_type =
66 class_linker->ResolveType(class_def.class_idx_, dex_cache, class_loader);
67 ASSERT_TRUE(resolved_type == nullptr) << resolved_type->PrettyDescriptor();
68 ASSERT_TRUE(self->IsExceptionPending());
69 self->ClearException();
70 }
71 }
72 }
73
74 private:
GetModuleFileName() const75 std::string GetModuleFileName() const {
76 std::vector<std::string> filename = GetLibCoreDexFileNames({ module_ });
77 CHECK_EQ(filename.size(), 1u);
78 return filename[0];
79 }
80
81 // Load the module as an app, i.e. in a class loader other than the boot class loader.
LoadModule(ScopedObjectAccess & soa,ClassLinker * class_linker)82 ObjPtr<mirror::ClassLoader> LoadModule(ScopedObjectAccess& soa, ClassLinker* class_linker)
83 REQUIRES_SHARED(Locks::mutator_lock_) {
84 std::string filename = GetModuleFileName();
85 std::vector<std::unique_ptr<const DexFile>> dex_files = OpenDexFiles(filename.c_str());
86
87 std::vector<const DexFile*> class_path;
88 CHECK_NE(0U, dex_files.size());
89 for (auto& dex_file : dex_files) {
90 class_path.push_back(dex_file.get());
91 loaded_dex_files_.push_back(std::move(dex_file));
92 }
93
94 StackHandleScope<1u> hs(soa.Self());
95 Handle<mirror::Class> loader_class(hs.NewHandle(
96 soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_PathClassLoader)));
97 ScopedNullHandle<mirror::ClassLoader> parent_loader;
98 ScopedNullHandle<mirror::ObjectArray<mirror::ClassLoader>> shared_libraries;
99
100 ObjPtr<mirror::ClassLoader> result = class_linker->CreateWellKnownClassLoader(
101 soa.Self(),
102 class_path,
103 loader_class,
104 parent_loader,
105 shared_libraries);
106
107 // Verify that the result has the correct class.
108 CHECK_EQ(loader_class.Get(), result->GetClass());
109 // Verify that the parent is not null. The boot class loader will be set up as a
110 // proper BootClassLoader object.
111 ObjPtr<mirror::ClassLoader> actual_parent(result->GetParent());
112 CHECK(actual_parent != nullptr);
113 CHECK(class_linker->IsBootClassLoader(soa, actual_parent));
114
115 return result;
116 }
117
118 const std::string module_;
119 };
120
121 class ConscryptExclusionTest : public ModuleExclusionTest {
122 public:
ConscryptExclusionTest()123 ConscryptExclusionTest() : ModuleExclusionTest("conscrypt") {}
124 };
125
TEST_F(ConscryptExclusionTest,Test)126 TEST_F(ConscryptExclusionTest, Test) {
127 DoTest();
128 }
129
130 } // namespace art
131