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 
17 #include "java_lang_reflect_Constructor.h"
18 
19 #include "art_method-inl.h"
20 #include "base/enums.h"
21 #include "class_linker.h"
22 #include "class_linker-inl.h"
23 #include "dex_file_annotations.h"
24 #include "jni_internal.h"
25 #include "mirror/class-inl.h"
26 #include "mirror/method.h"
27 #include "mirror/object-inl.h"
28 #include "reflection.h"
29 #include "scoped_fast_native_object_access-inl.h"
30 #include "well_known_classes.h"
31 
32 namespace art {
33 
Constructor_getExceptionTypes(JNIEnv * env,jobject javaMethod)34 static jobjectArray Constructor_getExceptionTypes(JNIEnv* env, jobject javaMethod) {
35   ScopedFastNativeObjectAccess soa(env);
36   ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod)
37       ->GetInterfaceMethodIfProxy(kRuntimePointerSize);
38   mirror::ObjectArray<mirror::Class>* result_array =
39       annotations::GetExceptionTypesForMethod(method);
40   if (result_array == nullptr) {
41     // Return an empty array instead of a null pointer.
42     ObjPtr<mirror::Class> class_class = mirror::Class::GetJavaLangClass();
43     ObjPtr<mirror::Class> class_array_class =
44         Runtime::Current()->GetClassLinker()->FindArrayClass(soa.Self(), &class_class);
45     if (class_array_class == nullptr) {
46       return nullptr;
47     }
48     ObjPtr<mirror::ObjectArray<mirror::Class>> empty_array =
49         mirror::ObjectArray<mirror::Class>::Alloc(soa.Self(), class_array_class, 0);
50     return soa.AddLocalReference<jobjectArray>(empty_array);
51   } else {
52     return soa.AddLocalReference<jobjectArray>(result_array);
53   }
54 }
55 
56 /*
57  * We can also safely assume the constructor isn't associated
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  */
Constructor_newInstance0(JNIEnv * env,jobject javaMethod,jobjectArray javaArgs)61 static jobject Constructor_newInstance0(JNIEnv* env, jobject javaMethod, jobjectArray javaArgs) {
62   ScopedFastNativeObjectAccess soa(env);
63   ObjPtr<mirror::Constructor> m = soa.Decode<mirror::Constructor>(javaMethod);
64   StackHandleScope<1> hs(soa.Self());
65   Handle<mirror::Class> c(hs.NewHandle(m->GetDeclaringClass()));
66   if (UNLIKELY(c->IsAbstract())) {
67     soa.Self()->ThrowNewExceptionF("Ljava/lang/InstantiationException;", "Can't instantiate %s %s",
68                                    c->IsInterface() ? "interface" : "abstract class",
69                                    c->PrettyDescriptor().c_str());
70     return nullptr;
71   }
72   // Verify that we can access the class.
73   if (!m->IsAccessible() && !c->IsPublic()) {
74     // Go 2 frames back, this method is always called from newInstance0, which is called from
75     // Constructor.newInstance(Object... args).
76     ObjPtr<mirror::Class> caller = GetCallingClass(soa.Self(), 2);
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())) {
80       if (c->PrettyDescriptor() == "dalvik.system.DexPathList$Element") {
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",
88             c->PrettyClass().c_str(),
89             caller->PrettyClass().c_str());
90         return nullptr;
91       }
92     }
93   }
94   if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(soa.Self(), c, true, true)) {
95     DCHECK(soa.Self()->IsExceptionPending());
96     return nullptr;
97   }
98   bool movable = true;
99   if (!kMovingClasses && c->IsClassClass()) {
100     movable = false;
101   }
102 
103   // String constructor is replaced by a StringFactory method in InvokeMethod.
104   if (c->IsStringClass()) {
105     return InvokeMethod(soa, javaMethod, nullptr, javaArgs, 2);
106   }
107 
108   ObjPtr<mirror::Object> receiver =
109       movable ? c->AllocObject(soa.Self()) : c->AllocNonMovableObject(soa.Self());
110   if (receiver == nullptr) {
111     return nullptr;
112   }
113   jobject javaReceiver = soa.AddLocalReference<jobject>(receiver);
114   InvokeMethod(soa, javaMethod, javaReceiver, javaArgs, 2);
115   // Constructors are ()V methods, so we shouldn't touch the result of InvokeMethod.
116   return javaReceiver;
117 }
118 
Constructor_newInstanceFromSerialization(JNIEnv * env,jclass unused ATTRIBUTE_UNUSED,jclass ctorClass,jclass allocClass)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 
126 static JNINativeMethod gMethods[] = {
127   FAST_NATIVE_METHOD(Constructor, getExceptionTypes, "()[Ljava/lang/Class;"),
128   FAST_NATIVE_METHOD(Constructor, newInstance0, "([Ljava/lang/Object;)Ljava/lang/Object;"),
129   FAST_NATIVE_METHOD(Constructor, newInstanceFromSerialization, "(Ljava/lang/Class;Ljava/lang/Class;)Ljava/lang/Object;"),
130 };
131 
register_java_lang_reflect_Constructor(JNIEnv * env)132 void register_java_lang_reflect_Constructor(JNIEnv* env) {
133   REGISTER_NATIVE_METHODS("java/lang/reflect/Constructor");
134 }
135 
136 }  // namespace art
137