blob: e5eae9f2582e68848c56089da61d50a3a7fca6c2 [file] [log] [blame]
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001/*
2 * Copyright (C) 2015 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_OAT_FILE_MANAGER_H_
18#define ART_RUNTIME_OAT_FILE_MANAGER_H_
19
20#include <memory>
Mathieu Chartiere58991b2015-10-13 07:59:34 -070021#include <set>
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070022#include <string>
Mathieu Chartiere58991b2015-10-13 07:59:34 -070023#include <unordered_map>
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070024#include <vector>
25
Andreas Gampe7fbc4a52018-11-28 08:26:47 -080026#include "base/locks.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070027#include "base/macros.h"
Mathieu Chartierfbc31082016-01-24 11:59:56 -080028#include "jni.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070029
30namespace art {
31
32namespace gc {
33namespace space {
34class ImageSpace;
35} // namespace space
36} // namespace gc
37
Calin Juravle27e0d1f2017-07-26 00:16:07 -070038class ClassLoaderContext;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070039class DexFile;
40class OatFile;
David Brazdil331a5e12019-04-01 22:46:16 +000041class ThreadPool;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070042
43// Class for dealing with oat file management.
44//
45// This class knows about all the loaded oat files and provides utility functions. The oat file
46// pointers returned from functions are always valid.
47class OatFileManager {
48 public:
Vladimir Markob0b68cf2017-11-14 18:11:50 +000049 OatFileManager();
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070050 ~OatFileManager();
51
52 // Add an oat file to the internal accounting, std::aborts if there already exists an oat file
53 // with the same base address. Returns the oat file pointer from oat_file.
54 const OatFile* RegisterOatFile(std::unique_ptr<const OatFile> oat_file)
55 REQUIRES(!Locks::oat_file_manager_lock_);
56
Mathieu Chartiere58991b2015-10-13 07:59:34 -070057 void UnRegisterAndDeleteOatFile(const OatFile* oat_file)
58 REQUIRES(!Locks::oat_file_manager_lock_);
59
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070060 // Find the first opened oat file with the same location, returns null if there are none.
61 const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location) const
62 REQUIRES(!Locks::oat_file_manager_lock_);
63
Calin Juravle0b791272016-04-18 16:38:27 +010064 // Find the oat file which contains a dex files with the given dex base location,
65 // returns null if there are none.
66 const OatFile* FindOpenedOatFileFromDexLocation(const std::string& dex_base_location) const
67 REQUIRES(!Locks::oat_file_manager_lock_);
68
Jeff Haodcdc85b2015-12-04 14:06:18 -080069 // Returns the boot image oat files.
70 std::vector<const OatFile*> GetBootOatFiles() const;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070071
72 // Returns the first non-image oat file in the class path.
73 const OatFile* GetPrimaryOatFile() const REQUIRES(!Locks::oat_file_manager_lock_);
74
Jeff Haodcdc85b2015-12-04 14:06:18 -080075 // Returns the oat files for the images, registers the oat files.
76 // Takes ownership of the imagespace's underlying oat files.
Stephen Hines48ba1972018-09-24 13:35:54 -070077 std::vector<const OatFile*> RegisterImageOatFiles(
78 const std::vector<gc::space::ImageSpace*>& spaces)
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070079 REQUIRES(!Locks::oat_file_manager_lock_);
80
81 // Finds or creates the oat file holding dex_location. Then loads and returns
82 // all corresponding dex files (there may be more than one dex file loaded
83 // in the case of multidex).
84 // This may return the original, unquickened dex files if the oat file could
85 // not be generated.
86 //
87 // Returns an empty vector if the dex files could not be loaded. In this
88 // case, there will be at least one error message returned describing why no
89 // dex files could not be loaded. The 'error_msgs' argument must not be
90 // null, regardless of whether there is an error or not.
91 //
92 // This method should not be called with the mutator_lock_ held, because it
93 // could end up starving GC if we need to generate or relocate any oat
94 // files.
95 std::vector<std::unique_ptr<const DexFile>> OpenDexFilesFromOat(
96 const char* dex_location,
Mathieu Chartierfbc31082016-01-24 11:59:56 -080097 jobject class_loader,
98 jobjectArray dex_elements,
Mathieu Chartiere58991b2015-10-13 07:59:34 -070099 /*out*/ const OatFile** out_oat_file,
100 /*out*/ std::vector<std::string>* error_msgs)
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700101 REQUIRES(!Locks::oat_file_manager_lock_, !Locks::mutator_lock_);
102
Nicolas Geoffray04680f32016-03-17 11:56:54 +0000103 void DumpForSigQuit(std::ostream& os);
104
Andreas Gampe9ef308d2019-03-28 11:06:26 -0700105 void SetOnlyUseSystemOatFiles(bool enforce, bool assert_no_files_loaded);
Nicolas Geoffray68bf3902017-09-07 14:40:48 +0100106
David Brazdil331a5e12019-04-01 22:46:16 +0000107 // Spawn a background thread which verifies all classes in the given dex files.
108 void RunBackgroundVerification(const std::vector<const DexFile*>& dex_files,
David Brazdil35a3f6a2019-03-04 15:59:06 +0000109 jobject class_loader,
110 const char* class_loader_context);
David Brazdil331a5e12019-04-01 22:46:16 +0000111
112 // Wait for thread pool workers to be created. This is used during shutdown as
113 // threads are not allowed to attach while runtime is in shutdown lock.
114 void WaitForWorkersToBeCreated();
115
116 // If allocated, delete a thread pool of background verification threads.
117 void DeleteThreadPool();
118
119 // Wait for all background verification tasks to finish. This is only used by tests.
120 void WaitForBackgroundVerificationTasks();
121
David Brazdil35a3f6a2019-03-04 15:59:06 +0000122 // Maximum number of anonymous vdex files kept in the process' data folder.
123 static constexpr size_t kAnonymousVdexCacheSize = 8u;
124
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700125 private:
Mathieu Chartieradc90862018-05-11 13:03:06 -0700126 enum class CheckCollisionResult {
127 kSkippedUnsupportedClassLoader,
128 kSkippedClassLoaderContextSharedLibrary,
129 kNoCollisions,
130 kPerformedHasCollisions,
131 };
132
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700133 // Check that the class loader context of the given oat file matches the given context.
134 // This will perform a check that all class loaders in the chain have the same type and
135 // classpath.
136 // If the context is null (which means the initial class loader was null or unsupported)
Mathieu Chartieradc90862018-05-11 13:03:06 -0700137 // this returns kSkippedUnsupportedClassLoader.
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700138 // If the context does not validate the method will check for duplicate class definitions of
139 // the given oat file against the oat files (either from the class loaders if possible or all
140 // non-boot oat files otherwise).
Mathieu Chartieradc90862018-05-11 13:03:06 -0700141 // Return kPerformedHasCollisions if there are any class definition collisions in the oat_file.
142 CheckCollisionResult CheckCollision(const OatFile* oat_file,
143 const ClassLoaderContext* context,
144 /*out*/ std::string* error_msg) const
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700145 REQUIRES(!Locks::oat_file_manager_lock_);
146
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700147 const OatFile* FindOpenedOatFileFromOatLocationLocked(const std::string& oat_location) const
148 REQUIRES(Locks::oat_file_manager_lock_);
149
Mathieu Chartieradc90862018-05-11 13:03:06 -0700150 // Return true if we should accept the oat file.
151 bool AcceptOatFile(CheckCollisionResult result) const;
152
153 // Return true if we should attempt to load the app image.
154 bool ShouldLoadAppImage(CheckCollisionResult check_collision_result,
155 const OatFile* source_oat_file,
156 ClassLoaderContext* context,
157 std::string* error_msg);
158
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700159 std::set<std::unique_ptr<const OatFile>> oat_files_ GUARDED_BY(Locks::oat_file_manager_lock_);
Nicolas Geoffray29742602017-12-14 10:09:03 +0000160
161 // Only use the compiled code in an OAT file when the file is on /system. If the OAT file
162 // is not on /system, don't load it "executable".
Nicolas Geoffray68bf3902017-09-07 14:40:48 +0100163 bool only_use_system_oat_files_;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000164
David Brazdil331a5e12019-04-01 22:46:16 +0000165 // Single-thread pool used to run the verifier in the background.
166 std::unique_ptr<ThreadPool> verification_thread_pool_;
167
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700168 DISALLOW_COPY_AND_ASSIGN(OatFileManager);
169};
170
171} // namespace art
172
173#endif // ART_RUNTIME_OAT_FILE_MANAGER_H_