blob: 46162c1989f7b2c26ff2c60f99d058db2950f00d [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
David Sehr79e26072018-04-06 17:58:50 -070019#include "base/zip_archive.h"
Elliott Hughes64bf5a32011-09-20 14:43:12 -070020#include "class_linker.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080021#include "dex/descriptors_names.h"
David Sehr9e734c72018-01-04 17:56:19 -080022#include "dex/dex_file_loader.h"
Vladimir Marko5924a4a2018-05-29 17:40:41 +010023#include "dex/utf.h"
Andreas Gampe88dbad32018-06-26 19:54:12 -070024#include "handle_scope-inl.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010025#include "jni/jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "mirror/class_loader.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070027#include "mirror/object-inl.h"
Andreas Gampe87583b32017-05-25 11:22:18 -070028#include "native_util.h"
Steven Morelande431e272017-07-18 16:53:49 -070029#include "nativehelper/jni_macros.h"
Andreas Gampe373a9b52017-10-18 09:01:57 -070030#include "nativehelper/scoped_local_ref.h"
31#include "nativehelper/scoped_utf_chars.h"
Andreas Gampe34ee6842014-12-02 15:43:52 -080032#include "obj_ptr.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070033#include "scoped_fast_native_object_access-inl.h"
Andreas Gampea1d2f952017-04-20 22:53:58 -070034#include "well_known_classes.h"
Elliott Hughes64bf5a32011-09-20 14:43:12 -070035
Elliott Hughes64bf5a32011-09-20 14:43:12 -070036namespace art {
37
Andreas Gampe34ee6842014-12-02 15:43:52 -080038// A class so we can be friends with ClassLinker and access internal methods.
39class VMClassLoader {
40 public:
Vladimir Markoa8bba7d2018-05-30 15:18:48 +010041 static ObjPtr<mirror::Class> LookupClass(ClassLinker* cl,
42 Thread* self,
43 const char* descriptor,
44 size_t hash,
45 ObjPtr<mirror::ClassLoader> class_loader)
Andreas Gampe34ee6842014-12-02 15:43:52 -080046 REQUIRES(!Locks::classlinker_classes_lock_)
47 REQUIRES_SHARED(Locks::mutator_lock_) {
48 return cl->LookupClass(self, descriptor, hash, class_loader);
49 }
50
51 static ObjPtr<mirror::Class> FindClassInPathClassLoader(ClassLinker* cl,
52 ScopedObjectAccessAlreadyRunnable& soa,
53 Thread* self,
54 const char* descriptor,
55 size_t hash,
56 Handle<mirror::ClassLoader> class_loader)
57 REQUIRES_SHARED(Locks::mutator_lock_) {
58 ObjPtr<mirror::Class> result;
Nicolas Geoffray7d8d8ff2016-11-02 12:38:05 +000059 if (cl->FindClassInBaseDexClassLoader(soa, self, descriptor, hash, class_loader, &result)) {
Andreas Gampe34ee6842014-12-02 15:43:52 -080060 return result;
61 }
62 return nullptr;
63 }
64};
65
Ian Rogers7b078e82014-09-10 14:44:24 -070066static jclass VMClassLoader_findLoadedClass(JNIEnv* env, jclass, jobject javaLoader,
67 jstring javaName) {
Ian Rogers53b8b092014-03-13 23:45:53 -070068 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -070069 ObjPtr<mirror::ClassLoader> loader = soa.Decode<mirror::ClassLoader>(javaLoader);
Elliott Hughes64bf5a32011-09-20 14:43:12 -070070 ScopedUtfChars name(env, javaName);
Ian Rogers7b078e82014-09-10 14:44:24 -070071 if (name.c_str() == nullptr) {
72 return nullptr;
Elliott Hughes64bf5a32011-09-20 14:43:12 -070073 }
Mathieu Chartierab0ed822014-09-11 14:21:41 -070074 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Andreas Gampe34ee6842014-12-02 15:43:52 -080075
76 // Compute hash once.
Brian Carlstromf91c8c32011-09-21 17:30:34 -070077 std::string descriptor(DotToDescriptor(name.c_str()));
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -080078 const size_t descriptor_hash = ComputeModifiedUtf8Hash(descriptor.c_str());
Andreas Gampe34ee6842014-12-02 15:43:52 -080079
80 ObjPtr<mirror::Class> c = VMClassLoader::LookupClass(cl,
81 soa.Self(),
82 descriptor.c_str(),
83 descriptor_hash,
84 loader);
Ian Rogers7b078e82014-09-10 14:44:24 -070085 if (c != nullptr && c->IsResolved()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070086 return soa.AddLocalReference<jclass>(c);
Ian Rogersbe125a92012-01-11 15:19:49 -080087 }
Jeff Hao7c8aa832016-06-06 11:09:20 -070088 // If class is erroneous, throw the earlier failure, wrapped in certain cases. See b/28787733.
89 if (c != nullptr && c->IsErroneous()) {
Vladimir Markobcf17522018-06-01 13:14:32 +010090 cl->ThrowEarlierClassFailure(c);
Jeff Hao7c8aa832016-06-06 11:09:20 -070091 Thread* self = soa.Self();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070092 ObjPtr<mirror::Class> iae_class =
Jeff Hao7c8aa832016-06-06 11:09:20 -070093 self->DecodeJObject(WellKnownClasses::java_lang_IllegalAccessError)->AsClass();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070094 ObjPtr<mirror::Class> ncdfe_class =
Jeff Hao7c8aa832016-06-06 11:09:20 -070095 self->DecodeJObject(WellKnownClasses::java_lang_NoClassDefFoundError)->AsClass();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070096 ObjPtr<mirror::Class> exception = self->GetException()->GetClass();
Vladimir Marko72ab6842017-01-20 19:32:50 +000097 if (exception == iae_class || exception == ncdfe_class) {
Jeff Hao7c8aa832016-06-06 11:09:20 -070098 self->ThrowNewWrappedException("Ljava/lang/ClassNotFoundException;",
David Sehr709b0702016-10-13 09:12:37 -070099 c->PrettyDescriptor().c_str());
Jeff Hao7c8aa832016-06-06 11:09:20 -0700100 }
101 return nullptr;
102 }
Andreas Gampe34ee6842014-12-02 15:43:52 -0800103
104 // Hard-coded performance optimization: We know that all failed libcore calls to findLoadedClass
105 // are followed by a call to the the classloader to actually
106 // load the class.
Mathieu Chartierab0ed822014-09-11 14:21:41 -0700107 if (loader != nullptr) {
108 // Try the common case.
109 StackHandleScope<1> hs(soa.Self());
Andreas Gampe34ee6842014-12-02 15:43:52 -0800110 c = VMClassLoader::FindClassInPathClassLoader(cl,
111 soa,
112 soa.Self(),
113 descriptor.c_str(),
114 descriptor_hash,
115 hs.NewHandle(loader));
Mathieu Chartierab0ed822014-09-11 14:21:41 -0700116 if (c != nullptr) {
117 return soa.AddLocalReference<jclass>(c);
118 }
119 }
Andreas Gampe34ee6842014-12-02 15:43:52 -0800120
121 // The class wasn't loaded, yet, and our fast-path did not apply (e.g., we didn't understand the
122 // classloader chain).
Ian Rogers7b078e82014-09-10 14:44:24 -0700123 return nullptr;
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700124}
125
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700126/*
Neil Fulleref486052015-06-04 12:24:08 +0000127 * Returns an array of entries from the boot classpath that could contain resources.
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700128 */
Neil Fulleref486052015-06-04 12:24:08 +0000129static jobjectArray VMClassLoader_getBootClassPathEntries(JNIEnv* env, jclass) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700130 const std::vector<const DexFile*>& path =
131 Runtime::Current()->GetClassLinker()->GetBootClassPath();
Vladimir Marko4b3d6902017-05-30 15:23:54 +0100132 jobjectArray array =
133 env->NewObjectArray(path.size(), WellKnownClasses::java_lang_String, nullptr);
134 if (array == nullptr) {
135 DCHECK(env->ExceptionCheck());
136 return nullptr;
137 }
Neil Fulleref486052015-06-04 12:24:08 +0000138 for (size_t i = 0; i < path.size(); ++i) {
139 const DexFile* dex_file = path[i];
Neil Fuller1e27c5b2015-06-03 15:46:29 +0000140
Calin Juravlea308a322017-07-18 16:51:51 -0700141 // For multidex locations, e.g., x.jar!classes2.dex, we want to look into x.jar.
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700142 const std::string location(DexFileLoader::GetBaseLocation(dex_file->GetLocation()));
Neil Fuller1e27c5b2015-06-03 15:46:29 +0000143
Vladimir Marko4b3d6902017-05-30 15:23:54 +0100144 ScopedLocalRef<jstring> javaPath(env, env->NewStringUTF(location.c_str()));
145 if (javaPath.get() == nullptr) {
146 DCHECK(env->ExceptionCheck());
147 return nullptr;
148 }
149 env->SetObjectArrayElement(array, i, javaPath.get());
Neil Fuller1e27c5b2015-06-03 15:46:29 +0000150 }
Neil Fulleref486052015-06-04 12:24:08 +0000151 return array;
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700152}
153
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700154static JNINativeMethod gMethods[] = {
Igor Murashkin3b6f4402017-02-16 16:13:17 -0800155 FAST_NATIVE_METHOD(VMClassLoader, findLoadedClass, "(Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;"),
Neil Fulleref486052015-06-04 12:24:08 +0000156 NATIVE_METHOD(VMClassLoader, getBootClassPathEntries, "()[Ljava/lang/String;"),
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700157};
158
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700159void register_java_lang_VMClassLoader(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700160 REGISTER_NATIVE_METHODS("java/lang/VMClassLoader");
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700161}
162
163} // namespace art