Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -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 | |
| 17 | #ifndef ART_RUNTIME_DEX_INSTRUCTION_ITERATOR_H_ |
| 18 | #define ART_RUNTIME_DEX_INSTRUCTION_ITERATOR_H_ |
| 19 | |
| 20 | #include <iterator> |
| 21 | |
| 22 | #include "dex_instruction.h" |
| 23 | #include "base/logging.h" |
| 24 | |
| 25 | namespace art { |
| 26 | |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 27 | class DexInstructionPcPair { |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 28 | public: |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 29 | ALWAYS_INLINE const Instruction& Inst() const { |
| 30 | return *Instruction::At(instructions_ + DexPc()); |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 33 | ALWAYS_INLINE const Instruction* operator->() const { |
| 34 | return &Inst(); |
| 35 | } |
| 36 | |
| 37 | ALWAYS_INLINE uint32_t DexPc() const { |
| 38 | return dex_pc_; |
| 39 | } |
| 40 | |
| 41 | ALWAYS_INLINE const uint16_t* Instructions() const { |
| 42 | return instructions_; |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | protected: |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 46 | explicit DexInstructionPcPair(const uint16_t* instructions, uint32_t dex_pc) |
| 47 | : instructions_(instructions), dex_pc_(dex_pc) {} |
| 48 | |
| 49 | const uint16_t* instructions_ = nullptr; |
| 50 | uint32_t dex_pc_ = 0; |
| 51 | |
| 52 | friend class DexInstructionIteratorBase; |
| 53 | friend class DexInstructionIterator; |
| 54 | friend class SafeDexInstructionIterator; |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 57 | // Base helper class to prevent duplicated comparators. |
| 58 | class DexInstructionIteratorBase : public |
| 59 | std::iterator<std::forward_iterator_tag, DexInstructionPcPair> { |
| 60 | public: |
| 61 | using value_type = std::iterator<std::forward_iterator_tag, DexInstructionPcPair>::value_type; |
| 62 | using difference_type = std::iterator<std::forward_iterator_tag, value_type>::difference_type; |
| 63 | |
| 64 | DexInstructionIteratorBase() = default; |
| 65 | explicit DexInstructionIteratorBase(const Instruction* inst, uint32_t dex_pc) |
| 66 | : data_(reinterpret_cast<const uint16_t*>(inst), dex_pc) {} |
| 67 | |
| 68 | const Instruction& Inst() const { |
| 69 | return data_.Inst(); |
| 70 | } |
| 71 | |
| 72 | // Return the dex pc for an iterator compared to the code item begin. |
| 73 | ALWAYS_INLINE uint32_t DexPc() const { |
| 74 | return data_.DexPc(); |
| 75 | } |
| 76 | |
| 77 | // Instructions from the start of the code item. |
| 78 | ALWAYS_INLINE const uint16_t* Instructions() const { |
| 79 | return data_.Instructions(); |
| 80 | } |
| 81 | |
| 82 | protected: |
| 83 | DexInstructionPcPair data_; |
| 84 | }; |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 85 | |
| 86 | static ALWAYS_INLINE inline bool operator==(const DexInstructionIteratorBase& lhs, |
| 87 | const DexInstructionIteratorBase& rhs) { |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 88 | DCHECK_EQ(lhs.Instructions(), rhs.Instructions()) << "Comparing different code items."; |
| 89 | return lhs.DexPc() == rhs.DexPc(); |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static inline bool operator!=(const DexInstructionIteratorBase& lhs, |
| 93 | const DexInstructionIteratorBase& rhs) { |
| 94 | return !(lhs == rhs); |
| 95 | } |
| 96 | |
| 97 | static inline bool operator<(const DexInstructionIteratorBase& lhs, |
| 98 | const DexInstructionIteratorBase& rhs) { |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 99 | DCHECK_EQ(lhs.Instructions(), rhs.Instructions()) << "Comparing different code items."; |
| 100 | return lhs.DexPc() < rhs.DexPc(); |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | static inline bool operator>(const DexInstructionIteratorBase& lhs, |
| 104 | const DexInstructionIteratorBase& rhs) { |
| 105 | return rhs < lhs; |
| 106 | } |
| 107 | |
| 108 | static inline bool operator<=(const DexInstructionIteratorBase& lhs, |
| 109 | const DexInstructionIteratorBase& rhs) { |
| 110 | return !(rhs < lhs); |
| 111 | } |
| 112 | |
| 113 | static inline bool operator>=(const DexInstructionIteratorBase& lhs, |
| 114 | const DexInstructionIteratorBase& rhs) { |
| 115 | return !(lhs < rhs); |
| 116 | } |
| 117 | |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 118 | // A helper class for a code_item's instructions using range based for loop syntax. |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 119 | class DexInstructionIterator : public DexInstructionIteratorBase { |
| 120 | public: |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 121 | using DexInstructionIteratorBase::DexInstructionIteratorBase; |
| 122 | |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 123 | explicit DexInstructionIterator(const uint16_t* inst, uint32_t dex_pc) |
| 124 | : DexInstructionIteratorBase(Instruction::At(inst), dex_pc) {} |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 125 | |
Mathieu Chartier | 0021feb | 2017-11-07 00:08:52 -0800 | [diff] [blame] | 126 | explicit DexInstructionIterator(const DexInstructionPcPair& pair) |
| 127 | : DexInstructionIterator(pair.Instructions(), pair.DexPc()) {} |
| 128 | |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 129 | // Value after modification. |
| 130 | DexInstructionIterator& operator++() { |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 131 | data_.dex_pc_ += Inst().SizeInCodeUnits(); |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 132 | return *this; |
| 133 | } |
| 134 | |
| 135 | // Value before modification. |
| 136 | DexInstructionIterator operator++(int) { |
| 137 | DexInstructionIterator temp = *this; |
| 138 | ++*this; |
| 139 | return temp; |
| 140 | } |
| 141 | |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 142 | const value_type& operator*() const { |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 143 | return data_; |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 146 | const Instruction* operator->() const { |
| 147 | return &data_.Inst(); |
| 148 | } |
| 149 | |
| 150 | // Return the dex pc for the iterator. |
| 151 | ALWAYS_INLINE uint32_t DexPc() const { |
| 152 | return data_.DexPc(); |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 153 | } |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 154 | }; |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 155 | |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 156 | // A safe version of DexInstructionIterator that is guaranteed to not go past the end of the code |
| 157 | // item. |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 158 | class SafeDexInstructionIterator : public DexInstructionIteratorBase { |
| 159 | public: |
| 160 | explicit SafeDexInstructionIterator(const DexInstructionIteratorBase& start, |
| 161 | const DexInstructionIteratorBase& end) |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 162 | : DexInstructionIteratorBase(&start.Inst(), start.DexPc()) |
| 163 | , num_code_units_(end.DexPc()) { |
| 164 | DCHECK_EQ(start.Instructions(), end.Instructions()) |
| 165 | << "start and end must be in the same code item."; |
| 166 | } |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 167 | |
| 168 | // Value after modification, does not read past the end of the allowed region. May increment past |
| 169 | // the end of the code item though. |
| 170 | SafeDexInstructionIterator& operator++() { |
| 171 | AssertValid(); |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 172 | const size_t size_code_units = Inst().CodeUnitsRequiredForSizeComputation(); |
| 173 | const size_t available = NumCodeUnits() - DexPc(); |
Mathieu Chartier | 176190c | 2017-10-31 09:58:07 -0700 | [diff] [blame] | 174 | if (UNLIKELY(size_code_units > available)) { |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 175 | error_state_ = true; |
Mathieu Chartier | 176190c | 2017-10-31 09:58:07 -0700 | [diff] [blame] | 176 | return *this; |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 177 | } |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 178 | const size_t instruction_code_units = Inst().SizeInCodeUnits(); |
| 179 | if (UNLIKELY(instruction_code_units > available)) { |
Mathieu Chartier | 176190c | 2017-10-31 09:58:07 -0700 | [diff] [blame] | 180 | error_state_ = true; |
| 181 | return *this; |
| 182 | } |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 183 | data_.dex_pc_ += instruction_code_units; |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 184 | return *this; |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 187 | // Value before modification. |
| 188 | SafeDexInstructionIterator operator++(int) { |
| 189 | SafeDexInstructionIterator temp = *this; |
| 190 | ++*this; |
| 191 | return temp; |
| 192 | } |
| 193 | |
| 194 | const value_type& operator*() const { |
| 195 | AssertValid(); |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 196 | return data_; |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 199 | const Instruction* operator->() const { |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 200 | AssertValid(); |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 201 | return &data_.Inst(); |
| 202 | } |
| 203 | |
| 204 | // Return the current instruction of the iterator. |
| 205 | ALWAYS_INLINE const Instruction& Inst() const { |
| 206 | return data_.Inst(); |
| 207 | } |
| 208 | |
| 209 | const uint16_t* Instructions() const { |
| 210 | return data_.Instructions(); |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | // Returns true if the iterator is in an error state. This occurs when an instruction couldn't |
| 214 | // have its size computed without reading past the end iterator. |
| 215 | bool IsErrorState() const { |
| 216 | return error_state_; |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | private: |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 220 | ALWAYS_INLINE void AssertValid() const { |
| 221 | DCHECK(!IsErrorState()); |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 222 | DCHECK_LT(DexPc(), NumCodeUnits()); |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 225 | ALWAYS_INLINE uint32_t NumCodeUnits() const { |
| 226 | return num_code_units_; |
| 227 | } |
| 228 | |
| 229 | const uint32_t num_code_units_ = 0; |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 230 | bool error_state_ = false; |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 231 | }; |
| 232 | |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 233 | } // namespace art |
| 234 | |
| 235 | #endif // ART_RUNTIME_DEX_INSTRUCTION_ITERATOR_H_ |