blob: 7c47a8f3038e72bb10a572d6f5ddc1ccd28d4f5d [file] [log] [blame]
Shih-wei Liao2d831012011-09-28 22:06:53 -07001/*
2 * Copyright 2011 Google Inc. All Rights Reserved.
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 "runtime_support.h"
18
Ian Rogerscaab8c42011-10-12 12:11:18 -070019#include "dex_cache.h"
Elliott Hughes6c8867d2011-10-03 16:34:05 -070020#include "dex_verifier.h"
Ian Rogerscaab8c42011-10-12 12:11:18 -070021#include "macros.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080022#include "object.h"
23#include "object_utils.h"
Ian Rogersdfcdf1a2011-10-10 17:50:35 -070024#include "reflection.h"
jeffhaoe343b762011-12-05 16:36:44 -080025#include "trace.h"
Ian Rogersdfcdf1a2011-10-10 17:50:35 -070026#include "ScopedLocalRef.h"
Elliott Hughes6c8867d2011-10-03 16:34:05 -070027
Shih-wei Liao2d831012011-09-28 22:06:53 -070028namespace art {
29
Ian Rogers4f0d07c2011-10-06 23:38:47 -070030// Place a special frame at the TOS that will save the callee saves for the given type
31static void FinishCalleeSaveFrameSetup(Thread* self, Method** sp, Runtime::CalleeSaveType type) {
Ian Rogersce9eca62011-10-07 17:11:03 -070032 // Be aware the store below may well stomp on an incoming argument
Ian Rogers4f0d07c2011-10-06 23:38:47 -070033 *sp = Runtime::Current()->GetCalleeSaveMethod(type);
34 self->SetTopOfStack(sp, 0);
35}
36
buzbee44b412b2012-02-04 08:50:53 -080037/*
38 * Report location to debugger. Note: dalvikPC is the current offset within
39 * the method. However, because the offset alone cannot distinguish between
40 * method entry and offset 0 within the method, we'll use an offset of -1
41 * to denote method entry.
42 */
43extern "C" void artUpdateDebuggerFromCode(int32_t dalvikPC, Thread* self, Method** sp) {
44 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
45 // TODO: fill this out similar to old "updateDebugger"
46}
47
Shih-wei Liao2d831012011-09-28 22:06:53 -070048// Temporary debugging hook for compiler.
49extern void DebugMe(Method* method, uint32_t info) {
50 LOG(INFO) << "DebugMe";
51 if (method != NULL) {
52 LOG(INFO) << PrettyMethod(method);
53 }
54 LOG(INFO) << "Info: " << info;
55}
56
Brian Carlstrom6fd03fb2011-10-17 16:11:00 -070057extern "C" uint32_t artObjectInitFromCode(Object* o, Thread* self, Method** sp) {
58 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogerscaab8c42011-10-12 12:11:18 -070059 Class* c = o->GetClass();
60 if (UNLIKELY(c->IsFinalizable())) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -070061 Heap::AddFinalizerReference(self, o);
Ian Rogerscaab8c42011-10-12 12:11:18 -070062 }
63 /*
64 * NOTE: once debugger/profiler support is added, we'll need to check
65 * here and branch to actual compiled object.<init> to handle any
Ian Rogers0571d352011-11-03 19:51:38 -070066 * breakpoint/logging activities if either is active.
Ian Rogerscaab8c42011-10-12 12:11:18 -070067 */
Brian Carlstrom6fd03fb2011-10-17 16:11:00 -070068 return self->IsExceptionPending() ? -1 : 0;
Ian Rogerscaab8c42011-10-12 12:11:18 -070069}
70
Shih-wei Liao2d831012011-09-28 22:06:53 -070071// Return value helper for jobject return types
72extern Object* DecodeJObjectInThread(Thread* thread, jobject obj) {
Brian Carlstrom6f495f22011-10-10 15:05:03 -070073 if (thread->IsExceptionPending()) {
74 return NULL;
75 }
Shih-wei Liao2d831012011-09-28 22:06:53 -070076 return thread->DecodeJObject(obj);
77}
78
79extern void* FindNativeMethod(Thread* thread) {
80 DCHECK(Thread::Current() == thread);
81
82 Method* method = const_cast<Method*>(thread->GetCurrentMethod());
83 DCHECK(method != NULL);
84
85 // Lookup symbol address for method, on failure we'll return NULL with an
86 // exception set, otherwise we return the address of the method we found.
87 void* native_code = thread->GetJniEnv()->vm->FindCodeForNativeMethod(method);
88 if (native_code == NULL) {
89 DCHECK(thread->IsExceptionPending());
90 return NULL;
91 } else {
92 // Register so that future calls don't come here
93 method->RegisterNative(native_code);
94 return native_code;
95 }
96}
97
98// Called by generated call to throw an exception
99extern "C" void artDeliverExceptionFromCode(Throwable* exception, Thread* thread, Method** sp) {
100 /*
101 * exception may be NULL, in which case this routine should
102 * throw NPE. NOTE: this is a convenience for generated code,
103 * which previously did the null check inline and constructed
104 * and threw a NPE if NULL. This routine responsible for setting
105 * exception_ in thread and delivering the exception.
106 */
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700107 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700108 if (exception == NULL) {
109 thread->ThrowNewException("Ljava/lang/NullPointerException;", "throw with null exception");
110 } else {
111 thread->SetException(exception);
112 }
113 thread->DeliverException();
114}
115
116// Deliver an exception that's pending on thread helping set up a callee save frame on the way
117extern "C" void artDeliverPendingExceptionFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700118 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700119 thread->DeliverException();
120}
121
122// Called by generated call to throw a NPE exception
123extern "C" void artThrowNullPointerExceptionFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700124 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700125 thread->ThrowNewException("Ljava/lang/NullPointerException;", NULL);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700126 thread->DeliverException();
127}
128
129// Called by generated call to throw an arithmetic divide by zero exception
130extern "C" void artThrowDivZeroFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700131 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700132 thread->ThrowNewException("Ljava/lang/ArithmeticException;", "divide by zero");
133 thread->DeliverException();
134}
135
136// Called by generated call to throw an arithmetic divide by zero exception
137extern "C" void artThrowArrayBoundsFromCode(int index, int limit, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700138 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
139 thread->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
140 "length=%d; index=%d", limit, index);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700141 thread->DeliverException();
142}
143
144// Called by the AbstractMethodError stub (not runtime support)
145extern void ThrowAbstractMethodErrorFromCode(Method* method, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700146 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
147 thread->ThrowNewExceptionF("Ljava/lang/AbstractMethodError;",
148 "abstract method \"%s\"", PrettyMethod(method).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700149 thread->DeliverException();
150}
151
152extern "C" void artThrowStackOverflowFromCode(Method* method, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700153 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
jeffhaoe343b762011-12-05 16:36:44 -0800154 // Remove extra entry pushed onto second stack during method tracing
jeffhao2692b572011-12-16 15:42:28 -0800155 if (Runtime::Current()->IsMethodTracingActive()) {
jeffhaoe343b762011-12-05 16:36:44 -0800156 artTraceMethodUnwindFromCode(thread);
157 }
Shih-wei Liao2d831012011-09-28 22:06:53 -0700158 thread->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700159 thread->ThrowNewExceptionF("Ljava/lang/StackOverflowError;",
160 "stack size %zdkb; default stack size: %zdkb",
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700161 thread->GetStackSize() / KB, Runtime::Current()->GetDefaultStackSize() / KB);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700162 thread->ResetDefaultStackEnd(); // Return to default stack size
163 thread->DeliverException();
164}
165
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700166static std::string ClassNameFromIndex(Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700167 verifier::VerifyErrorRefType ref_type, bool access) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700168 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
169 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
170
171 uint16_t type_idx = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700172 if (ref_type == verifier::VERIFY_ERROR_REF_FIELD) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700173 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
174 type_idx = id.class_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700175 } else if (ref_type == verifier::VERIFY_ERROR_REF_METHOD) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700176 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
177 type_idx = id.class_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700178 } else if (ref_type == verifier::VERIFY_ERROR_REF_CLASS) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700179 type_idx = ref;
180 } else {
181 CHECK(false) << static_cast<int>(ref_type);
182 }
183
Ian Rogers0571d352011-11-03 19:51:38 -0700184 std::string class_name(PrettyDescriptor(dex_file.StringByTypeIdx(type_idx)));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700185 if (!access) {
186 return class_name;
187 }
188
189 std::string result;
190 result += "tried to access class ";
191 result += class_name;
192 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800193 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700194 return result;
195}
196
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700197static std::string FieldNameFromIndex(const Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700198 verifier::VerifyErrorRefType ref_type, bool access) {
199 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_FIELD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700200
201 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
202 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
203
204 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700205 std::string class_name(PrettyDescriptor(dex_file.GetFieldDeclaringClassDescriptor(id)));
Ian Rogers0571d352011-11-03 19:51:38 -0700206 const char* field_name = dex_file.StringDataByIdx(id.name_idx_);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700207 if (!access) {
208 return class_name + "." + field_name;
209 }
210
211 std::string result;
212 result += "tried to access field ";
213 result += class_name + "." + field_name;
214 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800215 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700216 return result;
217}
218
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700219static std::string MethodNameFromIndex(const Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700220 verifier::VerifyErrorRefType ref_type, bool access) {
221 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_METHOD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700222
223 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
224 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
225
226 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700227 std::string class_name(PrettyDescriptor(dex_file.GetMethodDeclaringClassDescriptor(id)));
Ian Rogers0571d352011-11-03 19:51:38 -0700228 const char* method_name = dex_file.StringDataByIdx(id.name_idx_);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700229 if (!access) {
230 return class_name + "." + method_name;
231 }
232
233 std::string result;
234 result += "tried to access method ";
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700235 result += class_name + "." + method_name + ":" +
Ian Rogers0571d352011-11-03 19:51:38 -0700236 dex_file.CreateMethodSignature(id.proto_idx_, NULL);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700237 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800238 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700239 return result;
240}
241
242extern "C" void artThrowVerificationErrorFromCode(int32_t kind, int32_t ref, Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700243 FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll);
244 Frame frame = self->GetTopOfStack(); // We need the calling method as context to interpret 'ref'
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700245 frame.Next();
246 Method* method = frame.GetMethod();
247
Ian Rogersd81871c2011-10-03 13:57:23 -0700248 verifier::VerifyErrorRefType ref_type =
249 static_cast<verifier::VerifyErrorRefType>(kind >> verifier::kVerifyErrorRefTypeShift);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700250
251 const char* exception_class = "Ljava/lang/VerifyError;";
252 std::string msg;
253
Ian Rogersd81871c2011-10-03 13:57:23 -0700254 switch (static_cast<verifier::VerifyError>(kind & ~(0xff << verifier::kVerifyErrorRefTypeShift))) {
255 case verifier::VERIFY_ERROR_NO_CLASS:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700256 exception_class = "Ljava/lang/NoClassDefFoundError;";
257 msg = ClassNameFromIndex(method, ref, ref_type, false);
258 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700259 case verifier::VERIFY_ERROR_NO_FIELD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700260 exception_class = "Ljava/lang/NoSuchFieldError;";
261 msg = FieldNameFromIndex(method, ref, ref_type, false);
262 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700263 case verifier::VERIFY_ERROR_NO_METHOD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700264 exception_class = "Ljava/lang/NoSuchMethodError;";
265 msg = MethodNameFromIndex(method, ref, ref_type, false);
266 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700267 case verifier::VERIFY_ERROR_ACCESS_CLASS:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700268 exception_class = "Ljava/lang/IllegalAccessError;";
269 msg = ClassNameFromIndex(method, ref, ref_type, true);
270 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700271 case verifier::VERIFY_ERROR_ACCESS_FIELD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700272 exception_class = "Ljava/lang/IllegalAccessError;";
273 msg = FieldNameFromIndex(method, ref, ref_type, true);
274 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700275 case verifier::VERIFY_ERROR_ACCESS_METHOD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700276 exception_class = "Ljava/lang/IllegalAccessError;";
277 msg = MethodNameFromIndex(method, ref, ref_type, true);
278 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700279 case verifier::VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700280 exception_class = "Ljava/lang/IncompatibleClassChangeError;";
281 msg = ClassNameFromIndex(method, ref, ref_type, false);
282 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700283 case verifier::VERIFY_ERROR_INSTANTIATION:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700284 exception_class = "Ljava/lang/InstantiationError;";
285 msg = ClassNameFromIndex(method, ref, ref_type, false);
286 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700287 case verifier::VERIFY_ERROR_GENERIC:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700288 // Generic VerifyError; use default exception, no message.
289 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700290 case verifier::VERIFY_ERROR_NONE:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700291 CHECK(false);
292 break;
293 }
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700294 self->ThrowNewException(exception_class, msg.c_str());
295 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700296}
297
298extern "C" void artThrowInternalErrorFromCode(int32_t errnum, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700299 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700300 LOG(WARNING) << "TODO: internal error detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700301 thread->ThrowNewExceptionF("Ljava/lang/InternalError;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700302 thread->DeliverException();
303}
304
305extern "C" void artThrowRuntimeExceptionFromCode(int32_t errnum, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700306 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700307 LOG(WARNING) << "TODO: runtime exception detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700308 thread->ThrowNewExceptionF("Ljava/lang/RuntimeException;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700309 thread->DeliverException();
310}
311
Elliott Hughese1410a22011-10-04 12:10:24 -0700312extern "C" void artThrowNoSuchMethodFromCode(int32_t method_idx, Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700313 FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll);
314 Frame frame = self->GetTopOfStack(); // We need the calling method as context for the method_idx
Elliott Hughese1410a22011-10-04 12:10:24 -0700315 frame.Next();
316 Method* method = frame.GetMethod();
Elliott Hughese1410a22011-10-04 12:10:24 -0700317 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Ian Rogersd81871c2011-10-03 13:57:23 -0700318 MethodNameFromIndex(method, method_idx, verifier::VERIFY_ERROR_REF_METHOD, false).c_str());
Elliott Hughese1410a22011-10-04 12:10:24 -0700319 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700320}
321
322extern "C" void artThrowNegArraySizeFromCode(int32_t size, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700323 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700324 LOG(WARNING) << "UNTESTED artThrowNegArraySizeFromCode";
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700325 thread->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", size);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700326 thread->DeliverException();
327}
328
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700329void* UnresolvedDirectMethodTrampolineFromCode(int32_t method_idx, Method** sp, Thread* thread,
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700330 Runtime::TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700331 // TODO: this code is specific to ARM
332 // On entry the stack pointed by sp is:
333 // | argN | |
334 // | ... | |
335 // | arg4 | |
336 // | arg3 spill | | Caller's frame
337 // | arg2 spill | |
338 // | arg1 spill | |
339 // | Method* | ---
340 // | LR |
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700341 // | ... | callee saves
Ian Rogersad25ac52011-10-04 19:13:33 -0700342 // | R3 | arg3
343 // | R2 | arg2
344 // | R1 | arg1
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700345 // | R0 |
346 // | Method* | <- sp
347 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp) + kPointerSize);
348 DCHECK_EQ(48U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
349 Method** caller_sp = reinterpret_cast<Method**>(reinterpret_cast<byte*>(sp) + 48);
350 uintptr_t caller_pc = regs[10];
351 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsAndArgs);
Ian Rogersad25ac52011-10-04 19:13:33 -0700352 // Start new JNI local reference state
353 JNIEnvExt* env = thread->GetJniEnv();
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700354 ScopedJniEnvLocalRefState env_state(env);
Ian Rogersad25ac52011-10-04 19:13:33 -0700355 // Discover shorty (avoid GCs)
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700356 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogersad25ac52011-10-04 19:13:33 -0700357 const char* shorty = linker->MethodShorty(method_idx, *caller_sp);
358 size_t shorty_len = strlen(shorty);
Ian Rogers14b1b242011-10-11 18:54:34 -0700359 size_t args_in_regs = 0;
360 for (size_t i = 1; i < shorty_len; i++) {
361 char c = shorty[i];
362 args_in_regs = args_in_regs + (c == 'J' || c == 'D' ? 2 : 1);
363 if (args_in_regs > 3) {
364 args_in_regs = 3;
365 break;
366 }
367 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700368 bool is_static;
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700369 if (type == Runtime::kUnknownMethod) {
Ian Rogersea2a11d2011-10-11 16:48:51 -0700370 Method* caller = *caller_sp;
Ian Rogersdf9a7822011-10-11 16:53:22 -0700371 // less two as return address may span into next dex instruction
372 uint32_t dex_pc = caller->ToDexPC(caller_pc - 2);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800373 const DexFile::CodeItem* code = MethodHelper(caller).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700374 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
375 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700376 Instruction::Code instr_code = instr->Opcode();
377 is_static = (instr_code == Instruction::INVOKE_STATIC) ||
378 (instr_code == Instruction::INVOKE_STATIC_RANGE);
379 DCHECK(is_static || (instr_code == Instruction::INVOKE_DIRECT) ||
380 (instr_code == Instruction::INVOKE_DIRECT_RANGE));
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700381 } else {
Ian Rogersea2a11d2011-10-11 16:48:51 -0700382 is_static = type == Runtime::kStaticMethod;
383 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700384 // Place into local references incoming arguments from the caller's register arguments
Ian Rogers14b1b242011-10-11 18:54:34 -0700385 size_t cur_arg = 1; // skip method_idx in R0, first arg is in R1
Ian Rogersea2a11d2011-10-11 16:48:51 -0700386 if (!is_static) {
387 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
388 cur_arg++;
Ian Rogers14b1b242011-10-11 18:54:34 -0700389 if (args_in_regs < 3) {
390 // If we thought we had fewer than 3 arguments in registers, account for the receiver
391 args_in_regs++;
392 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700393 AddLocalReference<jobject>(env, obj);
394 }
Ian Rogers14b1b242011-10-11 18:54:34 -0700395 size_t shorty_index = 1; // skip return value
396 // Iterate while arguments and arguments in registers (less 1 from cur_arg which is offset to skip
397 // R0)
398 while ((cur_arg - 1) < args_in_regs && shorty_index < shorty_len) {
399 char c = shorty[shorty_index];
400 shorty_index++;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700401 if (c == 'L') {
Ian Rogersad25ac52011-10-04 19:13:33 -0700402 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
403 AddLocalReference<jobject>(env, obj);
404 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700405 cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1);
406 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700407 // Place into local references incoming arguments from the caller's stack arguments
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700408 cur_arg += 11; // skip LR, Method* and spills for R1 to R3 and callee saves
Ian Rogers14b1b242011-10-11 18:54:34 -0700409 while (shorty_index < shorty_len) {
410 char c = shorty[shorty_index];
411 shorty_index++;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700412 if (c == 'L') {
Ian Rogers14b1b242011-10-11 18:54:34 -0700413 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700414 AddLocalReference<jobject>(env, obj);
Ian Rogersad25ac52011-10-04 19:13:33 -0700415 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700416 cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1);
Ian Rogersad25ac52011-10-04 19:13:33 -0700417 }
418 // Resolve method filling in dex cache
419 Method* called = linker->ResolveMethod(method_idx, *caller_sp, true);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700420 if (LIKELY(!thread->IsExceptionPending())) {
Ian Rogers573db4a2011-12-13 15:30:50 -0800421 if (LIKELY(called->IsDirect())) {
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800422 // Ensure that the called method's class is initialized
423 Class* called_class = called->GetDeclaringClass();
424 linker->EnsureInitialized(called_class, true);
425 if (LIKELY(called_class->IsInitialized())) {
426 // Update CodeAndDirectMethod table and avoid the trampoline when we know the called class
427 // is initialized (see test 084-class-init SlowInit)
428 Method* caller = *caller_sp;
429 DexCache* dex_cache = caller->GetDeclaringClass()->GetDexCache();
430 dex_cache->GetCodeAndDirectMethods()->SetResolvedDirectMethod(method_idx, called);
431 // We got this far, ensure that the declaring class is initialized
432 linker->EnsureInitialized(called->GetDeclaringClass(), true);
433 }
Ian Rogers573db4a2011-12-13 15:30:50 -0800434 } else {
435 // Direct method has been made virtual
436 thread->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;",
437 "Expected direct method but found virtual: %s",
438 PrettyMethod(called, true).c_str());
439 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700440 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700441 void* code;
Ian Rogerscaab8c42011-10-12 12:11:18 -0700442 if (UNLIKELY(thread->IsExceptionPending())) {
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700443 // Something went wrong in ResolveMethod or EnsureInitialized,
444 // go into deliver exception with the pending exception in r0
Ian Rogersad25ac52011-10-04 19:13:33 -0700445 code = reinterpret_cast<void*>(art_deliver_exception_from_code);
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700446 regs[0] = reinterpret_cast<uintptr_t>(thread->GetException());
Ian Rogersad25ac52011-10-04 19:13:33 -0700447 thread->ClearException();
448 } else {
449 // Expect class to at least be initializing
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800450 DCHECK(called->GetDeclaringClass()->IsInitializing());
Ian Rogersad25ac52011-10-04 19:13:33 -0700451 // Set up entry into main method
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700452 regs[0] = reinterpret_cast<uintptr_t>(called);
Ian Rogersad25ac52011-10-04 19:13:33 -0700453 code = const_cast<void*>(called->GetCode());
454 }
455 return code;
456}
457
Ian Rogerscaab8c42011-10-12 12:11:18 -0700458// Fast path field resolution that can't throw exceptions
Ian Rogers1bddec32012-02-04 12:27:34 -0800459static Field* FindFieldFast(uint32_t field_idx, const Method* referrer, bool is_primitive,
460 size_t expected_size) {
Ian Rogers53a77a52012-02-06 09:47:45 -0800461 Field* resolved_field = referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700462 if (UNLIKELY(resolved_field == NULL)) {
463 return NULL;
464 }
465 Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogers1bddec32012-02-04 12:27:34 -0800466 // Check class is initiliazed or initializing
Ian Rogerscaab8c42011-10-12 12:11:18 -0700467 if (UNLIKELY(!fields_class->IsInitializing())) {
468 return NULL;
469 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800470 Class* referring_class = referrer->GetDeclaringClass();
471 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
472 !referring_class->CanAccessMember(fields_class,
473 resolved_field->GetAccessFlags()))) {
474 // illegal access
475 return NULL;
476 }
477 FieldHelper fh(resolved_field);
478 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
479 fh.FieldSize() != expected_size)) {
480 return NULL;
481 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700482 return resolved_field;
483}
484
485// Slow path field resolution and declaring class initialization
Ian Rogers1bddec32012-02-04 12:27:34 -0800486Field* FindFieldFromCode(uint32_t field_idx, const Method* referrer, Thread* self,
487 bool is_static, bool is_primitive, size_t expected_size) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700488 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700489 Field* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
490 if (LIKELY(resolved_field != NULL)) {
491 Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogers1bddec32012-02-04 12:27:34 -0800492 Class* referring_class = referrer->GetDeclaringClass();
493 if (UNLIKELY(!referring_class->CanAccess(fields_class))) {
494 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;","%s tried to access class %s",
495 PrettyMethod(referrer).c_str(),
496 PrettyDescriptor(fields_class).c_str());
497 } else if (UNLIKELY(!referring_class->CanAccessMember(fields_class,
498 resolved_field->GetAccessFlags()))) {
499 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;","%s tried to access field %s",
500 PrettyMethod(referrer).c_str(),
501 PrettyField(resolved_field, false).c_str());
502 return NULL; // failure
Ian Rogerscaab8c42011-10-12 12:11:18 -0700503 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800504 FieldHelper fh(resolved_field);
505 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
506 fh.FieldSize() != expected_size)) {
507 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughes1e409252012-02-06 11:21:27 -0800508 "Attempted read of %zd-bit %s on field '%s'",
Ian Rogers1bddec32012-02-04 12:27:34 -0800509 expected_size * (32 / sizeof(int32_t)),
510 is_primitive ? "primitive" : "non-primitive",
511 PrettyField(resolved_field, true).c_str());
512 }
513 if (!is_static) {
514 // instance fields must be being accessed on an initialized class
Ian Rogerscaab8c42011-10-12 12:11:18 -0700515 return resolved_field;
Ian Rogers1bddec32012-02-04 12:27:34 -0800516 } else {
517 // If the class is already initializing, we must be inside <clinit>, or
518 // we'd still be waiting for the lock.
519 if (fields_class->IsInitializing()) {
520 return resolved_field;
521 } else if (Runtime::Current()->GetClassLinker()->EnsureInitialized(fields_class, true)) {
522 return resolved_field;
523 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700524 }
525 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800526 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
Ian Rogersce9eca62011-10-07 17:11:03 -0700527 return NULL;
528}
529
Ian Rogers1bddec32012-02-04 12:27:34 -0800530static void ThrowNullPointerExceptionForFieldAccess(Thread* self, Field* field, bool is_read) {
531 self->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
532 "Attempt to %s field '%s' of a null object",
533 is_read ? "read from" : "write to",
534 PrettyField(field, true).c_str());
Ian Rogersce9eca62011-10-07 17:11:03 -0700535}
536
537extern "C" uint32_t artGet32StaticFromCode(uint32_t field_idx, const Method* referrer,
538 Thread* self, Method** sp) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800539 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int32_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700540 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800541 return field->Get32(NULL);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700542 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700543 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers1bddec32012-02-04 12:27:34 -0800544 field = FindFieldFromCode(field_idx, referrer, self, true, true, sizeof(int32_t));
545 if (LIKELY(field != NULL)) {
546 return field->Get32(NULL);
Ian Rogersce9eca62011-10-07 17:11:03 -0700547 }
548 return 0; // Will throw exception by checking with Thread::Current
549}
550
551extern "C" uint64_t artGet64StaticFromCode(uint32_t field_idx, const Method* referrer,
552 Thread* self, Method** sp) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800553 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int64_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700554 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800555 return field->Get64(NULL);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700556 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700557 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers1bddec32012-02-04 12:27:34 -0800558 field = FindFieldFromCode(field_idx, referrer, self, true, true, sizeof(int64_t));
559 if (LIKELY(field != NULL)) {
560 return field->Get64(NULL);
Ian Rogersce9eca62011-10-07 17:11:03 -0700561 }
562 return 0; // Will throw exception by checking with Thread::Current
563}
564
565extern "C" Object* artGetObjStaticFromCode(uint32_t field_idx, const Method* referrer,
566 Thread* self, Method** sp) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800567 Field* field = FindFieldFast(field_idx, referrer, false, sizeof(Object*));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700568 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800569 return field->GetObj(NULL);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700570 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700571 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers1bddec32012-02-04 12:27:34 -0800572 field = FindFieldFromCode(field_idx, referrer, self, true, false, sizeof(Object*));
573 if (LIKELY(field != NULL)) {
574 return field->GetObj(NULL);
575 }
576 return NULL; // Will throw exception by checking with Thread::Current
577}
578
579extern "C" uint32_t artGet32InstanceFromCode(uint32_t field_idx, Object* obj,
580 const Method* referrer, Thread* self, Method** sp) {
581 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int32_t));
582 if (LIKELY(field != NULL && obj != NULL)) {
583 return field->Get32(obj);
584 }
585 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
586 field = FindFieldFromCode(field_idx, referrer, self, false, true, sizeof(int32_t));
587 if (LIKELY(field != NULL)) {
588 if (UNLIKELY(obj == NULL)) {
589 ThrowNullPointerExceptionForFieldAccess(self, field, true);
Ian Rogersce9eca62011-10-07 17:11:03 -0700590 } else {
Ian Rogers1bddec32012-02-04 12:27:34 -0800591 return field->Get32(obj);
592 }
593 }
594 return 0; // Will throw exception by checking with Thread::Current
595}
596
597extern "C" uint64_t artGet64InstanceFromCode(uint32_t field_idx, Object* obj,
598 const Method* referrer, Thread* self, Method** sp) {
599 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int64_t));
600 if (LIKELY(field != NULL && obj != NULL)) {
601 return field->Get64(obj);
602 }
603 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
604 field = FindFieldFromCode(field_idx, referrer, self, false, true, sizeof(int64_t));
605 if (LIKELY(field != NULL)) {
606 if (UNLIKELY(obj == NULL)) {
607 ThrowNullPointerExceptionForFieldAccess(self, field, true);
608 } else {
609 return field->Get64(obj);
610 }
611 }
612 return 0; // Will throw exception by checking with Thread::Current
613}
614
615extern "C" Object* artGetObjInstanceFromCode(uint32_t field_idx, Object* obj,
616 const Method* referrer, Thread* self, Method** sp) {
617 Field* field = FindFieldFast(field_idx, referrer, false, sizeof(Object*));
618 if (LIKELY(field != NULL && obj != NULL)) {
619 return field->GetObj(obj);
620 }
621 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
622 field = FindFieldFromCode(field_idx, referrer, self, false, false, sizeof(Object*));
623 if (LIKELY(field != NULL)) {
624 if (UNLIKELY(obj == NULL)) {
625 ThrowNullPointerExceptionForFieldAccess(self, field, true);
626 } else {
627 return field->GetObj(obj);
Ian Rogersce9eca62011-10-07 17:11:03 -0700628 }
629 }
630 return NULL; // Will throw exception by checking with Thread::Current
631}
632
Ian Rogers1bddec32012-02-04 12:27:34 -0800633extern "C" int artSet32StaticFromCode(uint32_t field_idx, uint32_t new_value,
634 const Method* referrer, Thread* self, Method** sp) {
635 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int32_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700636 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800637 field->Set32(NULL, new_value);
638 return 0; // success
Ian Rogerscaab8c42011-10-12 12:11:18 -0700639 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700640 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers1bddec32012-02-04 12:27:34 -0800641 field = FindFieldFromCode(field_idx, referrer, self, true, true, sizeof(int32_t));
642 if (LIKELY(field != NULL)) {
643 field->Set32(NULL, new_value);
644 return 0; // success
Ian Rogersce9eca62011-10-07 17:11:03 -0700645 }
646 return -1; // failure
647}
648
649extern "C" int artSet64StaticFromCode(uint32_t field_idx, const Method* referrer,
650 uint64_t new_value, Thread* self, Method** sp) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800651 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int64_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700652 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800653 field->Set64(NULL, new_value);
654 return 0; // success
Ian Rogerscaab8c42011-10-12 12:11:18 -0700655 }
656 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers1bddec32012-02-04 12:27:34 -0800657 field = FindFieldFromCode(field_idx, referrer, self, true, true, sizeof(int64_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700658 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800659 field->Set64(NULL, new_value);
660 return 0; // success
Ian Rogersce9eca62011-10-07 17:11:03 -0700661 }
662 return -1; // failure
663}
664
Ian Rogers1bddec32012-02-04 12:27:34 -0800665extern "C" int artSetObjStaticFromCode(uint32_t field_idx, Object* new_value,
666 const Method* referrer, Thread* self, Method** sp) {
667 Field* field = FindFieldFast(field_idx, referrer, false, sizeof(Object*));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700668 if (LIKELY(field != NULL)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800669 if (LIKELY(!FieldHelper(field).IsPrimitiveType())) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700670 field->SetObj(NULL, new_value);
671 return 0; // success
672 }
673 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700674 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers1bddec32012-02-04 12:27:34 -0800675 field = FindFieldFromCode(field_idx, referrer, self, true, false, sizeof(Object*));
676 if (LIKELY(field != NULL)) {
677 field->SetObj(NULL, new_value);
678 return 0; // success
679 }
680 return -1; // failure
681}
682
683extern "C" int artSet32InstanceFromCode(uint32_t field_idx, Object* obj, uint32_t new_value,
684 const Method* referrer, Thread* self, Method** sp) {
685 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int32_t));
686 if (LIKELY(field != NULL && obj != NULL)) {
687 field->Set32(obj, new_value);
688 return 0; // success
689 }
690 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
691 field = FindFieldFromCode(field_idx, referrer, self, false, true, sizeof(int32_t));
692 if (LIKELY(field != NULL)) {
693 if (UNLIKELY(obj == NULL)) {
694 ThrowNullPointerExceptionForFieldAccess(self, field, false);
Ian Rogersce9eca62011-10-07 17:11:03 -0700695 } else {
Ian Rogers1bddec32012-02-04 12:27:34 -0800696 field->Set32(obj, new_value);
697 return 0; // success
698 }
699 }
700 return -1; // failure
701}
702
703extern "C" int artSet64InstanceFromCode(uint32_t field_idx, Object* obj, uint64_t new_value,
704 Thread* self, Method** sp) {
705 Method* callee_save = Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsOnly);
706 Method* referrer = sp[callee_save->GetFrameSizeInBytes() / sizeof(Method*)];
707 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int64_t));
708 if (LIKELY(field != NULL && obj != NULL)) {
709 field->Set64(obj, new_value);
710 return 0; // success
711 }
712 *sp = callee_save;
713 self->SetTopOfStack(sp, 0);
714 field = FindFieldFromCode(field_idx, referrer, self, false, true, sizeof(int64_t));
715 if (LIKELY(field != NULL)) {
716 if (UNLIKELY(obj == NULL)) {
717 ThrowNullPointerExceptionForFieldAccess(self, field, false);
718 } else {
719 field->Set64(obj, new_value);
720 return 0; // success
721 }
722 }
723 return -1; // failure
724}
725
726extern "C" int artSetObjInstanceFromCode(uint32_t field_idx, Object* obj, Object* new_value,
727 const Method* referrer, Thread* self, Method** sp) {
728 Field* field = FindFieldFast(field_idx, referrer, false, sizeof(Object*));
729 if (LIKELY(field != NULL && obj != NULL)) {
730 field->SetObj(obj, new_value);
731 return 0; // success
732 }
733 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
734 field = FindFieldFromCode(field_idx, referrer, self, false, false, sizeof(Object*));
735 if (LIKELY(field != NULL)) {
736 if (UNLIKELY(obj == NULL)) {
737 ThrowNullPointerExceptionForFieldAccess(self, field, false);
738 } else {
739 field->SetObj(obj, new_value);
Ian Rogersce9eca62011-10-07 17:11:03 -0700740 return 0; // success
741 }
742 }
743 return -1; // failure
744}
745
Shih-wei Liao2d831012011-09-28 22:06:53 -0700746// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
747// cannot be resolved, throw an error. If it can, use it to create an instance.
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800748// When verification/compiler hasn't been able to verify access, optionally perform an access
749// check.
750static Object* AllocObjectFromCode(uint32_t type_idx, Method* method, Thread* self,
751 bool access_check) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700752 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700753 Runtime* runtime = Runtime::Current();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700754 if (UNLIKELY(klass == NULL)) {
buzbee33a129c2011-10-06 16:53:20 -0700755 klass = runtime->GetClassLinker()->ResolveType(type_idx, method);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700756 if (klass == NULL) {
buzbee33a129c2011-10-06 16:53:20 -0700757 DCHECK(self->IsExceptionPending());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700758 return NULL; // Failure
759 }
760 }
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800761 if (access_check) {
Ian Rogersd4135902012-02-03 18:05:08 -0800762 if (UNLIKELY(!klass->IsInstantiable())) {
763 self->ThrowNewException("Ljava/lang/InstantiationError;",
764 PrettyDescriptor(klass).c_str());
765 return NULL; // Failure
766 }
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800767 Class* referrer = method->GetDeclaringClass();
768 if (UNLIKELY(!referrer->CanAccess(klass))) {
769 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
770 "illegal class access: '%s' -> '%s'",
771 PrettyDescriptor(referrer).c_str(),
772 PrettyDescriptor(klass).c_str());
773 return NULL; // Failure
774 }
775 }
buzbee33a129c2011-10-06 16:53:20 -0700776 if (!runtime->GetClassLinker()->EnsureInitialized(klass, true)) {
777 DCHECK(self->IsExceptionPending());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700778 return NULL; // Failure
779 }
780 return klass->AllocObject();
781}
782
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800783extern "C" Object* artAllocObjectFromCode(uint32_t type_idx, Method* method,
784 Thread* self, Method** sp) {
785 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
786 return AllocObjectFromCode(type_idx, method, self, false);
787}
788
Ian Rogers28ad40d2011-10-27 15:19:26 -0700789extern "C" Object* artAllocObjectFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
790 Thread* self, Method** sp) {
791 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800792 return AllocObjectFromCode(type_idx, method, self, true);
793}
794
795// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
796// it cannot be resolved, throw an error. If it can, use it to create an array.
797// When verification/compiler hasn't been able to verify access, optionally perform an access
798// check.
799static Array* AllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
800 Thread* self, bool access_check) {
801 if (UNLIKELY(component_count < 0)) {
802 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d",
803 component_count);
804 return NULL; // Failure
805 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700806 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800807 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
808 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
809 if (klass == NULL) { // Error
810 DCHECK(Thread::Current()->IsExceptionPending());
811 return NULL; // Failure
812 }
813 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
814 }
815 if (access_check) {
816 Class* referrer = method->GetDeclaringClass();
817 if (UNLIKELY(!referrer->CanAccess(klass))) {
818 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
819 "illegal class access: '%s' -> '%s'",
820 PrettyDescriptor(referrer).c_str(),
821 PrettyDescriptor(klass).c_str());
Ian Rogers28ad40d2011-10-27 15:19:26 -0700822 return NULL; // Failure
823 }
824 }
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800825 return Array::Alloc(klass, component_count);
buzbeecc4540e2011-10-27 13:06:03 -0700826}
827
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800828extern "C" Array* artAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
829 Thread* self, Method** sp) {
830 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
831 return AllocArrayFromCode(type_idx, method, component_count, self, false);
832}
833
834extern "C" Array* artAllocArrayFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
835 int32_t component_count,
836 Thread* self, Method** sp) {
837 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
838 return AllocArrayFromCode(type_idx, method, component_count, self, true);
839}
840
841// Helper function to alloc array for OP_FILLED_NEW_ARRAY
Ian Rogersce9eca62011-10-07 17:11:03 -0700842Array* CheckAndAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800843 Thread* self, bool access_check) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700844 if (UNLIKELY(component_count < 0)) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700845 self->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", component_count);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700846 return NULL; // Failure
847 }
848 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700849 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
Shih-wei Liao2d831012011-09-28 22:06:53 -0700850 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
851 if (klass == NULL) { // Error
852 DCHECK(Thread::Current()->IsExceptionPending());
853 return NULL; // Failure
854 }
855 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700856 if (UNLIKELY(klass->IsPrimitive() && !klass->IsPrimitiveInt())) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700857 if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700858 Thread::Current()->ThrowNewExceptionF("Ljava/lang/RuntimeException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700859 "Bad filled array request for type %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800860 PrettyDescriptor(klass).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700861 } else {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700862 Thread::Current()->ThrowNewExceptionF("Ljava/lang/InternalError;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700863 "Found type %s; filled-new-array not implemented for anything but \'int\'",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800864 PrettyDescriptor(klass).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700865 }
866 return NULL; // Failure
867 } else {
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800868 if (access_check) {
869 Class* referrer = method->GetDeclaringClass();
870 if (UNLIKELY(!referrer->CanAccess(klass))) {
871 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
872 "illegal class access: '%s' -> '%s'",
873 PrettyDescriptor(referrer).c_str(),
874 PrettyDescriptor(klass).c_str());
875 return NULL; // Failure
876 }
877 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700878 DCHECK(klass->IsArrayClass()) << PrettyClass(klass);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700879 return Array::Alloc(klass, component_count);
880 }
881}
882
Ian Rogersce9eca62011-10-07 17:11:03 -0700883extern "C" Array* artCheckAndAllocArrayFromCode(uint32_t type_idx, Method* method,
884 int32_t component_count, Thread* self, Method** sp) {
885 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800886 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self, false);
Ian Rogersce9eca62011-10-07 17:11:03 -0700887}
888
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800889extern "C" Array* artCheckAndAllocArrayFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
890 int32_t component_count,
891 Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700892 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800893 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self, true);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700894}
895
Ian Rogerscaab8c42011-10-12 12:11:18 -0700896// Assignable test for code, won't throw. Null and equality tests already performed
897uint32_t IsAssignableFromCode(const Class* klass, const Class* ref_class) {
898 DCHECK(klass != NULL);
899 DCHECK(ref_class != NULL);
900 return klass->IsAssignableFrom(ref_class) ? 1 : 0;
901}
902
Shih-wei Liao2d831012011-09-28 22:06:53 -0700903// Check whether it is safe to cast one class to the other, throw exception and return -1 on failure
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700904extern "C" int artCheckCastFromCode(const Class* a, const Class* b, Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700905 DCHECK(a->IsClass()) << PrettyClass(a);
906 DCHECK(b->IsClass()) << PrettyClass(b);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700907 if (LIKELY(b->IsAssignableFrom(a))) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700908 return 0; // Success
909 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700910 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700911 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassCastException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700912 "%s cannot be cast to %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800913 PrettyDescriptor(a).c_str(),
914 PrettyDescriptor(b).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700915 return -1; // Failure
916 }
917}
918
919// Tests whether 'element' can be assigned into an array of type 'array_class'.
920// Returns 0 on success and -1 if an exception is pending.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700921extern "C" int artCanPutArrayElementFromCode(const Object* element, const Class* array_class,
922 Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700923 DCHECK(array_class != NULL);
924 // element can't be NULL as we catch this is screened in runtime_support
925 Class* element_class = element->GetClass();
926 Class* component_type = array_class->GetComponentType();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700927 if (LIKELY(component_type->IsAssignableFrom(element_class))) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700928 return 0; // Success
929 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700930 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700931 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughesd3127d62012-01-17 13:42:26 -0800932 "%s cannot be stored in an array of type %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800933 PrettyDescriptor(element_class).c_str(),
934 PrettyDescriptor(array_class).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700935 return -1; // Failure
936 }
937}
938
Elliott Hughesf3778f62012-01-26 14:14:35 -0800939Class* ResolveVerifyAndClinit(uint32_t type_idx, const Method* referrer, Thread* self,
940 bool can_run_clinit, bool verify_access) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700941 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
942 Class* klass = class_linker->ResolveType(type_idx, referrer);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700943 if (UNLIKELY(klass == NULL)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700944 CHECK(self->IsExceptionPending());
945 return NULL; // Failure - Indicate to caller to deliver exception
946 }
Elliott Hughesf3778f62012-01-26 14:14:35 -0800947 // Perform access check if necessary.
948 if (verify_access && !referrer->GetDeclaringClass()->CanAccess(klass)) {
Ian Rogersb093c6b2011-10-31 16:19:55 -0700949 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
Elliott Hughesf3778f62012-01-26 14:14:35 -0800950 "Class %s is inaccessible to method %s",
951 PrettyDescriptor(klass).c_str(),
952 PrettyMethod(referrer, true).c_str());
953 return NULL; // Failure - Indicate to caller to deliver exception
954 }
955 // If we're just implementing const-class, we shouldn't call <clinit>.
956 if (!can_run_clinit) {
957 return klass;
Ian Rogersb093c6b2011-10-31 16:19:55 -0700958 }
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700959 // If we are the <clinit> of this class, just return our storage.
960 //
961 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
962 // running.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800963 if (klass == referrer->GetDeclaringClass() && MethodHelper(referrer).IsClassInitializer()) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700964 return klass;
965 }
966 if (!class_linker->EnsureInitialized(klass, true)) {
967 CHECK(self->IsExceptionPending());
968 return NULL; // Failure - Indicate to caller to deliver exception
969 }
970 referrer->GetDexCacheInitializedStaticStorage()->Set(type_idx, klass);
971 return klass;
Shih-wei Liao2d831012011-09-28 22:06:53 -0700972}
973
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700974extern "C" Class* artInitializeStaticStorageFromCode(uint32_t type_idx, const Method* referrer,
975 Thread* self, Method** sp) {
976 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -0800977 return ResolveVerifyAndClinit(type_idx, referrer, self, true, true);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700978}
979
Ian Rogers28ad40d2011-10-27 15:19:26 -0700980extern "C" Class* artInitializeTypeFromCode(uint32_t type_idx, const Method* referrer, Thread* self,
981 Method** sp) {
982 // Called when method->dex_cache_resolved_types_[] misses
983 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -0800984 return ResolveVerifyAndClinit(type_idx, referrer, self, false, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700985}
986
Ian Rogersb093c6b2011-10-31 16:19:55 -0700987extern "C" Class* artInitializeTypeAndVerifyAccessFromCode(uint32_t type_idx,
988 const Method* referrer, Thread* self,
989 Method** sp) {
990 // Called when caller isn't guaranteed to have access to a type and the dex cache may be
991 // unpopulated
992 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -0800993 return ResolveVerifyAndClinit(type_idx, referrer, self, false, true);
Ian Rogersb093c6b2011-10-31 16:19:55 -0700994}
995
buzbee48d72222012-01-11 15:19:51 -0800996// Helper function to resolve virtual method
997extern "C" Method* artResolveMethodFromCode(Method* referrer,
998 uint32_t method_idx,
999 bool is_direct,
1000 Thread* self,
1001 Method** sp) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001002 /*
1003 * Slow-path handler on invoke virtual method path in which
buzbee48d72222012-01-11 15:19:51 -08001004 * base method is unresolved at compile-time. Caller will
1005 * unwind if can't resolve.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001006 */
buzbee48d72222012-01-11 15:19:51 -08001007 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
1008 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1009 Method* method = class_linker->ResolveMethod(method_idx, referrer, is_direct);
1010 referrer->GetDexCacheResolvedMethods()->Set(method_idx, method);
1011 return method;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001012}
1013
Brian Carlstromaded5f72011-10-07 17:15:04 -07001014String* ResolveStringFromCode(const Method* referrer, uint32_t string_idx) {
1015 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1016 return class_linker->ResolveString(string_idx, referrer);
1017}
1018
1019extern "C" String* artResolveStringFromCode(Method* referrer, int32_t string_idx,
1020 Thread* self, Method** sp) {
1021 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
1022 return ResolveStringFromCode(referrer, string_idx);
1023}
1024
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001025extern "C" int artUnlockObjectFromCode(Object* obj, Thread* self, Method** sp) {
1026 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001027 DCHECK(obj != NULL); // Assumed to have been checked before entry
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001028 // MonitorExit may throw exception
1029 return obj->MonitorExit(self) ? 0 /* Success */ : -1 /* Failure */;
1030}
1031
1032extern "C" void artLockObjectFromCode(Object* obj, Thread* thread, Method** sp) {
1033 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
1034 DCHECK(obj != NULL); // Assumed to have been checked before entry
1035 obj->MonitorEnter(thread); // May block
Shih-wei Liao2d831012011-09-28 22:06:53 -07001036 DCHECK(thread->HoldsLock(obj));
1037 // Only possible exception is NPE and is handled before entry
1038 DCHECK(!thread->IsExceptionPending());
1039}
1040
Ian Rogers4a510d82011-10-09 14:30:24 -07001041void CheckSuspendFromCode(Thread* thread) {
1042 // Called when thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001043 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
1044}
1045
Ian Rogers4a510d82011-10-09 14:30:24 -07001046extern "C" void artTestSuspendFromCode(Thread* thread, Method** sp) {
1047 // Called when suspend count check value is 0 and thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001048 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001049 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
1050}
1051
1052/*
1053 * Fill the array with predefined constant values, throwing exceptions if the array is null or
1054 * not of sufficient length.
1055 *
1056 * NOTE: When dealing with a raw dex file, the data to be copied uses
1057 * little-endian ordering. Require that oat2dex do any required swapping
1058 * so this routine can get by with a memcpy().
1059 *
1060 * Format of the data:
1061 * ushort ident = 0x0300 magic value
1062 * ushort width width of each element in the table
1063 * uint size number of elements in the table
1064 * ubyte data[size*width] table of data values (may contain a single-byte
1065 * padding at the end)
1066 */
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001067extern "C" int artHandleFillArrayDataFromCode(Array* array, const uint16_t* table,
1068 Thread* self, Method** sp) {
1069 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001070 DCHECK_EQ(table[0], 0x0300);
Ian Rogerscaab8c42011-10-12 12:11:18 -07001071 if (UNLIKELY(array == NULL)) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001072 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
1073 "null array in fill array");
Shih-wei Liao2d831012011-09-28 22:06:53 -07001074 return -1; // Error
1075 }
1076 DCHECK(array->IsArrayInstance() && !array->IsObjectArray());
1077 uint32_t size = (uint32_t)table[2] | (((uint32_t)table[3]) << 16);
Ian Rogerscaab8c42011-10-12 12:11:18 -07001078 if (UNLIKELY(static_cast<int32_t>(size) > array->GetLength())) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001079 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
1080 "failed array fill. length=%d; index=%d", array->GetLength(), size);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001081 return -1; // Error
1082 }
1083 uint16_t width = table[1];
1084 uint32_t size_in_bytes = size * width;
1085 memcpy((char*)array + Array::DataOffset().Int32Value(), (char*)&table[4], size_in_bytes);
1086 return 0; // Success
1087}
1088
1089// See comments in runtime_support_asm.S
1090extern "C" uint64_t artFindInterfaceMethodInCacheFromCode(uint32_t method_idx,
Ian Rogerscaab8c42011-10-12 12:11:18 -07001091 Object* this_object,
1092 Method* caller_method,
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001093 Thread* thread, Method** sp) {
Ian Rogerscaab8c42011-10-12 12:11:18 -07001094 Method* interface_method = caller_method->GetDexCacheResolvedMethods()->Get(method_idx);
1095 Method* found_method = NULL; // The found method
1096 if (LIKELY(interface_method != NULL && this_object != NULL)) {
Ian Rogersb04f69f2011-10-17 00:40:54 -07001097 found_method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method, false);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001098 }
Ian Rogerscaab8c42011-10-12 12:11:18 -07001099 if (UNLIKELY(found_method == NULL)) {
1100 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsAndArgs);
1101 if (this_object == NULL) {
1102 thread->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
1103 "null receiver during interface dispatch");
1104 return 0;
1105 }
1106 if (interface_method == NULL) {
1107 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1108 interface_method = class_linker->ResolveMethod(method_idx, caller_method, false);
1109 if (interface_method == NULL) {
1110 // Could not resolve interface method. Throw error and unwind
1111 CHECK(thread->IsExceptionPending());
1112 return 0;
1113 }
1114 }
Ian Rogersb04f69f2011-10-17 00:40:54 -07001115 found_method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method, true);
Ian Rogerscaab8c42011-10-12 12:11:18 -07001116 if (found_method == NULL) {
1117 CHECK(thread->IsExceptionPending());
1118 return 0;
1119 }
Shih-wei Liao2d831012011-09-28 22:06:53 -07001120 }
Ian Rogerscaab8c42011-10-12 12:11:18 -07001121 const void* code = found_method->GetCode();
Shih-wei Liao2d831012011-09-28 22:06:53 -07001122
Ian Rogerscaab8c42011-10-12 12:11:18 -07001123 uint32_t method_uint = reinterpret_cast<uint32_t>(found_method);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001124 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
1125 uint64_t result = ((code_uint << 32) | method_uint);
1126 return result;
1127}
1128
Ian Rogers466bb252011-10-14 03:29:56 -07001129static void ThrowNewUndeclaredThrowableException(Thread* self, JNIEnv* env, Throwable* exception) {
1130 ScopedLocalRef<jclass> jlr_UTE_class(env,
1131 env->FindClass("java/lang/reflect/UndeclaredThrowableException"));
1132 if (jlr_UTE_class.get() == NULL) {
1133 LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\"";
1134 } else {
1135 jmethodID jlre_UTE_constructor = env->GetMethodID(jlr_UTE_class.get(), "<init>",
1136 "(Ljava/lang/Throwable;)V");
1137 jthrowable jexception = AddLocalReference<jthrowable>(env, exception);
1138 ScopedLocalRef<jthrowable> jlr_UTE(env,
1139 reinterpret_cast<jthrowable>(env->NewObject(jlr_UTE_class.get(), jlre_UTE_constructor,
1140 jexception)));
1141 int rc = env->Throw(jlr_UTE.get());
1142 if (rc != JNI_OK) {
1143 LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\"";
1144 }
1145 }
1146 CHECK(self->IsExceptionPending());
1147}
1148
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001149// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
1150// which is responsible for recording callee save registers. We explicitly handlerize incoming
1151// reference arguments (so they survive GC) and create a boxed argument array. Finally we invoke
1152// the invocation handler which is a field within the proxy object receiver.
1153extern "C" void artProxyInvokeHandler(Method* proxy_method, Object* receiver,
Ian Rogers466bb252011-10-14 03:29:56 -07001154 Thread* self, byte* stack_args) {
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001155 // Register the top of the managed stack
Ian Rogers466bb252011-10-14 03:29:56 -07001156 Method** proxy_sp = reinterpret_cast<Method**>(stack_args - 12);
1157 DCHECK_EQ(*proxy_sp, proxy_method);
1158 self->SetTopOfStack(proxy_sp, 0);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001159 // TODO: ARM specific
1160 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(), 48u);
1161 // Start new JNI local reference state
1162 JNIEnvExt* env = self->GetJniEnv();
1163 ScopedJniEnvLocalRefState env_state(env);
1164 // Create local ref. copies of proxy method and the receiver
1165 jobject rcvr_jobj = AddLocalReference<jobject>(env, receiver);
1166 jobject proxy_method_jobj = AddLocalReference<jobject>(env, proxy_method);
1167
Ian Rogers14b1b242011-10-11 18:54:34 -07001168 // Placing into local references incoming arguments from the caller's register arguments,
1169 // replacing original Object* with jobject
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001170 MethodHelper proxy_mh(proxy_method);
1171 const size_t num_params = proxy_mh.NumArgs();
Ian Rogers14b1b242011-10-11 18:54:34 -07001172 size_t args_in_regs = 0;
1173 for (size_t i = 1; i < num_params; i++) { // skip receiver
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001174 args_in_regs = args_in_regs + (proxy_mh.IsParamALongOrDouble(i) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001175 if (args_in_regs > 2) {
1176 args_in_regs = 2;
1177 break;
1178 }
1179 }
1180 size_t cur_arg = 0; // current stack location to read
1181 size_t param_index = 1; // skip receiver
1182 while (cur_arg < args_in_regs && param_index < num_params) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001183 if (proxy_mh.IsParamAReference(param_index)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001184 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001185 jobject jobj = AddLocalReference<jobject>(env, obj);
Ian Rogers14b1b242011-10-11 18:54:34 -07001186 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001187 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001188 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001189 param_index++;
1190 }
1191 // Placing into local references incoming arguments from the caller's stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001192 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
Ian Rogers14b1b242011-10-11 18:54:34 -07001193 while (param_index < num_params) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001194 if (proxy_mh.IsParamAReference(param_index)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001195 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
1196 jobject jobj = AddLocalReference<jobject>(env, obj);
1197 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001198 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001199 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001200 param_index++;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001201 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001202 // Set up arguments array and place in local IRT during boxing (which may allocate/GC)
1203 jvalue args_jobj[3];
1204 args_jobj[0].l = rcvr_jobj;
1205 args_jobj[1].l = proxy_method_jobj;
Ian Rogers466bb252011-10-14 03:29:56 -07001206 // Args array, if no arguments then NULL (don't include receiver in argument count)
1207 args_jobj[2].l = NULL;
1208 ObjectArray<Object>* args = NULL;
1209 if ((num_params - 1) > 0) {
1210 args = Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(num_params - 1);
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001211 if (args == NULL) {
Ian Rogers466bb252011-10-14 03:29:56 -07001212 CHECK(self->IsExceptionPending());
1213 return;
1214 }
1215 args_jobj[2].l = AddLocalReference<jobjectArray>(env, args);
1216 }
1217 // Convert proxy method into expected interface method
1218 Method* interface_method = proxy_method->FindOverriddenMethod();
1219 CHECK(interface_method != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001220 CHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
Ian Rogers466bb252011-10-14 03:29:56 -07001221 args_jobj[1].l = AddLocalReference<jobject>(env, interface_method);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001222 // Box arguments
Ian Rogers14b1b242011-10-11 18:54:34 -07001223 cur_arg = 0; // reset stack location to read to start
1224 // reset index, will index into param type array which doesn't include the receiver
1225 param_index = 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001226 ObjectArray<Class>* param_types = proxy_mh.GetParameterTypes();
Ian Rogers14b1b242011-10-11 18:54:34 -07001227 CHECK(param_types != NULL);
1228 // Check number of parameter types agrees with number from the Method - less 1 for the receiver.
1229 CHECK_EQ(static_cast<size_t>(param_types->GetLength()), num_params - 1);
1230 while (cur_arg < args_in_regs && param_index < (num_params - 1)) {
1231 Class* param_type = param_types->Get(param_index);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001232 Object* obj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001233 if (!param_type->IsPrimitive()) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001234 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001235 } else {
Ian Rogers14b1b242011-10-11 18:54:34 -07001236 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
1237 if (cur_arg == 1 && (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble())) {
1238 // long/double split over regs and stack, mask in high half from stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001239 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args + (13 * kPointerSize));
Ian Rogerscaab8c42011-10-12 12:11:18 -07001240 val.j = (val.j & 0xffffffffULL) | (high_half << 32);
Ian Rogers14b1b242011-10-11 18:54:34 -07001241 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001242 BoxPrimitive(env, param_type->GetPrimitiveType(), val);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001243 if (self->IsExceptionPending()) {
1244 return;
1245 }
1246 obj = val.l;
1247 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001248 args->Set(param_index, obj);
1249 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
1250 param_index++;
1251 }
1252 // Placing into local references incoming arguments from the caller's stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001253 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
1254 while (param_index < (num_params - 1)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001255 Class* param_type = param_types->Get(param_index);
1256 Object* obj;
1257 if (!param_type->IsPrimitive()) {
1258 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
1259 } else {
1260 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001261 BoxPrimitive(env, param_type->GetPrimitiveType(), val);
Ian Rogers14b1b242011-10-11 18:54:34 -07001262 if (self->IsExceptionPending()) {
1263 return;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001264 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001265 obj = val.l;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001266 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001267 args->Set(param_index, obj);
1268 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
1269 param_index++;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001270 }
1271 // Get the InvocationHandler method and the field that holds it within the Proxy object
1272 static jmethodID inv_hand_invoke_mid = NULL;
1273 static jfieldID proxy_inv_hand_fid = NULL;
1274 if (proxy_inv_hand_fid == NULL) {
Ian Rogers466bb252011-10-14 03:29:56 -07001275 ScopedLocalRef<jclass> proxy(env, env->FindClass("java/lang/reflect/Proxy"));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001276 proxy_inv_hand_fid = env->GetFieldID(proxy.get(), "h", "Ljava/lang/reflect/InvocationHandler;");
Ian Rogers466bb252011-10-14 03:29:56 -07001277 ScopedLocalRef<jclass> inv_hand_class(env, env->FindClass("java/lang/reflect/InvocationHandler"));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001278 inv_hand_invoke_mid = env->GetMethodID(inv_hand_class.get(), "invoke",
1279 "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;");
1280 }
Ian Rogers466bb252011-10-14 03:29:56 -07001281 DCHECK(env->IsInstanceOf(rcvr_jobj, env->FindClass("java/lang/reflect/Proxy")));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001282 jobject inv_hand = env->GetObjectField(rcvr_jobj, proxy_inv_hand_fid);
1283 // Call InvocationHandler.invoke
1284 jobject result = env->CallObjectMethodA(inv_hand, inv_hand_invoke_mid, args_jobj);
1285 // Place result in stack args
1286 if (!self->IsExceptionPending()) {
1287 Object* result_ref = self->DecodeJObject(result);
1288 if (result_ref != NULL) {
1289 JValue result_unboxed;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001290 UnboxPrimitive(env, result_ref, proxy_mh.GetReturnType(), result_unboxed);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001291 *reinterpret_cast<JValue*>(stack_args) = result_unboxed;
1292 } else {
1293 *reinterpret_cast<jobject*>(stack_args) = NULL;
1294 }
Ian Rogers466bb252011-10-14 03:29:56 -07001295 } else {
1296 // In the case of checked exceptions that aren't declared, the exception must be wrapped by
1297 // a UndeclaredThrowableException.
1298 Throwable* exception = self->GetException();
1299 self->ClearException();
1300 if (!exception->IsCheckedException()) {
1301 self->SetException(exception);
1302 } else {
Ian Rogersc2b44472011-12-14 21:17:17 -08001303 SynthesizedProxyClass* proxy_class =
1304 down_cast<SynthesizedProxyClass*>(proxy_method->GetDeclaringClass());
1305 int throws_index = -1;
1306 size_t num_virt_methods = proxy_class->NumVirtualMethods();
1307 for (size_t i = 0; i < num_virt_methods; i++) {
1308 if (proxy_class->GetVirtualMethod(i) == proxy_method) {
1309 throws_index = i;
1310 break;
1311 }
1312 }
1313 CHECK_NE(throws_index, -1);
1314 ObjectArray<Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index);
Ian Rogers466bb252011-10-14 03:29:56 -07001315 Class* exception_class = exception->GetClass();
1316 bool declares_exception = false;
1317 for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
1318 Class* declared_exception = declared_exceptions->Get(i);
1319 declares_exception = declared_exception->IsAssignableFrom(exception_class);
1320 }
1321 if (declares_exception) {
1322 self->SetException(exception);
1323 } else {
1324 ThrowNewUndeclaredThrowableException(self, env, exception);
1325 }
1326 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001327 }
1328}
1329
jeffhaoe343b762011-12-05 16:36:44 -08001330extern "C" const void* artTraceMethodEntryFromCode(Method* method, Thread* self, uintptr_t lr) {
jeffhao2692b572011-12-16 15:42:28 -08001331 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001332 TraceStackFrame trace_frame = TraceStackFrame(method, lr);
1333 self->PushTraceStackFrame(trace_frame);
1334
jeffhao2692b572011-12-16 15:42:28 -08001335 tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceEnter);
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001336
jeffhao2692b572011-12-16 15:42:28 -08001337 return tracer->GetSavedCodeFromMap(method);
jeffhaoe343b762011-12-05 16:36:44 -08001338}
1339
1340extern "C" uintptr_t artTraceMethodExitFromCode() {
jeffhao2692b572011-12-16 15:42:28 -08001341 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001342 TraceStackFrame trace_frame = Thread::Current()->PopTraceStackFrame();
1343 Method* method = trace_frame.method_;
1344 uintptr_t lr = trace_frame.return_pc_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001345
jeffhao2692b572011-12-16 15:42:28 -08001346 tracer->LogMethodTraceEvent(Thread::Current(), method, Trace::kMethodTraceExit);
jeffhaoe343b762011-12-05 16:36:44 -08001347
1348 return lr;
1349}
1350
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001351uint32_t artTraceMethodUnwindFromCode(Thread* self) {
jeffhao2692b572011-12-16 15:42:28 -08001352 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001353 TraceStackFrame trace_frame = self->PopTraceStackFrame();
1354 Method* method = trace_frame.method_;
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001355 uint32_t lr = trace_frame.return_pc_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001356
jeffhao2692b572011-12-16 15:42:28 -08001357 tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceUnwind);
jeffhaoe343b762011-12-05 16:36:44 -08001358
1359 return lr;
1360}
1361
Shih-wei Liao2d831012011-09-28 22:06:53 -07001362/*
1363 * Float/double conversion requires clamping to min and max of integer form. If
1364 * target doesn't support this normally, use these.
1365 */
1366int64_t D2L(double d) {
1367 static const double kMaxLong = (double)(int64_t)0x7fffffffffffffffULL;
1368 static const double kMinLong = (double)(int64_t)0x8000000000000000ULL;
1369 if (d >= kMaxLong)
1370 return (int64_t)0x7fffffffffffffffULL;
1371 else if (d <= kMinLong)
1372 return (int64_t)0x8000000000000000ULL;
1373 else if (d != d) // NaN case
1374 return 0;
1375 else
1376 return (int64_t)d;
1377}
1378
1379int64_t F2L(float f) {
1380 static const float kMaxLong = (float)(int64_t)0x7fffffffffffffffULL;
1381 static const float kMinLong = (float)(int64_t)0x8000000000000000ULL;
1382 if (f >= kMaxLong)
1383 return (int64_t)0x7fffffffffffffffULL;
1384 else if (f <= kMinLong)
1385 return (int64_t)0x8000000000000000ULL;
1386 else if (f != f) // NaN case
1387 return 0;
1388 else
1389 return (int64_t)f;
1390}
1391
1392} // namespace art