quick_trampoline_entrypoints.cc revision 0cd81352a7c06e381951cea1b104fd73516f4341
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 "callee_save_frame.h" 18#include "common_throws.h" 19#include "dex_file-inl.h" 20#include "dex_instruction-inl.h" 21#include "entrypoints/entrypoint_utils.h" 22#include "gc/accounting/card_table-inl.h" 23#include "interpreter/interpreter.h" 24#include "mirror/art_method-inl.h" 25#include "mirror/class-inl.h" 26#include "mirror/dex_cache-inl.h" 27#include "mirror/object-inl.h" 28#include "mirror/object_array-inl.h" 29#include "object_utils.h" 30#include "runtime.h" 31#include "scoped_thread_state_change.h" 32 33namespace art { 34 35// Visits the arguments as saved to the stack by a Runtime::kRefAndArgs callee save frame. 36class QuickArgumentVisitor { 37 // Number of bytes for each out register in the caller method's frame. 38 static constexpr size_t kBytesStackArgLocation = 4; 39#if defined(__arm__) 40 // The callee save frame is pointed to by SP. 41 // | argN | | 42 // | ... | | 43 // | arg4 | | 44 // | arg3 spill | | Caller's frame 45 // | arg2 spill | | 46 // | arg1 spill | | 47 // | Method* | --- 48 // | LR | 49 // | ... | callee saves 50 // | R3 | arg3 51 // | R2 | arg2 52 // | R1 | arg1 53 // | R0 | padding 54 // | Method* | <- sp 55 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI. 56 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs. 57 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs. 58 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg. 59 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 8; // Offset of first GPR arg. 60 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 44; // Offset of return address. 61 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 48; // Frame size. 62 static size_t GprIndexToGprOffset(uint32_t gpr_index) { 63 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA); 64 } 65#elif defined(__aarch64__) 66 // The callee save frame is pointed to by SP. 67 // | argN | | 68 // | ... | | 69 // | arg4 | | 70 // | arg3 spill | | Caller's frame 71 // | arg2 spill | | 72 // | arg1 spill | | 73 // | Method* | --- 74 // | LR | 75 // | X28 | 76 // | : | 77 // | X19 | 78 // | X7 | 79 // | : | 80 // | X1 | 81 // | D15 | 82 // | : | 83 // | D0 | 84 // | | padding 85 // | Method* | <- sp 86 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI. 87 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs. 88 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs. 89 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =16; // Offset of first FPR arg. 90 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 144; // Offset of first GPR arg. 91 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 296; // Offset of return address. 92 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 304; // Frame size. 93 static size_t GprIndexToGprOffset(uint32_t gpr_index) { 94 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA); 95 } 96#elif defined(__mips__) 97 // The callee save frame is pointed to by SP. 98 // | argN | | 99 // | ... | | 100 // | arg4 | | 101 // | arg3 spill | | Caller's frame 102 // | arg2 spill | | 103 // | arg1 spill | | 104 // | Method* | --- 105 // | RA | 106 // | ... | callee saves 107 // | A3 | arg3 108 // | A2 | arg2 109 // | A1 | arg1 110 // | A0/Method* | <- sp 111 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI. 112 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs. 113 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs. 114 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg. 115 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4; // Offset of first GPR arg. 116 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 60; // Offset of return address. 117 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 64; // Frame size. 118 static size_t GprIndexToGprOffset(uint32_t gpr_index) { 119 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA); 120 } 121#elif defined(__i386__) 122 // The callee save frame is pointed to by SP. 123 // | argN | | 124 // | ... | | 125 // | arg4 | | 126 // | arg3 spill | | Caller's frame 127 // | arg2 spill | | 128 // | arg1 spill | | 129 // | Method* | --- 130 // | Return | 131 // | EBP,ESI,EDI | callee saves 132 // | EBX | arg3 133 // | EDX | arg2 134 // | ECX | arg1 135 // | EAX/Method* | <- sp 136 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI. 137 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs. 138 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs. 139 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg. 140 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4; // Offset of first GPR arg. 141 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28; // Offset of return address. 142 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 32; // Frame size. 143 static size_t GprIndexToGprOffset(uint32_t gpr_index) { 144 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA); 145 } 146#elif defined(__x86_64__) 147 // The callee save frame is pointed to by SP. 148 // | argN | | 149 // | ... | | 150 // | reg. arg spills | | Caller's frame 151 // | Method* | --- 152 // | Return | 153 // | R15 | callee save 154 // | R14 | callee save 155 // | R13 | callee save 156 // | R12 | callee save 157 // | R9 | arg5 158 // | R8 | arg4 159 // | RSI/R6 | arg1 160 // | RBP/R5 | callee save 161 // | RBX/R3 | callee save 162 // | RDX/R2 | arg2 163 // | RCX/R1 | arg3 164 // | XMM7 | float arg 8 165 // | XMM6 | float arg 7 166 // | XMM5 | float arg 6 167 // | XMM4 | float arg 5 168 // | XMM3 | float arg 4 169 // | XMM2 | float arg 3 170 // | XMM1 | float arg 2 171 // | XMM0 | float arg 1 172 // | Padding | 173 // | RDI/Method* | <- sp 174 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI. 175 static constexpr size_t kNumQuickGprArgs = 5; // 3 arguments passed in GPRs. 176 static constexpr size_t kNumQuickFprArgs = 8; // 0 arguments passed in FPRs. 177 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg. 178 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80; // Offset of first GPR arg. 179 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168; // Offset of return address. 180 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 176; // Frame size. 181 static size_t GprIndexToGprOffset(uint32_t gpr_index) { 182 switch (gpr_index) { 183 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA)); 184 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA)); 185 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA)); 186 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA)); 187 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA)); 188 default: 189 LOG(FATAL) << "Unexpected GPR index: " << gpr_index; 190 return 0; 191 } 192 } 193#else 194#error "Unsupported architecture" 195#endif 196 197 public: 198 static mirror::ArtMethod* GetCallingMethod(mirror::ArtMethod** sp) 199 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 200 DCHECK((*sp)->IsCalleeSaveMethod()); 201 byte* previous_sp = reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize; 202 return *reinterpret_cast<mirror::ArtMethod**>(previous_sp); 203 } 204 205 // For the given quick ref and args quick frame, return the caller's PC. 206 static uintptr_t GetCallingPc(mirror::ArtMethod** sp) 207 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 208 DCHECK((*sp)->IsCalleeSaveMethod()); 209 byte* lr = reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset; 210 return *reinterpret_cast<uintptr_t*>(lr); 211 } 212 213 QuickArgumentVisitor(mirror::ArtMethod** sp, bool is_static, 214 const char* shorty, uint32_t shorty_len) 215 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) : 216 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len), 217 gpr_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset), 218 fpr_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset), 219 stack_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize 220 + StackArgumentStartFromShorty(is_static, shorty, shorty_len)), 221 gpr_index_(0), fpr_index_(0), stack_index_(0), cur_type_(Primitive::kPrimVoid), 222 is_split_long_or_double_(false) { 223 DCHECK_EQ(kQuickCalleeSaveFrame_RefAndArgs_FrameSize, 224 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes()); 225 } 226 227 virtual ~QuickArgumentVisitor() {} 228 229 virtual void Visit() = 0; 230 231 Primitive::Type GetParamPrimitiveType() const { 232 return cur_type_; 233 } 234 235 byte* GetParamAddress() const { 236 if (!kQuickSoftFloatAbi) { 237 Primitive::Type type = GetParamPrimitiveType(); 238 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) { 239 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) { 240 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA)); 241 } 242 return stack_args_ + (stack_index_ * kBytesStackArgLocation); 243 } 244 } 245 if (gpr_index_ < kNumQuickGprArgs) { 246 return gpr_args_ + GprIndexToGprOffset(gpr_index_); 247 } 248 return stack_args_ + (stack_index_ * kBytesStackArgLocation); 249 } 250 251 bool IsSplitLongOrDouble() const { 252 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) || (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) { 253 return is_split_long_or_double_; 254 } else { 255 return false; // An optimization for when GPR and FPRs are 64bit. 256 } 257 } 258 259 bool IsParamAReference() const { 260 return GetParamPrimitiveType() == Primitive::kPrimNot; 261 } 262 263 bool IsParamALongOrDouble() const { 264 Primitive::Type type = GetParamPrimitiveType(); 265 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble; 266 } 267 268 uint64_t ReadSplitLongParam() const { 269 DCHECK(IsSplitLongOrDouble()); 270 uint64_t low_half = *reinterpret_cast<uint32_t*>(GetParamAddress()); 271 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args_); 272 return (low_half & 0xffffffffULL) | (high_half << 32); 273 } 274 275 void VisitArguments() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 276 // This implementation doesn't support reg-spill area for hard float 277 // ABI targets such as x86_64 and aarch64. So, for those targets whose 278 // 'kQuickSoftFloatAbi' is 'false': 279 // (a) 'stack_args_' should point to the first method's argument 280 // (b) whatever the argument type it is, the 'stack_index_' should 281 // be moved forward along with every visiting. 282 gpr_index_ = 0; 283 fpr_index_ = 0; 284 stack_index_ = 0; 285 if (!is_static_) { // Handle this. 286 cur_type_ = Primitive::kPrimNot; 287 is_split_long_or_double_ = false; 288 Visit(); 289 if (!kQuickSoftFloatAbi || kNumQuickGprArgs == 0) { 290 stack_index_++; 291 } 292 if (kNumQuickGprArgs > 0) { 293 gpr_index_++; 294 } 295 } 296 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) { 297 cur_type_ = Primitive::GetType(shorty_[shorty_index]); 298 switch (cur_type_) { 299 case Primitive::kPrimNot: 300 case Primitive::kPrimBoolean: 301 case Primitive::kPrimByte: 302 case Primitive::kPrimChar: 303 case Primitive::kPrimShort: 304 case Primitive::kPrimInt: 305 is_split_long_or_double_ = false; 306 Visit(); 307 if (!kQuickSoftFloatAbi || kNumQuickGprArgs == gpr_index_) { 308 stack_index_++; 309 } 310 if (gpr_index_ < kNumQuickGprArgs) { 311 gpr_index_++; 312 } 313 break; 314 case Primitive::kPrimFloat: 315 is_split_long_or_double_ = false; 316 Visit(); 317 if (kQuickSoftFloatAbi) { 318 if (gpr_index_ < kNumQuickGprArgs) { 319 gpr_index_++; 320 } else { 321 stack_index_++; 322 } 323 } else { 324 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) { 325 fpr_index_++; 326 } 327 stack_index_++; 328 } 329 break; 330 case Primitive::kPrimDouble: 331 case Primitive::kPrimLong: 332 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) { 333 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) && 334 ((gpr_index_ + 1) == kNumQuickGprArgs); 335 Visit(); 336 if (!kQuickSoftFloatAbi || kNumQuickGprArgs == gpr_index_) { 337 if (kBytesStackArgLocation == 4) { 338 stack_index_+= 2; 339 } else { 340 CHECK_EQ(kBytesStackArgLocation, 8U); 341 stack_index_++; 342 } 343 } 344 if (gpr_index_ < kNumQuickGprArgs) { 345 gpr_index_++; 346 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) { 347 if (gpr_index_ < kNumQuickGprArgs) { 348 gpr_index_++; 349 } else if (kQuickSoftFloatAbi) { 350 stack_index_++; 351 } 352 } 353 } 354 } else { 355 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) && 356 ((fpr_index_ + 1) == kNumQuickFprArgs); 357 Visit(); 358 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) { 359 fpr_index_++; 360 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) { 361 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) { 362 fpr_index_++; 363 } 364 } 365 } 366 if (kBytesStackArgLocation == 4) { 367 stack_index_+= 2; 368 } else { 369 CHECK_EQ(kBytesStackArgLocation, 8U); 370 stack_index_++; 371 } 372 } 373 break; 374 default: 375 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_; 376 } 377 } 378 } 379 380 private: 381 static size_t StackArgumentStartFromShorty(bool is_static, const char* shorty, 382 uint32_t shorty_len) { 383 if (kQuickSoftFloatAbi) { 384 CHECK_EQ(kNumQuickFprArgs, 0U); 385 return (kNumQuickGprArgs * GetBytesPerGprSpillLocation(kRuntimeISA)) 386 + GetBytesPerGprSpillLocation(kRuntimeISA) /* ArtMethod* */; 387 } else { 388 // For now, there is no reg-spill area for the targets with 389 // hard float ABI. So, the offset pointing to the first method's 390 // parameter ('this' for non-static methods) should be returned. 391 return GetBytesPerGprSpillLocation(kRuntimeISA); // Skip Method*. 392 } 393 } 394 395 const bool is_static_; 396 const char* const shorty_; 397 const uint32_t shorty_len_; 398 byte* const gpr_args_; // Address of GPR arguments in callee save frame. 399 byte* const fpr_args_; // Address of FPR arguments in callee save frame. 400 byte* const stack_args_; // Address of stack arguments in caller's frame. 401 uint32_t gpr_index_; // Index into spilled GPRs. 402 uint32_t fpr_index_; // Index into spilled FPRs. 403 uint32_t stack_index_; // Index into arguments on the stack. 404 // The current type of argument during VisitArguments. 405 Primitive::Type cur_type_; 406 // Does a 64bit parameter straddle the register and stack arguments? 407 bool is_split_long_or_double_; 408}; 409 410// Visits arguments on the stack placing them into the shadow frame. 411class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor { 412 public: 413 BuildQuickShadowFrameVisitor(mirror::ArtMethod** sp, bool is_static, const char* shorty, 414 uint32_t shorty_len, ShadowFrame* sf, size_t first_arg_reg) : 415 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {} 416 417 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE; 418 419 private: 420 ShadowFrame* const sf_; 421 uint32_t cur_reg_; 422 423 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor); 424}; 425 426void BuildQuickShadowFrameVisitor::Visit() { 427 Primitive::Type type = GetParamPrimitiveType(); 428 switch (type) { 429 case Primitive::kPrimLong: // Fall-through. 430 case Primitive::kPrimDouble: 431 if (IsSplitLongOrDouble()) { 432 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam()); 433 } else { 434 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress())); 435 } 436 ++cur_reg_; 437 break; 438 case Primitive::kPrimNot: { 439 StackReference<mirror::Object>* stack_ref = 440 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress()); 441 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr()); 442 } 443 break; 444 case Primitive::kPrimBoolean: // Fall-through. 445 case Primitive::kPrimByte: // Fall-through. 446 case Primitive::kPrimChar: // Fall-through. 447 case Primitive::kPrimShort: // Fall-through. 448 case Primitive::kPrimInt: // Fall-through. 449 case Primitive::kPrimFloat: 450 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress())); 451 break; 452 case Primitive::kPrimVoid: 453 LOG(FATAL) << "UNREACHABLE"; 454 break; 455 } 456 ++cur_reg_; 457} 458 459extern "C" uint64_t artQuickToInterpreterBridge(mirror::ArtMethod* method, Thread* self, 460 mirror::ArtMethod** sp) 461 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 462 // Ensure we don't get thread suspension until the object arguments are safely in the shadow 463 // frame. 464 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs); 465 466 if (method->IsAbstract()) { 467 ThrowAbstractMethodError(method); 468 return 0; 469 } else { 470 DCHECK(!method->IsNative()) << PrettyMethod(method); 471 const char* old_cause = self->StartAssertNoThreadSuspension("Building interpreter shadow frame"); 472 MethodHelper mh(method); 473 const DexFile::CodeItem* code_item = mh.GetCodeItem(); 474 DCHECK(code_item != nullptr) << PrettyMethod(method); 475 uint16_t num_regs = code_item->registers_size_; 476 void* memory = alloca(ShadowFrame::ComputeSize(num_regs)); 477 ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, NULL, // No last shadow coming from quick. 478 method, 0, memory)); 479 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_; 480 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, mh.IsStatic(), mh.GetShorty(), 481 mh.GetShortyLength(), 482 shadow_frame, first_arg_reg); 483 shadow_frame_builder.VisitArguments(); 484 // Push a transition back into managed code onto the linked list in thread. 485 ManagedStack fragment; 486 self->PushManagedStackFragment(&fragment); 487 self->PushShadowFrame(shadow_frame); 488 self->EndAssertNoThreadSuspension(old_cause); 489 490 if (method->IsStatic() && !method->GetDeclaringClass()->IsInitializing()) { 491 // Ensure static method's class is initialized. 492 StackHandleScope<1> hs(self); 493 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass())); 494 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(h_class, true, true)) { 495 DCHECK(Thread::Current()->IsExceptionPending()) << PrettyMethod(method); 496 self->PopManagedStackFragment(fragment); 497 return 0; 498 } 499 } 500 501 JValue result = interpreter::EnterInterpreterFromStub(self, mh, code_item, *shadow_frame); 502 // Pop transition. 503 self->PopManagedStackFragment(fragment); 504 // No need to restore the args since the method has already been run by the interpreter. 505 return result.GetJ(); 506 } 507} 508 509// Visits arguments on the stack placing them into the args vector, Object* arguments are converted 510// to jobjects. 511class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor { 512 public: 513 BuildQuickArgumentVisitor(mirror::ArtMethod** sp, bool is_static, const char* shorty, 514 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa, 515 std::vector<jvalue>* args) : 516 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {} 517 518 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE; 519 520 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 521 522 private: 523 ScopedObjectAccessUnchecked* const soa_; 524 std::vector<jvalue>* const args_; 525 // References which we must update when exiting in case the GC moved the objects. 526 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_; 527 528 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor); 529}; 530 531void BuildQuickArgumentVisitor::Visit() { 532 jvalue val; 533 Primitive::Type type = GetParamPrimitiveType(); 534 switch (type) { 535 case Primitive::kPrimNot: { 536 StackReference<mirror::Object>* stack_ref = 537 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress()); 538 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr()); 539 references_.push_back(std::make_pair(val.l, stack_ref)); 540 break; 541 } 542 case Primitive::kPrimLong: // Fall-through. 543 case Primitive::kPrimDouble: 544 if (IsSplitLongOrDouble()) { 545 val.j = ReadSplitLongParam(); 546 } else { 547 val.j = *reinterpret_cast<jlong*>(GetParamAddress()); 548 } 549 break; 550 case Primitive::kPrimBoolean: // Fall-through. 551 case Primitive::kPrimByte: // Fall-through. 552 case Primitive::kPrimChar: // Fall-through. 553 case Primitive::kPrimShort: // Fall-through. 554 case Primitive::kPrimInt: // Fall-through. 555 case Primitive::kPrimFloat: 556 val.i = *reinterpret_cast<jint*>(GetParamAddress()); 557 break; 558 case Primitive::kPrimVoid: 559 LOG(FATAL) << "UNREACHABLE"; 560 val.j = 0; 561 break; 562 } 563 args_->push_back(val); 564} 565 566void BuildQuickArgumentVisitor::FixupReferences() { 567 // Fixup any references which may have changed. 568 for (const auto& pair : references_) { 569 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first)); 570 soa_->Env()->DeleteLocalRef(pair.first); 571 } 572} 573 574// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method 575// which is responsible for recording callee save registers. We explicitly place into jobjects the 576// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a 577// field within the proxy object, which will box the primitive arguments and deal with error cases. 578extern "C" uint64_t artQuickProxyInvokeHandler(mirror::ArtMethod* proxy_method, 579 mirror::Object* receiver, 580 Thread* self, mirror::ArtMethod** sp) 581 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 582 DCHECK(proxy_method->IsProxyMethod()) << PrettyMethod(proxy_method); 583 DCHECK(receiver->GetClass()->IsProxyClass()) << PrettyMethod(proxy_method); 584 // Ensure we don't get thread suspension until the object arguments are safely in jobjects. 585 const char* old_cause = 586 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments"); 587 // Register the top of the managed stack, making stack crawlable. 588 DCHECK_EQ(*sp, proxy_method) << PrettyMethod(proxy_method); 589 self->SetTopOfStack(sp, 0); 590 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(), 591 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes()) 592 << PrettyMethod(proxy_method); 593 self->VerifyStack(); 594 // Start new JNI local reference state. 595 JNIEnvExt* env = self->GetJniEnv(); 596 ScopedObjectAccessUnchecked soa(env); 597 ScopedJniEnvLocalRefState env_state(env); 598 // Create local ref. copies of proxy method and the receiver. 599 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver); 600 601 // Placing arguments into args vector and remove the receiver. 602 MethodHelper proxy_mh(proxy_method); 603 DCHECK(!proxy_mh.IsStatic()) << PrettyMethod(proxy_method); 604 std::vector<jvalue> args; 605 BuildQuickArgumentVisitor local_ref_visitor(sp, proxy_mh.IsStatic(), proxy_mh.GetShorty(), 606 proxy_mh.GetShortyLength(), &soa, &args); 607 608 local_ref_visitor.VisitArguments(); 609 DCHECK_GT(args.size(), 0U) << PrettyMethod(proxy_method); 610 args.erase(args.begin()); 611 612 // Convert proxy method into expected interface method. 613 mirror::ArtMethod* interface_method = proxy_method->FindOverriddenMethod(); 614 DCHECK(interface_method != NULL) << PrettyMethod(proxy_method); 615 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method); 616 jobject interface_method_jobj = soa.AddLocalReference<jobject>(interface_method); 617 618 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code 619 // that performs allocations. 620 self->EndAssertNoThreadSuspension(old_cause); 621 JValue result = InvokeProxyInvocationHandler(soa, proxy_mh.GetShorty(), 622 rcvr_jobj, interface_method_jobj, args); 623 // Restore references which might have moved. 624 local_ref_visitor.FixupReferences(); 625 return result.GetJ(); 626} 627 628// Read object references held in arguments from quick frames and place in a JNI local references, 629// so they don't get garbage collected. 630class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor { 631 public: 632 RememberForGcArgumentVisitor(mirror::ArtMethod** sp, bool is_static, const char* shorty, 633 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) : 634 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {} 635 636 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE; 637 638 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 639 640 private: 641 ScopedObjectAccessUnchecked* const soa_; 642 // References which we must update when exiting in case the GC moved the objects. 643 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_; 644 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor); 645}; 646 647void RememberForGcArgumentVisitor::Visit() { 648 if (IsParamAReference()) { 649 StackReference<mirror::Object>* stack_ref = 650 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress()); 651 jobject reference = 652 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr()); 653 references_.push_back(std::make_pair(reference, stack_ref)); 654 } 655} 656 657void RememberForGcArgumentVisitor::FixupReferences() { 658 // Fixup any references which may have changed. 659 for (const auto& pair : references_) { 660 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first)); 661 soa_->Env()->DeleteLocalRef(pair.first); 662 } 663} 664 665 666// Lazily resolve a method for quick. Called by stub code. 667extern "C" const void* artQuickResolutionTrampoline(mirror::ArtMethod* called, 668 mirror::Object* receiver, 669 Thread* self, mirror::ArtMethod** sp) 670 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 671 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs); 672 // Start new JNI local reference state 673 JNIEnvExt* env = self->GetJniEnv(); 674 ScopedObjectAccessUnchecked soa(env); 675 ScopedJniEnvLocalRefState env_state(env); 676 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up"); 677 678 // Compute details about the called method (avoid GCs) 679 ClassLinker* linker = Runtime::Current()->GetClassLinker(); 680 mirror::ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp); 681 InvokeType invoke_type; 682 const DexFile* dex_file; 683 uint32_t dex_method_idx; 684 if (called->IsRuntimeMethod()) { 685 uint32_t dex_pc = caller->ToDexPc(QuickArgumentVisitor::GetCallingPc(sp)); 686 const DexFile::CodeItem* code; 687 { 688 MethodHelper mh(caller); 689 dex_file = &mh.GetDexFile(); 690 code = mh.GetCodeItem(); 691 } 692 CHECK_LT(dex_pc, code->insns_size_in_code_units_); 693 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]); 694 Instruction::Code instr_code = instr->Opcode(); 695 bool is_range; 696 switch (instr_code) { 697 case Instruction::INVOKE_DIRECT: 698 invoke_type = kDirect; 699 is_range = false; 700 break; 701 case Instruction::INVOKE_DIRECT_RANGE: 702 invoke_type = kDirect; 703 is_range = true; 704 break; 705 case Instruction::INVOKE_STATIC: 706 invoke_type = kStatic; 707 is_range = false; 708 break; 709 case Instruction::INVOKE_STATIC_RANGE: 710 invoke_type = kStatic; 711 is_range = true; 712 break; 713 case Instruction::INVOKE_SUPER: 714 invoke_type = kSuper; 715 is_range = false; 716 break; 717 case Instruction::INVOKE_SUPER_RANGE: 718 invoke_type = kSuper; 719 is_range = true; 720 break; 721 case Instruction::INVOKE_VIRTUAL: 722 invoke_type = kVirtual; 723 is_range = false; 724 break; 725 case Instruction::INVOKE_VIRTUAL_RANGE: 726 invoke_type = kVirtual; 727 is_range = true; 728 break; 729 case Instruction::INVOKE_INTERFACE: 730 invoke_type = kInterface; 731 is_range = false; 732 break; 733 case Instruction::INVOKE_INTERFACE_RANGE: 734 invoke_type = kInterface; 735 is_range = true; 736 break; 737 default: 738 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(NULL); 739 // Avoid used uninitialized warnings. 740 invoke_type = kDirect; 741 is_range = false; 742 } 743 dex_method_idx = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c(); 744 745 } else { 746 invoke_type = kStatic; 747 dex_file = &MethodHelper(called).GetDexFile(); 748 dex_method_idx = called->GetDexMethodIndex(); 749 } 750 uint32_t shorty_len; 751 const char* shorty = 752 dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx), &shorty_len); 753 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa); 754 visitor.VisitArguments(); 755 self->EndAssertNoThreadSuspension(old_cause); 756 bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface; 757 // Resolve method filling in dex cache. 758 if (UNLIKELY(called->IsRuntimeMethod())) { 759 StackHandleScope<1> hs(self); 760 mirror::Object* dummy = nullptr; 761 HandleWrapper<mirror::Object> h_receiver( 762 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy)); 763 called = linker->ResolveMethod(self, dex_method_idx, &caller, invoke_type); 764 } 765 const void* code = NULL; 766 if (LIKELY(!self->IsExceptionPending())) { 767 // Incompatible class change should have been handled in resolve method. 768 CHECK(!called->CheckIncompatibleClassChange(invoke_type)) 769 << PrettyMethod(called) << " " << invoke_type; 770 if (virtual_or_interface) { 771 // Refine called method based on receiver. 772 CHECK(receiver != nullptr) << invoke_type; 773 774 mirror::ArtMethod* orig_called = called; 775 if (invoke_type == kVirtual) { 776 called = receiver->GetClass()->FindVirtualMethodForVirtual(called); 777 } else { 778 called = receiver->GetClass()->FindVirtualMethodForInterface(called); 779 } 780 781 CHECK(called != nullptr) << PrettyMethod(orig_called) << " " 782 << PrettyTypeOf(receiver) << " " 783 << invoke_type << " " << orig_called->GetVtableIndex(); 784 785 // We came here because of sharpening. Ensure the dex cache is up-to-date on the method index 786 // of the sharpened method. 787 if (called->GetDexCacheResolvedMethods() == caller->GetDexCacheResolvedMethods()) { 788 caller->GetDexCacheResolvedMethods()->Set<false>(called->GetDexMethodIndex(), called); 789 } else { 790 // Calling from one dex file to another, need to compute the method index appropriate to 791 // the caller's dex file. Since we get here only if the original called was a runtime 792 // method, we've got the correct dex_file and a dex_method_idx from above. 793 DCHECK(&MethodHelper(caller).GetDexFile() == dex_file); 794 uint32_t method_index = 795 MethodHelper(called).FindDexMethodIndexInOtherDexFile(*dex_file, dex_method_idx); 796 if (method_index != DexFile::kDexNoIndex) { 797 caller->GetDexCacheResolvedMethods()->Set<false>(method_index, called); 798 } 799 } 800 } 801 // Ensure that the called method's class is initialized. 802 StackHandleScope<1> hs(soa.Self()); 803 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass())); 804 linker->EnsureInitialized(called_class, true, true); 805 if (LIKELY(called_class->IsInitialized())) { 806 code = called->GetEntryPointFromQuickCompiledCode(); 807 } else if (called_class->IsInitializing()) { 808 if (invoke_type == kStatic) { 809 // Class is still initializing, go to oat and grab code (trampoline must be left in place 810 // until class is initialized to stop races between threads). 811 code = linker->GetQuickOatCodeFor(called); 812 } else { 813 // No trampoline for non-static methods. 814 code = called->GetEntryPointFromQuickCompiledCode(); 815 } 816 } else { 817 DCHECK(called_class->IsErroneous()); 818 } 819 } 820 CHECK_EQ(code == NULL, self->IsExceptionPending()); 821 // Fixup any locally saved objects may have moved during a GC. 822 visitor.FixupReferences(); 823 // Place called method in callee-save frame to be placed as first argument to quick method. 824 *sp = called; 825 return code; 826} 827 828 829 830/* 831 * This class uses a couple of observations to unite the different calling conventions through 832 * a few constants. 833 * 834 * 1) Number of registers used for passing is normally even, so counting down has no penalty for 835 * possible alignment. 836 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point 837 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote 838 * when we have to split things 839 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats 840 * and we can use Int handling directly. 841 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code 842 * necessary when widening. Also, widening of Ints will take place implicitly, and the 843 * extension should be compatible with Aarch64, which mandates copying the available bits 844 * into LSB and leaving the rest unspecified. 845 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on 846 * the stack. 847 * 6) There is only little endian. 848 * 849 * 850 * Actual work is supposed to be done in a delegate of the template type. The interface is as 851 * follows: 852 * 853 * void PushGpr(uintptr_t): Add a value for the next GPR 854 * 855 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need 856 * padding, that is, think the architecture is 32b and aligns 64b. 857 * 858 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to 859 * split this if necessary. The current state will have aligned, if 860 * necessary. 861 * 862 * void PushStack(uintptr_t): Push a value to the stack. 863 * 864 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr, 865 * as this might be important for null initialization. 866 * Must return the jobject, that is, the reference to the 867 * entry in the HandleScope (nullptr if necessary). 868 * 869 */ 870template <class T> class BuildGenericJniFrameStateMachine { 871 public: 872#if defined(__arm__) 873 // TODO: These are all dummy values! 874 static constexpr bool kNativeSoftFloatAbi = true; 875 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3 876 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs. 877 878 static constexpr size_t kRegistersNeededForLong = 2; 879 static constexpr size_t kRegistersNeededForDouble = 2; 880 static constexpr bool kMultiRegistersAligned = true; 881 static constexpr bool kMultiRegistersWidened = false; 882 static constexpr bool kAlignLongOnStack = true; 883 static constexpr bool kAlignDoubleOnStack = true; 884#elif defined(__aarch64__) 885 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI. 886 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs. 887 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs. 888 889 static constexpr size_t kRegistersNeededForLong = 1; 890 static constexpr size_t kRegistersNeededForDouble = 1; 891 static constexpr bool kMultiRegistersAligned = false; 892 static constexpr bool kMultiRegistersWidened = false; 893 static constexpr bool kAlignLongOnStack = false; 894 static constexpr bool kAlignDoubleOnStack = false; 895#elif defined(__mips__) 896 // TODO: These are all dummy values! 897 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI. 898 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs. 899 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs. 900 901 static constexpr size_t kRegistersNeededForLong = 2; 902 static constexpr size_t kRegistersNeededForDouble = 2; 903 static constexpr bool kMultiRegistersAligned = true; 904 static constexpr bool kMultiRegistersWidened = true; 905 static constexpr bool kAlignLongOnStack = false; 906 static constexpr bool kAlignDoubleOnStack = false; 907#elif defined(__i386__) 908 // TODO: Check these! 909 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp 910 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs. 911 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs. 912 913 static constexpr size_t kRegistersNeededForLong = 2; 914 static constexpr size_t kRegistersNeededForDouble = 2; 915 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways 916 static constexpr bool kMultiRegistersWidened = false; 917 static constexpr bool kAlignLongOnStack = false; 918 static constexpr bool kAlignDoubleOnStack = false; 919#elif defined(__x86_64__) 920 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI. 921 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs. 922 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs. 923 924 static constexpr size_t kRegistersNeededForLong = 1; 925 static constexpr size_t kRegistersNeededForDouble = 1; 926 static constexpr bool kMultiRegistersAligned = false; 927 static constexpr bool kMultiRegistersWidened = false; 928 static constexpr bool kAlignLongOnStack = false; 929 static constexpr bool kAlignDoubleOnStack = false; 930#else 931#error "Unsupported architecture" 932#endif 933 934 public: 935 explicit BuildGenericJniFrameStateMachine(T* delegate) : gpr_index_(kNumNativeGprArgs), 936 fpr_index_(kNumNativeFprArgs), 937 stack_entries_(0), 938 delegate_(delegate) { 939 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff 940 // the next register is even; counting down is just to make the compiler happy... 941 CHECK_EQ(kNumNativeGprArgs % 2, 0U); 942 CHECK_EQ(kNumNativeFprArgs % 2, 0U); 943 } 944 945 virtual ~BuildGenericJniFrameStateMachine() {} 946 947 bool HavePointerGpr() { 948 return gpr_index_ > 0; 949 } 950 951 void AdvancePointer(void* val) { 952 if (HavePointerGpr()) { 953 gpr_index_--; 954 PushGpr(reinterpret_cast<uintptr_t>(val)); 955 } else { 956 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b 957 PushStack(reinterpret_cast<uintptr_t>(val)); 958 gpr_index_ = 0; 959 } 960 } 961 962 963 bool HaveHandleScopeGpr() { 964 return gpr_index_ > 0; 965 } 966 967 void AdvanceHandleScope(mirror::Object* ptr) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 968 uintptr_t handle = PushHandle(ptr); 969 if (HaveHandleScopeGpr()) { 970 gpr_index_--; 971 PushGpr(handle); 972 } else { 973 stack_entries_++; 974 PushStack(handle); 975 gpr_index_ = 0; 976 } 977 } 978 979 980 bool HaveIntGpr() { 981 return gpr_index_ > 0; 982 } 983 984 void AdvanceInt(uint32_t val) { 985 if (HaveIntGpr()) { 986 gpr_index_--; 987 PushGpr(val); 988 } else { 989 stack_entries_++; 990 PushStack(val); 991 gpr_index_ = 0; 992 } 993 } 994 995 996 bool HaveLongGpr() { 997 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0); 998 } 999 1000 bool LongGprNeedsPadding() { 1001 return kRegistersNeededForLong > 1 && // only pad when using multiple registers 1002 kAlignLongOnStack && // and when it needs alignment 1003 (gpr_index_ & 1) == 1; // counter is odd, see constructor 1004 } 1005 1006 bool LongStackNeedsPadding() { 1007 return kRegistersNeededForLong > 1 && // only pad when using multiple registers 1008 kAlignLongOnStack && // and when it needs 8B alignment 1009 (stack_entries_ & 1) == 1; // counter is odd 1010 } 1011 1012 void AdvanceLong(uint64_t val) { 1013 if (HaveLongGpr()) { 1014 if (LongGprNeedsPadding()) { 1015 PushGpr(0); 1016 gpr_index_--; 1017 } 1018 if (kRegistersNeededForLong == 1) { 1019 PushGpr(static_cast<uintptr_t>(val)); 1020 } else { 1021 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF)); 1022 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF)); 1023 } 1024 gpr_index_ -= kRegistersNeededForLong; 1025 } else { 1026 if (LongStackNeedsPadding()) { 1027 PushStack(0); 1028 stack_entries_++; 1029 } 1030 if (kRegistersNeededForLong == 1) { 1031 PushStack(static_cast<uintptr_t>(val)); 1032 stack_entries_++; 1033 } else { 1034 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF)); 1035 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF)); 1036 stack_entries_ += 2; 1037 } 1038 gpr_index_ = 0; 1039 } 1040 } 1041 1042 1043 bool HaveFloatFpr() { 1044 return fpr_index_ > 0; 1045 } 1046 1047 template <typename U, typename V> V convert(U in) { 1048 CHECK_LE(sizeof(U), sizeof(V)); 1049 union { U u; V v; } tmp; 1050 tmp.u = in; 1051 return tmp.v; 1052 } 1053 1054 void AdvanceFloat(float val) { 1055 if (kNativeSoftFloatAbi) { 1056 AdvanceInt(convert<float, uint32_t>(val)); 1057 } else { 1058 if (HaveFloatFpr()) { 1059 fpr_index_--; 1060 if (kRegistersNeededForDouble == 1) { 1061 if (kMultiRegistersWidened) { 1062 PushFpr8(convert<double, uint64_t>(val)); 1063 } else { 1064 // No widening, just use the bits. 1065 PushFpr8(convert<float, uint64_t>(val)); 1066 } 1067 } else { 1068 PushFpr4(val); 1069 } 1070 } else { 1071 stack_entries_++; 1072 if (kRegistersNeededForDouble == 1 && kMultiRegistersWidened) { 1073 // Need to widen before storing: Note the "double" in the template instantiation. 1074 PushStack(convert<double, uintptr_t>(val)); 1075 } else { 1076 PushStack(convert<float, uintptr_t>(val)); 1077 } 1078 fpr_index_ = 0; 1079 } 1080 } 1081 } 1082 1083 1084 bool HaveDoubleFpr() { 1085 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0); 1086 } 1087 1088 bool DoubleFprNeedsPadding() { 1089 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers 1090 kAlignDoubleOnStack && // and when it needs alignment 1091 (fpr_index_ & 1) == 1; // counter is odd, see constructor 1092 } 1093 1094 bool DoubleStackNeedsPadding() { 1095 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers 1096 kAlignDoubleOnStack && // and when it needs 8B alignment 1097 (stack_entries_ & 1) == 1; // counter is odd 1098 } 1099 1100 void AdvanceDouble(uint64_t val) { 1101 if (kNativeSoftFloatAbi) { 1102 AdvanceLong(val); 1103 } else { 1104 if (HaveDoubleFpr()) { 1105 if (DoubleFprNeedsPadding()) { 1106 PushFpr4(0); 1107 fpr_index_--; 1108 } 1109 PushFpr8(val); 1110 fpr_index_ -= kRegistersNeededForDouble; 1111 } else { 1112 if (DoubleStackNeedsPadding()) { 1113 PushStack(0); 1114 stack_entries_++; 1115 } 1116 if (kRegistersNeededForDouble == 1) { 1117 PushStack(static_cast<uintptr_t>(val)); 1118 stack_entries_++; 1119 } else { 1120 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF)); 1121 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF)); 1122 stack_entries_ += 2; 1123 } 1124 fpr_index_ = 0; 1125 } 1126 } 1127 } 1128 1129 uint32_t getStackEntries() { 1130 return stack_entries_; 1131 } 1132 1133 uint32_t getNumberOfUsedGprs() { 1134 return kNumNativeGprArgs - gpr_index_; 1135 } 1136 1137 uint32_t getNumberOfUsedFprs() { 1138 return kNumNativeFprArgs - fpr_index_; 1139 } 1140 1141 private: 1142 void PushGpr(uintptr_t val) { 1143 delegate_->PushGpr(val); 1144 } 1145 void PushFpr4(float val) { 1146 delegate_->PushFpr4(val); 1147 } 1148 void PushFpr8(uint64_t val) { 1149 delegate_->PushFpr8(val); 1150 } 1151 void PushStack(uintptr_t val) { 1152 delegate_->PushStack(val); 1153 } 1154 uintptr_t PushHandle(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1155 return delegate_->PushHandle(ref); 1156 } 1157 1158 uint32_t gpr_index_; // Number of free GPRs 1159 uint32_t fpr_index_; // Number of free FPRs 1160 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not 1161 // extended 1162 T* delegate_; // What Push implementation gets called 1163}; 1164 1165class ComputeGenericJniFrameSize FINAL { 1166 public: 1167 ComputeGenericJniFrameSize() : num_handle_scope_references_(0), num_stack_entries_(0) {} 1168 1169 uint32_t GetStackSize() { 1170 return num_stack_entries_ * sizeof(uintptr_t); 1171 } 1172 1173 // WARNING: After this, *sp won't be pointing to the method anymore! 1174 void ComputeLayout(mirror::ArtMethod*** m, bool is_static, const char* shorty, uint32_t shorty_len, 1175 void* sp, HandleScope** table, uint32_t* handle_scope_entries, 1176 uintptr_t** start_stack, uintptr_t** start_gpr, uint32_t** start_fpr, 1177 void** code_return, size_t* overall_size) 1178 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1179 ComputeAll(is_static, shorty, shorty_len); 1180 1181 mirror::ArtMethod* method = **m; 1182 1183 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp); 1184 1185 // First, fix up the layout of the callee-save frame. 1186 // We have to squeeze in the HandleScope, and relocate the method pointer. 1187 1188 // "Free" the slot for the method. 1189 sp8 += kPointerSize; 1190 1191 // Add the HandleScope. 1192 *handle_scope_entries = num_handle_scope_references_; 1193 size_t handle_scope_size = HandleScope::GetAlignedHandleScopeSize(num_handle_scope_references_); 1194 sp8 -= handle_scope_size; 1195 *table = reinterpret_cast<HandleScope*>(sp8); 1196 (*table)->SetNumberOfReferences(num_handle_scope_references_); 1197 1198 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us. 1199 sp8 -= kPointerSize; 1200 uint8_t* method_pointer = sp8; 1201 *(reinterpret_cast<mirror::ArtMethod**>(method_pointer)) = method; 1202 *m = reinterpret_cast<mirror::ArtMethod**>(method_pointer); 1203 1204 // Reference cookie and padding 1205 sp8 -= 8; 1206 // Store HandleScope size 1207 *reinterpret_cast<uint32_t*>(sp8) = static_cast<uint32_t>(handle_scope_size & 0xFFFFFFFF); 1208 1209 // Next comes the native call stack. 1210 sp8 -= GetStackSize(); 1211 // Now align the call stack below. This aligns by 16, as AArch64 seems to require. 1212 uintptr_t mask = ~0x0F; 1213 sp8 = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(sp8) & mask); 1214 *start_stack = reinterpret_cast<uintptr_t*>(sp8); 1215 1216 // put fprs and gprs below 1217 // Assumption is OK right now, as we have soft-float arm 1218 size_t fregs = BuildGenericJniFrameStateMachine<ComputeGenericJniFrameSize>::kNumNativeFprArgs; 1219 sp8 -= fregs * sizeof(uintptr_t); 1220 *start_fpr = reinterpret_cast<uint32_t*>(sp8); 1221 size_t iregs = BuildGenericJniFrameStateMachine<ComputeGenericJniFrameSize>::kNumNativeGprArgs; 1222 sp8 -= iregs * sizeof(uintptr_t); 1223 *start_gpr = reinterpret_cast<uintptr_t*>(sp8); 1224 1225 // reserve space for the code pointer 1226 sp8 -= kPointerSize; 1227 *code_return = reinterpret_cast<void*>(sp8); 1228 1229 *overall_size = reinterpret_cast<uint8_t*>(sp) - sp8; 1230 1231 // The new SP is stored at the end of the alloca, so it can be immediately popped 1232 sp8 = reinterpret_cast<uint8_t*>(sp) - 5 * KB; 1233 *(reinterpret_cast<uint8_t**>(sp8)) = method_pointer; 1234 } 1235 1236 void ComputeHandleScopeOffset() { } // nothing to do, static right now 1237 1238 void ComputeAll(bool is_static, const char* shorty, uint32_t shorty_len) 1239 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1240 BuildGenericJniFrameStateMachine<ComputeGenericJniFrameSize> sm(this); 1241 1242 // JNIEnv 1243 sm.AdvancePointer(nullptr); 1244 1245 // Class object or this as first argument 1246 sm.AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678)); 1247 1248 for (uint32_t i = 1; i < shorty_len; ++i) { 1249 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]); 1250 switch (cur_type_) { 1251 case Primitive::kPrimNot: 1252 sm.AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678)); 1253 break; 1254 1255 case Primitive::kPrimBoolean: 1256 case Primitive::kPrimByte: 1257 case Primitive::kPrimChar: 1258 case Primitive::kPrimShort: 1259 case Primitive::kPrimInt: 1260 sm.AdvanceInt(0); 1261 break; 1262 case Primitive::kPrimFloat: 1263 sm.AdvanceFloat(0); 1264 break; 1265 case Primitive::kPrimDouble: 1266 sm.AdvanceDouble(0); 1267 break; 1268 case Primitive::kPrimLong: 1269 sm.AdvanceLong(0); 1270 break; 1271 default: 1272 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty; 1273 } 1274 } 1275 1276 num_stack_entries_ = sm.getStackEntries(); 1277 } 1278 1279 void PushGpr(uintptr_t /* val */) { 1280 // not optimizing registers, yet 1281 } 1282 1283 void PushFpr4(float /* val */) { 1284 // not optimizing registers, yet 1285 } 1286 1287 void PushFpr8(uint64_t /* val */) { 1288 // not optimizing registers, yet 1289 } 1290 1291 void PushStack(uintptr_t /* val */) { 1292 // counting is already done in the superclass 1293 } 1294 1295 uintptr_t PushHandle(mirror::Object* /* ptr */) { 1296 num_handle_scope_references_++; 1297 return reinterpret_cast<uintptr_t>(nullptr); 1298 } 1299 1300 private: 1301 uint32_t num_handle_scope_references_; 1302 uint32_t num_stack_entries_; 1303}; 1304 1305// Visits arguments on the stack placing them into a region lower down the stack for the benefit 1306// of transitioning into native code. 1307class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor { 1308 public: 1309 BuildGenericJniFrameVisitor(mirror::ArtMethod*** sp, bool is_static, const char* shorty, 1310 uint32_t shorty_len, Thread* self) : 1311 QuickArgumentVisitor(*sp, is_static, shorty, shorty_len), sm_(this) { 1312 ComputeGenericJniFrameSize fsc; 1313 fsc.ComputeLayout(sp, is_static, shorty, shorty_len, *sp, &handle_scope_, &handle_scope_expected_refs_, 1314 &cur_stack_arg_, &cur_gpr_reg_, &cur_fpr_reg_, &code_return_, 1315 &alloca_used_size_); 1316 handle_scope_number_of_references_ = 0; 1317 cur_hs_entry_ = GetFirstHandleScopeEntry(); 1318 1319 // jni environment is always first argument 1320 sm_.AdvancePointer(self->GetJniEnv()); 1321 1322 if (is_static) { 1323 sm_.AdvanceHandleScope((**sp)->GetDeclaringClass()); 1324 } 1325 } 1326 1327 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE; 1328 1329 void FinalizeHandleScope(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 1330 1331 StackReference<mirror::Object>* GetFirstHandleScopeEntry() 1332 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1333 return handle_scope_->GetHandle(0).GetReference(); 1334 } 1335 1336 jobject GetFirstHandleScopeJObject() 1337 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1338 return handle_scope_->GetHandle(0).ToJObject(); 1339 } 1340 1341 void PushGpr(uintptr_t val) { 1342 *cur_gpr_reg_ = val; 1343 cur_gpr_reg_++; 1344 } 1345 1346 void PushFpr4(float val) { 1347 *cur_fpr_reg_ = val; 1348 cur_fpr_reg_++; 1349 } 1350 1351 void PushFpr8(uint64_t val) { 1352 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_); 1353 *tmp = val; 1354 cur_fpr_reg_ += 2; 1355 } 1356 1357 void PushStack(uintptr_t val) { 1358 *cur_stack_arg_ = val; 1359 cur_stack_arg_++; 1360 } 1361 1362 uintptr_t PushHandle(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1363 uintptr_t tmp; 1364 if (ref == nullptr) { 1365 *cur_hs_entry_ = StackReference<mirror::Object>(); 1366 tmp = reinterpret_cast<uintptr_t>(nullptr); 1367 } else { 1368 *cur_hs_entry_ = StackReference<mirror::Object>::FromMirrorPtr(ref); 1369 tmp = reinterpret_cast<uintptr_t>(cur_hs_entry_); 1370 } 1371 cur_hs_entry_++; 1372 handle_scope_number_of_references_++; 1373 return tmp; 1374 } 1375 1376 // Size of the part of the alloca that we actually need. 1377 size_t GetAllocaUsedSize() { 1378 return alloca_used_size_; 1379 } 1380 1381 void* GetCodeReturn() { 1382 return code_return_; 1383 } 1384 1385 private: 1386 uint32_t handle_scope_number_of_references_; 1387 StackReference<mirror::Object>* cur_hs_entry_; 1388 HandleScope* handle_scope_; 1389 uint32_t handle_scope_expected_refs_; 1390 uintptr_t* cur_gpr_reg_; 1391 uint32_t* cur_fpr_reg_; 1392 uintptr_t* cur_stack_arg_; 1393 // StackReference<mirror::Object>* top_of_handle_scope_; 1394 void* code_return_; 1395 size_t alloca_used_size_; 1396 1397 BuildGenericJniFrameStateMachine<BuildGenericJniFrameVisitor> sm_; 1398 1399 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor); 1400}; 1401 1402void BuildGenericJniFrameVisitor::Visit() { 1403 Primitive::Type type = GetParamPrimitiveType(); 1404 switch (type) { 1405 case Primitive::kPrimLong: { 1406 jlong long_arg; 1407 if (IsSplitLongOrDouble()) { 1408 long_arg = ReadSplitLongParam(); 1409 } else { 1410 long_arg = *reinterpret_cast<jlong*>(GetParamAddress()); 1411 } 1412 sm_.AdvanceLong(long_arg); 1413 break; 1414 } 1415 case Primitive::kPrimDouble: { 1416 uint64_t double_arg; 1417 if (IsSplitLongOrDouble()) { 1418 // Read into union so that we don't case to a double. 1419 double_arg = ReadSplitLongParam(); 1420 } else { 1421 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress()); 1422 } 1423 sm_.AdvanceDouble(double_arg); 1424 break; 1425 } 1426 case Primitive::kPrimNot: { 1427 StackReference<mirror::Object>* stack_ref = 1428 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress()); 1429 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr()); 1430 break; 1431 } 1432 case Primitive::kPrimFloat: 1433 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress())); 1434 break; 1435 case Primitive::kPrimBoolean: // Fall-through. 1436 case Primitive::kPrimByte: // Fall-through. 1437 case Primitive::kPrimChar: // Fall-through. 1438 case Primitive::kPrimShort: // Fall-through. 1439 case Primitive::kPrimInt: // Fall-through. 1440 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress())); 1441 break; 1442 case Primitive::kPrimVoid: 1443 LOG(FATAL) << "UNREACHABLE"; 1444 break; 1445 } 1446} 1447 1448void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) { 1449 // Initialize padding entries. 1450 while (handle_scope_number_of_references_ < handle_scope_expected_refs_) { 1451 *cur_hs_entry_ = StackReference<mirror::Object>(); 1452 cur_hs_entry_++; 1453 handle_scope_number_of_references_++; 1454 } 1455 handle_scope_->SetNumberOfReferences(handle_scope_expected_refs_); 1456 DCHECK_NE(handle_scope_expected_refs_, 0U); 1457 // Install HandleScope. 1458 self->PushHandleScope(handle_scope_); 1459} 1460 1461extern "C" void* artFindNativeMethod(); 1462 1463uint64_t artQuickGenericJniEndJNIRef(Thread* self, uint32_t cookie, jobject l, jobject lock) { 1464 if (lock != nullptr) { 1465 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self)); 1466 } else { 1467 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self)); 1468 } 1469} 1470 1471void artQuickGenericJniEndJNINonRef(Thread* self, uint32_t cookie, jobject lock) { 1472 if (lock != nullptr) { 1473 JniMethodEndSynchronized(cookie, lock, self); 1474 } else { 1475 JniMethodEnd(cookie, self); 1476 } 1477} 1478 1479/* 1480 * Initializes an alloca region assumed to be directly below sp for a native call: 1481 * Create a HandleScope and call stack and fill a mini stack with values to be pushed to registers. 1482 * The final element on the stack is a pointer to the native code. 1483 * 1484 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it. 1485 * We need to fix this, as the handle scope needs to go into the callee-save frame. 1486 * 1487 * The return of this function denotes: 1488 * 1) How many bytes of the alloca can be released, if the value is non-negative. 1489 * 2) An error, if the value is negative. 1490 */ 1491extern "C" ssize_t artQuickGenericJniTrampoline(Thread* self, mirror::ArtMethod** sp) 1492 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1493 mirror::ArtMethod* called = *sp; 1494 DCHECK(called->IsNative()) << PrettyMethod(called, true); 1495 1496 // run the visitor 1497 MethodHelper mh(called); 1498 1499 BuildGenericJniFrameVisitor visitor(&sp, called->IsStatic(), mh.GetShorty(), mh.GetShortyLength(), 1500 self); 1501 visitor.VisitArguments(); 1502 visitor.FinalizeHandleScope(self); 1503 1504 // fix up managed-stack things in Thread 1505 self->SetTopOfStack(sp, 0); 1506 1507 self->VerifyStack(); 1508 1509 // Start JNI, save the cookie. 1510 uint32_t cookie; 1511 if (called->IsSynchronized()) { 1512 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self); 1513 if (self->IsExceptionPending()) { 1514 self->PopHandleScope(); 1515 // A negative value denotes an error. 1516 return -1; 1517 } 1518 } else { 1519 cookie = JniMethodStart(self); 1520 } 1521 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp); 1522 *(sp32 - 1) = cookie; 1523 1524 // Retrieve the stored native code. 1525 const void* nativeCode = called->GetNativeMethod(); 1526 1527 // There are two cases for the content of nativeCode: 1528 // 1) Pointer to the native function. 1529 // 2) Pointer to the trampoline for native code binding. 1530 // In the second case, we need to execute the binding and continue with the actual native function 1531 // pointer. 1532 DCHECK(nativeCode != nullptr); 1533 if (nativeCode == GetJniDlsymLookupStub()) { 1534 nativeCode = artFindNativeMethod(); 1535 1536 if (nativeCode == nullptr) { 1537 DCHECK(self->IsExceptionPending()); // There should be an exception pending now. 1538 1539 // End JNI, as the assembly will move to deliver the exception. 1540 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr; 1541 if (mh.GetShorty()[0] == 'L') { 1542 artQuickGenericJniEndJNIRef(self, cookie, nullptr, lock); 1543 } else { 1544 artQuickGenericJniEndJNINonRef(self, cookie, lock); 1545 } 1546 1547 return -1; 1548 } 1549 // Note that the native code pointer will be automatically set by artFindNativeMethod(). 1550 } 1551 1552 // Store the native code pointer in the stack at the right location. 1553 uintptr_t* code_pointer = reinterpret_cast<uintptr_t*>(visitor.GetCodeReturn()); 1554 *code_pointer = reinterpret_cast<uintptr_t>(nativeCode); 1555 1556 // 5K reserved, window_size + frame pointer used. 1557 size_t window_size = visitor.GetAllocaUsedSize(); 1558 return (5 * KB) - window_size - kPointerSize; 1559} 1560 1561/* 1562 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and 1563 * unlocking. 1564 */ 1565extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self, mirror::ArtMethod** sp, 1566 jvalue result, uint64_t result_f) 1567 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1568 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp); 1569 mirror::ArtMethod* called = *sp; 1570 uint32_t cookie = *(sp32 - 1); 1571 1572 jobject lock = nullptr; 1573 if (called->IsSynchronized()) { 1574 HandleScope* table = reinterpret_cast<HandleScope*>( 1575 reinterpret_cast<uint8_t*>(sp) + kPointerSize); 1576 lock = table->GetHandle(0).ToJObject(); 1577 } 1578 1579 MethodHelper mh(called); 1580 char return_shorty_char = mh.GetShorty()[0]; 1581 1582 if (return_shorty_char == 'L') { 1583 return artQuickGenericJniEndJNIRef(self, cookie, result.l, lock); 1584 } else { 1585 artQuickGenericJniEndJNINonRef(self, cookie, lock); 1586 1587 switch (return_shorty_char) { 1588 case 'F': // Fall-through. 1589 case 'D': 1590 return result_f; 1591 case 'Z': 1592 return result.z; 1593 case 'B': 1594 return result.b; 1595 case 'C': 1596 return result.c; 1597 case 'S': 1598 return result.s; 1599 case 'I': 1600 return result.i; 1601 case 'J': 1602 return result.j; 1603 case 'V': 1604 return 0; 1605 default: 1606 LOG(FATAL) << "Unexpected return shorty character " << return_shorty_char; 1607 return 0; 1608 } 1609 } 1610} 1611 1612// The following definitions create return types for two word-sized entities that will be passed 1613// in registers so that memory operations for the interface trampolines can be avoided. The entities 1614// are the resolved method and the pointer to the code to be invoked. 1615// 1616// On x86, ARM32 and MIPS, this is given for a *scalar* 64bit value. The definition thus *must* be 1617// uint64_t or long long int. We use the upper 32b for code, and the lower 32b for the method. 1618// 1619// On x86_64 and ARM64, structs are decomposed for allocation, so we can create a structs of two 1620// size_t-sized values. 1621// 1622// We need two operations: 1623// 1624// 1) A flag value that signals failure. The assembly stubs expect the method part to be "0". 1625// GetFailureValue() will return a value that has method == 0. 1626// 1627// 2) A value that combines a code pointer and a method pointer. 1628// GetSuccessValue() constructs this. 1629 1630#if defined(__i386__) || defined(__arm__) || defined(__mips__) 1631typedef uint64_t MethodAndCode; 1632 1633// Encodes method_ptr==nullptr and code_ptr==nullptr 1634static constexpr MethodAndCode GetFailureValue() { 1635 return 0; 1636} 1637 1638// Use the lower 32b for the method pointer and the upper 32b for the code pointer. 1639static MethodAndCode GetSuccessValue(const void* code, mirror::ArtMethod* method) { 1640 uint32_t method_uint = reinterpret_cast<uint32_t>(method); 1641 uint64_t code_uint = reinterpret_cast<uint32_t>(code); 1642 return ((code_uint << 32) | method_uint); 1643} 1644 1645#elif defined(__x86_64__) || defined(__aarch64__) 1646struct MethodAndCode { 1647 uintptr_t method; 1648 uintptr_t code; 1649}; 1650 1651// Encodes method_ptr==nullptr. Leaves random value in code pointer. 1652static MethodAndCode GetFailureValue() { 1653 MethodAndCode ret; 1654 ret.method = 0; 1655 return ret; 1656} 1657 1658// Write values into their respective members. 1659static MethodAndCode GetSuccessValue(const void* code, mirror::ArtMethod* method) { 1660 MethodAndCode ret; 1661 ret.method = reinterpret_cast<uintptr_t>(method); 1662 ret.code = reinterpret_cast<uintptr_t>(code); 1663 return ret; 1664} 1665#else 1666#error "Unsupported architecture" 1667#endif 1668 1669template<InvokeType type, bool access_check> 1670static MethodAndCode artInvokeCommon(uint32_t method_idx, mirror::Object* this_object, 1671 mirror::ArtMethod* caller_method, 1672 Thread* self, mirror::ArtMethod** sp); 1673 1674template<InvokeType type, bool access_check> 1675static MethodAndCode artInvokeCommon(uint32_t method_idx, mirror::Object* this_object, 1676 mirror::ArtMethod* caller_method, 1677 Thread* self, mirror::ArtMethod** sp) { 1678 mirror::ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check, 1679 type); 1680 if (UNLIKELY(method == nullptr)) { 1681 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs); 1682 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile(); 1683 uint32_t shorty_len; 1684 const char* shorty = 1685 dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len); 1686 { 1687 // Remember the args in case a GC happens in FindMethodFromCode. 1688 ScopedObjectAccessUnchecked soa(self->GetJniEnv()); 1689 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa); 1690 visitor.VisitArguments(); 1691 method = FindMethodFromCode<type, access_check>(method_idx, &this_object, &caller_method, 1692 self); 1693 visitor.FixupReferences(); 1694 } 1695 1696 if (UNLIKELY(method == NULL)) { 1697 CHECK(self->IsExceptionPending()); 1698 return GetFailureValue(); // Failure. 1699 } 1700 } 1701 DCHECK(!self->IsExceptionPending()); 1702 const void* code = method->GetEntryPointFromQuickCompiledCode(); 1703 1704 // When we return, the caller will branch to this address, so it had better not be 0! 1705 DCHECK(code != nullptr) << "Code was NULL in method: " << PrettyMethod(method) << " location: " 1706 << MethodHelper(method).GetDexFile().GetLocation(); 1707 1708 return GetSuccessValue(code, method); 1709} 1710 1711// Explicit artInvokeCommon template function declarations to please analysis tool. 1712#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \ 1713 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \ 1714 MethodAndCode artInvokeCommon<type, access_check>(uint32_t method_idx, \ 1715 mirror::Object* this_object, \ 1716 mirror::ArtMethod* caller_method, \ 1717 Thread* self, mirror::ArtMethod** sp) \ 1718 1719EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false); 1720EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true); 1721EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false); 1722EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true); 1723EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false); 1724EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true); 1725EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false); 1726EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true); 1727EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false); 1728EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true); 1729#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL 1730 1731 1732// See comments in runtime_support_asm.S 1733extern "C" MethodAndCode artInvokeInterfaceTrampolineWithAccessCheck(uint32_t method_idx, 1734 mirror::Object* this_object, 1735 mirror::ArtMethod* caller_method, 1736 Thread* self, 1737 mirror::ArtMethod** sp) 1738 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1739 return artInvokeCommon<kInterface, true>(method_idx, this_object, caller_method, self, sp); 1740} 1741 1742 1743extern "C" MethodAndCode artInvokeDirectTrampolineWithAccessCheck(uint32_t method_idx, 1744 mirror::Object* this_object, 1745 mirror::ArtMethod* caller_method, 1746 Thread* self, 1747 mirror::ArtMethod** sp) 1748 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1749 return artInvokeCommon<kDirect, true>(method_idx, this_object, caller_method, self, sp); 1750} 1751 1752extern "C" MethodAndCode artInvokeStaticTrampolineWithAccessCheck(uint32_t method_idx, 1753 mirror::Object* this_object, 1754 mirror::ArtMethod* caller_method, 1755 Thread* self, 1756 mirror::ArtMethod** sp) 1757 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1758 return artInvokeCommon<kStatic, true>(method_idx, this_object, caller_method, self, sp); 1759} 1760 1761extern "C" MethodAndCode artInvokeSuperTrampolineWithAccessCheck(uint32_t method_idx, 1762 mirror::Object* this_object, 1763 mirror::ArtMethod* caller_method, 1764 Thread* self, 1765 mirror::ArtMethod** sp) 1766 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1767 return artInvokeCommon<kSuper, true>(method_idx, this_object, caller_method, self, sp); 1768} 1769 1770extern "C" MethodAndCode artInvokeVirtualTrampolineWithAccessCheck(uint32_t method_idx, 1771 mirror::Object* this_object, 1772 mirror::ArtMethod* caller_method, 1773 Thread* self, 1774 mirror::ArtMethod** sp) 1775 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1776 return artInvokeCommon<kVirtual, true>(method_idx, this_object, caller_method, self, sp); 1777} 1778 1779// Determine target of interface dispatch. This object is known non-null. 1780extern "C" MethodAndCode artInvokeInterfaceTrampoline(mirror::ArtMethod* interface_method, 1781 mirror::Object* this_object, 1782 mirror::ArtMethod* caller_method, 1783 Thread* self, mirror::ArtMethod** sp) 1784 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1785 mirror::ArtMethod* method; 1786 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) { 1787 method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method); 1788 if (UNLIKELY(method == NULL)) { 1789 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs); 1790 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(interface_method, this_object, 1791 caller_method); 1792 return GetFailureValue(); // Failure. 1793 } 1794 } else { 1795 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs); 1796 DCHECK(interface_method == Runtime::Current()->GetResolutionMethod()); 1797 // Determine method index from calling dex instruction. 1798#if defined(__arm__) 1799 // On entry the stack pointed by sp is: 1800 // | argN | | 1801 // | ... | | 1802 // | arg4 | | 1803 // | arg3 spill | | Caller's frame 1804 // | arg2 spill | | 1805 // | arg1 spill | | 1806 // | Method* | --- 1807 // | LR | 1808 // | ... | callee saves 1809 // | R3 | arg3 1810 // | R2 | arg2 1811 // | R1 | arg1 1812 // | R0 | 1813 // | Method* | <- sp 1814 DCHECK_EQ(48U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes()); 1815 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp) + kPointerSize); 1816 uintptr_t caller_pc = regs[10]; 1817#elif defined(__i386__) 1818 // On entry the stack pointed by sp is: 1819 // | argN | | 1820 // | ... | | 1821 // | arg4 | | 1822 // | arg3 spill | | Caller's frame 1823 // | arg2 spill | | 1824 // | arg1 spill | | 1825 // | Method* | --- 1826 // | Return | 1827 // | EBP,ESI,EDI | callee saves 1828 // | EBX | arg3 1829 // | EDX | arg2 1830 // | ECX | arg1 1831 // | EAX/Method* | <- sp 1832 DCHECK_EQ(32U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes()); 1833 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp)); 1834 uintptr_t caller_pc = regs[7]; 1835#elif defined(__mips__) 1836 // On entry the stack pointed by sp is: 1837 // | argN | | 1838 // | ... | | 1839 // | arg4 | | 1840 // | arg3 spill | | Caller's frame 1841 // | arg2 spill | | 1842 // | arg1 spill | | 1843 // | Method* | --- 1844 // | RA | 1845 // | ... | callee saves 1846 // | A3 | arg3 1847 // | A2 | arg2 1848 // | A1 | arg1 1849 // | A0/Method* | <- sp 1850 DCHECK_EQ(64U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes()); 1851 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp)); 1852 uintptr_t caller_pc = regs[15]; 1853#else 1854 UNIMPLEMENTED(FATAL); 1855 uintptr_t caller_pc = 0; 1856#endif 1857 uint32_t dex_pc = caller_method->ToDexPc(caller_pc); 1858 const DexFile::CodeItem* code = MethodHelper(caller_method).GetCodeItem(); 1859 CHECK_LT(dex_pc, code->insns_size_in_code_units_); 1860 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]); 1861 Instruction::Code instr_code = instr->Opcode(); 1862 CHECK(instr_code == Instruction::INVOKE_INTERFACE || 1863 instr_code == Instruction::INVOKE_INTERFACE_RANGE) 1864 << "Unexpected call into interface trampoline: " << instr->DumpString(NULL); 1865 uint32_t dex_method_idx; 1866 if (instr_code == Instruction::INVOKE_INTERFACE) { 1867 dex_method_idx = instr->VRegB_35c(); 1868 } else { 1869 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE); 1870 dex_method_idx = instr->VRegB_3rc(); 1871 } 1872 1873 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile(); 1874 uint32_t shorty_len; 1875 const char* shorty = 1876 dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx), &shorty_len); 1877 { 1878 // Remember the args in case a GC happens in FindMethodFromCode. 1879 ScopedObjectAccessUnchecked soa(self->GetJniEnv()); 1880 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa); 1881 visitor.VisitArguments(); 1882 method = FindMethodFromCode<kInterface, false>(dex_method_idx, &this_object, &caller_method, 1883 self); 1884 visitor.FixupReferences(); 1885 } 1886 1887 if (UNLIKELY(method == nullptr)) { 1888 CHECK(self->IsExceptionPending()); 1889 return GetFailureValue(); // Failure. 1890 } 1891 } 1892 const void* code = method->GetEntryPointFromQuickCompiledCode(); 1893 1894 // When we return, the caller will branch to this address, so it had better not be 0! 1895 DCHECK(code != nullptr) << "Code was NULL in method: " << PrettyMethod(method) << " location: " 1896 << MethodHelper(method).GetDexFile().GetLocation(); 1897 1898 return GetSuccessValue(code, method); 1899} 1900 1901} // namespace art 1902