1 /*
2 * Copyright (C) 2015 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 "stack_map.h"
18
19 #include <iomanip>
20 #include <stdint.h>
21
22 #include "art_method.h"
23 #include "base/indenter.h"
24 #include "base/stats.h"
25 #include "oat_quick_method_header.h"
26 #include "scoped_thread_state_change-inl.h"
27
28 namespace art {
29
30 // The callback is used to inform the caller about memory bounds of the bit-tables.
31 template<typename DecodeCallback>
CodeInfo(const uint8_t * data,size_t * num_read_bits,DecodeCallback callback)32 CodeInfo::CodeInfo(const uint8_t* data, size_t* num_read_bits, DecodeCallback callback) {
33 BitMemoryReader reader(data);
34 std::array<uint32_t, kNumHeaders> header = reader.ReadInterleavedVarints<kNumHeaders>();
35 ForEachHeaderField([this, &header](size_t i, auto member_pointer) {
36 this->*member_pointer = header[i];
37 });
38 ForEachBitTableField([this, &reader, &callback](size_t i, auto member_pointer) {
39 auto& table = this->*member_pointer;
40 if (LIKELY(HasBitTable(i))) {
41 if (UNLIKELY(IsBitTableDeduped(i))) {
42 ssize_t bit_offset = reader.NumberOfReadBits() - reader.ReadVarint();
43 BitMemoryReader reader2(reader.data(), bit_offset); // The offset is negative.
44 table.Decode(reader2);
45 callback(i, &table, reader2.GetReadRegion());
46 } else {
47 ssize_t bit_offset = reader.NumberOfReadBits();
48 table.Decode(reader);
49 callback(i, &table, reader.GetReadRegion().Subregion(bit_offset));
50 }
51 }
52 });
53 if (num_read_bits != nullptr) {
54 *num_read_bits = reader.NumberOfReadBits();
55 }
56 }
57
CodeInfo(const uint8_t * data,size_t * num_read_bits)58 CodeInfo::CodeInfo(const uint8_t* data, size_t* num_read_bits)
59 : CodeInfo(data, num_read_bits, [](size_t, auto*, BitMemoryRegion){}) {}
60
CodeInfo(const OatQuickMethodHeader * header)61 CodeInfo::CodeInfo(const OatQuickMethodHeader* header)
62 : CodeInfo(header->GetOptimizedCodeInfoPtr()) {}
63
DecodeFrameInfo(const uint8_t * data)64 QuickMethodFrameInfo CodeInfo::DecodeFrameInfo(const uint8_t* data) {
65 CodeInfo code_info(data);
66 return QuickMethodFrameInfo(code_info.packed_frame_size_ * kStackAlignment,
67 code_info.core_spill_mask_,
68 code_info.fp_spill_mask_);
69 }
70
DecodeGcMasksOnly(const OatQuickMethodHeader * header)71 CodeInfo CodeInfo::DecodeGcMasksOnly(const OatQuickMethodHeader* header) {
72 CodeInfo code_info(header->GetOptimizedCodeInfoPtr());
73 CodeInfo copy; // Copy to dead-code-eliminate all fields that we do not need.
74 copy.stack_maps_ = code_info.stack_maps_;
75 copy.register_masks_ = code_info.register_masks_;
76 copy.stack_masks_ = code_info.stack_masks_;
77 return copy;
78 }
79
DecodeInlineInfoOnly(const OatQuickMethodHeader * header)80 CodeInfo CodeInfo::DecodeInlineInfoOnly(const OatQuickMethodHeader* header) {
81 CodeInfo code_info(header->GetOptimizedCodeInfoPtr());
82 CodeInfo copy; // Copy to dead-code-eliminate all fields that we do not need.
83 copy.number_of_dex_registers_ = code_info.number_of_dex_registers_;
84 copy.stack_maps_ = code_info.stack_maps_;
85 copy.inline_infos_ = code_info.inline_infos_;
86 copy.method_infos_ = code_info.method_infos_;
87 return copy;
88 }
89
Dedupe(const uint8_t * code_info_data)90 size_t CodeInfo::Deduper::Dedupe(const uint8_t* code_info_data) {
91 writer_.ByteAlign();
92 size_t deduped_offset = writer_.NumberOfWrittenBits() / kBitsPerByte;
93
94 // The back-reference offset takes space so dedupe is not worth it for tiny tables.
95 constexpr size_t kMinDedupSize = 32; // Assume 32-bit offset on average.
96
97 // Read the existing code info and find (and keep) dedup-map iterator for each table.
98 // The iterator stores BitMemoryRegion and bit_offset of previous identical BitTable.
99 std::map<BitMemoryRegion, uint32_t, BitMemoryRegion::Less>::iterator it[kNumBitTables];
100 CodeInfo code_info(code_info_data, nullptr, [&](size_t i, auto*, BitMemoryRegion region) {
101 it[i] = dedupe_map_.emplace(region, /*bit_offset=*/0).first;
102 if (it[i]->second != 0 && region.size_in_bits() > kMinDedupSize) { // Seen before and large?
103 code_info.SetBitTableDeduped(i); // Mark as deduped before we write header.
104 }
105 });
106
107 // Write the code info back, but replace deduped tables with relative offsets.
108 std::array<uint32_t, kNumHeaders> header;
109 ForEachHeaderField([&code_info, &header](size_t i, auto member_pointer) {
110 header[i] = code_info.*member_pointer;
111 });
112 writer_.WriteInterleavedVarints(header);
113 ForEachBitTableField([this, &code_info, &it](size_t i, auto) {
114 if (code_info.HasBitTable(i)) {
115 uint32_t& bit_offset = it[i]->second;
116 if (code_info.IsBitTableDeduped(i)) {
117 DCHECK_NE(bit_offset, 0u);
118 writer_.WriteVarint(writer_.NumberOfWrittenBits() - bit_offset);
119 } else {
120 bit_offset = writer_.NumberOfWrittenBits(); // Store offset in dedup map.
121 writer_.WriteRegion(it[i]->first);
122 }
123 }
124 });
125
126 if (kIsDebugBuild) {
127 CodeInfo old_code_info(code_info_data);
128 CodeInfo new_code_info(writer_.data() + deduped_offset);
129 ForEachHeaderField([&old_code_info, &new_code_info](size_t, auto member_pointer) {
130 if (member_pointer != &CodeInfo::bit_table_flags_) { // Expected to differ.
131 DCHECK_EQ(old_code_info.*member_pointer, new_code_info.*member_pointer);
132 }
133 });
134 ForEachBitTableField([&old_code_info, &new_code_info](size_t i, auto member_pointer) {
135 DCHECK_EQ(old_code_info.HasBitTable(i), new_code_info.HasBitTable(i));
136 DCHECK((old_code_info.*member_pointer).Equals(new_code_info.*member_pointer));
137 });
138 }
139
140 return deduped_offset;
141 }
142
GetStackMapForNativePcOffset(uint32_t pc,InstructionSet isa) const143 StackMap CodeInfo::GetStackMapForNativePcOffset(uint32_t pc, InstructionSet isa) const {
144 uint32_t packed_pc = StackMap::PackNativePc(pc, isa);
145 // Binary search. All catch stack maps are stored separately at the end.
146 auto it = std::partition_point(
147 stack_maps_.begin(),
148 stack_maps_.end(),
149 [packed_pc](const StackMap& sm) {
150 return sm.GetPackedNativePc() < packed_pc && sm.GetKind() != StackMap::Kind::Catch;
151 });
152 // Start at the lower bound and iterate over all stack maps with the given native pc.
153 for (; it != stack_maps_.end() && (*it).GetNativePcOffset(isa) == pc; ++it) {
154 StackMap::Kind kind = static_cast<StackMap::Kind>((*it).GetKind());
155 if (kind == StackMap::Kind::Default || kind == StackMap::Kind::OSR) {
156 return *it;
157 }
158 }
159 return stack_maps_.GetInvalidRow();
160 }
161
162 // Scan backward to determine dex register locations at given stack map.
163 // All registers for a stack map are combined - inlined registers are just appended,
164 // therefore 'first_dex_register' allows us to select a sub-range to decode.
DecodeDexRegisterMap(uint32_t stack_map_index,uint32_t first_dex_register,DexRegisterMap * map) const165 void CodeInfo::DecodeDexRegisterMap(uint32_t stack_map_index,
166 uint32_t first_dex_register,
167 /*out*/ DexRegisterMap* map) const {
168 // Count remaining work so we know when we have finished.
169 uint32_t remaining_registers = map->size();
170
171 // Keep scanning backwards and collect the most recent location of each register.
172 for (int32_t s = stack_map_index; s >= 0 && remaining_registers != 0; s--) {
173 StackMap stack_map = GetStackMapAt(s);
174 DCHECK_LE(stack_map_index - s, kMaxDexRegisterMapSearchDistance) << "Unbounded search";
175
176 // The mask specifies which registers where modified in this stack map.
177 // NB: the mask can be shorter than expected if trailing zero bits were removed.
178 uint32_t mask_index = stack_map.GetDexRegisterMaskIndex();
179 if (mask_index == StackMap::kNoValue) {
180 continue; // Nothing changed at this stack map.
181 }
182 BitMemoryRegion mask = dex_register_masks_.GetBitMemoryRegion(mask_index);
183 if (mask.size_in_bits() <= first_dex_register) {
184 continue; // Nothing changed after the first register we are interested in.
185 }
186
187 // The map stores one catalogue index per each modified register location.
188 uint32_t map_index = stack_map.GetDexRegisterMapIndex();
189 DCHECK_NE(map_index, StackMap::kNoValue);
190
191 // Skip initial registers which we are not interested in (to get to inlined registers).
192 map_index += mask.PopCount(0, first_dex_register);
193 mask = mask.Subregion(first_dex_register, mask.size_in_bits() - first_dex_register);
194
195 // Update registers that we see for first time (i.e. most recent value).
196 DexRegisterLocation* regs = map->data();
197 const uint32_t end = std::min<uint32_t>(map->size(), mask.size_in_bits());
198 const size_t kNumBits = BitSizeOf<uint32_t>();
199 for (uint32_t reg = 0; reg < end; reg += kNumBits) {
200 // Process the mask in chunks of kNumBits for performance.
201 uint32_t bits = mask.LoadBits(reg, std::min<uint32_t>(end - reg, kNumBits));
202 while (bits != 0) {
203 uint32_t bit = CTZ(bits);
204 if (regs[reg + bit].GetKind() == DexRegisterLocation::Kind::kInvalid) {
205 regs[reg + bit] = GetDexRegisterCatalogEntry(dex_register_maps_.Get(map_index));
206 remaining_registers--;
207 }
208 map_index++;
209 bits ^= 1u << bit; // Clear the bit.
210 }
211 }
212 }
213
214 // Set any remaining registers to None (which is the default state at first stack map).
215 if (remaining_registers != 0) {
216 DexRegisterLocation* regs = map->data();
217 for (uint32_t r = 0; r < map->size(); r++) {
218 if (regs[r].GetKind() == DexRegisterLocation::Kind::kInvalid) {
219 regs[r] = DexRegisterLocation::None();
220 }
221 }
222 }
223 }
224
225 // Decode the CodeInfo while collecting size statistics.
CollectSizeStats(const uint8_t * code_info_data,Stats * parent)226 void CodeInfo::CollectSizeStats(const uint8_t* code_info_data, /*out*/ Stats* parent) {
227 Stats* codeinfo_stats = parent->Child("CodeInfo");
228 BitMemoryReader reader(code_info_data);
229 reader.ReadInterleavedVarints<kNumHeaders>();
230 codeinfo_stats->Child("Header")->AddBits(reader.NumberOfReadBits());
231 size_t num_bits;
232 CodeInfo code_info(code_info_data, &num_bits, [&](size_t i, auto* table, BitMemoryRegion region) {
233 if (!code_info.IsBitTableDeduped(i)) {
234 Stats* table_stats = codeinfo_stats->Child(table->GetName());
235 table_stats->AddBits(region.size_in_bits());
236 const char* const* column_names = table->GetColumnNames();
237 for (size_t c = 0; c < table->NumColumns(); c++) {
238 if (table->NumColumnBits(c) > 0) {
239 Stats* column_stats = table_stats->Child(column_names[c]);
240 column_stats->AddBits(table->NumRows() * table->NumColumnBits(c), table->NumRows());
241 }
242 }
243 }
244 });
245 codeinfo_stats->AddBytes(BitsToBytesRoundUp(num_bits));
246 }
247
Dump(VariableIndentationOutputStream * vios) const248 void DexRegisterMap::Dump(VariableIndentationOutputStream* vios) const {
249 if (HasAnyLiveDexRegisters()) {
250 ScopedIndentation indent1(vios);
251 for (size_t i = 0; i < size(); ++i) {
252 DexRegisterLocation reg = (*this)[i];
253 if (reg.IsLive()) {
254 vios->Stream() << "v" << i << ":" << reg << " ";
255 }
256 }
257 vios->Stream() << "\n";
258 }
259 }
260
Dump(VariableIndentationOutputStream * vios,uint32_t code_offset,bool verbose,InstructionSet instruction_set) const261 void CodeInfo::Dump(VariableIndentationOutputStream* vios,
262 uint32_t code_offset,
263 bool verbose,
264 InstructionSet instruction_set) const {
265 vios->Stream() << "CodeInfo "
266 << " FrameSize:" << packed_frame_size_ * kStackAlignment
267 << " CoreSpillMask:" << std::hex << core_spill_mask_
268 << " FpSpillMask:" << std::hex << fp_spill_mask_
269 << " NumberOfDexRegisters:" << std::dec << number_of_dex_registers_
270 << "\n";
271 ScopedIndentation indent1(vios);
272 ForEachBitTableField([this, &vios, verbose](size_t, auto member_pointer) {
273 const auto& table = this->*member_pointer;
274 if (table.NumRows() != 0) {
275 vios->Stream() << table.GetName() << " BitSize=" << table.DataBitSize();
276 vios->Stream() << " Rows=" << table.NumRows() << " Bits={";
277 const char* const* column_names = table.GetColumnNames();
278 for (size_t c = 0; c < table.NumColumns(); c++) {
279 vios->Stream() << (c != 0 ? " " : "");
280 vios->Stream() << column_names[c] << "=" << table.NumColumnBits(c);
281 }
282 vios->Stream() << "}\n";
283 if (verbose) {
284 ScopedIndentation indent1(vios);
285 for (size_t r = 0; r < table.NumRows(); r++) {
286 vios->Stream() << "[" << std::right << std::setw(3) << r << "]={";
287 for (size_t c = 0; c < table.NumColumns(); c++) {
288 vios->Stream() << (c != 0 ? " " : "");
289 if (&table == static_cast<const void*>(&stack_masks_) ||
290 &table == static_cast<const void*>(&dex_register_masks_)) {
291 BitMemoryRegion bits = table.GetBitMemoryRegion(r, c);
292 for (size_t b = 0, e = bits.size_in_bits(); b < e; b++) {
293 vios->Stream() << bits.LoadBit(e - b - 1);
294 }
295 } else {
296 vios->Stream() << std::right << std::setw(8) << static_cast<int32_t>(table.Get(r, c));
297 }
298 }
299 vios->Stream() << "}\n";
300 }
301 }
302 }
303 });
304
305 // Display stack maps along with (live) Dex register maps.
306 if (verbose) {
307 for (StackMap stack_map : stack_maps_) {
308 stack_map.Dump(vios, *this, code_offset, instruction_set);
309 }
310 }
311 }
312
Dump(VariableIndentationOutputStream * vios,const CodeInfo & code_info,uint32_t code_offset,InstructionSet instruction_set) const313 void StackMap::Dump(VariableIndentationOutputStream* vios,
314 const CodeInfo& code_info,
315 uint32_t code_offset,
316 InstructionSet instruction_set) const {
317 const uint32_t pc_offset = GetNativePcOffset(instruction_set);
318 vios->Stream()
319 << "StackMap[" << Row() << "]"
320 << std::hex
321 << " (native_pc=0x" << code_offset + pc_offset
322 << ", dex_pc=0x" << GetDexPc()
323 << ", register_mask=0x" << code_info.GetRegisterMaskOf(*this)
324 << std::dec
325 << ", stack_mask=0b";
326 BitMemoryRegion stack_mask = code_info.GetStackMaskOf(*this);
327 for (size_t i = 0, e = stack_mask.size_in_bits(); i < e; ++i) {
328 vios->Stream() << stack_mask.LoadBit(e - i - 1);
329 }
330 vios->Stream() << ")\n";
331 code_info.GetDexRegisterMapOf(*this).Dump(vios);
332 for (InlineInfo inline_info : code_info.GetInlineInfosOf(*this)) {
333 inline_info.Dump(vios, code_info, *this);
334 }
335 }
336
Dump(VariableIndentationOutputStream * vios,const CodeInfo & code_info,const StackMap & stack_map) const337 void InlineInfo::Dump(VariableIndentationOutputStream* vios,
338 const CodeInfo& code_info,
339 const StackMap& stack_map) const {
340 uint32_t depth = Row() - stack_map.GetInlineInfoIndex();
341 vios->Stream()
342 << "InlineInfo[" << Row() << "]"
343 << " (depth=" << depth
344 << std::hex
345 << ", dex_pc=0x" << GetDexPc();
346 if (EncodesArtMethod()) {
347 ScopedObjectAccess soa(Thread::Current());
348 vios->Stream() << ", method=" << GetArtMethod()->PrettyMethod();
349 } else {
350 vios->Stream()
351 << std::dec
352 << ", method_index=" << code_info.GetMethodIndexOf(*this);
353 }
354 vios->Stream() << ")\n";
355 code_info.GetInlineDexRegisterMapOf(stack_map, *this).Dump(vios);
356 }
357
358 } // namespace art
359