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