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 DummyOatFile;
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, const 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  protected:
382   OatFile(const std::string& filename, bool executable);
383 
384  private:
385   // The oat file name.
386   //
387   // The image will embed this to link its associated oat file.
388   const std::string location_;
389 
390   // Pointer to the Vdex file with the Dex files for this Oat file.
391   std::unique_ptr<VdexFile> vdex_;
392 
393   // Pointer to OatHeader.
394   const uint8_t* begin_;
395 
396   // Pointer to end of oat region for bounds checking.
397   const uint8_t* end_;
398 
399   // Pointer to the .data.bimg.rel.ro section, if present, otherwise null.
400   const uint8_t* data_bimg_rel_ro_begin_;
401 
402   // Pointer to the end of the .data.bimg.rel.ro section, if present, otherwise null.
403   const uint8_t* data_bimg_rel_ro_end_;
404 
405   // Pointer to the .bss section, if present, otherwise null.
406   uint8_t* bss_begin_;
407 
408   // Pointer to the end of the .bss section, if present, otherwise null.
409   uint8_t* bss_end_;
410 
411   // Pointer to the beginning of the ArtMethod*s in .bss section, if present, otherwise null.
412   uint8_t* bss_methods_;
413 
414   // Pointer to the beginning of the GC roots in .bss section, if present, otherwise null.
415   uint8_t* bss_roots_;
416 
417   // Was this oat_file loaded executable?
418   const bool is_executable_;
419 
420   // Pointer to the .vdex section, if present, otherwise null.
421   uint8_t* vdex_begin_;
422 
423   // Pointer to the end of the .vdex section, if present, otherwise null.
424   uint8_t* vdex_end_;
425 
426   // Owning storage for the OatDexFile objects.
427   std::vector<const OatDexFile*> oat_dex_files_storage_;
428 
429   // NOTE: We use a std::string_view as the key type to avoid a memory allocation on every
430   // lookup with a const char* key. The std::string_view doesn't own its backing storage,
431   // therefore we're using the OatFile's stored dex location as the backing storage
432   // for keys in oat_dex_files_ and the string_cache_ entries for the backing storage
433   // of keys in secondary_oat_dex_files_ and oat_dex_files_by_canonical_location_.
434   using Table =
435       AllocationTrackingSafeMap<std::string_view, const OatDexFile*, kAllocatorTagOatFile>;
436 
437   // Map each location and canonical location (if different) retrieved from the
438   // oat file to its OatDexFile. This map doesn't change after it's constructed in Setup()
439   // and therefore doesn't need any locking and provides the cheapest dex file lookup
440   // for GetOatDexFile() for a very frequent use case. Never contains a null value.
441   Table oat_dex_files_;
442 
443   // Lock guarding all members needed for secondary lookup in GetOatDexFile().
444   mutable Mutex secondary_lookup_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
445 
446   // If the primary oat_dex_files_ lookup fails, use a secondary map. This map stores
447   // the results of all previous secondary lookups, whether successful (non-null) or
448   // failed (null). If it doesn't contain an entry we need to calculate the canonical
449   // location and use oat_dex_files_by_canonical_location_.
450   mutable Table secondary_oat_dex_files_ GUARDED_BY(secondary_lookup_lock_);
451 
452   // Cache of strings. Contains the backing storage for keys in the secondary_oat_dex_files_
453   // and the lazily initialized oat_dex_files_by_canonical_location_.
454   // NOTE: We're keeping references to contained strings in form of std::string_view and adding
455   // new strings to the end. The adding of a new element must not touch any previously stored
456   // elements. std::list<> and std::deque<> satisfy this requirement, std::vector<> doesn't.
457   mutable std::list<std::string> string_cache_ GUARDED_BY(secondary_lookup_lock_);
458 
459   // Dex files opened directly from a file referenced from the oat file or specifed
460   // by the `dex_filenames` parameter, in case the OatFile does not embed the dex code.
461   std::vector<std::unique_ptr<const DexFile>> external_dex_files_;
462 
463   friend class gc::collector::DummyOatFile;  // For modifying begin_ and end_.
464   friend class OatClass;
465   friend class art::OatDexFile;
466   friend class OatDumper;  // For GetBase and GetLimit
467   friend class OatFileBase;
468   DISALLOW_COPY_AND_ASSIGN(OatFile);
469 };
470 
471 // OatDexFile should be an inner class of OatFile. Unfortunately, C++ doesn't
472 // support forward declarations of inner classes, and we want to
473 // forward-declare OatDexFile so that we can store an opaque pointer to an
474 // OatDexFile in DexFile.
475 class OatDexFile final {
476  public:
477   // Opens the DexFile referred to by this OatDexFile from within the containing OatFile.
478   std::unique_ptr<const DexFile> OpenDexFile(std::string* error_msg) const;
479 
480   // May return null if the OatDexFile only contains a type lookup table. This case only happens
481   // for the compiler to speed up compilation, or in jitzygote.
GetOatFile()482   const OatFile* GetOatFile() const {
483     return oat_file_;
484   }
485 
486   // Returns the size of the DexFile refered to by this OatDexFile.
487   size_t FileSize() const;
488 
489   // Returns original path of DexFile that was the source of this OatDexFile.
GetDexFileLocation()490   const std::string& GetDexFileLocation() const {
491     return dex_file_location_;
492   }
493 
494   // Returns the canonical location of DexFile that was the source of this OatDexFile.
GetCanonicalDexFileLocation()495   const std::string& GetCanonicalDexFileLocation() const {
496     return canonical_dex_file_location_;
497   }
498 
499   // Returns checksum of original DexFile that was the source of this OatDexFile;
GetDexFileLocationChecksum()500   uint32_t GetDexFileLocationChecksum() const {
501     return dex_file_location_checksum_;
502   }
503 
504   // Returns the OatClass for the class specified by the given DexFile class_def_index.
505   OatFile::OatClass GetOatClass(uint16_t class_def_index) const;
506 
507   // Returns the offset to the OatClass information. Most callers should use GetOatClass.
508   uint32_t GetOatClassOffset(uint16_t class_def_index) const;
509 
GetLookupTableData()510   const uint8_t* GetLookupTableData() const {
511     return lookup_table_data_;
512   }
513 
GetMethodBssMapping()514   const IndexBssMapping* GetMethodBssMapping() const {
515     return method_bss_mapping_;
516   }
517 
GetTypeBssMapping()518   const IndexBssMapping* GetTypeBssMapping() const {
519     return type_bss_mapping_;
520   }
521 
GetStringBssMapping()522   const IndexBssMapping* GetStringBssMapping() const {
523     return string_bss_mapping_;
524   }
525 
GetDexFilePointer()526   const uint8_t* GetDexFilePointer() const {
527     return dex_file_pointer_;
528   }
529 
530   ArrayRef<const uint8_t> GetQuickenedInfoOf(const DexFile& dex_file,
531                                              uint32_t dex_method_idx) const;
532 
533   // Looks up a class definition by its class descriptor. Hash must be
534   // ComputeModifiedUtf8Hash(descriptor).
535   static const dex::ClassDef* FindClassDef(const DexFile& dex_file,
536                                            const char* descriptor,
537                                            size_t hash);
538 
539   // Madvise the dex file based on the state we are moving to.
540   static void MadviseDexFile(const DexFile& dex_file, MadviseState state);
541 
GetTypeLookupTable()542   const TypeLookupTable& GetTypeLookupTable() const {
543     return lookup_table_;
544   }
545 
546   ~OatDexFile();
547 
548   // Create only with a type lookup table, used by the compiler to speed up compilation.
549   explicit OatDexFile(TypeLookupTable&& lookup_table);
550 
551   // Return the dex layout sections.
GetDexLayoutSections()552   const DexLayoutSections* GetDexLayoutSections() const {
553     return dex_layout_sections_;
554   }
555 
556  private:
557   OatDexFile(const OatFile* oat_file,
558              const std::string& dex_file_location,
559              const std::string& canonical_dex_file_location,
560              uint32_t dex_file_checksum,
561              const uint8_t* dex_file_pointer,
562              const uint8_t* lookup_table_data,
563              const IndexBssMapping* method_bss_mapping,
564              const IndexBssMapping* type_bss_mapping,
565              const IndexBssMapping* string_bss_mapping,
566              const uint32_t* oat_class_offsets_pointer,
567              const DexLayoutSections* dex_layout_sections);
568 
569   // Create an OatDexFile wrapping an existing DexFile. Will set the OatDexFile
570   // pointer in the DexFile.
571   OatDexFile(const OatFile* oat_file,
572              const DexFile* dex_file,
573              const std::string& dex_file_location,
574              const std::string& canonical_dex_file_location);
575 
576   bool IsBackedByVdexOnly() const;
577 
578   static void AssertAotCompiler();
579 
580   const OatFile* const oat_file_ = nullptr;
581   const std::string dex_file_location_;
582   const std::string canonical_dex_file_location_;
583   const uint32_t dex_file_location_checksum_ = 0u;
584   const uint8_t* const dex_file_pointer_ = nullptr;
585   const uint8_t* const lookup_table_data_ = nullptr;
586   const IndexBssMapping* const method_bss_mapping_ = nullptr;
587   const IndexBssMapping* const type_bss_mapping_ = nullptr;
588   const IndexBssMapping* const string_bss_mapping_ = nullptr;
589   const uint32_t* const oat_class_offsets_pointer_ = nullptr;
590   TypeLookupTable lookup_table_;
591   const DexLayoutSections* const dex_layout_sections_ = nullptr;
592 
593   friend class OatFile;
594   friend class OatFileBase;
595   DISALLOW_COPY_AND_ASSIGN(OatDexFile);
596 };
597 
598 }  // namespace art
599 
600 #endif  // ART_RUNTIME_OAT_FILE_H_
601