blob: f78335d347d7bffb60dab2260b15a4ad500afb8b [file] [log] [blame]
David Brazdil7b49e6c2016-09-01 11:06:18 +01001/*
2 * Copyright (C) 2016 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_VDEX_FILE_H_
18#define ART_RUNTIME_VDEX_FILE_H_
19
20#include <stdint.h>
21#include <string>
22
Nicolas Geoffraye70dd562016-10-30 21:03:35 +000023#include "base/array_ref.h"
David Brazdil7b49e6c2016-09-01 11:06:18 +010024#include "base/macros.h"
David Brazdil7b49e6c2016-09-01 11:06:18 +010025#include "mem_map.h"
26#include "os.h"
27
28namespace art {
29
David Sehrbeca4fe2017-03-30 17:50:24 -070030class DexFile;
31
David Brazdil7b49e6c2016-09-01 11:06:18 +010032// VDEX files contain extracted DEX files. The VdexFile class maps the file to
33// memory and provides tools for accessing its individual sections.
34//
35// File format:
36// VdexFile::Header fixed-length header
37//
Nicolas Geoffray67169412018-01-12 09:06:14 +000038// DEX[0] array of the input DEX files
39// DEX[1] the bytecode may have been quickened
David Brazdil7b49e6c2016-09-01 11:06:18 +010040// ...
41// DEX[D]
Nicolas Geoffrayb4c6acb2017-11-10 12:48:14 +000042// VerifierDeps
43// uint8[D][] verification dependencies
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +010044// QuickeningInfo
Nicolas Geoffrayb4c6acb2017-11-10 12:48:14 +000045// uint8[D][] quickening data
Nicolas Geoffray67169412018-01-12 09:06:14 +000046// unaligned_uint32_t[D][2][] table of offsets pair:
47// uint32_t[0] contains original CodeItem::debug_info_off_
48// uint32_t[1] contains quickening data offset from the start
49// of QuickeningInfo
David Brazdil7b49e6c2016-09-01 11:06:18 +010050
51class VdexFile {
52 public:
53 struct Header {
54 public:
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +000055 Header(uint32_t number_of_dex_files_,
56 uint32_t dex_size,
57 uint32_t verifier_deps_size,
58 uint32_t quickening_info_size);
David Brazdil7b49e6c2016-09-01 11:06:18 +010059
Nicolas Geoffraye70dd562016-10-30 21:03:35 +000060 const char* GetMagic() const { return reinterpret_cast<const char*>(magic_); }
61 const char* GetVersion() const { return reinterpret_cast<const char*>(version_); }
David Brazdil7b49e6c2016-09-01 11:06:18 +010062 bool IsMagicValid() const;
63 bool IsVersionValid() const;
Nicolas Geoffraye70dd562016-10-30 21:03:35 +000064 bool IsValid() const { return IsMagicValid() && IsVersionValid(); }
David Brazdil7b49e6c2016-09-01 11:06:18 +010065
David Brazdil5d5a36b2016-09-14 15:34:10 +010066 uint32_t GetDexSize() const { return dex_size_; }
67 uint32_t GetVerifierDepsSize() const { return verifier_deps_size_; }
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010068 uint32_t GetQuickeningInfoSize() const { return quickening_info_size_; }
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +000069 uint32_t GetNumberOfDexFiles() const { return number_of_dex_files_; }
David Brazdil5d5a36b2016-09-14 15:34:10 +010070
David Brazdil93592f52017-12-08 10:53:27 +000071 size_t GetComputedFileSize() const {
72 return sizeof(Header) +
73 GetSizeOfChecksumsSection() +
74 GetDexSize() +
75 GetVerifierDepsSize() +
76 GetQuickeningInfoSize();
77 }
78
79 size_t GetSizeOfChecksumsSection() const {
80 return sizeof(VdexChecksum) * GetNumberOfDexFiles();
81 }
82
Nicolas Geoffray36930ec2017-05-09 13:23:34 +010083 static constexpr uint8_t kVdexInvalidMagic[] = { 'w', 'd', 'e', 'x' };
84
David Brazdil7b49e6c2016-09-01 11:06:18 +010085 private:
86 static constexpr uint8_t kVdexMagic[] = { 'v', 'd', 'e', 'x' };
Nicolas Geoffray67169412018-01-12 09:06:14 +000087 // Last update: Lookup-friendly encoding for quickening info.
88 static constexpr uint8_t kVdexVersion[] = { '0', '1', '1', '\0' };
David Brazdil7b49e6c2016-09-01 11:06:18 +010089
90 uint8_t magic_[4];
91 uint8_t version_[4];
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +000092 uint32_t number_of_dex_files_;
David Brazdil5d5a36b2016-09-14 15:34:10 +010093 uint32_t dex_size_;
94 uint32_t verifier_deps_size_;
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010095 uint32_t quickening_info_size_;
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +010096
97 friend class VdexFile;
David Brazdil7b49e6c2016-09-01 11:06:18 +010098 };
99
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +0000100 typedef uint32_t VdexChecksum;
101
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300102 explicit VdexFile(MemMap* mmap) : mmap_(mmap) {}
103
Richard Uhlerb8ab63a2017-01-31 11:27:37 +0000104 // Returns nullptr if the vdex file cannot be opened or is not valid.
David Srbeckyec2cdf42017-12-08 16:21:25 +0000105 // The mmap_* parameters can be left empty (nullptr/0/false) to allocate at random address.
106 static std::unique_ptr<VdexFile> OpenAtAddress(uint8_t* mmap_addr,
107 size_t mmap_size,
108 bool mmap_reuse,
109 const std::string& vdex_filename,
110 bool writable,
111 bool low_4gb,
112 bool unquicken,
113 std::string* error_msg);
114
115 // Returns nullptr if the vdex file cannot be opened or is not valid.
116 // The mmap_* parameters can be left empty (nullptr/0/false) to allocate at random address.
117 static std::unique_ptr<VdexFile> OpenAtAddress(uint8_t* mmap_addr,
118 size_t mmap_size,
119 bool mmap_reuse,
120 int file_fd,
121 size_t vdex_length,
122 const std::string& vdex_filename,
123 bool writable,
124 bool low_4gb,
125 bool unquicken,
126 std::string* error_msg);
127
128 // Returns nullptr if the vdex file cannot be opened or is not valid.
Richard Uhlerb8ab63a2017-01-31 11:27:37 +0000129 static std::unique_ptr<VdexFile> Open(const std::string& vdex_filename,
130 bool writable,
131 bool low_4gb,
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100132 bool unquicken,
David Srbeckyec2cdf42017-12-08 16:21:25 +0000133 std::string* error_msg) {
134 return OpenAtAddress(nullptr,
135 0,
136 false,
137 vdex_filename,
138 writable,
139 low_4gb,
140 unquicken,
141 error_msg);
142 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100143
Richard Uhlerb8ab63a2017-01-31 11:27:37 +0000144 // Returns nullptr if the vdex file cannot be opened or is not valid.
145 static std::unique_ptr<VdexFile> Open(int file_fd,
146 size_t vdex_length,
147 const std::string& vdex_filename,
148 bool writable,
149 bool low_4gb,
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100150 bool unquicken,
David Srbeckyec2cdf42017-12-08 16:21:25 +0000151 std::string* error_msg) {
152 return OpenAtAddress(nullptr,
153 0,
154 false,
155 file_fd,
156 vdex_length,
157 vdex_filename,
158 writable,
159 low_4gb,
160 unquicken,
161 error_msg);
162 }
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000163
David Brazdil7b49e6c2016-09-01 11:06:18 +0100164 const uint8_t* Begin() const { return mmap_->Begin(); }
165 const uint8_t* End() const { return mmap_->End(); }
166 size_t Size() const { return mmap_->Size(); }
167
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000168 const Header& GetHeader() const {
169 return *reinterpret_cast<const Header*>(Begin());
170 }
171
172 ArrayRef<const uint8_t> GetVerifierDepsData() const {
173 return ArrayRef<const uint8_t>(
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +0000174 DexBegin() + GetHeader().GetDexSize(), GetHeader().GetVerifierDepsSize());
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000175 }
176
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000177 ArrayRef<const uint8_t> GetQuickeningInfo() const {
178 return ArrayRef<const uint8_t>(
179 GetVerifierDepsData().data() + GetHeader().GetVerifierDepsSize(),
180 GetHeader().GetQuickeningInfoSize());
181 }
182
183 bool IsValid() const {
184 return mmap_->Size() >= sizeof(Header) && GetHeader().IsValid();
185 }
186
187 // This method is for iterating over the dex files in the vdex. If `cursor` is null,
188 // the first dex file is returned. If `cursor` is not null, it must point to a dex
189 // file and this method returns the next dex file if there is one, or null if there
190 // is none.
191 const uint8_t* GetNextDexFileData(const uint8_t* cursor) const;
192
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +0000193 // Get the location checksum of the dex file number `dex_file_index`.
194 uint32_t GetLocationChecksum(uint32_t dex_file_index) const {
195 DCHECK_LT(dex_file_index, GetHeader().GetNumberOfDexFiles());
196 return reinterpret_cast<const uint32_t*>(Begin() + sizeof(Header))[dex_file_index];
197 }
198
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100199 // Open all the dex files contained in this vdex file.
David Sehrbeca4fe2017-03-30 17:50:24 -0700200 bool OpenAllDexFiles(std::vector<std::unique_ptr<const DexFile>>* dex_files,
201 std::string* error_msg);
202
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100203 // In-place unquicken the given `dex_files` based on `quickening_info`.
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300204 // `decompile_return_instruction` controls if RETURN_VOID_BARRIER instructions are
205 // decompiled to RETURN_VOID instructions using the slower ClassDataItemIterator
206 // instead of the faster QuickeningInfoIterator.
Nicolas Geoffray67169412018-01-12 09:06:14 +0000207 static void Unquicken(const std::vector<const DexFile*>& dex_files,
208 ArrayRef<const uint8_t> quickening_info,
209 bool decompile_return_instruction);
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100210
Nicolas Geoffrayb4c6acb2017-11-10 12:48:14 +0000211 // Fully unquicken `target_dex_file` based on `quickening_info`.
Nicolas Geoffray67169412018-01-12 09:06:14 +0000212 static void UnquickenDexFile(const DexFile& target_dex_file,
213 ArrayRef<const uint8_t> quickening_info,
214 bool decompile_return_instruction);
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100215
Nicolas Geoffray67169412018-01-12 09:06:14 +0000216 // Return the quickening info of the given code item.
217 const uint8_t* GetQuickenedInfoOf(const DexFile& dex_file, uint32_t code_item_offset) const;
218
219 uint32_t GetDebugInfoOffset(const DexFile& dex_file, uint32_t offset_in_code_item) const;
220
221 static bool CanEncodeQuickenedData(const DexFile& dex_file);
222
223 static constexpr uint32_t kNoQuickeningInfoOffset = -1;
Nicolas Geoffrayb4c6acb2017-11-10 12:48:14 +0000224
David Brazdil7b49e6c2016-09-01 11:06:18 +0100225 private:
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000226 bool HasDexSection() const {
227 return GetHeader().GetDexSize() != 0;
228 }
229
230 const uint8_t* DexBegin() const {
David Brazdil93592f52017-12-08 10:53:27 +0000231 return Begin() + sizeof(Header) + GetHeader().GetSizeOfChecksumsSection();
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000232 }
233
234 const uint8_t* DexEnd() const {
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +0000235 return DexBegin() + GetHeader().GetDexSize();
236 }
237
Nicolas Geoffray67169412018-01-12 09:06:14 +0000238 uint32_t GetDexFileIndex(const DexFile& dex_file) const;
239
David Brazdil7b49e6c2016-09-01 11:06:18 +0100240 std::unique_ptr<MemMap> mmap_;
241
242 DISALLOW_COPY_AND_ASSIGN(VdexFile);
243};
244
245} // namespace art
246
247#endif // ART_RUNTIME_VDEX_FILE_H_