blob: 05a5f5b7b9a0ee197677ad06d5820633df64781b [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
26#include "base/macros.h"
27#include "base/mutex.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
38class DexFile;
39class OatFile;
40
41// Class for dealing with oat file management.
42//
43// This class knows about all the loaded oat files and provides utility functions. The oat file
44// pointers returned from functions are always valid.
45class OatFileManager {
46 public:
47 OatFileManager() : have_non_pic_oat_file_(false) {}
48 ~OatFileManager();
49
50 // Add an oat file to the internal accounting, std::aborts if there already exists an oat file
51 // with the same base address. Returns the oat file pointer from oat_file.
52 const OatFile* RegisterOatFile(std::unique_ptr<const OatFile> oat_file)
53 REQUIRES(!Locks::oat_file_manager_lock_);
54
Mathieu Chartiere58991b2015-10-13 07:59:34 -070055 void UnRegisterAndDeleteOatFile(const OatFile* oat_file)
56 REQUIRES(!Locks::oat_file_manager_lock_);
57
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070058 // Find the first opened oat file with the same location, returns null if there are none.
59 const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location) const
60 REQUIRES(!Locks::oat_file_manager_lock_);
61
Calin Juravle0b791272016-04-18 16:38:27 +010062 // Find the oat file which contains a dex files with the given dex base location,
63 // returns null if there are none.
64 const OatFile* FindOpenedOatFileFromDexLocation(const std::string& dex_base_location) const
65 REQUIRES(!Locks::oat_file_manager_lock_);
66
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070067 // Returns true if we have a non pic oat file.
68 bool HaveNonPicOatFile() const {
69 return have_non_pic_oat_file_;
70 }
71
Jeff Haodcdc85b2015-12-04 14:06:18 -080072 // Returns the boot image oat files.
73 std::vector<const OatFile*> GetBootOatFiles() const;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070074
75 // Returns the first non-image oat file in the class path.
76 const OatFile* GetPrimaryOatFile() const REQUIRES(!Locks::oat_file_manager_lock_);
77
Jeff Haodcdc85b2015-12-04 14:06:18 -080078 // Returns the oat files for the images, registers the oat files.
79 // Takes ownership of the imagespace's underlying oat files.
80 std::vector<const OatFile*> RegisterImageOatFiles(std::vector<gc::space::ImageSpace*> spaces)
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070081 REQUIRES(!Locks::oat_file_manager_lock_);
82
83 // Finds or creates the oat file holding dex_location. Then loads and returns
84 // all corresponding dex files (there may be more than one dex file loaded
85 // in the case of multidex).
86 // This may return the original, unquickened dex files if the oat file could
87 // not be generated.
88 //
89 // Returns an empty vector if the dex files could not be loaded. In this
90 // case, there will be at least one error message returned describing why no
91 // dex files could not be loaded. The 'error_msgs' argument must not be
92 // null, regardless of whether there is an error or not.
93 //
94 // This method should not be called with the mutator_lock_ held, because it
95 // could end up starving GC if we need to generate or relocate any oat
96 // files.
97 std::vector<std::unique_ptr<const DexFile>> OpenDexFilesFromOat(
98 const char* dex_location,
Mathieu Chartierfbc31082016-01-24 11:59:56 -080099 jobject class_loader,
100 jobjectArray dex_elements,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700101 /*out*/ const OatFile** out_oat_file,
102 /*out*/ std::vector<std::string>* error_msgs)
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700103 REQUIRES(!Locks::oat_file_manager_lock_, !Locks::mutator_lock_);
104
Nicolas Geoffray04680f32016-03-17 11:56:54 +0000105 void DumpForSigQuit(std::ostream& os);
106
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700107 private:
Jeff Haof0192c82016-03-28 20:39:50 -0700108 // Check that the shared libraries in the given oat file match those in the given class loader and
109 // dex elements. If the class loader is null or we do not support one of the class loaders in the
110 // chain, compare against all non-boot oat files instead. If the shared libraries are not ok,
111 // check for duplicate class definitions of the given oat file against the oat files (either from
112 // the class loader and dex elements if possible or all non-boot oat files otherwise).
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700113 // Return true if there are any class definition collisions in the oat_file.
Jeff Haof0192c82016-03-28 20:39:50 -0700114 bool HasCollisions(const OatFile* oat_file,
115 jobject class_loader,
116 jobjectArray dex_elements,
117 /*out*/ std::string* error_msg) const
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700118 REQUIRES(!Locks::oat_file_manager_lock_);
119
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700120 const OatFile* FindOpenedOatFileFromOatLocationLocked(const std::string& oat_location) const
121 REQUIRES(Locks::oat_file_manager_lock_);
122
123 std::set<std::unique_ptr<const OatFile>> oat_files_ GUARDED_BY(Locks::oat_file_manager_lock_);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700124 bool have_non_pic_oat_file_;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000125
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700126 DISALLOW_COPY_AND_ASSIGN(OatFileManager);
127};
128
129} // namespace art
130
131#endif // ART_RUNTIME_OAT_FILE_MANAGER_H_