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