Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_GC_SPACE_IMAGE_SPACE_H_ |
| 18 | #define ART_RUNTIME_GC_SPACE_IMAGE_SPACE_H_ |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 19 | |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 20 | #include "gc/accounting/space_bitmap.h" |
Andreas Gampe | d490129 | 2017-05-30 18:41:34 -0700 | [diff] [blame] | 21 | #include "image.h" |
Andreas Gampe | 8682354 | 2019-02-25 09:38:49 -0800 | [diff] [blame] | 22 | #include "image_space_loading_order.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 23 | #include "space.h" |
| 24 | |
| 25 | namespace art { |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 26 | |
Vladimir Marko | 0ace563 | 2018-12-14 11:11:47 +0000 | [diff] [blame] | 27 | template <typename T> class ArrayRef; |
| 28 | class DexFile; |
Andreas Gampe | 639b2b1 | 2019-01-08 10:32:50 -0800 | [diff] [blame] | 29 | enum class InstructionSet; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 30 | class OatFile; |
| 31 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 32 | namespace gc { |
| 33 | namespace space { |
| 34 | |
| 35 | // An image space is a space backed with a memory mapped image. |
| 36 | class ImageSpace : public MemMapSpace { |
| 37 | public: |
Yi Kong | 3940254 | 2019-03-24 02:47:16 -0700 | [diff] [blame^] | 38 | SpaceType GetType() const override { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 39 | return kSpaceTypeImageSpace; |
| 40 | } |
| 41 | |
Andreas Gampe | 2bd8428 | 2016-12-05 12:37:36 -0800 | [diff] [blame] | 42 | // Load boot image spaces from a primary image file for a specified instruction set. |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 43 | // |
Andreas Gampe | 2bd8428 | 2016-12-05 12:37:36 -0800 | [diff] [blame] | 44 | // On successful return, the loaded spaces are added to boot_image_spaces (which must be |
Vladimir Marko | 91f1032 | 2018-12-07 18:04:10 +0000 | [diff] [blame] | 45 | // empty on entry) and `extra_reservation` is set to the requested reservation located |
| 46 | // after the end of the last loaded oat file. |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 47 | static bool LoadBootImage( |
Vladimir Marko | 91f1032 | 2018-12-07 18:04:10 +0000 | [diff] [blame] | 48 | const std::vector<std::string>& boot_class_path, |
| 49 | const std::vector<std::string>& boot_class_path_locations, |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 50 | const std::string& image_location, |
| 51 | const InstructionSet image_isa, |
Andreas Gampe | 8682354 | 2019-02-25 09:38:49 -0800 | [diff] [blame] | 52 | ImageSpaceLoadingOrder order, |
Vladimir Marko | 3364d18 | 2019-03-13 13:55:01 +0000 | [diff] [blame] | 53 | bool relocate, |
| 54 | bool executable, |
| 55 | bool is_zygote, |
Vladimir Marko | d44d703 | 2018-08-30 13:02:31 +0100 | [diff] [blame] | 56 | 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 Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 59 | |
| 60 | // Try to open an existing app image space. |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 61 | static std::unique_ptr<ImageSpace> CreateFromAppImage(const char* image, |
| 62 | const OatFile* oat_file, |
| 63 | std::string* error_msg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 64 | REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 65 | |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 66 | // Reads the image header from the specified image location for the |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 67 | // instruction set image_isa. Returns null on failure, with |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 68 | // reason in error_msg. |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 69 | static std::unique_ptr<ImageHeader> ReadImageHeader(const char* image_location, |
| 70 | InstructionSet image_isa, |
Andreas Gampe | 8682354 | 2019-02-25 09:38:49 -0800 | [diff] [blame] | 71 | ImageSpaceLoadingOrder order, |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 72 | std::string* error_msg); |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 73 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 74 | // Give access to the OatFile. |
| 75 | const OatFile* GetOatFile() const; |
| 76 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 77 | // Releases the OatFile from the ImageSpace so it can be transfer to |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 78 | // the caller, presumably the OatFileManager. |
| 79 | std::unique_ptr<const OatFile> ReleaseOatFile(); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 80 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 81 | void VerifyImageAllocations() |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 82 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 83 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 84 | const ImageHeader& GetImageHeader() const { |
| 85 | return *reinterpret_cast<ImageHeader*>(Begin()); |
| 86 | } |
| 87 | |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 88 | // Actual filename where image was loaded from. |
| 89 | // For example: /data/dalvik-cache/arm/system@framework@boot.art |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 90 | const std::string GetImageFilename() const { |
| 91 | return GetName(); |
| 92 | } |
| 93 | |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 94 | // Symbolic location for image. |
| 95 | // For example: /system/framework/boot.art |
| 96 | const std::string GetImageLocation() const { |
| 97 | return image_location_; |
| 98 | } |
| 99 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 100 | accounting::ContinuousSpaceBitmap* GetLiveBitmap() const override { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 101 | return live_bitmap_.get(); |
| 102 | } |
| 103 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 104 | accounting::ContinuousSpaceBitmap* GetMarkBitmap() const override { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 105 | // 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 Kong | 3940254 | 2019-03-24 02:47:16 -0700 | [diff] [blame^] | 110 | void Dump(std::ostream& os) const override; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 111 | |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 112 | // Sweeping image spaces is a NOP. |
| 113 | void Sweep(bool /* swap_bitmaps */, size_t* /* freed_objects */, size_t* /* freed_bytes */) { |
| 114 | } |
| 115 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 116 | bool CanMoveObjects() const override { |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 117 | return false; |
| 118 | } |
| 119 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 120 | // 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 Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 132 | bool* has_data, |
| 133 | bool *is_global_cache); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 134 | |
Vladimir Marko | 0ace563 | 2018-12-14 11:11:47 +0000 | [diff] [blame] | 135 | // 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 Marko | bcd99be | 2019-03-22 16:21:31 +0000 | [diff] [blame] | 138 | static std::string GetBootClassPathChecksums(ArrayRef<const std::string> boot_class_path, |
Vladimir Marko | 0ace563 | 2018-12-14 11:11:47 +0000 | [diff] [blame] | 139 | const std::string& image_location, |
| 140 | InstructionSet image_isa, |
Andreas Gampe | 8682354 | 2019-02-25 09:38:49 -0800 | [diff] [blame] | 141 | ImageSpaceLoadingOrder order, |
Vladimir Marko | 0ace563 | 2018-12-14 11:11:47 +0000 | [diff] [blame] | 142 | /*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 Marko | 91f1032 | 2018-12-07 18:04:10 +0000 | [diff] [blame] | 149 | // 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 Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 153 | |
Richard Uhler | 84f50ae | 2017-02-06 15:12:45 +0000 | [diff] [blame] | 154 | // 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 Chartier | 763a31e | 2015-11-16 16:05:55 -0800 | [diff] [blame] | 165 | // 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 Chartier | d5f3f32 | 2016-03-21 14:05:56 -0700 | [diff] [blame] | 170 | void DumpSections(std::ostream& os) const; |
| 171 | |
Igor Murashkin | 8275fba | 2017-05-02 15:58:02 -0700 | [diff] [blame] | 172 | // De-initialize the image-space by undoing the effects in Init(). |
| 173 | virtual ~ImageSpace(); |
| 174 | |
Mathieu Chartier | 6e7a72c | 2019-03-07 21:40:10 -0800 | [diff] [blame] | 175 | void DisablePreResolvedStrings() REQUIRES_SHARED(Locks::mutator_lock_); |
| 176 | void ReleaseMetadata() REQUIRES_SHARED(Locks::mutator_lock_); |
| 177 | |
Mathieu Chartier | 763a31e | 2015-11-16 16:05:55 -0800 | [diff] [blame] | 178 | protected: |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 179 | // Tries to initialize an ImageSpace from the given image path, returning null on error. |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 180 | // |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 181 | // 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 Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 185 | 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 Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 190 | REQUIRES_SHARED(Locks::mutator_lock_); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 191 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 192 | static Atomic<uint32_t> bitmap_index_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 193 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 194 | std::unique_ptr<accounting::ContinuousSpaceBitmap> live_bitmap_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 195 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 196 | ImageSpace(const std::string& name, |
| 197 | const char* image_location, |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 198 | MemMap&& mem_map, |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 199 | std::unique_ptr<accounting::ContinuousSpaceBitmap> live_bitmap, |
Mathieu Chartier | 2d124ec | 2016-01-05 18:03:15 -0800 | [diff] [blame] | 200 | uint8_t* end); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 201 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 202 | // 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 Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 205 | std::unique_ptr<OatFile> oat_file_; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 206 | |
Andreas Gampe | 88da3b0 | 2015-06-12 20:38:49 -0700 | [diff] [blame] | 207 | // 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 Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 211 | const std::string image_location_; |
| 212 | |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 213 | friend class Space; |
| 214 | |
Mathieu Chartier | 763a31e | 2015-11-16 16:05:55 -0800 | [diff] [blame] | 215 | private: |
Vladimir Marko | 0ace563 | 2018-12-14 11:11:47 +0000 | [diff] [blame] | 216 | // 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 Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 221 | class BootImageLoader; |
Mathieu Chartier | 25602dc | 2018-12-11 11:31:57 -0800 | [diff] [blame] | 222 | template <typename ReferenceVisitor> |
| 223 | class ClassTableVisitor; |
Mathieu Chartier | d3f037b | 2018-12-06 23:50:56 -0800 | [diff] [blame] | 224 | class Loader; |
| 225 | template <typename PatchObjectVisitor> |
| 226 | class PatchArtFieldVisitor; |
| 227 | template <PointerSize kPointerSize, typename PatchObjectVisitor, typename PatchCodeVisitor> |
| 228 | class PatchArtMethodVisitor; |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 229 | template <PointerSize kPointerSize, typename HeapVisitor, typename NativeVisitor> |
Mathieu Chartier | d3f037b | 2018-12-06 23:50:56 -0800 | [diff] [blame] | 230 | class PatchObjectVisitor; |
Andreas Gampe | 2bd8428 | 2016-12-05 12:37:36 -0800 | [diff] [blame] | 231 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 232 | DISALLOW_COPY_AND_ASSIGN(ImageSpace); |
| 233 | }; |
| 234 | |
| 235 | } // namespace space |
| 236 | } // namespace gc |
| 237 | } // namespace art |
| 238 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 239 | #endif // ART_RUNTIME_GC_SPACE_IMAGE_SPACE_H_ |