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 | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 27 | // Base helper class to prevent duplicated comparators. |
| 28 | class DexInstructionIteratorBase : public std::iterator<std::forward_iterator_tag, Instruction> { |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 29 | public: |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 30 | using value_type = std::iterator<std::forward_iterator_tag, Instruction>::value_type; |
| 31 | using difference_type = std::iterator<std::forward_iterator_tag, value_type>::difference_type; |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 32 | |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 33 | DexInstructionIteratorBase() = default; |
| 34 | explicit DexInstructionIteratorBase(const value_type* inst) : inst_(inst) {} |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 35 | |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 36 | const value_type* Inst() const { |
| 37 | return inst_; |
| 38 | } |
| 39 | |
| 40 | // Return the dex pc for an iterator compared to the code item begin. |
| 41 | uint32_t GetDexPC(const DexInstructionIteratorBase& code_item_begin) { |
| 42 | return reinterpret_cast<const uint16_t*>(inst_) - |
| 43 | reinterpret_cast<const uint16_t*>(code_item_begin.inst_); |
| 44 | } |
| 45 | |
| 46 | protected: |
| 47 | const value_type* inst_ = nullptr; |
| 48 | }; |
| 49 | |
| 50 | |
| 51 | static ALWAYS_INLINE inline bool operator==(const DexInstructionIteratorBase& lhs, |
| 52 | const DexInstructionIteratorBase& rhs) { |
| 53 | return lhs.Inst() == rhs.Inst(); |
| 54 | } |
| 55 | |
| 56 | static inline bool operator!=(const DexInstructionIteratorBase& lhs, |
| 57 | const DexInstructionIteratorBase& rhs) { |
| 58 | return !(lhs == rhs); |
| 59 | } |
| 60 | |
| 61 | static inline bool operator<(const DexInstructionIteratorBase& lhs, |
| 62 | const DexInstructionIteratorBase& rhs) { |
| 63 | return lhs.Inst() < rhs.Inst(); |
| 64 | } |
| 65 | |
| 66 | static inline bool operator>(const DexInstructionIteratorBase& lhs, |
| 67 | const DexInstructionIteratorBase& rhs) { |
| 68 | return rhs < lhs; |
| 69 | } |
| 70 | |
| 71 | static inline bool operator<=(const DexInstructionIteratorBase& lhs, |
| 72 | const DexInstructionIteratorBase& rhs) { |
| 73 | return !(rhs < lhs); |
| 74 | } |
| 75 | |
| 76 | static inline bool operator>=(const DexInstructionIteratorBase& lhs, |
| 77 | const DexInstructionIteratorBase& rhs) { |
| 78 | return !(lhs < rhs); |
| 79 | } |
| 80 | |
| 81 | class DexInstructionIterator : public DexInstructionIteratorBase { |
| 82 | public: |
| 83 | using value_type = std::iterator<std::forward_iterator_tag, Instruction>::value_type; |
| 84 | using difference_type = std::iterator<std::forward_iterator_tag, value_type>::difference_type; |
| 85 | using DexInstructionIteratorBase::DexInstructionIteratorBase; |
| 86 | |
| 87 | explicit DexInstructionIterator(const uint16_t* inst) |
| 88 | : DexInstructionIteratorBase(value_type::At(inst)) {} |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 89 | |
| 90 | // Value after modification. |
| 91 | DexInstructionIterator& operator++() { |
| 92 | inst_ = inst_->Next(); |
| 93 | return *this; |
| 94 | } |
| 95 | |
| 96 | // Value before modification. |
| 97 | DexInstructionIterator operator++(int) { |
| 98 | DexInstructionIterator temp = *this; |
| 99 | ++*this; |
| 100 | return temp; |
| 101 | } |
| 102 | |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 103 | const value_type& operator*() const { |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 104 | return *inst_; |
| 105 | } |
| 106 | |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 107 | const value_type* operator->() const { |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 108 | return &**this; |
| 109 | } |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 110 | }; |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 111 | |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 112 | class SafeDexInstructionIterator : public DexInstructionIteratorBase { |
| 113 | public: |
| 114 | explicit SafeDexInstructionIterator(const DexInstructionIteratorBase& start, |
| 115 | const DexInstructionIteratorBase& end) |
| 116 | : DexInstructionIteratorBase(start.Inst()) |
| 117 | , end_(end.Inst()) {} |
| 118 | |
| 119 | // Value after modification, does not read past the end of the allowed region. May increment past |
| 120 | // the end of the code item though. |
| 121 | SafeDexInstructionIterator& operator++() { |
| 122 | AssertValid(); |
| 123 | const size_t size_code_units = Inst()->CodeUnitsRequiredForSizeComputation(); |
| 124 | const size_t available = reinterpret_cast<const uint16_t*>(end_) - |
| 125 | reinterpret_cast<const uint16_t*>(Inst()); |
| 126 | if (size_code_units > available) { |
| 127 | error_state_ = true; |
| 128 | } else { |
| 129 | inst_ = inst_->Next(); |
| 130 | } |
| 131 | return *this; |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 134 | // Value before modification. |
| 135 | SafeDexInstructionIterator operator++(int) { |
| 136 | SafeDexInstructionIterator temp = *this; |
| 137 | ++*this; |
| 138 | return temp; |
| 139 | } |
| 140 | |
| 141 | const value_type& operator*() const { |
| 142 | AssertValid(); |
| 143 | return *inst_; |
| 144 | } |
| 145 | |
| 146 | const value_type* operator->() const { |
| 147 | AssertValid(); |
| 148 | return &**this; |
| 149 | } |
| 150 | |
| 151 | // Returns true if the iterator is in an error state. This occurs when an instruction couldn't |
| 152 | // have its size computed without reading past the end iterator. |
| 153 | bool IsErrorState() const { |
| 154 | return error_state_; |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | private: |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 158 | ALWAYS_INLINE void AssertValid() const { |
| 159 | DCHECK(!IsErrorState()); |
| 160 | DCHECK_LT(Inst(), end_); |
| 161 | } |
| 162 | |
| 163 | const value_type* end_ = nullptr; |
| 164 | bool error_state_ = false; |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 165 | }; |
| 166 | |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 167 | } // namespace art |
| 168 | |
| 169 | #endif // ART_RUNTIME_DEX_INSTRUCTION_ITERATOR_H_ |