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_VMClassLoader.h"
     18 
     19 #include "class_linker.h"
     20 #include "jni_internal.h"
     21 #include "mirror/class_loader.h"
     22 #include "mirror/object-inl.h"
     23 #include "obj_ptr.h"
     24 #include "scoped_fast_native_object_access-inl.h"
     25 #include "ScopedUtfChars.h"
     26 #include "well_known_classes.h"
     27 #include "zip_archive.h"
     28 
     29 namespace art {
     30 
     31 // A class so we can be friends with ClassLinker and access internal methods.
     32 class VMClassLoader {
     33  public:
     34   static mirror::Class* LookupClass(ClassLinker* cl,
     35                                     Thread* self,
     36                                     const char* descriptor,
     37                                     size_t hash,
     38                                     ObjPtr<mirror::ClassLoader> class_loader)
     39       REQUIRES(!Locks::classlinker_classes_lock_)
     40       REQUIRES_SHARED(Locks::mutator_lock_) {
     41     return cl->LookupClass(self, descriptor, hash, class_loader);
     42   }
     43 
     44   static ObjPtr<mirror::Class> FindClassInPathClassLoader(ClassLinker* cl,
     45                                                           ScopedObjectAccessAlreadyRunnable& soa,
     46                                                           Thread* self,
     47                                                           const char* descriptor,
     48                                                           size_t hash,
     49                                                           Handle<mirror::ClassLoader> class_loader)
     50       REQUIRES_SHARED(Locks::mutator_lock_) {
     51     ObjPtr<mirror::Class> result;
     52     if (cl->FindClassInBaseDexClassLoader(soa, self, descriptor, hash, class_loader, &result)) {
     53       return result;
     54     }
     55     return nullptr;
     56   }
     57 };
     58 
     59 static jclass VMClassLoader_findLoadedClass(JNIEnv* env, jclass, jobject javaLoader,
     60                                             jstring javaName) {
     61   ScopedFastNativeObjectAccess soa(env);
     62   ObjPtr<mirror::ClassLoader> loader = soa.Decode<mirror::ClassLoader>(javaLoader);
     63   ScopedUtfChars name(env, javaName);
     64   if (name.c_str() == nullptr) {
     65     return nullptr;
     66   }
     67   ClassLinker* cl = Runtime::Current()->GetClassLinker();
     68 
     69   // Compute hash once.
     70   std::string descriptor(DotToDescriptor(name.c_str()));
     71   const size_t descriptor_hash = ComputeModifiedUtf8Hash(descriptor.c_str());
     72 
     73   ObjPtr<mirror::Class> c = VMClassLoader::LookupClass(cl,
     74                                                        soa.Self(),
     75                                                        descriptor.c_str(),
     76                                                        descriptor_hash,
     77                                                        loader);
     78   if (c != nullptr && c->IsResolved()) {
     79     return soa.AddLocalReference<jclass>(c);
     80   }
     81   // If class is erroneous, throw the earlier failure, wrapped in certain cases. See b/28787733.
     82   if (c != nullptr && c->IsErroneous()) {
     83     cl->ThrowEarlierClassFailure(c.Ptr());
     84     Thread* self = soa.Self();
     85     ObjPtr<mirror::Class> iae_class =
     86         self->DecodeJObject(WellKnownClasses::java_lang_IllegalAccessError)->AsClass();
     87     ObjPtr<mirror::Class> ncdfe_class =
     88         self->DecodeJObject(WellKnownClasses::java_lang_NoClassDefFoundError)->AsClass();
     89     ObjPtr<mirror::Class> exception = self->GetException()->GetClass();
     90     if (exception == iae_class || exception == ncdfe_class) {
     91       self->ThrowNewWrappedException("Ljava/lang/ClassNotFoundException;",
     92                                      c->PrettyDescriptor().c_str());
     93     }
     94     return nullptr;
     95   }
     96 
     97   // Hard-coded performance optimization: We know that all failed libcore calls to findLoadedClass
     98   //                                      are followed by a call to the the classloader to actually
     99   //                                      load the class.
    100   if (loader != nullptr) {
    101     // Try the common case.
    102     StackHandleScope<1> hs(soa.Self());
    103     c = VMClassLoader::FindClassInPathClassLoader(cl,
    104                                                   soa,
    105                                                   soa.Self(),
    106                                                   descriptor.c_str(),
    107                                                   descriptor_hash,
    108                                                   hs.NewHandle(loader));
    109     if (c != nullptr) {
    110       return soa.AddLocalReference<jclass>(c);
    111     }
    112   }
    113 
    114   // The class wasn't loaded, yet, and our fast-path did not apply (e.g., we didn't understand the
    115   // classloader chain).
    116   return nullptr;
    117 }
    118 
    119 /*
    120  * Returns an array of entries from the boot classpath that could contain resources.
    121  */
    122 static jobjectArray VMClassLoader_getBootClassPathEntries(JNIEnv* env, jclass) {
    123   const std::vector<const DexFile*>& path =
    124       Runtime::Current()->GetClassLinker()->GetBootClassPath();
    125   jclass stringClass = env->FindClass("java/lang/String");
    126   jobjectArray array = env->NewObjectArray(path.size(), stringClass, nullptr);
    127   for (size_t i = 0; i < path.size(); ++i) {
    128     const DexFile* dex_file = path[i];
    129 
    130     // For multidex locations, e.g., x.jar:classes2.dex, we want to look into x.jar.
    131     const std::string& location(dex_file->GetBaseLocation());
    132 
    133     jstring javaPath = env->NewStringUTF(location.c_str());
    134     env->SetObjectArrayElement(array, i, javaPath);
    135   }
    136   return array;
    137 }
    138 
    139 static JNINativeMethod gMethods[] = {
    140   FAST_NATIVE_METHOD(VMClassLoader, findLoadedClass, "(Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;"),
    141   NATIVE_METHOD(VMClassLoader, getBootClassPathEntries, "()[Ljava/lang/String;"),
    142 };
    143 
    144 void register_java_lang_VMClassLoader(JNIEnv* env) {
    145   REGISTER_NATIVE_METHODS("java/lang/VMClassLoader");
    146 }
    147 
    148 }  // namespace art
    149