blob: af782a981aa7e22963031d41be7b531a6fbe3e14 [file] [log] [blame]
Mathieu Chartiercf76bf82017-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 Sehr9e734c72018-01-04 17:56:19 -080017#ifndef ART_RUNTIME_DEX_COMPACT_DEX_FILE_H_
18#define ART_RUNTIME_DEX_COMPACT_DEX_FILE_H_
Mathieu Chartiercf76bf82017-09-25 16:22:36 -070019
Mathieu Chartierf6e31472017-12-28 13:32:08 -080020#include "base/casts.h"
Mathieu Chartiercf76bf82017-09-25 16:22:36 -070021#include "dex_file.h"
Mathieu Chartier8892c6b2018-01-09 15:10:17 -080022#include "dex/compact_dex_debug_info.h"
Mathieu Chartiercf76bf82017-09-25 16:22:36 -070023
24namespace art {
25
26// CompactDex is a currently ART internal dex file format that aims to reduce storage/RAM usage.
27class CompactDexFile : public DexFile {
28 public:
Mathieu Chartier69147f12017-11-06 20:02:24 -080029 static constexpr uint8_t kDexMagic[kDexMagicSize] = { 'c', 'd', 'e', 'x' };
30 static constexpr uint8_t kDexMagicVersion[] = {'0', '0', '1', '\0'};
31
Mathieu Chartierf6e31472017-12-28 13:32:08 -080032 enum class FeatureFlags : uint32_t {
33 kDefaultMethods = 0x1,
34 };
35
Mathieu Chartierf95a75e2017-11-03 15:25:52 -070036 class Header : public DexFile::Header {
Mathieu Chartierf6e31472017-12-28 13:32:08 -080037 public:
38 uint32_t GetFeatureFlags() const {
39 return feature_flags_;
40 }
41
42 private:
43 uint32_t feature_flags_ = 0u;
44
Mathieu Chartier8892c6b2018-01-09 15:10:17 -080045 // 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 Chartierf6e31472017-12-28 13:32:08 -080055 friend class CompactDexWriter;
Mathieu Chartierf95a75e2017-11-03 15:25:52 -070056 };
Mathieu Chartier69147f12017-11-06 20:02:24 -080057
Mathieu Chartier8892c6b2018-01-09 15:10:17 -080058 // Like the standard code item except without a debug info offset.
Mathieu Chartier69147f12017-11-06 20:02:24 -080059 struct CodeItem : public DexFile::CodeItem {
Mathieu Chartier8892c6b2018-01-09 15:10:17 -080060 static constexpr size_t kAlignment = sizeof(uint32_t);
61
Mathieu Chartier69147f12017-11-06 20:02:24 -080062 private:
Mathieu Chartier8892c6b2018-01-09 15:10:17 -080063 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 Chartier6238c832018-01-04 09:55:13 -080082 friend class CompactDexFile;
Mathieu Chartier8892c6b2018-01-09 15:10:17 -080083 friend class CompactDexWriter;
Mathieu Chartier69147f12017-11-06 20:02:24 -080084 DISALLOW_COPY_AND_ASSIGN(CodeItem);
85 };
Mathieu Chartiercf76bf82017-09-25 16:22:36 -070086
Mathieu Chartier603ccab2017-10-20 14:34:28 -070087 // 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 Chartiercf76bf82017-09-25 16:22:36 -070093 // 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 Chartierf6e31472017-12-28 13:32:08 -0800101 const Header& GetHeader() const {
102 return down_cast<const Header&>(DexFile::GetHeader());
103 }
104
105 virtual bool SupportsDefaultMethods() const OVERRIDE;
106
Mathieu Chartier6238c832018-01-04 09:55:13 -0800107 uint32_t GetCodeItemSize(const DexFile::CodeItem& item) const OVERRIDE;
108
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800109 uint32_t GetDebugInfoOffset(uint32_t dex_method_index) const {
110 return debug_info_offsets_.GetDebugInfoOffset(dex_method_index);
111 }
112
Mathieu Chartiercf76bf82017-09-25 16:22:36 -0700113 private:
Mathieu Chartiercf76bf82017-09-25 16:22:36 -0700114 CompactDexFile(const uint8_t* base,
115 size_t size,
116 const std::string& location,
117 uint32_t location_checksum,
David Sehr733bd4d2017-10-26 10:39:15 -0700118 const OatDexFile* oat_dex_file,
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800119 DexFileContainer* container);
120
121 CompactDexDebugInfoOffsetTable::Accessor debug_info_offsets_;
Mathieu Chartiercf76bf82017-09-25 16:22:36 -0700122
123 friend class DexFile;
Mathieu Chartier603ccab2017-10-20 14:34:28 -0700124 friend class DexFileLoader;
Mathieu Chartiercf76bf82017-09-25 16:22:36 -0700125 DISALLOW_COPY_AND_ASSIGN(CompactDexFile);
126};
127
128} // namespace art
129
David Sehr9e734c72018-01-04 17:56:19 -0800130#endif // ART_RUNTIME_DEX_COMPACT_DEX_FILE_H_