Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 1 | /* |
| 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 Gampe | 7252738 | 2017-09-02 16:53:03 -0700 | [diff] [blame] | 17 | #include "class_loader_context.h" |
| 18 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 19 | #include <gtest/gtest.h> |
| 20 | |
Andreas Gampe | 7252738 | 2017-09-02 16:53:03 -0700 | [diff] [blame] | 21 | #include "android-base/strings.h" |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 22 | #include "base/dchecked_vector.h" |
| 23 | #include "base/stl_util.h" |
| 24 | #include "class_linker.h" |
Calin Juravle | 821a259 | 2017-08-11 14:33:38 -0700 | [diff] [blame] | 25 | #include "common_runtime_test.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 26 | #include "dex/dex_file.h" |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 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 | |
| 37 | namespace art { |
| 38 | |
| 39 | class ClassLoaderContextTest : public CommonRuntimeTest { |
| 40 | public: |
| 41 | void VerifyContextSize(ClassLoaderContext* context, size_t expected_size) { |
| 42 | ASSERT_TRUE(context != nullptr); |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame^] | 43 | ASSERT_EQ(expected_size, context->GetParentChainSize()); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void VerifyClassLoaderPCL(ClassLoaderContext* context, |
| 47 | size_t index, |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 48 | const std::string& classpath) { |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 49 | VerifyClassLoaderInfo( |
| 50 | context, index, ClassLoaderContext::kPathClassLoader, classpath); |
| 51 | } |
| 52 | |
| 53 | void VerifyClassLoaderDLC(ClassLoaderContext* context, |
| 54 | size_t index, |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 55 | const std::string& classpath) { |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 56 | VerifyClassLoaderInfo( |
| 57 | context, index, ClassLoaderContext::kDelegateLastClassLoader, classpath); |
| 58 | } |
| 59 | |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame^] | 60 | void VerifyClassLoaderSharedLibraryPCL(ClassLoaderContext* context, |
| 61 | size_t loader_index, |
| 62 | size_t shared_library_index, |
| 63 | const std::string& classpath) { |
| 64 | VerifyClassLoaderInfoSL( |
| 65 | context, loader_index, shared_library_index, ClassLoaderContext::kPathClassLoader, |
| 66 | classpath); |
| 67 | } |
| 68 | |
| 69 | void VerifySharedLibrariesSize(ClassLoaderContext* context, |
| 70 | size_t loader_index, |
| 71 | size_t expected_size) { |
| 72 | ASSERT_TRUE(context != nullptr); |
| 73 | ASSERT_GT(context->GetParentChainSize(), loader_index); |
| 74 | const ClassLoaderContext::ClassLoaderInfo& info = *context->GetParent(loader_index); |
| 75 | ASSERT_EQ(info.shared_libraries.size(), expected_size); |
| 76 | } |
| 77 | |
| 78 | void VerifyClassLoaderSharedLibraryDLC(ClassLoaderContext* context, |
| 79 | size_t loader_index, |
| 80 | size_t shared_library_index, |
| 81 | const std::string& classpath) { |
| 82 | VerifyClassLoaderInfoSL( |
| 83 | context, loader_index, shared_library_index, ClassLoaderContext::kDelegateLastClassLoader, |
| 84 | classpath); |
| 85 | } |
| 86 | |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 87 | void VerifyClassLoaderPCLFromTestDex(ClassLoaderContext* context, |
| 88 | size_t index, |
| 89 | const std::string& test_name) { |
| 90 | VerifyClassLoaderFromTestDex( |
| 91 | context, index, ClassLoaderContext::kPathClassLoader, test_name); |
| 92 | } |
| 93 | |
| 94 | void VerifyClassLoaderDLCFromTestDex(ClassLoaderContext* context, |
| 95 | size_t index, |
| 96 | const std::string& test_name) { |
| 97 | VerifyClassLoaderFromTestDex( |
| 98 | context, index, ClassLoaderContext::kDelegateLastClassLoader, test_name); |
| 99 | } |
| 100 | |
Andreas Gampe | 7252738 | 2017-09-02 16:53:03 -0700 | [diff] [blame] | 101 | enum class LocationCheck { |
| 102 | kEquals, |
| 103 | kEndsWith |
| 104 | }; |
| 105 | enum class BaseLocationCheck { |
| 106 | kEquals, |
| 107 | kEndsWith |
| 108 | }; |
| 109 | |
Calin Juravle | 92003fe | 2017-09-06 02:22:57 +0000 | [diff] [blame] | 110 | static bool IsAbsoluteLocation(const std::string& location) { |
| 111 | return !location.empty() && location[0] == '/'; |
| 112 | } |
| 113 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 114 | void VerifyOpenDexFiles( |
| 115 | ClassLoaderContext* context, |
| 116 | size_t index, |
Calin Juravle | c5b215f | 2017-09-12 14:49:37 -0700 | [diff] [blame] | 117 | std::vector<std::unique_ptr<const DexFile>>* all_dex_files) { |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 118 | ASSERT_TRUE(context != nullptr); |
| 119 | ASSERT_TRUE(context->dex_files_open_attempted_); |
| 120 | ASSERT_TRUE(context->dex_files_open_result_); |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame^] | 121 | ClassLoaderContext::ClassLoaderInfo& info = *context->GetParent(index); |
Calin Juravle | c5b215f | 2017-09-12 14:49:37 -0700 | [diff] [blame] | 122 | ASSERT_EQ(all_dex_files->size(), info.classpath.size()); |
| 123 | ASSERT_EQ(all_dex_files->size(), info.opened_dex_files.size()); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 124 | size_t cur_open_dex_index = 0; |
Calin Juravle | c5b215f | 2017-09-12 14:49:37 -0700 | [diff] [blame] | 125 | for (size_t k = 0; k < all_dex_files->size(); k++) { |
| 126 | std::unique_ptr<const DexFile>& opened_dex_file = |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 127 | info.opened_dex_files[cur_open_dex_index++]; |
Calin Juravle | c5b215f | 2017-09-12 14:49:37 -0700 | [diff] [blame] | 128 | std::unique_ptr<const DexFile>& expected_dex_file = (*all_dex_files)[k]; |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 129 | |
Calin Juravle | 92003fe | 2017-09-06 02:22:57 +0000 | [diff] [blame] | 130 | std::string expected_location = expected_dex_file->GetLocation(); |
Calin Juravle | 821a259 | 2017-08-11 14:33:38 -0700 | [diff] [blame] | 131 | |
Calin Juravle | 92003fe | 2017-09-06 02:22:57 +0000 | [diff] [blame] | 132 | const std::string& opened_location = opened_dex_file->GetLocation(); |
| 133 | if (!IsAbsoluteLocation(opened_location)) { |
| 134 | // If the opened location is relative (it was open from a relative path without a |
| 135 | // classpath_dir) it might not match the expected location which is absolute in tests). |
| 136 | // So we compare the endings (the checksum will validate it's actually the same file). |
| 137 | ASSERT_EQ(0, expected_location.compare( |
| 138 | expected_location.length() - opened_location.length(), |
| 139 | opened_location.length(), |
| 140 | opened_location)); |
| 141 | } else { |
| 142 | ASSERT_EQ(expected_location, opened_location); |
| 143 | } |
Calin Juravle | c5b215f | 2017-09-12 14:49:37 -0700 | [diff] [blame] | 144 | ASSERT_EQ(expected_dex_file->GetLocationChecksum(), opened_dex_file->GetLocationChecksum()); |
Calin Juravle | 92003fe | 2017-09-06 02:22:57 +0000 | [diff] [blame] | 145 | ASSERT_EQ(info.classpath[k], opened_location); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 149 | std::unique_ptr<ClassLoaderContext> CreateContextForClassLoader(jobject class_loader) { |
| 150 | return ClassLoaderContext::CreateContextForClassLoader(class_loader, nullptr); |
| 151 | } |
| 152 | |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 153 | std::unique_ptr<ClassLoaderContext> ParseContextWithChecksums(const std::string& context_spec) { |
| 154 | std::unique_ptr<ClassLoaderContext> context(new ClassLoaderContext()); |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 155 | if (!context->Parse(context_spec, /*parse_checksums=*/ true)) { |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 156 | return nullptr; |
| 157 | } |
| 158 | return context; |
| 159 | } |
| 160 | |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 161 | void VerifyContextForClassLoader(ClassLoaderContext* context) { |
| 162 | ASSERT_TRUE(context != nullptr); |
| 163 | ASSERT_TRUE(context->dex_files_open_attempted_); |
| 164 | ASSERT_TRUE(context->dex_files_open_result_); |
| 165 | ASSERT_FALSE(context->owns_the_dex_files_); |
| 166 | ASSERT_FALSE(context->special_shared_library_); |
| 167 | } |
| 168 | |
Calin Juravle | c79470d | 2017-07-12 17:37:42 -0700 | [diff] [blame] | 169 | void VerifyClassLoaderDexFiles(ScopedObjectAccess& soa, |
| 170 | Handle<mirror::ClassLoader> class_loader, |
| 171 | jclass type, |
| 172 | std::vector<const DexFile*>& expected_dex_files) |
| 173 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 174 | ASSERT_TRUE(class_loader->GetClass() == soa.Decode<mirror::Class>(type)); |
| 175 | |
| 176 | std::vector<const DexFile*> class_loader_dex_files = GetDexFiles(soa, class_loader); |
| 177 | ASSERT_EQ(expected_dex_files.size(), class_loader_dex_files.size()); |
| 178 | |
| 179 | for (size_t i = 0; i < expected_dex_files.size(); i++) { |
| 180 | ASSERT_EQ(expected_dex_files[i]->GetLocation(), |
| 181 | class_loader_dex_files[i]->GetLocation()); |
| 182 | ASSERT_EQ(expected_dex_files[i]->GetLocationChecksum(), |
| 183 | class_loader_dex_files[i]->GetLocationChecksum()); |
| 184 | } |
| 185 | } |
| 186 | |
Calin Juravle | c5b215f | 2017-09-12 14:49:37 -0700 | [diff] [blame] | 187 | void PretendContextOpenedDexFiles(ClassLoaderContext* context) { |
| 188 | context->dex_files_open_attempted_ = true; |
| 189 | context->dex_files_open_result_ = true; |
| 190 | } |
| 191 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 192 | private: |
| 193 | void VerifyClassLoaderInfo(ClassLoaderContext* context, |
| 194 | size_t index, |
| 195 | ClassLoaderContext::ClassLoaderType type, |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 196 | const std::string& classpath) { |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 197 | ASSERT_TRUE(context != nullptr); |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame^] | 198 | ASSERT_GT(context->GetParentChainSize(), index); |
| 199 | ClassLoaderContext::ClassLoaderInfo& info = *context->GetParent(index); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 200 | ASSERT_EQ(type, info.type); |
| 201 | std::vector<std::string> expected_classpath; |
| 202 | Split(classpath, ':', &expected_classpath); |
| 203 | ASSERT_EQ(expected_classpath, info.classpath); |
| 204 | } |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 205 | |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame^] | 206 | void VerifyClassLoaderInfoSL(ClassLoaderContext* context, |
| 207 | size_t loader_index, |
| 208 | size_t shared_library_index, |
| 209 | ClassLoaderContext::ClassLoaderType type, |
| 210 | const std::string& classpath) { |
| 211 | ASSERT_TRUE(context != nullptr); |
| 212 | ASSERT_GT(context->GetParentChainSize(), loader_index); |
| 213 | const ClassLoaderContext::ClassLoaderInfo& info = *context->GetParent(loader_index); |
| 214 | ASSERT_GT(info.shared_libraries.size(), shared_library_index); |
| 215 | const ClassLoaderContext::ClassLoaderInfo& sl = |
| 216 | *info.shared_libraries[shared_library_index].get(); |
| 217 | ASSERT_EQ(type, info.type); |
| 218 | std::vector<std::string> expected_classpath; |
| 219 | Split(classpath, ':', &expected_classpath); |
| 220 | ASSERT_EQ(expected_classpath, sl.classpath); |
| 221 | } |
| 222 | |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 223 | void VerifyClassLoaderFromTestDex(ClassLoaderContext* context, |
| 224 | size_t index, |
| 225 | ClassLoaderContext::ClassLoaderType type, |
| 226 | const std::string& test_name) { |
| 227 | std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles(test_name.c_str()); |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 228 | |
| 229 | VerifyClassLoaderInfo(context, index, type, GetTestDexFileName(test_name.c_str())); |
Calin Juravle | c5b215f | 2017-09-12 14:49:37 -0700 | [diff] [blame] | 230 | VerifyOpenDexFiles(context, index, &dex_files); |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 231 | } |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 232 | }; |
| 233 | |
Calin Juravle | 1a509c8 | 2017-07-24 16:51:21 -0700 | [diff] [blame] | 234 | TEST_F(ClassLoaderContextTest, ParseValidEmptyContext) { |
| 235 | std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::Create(""); |
| 236 | // An empty context should create a single empty PathClassLoader. |
| 237 | VerifyContextSize(context.get(), 1); |
| 238 | VerifyClassLoaderPCL(context.get(), 0, ""); |
| 239 | } |
| 240 | |
| 241 | TEST_F(ClassLoaderContextTest, ParseValidSharedLibraryContext) { |
| 242 | std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::Create("&"); |
| 243 | // An shared library context should have no class loader in the chain. |
| 244 | VerifyContextSize(context.get(), 0); |
| 245 | } |
| 246 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 247 | TEST_F(ClassLoaderContextTest, ParseValidContextPCL) { |
| 248 | std::unique_ptr<ClassLoaderContext> context = |
| 249 | ClassLoaderContext::Create("PCL[a.dex]"); |
| 250 | VerifyContextSize(context.get(), 1); |
| 251 | VerifyClassLoaderPCL(context.get(), 0, "a.dex"); |
| 252 | } |
| 253 | |
| 254 | TEST_F(ClassLoaderContextTest, ParseValidContextDLC) { |
| 255 | std::unique_ptr<ClassLoaderContext> context = |
| 256 | ClassLoaderContext::Create("DLC[a.dex]"); |
| 257 | VerifyContextSize(context.get(), 1); |
| 258 | VerifyClassLoaderDLC(context.get(), 0, "a.dex"); |
| 259 | } |
| 260 | |
| 261 | TEST_F(ClassLoaderContextTest, ParseValidContextChain) { |
| 262 | std::unique_ptr<ClassLoaderContext> context = |
| 263 | ClassLoaderContext::Create("PCL[a.dex:b.dex];DLC[c.dex:d.dex];PCL[e.dex]"); |
| 264 | VerifyContextSize(context.get(), 3); |
| 265 | VerifyClassLoaderPCL(context.get(), 0, "a.dex:b.dex"); |
| 266 | VerifyClassLoaderDLC(context.get(), 1, "c.dex:d.dex"); |
| 267 | VerifyClassLoaderPCL(context.get(), 2, "e.dex"); |
| 268 | } |
| 269 | |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame^] | 270 | TEST_F(ClassLoaderContextTest, ParseSharedLibraries) { |
| 271 | std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::Create( |
| 272 | "PCL[a.dex:b.dex]{PCL[s1.dex]#PCL[s2.dex:s3.dex]};DLC[c.dex:d.dex]{DLC[s4.dex]}"); |
| 273 | VerifyContextSize(context.get(), 2); |
| 274 | VerifyClassLoaderSharedLibraryPCL(context.get(), 0, 0, "s1.dex"); |
| 275 | VerifyClassLoaderSharedLibraryPCL(context.get(), 0, 1, "s2.dex:s3.dex"); |
| 276 | VerifyClassLoaderDLC(context.get(), 1, "c.dex:d.dex"); |
| 277 | VerifyClassLoaderSharedLibraryDLC(context.get(), 1, 0, "s4.dex"); |
| 278 | } |
| 279 | |
| 280 | TEST_F(ClassLoaderContextTest, ParseEnclosingSharedLibraries) { |
| 281 | std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::Create( |
| 282 | "PCL[a.dex:b.dex]{PCL[s1.dex]{PCL[s2.dex:s3.dex];PCL[s4.dex]}}"); |
| 283 | VerifyContextSize(context.get(), 1); |
| 284 | VerifyClassLoaderSharedLibraryPCL(context.get(), 0, 0, "s1.dex"); |
| 285 | } |
| 286 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 287 | TEST_F(ClassLoaderContextTest, ParseValidEmptyContextDLC) { |
| 288 | std::unique_ptr<ClassLoaderContext> context = |
| 289 | ClassLoaderContext::Create("DLC[]"); |
| 290 | VerifyContextSize(context.get(), 1); |
| 291 | VerifyClassLoaderDLC(context.get(), 0, ""); |
| 292 | } |
| 293 | |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame^] | 294 | TEST_F(ClassLoaderContextTest, ParseValidEmptyContextSharedLibrary) { |
| 295 | std::unique_ptr<ClassLoaderContext> context = |
| 296 | ClassLoaderContext::Create("DLC[]{}"); |
| 297 | VerifyContextSize(context.get(), 1); |
| 298 | VerifySharedLibrariesSize(context.get(), 0, 0); |
| 299 | } |
| 300 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 301 | TEST_F(ClassLoaderContextTest, ParseValidContextSpecialSymbol) { |
| 302 | std::unique_ptr<ClassLoaderContext> context = |
| 303 | ClassLoaderContext::Create(OatFile::kSpecialSharedLibrary); |
| 304 | VerifyContextSize(context.get(), 0); |
| 305 | } |
| 306 | |
| 307 | TEST_F(ClassLoaderContextTest, ParseInvalidValidContexts) { |
| 308 | ASSERT_TRUE(nullptr == ClassLoaderContext::Create("ABC[a.dex]")); |
| 309 | ASSERT_TRUE(nullptr == ClassLoaderContext::Create("PCL")); |
| 310 | ASSERT_TRUE(nullptr == ClassLoaderContext::Create("PCL[a.dex")); |
| 311 | ASSERT_TRUE(nullptr == ClassLoaderContext::Create("PCLa.dex]")); |
| 312 | ASSERT_TRUE(nullptr == ClassLoaderContext::Create("PCL{a.dex}")); |
| 313 | ASSERT_TRUE(nullptr == ClassLoaderContext::Create("PCL[a.dex];DLC[b.dex")); |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame^] | 314 | ASSERT_TRUE(nullptr == ClassLoaderContext::Create("PCL[a.dex]{ABC};DLC[b.dex")); |
| 315 | ASSERT_TRUE(nullptr == ClassLoaderContext::Create("PCL[a.dex]{};DLC[b.dex")); |
| 316 | ASSERT_TRUE(nullptr == ClassLoaderContext::Create("DLC[s4.dex]}")); |
| 317 | ASSERT_TRUE(nullptr == ClassLoaderContext::Create("DLC[s4.dex]{")); |
| 318 | ASSERT_TRUE(nullptr == ClassLoaderContext::Create("DLC{DLC[s4.dex]}")); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | TEST_F(ClassLoaderContextTest, OpenInvalidDexFiles) { |
| 322 | std::unique_ptr<ClassLoaderContext> context = |
| 323 | ClassLoaderContext::Create("PCL[does_not_exist.dex]"); |
| 324 | VerifyContextSize(context.get(), 1); |
| 325 | ASSERT_FALSE(context->OpenDexFiles(InstructionSet::kArm, ".")); |
| 326 | } |
| 327 | |
| 328 | TEST_F(ClassLoaderContextTest, OpenValidDexFiles) { |
| 329 | std::string multidex_name = GetTestDexFileName("MultiDex"); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 330 | std::string myclass_dex_name = GetTestDexFileName("MyClass"); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 331 | std::string dex_name = GetTestDexFileName("Main"); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 332 | |
Calin Juravle | 92003fe | 2017-09-06 02:22:57 +0000 | [diff] [blame] | 333 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 334 | std::unique_ptr<ClassLoaderContext> context = |
| 335 | ClassLoaderContext::Create( |
| 336 | "PCL[" + multidex_name + ":" + myclass_dex_name + "];" + |
| 337 | "DLC[" + dex_name + "]"); |
| 338 | |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 339 | ASSERT_TRUE(context->OpenDexFiles(InstructionSet::kArm, /*classpath_dir=*/ "")); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 340 | |
| 341 | VerifyContextSize(context.get(), 2); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 342 | |
Calin Juravle | c5b215f | 2017-09-12 14:49:37 -0700 | [diff] [blame] | 343 | std::vector<std::unique_ptr<const DexFile>> all_dex_files0 = OpenTestDexFiles("MultiDex"); |
| 344 | std::vector<std::unique_ptr<const DexFile>> myclass_dex_files = OpenTestDexFiles("MyClass"); |
| 345 | for (size_t i = 0; i < myclass_dex_files.size(); i++) { |
| 346 | all_dex_files0.emplace_back(myclass_dex_files[i].release()); |
| 347 | } |
| 348 | VerifyOpenDexFiles(context.get(), 0, &all_dex_files0); |
| 349 | |
| 350 | std::vector<std::unique_ptr<const DexFile>> all_dex_files1 = OpenTestDexFiles("Main"); |
| 351 | VerifyOpenDexFiles(context.get(), 1, &all_dex_files1); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Alex Light | 77ef93b | 2018-01-12 11:18:31 -0800 | [diff] [blame] | 354 | // Creates a relative path from cwd to 'in'. Returns false if it cannot be done. |
| 355 | // TODO We should somehow support this in all situations. b/72042237. |
| 356 | static bool CreateRelativeString(const std::string& in, const char* cwd, std::string* out) { |
Nicolas Geoffray | c4be2c8 | 2017-09-05 12:40:06 +0100 | [diff] [blame] | 357 | int cwd_len = strlen(cwd); |
| 358 | if (!android::base::StartsWith(in, cwd) || (cwd_len < 1)) { |
Alex Light | 77ef93b | 2018-01-12 11:18:31 -0800 | [diff] [blame] | 359 | return false; |
Andreas Gampe | 7252738 | 2017-09-02 16:53:03 -0700 | [diff] [blame] | 360 | } |
Nicolas Geoffray | c4be2c8 | 2017-09-05 12:40:06 +0100 | [diff] [blame] | 361 | bool contains_trailing_slash = (cwd[cwd_len - 1] == '/'); |
| 362 | int start_position = cwd_len + (contains_trailing_slash ? 0 : 1); |
Alex Light | 77ef93b | 2018-01-12 11:18:31 -0800 | [diff] [blame] | 363 | *out = in.substr(start_position); |
| 364 | return true; |
Andreas Gampe | 7252738 | 2017-09-02 16:53:03 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | TEST_F(ClassLoaderContextTest, OpenValidDexFilesRelative) { |
| 368 | char cwd_buf[4096]; |
| 369 | if (getcwd(cwd_buf, arraysize(cwd_buf)) == nullptr) { |
| 370 | PLOG(FATAL) << "Could not get working directory"; |
| 371 | } |
Alex Light | 77ef93b | 2018-01-12 11:18:31 -0800 | [diff] [blame] | 372 | std::string multidex_name; |
| 373 | std::string myclass_dex_name; |
| 374 | std::string dex_name; |
| 375 | if (!CreateRelativeString(GetTestDexFileName("MultiDex"), cwd_buf, &multidex_name) || |
| 376 | !CreateRelativeString(GetTestDexFileName("MyClass"), cwd_buf, &myclass_dex_name) || |
| 377 | !CreateRelativeString(GetTestDexFileName("Main"), cwd_buf, &dex_name)) { |
| 378 | LOG(ERROR) << "Test OpenValidDexFilesRelative cannot be run because target dex files have no " |
| 379 | << "relative path."; |
| 380 | SUCCEED(); |
| 381 | return; |
| 382 | } |
Andreas Gampe | 7252738 | 2017-09-02 16:53:03 -0700 | [diff] [blame] | 383 | |
| 384 | |
| 385 | std::unique_ptr<ClassLoaderContext> context = |
| 386 | ClassLoaderContext::Create( |
| 387 | "PCL[" + multidex_name + ":" + myclass_dex_name + "];" + |
| 388 | "DLC[" + dex_name + "]"); |
| 389 | |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 390 | ASSERT_TRUE(context->OpenDexFiles(InstructionSet::kArm, /*classpath_dir=*/ "")); |
Andreas Gampe | 7252738 | 2017-09-02 16:53:03 -0700 | [diff] [blame] | 391 | |
Calin Juravle | c5b215f | 2017-09-12 14:49:37 -0700 | [diff] [blame] | 392 | std::vector<std::unique_ptr<const DexFile>> all_dex_files0 = OpenTestDexFiles("MultiDex"); |
| 393 | std::vector<std::unique_ptr<const DexFile>> myclass_dex_files = OpenTestDexFiles("MyClass"); |
| 394 | for (size_t i = 0; i < myclass_dex_files.size(); i++) { |
| 395 | all_dex_files0.emplace_back(myclass_dex_files[i].release()); |
| 396 | } |
| 397 | VerifyOpenDexFiles(context.get(), 0, &all_dex_files0); |
Andreas Gampe | 7252738 | 2017-09-02 16:53:03 -0700 | [diff] [blame] | 398 | |
Calin Juravle | c5b215f | 2017-09-12 14:49:37 -0700 | [diff] [blame] | 399 | std::vector<std::unique_ptr<const DexFile>> all_dex_files1 = OpenTestDexFiles("Main"); |
| 400 | VerifyOpenDexFiles(context.get(), 1, &all_dex_files1); |
Andreas Gampe | 7252738 | 2017-09-02 16:53:03 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | TEST_F(ClassLoaderContextTest, OpenValidDexFilesClasspathDir) { |
| 404 | char cwd_buf[4096]; |
| 405 | if (getcwd(cwd_buf, arraysize(cwd_buf)) == nullptr) { |
| 406 | PLOG(FATAL) << "Could not get working directory"; |
| 407 | } |
Alex Light | 77ef93b | 2018-01-12 11:18:31 -0800 | [diff] [blame] | 408 | std::string multidex_name; |
| 409 | std::string myclass_dex_name; |
| 410 | std::string dex_name; |
| 411 | if (!CreateRelativeString(GetTestDexFileName("MultiDex"), cwd_buf, &multidex_name) || |
| 412 | !CreateRelativeString(GetTestDexFileName("MyClass"), cwd_buf, &myclass_dex_name) || |
| 413 | !CreateRelativeString(GetTestDexFileName("Main"), cwd_buf, &dex_name)) { |
| 414 | LOG(ERROR) << "Test OpenValidDexFilesClasspathDir cannot be run because target dex files have " |
| 415 | << "no relative path."; |
| 416 | SUCCEED(); |
| 417 | return; |
| 418 | } |
Andreas Gampe | 7252738 | 2017-09-02 16:53:03 -0700 | [diff] [blame] | 419 | std::unique_ptr<ClassLoaderContext> context = |
| 420 | ClassLoaderContext::Create( |
| 421 | "PCL[" + multidex_name + ":" + myclass_dex_name + "];" + |
| 422 | "DLC[" + dex_name + "]"); |
| 423 | |
| 424 | ASSERT_TRUE(context->OpenDexFiles(InstructionSet::kArm, cwd_buf)); |
| 425 | |
| 426 | VerifyContextSize(context.get(), 2); |
Calin Juravle | c5b215f | 2017-09-12 14:49:37 -0700 | [diff] [blame] | 427 | std::vector<std::unique_ptr<const DexFile>> all_dex_files0 = OpenTestDexFiles("MultiDex"); |
| 428 | std::vector<std::unique_ptr<const DexFile>> myclass_dex_files = OpenTestDexFiles("MyClass"); |
| 429 | for (size_t i = 0; i < myclass_dex_files.size(); i++) { |
| 430 | all_dex_files0.emplace_back(myclass_dex_files[i].release()); |
| 431 | } |
| 432 | VerifyOpenDexFiles(context.get(), 0, &all_dex_files0); |
Andreas Gampe | 7252738 | 2017-09-02 16:53:03 -0700 | [diff] [blame] | 433 | |
Calin Juravle | c5b215f | 2017-09-12 14:49:37 -0700 | [diff] [blame] | 434 | std::vector<std::unique_ptr<const DexFile>> all_dex_files1 = OpenTestDexFiles("Main"); |
| 435 | VerifyOpenDexFiles(context.get(), 1, &all_dex_files1); |
Andreas Gampe | 7252738 | 2017-09-02 16:53:03 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 438 | TEST_F(ClassLoaderContextTest, OpenInvalidDexFilesMix) { |
| 439 | std::string dex_name = GetTestDexFileName("Main"); |
| 440 | std::unique_ptr<ClassLoaderContext> context = |
| 441 | ClassLoaderContext::Create("PCL[does_not_exist.dex];DLC[" + dex_name + "]"); |
| 442 | ASSERT_FALSE(context->OpenDexFiles(InstructionSet::kArm, "")); |
| 443 | } |
| 444 | |
| 445 | TEST_F(ClassLoaderContextTest, CreateClassLoader) { |
| 446 | std::string dex_name = GetTestDexFileName("Main"); |
| 447 | std::unique_ptr<ClassLoaderContext> context = |
| 448 | ClassLoaderContext::Create("PCL[" + dex_name + "]"); |
| 449 | ASSERT_TRUE(context->OpenDexFiles(InstructionSet::kArm, "")); |
| 450 | |
| 451 | std::vector<std::unique_ptr<const DexFile>> classpath_dex = OpenTestDexFiles("Main"); |
| 452 | std::vector<std::unique_ptr<const DexFile>> compilation_sources = OpenTestDexFiles("MultiDex"); |
| 453 | |
| 454 | std::vector<const DexFile*> compilation_sources_raw = |
| 455 | MakeNonOwningPointerVector(compilation_sources); |
| 456 | jobject jclass_loader = context->CreateClassLoader(compilation_sources_raw); |
| 457 | ASSERT_TRUE(jclass_loader != nullptr); |
| 458 | |
| 459 | ScopedObjectAccess soa(Thread::Current()); |
| 460 | |
Calin Juravle | c79470d | 2017-07-12 17:37:42 -0700 | [diff] [blame] | 461 | StackHandleScope<1> hs(soa.Self()); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 462 | Handle<mirror::ClassLoader> class_loader = hs.NewHandle( |
| 463 | soa.Decode<mirror::ClassLoader>(jclass_loader)); |
| 464 | |
| 465 | ASSERT_TRUE(class_loader->GetClass() == |
| 466 | soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_PathClassLoader)); |
| 467 | ASSERT_TRUE(class_loader->GetParent()->GetClass() == |
| 468 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_BootClassLoader)); |
| 469 | |
Calin Juravle | c79470d | 2017-07-12 17:37:42 -0700 | [diff] [blame] | 470 | // For the first class loader the class path dex files must come first and then the |
| 471 | // compilation sources. |
| 472 | std::vector<const DexFile*> expected_classpath = MakeNonOwningPointerVector(classpath_dex); |
| 473 | for (auto& dex : compilation_sources_raw) { |
| 474 | expected_classpath.push_back(dex); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Calin Juravle | c79470d | 2017-07-12 17:37:42 -0700 | [diff] [blame] | 477 | VerifyClassLoaderDexFiles(soa, |
| 478 | class_loader, |
| 479 | WellKnownClasses::dalvik_system_PathClassLoader, |
| 480 | expected_classpath); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 483 | TEST_F(ClassLoaderContextTest, CreateClassLoaderWithEmptyContext) { |
| 484 | std::unique_ptr<ClassLoaderContext> context = |
| 485 | ClassLoaderContext::Create(""); |
| 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 | |
Calin Juravle | c79470d | 2017-07-12 17:37:42 -0700 | [diff] [blame] | 497 | StackHandleScope<1> hs(soa.Self()); |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 498 | Handle<mirror::ClassLoader> class_loader = hs.NewHandle( |
| 499 | soa.Decode<mirror::ClassLoader>(jclass_loader)); |
| 500 | |
Calin Juravle | c79470d | 2017-07-12 17:37:42 -0700 | [diff] [blame] | 501 | // An empty context should create a single PathClassLoader with only the compilation sources. |
| 502 | VerifyClassLoaderDexFiles(soa, |
| 503 | class_loader, |
| 504 | WellKnownClasses::dalvik_system_PathClassLoader, |
| 505 | compilation_sources_raw); |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 506 | ASSERT_TRUE(class_loader->GetParent()->GetClass() == |
| 507 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_BootClassLoader)); |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Calin Juravle | 1a509c8 | 2017-07-24 16:51:21 -0700 | [diff] [blame] | 510 | TEST_F(ClassLoaderContextTest, CreateClassLoaderWithSharedLibraryContext) { |
| 511 | std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::Create("&"); |
| 512 | |
| 513 | ASSERT_TRUE(context->OpenDexFiles(InstructionSet::kArm, "")); |
| 514 | |
| 515 | std::vector<std::unique_ptr<const DexFile>> compilation_sources = OpenTestDexFiles("MultiDex"); |
| 516 | |
| 517 | std::vector<const DexFile*> compilation_sources_raw = |
| 518 | MakeNonOwningPointerVector(compilation_sources); |
| 519 | jobject jclass_loader = context->CreateClassLoader(compilation_sources_raw); |
| 520 | ASSERT_TRUE(jclass_loader != nullptr); |
| 521 | |
| 522 | ScopedObjectAccess soa(Thread::Current()); |
| 523 | |
| 524 | StackHandleScope<1> hs(soa.Self()); |
| 525 | Handle<mirror::ClassLoader> class_loader = hs.NewHandle( |
| 526 | soa.Decode<mirror::ClassLoader>(jclass_loader)); |
| 527 | |
| 528 | // A shared library context should create a single PathClassLoader with only the compilation |
| 529 | // sources. |
| 530 | VerifyClassLoaderDexFiles(soa, |
| 531 | class_loader, |
| 532 | WellKnownClasses::dalvik_system_PathClassLoader, |
| 533 | compilation_sources_raw); |
| 534 | ASSERT_TRUE(class_loader->GetParent()->GetClass() == |
| 535 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_BootClassLoader)); |
| 536 | } |
| 537 | |
Calin Juravle | c79470d | 2017-07-12 17:37:42 -0700 | [diff] [blame] | 538 | TEST_F(ClassLoaderContextTest, CreateClassLoaderWithComplexChain) { |
| 539 | // Setup the context. |
| 540 | std::vector<std::unique_ptr<const DexFile>> classpath_dex_a = OpenTestDexFiles("ForClassLoaderA"); |
| 541 | std::vector<std::unique_ptr<const DexFile>> classpath_dex_b = OpenTestDexFiles("ForClassLoaderB"); |
| 542 | std::vector<std::unique_ptr<const DexFile>> classpath_dex_c = OpenTestDexFiles("ForClassLoaderC"); |
| 543 | std::vector<std::unique_ptr<const DexFile>> classpath_dex_d = OpenTestDexFiles("ForClassLoaderD"); |
| 544 | |
| 545 | std::string context_spec = |
| 546 | "PCL[" + CreateClassPath(classpath_dex_a) + ":" + CreateClassPath(classpath_dex_b) + "];" + |
| 547 | "DLC[" + CreateClassPath(classpath_dex_c) + "];" + |
| 548 | "PCL[" + CreateClassPath(classpath_dex_d) + "]"; |
| 549 | |
| 550 | std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::Create(context_spec); |
| 551 | ASSERT_TRUE(context->OpenDexFiles(InstructionSet::kArm, "")); |
| 552 | |
| 553 | // Setup the compilation sources. |
| 554 | std::vector<std::unique_ptr<const DexFile>> compilation_sources = OpenTestDexFiles("MultiDex"); |
| 555 | std::vector<const DexFile*> compilation_sources_raw = |
| 556 | MakeNonOwningPointerVector(compilation_sources); |
| 557 | |
| 558 | // Create the class loader. |
| 559 | jobject jclass_loader = context->CreateClassLoader(compilation_sources_raw); |
| 560 | ASSERT_TRUE(jclass_loader != nullptr); |
| 561 | |
| 562 | // Verify the class loader. |
| 563 | ScopedObjectAccess soa(Thread::Current()); |
| 564 | |
| 565 | StackHandleScope<3> hs(soa.Self()); |
| 566 | Handle<mirror::ClassLoader> class_loader_1 = hs.NewHandle( |
| 567 | soa.Decode<mirror::ClassLoader>(jclass_loader)); |
| 568 | |
| 569 | // Verify the first class loader |
| 570 | |
| 571 | // For the first class loader the class path dex files must come first and then the |
| 572 | // compilation sources. |
| 573 | std::vector<const DexFile*> class_loader_1_dex_files = |
| 574 | MakeNonOwningPointerVector(classpath_dex_a); |
| 575 | for (auto& dex : classpath_dex_b) { |
| 576 | class_loader_1_dex_files.push_back(dex.get()); |
| 577 | } |
| 578 | for (auto& dex : compilation_sources_raw) { |
| 579 | class_loader_1_dex_files.push_back(dex); |
| 580 | } |
| 581 | VerifyClassLoaderDexFiles(soa, |
| 582 | class_loader_1, |
| 583 | WellKnownClasses::dalvik_system_PathClassLoader, |
| 584 | class_loader_1_dex_files); |
| 585 | |
| 586 | // Verify the second class loader |
| 587 | Handle<mirror::ClassLoader> class_loader_2 = hs.NewHandle(class_loader_1->GetParent()); |
| 588 | std::vector<const DexFile*> class_loader_2_dex_files = |
| 589 | MakeNonOwningPointerVector(classpath_dex_c); |
| 590 | VerifyClassLoaderDexFiles(soa, |
| 591 | class_loader_2, |
| 592 | WellKnownClasses::dalvik_system_DelegateLastClassLoader, |
| 593 | class_loader_2_dex_files); |
| 594 | |
| 595 | // Verify the third class loader |
| 596 | Handle<mirror::ClassLoader> class_loader_3 = hs.NewHandle(class_loader_2->GetParent()); |
| 597 | std::vector<const DexFile*> class_loader_3_dex_files = |
| 598 | MakeNonOwningPointerVector(classpath_dex_d); |
| 599 | VerifyClassLoaderDexFiles(soa, |
| 600 | class_loader_3, |
| 601 | WellKnownClasses::dalvik_system_PathClassLoader, |
| 602 | class_loader_3_dex_files); |
| 603 | // The last class loader should have the BootClassLoader as a parent. |
| 604 | ASSERT_TRUE(class_loader_3->GetParent()->GetClass() == |
| 605 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_BootClassLoader)); |
| 606 | } |
| 607 | |
| 608 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 609 | TEST_F(ClassLoaderContextTest, RemoveSourceLocations) { |
| 610 | std::unique_ptr<ClassLoaderContext> context = |
| 611 | ClassLoaderContext::Create("PCL[a.dex]"); |
| 612 | dchecked_vector<std::string> classpath_dex; |
| 613 | classpath_dex.push_back("a.dex"); |
| 614 | dchecked_vector<std::string> compilation_sources; |
| 615 | compilation_sources.push_back("src.dex"); |
| 616 | |
| 617 | // Nothing should be removed. |
| 618 | ASSERT_FALSE(context->RemoveLocationsFromClassPaths(compilation_sources)); |
| 619 | VerifyClassLoaderPCL(context.get(), 0, "a.dex"); |
| 620 | // Classes should be removed. |
| 621 | ASSERT_TRUE(context->RemoveLocationsFromClassPaths(classpath_dex)); |
| 622 | VerifyClassLoaderPCL(context.get(), 0, ""); |
| 623 | } |
| 624 | |
| 625 | TEST_F(ClassLoaderContextTest, EncodeInOatFile) { |
| 626 | std::string dex1_name = GetTestDexFileName("Main"); |
| 627 | std::string dex2_name = GetTestDexFileName("MyClass"); |
| 628 | std::unique_ptr<ClassLoaderContext> context = |
| 629 | ClassLoaderContext::Create("PCL[" + dex1_name + ":" + dex2_name + "]"); |
| 630 | ASSERT_TRUE(context->OpenDexFiles(InstructionSet::kArm, "")); |
| 631 | |
| 632 | std::vector<std::unique_ptr<const DexFile>> dex1 = OpenTestDexFiles("Main"); |
| 633 | std::vector<std::unique_ptr<const DexFile>> dex2 = OpenTestDexFiles("MyClass"); |
| 634 | std::string encoding = context->EncodeContextForOatFile(""); |
Calin Juravle | c79470d | 2017-07-12 17:37:42 -0700 | [diff] [blame] | 635 | std::string expected_encoding = "PCL[" + CreateClassPathWithChecksums(dex1) + ":" + |
| 636 | CreateClassPathWithChecksums(dex2) + "]"; |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 637 | ASSERT_EQ(expected_encoding, context->EncodeContextForOatFile("")); |
| 638 | } |
| 639 | |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 640 | TEST_F(ClassLoaderContextTest, EncodeForDex2oat) { |
| 641 | std::string dex1_name = GetTestDexFileName("Main"); |
| 642 | std::string dex2_name = GetTestDexFileName("MultiDex"); |
| 643 | std::unique_ptr<ClassLoaderContext> context = |
| 644 | ClassLoaderContext::Create("PCL[" + dex1_name + ":" + dex2_name + "]"); |
| 645 | ASSERT_TRUE(context->OpenDexFiles(InstructionSet::kArm, "")); |
| 646 | |
| 647 | std::vector<std::unique_ptr<const DexFile>> dex1 = OpenTestDexFiles("Main"); |
| 648 | std::vector<std::unique_ptr<const DexFile>> dex2 = OpenTestDexFiles("MultiDex"); |
| 649 | std::string encoding = context->EncodeContextForDex2oat(""); |
| 650 | std::string expected_encoding = "PCL[" + dex1_name + ":" + dex2_name + "]"; |
| 651 | ASSERT_EQ(expected_encoding, context->EncodeContextForDex2oat("")); |
| 652 | } |
| 653 | |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 654 | // TODO(calin) add a test which creates the context for a class loader together with dex_elements. |
| 655 | TEST_F(ClassLoaderContextTest, CreateContextForClassLoader) { |
| 656 | // The chain is |
| 657 | // ClassLoaderA (PathClassLoader) |
| 658 | // ^ |
| 659 | // | |
| 660 | // ClassLoaderB (DelegateLastClassLoader) |
| 661 | // ^ |
| 662 | // | |
| 663 | // ClassLoaderC (PathClassLoader) |
| 664 | // ^ |
| 665 | // | |
| 666 | // ClassLoaderD (DelegateLastClassLoader) |
| 667 | |
| 668 | jobject class_loader_a = LoadDexInPathClassLoader("ForClassLoaderA", nullptr); |
| 669 | jobject class_loader_b = LoadDexInDelegateLastClassLoader("ForClassLoaderB", class_loader_a); |
| 670 | jobject class_loader_c = LoadDexInPathClassLoader("ForClassLoaderC", class_loader_b); |
| 671 | jobject class_loader_d = LoadDexInDelegateLastClassLoader("ForClassLoaderD", class_loader_c); |
| 672 | |
| 673 | std::unique_ptr<ClassLoaderContext> context = CreateContextForClassLoader(class_loader_d); |
| 674 | |
| 675 | VerifyContextForClassLoader(context.get()); |
| 676 | VerifyContextSize(context.get(), 4); |
| 677 | |
| 678 | VerifyClassLoaderDLCFromTestDex(context.get(), 0, "ForClassLoaderD"); |
| 679 | VerifyClassLoaderPCLFromTestDex(context.get(), 1, "ForClassLoaderC"); |
| 680 | VerifyClassLoaderDLCFromTestDex(context.get(), 2, "ForClassLoaderB"); |
| 681 | VerifyClassLoaderPCLFromTestDex(context.get(), 3, "ForClassLoaderA"); |
| 682 | } |
| 683 | |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 684 | |
| 685 | TEST_F(ClassLoaderContextTest, VerifyClassLoaderContextFirstElement) { |
| 686 | std::string context_spec = "PCL[]"; |
| 687 | std::unique_ptr<ClassLoaderContext> context = ParseContextWithChecksums(context_spec); |
| 688 | ASSERT_TRUE(context != nullptr); |
| 689 | PretendContextOpenedDexFiles(context.get()); |
| 690 | // Ensure that the special shared library marks as verified for the first thing in the class path. |
| 691 | ASSERT_EQ(context->VerifyClassLoaderContextMatch(OatFile::kSpecialSharedLibrary), |
| 692 | ClassLoaderContext::VerificationResult::kVerifies); |
| 693 | } |
| 694 | |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 695 | TEST_F(ClassLoaderContextTest, VerifyClassLoaderContextMatch) { |
| 696 | std::string context_spec = "PCL[a.dex*123:b.dex*456];DLC[c.dex*890]"; |
| 697 | std::unique_ptr<ClassLoaderContext> context = ParseContextWithChecksums(context_spec); |
Calin Juravle | c5b215f | 2017-09-12 14:49:37 -0700 | [diff] [blame] | 698 | // Pretend that we successfully open the dex files to pass the DCHECKS. |
| 699 | // (as it's much easier to test all the corner cases without relying on actual dex files). |
| 700 | PretendContextOpenedDexFiles(context.get()); |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 701 | |
| 702 | VerifyContextSize(context.get(), 2); |
| 703 | VerifyClassLoaderPCL(context.get(), 0, "a.dex:b.dex"); |
| 704 | VerifyClassLoaderDLC(context.get(), 1, "c.dex"); |
| 705 | |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 706 | ASSERT_EQ(context->VerifyClassLoaderContextMatch(context_spec), |
| 707 | ClassLoaderContext::VerificationResult::kVerifies); |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 708 | |
| 709 | std::string wrong_class_loader_type = "PCL[a.dex*123:b.dex*456];PCL[c.dex*890]"; |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 710 | ASSERT_EQ(context->VerifyClassLoaderContextMatch(wrong_class_loader_type), |
| 711 | ClassLoaderContext::VerificationResult::kMismatch); |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 712 | |
| 713 | std::string wrong_class_loader_order = "DLC[c.dex*890];PCL[a.dex*123:b.dex*456]"; |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 714 | ASSERT_EQ(context->VerifyClassLoaderContextMatch(wrong_class_loader_order), |
| 715 | ClassLoaderContext::VerificationResult::kMismatch); |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 716 | |
| 717 | std::string wrong_classpath_order = "PCL[b.dex*456:a.dex*123];DLC[c.dex*890]"; |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 718 | ASSERT_EQ(context->VerifyClassLoaderContextMatch(wrong_classpath_order), |
| 719 | ClassLoaderContext::VerificationResult::kMismatch); |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 720 | |
| 721 | std::string wrong_checksum = "PCL[a.dex*999:b.dex*456];DLC[c.dex*890]"; |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 722 | ASSERT_EQ(context->VerifyClassLoaderContextMatch(wrong_checksum), |
| 723 | ClassLoaderContext::VerificationResult::kMismatch); |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 724 | |
| 725 | std::string wrong_extra_class_loader = "PCL[a.dex*123:b.dex*456];DLC[c.dex*890];PCL[d.dex*321]"; |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 726 | ASSERT_EQ(context->VerifyClassLoaderContextMatch(wrong_extra_class_loader), |
| 727 | ClassLoaderContext::VerificationResult::kMismatch); |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 728 | |
| 729 | std::string wrong_extra_classpath = "PCL[a.dex*123:b.dex*456];DLC[c.dex*890:d.dex*321]"; |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 730 | ASSERT_EQ(context->VerifyClassLoaderContextMatch(wrong_extra_classpath), |
| 731 | ClassLoaderContext::VerificationResult::kMismatch); |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 732 | |
| 733 | std::string wrong_spec = "PCL[a.dex*999:b.dex*456];DLC["; |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 734 | ASSERT_EQ(context->VerifyClassLoaderContextMatch(wrong_spec), |
| 735 | ClassLoaderContext::VerificationResult::kMismatch); |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 736 | } |
| 737 | |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame^] | 738 | TEST_F(ClassLoaderContextTest, VerifyClassLoaderContextMatchWithSL) { |
| 739 | std::string context_spec = |
| 740 | "PCL[a.dex*123:b.dex*456]{PCL[d.dex*321];PCL[e.dex*654]#PCL[f.dex*098:g.dex*999]}" |
| 741 | ";DLC[c.dex*890]"; |
| 742 | std::unique_ptr<ClassLoaderContext> context = ParseContextWithChecksums(context_spec); |
| 743 | // Pretend that we successfully open the dex files to pass the DCHECKS. |
| 744 | // (as it's much easier to test all the corner cases without relying on actual dex files). |
| 745 | PretendContextOpenedDexFiles(context.get()); |
| 746 | |
| 747 | VerifyContextSize(context.get(), 2); |
| 748 | VerifyClassLoaderPCL(context.get(), 0, "a.dex:b.dex"); |
| 749 | VerifyClassLoaderDLC(context.get(), 1, "c.dex"); |
| 750 | VerifyClassLoaderSharedLibraryPCL(context.get(), 0, 0, "d.dex"); |
| 751 | VerifyClassLoaderSharedLibraryPCL(context.get(), 0, 1, "f.dex:g.dex"); |
| 752 | |
| 753 | ASSERT_EQ(context->VerifyClassLoaderContextMatch(context_spec), |
| 754 | ClassLoaderContext::VerificationResult::kVerifies); |
| 755 | |
| 756 | std::string wrong_class_loader_type = |
| 757 | "PCL[a.dex*123:b.dex*456]{DLC[d.dex*321];PCL[e.dex*654]#PCL[f.dex*098:g.dex*999]}" |
| 758 | ";DLC[c.dex*890]"; |
| 759 | ASSERT_EQ(context->VerifyClassLoaderContextMatch(wrong_class_loader_type), |
| 760 | ClassLoaderContext::VerificationResult::kMismatch); |
| 761 | |
| 762 | std::string wrong_class_loader_order = |
| 763 | "PCL[a.dex*123:b.dex*456]{PCL[f.dex#098:g.dex#999}#PCL[d.dex*321];PCL[e.dex*654]}" |
| 764 | ";DLC[c.dex*890]"; |
| 765 | ASSERT_EQ(context->VerifyClassLoaderContextMatch(wrong_class_loader_order), |
| 766 | ClassLoaderContext::VerificationResult::kMismatch); |
| 767 | |
| 768 | std::string wrong_classpath_order = |
| 769 | "PCL[a.dex*123:b.dex*456]{PCL[d.dex*321];PCL[e.dex*654]#PCL[g.dex*999:f.dex*098]}" |
| 770 | ";DLC[c.dex*890]"; |
| 771 | ASSERT_EQ(context->VerifyClassLoaderContextMatch(wrong_classpath_order), |
| 772 | ClassLoaderContext::VerificationResult::kMismatch); |
| 773 | |
| 774 | std::string wrong_checksum = |
| 775 | "PCL[a.dex*123:b.dex*456]{PCL[d.dex*333];PCL[e.dex*654]#PCL[g.dex*999:f.dex*098]}" |
| 776 | ";DLC[c.dex*890]"; |
| 777 | ASSERT_EQ(context->VerifyClassLoaderContextMatch(wrong_checksum), |
| 778 | ClassLoaderContext::VerificationResult::kMismatch); |
| 779 | |
| 780 | std::string wrong_extra_class_loader = |
| 781 | "PCL[a.dex*123:b.dex*456]" |
| 782 | "{PCL[d.dex*321];PCL[e.dex*654]#PCL[f.dex*098:g.dex*999];PCL[i.dex#444]}" |
| 783 | ";DLC[c.dex*890]"; |
| 784 | ASSERT_EQ(context->VerifyClassLoaderContextMatch(wrong_extra_class_loader), |
| 785 | ClassLoaderContext::VerificationResult::kMismatch); |
| 786 | |
| 787 | std::string wrong_extra_classpath = |
| 788 | "PCL[a.dex*123:b.dex*456]{PCL[d.dex*321:i.dex#444];PCL[e.dex*654]#PCL[f.dex*098:g.dex*999]}" |
| 789 | ";DLC[c.dex*890]"; |
| 790 | ASSERT_EQ(context->VerifyClassLoaderContextMatch(wrong_extra_classpath), |
| 791 | ClassLoaderContext::VerificationResult::kMismatch); |
| 792 | } |
| 793 | |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 794 | TEST_F(ClassLoaderContextTest, VerifyClassLoaderContextMatchAfterEncoding) { |
| 795 | jobject class_loader_a = LoadDexInPathClassLoader("ForClassLoaderA", nullptr); |
| 796 | jobject class_loader_b = LoadDexInDelegateLastClassLoader("ForClassLoaderB", class_loader_a); |
| 797 | jobject class_loader_c = LoadDexInPathClassLoader("ForClassLoaderC", class_loader_b); |
| 798 | jobject class_loader_d = LoadDexInDelegateLastClassLoader("ForClassLoaderD", class_loader_c); |
| 799 | |
| 800 | std::unique_ptr<ClassLoaderContext> context = CreateContextForClassLoader(class_loader_d); |
| 801 | |
Calin Juravle | 1e96a5d | 2017-09-05 17:10:48 -0700 | [diff] [blame] | 802 | std::string context_with_no_base_dir = context->EncodeContextForOatFile(""); |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 803 | ASSERT_EQ(context->VerifyClassLoaderContextMatch(context_with_no_base_dir), |
| 804 | ClassLoaderContext::VerificationResult::kVerifies); |
Calin Juravle | 1e96a5d | 2017-09-05 17:10:48 -0700 | [diff] [blame] | 805 | |
| 806 | std::string dex_location = GetTestDexFileName("ForClassLoaderA"); |
| 807 | size_t pos = dex_location.rfind('/'); |
| 808 | ASSERT_NE(std::string::npos, pos); |
| 809 | std::string parent = dex_location.substr(0, pos); |
| 810 | |
| 811 | std::string context_with_base_dir = context->EncodeContextForOatFile(parent); |
| 812 | ASSERT_NE(context_with_base_dir, context_with_no_base_dir); |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 813 | ASSERT_EQ(context->VerifyClassLoaderContextMatch(context_with_base_dir), |
| 814 | ClassLoaderContext::VerificationResult::kVerifies); |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 815 | } |
| 816 | |
Calin Juravle | a308a32 | 2017-07-18 16:51:51 -0700 | [diff] [blame] | 817 | TEST_F(ClassLoaderContextTest, VerifyClassLoaderContextMatchAfterEncodingMultidex) { |
| 818 | jobject class_loader = LoadDexInPathClassLoader("MultiDex", nullptr); |
| 819 | |
| 820 | std::unique_ptr<ClassLoaderContext> context = CreateContextForClassLoader(class_loader); |
| 821 | |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 822 | ASSERT_EQ(context->VerifyClassLoaderContextMatch(context->EncodeContextForOatFile("")), |
| 823 | ClassLoaderContext::VerificationResult::kVerifies); |
Calin Juravle | a308a32 | 2017-07-18 16:51:51 -0700 | [diff] [blame] | 824 | } |
| 825 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 826 | } // namespace art |