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