blob: e5bab36870ae91b2a3f821257fc0fa189cc9b367 [file] [log] [blame]
Elliott Hughes64bf5a32011-09-20 14:43:12 -07001/*
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
Andreas Gampe277ccbd2014-11-03 21:36:10 -080017#include "java_lang_VMClassLoader.h"
18
Elliott Hughes64bf5a32011-09-20 14:43:12 -070019#include "class_linker.h"
20#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "mirror/class_loader.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070022#include "mirror/object-inl.h"
Andreas Gampe34ee6842014-12-02 15:43:52 -080023#include "obj_ptr.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070024#include "scoped_fast_native_object_access-inl.h"
Elliott Hughes64bf5a32011-09-20 14:43:12 -070025#include "ScopedUtfChars.h"
26#include "zip_archive.h"
27
Elliott Hughes64bf5a32011-09-20 14:43:12 -070028namespace art {
29
Andreas Gampe34ee6842014-12-02 15:43:52 -080030// A class so we can be friends with ClassLinker and access internal methods.
31class VMClassLoader {
32 public:
33 static mirror::Class* LookupClass(ClassLinker* cl,
34 Thread* self,
35 const char* descriptor,
36 size_t hash,
37 ObjPtr<mirror::ClassLoader> class_loader)
38 REQUIRES(!Locks::classlinker_classes_lock_)
39 REQUIRES_SHARED(Locks::mutator_lock_) {
40 return cl->LookupClass(self, descriptor, hash, class_loader);
41 }
42
43 static ObjPtr<mirror::Class> FindClassInPathClassLoader(ClassLinker* cl,
44 ScopedObjectAccessAlreadyRunnable& soa,
45 Thread* self,
46 const char* descriptor,
47 size_t hash,
48 Handle<mirror::ClassLoader> class_loader)
49 REQUIRES_SHARED(Locks::mutator_lock_) {
50 ObjPtr<mirror::Class> result;
51 if (cl->FindClassInPathClassLoader(soa, self, descriptor, hash, class_loader, &result)) {
52 return result;
53 }
54 return nullptr;
55 }
56};
57
Ian Rogers7b078e82014-09-10 14:44:24 -070058static jclass VMClassLoader_findLoadedClass(JNIEnv* env, jclass, jobject javaLoader,
59 jstring javaName) {
Ian Rogers53b8b092014-03-13 23:45:53 -070060 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -070061 ObjPtr<mirror::ClassLoader> loader = soa.Decode<mirror::ClassLoader>(javaLoader);
Elliott Hughes64bf5a32011-09-20 14:43:12 -070062 ScopedUtfChars name(env, javaName);
Ian Rogers7b078e82014-09-10 14:44:24 -070063 if (name.c_str() == nullptr) {
64 return nullptr;
Elliott Hughes64bf5a32011-09-20 14:43:12 -070065 }
Mathieu Chartierab0ed822014-09-11 14:21:41 -070066 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Andreas Gampe34ee6842014-12-02 15:43:52 -080067
68 // Compute hash once.
Brian Carlstromf91c8c32011-09-21 17:30:34 -070069 std::string descriptor(DotToDescriptor(name.c_str()));
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -080070 const size_t descriptor_hash = ComputeModifiedUtf8Hash(descriptor.c_str());
Andreas Gampe34ee6842014-12-02 15:43:52 -080071
72 ObjPtr<mirror::Class> c = VMClassLoader::LookupClass(cl,
73 soa.Self(),
74 descriptor.c_str(),
75 descriptor_hash,
76 loader);
Ian Rogers7b078e82014-09-10 14:44:24 -070077 if (c != nullptr && c->IsResolved()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070078 return soa.AddLocalReference<jclass>(c);
Ian Rogersbe125a92012-01-11 15:19:49 -080079 }
Jeff Hao7c8aa832016-06-06 11:09:20 -070080 // If class is erroneous, throw the earlier failure, wrapped in certain cases. See b/28787733.
81 if (c != nullptr && c->IsErroneous()) {
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070082 cl->ThrowEarlierClassFailure(c.Ptr());
Jeff Hao7c8aa832016-06-06 11:09:20 -070083 Thread* self = soa.Self();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070084 ObjPtr<mirror::Class> eiie_class =
Jeff Hao7c8aa832016-06-06 11:09:20 -070085 self->DecodeJObject(WellKnownClasses::java_lang_ExceptionInInitializerError)->AsClass();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070086 ObjPtr<mirror::Class> iae_class =
Jeff Hao7c8aa832016-06-06 11:09:20 -070087 self->DecodeJObject(WellKnownClasses::java_lang_IllegalAccessError)->AsClass();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070088 ObjPtr<mirror::Class> ncdfe_class =
Jeff Hao7c8aa832016-06-06 11:09:20 -070089 self->DecodeJObject(WellKnownClasses::java_lang_NoClassDefFoundError)->AsClass();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070090 ObjPtr<mirror::Class> exception = self->GetException()->GetClass();
Jeff Hao7c8aa832016-06-06 11:09:20 -070091 if (exception == eiie_class || exception == iae_class || exception == ncdfe_class) {
92 self->ThrowNewWrappedException("Ljava/lang/ClassNotFoundException;",
David Sehr709b0702016-10-13 09:12:37 -070093 c->PrettyDescriptor().c_str());
Jeff Hao7c8aa832016-06-06 11:09:20 -070094 }
95 return nullptr;
96 }
Andreas Gampe34ee6842014-12-02 15:43:52 -080097
98 // Hard-coded performance optimization: We know that all failed libcore calls to findLoadedClass
99 // are followed by a call to the the classloader to actually
100 // load the class.
Mathieu Chartierab0ed822014-09-11 14:21:41 -0700101 if (loader != nullptr) {
102 // Try the common case.
103 StackHandleScope<1> hs(soa.Self());
Andreas Gampe34ee6842014-12-02 15:43:52 -0800104 c = VMClassLoader::FindClassInPathClassLoader(cl,
105 soa,
106 soa.Self(),
107 descriptor.c_str(),
108 descriptor_hash,
109 hs.NewHandle(loader));
Mathieu Chartierab0ed822014-09-11 14:21:41 -0700110 if (c != nullptr) {
111 return soa.AddLocalReference<jclass>(c);
112 }
113 }
Andreas Gampe34ee6842014-12-02 15:43:52 -0800114
115 // The class wasn't loaded, yet, and our fast-path did not apply (e.g., we didn't understand the
116 // classloader chain).
Ian Rogers7b078e82014-09-10 14:44:24 -0700117 return nullptr;
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700118}
119
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700120/*
Neil Fulleref486052015-06-04 12:24:08 +0000121 * Returns an array of entries from the boot classpath that could contain resources.
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700122 */
Neil Fulleref486052015-06-04 12:24:08 +0000123static jobjectArray VMClassLoader_getBootClassPathEntries(JNIEnv* env, jclass) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700124 const std::vector<const DexFile*>& path =
125 Runtime::Current()->GetClassLinker()->GetBootClassPath();
Neil Fulleref486052015-06-04 12:24:08 +0000126 jclass stringClass = env->FindClass("java/lang/String");
127 jobjectArray array = env->NewObjectArray(path.size(), stringClass, nullptr);
128 for (size_t i = 0; i < path.size(); ++i) {
129 const DexFile* dex_file = path[i];
Neil Fuller1e27c5b2015-06-03 15:46:29 +0000130
Neil Fulleref486052015-06-04 12:24:08 +0000131 // For multidex locations, e.g., x.jar:classes2.dex, we want to look into x.jar.
132 const std::string& location(dex_file->GetBaseLocation());
Neil Fuller1e27c5b2015-06-03 15:46:29 +0000133
Neil Fulleref486052015-06-04 12:24:08 +0000134 jstring javaPath = env->NewStringUTF(location.c_str());
135 env->SetObjectArrayElement(array, i, javaPath);
Neil Fuller1e27c5b2015-06-03 15:46:29 +0000136 }
Neil Fulleref486052015-06-04 12:24:08 +0000137 return array;
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700138}
139
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700140static JNINativeMethod gMethods[] = {
Ian Rogers53b8b092014-03-13 23:45:53 -0700141 NATIVE_METHOD(VMClassLoader, findLoadedClass, "!(Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;"),
Neil Fulleref486052015-06-04 12:24:08 +0000142 NATIVE_METHOD(VMClassLoader, getBootClassPathEntries, "()[Ljava/lang/String;"),
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700143};
144
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700145void register_java_lang_VMClassLoader(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700146 REGISTER_NATIVE_METHODS("java/lang/VMClassLoader");
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700147}
148
149} // namespace art