David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1 | /* |
| 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 Geoffray | e70dd56 | 2016-10-30 21:03:35 +0000 | [diff] [blame] | 23 | #include "base/array_ref.h" |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 24 | #include "base/macros.h" |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 25 | #include "mem_map.h" |
| 26 | #include "os.h" |
| 27 | |
| 28 | namespace 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 | |
| 42 | class VdexFile { |
| 43 | public: |
| 44 | struct Header { |
| 45 | public: |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 46 | Header(uint32_t dex_size, uint32_t verifier_deps_size, uint32_t quickening_info_size); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 47 | |
Nicolas Geoffray | e70dd56 | 2016-10-30 21:03:35 +0000 | [diff] [blame] | 48 | const char* GetMagic() const { return reinterpret_cast<const char*>(magic_); } |
| 49 | const char* GetVersion() const { return reinterpret_cast<const char*>(version_); } |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 50 | bool IsMagicValid() const; |
| 51 | bool IsVersionValid() const; |
Nicolas Geoffray | e70dd56 | 2016-10-30 21:03:35 +0000 | [diff] [blame] | 52 | bool IsValid() const { return IsMagicValid() && IsVersionValid(); } |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 53 | |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 54 | uint32_t GetDexSize() const { return dex_size_; } |
| 55 | uint32_t GetVerifierDepsSize() const { return verifier_deps_size_; } |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 56 | uint32_t GetQuickeningInfoSize() const { return quickening_info_size_; } |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 57 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 58 | 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 Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 64 | uint32_t dex_size_; |
| 65 | uint32_t verifier_deps_size_; |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 66 | uint32_t quickening_info_size_; |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | static VdexFile* Open(const std::string& vdex_filename, |
| 70 | bool writable, |
| 71 | bool low_4gb, |
| 72 | std::string* error_msg); |
| 73 | |
| 74 | const uint8_t* Begin() const { return mmap_->Begin(); } |
| 75 | const uint8_t* End() const { return mmap_->End(); } |
| 76 | size_t Size() const { return mmap_->Size(); } |
| 77 | |
Nicolas Geoffray | e70dd56 | 2016-10-30 21:03:35 +0000 | [diff] [blame] | 78 | const Header& GetHeader() const { |
| 79 | return *reinterpret_cast<const Header*>(Begin()); |
| 80 | } |
| 81 | |
| 82 | ArrayRef<const uint8_t> GetVerifierDepsData() const { |
| 83 | return ArrayRef<const uint8_t>( |
| 84 | Begin() + sizeof(Header) + GetHeader().GetDexSize(), GetHeader().GetVerifierDepsSize()); |
| 85 | } |
| 86 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 87 | private: |
Andreas Gampe | f7e8223 | 2016-09-12 15:55:56 -0700 | [diff] [blame] | 88 | explicit VdexFile(MemMap* mmap) : mmap_(mmap) {} |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 89 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 90 | std::unique_ptr<MemMap> mmap_; |
| 91 | |
| 92 | DISALLOW_COPY_AND_ASSIGN(VdexFile); |
| 93 | }; |
| 94 | |
| 95 | } // namespace art |
| 96 | |
| 97 | #endif // ART_RUNTIME_VDEX_FILE_H_ |