blob: 9727a3b0dd7467c2332e2ae1ea3e7cdcd823ec93 [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"
25#include "jni.h"
26
27namespace art {
28
29class DexFile;
30class OatFile;
31
32// Utility class which holds the class loader context used during compilation/verification.
33class ClassLoaderContext {
34 public:
35 // Creates an empty context (with no class loaders).
36 ClassLoaderContext();
37
38 // Opens requested class path files and appends them to ClassLoaderInfo::opened_dex_files.
39 // If the dex files have been stripped, the method opens them from their oat files which are added
40 // to ClassLoaderInfo::opened_oat_files. The 'classpath_dir' argument specifies the directory to
41 // use for the relative class paths.
42 // Returns true if all dex files where successfully opened.
43 // It may be called only once per ClassLoaderContext. The second call will abort.
44 //
45 // Note that a "false" return could mean that either an apk/jar contained no dex files or
46 // that we hit a I/O or checksum mismatch error.
47 // TODO(calin): Currently there's no easy way to tell the difference.
48 //
49 // TODO(calin): we're forced to complicate the flow in this class with a different
50 // OpenDexFiles step because the current dex2oat flow requires the dex files be opened before
51 // the class loader is created. Consider reworking the dex2oat part.
52 bool OpenDexFiles(InstructionSet isa, const std::string& classpath_dir);
53
54 // Remove the specified compilation sources from all classpaths present in this context.
55 // Should only be called before the first call to OpenDexFiles().
56 bool RemoveLocationsFromClassPaths(const dchecked_vector<std::string>& compilation_sources);
57
58 // Creates the entire class loader hierarchy according to the current context.
59 // The compilation sources are appended to the classpath of the top class loader
60 // (i.e the class loader whose parent is the BootClassLoader).
61 // Should only be called if OpenDexFiles() returned true.
Calin Juravle7b0648a2017-07-07 18:40:50 -070062 // If the context is empty, this method only creates a single PathClassLoader with the
63 // given compilation_sources.
Calin Juravle87e2cb62017-06-13 21:48:45 -070064 jobject CreateClassLoader(const std::vector<const DexFile*>& compilation_sources) const;
65
66 // Encodes the context as a string suitable to be added in oat files.
67 // (so that it can be read and verified at runtime against the actual class
68 // loader hierarchy).
69 // Should only be called if OpenDexFiles() returned true.
70 // E.g. if the context is PCL[a.dex:b.dex] this will return "a.dex*a_checksum*b.dex*a_checksum".
71 std::string EncodeContextForOatFile(const std::string& base_dir) const;
72
73 // Flattens the opened dex files into the given vector.
74 // Should only be called if OpenDexFiles() returned true.
75 std::vector<const DexFile*> FlattenOpenedDexFiles() const;
76
77 // Creates the class loader context from the given string.
78 // The format: ClassLoaderType1[ClasspathElem1:ClasspathElem2...];ClassLoaderType2[...]...
79 // ClassLoaderType is either "PCL" (PathClassLoader) or "DLC" (DelegateLastClassLoader).
80 // ClasspathElem is the path of dex/jar/apk file.
81 // Note that we allowed class loaders with an empty class path in order to support a custom
82 // class loader for the source dex files.
83 static std::unique_ptr<ClassLoaderContext> Create(const std::string& spec);
84
Calin Juravle7b0648a2017-07-07 18:40:50 -070085 // Decodes the class loader context stored in the oat file with EncodeContextForOatFile.
86 // Returns true if the format matches, or false otherwise. If the return is true, the out
87 // arguments will contain the classpath dex files, their checksums and whether or not the
88 // context is a special shared library.
89 // The method asserts that the context is made out of only one PathClassLoader.
90 static bool DecodePathClassLoaderContextFromOatFileKey(
91 const std::string& context_spec,
92 std::vector<std::string>* out_classpath,
93 std::vector<uint32_t>* out_checksums,
94 bool* out_is_special_shared_library);
95
Calin Juravle87e2cb62017-06-13 21:48:45 -070096 private:
97 enum ClassLoaderType {
98 kInvalidClassLoader = 0,
99 kPathClassLoader = 1,
100 kDelegateLastClassLoader = 2
101 };
102
103 struct ClassLoaderInfo {
104 // The type of this class loader.
105 ClassLoaderType type;
106 // The list of class path elements that this loader loads.
107 // Note that this list may contain relative paths.
108 std::vector<std::string> classpath;
Calin Juravle7b0648a2017-07-07 18:40:50 -0700109 // The list of class path elements checksums.
110 // May be empty if the checksums are not given when the context is created.
111 std::vector<uint32_t> checksums;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700112 // After OpenDexFiles is called this holds the opened dex files.
113 std::vector<std::unique_ptr<const DexFile>> opened_dex_files;
114 // After OpenDexFiles, in case some of the dex files were opened from their oat files
115 // this holds the list of opened oat files.
116 std::vector<std::unique_ptr<OatFile>> opened_oat_files;
117
118 explicit ClassLoaderInfo(ClassLoaderType cl_type) : type(cl_type) {}
119 };
120
121 // Reads the class loader spec in place and returns true if the spec is valid and the
122 // compilation context was constructed.
Calin Juravle7b0648a2017-07-07 18:40:50 -0700123 bool Parse(const std::string& spec, bool parse_checksums = false);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700124
125 // Attempts to parse a single class loader spec for the given class_loader_type.
126 // If successful the class loader spec will be added to the chain.
127 // Returns whether or not the operation was successful.
128 bool ParseClassLoaderSpec(const std::string& class_loader_spec,
Calin Juravle7b0648a2017-07-07 18:40:50 -0700129 ClassLoaderType class_loader_type,
130 bool parse_checksums = false);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700131
132 // Extracts the class loader type from the given spec.
133 // Return ClassLoaderContext::kInvalidClassLoader if the class loader type is not
134 // recognized.
135 static ClassLoaderType ExtractClassLoaderType(const std::string& class_loader_spec);
136
137 // Returns the string representation of the class loader type.
138 // The returned format can be used when parsing a context spec.
139 static const char* GetClassLoaderTypeName(ClassLoaderType type);
140
141 // CHECKs that the dex files were opened (OpenDexFiles was called). Aborts if not.
142 void CheckDexFilesOpened(const std::string& calling_method) const;
143
144 // The class loader chain represented as a vector.
145 // The parent of class_loader_chain_[i] is class_loader_chain_[i++].
146 // The parent of the last element is assumed to be the boot class loader.
147 std::vector<ClassLoaderInfo> class_loader_chain_;
148
149 // Whether or not the class loader context should be ignored at runtime when loading the oat
150 // files. When true, dex2oat will use OatFile::kSpecialSharedLibrary as the classpath key in
151 // the oat file.
152 // TODO(calin): Can we get rid of this and cover all relevant use cases?
153 // (e.g. packages using prebuild system packages as shared libraries b/36480683)
154 bool special_shared_library_;
155
156 // Whether or not OpenDexFiles() was called.
157 bool dex_files_open_attempted_;
158 // The result of the last OpenDexFiles() operation.
159 bool dex_files_open_result_;
160
161 friend class ClassLoaderContextTest;
162
163 DISALLOW_COPY_AND_ASSIGN(ClassLoaderContext);
164};
165
166} // namespace art
167#endif // ART_RUNTIME_CLASS_LOADER_CONTEXT_H_