quick_trampoline_entrypoints.cc revision 42fcd9838a87abaf7a2ef86853a5287f86dbe391
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 SirtRef<mirror::Class> sirt_c(self, method->GetDeclaringClass()); 493 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(sirt_c, true, true)) { 494 DCHECK(Thread::Current()->IsExceptionPending()) << PrettyMethod(method); 495 self->PopManagedStackFragment(fragment); 496 return 0; 497 } 498 } 499 500 JValue result = interpreter::EnterInterpreterFromStub(self, mh, code_item, *shadow_frame); 501 // Pop transition. 502 self->PopManagedStackFragment(fragment); 503 // No need to restore the args since the method has already been run by the interpreter. 504 return result.GetJ(); 505 } 506} 507 508// Visits arguments on the stack placing them into the args vector, Object* arguments are converted 509// to jobjects. 510class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor { 511 public: 512 BuildQuickArgumentVisitor(mirror::ArtMethod** sp, bool is_static, const char* shorty, 513 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa, 514 std::vector<jvalue>* args) : 515 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {} 516 517 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE; 518 519 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 520 521 private: 522 ScopedObjectAccessUnchecked* const soa_; 523 std::vector<jvalue>* const args_; 524 // References which we must update when exiting in case the GC moved the objects. 525 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_; 526 527 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor); 528}; 529 530void BuildQuickArgumentVisitor::Visit() { 531 jvalue val; 532 Primitive::Type type = GetParamPrimitiveType(); 533 switch (type) { 534 case Primitive::kPrimNot: { 535 StackReference<mirror::Object>* stack_ref = 536 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress()); 537 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr()); 538 references_.push_back(std::make_pair(val.l, stack_ref)); 539 break; 540 } 541 case Primitive::kPrimLong: // Fall-through. 542 case Primitive::kPrimDouble: 543 if (IsSplitLongOrDouble()) { 544 val.j = ReadSplitLongParam(); 545 } else { 546 val.j = *reinterpret_cast<jlong*>(GetParamAddress()); 547 } 548 break; 549 case Primitive::kPrimBoolean: // Fall-through. 550 case Primitive::kPrimByte: // Fall-through. 551 case Primitive::kPrimChar: // Fall-through. 552 case Primitive::kPrimShort: // Fall-through. 553 case Primitive::kPrimInt: // Fall-through. 554 case Primitive::kPrimFloat: 555 val.i = *reinterpret_cast<jint*>(GetParamAddress()); 556 break; 557 case Primitive::kPrimVoid: 558 LOG(FATAL) << "UNREACHABLE"; 559 val.j = 0; 560 break; 561 } 562 args_->push_back(val); 563} 564 565void BuildQuickArgumentVisitor::FixupReferences() { 566 // Fixup any references which may have changed. 567 for (const auto& pair : references_) { 568 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first)); 569 soa_->Env()->DeleteLocalRef(pair.first); 570 } 571} 572 573// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method 574// which is responsible for recording callee save registers. We explicitly place into jobjects the 575// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a 576// field within the proxy object, which will box the primitive arguments and deal with error cases. 577extern "C" uint64_t artQuickProxyInvokeHandler(mirror::ArtMethod* proxy_method, 578 mirror::Object* receiver, 579 Thread* self, mirror::ArtMethod** sp) 580 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 581 DCHECK(proxy_method->IsProxyMethod()) << PrettyMethod(proxy_method); 582 DCHECK(receiver->GetClass()->IsProxyClass()) << PrettyMethod(proxy_method); 583 // Ensure we don't get thread suspension until the object arguments are safely in jobjects. 584 const char* old_cause = 585 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments"); 586 // Register the top of the managed stack, making stack crawlable. 587 DCHECK_EQ(*sp, proxy_method) << PrettyMethod(proxy_method); 588 self->SetTopOfStack(sp, 0); 589 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(), 590 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes()) 591 << PrettyMethod(proxy_method); 592 self->VerifyStack(); 593 // Start new JNI local reference state. 594 JNIEnvExt* env = self->GetJniEnv(); 595 ScopedObjectAccessUnchecked soa(env); 596 ScopedJniEnvLocalRefState env_state(env); 597 // Create local ref. copies of proxy method and the receiver. 598 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver); 599 600 // Placing arguments into args vector and remove the receiver. 601 MethodHelper proxy_mh(proxy_method); 602 DCHECK(!proxy_mh.IsStatic()) << PrettyMethod(proxy_method); 603 std::vector<jvalue> args; 604 BuildQuickArgumentVisitor local_ref_visitor(sp, proxy_mh.IsStatic(), proxy_mh.GetShorty(), 605 proxy_mh.GetShortyLength(), &soa, &args); 606 607 local_ref_visitor.VisitArguments(); 608 DCHECK_GT(args.size(), 0U) << PrettyMethod(proxy_method); 609 args.erase(args.begin()); 610 611 // Convert proxy method into expected interface method. 612 mirror::ArtMethod* interface_method = proxy_method->FindOverriddenMethod(); 613 DCHECK(interface_method != NULL) << PrettyMethod(proxy_method); 614 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method); 615 jobject interface_method_jobj = soa.AddLocalReference<jobject>(interface_method); 616 617 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code 618 // that performs allocations. 619 self->EndAssertNoThreadSuspension(old_cause); 620 JValue result = InvokeProxyInvocationHandler(soa, proxy_mh.GetShorty(), 621 rcvr_jobj, interface_method_jobj, args); 622 // Restore references which might have moved. 623 local_ref_visitor.FixupReferences(); 624 return result.GetJ(); 625} 626 627// Read object references held in arguments from quick frames and place in a JNI local references, 628// so they don't get garbage collected. 629class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor { 630 public: 631 RememberForGcArgumentVisitor(mirror::ArtMethod** sp, bool is_static, const char* shorty, 632 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) : 633 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {} 634 635 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE; 636 637 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 638 639 private: 640 ScopedObjectAccessUnchecked* const soa_; 641 // References which we must update when exiting in case the GC moved the objects. 642 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_; 643 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor); 644}; 645 646void RememberForGcArgumentVisitor::Visit() { 647 if (IsParamAReference()) { 648 StackReference<mirror::Object>* stack_ref = 649 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress()); 650 jobject reference = 651 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr()); 652 references_.push_back(std::make_pair(reference, stack_ref)); 653 } 654} 655 656void RememberForGcArgumentVisitor::FixupReferences() { 657 // Fixup any references which may have changed. 658 for (const auto& pair : references_) { 659 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first)); 660 soa_->Env()->DeleteLocalRef(pair.first); 661 } 662} 663 664 665// Lazily resolve a method for quick. Called by stub code. 666extern "C" const void* artQuickResolutionTrampoline(mirror::ArtMethod* called, 667 mirror::Object* receiver, 668 Thread* self, mirror::ArtMethod** sp) 669 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 670 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs); 671 // Start new JNI local reference state 672 JNIEnvExt* env = self->GetJniEnv(); 673 ScopedObjectAccessUnchecked soa(env); 674 ScopedJniEnvLocalRefState env_state(env); 675 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up"); 676 677 // Compute details about the called method (avoid GCs) 678 ClassLinker* linker = Runtime::Current()->GetClassLinker(); 679 mirror::ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp); 680 InvokeType invoke_type; 681 const DexFile* dex_file; 682 uint32_t dex_method_idx; 683 if (called->IsRuntimeMethod()) { 684 uint32_t dex_pc = caller->ToDexPc(QuickArgumentVisitor::GetCallingPc(sp)); 685 const DexFile::CodeItem* code; 686 { 687 MethodHelper mh(caller); 688 dex_file = &mh.GetDexFile(); 689 code = mh.GetCodeItem(); 690 } 691 CHECK_LT(dex_pc, code->insns_size_in_code_units_); 692 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]); 693 Instruction::Code instr_code = instr->Opcode(); 694 bool is_range; 695 switch (instr_code) { 696 case Instruction::INVOKE_DIRECT: 697 invoke_type = kDirect; 698 is_range = false; 699 break; 700 case Instruction::INVOKE_DIRECT_RANGE: 701 invoke_type = kDirect; 702 is_range = true; 703 break; 704 case Instruction::INVOKE_STATIC: 705 invoke_type = kStatic; 706 is_range = false; 707 break; 708 case Instruction::INVOKE_STATIC_RANGE: 709 invoke_type = kStatic; 710 is_range = true; 711 break; 712 case Instruction::INVOKE_SUPER: 713 invoke_type = kSuper; 714 is_range = false; 715 break; 716 case Instruction::INVOKE_SUPER_RANGE: 717 invoke_type = kSuper; 718 is_range = true; 719 break; 720 case Instruction::INVOKE_VIRTUAL: 721 invoke_type = kVirtual; 722 is_range = false; 723 break; 724 case Instruction::INVOKE_VIRTUAL_RANGE: 725 invoke_type = kVirtual; 726 is_range = true; 727 break; 728 case Instruction::INVOKE_INTERFACE: 729 invoke_type = kInterface; 730 is_range = false; 731 break; 732 case Instruction::INVOKE_INTERFACE_RANGE: 733 invoke_type = kInterface; 734 is_range = true; 735 break; 736 default: 737 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(NULL); 738 // Avoid used uninitialized warnings. 739 invoke_type = kDirect; 740 is_range = false; 741 } 742 dex_method_idx = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c(); 743 744 } else { 745 invoke_type = kStatic; 746 dex_file = &MethodHelper(called).GetDexFile(); 747 dex_method_idx = called->GetDexMethodIndex(); 748 } 749 uint32_t shorty_len; 750 const char* shorty = 751 dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx), &shorty_len); 752 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa); 753 visitor.VisitArguments(); 754 self->EndAssertNoThreadSuspension(old_cause); 755 bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface; 756 // Resolve method filling in dex cache. 757 if (called->IsRuntimeMethod()) { 758 SirtRef<mirror::Object> sirt_receiver(soa.Self(), virtual_or_interface ? receiver : nullptr); 759 called = linker->ResolveMethod(dex_method_idx, caller, invoke_type); 760 receiver = sirt_receiver.get(); 761 } 762 const void* code = NULL; 763 if (LIKELY(!self->IsExceptionPending())) { 764 // Incompatible class change should have been handled in resolve method. 765 CHECK(!called->CheckIncompatibleClassChange(invoke_type)) 766 << PrettyMethod(called) << " " << invoke_type; 767 if (virtual_or_interface) { 768 // Refine called method based on receiver. 769 CHECK(receiver != nullptr) << invoke_type; 770 if (invoke_type == kVirtual) { 771 called = receiver->GetClass()->FindVirtualMethodForVirtual(called); 772 } else { 773 called = receiver->GetClass()->FindVirtualMethodForInterface(called); 774 } 775 // We came here because of sharpening. Ensure the dex cache is up-to-date on the method index 776 // of the sharpened method. 777 if (called->GetDexCacheResolvedMethods() == caller->GetDexCacheResolvedMethods()) { 778 caller->GetDexCacheResolvedMethods()->Set<false>(called->GetDexMethodIndex(), called); 779 } else { 780 // Calling from one dex file to another, need to compute the method index appropriate to 781 // the caller's dex file. Since we get here only if the original called was a runtime 782 // method, we've got the correct dex_file and a dex_method_idx from above. 783 DCHECK(&MethodHelper(caller).GetDexFile() == dex_file); 784 uint32_t method_index = 785 MethodHelper(called).FindDexMethodIndexInOtherDexFile(*dex_file, dex_method_idx); 786 if (method_index != DexFile::kDexNoIndex) { 787 caller->GetDexCacheResolvedMethods()->Set<false>(method_index, called); 788 } 789 } 790 } 791 // Ensure that the called method's class is initialized. 792 SirtRef<mirror::Class> called_class(soa.Self(), called->GetDeclaringClass()); 793 linker->EnsureInitialized(called_class, true, true); 794 if (LIKELY(called_class->IsInitialized())) { 795 code = called->GetEntryPointFromQuickCompiledCode(); 796 } else if (called_class->IsInitializing()) { 797 if (invoke_type == kStatic) { 798 // Class is still initializing, go to oat and grab code (trampoline must be left in place 799 // until class is initialized to stop races between threads). 800 code = linker->GetQuickOatCodeFor(called); 801 } else { 802 // No trampoline for non-static methods. 803 code = called->GetEntryPointFromQuickCompiledCode(); 804 } 805 } else { 806 DCHECK(called_class->IsErroneous()); 807 } 808 } 809 CHECK_EQ(code == NULL, self->IsExceptionPending()); 810 // Fixup any locally saved objects may have moved during a GC. 811 visitor.FixupReferences(); 812 // Place called method in callee-save frame to be placed as first argument to quick method. 813 *sp = called; 814 return code; 815} 816 817 818 819/* 820 * This class uses a couple of observations to unite the different calling conventions through 821 * a few constants. 822 * 823 * 1) Number of registers used for passing is normally even, so counting down has no penalty for 824 * possible alignment. 825 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point 826 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote 827 * when we have to split things 828 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats 829 * and we can use Int handling directly. 830 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code 831 * necessary when widening. Also, widening of Ints will take place implicitly, and the 832 * extension should be compatible with Aarch64, which mandates copying the available bits 833 * into LSB and leaving the rest unspecified. 834 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on 835 * the stack. 836 * 6) There is only little endian. 837 * 838 * 839 * Actual work is supposed to be done in a delegate of the template type. The interface is as 840 * follows: 841 * 842 * void PushGpr(uintptr_t): Add a value for the next GPR 843 * 844 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need 845 * padding, that is, think the architecture is 32b and aligns 64b. 846 * 847 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to 848 * split this if necessary. The current state will have aligned, if 849 * necessary. 850 * 851 * void PushStack(uintptr_t): Push a value to the stack. 852 * 853 * uintptr_t PushSirt(mirror::Object* ref): Add a reference to the Sirt. This _will_ have nullptr, 854 * as this might be important for null initialization. 855 * Must return the jobject, that is, the reference to the 856 * entry in the Sirt (nullptr if necessary). 857 * 858 */ 859template <class T> class BuildGenericJniFrameStateMachine { 860 public: 861#if defined(__arm__) 862 // TODO: These are all dummy values! 863 static constexpr bool kNativeSoftFloatAbi = true; 864 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3 865 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs. 866 867 static constexpr size_t kRegistersNeededForLong = 2; 868 static constexpr size_t kRegistersNeededForDouble = 2; 869 static constexpr bool kMultiRegistersAligned = true; 870 static constexpr bool kMultiRegistersWidened = false; 871 static constexpr bool kAlignLongOnStack = true; 872 static constexpr bool kAlignDoubleOnStack = true; 873#elif defined(__aarch64__) 874 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI. 875 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs. 876 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs. 877 878 static constexpr size_t kRegistersNeededForLong = 1; 879 static constexpr size_t kRegistersNeededForDouble = 1; 880 static constexpr bool kMultiRegistersAligned = false; 881 static constexpr bool kMultiRegistersWidened = false; 882 static constexpr bool kAlignLongOnStack = false; 883 static constexpr bool kAlignDoubleOnStack = false; 884#elif defined(__mips__) 885 // TODO: These are all dummy values! 886 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI. 887 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs. 888 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs. 889 890 static constexpr size_t kRegistersNeededForLong = 2; 891 static constexpr size_t kRegistersNeededForDouble = 2; 892 static constexpr bool kMultiRegistersAligned = true; 893 static constexpr bool kMultiRegistersWidened = true; 894 static constexpr bool kAlignLongOnStack = false; 895 static constexpr bool kAlignDoubleOnStack = false; 896#elif defined(__i386__) 897 // TODO: Check these! 898 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp 899 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs. 900 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs. 901 902 static constexpr size_t kRegistersNeededForLong = 2; 903 static constexpr size_t kRegistersNeededForDouble = 2; 904 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways 905 static constexpr bool kMultiRegistersWidened = false; 906 static constexpr bool kAlignLongOnStack = false; 907 static constexpr bool kAlignDoubleOnStack = false; 908#elif defined(__x86_64__) 909 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI. 910 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs. 911 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs. 912 913 static constexpr size_t kRegistersNeededForLong = 1; 914 static constexpr size_t kRegistersNeededForDouble = 1; 915 static constexpr bool kMultiRegistersAligned = false; 916 static constexpr bool kMultiRegistersWidened = false; 917 static constexpr bool kAlignLongOnStack = false; 918 static constexpr bool kAlignDoubleOnStack = false; 919#else 920#error "Unsupported architecture" 921#endif 922 923 public: 924 explicit BuildGenericJniFrameStateMachine(T* delegate) : gpr_index_(kNumNativeGprArgs), 925 fpr_index_(kNumNativeFprArgs), 926 stack_entries_(0), 927 delegate_(delegate) { 928 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff 929 // the next register is even; counting down is just to make the compiler happy... 930 CHECK_EQ(kNumNativeGprArgs % 2, 0U); 931 CHECK_EQ(kNumNativeFprArgs % 2, 0U); 932 } 933 934 virtual ~BuildGenericJniFrameStateMachine() {} 935 936 bool HavePointerGpr() { 937 return gpr_index_ > 0; 938 } 939 940 void AdvancePointer(void* val) { 941 if (HavePointerGpr()) { 942 gpr_index_--; 943 PushGpr(reinterpret_cast<uintptr_t>(val)); 944 } else { 945 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b 946 PushStack(reinterpret_cast<uintptr_t>(val)); 947 gpr_index_ = 0; 948 } 949 } 950 951 952 bool HaveSirtGpr() { 953 return gpr_index_ > 0; 954 } 955 956 void AdvanceSirt(mirror::Object* ptr) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 957 uintptr_t sirtRef = PushSirt(ptr); 958 if (HaveSirtGpr()) { 959 gpr_index_--; 960 PushGpr(sirtRef); 961 } else { 962 stack_entries_++; 963 PushStack(sirtRef); 964 gpr_index_ = 0; 965 } 966 } 967 968 969 bool HaveIntGpr() { 970 return gpr_index_ > 0; 971 } 972 973 void AdvanceInt(uint32_t val) { 974 if (HaveIntGpr()) { 975 gpr_index_--; 976 PushGpr(val); 977 } else { 978 stack_entries_++; 979 PushStack(val); 980 gpr_index_ = 0; 981 } 982 } 983 984 985 bool HaveLongGpr() { 986 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0); 987 } 988 989 bool LongGprNeedsPadding() { 990 return kRegistersNeededForLong > 1 && // only pad when using multiple registers 991 kAlignLongOnStack && // and when it needs alignment 992 (gpr_index_ & 1) == 1; // counter is odd, see constructor 993 } 994 995 bool LongStackNeedsPadding() { 996 return kRegistersNeededForLong > 1 && // only pad when using multiple registers 997 kAlignLongOnStack && // and when it needs 8B alignment 998 (stack_entries_ & 1) == 1; // counter is odd 999 } 1000 1001 void AdvanceLong(uint64_t val) { 1002 if (HaveLongGpr()) { 1003 if (LongGprNeedsPadding()) { 1004 PushGpr(0); 1005 gpr_index_--; 1006 } 1007 if (kRegistersNeededForLong == 1) { 1008 PushGpr(static_cast<uintptr_t>(val)); 1009 } else { 1010 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF)); 1011 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF)); 1012 } 1013 gpr_index_ -= kRegistersNeededForLong; 1014 } else { 1015 if (LongStackNeedsPadding()) { 1016 PushStack(0); 1017 stack_entries_++; 1018 } 1019 if (kRegistersNeededForLong == 1) { 1020 PushStack(static_cast<uintptr_t>(val)); 1021 stack_entries_++; 1022 } else { 1023 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF)); 1024 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF)); 1025 stack_entries_ += 2; 1026 } 1027 gpr_index_ = 0; 1028 } 1029 } 1030 1031 1032 bool HaveFloatFpr() { 1033 return fpr_index_ > 0; 1034 } 1035 1036 template <typename U, typename V> V convert(U in) { 1037 CHECK_LE(sizeof(U), sizeof(V)); 1038 union { U u; V v; } tmp; 1039 tmp.u = in; 1040 return tmp.v; 1041 } 1042 1043 void AdvanceFloat(float val) { 1044 if (kNativeSoftFloatAbi) { 1045 AdvanceInt(convert<float, uint32_t>(val)); 1046 } else { 1047 if (HaveFloatFpr()) { 1048 fpr_index_--; 1049 if (kRegistersNeededForDouble == 1) { 1050 if (kMultiRegistersWidened) { 1051 PushFpr8(convert<double, uint64_t>(val)); 1052 } else { 1053 // No widening, just use the bits. 1054 PushFpr8(convert<float, uint64_t>(val)); 1055 } 1056 } else { 1057 PushFpr4(val); 1058 } 1059 } else { 1060 stack_entries_++; 1061 if (kRegistersNeededForDouble == 1 && kMultiRegistersWidened) { 1062 // Need to widen before storing: Note the "double" in the template instantiation. 1063 PushStack(convert<double, uintptr_t>(val)); 1064 } else { 1065 PushStack(convert<float, uintptr_t>(val)); 1066 } 1067 fpr_index_ = 0; 1068 } 1069 } 1070 } 1071 1072 1073 bool HaveDoubleFpr() { 1074 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0); 1075 } 1076 1077 bool DoubleFprNeedsPadding() { 1078 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers 1079 kAlignDoubleOnStack && // and when it needs alignment 1080 (fpr_index_ & 1) == 1; // counter is odd, see constructor 1081 } 1082 1083 bool DoubleStackNeedsPadding() { 1084 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers 1085 kAlignDoubleOnStack && // and when it needs 8B alignment 1086 (stack_entries_ & 1) == 1; // counter is odd 1087 } 1088 1089 void AdvanceDouble(uint64_t val) { 1090 if (kNativeSoftFloatAbi) { 1091 AdvanceLong(val); 1092 } else { 1093 if (HaveDoubleFpr()) { 1094 if (DoubleFprNeedsPadding()) { 1095 PushFpr4(0); 1096 fpr_index_--; 1097 } 1098 PushFpr8(val); 1099 fpr_index_ -= kRegistersNeededForDouble; 1100 } else { 1101 if (DoubleStackNeedsPadding()) { 1102 PushStack(0); 1103 stack_entries_++; 1104 } 1105 if (kRegistersNeededForDouble == 1) { 1106 PushStack(static_cast<uintptr_t>(val)); 1107 stack_entries_++; 1108 } else { 1109 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF)); 1110 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF)); 1111 stack_entries_ += 2; 1112 } 1113 fpr_index_ = 0; 1114 } 1115 } 1116 } 1117 1118 uint32_t getStackEntries() { 1119 return stack_entries_; 1120 } 1121 1122 uint32_t getNumberOfUsedGprs() { 1123 return kNumNativeGprArgs - gpr_index_; 1124 } 1125 1126 uint32_t getNumberOfUsedFprs() { 1127 return kNumNativeFprArgs - fpr_index_; 1128 } 1129 1130 private: 1131 void PushGpr(uintptr_t val) { 1132 delegate_->PushGpr(val); 1133 } 1134 void PushFpr4(float val) { 1135 delegate_->PushFpr4(val); 1136 } 1137 void PushFpr8(uint64_t val) { 1138 delegate_->PushFpr8(val); 1139 } 1140 void PushStack(uintptr_t val) { 1141 delegate_->PushStack(val); 1142 } 1143 uintptr_t PushSirt(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1144 return delegate_->PushSirt(ref); 1145 } 1146 1147 uint32_t gpr_index_; // Number of free GPRs 1148 uint32_t fpr_index_; // Number of free FPRs 1149 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not 1150 // extended 1151 T* delegate_; // What Push implementation gets called 1152}; 1153 1154class ComputeGenericJniFrameSize FINAL { 1155 public: 1156 ComputeGenericJniFrameSize() : num_sirt_references_(0), num_stack_entries_(0) {} 1157 1158 uint32_t GetStackSize() { 1159 return num_stack_entries_ * sizeof(uintptr_t); 1160 } 1161 1162 // WARNING: After this, *sp won't be pointing to the method anymore! 1163 void ComputeLayout(mirror::ArtMethod*** m, bool is_static, const char* shorty, uint32_t shorty_len, 1164 void* sp, StackIndirectReferenceTable** table, uint32_t* sirt_entries, 1165 uintptr_t** start_stack, uintptr_t** start_gpr, uint32_t** start_fpr, 1166 void** code_return, size_t* overall_size) 1167 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1168 ComputeAll(is_static, shorty, shorty_len); 1169 1170 mirror::ArtMethod* method = **m; 1171 1172 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp); 1173 1174 // First, fix up the layout of the callee-save frame. 1175 // We have to squeeze in the Sirt, and relocate the method pointer. 1176 1177 // "Free" the slot for the method. 1178 sp8 += kPointerSize; 1179 1180 // Add the Sirt. 1181 *sirt_entries = num_sirt_references_; 1182 size_t sirt_size = StackIndirectReferenceTable::GetAlignedSirtSize(num_sirt_references_); 1183 sp8 -= sirt_size; 1184 *table = reinterpret_cast<StackIndirectReferenceTable*>(sp8); 1185 (*table)->SetNumberOfReferences(num_sirt_references_); 1186 1187 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us. 1188 sp8 -= kPointerSize; 1189 uint8_t* method_pointer = sp8; 1190 *(reinterpret_cast<mirror::ArtMethod**>(method_pointer)) = method; 1191 *m = reinterpret_cast<mirror::ArtMethod**>(method_pointer); 1192 1193 // Reference cookie and padding 1194 sp8 -= 8; 1195 // Store Sirt size 1196 *reinterpret_cast<uint32_t*>(sp8) = static_cast<uint32_t>(sirt_size & 0xFFFFFFFF); 1197 1198 // Next comes the native call stack. 1199 sp8 -= GetStackSize(); 1200 // Now align the call stack below. This aligns by 16, as AArch64 seems to require. 1201 uintptr_t mask = ~0x0F; 1202 sp8 = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(sp8) & mask); 1203 *start_stack = reinterpret_cast<uintptr_t*>(sp8); 1204 1205 // put fprs and gprs below 1206 // Assumption is OK right now, as we have soft-float arm 1207 size_t fregs = BuildGenericJniFrameStateMachine<ComputeGenericJniFrameSize>::kNumNativeFprArgs; 1208 sp8 -= fregs * sizeof(uintptr_t); 1209 *start_fpr = reinterpret_cast<uint32_t*>(sp8); 1210 size_t iregs = BuildGenericJniFrameStateMachine<ComputeGenericJniFrameSize>::kNumNativeGprArgs; 1211 sp8 -= iregs * sizeof(uintptr_t); 1212 *start_gpr = reinterpret_cast<uintptr_t*>(sp8); 1213 1214 // reserve space for the code pointer 1215 sp8 -= kPointerSize; 1216 *code_return = reinterpret_cast<void*>(sp8); 1217 1218 *overall_size = reinterpret_cast<uint8_t*>(sp) - sp8; 1219 1220 // The new SP is stored at the end of the alloca, so it can be immediately popped 1221 sp8 = reinterpret_cast<uint8_t*>(sp) - 5 * KB; 1222 *(reinterpret_cast<uint8_t**>(sp8)) = method_pointer; 1223 } 1224 1225 void ComputeSirtOffset() { } // nothing to do, static right now 1226 1227 void ComputeAll(bool is_static, const char* shorty, uint32_t shorty_len) 1228 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1229 BuildGenericJniFrameStateMachine<ComputeGenericJniFrameSize> sm(this); 1230 1231 // JNIEnv 1232 sm.AdvancePointer(nullptr); 1233 1234 // Class object or this as first argument 1235 sm.AdvanceSirt(reinterpret_cast<mirror::Object*>(0x12345678)); 1236 1237 for (uint32_t i = 1; i < shorty_len; ++i) { 1238 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]); 1239 switch (cur_type_) { 1240 case Primitive::kPrimNot: 1241 sm.AdvanceSirt(reinterpret_cast<mirror::Object*>(0x12345678)); 1242 break; 1243 1244 case Primitive::kPrimBoolean: 1245 case Primitive::kPrimByte: 1246 case Primitive::kPrimChar: 1247 case Primitive::kPrimShort: 1248 case Primitive::kPrimInt: 1249 sm.AdvanceInt(0); 1250 break; 1251 case Primitive::kPrimFloat: 1252 sm.AdvanceFloat(0); 1253 break; 1254 case Primitive::kPrimDouble: 1255 sm.AdvanceDouble(0); 1256 break; 1257 case Primitive::kPrimLong: 1258 sm.AdvanceLong(0); 1259 break; 1260 default: 1261 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty; 1262 } 1263 } 1264 1265 num_stack_entries_ = sm.getStackEntries(); 1266 } 1267 1268 void PushGpr(uintptr_t /* val */) { 1269 // not optimizing registers, yet 1270 } 1271 1272 void PushFpr4(float /* val */) { 1273 // not optimizing registers, yet 1274 } 1275 1276 void PushFpr8(uint64_t /* val */) { 1277 // not optimizing registers, yet 1278 } 1279 1280 void PushStack(uintptr_t /* val */) { 1281 // counting is already done in the superclass 1282 } 1283 1284 uintptr_t PushSirt(mirror::Object* /* ptr */) { 1285 num_sirt_references_++; 1286 return reinterpret_cast<uintptr_t>(nullptr); 1287 } 1288 1289 private: 1290 uint32_t num_sirt_references_; 1291 uint32_t num_stack_entries_; 1292}; 1293 1294// Visits arguments on the stack placing them into a region lower down the stack for the benefit 1295// of transitioning into native code. 1296class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor { 1297 public: 1298 BuildGenericJniFrameVisitor(mirror::ArtMethod*** sp, bool is_static, const char* shorty, 1299 uint32_t shorty_len, Thread* self) : 1300 QuickArgumentVisitor(*sp, is_static, shorty, shorty_len), sm_(this) { 1301 ComputeGenericJniFrameSize fsc; 1302 fsc.ComputeLayout(sp, is_static, shorty, shorty_len, *sp, &sirt_, &sirt_expected_refs_, 1303 &cur_stack_arg_, &cur_gpr_reg_, &cur_fpr_reg_, &code_return_, 1304 &alloca_used_size_); 1305 sirt_number_of_references_ = 0; 1306 cur_sirt_entry_ = reinterpret_cast<StackReference<mirror::Object>*>(GetFirstSirtEntry()); 1307 1308 // jni environment is always first argument 1309 sm_.AdvancePointer(self->GetJniEnv()); 1310 1311 if (is_static) { 1312 sm_.AdvanceSirt((**sp)->GetDeclaringClass()); 1313 } 1314 } 1315 1316 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE; 1317 1318 void FinalizeSirt(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 1319 1320 jobject GetFirstSirtEntry() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1321 return reinterpret_cast<jobject>(sirt_->GetStackReference(0)); 1322 } 1323 1324 void PushGpr(uintptr_t val) { 1325 *cur_gpr_reg_ = val; 1326 cur_gpr_reg_++; 1327 } 1328 1329 void PushFpr4(float val) { 1330 *cur_fpr_reg_ = val; 1331 cur_fpr_reg_++; 1332 } 1333 1334 void PushFpr8(uint64_t val) { 1335 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_); 1336 *tmp = val; 1337 cur_fpr_reg_ += 2; 1338 } 1339 1340 void PushStack(uintptr_t val) { 1341 *cur_stack_arg_ = val; 1342 cur_stack_arg_++; 1343 } 1344 1345 uintptr_t PushSirt(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1346 uintptr_t tmp; 1347 if (ref == nullptr) { 1348 *cur_sirt_entry_ = StackReference<mirror::Object>(); 1349 tmp = reinterpret_cast<uintptr_t>(nullptr); 1350 } else { 1351 *cur_sirt_entry_ = StackReference<mirror::Object>::FromMirrorPtr(ref); 1352 tmp = reinterpret_cast<uintptr_t>(cur_sirt_entry_); 1353 } 1354 cur_sirt_entry_++; 1355 sirt_number_of_references_++; 1356 return tmp; 1357 } 1358 1359 // Size of the part of the alloca that we actually need. 1360 size_t GetAllocaUsedSize() { 1361 return alloca_used_size_; 1362 } 1363 1364 void* GetCodeReturn() { 1365 return code_return_; 1366 } 1367 1368 private: 1369 uint32_t sirt_number_of_references_; 1370 StackReference<mirror::Object>* cur_sirt_entry_; 1371 StackIndirectReferenceTable* sirt_; 1372 uint32_t sirt_expected_refs_; 1373 uintptr_t* cur_gpr_reg_; 1374 uint32_t* cur_fpr_reg_; 1375 uintptr_t* cur_stack_arg_; 1376 // StackReference<mirror::Object>* top_of_sirt_; 1377 void* code_return_; 1378 size_t alloca_used_size_; 1379 1380 BuildGenericJniFrameStateMachine<BuildGenericJniFrameVisitor> sm_; 1381 1382 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor); 1383}; 1384 1385void BuildGenericJniFrameVisitor::Visit() { 1386 Primitive::Type type = GetParamPrimitiveType(); 1387 switch (type) { 1388 case Primitive::kPrimLong: { 1389 jlong long_arg; 1390 if (IsSplitLongOrDouble()) { 1391 long_arg = ReadSplitLongParam(); 1392 } else { 1393 long_arg = *reinterpret_cast<jlong*>(GetParamAddress()); 1394 } 1395 sm_.AdvanceLong(long_arg); 1396 break; 1397 } 1398 case Primitive::kPrimDouble: { 1399 uint64_t double_arg; 1400 if (IsSplitLongOrDouble()) { 1401 // Read into union so that we don't case to a double. 1402 double_arg = ReadSplitLongParam(); 1403 } else { 1404 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress()); 1405 } 1406 sm_.AdvanceDouble(double_arg); 1407 break; 1408 } 1409 case Primitive::kPrimNot: { 1410 StackReference<mirror::Object>* stack_ref = 1411 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress()); 1412 sm_.AdvanceSirt(stack_ref->AsMirrorPtr()); 1413 break; 1414 } 1415 case Primitive::kPrimFloat: 1416 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress())); 1417 break; 1418 case Primitive::kPrimBoolean: // Fall-through. 1419 case Primitive::kPrimByte: // Fall-through. 1420 case Primitive::kPrimChar: // Fall-through. 1421 case Primitive::kPrimShort: // Fall-through. 1422 case Primitive::kPrimInt: // Fall-through. 1423 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress())); 1424 break; 1425 case Primitive::kPrimVoid: 1426 LOG(FATAL) << "UNREACHABLE"; 1427 break; 1428 } 1429} 1430 1431void BuildGenericJniFrameVisitor::FinalizeSirt(Thread* self) { 1432 // Initialize padding entries. 1433 while (sirt_number_of_references_ < sirt_expected_refs_) { 1434 *cur_sirt_entry_ = StackReference<mirror::Object>(); 1435 cur_sirt_entry_++; 1436 sirt_number_of_references_++; 1437 } 1438 sirt_->SetNumberOfReferences(sirt_expected_refs_); 1439 DCHECK_NE(sirt_expected_refs_, 0U); 1440 // Install Sirt. 1441 self->PushSirt(sirt_); 1442} 1443 1444extern "C" void* artFindNativeMethod(); 1445 1446uint64_t artQuickGenericJniEndJNIRef(Thread* self, uint32_t cookie, jobject l, jobject lock) { 1447 if (lock != nullptr) { 1448 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self)); 1449 } else { 1450 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self)); 1451 } 1452} 1453 1454void artQuickGenericJniEndJNINonRef(Thread* self, uint32_t cookie, jobject lock) { 1455 if (lock != nullptr) { 1456 JniMethodEndSynchronized(cookie, lock, self); 1457 } else { 1458 JniMethodEnd(cookie, self); 1459 } 1460} 1461 1462/* 1463 * Initializes an alloca region assumed to be directly below sp for a native call: 1464 * Create a Sirt and call stack and fill a mini stack with values to be pushed to registers. 1465 * The final element on the stack is a pointer to the native code. 1466 * 1467 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it. 1468 * We need to fix this, as the Sirt needs to go into the callee-save frame. 1469 * 1470 * The return of this function denotes: 1471 * 1) How many bytes of the alloca can be released, if the value is non-negative. 1472 * 2) An error, if the value is negative. 1473 */ 1474extern "C" ssize_t artQuickGenericJniTrampoline(Thread* self, mirror::ArtMethod** sp) 1475 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1476 mirror::ArtMethod* called = *sp; 1477 DCHECK(called->IsNative()) << PrettyMethod(called, true); 1478 1479 // run the visitor 1480 MethodHelper mh(called); 1481 1482 BuildGenericJniFrameVisitor visitor(&sp, called->IsStatic(), mh.GetShorty(), mh.GetShortyLength(), 1483 self); 1484 visitor.VisitArguments(); 1485 visitor.FinalizeSirt(self); 1486 1487 // fix up managed-stack things in Thread 1488 self->SetTopOfStack(sp, 0); 1489 1490 self->VerifyStack(); 1491 1492 // Start JNI, save the cookie. 1493 uint32_t cookie; 1494 if (called->IsSynchronized()) { 1495 cookie = JniMethodStartSynchronized(visitor.GetFirstSirtEntry(), self); 1496 if (self->IsExceptionPending()) { 1497 self->PopSirt(); 1498 // A negative value denotes an error. 1499 return -1; 1500 } 1501 } else { 1502 cookie = JniMethodStart(self); 1503 } 1504 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp); 1505 *(sp32 - 1) = cookie; 1506 1507 // Retrieve the stored native code. 1508 const void* nativeCode = called->GetNativeMethod(); 1509 1510 // There are two cases for the content of nativeCode: 1511 // 1) Pointer to the native function. 1512 // 2) Pointer to the trampoline for native code binding. 1513 // In the second case, we need to execute the binding and continue with the actual native function 1514 // pointer. 1515 DCHECK(nativeCode != nullptr); 1516 if (nativeCode == GetJniDlsymLookupStub()) { 1517 nativeCode = artFindNativeMethod(); 1518 1519 if (nativeCode == nullptr) { 1520 DCHECK(self->IsExceptionPending()); // There should be an exception pending now. 1521 1522 // End JNI, as the assembly will move to deliver the exception. 1523 jobject lock = called->IsSynchronized() ? visitor.GetFirstSirtEntry() : nullptr; 1524 if (mh.GetShorty()[0] == 'L') { 1525 artQuickGenericJniEndJNIRef(self, cookie, nullptr, lock); 1526 } else { 1527 artQuickGenericJniEndJNINonRef(self, cookie, lock); 1528 } 1529 1530 return -1; 1531 } 1532 // Note that the native code pointer will be automatically set by artFindNativeMethod(). 1533 } 1534 1535 // Store the native code pointer in the stack at the right location. 1536 uintptr_t* code_pointer = reinterpret_cast<uintptr_t*>(visitor.GetCodeReturn()); 1537 *code_pointer = reinterpret_cast<uintptr_t>(nativeCode); 1538 1539 // 5K reserved, window_size + frame pointer used. 1540 size_t window_size = visitor.GetAllocaUsedSize(); 1541 return (5 * KB) - window_size - kPointerSize; 1542} 1543 1544/* 1545 * Is called after the native JNI code. Responsible for cleanup (SIRT, saved state) and 1546 * unlocking. 1547 */ 1548extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self, mirror::ArtMethod** sp, 1549 jvalue result, uint64_t result_f) 1550 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1551 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp); 1552 mirror::ArtMethod* called = *sp; 1553 uint32_t cookie = *(sp32 - 1); 1554 1555 jobject lock = nullptr; 1556 if (called->IsSynchronized()) { 1557 StackIndirectReferenceTable* table = 1558 reinterpret_cast<StackIndirectReferenceTable*>( 1559 reinterpret_cast<uint8_t*>(sp) + kPointerSize); 1560 lock = reinterpret_cast<jobject>(table->GetStackReference(0)); 1561 } 1562 1563 MethodHelper mh(called); 1564 char return_shorty_char = mh.GetShorty()[0]; 1565 1566 if (return_shorty_char == 'L') { 1567 return artQuickGenericJniEndJNIRef(self, cookie, result.l, lock); 1568 } else { 1569 artQuickGenericJniEndJNINonRef(self, cookie, lock); 1570 1571 switch (return_shorty_char) { 1572 case 'F': // Fall-through. 1573 case 'D': 1574 return result_f; 1575 case 'Z': 1576 return result.z; 1577 case 'B': 1578 return result.b; 1579 case 'C': 1580 return result.c; 1581 case 'S': 1582 return result.s; 1583 case 'I': 1584 return result.i; 1585 case 'J': 1586 return result.j; 1587 case 'V': 1588 return 0; 1589 default: 1590 LOG(FATAL) << "Unexpected return shorty character " << return_shorty_char; 1591 return 0; 1592 } 1593 } 1594} 1595 1596template<InvokeType type, bool access_check> 1597static uint64_t artInvokeCommon(uint32_t method_idx, mirror::Object* this_object, 1598 mirror::ArtMethod* caller_method, 1599 Thread* self, mirror::ArtMethod** sp); 1600 1601template<InvokeType type, bool access_check> 1602static uint64_t artInvokeCommon(uint32_t method_idx, mirror::Object* this_object, 1603 mirror::ArtMethod* caller_method, 1604 Thread* self, mirror::ArtMethod** sp) { 1605 mirror::ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check, 1606 type); 1607 if (UNLIKELY(method == nullptr)) { 1608 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs); 1609 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile(); 1610 uint32_t shorty_len; 1611 const char* shorty = 1612 dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len); 1613 { 1614 // Remember the args in case a GC happens in FindMethodFromCode. 1615 ScopedObjectAccessUnchecked soa(self->GetJniEnv()); 1616 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa); 1617 visitor.VisitArguments(); 1618 method = FindMethodFromCode<type, access_check>(method_idx, this_object, caller_method, self); 1619 visitor.FixupReferences(); 1620 } 1621 1622 if (UNLIKELY(method == NULL)) { 1623 CHECK(self->IsExceptionPending()); 1624 return 0; // failure 1625 } 1626 } 1627 DCHECK(!self->IsExceptionPending()); 1628 const void* code = method->GetEntryPointFromQuickCompiledCode(); 1629 1630 // When we return, the caller will branch to this address, so it had better not be 0! 1631 DCHECK(code != nullptr) << "Code was NULL in method: " << PrettyMethod(method) << " location: " 1632 << MethodHelper(method).GetDexFile().GetLocation(); 1633#ifdef __LP64__ 1634 UNIMPLEMENTED(FATAL); 1635 return 0; 1636#else 1637 uint32_t method_uint = reinterpret_cast<uint32_t>(method); 1638 uint64_t code_uint = reinterpret_cast<uint32_t>(code); 1639 uint64_t result = ((code_uint << 32) | method_uint); 1640 return result; 1641#endif 1642} 1643 1644// Explicit artInvokeCommon template function declarations to please analysis tool. 1645#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \ 1646 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \ 1647 uint64_t artInvokeCommon<type, access_check>(uint32_t method_idx, \ 1648 mirror::Object* this_object, \ 1649 mirror::ArtMethod* caller_method, \ 1650 Thread* self, mirror::ArtMethod** sp) \ 1651 1652EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false); 1653EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true); 1654EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false); 1655EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true); 1656EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false); 1657EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true); 1658EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false); 1659EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true); 1660EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false); 1661EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true); 1662#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL 1663 1664 1665// See comments in runtime_support_asm.S 1666extern "C" uint64_t artInvokeInterfaceTrampolineWithAccessCheck(uint32_t method_idx, 1667 mirror::Object* this_object, 1668 mirror::ArtMethod* caller_method, 1669 Thread* self, 1670 mirror::ArtMethod** sp) 1671 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1672 return artInvokeCommon<kInterface, true>(method_idx, this_object, caller_method, self, sp); 1673} 1674 1675 1676extern "C" uint64_t artInvokeDirectTrampolineWithAccessCheck(uint32_t method_idx, 1677 mirror::Object* this_object, 1678 mirror::ArtMethod* caller_method, 1679 Thread* self, 1680 mirror::ArtMethod** sp) 1681 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1682 return artInvokeCommon<kDirect, true>(method_idx, this_object, caller_method, self, sp); 1683} 1684 1685extern "C" uint64_t artInvokeStaticTrampolineWithAccessCheck(uint32_t method_idx, 1686 mirror::Object* this_object, 1687 mirror::ArtMethod* caller_method, 1688 Thread* self, 1689 mirror::ArtMethod** sp) 1690 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1691 return artInvokeCommon<kStatic, true>(method_idx, this_object, caller_method, self, sp); 1692} 1693 1694extern "C" uint64_t artInvokeSuperTrampolineWithAccessCheck(uint32_t method_idx, 1695 mirror::Object* this_object, 1696 mirror::ArtMethod* caller_method, 1697 Thread* self, 1698 mirror::ArtMethod** sp) 1699 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1700 return artInvokeCommon<kSuper, true>(method_idx, this_object, caller_method, self, sp); 1701} 1702 1703extern "C" uint64_t artInvokeVirtualTrampolineWithAccessCheck(uint32_t method_idx, 1704 mirror::Object* this_object, 1705 mirror::ArtMethod* caller_method, 1706 Thread* self, 1707 mirror::ArtMethod** sp) 1708 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1709 return artInvokeCommon<kVirtual, true>(method_idx, this_object, caller_method, self, sp); 1710} 1711 1712// Determine target of interface dispatch. This object is known non-null. 1713extern "C" uint64_t artInvokeInterfaceTrampoline(mirror::ArtMethod* interface_method, 1714 mirror::Object* this_object, 1715 mirror::ArtMethod* caller_method, 1716 Thread* self, mirror::ArtMethod** sp) 1717 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1718 mirror::ArtMethod* method; 1719 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) { 1720 method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method); 1721 if (UNLIKELY(method == NULL)) { 1722 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs); 1723 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(interface_method, this_object, 1724 caller_method); 1725 return 0; // Failure. 1726 } 1727 } else { 1728 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs); 1729 DCHECK(interface_method == Runtime::Current()->GetResolutionMethod()); 1730 // Determine method index from calling dex instruction. 1731#if defined(__arm__) 1732 // On entry the stack pointed by sp is: 1733 // | argN | | 1734 // | ... | | 1735 // | arg4 | | 1736 // | arg3 spill | | Caller's frame 1737 // | arg2 spill | | 1738 // | arg1 spill | | 1739 // | Method* | --- 1740 // | LR | 1741 // | ... | callee saves 1742 // | R3 | arg3 1743 // | R2 | arg2 1744 // | R1 | arg1 1745 // | R0 | 1746 // | Method* | <- sp 1747 DCHECK_EQ(48U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes()); 1748 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp) + kPointerSize); 1749 uintptr_t caller_pc = regs[10]; 1750#elif defined(__i386__) 1751 // On entry the stack pointed by sp is: 1752 // | argN | | 1753 // | ... | | 1754 // | arg4 | | 1755 // | arg3 spill | | Caller's frame 1756 // | arg2 spill | | 1757 // | arg1 spill | | 1758 // | Method* | --- 1759 // | Return | 1760 // | EBP,ESI,EDI | callee saves 1761 // | EBX | arg3 1762 // | EDX | arg2 1763 // | ECX | arg1 1764 // | EAX/Method* | <- sp 1765 DCHECK_EQ(32U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes()); 1766 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp)); 1767 uintptr_t caller_pc = regs[7]; 1768#elif defined(__mips__) 1769 // On entry the stack pointed by sp is: 1770 // | argN | | 1771 // | ... | | 1772 // | arg4 | | 1773 // | arg3 spill | | Caller's frame 1774 // | arg2 spill | | 1775 // | arg1 spill | | 1776 // | Method* | --- 1777 // | RA | 1778 // | ... | callee saves 1779 // | A3 | arg3 1780 // | A2 | arg2 1781 // | A1 | arg1 1782 // | A0/Method* | <- sp 1783 DCHECK_EQ(64U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes()); 1784 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp)); 1785 uintptr_t caller_pc = regs[15]; 1786#else 1787 UNIMPLEMENTED(FATAL); 1788 uintptr_t caller_pc = 0; 1789#endif 1790 uint32_t dex_pc = caller_method->ToDexPc(caller_pc); 1791 const DexFile::CodeItem* code = MethodHelper(caller_method).GetCodeItem(); 1792 CHECK_LT(dex_pc, code->insns_size_in_code_units_); 1793 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]); 1794 Instruction::Code instr_code = instr->Opcode(); 1795 CHECK(instr_code == Instruction::INVOKE_INTERFACE || 1796 instr_code == Instruction::INVOKE_INTERFACE_RANGE) 1797 << "Unexpected call into interface trampoline: " << instr->DumpString(NULL); 1798 uint32_t dex_method_idx; 1799 if (instr_code == Instruction::INVOKE_INTERFACE) { 1800 dex_method_idx = instr->VRegB_35c(); 1801 } else { 1802 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE); 1803 dex_method_idx = instr->VRegB_3rc(); 1804 } 1805 1806 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile(); 1807 uint32_t shorty_len; 1808 const char* shorty = 1809 dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx), &shorty_len); 1810 { 1811 // Remember the args in case a GC happens in FindMethodFromCode. 1812 ScopedObjectAccessUnchecked soa(self->GetJniEnv()); 1813 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa); 1814 visitor.VisitArguments(); 1815 method = FindMethodFromCode<kInterface, false>(dex_method_idx, this_object, caller_method, 1816 self); 1817 visitor.FixupReferences(); 1818 } 1819 1820 if (UNLIKELY(method == nullptr)) { 1821 CHECK(self->IsExceptionPending()); 1822 return 0; // Failure. 1823 } 1824 } 1825 const void* code = method->GetEntryPointFromQuickCompiledCode(); 1826 1827 // When we return, the caller will branch to this address, so it had better not be 0! 1828 DCHECK(code != nullptr) << "Code was NULL in method: " << PrettyMethod(method) << " location: " 1829 << MethodHelper(method).GetDexFile().GetLocation(); 1830#ifdef __LP64__ 1831 UNIMPLEMENTED(FATAL); 1832 return 0; 1833#else 1834 uint32_t method_uint = reinterpret_cast<uint32_t>(method); 1835 uint64_t code_uint = reinterpret_cast<uint32_t>(code); 1836 uint64_t result = ((code_uint << 32) | method_uint); 1837 return result; 1838#endif 1839} 1840 1841} // namespace art 1842