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