blob: c508c4bf07f7dbeca4e0ec3a9ce139653612a0e8 [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
Mathieu Chartiere58991b2015-10-13 07:59:34 -070062 // Attempt to reserve a location, returns false if it is already reserved or already in used by
63 // an oat file.
64 bool RegisterOatFileLocation(const std::string& oat_location)
65 REQUIRES(!Locks::oat_file_count_lock_);
66
67 // Unreserve oat file location, should only be used for error cases since RegisterOatFile will
68 // remove the reserved location.
69 void UnRegisterOatFileLocation(const std::string& oat_location)
70 REQUIRES(!Locks::oat_file_count_lock_);
71
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070072 // Returns true if we have a non pic oat file.
73 bool HaveNonPicOatFile() const {
74 return have_non_pic_oat_file_;
75 }
76
Jeff Haodcdc85b2015-12-04 14:06:18 -080077 // Returns the boot image oat files.
78 std::vector<const OatFile*> GetBootOatFiles() const;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070079
80 // Returns the first non-image oat file in the class path.
81 const OatFile* GetPrimaryOatFile() const REQUIRES(!Locks::oat_file_manager_lock_);
82
Jeff Haodcdc85b2015-12-04 14:06:18 -080083 // Returns the oat files for the images, registers the oat files.
84 // Takes ownership of the imagespace's underlying oat files.
85 std::vector<const OatFile*> RegisterImageOatFiles(std::vector<gc::space::ImageSpace*> spaces)
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070086 REQUIRES(!Locks::oat_file_manager_lock_);
87
88 // Finds or creates the oat file holding dex_location. Then loads and returns
89 // all corresponding dex files (there may be more than one dex file loaded
90 // in the case of multidex).
91 // This may return the original, unquickened dex files if the oat file could
92 // not be generated.
93 //
94 // Returns an empty vector if the dex files could not be loaded. In this
95 // case, there will be at least one error message returned describing why no
96 // dex files could not be loaded. The 'error_msgs' argument must not be
97 // null, regardless of whether there is an error or not.
98 //
99 // This method should not be called with the mutator_lock_ held, because it
100 // could end up starving GC if we need to generate or relocate any oat
101 // files.
102 std::vector<std::unique_ptr<const DexFile>> OpenDexFilesFromOat(
103 const char* dex_location,
104 const char* oat_location,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800105 jobject class_loader,
106 jobjectArray dex_elements,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700107 /*out*/ const OatFile** out_oat_file,
108 /*out*/ std::vector<std::string>* error_msgs)
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700109 REQUIRES(!Locks::oat_file_manager_lock_, !Locks::mutator_lock_);
110
111 private:
112 // Check for duplicate class definitions of the given oat file against all open oat files.
113 // Return true if there are any class definition collisions in the oat_file.
114 bool HasCollisions(const OatFile* oat_file, /*out*/std::string* error_msg) const
115 REQUIRES(!Locks::oat_file_manager_lock_);
116
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700117 const OatFile* FindOpenedOatFileFromOatLocationLocked(const std::string& oat_location) const
118 REQUIRES(Locks::oat_file_manager_lock_);
119
120 std::set<std::unique_ptr<const OatFile>> oat_files_ GUARDED_BY(Locks::oat_file_manager_lock_);
121 std::unordered_map<std::string, size_t> oat_file_count_ GUARDED_BY(Locks::oat_file_count_lock_);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700122 bool have_non_pic_oat_file_;
123 DISALLOW_COPY_AND_ASSIGN(OatFileManager);
124};
125
126} // namespace art
127
128#endif // ART_RUNTIME_OAT_FILE_MANAGER_H_