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 "scoped_fast_native_object_access.h" 24 #include "ScopedUtfChars.h" 25 #include "zip_archive.h" 26 27 namespace art { 28 29 static jclass VMClassLoader_findLoadedClass(JNIEnv* env, jclass, jobject javaLoader, 30 jstring javaName) { 31 ScopedFastNativeObjectAccess soa(env); 32 mirror::ClassLoader* loader = soa.Decode<mirror::ClassLoader*>(javaLoader); 33 ScopedUtfChars name(env, javaName); 34 if (name.c_str() == nullptr) { 35 return nullptr; 36 } 37 ClassLinker* cl = Runtime::Current()->GetClassLinker(); 38 std::string descriptor(DotToDescriptor(name.c_str())); 39 const size_t descriptor_hash = ComputeModifiedUtf8Hash(descriptor.c_str()); 40 mirror::Class* c = cl->LookupClass(soa.Self(), descriptor.c_str(), descriptor_hash, loader); 41 if (c != nullptr && c->IsResolved()) { 42 return soa.AddLocalReference<jclass>(c); 43 } 44 if (loader != nullptr) { 45 // Try the common case. 46 StackHandleScope<1> hs(soa.Self()); 47 cl->FindClassInPathClassLoader(soa, soa.Self(), descriptor.c_str(), descriptor_hash, 48 hs.NewHandle(loader), &c); 49 if (c != nullptr) { 50 return soa.AddLocalReference<jclass>(c); 51 } 52 } 53 // Class wasn't resolved so it may be erroneous or not yet ready, force the caller to go into 54 // the regular loadClass code. 55 return nullptr; 56 } 57 58 static jint VMClassLoader_getBootClassPathSize(JNIEnv*, jclass) { 59 return Runtime::Current()->GetClassLinker()->GetBootClassPath().size(); 60 } 61 62 /* 63 * Returns a string URL for a resource with the specified 'javaName' in 64 * entry 'index' of the boot class path. 65 * 66 * We return a newly-allocated String in the following form: 67 * 68 * jar:file://path!/name 69 * 70 * Where "path" is the bootstrap class path entry and "name" is the string 71 * passed into this method. "path" needs to be an absolute path (starting 72 * with '/'); if it's not we'd need to make it absolute as part of forming 73 * the URL string. 74 */ 75 static jstring VMClassLoader_getBootClassPathResource(JNIEnv* env, jclass, jstring javaName, 76 jint index) { 77 ScopedUtfChars name(env, javaName); 78 if (name.c_str() == nullptr) { 79 return nullptr; 80 } 81 82 const std::vector<const DexFile*>& path = 83 Runtime::Current()->GetClassLinker()->GetBootClassPath(); 84 if (index < 0 || size_t(index) >= path.size()) { 85 return nullptr; 86 } 87 const DexFile* dex_file = path[index]; 88 89 // For multidex locations, e.g., x.jar:classes2.dex, we want to look into x.jar. 90 const std::string& location(dex_file->GetBaseLocation()); 91 92 std::string error_msg; 93 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(location.c_str(), &error_msg)); 94 if (zip_archive.get() == nullptr) { 95 LOG(WARNING) << "Failed to open zip archive '" << location << "': " << error_msg; 96 return nullptr; 97 } 98 std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(name.c_str(), &error_msg)); 99 if (zip_entry.get() == nullptr) { 100 return nullptr; 101 } 102 103 std::string url; 104 StringAppendF(&url, "jar:file://%s!/%s", location.c_str(), name.c_str()); 105 return env->NewStringUTF(url.c_str()); 106 } 107 108 static JNINativeMethod gMethods[] = { 109 NATIVE_METHOD(VMClassLoader, findLoadedClass, "!(Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;"), 110 NATIVE_METHOD(VMClassLoader, getBootClassPathResource, "(Ljava/lang/String;I)Ljava/lang/String;"), 111 NATIVE_METHOD(VMClassLoader, getBootClassPathSize, "!()I"), 112 }; 113 114 void register_java_lang_VMClassLoader(JNIEnv* env) { 115 REGISTER_NATIVE_METHODS("java/lang/VMClassLoader"); 116 } 117 118 } // namespace art 119