xref: /aosp_15_r20/art/libdexfile/dex/bytecode_utils.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2016 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_LIBDEXFILE_DEX_BYTECODE_UTILS_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_LIBDEXFILE_DEX_BYTECODE_UTILS_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include "base/value_object.h"
21*795d594fSAndroid Build Coastguard Worker #include "dex/dex_file-inl.h"
22*795d594fSAndroid Build Coastguard Worker #include "dex/dex_file.h"
23*795d594fSAndroid Build Coastguard Worker #include "dex/dex_instruction-inl.h"
24*795d594fSAndroid Build Coastguard Worker 
25*795d594fSAndroid Build Coastguard Worker namespace art {
26*795d594fSAndroid Build Coastguard Worker 
27*795d594fSAndroid Build Coastguard Worker class DexSwitchTable : public ValueObject {
28*795d594fSAndroid Build Coastguard Worker  public:
DexSwitchTable(const Instruction & instruction,uint32_t dex_pc)29*795d594fSAndroid Build Coastguard Worker   DexSwitchTable(const Instruction& instruction, uint32_t dex_pc)
30*795d594fSAndroid Build Coastguard Worker       : instruction_(instruction),
31*795d594fSAndroid Build Coastguard Worker         dex_pc_(dex_pc),
32*795d594fSAndroid Build Coastguard Worker         sparse_(instruction.Opcode() == Instruction::SPARSE_SWITCH) {
33*795d594fSAndroid Build Coastguard Worker     int32_t table_offset = instruction.VRegB_31t();
34*795d594fSAndroid Build Coastguard Worker     const uint16_t* table = reinterpret_cast<const uint16_t*>(&instruction) + table_offset;
35*795d594fSAndroid Build Coastguard Worker     DCHECK_EQ(table[0], sparse_ ? static_cast<uint16_t>(Instruction::kSparseSwitchSignature)
36*795d594fSAndroid Build Coastguard Worker                                 : static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
37*795d594fSAndroid Build Coastguard Worker     num_entries_ = table[1];
38*795d594fSAndroid Build Coastguard Worker     values_ = reinterpret_cast<const int32_t*>(&table[2]);
39*795d594fSAndroid Build Coastguard Worker   }
40*795d594fSAndroid Build Coastguard Worker 
GetNumEntries()41*795d594fSAndroid Build Coastguard Worker   uint16_t GetNumEntries() const {
42*795d594fSAndroid Build Coastguard Worker     return num_entries_;
43*795d594fSAndroid Build Coastguard Worker   }
44*795d594fSAndroid Build Coastguard Worker 
CheckIndex(size_t index)45*795d594fSAndroid Build Coastguard Worker   void CheckIndex(size_t index) const {
46*795d594fSAndroid Build Coastguard Worker     if (sparse_) {
47*795d594fSAndroid Build Coastguard Worker       // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
48*795d594fSAndroid Build Coastguard Worker       DCHECK_LT(index, 2 * static_cast<size_t>(num_entries_));
49*795d594fSAndroid Build Coastguard Worker     } else {
50*795d594fSAndroid Build Coastguard Worker       // In a packed table, we have the starting key and num_entries_ values.
51*795d594fSAndroid Build Coastguard Worker       DCHECK_LT(index, 1 + static_cast<size_t>(num_entries_));
52*795d594fSAndroid Build Coastguard Worker     }
53*795d594fSAndroid Build Coastguard Worker   }
54*795d594fSAndroid Build Coastguard Worker 
GetEntryAt(size_t index)55*795d594fSAndroid Build Coastguard Worker   int32_t GetEntryAt(size_t index) const {
56*795d594fSAndroid Build Coastguard Worker     CheckIndex(index);
57*795d594fSAndroid Build Coastguard Worker     return values_[index];
58*795d594fSAndroid Build Coastguard Worker   }
59*795d594fSAndroid Build Coastguard Worker 
GetDexPcForIndex(size_t index)60*795d594fSAndroid Build Coastguard Worker   uint32_t GetDexPcForIndex(size_t index) const {
61*795d594fSAndroid Build Coastguard Worker     CheckIndex(index);
62*795d594fSAndroid Build Coastguard Worker     return dex_pc_ +
63*795d594fSAndroid Build Coastguard Worker         (reinterpret_cast<const int16_t*>(values_ + index) -
64*795d594fSAndroid Build Coastguard Worker          reinterpret_cast<const int16_t*>(&instruction_));
65*795d594fSAndroid Build Coastguard Worker   }
66*795d594fSAndroid Build Coastguard Worker 
67*795d594fSAndroid Build Coastguard Worker   // Index of the first value in the table.
GetFirstValueIndex()68*795d594fSAndroid Build Coastguard Worker   size_t GetFirstValueIndex() const {
69*795d594fSAndroid Build Coastguard Worker     if (sparse_) {
70*795d594fSAndroid Build Coastguard Worker       // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
71*795d594fSAndroid Build Coastguard Worker       return num_entries_;
72*795d594fSAndroid Build Coastguard Worker     } else {
73*795d594fSAndroid Build Coastguard Worker       // In a packed table, we have the starting key and num_entries_ values.
74*795d594fSAndroid Build Coastguard Worker       return 1;
75*795d594fSAndroid Build Coastguard Worker     }
76*795d594fSAndroid Build Coastguard Worker   }
77*795d594fSAndroid Build Coastguard Worker 
IsSparse()78*795d594fSAndroid Build Coastguard Worker   bool IsSparse() const { return sparse_; }
79*795d594fSAndroid Build Coastguard Worker 
ShouldBuildDecisionTree()80*795d594fSAndroid Build Coastguard Worker   bool ShouldBuildDecisionTree() {
81*795d594fSAndroid Build Coastguard Worker     return IsSparse() || GetNumEntries() <= kSmallSwitchThreshold;
82*795d594fSAndroid Build Coastguard Worker   }
83*795d594fSAndroid Build Coastguard Worker 
84*795d594fSAndroid Build Coastguard Worker  private:
85*795d594fSAndroid Build Coastguard Worker   const Instruction& instruction_;
86*795d594fSAndroid Build Coastguard Worker   const uint32_t dex_pc_;
87*795d594fSAndroid Build Coastguard Worker 
88*795d594fSAndroid Build Coastguard Worker   // Whether this is a sparse-switch table (or a packed-switch one).
89*795d594fSAndroid Build Coastguard Worker   const bool sparse_;
90*795d594fSAndroid Build Coastguard Worker 
91*795d594fSAndroid Build Coastguard Worker   // This can't be const as it needs to be computed off of the given instruction, and complicated
92*795d594fSAndroid Build Coastguard Worker   // expressions in the initializer list seemed very ugly.
93*795d594fSAndroid Build Coastguard Worker   uint16_t num_entries_;
94*795d594fSAndroid Build Coastguard Worker 
95*795d594fSAndroid Build Coastguard Worker   const int32_t* values_;
96*795d594fSAndroid Build Coastguard Worker 
97*795d594fSAndroid Build Coastguard Worker   // The number of entries in a packed switch before we use a jump table or specified
98*795d594fSAndroid Build Coastguard Worker   // compare/jump series.
99*795d594fSAndroid Build Coastguard Worker   static constexpr uint16_t kSmallSwitchThreshold = 3;
100*795d594fSAndroid Build Coastguard Worker 
101*795d594fSAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(DexSwitchTable);
102*795d594fSAndroid Build Coastguard Worker };
103*795d594fSAndroid Build Coastguard Worker 
104*795d594fSAndroid Build Coastguard Worker class DexSwitchTableIterator {
105*795d594fSAndroid Build Coastguard Worker  public:
DexSwitchTableIterator(const DexSwitchTable & table)106*795d594fSAndroid Build Coastguard Worker   explicit DexSwitchTableIterator(const DexSwitchTable& table)
107*795d594fSAndroid Build Coastguard Worker       : table_(table),
108*795d594fSAndroid Build Coastguard Worker         num_entries_(static_cast<size_t>(table_.GetNumEntries())),
109*795d594fSAndroid Build Coastguard Worker         first_target_offset_(table_.GetFirstValueIndex()),
110*795d594fSAndroid Build Coastguard Worker         index_(0u) {}
111*795d594fSAndroid Build Coastguard Worker 
Done()112*795d594fSAndroid Build Coastguard Worker   bool Done() const { return index_ >= num_entries_; }
IsLast()113*795d594fSAndroid Build Coastguard Worker   bool IsLast() const { return index_ == num_entries_ - 1; }
114*795d594fSAndroid Build Coastguard Worker 
Advance()115*795d594fSAndroid Build Coastguard Worker   void Advance() {
116*795d594fSAndroid Build Coastguard Worker     DCHECK(!Done());
117*795d594fSAndroid Build Coastguard Worker     index_++;
118*795d594fSAndroid Build Coastguard Worker   }
119*795d594fSAndroid Build Coastguard Worker 
CurrentKey()120*795d594fSAndroid Build Coastguard Worker   int32_t CurrentKey() const {
121*795d594fSAndroid Build Coastguard Worker     return table_.IsSparse() ? table_.GetEntryAt(index_) : table_.GetEntryAt(0) + index_;
122*795d594fSAndroid Build Coastguard Worker   }
123*795d594fSAndroid Build Coastguard Worker 
CurrentTargetOffset()124*795d594fSAndroid Build Coastguard Worker   int32_t CurrentTargetOffset() const {
125*795d594fSAndroid Build Coastguard Worker     return table_.GetEntryAt(index_ + first_target_offset_);
126*795d594fSAndroid Build Coastguard Worker   }
127*795d594fSAndroid Build Coastguard Worker 
GetDexPcForCurrentIndex()128*795d594fSAndroid Build Coastguard Worker   uint32_t GetDexPcForCurrentIndex() const { return table_.GetDexPcForIndex(index_); }
129*795d594fSAndroid Build Coastguard Worker 
130*795d594fSAndroid Build Coastguard Worker  private:
131*795d594fSAndroid Build Coastguard Worker   const DexSwitchTable& table_;
132*795d594fSAndroid Build Coastguard Worker   const size_t num_entries_;
133*795d594fSAndroid Build Coastguard Worker   const size_t first_target_offset_;
134*795d594fSAndroid Build Coastguard Worker 
135*795d594fSAndroid Build Coastguard Worker   size_t index_;
136*795d594fSAndroid Build Coastguard Worker };
137*795d594fSAndroid Build Coastguard Worker 
IsThrowingDexInstruction(const Instruction & instruction)138*795d594fSAndroid Build Coastguard Worker inline bool IsThrowingDexInstruction(const Instruction& instruction) {
139*795d594fSAndroid Build Coastguard Worker   // Special-case MONITOR_EXIT which is a throwing instruction but the verifier
140*795d594fSAndroid Build Coastguard Worker   // guarantees that it will never throw. This is necessary to avoid rejecting
141*795d594fSAndroid Build Coastguard Worker   // 'synchronized' blocks/methods.
142*795d594fSAndroid Build Coastguard Worker   return instruction.IsThrow() && instruction.Opcode() != Instruction::MONITOR_EXIT;
143*795d594fSAndroid Build Coastguard Worker }
144*795d594fSAndroid Build Coastguard Worker 
145*795d594fSAndroid Build Coastguard Worker }  // namespace art
146*795d594fSAndroid Build Coastguard Worker 
147*795d594fSAndroid Build Coastguard Worker #endif  // ART_LIBDEXFILE_DEX_BYTECODE_UTILS_H_
148