blob: f98102e844bd6690ff975df2fd969947477ef479 [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"
Andreas Gampe29d38e72016-03-23 15:31:51 +000028#include "compiler_filter.h"
Mathieu Chartierfbc31082016-01-24 11:59:56 -080029#include "jni.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070030
31namespace art {
32
33namespace gc {
34namespace space {
35class ImageSpace;
36} // namespace space
37} // namespace gc
38
39class 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:
48 OatFileManager() : have_non_pic_oat_file_(false) {}
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 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 Chartiere58991b2015-10-13 07:59:34 -070068 // Attempt to reserve a location, returns false if it is already reserved or already in used by
69 // an oat file.
70 bool RegisterOatFileLocation(const std::string& oat_location)
71 REQUIRES(!Locks::oat_file_count_lock_);
72
73 // Unreserve oat file location, should only be used for error cases since RegisterOatFile will
74 // remove the reserved location.
75 void UnRegisterOatFileLocation(const std::string& oat_location)
76 REQUIRES(!Locks::oat_file_count_lock_);
77
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070078 // Returns true if we have a non pic oat file.
79 bool HaveNonPicOatFile() const {
80 return have_non_pic_oat_file_;
81 }
82
Jeff Haodcdc85b2015-12-04 14:06:18 -080083 // Returns the boot image oat files.
84 std::vector<const OatFile*> GetBootOatFiles() const;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070085
86 // Returns the first non-image oat file in the class path.
87 const OatFile* GetPrimaryOatFile() const REQUIRES(!Locks::oat_file_manager_lock_);
88
Jeff Haodcdc85b2015-12-04 14:06:18 -080089 // Returns the oat files for the images, registers the oat files.
90 // Takes ownership of the imagespace's underlying oat files.
91 std::vector<const OatFile*> RegisterImageOatFiles(std::vector<gc::space::ImageSpace*> spaces)
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070092 REQUIRES(!Locks::oat_file_manager_lock_);
93
94 // Finds or creates the oat file holding dex_location. Then loads and returns
95 // all corresponding dex files (there may be more than one dex file loaded
96 // in the case of multidex).
97 // This may return the original, unquickened dex files if the oat file could
98 // not be generated.
99 //
100 // Returns an empty vector if the dex files could not be loaded. In this
101 // case, there will be at least one error message returned describing why no
102 // dex files could not be loaded. The 'error_msgs' argument must not be
103 // null, regardless of whether there is an error or not.
104 //
105 // This method should not be called with the mutator_lock_ held, because it
106 // could end up starving GC if we need to generate or relocate any oat
107 // files.
108 std::vector<std::unique_ptr<const DexFile>> OpenDexFilesFromOat(
109 const char* dex_location,
110 const char* oat_location,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800111 jobject class_loader,
112 jobjectArray dex_elements,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700113 /*out*/ const OatFile** out_oat_file,
114 /*out*/ std::vector<std::string>* error_msgs)
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700115 REQUIRES(!Locks::oat_file_manager_lock_, !Locks::mutator_lock_);
116
Nicolas Geoffray04680f32016-03-17 11:56:54 +0000117 void DumpForSigQuit(std::ostream& os);
118
Andreas Gampe29d38e72016-03-23 15:31:51 +0000119 static void SetCompilerFilter(CompilerFilter::Filter filter) {
120 filter_ = filter;
121 }
122
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700123 private:
124 // Check for duplicate class definitions of the given oat file against all open oat files.
125 // Return true if there are any class definition collisions in the oat_file.
126 bool HasCollisions(const OatFile* oat_file, /*out*/std::string* error_msg) const
127 REQUIRES(!Locks::oat_file_manager_lock_);
128
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700129 const OatFile* FindOpenedOatFileFromOatLocationLocked(const std::string& oat_location) const
130 REQUIRES(Locks::oat_file_manager_lock_);
131
132 std::set<std::unique_ptr<const OatFile>> oat_files_ GUARDED_BY(Locks::oat_file_manager_lock_);
133 std::unordered_map<std::string, size_t> oat_file_count_ GUARDED_BY(Locks::oat_file_count_lock_);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700134 bool have_non_pic_oat_file_;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000135
136 // The compiler filter used for oat files loaded by the oat file manager.
137 static CompilerFilter::Filter filter_;
138
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700139 DISALLOW_COPY_AND_ASSIGN(OatFileManager);
140};
141
142} // namespace art
143
144#endif // ART_RUNTIME_OAT_FILE_MANAGER_H_