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