blob: 0f347952c9bedaf6bd2bd7d4da40efc029993173 [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"
Mathieu Chartier210531f2018-01-12 10:15:51 -080027#include "quicken_info.h"
David Brazdil7b49e6c2016-09-01 11:06:18 +010028
29namespace art {
30
David Sehrbeca4fe2017-03-30 17:50:24 -070031class DexFile;
32
David Brazdil7b49e6c2016-09-01 11:06:18 +010033// VDEX files contain extracted DEX files. The VdexFile class maps the file to
34// memory and provides tools for accessing its individual sections.
35//
36// File format:
37// VdexFile::Header fixed-length header
38//
Mathieu Chartier210531f2018-01-12 10:15:51 -080039// quicken_table_off[0] offset into QuickeningInfo section for offset table for DEX[0].
40// DEX[0] array of the input DEX files, the bytecode may have been quickened.
41// quicken_table_off[1]
42// DEX[1]
David Brazdil7b49e6c2016-09-01 11:06:18 +010043// ...
44// DEX[D]
Nicolas Geoffrayb4c6acb2017-11-10 12:48:14 +000045// VerifierDeps
46// uint8[D][] verification dependencies
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +010047// QuickeningInfo
Nicolas Geoffrayb4c6acb2017-11-10 12:48:14 +000048// uint8[D][] quickening data
Mathieu Chartier210531f2018-01-12 10:15:51 -080049// uint32[D][] quickening data offset tables
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,
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -080057 uint32_t dex_shared_data_size,
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +000058 uint32_t verifier_deps_size,
59 uint32_t quickening_info_size);
David Brazdil7b49e6c2016-09-01 11:06:18 +010060
Nicolas Geoffraye70dd562016-10-30 21:03:35 +000061 const char* GetMagic() const { return reinterpret_cast<const char*>(magic_); }
62 const char* GetVersion() const { return reinterpret_cast<const char*>(version_); }
David Brazdil7b49e6c2016-09-01 11:06:18 +010063 bool IsMagicValid() const;
64 bool IsVersionValid() const;
Nicolas Geoffraye70dd562016-10-30 21:03:35 +000065 bool IsValid() const { return IsMagicValid() && IsVersionValid(); }
David Brazdil7b49e6c2016-09-01 11:06:18 +010066
David Brazdil5d5a36b2016-09-14 15:34:10 +010067 uint32_t GetDexSize() const { return dex_size_; }
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -080068 uint32_t GetDexSharedDataSize() const { return dex_shared_data_size_; }
David Brazdil5d5a36b2016-09-14 15:34:10 +010069 uint32_t GetVerifierDepsSize() const { return verifier_deps_size_; }
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010070 uint32_t GetQuickeningInfoSize() const { return quickening_info_size_; }
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +000071 uint32_t GetNumberOfDexFiles() const { return number_of_dex_files_; }
David Brazdil5d5a36b2016-09-14 15:34:10 +010072
David Brazdil93592f52017-12-08 10:53:27 +000073 size_t GetComputedFileSize() const {
74 return sizeof(Header) +
75 GetSizeOfChecksumsSection() +
76 GetDexSize() +
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -080077 GetDexSharedDataSize() +
David Brazdil93592f52017-12-08 10:53:27 +000078 GetVerifierDepsSize() +
79 GetQuickeningInfoSize();
80 }
81
82 size_t GetSizeOfChecksumsSection() const {
83 return sizeof(VdexChecksum) * GetNumberOfDexFiles();
84 }
85
Nicolas Geoffray36930ec2017-05-09 13:23:34 +010086 static constexpr uint8_t kVdexInvalidMagic[] = { 'w', 'd', 'e', 'x' };
87
David Brazdil7b49e6c2016-09-01 11:06:18 +010088 private:
89 static constexpr uint8_t kVdexMagic[] = { 'v', 'd', 'e', 'x' };
Mathieu Chartier8e9a5e82018-01-31 09:45:27 -080090 // Last update: Fix separate section for compact dex data.
91 static constexpr uint8_t kVdexVersion[] = { '0', '1', '7', '\0' };
David Brazdil7b49e6c2016-09-01 11:06:18 +010092
93 uint8_t magic_[4];
94 uint8_t version_[4];
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +000095 uint32_t number_of_dex_files_;
David Brazdil5d5a36b2016-09-14 15:34:10 +010096 uint32_t dex_size_;
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -080097 uint32_t dex_shared_data_size_;
David Brazdil5d5a36b2016-09-14 15:34:10 +010098 uint32_t verifier_deps_size_;
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010099 uint32_t quickening_info_size_;
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100100
101 friend class VdexFile;
David Brazdil7b49e6c2016-09-01 11:06:18 +0100102 };
103
Nicolas Geoffraybaeaa9b2018-01-26 14:31:17 +0000104 // Note: The file is called "primary" to match the naming with profiles.
105 static const constexpr char* kVdexNameInDmFile = "primary.vdex";
106
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +0000107 typedef uint32_t VdexChecksum;
Mathieu Chartier210531f2018-01-12 10:15:51 -0800108 using QuickeningTableOffsetType = uint32_t;
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +0000109
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300110 explicit VdexFile(MemMap* mmap) : mmap_(mmap) {}
111
Richard Uhlerb8ab63a2017-01-31 11:27:37 +0000112 // Returns nullptr if the vdex file cannot be opened or is not valid.
David Srbeckyec2cdf42017-12-08 16:21:25 +0000113 // The mmap_* parameters can be left empty (nullptr/0/false) to allocate at random address.
114 static std::unique_ptr<VdexFile> OpenAtAddress(uint8_t* mmap_addr,
115 size_t mmap_size,
116 bool mmap_reuse,
117 const std::string& vdex_filename,
118 bool writable,
119 bool low_4gb,
120 bool unquicken,
121 std::string* error_msg);
122
123 // Returns nullptr if the vdex file cannot be opened or is not valid.
124 // The mmap_* parameters can be left empty (nullptr/0/false) to allocate at random address.
125 static std::unique_ptr<VdexFile> OpenAtAddress(uint8_t* mmap_addr,
126 size_t mmap_size,
127 bool mmap_reuse,
128 int file_fd,
129 size_t vdex_length,
130 const std::string& vdex_filename,
131 bool writable,
132 bool low_4gb,
133 bool unquicken,
134 std::string* error_msg);
135
136 // Returns nullptr if the vdex file cannot be opened or is not valid.
Richard Uhlerb8ab63a2017-01-31 11:27:37 +0000137 static std::unique_ptr<VdexFile> Open(const std::string& vdex_filename,
138 bool writable,
139 bool low_4gb,
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100140 bool unquicken,
David Srbeckyec2cdf42017-12-08 16:21:25 +0000141 std::string* error_msg) {
142 return OpenAtAddress(nullptr,
143 0,
144 false,
145 vdex_filename,
146 writable,
147 low_4gb,
148 unquicken,
149 error_msg);
150 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100151
Richard Uhlerb8ab63a2017-01-31 11:27:37 +0000152 // Returns nullptr if the vdex file cannot be opened or is not valid.
153 static std::unique_ptr<VdexFile> Open(int file_fd,
154 size_t vdex_length,
155 const std::string& vdex_filename,
156 bool writable,
157 bool low_4gb,
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100158 bool unquicken,
David Srbeckyec2cdf42017-12-08 16:21:25 +0000159 std::string* error_msg) {
160 return OpenAtAddress(nullptr,
161 0,
162 false,
163 file_fd,
164 vdex_length,
165 vdex_filename,
166 writable,
167 low_4gb,
168 unquicken,
169 error_msg);
170 }
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000171
David Brazdil7b49e6c2016-09-01 11:06:18 +0100172 const uint8_t* Begin() const { return mmap_->Begin(); }
173 const uint8_t* End() const { return mmap_->End(); }
174 size_t Size() const { return mmap_->Size(); }
175
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000176 const Header& GetHeader() const {
177 return *reinterpret_cast<const Header*>(Begin());
178 }
179
180 ArrayRef<const uint8_t> GetVerifierDepsData() const {
181 return ArrayRef<const uint8_t>(
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -0800182 DexBegin() + GetHeader().GetDexSize() + GetHeader().GetDexSharedDataSize(),
183 GetHeader().GetVerifierDepsSize());
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000184 }
185
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000186 ArrayRef<const uint8_t> GetQuickeningInfo() const {
187 return ArrayRef<const uint8_t>(
188 GetVerifierDepsData().data() + GetHeader().GetVerifierDepsSize(),
189 GetHeader().GetQuickeningInfoSize());
190 }
191
192 bool IsValid() const {
193 return mmap_->Size() >= sizeof(Header) && GetHeader().IsValid();
194 }
195
196 // This method is for iterating over the dex files in the vdex. If `cursor` is null,
197 // the first dex file is returned. If `cursor` is not null, it must point to a dex
198 // file and this method returns the next dex file if there is one, or null if there
199 // is none.
200 const uint8_t* GetNextDexFileData(const uint8_t* cursor) const;
201
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +0000202 // Get the location checksum of the dex file number `dex_file_index`.
203 uint32_t GetLocationChecksum(uint32_t dex_file_index) const {
204 DCHECK_LT(dex_file_index, GetHeader().GetNumberOfDexFiles());
205 return reinterpret_cast<const uint32_t*>(Begin() + sizeof(Header))[dex_file_index];
206 }
207
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100208 // Open all the dex files contained in this vdex file.
David Sehrbeca4fe2017-03-30 17:50:24 -0700209 bool OpenAllDexFiles(std::vector<std::unique_ptr<const DexFile>>* dex_files,
210 std::string* error_msg);
211
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100212 // In-place unquicken the given `dex_files` based on `quickening_info`.
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300213 // `decompile_return_instruction` controls if RETURN_VOID_BARRIER instructions are
214 // decompiled to RETURN_VOID instructions using the slower ClassDataItemIterator
215 // instead of the faster QuickeningInfoIterator.
Mathieu Chartier210531f2018-01-12 10:15:51 -0800216 // Always unquickens using the vdex dex files as the source for quicken tables.
217 void Unquicken(const std::vector<const DexFile*>& target_dex_files,
218 bool decompile_return_instruction) const;
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100219
Nicolas Geoffrayb4c6acb2017-11-10 12:48:14 +0000220 // Fully unquicken `target_dex_file` based on `quickening_info`.
Mathieu Chartier210531f2018-01-12 10:15:51 -0800221 void UnquickenDexFile(const DexFile& target_dex_file,
222 const DexFile& source_dex_file,
223 bool decompile_return_instruction) const;
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100224
Mathieu Chartier210531f2018-01-12 10:15:51 -0800225 // Return the quickening info of a given method index (or null if it's empty).
226 ArrayRef<const uint8_t> GetQuickenedInfoOf(const DexFile& dex_file,
227 uint32_t dex_method_idx) const;
Nicolas Geoffrayb4c6acb2017-11-10 12:48:14 +0000228
Nicolas Geoffraybc177272018-01-24 14:55:32 +0000229 bool HasDexSection() const {
230 return GetHeader().GetDexSize() != 0;
231 }
232
David Brazdil7b49e6c2016-09-01 11:06:18 +0100233 private:
Mathieu Chartier210531f2018-01-12 10:15:51 -0800234 uint32_t GetQuickeningInfoTableOffset(const uint8_t* source_dex_begin) const;
235
236 // Source dex must be the in the vdex file.
237 void UnquickenDexFile(const DexFile& target_dex_file,
238 const uint8_t* source_dex_begin,
239 bool decompile_return_instruction) const;
240
241 QuickenInfoOffsetTableAccessor GetQuickenInfoOffsetTable(
242 const DexFile& dex_file,
243 const ArrayRef<const uint8_t>& quickening_info) const;
244
245 QuickenInfoOffsetTableAccessor GetQuickenInfoOffsetTable(
246 const uint8_t* source_dex_begin,
247 uint32_t num_method_ids,
248 const ArrayRef<const uint8_t>& quickening_info) const;
249
Mathieu Chartier210531f2018-01-12 10:15:51 -0800250 bool ContainsDexFile(const DexFile& dex_file) const;
251
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000252 const uint8_t* DexBegin() const {
David Brazdil93592f52017-12-08 10:53:27 +0000253 return Begin() + sizeof(Header) + GetHeader().GetSizeOfChecksumsSection();
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000254 }
255
256 const uint8_t* DexEnd() const {
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +0000257 return DexBegin() + GetHeader().GetDexSize();
258 }
259
David Brazdil7b49e6c2016-09-01 11:06:18 +0100260 std::unique_ptr<MemMap> mmap_;
261
262 DISALLOW_COPY_AND_ASSIGN(VdexFile);
263};
264
265} // namespace art
266
267#endif // ART_RUNTIME_VDEX_FILE_H_