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