Home | History | Annotate | Download | only in native
      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_linker-inl.h"
     25 #include "dex_file_annotations.h"
     26 #include "jni_internal.h"
     27 #include "mirror/class-inl.h"
     28 #include "mirror/method.h"
     29 #include "mirror/object-inl.h"
     30 #include "native_util.h"
     31 #include "reflection.h"
     32 #include "scoped_fast_native_object_access-inl.h"
     33 #include "well_known_classes.h"
     34 
     35 namespace art {
     36 
     37 static jobjectArray Constructor_getExceptionTypes(JNIEnv* env, jobject javaMethod) {
     38   ScopedFastNativeObjectAccess soa(env);
     39   ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod)
     40       ->GetInterfaceMethodIfProxy(kRuntimePointerSize);
     41   mirror::ObjectArray<mirror::Class>* result_array =
     42       annotations::GetExceptionTypesForMethod(method);
     43   if (result_array == nullptr) {
     44     // Return an empty array instead of a null pointer.
     45     ObjPtr<mirror::Class> class_class = mirror::Class::GetJavaLangClass();
     46     ObjPtr<mirror::Class> class_array_class =
     47         Runtime::Current()->GetClassLinker()->FindArrayClass(soa.Self(), &class_class);
     48     if (class_array_class == nullptr) {
     49       return nullptr;
     50     }
     51     ObjPtr<mirror::ObjectArray<mirror::Class>> empty_array =
     52         mirror::ObjectArray<mirror::Class>::Alloc(soa.Self(), class_array_class, 0);
     53     return soa.AddLocalReference<jobjectArray>(empty_array);
     54   } else {
     55     return soa.AddLocalReference<jobjectArray>(result_array);
     56   }
     57 }
     58 
     59 /*
     60  * We can also safely assume the constructor isn't associated
     61  * with an interface, array, or primitive class. If this is coming from
     62  * native, it is OK to avoid access checks since JNI does not enforce them.
     63  */
     64 static jobject Constructor_newInstance0(JNIEnv* env, jobject javaMethod, jobjectArray javaArgs) {
     65   ScopedFastNativeObjectAccess soa(env);
     66   ObjPtr<mirror::Constructor> m = soa.Decode<mirror::Constructor>(javaMethod);
     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 (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 (receiver == nullptr) {
    114     return nullptr;
    115   }
    116   jobject javaReceiver = soa.AddLocalReference<jobject>(receiver);
    117   InvokeMethod(soa, javaMethod, javaReceiver, javaArgs, 2);
    118   // Constructors are ()V methods, so we shouldn't touch the result of InvokeMethod.
    119   return javaReceiver;
    120 }
    121 
    122 static jobject Constructor_newInstanceFromSerialization(JNIEnv* env, jclass unused ATTRIBUTE_UNUSED,
    123                                                         jclass ctorClass, jclass allocClass) {
    124     jmethodID ctor = env->GetMethodID(ctorClass, "<init>", "()V");
    125     DCHECK(ctor != NULL);
    126     return env->NewObject(allocClass, ctor);
    127 }
    128 
    129 static JNINativeMethod gMethods[] = {
    130   FAST_NATIVE_METHOD(Constructor, getExceptionTypes, "()[Ljava/lang/Class;"),
    131   FAST_NATIVE_METHOD(Constructor, newInstance0, "([Ljava/lang/Object;)Ljava/lang/Object;"),
    132   FAST_NATIVE_METHOD(Constructor, newInstanceFromSerialization, "(Ljava/lang/Class;Ljava/lang/Class;)Ljava/lang/Object;"),
    133 };
    134 
    135 void register_java_lang_reflect_Constructor(JNIEnv* env) {
    136   REGISTER_NATIVE_METHODS("java/lang/reflect/Constructor");
    137 }
    138 
    139 }  // namespace art
    140