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 "class_linker.h"
18#include "jni_internal.h"
19#include "mirror/class_loader.h"
20#include "mirror/object-inl.h"
21#include "scoped_fast_native_object_access.h"
22#include "ScopedUtfChars.h"
23#include "zip_archive.h"
24
25namespace art {
26
27static jclass VMClassLoader_findLoadedClass(JNIEnv* env, jclass, jobject javaLoader, jstring javaName) {
28  ScopedFastNativeObjectAccess soa(env);
29  mirror::ClassLoader* loader = soa.Decode<mirror::ClassLoader*>(javaLoader);
30  ScopedUtfChars name(env, javaName);
31  if (name.c_str() == nullptr) {
32    return nullptr;
33  }
34  ClassLinker* cl = Runtime::Current()->GetClassLinker();
35  std::string descriptor(DotToDescriptor(name.c_str()));
36  const size_t descriptor_hash = ComputeModifiedUtf8Hash(descriptor.c_str());
37  mirror::Class* c = cl->LookupClass(descriptor.c_str(), descriptor_hash, loader);
38  if (c != nullptr && c->IsResolved()) {
39    return soa.AddLocalReference<jclass>(c);
40  }
41  if (loader != nullptr) {
42    // Try the common case.
43    StackHandleScope<1> hs(soa.Self());
44    c = cl->FindClassInPathClassLoader(soa, soa.Self(), descriptor.c_str(), descriptor_hash,
45                                       hs.NewHandle(loader));
46    if (c != nullptr) {
47      return soa.AddLocalReference<jclass>(c);
48    }
49  }
50  // Class wasn't resolved so it may be erroneous or not yet ready, force the caller to go into
51  // the regular loadClass code.
52  return nullptr;
53}
54
55static jint VMClassLoader_getBootClassPathSize(JNIEnv*, jclass) {
56  return Runtime::Current()->GetClassLinker()->GetBootClassPath().size();
57}
58
59/*
60 * Returns a string URL for a resource with the specified 'javaName' in
61 * entry 'index' of the boot class path.
62 *
63 * We return a newly-allocated String in the following form:
64 *
65 *   jar:file://path!/name
66 *
67 * Where "path" is the bootstrap class path entry and "name" is the string
68 * passed into this method.  "path" needs to be an absolute path (starting
69 * with '/'); if it's not we'd need to make it absolute as part of forming
70 * the URL string.
71 */
72static jstring VMClassLoader_getBootClassPathResource(JNIEnv* env, jclass, jstring javaName, jint index) {
73  ScopedUtfChars name(env, javaName);
74  if (name.c_str() == nullptr) {
75    return nullptr;
76  }
77
78  const std::vector<const DexFile*>& path = Runtime::Current()->GetClassLinker()->GetBootClassPath();
79  if (index < 0 || size_t(index) >= path.size()) {
80    return nullptr;
81  }
82  const DexFile* dex_file = path[index];
83
84  // For multidex locations, e.g., x.jar:classes2.dex, we want to look into x.jar.
85  const std::string& location(dex_file->GetBaseLocation());
86
87  std::string error_msg;
88  std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(location.c_str(), &error_msg));
89  if (zip_archive.get() == nullptr) {
90    LOG(WARNING) << "Failed to open zip archive '" << location << "': " << error_msg;
91    return nullptr;
92  }
93  std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(name.c_str(), &error_msg));
94  if (zip_entry.get() == nullptr) {
95    return nullptr;
96  }
97
98  std::string url;
99  StringAppendF(&url, "jar:file://%s!/%s", location.c_str(), name.c_str());
100  return env->NewStringUTF(url.c_str());
101}
102
103static JNINativeMethod gMethods[] = {
104  NATIVE_METHOD(VMClassLoader, findLoadedClass, "!(Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;"),
105  NATIVE_METHOD(VMClassLoader, getBootClassPathResource, "(Ljava/lang/String;I)Ljava/lang/String;"),
106  NATIVE_METHOD(VMClassLoader, getBootClassPathSize, "!()I"),
107};
108
109void register_java_lang_VMClassLoader(JNIEnv* env) {
110  REGISTER_NATIVE_METHODS("java/lang/VMClassLoader");
111}
112
113}  // namespace art
114