stack.cc revision b331febbab8e916680faba722cc84b66b84218a3
1/* 2 * Copyright (C) 2011 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.h" 18 19#include "arch/context.h" 20#include "art_method-inl.h" 21#include "base/hex_dump.h" 22#include "entrypoints/entrypoint_utils-inl.h" 23#include "entrypoints/runtime_asm_entrypoints.h" 24#include "gc_map.h" 25#include "gc/space/image_space.h" 26#include "gc/space/space-inl.h" 27#include "jit/jit.h" 28#include "jit/jit_code_cache.h" 29#include "linear_alloc.h" 30#include "mirror/class-inl.h" 31#include "mirror/object-inl.h" 32#include "mirror/object_array-inl.h" 33#include "oat_quick_method_header.h" 34#include "quick/quick_method_frame_info.h" 35#include "runtime.h" 36#include "thread.h" 37#include "thread_list.h" 38#include "verify_object-inl.h" 39#include "vmap_table.h" 40 41namespace art { 42 43static constexpr bool kDebugStackWalk = true; 44 45mirror::Object* ShadowFrame::GetThisObject() const { 46 ArtMethod* m = GetMethod(); 47 if (m->IsStatic()) { 48 return nullptr; 49 } else if (m->IsNative()) { 50 return GetVRegReference(0); 51 } else { 52 const DexFile::CodeItem* code_item = m->GetCodeItem(); 53 CHECK(code_item != nullptr) << PrettyMethod(m); 54 uint16_t reg = code_item->registers_size_ - code_item->ins_size_; 55 return GetVRegReference(reg); 56 } 57} 58 59mirror::Object* ShadowFrame::GetThisObject(uint16_t num_ins) const { 60 ArtMethod* m = GetMethod(); 61 if (m->IsStatic()) { 62 return nullptr; 63 } else { 64 return GetVRegReference(NumberOfVRegs() - num_ins); 65 } 66} 67 68size_t ManagedStack::NumJniShadowFrameReferences() const { 69 size_t count = 0; 70 for (const ManagedStack* current_fragment = this; current_fragment != nullptr; 71 current_fragment = current_fragment->GetLink()) { 72 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != nullptr; 73 current_frame = current_frame->GetLink()) { 74 if (current_frame->GetMethod()->IsNative()) { 75 // The JNI ShadowFrame only contains references. (For indirect reference.) 76 count += current_frame->NumberOfVRegs(); 77 } 78 } 79 } 80 return count; 81} 82 83bool ManagedStack::ShadowFramesContain(StackReference<mirror::Object>* shadow_frame_entry) const { 84 for (const ManagedStack* current_fragment = this; current_fragment != nullptr; 85 current_fragment = current_fragment->GetLink()) { 86 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != nullptr; 87 current_frame = current_frame->GetLink()) { 88 if (current_frame->Contains(shadow_frame_entry)) { 89 return true; 90 } 91 } 92 } 93 return false; 94} 95 96StackVisitor::StackVisitor(Thread* thread, Context* context, StackWalkKind walk_kind) 97 : StackVisitor(thread, context, walk_kind, 0) {} 98 99StackVisitor::StackVisitor(Thread* thread, 100 Context* context, 101 StackWalkKind walk_kind, 102 size_t num_frames) 103 : thread_(thread), 104 walk_kind_(walk_kind), 105 cur_shadow_frame_(nullptr), 106 cur_quick_frame_(nullptr), 107 cur_quick_frame_pc_(0), 108 cur_oat_quick_method_header_(nullptr), 109 num_frames_(num_frames), 110 cur_depth_(0), 111 current_inlining_depth_(0), 112 context_(context) { 113 DCHECK(thread == Thread::Current() || thread->IsSuspended()) << *thread; 114} 115 116InlineInfo StackVisitor::GetCurrentInlineInfo() const { 117 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader(); 118 uint32_t native_pc_offset = method_header->NativeQuickPcOffset(cur_quick_frame_pc_); 119 CodeInfo code_info = method_header->GetOptimizedCodeInfo(); 120 StackMapEncoding encoding = code_info.ExtractEncoding(); 121 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding); 122 DCHECK(stack_map.IsValid()); 123 return code_info.GetInlineInfoOf(stack_map, encoding); 124} 125 126ArtMethod* StackVisitor::GetMethod() const { 127 if (cur_shadow_frame_ != nullptr) { 128 return cur_shadow_frame_->GetMethod(); 129 } else if (cur_quick_frame_ != nullptr) { 130 if (IsInInlinedFrame()) { 131 size_t depth_in_stack_map = current_inlining_depth_ - 1; 132 InlineInfo inline_info = GetCurrentInlineInfo(); 133 return GetResolvedMethod(*GetCurrentQuickFrame(), inline_info, depth_in_stack_map); 134 } else { 135 return *cur_quick_frame_; 136 } 137 } 138 return nullptr; 139} 140 141uint32_t StackVisitor::GetDexPc(bool abort_on_failure) const { 142 if (cur_shadow_frame_ != nullptr) { 143 return cur_shadow_frame_->GetDexPC(); 144 } else if (cur_quick_frame_ != nullptr) { 145 if (IsInInlinedFrame()) { 146 size_t depth_in_stack_map = current_inlining_depth_ - 1; 147 return GetCurrentInlineInfo().GetDexPcAtDepth(depth_in_stack_map); 148 } else if (cur_oat_quick_method_header_ == nullptr) { 149 return DexFile::kDexNoIndex; 150 } else { 151 return cur_oat_quick_method_header_->ToDexPc( 152 GetMethod(), cur_quick_frame_pc_, abort_on_failure); 153 } 154 } else { 155 return 0; 156 } 157} 158 159extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp) 160 SHARED_REQUIRES(Locks::mutator_lock_); 161 162mirror::Object* StackVisitor::GetThisObject() const { 163 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), sizeof(void*)); 164 ArtMethod* m = GetMethod(); 165 if (m->IsStatic()) { 166 return nullptr; 167 } else if (m->IsNative()) { 168 if (cur_quick_frame_ != nullptr) { 169 HandleScope* hs = reinterpret_cast<HandleScope*>( 170 reinterpret_cast<char*>(cur_quick_frame_) + sizeof(ArtMethod*)); 171 return hs->GetReference(0); 172 } else { 173 return cur_shadow_frame_->GetVRegReference(0); 174 } 175 } else if (m->IsProxyMethod()) { 176 if (cur_quick_frame_ != nullptr) { 177 return artQuickGetProxyThisObject(cur_quick_frame_); 178 } else { 179 return cur_shadow_frame_->GetVRegReference(0); 180 } 181 } else { 182 const DexFile::CodeItem* code_item = m->GetCodeItem(); 183 if (code_item == nullptr) { 184 UNIMPLEMENTED(ERROR) << "Failed to determine this object of abstract or proxy method: " 185 << PrettyMethod(m); 186 return nullptr; 187 } else { 188 uint16_t reg = code_item->registers_size_ - code_item->ins_size_; 189 uint32_t value = 0; 190 bool success = GetVReg(m, reg, kReferenceVReg, &value); 191 // We currently always guarantee the `this` object is live throughout the method. 192 CHECK(success) << "Failed to read the this object in " << PrettyMethod(m); 193 return reinterpret_cast<mirror::Object*>(value); 194 } 195 } 196} 197 198size_t StackVisitor::GetNativePcOffset() const { 199 DCHECK(!IsShadowFrame()); 200 return GetCurrentOatQuickMethodHeader()->NativeQuickPcOffset(cur_quick_frame_pc_); 201} 202 203bool StackVisitor::IsReferenceVReg(ArtMethod* m, uint16_t vreg) { 204 DCHECK_EQ(m, GetMethod()); 205 // Process register map (which native and runtime methods don't have) 206 if (m->IsNative() || m->IsRuntimeMethod() || m->IsProxyMethod()) { 207 return false; 208 } 209 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader(); 210 if (method_header->IsOptimized()) { 211 return true; // TODO: Implement. 212 } 213 const uint8_t* native_gc_map = method_header->GetNativeGcMap(); 214 CHECK(native_gc_map != nullptr) << PrettyMethod(m); 215 const DexFile::CodeItem* code_item = m->GetCodeItem(); 216 // Can't be null or how would we compile its instructions? 217 DCHECK(code_item != nullptr) << PrettyMethod(m); 218 NativePcOffsetToReferenceMap map(native_gc_map); 219 size_t num_regs = std::min(map.RegWidth() * 8, static_cast<size_t>(code_item->registers_size_)); 220 const uint8_t* reg_bitmap = nullptr; 221 if (num_regs > 0) { 222 uintptr_t native_pc_offset = method_header->NativeQuickPcOffset(GetCurrentQuickFramePc()); 223 reg_bitmap = map.FindBitMap(native_pc_offset); 224 DCHECK(reg_bitmap != nullptr); 225 } 226 // Does this register hold a reference? 227 return vreg < num_regs && TestBitmap(vreg, reg_bitmap); 228} 229 230bool StackVisitor::GetVRegFromDebuggerShadowFrame(uint16_t vreg, 231 VRegKind kind, 232 uint32_t* val) const { 233 size_t frame_id = const_cast<StackVisitor*>(this)->GetFrameId(); 234 ShadowFrame* shadow_frame = thread_->FindDebuggerShadowFrame(frame_id); 235 if (shadow_frame != nullptr) { 236 bool* updated_vreg_flags = thread_->GetUpdatedVRegFlags(frame_id); 237 DCHECK(updated_vreg_flags != nullptr); 238 if (updated_vreg_flags[vreg]) { 239 // Value is set by the debugger. 240 if (kind == kReferenceVReg) { 241 *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>( 242 shadow_frame->GetVRegReference(vreg))); 243 } else { 244 *val = shadow_frame->GetVReg(vreg); 245 } 246 return true; 247 } 248 } 249 // No value is set by the debugger. 250 return false; 251} 252 253bool StackVisitor::GetVReg(ArtMethod* m, uint16_t vreg, VRegKind kind, uint32_t* val) const { 254 if (cur_quick_frame_ != nullptr) { 255 DCHECK(context_ != nullptr); // You can't reliably read registers without a context. 256 DCHECK(m == GetMethod()); 257 // Check if there is value set by the debugger. 258 if (GetVRegFromDebuggerShadowFrame(vreg, kind, val)) { 259 return true; 260 } 261 if (cur_oat_quick_method_header_->IsOptimized()) { 262 return GetVRegFromOptimizedCode(m, vreg, kind, val); 263 } else { 264 return GetVRegFromQuickCode(m, vreg, kind, val); 265 } 266 } else { 267 DCHECK(cur_shadow_frame_ != nullptr); 268 if (kind == kReferenceVReg) { 269 *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>( 270 cur_shadow_frame_->GetVRegReference(vreg))); 271 } else { 272 *val = cur_shadow_frame_->GetVReg(vreg); 273 } 274 return true; 275 } 276} 277 278bool StackVisitor::GetVRegFromQuickCode(ArtMethod* m, uint16_t vreg, VRegKind kind, 279 uint32_t* val) const { 280 DCHECK_EQ(m, GetMethod()); 281 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader(); 282 QuickMethodFrameInfo frame_info = method_header->GetFrameInfo(); 283 const VmapTable vmap_table(method_header->GetVmapTable()); 284 uint32_t vmap_offset; 285 // TODO: IsInContext stops before spotting floating point registers. 286 if (vmap_table.IsInContext(vreg, kind, &vmap_offset)) { 287 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg); 288 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask(); 289 uint32_t reg = vmap_table.ComputeRegister(spill_mask, vmap_offset, kind); 290 return GetRegisterIfAccessible(reg, kind, val); 291 } else { 292 const DexFile::CodeItem* code_item = m->GetCodeItem(); 293 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile 294 // its instructions? 295 *val = *GetVRegAddrFromQuickCode(cur_quick_frame_, code_item, frame_info.CoreSpillMask(), 296 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg); 297 return true; 298 } 299} 300 301bool StackVisitor::GetVRegFromOptimizedCode(ArtMethod* m, uint16_t vreg, VRegKind kind, 302 uint32_t* val) const { 303 DCHECK_EQ(m, GetMethod()); 304 const DexFile::CodeItem* code_item = m->GetCodeItem(); 305 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile 306 // its instructions? 307 uint16_t number_of_dex_registers = code_item->registers_size_; 308 DCHECK_LT(vreg, code_item->registers_size_); 309 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader(); 310 CodeInfo code_info = method_header->GetOptimizedCodeInfo(); 311 StackMapEncoding encoding = code_info.ExtractEncoding(); 312 313 uint32_t native_pc_offset = method_header->NativeQuickPcOffset(cur_quick_frame_pc_); 314 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding); 315 DCHECK(stack_map.IsValid()); 316 size_t depth_in_stack_map = current_inlining_depth_ - 1; 317 318 DexRegisterMap dex_register_map = IsInInlinedFrame() 319 ? code_info.GetDexRegisterMapAtDepth(depth_in_stack_map, 320 code_info.GetInlineInfoOf(stack_map, encoding), 321 encoding, 322 number_of_dex_registers) 323 : code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_dex_registers); 324 325 if (!dex_register_map.IsValid()) { 326 return false; 327 } 328 DexRegisterLocation::Kind location_kind = 329 dex_register_map.GetLocationKind(vreg, number_of_dex_registers, code_info, encoding); 330 switch (location_kind) { 331 case DexRegisterLocation::Kind::kInStack: { 332 const int32_t offset = dex_register_map.GetStackOffsetInBytes(vreg, 333 number_of_dex_registers, 334 code_info, 335 encoding); 336 const uint8_t* addr = reinterpret_cast<const uint8_t*>(cur_quick_frame_) + offset; 337 *val = *reinterpret_cast<const uint32_t*>(addr); 338 return true; 339 } 340 case DexRegisterLocation::Kind::kInRegister: 341 case DexRegisterLocation::Kind::kInRegisterHigh: 342 case DexRegisterLocation::Kind::kInFpuRegister: 343 case DexRegisterLocation::Kind::kInFpuRegisterHigh: { 344 uint32_t reg = 345 dex_register_map.GetMachineRegister(vreg, number_of_dex_registers, code_info, encoding); 346 return GetRegisterIfAccessible(reg, kind, val); 347 } 348 case DexRegisterLocation::Kind::kConstant: 349 *val = dex_register_map.GetConstant(vreg, number_of_dex_registers, code_info, encoding); 350 return true; 351 case DexRegisterLocation::Kind::kNone: 352 return false; 353 default: 354 LOG(FATAL) 355 << "Unexpected location kind" 356 << DexRegisterLocation::PrettyDescriptor( 357 dex_register_map.GetLocationInternalKind(vreg, 358 number_of_dex_registers, 359 code_info, 360 encoding)); 361 UNREACHABLE(); 362 } 363} 364 365bool StackVisitor::GetRegisterIfAccessible(uint32_t reg, VRegKind kind, uint32_t* val) const { 366 const bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg); 367 368 // X86 float registers are 64-bit and the logic below does not apply. 369 DCHECK(!is_float || kRuntimeISA != InstructionSet::kX86); 370 371 if (!IsAccessibleRegister(reg, is_float)) { 372 return false; 373 } 374 uintptr_t ptr_val = GetRegister(reg, is_float); 375 const bool target64 = Is64BitInstructionSet(kRuntimeISA); 376 if (target64) { 377 const bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg); 378 const bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg); 379 int64_t value_long = static_cast<int64_t>(ptr_val); 380 if (wide_lo) { 381 ptr_val = static_cast<uintptr_t>(Low32Bits(value_long)); 382 } else if (wide_hi) { 383 ptr_val = static_cast<uintptr_t>(High32Bits(value_long)); 384 } 385 } 386 *val = ptr_val; 387 return true; 388} 389 390bool StackVisitor::GetVRegPairFromDebuggerShadowFrame(uint16_t vreg, 391 VRegKind kind_lo, 392 VRegKind kind_hi, 393 uint64_t* val) const { 394 uint32_t low_32bits; 395 uint32_t high_32bits; 396 bool success = GetVRegFromDebuggerShadowFrame(vreg, kind_lo, &low_32bits); 397 success &= GetVRegFromDebuggerShadowFrame(vreg + 1, kind_hi, &high_32bits); 398 if (success) { 399 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits); 400 } 401 return success; 402} 403 404bool StackVisitor::GetVRegPair(ArtMethod* m, uint16_t vreg, VRegKind kind_lo, 405 VRegKind kind_hi, uint64_t* val) const { 406 if (kind_lo == kLongLoVReg) { 407 DCHECK_EQ(kind_hi, kLongHiVReg); 408 } else if (kind_lo == kDoubleLoVReg) { 409 DCHECK_EQ(kind_hi, kDoubleHiVReg); 410 } else { 411 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi; 412 UNREACHABLE(); 413 } 414 // Check if there is value set by the debugger. 415 if (GetVRegPairFromDebuggerShadowFrame(vreg, kind_lo, kind_hi, val)) { 416 return true; 417 } 418 if (cur_quick_frame_ != nullptr) { 419 DCHECK(context_ != nullptr); // You can't reliably read registers without a context. 420 DCHECK(m == GetMethod()); 421 if (cur_oat_quick_method_header_->IsOptimized()) { 422 return GetVRegPairFromOptimizedCode(m, vreg, kind_lo, kind_hi, val); 423 } else { 424 return GetVRegPairFromQuickCode(m, vreg, kind_lo, kind_hi, val); 425 } 426 } else { 427 DCHECK(cur_shadow_frame_ != nullptr); 428 *val = cur_shadow_frame_->GetVRegLong(vreg); 429 return true; 430 } 431} 432 433bool StackVisitor::GetVRegPairFromQuickCode(ArtMethod* m, uint16_t vreg, VRegKind kind_lo, 434 VRegKind kind_hi, uint64_t* val) const { 435 DCHECK_EQ(m, GetMethod()); 436 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader(); 437 QuickMethodFrameInfo frame_info = method_header->GetFrameInfo(); 438 const VmapTable vmap_table(method_header->GetVmapTable()); 439 uint32_t vmap_offset_lo, vmap_offset_hi; 440 // TODO: IsInContext stops before spotting floating point registers. 441 if (vmap_table.IsInContext(vreg, kind_lo, &vmap_offset_lo) && 442 vmap_table.IsInContext(vreg + 1, kind_hi, &vmap_offset_hi)) { 443 bool is_float = (kind_lo == kDoubleLoVReg); 444 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask(); 445 uint32_t reg_lo = vmap_table.ComputeRegister(spill_mask, vmap_offset_lo, kind_lo); 446 uint32_t reg_hi = vmap_table.ComputeRegister(spill_mask, vmap_offset_hi, kind_hi); 447 return GetRegisterPairIfAccessible(reg_lo, reg_hi, kind_lo, val); 448 } else { 449 const DexFile::CodeItem* code_item = m->GetCodeItem(); 450 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile 451 // its instructions? 452 uint32_t* addr = GetVRegAddrFromQuickCode( 453 cur_quick_frame_, code_item, frame_info.CoreSpillMask(), 454 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg); 455 *val = *reinterpret_cast<uint64_t*>(addr); 456 return true; 457 } 458} 459 460bool StackVisitor::GetVRegPairFromOptimizedCode(ArtMethod* m, uint16_t vreg, 461 VRegKind kind_lo, VRegKind kind_hi, 462 uint64_t* val) const { 463 uint32_t low_32bits; 464 uint32_t high_32bits; 465 bool success = GetVRegFromOptimizedCode(m, vreg, kind_lo, &low_32bits); 466 success &= GetVRegFromOptimizedCode(m, vreg + 1, kind_hi, &high_32bits); 467 if (success) { 468 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits); 469 } 470 return success; 471} 472 473bool StackVisitor::GetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi, 474 VRegKind kind_lo, uint64_t* val) const { 475 const bool is_float = (kind_lo == kDoubleLoVReg); 476 if (!IsAccessibleRegister(reg_lo, is_float) || !IsAccessibleRegister(reg_hi, is_float)) { 477 return false; 478 } 479 uintptr_t ptr_val_lo = GetRegister(reg_lo, is_float); 480 uintptr_t ptr_val_hi = GetRegister(reg_hi, is_float); 481 bool target64 = Is64BitInstructionSet(kRuntimeISA); 482 if (target64) { 483 int64_t value_long_lo = static_cast<int64_t>(ptr_val_lo); 484 int64_t value_long_hi = static_cast<int64_t>(ptr_val_hi); 485 ptr_val_lo = static_cast<uintptr_t>(Low32Bits(value_long_lo)); 486 ptr_val_hi = static_cast<uintptr_t>(High32Bits(value_long_hi)); 487 } 488 *val = (static_cast<uint64_t>(ptr_val_hi) << 32) | static_cast<uint32_t>(ptr_val_lo); 489 return true; 490} 491 492bool StackVisitor::SetVReg(ArtMethod* m, 493 uint16_t vreg, 494 uint32_t new_value, 495 VRegKind kind) { 496 const DexFile::CodeItem* code_item = m->GetCodeItem(); 497 if (code_item == nullptr) { 498 return false; 499 } 500 ShadowFrame* shadow_frame = GetCurrentShadowFrame(); 501 if (shadow_frame == nullptr) { 502 // This is a compiled frame: we must prepare and update a shadow frame that will 503 // be executed by the interpreter after deoptimization of the stack. 504 const size_t frame_id = GetFrameId(); 505 const uint16_t num_regs = code_item->registers_size_; 506 shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc()); 507 CHECK(shadow_frame != nullptr); 508 // Remember the vreg has been set for debugging and must not be overwritten by the 509 // original value during deoptimization of the stack. 510 thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true; 511 } 512 if (kind == kReferenceVReg) { 513 shadow_frame->SetVRegReference(vreg, reinterpret_cast<mirror::Object*>(new_value)); 514 } else { 515 shadow_frame->SetVReg(vreg, new_value); 516 } 517 return true; 518} 519 520bool StackVisitor::SetVRegPair(ArtMethod* m, 521 uint16_t vreg, 522 uint64_t new_value, 523 VRegKind kind_lo, 524 VRegKind kind_hi) { 525 if (kind_lo == kLongLoVReg) { 526 DCHECK_EQ(kind_hi, kLongHiVReg); 527 } else if (kind_lo == kDoubleLoVReg) { 528 DCHECK_EQ(kind_hi, kDoubleHiVReg); 529 } else { 530 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi; 531 UNREACHABLE(); 532 } 533 const DexFile::CodeItem* code_item = m->GetCodeItem(); 534 if (code_item == nullptr) { 535 return false; 536 } 537 ShadowFrame* shadow_frame = GetCurrentShadowFrame(); 538 if (shadow_frame == nullptr) { 539 // This is a compiled frame: we must prepare for deoptimization (see SetVRegFromDebugger). 540 const size_t frame_id = GetFrameId(); 541 const uint16_t num_regs = code_item->registers_size_; 542 shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc()); 543 CHECK(shadow_frame != nullptr); 544 // Remember the vreg pair has been set for debugging and must not be overwritten by the 545 // original value during deoptimization of the stack. 546 thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true; 547 thread_->GetUpdatedVRegFlags(frame_id)[vreg + 1] = true; 548 } 549 shadow_frame->SetVRegLong(vreg, new_value); 550 return true; 551} 552 553bool StackVisitor::IsAccessibleGPR(uint32_t reg) const { 554 DCHECK(context_ != nullptr); 555 return context_->IsAccessibleGPR(reg); 556} 557 558uintptr_t* StackVisitor::GetGPRAddress(uint32_t reg) const { 559 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine"; 560 DCHECK(context_ != nullptr); 561 return context_->GetGPRAddress(reg); 562} 563 564uintptr_t StackVisitor::GetGPR(uint32_t reg) const { 565 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine"; 566 DCHECK(context_ != nullptr); 567 return context_->GetGPR(reg); 568} 569 570bool StackVisitor::IsAccessibleFPR(uint32_t reg) const { 571 DCHECK(context_ != nullptr); 572 return context_->IsAccessibleFPR(reg); 573} 574 575uintptr_t StackVisitor::GetFPR(uint32_t reg) const { 576 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine"; 577 DCHECK(context_ != nullptr); 578 return context_->GetFPR(reg); 579} 580 581uintptr_t StackVisitor::GetReturnPc() const { 582 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame()); 583 DCHECK(sp != nullptr); 584 uint8_t* pc_addr = sp + GetCurrentQuickFrameInfo().GetReturnPcOffset(); 585 return *reinterpret_cast<uintptr_t*>(pc_addr); 586} 587 588void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) { 589 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame()); 590 CHECK(sp != nullptr); 591 uint8_t* pc_addr = sp + GetCurrentQuickFrameInfo().GetReturnPcOffset(); 592 *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc; 593} 594 595size_t StackVisitor::ComputeNumFrames(Thread* thread, StackWalkKind walk_kind) { 596 struct NumFramesVisitor : public StackVisitor { 597 NumFramesVisitor(Thread* thread_in, StackWalkKind walk_kind_in) 598 : StackVisitor(thread_in, nullptr, walk_kind_in), frames(0) {} 599 600 bool VisitFrame() OVERRIDE { 601 frames++; 602 return true; 603 } 604 605 size_t frames; 606 }; 607 NumFramesVisitor visitor(thread, walk_kind); 608 visitor.WalkStack(true); 609 return visitor.frames; 610} 611 612bool StackVisitor::GetNextMethodAndDexPc(ArtMethod** next_method, uint32_t* next_dex_pc) { 613 struct HasMoreFramesVisitor : public StackVisitor { 614 HasMoreFramesVisitor(Thread* thread, 615 StackWalkKind walk_kind, 616 size_t num_frames, 617 size_t frame_height) 618 : StackVisitor(thread, nullptr, walk_kind, num_frames), 619 frame_height_(frame_height), 620 found_frame_(false), 621 has_more_frames_(false), 622 next_method_(nullptr), 623 next_dex_pc_(0) { 624 } 625 626 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { 627 if (found_frame_) { 628 ArtMethod* method = GetMethod(); 629 if (method != nullptr && !method->IsRuntimeMethod()) { 630 has_more_frames_ = true; 631 next_method_ = method; 632 next_dex_pc_ = GetDexPc(); 633 return false; // End stack walk once next method is found. 634 } 635 } else if (GetFrameHeight() == frame_height_) { 636 found_frame_ = true; 637 } 638 return true; 639 } 640 641 size_t frame_height_; 642 bool found_frame_; 643 bool has_more_frames_; 644 ArtMethod* next_method_; 645 uint32_t next_dex_pc_; 646 }; 647 HasMoreFramesVisitor visitor(thread_, walk_kind_, GetNumFrames(), GetFrameHeight()); 648 visitor.WalkStack(true); 649 *next_method = visitor.next_method_; 650 *next_dex_pc = visitor.next_dex_pc_; 651 return visitor.has_more_frames_; 652} 653 654void StackVisitor::DescribeStack(Thread* thread) { 655 struct DescribeStackVisitor : public StackVisitor { 656 explicit DescribeStackVisitor(Thread* thread_in) 657 : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {} 658 659 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { 660 LOG(INFO) << "Frame Id=" << GetFrameId() << " " << DescribeLocation(); 661 return true; 662 } 663 }; 664 DescribeStackVisitor visitor(thread); 665 visitor.WalkStack(true); 666} 667 668std::string StackVisitor::DescribeLocation() const { 669 std::string result("Visiting method '"); 670 ArtMethod* m = GetMethod(); 671 if (m == nullptr) { 672 return "upcall"; 673 } 674 result += PrettyMethod(m); 675 result += StringPrintf("' at dex PC 0x%04x", GetDexPc()); 676 if (!IsShadowFrame()) { 677 result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc())); 678 } 679 return result; 680} 681 682static instrumentation::InstrumentationStackFrame& GetInstrumentationStackFrame(Thread* thread, 683 uint32_t depth) { 684 CHECK_LT(depth, thread->GetInstrumentationStack()->size()); 685 return thread->GetInstrumentationStack()->at(depth); 686} 687 688static void AssertPcIsWithinQuickCode(ArtMethod* method, uintptr_t pc) 689 SHARED_REQUIRES(Locks::mutator_lock_) { 690 if (method->IsNative() || method->IsRuntimeMethod() || method->IsProxyMethod()) { 691 return; 692 } 693 694 if (pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc())) { 695 return; 696 } 697 698 const void* code = method->GetEntryPointFromQuickCompiledCode(); 699 if (code == GetQuickInstrumentationEntryPoint()) { 700 return; 701 } 702 703 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 704 if (class_linker->IsQuickToInterpreterBridge(code) || 705 class_linker->IsQuickResolutionStub(code)) { 706 return; 707 } 708 709 // If we are the JIT then we may have just compiled the method after the 710 // IsQuickToInterpreterBridge check. 711 jit::Jit* const jit = Runtime::Current()->GetJit(); 712 if (jit != nullptr && jit->GetCodeCache()->ContainsPc(code)) { 713 return; 714 } 715 716 uint32_t code_size = OatQuickMethodHeader::FromEntryPoint(code)->code_size_; 717 uintptr_t code_start = reinterpret_cast<uintptr_t>(code); 718 CHECK(code_start <= pc && pc <= (code_start + code_size)) 719 << PrettyMethod(method) 720 << " pc=" << std::hex << pc 721 << " code_start=" << code_start 722 << " code_size=" << code_size; 723} 724 725void StackVisitor::SanityCheckFrame() const { 726 if (kIsDebugBuild) { 727 ArtMethod* method = GetMethod(); 728 auto* declaring_class = method->GetDeclaringClass(); 729 // Runtime methods have null declaring class. 730 if (!method->IsRuntimeMethod()) { 731 CHECK(declaring_class != nullptr); 732 CHECK_EQ(declaring_class->GetClass(), declaring_class->GetClass()->GetClass()) 733 << declaring_class; 734 } else { 735 CHECK(declaring_class == nullptr); 736 } 737 Runtime* const runtime = Runtime::Current(); 738 LinearAlloc* const linear_alloc = runtime->GetLinearAlloc(); 739 if (!linear_alloc->Contains(method)) { 740 // Check class linker linear allocs. 741 mirror::Class* klass = method->GetDeclaringClass(); 742 LinearAlloc* const class_linear_alloc = (klass != nullptr) 743 ? ClassLinker::GetAllocatorForClassLoader(klass->GetClassLoader()) 744 : linear_alloc; 745 if (!class_linear_alloc->Contains(method)) { 746 // Check image space. 747 bool in_image = false; 748 for (auto& space : runtime->GetHeap()->GetContinuousSpaces()) { 749 if (space->IsImageSpace()) { 750 auto* image_space = space->AsImageSpace(); 751 const auto& header = image_space->GetImageHeader(); 752 const auto* methods = &header.GetMethodsSection(); 753 if (methods->Contains(reinterpret_cast<const uint8_t*>(method) - image_space->Begin())) { 754 in_image = true; 755 break; 756 } 757 } 758 } 759 CHECK(in_image) << PrettyMethod(method) << " not in linear alloc or image"; 760 } 761 } 762 if (cur_quick_frame_ != nullptr) { 763 AssertPcIsWithinQuickCode(method, cur_quick_frame_pc_); 764 // Frame sanity. 765 size_t frame_size = GetCurrentQuickFrameInfo().FrameSizeInBytes(); 766 CHECK_NE(frame_size, 0u); 767 // A rough guess at an upper size we expect to see for a frame. 768 // 256 registers 769 // 2 words HandleScope overhead 770 // 3+3 register spills 771 // TODO: this seems architecture specific for the case of JNI frames. 772 // TODO: 083-compiler-regressions ManyFloatArgs shows this estimate is wrong. 773 // const size_t kMaxExpectedFrameSize = (256 + 2 + 3 + 3) * sizeof(word); 774 const size_t kMaxExpectedFrameSize = 2 * KB; 775 CHECK_LE(frame_size, kMaxExpectedFrameSize) << PrettyMethod(method); 776 size_t return_pc_offset = GetCurrentQuickFrameInfo().GetReturnPcOffset(); 777 CHECK_LT(return_pc_offset, frame_size); 778 } 779 } 780} 781 782// Counts the number of references in the parameter list of the corresponding method. 783// Note: Thus does _not_ include "this" for non-static methods. 784static uint32_t GetNumberOfReferenceArgsWithoutReceiver(ArtMethod* method) 785 SHARED_REQUIRES(Locks::mutator_lock_) { 786 uint32_t shorty_len; 787 const char* shorty = method->GetShorty(&shorty_len); 788 uint32_t refs = 0; 789 for (uint32_t i = 1; i < shorty_len ; ++i) { 790 if (shorty[i] == 'L') { 791 refs++; 792 } 793 } 794 return refs; 795} 796 797QuickMethodFrameInfo StackVisitor::GetCurrentQuickFrameInfo() const { 798 if (cur_oat_quick_method_header_ != nullptr) { 799 return cur_oat_quick_method_header_->GetFrameInfo(); 800 } 801 802 ArtMethod* method = GetMethod(); 803 Runtime* runtime = Runtime::Current(); 804 805 if (method->IsAbstract()) { 806 return runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs); 807 } 808 809 // This goes before IsProxyMethod since runtime methods have a null declaring class. 810 if (method->IsRuntimeMethod()) { 811 return runtime->GetRuntimeMethodFrameInfo(method); 812 } 813 814 if (method->IsProxyMethod()) { 815 // There is only one direct method of a proxy class: the constructor. A direct method is 816 // cloned from the original java.lang.reflect.Proxy and is executed as usual quick 817 // compiled method without any stubs. Therefore the method must have a OatQuickMethodHeader. 818 DCHECK(!method->IsDirect() && !method->IsConstructor()) 819 << "Constructors of proxy classes must have a OatQuickMethodHeader"; 820 return runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs); 821 } 822 823 // The only remaining case is if the method is native and uses the generic JNI stub. 824 DCHECK(method->IsNative()); 825 ClassLinker* class_linker = runtime->GetClassLinker(); 826 const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(method, sizeof(void*)); 827 DCHECK(class_linker->IsQuickGenericJniStub(entry_point)) << PrettyMethod(method); 828 // Generic JNI frame. 829 uint32_t handle_refs = GetNumberOfReferenceArgsWithoutReceiver(method) + 1; 830 size_t scope_size = HandleScope::SizeOf(handle_refs); 831 QuickMethodFrameInfo callee_info = runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs); 832 833 // Callee saves + handle scope + method ref + alignment 834 // Note: -sizeof(void*) since callee-save frame stores a whole method pointer. 835 size_t frame_size = RoundUp( 836 callee_info.FrameSizeInBytes() - sizeof(void*) + sizeof(ArtMethod*) + scope_size, 837 kStackAlignment); 838 return QuickMethodFrameInfo(frame_size, callee_info.CoreSpillMask(), callee_info.FpSpillMask()); 839} 840 841void StackVisitor::WalkStack(bool include_transitions) { 842 DCHECK(thread_ == Thread::Current() || thread_->IsSuspended()); 843 CHECK_EQ(cur_depth_, 0U); 844 bool exit_stubs_installed = Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled(); 845 uint32_t instrumentation_stack_depth = 0; 846 size_t inlined_frames_count = 0; 847 848 for (const ManagedStack* current_fragment = thread_->GetManagedStack(); 849 current_fragment != nullptr; current_fragment = current_fragment->GetLink()) { 850 cur_shadow_frame_ = current_fragment->GetTopShadowFrame(); 851 cur_quick_frame_ = current_fragment->GetTopQuickFrame(); 852 cur_quick_frame_pc_ = 0; 853 cur_oat_quick_method_header_ = nullptr; 854 855 if (cur_quick_frame_ != nullptr) { // Handle quick stack frames. 856 // Can't be both a shadow and a quick fragment. 857 DCHECK(current_fragment->GetTopShadowFrame() == nullptr); 858 ArtMethod* method = *cur_quick_frame_; 859 while (method != nullptr) { 860 cur_oat_quick_method_header_ = method->GetOatQuickMethodHeader(cur_quick_frame_pc_); 861 SanityCheckFrame(); 862 863 if ((walk_kind_ == StackWalkKind::kIncludeInlinedFrames) 864 && (cur_oat_quick_method_header_ != nullptr) 865 && cur_oat_quick_method_header_->IsOptimized()) { 866 CodeInfo code_info = cur_oat_quick_method_header_->GetOptimizedCodeInfo(); 867 StackMapEncoding encoding = code_info.ExtractEncoding(); 868 uint32_t native_pc_offset = 869 cur_oat_quick_method_header_->NativeQuickPcOffset(cur_quick_frame_pc_); 870 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding); 871 if (stack_map.IsValid() && stack_map.HasInlineInfo(encoding)) { 872 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding); 873 DCHECK_EQ(current_inlining_depth_, 0u); 874 for (current_inlining_depth_ = inline_info.GetDepth(); 875 current_inlining_depth_ != 0; 876 --current_inlining_depth_) { 877 bool should_continue = VisitFrame(); 878 if (UNLIKELY(!should_continue)) { 879 return; 880 } 881 cur_depth_++; 882 inlined_frames_count++; 883 } 884 } 885 } 886 887 bool should_continue = VisitFrame(); 888 if (UNLIKELY(!should_continue)) { 889 return; 890 } 891 892 QuickMethodFrameInfo frame_info = GetCurrentQuickFrameInfo(); 893 if (context_ != nullptr) { 894 context_->FillCalleeSaves(reinterpret_cast<uint8_t*>(cur_quick_frame_), frame_info); 895 } 896 // Compute PC for next stack frame from return PC. 897 size_t frame_size = frame_info.FrameSizeInBytes(); 898 size_t return_pc_offset = frame_size - sizeof(void*); 899 uint8_t* return_pc_addr = reinterpret_cast<uint8_t*>(cur_quick_frame_) + return_pc_offset; 900 uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr); 901 902 if (UNLIKELY(exit_stubs_installed)) { 903 // While profiling, the return pc is restored from the side stack, except when walking 904 // the stack for an exception where the side stack will be unwound in VisitFrame. 905 if (reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == return_pc) { 906 const instrumentation::InstrumentationStackFrame& instrumentation_frame = 907 GetInstrumentationStackFrame(thread_, instrumentation_stack_depth); 908 instrumentation_stack_depth++; 909 if (GetMethod() == Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveAll)) { 910 // Skip runtime save all callee frames which are used to deliver exceptions. 911 } else if (instrumentation_frame.interpreter_entry_) { 912 ArtMethod* callee = Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs); 913 CHECK_EQ(GetMethod(), callee) << "Expected: " << PrettyMethod(callee) << " Found: " 914 << PrettyMethod(GetMethod()); 915 } else { 916 CHECK_EQ(instrumentation_frame.method_, GetMethod()) 917 << "Expected: " << PrettyMethod(instrumentation_frame.method_) 918 << " Found: " << PrettyMethod(GetMethod()); 919 } 920 if (num_frames_ != 0) { 921 // Check agreement of frame Ids only if num_frames_ is computed to avoid infinite 922 // recursion. 923 size_t frame_id = instrumentation::Instrumentation::ComputeFrameId( 924 thread_, 925 cur_depth_, 926 inlined_frames_count); 927 CHECK_EQ(instrumentation_frame.frame_id_, frame_id); 928 } 929 return_pc = instrumentation_frame.return_pc_; 930 } 931 } 932 933 cur_quick_frame_pc_ = return_pc; 934 uint8_t* next_frame = reinterpret_cast<uint8_t*>(cur_quick_frame_) + frame_size; 935 cur_quick_frame_ = reinterpret_cast<ArtMethod**>(next_frame); 936 937 if (kDebugStackWalk) { 938 LOG(INFO) << PrettyMethod(method) << "@" << method << " size=" << frame_size 939 << std::boolalpha 940 << " optimized=" << (cur_oat_quick_method_header_ != nullptr && 941 cur_oat_quick_method_header_->IsOptimized()) 942 << " native=" << method->IsNative() 943 << std::noboolalpha 944 << " entrypoints=" << method->GetEntryPointFromQuickCompiledCode() 945 << "," << method->GetEntryPointFromJni() 946 << " next=" << *cur_quick_frame_; 947 } 948 949 cur_depth_++; 950 method = *cur_quick_frame_; 951 } 952 } else if (cur_shadow_frame_ != nullptr) { 953 do { 954 SanityCheckFrame(); 955 bool should_continue = VisitFrame(); 956 if (UNLIKELY(!should_continue)) { 957 return; 958 } 959 cur_depth_++; 960 cur_shadow_frame_ = cur_shadow_frame_->GetLink(); 961 } while (cur_shadow_frame_ != nullptr); 962 } 963 if (include_transitions) { 964 bool should_continue = VisitFrame(); 965 if (!should_continue) { 966 return; 967 } 968 } 969 cur_depth_++; 970 } 971 if (num_frames_ != 0) { 972 CHECK_EQ(cur_depth_, num_frames_); 973 } 974} 975 976void JavaFrameRootInfo::Describe(std::ostream& os) const { 977 const StackVisitor* visitor = stack_visitor_; 978 CHECK(visitor != nullptr); 979 os << "Type=" << GetType() << " thread_id=" << GetThreadId() << " location=" << 980 visitor->DescribeLocation() << " vreg=" << vreg_; 981} 982 983int StackVisitor::GetVRegOffsetFromQuickCode(const DexFile::CodeItem* code_item, 984 uint32_t core_spills, uint32_t fp_spills, 985 size_t frame_size, int reg, InstructionSet isa) { 986 size_t pointer_size = InstructionSetPointerSize(isa); 987 if (kIsDebugBuild) { 988 auto* runtime = Runtime::Current(); 989 if (runtime != nullptr) { 990 CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size); 991 } 992 } 993 DCHECK_ALIGNED(frame_size, kStackAlignment); 994 DCHECK_NE(reg, -1); 995 int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa) 996 + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa) 997 + sizeof(uint32_t); // Filler. 998 int num_regs = code_item->registers_size_ - code_item->ins_size_; 999 int temp_threshold = code_item->registers_size_; 1000 const int max_num_special_temps = 1; 1001 if (reg == temp_threshold) { 1002 // The current method pointer corresponds to special location on stack. 1003 return 0; 1004 } else if (reg >= temp_threshold + max_num_special_temps) { 1005 /* 1006 * Special temporaries may have custom locations and the logic above deals with that. 1007 * However, non-special temporaries are placed relative to the outs. 1008 */ 1009 int temps_start = code_item->outs_size_ * sizeof(uint32_t) + pointer_size /* art method */; 1010 int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t); 1011 return temps_start + relative_offset; 1012 } else if (reg < num_regs) { 1013 int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t); 1014 return locals_start + (reg * sizeof(uint32_t)); 1015 } else { 1016 // Handle ins. 1017 return frame_size + ((reg - num_regs) * sizeof(uint32_t)) + pointer_size /* art method */; 1018 } 1019} 1020 1021void LockCountData::AddMonitorInternal(Thread* self, mirror::Object* obj) { 1022 if (obj == nullptr) { 1023 return; 1024 } 1025 1026 // If there's an error during enter, we won't have locked the monitor. So check there's no 1027 // exception. 1028 if (self->IsExceptionPending()) { 1029 return; 1030 } 1031 1032 if (monitors_ == nullptr) { 1033 monitors_.reset(new std::vector<mirror::Object*>()); 1034 } 1035 monitors_->push_back(obj); 1036} 1037 1038void LockCountData::RemoveMonitorInternal(Thread* self, const mirror::Object* obj) { 1039 if (obj == nullptr) { 1040 return; 1041 } 1042 bool found_object = false; 1043 if (monitors_ != nullptr) { 1044 // We need to remove one pointer to ref, as duplicates are used for counting recursive locks. 1045 // We arbitrarily choose the first one. 1046 auto it = std::find(monitors_->begin(), monitors_->end(), obj); 1047 if (it != monitors_->end()) { 1048 monitors_->erase(it); 1049 found_object = true; 1050 } 1051 } 1052 if (!found_object) { 1053 // The object wasn't found. Time for an IllegalMonitorStateException. 1054 // The order here isn't fully clear. Assume that any other pending exception is swallowed. 1055 // TODO: Maybe make already pending exception a suppressed exception. 1056 self->ClearException(); 1057 self->ThrowNewExceptionF("Ljava/lang/IllegalMonitorStateException;", 1058 "did not lock monitor on object of type '%s' before unlocking", 1059 PrettyTypeOf(const_cast<mirror::Object*>(obj)).c_str()); 1060 } 1061} 1062 1063// Helper to unlock a monitor. Must be NO_THREAD_SAFETY_ANALYSIS, as we can't statically show 1064// that the object was locked. 1065void MonitorExitHelper(Thread* self, mirror::Object* obj) NO_THREAD_SAFETY_ANALYSIS { 1066 DCHECK(self != nullptr); 1067 DCHECK(obj != nullptr); 1068 obj->MonitorExit(self); 1069} 1070 1071bool LockCountData::CheckAllMonitorsReleasedInternal(Thread* self) { 1072 DCHECK(self != nullptr); 1073 if (monitors_ != nullptr) { 1074 if (!monitors_->empty()) { 1075 // There may be an exception pending, if the method is terminating abruptly. Clear it. 1076 // TODO: Should we add this as a suppressed exception? 1077 self->ClearException(); 1078 1079 // OK, there are monitors that are still locked. To enforce structured locking (and avoid 1080 // deadlocks) we unlock all of them before we raise the IllegalMonitorState exception. 1081 for (mirror::Object* obj : *monitors_) { 1082 MonitorExitHelper(self, obj); 1083 // If this raised an exception, ignore. TODO: Should we add this as suppressed 1084 // exceptions? 1085 if (self->IsExceptionPending()) { 1086 self->ClearException(); 1087 } 1088 } 1089 // Raise an exception, just give the first object as the sample. 1090 mirror::Object* first = (*monitors_)[0]; 1091 self->ThrowNewExceptionF("Ljava/lang/IllegalMonitorStateException;", 1092 "did not unlock monitor on object of type '%s'", 1093 PrettyTypeOf(first).c_str()); 1094 1095 // To make sure this path is not triggered again, clean out the monitors. 1096 monitors_->clear(); 1097 1098 return false; 1099 } 1100 } 1101 return true; 1102} 1103 1104} // namespace art 1105