blob: d063dfb425120f62920d36f6c99f6a4792fc84e7 [file] [log] [blame]
Shih-wei Liao2d831012011-09-28 22:06:53 -07001/*
Elliott Hughes0f3c5532012-03-30 14:51:51 -07002 * Copyright (C) 2012 The Android Open Source Project
Shih-wei Liao2d831012011-09-28 22:06:53 -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 Rogers7655f292013-07-29 11:07:13 -070017#include "entrypoints/entrypoint_utils.h"
Shih-wei Liao2d831012011-09-28 22:06:53 -070018
Mingyao Yang98d1cc82014-05-15 17:02:16 -070019#include "base/mutex.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080020#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070021#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070022#include "gc/accounting/card_table-inl.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070023#include "mirror/art_field-inl.h"
24#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "mirror/object-inl.h"
27#include "mirror/object_array-inl.h"
Ian Rogersaf6e67a2013-01-16 08:38:37 -080028#include "reflection.h"
29#include "scoped_thread_state_change.h"
TDYa1275bb86012012-04-11 05:57:28 -070030#include "ScopedLocalRef.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070031#include "well_known_classes.h"
TDYa1275bb86012012-04-11 05:57:28 -070032
jeffhao41005dd2012-05-09 17:58:52 -070033namespace art {
34
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080035static inline mirror::Class* CheckFilledNewArrayAlloc(uint32_t type_idx, mirror::ArtMethod* referrer,
36 int32_t component_count, Thread* self,
37 bool access_check)
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070038 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -070039 if (UNLIKELY(component_count < 0)) {
Ian Rogers62d6c772013-02-27 08:32:07 -080040 ThrowNegativeArraySizeException(component_count);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080041 return nullptr; // Failure
Elliott Hughes6c8867d2011-10-03 16:34:05 -070042 }
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070043 mirror::Class* klass = referrer->GetDexCacheResolvedTypes()->GetWithoutChecks(type_idx);
Ian Rogers57b86d42012-03-27 16:05:41 -070044 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
Ian Rogers62d6c772013-02-27 08:32:07 -080045 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, referrer);
Ian Rogers57b86d42012-03-27 16:05:41 -070046 if (klass == NULL) { // Error
Ian Rogers50b35e22012-10-04 10:09:15 -070047 DCHECK(self->IsExceptionPending());
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080048 return nullptr; // Failure
Ian Rogers19846512012-02-24 11:42:47 -080049 }
Ian Rogersea2a11d2011-10-11 16:48:51 -070050 }
Ian Rogers57b86d42012-03-27 16:05:41 -070051 if (UNLIKELY(klass->IsPrimitive() && !klass->IsPrimitiveInt())) {
52 if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) {
Ian Rogers62d6c772013-02-27 08:32:07 -080053 ThrowRuntimeException("Bad filled array request for type %s",
54 PrettyDescriptor(klass).c_str());
Ian Rogers573db4a2011-12-13 15:30:50 -080055 } else {
Ian Rogers62d6c772013-02-27 08:32:07 -080056 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
57 DCHECK(throw_location.GetMethod() == referrer);
58 self->ThrowNewExceptionF(throw_location, "Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -080059 "Found type %s; filled-new-array not implemented for anything but 'int'",
Ian Rogers50b35e22012-10-04 10:09:15 -070060 PrettyDescriptor(klass).c_str());
Ian Rogers573db4a2011-12-13 15:30:50 -080061 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080062 return nullptr; // Failure
Ian Rogers57b86d42012-03-27 16:05:41 -070063 }
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070064 if (access_check) {
65 mirror::Class* referrer_klass = referrer->GetDeclaringClass();
66 if (UNLIKELY(!referrer_klass->CanAccess(klass))) {
67 ThrowIllegalAccessErrorClass(referrer_klass, klass);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080068 return nullptr; // Failure
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070069 }
70 }
71 DCHECK(klass->IsArrayClass()) << PrettyClass(klass);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080072 return klass;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070073}
74
75// Helper function to allocate array for FILLED_NEW_ARRAY.
76mirror::Array* CheckAndAllocArrayFromCode(uint32_t type_idx, mirror::ArtMethod* referrer,
77 int32_t component_count, Thread* self,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080078 bool access_check,
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080079 gc::AllocatorType /* allocator_type */) {
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080080 mirror::Class* klass = CheckFilledNewArrayAlloc(type_idx, referrer, component_count, self,
81 access_check);
82 if (UNLIKELY(klass == nullptr)) {
83 return nullptr;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070084 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080085 // Always go slow path for now, filled new array is not common.
86 gc::Heap* heap = Runtime::Current()->GetHeap();
87 // Use the current allocator type in case CheckFilledNewArrayAlloc caused us to suspend and then
88 // the heap switched the allocator type while we were suspended.
Ian Rogers6fac4472014-02-25 17:01:10 -080089 return mirror::Array::Alloc<false>(self, klass, component_count, klass->GetComponentSize(),
90 heap->GetCurrentAllocator());
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070091}
92
93// Helper function to allocate array for FILLED_NEW_ARRAY.
94mirror::Array* CheckAndAllocArrayFromCodeInstrumented(uint32_t type_idx, mirror::ArtMethod* referrer,
95 int32_t component_count, Thread* self,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080096 bool access_check,
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080097 gc::AllocatorType /* allocator_type */) {
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080098 mirror::Class* klass = CheckFilledNewArrayAlloc(type_idx, referrer, component_count, self,
99 access_check);
100 if (UNLIKELY(klass == nullptr)) {
101 return nullptr;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700102 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800103 gc::Heap* heap = Runtime::Current()->GetHeap();
104 // Use the current allocator type in case CheckFilledNewArrayAlloc caused us to suspend and then
105 // the heap switched the allocator type while we were suspended.
Ian Rogers6fac4472014-02-25 17:01:10 -0800106 return mirror::Array::Alloc<true>(self, klass, component_count, klass->GetComponentSize(),
107 heap->GetCurrentAllocator());
Ian Rogers57b86d42012-03-27 16:05:41 -0700108}
109
jeffhaod7521322012-11-21 15:38:24 -0800110void ThrowStackOverflowError(Thread* self) {
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700111 if (self->IsHandlingStackOverflow()) {
112 LOG(ERROR) << "Recursive stack overflow.";
113 // We don't fail here because SetStackEndForStackOverflow will print better diagnostics.
114 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800115
116 if (Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()) {
117 // Remove extra entry pushed onto second stack during method tracing.
118 Runtime::Current()->GetInstrumentation()->PopMethodForUnwind(self, false);
jeffhaod7521322012-11-21 15:38:24 -0800119 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800120
jeffhaod7521322012-11-21 15:38:24 -0800121 self->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute.
122 JNIEnvExt* env = self->GetJniEnv();
123 std::string msg("stack size ");
124 msg += PrettySize(self->GetStackSize());
125 // Use low-level JNI routine and pre-baked error class to avoid class linking operations that
126 // would consume more stack.
127 int rc = ::art::ThrowNewException(env, WellKnownClasses::java_lang_StackOverflowError,
128 msg.c_str(), NULL);
129 if (rc != JNI_OK) {
130 // TODO: ThrowNewException failed presumably because of an OOME, we continue to throw the OOME
131 // or die in the CHECK below. We may want to throw a pre-baked StackOverflowError
132 // instead.
133 LOG(ERROR) << "Couldn't throw new StackOverflowError because JNI ThrowNew failed.";
134 CHECK(self->IsExceptionPending());
135 }
Dave Allisonf9439142014-03-27 15:10:22 -0700136
137 bool explicit_overflow_check = Runtime::Current()->ExplicitStackOverflowChecks();
138 self->ResetDefaultStackEnd(!explicit_overflow_check); // Return to default stack size.
jeffhaod7521322012-11-21 15:38:24 -0800139}
140
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700141JValue InvokeProxyInvocationHandler(ScopedObjectAccessAlreadyRunnable& soa, const char* shorty,
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800142 jobject rcvr_jobj, jobject interface_method_jobj,
143 std::vector<jvalue>& args) {
144 DCHECK(soa.Env()->IsInstanceOf(rcvr_jobj, WellKnownClasses::java_lang_reflect_Proxy));
145
146 // Build argument array possibly triggering GC.
147 soa.Self()->AssertThreadSuspensionIsAllowable();
148 jobjectArray args_jobj = NULL;
149 const JValue zero;
Jeff Haof00571c2014-05-29 17:29:47 -0700150 int32_t target_sdk_version = Runtime::Current()->GetTargetSdkVersion();
151 // Do not create empty arrays unless needed to maintain Dalvik bug compatibility.
152 if (args.size() > 0 || (target_sdk_version > 0 && target_sdk_version <= 21)) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800153 args_jobj = soa.Env()->NewObjectArray(args.size(), WellKnownClasses::java_lang_Object, NULL);
154 if (args_jobj == NULL) {
155 CHECK(soa.Self()->IsExceptionPending());
156 return zero;
157 }
158 for (size_t i = 0; i < args.size(); ++i) {
159 if (shorty[i + 1] == 'L') {
160 jobject val = args.at(i).l;
161 soa.Env()->SetObjectArrayElement(args_jobj, i, val);
162 } else {
163 JValue jv;
164 jv.SetJ(args.at(i).j);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800165 mirror::Object* val = BoxPrimitive(Primitive::GetType(shorty[i + 1]), jv);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800166 if (val == NULL) {
167 CHECK(soa.Self()->IsExceptionPending());
168 return zero;
169 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100170 soa.Decode<mirror::ObjectArray<mirror::Object>* >(args_jobj)->Set<false>(i, val);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800171 }
172 }
173 }
174
Brian Carlstromea46f952013-07-30 01:26:50 -0700175 // Call Proxy.invoke(Proxy proxy, ArtMethod method, Object[] args).
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800176 jvalue invocation_args[3];
177 invocation_args[0].l = rcvr_jobj;
178 invocation_args[1].l = interface_method_jobj;
179 invocation_args[2].l = args_jobj;
180 jobject result =
Brian Carlstromea46f952013-07-30 01:26:50 -0700181 soa.Env()->CallStaticObjectMethodA(WellKnownClasses::java_lang_reflect_Proxy,
182 WellKnownClasses::java_lang_reflect_Proxy_invoke,
183 invocation_args);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800184
185 // Unbox result and handle error conditions.
Ian Rogers62d6c772013-02-27 08:32:07 -0800186 if (LIKELY(!soa.Self()->IsExceptionPending())) {
187 if (shorty[0] == 'V' || (shorty[0] == 'L' && result == NULL)) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800188 // Do nothing.
189 return zero;
190 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700191 StackHandleScope<1> hs(soa.Self());
192 MethodHelper mh_interface_method(
193 hs.NewHandle(soa.Decode<mirror::ArtMethod*>(interface_method_jobj)));
194 // This can cause thread suspension.
195 mirror::Class* result_type = mh_interface_method.GetReturnType();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800196 mirror::Object* result_ref = soa.Decode<mirror::Object*>(result);
Ian Rogers62d6c772013-02-27 08:32:07 -0800197 mirror::Object* rcvr = soa.Decode<mirror::Object*>(rcvr_jobj);
Brian Carlstromea46f952013-07-30 01:26:50 -0700198 mirror::ArtMethod* proxy_method;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700199 if (mh_interface_method.GetMethod()->GetDeclaringClass()->IsInterface()) {
200 proxy_method = rcvr->GetClass()->FindVirtualMethodForInterface(
201 mh_interface_method.GetMethod());
Ian Rogers62d6c772013-02-27 08:32:07 -0800202 } else {
203 // Proxy dispatch to a method defined in Object.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700204 DCHECK(mh_interface_method.GetMethod()->GetDeclaringClass()->IsObjectClass());
205 proxy_method = mh_interface_method.GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800206 }
207 ThrowLocation throw_location(rcvr, proxy_method, -1);
208 JValue result_unboxed;
Ian Rogers84956ff2014-03-26 23:52:41 -0700209 if (!UnboxPrimitiveForResult(throw_location, result_ref, result_type, &result_unboxed)) {
Ian Rogers530f71c2013-02-22 23:29:00 -0800210 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800211 return zero;
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800212 }
213 return result_unboxed;
214 }
215 } else {
216 // In the case of checked exceptions that aren't declared, the exception must be wrapped by
217 // a UndeclaredThrowableException.
Ian Rogers62d6c772013-02-27 08:32:07 -0800218 mirror::Throwable* exception = soa.Self()->GetException(NULL);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800219 if (exception->IsCheckedException()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800220 mirror::Object* rcvr = soa.Decode<mirror::Object*>(rcvr_jobj);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700221 mirror::Class* proxy_class = rcvr->GetClass();
Brian Carlstromea46f952013-07-30 01:26:50 -0700222 mirror::ArtMethod* interface_method =
223 soa.Decode<mirror::ArtMethod*>(interface_method_jobj);
224 mirror::ArtMethod* proxy_method =
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800225 rcvr->GetClass()->FindVirtualMethodForInterface(interface_method);
226 int throws_index = -1;
227 size_t num_virt_methods = proxy_class->NumVirtualMethods();
228 for (size_t i = 0; i < num_virt_methods; i++) {
229 if (proxy_class->GetVirtualMethod(i) == proxy_method) {
230 throws_index = i;
231 break;
232 }
233 }
234 CHECK_NE(throws_index, -1);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800235 mirror::ObjectArray<mirror::Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index);
236 mirror::Class* exception_class = exception->GetClass();
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800237 bool declares_exception = false;
238 for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800239 mirror::Class* declared_exception = declared_exceptions->Get(i);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800240 declares_exception = declared_exception->IsAssignableFrom(exception_class);
241 }
242 if (!declares_exception) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800243 ThrowLocation throw_location(rcvr, proxy_method, -1);
244 soa.Self()->ThrowNewWrappedException(throw_location,
245 "Ljava/lang/reflect/UndeclaredThrowableException;",
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800246 NULL);
247 }
248 }
249 return zero;
250 }
251}
Shih-wei Liao2d831012011-09-28 22:06:53 -0700252} // namespace art