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