blob: fb780463ff3f0b561e5f5f5d72326be798b450ea [file] [log] [blame]
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Andreas Gampe277ccbd2014-11-03 21:36:10 -080017#include "java_lang_reflect_Constructor.h"
18
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070020#include "base/enums.h"
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070021#include "class_linker.h"
Jeff Hao13e748b2015-08-25 20:44:19 +000022#include "class_linker-inl.h"
David Sehr9323e6e2016-09-13 08:58:35 -070023#include "dex_file_annotations.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070024#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "mirror/class-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070026#include "mirror/method.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "mirror/object-inl.h"
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070028#include "reflection.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070029#include "scoped_fast_native_object_access-inl.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070030#include "well_known_classes.h"
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070031
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070032namespace art {
33
Jeff Hao13e748b2015-08-25 20:44:19 +000034static jobjectArray Constructor_getExceptionTypes(JNIEnv* env, jobject javaMethod) {
35 ScopedFastNativeObjectAccess soa(env);
Alex Light52c9da02016-05-09 15:31:18 -070036 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod)
Andreas Gampe542451c2016-07-26 09:02:02 -070037 ->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Jeff Hao2a5892f2015-08-31 15:00:40 -070038 mirror::ObjectArray<mirror::Class>* result_array =
David Sehr9323e6e2016-09-13 08:58:35 -070039 annotations::GetExceptionTypesForMethod(method);
Jeff Hao13e748b2015-08-25 20:44:19 +000040 if (result_array == nullptr) {
41 // Return an empty array instead of a null pointer.
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070042 ObjPtr<mirror::Class> class_class = mirror::Class::GetJavaLangClass();
43 ObjPtr<mirror::Class> class_array_class =
Jeff Hao13e748b2015-08-25 20:44:19 +000044 Runtime::Current()->GetClassLinker()->FindArrayClass(soa.Self(), &class_class);
Jeff Hao2a5892f2015-08-31 15:00:40 -070045 if (class_array_class == nullptr) {
46 return nullptr;
47 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070048 ObjPtr<mirror::ObjectArray<mirror::Class>> empty_array =
Jeff Hao2a5892f2015-08-31 15:00:40 -070049 mirror::ObjectArray<mirror::Class>::Alloc(soa.Self(), class_array_class, 0);
Jeff Hao13e748b2015-08-25 20:44:19 +000050 return soa.AddLocalReference<jobjectArray>(empty_array);
51 } else {
52 return soa.AddLocalReference<jobjectArray>(result_array);
53 }
54}
55
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -070056/*
Andreas Gampe8208bdd2015-04-27 17:26:37 -070057 * We can also safely assume the constructor isn't associated
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -070058 * with an interface, array, or primitive class. If this is coming from
59 * native, it is OK to avoid access checks since JNI does not enforce them.
60 */
Przemyslaw Szczepaniakdcf1b592015-10-12 16:34:14 +010061static jobject Constructor_newInstance0(JNIEnv* env, jobject javaMethod, jobjectArray javaArgs) {
Ian Rogers53b8b092014-03-13 23:45:53 -070062 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -070063 ObjPtr<mirror::Constructor> m = soa.Decode<mirror::Constructor>(javaMethod);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070064 StackHandleScope<1> hs(soa.Self());
65 Handle<mirror::Class> c(hs.NewHandle(m->GetDeclaringClass()));
Ian Rogers62d6c772013-02-27 08:32:07 -080066 if (UNLIKELY(c->IsAbstract())) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -070067 soa.Self()->ThrowNewExceptionF("Ljava/lang/InstantiationException;", "Can't instantiate %s %s",
Ian Rogers62d6c772013-02-27 08:32:07 -080068 c->IsInterface() ? "interface" : "abstract class",
David Sehr709b0702016-10-13 09:12:37 -070069 c->PrettyDescriptor().c_str());
Mathieu Chartierc528dba2013-11-26 12:00:11 -080070 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070071 }
Andreas Gampe8208bdd2015-04-27 17:26:37 -070072 // Verify that we can access the class.
Sebastien Hertz2d2f2a92015-04-28 15:00:41 +020073 if (!m->IsAccessible() && !c->IsPublic()) {
Przemyslaw Szczepaniak3ddd5932015-11-23 16:08:03 +000074 // Go 2 frames back, this method is always called from newInstance0, which is called from
Narayan Kamathe0915822015-11-18 13:00:18 +000075 // Constructor.newInstance(Object... args).
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070076 ObjPtr<mirror::Class> caller = GetCallingClass(soa.Self(), 2);
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -070077 // If caller is null, then we called from JNI, just avoid the check since JNI avoids most
78 // access checks anyways. TODO: Investigate if this the correct behavior.
79 if (caller != nullptr && !caller->CanAccess(c.Get())) {
David Sehr709b0702016-10-13 09:12:37 -070080 if (c->PrettyDescriptor() == "dalvik.system.DexPathList$Element") {
Andreas Gampe61d7ca82015-04-29 19:56:36 -070081 // b/20699073.
82 LOG(WARNING) << "The dalvik.system.DexPathList$Element constructor is not accessible by "
83 "default. This is a temporary workaround for backwards compatibility "
84 "with class-loader hacks. Please update your application.";
85 } else {
86 soa.Self()->ThrowNewExceptionF(
87 "Ljava/lang/IllegalAccessException;", "%s is not accessible from %s",
David Sehr709b0702016-10-13 09:12:37 -070088 c->PrettyClass().c_str(),
89 caller->PrettyClass().c_str());
Andreas Gampe61d7ca82015-04-29 19:56:36 -070090 return nullptr;
91 }
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -070092 }
93 }
Ian Rogers7b078e82014-09-10 14:44:24 -070094 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(soa.Self(), c, true, true)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070095 DCHECK(soa.Self()->IsExceptionPending());
Mathieu Chartierc528dba2013-11-26 12:00:11 -080096 return nullptr;
Brian Carlstrom5d40f182011-09-26 22:29:18 -070097 }
Mathieu Chartierf4b97622014-03-10 13:26:27 -070098 bool movable = true;
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -070099 if (!kMovingClasses && c->IsClassClass()) {
Mathieu Chartierf4b97622014-03-10 13:26:27 -0700100 movable = false;
101 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800102
103 // String constructor is replaced by a StringFactory method in InvokeMethod.
104 if (c->IsStringClass()) {
Narayan Kamathe0915822015-11-18 13:00:18 +0000105 return InvokeMethod(soa, javaMethod, nullptr, javaArgs, 2);
Jeff Hao848f70a2014-01-15 13:49:50 -0800106 }
107
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700108 ObjPtr<mirror::Object> receiver =
Mathieu Chartierf4b97622014-03-10 13:26:27 -0700109 movable ? c->AllocObject(soa.Self()) : c->AllocNonMovableObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800110 if (receiver == nullptr) {
111 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700112 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700113 jobject javaReceiver = soa.AddLocalReference<jobject>(receiver);
Narayan Kamathe0915822015-11-18 13:00:18 +0000114 InvokeMethod(soa, javaMethod, javaReceiver, javaArgs, 2);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700115 // Constructors are ()V methods, so we shouldn't touch the result of InvokeMethod.
116 return javaReceiver;
117}
118
Przemyslaw Szczepaniakdcf1b592015-10-12 16:34:14 +0100119static jobject Constructor_newInstanceFromSerialization(JNIEnv* env, jclass unused ATTRIBUTE_UNUSED,
120 jclass ctorClass, jclass allocClass) {
121 jmethodID ctor = env->GetMethodID(ctorClass, "<init>", "()V");
122 DCHECK(ctor != NULL);
123 return env->NewObject(allocClass, ctor);
124}
125
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700126static JNINativeMethod gMethods[] = {
Igor Murashkin3b6f4402017-02-16 16:13:17 -0800127 FAST_NATIVE_METHOD(Constructor, getExceptionTypes, "()[Ljava/lang/Class;"),
128 FAST_NATIVE_METHOD(Constructor, newInstance0, "([Ljava/lang/Object;)Ljava/lang/Object;"),
129 FAST_NATIVE_METHOD(Constructor, newInstanceFromSerialization, "(Ljava/lang/Class;Ljava/lang/Class;)Ljava/lang/Object;"),
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700130};
131
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700132void register_java_lang_reflect_Constructor(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700133 REGISTER_NATIVE_METHODS("java/lang/reflect/Constructor");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700134}
135
136} // namespace art