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 | |
| 26 | #include "base/macros.h" |
| 27 | #include "base/mutex.h" |
| 28 | |
| 29 | namespace art { |
| 30 | |
| 31 | namespace gc { |
| 32 | namespace space { |
| 33 | class ImageSpace; |
| 34 | } // namespace space |
| 35 | } // namespace gc |
| 36 | |
| 37 | class DexFile; |
| 38 | class OatFile; |
| 39 | |
| 40 | // Class for dealing with oat file management. |
| 41 | // |
| 42 | // This class knows about all the loaded oat files and provides utility functions. The oat file |
| 43 | // pointers returned from functions are always valid. |
| 44 | class OatFileManager { |
| 45 | public: |
| 46 | OatFileManager() : have_non_pic_oat_file_(false) {} |
| 47 | ~OatFileManager(); |
| 48 | |
| 49 | // Add an oat file to the internal accounting, std::aborts if there already exists an oat file |
| 50 | // with the same base address. Returns the oat file pointer from oat_file. |
| 51 | const OatFile* RegisterOatFile(std::unique_ptr<const OatFile> oat_file) |
| 52 | REQUIRES(!Locks::oat_file_manager_lock_); |
| 53 | |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 54 | void UnRegisterAndDeleteOatFile(const OatFile* oat_file) |
| 55 | REQUIRES(!Locks::oat_file_manager_lock_); |
| 56 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 57 | // Find the first opened oat file with the same location, returns null if there are none. |
| 58 | const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location) const |
| 59 | REQUIRES(!Locks::oat_file_manager_lock_); |
| 60 | |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 61 | // Attempt to reserve a location, returns false if it is already reserved or already in used by |
| 62 | // an oat file. |
| 63 | bool RegisterOatFileLocation(const std::string& oat_location) |
| 64 | REQUIRES(!Locks::oat_file_count_lock_); |
| 65 | |
| 66 | // Unreserve oat file location, should only be used for error cases since RegisterOatFile will |
| 67 | // remove the reserved location. |
| 68 | void UnRegisterOatFileLocation(const std::string& oat_location) |
| 69 | REQUIRES(!Locks::oat_file_count_lock_); |
| 70 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 71 | // Returns true if we have a non pic oat file. |
| 72 | bool HaveNonPicOatFile() const { |
| 73 | return have_non_pic_oat_file_; |
| 74 | } |
| 75 | |
| 76 | // Returns the boot image oat file. |
| 77 | const OatFile* GetBootOatFile() const; |
| 78 | |
| 79 | // Returns the first non-image oat file in the class path. |
| 80 | const OatFile* GetPrimaryOatFile() const REQUIRES(!Locks::oat_file_manager_lock_); |
| 81 | |
| 82 | // Return the oat file for an image, registers the oat file. Takes ownership of the imagespace's |
| 83 | // underlying oat file. |
| 84 | const OatFile* RegisterImageOatFile(gc::space::ImageSpace* space) |
| 85 | REQUIRES(!Locks::oat_file_manager_lock_); |
| 86 | |
| 87 | // Finds or creates the oat file holding dex_location. Then loads and returns |
| 88 | // all corresponding dex files (there may be more than one dex file loaded |
| 89 | // in the case of multidex). |
| 90 | // This may return the original, unquickened dex files if the oat file could |
| 91 | // not be generated. |
| 92 | // |
| 93 | // Returns an empty vector if the dex files could not be loaded. In this |
| 94 | // case, there will be at least one error message returned describing why no |
| 95 | // dex files could not be loaded. The 'error_msgs' argument must not be |
| 96 | // null, regardless of whether there is an error or not. |
| 97 | // |
| 98 | // This method should not be called with the mutator_lock_ held, because it |
| 99 | // could end up starving GC if we need to generate or relocate any oat |
| 100 | // files. |
| 101 | std::vector<std::unique_ptr<const DexFile>> OpenDexFilesFromOat( |
| 102 | const char* dex_location, |
| 103 | const char* oat_location, |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 104 | /*out*/ const OatFile** out_oat_file, |
| 105 | /*out*/ std::vector<std::string>* error_msgs) |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 106 | REQUIRES(!Locks::oat_file_manager_lock_, !Locks::mutator_lock_); |
| 107 | |
| 108 | private: |
| 109 | // Check for duplicate class definitions of the given oat file against all open oat files. |
| 110 | // Return true if there are any class definition collisions in the oat_file. |
| 111 | bool HasCollisions(const OatFile* oat_file, /*out*/std::string* error_msg) const |
| 112 | REQUIRES(!Locks::oat_file_manager_lock_); |
| 113 | |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 114 | const OatFile* FindOpenedOatFileFromOatLocationLocked(const std::string& oat_location) const |
| 115 | REQUIRES(Locks::oat_file_manager_lock_); |
| 116 | |
| 117 | std::set<std::unique_ptr<const OatFile>> oat_files_ GUARDED_BY(Locks::oat_file_manager_lock_); |
| 118 | std::unordered_map<std::string, size_t> oat_file_count_ GUARDED_BY(Locks::oat_file_count_lock_); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 119 | bool have_non_pic_oat_file_; |
| 120 | DISALLOW_COPY_AND_ASSIGN(OatFileManager); |
| 121 | }; |
| 122 | |
| 123 | } // namespace art |
| 124 | |
| 125 | #endif // ART_RUNTIME_OAT_FILE_MANAGER_H_ |