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 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 19 | #include <android-base/logging.h> |
| 20 | |
| 21 | #include "base/macros.h" |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 22 | #include "base/mutex.h" |
David Sehr | 312f3b2 | 2018-03-19 08:39:26 -0700 | [diff] [blame] | 23 | #include "dex/bytecode_utils.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 24 | #include "dex/code_item_accessors-inl.h" |
| 25 | #include "dex/dex_file-inl.h" |
| 26 | #include "dex/dex_instruction-inl.h" |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 27 | #include "quicken_info.h" |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | namespace optimizer { |
| 31 | |
| 32 | class DexDecompiler { |
| 33 | public: |
Mathieu Chartier | 6238c83 | 2018-01-04 09:55:13 -0800 | [diff] [blame] | 34 | DexDecompiler(const DexFile& dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 35 | const dex::CodeItem& code_item, |
Nicolas Geoffray | b1677e2 | 2016-12-16 16:23:16 +0000 | [diff] [blame] | 36 | const ArrayRef<const uint8_t>& quickened_info, |
| 37 | bool decompile_return_instruction) |
Mathieu Chartier | 698ebbc | 2018-01-05 11:00:42 -0800 | [diff] [blame] | 38 | : code_item_accessor_(dex_file, &code_item), |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 39 | quicken_info_(quickened_info), |
Nicolas Geoffray | b1677e2 | 2016-12-16 16:23:16 +0000 | [diff] [blame] | 40 | decompile_return_instruction_(decompile_return_instruction) {} |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 41 | |
| 42 | bool Decompile(); |
| 43 | |
| 44 | private: |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 45 | void DecompileInstanceFieldAccess(Instruction* inst, Instruction::Code new_opcode) { |
| 46 | uint16_t index = NextIndex(); |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 47 | inst->SetOpcode(new_opcode); |
| 48 | inst->SetVRegC_22c(index); |
| 49 | } |
| 50 | |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 51 | void DecompileInvokeVirtual(Instruction* inst, Instruction::Code new_opcode, bool is_range) { |
| 52 | const uint16_t index = NextIndex(); |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 53 | inst->SetOpcode(new_opcode); |
| 54 | if (is_range) { |
| 55 | inst->SetVRegB_3rc(index); |
| 56 | } else { |
| 57 | inst->SetVRegB_35c(index); |
| 58 | } |
| 59 | } |
| 60 | |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 61 | void DecompileNop(Instruction* inst) { |
| 62 | const uint16_t reference_index = NextIndex(); |
| 63 | if (reference_index == DexFile::kDexNoIndex16) { |
| 64 | // This means it was a normal nop and not a check-cast. |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 65 | return; |
| 66 | } |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 67 | const uint16_t type_index = NextIndex(); |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 68 | inst->SetOpcode(Instruction::CHECK_CAST); |
| 69 | inst->SetVRegA_21c(reference_index); |
| 70 | inst->SetVRegB_21c(type_index); |
| 71 | } |
| 72 | |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 73 | uint16_t NextIndex() { |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 74 | DCHECK_LT(quicken_index_, quicken_info_.NumIndices()); |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 75 | const uint16_t ret = quicken_info_.GetData(quicken_index_); |
| 76 | quicken_index_++; |
| 77 | return ret; |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Mathieu Chartier | 73f21d4 | 2018-01-02 14:26:50 -0800 | [diff] [blame] | 80 | const CodeItemInstructionAccessor code_item_accessor_; |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 81 | const QuickenInfoTable quicken_info_; |
Nicolas Geoffray | b1677e2 | 2016-12-16 16:23:16 +0000 | [diff] [blame] | 82 | const bool decompile_return_instruction_; |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 83 | |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 84 | size_t quicken_index_ = 0u; |
| 85 | |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 86 | DISALLOW_COPY_AND_ASSIGN(DexDecompiler); |
| 87 | }; |
| 88 | |
| 89 | bool DexDecompiler::Decompile() { |
| 90 | // We need to iterate over the code item, and not over the quickening data, |
| 91 | // because the RETURN_VOID quickening is not encoded in the quickening data. Because |
| 92 | // unquickening is a rare need and not performance sensitive, it is not worth the |
| 93 | // added storage to also add the RETURN_VOID quickening in the quickened data. |
Mathieu Chartier | 73f21d4 | 2018-01-02 14:26:50 -0800 | [diff] [blame] | 94 | for (const DexInstructionPcPair& pair : code_item_accessor_) { |
Mathieu Chartier | 0021feb | 2017-11-07 00:08:52 -0800 | [diff] [blame] | 95 | Instruction* inst = const_cast<Instruction*>(&pair.Inst()); |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 96 | |
| 97 | switch (inst->Opcode()) { |
| 98 | case Instruction::RETURN_VOID_NO_BARRIER: |
Nicolas Geoffray | b1677e2 | 2016-12-16 16:23:16 +0000 | [diff] [blame] | 99 | if (decompile_return_instruction_) { |
| 100 | inst->SetOpcode(Instruction::RETURN_VOID); |
| 101 | } |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 102 | break; |
| 103 | |
| 104 | case Instruction::NOP: |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 105 | if (quicken_info_.NumIndices() > 0) { |
Nicolas Geoffray | a55e268 | 2017-07-13 09:22:59 +0100 | [diff] [blame] | 106 | // Only try to decompile NOP if there are more than 0 indices. Not having |
| 107 | // any index happens when we unquicken a code item that only has |
| 108 | // RETURN_VOID_NO_BARRIER as quickened instruction. |
Mathieu Chartier | 36f549e | 2017-07-12 21:43:37 -0700 | [diff] [blame] | 109 | DecompileNop(inst); |
| 110 | } |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 111 | break; |
| 112 | |
| 113 | case Instruction::IGET_QUICK: |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 114 | DecompileInstanceFieldAccess(inst, Instruction::IGET); |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 115 | break; |
| 116 | |
| 117 | case Instruction::IGET_WIDE_QUICK: |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 118 | DecompileInstanceFieldAccess(inst, Instruction::IGET_WIDE); |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 119 | break; |
| 120 | |
| 121 | case Instruction::IGET_OBJECT_QUICK: |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 122 | DecompileInstanceFieldAccess(inst, Instruction::IGET_OBJECT); |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 123 | break; |
| 124 | |
| 125 | case Instruction::IGET_BOOLEAN_QUICK: |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 126 | DecompileInstanceFieldAccess(inst, Instruction::IGET_BOOLEAN); |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 127 | break; |
| 128 | |
| 129 | case Instruction::IGET_BYTE_QUICK: |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 130 | DecompileInstanceFieldAccess(inst, Instruction::IGET_BYTE); |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 131 | break; |
| 132 | |
| 133 | case Instruction::IGET_CHAR_QUICK: |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 134 | DecompileInstanceFieldAccess(inst, Instruction::IGET_CHAR); |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 135 | break; |
| 136 | |
| 137 | case Instruction::IGET_SHORT_QUICK: |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 138 | DecompileInstanceFieldAccess(inst, Instruction::IGET_SHORT); |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 139 | break; |
| 140 | |
| 141 | case Instruction::IPUT_QUICK: |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 142 | DecompileInstanceFieldAccess(inst, Instruction::IPUT); |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 143 | break; |
| 144 | |
| 145 | case Instruction::IPUT_BOOLEAN_QUICK: |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 146 | DecompileInstanceFieldAccess(inst, Instruction::IPUT_BOOLEAN); |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 147 | break; |
| 148 | |
| 149 | case Instruction::IPUT_BYTE_QUICK: |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 150 | DecompileInstanceFieldAccess(inst, Instruction::IPUT_BYTE); |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 151 | break; |
| 152 | |
| 153 | case Instruction::IPUT_CHAR_QUICK: |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 154 | DecompileInstanceFieldAccess(inst, Instruction::IPUT_CHAR); |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 155 | break; |
| 156 | |
| 157 | case Instruction::IPUT_SHORT_QUICK: |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 158 | DecompileInstanceFieldAccess(inst, Instruction::IPUT_SHORT); |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 159 | break; |
| 160 | |
| 161 | case Instruction::IPUT_WIDE_QUICK: |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 162 | DecompileInstanceFieldAccess(inst, Instruction::IPUT_WIDE); |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 163 | break; |
| 164 | |
| 165 | case Instruction::IPUT_OBJECT_QUICK: |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 166 | DecompileInstanceFieldAccess(inst, Instruction::IPUT_OBJECT); |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 167 | break; |
| 168 | |
| 169 | case Instruction::INVOKE_VIRTUAL_QUICK: |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 170 | DecompileInvokeVirtual(inst, Instruction::INVOKE_VIRTUAL, false); |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 171 | break; |
| 172 | |
| 173 | case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 174 | DecompileInvokeVirtual(inst, Instruction::INVOKE_VIRTUAL_RANGE, true); |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 175 | break; |
| 176 | |
| 177 | default: |
| 178 | break; |
| 179 | } |
| 180 | } |
| 181 | |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 182 | if (quicken_index_ != quicken_info_.NumIndices()) { |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 183 | if (quicken_index_ == 0) { |
Nicolas Geoffray | 095d6a6 | 2017-04-27 16:08:50 +0100 | [diff] [blame] | 184 | LOG(WARNING) << "Failed to use any value in quickening info," |
| 185 | << " potentially due to duplicate methods."; |
| 186 | } else { |
| 187 | LOG(FATAL) << "Failed to use all values in quickening info." |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 188 | << " Actual: " << std::hex << quicken_index_ |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 189 | << " Expected: " << quicken_info_.NumIndices(); |
Nicolas Geoffray | 095d6a6 | 2017-04-27 16:08:50 +0100 | [diff] [blame] | 190 | } |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | return true; |
| 194 | } |
| 195 | |
Mathieu Chartier | 6238c83 | 2018-01-04 09:55:13 -0800 | [diff] [blame] | 196 | bool ArtDecompileDEX(const DexFile& dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 197 | const dex::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 | } |
Mathieu Chartier | 73f21d4 | 2018-01-02 14:26:50 -0800 | [diff] [blame] | 203 | DexDecompiler decompiler(dex_file, 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 |