blob: 9afa880da4a9f9db7820e7217f7ddaa276454a1b [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"
Calin Juravle57d0acc2017-07-11 17:41:30 -070025#include "handle_scope.h"
26#include "mirror/class_loader.h"
27#include "scoped_thread_state_change.h"
Calin Juravle87e2cb62017-06-13 21:48:45 -070028
29namespace art {
30
31class DexFile;
32class OatFile;
33
34// Utility class which holds the class loader context used during compilation/verification.
35class ClassLoaderContext {
36 public:
Calin Juravle57d0acc2017-07-11 17:41:30 -070037 ~ClassLoaderContext();
38
Calin Juravle87e2cb62017-06-13 21:48:45 -070039 // Opens requested class path files and appends them to ClassLoaderInfo::opened_dex_files.
40 // If the dex files have been stripped, the method opens them from their oat files which are added
41 // to ClassLoaderInfo::opened_oat_files. The 'classpath_dir' argument specifies the directory to
42 // use for the relative class paths.
43 // Returns true if all dex files where successfully opened.
44 // It may be called only once per ClassLoaderContext. The second call will abort.
45 //
46 // Note that a "false" return could mean that either an apk/jar contained no dex files or
47 // that we hit a I/O or checksum mismatch error.
48 // TODO(calin): Currently there's no easy way to tell the difference.
49 //
50 // TODO(calin): we're forced to complicate the flow in this class with a different
51 // OpenDexFiles step because the current dex2oat flow requires the dex files be opened before
52 // the class loader is created. Consider reworking the dex2oat part.
53 bool OpenDexFiles(InstructionSet isa, const std::string& classpath_dir);
54
55 // Remove the specified compilation sources from all classpaths present in this context.
56 // Should only be called before the first call to OpenDexFiles().
57 bool RemoveLocationsFromClassPaths(const dchecked_vector<std::string>& compilation_sources);
58
59 // Creates the entire class loader hierarchy according to the current context.
Calin Juravlec79470d2017-07-12 17:37:42 -070060 // Returns the first class loader from the chain.
61 //
62 // For example: if the context was built from the spec
63 // "ClassLoaderType1[ClasspathElem1:ClasspathElem2...];ClassLoaderType2[...]..."
64 // the method returns the class loader correponding to ClassLoader1. The parent chain will be
65 // ClassLoader1 --> ClassLoader2 --> ... --> BootClassLoader.
66 //
67 // The compilation sources are appended to the classpath of the first class loader (in the above
68 // example ClassLoader1).
69 //
Calin Juravle7b0648a2017-07-07 18:40:50 -070070 // If the context is empty, this method only creates a single PathClassLoader with the
71 // given compilation_sources.
Calin Juravlec79470d2017-07-12 17:37:42 -070072 //
73 // Notes:
74 // 1) the objects are not completely set up. Do not use this outside of tests and the compiler.
75 // 2) should only be called before the first call to OpenDexFiles().
Calin Juravle87e2cb62017-06-13 21:48:45 -070076 jobject CreateClassLoader(const std::vector<const DexFile*>& compilation_sources) const;
77
78 // Encodes the context as a string suitable to be added in oat files.
79 // (so that it can be read and verified at runtime against the actual class
80 // loader hierarchy).
81 // Should only be called if OpenDexFiles() returned true.
Calin Juravle27e0d1f2017-07-26 00:16:07 -070082 // E.g. if the context is PCL[a.dex:b.dex] this will return
83 // "PCL[a.dex*a_checksum*b.dex*a_checksum]".
Calin Juravle87e2cb62017-06-13 21:48:45 -070084 std::string EncodeContextForOatFile(const std::string& base_dir) const;
85
Calin Juravle27e0d1f2017-07-26 00:16:07 -070086 // Encodes the context as a string suitable to be passed to dex2oat.
87 // This is the same as EncodeContextForOatFile but without adding the checksums
88 // and only adding each dex files once (no multidex).
89 // Should only be called if OpenDexFiles() returned true.
90 std::string EncodeContextForDex2oat(const std::string& base_dir) const;
91
Calin Juravle87e2cb62017-06-13 21:48:45 -070092 // Flattens the opened dex files into the given vector.
93 // Should only be called if OpenDexFiles() returned true.
94 std::vector<const DexFile*> FlattenOpenedDexFiles() const;
95
Calin Juravle3f918642017-07-11 19:04:20 -070096 // Verifies that the current context is identical to the context encoded as `context_spec`.
97 // Identical means:
98 // - the number and type of the class loaders from the chain matches
99 // - the class loader from the same position have the same classpath
100 // (the order and checksum of the dex files matches)
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700101 bool VerifyClassLoaderContextMatch(const std::string& context_spec) const;
Calin Juravle3f918642017-07-11 19:04:20 -0700102
Calin Juravle87e2cb62017-06-13 21:48:45 -0700103 // Creates the class loader context from the given string.
104 // The format: ClassLoaderType1[ClasspathElem1:ClasspathElem2...];ClassLoaderType2[...]...
105 // ClassLoaderType is either "PCL" (PathClassLoader) or "DLC" (DelegateLastClassLoader).
106 // ClasspathElem is the path of dex/jar/apk file.
Calin Juravlec79470d2017-07-12 17:37:42 -0700107 //
108 // The spec represents a class loader chain with the natural interpretation:
109 // ClassLoader1 has ClassLoader2 as parent which has ClassLoader3 as a parent and so on.
110 // The last class loader is assumed to have the BootClassLoader as a parent.
111 //
Calin Juravle87e2cb62017-06-13 21:48:45 -0700112 // Note that we allowed class loaders with an empty class path in order to support a custom
113 // class loader for the source dex files.
114 static std::unique_ptr<ClassLoaderContext> Create(const std::string& spec);
115
Calin Juravle57d0acc2017-07-11 17:41:30 -0700116 // Creates a context for the given class_loader and dex_elements.
117 // The method will walk the parent chain starting from `class_loader` and add their dex files
118 // to the current class loaders chain. The `dex_elements` will be added at the end of the
119 // classpath belonging to the `class_loader` argument.
120 // The ownership of the opened dex files will be retained by the given `class_loader`.
121 // If there are errors in processing the class loader chain (e.g. unsupported elements) the
122 // method returns null.
123 static std::unique_ptr<ClassLoaderContext> CreateContextForClassLoader(jobject class_loader,
124 jobjectArray dex_elements);
125
Calin Juravle19915892017-08-03 17:10:36 +0000126 // Returns the default class loader context to be used when none is specified.
127 // This will return a context with a single and empty PathClassLoader.
128 static std::unique_ptr<ClassLoaderContext> Default();
129
Calin Juravle87e2cb62017-06-13 21:48:45 -0700130 private:
131 enum ClassLoaderType {
132 kInvalidClassLoader = 0,
133 kPathClassLoader = 1,
134 kDelegateLastClassLoader = 2
135 };
136
137 struct ClassLoaderInfo {
138 // The type of this class loader.
139 ClassLoaderType type;
140 // The list of class path elements that this loader loads.
141 // Note that this list may contain relative paths.
142 std::vector<std::string> classpath;
Calin Juravle7b0648a2017-07-07 18:40:50 -0700143 // The list of class path elements checksums.
144 // May be empty if the checksums are not given when the context is created.
145 std::vector<uint32_t> checksums;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700146 // After OpenDexFiles is called this holds the opened dex files.
147 std::vector<std::unique_ptr<const DexFile>> opened_dex_files;
148 // After OpenDexFiles, in case some of the dex files were opened from their oat files
149 // this holds the list of opened oat files.
150 std::vector<std::unique_ptr<OatFile>> opened_oat_files;
151
152 explicit ClassLoaderInfo(ClassLoaderType cl_type) : type(cl_type) {}
153 };
154
Calin Juravle19915892017-08-03 17:10:36 +0000155 // Creates an empty context (with no class loaders).
156 ClassLoaderContext();
157
Calin Juravle57d0acc2017-07-11 17:41:30 -0700158 // Constructs an empty context.
159 // `owns_the_dex_files` specifies whether or not the context will own the opened dex files
160 // present in the class loader chain. If `owns_the_dex_files` is true then OpenDexFiles cannot
161 // be called on this context (dex_files_open_attempted_ and dex_files_open_result_ will be set
162 // to true as well)
163 explicit ClassLoaderContext(bool owns_the_dex_files);
164
Calin Juravle87e2cb62017-06-13 21:48:45 -0700165 // Reads the class loader spec in place and returns true if the spec is valid and the
166 // compilation context was constructed.
Calin Juravle7b0648a2017-07-07 18:40:50 -0700167 bool Parse(const std::string& spec, bool parse_checksums = false);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700168
169 // Attempts to parse a single class loader spec for the given class_loader_type.
170 // If successful the class loader spec will be added to the chain.
171 // Returns whether or not the operation was successful.
172 bool ParseClassLoaderSpec(const std::string& class_loader_spec,
Calin Juravle7b0648a2017-07-07 18:40:50 -0700173 ClassLoaderType class_loader_type,
174 bool parse_checksums = false);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700175
Calin Juravle57d0acc2017-07-11 17:41:30 -0700176 // CHECKs that the dex files were opened (OpenDexFiles was called and set dex_files_open_result_
177 // to true). Aborts if not. The `calling_method` is used in the log message to identify the source
178 // of the call.
179 void CheckDexFilesOpened(const std::string& calling_method) const;
180
181 // Adds the `class_loader` info to the context.
182 // The dex file present in `dex_elements` array (if not null) will be added at the end of
183 // the classpath.
184 bool AddInfoToContextFromClassLoader(ScopedObjectAccessAlreadyRunnable& soa,
185 Handle<mirror::ClassLoader> class_loader,
186 Handle<mirror::ObjectArray<mirror::Object>> dex_elements)
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700187 REQUIRES_SHARED(Locks::mutator_lock_);
188
189 // Encodes the context as a string suitable to be passed to dex2oat or to be added to the
190 // oat file as the class path key.
191 // If for_dex2oat is true, the encoding adds each file once (i.e. it does not add multidex
192 // location). Otherwise, for oat files, the encoding adds all the dex files (including multidex)
193 // together with their checksums.
194 // Should only be called if OpenDexFiles() returned true.
195 std::string EncodeContext(const std::string& base_dir, bool for_dex2oat) const;
Calin Juravle57d0acc2017-07-11 17:41:30 -0700196
Calin Juravle87e2cb62017-06-13 21:48:45 -0700197 // Extracts the class loader type from the given spec.
198 // Return ClassLoaderContext::kInvalidClassLoader if the class loader type is not
199 // recognized.
200 static ClassLoaderType ExtractClassLoaderType(const std::string& class_loader_spec);
201
202 // Returns the string representation of the class loader type.
203 // The returned format can be used when parsing a context spec.
204 static const char* GetClassLoaderTypeName(ClassLoaderType type);
205
Calin Juravlec79470d2017-07-12 17:37:42 -0700206 // Returns the WellKnownClass for the given class loader type.
207 static jclass GetClassLoaderClass(ClassLoaderType type);
208
Calin Juravle87e2cb62017-06-13 21:48:45 -0700209 // The class loader chain represented as a vector.
210 // The parent of class_loader_chain_[i] is class_loader_chain_[i++].
211 // The parent of the last element is assumed to be the boot class loader.
212 std::vector<ClassLoaderInfo> class_loader_chain_;
213
214 // Whether or not the class loader context should be ignored at runtime when loading the oat
215 // files. When true, dex2oat will use OatFile::kSpecialSharedLibrary as the classpath key in
216 // the oat file.
217 // TODO(calin): Can we get rid of this and cover all relevant use cases?
218 // (e.g. packages using prebuild system packages as shared libraries b/36480683)
219 bool special_shared_library_;
220
221 // Whether or not OpenDexFiles() was called.
222 bool dex_files_open_attempted_;
223 // The result of the last OpenDexFiles() operation.
224 bool dex_files_open_result_;
225
Calin Juravle57d0acc2017-07-11 17:41:30 -0700226 // Whether or not the context owns the opened dex and oat files.
227 // If true, the opened dex files will be de-allocated when the context is destructed.
228 // If false, the objects will continue to be alive.
229 // Note that for convenience the the opened dex/oat files are stored as unique pointers
230 // which will release their ownership in the destructor based on this flag.
231 const bool owns_the_dex_files_;
232
Calin Juravle87e2cb62017-06-13 21:48:45 -0700233 friend class ClassLoaderContextTest;
234
235 DISALLOW_COPY_AND_ASSIGN(ClassLoaderContext);
236};
237
238} // namespace art
239#endif // ART_RUNTIME_CLASS_LOADER_CONTEXT_H_