blob: 65e404ab55c9927b9fce1d9b4fde86ade6963c2f [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 Rogers2dd0e2c2013-01-24 12:42:14 -080018#include "mirror/abstract_method-inl.h"
19#include "mirror/object.h"
20#include "mirror/object_array-inl.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070021#include "object_utils.h"
22#include "reflection.h"
TDYa1275bb86012012-04-11 05:57:28 -070023#include "runtime_support.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070024#include "scoped_thread_state_change.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070025#include "thread.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070026#include "well_known_classes.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070027
28#include "ScopedLocalRef.h"
29
30namespace art {
31
Ian Rogersaf6e67a2013-01-16 08:38:37 -080032// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
33// to jobjects.
34class BuildArgumentVisitor : public ArgumentVisitor {
35 public:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036 BuildArgumentVisitor(MethodHelper& caller_mh, mirror::AbstractMethod** sp,
Ian Rogersaf6e67a2013-01-16 08:38:37 -080037 ScopedObjectAccessUnchecked& soa, std::vector<jvalue>& args) :
38 ArgumentVisitor(caller_mh, sp), soa_(soa), args_(args) {}
39
40 virtual void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
41 jvalue val;
42 Primitive::Type type = GetParamPrimitiveType();
43 switch (type) {
44 case Primitive::kPrimNot: {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045 mirror::Object* obj = *reinterpret_cast<mirror::Object**>(GetParamAddress());
Ian Rogersaf6e67a2013-01-16 08:38:37 -080046 val.l = soa_.AddLocalReference<jobject>(obj);
47 break;
48 }
49 case Primitive::kPrimLong: // Fall-through.
50 case Primitive::kPrimDouble:
51 if (IsSplitLongOrDouble()) {
52 val.j = ReadSplitLongParam();
53 } else {
54 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
55 }
56 break;
Ian Rogers7db619b2013-01-16 18:35:48 -080057 case Primitive::kPrimBoolean: // Fall-through.
58 case Primitive::kPrimByte: // Fall-through.
59 case Primitive::kPrimChar: // Fall-through.
60 case Primitive::kPrimShort: // Fall-through.
61 case Primitive::kPrimInt: // Fall-through.
Ian Rogersaf6e67a2013-01-16 08:38:37 -080062 case Primitive::kPrimFloat:
63 val.i = *reinterpret_cast<jint*>(GetParamAddress());
64 break;
65 case Primitive::kPrimVoid:
66 LOG(FATAL) << "UNREACHABLE";
67 val.j = 0;
68 break;
69 }
70 args_.push_back(val);
71 }
72
73 private:
74 ScopedObjectAccessUnchecked& soa_;
75 std::vector<jvalue>& args_;
Ian Rogers7db619b2013-01-16 18:35:48 -080076
77 DISALLOW_COPY_AND_ASSIGN(BuildArgumentVisitor);
Ian Rogersaf6e67a2013-01-16 08:38:37 -080078};
79
Ian Rogers57b86d42012-03-27 16:05:41 -070080// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
Ian Rogersaf6e67a2013-01-16 08:38:37 -080081// which is responsible for recording callee save registers. We explicitly place into jobjects the
82// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
83// field within the proxy object, which will box the primitive arguments and deal with error cases.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080084extern "C" uint64_t artProxyInvokeHandler(mirror::AbstractMethod* proxy_method,
85 mirror::Object* receiver,
86 Thread* self, mirror::AbstractMethod** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -070087 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersf58dffd2013-01-13 09:25:49 -080088 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
89 const char* old_cause =
90 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
91 // Register the top of the managed stack, making stack crawlable.
Ian Rogersaf6e67a2013-01-16 08:38:37 -080092 DCHECK_EQ(*sp, proxy_method);
93 self->SetTopOfStack(sp, 0);
94 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(),
95 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
Ian Rogersf58dffd2013-01-13 09:25:49 -080096 self->VerifyStack();
97 // Start new JNI local reference state.
Ian Rogers57b86d42012-03-27 16:05:41 -070098 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070099 ScopedObjectAccessUnchecked soa(env);
Ian Rogers57b86d42012-03-27 16:05:41 -0700100 ScopedJniEnvLocalRefState env_state(env);
Ian Rogersf58dffd2013-01-13 09:25:49 -0800101 // Create local ref. copies of proxy method and the receiver.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700102 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
Ian Rogers57b86d42012-03-27 16:05:41 -0700103
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800104 // Placing arguments into args vector and remove the receiver.
Ian Rogers57b86d42012-03-27 16:05:41 -0700105 MethodHelper proxy_mh(proxy_method);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800106 std::vector<jvalue> args;
107 BuildArgumentVisitor local_ref_visitor(proxy_mh, sp, soa, args);
108 local_ref_visitor.VisitArguments();
109 args.erase(args.begin());
Ian Rogersf58dffd2013-01-13 09:25:49 -0800110
Ian Rogersf58dffd2013-01-13 09:25:49 -0800111 // Convert proxy method into expected interface method.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800112 mirror::AbstractMethod* interface_method = proxy_method->FindOverriddenMethod();
Ian Rogers57b86d42012-03-27 16:05:41 -0700113 DCHECK(interface_method != NULL);
114 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800115 jobject interface_method_jobj = soa.AddLocalReference<jobject>(interface_method);
116
117 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
118 // that performs allocations.
119 self->EndAssertNoThreadSuspension(old_cause);
120 JValue result = InvokeProxyInvocationHandler(soa, proxy_mh.GetShorty(),
121 rcvr_jobj, interface_method_jobj, args);
122 return result.GetJ();
Ian Rogers57b86d42012-03-27 16:05:41 -0700123}
124
125} // namespace art