| 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 | |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 19 | #include "art_method-inl.h" |
| Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 20 | #include "base/enums.h" |
| Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 21 | #include "class_linker.h" |
| Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 22 | #include "class_linker-inl.h" |
| David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 23 | #include "dex_file_annotations.h" |
| Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 24 | #include "jni_internal.h" |
| Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 25 | #include "mirror/class-inl.h" |
| Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 26 | #include "mirror/method.h" |
| Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 27 | #include "mirror/object-inl.h" |
| Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 28 | #include "reflection.h" |
| Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 29 | #include "scoped_fast_native_object_access-inl.h" |
| Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 30 | #include "well_known_classes.h" |
| Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 31 | |
| Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 32 | namespace art { |
| 33 | |
| Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 34 | static jobjectArray Constructor_getExceptionTypes(JNIEnv* env, jobject javaMethod) { |
| 35 | ScopedFastNativeObjectAccess soa(env); |
| Alex Light | 52c9da0 | 2016-05-09 15:31:18 -0700 | [diff] [blame] | 36 | ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod) |
| Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 37 | ->GetInterfaceMethodIfProxy(kRuntimePointerSize); |
| Jeff Hao | 2a5892f | 2015-08-31 15:00:40 -0700 | [diff] [blame] | 38 | mirror::ObjectArray<mirror::Class>* result_array = |
| David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 39 | annotations::GetExceptionTypesForMethod(method); |
| Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 40 | if (result_array == nullptr) { |
| 41 | // Return an empty array instead of a null pointer. |
| Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 42 | ObjPtr<mirror::Class> class_class = mirror::Class::GetJavaLangClass(); |
| 43 | ObjPtr<mirror::Class> class_array_class = |
| Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 44 | Runtime::Current()->GetClassLinker()->FindArrayClass(soa.Self(), &class_class); |
| Jeff Hao | 2a5892f | 2015-08-31 15:00:40 -0700 | [diff] [blame] | 45 | if (class_array_class == nullptr) { |
| 46 | return nullptr; |
| 47 | } |
| Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 48 | ObjPtr<mirror::ObjectArray<mirror::Class>> empty_array = |
| Jeff Hao | 2a5892f | 2015-08-31 15:00:40 -0700 | [diff] [blame] | 49 | mirror::ObjectArray<mirror::Class>::Alloc(soa.Self(), class_array_class, 0); |
| Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 50 | return soa.AddLocalReference<jobjectArray>(empty_array); |
| 51 | } else { |
| 52 | return soa.AddLocalReference<jobjectArray>(result_array); |
| 53 | } |
| 54 | } |
| 55 | |
| Mathieu Chartier | f36cb5f | 2015-04-24 16:55:16 -0700 | [diff] [blame] | 56 | /* |
| Andreas Gampe | 8208bdd | 2015-04-27 17:26:37 -0700 | [diff] [blame] | 57 | * We can also safely assume the constructor isn't associated |
| Mathieu Chartier | f36cb5f | 2015-04-24 16:55:16 -0700 | [diff] [blame] | 58 | * 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 Szczepaniak | dcf1b59 | 2015-10-12 16:34:14 +0100 | [diff] [blame] | 61 | static jobject Constructor_newInstance0(JNIEnv* env, jobject javaMethod, jobjectArray javaArgs) { |
| Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 62 | ScopedFastNativeObjectAccess soa(env); |
| Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 63 | ObjPtr<mirror::Constructor> m = soa.Decode<mirror::Constructor>(javaMethod); |
| Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 64 | StackHandleScope<1> hs(soa.Self()); |
| 65 | Handle<mirror::Class> c(hs.NewHandle(m->GetDeclaringClass())); |
| Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 66 | if (UNLIKELY(c->IsAbstract())) { |
| Mathieu Chartier | f36cb5f | 2015-04-24 16:55:16 -0700 | [diff] [blame] | 67 | soa.Self()->ThrowNewExceptionF("Ljava/lang/InstantiationException;", "Can't instantiate %s %s", |
| Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 68 | c->IsInterface() ? "interface" : "abstract class", |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 69 | c->PrettyDescriptor().c_str()); |
| Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 70 | return nullptr; |
| Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 71 | } |
| Andreas Gampe | 8208bdd | 2015-04-27 17:26:37 -0700 | [diff] [blame] | 72 | // Verify that we can access the class. |
| Sebastien Hertz | 2d2f2a9 | 2015-04-28 15:00:41 +0200 | [diff] [blame] | 73 | if (!m->IsAccessible() && !c->IsPublic()) { |
| Przemyslaw Szczepaniak | 3ddd593 | 2015-11-23 16:08:03 +0000 | [diff] [blame] | 74 | // 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] | 75 | // Constructor.newInstance(Object... args). |
| Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 76 | ObjPtr<mirror::Class> caller = GetCallingClass(soa.Self(), 2); |
| Mathieu Chartier | f36cb5f | 2015-04-24 16:55:16 -0700 | [diff] [blame] | 77 | // 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 Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 80 | if (c->PrettyDescriptor() == "dalvik.system.DexPathList$Element") { |
| Andreas Gampe | 61d7ca8 | 2015-04-29 19:56:36 -0700 | [diff] [blame] | 81 | // 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 Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 88 | c->PrettyClass().c_str(), |
| 89 | caller->PrettyClass().c_str()); |
| Andreas Gampe | 61d7ca8 | 2015-04-29 19:56:36 -0700 | [diff] [blame] | 90 | return nullptr; |
| 91 | } |
| Mathieu Chartier | f36cb5f | 2015-04-24 16:55:16 -0700 | [diff] [blame] | 92 | } |
| 93 | } |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 94 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(soa.Self(), c, true, true)) { |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 95 | DCHECK(soa.Self()->IsExceptionPending()); |
| Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 96 | return nullptr; |
| Brian Carlstrom | 5d40f18 | 2011-09-26 22:29:18 -0700 | [diff] [blame] | 97 | } |
| Mathieu Chartier | f4b9762 | 2014-03-10 13:26:27 -0700 | [diff] [blame] | 98 | bool movable = true; |
| Mathieu Chartier | f36cb5f | 2015-04-24 16:55:16 -0700 | [diff] [blame] | 99 | if (!kMovingClasses && c->IsClassClass()) { |
| Mathieu Chartier | f4b9762 | 2014-03-10 13:26:27 -0700 | [diff] [blame] | 100 | movable = false; |
| 101 | } |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 102 | |
| 103 | // String constructor is replaced by a StringFactory method in InvokeMethod. |
| 104 | if (c->IsStringClass()) { |
| Narayan Kamath | e091582 | 2015-11-18 13:00:18 +0000 | [diff] [blame] | 105 | return InvokeMethod(soa, javaMethod, nullptr, javaArgs, 2); |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 108 | ObjPtr<mirror::Object> receiver = |
| Mathieu Chartier | f4b9762 | 2014-03-10 13:26:27 -0700 | [diff] [blame] | 109 | movable ? c->AllocObject(soa.Self()) : c->AllocNonMovableObject(soa.Self()); |
| Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 110 | if (receiver == nullptr) { |
| 111 | return nullptr; |
| Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 112 | } |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 113 | jobject javaReceiver = soa.AddLocalReference<jobject>(receiver); |
| Narayan Kamath | e091582 | 2015-11-18 13:00:18 +0000 | [diff] [blame] | 114 | InvokeMethod(soa, javaMethod, javaReceiver, javaArgs, 2); |
| Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 115 | // Constructors are ()V methods, so we shouldn't touch the result of InvokeMethod. |
| 116 | return javaReceiver; |
| 117 | } |
| 118 | |
| Przemyslaw Szczepaniak | dcf1b59 | 2015-10-12 16:34:14 +0100 | [diff] [blame] | 119 | static 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 Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 126 | static JNINativeMethod gMethods[] = { |
| Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 127 | NATIVE_METHOD(Constructor, getExceptionTypes, "!()[Ljava/lang/Class;"), |
| Przemyslaw Szczepaniak | dcf1b59 | 2015-10-12 16:34:14 +0100 | [diff] [blame] | 128 | NATIVE_METHOD(Constructor, newInstance0, "!([Ljava/lang/Object;)Ljava/lang/Object;"), |
| 129 | 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] | 130 | }; |
| 131 | |
| Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 132 | void register_java_lang_reflect_Constructor(JNIEnv* env) { |
| Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 133 | REGISTER_NATIVE_METHODS("java/lang/reflect/Constructor"); |
| Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | } // namespace art |