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