blob: 038474e31fb493d5d3504a99a132486189a77b2b [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
Calin Juravle27e0d1f2017-07-26 00:16:07 -070038class ClassLoaderContext;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070039class DexFile;
40class 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.
46class OatFileManager {
47 public:
Vladimir Markob0b68cf2017-11-14 18:11:50 +000048 OatFileManager();
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070049 ~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 Chartiere58991b2015-10-13 07:59:34 -070056 void UnRegisterAndDeleteOatFile(const OatFile* oat_file)
57 REQUIRES(!Locks::oat_file_manager_lock_);
58
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070059 // 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 Juravle0b791272016-04-18 16:38:27 +010063 // 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
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070068 // Returns true if we have a non pic oat file.
69 bool HaveNonPicOatFile() const {
70 return have_non_pic_oat_file_;
71 }
72
Jeff Haodcdc85b2015-12-04 14:06:18 -080073 // Returns the boot image oat files.
74 std::vector<const OatFile*> GetBootOatFiles() const;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070075
76 // Returns the first non-image oat file in the class path.
77 const OatFile* GetPrimaryOatFile() const REQUIRES(!Locks::oat_file_manager_lock_);
78
Jeff Haodcdc85b2015-12-04 14:06:18 -080079 // Returns the oat files for the images, registers the oat files.
80 // Takes ownership of the imagespace's underlying oat files.
81 std::vector<const OatFile*> RegisterImageOatFiles(std::vector<gc::space::ImageSpace*> spaces)
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070082 REQUIRES(!Locks::oat_file_manager_lock_);
83
84 // Finds or creates the oat file holding dex_location. Then loads and returns
85 // all corresponding dex files (there may be more than one dex file loaded
86 // in the case of multidex).
87 // This may return the original, unquickened dex files if the oat file could
88 // not be generated.
89 //
90 // Returns an empty vector if the dex files could not be loaded. In this
91 // case, there will be at least one error message returned describing why no
92 // dex files could not be loaded. The 'error_msgs' argument must not be
93 // null, regardless of whether there is an error or not.
94 //
95 // This method should not be called with the mutator_lock_ held, because it
96 // could end up starving GC if we need to generate or relocate any oat
97 // files.
98 std::vector<std::unique_ptr<const DexFile>> OpenDexFilesFromOat(
99 const char* dex_location,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800100 jobject class_loader,
101 jobjectArray dex_elements,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700102 /*out*/ const OatFile** out_oat_file,
103 /*out*/ std::vector<std::string>* error_msgs)
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700104 REQUIRES(!Locks::oat_file_manager_lock_, !Locks::mutator_lock_);
105
Nicolas Geoffray04680f32016-03-17 11:56:54 +0000106 void DumpForSigQuit(std::ostream& os);
107
Nicolas Geoffray68bf3902017-09-07 14:40:48 +0100108 void SetOnlyUseSystemOatFiles();
109
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700110 private:
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700111 // Check that the class loader context of the given oat file matches the given context.
112 // This will perform a check that all class loaders in the chain have the same type and
113 // classpath.
114 // If the context is null (which means the initial class loader was null or unsupported)
115 // this returns false.
116 // If the context does not validate the method will check for duplicate class definitions of
117 // the given oat file against the oat files (either from the class loaders if possible or all
118 // non-boot oat files otherwise).
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700119 // Return true if there are any class definition collisions in the oat_file.
Jeff Haof0192c82016-03-28 20:39:50 -0700120 bool HasCollisions(const OatFile* oat_file,
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700121 const ClassLoaderContext* context,
Jeff Haof0192c82016-03-28 20:39:50 -0700122 /*out*/ std::string* error_msg) const
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700123 REQUIRES(!Locks::oat_file_manager_lock_);
124
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700125 const OatFile* FindOpenedOatFileFromOatLocationLocked(const std::string& oat_location) const
126 REQUIRES(Locks::oat_file_manager_lock_);
127
128 std::set<std::unique_ptr<const OatFile>> oat_files_ GUARDED_BY(Locks::oat_file_manager_lock_);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700129 bool have_non_pic_oat_file_;
Nicolas Geoffray29742602017-12-14 10:09:03 +0000130
131 // Only use the compiled code in an OAT file when the file is on /system. If the OAT file
132 // is not on /system, don't load it "executable".
Nicolas Geoffray68bf3902017-09-07 14:40:48 +0100133 bool only_use_system_oat_files_;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000134
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700135 DISALLOW_COPY_AND_ASSIGN(OatFileManager);
136};
137
138} // namespace art
139
140#endif // ART_RUNTIME_OAT_FILE_MANAGER_H_