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
17 #include "java_lang_VMClassLoader.h"
18
19 #include "base/zip_archive.h"
20 #include "class_linker.h"
21 #include "base/transform_iterator.h"
22 #include "base/stl_util.h"
23 #include "dex/descriptors_names.h"
24 #include "dex/dex_file_loader.h"
25 #include "dex/utf.h"
26 #include "handle_scope-inl.h"
27 #include "jni/jni_internal.h"
28 #include "mirror/class_loader.h"
29 #include "mirror/object-inl.h"
30 #include "mirror/object_array-alloc-inl.h"
31 #include "native_util.h"
32 #include "nativehelper/jni_macros.h"
33 #include "nativehelper/scoped_local_ref.h"
34 #include "nativehelper/scoped_utf_chars.h"
35 #include "obj_ptr.h"
36 #include "scoped_fast_native_object_access-inl.h"
37 #include "string_array_utils.h"
38 #include "thread-inl.h"
39 #include "well_known_classes-inl.h"
40
41 namespace art HIDDEN {
42
43 // A class so we can be friends with ClassLinker and access internal methods.
44 class VMClassLoader {
45 public:
LookupClass(ClassLinker * cl,Thread * self,const char * descriptor,size_t hash,ObjPtr<mirror::ClassLoader> class_loader)46 static ObjPtr<mirror::Class> LookupClass(ClassLinker* cl,
47 Thread* self,
48 const char* descriptor,
49 size_t hash,
50 ObjPtr<mirror::ClassLoader> class_loader)
51 REQUIRES(!Locks::classlinker_classes_lock_)
52 REQUIRES_SHARED(Locks::mutator_lock_) {
53 return cl->LookupClass(self, descriptor, hash, class_loader);
54 }
55
FindClassInPathClassLoader(ClassLinker * cl,Thread * self,const char * descriptor,size_t hash,Handle<mirror::ClassLoader> class_loader)56 static ObjPtr<mirror::Class> FindClassInPathClassLoader(ClassLinker* cl,
57 Thread* self,
58 const char* descriptor,
59 size_t hash,
60 Handle<mirror::ClassLoader> class_loader)
61 REQUIRES_SHARED(Locks::mutator_lock_) {
62 ObjPtr<mirror::Class> result;
63 if (cl->FindClassInBaseDexClassLoader(self, descriptor, hash, class_loader, &result)) {
64 DCHECK(!self->IsExceptionPending());
65 return result;
66 }
67 if (self->IsExceptionPending()) {
68 self->ClearException();
69 }
70 return nullptr;
71 }
72 };
73
VMClassLoader_findLoadedClass(JNIEnv * env,jclass,jobject javaLoader,jstring javaName)74 static jclass VMClassLoader_findLoadedClass(JNIEnv* env, jclass, jobject javaLoader,
75 jstring javaName) {
76 ScopedFastNativeObjectAccess soa(env);
77 ObjPtr<mirror::ClassLoader> loader = soa.Decode<mirror::ClassLoader>(javaLoader);
78 ScopedUtfChars name(env, javaName);
79 if (name.c_str() == nullptr) {
80 return nullptr;
81 }
82 ClassLinker* cl = Runtime::Current()->GetClassLinker();
83
84 // Compute hash once.
85 std::string descriptor(DotToDescriptor(name.c_str()));
86 const size_t descriptor_hash = ComputeModifiedUtf8Hash(descriptor.c_str());
87
88 ObjPtr<mirror::Class> c = VMClassLoader::LookupClass(cl,
89 soa.Self(),
90 descriptor.c_str(),
91 descriptor_hash,
92 loader);
93 if (c != nullptr && c->IsResolved()) {
94 return soa.AddLocalReference<jclass>(c);
95 }
96 // If class is erroneous, throw the earlier failure, wrapped in certain cases. See b/28787733.
97 if (c != nullptr && c->IsErroneous()) {
98 cl->ThrowEarlierClassFailure(c);
99 Thread* self = soa.Self();
100 ObjPtr<mirror::Class> exception_class = self->GetException()->GetClass();
101 if (exception_class == WellKnownClasses::java_lang_IllegalAccessError ||
102 exception_class == WellKnownClasses::java_lang_NoClassDefFoundError) {
103 self->ThrowNewWrappedException("Ljava/lang/ClassNotFoundException;",
104 c->PrettyDescriptor().c_str());
105 }
106 return nullptr;
107 }
108
109 // Hard-coded performance optimization: We know that all failed libcore calls to findLoadedClass
110 // are followed by a call to the the classloader to actually
111 // load the class.
112 if (loader != nullptr) {
113 // Try the common case.
114 StackHandleScope<1> hs(soa.Self());
115 c = VMClassLoader::FindClassInPathClassLoader(cl,
116 soa.Self(),
117 descriptor.c_str(),
118 descriptor_hash,
119 hs.NewHandle(loader));
120 if (c != nullptr) {
121 return soa.AddLocalReference<jclass>(c);
122 }
123 }
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).
127 return nullptr;
128 }
129
130 /*
131 * Returns an array of entries from the boot classpath that could contain resources.
132 */
VMClassLoader_getBootClassPathEntries(JNIEnv * env,jclass)133 static jobjectArray VMClassLoader_getBootClassPathEntries(JNIEnv* env, jclass) {
134 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
135 const std::vector<const DexFile*>& path = class_linker->GetBootClassPath();
136 auto is_base_dex = [](const DexFile* dex_file) {
137 return !DexFileLoader::IsMultiDexLocation(dex_file->GetLocation());
138 };
139 size_t jar_count = std::count_if(path.begin(), path.end(), is_base_dex);
140
141 const DexFile* last_dex_file = nullptr;
142 auto dchecked_is_base_dex = [&](const DexFile* dex_file) {
143 // For multidex locations, e.g., x.jar!classes2.dex, we want to look into x.jar.
144 // But we do not need to look into the base dex file more than once so we filter
145 // out multidex locations using the fact that they follow the base location.
146 if (kIsDebugBuild) {
147 if (is_base_dex(dex_file)) {
148 CHECK_EQ(DexFileLoader::GetBaseLocation(dex_file->GetLocation().c_str()),
149 dex_file->GetLocation());
150 } else {
151 CHECK(last_dex_file != nullptr);
152 CHECK_EQ(DexFileLoader::GetBaseLocation(dex_file->GetLocation().c_str()),
153 DexFileLoader::GetBaseLocation(last_dex_file->GetLocation().c_str()));
154 }
155 last_dex_file = dex_file;
156 }
157 return is_base_dex(dex_file);
158 };
159 auto get_location = [](const DexFile* dex_file) { return dex_file->GetLocation(); };
160 ScopedObjectAccess soa(Thread::ForEnv(env));
161 return soa.AddLocalReference<jobjectArray>(CreateStringArray(
162 soa.Self(),
163 jar_count,
164 MakeTransformRange(Filter(path, dchecked_is_base_dex), get_location)));
165 }
166
167 static JNINativeMethod gMethods[] = {
168 FAST_NATIVE_METHOD(VMClassLoader, findLoadedClass, "(Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;"),
169 NATIVE_METHOD(VMClassLoader, getBootClassPathEntries, "()[Ljava/lang/String;"),
170 };
171
register_java_lang_VMClassLoader(JNIEnv * env)172 void register_java_lang_VMClassLoader(JNIEnv* env) {
173 REGISTER_NATIVE_METHODS("java/lang/VMClassLoader");
174 }
175
176 } // namespace art
177