blob: 75a0d5e31ee266f293c933a7baaf738a4014e12f [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
30// VDEX files contain extracted DEX files. The VdexFile class maps the file to
31// memory and provides tools for accessing its individual sections.
32//
33// File format:
34// VdexFile::Header fixed-length header
35//
36// DEX[0] array of the input DEX files
37// DEX[1] the bytecode may have been quickened
38// ...
39// DEX[D]
40//
41
42class VdexFile {
43 public:
44 struct Header {
45 public:
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010046 Header(uint32_t dex_size, uint32_t verifier_deps_size, uint32_t quickening_info_size);
David Brazdil7b49e6c2016-09-01 11:06:18 +010047
Nicolas Geoffraye70dd562016-10-30 21:03:35 +000048 const char* GetMagic() const { return reinterpret_cast<const char*>(magic_); }
49 const char* GetVersion() const { return reinterpret_cast<const char*>(version_); }
David Brazdil7b49e6c2016-09-01 11:06:18 +010050 bool IsMagicValid() const;
51 bool IsVersionValid() const;
Nicolas Geoffraye70dd562016-10-30 21:03:35 +000052 bool IsValid() const { return IsMagicValid() && IsVersionValid(); }
David Brazdil7b49e6c2016-09-01 11:06:18 +010053
David Brazdil5d5a36b2016-09-14 15:34:10 +010054 uint32_t GetDexSize() const { return dex_size_; }
55 uint32_t GetVerifierDepsSize() const { return verifier_deps_size_; }
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010056 uint32_t GetQuickeningInfoSize() const { return quickening_info_size_; }
David Brazdil5d5a36b2016-09-14 15:34:10 +010057
David Brazdil7b49e6c2016-09-01 11:06:18 +010058 private:
59 static constexpr uint8_t kVdexMagic[] = { 'v', 'd', 'e', 'x' };
60 static constexpr uint8_t kVdexVersion[] = { '0', '0', '0', '\0' };
61
62 uint8_t magic_[4];
63 uint8_t version_[4];
David Brazdil5d5a36b2016-09-14 15:34:10 +010064 uint32_t dex_size_;
65 uint32_t verifier_deps_size_;
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010066 uint32_t quickening_info_size_;
David Brazdil7b49e6c2016-09-01 11:06:18 +010067 };
68
69 static VdexFile* Open(const std::string& vdex_filename,
70 bool writable,
71 bool low_4gb,
72 std::string* error_msg);
73
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +000074 static VdexFile* Open(int file_fd,
75 size_t vdex_length,
76 const std::string& vdex_filename,
77 bool writable,
78 bool low_4gb,
79 std::string* error_msg);
80
David Brazdil7b49e6c2016-09-01 11:06:18 +010081 const uint8_t* Begin() const { return mmap_->Begin(); }
82 const uint8_t* End() const { return mmap_->End(); }
83 size_t Size() const { return mmap_->Size(); }
84
Nicolas Geoffraye70dd562016-10-30 21:03:35 +000085 const Header& GetHeader() const {
86 return *reinterpret_cast<const Header*>(Begin());
87 }
88
89 ArrayRef<const uint8_t> GetVerifierDepsData() const {
90 return ArrayRef<const uint8_t>(
91 Begin() + sizeof(Header) + GetHeader().GetDexSize(), GetHeader().GetVerifierDepsSize());
92 }
93
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +000094 ArrayRef<const uint8_t> GetQuickeningInfo() const {
95 return ArrayRef<const uint8_t>(
96 GetVerifierDepsData().data() + GetHeader().GetVerifierDepsSize(),
97 GetHeader().GetQuickeningInfoSize());
98 }
99
100 bool IsValid() const {
101 return mmap_->Size() >= sizeof(Header) && GetHeader().IsValid();
102 }
103
104 // This method is for iterating over the dex files in the vdex. If `cursor` is null,
105 // the first dex file is returned. If `cursor` is not null, it must point to a dex
106 // file and this method returns the next dex file if there is one, or null if there
107 // is none.
108 const uint8_t* GetNextDexFileData(const uint8_t* cursor) const;
109
David Brazdil7b49e6c2016-09-01 11:06:18 +0100110 private:
Andreas Gampef7e82232016-09-12 15:55:56 -0700111 explicit VdexFile(MemMap* mmap) : mmap_(mmap) {}
David Brazdil7b49e6c2016-09-01 11:06:18 +0100112
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000113 bool HasDexSection() const {
114 return GetHeader().GetDexSize() != 0;
115 }
116
117 const uint8_t* DexBegin() const {
118 return Begin() + sizeof(Header);
119 }
120
121 const uint8_t* DexEnd() const {
122 return Begin() + sizeof(Header) + GetHeader().GetDexSize();
123 }
124
David Brazdil7b49e6c2016-09-01 11:06:18 +0100125 std::unique_ptr<MemMap> mmap_;
126
127 DISALLOW_COPY_AND_ASSIGN(VdexFile);
128};
129
130} // namespace art
131
132#endif // ART_RUNTIME_VDEX_FILE_H_