huangcm
2025-07-01 676035278781360996553c427a12bf358249ebf7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#include "common_compiler_test.h"
 
#include "class_linker-inl.h"
#include "handle.h"
#include "handle_scope-inl.h"
#include "mirror/class_loader-inl.h"
#include "mirror/dex_cache.h"
#include "mirror/object-inl.h"
#include "runtime.h"
#include "scoped_thread_state_change-inl.h"
#include "thread-current-inl.h"
#include "well_known_classes.h"
 
namespace art {
 
class ModuleExclusionTest : public CommonCompilerTest {
 public:
  explicit ModuleExclusionTest(const std::string& module)
      : CommonCompilerTest(),
        module_(module) {}
 
  std::vector<std::string> GetLibCoreModuleNames() const override {
    std::vector<std::string> modules = CommonCompilerTest::GetLibCoreModuleNames();
    // Exclude `module_` from boot class path.
    auto it = std::find(modules.begin(), modules.end(), module_);
    if (it != modules.end()) {
      modules.erase(it);
    }
    return modules;
  }
 
  void DoTest() {
    Thread* self = Thread::Current();
    ScopedObjectAccess soa(self);
    StackHandleScope<2u> hs(self);
    Runtime* runtime = Runtime::Current();
    ASSERT_TRUE(runtime->IsAotCompiler());
    ClassLinker* class_linker = runtime->GetClassLinker();
    CHECK(loaded_dex_files_.empty());
    Handle<mirror::ClassLoader> class_loader = hs.NewHandle(LoadModule(soa, class_linker));
    MutableHandle<mirror::DexCache> dex_cache = hs.NewHandle<mirror::DexCache>(nullptr);
    CHECK(!loaded_dex_files_.empty());
 
    // Verify that classes defined in the loaded dex files cannot be resolved.
    for (const std::unique_ptr<const DexFile>& dex_file : loaded_dex_files_) {
      dex_cache.Assign(class_linker->RegisterDexFile(*dex_file, class_loader.Get()));
      for (size_t i = 0u, size = dex_file->NumClassDefs(); i != size; ++i) {
        const dex::ClassDef& class_def = dex_file->GetClassDef(i);
        ObjPtr<mirror::Class> resolved_type =
            class_linker->ResolveType(class_def.class_idx_, dex_cache, class_loader);
        ASSERT_TRUE(resolved_type == nullptr) << resolved_type->PrettyDescriptor();
        ASSERT_TRUE(self->IsExceptionPending());
        self->ClearException();
      }
    }
  }
 
 private:
  std::string GetModuleFileName() const {
    std::vector<std::string> filename = GetLibCoreDexFileNames({ module_ });
    CHECK_EQ(filename.size(), 1u);
    return filename[0];
  }
 
  // Load the module as an app, i.e. in a class loader other than the boot class loader.
  ObjPtr<mirror::ClassLoader> LoadModule(ScopedObjectAccess& soa, ClassLinker* class_linker)
      REQUIRES_SHARED(Locks::mutator_lock_) {
    std::string filename = GetModuleFileName();
    std::vector<std::unique_ptr<const DexFile>> dex_files = OpenDexFiles(filename.c_str());
 
    std::vector<const DexFile*> class_path;
    CHECK_NE(0U, dex_files.size());
    for (auto& dex_file : dex_files) {
      class_path.push_back(dex_file.get());
      loaded_dex_files_.push_back(std::move(dex_file));
    }
 
    StackHandleScope<1u> hs(soa.Self());
    Handle<mirror::Class> loader_class(hs.NewHandle(
        soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_PathClassLoader)));
    ScopedNullHandle<mirror::ClassLoader> parent_loader;
    ScopedNullHandle<mirror::ObjectArray<mirror::ClassLoader>> shared_libraries;
 
    ObjPtr<mirror::ClassLoader> result = class_linker->CreateWellKnownClassLoader(
        soa.Self(),
        class_path,
        loader_class,
        parent_loader,
        shared_libraries);
 
    // Verify that the result has the correct class.
    CHECK_EQ(loader_class.Get(), result->GetClass());
    // Verify that the parent is not null. The boot class loader will be set up as a
    // proper BootClassLoader object.
    ObjPtr<mirror::ClassLoader> actual_parent(result->GetParent());
    CHECK(actual_parent != nullptr);
    CHECK(class_linker->IsBootClassLoader(soa, actual_parent));
 
    return result;
  }
 
  const std::string module_;
};
 
class ConscryptExclusionTest : public ModuleExclusionTest {
 public:
  ConscryptExclusionTest() : ModuleExclusionTest("conscrypt") {}
};
 
TEST_F(ConscryptExclusionTest, Test) {
  DoTest();
}
 
}  // namespace art