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