blob: 040a701e1de07f62970569bc9593d50f8965c65a [file] [log] [blame]
Ian Rogers57b86d42012-03-27 16:05:41 -07001/*
Elliott Hughes0f3c5532012-03-30 14:51:51 -07002 * Copyright (C) 2012 The Android Open Source Project
Ian Rogers57b86d42012-03-27 16:05:41 -07003 *
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
Ian Rogersaf6e67a2013-01-16 08:38:37 -080017#include "argument_visitor.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070018#include "dex_file-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019#include "mirror/abstract_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080020#include "mirror/object_array-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070021#include "mirror/object-inl.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070022#include "object_utils.h"
23#include "reflection.h"
TDYa1275bb86012012-04-11 05:57:28 -070024#include "runtime_support.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070025#include "scoped_thread_state_change.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070026#include "thread.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070027#include "well_known_classes.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070028
29#include "ScopedLocalRef.h"
30
31namespace art {
32
Ian Rogersaf6e67a2013-01-16 08:38:37 -080033// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
34// to jobjects.
35class BuildArgumentVisitor : public ArgumentVisitor {
36 public:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037 BuildArgumentVisitor(MethodHelper& caller_mh, mirror::AbstractMethod** sp,
Ian Rogersaf6e67a2013-01-16 08:38:37 -080038 ScopedObjectAccessUnchecked& soa, std::vector<jvalue>& args) :
39 ArgumentVisitor(caller_mh, sp), soa_(soa), args_(args) {}
40
41 virtual void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
42 jvalue val;
43 Primitive::Type type = GetParamPrimitiveType();
44 switch (type) {
45 case Primitive::kPrimNot: {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046 mirror::Object* obj = *reinterpret_cast<mirror::Object**>(GetParamAddress());
Ian Rogersaf6e67a2013-01-16 08:38:37 -080047 val.l = soa_.AddLocalReference<jobject>(obj);
48 break;
49 }
50 case Primitive::kPrimLong: // Fall-through.
51 case Primitive::kPrimDouble:
52 if (IsSplitLongOrDouble()) {
53 val.j = ReadSplitLongParam();
54 } else {
55 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
56 }
57 break;
Ian Rogers7db619b2013-01-16 18:35:48 -080058 case Primitive::kPrimBoolean: // Fall-through.
59 case Primitive::kPrimByte: // Fall-through.
60 case Primitive::kPrimChar: // Fall-through.
61 case Primitive::kPrimShort: // Fall-through.
62 case Primitive::kPrimInt: // Fall-through.
Ian Rogersaf6e67a2013-01-16 08:38:37 -080063 case Primitive::kPrimFloat:
64 val.i = *reinterpret_cast<jint*>(GetParamAddress());
65 break;
66 case Primitive::kPrimVoid:
67 LOG(FATAL) << "UNREACHABLE";
68 val.j = 0;
69 break;
70 }
71 args_.push_back(val);
72 }
73
74 private:
75 ScopedObjectAccessUnchecked& soa_;
76 std::vector<jvalue>& args_;
Ian Rogers7db619b2013-01-16 18:35:48 -080077
78 DISALLOW_COPY_AND_ASSIGN(BuildArgumentVisitor);
Ian Rogersaf6e67a2013-01-16 08:38:37 -080079};
80
Ian Rogers57b86d42012-03-27 16:05:41 -070081// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
Ian Rogersaf6e67a2013-01-16 08:38:37 -080082// which is responsible for recording callee save registers. We explicitly place into jobjects the
83// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
84// field within the proxy object, which will box the primitive arguments and deal with error cases.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080085extern "C" uint64_t artProxyInvokeHandler(mirror::AbstractMethod* proxy_method,
86 mirror::Object* receiver,
87 Thread* self, mirror::AbstractMethod** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -070088 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersf58dffd2013-01-13 09:25:49 -080089 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
90 const char* old_cause =
91 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
92 // Register the top of the managed stack, making stack crawlable.
Ian Rogersaf6e67a2013-01-16 08:38:37 -080093 DCHECK_EQ(*sp, proxy_method);
94 self->SetTopOfStack(sp, 0);
95 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(),
96 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
Ian Rogersf58dffd2013-01-13 09:25:49 -080097 self->VerifyStack();
98 // Start new JNI local reference state.
Ian Rogers57b86d42012-03-27 16:05:41 -070099 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700100 ScopedObjectAccessUnchecked soa(env);
Ian Rogers57b86d42012-03-27 16:05:41 -0700101 ScopedJniEnvLocalRefState env_state(env);
Ian Rogersf58dffd2013-01-13 09:25:49 -0800102 // Create local ref. copies of proxy method and the receiver.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700103 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
Ian Rogers57b86d42012-03-27 16:05:41 -0700104
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800105 // Placing arguments into args vector and remove the receiver.
Ian Rogers57b86d42012-03-27 16:05:41 -0700106 MethodHelper proxy_mh(proxy_method);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800107 std::vector<jvalue> args;
108 BuildArgumentVisitor local_ref_visitor(proxy_mh, sp, soa, args);
109 local_ref_visitor.VisitArguments();
110 args.erase(args.begin());
Ian Rogersf58dffd2013-01-13 09:25:49 -0800111
Ian Rogersf58dffd2013-01-13 09:25:49 -0800112 // Convert proxy method into expected interface method.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800113 mirror::AbstractMethod* interface_method = proxy_method->FindOverriddenMethod();
Ian Rogers57b86d42012-03-27 16:05:41 -0700114 DCHECK(interface_method != NULL);
115 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800116 jobject interface_method_jobj = soa.AddLocalReference<jobject>(interface_method);
117
118 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
119 // that performs allocations.
120 self->EndAssertNoThreadSuspension(old_cause);
121 JValue result = InvokeProxyInvocationHandler(soa, proxy_mh.GetShorty(),
122 rcvr_jobj, interface_method_jobj, args);
123 return result.GetJ();
Ian Rogers57b86d42012-03-27 16:05:41 -0700124}
125
126} // namespace art