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_OAT_FILE_H_
18 #define ART_RUNTIME_OAT_FILE_H_
19 
20 #include <list>
21 #include <string>
22 #include <string_view>
23 #include <vector>
24 
25 #include "base/array_ref.h"
26 #include "base/mutex.h"
27 #include "base/os.h"
28 #include "base/safe_map.h"
29 #include "base/tracking_safe_map.h"
30 #include "class_status.h"
31 #include "compiler_filter.h"
32 #include "dex/dex_file_layout.h"
33 #include "dex/type_lookup_table.h"
34 #include "dex/utf.h"
35 #include "index_bss_mapping.h"
36 #include "mirror/object.h"
37 
38 namespace art {
39 
40 class BitVector;
41 class DexFile;
42 class ElfFile;
43 class DexLayoutSections;
44 template <class MirrorType> class GcRoot;
45 class MemMap;
46 class OatDexFile;
47 class OatHeader;
48 class OatMethodOffsets;
49 class OatQuickMethodHeader;
50 class VdexFile;
51 
52 namespace dex {
53 struct ClassDef;
54 }  // namespace dex
55 
56 namespace gc {
57 namespace collector {
58 class FakeOatFile;
59 }  // namespace collector
60 }  // namespace gc
61 
62 // OatMethodOffsets are currently 5x32-bits=160-bits long, so if we can
63 // save even one OatMethodOffsets struct, the more complicated encoding
64 // using a bitmap pays for itself since few classes will have 160
65 // methods.
66 enum OatClassType {
67   kOatClassAllCompiled = 0,   // OatClass is followed by an OatMethodOffsets for each method.
68   kOatClassSomeCompiled = 1,  // A bitmap of OatMethodOffsets that are present follows the OatClass.
69   kOatClassNoneCompiled = 2,  // All methods are interpreted so no OatMethodOffsets are necessary.
70   kOatClassMax = 3,
71 };
72 
73 std::ostream& operator<<(std::ostream& os, OatClassType rhs);
74 
75 class PACKED(4) OatMethodOffsets {
76  public:
code_offset_(code_offset)77   explicit OatMethodOffsets(uint32_t code_offset = 0) : code_offset_(code_offset) {}
78 
~OatMethodOffsets()79   ~OatMethodOffsets() {}
80 
81   OatMethodOffsets(const OatMethodOffsets&) = default;
82   OatMethodOffsets& operator=(const OatMethodOffsets&) = default;
83 
84   uint32_t code_offset_;
85 };
86 
87 // Runtime representation of the OAT file format which holds compiler output.
88 // The class opens an OAT file from storage and maps it to memory, typically with
89 // dlopen and provides access to its internal data structures (see OatWriter for
90 // for more details about the OAT format).
91 // In the process of loading OAT, the class also loads the associated VDEX file
92 // with the input DEX files (see VdexFile for details about the VDEX format).
93 // The raw DEX data are accessible transparently through the OatDexFile objects.
94 
95 class OatFile {
96  public:
97   // Special classpath that skips shared library check.
98   static constexpr const char* kSpecialSharedLibrary = "&";
99 
100   // Open an oat file. Returns null on failure.
101   // The `dex_filenames` argument, if provided, overrides the dex locations
102   // from oat file when opening the dex files if they are not embedded in the
103   // vdex file. These may differ for cross-compilation (the dex file name is
104   // the host path and dex location is the future path on target) and testing.
105   static OatFile* Open(int zip_fd,
106                        const std::string& filename,
107                        const std::string& location,
108                        bool executable,
109                        bool low_4gb,
110                        ArrayRef<const std::string> dex_filenames,
111                        /*inout*/MemMap* reservation,  // Where to load if not null.
112                        /*out*/std::string* error_msg);
113   // Helper overload that takes a single dex filename and no reservation.
Open(int zip_fd,const std::string & filename,const std::string & location,bool executable,bool low_4gb,const std::string & dex_filename,std::string * error_msg)114   static OatFile* Open(int zip_fd,
115                        const std::string& filename,
116                        const std::string& location,
117                        bool executable,
118                        bool low_4gb,
119                        const std::string& dex_filename,
120                        /*out*/std::string* error_msg) {
121     return Open(zip_fd,
122                 filename,
123                 location,
124                 executable,
125                 low_4gb,
126                 ArrayRef<const std::string>(&dex_filename, /*size=*/ 1u),
127                 /*reservation=*/ nullptr,
128                 error_msg);
129   }
130   // Helper overload that takes no dex filename and no reservation.
Open(int zip_fd,const std::string & filename,const std::string & location,bool executable,bool low_4gb,std::string * error_msg)131   static OatFile* Open(int zip_fd,
132                        const std::string& filename,
133                        const std::string& location,
134                        bool executable,
135                        bool low_4gb,
136                        /*out*/std::string* error_msg) {
137     return Open(zip_fd,
138                 filename,
139                 location,
140                 executable,
141                 low_4gb,
142                 ArrayRef<const std::string>(),
143                 /*reservation=*/ nullptr,
144                 error_msg);
145   }
146 
147   // Similar to OatFile::Open(const std::string...), but accepts input vdex and
148   // odex files as file descriptors. We also take zip_fd in case the vdex does not
149   // contain the dex code, and we need to read it from the zip file.
150   static OatFile* Open(int zip_fd,
151                        int vdex_fd,
152                        int oat_fd,
153                        const std::string& oat_location,
154                        bool executable,
155                        bool low_4gb,
156                        ArrayRef<const std::string> dex_filenames,
157                        /*inout*/MemMap* reservation,  // Where to load if not null.
158                        /*out*/std::string* error_msg);
159 
160   // Initialize OatFile instance from an already loaded VdexFile. This assumes
161   // the vdex does not have a dex section and accepts a vector of DexFiles separately.
162   static OatFile* OpenFromVdex(const std::vector<const DexFile*>& dex_files,
163                                std::unique_ptr<VdexFile>&& vdex_file,
164                                const std::string& location);
165 
166   virtual ~OatFile();
167 
IsExecutable()168   bool IsExecutable() const {
169     return is_executable_;
170   }
171 
172   // Indicates whether the oat file was compiled with full debugging capability.
173   bool IsDebuggable() const;
174 
175   CompilerFilter::Filter GetCompilerFilter() const;
176 
177   std::string GetClassLoaderContext() const;
178 
179   const char* GetCompilationReason() const;
180 
GetLocation()181   const std::string& GetLocation() const {
182     return location_;
183   }
184 
185   const OatHeader& GetOatHeader() const;
186 
187   class OatMethod final {
188    public:
189     uint32_t GetCodeOffset() const;
190 
191     const void* GetQuickCode() const;
192 
193     // Returns size of quick code.
194     uint32_t GetQuickCodeSize() const;
195     uint32_t GetQuickCodeSizeOffset() const;
196 
197     // Returns OatQuickMethodHeader for debugging. Most callers should
198     // use more specific methods such as GetQuickCodeSize.
199     const OatQuickMethodHeader* GetOatQuickMethodHeader() const;
200     uint32_t GetOatQuickMethodHeaderOffset() const;
201 
202     size_t GetFrameSizeInBytes() const;
203     uint32_t GetCoreSpillMask() const;
204     uint32_t GetFpSpillMask() const;
205 
206     const uint8_t* GetVmapTable() const;
207     uint32_t GetVmapTableOffset() const;
208     uint32_t GetVmapTableOffsetOffset() const;
209 
210     // Create an OatMethod with offsets relative to the given base address
OatMethod(const uint8_t * base,const uint32_t code_offset)211     OatMethod(const uint8_t* base, const uint32_t code_offset)
212         : begin_(base), code_offset_(code_offset) {
213     }
214     OatMethod(const OatMethod&) = default;
~OatMethod()215     ~OatMethod() {}
216 
217     OatMethod& operator=(const OatMethod&) = default;
218 
219     // A representation of an invalid OatMethod, used when an OatMethod or OatClass can't be found.
220     // See ClassLinker::FindOatMethodFor.
Invalid()221     static const OatMethod Invalid() {
222       return OatMethod(nullptr, -1);
223     }
224 
225    private:
226     template<class T>
GetOatPointer(uint32_t offset)227     T GetOatPointer(uint32_t offset) const {
228       if (offset == 0) {
229         return nullptr;
230       }
231       return reinterpret_cast<T>(begin_ + offset);
232     }
233 
234     const uint8_t* begin_;
235     uint32_t code_offset_;
236 
237     friend class OatClass;
238   };
239 
240   class OatClass final {
241    public:
GetStatus()242     ClassStatus GetStatus() const {
243       return status_;
244     }
245 
GetType()246     OatClassType GetType() const {
247       return type_;
248     }
249 
250     // Get the OatMethod entry based on its index into the class
251     // defintion. Direct methods come first, followed by virtual
252     // methods. Note that runtime created methods such as miranda
253     // methods are not included.
254     const OatMethod GetOatMethod(uint32_t method_index) const;
255 
256     // Return a pointer to the OatMethodOffsets for the requested
257     // method_index, or null if none is present. Note that most
258     // callers should use GetOatMethod.
259     const OatMethodOffsets* GetOatMethodOffsets(uint32_t method_index) const;
260 
261     // Return the offset from the start of the OatFile to the
262     // OatMethodOffsets for the requested method_index, or 0 if none
263     // is present. Note that most callers should use GetOatMethod.
264     uint32_t GetOatMethodOffsetsOffset(uint32_t method_index) const;
265 
266     // A representation of an invalid OatClass, used when an OatClass can't be found.
267     // See FindOatClass().
Invalid()268     static OatClass Invalid() {
269       return OatClass(/* oat_file= */ nullptr,
270                       ClassStatus::kErrorUnresolved,
271                       kOatClassNoneCompiled,
272                       /* bitmap_size= */ 0,
273                       /* bitmap_pointer= */ nullptr,
274                       /* methods_pointer= */ nullptr);
275     }
276 
277    private:
278     OatClass(const OatFile* oat_file,
279              ClassStatus status,
280              OatClassType type,
281              uint32_t bitmap_size,
282              const uint32_t* bitmap_pointer,
283              const OatMethodOffsets* methods_pointer);
284 
285     const OatFile* const oat_file_;
286 
287     const ClassStatus status_;
288 
289     const OatClassType type_;
290 
291     const uint32_t* const bitmap_;
292 
293     const OatMethodOffsets* const methods_pointer_;
294 
295     friend class art::OatDexFile;
296   };
297 
298   // Get the OatDexFile for the given dex_location within this oat file.
299   // If dex_location_checksum is non-null, the OatDexFile will only be
300   // returned if it has a matching checksum.
301   // If error_msg is non-null and no OatDexFile is returned, error_msg will
302   // be updated with a description of why no OatDexFile was returned.
303   const OatDexFile* GetOatDexFile(const char* dex_location,
304                                   const uint32_t* const dex_location_checksum,
305                                   /*out*/std::string* error_msg = nullptr) const
306       REQUIRES(!secondary_lookup_lock_);
307 
GetOatDexFiles()308   const std::vector<const OatDexFile*>& GetOatDexFiles() const {
309     return oat_dex_files_storage_;
310   }
311 
Size()312   size_t Size() const {
313     return End() - Begin();
314   }
315 
Contains(const void * p)316   bool Contains(const void* p) const {
317     return p >= Begin() && p < End();
318   }
319 
DataBimgRelRoSize()320   size_t DataBimgRelRoSize() const {
321     return DataBimgRelRoEnd() - DataBimgRelRoBegin();
322   }
323 
BssSize()324   size_t BssSize() const {
325     return BssEnd() - BssBegin();
326   }
327 
VdexSize()328   size_t VdexSize() const {
329     return VdexEnd() - VdexBegin();
330   }
331 
BssMethodsOffset()332   size_t BssMethodsOffset() const {
333     // Note: This is used only for symbolizer and needs to return a valid .bss offset.
334     return (bss_methods_ != nullptr) ? bss_methods_ - BssBegin() : BssRootsOffset();
335   }
336 
BssRootsOffset()337   size_t BssRootsOffset() const {
338     // Note: This is used only for symbolizer and needs to return a valid .bss offset.
339     return (bss_roots_ != nullptr) ? bss_roots_ - BssBegin() : BssSize();
340   }
341 
DexSize()342   size_t DexSize() const {
343     return DexEnd() - DexBegin();
344   }
345 
346   const uint8_t* Begin() const;
347   const uint8_t* End() const;
348 
DataBimgRelRoBegin()349   const uint8_t* DataBimgRelRoBegin() const { return data_bimg_rel_ro_begin_; }
DataBimgRelRoEnd()350   const uint8_t* DataBimgRelRoEnd() const { return data_bimg_rel_ro_end_; }
351 
BssBegin()352   const uint8_t* BssBegin() const { return bss_begin_; }
BssEnd()353   const uint8_t* BssEnd() const { return bss_end_; }
354 
VdexBegin()355   const uint8_t* VdexBegin() const { return vdex_begin_; }
VdexEnd()356   const uint8_t* VdexEnd() const { return vdex_end_; }
357 
358   const uint8_t* DexBegin() const;
359   const uint8_t* DexEnd() const;
360 
361   ArrayRef<const uint32_t> GetBootImageRelocations() const;
362   ArrayRef<ArtMethod*> GetBssMethods() const;
363   ArrayRef<GcRoot<mirror::Object>> GetBssGcRoots() const;
364 
365   // Initialize relocation sections (.data.bimg.rel.ro and .bss).
366   void InitializeRelocations() const;
367 
368   // Finds the associated oat class for a dex_file and descriptor. Returns an invalid OatClass on
369   // error and sets found to false.
370   static OatClass FindOatClass(const DexFile& dex_file, uint16_t class_def_idx, bool* found);
371 
GetVdexFile()372   VdexFile* GetVdexFile() const {
373     return vdex_.get();
374   }
375 
376   // Whether the OatFile embeds the Dex code.
ContainsDexCode()377   bool ContainsDexCode() const {
378     return external_dex_files_.empty();
379   }
380 
381   // Returns whether an image (e.g. app image) is required to safely execute this OAT file.
382   bool RequiresImage() const;
383 
384  protected:
385   OatFile(const std::string& filename, bool executable);
386 
387  private:
388   // The oat file name.
389   //
390   // The image will embed this to link its associated oat file.
391   const std::string location_;
392 
393   // Pointer to the Vdex file with the Dex files for this Oat file.
394   std::unique_ptr<VdexFile> vdex_;
395 
396   // Pointer to OatHeader.
397   const uint8_t* begin_;
398 
399   // Pointer to end of oat region for bounds checking.
400   const uint8_t* end_;
401 
402   // Pointer to the .data.bimg.rel.ro section, if present, otherwise null.
403   const uint8_t* data_bimg_rel_ro_begin_;
404 
405   // Pointer to the end of the .data.bimg.rel.ro section, if present, otherwise null.
406   const uint8_t* data_bimg_rel_ro_end_;
407 
408   // Pointer to the .bss section, if present, otherwise null.
409   uint8_t* bss_begin_;
410 
411   // Pointer to the end of the .bss section, if present, otherwise null.
412   uint8_t* bss_end_;
413 
414   // Pointer to the beginning of the ArtMethod*s in .bss section, if present, otherwise null.
415   uint8_t* bss_methods_;
416 
417   // Pointer to the beginning of the GC roots in .bss section, if present, otherwise null.
418   uint8_t* bss_roots_;
419 
420   // Was this oat_file loaded executable?
421   const bool is_executable_;
422 
423   // Pointer to the .vdex section, if present, otherwise null.
424   uint8_t* vdex_begin_;
425 
426   // Pointer to the end of the .vdex section, if present, otherwise null.
427   uint8_t* vdex_end_;
428 
429   // Owning storage for the OatDexFile objects.
430   std::vector<const OatDexFile*> oat_dex_files_storage_;
431 
432   // NOTE: We use a std::string_view as the key type to avoid a memory allocation on every
433   // lookup with a const char* key. The std::string_view doesn't own its backing storage,
434   // therefore we're using the OatFile's stored dex location as the backing storage
435   // for keys in oat_dex_files_ and the string_cache_ entries for the backing storage
436   // of keys in secondary_oat_dex_files_ and oat_dex_files_by_canonical_location_.
437   using Table =
438       AllocationTrackingSafeMap<std::string_view, const OatDexFile*, kAllocatorTagOatFile>;
439 
440   // Map each location and canonical location (if different) retrieved from the
441   // oat file to its OatDexFile. This map doesn't change after it's constructed in Setup()
442   // and therefore doesn't need any locking and provides the cheapest dex file lookup
443   // for GetOatDexFile() for a very frequent use case. Never contains a null value.
444   Table oat_dex_files_;
445 
446   // Lock guarding all members needed for secondary lookup in GetOatDexFile().
447   mutable Mutex secondary_lookup_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
448 
449   // If the primary oat_dex_files_ lookup fails, use a secondary map. This map stores
450   // the results of all previous secondary lookups, whether successful (non-null) or
451   // failed (null). If it doesn't contain an entry we need to calculate the canonical
452   // location and use oat_dex_files_by_canonical_location_.
453   mutable Table secondary_oat_dex_files_ GUARDED_BY(secondary_lookup_lock_);
454 
455   // Cache of strings. Contains the backing storage for keys in the secondary_oat_dex_files_
456   // and the lazily initialized oat_dex_files_by_canonical_location_.
457   // NOTE: We're keeping references to contained strings in form of std::string_view and adding
458   // new strings to the end. The adding of a new element must not touch any previously stored
459   // elements. std::list<> and std::deque<> satisfy this requirement, std::vector<> doesn't.
460   mutable std::list<std::string> string_cache_ GUARDED_BY(secondary_lookup_lock_);
461 
462   // Dex files opened directly from a file referenced from the oat file or specifed
463   // by the `dex_filenames` parameter, in case the OatFile does not embed the dex code.
464   std::vector<std::unique_ptr<const DexFile>> external_dex_files_;
465 
466   friend class gc::collector::FakeOatFile;  // For modifying begin_ and end_.
467   friend class OatClass;
468   friend class art::OatDexFile;
469   friend class OatDumper;  // For GetBase and GetLimit
470   friend class OatFileBase;
471   DISALLOW_COPY_AND_ASSIGN(OatFile);
472 };
473 
474 // OatDexFile should be an inner class of OatFile. Unfortunately, C++ doesn't
475 // support forward declarations of inner classes, and we want to
476 // forward-declare OatDexFile so that we can store an opaque pointer to an
477 // OatDexFile in DexFile.
478 class OatDexFile final {
479  public:
480   // Opens the DexFile referred to by this OatDexFile from within the containing OatFile.
481   std::unique_ptr<const DexFile> OpenDexFile(std::string* error_msg) const;
482 
483   // May return null if the OatDexFile only contains a type lookup table. This case only happens
484   // for the compiler to speed up compilation, or in jitzygote.
GetOatFile()485   const OatFile* GetOatFile() const {
486     return oat_file_;
487   }
488 
489   // Returns the size of the DexFile refered to by this OatDexFile.
490   size_t FileSize() const;
491 
492   // Returns original path of DexFile that was the source of this OatDexFile.
GetDexFileLocation()493   const std::string& GetDexFileLocation() const {
494     return dex_file_location_;
495   }
496 
497   // Returns the canonical location of DexFile that was the source of this OatDexFile.
GetCanonicalDexFileLocation()498   const std::string& GetCanonicalDexFileLocation() const {
499     return canonical_dex_file_location_;
500   }
501 
502   // Returns checksum of original DexFile that was the source of this OatDexFile;
GetDexFileLocationChecksum()503   uint32_t GetDexFileLocationChecksum() const {
504     return dex_file_location_checksum_;
505   }
506 
507   // Returns the OatClass for the class specified by the given DexFile class_def_index.
508   OatFile::OatClass GetOatClass(uint16_t class_def_index) const;
509 
510   // Returns the offset to the OatClass information. Most callers should use GetOatClass.
511   uint32_t GetOatClassOffset(uint16_t class_def_index) const;
512 
GetLookupTableData()513   const uint8_t* GetLookupTableData() const {
514     return lookup_table_data_;
515   }
516 
GetMethodBssMapping()517   const IndexBssMapping* GetMethodBssMapping() const {
518     return method_bss_mapping_;
519   }
520 
GetTypeBssMapping()521   const IndexBssMapping* GetTypeBssMapping() const {
522     return type_bss_mapping_;
523   }
524 
GetStringBssMapping()525   const IndexBssMapping* GetStringBssMapping() const {
526     return string_bss_mapping_;
527   }
528 
GetDexFilePointer()529   const uint8_t* GetDexFilePointer() const {
530     return dex_file_pointer_;
531   }
532 
533   ArrayRef<const uint8_t> GetQuickenedInfoOf(const DexFile& dex_file,
534                                              uint32_t dex_method_idx) const;
535 
536   // Looks up a class definition by its class descriptor. Hash must be
537   // ComputeModifiedUtf8Hash(descriptor).
538   static const dex::ClassDef* FindClassDef(const DexFile& dex_file,
539                                            const char* descriptor,
540                                            size_t hash);
541 
542   // Madvise the dex file based on the state we are moving to.
543   static void MadviseDexFile(const DexFile& dex_file, MadviseState state);
544 
GetTypeLookupTable()545   const TypeLookupTable& GetTypeLookupTable() const {
546     return lookup_table_;
547   }
548 
549   ~OatDexFile();
550 
551   // Create only with a type lookup table, used by the compiler to speed up compilation.
552   explicit OatDexFile(TypeLookupTable&& lookup_table);
553 
554   // Return the dex layout sections.
GetDexLayoutSections()555   const DexLayoutSections* GetDexLayoutSections() const {
556     return dex_layout_sections_;
557   }
558 
559  private:
560   OatDexFile(const OatFile* oat_file,
561              const std::string& dex_file_location,
562              const std::string& canonical_dex_file_location,
563              uint32_t dex_file_checksum,
564              const uint8_t* dex_file_pointer,
565              const uint8_t* lookup_table_data,
566              const IndexBssMapping* method_bss_mapping,
567              const IndexBssMapping* type_bss_mapping,
568              const IndexBssMapping* string_bss_mapping,
569              const uint32_t* oat_class_offsets_pointer,
570              const DexLayoutSections* dex_layout_sections);
571 
572   // Create an OatDexFile wrapping an existing DexFile. Will set the OatDexFile
573   // pointer in the DexFile.
574   OatDexFile(const OatFile* oat_file,
575              const DexFile* dex_file,
576              const std::string& dex_file_location,
577              const std::string& canonical_dex_file_location);
578 
579   bool IsBackedByVdexOnly() const;
580 
581   static void AssertAotCompiler();
582 
583   const OatFile* const oat_file_ = nullptr;
584   const std::string dex_file_location_;
585   const std::string canonical_dex_file_location_;
586   const uint32_t dex_file_location_checksum_ = 0u;
587   const uint8_t* const dex_file_pointer_ = nullptr;
588   const uint8_t* const lookup_table_data_ = nullptr;
589   const IndexBssMapping* const method_bss_mapping_ = nullptr;
590   const IndexBssMapping* const type_bss_mapping_ = nullptr;
591   const IndexBssMapping* const string_bss_mapping_ = nullptr;
592   const uint32_t* const oat_class_offsets_pointer_ = nullptr;
593   TypeLookupTable lookup_table_;
594   const DexLayoutSections* const dex_layout_sections_ = nullptr;
595 
596   friend class OatFile;
597   friend class OatFileBase;
598   DISALLOW_COPY_AND_ASSIGN(OatDexFile);
599 };
600 
601 }  // namespace art
602 
603 #endif  // ART_RUNTIME_OAT_FILE_H_
604