Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1 | /* |
| 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 Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 21 | #include <set> |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 22 | #include <string> |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 23 | #include <unordered_map> |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 24 | #include <vector> |
| 25 | |
Andreas Gampe | 7fbc4a5 | 2018-11-28 08:26:47 -0800 | [diff] [blame] | 26 | #include "base/locks.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 27 | #include "base/macros.h" |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 28 | #include "jni.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 29 | |
| 30 | namespace art { |
| 31 | |
| 32 | namespace gc { |
| 33 | namespace space { |
| 34 | class ImageSpace; |
| 35 | } // namespace space |
| 36 | } // namespace gc |
| 37 | |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 38 | class ClassLoaderContext; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 39 | class DexFile; |
| 40 | class OatFile; |
| 41 | |
| 42 | // Class for dealing with oat file management. |
| 43 | // |
| 44 | // This class knows about all the loaded oat files and provides utility functions. The oat file |
| 45 | // pointers returned from functions are always valid. |
| 46 | class OatFileManager { |
| 47 | public: |
Vladimir Marko | b0b68cf | 2017-11-14 18:11:50 +0000 | [diff] [blame] | 48 | OatFileManager(); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 49 | ~OatFileManager(); |
| 50 | |
| 51 | // Add an oat file to the internal accounting, std::aborts if there already exists an oat file |
| 52 | // with the same base address. Returns the oat file pointer from oat_file. |
| 53 | const OatFile* RegisterOatFile(std::unique_ptr<const OatFile> oat_file) |
| 54 | REQUIRES(!Locks::oat_file_manager_lock_); |
| 55 | |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 56 | void UnRegisterAndDeleteOatFile(const OatFile* oat_file) |
| 57 | REQUIRES(!Locks::oat_file_manager_lock_); |
| 58 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 59 | // Find the first opened oat file with the same location, returns null if there are none. |
| 60 | const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location) const |
| 61 | REQUIRES(!Locks::oat_file_manager_lock_); |
| 62 | |
Calin Juravle | 0b79127 | 2016-04-18 16:38:27 +0100 | [diff] [blame] | 63 | // Find the oat file which contains a dex files with the given dex base location, |
| 64 | // returns null if there are none. |
| 65 | const OatFile* FindOpenedOatFileFromDexLocation(const std::string& dex_base_location) const |
| 66 | REQUIRES(!Locks::oat_file_manager_lock_); |
| 67 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 68 | // Returns the boot image oat files. |
| 69 | std::vector<const OatFile*> GetBootOatFiles() const; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 70 | |
| 71 | // Returns the first non-image oat file in the class path. |
| 72 | const OatFile* GetPrimaryOatFile() const REQUIRES(!Locks::oat_file_manager_lock_); |
| 73 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 74 | // Returns the oat files for the images, registers the oat files. |
| 75 | // Takes ownership of the imagespace's underlying oat files. |
Stephen Hines | 48ba197 | 2018-09-24 13:35:54 -0700 | [diff] [blame] | 76 | std::vector<const OatFile*> RegisterImageOatFiles( |
| 77 | const std::vector<gc::space::ImageSpace*>& spaces) |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 78 | REQUIRES(!Locks::oat_file_manager_lock_); |
| 79 | |
| 80 | // Finds or creates the oat file holding dex_location. Then loads and returns |
| 81 | // all corresponding dex files (there may be more than one dex file loaded |
| 82 | // in the case of multidex). |
| 83 | // This may return the original, unquickened dex files if the oat file could |
| 84 | // not be generated. |
| 85 | // |
| 86 | // Returns an empty vector if the dex files could not be loaded. In this |
| 87 | // case, there will be at least one error message returned describing why no |
| 88 | // dex files could not be loaded. The 'error_msgs' argument must not be |
| 89 | // null, regardless of whether there is an error or not. |
| 90 | // |
| 91 | // This method should not be called with the mutator_lock_ held, because it |
| 92 | // could end up starving GC if we need to generate or relocate any oat |
| 93 | // files. |
| 94 | std::vector<std::unique_ptr<const DexFile>> OpenDexFilesFromOat( |
| 95 | const char* dex_location, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 96 | jobject class_loader, |
| 97 | jobjectArray dex_elements, |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 98 | /*out*/ const OatFile** out_oat_file, |
| 99 | /*out*/ std::vector<std::string>* error_msgs) |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 100 | REQUIRES(!Locks::oat_file_manager_lock_, !Locks::mutator_lock_); |
| 101 | |
Nicolas Geoffray | 04680f3 | 2016-03-17 11:56:54 +0000 | [diff] [blame] | 102 | void DumpForSigQuit(std::ostream& os); |
| 103 | |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 104 | void SetOnlyUseSystemOatFiles(); |
| 105 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 106 | private: |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 107 | enum class CheckCollisionResult { |
| 108 | kSkippedUnsupportedClassLoader, |
| 109 | kSkippedClassLoaderContextSharedLibrary, |
| 110 | kNoCollisions, |
| 111 | kPerformedHasCollisions, |
| 112 | }; |
| 113 | |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 114 | // Check that the class loader context of the given oat file matches the given context. |
| 115 | // This will perform a check that all class loaders in the chain have the same type and |
| 116 | // classpath. |
| 117 | // If the context is null (which means the initial class loader was null or unsupported) |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 118 | // this returns kSkippedUnsupportedClassLoader. |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 119 | // If the context does not validate the method will check for duplicate class definitions of |
| 120 | // the given oat file against the oat files (either from the class loaders if possible or all |
| 121 | // non-boot oat files otherwise). |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 122 | // Return kPerformedHasCollisions if there are any class definition collisions in the oat_file. |
| 123 | CheckCollisionResult CheckCollision(const OatFile* oat_file, |
| 124 | const ClassLoaderContext* context, |
| 125 | /*out*/ std::string* error_msg) const |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 126 | REQUIRES(!Locks::oat_file_manager_lock_); |
| 127 | |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 128 | const OatFile* FindOpenedOatFileFromOatLocationLocked(const std::string& oat_location) const |
| 129 | REQUIRES(Locks::oat_file_manager_lock_); |
| 130 | |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 131 | // Return true if we should accept the oat file. |
| 132 | bool AcceptOatFile(CheckCollisionResult result) const; |
| 133 | |
| 134 | // Return true if we should attempt to load the app image. |
| 135 | bool ShouldLoadAppImage(CheckCollisionResult check_collision_result, |
| 136 | const OatFile* source_oat_file, |
| 137 | ClassLoaderContext* context, |
| 138 | std::string* error_msg); |
| 139 | |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 140 | std::set<std::unique_ptr<const OatFile>> oat_files_ GUARDED_BY(Locks::oat_file_manager_lock_); |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 141 | |
| 142 | // Only use the compiled code in an OAT file when the file is on /system. If the OAT file |
| 143 | // is not on /system, don't load it "executable". |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 144 | bool only_use_system_oat_files_; |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 145 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 146 | DISALLOW_COPY_AND_ASSIGN(OatFileManager); |
| 147 | }; |
| 148 | |
| 149 | } // namespace art |
| 150 | |
| 151 | #endif // ART_RUNTIME_OAT_FILE_MANAGER_H_ |