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