Mathieu Chartier | cf76bf8 | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_DEX_COMPACT_DEX_FILE_H_ |
| 18 | #define ART_RUNTIME_DEX_COMPACT_DEX_FILE_H_ |
Mathieu Chartier | cf76bf8 | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 19 | |
Mathieu Chartier | f6e3147 | 2017-12-28 13:32:08 -0800 | [diff] [blame] | 20 | #include "base/casts.h" |
Mathieu Chartier | cf76bf8 | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 21 | #include "dex_file.h" |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 22 | #include "dex/compact_dex_debug_info.h" |
Mathieu Chartier | cf76bf8 | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | // CompactDex is a currently ART internal dex file format that aims to reduce storage/RAM usage. |
| 27 | class CompactDexFile : public DexFile { |
| 28 | public: |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 29 | static constexpr uint8_t kDexMagic[kDexMagicSize] = { 'c', 'd', 'e', 'x' }; |
| 30 | static constexpr uint8_t kDexMagicVersion[] = {'0', '0', '1', '\0'}; |
| 31 | |
Mathieu Chartier | f6e3147 | 2017-12-28 13:32:08 -0800 | [diff] [blame] | 32 | enum class FeatureFlags : uint32_t { |
| 33 | kDefaultMethods = 0x1, |
| 34 | }; |
| 35 | |
Mathieu Chartier | f95a75e | 2017-11-03 15:25:52 -0700 | [diff] [blame] | 36 | class Header : public DexFile::Header { |
Mathieu Chartier | f6e3147 | 2017-12-28 13:32:08 -0800 | [diff] [blame] | 37 | public: |
| 38 | uint32_t GetFeatureFlags() const { |
| 39 | return feature_flags_; |
| 40 | } |
| 41 | |
| 42 | private: |
| 43 | uint32_t feature_flags_ = 0u; |
| 44 | |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 45 | // Position in the compact dex file for the debug info table data starts. |
| 46 | uint32_t debug_info_offsets_pos_ = 0u; |
| 47 | |
| 48 | // Offset into the debug info table data where the lookup table is. |
| 49 | uint32_t debug_info_offsets_table_offset_ = 0u; |
| 50 | |
| 51 | // Base offset of where debug info starts in the dex file. |
| 52 | uint32_t debug_info_base_ = 0u; |
| 53 | |
| 54 | friend class CompactDexFile; |
Mathieu Chartier | f6e3147 | 2017-12-28 13:32:08 -0800 | [diff] [blame] | 55 | friend class CompactDexWriter; |
Mathieu Chartier | f95a75e | 2017-11-03 15:25:52 -0700 | [diff] [blame] | 56 | }; |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 57 | |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 58 | // Like the standard code item except without a debug info offset. |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 59 | struct CodeItem : public DexFile::CodeItem { |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 60 | static constexpr size_t kAlignment = sizeof(uint32_t); |
| 61 | |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 62 | private: |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 63 | CodeItem() = default; |
| 64 | |
| 65 | uint16_t registers_size_; // the number of registers used by this code |
| 66 | // (locals + parameters) |
| 67 | uint16_t ins_size_; // the number of words of incoming arguments to the method |
| 68 | // that this code is for |
| 69 | uint16_t outs_size_; // the number of words of outgoing argument space required |
| 70 | // by this code for method invocation |
| 71 | uint16_t tries_size_; // the number of try_items for this instance. If non-zero, |
| 72 | // then these appear as the tries array just after the |
| 73 | // insns in this instance. |
| 74 | |
| 75 | uint32_t insns_size_in_code_units_; // size of the insns array, in 2 byte code units |
| 76 | uint16_t insns_[1]; // actual array of bytecode. |
| 77 | |
| 78 | ART_FRIEND_TEST(CodeItemAccessorsTest, TestDexInstructionsAccessor); |
| 79 | friend class CodeItemDataAccessor; |
| 80 | friend class CodeItemDebugInfoAccessor; |
| 81 | friend class CodeItemInstructionAccessor; |
Mathieu Chartier | 6238c83 | 2018-01-04 09:55:13 -0800 | [diff] [blame] | 82 | friend class CompactDexFile; |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 83 | friend class CompactDexWriter; |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 84 | DISALLOW_COPY_AND_ASSIGN(CodeItem); |
| 85 | }; |
Mathieu Chartier | cf76bf8 | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 86 | |
Mathieu Chartier | 603ccab | 2017-10-20 14:34:28 -0700 | [diff] [blame] | 87 | // Write the compact dex specific magic. |
| 88 | static void WriteMagic(uint8_t* magic); |
| 89 | |
| 90 | // Write the current version, note that the input is the address of the magic. |
| 91 | static void WriteCurrentVersion(uint8_t* magic); |
| 92 | |
Mathieu Chartier | cf76bf8 | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 93 | // Returns true if the byte string points to the magic value. |
| 94 | static bool IsMagicValid(const uint8_t* magic); |
| 95 | virtual bool IsMagicValid() const OVERRIDE; |
| 96 | |
| 97 | // Returns true if the byte string after the magic is the correct value. |
| 98 | static bool IsVersionValid(const uint8_t* magic); |
| 99 | virtual bool IsVersionValid() const OVERRIDE; |
| 100 | |
Mathieu Chartier | f6e3147 | 2017-12-28 13:32:08 -0800 | [diff] [blame] | 101 | const Header& GetHeader() const { |
| 102 | return down_cast<const Header&>(DexFile::GetHeader()); |
| 103 | } |
| 104 | |
| 105 | virtual bool SupportsDefaultMethods() const OVERRIDE; |
| 106 | |
Mathieu Chartier | 6238c83 | 2018-01-04 09:55:13 -0800 | [diff] [blame] | 107 | uint32_t GetCodeItemSize(const DexFile::CodeItem& item) const OVERRIDE; |
| 108 | |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 109 | uint32_t GetDebugInfoOffset(uint32_t dex_method_index) const { |
| 110 | return debug_info_offsets_.GetDebugInfoOffset(dex_method_index); |
| 111 | } |
| 112 | |
Mathieu Chartier | cf76bf8 | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 113 | private: |
Mathieu Chartier | cf76bf8 | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 114 | CompactDexFile(const uint8_t* base, |
| 115 | size_t size, |
| 116 | const std::string& location, |
| 117 | uint32_t location_checksum, |
David Sehr | 733bd4d | 2017-10-26 10:39:15 -0700 | [diff] [blame] | 118 | const OatDexFile* oat_dex_file, |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 119 | DexFileContainer* container); |
| 120 | |
| 121 | CompactDexDebugInfoOffsetTable::Accessor debug_info_offsets_; |
Mathieu Chartier | cf76bf8 | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 122 | |
| 123 | friend class DexFile; |
Mathieu Chartier | 603ccab | 2017-10-20 14:34:28 -0700 | [diff] [blame] | 124 | friend class DexFileLoader; |
Mathieu Chartier | cf76bf8 | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 125 | DISALLOW_COPY_AND_ASSIGN(CompactDexFile); |
| 126 | }; |
| 127 | |
| 128 | } // namespace art |
| 129 | |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 130 | #endif // ART_RUNTIME_DEX_COMPACT_DEX_FILE_H_ |