blob: f9486c35061e456cef81d1ed23af557108b67bcf [file] [log] [blame]
Ian Rogers848871b2013-08-05 10:56:33 -07001/*
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"
Dragos Sbirleabd136a22013-08-13 18:07:04 -070018#include "common_throws.h"
Ian Rogers848871b2013-08-05 10:56:33 -070019#include "dex_file-inl.h"
20#include "dex_instruction-inl.h"
Dragos Sbirleabd136a22013-08-13 18:07:04 -070021#include "entrypoints/entrypoint_utils.h"
Ian Rogers83883d72013-10-21 21:07:24 -070022#include "gc/accounting/card_table-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070023#include "interpreter/interpreter.h"
24#include "invoke_arg_array_builder.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070025#include "mirror/art_method-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070026#include "mirror/class-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
Dragos Sbirleabd136a22013-08-13 18:07:04 -070032
33
Ian Rogers848871b2013-08-05 10:56:33 -070034namespace art {
35
36// Visits the arguments as saved to the stack by a Runtime::kRefAndArgs callee save frame.
37class QuickArgumentVisitor {
38 public:
39// Offset to first (not the Method*) argument in a Runtime::kRefAndArgs callee save frame.
40// Size of Runtime::kRefAndArgs callee save frame.
41// Size of Method* and register parameters in out stack arguments.
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 |
57 // | Method* | <- sp
58#define QUICK_CALLEE_SAVE_FRAME__REF_AND_ARGS__R1_OFFSET 8
59#define QUICK_CALLEE_SAVE_FRAME__REF_AND_ARGS__LR_OFFSET 44
60#define QUICK_CALLEE_SAVE_FRAME__REF_AND_ARGS__FRAME_SIZE 48
61#define QUICK_STACK_ARG_SKIP 16
62#elif defined(__mips__)
63 // The callee save frame is pointed to by SP.
64 // | argN | |
65 // | ... | |
66 // | arg4 | |
67 // | arg3 spill | | Caller's frame
68 // | arg2 spill | |
69 // | arg1 spill | |
70 // | Method* | ---
71 // | RA |
72 // | ... | callee saves
73 // | A3 | arg3
74 // | A2 | arg2
75 // | A1 | arg1
76 // | A0/Method* | <- sp
77#define QUICK_CALLEE_SAVE_FRAME__REF_AND_ARGS__R1_OFFSET 4
78#define QUICK_CALLEE_SAVE_FRAME__REF_AND_ARGS__LR_OFFSET 60
79#define QUICK_CALLEE_SAVE_FRAME__REF_AND_ARGS__FRAME_SIZE 64
80#define QUICK_STACK_ARG_SKIP 16
81#elif defined(__i386__)
82 // The callee save frame is pointed to by SP.
83 // | argN | |
84 // | ... | |
85 // | arg4 | |
86 // | arg3 spill | | Caller's frame
87 // | arg2 spill | |
88 // | arg1 spill | |
89 // | Method* | ---
90 // | Return |
91 // | EBP,ESI,EDI | callee saves
92 // | EBX | arg3
93 // | EDX | arg2
94 // | ECX | arg1
95 // | EAX/Method* | <- sp
96#define QUICK_CALLEE_SAVE_FRAME__REF_AND_ARGS__R1_OFFSET 4
97#define QUICK_CALLEE_SAVE_FRAME__REF_AND_ARGS__LR_OFFSET 28
98#define QUICK_CALLEE_SAVE_FRAME__REF_AND_ARGS__FRAME_SIZE 32
99#define QUICK_STACK_ARG_SKIP 16
Ian Rogersef7d42f2014-01-06 12:55:46 -0800100#elif defined(__x86_64__)
101// TODO: implement and check these.
102#define QUICK_CALLEE_SAVE_FRAME__REF_AND_ARGS__R1_OFFSET 8
103#define QUICK_CALLEE_SAVE_FRAME__REF_AND_ARGS__LR_OFFSET 56
104#define QUICK_CALLEE_SAVE_FRAME__REF_AND_ARGS__FRAME_SIZE 64
105#define QUICK_STACK_ARG_SKIP 32
Ian Rogers848871b2013-08-05 10:56:33 -0700106#else
107#error "Unsupported architecture"
108#define QUICK_CALLEE_SAVE_FRAME__REF_AND_ARGS__R1_OFFSET 0
109#define QUICK_CALLEE_SAVE_FRAME__REF_AND_ARGS__LR_OFFSET 0
110#define QUICK_CALLEE_SAVE_FRAME__REF_AND_ARGS__FRAME_SIZE 0
111#define QUICK_STACK_ARG_SKIP 0
112#endif
113
Brian Carlstromea46f952013-07-30 01:26:50 -0700114 static mirror::ArtMethod* GetCallingMethod(mirror::ArtMethod** sp) {
Ian Rogers848871b2013-08-05 10:56:33 -0700115 byte* previous_sp = reinterpret_cast<byte*>(sp) +
116 QUICK_CALLEE_SAVE_FRAME__REF_AND_ARGS__FRAME_SIZE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700117 return *reinterpret_cast<mirror::ArtMethod**>(previous_sp);
Ian Rogers848871b2013-08-05 10:56:33 -0700118 }
119
Brian Carlstromea46f952013-07-30 01:26:50 -0700120 static uintptr_t GetCallingPc(mirror::ArtMethod** sp) {
Ian Rogers848871b2013-08-05 10:56:33 -0700121 byte* lr = reinterpret_cast<byte*>(sp) + QUICK_CALLEE_SAVE_FRAME__REF_AND_ARGS__LR_OFFSET;
122 return *reinterpret_cast<uintptr_t*>(lr);
123 }
124
Brian Carlstromea46f952013-07-30 01:26:50 -0700125 QuickArgumentVisitor(mirror::ArtMethod** sp, bool is_static,
Ian Rogers848871b2013-08-05 10:56:33 -0700126 const char* shorty, uint32_t shorty_len)
127 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) :
128 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
129 args_in_regs_(ComputeArgsInRegs(is_static, shorty, shorty_len)),
130 num_params_((is_static ? 0 : 1) + shorty_len - 1), // +1 for this, -1 for return type
131 reg_args_(reinterpret_cast<byte*>(sp) + QUICK_CALLEE_SAVE_FRAME__REF_AND_ARGS__R1_OFFSET),
132 stack_args_(reinterpret_cast<byte*>(sp) + QUICK_CALLEE_SAVE_FRAME__REF_AND_ARGS__FRAME_SIZE
133 + QUICK_STACK_ARG_SKIP),
134 cur_args_(reg_args_),
135 cur_arg_index_(0),
136 param_index_(0),
137 is_split_long_or_double_(false) {
138 DCHECK_EQ(static_cast<size_t>(QUICK_CALLEE_SAVE_FRAME__REF_AND_ARGS__FRAME_SIZE),
139 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
140 }
141
142 virtual ~QuickArgumentVisitor() {}
143
144 virtual void Visit() = 0;
145
146 Primitive::Type GetParamPrimitiveType() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
147 size_t index = param_index_;
148 if (is_static_) {
149 index++; // 0th argument must skip return value at start of the shorty
150 } else if (index == 0) {
151 return Primitive::kPrimNot;
152 }
153 CHECK_LT(index, shorty_len_);
154 return Primitive::GetType(shorty_[index]);
155 }
156
157 byte* GetParamAddress() const {
158 return cur_args_ + (cur_arg_index_ * kPointerSize);
159 }
160
161 bool IsSplitLongOrDouble() const {
162 return is_split_long_or_double_;
163 }
164
165 bool IsParamAReference() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
166 return GetParamPrimitiveType() == Primitive::kPrimNot;
167 }
168
169 bool IsParamALongOrDouble() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
170 Primitive::Type type = GetParamPrimitiveType();
171 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
172 }
173
174 uint64_t ReadSplitLongParam() const {
175 DCHECK(IsSplitLongOrDouble());
176 uint64_t low_half = *reinterpret_cast<uint32_t*>(GetParamAddress());
177 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args_);
178 return (low_half & 0xffffffffULL) | (high_half << 32);
179 }
180
181 void VisitArguments() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
182 for (cur_arg_index_ = 0; cur_arg_index_ < args_in_regs_ && param_index_ < num_params_; ) {
183 is_split_long_or_double_ = (cur_arg_index_ == 2) && IsParamALongOrDouble();
184 Visit();
185 cur_arg_index_ += (IsParamALongOrDouble() ? 2 : 1);
186 param_index_++;
187 }
188 cur_args_ = stack_args_;
189 cur_arg_index_ = is_split_long_or_double_ ? 1 : 0;
190 is_split_long_or_double_ = false;
191 while (param_index_ < num_params_) {
192 Visit();
193 cur_arg_index_ += (IsParamALongOrDouble() ? 2 : 1);
194 param_index_++;
195 }
196 }
197
198 private:
199 static size_t ComputeArgsInRegs(bool is_static, const char* shorty, uint32_t shorty_len)
200 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
201 size_t args_in_regs = (is_static ? 0 : 1);
202 for (size_t i = 0; i < shorty_len; i++) {
203 char s = shorty[i];
204 if (s == 'J' || s == 'D') {
205 args_in_regs += 2;
206 } else {
207 args_in_regs++;
208 }
209 if (args_in_regs > 3) {
210 args_in_regs = 3;
211 break;
212 }
213 }
214 return args_in_regs;
215 }
216
217 const bool is_static_;
218 const char* const shorty_;
219 const uint32_t shorty_len_;
220 const size_t args_in_regs_;
221 const size_t num_params_;
222 byte* const reg_args_;
223 byte* const stack_args_;
224 byte* cur_args_;
225 size_t cur_arg_index_;
226 size_t param_index_;
227 // Does a 64bit parameter straddle the register and stack arguments?
228 bool is_split_long_or_double_;
229};
230
231// Visits arguments on the stack placing them into the shadow frame.
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700232class BuildQuickShadowFrameVisitor : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700233 public:
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700234 BuildQuickShadowFrameVisitor(mirror::ArtMethod** sp,
235 bool is_static, const char* shorty,
236 uint32_t shorty_len, ShadowFrame& sf, size_t first_arg_reg) :
Ian Rogers848871b2013-08-05 10:56:33 -0700237 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
238
239 virtual void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
240 Primitive::Type type = GetParamPrimitiveType();
241 switch (type) {
242 case Primitive::kPrimLong: // Fall-through.
243 case Primitive::kPrimDouble:
244 if (IsSplitLongOrDouble()) {
245 sf_.SetVRegLong(cur_reg_, ReadSplitLongParam());
246 } else {
247 sf_.SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
248 }
249 ++cur_reg_;
250 break;
251 case Primitive::kPrimNot:
252 sf_.SetVRegReference(cur_reg_, *reinterpret_cast<mirror::Object**>(GetParamAddress()));
253 break;
254 case Primitive::kPrimBoolean: // Fall-through.
255 case Primitive::kPrimByte: // Fall-through.
256 case Primitive::kPrimChar: // Fall-through.
257 case Primitive::kPrimShort: // Fall-through.
258 case Primitive::kPrimInt: // Fall-through.
259 case Primitive::kPrimFloat:
260 sf_.SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
261 break;
262 case Primitive::kPrimVoid:
263 LOG(FATAL) << "UNREACHABLE";
264 break;
265 }
266 ++cur_reg_;
267 }
268
269 private:
270 ShadowFrame& sf_;
271 size_t cur_reg_;
272
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700273 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700274};
275
Brian Carlstromea46f952013-07-30 01:26:50 -0700276extern "C" uint64_t artQuickToInterpreterBridge(mirror::ArtMethod* method, Thread* self,
277 mirror::ArtMethod** sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700278 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
279 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
280 // frame.
281 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
282
283 if (method->IsAbstract()) {
284 ThrowAbstractMethodError(method);
285 return 0;
286 } else {
287 const char* old_cause = self->StartAssertNoThreadSuspension("Building interpreter shadow frame");
288 MethodHelper mh(method);
289 const DexFile::CodeItem* code_item = mh.GetCodeItem();
290 uint16_t num_regs = code_item->registers_size_;
291 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
292 ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, NULL, // No last shadow coming from quick.
293 method, 0, memory));
294 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700295 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, mh.IsStatic(), mh.GetShorty(),
Ian Rogers848871b2013-08-05 10:56:33 -0700296 mh.GetShortyLength(),
297 *shadow_frame, first_arg_reg);
298 shadow_frame_builder.VisitArguments();
299 // Push a transition back into managed code onto the linked list in thread.
300 ManagedStack fragment;
301 self->PushManagedStackFragment(&fragment);
302 self->PushShadowFrame(shadow_frame);
303 self->EndAssertNoThreadSuspension(old_cause);
304
305 if (method->IsStatic() && !method->GetDeclaringClass()->IsInitializing()) {
306 // Ensure static method's class is initialized.
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800307 SirtRef<mirror::Class> sirt_c(self, method->GetDeclaringClass());
308 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(sirt_c, true, true)) {
Ian Rogers848871b2013-08-05 10:56:33 -0700309 DCHECK(Thread::Current()->IsExceptionPending());
310 self->PopManagedStackFragment(fragment);
311 return 0;
312 }
313 }
314
315 JValue result = interpreter::EnterInterpreterFromStub(self, mh, code_item, *shadow_frame);
316 // Pop transition.
317 self->PopManagedStackFragment(fragment);
318 return result.GetJ();
319 }
320}
321
322// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
323// to jobjects.
324class BuildQuickArgumentVisitor : public QuickArgumentVisitor {
325 public:
Brian Carlstromea46f952013-07-30 01:26:50 -0700326 BuildQuickArgumentVisitor(mirror::ArtMethod** sp, bool is_static, const char* shorty,
Ian Rogers848871b2013-08-05 10:56:33 -0700327 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa,
328 std::vector<jvalue>* args) :
329 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
330
331 virtual void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
332 jvalue val;
333 Primitive::Type type = GetParamPrimitiveType();
334 switch (type) {
335 case Primitive::kPrimNot: {
336 mirror::Object* obj = *reinterpret_cast<mirror::Object**>(GetParamAddress());
337 val.l = soa_->AddLocalReference<jobject>(obj);
338 break;
339 }
340 case Primitive::kPrimLong: // Fall-through.
341 case Primitive::kPrimDouble:
342 if (IsSplitLongOrDouble()) {
343 val.j = ReadSplitLongParam();
344 } else {
345 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
346 }
347 break;
348 case Primitive::kPrimBoolean: // Fall-through.
349 case Primitive::kPrimByte: // Fall-through.
350 case Primitive::kPrimChar: // Fall-through.
351 case Primitive::kPrimShort: // Fall-through.
352 case Primitive::kPrimInt: // Fall-through.
353 case Primitive::kPrimFloat:
354 val.i = *reinterpret_cast<jint*>(GetParamAddress());
355 break;
356 case Primitive::kPrimVoid:
357 LOG(FATAL) << "UNREACHABLE";
358 val.j = 0;
359 break;
360 }
361 args_->push_back(val);
362 }
363
364 private:
365 ScopedObjectAccessUnchecked* soa_;
366 std::vector<jvalue>* args_;
367
368 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
369};
370
371// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
372// which is responsible for recording callee save registers. We explicitly place into jobjects the
373// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
374// field within the proxy object, which will box the primitive arguments and deal with error cases.
Brian Carlstromea46f952013-07-30 01:26:50 -0700375extern "C" uint64_t artQuickProxyInvokeHandler(mirror::ArtMethod* proxy_method,
Ian Rogers848871b2013-08-05 10:56:33 -0700376 mirror::Object* receiver,
Brian Carlstromea46f952013-07-30 01:26:50 -0700377 Thread* self, mirror::ArtMethod** sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700378 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromd3633d52013-08-20 21:06:26 -0700379 DCHECK(proxy_method->IsProxyMethod()) << PrettyMethod(proxy_method);
380 DCHECK(receiver->GetClass()->IsProxyClass()) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700381 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
382 const char* old_cause =
383 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
384 // Register the top of the managed stack, making stack crawlable.
Brian Carlstromd3633d52013-08-20 21:06:26 -0700385 DCHECK_EQ(*sp, proxy_method) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700386 self->SetTopOfStack(sp, 0);
387 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(),
Brian Carlstromd3633d52013-08-20 21:06:26 -0700388 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes())
389 << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700390 self->VerifyStack();
391 // Start new JNI local reference state.
392 JNIEnvExt* env = self->GetJniEnv();
393 ScopedObjectAccessUnchecked soa(env);
394 ScopedJniEnvLocalRefState env_state(env);
395 // Create local ref. copies of proxy method and the receiver.
396 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
397
398 // Placing arguments into args vector and remove the receiver.
399 MethodHelper proxy_mh(proxy_method);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700400 DCHECK(!proxy_mh.IsStatic()) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700401 std::vector<jvalue> args;
402 BuildQuickArgumentVisitor local_ref_visitor(sp, proxy_mh.IsStatic(), proxy_mh.GetShorty(),
403 proxy_mh.GetShortyLength(), &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700404
Ian Rogers848871b2013-08-05 10:56:33 -0700405 local_ref_visitor.VisitArguments();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700406 DCHECK_GT(args.size(), 0U) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700407 args.erase(args.begin());
408
409 // Convert proxy method into expected interface method.
Brian Carlstromea46f952013-07-30 01:26:50 -0700410 mirror::ArtMethod* interface_method = proxy_method->FindOverriddenMethod();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700411 DCHECK(interface_method != NULL) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700412 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
413 jobject interface_method_jobj = soa.AddLocalReference<jobject>(interface_method);
414
415 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
416 // that performs allocations.
417 self->EndAssertNoThreadSuspension(old_cause);
418 JValue result = InvokeProxyInvocationHandler(soa, proxy_mh.GetShorty(),
419 rcvr_jobj, interface_method_jobj, args);
420 return result.GetJ();
421}
422
423// Read object references held in arguments from quick frames and place in a JNI local references,
424// so they don't get garbage collected.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700425class RememberForGcArgumentVisitor : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700426 public:
Mathieu Chartier590fee92013-09-13 13:46:47 -0700427 RememberForGcArgumentVisitor(mirror::ArtMethod** sp, bool is_static, const char* shorty,
428 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) :
Ian Rogers848871b2013-08-05 10:56:33 -0700429 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
430
431 virtual void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
432 if (IsParamAReference()) {
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700433 mirror::Object** param_address = reinterpret_cast<mirror::Object**>(GetParamAddress());
434 jobject reference =
435 soa_->AddLocalReference<jobject>(*param_address);
436 references_.push_back(std::make_pair(reference, param_address));
437 }
438 }
439
440 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
441 // Fixup any references which may have changed.
442 for (std::pair<jobject, mirror::Object**>& it : references_) {
443 *it.second = soa_->Decode<mirror::Object*>(it.first);
Ian Rogers848871b2013-08-05 10:56:33 -0700444 }
445 }
446
447 private:
448 ScopedObjectAccessUnchecked* soa_;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700449 std::vector<std::pair<jobject, mirror::Object**> > references_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700450 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700451};
452
453// Lazily resolve a method for quick. Called by stub code.
Brian Carlstromea46f952013-07-30 01:26:50 -0700454extern "C" const void* artQuickResolutionTrampoline(mirror::ArtMethod* called,
Ian Rogers848871b2013-08-05 10:56:33 -0700455 mirror::Object* receiver,
Brian Carlstromea46f952013-07-30 01:26:50 -0700456 Thread* thread, mirror::ArtMethod** sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700457 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
458 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsAndArgs);
459 // Start new JNI local reference state
460 JNIEnvExt* env = thread->GetJniEnv();
461 ScopedObjectAccessUnchecked soa(env);
462 ScopedJniEnvLocalRefState env_state(env);
463 const char* old_cause = thread->StartAssertNoThreadSuspension("Quick method resolution set up");
464
465 // Compute details about the called method (avoid GCs)
466 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Brian Carlstromea46f952013-07-30 01:26:50 -0700467 mirror::ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Ian Rogers848871b2013-08-05 10:56:33 -0700468 InvokeType invoke_type;
469 const DexFile* dex_file;
470 uint32_t dex_method_idx;
471 if (called->IsRuntimeMethod()) {
472 uint32_t dex_pc = caller->ToDexPc(QuickArgumentVisitor::GetCallingPc(sp));
473 const DexFile::CodeItem* code;
474 {
475 MethodHelper mh(caller);
476 dex_file = &mh.GetDexFile();
477 code = mh.GetCodeItem();
478 }
479 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
480 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
481 Instruction::Code instr_code = instr->Opcode();
482 bool is_range;
483 switch (instr_code) {
484 case Instruction::INVOKE_DIRECT:
485 invoke_type = kDirect;
486 is_range = false;
487 break;
488 case Instruction::INVOKE_DIRECT_RANGE:
489 invoke_type = kDirect;
490 is_range = true;
491 break;
492 case Instruction::INVOKE_STATIC:
493 invoke_type = kStatic;
494 is_range = false;
495 break;
496 case Instruction::INVOKE_STATIC_RANGE:
497 invoke_type = kStatic;
498 is_range = true;
499 break;
500 case Instruction::INVOKE_SUPER:
501 invoke_type = kSuper;
502 is_range = false;
503 break;
504 case Instruction::INVOKE_SUPER_RANGE:
505 invoke_type = kSuper;
506 is_range = true;
507 break;
508 case Instruction::INVOKE_VIRTUAL:
509 invoke_type = kVirtual;
510 is_range = false;
511 break;
512 case Instruction::INVOKE_VIRTUAL_RANGE:
513 invoke_type = kVirtual;
514 is_range = true;
515 break;
516 case Instruction::INVOKE_INTERFACE:
517 invoke_type = kInterface;
518 is_range = false;
519 break;
520 case Instruction::INVOKE_INTERFACE_RANGE:
521 invoke_type = kInterface;
522 is_range = true;
523 break;
524 default:
525 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(NULL);
526 // Avoid used uninitialized warnings.
527 invoke_type = kDirect;
528 is_range = false;
529 }
530 dex_method_idx = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
531
532 } else {
533 invoke_type = kStatic;
534 dex_file = &MethodHelper(called).GetDexFile();
535 dex_method_idx = called->GetDexMethodIndex();
536 }
537 uint32_t shorty_len;
538 const char* shorty =
539 dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700540 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -0700541 visitor.VisitArguments();
542 thread->EndAssertNoThreadSuspension(old_cause);
543 // Resolve method filling in dex cache.
544 if (called->IsRuntimeMethod()) {
545 called = linker->ResolveMethod(dex_method_idx, caller, invoke_type);
546 }
547 const void* code = NULL;
548 if (LIKELY(!thread->IsExceptionPending())) {
549 // Incompatible class change should have been handled in resolve method.
550 CHECK(!called->CheckIncompatibleClassChange(invoke_type));
551 // Refine called method based on receiver.
552 if (invoke_type == kVirtual) {
553 called = receiver->GetClass()->FindVirtualMethodForVirtual(called);
554 } else if (invoke_type == kInterface) {
555 called = receiver->GetClass()->FindVirtualMethodForInterface(called);
556 }
Ian Rogers83883d72013-10-21 21:07:24 -0700557 if ((invoke_type == kVirtual) || (invoke_type == kInterface)) {
558 // We came here because of sharpening. Ensure the dex cache is up-to-date on the method index
559 // of the sharpened method.
560 if (called->GetDexCacheResolvedMethods() == caller->GetDexCacheResolvedMethods()) {
561 caller->GetDexCacheResolvedMethods()->Set(called->GetDexMethodIndex(), called);
562 } else {
563 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000564 // the caller's dex file. Since we get here only if the original called was a runtime
565 // method, we've got the correct dex_file and a dex_method_idx from above.
566 DCHECK(&MethodHelper(caller).GetDexFile() == dex_file);
Ian Rogers83883d72013-10-21 21:07:24 -0700567 uint32_t method_index =
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000568 MethodHelper(called).FindDexMethodIndexInOtherDexFile(*dex_file, dex_method_idx);
Ian Rogers83883d72013-10-21 21:07:24 -0700569 if (method_index != DexFile::kDexNoIndex) {
570 caller->GetDexCacheResolvedMethods()->Set(method_index, called);
571 }
572 }
573 }
Ian Rogers848871b2013-08-05 10:56:33 -0700574 // Ensure that the called method's class is initialized.
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800575 SirtRef<mirror::Class> called_class(soa.Self(), called->GetDeclaringClass());
Ian Rogers848871b2013-08-05 10:56:33 -0700576 linker->EnsureInitialized(called_class, true, true);
577 if (LIKELY(called_class->IsInitialized())) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800578 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -0700579 } else if (called_class->IsInitializing()) {
580 if (invoke_type == kStatic) {
581 // Class is still initializing, go to oat and grab code (trampoline must be left in place
582 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -0800583 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -0700584 } else {
585 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800586 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -0700587 }
588 } else {
589 DCHECK(called_class->IsErroneous());
590 }
591 }
592 CHECK_EQ(code == NULL, thread->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700593 // Fixup any locally saved objects may have moved during a GC.
594 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700595 // Place called method in callee-save frame to be placed as first argument to quick method.
596 *sp = called;
597 return code;
598}
599
600} // namespace art