blob: a2551412ffc346901dd4e06e2d1f328bfaaed370 [file] [log] [blame]
Brian Carlstromf91c8c32011-09-21 17:30:34 -07001/*
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
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -070017#include <unistd.h>
18
Brian Carlstromaded5f72011-10-07 17:15:04 -070019#include "class_loader.h"
20#include "class_linker.h"
21#include "dex_file.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070022#include "logging.h"
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -070023#include "os.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070024#include "runtime.h"
jeffhaoc393a4f2011-10-19 13:46:09 -070025#include "zip_archive.h"
Brian Carlstrom03a20ba2011-10-13 10:24:13 -070026#include "toStringArray.h"
Ian Rogersc9818482012-01-11 08:52:51 -080027#include "ScopedLocalRef.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070028#include "ScopedUtfChars.h"
29
30#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
31
32namespace art {
33
34namespace {
35
36// A smart pointer that provides read-only access to a Java string's UTF chars.
37// Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if
38// passed a null jstring. The correct idiom is:
39//
40// NullableScopedUtfChars name(env, javaName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -070041// if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070042// return NULL;
43// }
44// // ... use name.c_str()
45//
46// TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option.
47class NullableScopedUtfChars {
Elliott Hughesba8eee12012-01-24 20:25:24 -080048 public:
49 NullableScopedUtfChars(JNIEnv* env, jstring s) : mEnv(env), mString(s) {
50 mUtfChars = (s != NULL) ? env->GetStringUTFChars(s, NULL) : NULL;
51 }
52
53 ~NullableScopedUtfChars() {
54 if (mUtfChars) {
55 mEnv->ReleaseStringUTFChars(mString, mUtfChars);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070056 }
Elliott Hughesba8eee12012-01-24 20:25:24 -080057 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070058
Elliott Hughesba8eee12012-01-24 20:25:24 -080059 const char* c_str() const {
60 return mUtfChars;
61 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070062
Elliott Hughesba8eee12012-01-24 20:25:24 -080063 size_t size() const {
64 return strlen(mUtfChars);
65 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070066
Elliott Hughesba8eee12012-01-24 20:25:24 -080067 // Element access.
68 const char& operator[](size_t n) const {
69 return mUtfChars[n];
70 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070071
Elliott Hughesba8eee12012-01-24 20:25:24 -080072 private:
73 JNIEnv* mEnv;
74 jstring mString;
75 const char* mUtfChars;
Brian Carlstromf91c8c32011-09-21 17:30:34 -070076
Elliott Hughesba8eee12012-01-24 20:25:24 -080077 // Disallow copy and assignment.
78 NullableScopedUtfChars(const NullableScopedUtfChars&);
79 void operator=(const NullableScopedUtfChars&);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070080};
81
jeffhaoc393a4f2011-10-19 13:46:09 -070082static jint DexFile_openDexFile(JNIEnv* env, jclass, jstring javaSourceName, jstring javaOutputName, jint) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070083 ScopedUtfChars sourceName(env, javaSourceName);
84 if (sourceName.c_str() == NULL) {
85 return 0;
86 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -080087 std::string source(sourceName.c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -070088 NullableScopedUtfChars outputName(env, javaOutputName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -070089 if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070090 return 0;
91 }
jeffhaoc393a4f2011-10-19 13:46:09 -070092 const DexFile* dex_file;
93 if (outputName.c_str() == NULL) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -080094 dex_file = Runtime::Current()->GetClassLinker()->FindDexFileInOatFileFromDexLocation(source);
jeffhaoc393a4f2011-10-19 13:46:09 -070095 } else {
Brian Carlstrom5b332c82012-02-01 15:02:31 -080096 std::string output(outputName.c_str());
97 dex_file = Runtime::Current()->GetClassLinker()->FindOrCreateOatFileForDexLocation(source, output);
jeffhaoc393a4f2011-10-19 13:46:09 -070098 }
Brian Carlstromaded5f72011-10-07 17:15:04 -070099 if (dex_file == NULL) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800100 LOG(WARNING) << "Failed to open dex file: " << source;
Brian Carlstromd601af82012-01-06 10:15:19 -0800101 jniThrowExceptionFmt(env, "java/io/IOException", "unable to open dex file: %s",
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800102 source.c_str());
jeffhaoc393a4f2011-10-19 13:46:09 -0700103 return 0;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700104 }
105 return static_cast<jint>(reinterpret_cast<uintptr_t>(dex_file));
106}
107
108static const DexFile* toDexFile(JNIEnv* env, int dex_file_address) {
109 const DexFile* dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(dex_file_address));
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800110 if (dex_file == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700111 jniThrowNullPointerException(env, "dex_file == null");
112 }
113 return dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700114}
115
116void DexFile_closeDexFile(JNIEnv* env, jclass, jint cookie) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700117 const DexFile* dex_file = toDexFile(env, cookie);
118 if (dex_file == NULL) {
119 return;
120 }
121 if (Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) {
122 return;
123 }
124 delete dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700125}
126
127jclass DexFile_defineClass(JNIEnv* env, jclass, jstring javaName, jobject javaLoader, jint cookie) {
Brian Carlstromb82b6872011-10-26 17:18:07 -0700128 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700129 const DexFile* dex_file = toDexFile(env, cookie);
130 if (dex_file == NULL) {
131 return NULL;
132 }
Brian Carlstromdf143242011-10-10 18:05:34 -0700133 ScopedUtfChars class_name(env, javaName);
134 if (class_name.c_str() == NULL) {
135 return NULL;
136 }
Elliott Hughes95572412011-12-13 18:14:20 -0800137 const std::string descriptor(DotToDescriptor(class_name.c_str()));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700138 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
139 if (dex_class_def == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700140 return NULL;
141 }
142
143 Object* class_loader_object = Decode<Object*>(env, javaLoader);
144 ClassLoader* class_loader = down_cast<ClassLoader*>(class_loader_object);
145 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
146 class_linker->RegisterDexFile(*dex_file);
147 Class* result = class_linker->DefineClass(descriptor, class_loader, *dex_file, *dex_class_def);
Brian Carlstrom41ec4772011-10-11 01:35:58 -0700148 if (env->ExceptionCheck()) {
Elliott Hughes7bfc9632012-01-26 12:02:54 -0800149 // Swallow any ClassNotFoundException or NoClassDefFoundError; the contract with the caller
150 // is that we return null if the class is not found.
Ian Rogerscab01012012-01-10 17:35:46 -0800151 jthrowable exception = env->ExceptionOccurred();
Brian Carlstrom41ec4772011-10-11 01:35:58 -0700152 env->ExceptionClear();
Elliott Hughes7bfc9632012-01-26 12:02:54 -0800153
154 static jclass ClassNotFoundException_class = CacheClass(env, "java/lang/ClassNotFoundException");
155 static jclass NoClassDefFoundError_class = CacheClass(env, "java/lang/NoClassDefFoundError");
156
157 if (!env->IsInstanceOf(exception, ClassNotFoundException_class) && !env->IsInstanceOf(exception, NoClassDefFoundError_class)) {
Ian Rogerscab01012012-01-10 17:35:46 -0800158 env->Throw(exception);
159 }
Brian Carlstrom41ec4772011-10-11 01:35:58 -0700160 return NULL;
161 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700162 return AddLocalReference<jclass>(env, result);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700163}
164
165jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jint cookie) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700166 const DexFile* dex_file = toDexFile(env, cookie);
167 if (dex_file == NULL) {
168 return NULL;
169 }
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700170
171 std::vector<std::string> class_names;
172 for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
173 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
174 const char* descriptor = dex_file->GetClassDescriptor(class_def);
175 class_names.push_back(DescriptorToDot(descriptor));
176 }
177 return toStringArray(env, class_names);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700178}
179
180jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700181 ScopedUtfChars filename(env, javaFilename);
182 if (filename.c_str() == NULL) {
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700183 return JNI_TRUE;
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700184 }
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700185
186 if (!OS::FileExists(filename.c_str())) {
187 jniThrowExceptionFmt(env, "java/io/FileNotFoundException", "%s", filename.c_str());
188 return JNI_TRUE;
189 }
190
191 // Always treat elements of the bootclasspath as up-to-date. The
192 // fact that code is running at all means that this should be true.
193 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
194 const std::vector<const DexFile*>& boot_class_path = class_linker->GetBootClassPath();
195 for (size_t i = 0; i < boot_class_path.size(); i++) {
196 if (boot_class_path[i]->GetLocation() == filename.c_str()) {
197 return JNI_FALSE;
198 }
199 }
200
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800201 uint32_t location_checksum;
202 if (!DexFile::GetChecksum(filename.c_str(), location_checksum)) {
Brian Carlstromfad71432011-10-16 20:25:10 -0700203 return JNI_TRUE;
204 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800205
206 std::string oat_filename(OatFile::DexFilenameToOatFilename(filename.c_str()));
207 const OatFile* oat_file(class_linker->FindOatFileFromOatLocation(oat_filename));
208 if (oat_file == NULL) {
209 return JNI_TRUE;
210 }
211
212 const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(filename.c_str());
213 if (oat_dex_file == NULL) {
214 return JNI_TRUE;
215 }
216
217 if (location_checksum != oat_dex_file->GetDexFileLocationChecksum()) {
218 return JNI_TRUE;
219 }
220
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700221 return JNI_FALSE;
222}
223
224static JNINativeMethod gMethods[] = {
225 NATIVE_METHOD(DexFile, closeDexFile, "(I)V"),
226 NATIVE_METHOD(DexFile, defineClass, "(Ljava/lang/String;Ljava/lang/ClassLoader;I)Ljava/lang/Class;"),
227 NATIVE_METHOD(DexFile, getClassNameList, "(I)[Ljava/lang/String;"),
228 NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
229 NATIVE_METHOD(DexFile, openDexFile, "(Ljava/lang/String;Ljava/lang/String;I)I"),
230};
231
232} // namespace
233
234void register_dalvik_system_DexFile(JNIEnv* env) {
235 jniRegisterNativeMethods(env, "dalvik/system/DexFile", gMethods, NELEM(gMethods));
236}
237
238} // namespace art