blob: e54674f72b24779d610b989e8931e91c518166be [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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "mirror/class-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070028#include "mirror/method.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "mirror/object-inl.h"
Andreas Gampe87583b32017-05-25 11:22:18 -070030#include "native_util.h"
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070031#include "reflection.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070032#include "scoped_fast_native_object_access-inl.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070033#include "well_known_classes.h"
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070034
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070035namespace art {
36
Jeff Hao13e748b2015-08-25 20:44:19 +000037static jobjectArray Constructor_getExceptionTypes(JNIEnv* env, jobject javaMethod) {
38 ScopedFastNativeObjectAccess soa(env);
Alex Light52c9da02016-05-09 15:31:18 -070039 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod)
Andreas Gampe542451c2016-07-26 09:02:02 -070040 ->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Vladimir Marko2d3065e2018-05-22 13:56:09 +010041 ObjPtr<mirror::ObjectArray<mirror::Class>> result_array =
David Sehr9323e6e2016-09-13 08:58:35 -070042 annotations::GetExceptionTypesForMethod(method);
Jeff Hao13e748b2015-08-25 20:44:19 +000043 if (result_array == nullptr) {
44 // Return an empty array instead of a null pointer.
Vladimir Markoa8bba7d2018-05-30 15:18:48 +010045 ObjPtr<mirror::Class> class_array_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>();
46 DCHECK(class_array_class != nullptr);
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070047 ObjPtr<mirror::ObjectArray<mirror::Class>> empty_array =
Jeff Hao2a5892f2015-08-31 15:00:40 -070048 mirror::ObjectArray<mirror::Class>::Alloc(soa.Self(), class_array_class, 0);
Jeff Hao13e748b2015-08-25 20:44:19 +000049 return soa.AddLocalReference<jobjectArray>(empty_array);
50 } else {
51 return soa.AddLocalReference<jobjectArray>(result_array);
52 }
53}
54
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -070055/*
Andreas Gampe8208bdd2015-04-27 17:26:37 -070056 * We can also safely assume the constructor isn't associated
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -070057 * with an interface, array, or primitive class. If this is coming from
58 * native, it is OK to avoid access checks since JNI does not enforce them.
59 */
Przemyslaw Szczepaniakdcf1b592015-10-12 16:34:14 +010060static jobject Constructor_newInstance0(JNIEnv* env, jobject javaMethod, jobjectArray javaArgs) {
Ian Rogers53b8b092014-03-13 23:45:53 -070061 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -070062 ObjPtr<mirror::Constructor> m = soa.Decode<mirror::Constructor>(javaMethod);
Andreas Gampe8ad7a3b2017-05-22 16:08:52 -070063 ArtMethod* constructor_art_method = m->GetArtMethod();
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.
Andreas Gampe8ad7a3b2017-05-22 16:08:52 -0700104 if (UNLIKELY(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());
Andreas Gampe8ad7a3b2017-05-22 16:08:52 -0700110 if (UNLIKELY(receiver == nullptr)) {
111 DCHECK(soa.Self()->IsExceptionPending());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800112 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700113 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700114 jobject javaReceiver = soa.AddLocalReference<jobject>(receiver);
Andreas Gampe8ad7a3b2017-05-22 16:08:52 -0700115
116 InvokeConstructor(soa, constructor_art_method, receiver, javaArgs);
117
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700118 return javaReceiver;
119}
120
Przemyslaw Szczepaniakdcf1b592015-10-12 16:34:14 +0100121static jobject Constructor_newInstanceFromSerialization(JNIEnv* env, jclass unused ATTRIBUTE_UNUSED,
122 jclass ctorClass, jclass allocClass) {
123 jmethodID ctor = env->GetMethodID(ctorClass, "<init>", "()V");
124 DCHECK(ctor != NULL);
125 return env->NewObject(allocClass, ctor);
126}
127
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700128static JNINativeMethod gMethods[] = {
Igor Murashkin3b6f4402017-02-16 16:13:17 -0800129 FAST_NATIVE_METHOD(Constructor, getExceptionTypes, "()[Ljava/lang/Class;"),
130 FAST_NATIVE_METHOD(Constructor, newInstance0, "([Ljava/lang/Object;)Ljava/lang/Object;"),
131 FAST_NATIVE_METHOD(Constructor, newInstanceFromSerialization, "(Ljava/lang/Class;Ljava/lang/Class;)Ljava/lang/Object;"),
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700132};
133
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700134void register_java_lang_reflect_Constructor(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700135 REGISTER_NATIVE_METHODS("java/lang/reflect/Constructor");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700136}
137
138} // namespace art