blob: 337c084e6c4b45dd00565adaaf3e1ae0b1746f4e [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
Andreas Gampea14100c2017-04-24 15:09:56 -070019#include "nativehelper/jni_macros.h"
20
Mathieu Chartiere401d142015-04-22 13:56:20 -070021#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070022#include "base/enums.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070023#include "class_linker.h"
Vladimir Markoa8bba7d2018-05-30 15:18:48 +010024#include "class_root.h"
David Sehr9e734c72018-01-04 17:56:19 -080025#include "dex/dex_file_annotations.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010026#include "jni/jni_internal.h"
Andreas Gampe70f5fd02018-10-24 19:58:37 -070027#include "mirror/class-alloc-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "mirror/class-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070029#include "mirror/method.h"
Andreas Gampe52ecb652018-10-24 15:18:21 -070030#include "mirror/object_array-alloc-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031#include "mirror/object-inl.h"
Andreas Gampe87583b32017-05-25 11:22:18 -070032#include "native_util.h"
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070033#include "reflection.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070034#include "scoped_fast_native_object_access-inl.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070035#include "well_known_classes.h"
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070036
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070037namespace art {
38
Jeff Hao13e748b2015-08-25 20:44:19 +000039static jobjectArray Constructor_getExceptionTypes(JNIEnv* env, jobject javaMethod) {
40 ScopedFastNativeObjectAccess soa(env);
Alex Light52c9da02016-05-09 15:31:18 -070041 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod)
Andreas Gampe542451c2016-07-26 09:02:02 -070042 ->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Vladimir Marko2d3065e2018-05-22 13:56:09 +010043 ObjPtr<mirror::ObjectArray<mirror::Class>> result_array =
David Sehr9323e6e2016-09-13 08:58:35 -070044 annotations::GetExceptionTypesForMethod(method);
Jeff Hao13e748b2015-08-25 20:44:19 +000045 if (result_array == nullptr) {
46 // Return an empty array instead of a null pointer.
Vladimir Markoa8bba7d2018-05-30 15:18:48 +010047 ObjPtr<mirror::Class> class_array_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>();
48 DCHECK(class_array_class != nullptr);
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070049 ObjPtr<mirror::ObjectArray<mirror::Class>> empty_array =
Jeff Hao2a5892f2015-08-31 15:00:40 -070050 mirror::ObjectArray<mirror::Class>::Alloc(soa.Self(), class_array_class, 0);
Jeff Hao13e748b2015-08-25 20:44:19 +000051 return soa.AddLocalReference<jobjectArray>(empty_array);
52 } else {
53 return soa.AddLocalReference<jobjectArray>(result_array);
54 }
55}
56
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -070057/*
Andreas Gampe8208bdd2015-04-27 17:26:37 -070058 * We can also safely assume the constructor isn't associated
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -070059 * with an interface, array, or primitive class. If this is coming from
60 * native, it is OK to avoid access checks since JNI does not enforce them.
61 */
Przemyslaw Szczepaniakdcf1b592015-10-12 16:34:14 +010062static jobject Constructor_newInstance0(JNIEnv* env, jobject javaMethod, jobjectArray javaArgs) {
Ian Rogers53b8b092014-03-13 23:45:53 -070063 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -070064 ObjPtr<mirror::Constructor> m = soa.Decode<mirror::Constructor>(javaMethod);
Andreas Gampe8ad7a3b2017-05-22 16:08:52 -070065 ArtMethod* constructor_art_method = m->GetArtMethod();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070066 StackHandleScope<1> hs(soa.Self());
67 Handle<mirror::Class> c(hs.NewHandle(m->GetDeclaringClass()));
Ian Rogers62d6c772013-02-27 08:32:07 -080068 if (UNLIKELY(c->IsAbstract())) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -070069 soa.Self()->ThrowNewExceptionF("Ljava/lang/InstantiationException;", "Can't instantiate %s %s",
Ian Rogers62d6c772013-02-27 08:32:07 -080070 c->IsInterface() ? "interface" : "abstract class",
David Sehr709b0702016-10-13 09:12:37 -070071 c->PrettyDescriptor().c_str());
Mathieu Chartierc528dba2013-11-26 12:00:11 -080072 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070073 }
Andreas Gampe8208bdd2015-04-27 17:26:37 -070074 // Verify that we can access the class.
Sebastien Hertz2d2f2a92015-04-28 15:00:41 +020075 if (!m->IsAccessible() && !c->IsPublic()) {
Przemyslaw Szczepaniak3ddd5932015-11-23 16:08:03 +000076 // Go 2 frames back, this method is always called from newInstance0, which is called from
Narayan Kamathe0915822015-11-18 13:00:18 +000077 // Constructor.newInstance(Object... args).
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070078 ObjPtr<mirror::Class> caller = GetCallingClass(soa.Self(), 2);
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -070079 // If caller is null, then we called from JNI, just avoid the check since JNI avoids most
80 // access checks anyways. TODO: Investigate if this the correct behavior.
81 if (caller != nullptr && !caller->CanAccess(c.Get())) {
David Sehr709b0702016-10-13 09:12:37 -070082 if (c->PrettyDescriptor() == "dalvik.system.DexPathList$Element") {
Andreas Gampe61d7ca82015-04-29 19:56:36 -070083 // b/20699073.
84 LOG(WARNING) << "The dalvik.system.DexPathList$Element constructor is not accessible by "
85 "default. This is a temporary workaround for backwards compatibility "
86 "with class-loader hacks. Please update your application.";
87 } else {
88 soa.Self()->ThrowNewExceptionF(
89 "Ljava/lang/IllegalAccessException;", "%s is not accessible from %s",
David Sehr709b0702016-10-13 09:12:37 -070090 c->PrettyClass().c_str(),
91 caller->PrettyClass().c_str());
Andreas Gampe61d7ca82015-04-29 19:56:36 -070092 return nullptr;
93 }
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -070094 }
95 }
Ian Rogers7b078e82014-09-10 14:44:24 -070096 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(soa.Self(), c, true, true)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070097 DCHECK(soa.Self()->IsExceptionPending());
Mathieu Chartierc528dba2013-11-26 12:00:11 -080098 return nullptr;
Brian Carlstrom5d40f182011-09-26 22:29:18 -070099 }
Mathieu Chartierf4b97622014-03-10 13:26:27 -0700100 bool movable = true;
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700101 if (!kMovingClasses && c->IsClassClass()) {
Mathieu Chartierf4b97622014-03-10 13:26:27 -0700102 movable = false;
103 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800104
105 // String constructor is replaced by a StringFactory method in InvokeMethod.
Andreas Gampe8ad7a3b2017-05-22 16:08:52 -0700106 if (UNLIKELY(c->IsStringClass())) {
Narayan Kamathe0915822015-11-18 13:00:18 +0000107 return InvokeMethod(soa, javaMethod, nullptr, javaArgs, 2);
Jeff Hao848f70a2014-01-15 13:49:50 -0800108 }
109
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700110 ObjPtr<mirror::Object> receiver =
Mathieu Chartierf4b97622014-03-10 13:26:27 -0700111 movable ? c->AllocObject(soa.Self()) : c->AllocNonMovableObject(soa.Self());
Andreas Gampe8ad7a3b2017-05-22 16:08:52 -0700112 if (UNLIKELY(receiver == nullptr)) {
113 DCHECK(soa.Self()->IsExceptionPending());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800114 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700115 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700116 jobject javaReceiver = soa.AddLocalReference<jobject>(receiver);
Andreas Gampe8ad7a3b2017-05-22 16:08:52 -0700117
118 InvokeConstructor(soa, constructor_art_method, receiver, javaArgs);
119
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700120 return javaReceiver;
121}
122
Przemyslaw Szczepaniakdcf1b592015-10-12 16:34:14 +0100123static jobject Constructor_newInstanceFromSerialization(JNIEnv* env, jclass unused ATTRIBUTE_UNUSED,
124 jclass ctorClass, jclass allocClass) {
125 jmethodID ctor = env->GetMethodID(ctorClass, "<init>", "()V");
Yi Kong4b22b342018-08-02 14:43:21 -0700126 DCHECK(ctor != nullptr);
Przemyslaw Szczepaniakdcf1b592015-10-12 16:34:14 +0100127 return env->NewObject(allocClass, ctor);
128}
129
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700130static JNINativeMethod gMethods[] = {
Igor Murashkin3b6f4402017-02-16 16:13:17 -0800131 FAST_NATIVE_METHOD(Constructor, getExceptionTypes, "()[Ljava/lang/Class;"),
132 FAST_NATIVE_METHOD(Constructor, newInstance0, "([Ljava/lang/Object;)Ljava/lang/Object;"),
133 FAST_NATIVE_METHOD(Constructor, newInstanceFromSerialization, "(Ljava/lang/Class;Ljava/lang/Class;)Ljava/lang/Object;"),
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700134};
135
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700136void register_java_lang_reflect_Constructor(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700137 REGISTER_NATIVE_METHODS("java/lang/reflect/Constructor");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700138}
139
140} // namespace art