Mathieu Chartier | 7b074bf | 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 | 334b9d7 | 2018-02-12 18:27:56 -0800 | [diff] [blame] | 17 | #ifndef ART_LIBDEXFILE_DEX_STANDARD_DEX_FILE_H_ |
| 18 | #define ART_LIBDEXFILE_DEX_STANDARD_DEX_FILE_H_ |
Mathieu Chartier | 7b074bf | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 19 | |
| 20 | #include <iosfwd> |
| 21 | |
| 22 | #include "dex_file.h" |
| 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | class OatDexFile; |
| 27 | |
Mathieu Chartier | 292567e | 2017-10-12 13:24:38 -0700 | [diff] [blame] | 28 | // Standard dex file. This is the format that is packaged in APKs and produced by tools. |
| 29 | class StandardDexFile : public DexFile { |
Mathieu Chartier | 7b074bf | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 30 | public: |
Mathieu Chartier | f95a75e | 2017-11-03 15:25:52 -0700 | [diff] [blame] | 31 | class Header : public DexFile::Header { |
| 32 | // Same for now. |
| 33 | }; |
| 34 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 35 | struct CodeItem : public dex::CodeItem { |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 36 | static constexpr size_t kAlignment = 4; |
| 37 | |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 38 | private: |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 39 | CodeItem() = default; |
| 40 | |
| 41 | uint16_t registers_size_; // the number of registers used by this code |
| 42 | // (locals + parameters) |
| 43 | uint16_t ins_size_; // the number of words of incoming arguments to the method |
| 44 | // that this code is for |
| 45 | uint16_t outs_size_; // the number of words of outgoing argument space required |
| 46 | // by this code for method invocation |
| 47 | uint16_t tries_size_; // the number of try_items for this instance. If non-zero, |
| 48 | // then these appear as the tries array just after the |
| 49 | // insns in this instance. |
| 50 | uint32_t debug_info_off_; // Holds file offset to debug info stream. |
| 51 | |
| 52 | uint32_t insns_size_in_code_units_; // size of the insns array, in 2 byte code units |
| 53 | uint16_t insns_[1]; // actual array of bytecode. |
| 54 | |
| 55 | ART_FRIEND_TEST(CodeItemAccessorsTest, TestDexInstructionsAccessor); |
| 56 | friend class CodeItemDataAccessor; |
| 57 | friend class CodeItemDebugInfoAccessor; |
| 58 | friend class CodeItemInstructionAccessor; |
| 59 | friend class DexWriter; |
Mathieu Chartier | 6238c83 | 2018-01-04 09:55:13 -0800 | [diff] [blame] | 60 | friend class StandardDexFile; |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 61 | DISALLOW_COPY_AND_ASSIGN(CodeItem); |
| 62 | }; |
| 63 | |
| 64 | // Write the standard dex specific magic. |
| 65 | static void WriteMagic(uint8_t* magic); |
| 66 | |
| 67 | // Write the current version, note that the input is the address of the magic. |
| 68 | static void WriteCurrentVersion(uint8_t* magic); |
| 69 | |
Mathieu Chartier | 7b074bf | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 70 | static const uint8_t kDexMagic[kDexMagicSize]; |
Ulya Trafimovich | ab5f4c1 | 2019-08-16 13:59:11 +0100 | [diff] [blame] | 71 | static constexpr size_t kNumDexVersions = 5; |
Mathieu Chartier | 7b074bf | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 72 | static const uint8_t kDexMagicVersions[kNumDexVersions][kDexVersionLen]; |
| 73 | |
| 74 | // Returns true if the byte string points to the magic value. |
| 75 | static bool IsMagicValid(const uint8_t* magic); |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 76 | bool IsMagicValid() const override; |
Mathieu Chartier | 7b074bf | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 77 | |
| 78 | // Returns true if the byte string after the magic is the correct value. |
| 79 | static bool IsVersionValid(const uint8_t* magic); |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 80 | bool IsVersionValid() const override; |
Mathieu Chartier | 7b074bf | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 81 | |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 82 | bool SupportsDefaultMethods() const override; |
Mathieu Chartier | f6e3147 | 2017-12-28 13:32:08 -0800 | [diff] [blame] | 83 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 84 | uint32_t GetCodeItemSize(const dex::CodeItem& item) const override; |
Mathieu Chartier | 6238c83 | 2018-01-04 09:55:13 -0800 | [diff] [blame] | 85 | |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 86 | size_t GetDequickenedSize() const override { |
Nicolas Geoffray | 16fc474 | 2019-01-30 16:53:24 +0000 | [diff] [blame] | 87 | // JVMTI will run dex layout on standard dex files that have hidden API data, |
| 88 | // in order to remove that data. As dexlayout may increase the size of the dex file, |
| 89 | // be (very) conservative and add one MB to the size. |
| 90 | return Size() + (HasHiddenapiClassData() ? 1 * MB : 0); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Mathieu Chartier | 7b074bf | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 93 | private: |
David Sehr | 0b42677 | 2018-07-03 23:03:42 +0000 | [diff] [blame] | 94 | StandardDexFile(const uint8_t* base, |
| 95 | size_t size, |
Mathieu Chartier | 292567e | 2017-10-12 13:24:38 -0700 | [diff] [blame] | 96 | const std::string& location, |
| 97 | uint32_t location_checksum, |
David Sehr | 0b42677 | 2018-07-03 23:03:42 +0000 | [diff] [blame] | 98 | const OatDexFile* oat_dex_file, |
| 99 | std::unique_ptr<DexFileContainer> container) |
| 100 | : DexFile(base, |
| 101 | size, |
| 102 | /*data_begin*/ base, |
| 103 | /*data_size*/ size, |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 104 | location, |
| 105 | location_checksum, |
| 106 | oat_dex_file, |
David Sehr | 0b42677 | 2018-07-03 23:03:42 +0000 | [diff] [blame] | 107 | std::move(container), |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 108 | /*is_compact_dex*/ false) {} |
Mathieu Chartier | 7b074bf | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 109 | |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 110 | friend class DexFileLoader; |
Mathieu Chartier | 7b074bf | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 111 | friend class DexFileVerifierTest; |
| 112 | |
| 113 | ART_FRIEND_TEST(ClassLinkerTest, RegisterDexFileName); // for constructor |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 114 | friend class OptimizingUnitTestHelper; // for constructor |
Mathieu Chartier | 7b074bf | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 115 | |
Mathieu Chartier | 292567e | 2017-10-12 13:24:38 -0700 | [diff] [blame] | 116 | DISALLOW_COPY_AND_ASSIGN(StandardDexFile); |
Mathieu Chartier | 7b074bf | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | } // namespace art |
| 120 | |
David Sehr | 334b9d7 | 2018-02-12 18:27:56 -0800 | [diff] [blame] | 121 | #endif // ART_LIBDEXFILE_DEX_STANDARD_DEX_FILE_H_ |