OSDN Git Service

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