blob: 3af36f6791fe553b2ba5a5adf242fd8ce55ce474 [file] [log] [blame]
Mathieu Chartier7b074bf2017-09-25 16:22:36 -07001/*
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 Sehr334b9d72018-02-12 18:27:56 -080017#ifndef ART_LIBDEXFILE_DEX_STANDARD_DEX_FILE_H_
18#define ART_LIBDEXFILE_DEX_STANDARD_DEX_FILE_H_
Mathieu Chartier7b074bf2017-09-25 16:22:36 -070019
20#include <iosfwd>
21
22#include "dex_file.h"
23
24namespace art {
25
26class OatDexFile;
27
Mathieu Chartier292567e2017-10-12 13:24:38 -070028// Standard dex file. This is the format that is packaged in APKs and produced by tools.
29class StandardDexFile : public DexFile {
Mathieu Chartier7b074bf2017-09-25 16:22:36 -070030 public:
Mathieu Chartierf95a75e2017-11-03 15:25:52 -070031 class Header : public DexFile::Header {
32 // Same for now.
33 };
34
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080035 struct CodeItem : public dex::CodeItem {
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080036 static constexpr size_t kAlignment = 4;
37
Nicolas Geoffraya00b54b2019-12-03 14:36:42 +000038 static constexpr size_t InsSizeOffset() {
39 return OFFSETOF_MEMBER(CodeItem, ins_size_);
40 }
41
42 static constexpr size_t OutsSizeOffset() {
43 return OFFSETOF_MEMBER(CodeItem, outs_size_);
44 }
45
46 static constexpr size_t RegistersSizeOffset() {
47 return OFFSETOF_MEMBER(CodeItem, registers_size_);
48 }
49
50 static constexpr size_t InsnsOffset() {
51 return OFFSETOF_MEMBER(CodeItem, insns_);
52 }
53
Mathieu Chartier69147f12017-11-06 20:02:24 -080054 private:
Mathieu Chartier8892c6b2018-01-09 15:10:17 -080055 CodeItem() = default;
56
57 uint16_t registers_size_; // the number of registers used by this code
58 // (locals + parameters)
59 uint16_t ins_size_; // the number of words of incoming arguments to the method
60 // that this code is for
61 uint16_t outs_size_; // the number of words of outgoing argument space required
62 // by this code for method invocation
63 uint16_t tries_size_; // the number of try_items for this instance. If non-zero,
64 // then these appear as the tries array just after the
65 // insns in this instance.
66 uint32_t debug_info_off_; // Holds file offset to debug info stream.
67
68 uint32_t insns_size_in_code_units_; // size of the insns array, in 2 byte code units
69 uint16_t insns_[1]; // actual array of bytecode.
70
71 ART_FRIEND_TEST(CodeItemAccessorsTest, TestDexInstructionsAccessor);
72 friend class CodeItemDataAccessor;
73 friend class CodeItemDebugInfoAccessor;
74 friend class CodeItemInstructionAccessor;
75 friend class DexWriter;
Mathieu Chartier6238c832018-01-04 09:55:13 -080076 friend class StandardDexFile;
Mathieu Chartier69147f12017-11-06 20:02:24 -080077 DISALLOW_COPY_AND_ASSIGN(CodeItem);
78 };
79
80 // Write the standard dex specific magic.
81 static void WriteMagic(uint8_t* magic);
82
83 // Write the current version, note that the input is the address of the magic.
84 static void WriteCurrentVersion(uint8_t* magic);
85
Mathieu Chartier7b074bf2017-09-25 16:22:36 -070086 static const uint8_t kDexMagic[kDexMagicSize];
Ulya Trafimovichab5f4c12019-08-16 13:59:11 +010087 static constexpr size_t kNumDexVersions = 5;
Mathieu Chartier7b074bf2017-09-25 16:22:36 -070088 static const uint8_t kDexMagicVersions[kNumDexVersions][kDexVersionLen];
89
90 // Returns true if the byte string points to the magic value.
91 static bool IsMagicValid(const uint8_t* magic);
Roland Levillainf73caca2018-08-24 17:19:07 +010092 bool IsMagicValid() const override;
Mathieu Chartier7b074bf2017-09-25 16:22:36 -070093
94 // Returns true if the byte string after the magic is the correct value.
95 static bool IsVersionValid(const uint8_t* magic);
Roland Levillainf73caca2018-08-24 17:19:07 +010096 bool IsVersionValid() const override;
Mathieu Chartier7b074bf2017-09-25 16:22:36 -070097
Roland Levillainf73caca2018-08-24 17:19:07 +010098 bool SupportsDefaultMethods() const override;
Mathieu Chartierf6e31472017-12-28 13:32:08 -080099
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800100 uint32_t GetCodeItemSize(const dex::CodeItem& item) const override;
Mathieu Chartier6238c832018-01-04 09:55:13 -0800101
Roland Levillainf73caca2018-08-24 17:19:07 +0100102 size_t GetDequickenedSize() const override {
Nicolas Geoffray16fc4742019-01-30 16:53:24 +0000103 // JVMTI will run dex layout on standard dex files that have hidden API data,
104 // in order to remove that data. As dexlayout may increase the size of the dex file,
105 // be (very) conservative and add one MB to the size.
106 return Size() + (HasHiddenapiClassData() ? 1 * MB : 0);
Alex Lightca97ada2018-02-02 09:25:31 -0800107 }
108
Mathieu Chartier7b074bf2017-09-25 16:22:36 -0700109 private:
David Sehr0b426772018-07-03 23:03:42 +0000110 StandardDexFile(const uint8_t* base,
111 size_t size,
Mathieu Chartier292567e2017-10-12 13:24:38 -0700112 const std::string& location,
113 uint32_t location_checksum,
David Sehr0b426772018-07-03 23:03:42 +0000114 const OatDexFile* oat_dex_file,
115 std::unique_ptr<DexFileContainer> container)
116 : DexFile(base,
117 size,
118 /*data_begin*/ base,
119 /*data_size*/ size,
Mathieu Chartier69147f12017-11-06 20:02:24 -0800120 location,
121 location_checksum,
122 oat_dex_file,
David Sehr0b426772018-07-03 23:03:42 +0000123 std::move(container),
Mathieu Chartier69147f12017-11-06 20:02:24 -0800124 /*is_compact_dex*/ false) {}
Mathieu Chartier7b074bf2017-09-25 16:22:36 -0700125
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700126 friend class DexFileLoader;
Mathieu Chartier7b074bf2017-09-25 16:22:36 -0700127 friend class DexFileVerifierTest;
128
129 ART_FRIEND_TEST(ClassLinkerTest, RegisterDexFileName); // for constructor
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800130 friend class OptimizingUnitTestHelper; // for constructor
Mathieu Chartier7b074bf2017-09-25 16:22:36 -0700131
Mathieu Chartier292567e2017-10-12 13:24:38 -0700132 DISALLOW_COPY_AND_ASSIGN(StandardDexFile);
Mathieu Chartier7b074bf2017-09-25 16:22:36 -0700133};
134
135} // namespace art
136
David Sehr334b9d72018-02-12 18:27:56 -0800137#endif // ART_LIBDEXFILE_DEX_STANDARD_DEX_FILE_H_