• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #ifndef ART_RUNTIME_GC_SPACE_IMAGE_SPACE_H_
18 #define ART_RUNTIME_GC_SPACE_IMAGE_SPACE_H_
19 
20 #include "gc/accounting/space_bitmap.h"
21 #include "image.h"
22 #include "image_space_loading_order.h"
23 #include "space.h"
24 
25 namespace art {
26 
27 template <typename T> class ArrayRef;
28 class DexFile;
29 enum class InstructionSet;
30 class OatFile;
31 
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:
GetType()38   SpaceType GetType() const override {
39     return kSpaceTypeImageSpace;
40   }
41 
42   // Load boot image spaces from a primary image file for a specified instruction set.
43   //
44   // On successful return, the loaded spaces are added to boot_image_spaces (which must be
45   // empty on entry) and `extra_reservation` is set to the requested reservation located
46   // after the end of the last loaded oat file.
47   static bool LoadBootImage(
48       const std::vector<std::string>& boot_class_path,
49       const std::vector<std::string>& boot_class_path_locations,
50       const std::string& image_location,
51       const InstructionSet image_isa,
52       ImageSpaceLoadingOrder order,
53       bool relocate,
54       bool executable,
55       bool is_zygote,
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_);
59 
60   // Try to open an existing app image space.
61   static std::unique_ptr<ImageSpace> CreateFromAppImage(const char* image,
62                                                         const OatFile* oat_file,
63                                                         std::string* error_msg)
64       REQUIRES_SHARED(Locks::mutator_lock_);
65 
66   // Reads the image header from the specified image location for the
67   // instruction set image_isa. Returns null on failure, with
68   // reason in error_msg.
69   static std::unique_ptr<ImageHeader> ReadImageHeader(const char* image_location,
70                                                       InstructionSet image_isa,
71                                                       ImageSpaceLoadingOrder order,
72                                                       std::string* error_msg);
73 
74   // Give access to the OatFile.
75   const OatFile* GetOatFile() const;
76 
77   // Releases the OatFile from the ImageSpace so it can be transfer to
78   // the caller, presumably the OatFileManager.
79   std::unique_ptr<const OatFile> ReleaseOatFile();
80 
81   void VerifyImageAllocations()
82       REQUIRES_SHARED(Locks::mutator_lock_);
83 
GetImageHeader()84   const ImageHeader& GetImageHeader() const {
85     return *reinterpret_cast<ImageHeader*>(Begin());
86   }
87 
88   // Actual filename where image was loaded from.
89   // For example: /data/dalvik-cache/arm/system@framework@boot.art
GetImageFilename()90   const std::string GetImageFilename() const {
91     return GetName();
92   }
93 
94   // Symbolic location for image.
95   // For example: /system/framework/boot.art
GetImageLocation()96   const std::string GetImageLocation() const {
97     return image_location_;
98   }
99 
GetLiveBitmap()100   accounting::ContinuousSpaceBitmap* GetLiveBitmap() const override {
101     return live_bitmap_.get();
102   }
103 
GetMarkBitmap()104   accounting::ContinuousSpaceBitmap* GetMarkBitmap() const override {
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 
110   void Dump(std::ostream& os) const override;
111 
112   // Sweeping image spaces is a NOP.
Sweep(bool,size_t *,size_t *)113   void Sweep(bool /* swap_bitmaps */, size_t* /* freed_objects */, size_t* /* freed_bytes */) {
114   }
115 
CanMoveObjects()116   bool CanMoveObjects() const override {
117     return false;
118   }
119 
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,
132                                 bool* has_data,
133                                 bool *is_global_cache);
134 
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.
138   static std::string GetBootClassPathChecksums(ArrayRef<const std::string> boot_class_path,
139                                                const std::string& image_location,
140                                                InstructionSet image_isa,
141                                                ImageSpaceLoadingOrder order,
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 
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);
153 
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 
165   // Return the end of the image which includes non-heap objects such as ArtMethods and ArtFields.
GetImageEnd()166   uint8_t* GetImageEnd() const {
167     return Begin() + GetImageHeader().GetImageSize();
168   }
169 
170   void DumpSections(std::ostream& os) const;
171 
172   // De-initialize the image-space by undoing the effects in Init().
173   virtual ~ImageSpace();
174 
175   void DisablePreResolvedStrings() REQUIRES_SHARED(Locks::mutator_lock_);
176   void ReleaseMetadata() REQUIRES_SHARED(Locks::mutator_lock_);
177 
178  protected:
179   // Tries to initialize an ImageSpace from the given image path, returning null on error.
180   //
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.
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)
190       REQUIRES_SHARED(Locks::mutator_lock_);
191 
192   static Atomic<uint32_t> bitmap_index_;
193 
194   std::unique_ptr<accounting::ContinuousSpaceBitmap> live_bitmap_;
195 
196   ImageSpace(const std::string& name,
197              const char* image_location,
198              MemMap&& mem_map,
199              std::unique_ptr<accounting::ContinuousSpaceBitmap> live_bitmap,
200              uint8_t* end);
201 
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.
205   std::unique_ptr<OatFile> oat_file_;
206 
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 
211   const std::string image_location_;
212 
213   friend class Space;
214 
215  private:
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 
221   class BootImageLoader;
222   template <typename ReferenceVisitor>
223   class ClassTableVisitor;
224   class Loader;
225   template <typename PatchObjectVisitor>
226   class PatchArtFieldVisitor;
227   template <PointerSize kPointerSize, typename PatchObjectVisitor, typename PatchCodeVisitor>
228   class PatchArtMethodVisitor;
229   template <PointerSize kPointerSize, typename HeapVisitor, typename NativeVisitor>
230   class PatchObjectVisitor;
231 
232   DISALLOW_COPY_AND_ASSIGN(ImageSpace);
233 };
234 
235 }  // namespace space
236 }  // namespace gc
237 }  // namespace art
238 
239 #endif  // ART_RUNTIME_GC_SPACE_IMAGE_SPACE_H_
240