Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_VERIFIER_DEX_GC_MAP_H_ |
| 18 | #define ART_RUNTIME_VERIFIER_DEX_GC_MAP_H_ |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 19 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 20 | #include <stdint.h> |
| 21 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 22 | #include "base/logging.h" |
| 23 | #include "base/macros.h" |
| 24 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 25 | namespace art { |
| 26 | namespace verifier { |
| 27 | |
| 28 | /* |
| 29 | * Format enumeration for RegisterMap data area. |
| 30 | */ |
| 31 | enum RegisterMapFormat { |
| 32 | kRegMapFormatUnknown = 0, |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 33 | kRegMapFormatNone = 1, // Indicates no map data follows. |
| 34 | kRegMapFormatCompact8 = 2, // Compact layout, 8-bit addresses. |
| 35 | kRegMapFormatCompact16 = 3, // Compact layout, 16-bit addresses. |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 36 | }; |
| 37 | |
Ian Rogers | 46c6bb2 | 2012-09-18 13:47:36 -0700 | [diff] [blame] | 38 | // Lightweight wrapper for Dex PC to reference bit maps. |
| 39 | class DexPcToReferenceMap { |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 40 | public: |
Vladimir Marko | 07a3212 | 2013-12-10 10:41:18 +0000 | [diff] [blame] | 41 | explicit DexPcToReferenceMap(const uint8_t* data) : data_(data) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame^] | 42 | CHECK(data_ != nullptr); |
Vladimir Marko | 8171fc3 | 2013-11-26 17:05:58 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | // The total size of the reference bit map including header. |
| 46 | size_t RawSize() const { |
| 47 | return EntryWidth() * NumEntries() + 4u /* header */; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | // The number of entries in the table |
| 51 | size_t NumEntries() const { |
| 52 | return GetData()[2] | (GetData()[3] << 8); |
| 53 | } |
| 54 | |
Ian Rogers | 46c6bb2 | 2012-09-18 13:47:36 -0700 | [diff] [blame] | 55 | // Get the Dex PC at the given index |
| 56 | uint16_t GetDexPc(size_t index) const { |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 57 | size_t entry_offset = index * EntryWidth(); |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 58 | if (DexPcWidth() == 1) { |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 59 | 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 Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 68 | return &Table()[entry_offset + DexPcWidth()]; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 69 | } |
| 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 Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 91 | size_t DexPcWidth() const { |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 92 | 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 Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 106 | return DexPcWidth() + RegWidth(); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | const uint8_t* GetData() const { |
| 110 | return data_; |
| 111 | } |
| 112 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 113 | static const int kRegMapFormatShift = 5; |
| 114 | static const uint8_t kRegMapFormatMask = 0x7; |
| 115 | |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 116 | const uint8_t* const data_; // The header and table data |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | } // namespace verifier |
| 120 | } // namespace art |
| 121 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 122 | #endif // ART_RUNTIME_VERIFIER_DEX_GC_MAP_H_ |