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 | |
| 17 | #ifndef ART_RUNTIME_CLASS_LOADER_CONTEXT_H_ |
| 18 | #define ART_RUNTIME_CLASS_LOADER_CONTEXT_H_ |
| 19 | |
| 20 | #include <string> |
| 21 | #include <vector> |
Calin Juravle | a63a2e9 | 2020-04-15 20:02:00 -0700 | [diff] [blame] | 22 | #include <set> |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 23 | |
| 24 | #include "arch/instruction_set.h" |
| 25 | #include "base/dchecked_vector.h" |
Dmitrii Ishcheikin | f63924e | 2024-01-17 14:53:20 +0000 | [diff] [blame] | 26 | #include "base/macros.h" |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 27 | #include "dex/dex_file.h" |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 28 | #include "handle_scope.h" |
| 29 | #include "mirror/class_loader.h" |
Dmitrii Ishcheikin | 06d94bd | 2024-01-17 15:54:51 +0000 | [diff] [blame] | 30 | #include "oat/oat_file.h" |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 31 | #include "scoped_thread_state_change.h" |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 32 | |
Dmitrii Ishcheikin | f63924e | 2024-01-17 14:53:20 +0000 | [diff] [blame] | 33 | namespace art HIDDEN { |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 34 | |
| 35 | class DexFile; |
| 36 | class OatFile; |
| 37 | |
| 38 | // Utility class which holds the class loader context used during compilation/verification. |
Dmitrii Ishcheikin | f63924e | 2024-01-17 14:53:20 +0000 | [diff] [blame] | 39 | class EXPORT ClassLoaderContext { |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 40 | public: |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 41 | enum class VerificationResult { |
| 42 | kVerifies, |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 43 | kMismatch, |
| 44 | }; |
| 45 | |
| 46 | enum ClassLoaderType { |
| 47 | kInvalidClassLoader = 0, |
| 48 | kPathClassLoader = 1, |
David Brazdil | 1a9ac53 | 2019-03-05 11:57:13 +0000 | [diff] [blame] | 49 | kDelegateLastClassLoader = 2, |
| 50 | kInMemoryDexClassLoader = 3 |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Dan Zimmerman | b682ea4 | 2019-12-23 06:59:06 -0800 | [diff] [blame] | 53 | // Special encoding used to denote a foreign ClassLoader was found when trying to encode class |
| 54 | // loader contexts for each classpath element in a ClassLoader. See |
Jiakai Zhang | 122ce6a | 2023-01-04 16:20:47 +0000 | [diff] [blame] | 55 | // EncodeClassPathContextsForClassLoader. Keep in sync with PackageDexUsage in the framework |
| 56 | // (frameworks/base/services/core/java/com/android/server/pm/dex/PackageDexUsage.java) and |
| 57 | // DexUseManager in ART Services |
| 58 | // (art/libartservice/service/java/com/android/server/art/DexUseManager.java). |
Dan Zimmerman | b682ea4 | 2019-12-23 06:59:06 -0800 | [diff] [blame] | 59 | static constexpr const char* kUnsupportedClassLoaderContextEncoding = |
| 60 | "=UnsupportedClassLoaderContext="; |
| 61 | |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 62 | ~ClassLoaderContext(); |
| 63 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 64 | // Opens requested class path files and appends them to ClassLoaderInfo::opened_dex_files. |
| 65 | // If the dex files have been stripped, the method opens them from their oat files which are added |
| 66 | // to ClassLoaderInfo::opened_oat_files. The 'classpath_dir' argument specifies the directory to |
| 67 | // use for the relative class paths. |
| 68 | // Returns true if all dex files where successfully opened. |
Calin Juravle | c5b215f | 2017-09-12 14:49:37 -0700 | [diff] [blame] | 69 | // It may be called only once per ClassLoaderContext. Subsequent calls will return the same |
| 70 | // result without doing anything. |
David Brazdil | 8982186 | 2019-03-19 13:57:43 +0000 | [diff] [blame] | 71 | // If `context_fds` is an empty vector, files will be opened using the class path locations as |
| 72 | // filenames. Otherwise `context_fds` is expected to contain file descriptors to class path dex |
| 73 | // files, following the order of dex file locations in a flattened class loader context. If their |
| 74 | // number (size of `context_fds`) does not match the number of dex files, OpenDexFiles will fail. |
Calin Juravle | c5b215f | 2017-09-12 14:49:37 -0700 | [diff] [blame] | 75 | // |
| 76 | // This will replace the class path locations with the locations of the opened dex files. |
| 77 | // (Note that one dex file can contain multidexes. Each multidex will be added to the classpath |
| 78 | // separately.) |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 79 | // |
Calin Juravle | 6e6f1b2 | 2020-12-15 19:13:19 -0800 | [diff] [blame] | 80 | // only_read_checksums controls whether or not we only read the dex locations and the checksums |
| 81 | // from the apk instead of fully opening the dex files. |
| 82 | // |
| 83 | // This method is not thread safe. |
| 84 | // |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 85 | // Note that a "false" return could mean that either an apk/jar contained no dex files or |
| 86 | // that we hit a I/O or checksum mismatch error. |
| 87 | // TODO(calin): Currently there's no easy way to tell the difference. |
| 88 | // |
| 89 | // TODO(calin): we're forced to complicate the flow in this class with a different |
| 90 | // OpenDexFiles step because the current dex2oat flow requires the dex files be opened before |
| 91 | // the class loader is created. Consider reworking the dex2oat part. |
Calin Juravle | 5ff2393 | 2020-12-11 18:26:14 -0800 | [diff] [blame] | 92 | bool OpenDexFiles(const std::string& classpath_dir = "", |
Calin Juravle | 6e6f1b2 | 2020-12-15 19:13:19 -0800 | [diff] [blame] | 93 | const std::vector<int>& context_fds = std::vector<int>(), |
| 94 | bool only_read_checksums = false); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 95 | |
| 96 | // Remove the specified compilation sources from all classpaths present in this context. |
| 97 | // Should only be called before the first call to OpenDexFiles(). |
| 98 | bool RemoveLocationsFromClassPaths(const dchecked_vector<std::string>& compilation_sources); |
| 99 | |
| 100 | // Creates the entire class loader hierarchy according to the current context. |
Calin Juravle | c79470d | 2017-07-12 17:37:42 -0700 | [diff] [blame] | 101 | // Returns the first class loader from the chain. |
| 102 | // |
| 103 | // For example: if the context was built from the spec |
| 104 | // "ClassLoaderType1[ClasspathElem1:ClasspathElem2...];ClassLoaderType2[...]..." |
| 105 | // the method returns the class loader correponding to ClassLoader1. The parent chain will be |
| 106 | // ClassLoader1 --> ClassLoader2 --> ... --> BootClassLoader. |
| 107 | // |
| 108 | // The compilation sources are appended to the classpath of the first class loader (in the above |
| 109 | // example ClassLoader1). |
| 110 | // |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 111 | // If the context is empty, this method only creates a single PathClassLoader with the |
| 112 | // given compilation_sources. |
Calin Juravle | c79470d | 2017-07-12 17:37:42 -0700 | [diff] [blame] | 113 | // |
Nicolas Geoffray | cb2e1dd | 2018-11-20 11:15:13 +0000 | [diff] [blame] | 114 | // Shared libraries found in the chain will be canonicalized based on the dex files they |
| 115 | // contain. |
| 116 | // |
| 117 | // Implementation notes: |
Calin Juravle | c79470d | 2017-07-12 17:37:42 -0700 | [diff] [blame] | 118 | // 1) the objects are not completely set up. Do not use this outside of tests and the compiler. |
| 119 | // 2) should only be called before the first call to OpenDexFiles(). |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 120 | jobject CreateClassLoader(const std::vector<const DexFile*>& compilation_sources) const; |
| 121 | |
| 122 | // Encodes the context as a string suitable to be added in oat files. |
| 123 | // (so that it can be read and verified at runtime against the actual class |
| 124 | // loader hierarchy). |
| 125 | // Should only be called if OpenDexFiles() returned true. |
Mathieu Chartier | c444077 | 2018-04-16 14:40:56 -0700 | [diff] [blame] | 126 | // If stored context is non-null, the stored names are overwritten by the class path from the |
| 127 | // stored context. |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 128 | // E.g. if the context is PCL[a.dex:b.dex] this will return |
| 129 | // "PCL[a.dex*a_checksum*b.dex*a_checksum]". |
Mathieu Chartier | c444077 | 2018-04-16 14:40:56 -0700 | [diff] [blame] | 130 | std::string EncodeContextForOatFile(const std::string& base_dir, |
| 131 | ClassLoaderContext* stored_context = nullptr) const; |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 132 | |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 133 | // Encodes the context as a string suitable to be passed to dex2oat. |
| 134 | // This is the same as EncodeContextForOatFile but without adding the checksums |
| 135 | // and only adding each dex files once (no multidex). |
| 136 | // Should only be called if OpenDexFiles() returned true. |
| 137 | std::string EncodeContextForDex2oat(const std::string& base_dir) const; |
| 138 | |
Dan Zimmerman | b682ea4 | 2019-12-23 06:59:06 -0800 | [diff] [blame] | 139 | // Encodes the contexts for each of the classpath elements in the child-most |
| 140 | // classloader. Under the hood EncodeContextForDex2oat is used, so no checksums |
| 141 | // will be encoded. |
| 142 | // Should only be called if the dex files are opened (either via OpenDexFiles() or by creating the |
| 143 | // context from a live class loader). |
| 144 | // Notably, for each classpath element the encoded classloader context will contain only the |
| 145 | // elements that appear before it in the containing classloader. E.g. if `this` contains |
| 146 | // (from child to parent): |
| 147 | // |
| 148 | // PathClassLoader { multidex.apk!classes.dex, multidex.apk!classes2.dex, foo.dex, bar.dex } -> |
| 149 | // PathClassLoader { baz.dex } -> BootClassLoader |
| 150 | // |
| 151 | // then the return value will look like: |
| 152 | // |
| 153 | // `{ "multidex.apk": "PCL[];PCL[baz.dex]", |
| 154 | // "foo.dex" : "PCL[multidex.apk];PCL[baz.dex]", |
| 155 | // "bar.dex" : "PCL[multidex.apk:foo.dex];PCL[baz.dex]" }` |
| 156 | std::map<std::string, std::string> EncodeClassPathContexts(const std::string& base_dir) const; |
| 157 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 158 | // Flattens the opened dex files into the given vector. |
| 159 | // Should only be called if OpenDexFiles() returned true. |
| 160 | std::vector<const DexFile*> FlattenOpenedDexFiles() const; |
| 161 | |
Jiakai Zhang | 33dce90 | 2022-07-04 14:49:08 +0100 | [diff] [blame] | 162 | // Return a list of dex file locations from this class loader context after flattening. |
| 163 | std::vector<std::string> FlattenDexPaths() const; |
David Brazdil | 8982186 | 2019-03-19 13:57:43 +0000 | [diff] [blame] | 164 | |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 165 | // Verifies that the current context is identical to the context encoded as `context_spec`. |
| 166 | // Identical means: |
| 167 | // - the number and type of the class loaders from the chain matches |
| 168 | // - the class loader from the same position have the same classpath |
| 169 | // (the order and checksum of the dex files matches) |
Calin Juravle | 6e6f1b2 | 2020-12-15 19:13:19 -0800 | [diff] [blame] | 170 | // This should be called after OpenDexFiles() with only_read_checksums=true. There's no |
| 171 | // need to fully open the dex files if the only thing that needs to be done is to verify |
| 172 | // the context. |
| 173 | // |
Mathieu Chartier | f5abfc4 | 2018-03-23 21:51:54 -0700 | [diff] [blame] | 174 | // Names are only verified if verify_names is true. |
| 175 | // Checksums are only verified if verify_checksums is true. |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 176 | VerificationResult VerifyClassLoaderContextMatch(const std::string& context_spec, |
David Brazdil | 8982186 | 2019-03-19 13:57:43 +0000 | [diff] [blame] | 177 | bool verify_names = true, |
| 178 | bool verify_checksums = true) const; |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 179 | |
Calin Juravle | b495e7f | 2020-04-06 19:29:45 -0700 | [diff] [blame] | 180 | // Checks if any of the given dex files is already loaded in the current class loader context. |
Calin Juravle | a63a2e9 | 2020-04-15 20:02:00 -0700 | [diff] [blame] | 181 | // It only checks the first class loader. |
Calin Juravle | b495e7f | 2020-04-06 19:29:45 -0700 | [diff] [blame] | 182 | // Returns the list of duplicate dex files (empty if there are no duplicates). |
Calin Juravle | a63a2e9 | 2020-04-15 20:02:00 -0700 | [diff] [blame] | 183 | std::set<const DexFile*> CheckForDuplicateDexFiles( |
Calin Juravle | b495e7f | 2020-04-06 19:29:45 -0700 | [diff] [blame] | 184 | const std::vector<const DexFile*>& dex_files); |
| 185 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 186 | // Creates the class loader context from the given string. |
| 187 | // The format: ClassLoaderType1[ClasspathElem1:ClasspathElem2...];ClassLoaderType2[...]... |
| 188 | // ClassLoaderType is either "PCL" (PathClassLoader) or "DLC" (DelegateLastClassLoader). |
| 189 | // ClasspathElem is the path of dex/jar/apk file. |
Calin Juravle | c79470d | 2017-07-12 17:37:42 -0700 | [diff] [blame] | 190 | // |
| 191 | // The spec represents a class loader chain with the natural interpretation: |
| 192 | // ClassLoader1 has ClassLoader2 as parent which has ClassLoader3 as a parent and so on. |
| 193 | // The last class loader is assumed to have the BootClassLoader as a parent. |
| 194 | // |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 195 | // Note that we allowed class loaders with an empty class path in order to support a custom |
| 196 | // class loader for the source dex files. |
| 197 | static std::unique_ptr<ClassLoaderContext> Create(const std::string& spec); |
| 198 | |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 199 | // Creates a context for the given class_loader and dex_elements. |
| 200 | // The method will walk the parent chain starting from `class_loader` and add their dex files |
| 201 | // to the current class loaders chain. The `dex_elements` will be added at the end of the |
| 202 | // classpath belonging to the `class_loader` argument. |
| 203 | // The ownership of the opened dex files will be retained by the given `class_loader`. |
| 204 | // If there are errors in processing the class loader chain (e.g. unsupported elements) the |
| 205 | // method returns null. |
| 206 | static std::unique_ptr<ClassLoaderContext> CreateContextForClassLoader(jobject class_loader, |
| 207 | jobjectArray dex_elements); |
| 208 | |
Calin Juravle | 1991589 | 2017-08-03 17:10:36 +0000 | [diff] [blame] | 209 | // Returns the default class loader context to be used when none is specified. |
| 210 | // This will return a context with a single and empty PathClassLoader. |
| 211 | static std::unique_ptr<ClassLoaderContext> Default(); |
| 212 | |
Dan Zimmerman | b682ea4 | 2019-12-23 06:59:06 -0800 | [diff] [blame] | 213 | // Encodes the contexts for each of the classpath elements in `class_loader`. See |
| 214 | // ClassLoaderContext::EncodeClassPathContexts for more information about the return value. |
| 215 | // |
| 216 | // If `class_loader` does not derive from BaseDexClassLoader then an empty map is returned. |
| 217 | // Otherwise if a foreign ClassLoader is found in the class loader chain then the results values |
| 218 | // will all be ClassLoaderContext::kUnsupportedClassLoaderContextEncoding. |
| 219 | static std::map<std::string, std::string> EncodeClassPathContextsForClassLoader( |
| 220 | jobject class_loader); |
| 221 | |
Dan Zimmerman | c9fa770 | 2020-01-31 13:35:12 -0800 | [diff] [blame] | 222 | // Returns whether `encoded_class_loader_context` is a valid encoded ClassLoaderContext or |
| 223 | // EncodedUnsupportedClassLoaderContext. |
| 224 | static bool IsValidEncoding(const std::string& possible_encoded_class_loader_context); |
| 225 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 226 | struct ClassLoaderInfo { |
| 227 | // The type of this class loader. |
| 228 | ClassLoaderType type; |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 229 | // Shared libraries this context has. |
| 230 | std::vector<std::unique_ptr<ClassLoaderInfo>> shared_libraries; |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 231 | // Shared libraries that will be loaded after apks code that this context has. |
| 232 | std::vector<std::unique_ptr<ClassLoaderInfo>> shared_libraries_after; |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 233 | // The list of class path elements that this loader loads. |
| 234 | // Note that this list may contain relative paths. |
| 235 | std::vector<std::string> classpath; |
Mathieu Chartier | c444077 | 2018-04-16 14:40:56 -0700 | [diff] [blame] | 236 | // Original opened class path (ignoring multidex). |
| 237 | std::vector<std::string> original_classpath; |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 238 | // The list of class path elements checksums. |
| 239 | // May be empty if the checksums are not given when the context is created. |
| 240 | std::vector<uint32_t> checksums; |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 241 | // After OpenDexFiles is called this holds the opened dex files. |
| 242 | std::vector<std::unique_ptr<const DexFile>> opened_dex_files; |
| 243 | // After OpenDexFiles, in case some of the dex files were opened from their oat files |
| 244 | // this holds the list of opened oat files. |
| 245 | std::vector<std::unique_ptr<OatFile>> opened_oat_files; |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 246 | // The parent class loader. |
| 247 | std::unique_ptr<ClassLoaderInfo> parent; |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 248 | |
| 249 | explicit ClassLoaderInfo(ClassLoaderType cl_type) : type(cl_type) {} |
| 250 | }; |
| 251 | |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 252 | private: |
Calin Juravle | 1991589 | 2017-08-03 17:10:36 +0000 | [diff] [blame] | 253 | // Creates an empty context (with no class loaders). |
| 254 | ClassLoaderContext(); |
| 255 | |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 256 | // Get the parent of the class loader chain at depth `index`. |
| 257 | ClassLoaderInfo* GetParent(size_t index) const { |
| 258 | ClassLoaderInfo* result = class_loader_chain_.get(); |
| 259 | while ((result != nullptr) && (index-- != 0)) { |
| 260 | result = result->parent.get(); |
| 261 | } |
| 262 | return result; |
| 263 | } |
| 264 | |
| 265 | size_t GetParentChainSize() const { |
| 266 | size_t result = 0; |
| 267 | ClassLoaderInfo* info = class_loader_chain_.get(); |
| 268 | while (info != nullptr) { |
| 269 | ++result; |
| 270 | info = info->parent.get(); |
| 271 | } |
| 272 | return result; |
| 273 | } |
| 274 | |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 275 | // Constructs an empty context. |
| 276 | // `owns_the_dex_files` specifies whether or not the context will own the opened dex files |
| 277 | // present in the class loader chain. If `owns_the_dex_files` is true then OpenDexFiles cannot |
| 278 | // be called on this context (dex_files_open_attempted_ and dex_files_open_result_ will be set |
| 279 | // to true as well) |
| 280 | explicit ClassLoaderContext(bool owns_the_dex_files); |
| 281 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 282 | // Reads the class loader spec in place and returns true if the spec is valid and the |
| 283 | // compilation context was constructed. |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 284 | bool Parse(const std::string& spec, bool parse_checksums = false); |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 285 | ClassLoaderInfo* ParseInternal(const std::string& spec, bool parse_checksums); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 286 | |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 287 | // Attempts to parse a single class loader spec. |
| 288 | // Returns the ClassLoaderInfo abstraction for this spec, or null if it cannot be parsed. |
| 289 | std::unique_ptr<ClassLoaderInfo> ParseClassLoaderSpec( |
| 290 | const std::string& class_loader_spec, |
| 291 | bool parse_checksums = false); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 292 | |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 293 | // CHECKs that the dex files were opened (OpenDexFiles was called and set dex_files_open_result_ |
| 294 | // to true). Aborts if not. The `calling_method` is used in the log message to identify the source |
| 295 | // of the call. |
| 296 | void CheckDexFilesOpened(const std::string& calling_method) const; |
| 297 | |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 298 | // Creates the `ClassLoaderInfo` representing`class_loader` and attach it to `this`. |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 299 | // The dex file present in `dex_elements` array (if not null) will be added at the end of |
| 300 | // the classpath. |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 301 | bool CreateInfoFromClassLoader(ScopedObjectAccessAlreadyRunnable& soa, |
| 302 | Handle<mirror::ClassLoader> class_loader, |
| 303 | Handle<mirror::ObjectArray<mirror::Object>> dex_elements, |
| 304 | ClassLoaderInfo* child_info, |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 305 | bool is_shared_library, |
| 306 | bool is_after) |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 307 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 308 | |
| 309 | // Encodes the context as a string suitable to be passed to dex2oat or to be added to the |
| 310 | // oat file as the class path key. |
| 311 | // If for_dex2oat is true, the encoding adds each file once (i.e. it does not add multidex |
| 312 | // location). Otherwise, for oat files, the encoding adds all the dex files (including multidex) |
| 313 | // together with their checksums. |
| 314 | // Should only be called if OpenDexFiles() returned true. |
Mathieu Chartier | c444077 | 2018-04-16 14:40:56 -0700 | [diff] [blame] | 315 | std::string EncodeContext(const std::string& base_dir, |
| 316 | bool for_dex2oat, |
| 317 | ClassLoaderContext* stored_context) const; |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 318 | |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 319 | // Internal version of `EncodeContext`, which will be called recursively |
| 320 | // on the parent and shared libraries. |
| 321 | void EncodeContextInternal(const ClassLoaderInfo& info, |
| 322 | const std::string& base_dir, |
| 323 | bool for_dex2oat, |
| 324 | ClassLoaderInfo* stored_info, |
| 325 | std::ostringstream& out) const; |
| 326 | |
Dan Zimmerman | 7d511d9 | 2019-12-23 07:00:51 -0800 | [diff] [blame] | 327 | // Encodes e.g. PCL[foo.dex:bar.dex] |
| 328 | void EncodeClassPath(const std::string& base_dir, |
| 329 | const std::vector<std::string>& dex_locations, |
| 330 | const std::vector<uint32_t>& checksums, |
| 331 | ClassLoaderType type, |
| 332 | std::ostringstream& out) const; |
| 333 | |
| 334 | // Encodes the shared libraries classloaders and the parent classloader if |
| 335 | // either are present in info, e.g. {PCL[foo.dex]#PCL[bar.dex]};PCL[baz.dex] |
| 336 | void EncodeSharedLibAndParent(const ClassLoaderInfo& info, |
| 337 | const std::string& base_dir, |
| 338 | bool for_dex2oat, |
| 339 | ClassLoaderInfo* stored_info, |
| 340 | std::ostringstream& out) const; |
| 341 | |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 342 | bool ClassLoaderInfoMatch(const ClassLoaderInfo& info, |
| 343 | const ClassLoaderInfo& expected_info, |
| 344 | const std::string& context_spec, |
| 345 | bool verify_names, |
| 346 | bool verify_checksums) const; |
| 347 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 348 | // Extracts the class loader type from the given spec. |
| 349 | // Return ClassLoaderContext::kInvalidClassLoader if the class loader type is not |
| 350 | // recognized. |
| 351 | static ClassLoaderType ExtractClassLoaderType(const std::string& class_loader_spec); |
| 352 | |
| 353 | // Returns the string representation of the class loader type. |
| 354 | // The returned format can be used when parsing a context spec. |
| 355 | static const char* GetClassLoaderTypeName(ClassLoaderType type); |
| 356 | |
Calin Juravle | 6e6f1b2 | 2020-12-15 19:13:19 -0800 | [diff] [blame] | 357 | // Encodes the state of processing the dex files associated with the context. |
| 358 | enum ContextDexFilesState { |
| 359 | // The dex files are not opened. |
| 360 | kDexFilesNotOpened = 1, |
| 361 | // The dex checksums/locations were read from the apk/dex but the dex files were not opened. |
| 362 | kDexFilesChecksumsRead = 2, |
| 363 | // The dex files are opened (either because we called OpenDexFiles, or we used a class loader |
| 364 | // to create the context). This implies kDexFilesChecksumsRead. |
| 365 | kDexFilesOpened = 3, |
| 366 | // We failed to open the dex files or read the checksums. |
| 367 | kDexFilesOpenFailed = 4 |
| 368 | }; |
| 369 | |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 370 | // The class loader chain. |
| 371 | std::unique_ptr<ClassLoaderInfo> class_loader_chain_; |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 372 | |
Calin Juravle | 6e6f1b2 | 2020-12-15 19:13:19 -0800 | [diff] [blame] | 373 | // The opening state of the dex files. |
| 374 | ContextDexFilesState dex_files_state_; |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 375 | |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 376 | // Whether or not the context owns the opened dex and oat files. |
| 377 | // If true, the opened dex files will be de-allocated when the context is destructed. |
| 378 | // If false, the objects will continue to be alive. |
| 379 | // Note that for convenience the the opened dex/oat files are stored as unique pointers |
| 380 | // which will release their ownership in the destructor based on this flag. |
| 381 | const bool owns_the_dex_files_; |
| 382 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 383 | friend class ClassLoaderContextTest; |
| 384 | |
| 385 | DISALLOW_COPY_AND_ASSIGN(ClassLoaderContext); |
| 386 | }; |
| 387 | |
| 388 | } // namespace art |
| 389 | #endif // ART_RUNTIME_CLASS_LOADER_CONTEXT_H_ |