blob: 574d0e2582174d672fcc6ef9c8069fb0c76c2510 [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
Mathieu Chartiere58991b2015-10-13 07:59:34 -070063 // Attempt to reserve a location, returns false if it is already reserved or already in used by
64 // an oat file.
65 bool RegisterOatFileLocation(const std::string& oat_location)
66 REQUIRES(!Locks::oat_file_count_lock_);
67
68 // Unreserve oat file location, should only be used for error cases since RegisterOatFile will
69 // remove the reserved location.
70 void UnRegisterOatFileLocation(const std::string& oat_location)
71 REQUIRES(!Locks::oat_file_count_lock_);
72
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070073 // Returns true if we have a non pic oat file.
74 bool HaveNonPicOatFile() const {
75 return have_non_pic_oat_file_;
76 }
77
Jeff Haodcdc85b2015-12-04 14:06:18 -080078 // Returns the boot image oat files.
79 std::vector<const OatFile*> GetBootOatFiles() const;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070080
81 // Returns the first non-image oat file in the class path.
82 const OatFile* GetPrimaryOatFile() const REQUIRES(!Locks::oat_file_manager_lock_);
83
Jeff Haodcdc85b2015-12-04 14:06:18 -080084 // Returns the oat files for the images, registers the oat files.
85 // Takes ownership of the imagespace's underlying oat files.
86 std::vector<const OatFile*> RegisterImageOatFiles(std::vector<gc::space::ImageSpace*> spaces)
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070087 REQUIRES(!Locks::oat_file_manager_lock_);
88
89 // Finds or creates the oat file holding dex_location. Then loads and returns
90 // all corresponding dex files (there may be more than one dex file loaded
91 // in the case of multidex).
92 // This may return the original, unquickened dex files if the oat file could
93 // not be generated.
94 //
95 // Returns an empty vector if the dex files could not be loaded. In this
96 // case, there will be at least one error message returned describing why no
97 // dex files could not be loaded. The 'error_msgs' argument must not be
98 // null, regardless of whether there is an error or not.
99 //
100 // This method should not be called with the mutator_lock_ held, because it
101 // could end up starving GC if we need to generate or relocate any oat
102 // files.
103 std::vector<std::unique_ptr<const DexFile>> OpenDexFilesFromOat(
104 const char* dex_location,
105 const char* oat_location,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800106 jobject class_loader,
107 jobjectArray dex_elements,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700108 /*out*/ const OatFile** out_oat_file,
109 /*out*/ std::vector<std::string>* error_msgs)
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700110 REQUIRES(!Locks::oat_file_manager_lock_, !Locks::mutator_lock_);
111
Nicolas Geoffray04680f32016-03-17 11:56:54 +0000112 void DumpForSigQuit(std::ostream& os);
113
Andreas Gampe29d38e72016-03-23 15:31:51 +0000114 static void SetCompilerFilter(CompilerFilter::Filter filter) {
115 filter_ = filter;
116 }
117
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700118 private:
119 // Check for duplicate class definitions of the given oat file against all open oat files.
120 // Return true if there are any class definition collisions in the oat_file.
121 bool HasCollisions(const OatFile* oat_file, /*out*/std::string* error_msg) const
122 REQUIRES(!Locks::oat_file_manager_lock_);
123
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700124 const OatFile* FindOpenedOatFileFromOatLocationLocked(const std::string& oat_location) const
125 REQUIRES(Locks::oat_file_manager_lock_);
126
127 std::set<std::unique_ptr<const OatFile>> oat_files_ GUARDED_BY(Locks::oat_file_manager_lock_);
128 std::unordered_map<std::string, size_t> oat_file_count_ GUARDED_BY(Locks::oat_file_count_lock_);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700129 bool have_non_pic_oat_file_;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000130
131 // The compiler filter used for oat files loaded by the oat file manager.
132 static CompilerFilter::Filter filter_;
133
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700134 DISALLOW_COPY_AND_ASSIGN(OatFileManager);
135};
136
137} // namespace art
138
139#endif // ART_RUNTIME_OAT_FILE_MANAGER_H_