blob: 37cef8196d7e2f2ad85e7b0386c8a4d444799ddf [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
17#ifndef ART_RUNTIME_CLASS_LOADER_CONTEXT_H_
18#define ART_RUNTIME_CLASS_LOADER_CONTEXT_H_
19
20#include <string>
21#include <vector>
22
23#include "arch/instruction_set.h"
24#include "base/dchecked_vector.h"
Mathieu Chartieradc90862018-05-11 13:03:06 -070025#include "dex/dex_file.h"
Calin Juravle57d0acc2017-07-11 17:41:30 -070026#include "handle_scope.h"
27#include "mirror/class_loader.h"
Mathieu Chartieradc90862018-05-11 13:03:06 -070028#include "oat_file.h"
Calin Juravle57d0acc2017-07-11 17:41:30 -070029#include "scoped_thread_state_change.h"
Calin Juravle87e2cb62017-06-13 21:48:45 -070030
31namespace art {
32
33class DexFile;
34class OatFile;
35
36// Utility class which holds the class loader context used during compilation/verification.
37class ClassLoaderContext {
38 public:
Mathieu Chartieradc90862018-05-11 13:03:06 -070039 enum class VerificationResult {
40 kVerifies,
41 kForcedToSkipChecks,
42 kMismatch,
43 };
44
45 enum ClassLoaderType {
46 kInvalidClassLoader = 0,
47 kPathClassLoader = 1,
48 kDelegateLastClassLoader = 2
49 };
50
Calin Juravle57d0acc2017-07-11 17:41:30 -070051 ~ClassLoaderContext();
52
Calin Juravle87e2cb62017-06-13 21:48:45 -070053 // Opens requested class path files and appends them to ClassLoaderInfo::opened_dex_files.
54 // If the dex files have been stripped, the method opens them from their oat files which are added
55 // to ClassLoaderInfo::opened_oat_files. The 'classpath_dir' argument specifies the directory to
56 // use for the relative class paths.
57 // Returns true if all dex files where successfully opened.
Calin Juravlec5b215f2017-09-12 14:49:37 -070058 // It may be called only once per ClassLoaderContext. Subsequent calls will return the same
59 // result without doing anything.
60 //
61 // This will replace the class path locations with the locations of the opened dex files.
62 // (Note that one dex file can contain multidexes. Each multidex will be added to the classpath
63 // separately.)
Calin Juravle87e2cb62017-06-13 21:48:45 -070064 //
65 // Note that a "false" return could mean that either an apk/jar contained no dex files or
66 // that we hit a I/O or checksum mismatch error.
67 // TODO(calin): Currently there's no easy way to tell the difference.
68 //
69 // TODO(calin): we're forced to complicate the flow in this class with a different
70 // OpenDexFiles step because the current dex2oat flow requires the dex files be opened before
71 // the class loader is created. Consider reworking the dex2oat part.
72 bool OpenDexFiles(InstructionSet isa, const std::string& classpath_dir);
73
74 // Remove the specified compilation sources from all classpaths present in this context.
75 // Should only be called before the first call to OpenDexFiles().
76 bool RemoveLocationsFromClassPaths(const dchecked_vector<std::string>& compilation_sources);
77
78 // Creates the entire class loader hierarchy according to the current context.
Calin Juravlec79470d2017-07-12 17:37:42 -070079 // Returns the first class loader from the chain.
80 //
81 // For example: if the context was built from the spec
82 // "ClassLoaderType1[ClasspathElem1:ClasspathElem2...];ClassLoaderType2[...]..."
83 // the method returns the class loader correponding to ClassLoader1. The parent chain will be
84 // ClassLoader1 --> ClassLoader2 --> ... --> BootClassLoader.
85 //
86 // The compilation sources are appended to the classpath of the first class loader (in the above
87 // example ClassLoader1).
88 //
Calin Juravle7b0648a2017-07-07 18:40:50 -070089 // If the context is empty, this method only creates a single PathClassLoader with the
90 // given compilation_sources.
Calin Juravlec79470d2017-07-12 17:37:42 -070091 //
92 // Notes:
93 // 1) the objects are not completely set up. Do not use this outside of tests and the compiler.
94 // 2) should only be called before the first call to OpenDexFiles().
Calin Juravle87e2cb62017-06-13 21:48:45 -070095 jobject CreateClassLoader(const std::vector<const DexFile*>& compilation_sources) const;
96
97 // Encodes the context as a string suitable to be added in oat files.
98 // (so that it can be read and verified at runtime against the actual class
99 // loader hierarchy).
100 // Should only be called if OpenDexFiles() returned true.
Mathieu Chartierc4440772018-04-16 14:40:56 -0700101 // If stored context is non-null, the stored names are overwritten by the class path from the
102 // stored context.
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700103 // E.g. if the context is PCL[a.dex:b.dex] this will return
104 // "PCL[a.dex*a_checksum*b.dex*a_checksum]".
Mathieu Chartierc4440772018-04-16 14:40:56 -0700105 std::string EncodeContextForOatFile(const std::string& base_dir,
106 ClassLoaderContext* stored_context = nullptr) const;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700107
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700108 // Encodes the context as a string suitable to be passed to dex2oat.
109 // This is the same as EncodeContextForOatFile but without adding the checksums
110 // and only adding each dex files once (no multidex).
111 // Should only be called if OpenDexFiles() returned true.
112 std::string EncodeContextForDex2oat(const std::string& base_dir) const;
113
Calin Juravle87e2cb62017-06-13 21:48:45 -0700114 // Flattens the opened dex files into the given vector.
115 // Should only be called if OpenDexFiles() returned true.
116 std::vector<const DexFile*> FlattenOpenedDexFiles() const;
117
Calin Juravle3f918642017-07-11 19:04:20 -0700118 // Verifies that the current context is identical to the context encoded as `context_spec`.
119 // Identical means:
120 // - the number and type of the class loaders from the chain matches
121 // - the class loader from the same position have the same classpath
122 // (the order and checksum of the dex files matches)
Calin Juravlec5b215f2017-09-12 14:49:37 -0700123 // This should be called after OpenDexFiles().
Mathieu Chartierf5abfc42018-03-23 21:51:54 -0700124 // Names are only verified if verify_names is true.
125 // Checksums are only verified if verify_checksums is true.
Mathieu Chartieradc90862018-05-11 13:03:06 -0700126 VerificationResult VerifyClassLoaderContextMatch(const std::string& context_spec,
Mathieu Chartierf5abfc42018-03-23 21:51:54 -0700127 bool verify_names = true,
128 bool verify_checksums = true) const;
Calin Juravle3f918642017-07-11 19:04:20 -0700129
Calin Juravle87e2cb62017-06-13 21:48:45 -0700130 // Creates the class loader context from the given string.
131 // The format: ClassLoaderType1[ClasspathElem1:ClasspathElem2...];ClassLoaderType2[...]...
132 // ClassLoaderType is either "PCL" (PathClassLoader) or "DLC" (DelegateLastClassLoader).
133 // ClasspathElem is the path of dex/jar/apk file.
Calin Juravlec79470d2017-07-12 17:37:42 -0700134 //
135 // The spec represents a class loader chain with the natural interpretation:
136 // ClassLoader1 has ClassLoader2 as parent which has ClassLoader3 as a parent and so on.
137 // The last class loader is assumed to have the BootClassLoader as a parent.
138 //
Calin Juravle87e2cb62017-06-13 21:48:45 -0700139 // Note that we allowed class loaders with an empty class path in order to support a custom
140 // class loader for the source dex files.
141 static std::unique_ptr<ClassLoaderContext> Create(const std::string& spec);
142
Calin Juravle57d0acc2017-07-11 17:41:30 -0700143 // Creates a context for the given class_loader and dex_elements.
144 // The method will walk the parent chain starting from `class_loader` and add their dex files
145 // to the current class loaders chain. The `dex_elements` will be added at the end of the
146 // classpath belonging to the `class_loader` argument.
147 // The ownership of the opened dex files will be retained by the given `class_loader`.
148 // If there are errors in processing the class loader chain (e.g. unsupported elements) the
149 // method returns null.
150 static std::unique_ptr<ClassLoaderContext> CreateContextForClassLoader(jobject class_loader,
151 jobjectArray dex_elements);
152
Calin Juravle19915892017-08-03 17:10:36 +0000153 // Returns the default class loader context to be used when none is specified.
154 // This will return a context with a single and empty PathClassLoader.
155 static std::unique_ptr<ClassLoaderContext> Default();
156
Calin Juravle87e2cb62017-06-13 21:48:45 -0700157 struct ClassLoaderInfo {
158 // The type of this class loader.
159 ClassLoaderType type;
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000160 // Shared libraries this context has.
161 std::vector<std::unique_ptr<ClassLoaderInfo>> shared_libraries;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700162 // The list of class path elements that this loader loads.
163 // Note that this list may contain relative paths.
164 std::vector<std::string> classpath;
Mathieu Chartierc4440772018-04-16 14:40:56 -0700165 // Original opened class path (ignoring multidex).
166 std::vector<std::string> original_classpath;
Calin Juravle7b0648a2017-07-07 18:40:50 -0700167 // The list of class path elements checksums.
168 // May be empty if the checksums are not given when the context is created.
169 std::vector<uint32_t> checksums;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700170 // After OpenDexFiles is called this holds the opened dex files.
171 std::vector<std::unique_ptr<const DexFile>> opened_dex_files;
172 // After OpenDexFiles, in case some of the dex files were opened from their oat files
173 // this holds the list of opened oat files.
174 std::vector<std::unique_ptr<OatFile>> opened_oat_files;
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000175 // The parent class loader.
176 std::unique_ptr<ClassLoaderInfo> parent;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700177
178 explicit ClassLoaderInfo(ClassLoaderType cl_type) : type(cl_type) {}
179 };
180
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000181 private:
Calin Juravle19915892017-08-03 17:10:36 +0000182 // Creates an empty context (with no class loaders).
183 ClassLoaderContext();
184
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000185 // Get the parent of the class loader chain at depth `index`.
186 ClassLoaderInfo* GetParent(size_t index) const {
187 ClassLoaderInfo* result = class_loader_chain_.get();
188 while ((result != nullptr) && (index-- != 0)) {
189 result = result->parent.get();
190 }
191 return result;
192 }
193
194 size_t GetParentChainSize() const {
195 size_t result = 0;
196 ClassLoaderInfo* info = class_loader_chain_.get();
197 while (info != nullptr) {
198 ++result;
199 info = info->parent.get();
200 }
201 return result;
202 }
203
Calin Juravle57d0acc2017-07-11 17:41:30 -0700204 // Constructs an empty context.
205 // `owns_the_dex_files` specifies whether or not the context will own the opened dex files
206 // present in the class loader chain. If `owns_the_dex_files` is true then OpenDexFiles cannot
207 // be called on this context (dex_files_open_attempted_ and dex_files_open_result_ will be set
208 // to true as well)
209 explicit ClassLoaderContext(bool owns_the_dex_files);
210
Calin Juravle87e2cb62017-06-13 21:48:45 -0700211 // Reads the class loader spec in place and returns true if the spec is valid and the
212 // compilation context was constructed.
Calin Juravle7b0648a2017-07-07 18:40:50 -0700213 bool Parse(const std::string& spec, bool parse_checksums = false);
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000214 ClassLoaderInfo* ParseInternal(const std::string& spec, bool parse_checksums);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700215
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000216 // Attempts to parse a single class loader spec.
217 // Returns the ClassLoaderInfo abstraction for this spec, or null if it cannot be parsed.
218 std::unique_ptr<ClassLoaderInfo> ParseClassLoaderSpec(
219 const std::string& class_loader_spec,
220 bool parse_checksums = false);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700221
Calin Juravle57d0acc2017-07-11 17:41:30 -0700222 // CHECKs that the dex files were opened (OpenDexFiles was called and set dex_files_open_result_
223 // to true). Aborts if not. The `calling_method` is used in the log message to identify the source
224 // of the call.
225 void CheckDexFilesOpened(const std::string& calling_method) const;
226
227 // Adds the `class_loader` info to the context.
228 // The dex file present in `dex_elements` array (if not null) will be added at the end of
229 // the classpath.
230 bool AddInfoToContextFromClassLoader(ScopedObjectAccessAlreadyRunnable& soa,
231 Handle<mirror::ClassLoader> class_loader,
232 Handle<mirror::ObjectArray<mirror::Object>> dex_elements)
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700233 REQUIRES_SHARED(Locks::mutator_lock_);
234
235 // Encodes the context as a string suitable to be passed to dex2oat or to be added to the
236 // oat file as the class path key.
237 // If for_dex2oat is true, the encoding adds each file once (i.e. it does not add multidex
238 // location). Otherwise, for oat files, the encoding adds all the dex files (including multidex)
239 // together with their checksums.
240 // Should only be called if OpenDexFiles() returned true.
Mathieu Chartierc4440772018-04-16 14:40:56 -0700241 std::string EncodeContext(const std::string& base_dir,
242 bool for_dex2oat,
243 ClassLoaderContext* stored_context) const;
Calin Juravle57d0acc2017-07-11 17:41:30 -0700244
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000245 // Internal version of `EncodeContext`, which will be called recursively
246 // on the parent and shared libraries.
247 void EncodeContextInternal(const ClassLoaderInfo& info,
248 const std::string& base_dir,
249 bool for_dex2oat,
250 ClassLoaderInfo* stored_info,
251 std::ostringstream& out) const;
252
253 bool ClassLoaderInfoMatch(const ClassLoaderInfo& info,
254 const ClassLoaderInfo& expected_info,
255 const std::string& context_spec,
256 bool verify_names,
257 bool verify_checksums) const;
258
Calin Juravle87e2cb62017-06-13 21:48:45 -0700259 // Extracts the class loader type from the given spec.
260 // Return ClassLoaderContext::kInvalidClassLoader if the class loader type is not
261 // recognized.
262 static ClassLoaderType ExtractClassLoaderType(const std::string& class_loader_spec);
263
264 // Returns the string representation of the class loader type.
265 // The returned format can be used when parsing a context spec.
266 static const char* GetClassLoaderTypeName(ClassLoaderType type);
267
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000268 // The class loader chain.
269 std::unique_ptr<ClassLoaderInfo> class_loader_chain_;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700270
271 // Whether or not the class loader context should be ignored at runtime when loading the oat
272 // files. When true, dex2oat will use OatFile::kSpecialSharedLibrary as the classpath key in
273 // the oat file.
274 // TODO(calin): Can we get rid of this and cover all relevant use cases?
275 // (e.g. packages using prebuild system packages as shared libraries b/36480683)
276 bool special_shared_library_;
277
278 // Whether or not OpenDexFiles() was called.
279 bool dex_files_open_attempted_;
280 // The result of the last OpenDexFiles() operation.
281 bool dex_files_open_result_;
282
Calin Juravle57d0acc2017-07-11 17:41:30 -0700283 // Whether or not the context owns the opened dex and oat files.
284 // If true, the opened dex files will be de-allocated when the context is destructed.
285 // If false, the objects will continue to be alive.
286 // Note that for convenience the the opened dex/oat files are stored as unique pointers
287 // which will release their ownership in the destructor based on this flag.
288 const bool owns_the_dex_files_;
289
Calin Juravle87e2cb62017-06-13 21:48:45 -0700290 friend class ClassLoaderContextTest;
291
292 DISALLOW_COPY_AND_ASSIGN(ClassLoaderContext);
293};
294
295} // namespace art
296#endif // ART_RUNTIME_CLASS_LOADER_CONTEXT_H_