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" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 23 | #include "scoped_fast_native_object_access-inl.h" |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 24 | #include "ScopedUtfChars.h" |
| 25 | #include "zip_archive.h" |
| 26 | |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 27 | namespace art { |
| 28 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 29 | static jclass VMClassLoader_findLoadedClass(JNIEnv* env, jclass, jobject javaLoader, |
| 30 | jstring javaName) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 31 | ScopedFastNativeObjectAccess soa(env); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 32 | ObjPtr<mirror::ClassLoader> loader = soa.Decode<mirror::ClassLoader>(javaLoader); |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 33 | ScopedUtfChars name(env, javaName); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 34 | if (name.c_str() == nullptr) { |
| 35 | return nullptr; |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 36 | } |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 37 | ClassLinker* cl = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 38 | std::string descriptor(DotToDescriptor(name.c_str())); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 39 | const size_t descriptor_hash = ComputeModifiedUtf8Hash(descriptor.c_str()); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 40 | mirror::Class* c = cl->LookupClass(soa.Self(), |
| 41 | descriptor.c_str(), |
| 42 | descriptor_hash, |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 43 | loader.Ptr()); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 44 | if (c != nullptr && c->IsResolved()) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 45 | return soa.AddLocalReference<jclass>(c); |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 46 | } |
Jeff Hao | 7c8aa83 | 2016-06-06 11:09:20 -0700 | [diff] [blame] | 47 | // If class is erroneous, throw the earlier failure, wrapped in certain cases. See b/28787733. |
| 48 | if (c != nullptr && c->IsErroneous()) { |
| 49 | cl->ThrowEarlierClassFailure(c); |
| 50 | Thread* self = soa.Self(); |
| 51 | mirror::Class* eiie_class = |
| 52 | self->DecodeJObject(WellKnownClasses::java_lang_ExceptionInInitializerError)->AsClass(); |
| 53 | mirror::Class* iae_class = |
| 54 | self->DecodeJObject(WellKnownClasses::java_lang_IllegalAccessError)->AsClass(); |
| 55 | mirror::Class* ncdfe_class = |
| 56 | self->DecodeJObject(WellKnownClasses::java_lang_NoClassDefFoundError)->AsClass(); |
| 57 | mirror::Class* exception = self->GetException()->GetClass(); |
| 58 | if (exception == eiie_class || exception == iae_class || exception == ncdfe_class) { |
| 59 | self->ThrowNewWrappedException("Ljava/lang/ClassNotFoundException;", |
| 60 | PrettyDescriptor(c).c_str()); |
| 61 | } |
| 62 | return nullptr; |
| 63 | } |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 64 | if (loader != nullptr) { |
| 65 | // Try the common case. |
| 66 | StackHandleScope<1> hs(soa.Self()); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 67 | cl->FindClassInPathClassLoader(soa, soa.Self(), descriptor.c_str(), descriptor_hash, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 68 | hs.NewHandle(loader), &c); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 69 | if (c != nullptr) { |
| 70 | return soa.AddLocalReference<jclass>(c); |
| 71 | } |
| 72 | } |
| 73 | // Class wasn't resolved so it may be erroneous or not yet ready, force the caller to go into |
| 74 | // the regular loadClass code. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 75 | return nullptr; |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 78 | /* |
Neil Fuller | ef48605 | 2015-06-04 12:24:08 +0000 | [diff] [blame] | 79 | * 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] | 80 | */ |
Neil Fuller | ef48605 | 2015-06-04 12:24:08 +0000 | [diff] [blame] | 81 | static jobjectArray VMClassLoader_getBootClassPathEntries(JNIEnv* env, jclass) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 82 | const std::vector<const DexFile*>& path = |
| 83 | Runtime::Current()->GetClassLinker()->GetBootClassPath(); |
Neil Fuller | ef48605 | 2015-06-04 12:24:08 +0000 | [diff] [blame] | 84 | jclass stringClass = env->FindClass("java/lang/String"); |
| 85 | jobjectArray array = env->NewObjectArray(path.size(), stringClass, nullptr); |
| 86 | for (size_t i = 0; i < path.size(); ++i) { |
| 87 | const DexFile* dex_file = path[i]; |
Neil Fuller | 1e27c5b | 2015-06-03 15:46:29 +0000 | [diff] [blame] | 88 | |
Neil Fuller | ef48605 | 2015-06-04 12:24:08 +0000 | [diff] [blame] | 89 | // For multidex locations, e.g., x.jar:classes2.dex, we want to look into x.jar. |
| 90 | const std::string& location(dex_file->GetBaseLocation()); |
Neil Fuller | 1e27c5b | 2015-06-03 15:46:29 +0000 | [diff] [blame] | 91 | |
Neil Fuller | ef48605 | 2015-06-04 12:24:08 +0000 | [diff] [blame] | 92 | jstring javaPath = env->NewStringUTF(location.c_str()); |
| 93 | env->SetObjectArrayElement(array, i, javaPath); |
Neil Fuller | 1e27c5b | 2015-06-03 15:46:29 +0000 | [diff] [blame] | 94 | } |
Neil Fuller | ef48605 | 2015-06-04 12:24:08 +0000 | [diff] [blame] | 95 | return array; |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 98 | static JNINativeMethod gMethods[] = { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 99 | 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] | 100 | NATIVE_METHOD(VMClassLoader, getBootClassPathEntries, "()[Ljava/lang/String;"), |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 103 | void register_java_lang_VMClassLoader(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 104 | REGISTER_NATIVE_METHODS("java/lang/VMClassLoader"); |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | } // namespace art |