Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 1 | /* |
| 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 Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 17 | #include "java_lang_reflect_Constructor.h" |
| 18 | |
Andreas Gampe | a14100c | 2017-04-24 15:09:56 -0700 | [diff] [blame] | 19 | #include "nativehelper/jni_macros.h" |
| 20 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 21 | #include "art_method-inl.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 22 | #include "base/enums.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 23 | #include "class_linker.h" |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 24 | #include "class_root.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 25 | #include "dex/dex_file_annotations.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 26 | #include "jni/jni_internal.h" |
Andreas Gampe | 70f5fd0 | 2018-10-24 19:58:37 -0700 | [diff] [blame] | 27 | #include "mirror/class-alloc-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 28 | #include "mirror/class-inl.h" |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 29 | #include "mirror/method.h" |
Andreas Gampe | 52ecb65 | 2018-10-24 15:18:21 -0700 | [diff] [blame] | 30 | #include "mirror/object_array-alloc-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 31 | #include "mirror/object-inl.h" |
Andreas Gampe | 87583b3 | 2017-05-25 11:22:18 -0700 | [diff] [blame] | 32 | #include "native_util.h" |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 33 | #include "reflection.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 34 | #include "scoped_fast_native_object_access-inl.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 35 | #include "well_known_classes.h" |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 36 | |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 37 | namespace art { |
| 38 | |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 39 | static jobjectArray Constructor_getExceptionTypes(JNIEnv* env, jobject javaMethod) { |
| 40 | ScopedFastNativeObjectAccess soa(env); |
Alex Light | 52c9da0 | 2016-05-09 15:31:18 -0700 | [diff] [blame] | 41 | ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod) |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 42 | ->GetInterfaceMethodIfProxy(kRuntimePointerSize); |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 43 | ObjPtr<mirror::ObjectArray<mirror::Class>> result_array = |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 44 | annotations::GetExceptionTypesForMethod(method); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 45 | if (result_array == nullptr) { |
| 46 | // Return an empty array instead of a null pointer. |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 47 | ObjPtr<mirror::Class> class_array_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(); |
| 48 | DCHECK(class_array_class != nullptr); |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 49 | ObjPtr<mirror::ObjectArray<mirror::Class>> empty_array = |
Jeff Hao | 2a5892f | 2015-08-31 15:00:40 -0700 | [diff] [blame] | 50 | mirror::ObjectArray<mirror::Class>::Alloc(soa.Self(), class_array_class, 0); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 51 | return soa.AddLocalReference<jobjectArray>(empty_array); |
| 52 | } else { |
| 53 | return soa.AddLocalReference<jobjectArray>(result_array); |
| 54 | } |
| 55 | } |
| 56 | |
Mathieu Chartier | f36cb5f | 2015-04-24 16:55:16 -0700 | [diff] [blame] | 57 | /* |
Andreas Gampe | 8208bdd | 2015-04-27 17:26:37 -0700 | [diff] [blame] | 58 | * We can also safely assume the constructor isn't associated |
Mathieu Chartier | f36cb5f | 2015-04-24 16:55:16 -0700 | [diff] [blame] | 59 | * 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 Szczepaniak | dcf1b59 | 2015-10-12 16:34:14 +0100 | [diff] [blame] | 62 | static jobject Constructor_newInstance0(JNIEnv* env, jobject javaMethod, jobjectArray javaArgs) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 63 | ScopedFastNativeObjectAccess soa(env); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 64 | ObjPtr<mirror::Constructor> m = soa.Decode<mirror::Constructor>(javaMethod); |
Andreas Gampe | 8ad7a3b | 2017-05-22 16:08:52 -0700 | [diff] [blame] | 65 | ArtMethod* constructor_art_method = m->GetArtMethod(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 66 | StackHandleScope<1> hs(soa.Self()); |
| 67 | Handle<mirror::Class> c(hs.NewHandle(m->GetDeclaringClass())); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 68 | if (UNLIKELY(c->IsAbstract())) { |
Mathieu Chartier | f36cb5f | 2015-04-24 16:55:16 -0700 | [diff] [blame] | 69 | soa.Self()->ThrowNewExceptionF("Ljava/lang/InstantiationException;", "Can't instantiate %s %s", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 70 | c->IsInterface() ? "interface" : "abstract class", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 71 | c->PrettyDescriptor().c_str()); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 72 | return nullptr; |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 73 | } |
Andreas Gampe | 8208bdd | 2015-04-27 17:26:37 -0700 | [diff] [blame] | 74 | // Verify that we can access the class. |
Sebastien Hertz | 2d2f2a9 | 2015-04-28 15:00:41 +0200 | [diff] [blame] | 75 | if (!m->IsAccessible() && !c->IsPublic()) { |
Przemyslaw Szczepaniak | 3ddd593 | 2015-11-23 16:08:03 +0000 | [diff] [blame] | 76 | // Go 2 frames back, this method is always called from newInstance0, which is called from |
Narayan Kamath | e091582 | 2015-11-18 13:00:18 +0000 | [diff] [blame] | 77 | // Constructor.newInstance(Object... args). |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 78 | ObjPtr<mirror::Class> caller = GetCallingClass(soa.Self(), 2); |
Mathieu Chartier | f36cb5f | 2015-04-24 16:55:16 -0700 | [diff] [blame] | 79 | // 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 Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 82 | if (c->PrettyDescriptor() == "dalvik.system.DexPathList$Element") { |
Andreas Gampe | 61d7ca8 | 2015-04-29 19:56:36 -0700 | [diff] [blame] | 83 | // 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 Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 90 | c->PrettyClass().c_str(), |
| 91 | caller->PrettyClass().c_str()); |
Andreas Gampe | 61d7ca8 | 2015-04-29 19:56:36 -0700 | [diff] [blame] | 92 | return nullptr; |
| 93 | } |
Mathieu Chartier | f36cb5f | 2015-04-24 16:55:16 -0700 | [diff] [blame] | 94 | } |
| 95 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 96 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(soa.Self(), c, true, true)) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 97 | DCHECK(soa.Self()->IsExceptionPending()); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 98 | return nullptr; |
Brian Carlstrom | 5d40f18 | 2011-09-26 22:29:18 -0700 | [diff] [blame] | 99 | } |
Mathieu Chartier | f4b9762 | 2014-03-10 13:26:27 -0700 | [diff] [blame] | 100 | bool movable = true; |
Mathieu Chartier | f36cb5f | 2015-04-24 16:55:16 -0700 | [diff] [blame] | 101 | if (!kMovingClasses && c->IsClassClass()) { |
Mathieu Chartier | f4b9762 | 2014-03-10 13:26:27 -0700 | [diff] [blame] | 102 | movable = false; |
| 103 | } |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 104 | |
| 105 | // String constructor is replaced by a StringFactory method in InvokeMethod. |
Andreas Gampe | 8ad7a3b | 2017-05-22 16:08:52 -0700 | [diff] [blame] | 106 | if (UNLIKELY(c->IsStringClass())) { |
Narayan Kamath | e091582 | 2015-11-18 13:00:18 +0000 | [diff] [blame] | 107 | return InvokeMethod(soa, javaMethod, nullptr, javaArgs, 2); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 110 | ObjPtr<mirror::Object> receiver = |
Mathieu Chartier | f4b9762 | 2014-03-10 13:26:27 -0700 | [diff] [blame] | 111 | movable ? c->AllocObject(soa.Self()) : c->AllocNonMovableObject(soa.Self()); |
Andreas Gampe | 8ad7a3b | 2017-05-22 16:08:52 -0700 | [diff] [blame] | 112 | if (UNLIKELY(receiver == nullptr)) { |
| 113 | DCHECK(soa.Self()->IsExceptionPending()); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 114 | return nullptr; |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 115 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 116 | jobject javaReceiver = soa.AddLocalReference<jobject>(receiver); |
Andreas Gampe | 8ad7a3b | 2017-05-22 16:08:52 -0700 | [diff] [blame] | 117 | |
| 118 | InvokeConstructor(soa, constructor_art_method, receiver, javaArgs); |
| 119 | |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 120 | return javaReceiver; |
| 121 | } |
| 122 | |
Przemyslaw Szczepaniak | dcf1b59 | 2015-10-12 16:34:14 +0100 | [diff] [blame] | 123 | static jobject Constructor_newInstanceFromSerialization(JNIEnv* env, jclass unused ATTRIBUTE_UNUSED, |
| 124 | jclass ctorClass, jclass allocClass) { |
| 125 | jmethodID ctor = env->GetMethodID(ctorClass, "<init>", "()V"); |
Yi Kong | 4b22b34 | 2018-08-02 14:43:21 -0700 | [diff] [blame] | 126 | DCHECK(ctor != nullptr); |
Przemyslaw Szczepaniak | dcf1b59 | 2015-10-12 16:34:14 +0100 | [diff] [blame] | 127 | return env->NewObject(allocClass, ctor); |
| 128 | } |
| 129 | |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 130 | static JNINativeMethod gMethods[] = { |
Igor Murashkin | 3b6f440 | 2017-02-16 16:13:17 -0800 | [diff] [blame] | 131 | 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 Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 134 | }; |
| 135 | |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 136 | void register_java_lang_reflect_Constructor(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 137 | REGISTER_NATIVE_METHODS("java/lang/reflect/Constructor"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | } // namespace art |