blob: d77ea650fe9e29af1e0db945d5655fa94c1ff6cb [file] [log] [blame]
Ian Rogers776ac1f2012-04-13 23:36:36 -07001/*
2 * Copyright (C) 2012 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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_VERIFIER_DEX_GC_MAP_H_
18#define ART_RUNTIME_VERIFIER_DEX_GC_MAP_H_
Ian Rogers776ac1f2012-04-13 23:36:36 -070019
Ian Rogers776ac1f2012-04-13 23:36:36 -070020#include <stdint.h>
21
Elliott Hughes07ed66b2012-12-12 18:34:25 -080022#include "base/logging.h"
23#include "base/macros.h"
24
Ian Rogers776ac1f2012-04-13 23:36:36 -070025namespace art {
26namespace verifier {
27
28/*
29 * Format enumeration for RegisterMap data area.
30 */
31enum RegisterMapFormat {
32 kRegMapFormatUnknown = 0,
Brian Carlstrom7934ac22013-07-26 10:54:15 -070033 kRegMapFormatNone = 1, // Indicates no map data follows.
34 kRegMapFormatCompact8 = 2, // Compact layout, 8-bit addresses.
35 kRegMapFormatCompact16 = 3, // Compact layout, 16-bit addresses.
Ian Rogers776ac1f2012-04-13 23:36:36 -070036};
37
Ian Rogers46c6bb22012-09-18 13:47:36 -070038// Lightweight wrapper for Dex PC to reference bit maps.
39class DexPcToReferenceMap {
Ian Rogers776ac1f2012-04-13 23:36:36 -070040 public:
Vladimir Marko07a32122013-12-10 10:41:18 +000041 explicit DexPcToReferenceMap(const uint8_t* data) : data_(data) {
Ian Rogers776ac1f2012-04-13 23:36:36 -070042 CHECK(data_ != NULL);
Vladimir Marko8171fc32013-11-26 17:05:58 +000043 }
44
45 // The total size of the reference bit map including header.
46 size_t RawSize() const {
47 return EntryWidth() * NumEntries() + 4u /* header */;
Ian Rogers776ac1f2012-04-13 23:36:36 -070048 }
49
50 // The number of entries in the table
51 size_t NumEntries() const {
52 return GetData()[2] | (GetData()[3] << 8);
53 }
54
Ian Rogers46c6bb22012-09-18 13:47:36 -070055 // Get the Dex PC at the given index
56 uint16_t GetDexPc(size_t index) const {
Ian Rogers776ac1f2012-04-13 23:36:36 -070057 size_t entry_offset = index * EntryWidth();
Ian Rogers0c7abda2012-09-19 13:33:42 -070058 if (DexPcWidth() == 1) {
Ian Rogers776ac1f2012-04-13 23:36:36 -070059 return Table()[entry_offset];
60 } else {
61 return Table()[entry_offset] | (Table()[entry_offset + 1] << 8);
62 }
63 }
64
65 // Return address of bitmap encoding what are live references
66 const uint8_t* GetBitMap(size_t index) const {
67 size_t entry_offset = index * EntryWidth();
Ian Rogers0c7abda2012-09-19 13:33:42 -070068 return &Table()[entry_offset + DexPcWidth()];
Ian Rogers776ac1f2012-04-13 23:36:36 -070069 }
70
71 // Find the bitmap associated with the given dex pc
72 const uint8_t* FindBitMap(uint16_t dex_pc, bool error_if_not_present = true) const;
73
74 // The number of bytes used to encode registers
75 size_t RegWidth() const {
76 return GetData()[1] | ((GetData()[0] & ~kRegMapFormatMask) << kRegMapFormatShift);
77 }
78
79 private:
80 // Table of num_entries * (dex pc, bitmap)
81 const uint8_t* Table() const {
82 return GetData() + 4;
83 }
84
85 // The format of the table of the PCs for the table
86 RegisterMapFormat Format() const {
87 return static_cast<RegisterMapFormat>(GetData()[0] & kRegMapFormatMask);
88 }
89
90 // Number of bytes used to encode a dex pc
Ian Rogers0c7abda2012-09-19 13:33:42 -070091 size_t DexPcWidth() const {
Ian Rogers776ac1f2012-04-13 23:36:36 -070092 RegisterMapFormat format = Format();
93 switch (format) {
94 case kRegMapFormatCompact8:
95 return 1;
96 case kRegMapFormatCompact16:
97 return 2;
98 default:
99 LOG(FATAL) << "Invalid format " << static_cast<int>(format);
100 return -1;
101 }
102 }
103
104 // The width of an entry in the table
105 size_t EntryWidth() const {
Ian Rogers0c7abda2012-09-19 13:33:42 -0700106 return DexPcWidth() + RegWidth();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700107 }
108
109 const uint8_t* GetData() const {
110 return data_;
111 }
112
Ian Rogers776ac1f2012-04-13 23:36:36 -0700113 static const int kRegMapFormatShift = 5;
114 static const uint8_t kRegMapFormatMask = 0x7;
115
Ian Rogers0c7abda2012-09-19 13:33:42 -0700116 const uint8_t* const data_; // The header and table data
Ian Rogers776ac1f2012-04-13 23:36:36 -0700117};
118
119} // namespace verifier
120} // namespace art
121
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700122#endif // ART_RUNTIME_VERIFIER_DEX_GC_MAP_H_