Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 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 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 17 | #include "java_lang_VMClassLoader.h" |
| 18 | |
David Sehr | 79e2607 | 2018-04-06 17:58:50 -0700 | [diff] [blame] | 19 | #include "base/zip_archive.h" |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 20 | #include "class_linker.h" |
David Sehr | b2ec9f5 | 2018-02-21 13:20:31 -0800 | [diff] [blame] | 21 | #include "dex/descriptors_names.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 22 | #include "dex/dex_file_loader.h" |
Vladimir Marko | 5924a4a | 2018-05-29 17:40:41 +0100 | [diff] [blame] | 23 | #include "dex/utf.h" |
Andreas Gampe | 88dbad3 | 2018-06-26 19:54:12 -0700 | [diff] [blame] | 24 | #include "handle_scope-inl.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 25 | #include "jni/jni_internal.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 26 | #include "mirror/class_loader.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 27 | #include "mirror/object-inl.h" |
Andreas Gampe | 87583b3 | 2017-05-25 11:22:18 -0700 | [diff] [blame] | 28 | #include "native_util.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 29 | #include "nativehelper/jni_macros.h" |
Andreas Gampe | 373a9b5 | 2017-10-18 09:01:57 -0700 | [diff] [blame] | 30 | #include "nativehelper/scoped_local_ref.h" |
| 31 | #include "nativehelper/scoped_utf_chars.h" |
Andreas Gampe | 34ee684 | 2014-12-02 15:43:52 -0800 | [diff] [blame] | 32 | #include "obj_ptr.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 33 | #include "scoped_fast_native_object_access-inl.h" |
Andreas Gampe | a1d2f95 | 2017-04-20 22:53:58 -0700 | [diff] [blame] | 34 | #include "well_known_classes.h" |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 35 | |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 36 | namespace art { |
| 37 | |
Andreas Gampe | 34ee684 | 2014-12-02 15:43:52 -0800 | [diff] [blame] | 38 | // A class so we can be friends with ClassLinker and access internal methods. |
| 39 | class VMClassLoader { |
| 40 | public: |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 41 | 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 Gampe | 34ee684 | 2014-12-02 15:43:52 -0800 | [diff] [blame] | 46 | 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 Geoffray | 7d8d8ff | 2016-11-02 12:38:05 +0000 | [diff] [blame] | 59 | if (cl->FindClassInBaseDexClassLoader(soa, self, descriptor, hash, class_loader, &result)) { |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 60 | DCHECK(!self->IsExceptionPending()); |
Andreas Gampe | 34ee684 | 2014-12-02 15:43:52 -0800 | [diff] [blame] | 61 | return result; |
| 62 | } |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 63 | if (self->IsExceptionPending()) { |
| 64 | self->ClearException(); |
| 65 | } |
Andreas Gampe | 34ee684 | 2014-12-02 15:43:52 -0800 | [diff] [blame] | 66 | return nullptr; |
| 67 | } |
| 68 | }; |
| 69 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 70 | static jclass VMClassLoader_findLoadedClass(JNIEnv* env, jclass, jobject javaLoader, |
| 71 | jstring javaName) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 72 | ScopedFastNativeObjectAccess soa(env); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 73 | ObjPtr<mirror::ClassLoader> loader = soa.Decode<mirror::ClassLoader>(javaLoader); |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 74 | ScopedUtfChars name(env, javaName); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 75 | if (name.c_str() == nullptr) { |
| 76 | return nullptr; |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 77 | } |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 78 | ClassLinker* cl = Runtime::Current()->GetClassLinker(); |
Andreas Gampe | 34ee684 | 2014-12-02 15:43:52 -0800 | [diff] [blame] | 79 | |
| 80 | // Compute hash once. |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 81 | std::string descriptor(DotToDescriptor(name.c_str())); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 82 | const size_t descriptor_hash = ComputeModifiedUtf8Hash(descriptor.c_str()); |
Andreas Gampe | 34ee684 | 2014-12-02 15:43:52 -0800 | [diff] [blame] | 83 | |
| 84 | ObjPtr<mirror::Class> c = VMClassLoader::LookupClass(cl, |
| 85 | soa.Self(), |
| 86 | descriptor.c_str(), |
| 87 | descriptor_hash, |
| 88 | loader); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 89 | if (c != nullptr && c->IsResolved()) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 90 | return soa.AddLocalReference<jclass>(c); |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 91 | } |
Jeff Hao | 7c8aa83 | 2016-06-06 11:09:20 -0700 | [diff] [blame] | 92 | // If class is erroneous, throw the earlier failure, wrapped in certain cases. See b/28787733. |
| 93 | if (c != nullptr && c->IsErroneous()) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 94 | cl->ThrowEarlierClassFailure(c); |
Jeff Hao | 7c8aa83 | 2016-06-06 11:09:20 -0700 | [diff] [blame] | 95 | Thread* self = soa.Self(); |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 96 | ObjPtr<mirror::Class> iae_class = |
Jeff Hao | 7c8aa83 | 2016-06-06 11:09:20 -0700 | [diff] [blame] | 97 | self->DecodeJObject(WellKnownClasses::java_lang_IllegalAccessError)->AsClass(); |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 98 | ObjPtr<mirror::Class> ncdfe_class = |
Jeff Hao | 7c8aa83 | 2016-06-06 11:09:20 -0700 | [diff] [blame] | 99 | self->DecodeJObject(WellKnownClasses::java_lang_NoClassDefFoundError)->AsClass(); |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 100 | ObjPtr<mirror::Class> exception = self->GetException()->GetClass(); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 101 | if (exception == iae_class || exception == ncdfe_class) { |
Jeff Hao | 7c8aa83 | 2016-06-06 11:09:20 -0700 | [diff] [blame] | 102 | self->ThrowNewWrappedException("Ljava/lang/ClassNotFoundException;", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 103 | c->PrettyDescriptor().c_str()); |
Jeff Hao | 7c8aa83 | 2016-06-06 11:09:20 -0700 | [diff] [blame] | 104 | } |
| 105 | return nullptr; |
| 106 | } |
Andreas Gampe | 34ee684 | 2014-12-02 15:43:52 -0800 | [diff] [blame] | 107 | |
| 108 | // Hard-coded performance optimization: We know that all failed libcore calls to findLoadedClass |
| 109 | // are followed by a call to the the classloader to actually |
| 110 | // load the class. |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 111 | if (loader != nullptr) { |
| 112 | // Try the common case. |
| 113 | StackHandleScope<1> hs(soa.Self()); |
Andreas Gampe | 34ee684 | 2014-12-02 15:43:52 -0800 | [diff] [blame] | 114 | c = VMClassLoader::FindClassInPathClassLoader(cl, |
| 115 | soa, |
| 116 | soa.Self(), |
| 117 | descriptor.c_str(), |
| 118 | descriptor_hash, |
| 119 | hs.NewHandle(loader)); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 120 | if (c != nullptr) { |
| 121 | return soa.AddLocalReference<jclass>(c); |
| 122 | } |
| 123 | } |
Andreas Gampe | 34ee684 | 2014-12-02 15:43:52 -0800 | [diff] [blame] | 124 | |
| 125 | // The class wasn't loaded, yet, and our fast-path did not apply (e.g., we didn't understand the |
| 126 | // classloader chain). |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 127 | return nullptr; |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 130 | /* |
Neil Fuller | ef48605 | 2015-06-04 12:24:08 +0000 | [diff] [blame] | 131 | * Returns an array of entries from the boot classpath that could contain resources. |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 132 | */ |
Neil Fuller | ef48605 | 2015-06-04 12:24:08 +0000 | [diff] [blame] | 133 | static jobjectArray VMClassLoader_getBootClassPathEntries(JNIEnv* env, jclass) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 134 | const std::vector<const DexFile*>& path = |
| 135 | Runtime::Current()->GetClassLinker()->GetBootClassPath(); |
Vladimir Marko | 4b3d690 | 2017-05-30 15:23:54 +0100 | [diff] [blame] | 136 | jobjectArray array = |
| 137 | env->NewObjectArray(path.size(), WellKnownClasses::java_lang_String, nullptr); |
| 138 | if (array == nullptr) { |
| 139 | DCHECK(env->ExceptionCheck()); |
| 140 | return nullptr; |
| 141 | } |
Neil Fuller | ef48605 | 2015-06-04 12:24:08 +0000 | [diff] [blame] | 142 | for (size_t i = 0; i < path.size(); ++i) { |
| 143 | const DexFile* dex_file = path[i]; |
Neil Fuller | 1e27c5b | 2015-06-03 15:46:29 +0000 | [diff] [blame] | 144 | |
Calin Juravle | a308a32 | 2017-07-18 16:51:51 -0700 | [diff] [blame] | 145 | // For multidex locations, e.g., x.jar!classes2.dex, we want to look into x.jar. |
Ulyana Trafimovich | e886d68 | 2020-07-16 15:09:38 +0000 | [diff] [blame^] | 146 | std::string location(DexFileLoader::GetBaseLocation(dex_file->GetLocation())); |
| 147 | if (Runtime::SimulatorMode()) { |
| 148 | location = getenv("ANDROID_PRODUCT_OUT") + location; |
| 149 | } |
Vladimir Marko | 4b3d690 | 2017-05-30 15:23:54 +0100 | [diff] [blame] | 150 | ScopedLocalRef<jstring> javaPath(env, env->NewStringUTF(location.c_str())); |
| 151 | if (javaPath.get() == nullptr) { |
| 152 | DCHECK(env->ExceptionCheck()); |
| 153 | return nullptr; |
| 154 | } |
| 155 | env->SetObjectArrayElement(array, i, javaPath.get()); |
Neil Fuller | 1e27c5b | 2015-06-03 15:46:29 +0000 | [diff] [blame] | 156 | } |
Neil Fuller | ef48605 | 2015-06-04 12:24:08 +0000 | [diff] [blame] | 157 | return array; |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 160 | static JNINativeMethod gMethods[] = { |
Igor Murashkin | 3b6f440 | 2017-02-16 16:13:17 -0800 | [diff] [blame] | 161 | FAST_NATIVE_METHOD(VMClassLoader, findLoadedClass, "(Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;"), |
Neil Fuller | ef48605 | 2015-06-04 12:24:08 +0000 | [diff] [blame] | 162 | NATIVE_METHOD(VMClassLoader, getBootClassPathEntries, "()[Ljava/lang/String;"), |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 163 | }; |
| 164 | |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 165 | void register_java_lang_VMClassLoader(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 166 | REGISTER_NATIVE_METHODS("java/lang/VMClassLoader"); |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | } // namespace art |