blob: 9c0d9dba8f33e207b17be161739e30bd36821dcd [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//
38// DEX[0] array of the input DEX files
39// DEX[1] the bytecode may have been quickened
40// ...
41// DEX[D]
42//
43
44class VdexFile {
45 public:
46 struct Header {
47 public:
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +000048 Header(uint32_t number_of_dex_files_,
49 uint32_t dex_size,
50 uint32_t verifier_deps_size,
51 uint32_t quickening_info_size);
David Brazdil7b49e6c2016-09-01 11:06:18 +010052
Nicolas Geoffraye70dd562016-10-30 21:03:35 +000053 const char* GetMagic() const { return reinterpret_cast<const char*>(magic_); }
54 const char* GetVersion() const { return reinterpret_cast<const char*>(version_); }
David Brazdil7b49e6c2016-09-01 11:06:18 +010055 bool IsMagicValid() const;
56 bool IsVersionValid() const;
Nicolas Geoffraye70dd562016-10-30 21:03:35 +000057 bool IsValid() const { return IsMagicValid() && IsVersionValid(); }
David Brazdil7b49e6c2016-09-01 11:06:18 +010058
David Brazdil5d5a36b2016-09-14 15:34:10 +010059 uint32_t GetDexSize() const { return dex_size_; }
60 uint32_t GetVerifierDepsSize() const { return verifier_deps_size_; }
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010061 uint32_t GetQuickeningInfoSize() const { return quickening_info_size_; }
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +000062 uint32_t GetNumberOfDexFiles() const { return number_of_dex_files_; }
David Brazdil5d5a36b2016-09-14 15:34:10 +010063
David Brazdil7b49e6c2016-09-01 11:06:18 +010064 private:
65 static constexpr uint8_t kVdexMagic[] = { 'v', 'd', 'e', 'x' };
Nicolas Geoffray6e54f782017-03-08 15:27:09 +000066 static constexpr uint8_t kVdexVersion[] = { '0', '0', '5', '\0' }; // access flags
David Brazdil7b49e6c2016-09-01 11:06:18 +010067
68 uint8_t magic_[4];
69 uint8_t version_[4];
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +000070 uint32_t number_of_dex_files_;
David Brazdil5d5a36b2016-09-14 15:34:10 +010071 uint32_t dex_size_;
72 uint32_t verifier_deps_size_;
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010073 uint32_t quickening_info_size_;
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +010074
75 friend class VdexFile;
David Brazdil7b49e6c2016-09-01 11:06:18 +010076 };
77
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +000078 typedef uint32_t VdexChecksum;
79
Richard Uhlerb8ab63a2017-01-31 11:27:37 +000080 // Returns nullptr if the vdex file cannot be opened or is not valid.
81 static std::unique_ptr<VdexFile> Open(const std::string& vdex_filename,
82 bool writable,
83 bool low_4gb,
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +010084 bool unquicken,
Richard Uhlerb8ab63a2017-01-31 11:27:37 +000085 std::string* error_msg);
David Brazdil7b49e6c2016-09-01 11:06:18 +010086
Richard Uhlerb8ab63a2017-01-31 11:27:37 +000087 // Returns nullptr if the vdex file cannot be opened or is not valid.
88 static std::unique_ptr<VdexFile> Open(int file_fd,
89 size_t vdex_length,
90 const std::string& vdex_filename,
91 bool writable,
92 bool low_4gb,
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +010093 bool unquicken,
Richard Uhlerb8ab63a2017-01-31 11:27:37 +000094 std::string* error_msg);
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +000095
David Brazdil7b49e6c2016-09-01 11:06:18 +010096 const uint8_t* Begin() const { return mmap_->Begin(); }
97 const uint8_t* End() const { return mmap_->End(); }
98 size_t Size() const { return mmap_->Size(); }
99
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000100 const Header& GetHeader() const {
101 return *reinterpret_cast<const Header*>(Begin());
102 }
103
104 ArrayRef<const uint8_t> GetVerifierDepsData() const {
105 return ArrayRef<const uint8_t>(
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +0000106 DexBegin() + GetHeader().GetDexSize(), GetHeader().GetVerifierDepsSize());
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000107 }
108
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000109 ArrayRef<const uint8_t> GetQuickeningInfo() const {
110 return ArrayRef<const uint8_t>(
111 GetVerifierDepsData().data() + GetHeader().GetVerifierDepsSize(),
112 GetHeader().GetQuickeningInfoSize());
113 }
114
115 bool IsValid() const {
116 return mmap_->Size() >= sizeof(Header) && GetHeader().IsValid();
117 }
118
119 // This method is for iterating over the dex files in the vdex. If `cursor` is null,
120 // the first dex file is returned. If `cursor` is not null, it must point to a dex
121 // file and this method returns the next dex file if there is one, or null if there
122 // is none.
123 const uint8_t* GetNextDexFileData(const uint8_t* cursor) const;
124
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +0000125 // Get the location checksum of the dex file number `dex_file_index`.
126 uint32_t GetLocationChecksum(uint32_t dex_file_index) const {
127 DCHECK_LT(dex_file_index, GetHeader().GetNumberOfDexFiles());
128 return reinterpret_cast<const uint32_t*>(Begin() + sizeof(Header))[dex_file_index];
129 }
130
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100131 // Opens all the dex files contained in this vdex file.
David Sehrbeca4fe2017-03-30 17:50:24 -0700132 bool OpenAllDexFiles(std::vector<std::unique_ptr<const DexFile>>* dex_files,
133 std::string* error_msg);
134
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100135 // In-place unquicken the given `dex_files` based on `quickening_info`.
136 static void Unquicken(const std::vector<const DexFile*>& dex_files,
137 const ArrayRef<const uint8_t>& quickening_info);
138
David Brazdil7b49e6c2016-09-01 11:06:18 +0100139 private:
Andreas Gampef7e82232016-09-12 15:55:56 -0700140 explicit VdexFile(MemMap* mmap) : mmap_(mmap) {}
David Brazdil7b49e6c2016-09-01 11:06:18 +0100141
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000142 bool HasDexSection() const {
143 return GetHeader().GetDexSize() != 0;
144 }
145
146 const uint8_t* DexBegin() const {
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +0000147 return Begin() + sizeof(Header) + GetSizeOfChecksumsSection();
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000148 }
149
150 const uint8_t* DexEnd() const {
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +0000151 return DexBegin() + GetHeader().GetDexSize();
152 }
153
154 size_t GetSizeOfChecksumsSection() const {
155 return sizeof(VdexChecksum) * GetHeader().GetNumberOfDexFiles();
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000156 }
157
David Brazdil7b49e6c2016-09-01 11:06:18 +0100158 std::unique_ptr<MemMap> mmap_;
159
160 DISALLOW_COPY_AND_ASSIGN(VdexFile);
161};
162
163} // namespace art
164
165#endif // ART_RUNTIME_VDEX_FILE_H_