blob: 81c8903c24efdfd04804c882fbac05f5f84cd9ee [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:
37 // Creates an empty context (with no class loaders).
38 ClassLoaderContext();
39
Calin Juravle57d0acc2017-07-11 17:41:30 -070040 ~ClassLoaderContext();
41
Calin Juravle87e2cb62017-06-13 21:48:45 -070042 // Opens requested class path files and appends them to ClassLoaderInfo::opened_dex_files.
43 // If the dex files have been stripped, the method opens them from their oat files which are added
44 // to ClassLoaderInfo::opened_oat_files. The 'classpath_dir' argument specifies the directory to
45 // use for the relative class paths.
46 // Returns true if all dex files where successfully opened.
47 // It may be called only once per ClassLoaderContext. The second call will abort.
48 //
49 // Note that a "false" return could mean that either an apk/jar contained no dex files or
50 // that we hit a I/O or checksum mismatch error.
51 // TODO(calin): Currently there's no easy way to tell the difference.
52 //
53 // TODO(calin): we're forced to complicate the flow in this class with a different
54 // OpenDexFiles step because the current dex2oat flow requires the dex files be opened before
55 // the class loader is created. Consider reworking the dex2oat part.
56 bool OpenDexFiles(InstructionSet isa, const std::string& classpath_dir);
57
58 // Remove the specified compilation sources from all classpaths present in this context.
59 // Should only be called before the first call to OpenDexFiles().
60 bool RemoveLocationsFromClassPaths(const dchecked_vector<std::string>& compilation_sources);
61
62 // Creates the entire class loader hierarchy according to the current context.
63 // The compilation sources are appended to the classpath of the top class loader
64 // (i.e the class loader whose parent is the BootClassLoader).
65 // Should only be called if OpenDexFiles() returned true.
Calin Juravle7b0648a2017-07-07 18:40:50 -070066 // If the context is empty, this method only creates a single PathClassLoader with the
67 // given compilation_sources.
Calin Juravle87e2cb62017-06-13 21:48:45 -070068 jobject CreateClassLoader(const std::vector<const DexFile*>& compilation_sources) const;
69
70 // Encodes the context as a string suitable to be added in oat files.
71 // (so that it can be read and verified at runtime against the actual class
72 // loader hierarchy).
73 // Should only be called if OpenDexFiles() returned true.
74 // E.g. if the context is PCL[a.dex:b.dex] this will return "a.dex*a_checksum*b.dex*a_checksum".
75 std::string EncodeContextForOatFile(const std::string& base_dir) const;
76
77 // Flattens the opened dex files into the given vector.
78 // Should only be called if OpenDexFiles() returned true.
79 std::vector<const DexFile*> FlattenOpenedDexFiles() const;
80
81 // Creates the class loader context from the given string.
82 // The format: ClassLoaderType1[ClasspathElem1:ClasspathElem2...];ClassLoaderType2[...]...
83 // ClassLoaderType is either "PCL" (PathClassLoader) or "DLC" (DelegateLastClassLoader).
84 // ClasspathElem is the path of dex/jar/apk file.
85 // Note that we allowed class loaders with an empty class path in order to support a custom
86 // class loader for the source dex files.
87 static std::unique_ptr<ClassLoaderContext> Create(const std::string& spec);
88
Calin Juravle7b0648a2017-07-07 18:40:50 -070089 // Decodes the class loader context stored in the oat file with EncodeContextForOatFile.
90 // Returns true if the format matches, or false otherwise. If the return is true, the out
91 // arguments will contain the classpath dex files, their checksums and whether or not the
92 // context is a special shared library.
93 // The method asserts that the context is made out of only one PathClassLoader.
94 static bool DecodePathClassLoaderContextFromOatFileKey(
95 const std::string& context_spec,
96 std::vector<std::string>* out_classpath,
97 std::vector<uint32_t>* out_checksums,
98 bool* out_is_special_shared_library);
99
Calin Juravle57d0acc2017-07-11 17:41:30 -0700100 // Creates a context for the given class_loader and dex_elements.
101 // The method will walk the parent chain starting from `class_loader` and add their dex files
102 // to the current class loaders chain. The `dex_elements` will be added at the end of the
103 // classpath belonging to the `class_loader` argument.
104 // The ownership of the opened dex files will be retained by the given `class_loader`.
105 // If there are errors in processing the class loader chain (e.g. unsupported elements) the
106 // method returns null.
107 static std::unique_ptr<ClassLoaderContext> CreateContextForClassLoader(jobject class_loader,
108 jobjectArray dex_elements);
109
Calin Juravle87e2cb62017-06-13 21:48:45 -0700110 private:
111 enum ClassLoaderType {
112 kInvalidClassLoader = 0,
113 kPathClassLoader = 1,
114 kDelegateLastClassLoader = 2
115 };
116
117 struct ClassLoaderInfo {
118 // The type of this class loader.
119 ClassLoaderType type;
120 // The list of class path elements that this loader loads.
121 // Note that this list may contain relative paths.
122 std::vector<std::string> classpath;
Calin Juravle7b0648a2017-07-07 18:40:50 -0700123 // The list of class path elements checksums.
124 // May be empty if the checksums are not given when the context is created.
125 std::vector<uint32_t> checksums;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700126 // After OpenDexFiles is called this holds the opened dex files.
127 std::vector<std::unique_ptr<const DexFile>> opened_dex_files;
128 // After OpenDexFiles, in case some of the dex files were opened from their oat files
129 // this holds the list of opened oat files.
130 std::vector<std::unique_ptr<OatFile>> opened_oat_files;
131
132 explicit ClassLoaderInfo(ClassLoaderType cl_type) : type(cl_type) {}
133 };
134
Calin Juravle57d0acc2017-07-11 17:41:30 -0700135 // Constructs an empty context.
136 // `owns_the_dex_files` specifies whether or not the context will own the opened dex files
137 // present in the class loader chain. If `owns_the_dex_files` is true then OpenDexFiles cannot
138 // be called on this context (dex_files_open_attempted_ and dex_files_open_result_ will be set
139 // to true as well)
140 explicit ClassLoaderContext(bool owns_the_dex_files);
141
Calin Juravle87e2cb62017-06-13 21:48:45 -0700142 // Reads the class loader spec in place and returns true if the spec is valid and the
143 // compilation context was constructed.
Calin Juravle7b0648a2017-07-07 18:40:50 -0700144 bool Parse(const std::string& spec, bool parse_checksums = false);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700145
146 // Attempts to parse a single class loader spec for the given class_loader_type.
147 // If successful the class loader spec will be added to the chain.
148 // Returns whether or not the operation was successful.
149 bool ParseClassLoaderSpec(const std::string& class_loader_spec,
Calin Juravle7b0648a2017-07-07 18:40:50 -0700150 ClassLoaderType class_loader_type,
151 bool parse_checksums = false);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700152
Calin Juravle57d0acc2017-07-11 17:41:30 -0700153 // CHECKs that the dex files were opened (OpenDexFiles was called and set dex_files_open_result_
154 // to true). Aborts if not. The `calling_method` is used in the log message to identify the source
155 // of the call.
156 void CheckDexFilesOpened(const std::string& calling_method) const;
157
158 // Adds the `class_loader` info to the context.
159 // The dex file present in `dex_elements` array (if not null) will be added at the end of
160 // the classpath.
161 bool AddInfoToContextFromClassLoader(ScopedObjectAccessAlreadyRunnable& soa,
162 Handle<mirror::ClassLoader> class_loader,
163 Handle<mirror::ObjectArray<mirror::Object>> dex_elements)
164 REQUIRES_SHARED(Locks::mutator_lock_);
165
Calin Juravle87e2cb62017-06-13 21:48:45 -0700166 // Extracts the class loader type from the given spec.
167 // Return ClassLoaderContext::kInvalidClassLoader if the class loader type is not
168 // recognized.
169 static ClassLoaderType ExtractClassLoaderType(const std::string& class_loader_spec);
170
171 // Returns the string representation of the class loader type.
172 // The returned format can be used when parsing a context spec.
173 static const char* GetClassLoaderTypeName(ClassLoaderType type);
174
Calin Juravle87e2cb62017-06-13 21:48:45 -0700175 // The class loader chain represented as a vector.
176 // The parent of class_loader_chain_[i] is class_loader_chain_[i++].
177 // The parent of the last element is assumed to be the boot class loader.
178 std::vector<ClassLoaderInfo> class_loader_chain_;
179
180 // Whether or not the class loader context should be ignored at runtime when loading the oat
181 // files. When true, dex2oat will use OatFile::kSpecialSharedLibrary as the classpath key in
182 // the oat file.
183 // TODO(calin): Can we get rid of this and cover all relevant use cases?
184 // (e.g. packages using prebuild system packages as shared libraries b/36480683)
185 bool special_shared_library_;
186
187 // Whether or not OpenDexFiles() was called.
188 bool dex_files_open_attempted_;
189 // The result of the last OpenDexFiles() operation.
190 bool dex_files_open_result_;
191
Calin Juravle57d0acc2017-07-11 17:41:30 -0700192 // Whether or not the context owns the opened dex and oat files.
193 // If true, the opened dex files will be de-allocated when the context is destructed.
194 // If false, the objects will continue to be alive.
195 // Note that for convenience the the opened dex/oat files are stored as unique pointers
196 // which will release their ownership in the destructor based on this flag.
197 const bool owns_the_dex_files_;
198
Calin Juravle87e2cb62017-06-13 21:48:45 -0700199 friend class ClassLoaderContextTest;
200
201 DISALLOW_COPY_AND_ASSIGN(ClassLoaderContext);
202};
203
204} // namespace art
205#endif // ART_RUNTIME_CLASS_LOADER_CONTEXT_H_