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