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