Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #include "dex_to_dex_decompiler.h" |
| 18 | |
| 19 | #include "base/logging.h" |
| 20 | #include "base/mutex.h" |
| 21 | #include "dex_file-inl.h" |
| 22 | #include "dex_instruction-inl.h" |
| 23 | #include "optimizing/bytecode_utils.h" |
| 24 | |
| 25 | namespace art { |
| 26 | namespace optimizer { |
| 27 | |
| 28 | class DexDecompiler { |
| 29 | public: |
Nicolas Geoffray | b1677e2 | 2016-12-16 16:23:16 +0000 | [diff] [blame] | 30 | DexDecompiler(const DexFile::CodeItem& code_item, |
| 31 | const ArrayRef<const uint8_t>& quickened_info, |
| 32 | bool decompile_return_instruction) |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 33 | : code_item_(code_item), |
| 34 | quickened_info_ptr_(quickened_info.data()), |
Nicolas Geoffray | b1677e2 | 2016-12-16 16:23:16 +0000 | [diff] [blame] | 35 | quickened_info_end_(quickened_info.data() + quickened_info.size()), |
| 36 | decompile_return_instruction_(decompile_return_instruction) {} |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 37 | |
| 38 | bool Decompile(); |
| 39 | |
| 40 | private: |
| 41 | void DecompileInstanceFieldAccess(Instruction* inst, |
| 42 | uint32_t dex_pc, |
| 43 | Instruction::Code new_opcode) { |
| 44 | uint16_t index = GetIndexAt(dex_pc); |
| 45 | inst->SetOpcode(new_opcode); |
| 46 | inst->SetVRegC_22c(index); |
| 47 | } |
| 48 | |
| 49 | void DecompileInvokeVirtual(Instruction* inst, |
| 50 | uint32_t dex_pc, |
| 51 | Instruction::Code new_opcode, |
| 52 | bool is_range) { |
| 53 | uint16_t index = GetIndexAt(dex_pc); |
| 54 | inst->SetOpcode(new_opcode); |
| 55 | if (is_range) { |
| 56 | inst->SetVRegB_3rc(index); |
| 57 | } else { |
| 58 | inst->SetVRegB_35c(index); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void DecompileNop(Instruction* inst, uint32_t dex_pc) { |
| 63 | if (quickened_info_ptr_ == quickened_info_end_) { |
| 64 | return; |
| 65 | } |
| 66 | const uint8_t* temporary_pointer = quickened_info_ptr_; |
| 67 | uint32_t quickened_pc = DecodeUnsignedLeb128(&temporary_pointer); |
| 68 | if (quickened_pc != dex_pc) { |
| 69 | return; |
| 70 | } |
| 71 | uint16_t reference_index = GetIndexAt(dex_pc); |
| 72 | uint16_t type_index = GetIndexAt(dex_pc); |
| 73 | inst->SetOpcode(Instruction::CHECK_CAST); |
| 74 | inst->SetVRegA_21c(reference_index); |
| 75 | inst->SetVRegB_21c(type_index); |
| 76 | } |
| 77 | |
| 78 | uint16_t GetIndexAt(uint32_t dex_pc) { |
| 79 | // Note that as a side effect, DecodeUnsignedLeb128 update the given pointer |
| 80 | // to the new position in the buffer. |
| 81 | DCHECK_LT(quickened_info_ptr_, quickened_info_end_); |
| 82 | uint32_t quickened_pc = DecodeUnsignedLeb128(&quickened_info_ptr_); |
| 83 | DCHECK_LT(quickened_info_ptr_, quickened_info_end_); |
| 84 | uint16_t index = DecodeUnsignedLeb128(&quickened_info_ptr_); |
| 85 | DCHECK_LE(quickened_info_ptr_, quickened_info_end_); |
| 86 | DCHECK_EQ(quickened_pc, dex_pc); |
| 87 | return index; |
| 88 | } |
| 89 | |
| 90 | const DexFile::CodeItem& code_item_; |
| 91 | const uint8_t* quickened_info_ptr_; |
| 92 | const uint8_t* const quickened_info_end_; |
Nicolas Geoffray | b1677e2 | 2016-12-16 16:23:16 +0000 | [diff] [blame] | 93 | const bool decompile_return_instruction_; |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 94 | |
| 95 | DISALLOW_COPY_AND_ASSIGN(DexDecompiler); |
| 96 | }; |
| 97 | |
| 98 | bool DexDecompiler::Decompile() { |
| 99 | // We need to iterate over the code item, and not over the quickening data, |
| 100 | // because the RETURN_VOID quickening is not encoded in the quickening data. Because |
| 101 | // unquickening is a rare need and not performance sensitive, it is not worth the |
| 102 | // added storage to also add the RETURN_VOID quickening in the quickened data. |
| 103 | for (CodeItemIterator it(code_item_); !it.Done(); it.Advance()) { |
| 104 | uint32_t dex_pc = it.CurrentDexPc(); |
| 105 | Instruction* inst = const_cast<Instruction*>(&it.CurrentInstruction()); |
| 106 | |
| 107 | switch (inst->Opcode()) { |
| 108 | case Instruction::RETURN_VOID_NO_BARRIER: |
Nicolas Geoffray | b1677e2 | 2016-12-16 16:23:16 +0000 | [diff] [blame] | 109 | if (decompile_return_instruction_) { |
| 110 | inst->SetOpcode(Instruction::RETURN_VOID); |
| 111 | } |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 112 | break; |
| 113 | |
| 114 | case Instruction::NOP: |
| 115 | DecompileNop(inst, dex_pc); |
| 116 | break; |
| 117 | |
| 118 | case Instruction::IGET_QUICK: |
| 119 | DecompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET); |
| 120 | break; |
| 121 | |
| 122 | case Instruction::IGET_WIDE_QUICK: |
| 123 | DecompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_WIDE); |
| 124 | break; |
| 125 | |
| 126 | case Instruction::IGET_OBJECT_QUICK: |
| 127 | DecompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_OBJECT); |
| 128 | break; |
| 129 | |
| 130 | case Instruction::IGET_BOOLEAN_QUICK: |
| 131 | DecompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_BOOLEAN); |
| 132 | break; |
| 133 | |
| 134 | case Instruction::IGET_BYTE_QUICK: |
| 135 | DecompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_BYTE); |
| 136 | break; |
| 137 | |
| 138 | case Instruction::IGET_CHAR_QUICK: |
| 139 | DecompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_CHAR); |
| 140 | break; |
| 141 | |
| 142 | case Instruction::IGET_SHORT_QUICK: |
| 143 | DecompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_SHORT); |
| 144 | break; |
| 145 | |
| 146 | case Instruction::IPUT_QUICK: |
| 147 | DecompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT); |
| 148 | break; |
| 149 | |
| 150 | case Instruction::IPUT_BOOLEAN_QUICK: |
| 151 | DecompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_BOOLEAN); |
| 152 | break; |
| 153 | |
| 154 | case Instruction::IPUT_BYTE_QUICK: |
| 155 | DecompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_BYTE); |
| 156 | break; |
| 157 | |
| 158 | case Instruction::IPUT_CHAR_QUICK: |
| 159 | DecompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_CHAR); |
| 160 | break; |
| 161 | |
| 162 | case Instruction::IPUT_SHORT_QUICK: |
| 163 | DecompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_SHORT); |
| 164 | break; |
| 165 | |
| 166 | case Instruction::IPUT_WIDE_QUICK: |
| 167 | DecompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_WIDE); |
| 168 | break; |
| 169 | |
| 170 | case Instruction::IPUT_OBJECT_QUICK: |
| 171 | DecompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_OBJECT); |
| 172 | break; |
| 173 | |
| 174 | case Instruction::INVOKE_VIRTUAL_QUICK: |
| 175 | DecompileInvokeVirtual(inst, dex_pc, Instruction::INVOKE_VIRTUAL, false); |
| 176 | break; |
| 177 | |
| 178 | case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: |
| 179 | DecompileInvokeVirtual(inst, dex_pc, Instruction::INVOKE_VIRTUAL_RANGE, true); |
| 180 | break; |
| 181 | |
| 182 | default: |
| 183 | break; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | if (quickened_info_ptr_ != quickened_info_end_) { |
| 188 | LOG(ERROR) << "Failed to use all values in quickening info." |
| 189 | << " Actual: " << std::hex << quickened_info_ptr_ |
| 190 | << " Expected: " << quickened_info_end_; |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | return true; |
| 195 | } |
| 196 | |
| 197 | bool ArtDecompileDEX(const DexFile::CodeItem& code_item, |
Nicolas Geoffray | b1677e2 | 2016-12-16 16:23:16 +0000 | [diff] [blame] | 198 | const ArrayRef<const uint8_t>& quickened_info, |
| 199 | bool decompile_return_instruction) { |
| 200 | if (quickened_info.size() == 0 && !decompile_return_instruction) { |
| 201 | return true; |
| 202 | } |
| 203 | DexDecompiler decompiler(code_item, quickened_info, decompile_return_instruction); |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 204 | return decompiler.Decompile(); |
| 205 | } |
| 206 | |
| 207 | } // namespace optimizer |
| 208 | } // namespace art |