quick_trampoline_entrypoints.cc revision 666ee3d7c6039c80e75287e311895bd6a9b01e9f
1/* 2 * Copyright (C) 2012 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 "art_method-inl.h" 18#include "base/callee_save_type.h" 19#include "base/enums.h" 20#include "callee_save_frame.h" 21#include "common_throws.h" 22#include "debugger.h" 23#include "dex_file-inl.h" 24#include "dex_file_types.h" 25#include "dex_instruction-inl.h" 26#include "entrypoints/entrypoint_utils-inl.h" 27#include "entrypoints/runtime_asm_entrypoints.h" 28#include "gc/accounting/card_table-inl.h" 29#include "imt_conflict_table.h" 30#include "imtable-inl.h" 31#include "index_bss_mapping.h" 32#include "instrumentation.h" 33#include "interpreter/interpreter.h" 34#include "jit/jit.h" 35#include "linear_alloc.h" 36#include "method_handles.h" 37#include "method_reference.h" 38#include "mirror/class-inl.h" 39#include "mirror/dex_cache-inl.h" 40#include "mirror/method.h" 41#include "mirror/method_handle_impl.h" 42#include "mirror/object-inl.h" 43#include "mirror/object_array-inl.h" 44#include "oat_file.h" 45#include "oat_quick_method_header.h" 46#include "quick_exception_handler.h" 47#include "runtime.h" 48#include "scoped_thread_state_change-inl.h" 49#include "stack.h" 50#include "thread-inl.h" 51#include "well_known_classes.h" 52 53namespace art { 54 55// Visits the arguments as saved to the stack by a CalleeSaveType::kRefAndArgs callee save frame. 56class QuickArgumentVisitor { 57 // Number of bytes for each out register in the caller method's frame. 58 static constexpr size_t kBytesStackArgLocation = 4; 59 // Frame size in bytes of a callee-save frame for RefsAndArgs. 60 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 61 GetCalleeSaveFrameSize(kRuntimeISA, CalleeSaveType::kSaveRefsAndArgs); 62#if defined(__arm__) 63 // The callee save frame is pointed to by SP. 64 // | argN | | 65 // | ... | | 66 // | arg4 | | 67 // | arg3 spill | | Caller's frame 68 // | arg2 spill | | 69 // | arg1 spill | | 70 // | Method* | --- 71 // | LR | 72 // | ... | 4x6 bytes callee saves 73 // | R3 | 74 // | R2 | 75 // | R1 | 76 // | S15 | 77 // | : | 78 // | S0 | 79 // | | 4x2 bytes padding 80 // | Method* | <- sp 81 static constexpr bool kSplitPairAcrossRegisterAndStack = false; 82 static constexpr bool kAlignPairRegister = true; 83 static constexpr bool kQuickSoftFloatAbi = false; 84 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = true; 85 static constexpr bool kQuickSkipOddFpRegisters = false; 86 static constexpr size_t kNumQuickGprArgs = 3; 87 static constexpr size_t kNumQuickFprArgs = 16; 88 static constexpr bool kGprFprLockstep = false; 89 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 90 arm::ArmCalleeSaveFpr1Offset(CalleeSaveType::kSaveRefsAndArgs); // Offset of first FPR arg. 91 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 92 arm::ArmCalleeSaveGpr1Offset(CalleeSaveType::kSaveRefsAndArgs); // Offset of first GPR arg. 93 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 94 arm::ArmCalleeSaveLrOffset(CalleeSaveType::kSaveRefsAndArgs); // Offset of return address. 95 static size_t GprIndexToGprOffset(uint32_t gpr_index) { 96 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA); 97 } 98#elif defined(__aarch64__) 99 // The callee save frame is pointed to by SP. 100 // | argN | | 101 // | ... | | 102 // | arg4 | | 103 // | arg3 spill | | Caller's frame 104 // | arg2 spill | | 105 // | arg1 spill | | 106 // | Method* | --- 107 // | LR | 108 // | X29 | 109 // | : | 110 // | X20 | 111 // | X7 | 112 // | : | 113 // | X1 | 114 // | D7 | 115 // | : | 116 // | D0 | 117 // | | padding 118 // | Method* | <- sp 119 static constexpr bool kSplitPairAcrossRegisterAndStack = false; 120 static constexpr bool kAlignPairRegister = false; 121 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI. 122 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false; 123 static constexpr bool kQuickSkipOddFpRegisters = false; 124 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs. 125 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs. 126 static constexpr bool kGprFprLockstep = false; 127 // Offset of first FPR arg. 128 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 129 arm64::Arm64CalleeSaveFpr1Offset(CalleeSaveType::kSaveRefsAndArgs); 130 // Offset of first GPR arg. 131 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 132 arm64::Arm64CalleeSaveGpr1Offset(CalleeSaveType::kSaveRefsAndArgs); 133 // Offset of return address. 134 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 135 arm64::Arm64CalleeSaveLrOffset(CalleeSaveType::kSaveRefsAndArgs); 136 static size_t GprIndexToGprOffset(uint32_t gpr_index) { 137 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA); 138 } 139#elif defined(__mips__) && !defined(__LP64__) 140 // The callee save frame is pointed to by SP. 141 // | argN | | 142 // | ... | | 143 // | arg4 | | 144 // | arg3 spill | | Caller's frame 145 // | arg2 spill | | 146 // | arg1 spill | | 147 // | Method* | --- 148 // | RA | 149 // | ... | callee saves 150 // | T1 | arg5 151 // | T0 | arg4 152 // | A3 | arg3 153 // | A2 | arg2 154 // | A1 | arg1 155 // | F19 | 156 // | F18 | f_arg5 157 // | F17 | 158 // | F16 | f_arg4 159 // | F15 | 160 // | F14 | f_arg3 161 // | F13 | 162 // | F12 | f_arg2 163 // | F11 | 164 // | F10 | f_arg1 165 // | F9 | 166 // | F8 | f_arg0 167 // | | padding 168 // | A0/Method* | <- sp 169 static constexpr bool kSplitPairAcrossRegisterAndStack = false; 170 static constexpr bool kAlignPairRegister = true; 171 static constexpr bool kQuickSoftFloatAbi = false; 172 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false; 173 static constexpr bool kQuickSkipOddFpRegisters = true; 174 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs. 175 static constexpr size_t kNumQuickFprArgs = 12; // 6 arguments passed in FPRs. Floats can be 176 // passed only in even numbered registers and each 177 // double occupies two registers. 178 static constexpr bool kGprFprLockstep = false; 179 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 8; // Offset of first FPR arg. 180 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 56; // Offset of first GPR arg. 181 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 108; // Offset of return address. 182 static size_t GprIndexToGprOffset(uint32_t gpr_index) { 183 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA); 184 } 185#elif defined(__mips__) && defined(__LP64__) 186 // The callee save frame is pointed to by SP. 187 // | argN | | 188 // | ... | | 189 // | arg4 | | 190 // | arg3 spill | | Caller's frame 191 // | arg2 spill | | 192 // | arg1 spill | | 193 // | Method* | --- 194 // | RA | 195 // | ... | callee saves 196 // | A7 | arg7 197 // | A6 | arg6 198 // | A5 | arg5 199 // | A4 | arg4 200 // | A3 | arg3 201 // | A2 | arg2 202 // | A1 | arg1 203 // | F19 | f_arg7 204 // | F18 | f_arg6 205 // | F17 | f_arg5 206 // | F16 | f_arg4 207 // | F15 | f_arg3 208 // | F14 | f_arg2 209 // | F13 | f_arg1 210 // | F12 | f_arg0 211 // | | padding 212 // | A0/Method* | <- sp 213 // NOTE: for Mip64, when A0 is skipped, F12 is also skipped. 214 static constexpr bool kSplitPairAcrossRegisterAndStack = false; 215 static constexpr bool kAlignPairRegister = false; 216 static constexpr bool kQuickSoftFloatAbi = false; 217 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false; 218 static constexpr bool kQuickSkipOddFpRegisters = false; 219 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs. 220 static constexpr size_t kNumQuickFprArgs = 7; // 7 arguments passed in FPRs. 221 static constexpr bool kGprFprLockstep = true; 222 223 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 24; // Offset of first FPR arg (F13). 224 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80; // Offset of first GPR arg (A1). 225 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 200; // Offset of return address. 226 static size_t GprIndexToGprOffset(uint32_t gpr_index) { 227 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA); 228 } 229#elif defined(__i386__) 230 // The callee save frame is pointed to by SP. 231 // | argN | | 232 // | ... | | 233 // | arg4 | | 234 // | arg3 spill | | Caller's frame 235 // | arg2 spill | | 236 // | arg1 spill | | 237 // | Method* | --- 238 // | Return | 239 // | EBP,ESI,EDI | callee saves 240 // | EBX | arg3 241 // | EDX | arg2 242 // | ECX | arg1 243 // | XMM3 | float arg 4 244 // | XMM2 | float arg 3 245 // | XMM1 | float arg 2 246 // | XMM0 | float arg 1 247 // | EAX/Method* | <- sp 248 static constexpr bool kSplitPairAcrossRegisterAndStack = false; 249 static constexpr bool kAlignPairRegister = false; 250 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI. 251 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false; 252 static constexpr bool kQuickSkipOddFpRegisters = false; 253 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs. 254 static constexpr size_t kNumQuickFprArgs = 4; // 4 arguments passed in FPRs. 255 static constexpr bool kGprFprLockstep = false; 256 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 4; // Offset of first FPR arg. 257 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4 + 4*8; // Offset of first GPR arg. 258 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28 + 4*8; // Offset of return address. 259 static size_t GprIndexToGprOffset(uint32_t gpr_index) { 260 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA); 261 } 262#elif defined(__x86_64__) 263 // The callee save frame is pointed to by SP. 264 // | argN | | 265 // | ... | | 266 // | reg. arg spills | | Caller's frame 267 // | Method* | --- 268 // | Return | 269 // | R15 | callee save 270 // | R14 | callee save 271 // | R13 | callee save 272 // | R12 | callee save 273 // | R9 | arg5 274 // | R8 | arg4 275 // | RSI/R6 | arg1 276 // | RBP/R5 | callee save 277 // | RBX/R3 | callee save 278 // | RDX/R2 | arg2 279 // | RCX/R1 | arg3 280 // | XMM7 | float arg 8 281 // | XMM6 | float arg 7 282 // | XMM5 | float arg 6 283 // | XMM4 | float arg 5 284 // | XMM3 | float arg 4 285 // | XMM2 | float arg 3 286 // | XMM1 | float arg 2 287 // | XMM0 | float arg 1 288 // | Padding | 289 // | RDI/Method* | <- sp 290 static constexpr bool kSplitPairAcrossRegisterAndStack = false; 291 static constexpr bool kAlignPairRegister = false; 292 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI. 293 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false; 294 static constexpr bool kQuickSkipOddFpRegisters = false; 295 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs. 296 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs. 297 static constexpr bool kGprFprLockstep = false; 298 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg. 299 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80 + 4*8; // Offset of first GPR arg. 300 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168 + 4*8; // Offset of return address. 301 static size_t GprIndexToGprOffset(uint32_t gpr_index) { 302 switch (gpr_index) { 303 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA)); 304 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA)); 305 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA)); 306 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA)); 307 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA)); 308 default: 309 LOG(FATAL) << "Unexpected GPR index: " << gpr_index; 310 return 0; 311 } 312 } 313#else 314#error "Unsupported architecture" 315#endif 316 317 public: 318 // Special handling for proxy methods. Proxy methods are instance methods so the 319 // 'this' object is the 1st argument. They also have the same frame layout as the 320 // kRefAndArgs runtime method. Since 'this' is a reference, it is located in the 321 // 1st GPR. 322 static mirror::Object* GetProxyThisObject(ArtMethod** sp) 323 REQUIRES_SHARED(Locks::mutator_lock_) { 324 CHECK((*sp)->IsProxyMethod()); 325 CHECK_GT(kNumQuickGprArgs, 0u); 326 constexpr uint32_t kThisGprIndex = 0u; // 'this' is in the 1st GPR. 327 size_t this_arg_offset = kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset + 328 GprIndexToGprOffset(kThisGprIndex); 329 uint8_t* this_arg_address = reinterpret_cast<uint8_t*>(sp) + this_arg_offset; 330 return reinterpret_cast<StackReference<mirror::Object>*>(this_arg_address)->AsMirrorPtr(); 331 } 332 333 static ArtMethod* GetCallingMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) { 334 DCHECK((*sp)->IsCalleeSaveMethod()); 335 return GetCalleeSaveMethodCaller(sp, CalleeSaveType::kSaveRefsAndArgs); 336 } 337 338 static ArtMethod* GetOuterMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) { 339 DCHECK((*sp)->IsCalleeSaveMethod()); 340 uint8_t* previous_sp = 341 reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize; 342 return *reinterpret_cast<ArtMethod**>(previous_sp); 343 } 344 345 static uint32_t GetCallingDexPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) { 346 DCHECK((*sp)->IsCalleeSaveMethod()); 347 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA, 348 CalleeSaveType::kSaveRefsAndArgs); 349 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>( 350 reinterpret_cast<uintptr_t>(sp) + callee_frame_size); 351 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp); 352 const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc); 353 uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc); 354 355 if (current_code->IsOptimized()) { 356 CodeInfo code_info = current_code->GetOptimizedCodeInfo(); 357 CodeInfoEncoding encoding = code_info.ExtractEncoding(); 358 StackMap stack_map = code_info.GetStackMapForNativePcOffset(outer_pc_offset, encoding); 359 DCHECK(stack_map.IsValid()); 360 if (stack_map.HasInlineInfo(encoding.stack_map.encoding)) { 361 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding); 362 return inline_info.GetDexPcAtDepth(encoding.inline_info.encoding, 363 inline_info.GetDepth(encoding.inline_info.encoding)-1); 364 } else { 365 return stack_map.GetDexPc(encoding.stack_map.encoding); 366 } 367 } else { 368 return current_code->ToDexPc(*caller_sp, outer_pc); 369 } 370 } 371 372 static bool GetInvokeType(ArtMethod** sp, InvokeType* invoke_type, uint32_t* dex_method_index) 373 REQUIRES_SHARED(Locks::mutator_lock_) { 374 DCHECK((*sp)->IsCalleeSaveMethod()); 375 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA, 376 CalleeSaveType::kSaveRefsAndArgs); 377 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>( 378 reinterpret_cast<uintptr_t>(sp) + callee_frame_size); 379 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp); 380 const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc); 381 if (!current_code->IsOptimized()) { 382 return false; 383 } 384 uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc); 385 CodeInfo code_info = current_code->GetOptimizedCodeInfo(); 386 CodeInfoEncoding encoding = code_info.ExtractEncoding(); 387 MethodInfo method_info = current_code->GetOptimizedMethodInfo(); 388 InvokeInfo invoke(code_info.GetInvokeInfoForNativePcOffset(outer_pc_offset, encoding)); 389 if (invoke.IsValid()) { 390 *invoke_type = static_cast<InvokeType>(invoke.GetInvokeType(encoding.invoke_info.encoding)); 391 *dex_method_index = invoke.GetMethodIndex(encoding.invoke_info.encoding, method_info); 392 return true; 393 } 394 return false; 395 } 396 397 // For the given quick ref and args quick frame, return the caller's PC. 398 static uintptr_t GetCallingPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) { 399 DCHECK((*sp)->IsCalleeSaveMethod()); 400 uint8_t* lr = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset; 401 return *reinterpret_cast<uintptr_t*>(lr); 402 } 403 404 QuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty, 405 uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) : 406 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len), 407 gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset), 408 fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset), 409 stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize 410 + sizeof(ArtMethod*)), // Skip ArtMethod*. 411 gpr_index_(0), fpr_index_(0), fpr_double_index_(0), stack_index_(0), 412 cur_type_(Primitive::kPrimVoid), is_split_long_or_double_(false) { 413 static_assert(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0), 414 "Number of Quick FPR arguments unexpected"); 415 static_assert(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled), 416 "Double alignment unexpected"); 417 // For register alignment, we want to assume that counters(fpr_double_index_) are even if the 418 // next register is even. 419 static_assert(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0, 420 "Number of Quick FPR arguments not even"); 421 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize); 422 } 423 424 virtual ~QuickArgumentVisitor() {} 425 426 virtual void Visit() = 0; 427 428 Primitive::Type GetParamPrimitiveType() const { 429 return cur_type_; 430 } 431 432 uint8_t* GetParamAddress() const { 433 if (!kQuickSoftFloatAbi) { 434 Primitive::Type type = GetParamPrimitiveType(); 435 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) { 436 if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) { 437 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) { 438 return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA)); 439 } 440 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) { 441 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA)); 442 } 443 return stack_args_ + (stack_index_ * kBytesStackArgLocation); 444 } 445 } 446 if (gpr_index_ < kNumQuickGprArgs) { 447 return gpr_args_ + GprIndexToGprOffset(gpr_index_); 448 } 449 return stack_args_ + (stack_index_ * kBytesStackArgLocation); 450 } 451 452 bool IsSplitLongOrDouble() const { 453 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) || 454 (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) { 455 return is_split_long_or_double_; 456 } else { 457 return false; // An optimization for when GPR and FPRs are 64bit. 458 } 459 } 460 461 bool IsParamAReference() const { 462 return GetParamPrimitiveType() == Primitive::kPrimNot; 463 } 464 465 bool IsParamALongOrDouble() const { 466 Primitive::Type type = GetParamPrimitiveType(); 467 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble; 468 } 469 470 uint64_t ReadSplitLongParam() const { 471 // The splitted long is always available through the stack. 472 return *reinterpret_cast<uint64_t*>(stack_args_ 473 + stack_index_ * kBytesStackArgLocation); 474 } 475 476 void IncGprIndex() { 477 gpr_index_++; 478 if (kGprFprLockstep) { 479 fpr_index_++; 480 } 481 } 482 483 void IncFprIndex() { 484 fpr_index_++; 485 if (kGprFprLockstep) { 486 gpr_index_++; 487 } 488 } 489 490 void VisitArguments() REQUIRES_SHARED(Locks::mutator_lock_) { 491 // (a) 'stack_args_' should point to the first method's argument 492 // (b) whatever the argument type it is, the 'stack_index_' should 493 // be moved forward along with every visiting. 494 gpr_index_ = 0; 495 fpr_index_ = 0; 496 if (kQuickDoubleRegAlignedFloatBackFilled) { 497 fpr_double_index_ = 0; 498 } 499 stack_index_ = 0; 500 if (!is_static_) { // Handle this. 501 cur_type_ = Primitive::kPrimNot; 502 is_split_long_or_double_ = false; 503 Visit(); 504 stack_index_++; 505 if (kNumQuickGprArgs > 0) { 506 IncGprIndex(); 507 } 508 } 509 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) { 510 cur_type_ = Primitive::GetType(shorty_[shorty_index]); 511 switch (cur_type_) { 512 case Primitive::kPrimNot: 513 case Primitive::kPrimBoolean: 514 case Primitive::kPrimByte: 515 case Primitive::kPrimChar: 516 case Primitive::kPrimShort: 517 case Primitive::kPrimInt: 518 is_split_long_or_double_ = false; 519 Visit(); 520 stack_index_++; 521 if (gpr_index_ < kNumQuickGprArgs) { 522 IncGprIndex(); 523 } 524 break; 525 case Primitive::kPrimFloat: 526 is_split_long_or_double_ = false; 527 Visit(); 528 stack_index_++; 529 if (kQuickSoftFloatAbi) { 530 if (gpr_index_ < kNumQuickGprArgs) { 531 IncGprIndex(); 532 } 533 } else { 534 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) { 535 IncFprIndex(); 536 if (kQuickDoubleRegAlignedFloatBackFilled) { 537 // Double should not overlap with float. 538 // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4. 539 fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2)); 540 // Float should not overlap with double. 541 if (fpr_index_ % 2 == 0) { 542 fpr_index_ = std::max(fpr_double_index_, fpr_index_); 543 } 544 } else if (kQuickSkipOddFpRegisters) { 545 IncFprIndex(); 546 } 547 } 548 } 549 break; 550 case Primitive::kPrimDouble: 551 case Primitive::kPrimLong: 552 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) { 553 if (cur_type_ == Primitive::kPrimLong && 554#if defined(__mips__) && !defined(__LP64__) 555 (gpr_index_ == 0 || gpr_index_ == 2) && 556#else 557 gpr_index_ == 0 && 558#endif 559 kAlignPairRegister) { 560 // Currently, this is only for ARM and MIPS, where we align long parameters with 561 // even-numbered registers by skipping R1 (on ARM) or A1(A3) (on MIPS) and using 562 // R2 (on ARM) or A2(T0) (on MIPS) instead. 563 IncGprIndex(); 564 } 565 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) && 566 ((gpr_index_ + 1) == kNumQuickGprArgs); 567 if (!kSplitPairAcrossRegisterAndStack && is_split_long_or_double_) { 568 // We don't want to split this. Pass over this register. 569 gpr_index_++; 570 is_split_long_or_double_ = false; 571 } 572 Visit(); 573 if (kBytesStackArgLocation == 4) { 574 stack_index_+= 2; 575 } else { 576 CHECK_EQ(kBytesStackArgLocation, 8U); 577 stack_index_++; 578 } 579 if (gpr_index_ < kNumQuickGprArgs) { 580 IncGprIndex(); 581 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) { 582 if (gpr_index_ < kNumQuickGprArgs) { 583 IncGprIndex(); 584 } 585 } 586 } 587 } else { 588 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) && 589 ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled; 590 Visit(); 591 if (kBytesStackArgLocation == 4) { 592 stack_index_+= 2; 593 } else { 594 CHECK_EQ(kBytesStackArgLocation, 8U); 595 stack_index_++; 596 } 597 if (kQuickDoubleRegAlignedFloatBackFilled) { 598 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) { 599 fpr_double_index_ += 2; 600 // Float should not overlap with double. 601 if (fpr_index_ % 2 == 0) { 602 fpr_index_ = std::max(fpr_double_index_, fpr_index_); 603 } 604 } 605 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) { 606 IncFprIndex(); 607 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) { 608 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) { 609 IncFprIndex(); 610 } 611 } 612 } 613 } 614 break; 615 default: 616 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_; 617 } 618 } 619 } 620 621 protected: 622 const bool is_static_; 623 const char* const shorty_; 624 const uint32_t shorty_len_; 625 626 private: 627 uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame. 628 uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame. 629 uint8_t* const stack_args_; // Address of stack arguments in caller's frame. 630 uint32_t gpr_index_; // Index into spilled GPRs. 631 // Index into spilled FPRs. 632 // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_ 633 // holds a higher register number. 634 uint32_t fpr_index_; 635 // Index into spilled FPRs for aligned double. 636 // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in 637 // terms of singles, may be behind fpr_index. 638 uint32_t fpr_double_index_; 639 uint32_t stack_index_; // Index into arguments on the stack. 640 // The current type of argument during VisitArguments. 641 Primitive::Type cur_type_; 642 // Does a 64bit parameter straddle the register and stack arguments? 643 bool is_split_long_or_double_; 644}; 645 646// Returns the 'this' object of a proxy method. This function is only used by StackVisitor. It 647// allows to use the QuickArgumentVisitor constants without moving all the code in its own module. 648extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp) 649 REQUIRES_SHARED(Locks::mutator_lock_) { 650 return QuickArgumentVisitor::GetProxyThisObject(sp); 651} 652 653// Visits arguments on the stack placing them into the shadow frame. 654class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor { 655 public: 656 BuildQuickShadowFrameVisitor(ArtMethod** sp, bool is_static, const char* shorty, 657 uint32_t shorty_len, ShadowFrame* sf, size_t first_arg_reg) : 658 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {} 659 660 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE; 661 662 private: 663 ShadowFrame* const sf_; 664 uint32_t cur_reg_; 665 666 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor); 667}; 668 669void BuildQuickShadowFrameVisitor::Visit() { 670 Primitive::Type type = GetParamPrimitiveType(); 671 switch (type) { 672 case Primitive::kPrimLong: // Fall-through. 673 case Primitive::kPrimDouble: 674 if (IsSplitLongOrDouble()) { 675 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam()); 676 } else { 677 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress())); 678 } 679 ++cur_reg_; 680 break; 681 case Primitive::kPrimNot: { 682 StackReference<mirror::Object>* stack_ref = 683 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress()); 684 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr()); 685 } 686 break; 687 case Primitive::kPrimBoolean: // Fall-through. 688 case Primitive::kPrimByte: // Fall-through. 689 case Primitive::kPrimChar: // Fall-through. 690 case Primitive::kPrimShort: // Fall-through. 691 case Primitive::kPrimInt: // Fall-through. 692 case Primitive::kPrimFloat: 693 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress())); 694 break; 695 case Primitive::kPrimVoid: 696 LOG(FATAL) << "UNREACHABLE"; 697 UNREACHABLE(); 698 } 699 ++cur_reg_; 700} 701 702// Don't inline. See b/65159206. 703NO_INLINE 704static void HandleDeoptimization(JValue* result, 705 ArtMethod* method, 706 ShadowFrame* deopt_frame, 707 ManagedStack* fragment) 708 REQUIRES_SHARED(Locks::mutator_lock_) { 709 // Coming from partial-fragment deopt. 710 Thread* self = Thread::Current(); 711 if (kIsDebugBuild) { 712 // Sanity-check: are the methods as expected? We check that the last shadow frame (the bottom 713 // of the call-stack) corresponds to the called method. 714 ShadowFrame* linked = deopt_frame; 715 while (linked->GetLink() != nullptr) { 716 linked = linked->GetLink(); 717 } 718 CHECK_EQ(method, linked->GetMethod()) << method->PrettyMethod() << " " 719 << ArtMethod::PrettyMethod(linked->GetMethod()); 720 } 721 722 if (VLOG_IS_ON(deopt)) { 723 // Print out the stack to verify that it was a partial-fragment deopt. 724 LOG(INFO) << "Continue-ing from deopt. Stack is:"; 725 QuickExceptionHandler::DumpFramesWithType(self, true); 726 } 727 728 ObjPtr<mirror::Throwable> pending_exception; 729 bool from_code = false; 730 DeoptimizationMethodType method_type; 731 self->PopDeoptimizationContext(/* out */ result, 732 /* out */ &pending_exception, 733 /* out */ &from_code, 734 /* out */ &method_type); 735 736 // Push a transition back into managed code onto the linked list in thread. 737 self->PushManagedStackFragment(fragment); 738 739 // Ensure that the stack is still in order. 740 if (kIsDebugBuild) { 741 class DummyStackVisitor : public StackVisitor { 742 public: 743 explicit DummyStackVisitor(Thread* self_in) REQUIRES_SHARED(Locks::mutator_lock_) 744 : StackVisitor(self_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {} 745 746 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { 747 // Nothing to do here. In a debug build, SanityCheckFrame will do the work in the walking 748 // logic. Just always say we want to continue. 749 return true; 750 } 751 }; 752 DummyStackVisitor dsv(self); 753 dsv.WalkStack(); 754 } 755 756 // Restore the exception that was pending before deoptimization then interpret the 757 // deoptimized frames. 758 if (pending_exception != nullptr) { 759 self->SetException(pending_exception); 760 } 761 interpreter::EnterInterpreterFromDeoptimize(self, 762 deopt_frame, 763 result, 764 from_code, 765 DeoptimizationMethodType::kDefault); 766} 767 768extern "C" uint64_t artQuickToInterpreterBridge(ArtMethod* method, Thread* self, ArtMethod** sp) 769 REQUIRES_SHARED(Locks::mutator_lock_) { 770 // Ensure we don't get thread suspension until the object arguments are safely in the shadow 771 // frame. 772 ScopedQuickEntrypointChecks sqec(self); 773 774 if (UNLIKELY(!method->IsInvokable())) { 775 method->ThrowInvocationTimeError(); 776 return 0; 777 } 778 779 JValue tmp_value; 780 ShadowFrame* deopt_frame = self->PopStackedShadowFrame( 781 StackedShadowFrameType::kDeoptimizationShadowFrame, false); 782 ManagedStack fragment; 783 784 DCHECK(!method->IsNative()) << method->PrettyMethod(); 785 uint32_t shorty_len = 0; 786 ArtMethod* non_proxy_method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize); 787 const DexFile::CodeItem* code_item = non_proxy_method->GetCodeItem(); 788 DCHECK(code_item != nullptr) << method->PrettyMethod(); 789 const char* shorty = non_proxy_method->GetShorty(&shorty_len); 790 791 JValue result; 792 793 if (UNLIKELY(deopt_frame != nullptr)) { 794 HandleDeoptimization(&result, method, deopt_frame, &fragment); 795 } else { 796 const char* old_cause = self->StartAssertNoThreadSuspension( 797 "Building interpreter shadow frame"); 798 uint16_t num_regs = code_item->registers_size_; 799 // No last shadow coming from quick. 800 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr = 801 CREATE_SHADOW_FRAME(num_regs, /* link */ nullptr, method, /* dex pc */ 0); 802 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get(); 803 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_; 804 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len, 805 shadow_frame, first_arg_reg); 806 shadow_frame_builder.VisitArguments(); 807 const bool needs_initialization = 808 method->IsStatic() && !method->GetDeclaringClass()->IsInitialized(); 809 // Push a transition back into managed code onto the linked list in thread. 810 self->PushManagedStackFragment(&fragment); 811 self->PushShadowFrame(shadow_frame); 812 self->EndAssertNoThreadSuspension(old_cause); 813 814 if (needs_initialization) { 815 // Ensure static method's class is initialized. 816 StackHandleScope<1> hs(self); 817 Handle<mirror::Class> h_class(hs.NewHandle(shadow_frame->GetMethod()->GetDeclaringClass())); 818 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) { 819 DCHECK(Thread::Current()->IsExceptionPending()) 820 << shadow_frame->GetMethod()->PrettyMethod(); 821 self->PopManagedStackFragment(fragment); 822 return 0; 823 } 824 } 825 826 result = interpreter::EnterInterpreterFromEntryPoint(self, code_item, shadow_frame); 827 } 828 829 // Pop transition. 830 self->PopManagedStackFragment(fragment); 831 832 // Request a stack deoptimization if needed 833 ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp); 834 uintptr_t caller_pc = QuickArgumentVisitor::GetCallingPc(sp); 835 // If caller_pc is the instrumentation exit stub, the stub will check to see if deoptimization 836 // should be done and it knows the real return pc. 837 if (UNLIKELY(caller_pc != reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) && 838 Dbg::IsForcedInterpreterNeededForUpcall(self, caller))) { 839 if (!Runtime::Current()->IsAsyncDeoptimizeable(caller_pc)) { 840 LOG(WARNING) << "Got a deoptimization request on un-deoptimizable method " 841 << caller->PrettyMethod(); 842 } else { 843 // Push the context of the deoptimization stack so we can restore the return value and the 844 // exception before executing the deoptimized frames. 845 self->PushDeoptimizationContext( 846 result, 847 shorty[0] == 'L' || shorty[0] == '[', /* class or array */ 848 self->GetException(), 849 false /* from_code */, 850 DeoptimizationMethodType::kDefault); 851 852 // Set special exception to cause deoptimization. 853 self->SetException(Thread::GetDeoptimizationException()); 854 } 855 } 856 857 // No need to restore the args since the method has already been run by the interpreter. 858 return result.GetJ(); 859} 860 861// Visits arguments on the stack placing them into the args vector, Object* arguments are converted 862// to jobjects. 863class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor { 864 public: 865 BuildQuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty, uint32_t shorty_len, 866 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) : 867 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {} 868 869 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE; 870 871 void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_); 872 873 private: 874 ScopedObjectAccessUnchecked* const soa_; 875 std::vector<jvalue>* const args_; 876 // References which we must update when exiting in case the GC moved the objects. 877 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_; 878 879 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor); 880}; 881 882void BuildQuickArgumentVisitor::Visit() { 883 jvalue val; 884 Primitive::Type type = GetParamPrimitiveType(); 885 switch (type) { 886 case Primitive::kPrimNot: { 887 StackReference<mirror::Object>* stack_ref = 888 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress()); 889 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr()); 890 references_.push_back(std::make_pair(val.l, stack_ref)); 891 break; 892 } 893 case Primitive::kPrimLong: // Fall-through. 894 case Primitive::kPrimDouble: 895 if (IsSplitLongOrDouble()) { 896 val.j = ReadSplitLongParam(); 897 } else { 898 val.j = *reinterpret_cast<jlong*>(GetParamAddress()); 899 } 900 break; 901 case Primitive::kPrimBoolean: // Fall-through. 902 case Primitive::kPrimByte: // Fall-through. 903 case Primitive::kPrimChar: // Fall-through. 904 case Primitive::kPrimShort: // Fall-through. 905 case Primitive::kPrimInt: // Fall-through. 906 case Primitive::kPrimFloat: 907 val.i = *reinterpret_cast<jint*>(GetParamAddress()); 908 break; 909 case Primitive::kPrimVoid: 910 LOG(FATAL) << "UNREACHABLE"; 911 UNREACHABLE(); 912 } 913 args_->push_back(val); 914} 915 916void BuildQuickArgumentVisitor::FixupReferences() { 917 // Fixup any references which may have changed. 918 for (const auto& pair : references_) { 919 pair.second->Assign(soa_->Decode<mirror::Object>(pair.first)); 920 soa_->Env()->DeleteLocalRef(pair.first); 921 } 922} 923// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method 924// which is responsible for recording callee save registers. We explicitly place into jobjects the 925// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a 926// field within the proxy object, which will box the primitive arguments and deal with error cases. 927extern "C" uint64_t artQuickProxyInvokeHandler( 928 ArtMethod* proxy_method, mirror::Object* receiver, Thread* self, ArtMethod** sp) 929 REQUIRES_SHARED(Locks::mutator_lock_) { 930 DCHECK(proxy_method->IsProxyMethod()) << proxy_method->PrettyMethod(); 931 DCHECK(receiver->GetClass()->IsProxyClass()) << proxy_method->PrettyMethod(); 932 // Ensure we don't get thread suspension until the object arguments are safely in jobjects. 933 const char* old_cause = 934 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments"); 935 // Register the top of the managed stack, making stack crawlable. 936 DCHECK_EQ((*sp), proxy_method) << proxy_method->PrettyMethod(); 937 self->VerifyStack(); 938 // Start new JNI local reference state. 939 JNIEnvExt* env = self->GetJniEnv(); 940 ScopedObjectAccessUnchecked soa(env); 941 ScopedJniEnvLocalRefState env_state(env); 942 // Create local ref. copies of proxy method and the receiver. 943 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver); 944 945 // Placing arguments into args vector and remove the receiver. 946 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize); 947 CHECK(!non_proxy_method->IsStatic()) << proxy_method->PrettyMethod() << " " 948 << non_proxy_method->PrettyMethod(); 949 std::vector<jvalue> args; 950 uint32_t shorty_len = 0; 951 const char* shorty = non_proxy_method->GetShorty(&shorty_len); 952 BuildQuickArgumentVisitor local_ref_visitor(sp, false, shorty, shorty_len, &soa, &args); 953 954 local_ref_visitor.VisitArguments(); 955 DCHECK_GT(args.size(), 0U) << proxy_method->PrettyMethod(); 956 args.erase(args.begin()); 957 958 // Convert proxy method into expected interface method. 959 ArtMethod* interface_method = proxy_method->FindOverriddenMethod(kRuntimePointerSize); 960 DCHECK(interface_method != nullptr) << proxy_method->PrettyMethod(); 961 DCHECK(!interface_method->IsProxyMethod()) << interface_method->PrettyMethod(); 962 self->EndAssertNoThreadSuspension(old_cause); 963 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize); 964 DCHECK(!Runtime::Current()->IsActiveTransaction()); 965 ObjPtr<mirror::Method> interface_reflect_method = 966 mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(), interface_method); 967 if (interface_reflect_method == nullptr) { 968 soa.Self()->AssertPendingOOMException(); 969 return 0; 970 } 971 jobject interface_method_jobj = soa.AddLocalReference<jobject>(interface_reflect_method); 972 973 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code 974 // that performs allocations. 975 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args); 976 // Restore references which might have moved. 977 local_ref_visitor.FixupReferences(); 978 return result.GetJ(); 979} 980 981// Read object references held in arguments from quick frames and place in a JNI local references, 982// so they don't get garbage collected. 983class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor { 984 public: 985 RememberForGcArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty, 986 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) : 987 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {} 988 989 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE; 990 991 void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_); 992 993 private: 994 ScopedObjectAccessUnchecked* const soa_; 995 // References which we must update when exiting in case the GC moved the objects. 996 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_; 997 998 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor); 999}; 1000 1001void RememberForGcArgumentVisitor::Visit() { 1002 if (IsParamAReference()) { 1003 StackReference<mirror::Object>* stack_ref = 1004 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress()); 1005 jobject reference = 1006 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr()); 1007 references_.push_back(std::make_pair(reference, stack_ref)); 1008 } 1009} 1010 1011void RememberForGcArgumentVisitor::FixupReferences() { 1012 // Fixup any references which may have changed. 1013 for (const auto& pair : references_) { 1014 pair.second->Assign(soa_->Decode<mirror::Object>(pair.first)); 1015 soa_->Env()->DeleteLocalRef(pair.first); 1016 } 1017} 1018 1019extern "C" const void* artInstrumentationMethodEntryFromCode(ArtMethod* method, 1020 mirror::Object* this_object, 1021 Thread* self, 1022 ArtMethod** sp) 1023 REQUIRES_SHARED(Locks::mutator_lock_) { 1024 const void* result; 1025 // Instrumentation changes the stack. Thus, when exiting, the stack cannot be verified, so skip 1026 // that part. 1027 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false); 1028 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); 1029 if (instrumentation->IsDeoptimized(method)) { 1030 result = GetQuickToInterpreterBridge(); 1031 } else { 1032 result = instrumentation->GetQuickCodeFor(method, kRuntimePointerSize); 1033 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge(result)); 1034 } 1035 1036 bool interpreter_entry = (result == GetQuickToInterpreterBridge()); 1037 bool is_static = method->IsStatic(); 1038 uint32_t shorty_len; 1039 const char* shorty = 1040 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len); 1041 1042 ScopedObjectAccessUnchecked soa(self); 1043 RememberForGcArgumentVisitor visitor(sp, is_static, shorty, shorty_len, &soa); 1044 visitor.VisitArguments(); 1045 1046 instrumentation->PushInstrumentationStackFrame(self, 1047 is_static ? nullptr : this_object, 1048 method, 1049 QuickArgumentVisitor::GetCallingPc(sp), 1050 interpreter_entry); 1051 1052 visitor.FixupReferences(); 1053 if (UNLIKELY(self->IsExceptionPending())) { 1054 return nullptr; 1055 } 1056 CHECK(result != nullptr) << method->PrettyMethod(); 1057 return result; 1058} 1059 1060extern "C" TwoWordReturn artInstrumentationMethodExitFromCode(Thread* self, 1061 ArtMethod** sp, 1062 uint64_t* gpr_result, 1063 uint64_t* fpr_result) 1064 REQUIRES_SHARED(Locks::mutator_lock_) { 1065 DCHECK_EQ(reinterpret_cast<uintptr_t>(self), reinterpret_cast<uintptr_t>(Thread::Current())); 1066 CHECK(gpr_result != nullptr); 1067 CHECK(fpr_result != nullptr); 1068 // Instrumentation exit stub must not be entered with a pending exception. 1069 CHECK(!self->IsExceptionPending()) << "Enter instrumentation exit stub with pending exception " 1070 << self->GetException()->Dump(); 1071 // Compute address of return PC and sanity check that it currently holds 0. 1072 size_t return_pc_offset = GetCalleeSaveReturnPcOffset(kRuntimeISA, 1073 CalleeSaveType::kSaveEverything); 1074 uintptr_t* return_pc = reinterpret_cast<uintptr_t*>(reinterpret_cast<uint8_t*>(sp) + 1075 return_pc_offset); 1076 CHECK_EQ(*return_pc, 0U); 1077 1078 // Pop the frame filling in the return pc. The low half of the return value is 0 when 1079 // deoptimization shouldn't be performed with the high-half having the return address. When 1080 // deoptimization should be performed the low half is zero and the high-half the address of the 1081 // deoptimization entry point. 1082 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); 1083 TwoWordReturn return_or_deoptimize_pc = instrumentation->PopInstrumentationStackFrame( 1084 self, return_pc, gpr_result, fpr_result); 1085 if (self->IsExceptionPending()) { 1086 return GetTwoWordFailureValue(); 1087 } 1088 return return_or_deoptimize_pc; 1089} 1090 1091// Lazily resolve a method for quick. Called by stub code. 1092extern "C" const void* artQuickResolutionTrampoline( 1093 ArtMethod* called, mirror::Object* receiver, Thread* self, ArtMethod** sp) 1094 REQUIRES_SHARED(Locks::mutator_lock_) { 1095 // The resolution trampoline stashes the resolved method into the callee-save frame to transport 1096 // it. Thus, when exiting, the stack cannot be verified (as the resolved method most likely 1097 // does not have the same stack layout as the callee-save method). 1098 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false); 1099 // Start new JNI local reference state 1100 JNIEnvExt* env = self->GetJniEnv(); 1101 ScopedObjectAccessUnchecked soa(env); 1102 ScopedJniEnvLocalRefState env_state(env); 1103 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up"); 1104 1105 // Compute details about the called method (avoid GCs) 1106 ClassLinker* linker = Runtime::Current()->GetClassLinker(); 1107 InvokeType invoke_type; 1108 MethodReference called_method(nullptr, 0); 1109 const bool called_method_known_on_entry = !called->IsRuntimeMethod(); 1110 ArtMethod* caller = nullptr; 1111 if (!called_method_known_on_entry) { 1112 caller = QuickArgumentVisitor::GetCallingMethod(sp); 1113 called_method.dex_file = caller->GetDexFile(); 1114 1115 InvokeType stack_map_invoke_type; 1116 uint32_t stack_map_dex_method_idx; 1117 const bool found_stack_map = QuickArgumentVisitor::GetInvokeType(sp, 1118 &stack_map_invoke_type, 1119 &stack_map_dex_method_idx); 1120 // For debug builds, we make sure both of the paths are consistent by also looking at the dex 1121 // code. 1122 if (!found_stack_map || kIsDebugBuild) { 1123 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp); 1124 const DexFile::CodeItem* code; 1125 code = caller->GetCodeItem(); 1126 CHECK_LT(dex_pc, code->insns_size_in_code_units_); 1127 const Instruction& instr = code->InstructionAt(dex_pc); 1128 Instruction::Code instr_code = instr.Opcode(); 1129 bool is_range; 1130 switch (instr_code) { 1131 case Instruction::INVOKE_DIRECT: 1132 invoke_type = kDirect; 1133 is_range = false; 1134 break; 1135 case Instruction::INVOKE_DIRECT_RANGE: 1136 invoke_type = kDirect; 1137 is_range = true; 1138 break; 1139 case Instruction::INVOKE_STATIC: 1140 invoke_type = kStatic; 1141 is_range = false; 1142 break; 1143 case Instruction::INVOKE_STATIC_RANGE: 1144 invoke_type = kStatic; 1145 is_range = true; 1146 break; 1147 case Instruction::INVOKE_SUPER: 1148 invoke_type = kSuper; 1149 is_range = false; 1150 break; 1151 case Instruction::INVOKE_SUPER_RANGE: 1152 invoke_type = kSuper; 1153 is_range = true; 1154 break; 1155 case Instruction::INVOKE_VIRTUAL: 1156 invoke_type = kVirtual; 1157 is_range = false; 1158 break; 1159 case Instruction::INVOKE_VIRTUAL_RANGE: 1160 invoke_type = kVirtual; 1161 is_range = true; 1162 break; 1163 case Instruction::INVOKE_INTERFACE: 1164 invoke_type = kInterface; 1165 is_range = false; 1166 break; 1167 case Instruction::INVOKE_INTERFACE_RANGE: 1168 invoke_type = kInterface; 1169 is_range = true; 1170 break; 1171 default: 1172 LOG(FATAL) << "Unexpected call into trampoline: " << instr.DumpString(nullptr); 1173 UNREACHABLE(); 1174 } 1175 called_method.index = (is_range) ? instr.VRegB_3rc() : instr.VRegB_35c(); 1176 // Check that the invoke matches what we expected, note that this path only happens for debug 1177 // builds. 1178 if (found_stack_map) { 1179 DCHECK_EQ(stack_map_invoke_type, invoke_type); 1180 if (invoke_type != kSuper) { 1181 // Super may be sharpened. 1182 DCHECK_EQ(stack_map_dex_method_idx, called_method.index) 1183 << called_method.dex_file->PrettyMethod(stack_map_dex_method_idx) << " " 1184 << called_method.PrettyMethod(); 1185 } 1186 } else { 1187 VLOG(dex) << "Accessed dex file for invoke " << invoke_type << " " 1188 << called_method.index; 1189 } 1190 } else { 1191 invoke_type = stack_map_invoke_type; 1192 called_method.index = stack_map_dex_method_idx; 1193 } 1194 } else { 1195 invoke_type = kStatic; 1196 called_method.dex_file = called->GetDexFile(); 1197 called_method.index = called->GetDexMethodIndex(); 1198 } 1199 uint32_t shorty_len; 1200 const char* shorty = 1201 called_method.dex_file->GetMethodShorty(called_method.GetMethodId(), &shorty_len); 1202 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa); 1203 visitor.VisitArguments(); 1204 self->EndAssertNoThreadSuspension(old_cause); 1205 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface; 1206 // Resolve method filling in dex cache. 1207 if (!called_method_known_on_entry) { 1208 StackHandleScope<1> hs(self); 1209 mirror::Object* dummy = nullptr; 1210 HandleWrapper<mirror::Object> h_receiver( 1211 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy)); 1212 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file); 1213 called = linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>( 1214 self, called_method.index, caller, invoke_type); 1215 1216 // Update .bss entry in oat file if any. 1217 if (called != nullptr && called_method.dex_file->GetOatDexFile() != nullptr) { 1218 size_t bss_offset = IndexBssMappingLookup::GetBssOffset( 1219 called_method.dex_file->GetOatDexFile()->GetMethodBssMapping(), 1220 called_method.index, 1221 called_method.dex_file->NumMethodIds(), 1222 static_cast<size_t>(kRuntimePointerSize)); 1223 if (bss_offset != IndexBssMappingLookup::npos) { 1224 DCHECK_ALIGNED(bss_offset, static_cast<size_t>(kRuntimePointerSize)); 1225 const OatFile* oat_file = called_method.dex_file->GetOatDexFile()->GetOatFile(); 1226 ArtMethod** method_entry = reinterpret_cast<ArtMethod**>(const_cast<uint8_t*>( 1227 oat_file->BssBegin() + bss_offset)); 1228 DCHECK_GE(method_entry, oat_file->GetBssMethods().data()); 1229 DCHECK_LT(method_entry, 1230 oat_file->GetBssMethods().data() + oat_file->GetBssMethods().size()); 1231 *method_entry = called; 1232 } 1233 } 1234 } 1235 const void* code = nullptr; 1236 if (LIKELY(!self->IsExceptionPending())) { 1237 // Incompatible class change should have been handled in resolve method. 1238 CHECK(!called->CheckIncompatibleClassChange(invoke_type)) 1239 << called->PrettyMethod() << " " << invoke_type; 1240 if (virtual_or_interface || invoke_type == kSuper) { 1241 // Refine called method based on receiver for kVirtual/kInterface, and 1242 // caller for kSuper. 1243 ArtMethod* orig_called = called; 1244 if (invoke_type == kVirtual) { 1245 CHECK(receiver != nullptr) << invoke_type; 1246 called = receiver->GetClass()->FindVirtualMethodForVirtual(called, kRuntimePointerSize); 1247 } else if (invoke_type == kInterface) { 1248 CHECK(receiver != nullptr) << invoke_type; 1249 called = receiver->GetClass()->FindVirtualMethodForInterface(called, kRuntimePointerSize); 1250 } else { 1251 DCHECK_EQ(invoke_type, kSuper); 1252 CHECK(caller != nullptr) << invoke_type; 1253 ObjPtr<mirror::Class> ref_class = linker->LookupResolvedType( 1254 caller->GetDexFile()->GetMethodId(called_method.index).class_idx_, caller); 1255 if (ref_class->IsInterface()) { 1256 called = ref_class->FindVirtualMethodForInterfaceSuper(called, kRuntimePointerSize); 1257 } else { 1258 called = caller->GetDeclaringClass()->GetSuperClass()->GetVTableEntry( 1259 called->GetMethodIndex(), kRuntimePointerSize); 1260 } 1261 } 1262 1263 CHECK(called != nullptr) << orig_called->PrettyMethod() << " " 1264 << mirror::Object::PrettyTypeOf(receiver) << " " 1265 << invoke_type << " " << orig_called->GetVtableIndex(); 1266 } 1267 1268 // Ensure that the called method's class is initialized. 1269 StackHandleScope<1> hs(soa.Self()); 1270 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass())); 1271 linker->EnsureInitialized(soa.Self(), called_class, true, true); 1272 if (LIKELY(called_class->IsInitialized())) { 1273 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) { 1274 // If we are single-stepping or the called method is deoptimized (by a 1275 // breakpoint, for example), then we have to execute the called method 1276 // with the interpreter. 1277 code = GetQuickToInterpreterBridge(); 1278 } else if (UNLIKELY(Dbg::IsForcedInstrumentationNeededForResolution(self, caller))) { 1279 // If the caller is deoptimized (by a breakpoint, for example), we have to 1280 // continue its execution with interpreter when returning from the called 1281 // method. Because we do not want to execute the called method with the 1282 // interpreter, we wrap its execution into the instrumentation stubs. 1283 // When the called method returns, it will execute the instrumentation 1284 // exit hook that will determine the need of the interpreter with a call 1285 // to Dbg::IsForcedInterpreterNeededForUpcall and deoptimize the stack if 1286 // it is needed. 1287 code = GetQuickInstrumentationEntryPoint(); 1288 } else { 1289 code = called->GetEntryPointFromQuickCompiledCode(); 1290 } 1291 } else if (called_class->IsInitializing()) { 1292 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) { 1293 // If we are single-stepping or the called method is deoptimized (by a 1294 // breakpoint, for example), then we have to execute the called method 1295 // with the interpreter. 1296 code = GetQuickToInterpreterBridge(); 1297 } else if (invoke_type == kStatic) { 1298 // Class is still initializing, go to oat and grab code (trampoline must be left in place 1299 // until class is initialized to stop races between threads). 1300 code = linker->GetQuickOatCodeFor(called); 1301 } else { 1302 // No trampoline for non-static methods. 1303 code = called->GetEntryPointFromQuickCompiledCode(); 1304 } 1305 } else { 1306 DCHECK(called_class->IsErroneous()); 1307 } 1308 } 1309 CHECK_EQ(code == nullptr, self->IsExceptionPending()); 1310 // Fixup any locally saved objects may have moved during a GC. 1311 visitor.FixupReferences(); 1312 // Place called method in callee-save frame to be placed as first argument to quick method. 1313 *sp = called; 1314 1315 return code; 1316} 1317 1318/* 1319 * This class uses a couple of observations to unite the different calling conventions through 1320 * a few constants. 1321 * 1322 * 1) Number of registers used for passing is normally even, so counting down has no penalty for 1323 * possible alignment. 1324 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point 1325 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote 1326 * when we have to split things 1327 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats 1328 * and we can use Int handling directly. 1329 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code 1330 * necessary when widening. Also, widening of Ints will take place implicitly, and the 1331 * extension should be compatible with Aarch64, which mandates copying the available bits 1332 * into LSB and leaving the rest unspecified. 1333 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on 1334 * the stack. 1335 * 6) There is only little endian. 1336 * 1337 * 1338 * Actual work is supposed to be done in a delegate of the template type. The interface is as 1339 * follows: 1340 * 1341 * void PushGpr(uintptr_t): Add a value for the next GPR 1342 * 1343 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need 1344 * padding, that is, think the architecture is 32b and aligns 64b. 1345 * 1346 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to 1347 * split this if necessary. The current state will have aligned, if 1348 * necessary. 1349 * 1350 * void PushStack(uintptr_t): Push a value to the stack. 1351 * 1352 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr, 1353 * as this might be important for null initialization. 1354 * Must return the jobject, that is, the reference to the 1355 * entry in the HandleScope (nullptr if necessary). 1356 * 1357 */ 1358template<class T> class BuildNativeCallFrameStateMachine { 1359 public: 1360#if defined(__arm__) 1361 // TODO: These are all dummy values! 1362 static constexpr bool kNativeSoftFloatAbi = true; 1363 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3 1364 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs. 1365 1366 static constexpr size_t kRegistersNeededForLong = 2; 1367 static constexpr size_t kRegistersNeededForDouble = 2; 1368 static constexpr bool kMultiRegistersAligned = true; 1369 static constexpr bool kMultiFPRegistersWidened = false; 1370 static constexpr bool kMultiGPRegistersWidened = false; 1371 static constexpr bool kAlignLongOnStack = true; 1372 static constexpr bool kAlignDoubleOnStack = true; 1373#elif defined(__aarch64__) 1374 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI. 1375 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs. 1376 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs. 1377 1378 static constexpr size_t kRegistersNeededForLong = 1; 1379 static constexpr size_t kRegistersNeededForDouble = 1; 1380 static constexpr bool kMultiRegistersAligned = false; 1381 static constexpr bool kMultiFPRegistersWidened = false; 1382 static constexpr bool kMultiGPRegistersWidened = false; 1383 static constexpr bool kAlignLongOnStack = false; 1384 static constexpr bool kAlignDoubleOnStack = false; 1385#elif defined(__mips__) && !defined(__LP64__) 1386 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI. 1387 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs. 1388 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs. 1389 1390 static constexpr size_t kRegistersNeededForLong = 2; 1391 static constexpr size_t kRegistersNeededForDouble = 2; 1392 static constexpr bool kMultiRegistersAligned = true; 1393 static constexpr bool kMultiFPRegistersWidened = true; 1394 static constexpr bool kMultiGPRegistersWidened = false; 1395 static constexpr bool kAlignLongOnStack = true; 1396 static constexpr bool kAlignDoubleOnStack = true; 1397#elif defined(__mips__) && defined(__LP64__) 1398 // Let the code prepare GPRs only and we will load the FPRs with same data. 1399 static constexpr bool kNativeSoftFloatAbi = true; 1400 static constexpr size_t kNumNativeGprArgs = 8; 1401 static constexpr size_t kNumNativeFprArgs = 0; 1402 1403 static constexpr size_t kRegistersNeededForLong = 1; 1404 static constexpr size_t kRegistersNeededForDouble = 1; 1405 static constexpr bool kMultiRegistersAligned = false; 1406 static constexpr bool kMultiFPRegistersWidened = false; 1407 static constexpr bool kMultiGPRegistersWidened = true; 1408 static constexpr bool kAlignLongOnStack = false; 1409 static constexpr bool kAlignDoubleOnStack = false; 1410#elif defined(__i386__) 1411 // TODO: Check these! 1412 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp 1413 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs. 1414 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs. 1415 1416 static constexpr size_t kRegistersNeededForLong = 2; 1417 static constexpr size_t kRegistersNeededForDouble = 2; 1418 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways 1419 static constexpr bool kMultiFPRegistersWidened = false; 1420 static constexpr bool kMultiGPRegistersWidened = false; 1421 static constexpr bool kAlignLongOnStack = false; 1422 static constexpr bool kAlignDoubleOnStack = false; 1423#elif defined(__x86_64__) 1424 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI. 1425 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs. 1426 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs. 1427 1428 static constexpr size_t kRegistersNeededForLong = 1; 1429 static constexpr size_t kRegistersNeededForDouble = 1; 1430 static constexpr bool kMultiRegistersAligned = false; 1431 static constexpr bool kMultiFPRegistersWidened = false; 1432 static constexpr bool kMultiGPRegistersWidened = false; 1433 static constexpr bool kAlignLongOnStack = false; 1434 static constexpr bool kAlignDoubleOnStack = false; 1435#else 1436#error "Unsupported architecture" 1437#endif 1438 1439 public: 1440 explicit BuildNativeCallFrameStateMachine(T* delegate) 1441 : gpr_index_(kNumNativeGprArgs), 1442 fpr_index_(kNumNativeFprArgs), 1443 stack_entries_(0), 1444 delegate_(delegate) { 1445 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff 1446 // the next register is even; counting down is just to make the compiler happy... 1447 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even"); 1448 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even"); 1449 } 1450 1451 virtual ~BuildNativeCallFrameStateMachine() {} 1452 1453 bool HavePointerGpr() const { 1454 return gpr_index_ > 0; 1455 } 1456 1457 void AdvancePointer(const void* val) { 1458 if (HavePointerGpr()) { 1459 gpr_index_--; 1460 PushGpr(reinterpret_cast<uintptr_t>(val)); 1461 } else { 1462 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b 1463 PushStack(reinterpret_cast<uintptr_t>(val)); 1464 gpr_index_ = 0; 1465 } 1466 } 1467 1468 bool HaveHandleScopeGpr() const { 1469 return gpr_index_ > 0; 1470 } 1471 1472 void AdvanceHandleScope(mirror::Object* ptr) REQUIRES_SHARED(Locks::mutator_lock_) { 1473 uintptr_t handle = PushHandle(ptr); 1474 if (HaveHandleScopeGpr()) { 1475 gpr_index_--; 1476 PushGpr(handle); 1477 } else { 1478 stack_entries_++; 1479 PushStack(handle); 1480 gpr_index_ = 0; 1481 } 1482 } 1483 1484 bool HaveIntGpr() const { 1485 return gpr_index_ > 0; 1486 } 1487 1488 void AdvanceInt(uint32_t val) { 1489 if (HaveIntGpr()) { 1490 gpr_index_--; 1491 if (kMultiGPRegistersWidened) { 1492 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t)); 1493 PushGpr(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val))); 1494 } else { 1495 PushGpr(val); 1496 } 1497 } else { 1498 stack_entries_++; 1499 if (kMultiGPRegistersWidened) { 1500 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t)); 1501 PushStack(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val))); 1502 } else { 1503 PushStack(val); 1504 } 1505 gpr_index_ = 0; 1506 } 1507 } 1508 1509 bool HaveLongGpr() const { 1510 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0); 1511 } 1512 1513 bool LongGprNeedsPadding() const { 1514 return kRegistersNeededForLong > 1 && // only pad when using multiple registers 1515 kAlignLongOnStack && // and when it needs alignment 1516 (gpr_index_ & 1) == 1; // counter is odd, see constructor 1517 } 1518 1519 bool LongStackNeedsPadding() const { 1520 return kRegistersNeededForLong > 1 && // only pad when using multiple registers 1521 kAlignLongOnStack && // and when it needs 8B alignment 1522 (stack_entries_ & 1) == 1; // counter is odd 1523 } 1524 1525 void AdvanceLong(uint64_t val) { 1526 if (HaveLongGpr()) { 1527 if (LongGprNeedsPadding()) { 1528 PushGpr(0); 1529 gpr_index_--; 1530 } 1531 if (kRegistersNeededForLong == 1) { 1532 PushGpr(static_cast<uintptr_t>(val)); 1533 } else { 1534 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF)); 1535 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF)); 1536 } 1537 gpr_index_ -= kRegistersNeededForLong; 1538 } else { 1539 if (LongStackNeedsPadding()) { 1540 PushStack(0); 1541 stack_entries_++; 1542 } 1543 if (kRegistersNeededForLong == 1) { 1544 PushStack(static_cast<uintptr_t>(val)); 1545 stack_entries_++; 1546 } else { 1547 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF)); 1548 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF)); 1549 stack_entries_ += 2; 1550 } 1551 gpr_index_ = 0; 1552 } 1553 } 1554 1555 bool HaveFloatFpr() const { 1556 return fpr_index_ > 0; 1557 } 1558 1559 void AdvanceFloat(float val) { 1560 if (kNativeSoftFloatAbi) { 1561 AdvanceInt(bit_cast<uint32_t, float>(val)); 1562 } else { 1563 if (HaveFloatFpr()) { 1564 fpr_index_--; 1565 if (kRegistersNeededForDouble == 1) { 1566 if (kMultiFPRegistersWidened) { 1567 PushFpr8(bit_cast<uint64_t, double>(val)); 1568 } else { 1569 // No widening, just use the bits. 1570 PushFpr8(static_cast<uint64_t>(bit_cast<uint32_t, float>(val))); 1571 } 1572 } else { 1573 PushFpr4(val); 1574 } 1575 } else { 1576 stack_entries_++; 1577 if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) { 1578 // Need to widen before storing: Note the "double" in the template instantiation. 1579 // Note: We need to jump through those hoops to make the compiler happy. 1580 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t)); 1581 PushStack(static_cast<uintptr_t>(bit_cast<uint64_t, double>(val))); 1582 } else { 1583 PushStack(static_cast<uintptr_t>(bit_cast<uint32_t, float>(val))); 1584 } 1585 fpr_index_ = 0; 1586 } 1587 } 1588 } 1589 1590 bool HaveDoubleFpr() const { 1591 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0); 1592 } 1593 1594 bool DoubleFprNeedsPadding() const { 1595 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers 1596 kAlignDoubleOnStack && // and when it needs alignment 1597 (fpr_index_ & 1) == 1; // counter is odd, see constructor 1598 } 1599 1600 bool DoubleStackNeedsPadding() const { 1601 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers 1602 kAlignDoubleOnStack && // and when it needs 8B alignment 1603 (stack_entries_ & 1) == 1; // counter is odd 1604 } 1605 1606 void AdvanceDouble(uint64_t val) { 1607 if (kNativeSoftFloatAbi) { 1608 AdvanceLong(val); 1609 } else { 1610 if (HaveDoubleFpr()) { 1611 if (DoubleFprNeedsPadding()) { 1612 PushFpr4(0); 1613 fpr_index_--; 1614 } 1615 PushFpr8(val); 1616 fpr_index_ -= kRegistersNeededForDouble; 1617 } else { 1618 if (DoubleStackNeedsPadding()) { 1619 PushStack(0); 1620 stack_entries_++; 1621 } 1622 if (kRegistersNeededForDouble == 1) { 1623 PushStack(static_cast<uintptr_t>(val)); 1624 stack_entries_++; 1625 } else { 1626 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF)); 1627 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF)); 1628 stack_entries_ += 2; 1629 } 1630 fpr_index_ = 0; 1631 } 1632 } 1633 } 1634 1635 uint32_t GetStackEntries() const { 1636 return stack_entries_; 1637 } 1638 1639 uint32_t GetNumberOfUsedGprs() const { 1640 return kNumNativeGprArgs - gpr_index_; 1641 } 1642 1643 uint32_t GetNumberOfUsedFprs() const { 1644 return kNumNativeFprArgs - fpr_index_; 1645 } 1646 1647 private: 1648 void PushGpr(uintptr_t val) { 1649 delegate_->PushGpr(val); 1650 } 1651 void PushFpr4(float val) { 1652 delegate_->PushFpr4(val); 1653 } 1654 void PushFpr8(uint64_t val) { 1655 delegate_->PushFpr8(val); 1656 } 1657 void PushStack(uintptr_t val) { 1658 delegate_->PushStack(val); 1659 } 1660 uintptr_t PushHandle(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_) { 1661 return delegate_->PushHandle(ref); 1662 } 1663 1664 uint32_t gpr_index_; // Number of free GPRs 1665 uint32_t fpr_index_; // Number of free FPRs 1666 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not 1667 // extended 1668 T* const delegate_; // What Push implementation gets called 1669}; 1670 1671// Computes the sizes of register stacks and call stack area. Handling of references can be extended 1672// in subclasses. 1673// 1674// To handle native pointers, use "L" in the shorty for an object reference, which simulates 1675// them with handles. 1676class ComputeNativeCallFrameSize { 1677 public: 1678 ComputeNativeCallFrameSize() : num_stack_entries_(0) {} 1679 1680 virtual ~ComputeNativeCallFrameSize() {} 1681 1682 uint32_t GetStackSize() const { 1683 return num_stack_entries_ * sizeof(uintptr_t); 1684 } 1685 1686 uint8_t* LayoutCallStack(uint8_t* sp8) const { 1687 sp8 -= GetStackSize(); 1688 // Align by kStackAlignment. 1689 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment)); 1690 return sp8; 1691 } 1692 1693 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr) 1694 const { 1695 // Assumption is OK right now, as we have soft-float arm 1696 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs; 1697 sp8 -= fregs * sizeof(uintptr_t); 1698 *start_fpr = reinterpret_cast<uint32_t*>(sp8); 1699 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs; 1700 sp8 -= iregs * sizeof(uintptr_t); 1701 *start_gpr = reinterpret_cast<uintptr_t*>(sp8); 1702 return sp8; 1703 } 1704 1705 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr, 1706 uint32_t** start_fpr) const { 1707 // Native call stack. 1708 sp8 = LayoutCallStack(sp8); 1709 *start_stack = reinterpret_cast<uintptr_t*>(sp8); 1710 1711 // Put fprs and gprs below. 1712 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr); 1713 1714 // Return the new bottom. 1715 return sp8; 1716 } 1717 1718 virtual void WalkHeader( 1719 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm ATTRIBUTE_UNUSED) 1720 REQUIRES_SHARED(Locks::mutator_lock_) { 1721 } 1722 1723 void Walk(const char* shorty, uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) { 1724 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this); 1725 1726 WalkHeader(&sm); 1727 1728 for (uint32_t i = 1; i < shorty_len; ++i) { 1729 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]); 1730 switch (cur_type_) { 1731 case Primitive::kPrimNot: 1732 // TODO: fix abuse of mirror types. 1733 sm.AdvanceHandleScope( 1734 reinterpret_cast<mirror::Object*>(0x12345678)); 1735 break; 1736 1737 case Primitive::kPrimBoolean: 1738 case Primitive::kPrimByte: 1739 case Primitive::kPrimChar: 1740 case Primitive::kPrimShort: 1741 case Primitive::kPrimInt: 1742 sm.AdvanceInt(0); 1743 break; 1744 case Primitive::kPrimFloat: 1745 sm.AdvanceFloat(0); 1746 break; 1747 case Primitive::kPrimDouble: 1748 sm.AdvanceDouble(0); 1749 break; 1750 case Primitive::kPrimLong: 1751 sm.AdvanceLong(0); 1752 break; 1753 default: 1754 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty; 1755 UNREACHABLE(); 1756 } 1757 } 1758 1759 num_stack_entries_ = sm.GetStackEntries(); 1760 } 1761 1762 void PushGpr(uintptr_t /* val */) { 1763 // not optimizing registers, yet 1764 } 1765 1766 void PushFpr4(float /* val */) { 1767 // not optimizing registers, yet 1768 } 1769 1770 void PushFpr8(uint64_t /* val */) { 1771 // not optimizing registers, yet 1772 } 1773 1774 void PushStack(uintptr_t /* val */) { 1775 // counting is already done in the superclass 1776 } 1777 1778 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) { 1779 return reinterpret_cast<uintptr_t>(nullptr); 1780 } 1781 1782 protected: 1783 uint32_t num_stack_entries_; 1784}; 1785 1786class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize { 1787 public: 1788 explicit ComputeGenericJniFrameSize(bool critical_native) 1789 : num_handle_scope_references_(0), critical_native_(critical_native) {} 1790 1791 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs 1792 // is at *m = sp. Will update to point to the bottom of the save frame. 1793 // 1794 // Note: assumes ComputeAll() has been run before. 1795 void LayoutCalleeSaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope) 1796 REQUIRES_SHARED(Locks::mutator_lock_) { 1797 ArtMethod* method = **m; 1798 1799 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize); 1800 1801 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp); 1802 1803 // First, fix up the layout of the callee-save frame. 1804 // We have to squeeze in the HandleScope, and relocate the method pointer. 1805 1806 // "Free" the slot for the method. 1807 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer. 1808 1809 // Under the callee saves put handle scope and new method stack reference. 1810 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_); 1811 size_t scope_and_method = handle_scope_size + sizeof(ArtMethod*); 1812 1813 sp8 -= scope_and_method; 1814 // Align by kStackAlignment. 1815 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment)); 1816 1817 uint8_t* sp8_table = sp8 + sizeof(ArtMethod*); 1818 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(), 1819 num_handle_scope_references_); 1820 1821 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us. 1822 uint8_t* method_pointer = sp8; 1823 auto** new_method_ref = reinterpret_cast<ArtMethod**>(method_pointer); 1824 *new_method_ref = method; 1825 *m = new_method_ref; 1826 } 1827 1828 // Adds space for the cookie. Note: may leave stack unaligned. 1829 void LayoutCookie(uint8_t** sp) const { 1830 // Reference cookie and padding 1831 *sp -= 8; 1832 } 1833 1834 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie. 1835 // Returns the new bottom. Note: this may be unaligned. 1836 uint8_t* LayoutJNISaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope) 1837 REQUIRES_SHARED(Locks::mutator_lock_) { 1838 // First, fix up the layout of the callee-save frame. 1839 // We have to squeeze in the HandleScope, and relocate the method pointer. 1840 LayoutCalleeSaveFrame(self, m, sp, handle_scope); 1841 1842 // The bottom of the callee-save frame is now where the method is, *m. 1843 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m); 1844 1845 // Add space for cookie. 1846 LayoutCookie(&sp8); 1847 1848 return sp8; 1849 } 1850 1851 // WARNING: After this, *sp won't be pointing to the method anymore! 1852 uint8_t* ComputeLayout(Thread* self, ArtMethod*** m, const char* shorty, uint32_t shorty_len, 1853 HandleScope** handle_scope, uintptr_t** start_stack, uintptr_t** start_gpr, 1854 uint32_t** start_fpr) 1855 REQUIRES_SHARED(Locks::mutator_lock_) { 1856 Walk(shorty, shorty_len); 1857 1858 // JNI part. 1859 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope); 1860 1861 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr); 1862 1863 // Return the new bottom. 1864 return sp8; 1865 } 1866 1867 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE; 1868 1869 // Add JNIEnv* and jobj/jclass before the shorty-derived elements. 1870 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE 1871 REQUIRES_SHARED(Locks::mutator_lock_); 1872 1873 private: 1874 uint32_t num_handle_scope_references_; 1875 const bool critical_native_; 1876}; 1877 1878uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) { 1879 num_handle_scope_references_++; 1880 return reinterpret_cast<uintptr_t>(nullptr); 1881} 1882 1883void ComputeGenericJniFrameSize::WalkHeader( 1884 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) { 1885 // First 2 parameters are always excluded for @CriticalNative. 1886 if (UNLIKELY(critical_native_)) { 1887 return; 1888 } 1889 1890 // JNIEnv 1891 sm->AdvancePointer(nullptr); 1892 1893 // Class object or this as first argument 1894 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678)); 1895} 1896 1897// Class to push values to three separate regions. Used to fill the native call part. Adheres to 1898// the template requirements of BuildGenericJniFrameStateMachine. 1899class FillNativeCall { 1900 public: 1901 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) : 1902 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {} 1903 1904 virtual ~FillNativeCall() {} 1905 1906 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) { 1907 cur_gpr_reg_ = gpr_regs; 1908 cur_fpr_reg_ = fpr_regs; 1909 cur_stack_arg_ = stack_args; 1910 } 1911 1912 void PushGpr(uintptr_t val) { 1913 *cur_gpr_reg_ = val; 1914 cur_gpr_reg_++; 1915 } 1916 1917 void PushFpr4(float val) { 1918 *cur_fpr_reg_ = val; 1919 cur_fpr_reg_++; 1920 } 1921 1922 void PushFpr8(uint64_t val) { 1923 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_); 1924 *tmp = val; 1925 cur_fpr_reg_ += 2; 1926 } 1927 1928 void PushStack(uintptr_t val) { 1929 *cur_stack_arg_ = val; 1930 cur_stack_arg_++; 1931 } 1932 1933 virtual uintptr_t PushHandle(mirror::Object*) REQUIRES_SHARED(Locks::mutator_lock_) { 1934 LOG(FATAL) << "(Non-JNI) Native call does not use handles."; 1935 UNREACHABLE(); 1936 } 1937 1938 private: 1939 uintptr_t* cur_gpr_reg_; 1940 uint32_t* cur_fpr_reg_; 1941 uintptr_t* cur_stack_arg_; 1942}; 1943 1944// Visits arguments on the stack placing them into a region lower down the stack for the benefit 1945// of transitioning into native code. 1946class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor { 1947 public: 1948 BuildGenericJniFrameVisitor(Thread* self, 1949 bool is_static, 1950 bool critical_native, 1951 const char* shorty, 1952 uint32_t shorty_len, 1953 ArtMethod*** sp) 1954 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len), 1955 jni_call_(nullptr, nullptr, nullptr, nullptr, critical_native), 1956 sm_(&jni_call_) { 1957 ComputeGenericJniFrameSize fsc(critical_native); 1958 uintptr_t* start_gpr_reg; 1959 uint32_t* start_fpr_reg; 1960 uintptr_t* start_stack_arg; 1961 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len, 1962 &handle_scope_, 1963 &start_stack_arg, 1964 &start_gpr_reg, &start_fpr_reg); 1965 1966 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_); 1967 1968 // First 2 parameters are always excluded for CriticalNative methods. 1969 if (LIKELY(!critical_native)) { 1970 // jni environment is always first argument 1971 sm_.AdvancePointer(self->GetJniEnv()); 1972 1973 if (is_static) { 1974 sm_.AdvanceHandleScope((**sp)->GetDeclaringClass()); 1975 } // else "this" reference is already handled by QuickArgumentVisitor. 1976 } 1977 } 1978 1979 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE; 1980 1981 void FinalizeHandleScope(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_); 1982 1983 StackReference<mirror::Object>* GetFirstHandleScopeEntry() { 1984 return handle_scope_->GetHandle(0).GetReference(); 1985 } 1986 1987 jobject GetFirstHandleScopeJObject() const REQUIRES_SHARED(Locks::mutator_lock_) { 1988 return handle_scope_->GetHandle(0).ToJObject(); 1989 } 1990 1991 void* GetBottomOfUsedArea() const { 1992 return bottom_of_used_area_; 1993 } 1994 1995 private: 1996 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall. 1997 class FillJniCall FINAL : public FillNativeCall { 1998 public: 1999 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, 2000 HandleScope* handle_scope, bool critical_native) 2001 : FillNativeCall(gpr_regs, fpr_regs, stack_args), 2002 handle_scope_(handle_scope), 2003 cur_entry_(0), 2004 critical_native_(critical_native) {} 2005 2006 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_); 2007 2008 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) { 2009 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args); 2010 handle_scope_ = scope; 2011 cur_entry_ = 0U; 2012 } 2013 2014 void ResetRemainingScopeSlots() REQUIRES_SHARED(Locks::mutator_lock_) { 2015 // Initialize padding entries. 2016 size_t expected_slots = handle_scope_->NumberOfReferences(); 2017 while (cur_entry_ < expected_slots) { 2018 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr); 2019 } 2020 2021 if (!critical_native_) { 2022 // Non-critical natives have at least the self class (jclass) or this (jobject). 2023 DCHECK_NE(cur_entry_, 0U); 2024 } 2025 } 2026 2027 bool CriticalNative() const { 2028 return critical_native_; 2029 } 2030 2031 private: 2032 HandleScope* handle_scope_; 2033 size_t cur_entry_; 2034 const bool critical_native_; 2035 }; 2036 2037 HandleScope* handle_scope_; 2038 FillJniCall jni_call_; 2039 void* bottom_of_used_area_; 2040 2041 BuildNativeCallFrameStateMachine<FillJniCall> sm_; 2042 2043 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor); 2044}; 2045 2046uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) { 2047 uintptr_t tmp; 2048 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_); 2049 h.Assign(ref); 2050 tmp = reinterpret_cast<uintptr_t>(h.ToJObject()); 2051 cur_entry_++; 2052 return tmp; 2053} 2054 2055void BuildGenericJniFrameVisitor::Visit() { 2056 Primitive::Type type = GetParamPrimitiveType(); 2057 switch (type) { 2058 case Primitive::kPrimLong: { 2059 jlong long_arg; 2060 if (IsSplitLongOrDouble()) { 2061 long_arg = ReadSplitLongParam(); 2062 } else { 2063 long_arg = *reinterpret_cast<jlong*>(GetParamAddress()); 2064 } 2065 sm_.AdvanceLong(long_arg); 2066 break; 2067 } 2068 case Primitive::kPrimDouble: { 2069 uint64_t double_arg; 2070 if (IsSplitLongOrDouble()) { 2071 // Read into union so that we don't case to a double. 2072 double_arg = ReadSplitLongParam(); 2073 } else { 2074 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress()); 2075 } 2076 sm_.AdvanceDouble(double_arg); 2077 break; 2078 } 2079 case Primitive::kPrimNot: { 2080 StackReference<mirror::Object>* stack_ref = 2081 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress()); 2082 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr()); 2083 break; 2084 } 2085 case Primitive::kPrimFloat: 2086 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress())); 2087 break; 2088 case Primitive::kPrimBoolean: // Fall-through. 2089 case Primitive::kPrimByte: // Fall-through. 2090 case Primitive::kPrimChar: // Fall-through. 2091 case Primitive::kPrimShort: // Fall-through. 2092 case Primitive::kPrimInt: // Fall-through. 2093 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress())); 2094 break; 2095 case Primitive::kPrimVoid: 2096 LOG(FATAL) << "UNREACHABLE"; 2097 UNREACHABLE(); 2098 } 2099} 2100 2101void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) { 2102 // Clear out rest of the scope. 2103 jni_call_.ResetRemainingScopeSlots(); 2104 if (!jni_call_.CriticalNative()) { 2105 // Install HandleScope. 2106 self->PushHandleScope(handle_scope_); 2107 } 2108} 2109 2110#if defined(__arm__) || defined(__aarch64__) 2111extern "C" const void* artFindNativeMethod(); 2112#else 2113extern "C" const void* artFindNativeMethod(Thread* self); 2114#endif 2115 2116static uint64_t artQuickGenericJniEndJNIRef(Thread* self, 2117 uint32_t cookie, 2118 bool fast_native ATTRIBUTE_UNUSED, 2119 jobject l, 2120 jobject lock) { 2121 // TODO: add entrypoints for @FastNative returning objects. 2122 if (lock != nullptr) { 2123 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self)); 2124 } else { 2125 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self)); 2126 } 2127} 2128 2129static void artQuickGenericJniEndJNINonRef(Thread* self, 2130 uint32_t cookie, 2131 bool fast_native, 2132 jobject lock) { 2133 if (lock != nullptr) { 2134 JniMethodEndSynchronized(cookie, lock, self); 2135 // Ignore "fast_native" here because synchronized functions aren't very fast. 2136 } else { 2137 if (UNLIKELY(fast_native)) { 2138 JniMethodFastEnd(cookie, self); 2139 } else { 2140 JniMethodEnd(cookie, self); 2141 } 2142 } 2143} 2144 2145/* 2146 * Initializes an alloca region assumed to be directly below sp for a native call: 2147 * Create a HandleScope and call stack and fill a mini stack with values to be pushed to registers. 2148 * The final element on the stack is a pointer to the native code. 2149 * 2150 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it. 2151 * We need to fix this, as the handle scope needs to go into the callee-save frame. 2152 * 2153 * The return of this function denotes: 2154 * 1) How many bytes of the alloca can be released, if the value is non-negative. 2155 * 2) An error, if the value is negative. 2156 */ 2157extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self, ArtMethod** sp) 2158 REQUIRES_SHARED(Locks::mutator_lock_) { 2159 // Note: We cannot walk the stack properly until fixed up below. 2160 ArtMethod* called = *sp; 2161 DCHECK(called->IsNative()) << called->PrettyMethod(true); 2162 Runtime* runtime = Runtime::Current(); 2163 jit::Jit* jit = runtime->GetJit(); 2164 if (jit != nullptr) { 2165 jit->AddSamples(self, called, 1u, /*with_backedges*/ false); 2166 } 2167 uint32_t shorty_len = 0; 2168 const char* shorty = called->GetShorty(&shorty_len); 2169 bool critical_native = called->IsCriticalNative(); 2170 bool fast_native = called->IsFastNative(); 2171 bool normal_native = !critical_native && !fast_native; 2172 2173 // Run the visitor and update sp. 2174 BuildGenericJniFrameVisitor visitor(self, 2175 called->IsStatic(), 2176 critical_native, 2177 shorty, 2178 shorty_len, 2179 &sp); 2180 { 2181 ScopedAssertNoThreadSuspension sants(__FUNCTION__); 2182 visitor.VisitArguments(); 2183 // FinalizeHandleScope pushes the handle scope on the thread. 2184 visitor.FinalizeHandleScope(self); 2185 } 2186 2187 // Fix up managed-stack things in Thread. After this we can walk the stack. 2188 self->SetTopOfStackTagged(sp); 2189 2190 self->VerifyStack(); 2191 2192 uint32_t cookie; 2193 uint32_t* sp32; 2194 // Skip calling JniMethodStart for @CriticalNative. 2195 if (LIKELY(!critical_native)) { 2196 // Start JNI, save the cookie. 2197 if (called->IsSynchronized()) { 2198 DCHECK(normal_native) << " @FastNative and synchronize is not supported"; 2199 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self); 2200 if (self->IsExceptionPending()) { 2201 self->PopHandleScope(); 2202 // A negative value denotes an error. 2203 return GetTwoWordFailureValue(); 2204 } 2205 } else { 2206 if (fast_native) { 2207 cookie = JniMethodFastStart(self); 2208 } else { 2209 DCHECK(normal_native); 2210 cookie = JniMethodStart(self); 2211 } 2212 } 2213 sp32 = reinterpret_cast<uint32_t*>(sp); 2214 *(sp32 - 1) = cookie; 2215 } 2216 2217 // Retrieve the stored native code. 2218 void const* nativeCode = called->GetEntryPointFromJni(); 2219 2220 // There are two cases for the content of nativeCode: 2221 // 1) Pointer to the native function. 2222 // 2) Pointer to the trampoline for native code binding. 2223 // In the second case, we need to execute the binding and continue with the actual native function 2224 // pointer. 2225 DCHECK(nativeCode != nullptr); 2226 if (nativeCode == GetJniDlsymLookupStub()) { 2227#if defined(__arm__) || defined(__aarch64__) 2228 nativeCode = artFindNativeMethod(); 2229#else 2230 nativeCode = artFindNativeMethod(self); 2231#endif 2232 2233 if (nativeCode == nullptr) { 2234 DCHECK(self->IsExceptionPending()); // There should be an exception pending now. 2235 2236 // @CriticalNative calls do not need to call back into JniMethodEnd. 2237 if (LIKELY(!critical_native)) { 2238 // End JNI, as the assembly will move to deliver the exception. 2239 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr; 2240 if (shorty[0] == 'L') { 2241 artQuickGenericJniEndJNIRef(self, cookie, fast_native, nullptr, lock); 2242 } else { 2243 artQuickGenericJniEndJNINonRef(self, cookie, fast_native, lock); 2244 } 2245 } 2246 2247 return GetTwoWordFailureValue(); 2248 } 2249 // Note that the native code pointer will be automatically set by artFindNativeMethod(). 2250 } 2251 2252#if defined(__mips__) && !defined(__LP64__) 2253 // On MIPS32 if the first two arguments are floating-point, we need to know their types 2254 // so that art_quick_generic_jni_trampoline can correctly extract them from the stack 2255 // and load into floating-point registers. 2256 // Possible arrangements of first two floating-point arguments on the stack (32-bit FPU 2257 // view): 2258 // (1) 2259 // | DOUBLE | DOUBLE | other args, if any 2260 // | F12 | F13 | F14 | F15 | 2261 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16 2262 // (2) 2263 // | DOUBLE | FLOAT | (PAD) | other args, if any 2264 // | F12 | F13 | F14 | | 2265 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16 2266 // (3) 2267 // | FLOAT | (PAD) | DOUBLE | other args, if any 2268 // | F12 | | F14 | F15 | 2269 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16 2270 // (4) 2271 // | FLOAT | FLOAT | other args, if any 2272 // | F12 | F14 | 2273 // | SP+0 | SP+4 | SP+8 2274 // As you can see, only the last case (4) is special. In all others we can just 2275 // load F12/F13 and F14/F15 in the same manner. 2276 // Set bit 0 of the native code address to 1 in this case (valid code addresses 2277 // are always a multiple of 4 on MIPS32, so we have 2 spare bits available). 2278 if (nativeCode != nullptr && 2279 shorty != nullptr && 2280 shorty_len >= 3 && 2281 shorty[1] == 'F' && 2282 shorty[2] == 'F') { 2283 nativeCode = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(nativeCode) | 1); 2284 } 2285#endif 2286 2287 // Return native code addr(lo) and bottom of alloca address(hi). 2288 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()), 2289 reinterpret_cast<uintptr_t>(nativeCode)); 2290} 2291 2292// Defined in quick_jni_entrypoints.cc. 2293extern uint64_t GenericJniMethodEnd(Thread* self, uint32_t saved_local_ref_cookie, 2294 jvalue result, uint64_t result_f, ArtMethod* called, 2295 HandleScope* handle_scope); 2296/* 2297 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and 2298 * unlocking. 2299 */ 2300extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self, 2301 jvalue result, 2302 uint64_t result_f) { 2303 // We're here just back from a native call. We don't have the shared mutator lock at this point 2304 // yet until we call GoToRunnable() later in GenericJniMethodEnd(). Accessing objects or doing 2305 // anything that requires a mutator lock before that would cause problems as GC may have the 2306 // exclusive mutator lock and may be moving objects, etc. 2307 ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrame(); 2308 DCHECK(self->GetManagedStack()->GetTopQuickFrameTag()); 2309 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp); 2310 ArtMethod* called = *sp; 2311 uint32_t cookie = *(sp32 - 1); 2312 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp) + sizeof(*sp)); 2313 return GenericJniMethodEnd(self, cookie, result, result_f, called, table); 2314} 2315 2316// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value 2317// for the method pointer. 2318// 2319// It is valid to use this, as at the usage points here (returns from C functions) we are assuming 2320// to hold the mutator lock (see REQUIRES_SHARED(Locks::mutator_lock_) annotations). 2321 2322template <InvokeType type, bool access_check> 2323static TwoWordReturn artInvokeCommon(uint32_t method_idx, 2324 ObjPtr<mirror::Object> this_object, 2325 Thread* self, 2326 ArtMethod** sp) { 2327 ScopedQuickEntrypointChecks sqec(self); 2328 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs)); 2329 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp); 2330 ArtMethod* method = FindMethodFast<type, access_check>(method_idx, this_object, caller_method); 2331 if (UNLIKELY(method == nullptr)) { 2332 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile(); 2333 uint32_t shorty_len; 2334 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len); 2335 { 2336 // Remember the args in case a GC happens in FindMethodFromCode. 2337 ScopedObjectAccessUnchecked soa(self->GetJniEnv()); 2338 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa); 2339 visitor.VisitArguments(); 2340 method = FindMethodFromCode<type, access_check>(method_idx, 2341 &this_object, 2342 caller_method, 2343 self); 2344 visitor.FixupReferences(); 2345 } 2346 2347 if (UNLIKELY(method == nullptr)) { 2348 CHECK(self->IsExceptionPending()); 2349 return GetTwoWordFailureValue(); // Failure. 2350 } 2351 } 2352 DCHECK(!self->IsExceptionPending()); 2353 const void* code = method->GetEntryPointFromQuickCompiledCode(); 2354 2355 // When we return, the caller will branch to this address, so it had better not be 0! 2356 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod() 2357 << " location: " 2358 << method->GetDexFile()->GetLocation(); 2359 2360 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code), 2361 reinterpret_cast<uintptr_t>(method)); 2362} 2363 2364// Explicit artInvokeCommon template function declarations to please analysis tool. 2365#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \ 2366 template REQUIRES_SHARED(Locks::mutator_lock_) \ 2367 TwoWordReturn artInvokeCommon<type, access_check>( \ 2368 uint32_t method_idx, ObjPtr<mirror::Object> his_object, Thread* self, ArtMethod** sp) 2369 2370EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false); 2371EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true); 2372EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false); 2373EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true); 2374EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false); 2375EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true); 2376EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false); 2377EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true); 2378EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false); 2379EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true); 2380#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL 2381 2382// See comments in runtime_support_asm.S 2383extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck( 2384 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp) 2385 REQUIRES_SHARED(Locks::mutator_lock_) { 2386 return artInvokeCommon<kInterface, true>(method_idx, this_object, self, sp); 2387} 2388 2389extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck( 2390 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp) 2391 REQUIRES_SHARED(Locks::mutator_lock_) { 2392 return artInvokeCommon<kDirect, true>(method_idx, this_object, self, sp); 2393} 2394 2395extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck( 2396 uint32_t method_idx, 2397 mirror::Object* this_object ATTRIBUTE_UNUSED, 2398 Thread* self, 2399 ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) { 2400 // For static, this_object is not required and may be random garbage. Don't pass it down so that 2401 // it doesn't cause ObjPtr alignment failure check. 2402 return artInvokeCommon<kStatic, true>(method_idx, nullptr, self, sp); 2403} 2404 2405extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck( 2406 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp) 2407 REQUIRES_SHARED(Locks::mutator_lock_) { 2408 return artInvokeCommon<kSuper, true>(method_idx, this_object, self, sp); 2409} 2410 2411extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck( 2412 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp) 2413 REQUIRES_SHARED(Locks::mutator_lock_) { 2414 return artInvokeCommon<kVirtual, true>(method_idx, this_object, self, sp); 2415} 2416 2417// Helper function for art_quick_imt_conflict_trampoline to look up the interface method. 2418extern "C" ArtMethod* artLookupResolvedMethod(uint32_t method_index, ArtMethod* referrer) 2419 REQUIRES_SHARED(Locks::mutator_lock_) { 2420 ScopedAssertNoThreadSuspension ants(__FUNCTION__); 2421 DCHECK(!referrer->IsProxyMethod()); 2422 ArtMethod* result = Runtime::Current()->GetClassLinker()->LookupResolvedMethod( 2423 method_index, referrer->GetDexCache(), referrer->GetClassLoader()); 2424 DCHECK(result == nullptr || 2425 result->GetDeclaringClass()->IsInterface() || 2426 result->GetDeclaringClass() == 2427 WellKnownClasses::ToClass(WellKnownClasses::java_lang_Object)) 2428 << result->PrettyMethod(); 2429 return result; 2430} 2431 2432// Determine target of interface dispatch. The interface method and this object are known non-null. 2433// The interface method is the method returned by the dex cache in the conflict trampoline. 2434extern "C" TwoWordReturn artInvokeInterfaceTrampoline(ArtMethod* interface_method, 2435 mirror::Object* raw_this_object, 2436 Thread* self, 2437 ArtMethod** sp) 2438 REQUIRES_SHARED(Locks::mutator_lock_) { 2439 ScopedQuickEntrypointChecks sqec(self); 2440 StackHandleScope<2> hs(self); 2441 Handle<mirror::Object> this_object = hs.NewHandle(raw_this_object); 2442 Handle<mirror::Class> cls = hs.NewHandle(this_object->GetClass()); 2443 2444 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp); 2445 ArtMethod* method = nullptr; 2446 ImTable* imt = cls->GetImt(kRuntimePointerSize); 2447 2448 if (UNLIKELY(interface_method == nullptr)) { 2449 // The interface method is unresolved, so resolve it in the dex file of the caller. 2450 // Fetch the dex_method_idx of the target interface method from the caller. 2451 uint32_t dex_method_idx; 2452 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp); 2453 const DexFile::CodeItem* code_item = caller_method->GetCodeItem(); 2454 DCHECK_LT(dex_pc, code_item->insns_size_in_code_units_); 2455 const Instruction& instr = code_item->InstructionAt(dex_pc); 2456 Instruction::Code instr_code = instr.Opcode(); 2457 DCHECK(instr_code == Instruction::INVOKE_INTERFACE || 2458 instr_code == Instruction::INVOKE_INTERFACE_RANGE) 2459 << "Unexpected call into interface trampoline: " << instr.DumpString(nullptr); 2460 if (instr_code == Instruction::INVOKE_INTERFACE) { 2461 dex_method_idx = instr.VRegB_35c(); 2462 } else { 2463 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE); 2464 dex_method_idx = instr.VRegB_3rc(); 2465 } 2466 2467 const DexFile& dex_file = caller_method->GetDeclaringClass()->GetDexFile(); 2468 uint32_t shorty_len; 2469 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(dex_method_idx), 2470 &shorty_len); 2471 { 2472 // Remember the args in case a GC happens in ClassLinker::ResolveMethod(). 2473 ScopedObjectAccessUnchecked soa(self->GetJniEnv()); 2474 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa); 2475 visitor.VisitArguments(); 2476 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 2477 interface_method = class_linker->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>( 2478 self, dex_method_idx, caller_method, kInterface); 2479 visitor.FixupReferences(); 2480 } 2481 2482 if (UNLIKELY(interface_method == nullptr)) { 2483 CHECK(self->IsExceptionPending()); 2484 return GetTwoWordFailureValue(); // Failure. 2485 } 2486 } 2487 2488 DCHECK(!interface_method->IsRuntimeMethod()); 2489 // Look whether we have a match in the ImtConflictTable. 2490 uint32_t imt_index = ImTable::GetImtIndex(interface_method); 2491 ArtMethod* conflict_method = imt->Get(imt_index, kRuntimePointerSize); 2492 if (LIKELY(conflict_method->IsRuntimeMethod())) { 2493 ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize); 2494 DCHECK(current_table != nullptr); 2495 method = current_table->Lookup(interface_method, kRuntimePointerSize); 2496 } else { 2497 // It seems we aren't really a conflict method! 2498 if (kIsDebugBuild) { 2499 ArtMethod* m = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize); 2500 CHECK_EQ(conflict_method, m) 2501 << interface_method->PrettyMethod() << " / " << conflict_method->PrettyMethod() << " / " 2502 << " / " << ArtMethod::PrettyMethod(m) << " / " << cls->PrettyClass(); 2503 } 2504 method = conflict_method; 2505 } 2506 if (method != nullptr) { 2507 return GetTwoWordSuccessValue( 2508 reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCode()), 2509 reinterpret_cast<uintptr_t>(method)); 2510 } 2511 2512 // No match, use the IfTable. 2513 method = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize); 2514 if (UNLIKELY(method == nullptr)) { 2515 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch( 2516 interface_method, this_object.Get(), caller_method); 2517 return GetTwoWordFailureValue(); // Failure. 2518 } 2519 2520 // We arrive here if we have found an implementation, and it is not in the ImtConflictTable. 2521 // We create a new table with the new pair { interface_method, method }. 2522 DCHECK(conflict_method->IsRuntimeMethod()); 2523 ArtMethod* new_conflict_method = Runtime::Current()->GetClassLinker()->AddMethodToConflictTable( 2524 cls.Get(), 2525 conflict_method, 2526 interface_method, 2527 method, 2528 /*force_new_conflict_method*/false); 2529 if (new_conflict_method != conflict_method) { 2530 // Update the IMT if we create a new conflict method. No fence needed here, as the 2531 // data is consistent. 2532 imt->Set(imt_index, 2533 new_conflict_method, 2534 kRuntimePointerSize); 2535 } 2536 2537 const void* code = method->GetEntryPointFromQuickCompiledCode(); 2538 2539 // When we return, the caller will branch to this address, so it had better not be 0! 2540 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod() 2541 << " location: " << method->GetDexFile()->GetLocation(); 2542 2543 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code), 2544 reinterpret_cast<uintptr_t>(method)); 2545} 2546 2547// Returns shorty type so the caller can determine how to put |result| 2548// into expected registers. The shorty type is static so the compiler 2549// could call different flavors of this code path depending on the 2550// shorty type though this would require different entry points for 2551// each type. 2552extern "C" uintptr_t artInvokePolymorphic( 2553 JValue* result, 2554 mirror::Object* raw_receiver, 2555 Thread* self, 2556 ArtMethod** sp) 2557 REQUIRES_SHARED(Locks::mutator_lock_) { 2558 ScopedQuickEntrypointChecks sqec(self); 2559 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs)); 2560 2561 // Start new JNI local reference state 2562 JNIEnvExt* env = self->GetJniEnv(); 2563 ScopedObjectAccessUnchecked soa(env); 2564 ScopedJniEnvLocalRefState env_state(env); 2565 const char* old_cause = self->StartAssertNoThreadSuspension("Making stack arguments safe."); 2566 2567 // From the instruction, get the |callsite_shorty| and expose arguments on the stack to the GC. 2568 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp); 2569 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp); 2570 const DexFile::CodeItem* code = caller_method->GetCodeItem(); 2571 const Instruction& inst = code->InstructionAt(dex_pc); 2572 DCHECK(inst.Opcode() == Instruction::INVOKE_POLYMORPHIC || 2573 inst.Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE); 2574 const uint32_t proto_idx = inst.VRegH(); 2575 const char* shorty = caller_method->GetDexFile()->GetShorty(proto_idx); 2576 const size_t shorty_length = strlen(shorty); 2577 static const bool kMethodIsStatic = false; // invoke() and invokeExact() are not static. 2578 RememberForGcArgumentVisitor gc_visitor(sp, kMethodIsStatic, shorty, shorty_length, &soa); 2579 gc_visitor.VisitArguments(); 2580 2581 // Wrap raw_receiver in a Handle for safety. 2582 StackHandleScope<3> hs(self); 2583 Handle<mirror::Object> receiver_handle(hs.NewHandle(raw_receiver)); 2584 raw_receiver = nullptr; 2585 self->EndAssertNoThreadSuspension(old_cause); 2586 2587 // Resolve method. 2588 ClassLinker* linker = Runtime::Current()->GetClassLinker(); 2589 ArtMethod* resolved_method = linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>( 2590 self, inst.VRegB(), caller_method, kVirtual); 2591 2592 if (UNLIKELY(receiver_handle.IsNull())) { 2593 ThrowNullPointerExceptionForMethodAccess(resolved_method, InvokeType::kVirtual); 2594 return static_cast<uintptr_t>('V'); 2595 } 2596 2597 // TODO(oth): Ensure this path isn't taken for VarHandle accessors (b/65872996). 2598 DCHECK_EQ(resolved_method->GetDeclaringClass(), 2599 WellKnownClasses::ToClass(WellKnownClasses::java_lang_invoke_MethodHandle)); 2600 2601 Handle<mirror::MethodHandle> method_handle(hs.NewHandle( 2602 ObjPtr<mirror::MethodHandle>::DownCast(MakeObjPtr(receiver_handle.Get())))); 2603 2604 Handle<mirror::MethodType> method_type( 2605 hs.NewHandle(linker->ResolveMethodType(self, proto_idx, caller_method))); 2606 2607 // This implies we couldn't resolve one or more types in this method handle. 2608 if (UNLIKELY(method_type.IsNull())) { 2609 CHECK(self->IsExceptionPending()); 2610 return static_cast<uintptr_t>('V'); 2611 } 2612 2613 DCHECK_EQ(ArtMethod::NumArgRegisters(shorty) + 1u, (uint32_t)inst.VRegA()); 2614 DCHECK_EQ(resolved_method->IsStatic(), kMethodIsStatic); 2615 2616 // Fix references before constructing the shadow frame. 2617 gc_visitor.FixupReferences(); 2618 2619 // Construct shadow frame placing arguments consecutively from |first_arg|. 2620 const bool is_range = (inst.Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE); 2621 const size_t num_vregs = is_range ? inst.VRegA_4rcc() : inst.VRegA_45cc(); 2622 const size_t first_arg = 0; 2623 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr = 2624 CREATE_SHADOW_FRAME(num_vregs, /* link */ nullptr, resolved_method, dex_pc); 2625 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get(); 2626 ScopedStackedShadowFramePusher 2627 frame_pusher(self, shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction); 2628 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, 2629 kMethodIsStatic, 2630 shorty, 2631 strlen(shorty), 2632 shadow_frame, 2633 first_arg); 2634 shadow_frame_builder.VisitArguments(); 2635 2636 // Push a transition back into managed code onto the linked list in thread. 2637 ManagedStack fragment; 2638 self->PushManagedStackFragment(&fragment); 2639 2640 // Call DoInvokePolymorphic with |is_range| = true, as shadow frame has argument registers in 2641 // consecutive order. 2642 RangeInstructionOperands operands(first_arg + 1, num_vregs - 1); 2643 bool isExact = (jni::EncodeArtMethod(resolved_method) == 2644 WellKnownClasses::java_lang_invoke_MethodHandle_invokeExact); 2645 bool success = false; 2646 if (isExact) { 2647 success = MethodHandleInvokeExact(self, 2648 *shadow_frame, 2649 method_handle, 2650 method_type, 2651 &operands, 2652 result); 2653 } else { 2654 success = MethodHandleInvoke(self, 2655 *shadow_frame, 2656 method_handle, 2657 method_type, 2658 &operands, 2659 result); 2660 } 2661 DCHECK(success || self->IsExceptionPending()); 2662 2663 // Pop transition record. 2664 self->PopManagedStackFragment(fragment); 2665 2666 return static_cast<uintptr_t>(shorty[0]); 2667} 2668 2669} // namespace art 2670