blob: c020dc1d8683fd90035afdcff29ec01290b91ac9 [file] [log] [blame]
Ian Rogers1d54e732013-05-02 21:10:01 -07001/*
2 * Copyright (C) 2011 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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_GC_SPACE_IMAGE_SPACE_H_
18#define ART_RUNTIME_GC_SPACE_IMAGE_SPACE_H_
Ian Rogers1d54e732013-05-02 21:10:01 -070019
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070020#include "gc/accounting/space_bitmap.h"
Andreas Gamped4901292017-05-30 18:41:34 -070021#include "image.h"
Andreas Gampe86823542019-02-25 09:38:49 -080022#include "image_space_loading_order.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070023#include "space.h"
24
25namespace art {
Brian Carlstrom56d947f2013-07-15 13:14:23 -070026
Vladimir Marko0ace5632018-12-14 11:11:47 +000027template <typename T> class ArrayRef;
28class DexFile;
Andreas Gampe639b2b12019-01-08 10:32:50 -080029enum class InstructionSet;
Brian Carlstrom56d947f2013-07-15 13:14:23 -070030class OatFile;
31
Ian Rogers1d54e732013-05-02 21:10:01 -070032namespace gc {
33namespace space {
34
35// An image space is a space backed with a memory mapped image.
36class ImageSpace : public MemMapSpace {
37 public:
Yi Kong39402542019-03-24 02:47:16 -070038 SpaceType GetType() const override {
Ian Rogers1d54e732013-05-02 21:10:01 -070039 return kSpaceTypeImageSpace;
40 }
41
Andreas Gampe2bd84282016-12-05 12:37:36 -080042 // Load boot image spaces from a primary image file for a specified instruction set.
Brian Carlstrom56d947f2013-07-15 13:14:23 -070043 //
Andreas Gampe2bd84282016-12-05 12:37:36 -080044 // On successful return, the loaded spaces are added to boot_image_spaces (which must be
Vladimir Marko91f10322018-12-07 18:04:10 +000045 // empty on entry) and `extra_reservation` is set to the requested reservation located
46 // after the end of the last loaded oat file.
Vladimir Marko82e1e272018-08-20 13:38:06 +000047 static bool LoadBootImage(
Vladimir Marko91f10322018-12-07 18:04:10 +000048 const std::vector<std::string>& boot_class_path,
49 const std::vector<std::string>& boot_class_path_locations,
Vladimir Marko82e1e272018-08-20 13:38:06 +000050 const std::string& image_location,
51 const InstructionSet image_isa,
Andreas Gampe86823542019-02-25 09:38:49 -080052 ImageSpaceLoadingOrder order,
Vladimir Marko3364d182019-03-13 13:55:01 +000053 bool relocate,
54 bool executable,
55 bool is_zygote,
Vladimir Markod44d7032018-08-30 13:02:31 +010056 size_t extra_reservation_size,
57 /*out*/std::vector<std::unique_ptr<space::ImageSpace>>* boot_image_spaces,
58 /*out*/MemMap* extra_reservation) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierfbc31082016-01-24 11:59:56 -080059
60 // Try to open an existing app image space.
Andreas Gampea463b6a2016-08-12 21:53:32 -070061 static std::unique_ptr<ImageSpace> CreateFromAppImage(const char* image,
62 const OatFile* oat_file,
63 std::string* error_msg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070064 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers1d54e732013-05-02 21:10:01 -070065
Narayan Kamath52f84882014-05-02 10:10:39 +010066 // Reads the image header from the specified image location for the
Mathieu Chartier2cebb242015-04-21 16:50:40 -070067 // instruction set image_isa. Returns null on failure, with
Brian Carlstrom31d8f522014-09-29 11:22:54 -070068 // reason in error_msg.
Vladimir Marko4df2d802018-09-27 16:42:44 +000069 static std::unique_ptr<ImageHeader> ReadImageHeader(const char* image_location,
70 InstructionSet image_isa,
Andreas Gampe86823542019-02-25 09:38:49 -080071 ImageSpaceLoadingOrder order,
Vladimir Marko4df2d802018-09-27 16:42:44 +000072 std::string* error_msg);
Brian Carlstrom31d8f522014-09-29 11:22:54 -070073
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070074 // Give access to the OatFile.
75 const OatFile* GetOatFile() const;
76
Brian Carlstrom56d947f2013-07-15 13:14:23 -070077 // Releases the OatFile from the ImageSpace so it can be transfer to
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070078 // the caller, presumably the OatFileManager.
79 std::unique_ptr<const OatFile> ReleaseOatFile();
Brian Carlstrom56d947f2013-07-15 13:14:23 -070080
Mathieu Chartier31e89252013-08-28 11:29:12 -070081 void VerifyImageAllocations()
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070082 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier31e89252013-08-28 11:29:12 -070083
Ian Rogers1d54e732013-05-02 21:10:01 -070084 const ImageHeader& GetImageHeader() const {
85 return *reinterpret_cast<ImageHeader*>(Begin());
86 }
87
Narayan Kamath52f84882014-05-02 10:10:39 +010088 // Actual filename where image was loaded from.
89 // For example: /data/dalvik-cache/arm/system@framework@boot.art
Ian Rogers1d54e732013-05-02 21:10:01 -070090 const std::string GetImageFilename() const {
91 return GetName();
92 }
93
Narayan Kamath52f84882014-05-02 10:10:39 +010094 // Symbolic location for image.
95 // For example: /system/framework/boot.art
96 const std::string GetImageLocation() const {
97 return image_location_;
98 }
99
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100100 accounting::ContinuousSpaceBitmap* GetLiveBitmap() const override {
Ian Rogers1d54e732013-05-02 21:10:01 -0700101 return live_bitmap_.get();
102 }
103
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100104 accounting::ContinuousSpaceBitmap* GetMarkBitmap() const override {
Ian Rogers1d54e732013-05-02 21:10:01 -0700105 // ImageSpaces have the same bitmap for both live and marked. This helps reduce the number of
106 // special cases to test against.
107 return live_bitmap_.get();
108 }
109
Yi Kong39402542019-03-24 02:47:16 -0700110 void Dump(std::ostream& os) const override;
Ian Rogers1d54e732013-05-02 21:10:01 -0700111
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800112 // Sweeping image spaces is a NOP.
113 void Sweep(bool /* swap_bitmaps */, size_t* /* freed_objects */, size_t* /* freed_bytes */) {
114 }
115
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100116 bool CanMoveObjects() const override {
Mathieu Chartier31f44142014-04-08 14:40:03 -0700117 return false;
118 }
119
Alex Lighta59dd802014-07-02 16:28:08 -0700120 // Returns the filename of the image corresponding to
121 // requested image_location, or the filename where a new image
122 // should be written if one doesn't exist. Looks for a generated
123 // image in the specified location and then in the dalvik-cache.
124 //
125 // Returns true if an image was found, false otherwise.
126 static bool FindImageFilename(const char* image_location,
127 InstructionSet image_isa,
128 std::string* system_location,
129 bool* has_system,
130 std::string* data_location,
131 bool* dalvik_cache_exists,
Andreas Gampe3c13a792014-09-18 20:56:04 -0700132 bool* has_data,
133 bool *is_global_cache);
Alex Lighta59dd802014-07-02 16:28:08 -0700134
Vladimir Marko0ace5632018-12-14 11:11:47 +0000135 // Returns the checksums for the boot image and extra boot class path dex files,
136 // based on the boot class path, image location and ISA (may differ from the ISA of an
137 // initialized Runtime). The boot image and dex files do not need to be loaded in memory.
Vladimir Markobcd99be2019-03-22 16:21:31 +0000138 static std::string GetBootClassPathChecksums(ArrayRef<const std::string> boot_class_path,
Vladimir Marko0ace5632018-12-14 11:11:47 +0000139 const std::string& image_location,
140 InstructionSet image_isa,
Andreas Gampe86823542019-02-25 09:38:49 -0800141 ImageSpaceLoadingOrder order,
Vladimir Marko0ace5632018-12-14 11:11:47 +0000142 /*out*/std::string* error_msg);
143
144 // Returns the checksums for the boot image and extra boot class path dex files,
145 // based on the boot image and boot class path dex files loaded in memory.
146 static std::string GetBootClassPathChecksums(const std::vector<ImageSpace*>& image_spaces,
147 const std::vector<const DexFile*>& boot_class_path);
148
Vladimir Marko91f10322018-12-07 18:04:10 +0000149 // Expand a single image location to multi-image locations based on the dex locations.
150 static std::vector<std::string> ExpandMultiImageLocations(
151 const std::vector<std::string>& dex_locations,
152 const std::string& image_location);
Mathieu Chartier866d8742016-09-21 15:24:18 -0700153
Richard Uhler84f50ae2017-02-06 15:12:45 +0000154 // Returns true if the dex checksums in the given oat file match the
155 // checksums of the original dex files on disk. This is intended to be used
156 // to validate the boot image oat file, which may contain dex entries from
157 // multiple different (possibly multidex) dex files on disk. Prefer the
158 // OatFileAssistant for validating regular app oat files because the
159 // OatFileAssistant caches dex checksums that are reused to check both the
160 // oat and odex file.
161 //
162 // This function is exposed for testing purposes.
163 static bool ValidateOatFile(const OatFile& oat_file, std::string* error_msg);
164
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800165 // Return the end of the image which includes non-heap objects such as ArtMethods and ArtFields.
166 uint8_t* GetImageEnd() const {
167 return Begin() + GetImageHeader().GetImageSize();
168 }
169
Mathieu Chartierd5f3f322016-03-21 14:05:56 -0700170 void DumpSections(std::ostream& os) const;
171
Igor Murashkin8275fba2017-05-02 15:58:02 -0700172 // De-initialize the image-space by undoing the effects in Init().
173 virtual ~ImageSpace();
174
Mathieu Chartier6e7a72c2019-03-07 21:40:10 -0800175 void DisablePreResolvedStrings() REQUIRES_SHARED(Locks::mutator_lock_);
176 void ReleaseMetadata() REQUIRES_SHARED(Locks::mutator_lock_);
177
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800178 protected:
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800179 // Tries to initialize an ImageSpace from the given image path, returning null on error.
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700180 //
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800181 // If validate_oat_file is false (for /system), do not verify that image's OatFile is up-to-date
182 // relative to its DexFile inputs. Otherwise (for /data), validate the inputs and generate the
183 // OatFile in /data/dalvik-cache if necessary. If the oat_file is null, it uses the oat file from
184 // the image.
Andreas Gampea463b6a2016-08-12 21:53:32 -0700185 static std::unique_ptr<ImageSpace> Init(const char* image_filename,
186 const char* image_location,
187 bool validate_oat_file,
188 const OatFile* oat_file,
189 std::string* error_msg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700190 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700191
Ian Rogersef7d42f2014-01-06 12:55:46 -0800192 static Atomic<uint32_t> bitmap_index_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700193
Ian Rogers700a4022014-05-19 16:49:03 -0700194 std::unique_ptr<accounting::ContinuousSpaceBitmap> live_bitmap_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700195
Jeff Haodcdc85b2015-12-04 14:06:18 -0800196 ImageSpace(const std::string& name,
197 const char* image_location,
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100198 MemMap&& mem_map,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100199 std::unique_ptr<accounting::ContinuousSpaceBitmap> live_bitmap,
Mathieu Chartier2d124ec2016-01-05 18:03:15 -0800200 uint8_t* end);
Ian Rogers1d54e732013-05-02 21:10:01 -0700201
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700202 // The OatFile associated with the image during early startup to
203 // reserve space contiguous to the image. It is later released to
204 // the ClassLinker during it's initialization.
Ian Rogers700a4022014-05-19 16:49:03 -0700205 std::unique_ptr<OatFile> oat_file_;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700206
Andreas Gampe88da3b02015-06-12 20:38:49 -0700207 // There are times when we need to find the boot image oat file. As
208 // we release ownership during startup, keep a non-owned reference.
209 const OatFile* oat_file_non_owned_;
210
Narayan Kamath52f84882014-05-02 10:10:39 +0100211 const std::string image_location_;
212
Andreas Gampea463b6a2016-08-12 21:53:32 -0700213 friend class Space;
214
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800215 private:
Vladimir Marko0ace5632018-12-14 11:11:47 +0000216 // Internal overload that takes ArrayRef<> instead of vector<>.
217 static std::vector<std::string> ExpandMultiImageLocations(
218 ArrayRef<const std::string> dex_locations,
219 const std::string& image_location);
220
Vladimir Marko82e1e272018-08-20 13:38:06 +0000221 class BootImageLoader;
Mathieu Chartier25602dc2018-12-11 11:31:57 -0800222 template <typename ReferenceVisitor>
223 class ClassTableVisitor;
Mathieu Chartierd3f037b2018-12-06 23:50:56 -0800224 class Loader;
225 template <typename PatchObjectVisitor>
226 class PatchArtFieldVisitor;
227 template <PointerSize kPointerSize, typename PatchObjectVisitor, typename PatchCodeVisitor>
228 class PatchArtMethodVisitor;
Mathieu Chartierf0a96eb2019-01-11 11:06:43 -0800229 template <PointerSize kPointerSize, typename HeapVisitor, typename NativeVisitor>
Mathieu Chartierd3f037b2018-12-06 23:50:56 -0800230 class PatchObjectVisitor;
Andreas Gampe2bd84282016-12-05 12:37:36 -0800231
Ian Rogers1d54e732013-05-02 21:10:01 -0700232 DISALLOW_COPY_AND_ASSIGN(ImageSpace);
233};
234
235} // namespace space
236} // namespace gc
237} // namespace art
238
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700239#endif // ART_RUNTIME_GC_SPACE_IMAGE_SPACE_H_