blob: 57c313e933ceeb0e7b4ec838dc7fcfb1f60a36ae [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
Andreas Gampea14100c2017-04-24 15:09:56 -070019#include "nativehelper/jni_macros.h"
20
Elliott Hughes64bf5a32011-09-20 14:43:12 -070021#include "class_linker.h"
22#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "mirror/class_loader.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070024#include "mirror/object-inl.h"
Andreas Gampe87583b32017-05-25 11:22:18 -070025#include "native_util.h"
Andreas Gampe34ee6842014-12-02 15:43:52 -080026#include "obj_ptr.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070027#include "scoped_fast_native_object_access-inl.h"
Vladimir Marko4b3d6902017-05-30 15:23:54 +010028#include "ScopedLocalRef.h"
Elliott Hughes64bf5a32011-09-20 14:43:12 -070029#include "ScopedUtfChars.h"
Andreas Gampea1d2f952017-04-20 22:53:58 -070030#include "well_known_classes.h"
Elliott Hughes64bf5a32011-09-20 14:43:12 -070031#include "zip_archive.h"
32
Elliott Hughes64bf5a32011-09-20 14:43:12 -070033namespace art {
34
Andreas Gampe34ee6842014-12-02 15:43:52 -080035// A class so we can be friends with ClassLinker and access internal methods.
36class VMClassLoader {
37 public:
38 static mirror::Class* LookupClass(ClassLinker* cl,
39 Thread* self,
40 const char* descriptor,
41 size_t hash,
42 ObjPtr<mirror::ClassLoader> class_loader)
43 REQUIRES(!Locks::classlinker_classes_lock_)
44 REQUIRES_SHARED(Locks::mutator_lock_) {
45 return cl->LookupClass(self, descriptor, hash, class_loader);
46 }
47
48 static ObjPtr<mirror::Class> FindClassInPathClassLoader(ClassLinker* cl,
49 ScopedObjectAccessAlreadyRunnable& soa,
50 Thread* self,
51 const char* descriptor,
52 size_t hash,
53 Handle<mirror::ClassLoader> class_loader)
54 REQUIRES_SHARED(Locks::mutator_lock_) {
55 ObjPtr<mirror::Class> result;
Nicolas Geoffray7d8d8ff2016-11-02 12:38:05 +000056 if (cl->FindClassInBaseDexClassLoader(soa, self, descriptor, hash, class_loader, &result)) {
Andreas Gampe34ee6842014-12-02 15:43:52 -080057 return result;
58 }
59 return nullptr;
60 }
61};
62
Ian Rogers7b078e82014-09-10 14:44:24 -070063static jclass VMClassLoader_findLoadedClass(JNIEnv* env, jclass, jobject javaLoader,
64 jstring javaName) {
Ian Rogers53b8b092014-03-13 23:45:53 -070065 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -070066 ObjPtr<mirror::ClassLoader> loader = soa.Decode<mirror::ClassLoader>(javaLoader);
Elliott Hughes64bf5a32011-09-20 14:43:12 -070067 ScopedUtfChars name(env, javaName);
Ian Rogers7b078e82014-09-10 14:44:24 -070068 if (name.c_str() == nullptr) {
69 return nullptr;
Elliott Hughes64bf5a32011-09-20 14:43:12 -070070 }
Mathieu Chartierab0ed822014-09-11 14:21:41 -070071 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Andreas Gampe34ee6842014-12-02 15:43:52 -080072
73 // Compute hash once.
Brian Carlstromf91c8c32011-09-21 17:30:34 -070074 std::string descriptor(DotToDescriptor(name.c_str()));
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -080075 const size_t descriptor_hash = ComputeModifiedUtf8Hash(descriptor.c_str());
Andreas Gampe34ee6842014-12-02 15:43:52 -080076
77 ObjPtr<mirror::Class> c = VMClassLoader::LookupClass(cl,
78 soa.Self(),
79 descriptor.c_str(),
80 descriptor_hash,
81 loader);
Ian Rogers7b078e82014-09-10 14:44:24 -070082 if (c != nullptr && c->IsResolved()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070083 return soa.AddLocalReference<jclass>(c);
Ian Rogersbe125a92012-01-11 15:19:49 -080084 }
Jeff Hao7c8aa832016-06-06 11:09:20 -070085 // If class is erroneous, throw the earlier failure, wrapped in certain cases. See b/28787733.
86 if (c != nullptr && c->IsErroneous()) {
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070087 cl->ThrowEarlierClassFailure(c.Ptr());
Jeff Hao7c8aa832016-06-06 11:09:20 -070088 Thread* self = soa.Self();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070089 ObjPtr<mirror::Class> iae_class =
Jeff Hao7c8aa832016-06-06 11:09:20 -070090 self->DecodeJObject(WellKnownClasses::java_lang_IllegalAccessError)->AsClass();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070091 ObjPtr<mirror::Class> ncdfe_class =
Jeff Hao7c8aa832016-06-06 11:09:20 -070092 self->DecodeJObject(WellKnownClasses::java_lang_NoClassDefFoundError)->AsClass();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070093 ObjPtr<mirror::Class> exception = self->GetException()->GetClass();
Vladimir Marko72ab6842017-01-20 19:32:50 +000094 if (exception == iae_class || exception == ncdfe_class) {
Jeff Hao7c8aa832016-06-06 11:09:20 -070095 self->ThrowNewWrappedException("Ljava/lang/ClassNotFoundException;",
David Sehr709b0702016-10-13 09:12:37 -070096 c->PrettyDescriptor().c_str());
Jeff Hao7c8aa832016-06-06 11:09:20 -070097 }
98 return nullptr;
99 }
Andreas Gampe34ee6842014-12-02 15:43:52 -0800100
101 // Hard-coded performance optimization: We know that all failed libcore calls to findLoadedClass
102 // are followed by a call to the the classloader to actually
103 // load the class.
Mathieu Chartierab0ed822014-09-11 14:21:41 -0700104 if (loader != nullptr) {
105 // Try the common case.
106 StackHandleScope<1> hs(soa.Self());
Andreas Gampe34ee6842014-12-02 15:43:52 -0800107 c = VMClassLoader::FindClassInPathClassLoader(cl,
108 soa,
109 soa.Self(),
110 descriptor.c_str(),
111 descriptor_hash,
112 hs.NewHandle(loader));
Mathieu Chartierab0ed822014-09-11 14:21:41 -0700113 if (c != nullptr) {
114 return soa.AddLocalReference<jclass>(c);
115 }
116 }
Andreas Gampe34ee6842014-12-02 15:43:52 -0800117
118 // The class wasn't loaded, yet, and our fast-path did not apply (e.g., we didn't understand the
119 // classloader chain).
Ian Rogers7b078e82014-09-10 14:44:24 -0700120 return nullptr;
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700121}
122
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700123/*
Neil Fulleref486052015-06-04 12:24:08 +0000124 * Returns an array of entries from the boot classpath that could contain resources.
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700125 */
Neil Fulleref486052015-06-04 12:24:08 +0000126static jobjectArray VMClassLoader_getBootClassPathEntries(JNIEnv* env, jclass) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700127 const std::vector<const DexFile*>& path =
128 Runtime::Current()->GetClassLinker()->GetBootClassPath();
Vladimir Marko4b3d6902017-05-30 15:23:54 +0100129 jobjectArray array =
130 env->NewObjectArray(path.size(), WellKnownClasses::java_lang_String, nullptr);
131 if (array == nullptr) {
132 DCHECK(env->ExceptionCheck());
133 return nullptr;
134 }
Neil Fulleref486052015-06-04 12:24:08 +0000135 for (size_t i = 0; i < path.size(); ++i) {
136 const DexFile* dex_file = path[i];
Neil Fuller1e27c5b2015-06-03 15:46:29 +0000137
Calin Juravlea308a322017-07-18 16:51:51 -0700138 // For multidex locations, e.g., x.jar!classes2.dex, we want to look into x.jar.
Neil Fulleref486052015-06-04 12:24:08 +0000139 const std::string& location(dex_file->GetBaseLocation());
Neil Fuller1e27c5b2015-06-03 15:46:29 +0000140
Vladimir Marko4b3d6902017-05-30 15:23:54 +0100141 ScopedLocalRef<jstring> javaPath(env, env->NewStringUTF(location.c_str()));
142 if (javaPath.get() == nullptr) {
143 DCHECK(env->ExceptionCheck());
144 return nullptr;
145 }
146 env->SetObjectArrayElement(array, i, javaPath.get());
Neil Fuller1e27c5b2015-06-03 15:46:29 +0000147 }
Neil Fulleref486052015-06-04 12:24:08 +0000148 return array;
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700149}
150
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700151static JNINativeMethod gMethods[] = {
Igor Murashkin3b6f4402017-02-16 16:13:17 -0800152 FAST_NATIVE_METHOD(VMClassLoader, findLoadedClass, "(Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;"),
Neil Fulleref486052015-06-04 12:24:08 +0000153 NATIVE_METHOD(VMClassLoader, getBootClassPathEntries, "()[Ljava/lang/String;"),
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700154};
155
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700156void register_java_lang_VMClassLoader(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700157 REGISTER_NATIVE_METHODS("java/lang/VMClassLoader");
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700158}
159
160} // namespace art