blob: dd91eacf4b48c04e288d3a3ee4299574e327489b [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 }
87 NullableScopedUtfChars outputName(env, javaOutputName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -070088 if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070089 return 0;
90 }
jeffhaoc393a4f2011-10-19 13:46:09 -070091 const DexFile* dex_file;
92 if (outputName.c_str() == NULL) {
93 dex_file = DexFile::Open(sourceName.c_str(), "");
94 } else {
jeffhao262bf462011-10-20 18:36:32 -070095 // Sanity check the arguments.
96 if (!IsValidZipFilename(sourceName.c_str()) || !IsValidDexFilename(outputName.c_str())) {
97 LOG(ERROR) << "Bad filenames extracting dex '" << outputName.c_str()
98 << "' from zip '" << sourceName.c_str() << "'";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -080099 return 0;
jeffhao262bf462011-10-20 18:36:32 -0700100 }
Brian Carlstromd601af82012-01-06 10:15:19 -0800101 // Generate the output oat file for the source dex file
jeffhao262bf462011-10-20 18:36:32 -0700102 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromd601af82012-01-06 10:15:19 -0800103 UniquePtr<File> file(OS::OpenFile(outputName.c_str(), true));
104 if (file.get() == NULL) {
Ian Rogers725aee52012-01-11 11:56:56 -0800105 LOG(WARNING) << "unable to create oat file: " << outputName.c_str();
Brian Carlstromd601af82012-01-06 10:15:19 -0800106 jniThrowExceptionFmt(env, "java/io/IOException", "unable to create oat file: %s",
107 outputName.c_str());
jeffhao262bf462011-10-20 18:36:32 -0700108 return 0;
109 }
Brian Carlstromd601af82012-01-06 10:15:19 -0800110 if (!class_linker->GenerateOatFile(sourceName.c_str(), file->Fd(), outputName.c_str())) {
Ian Rogers725aee52012-01-11 11:56:56 -0800111 LOG(WARNING) << "unable to generate oat file: " << outputName.c_str();
Brian Carlstromd601af82012-01-06 10:15:19 -0800112 jniThrowExceptionFmt(env, "java/io/IOException", "unable to generate oat file: %s",
113 outputName.c_str());
114 return 0;
115 }
116 UniquePtr<OatFile> oat_file(OatFile::Open(outputName.c_str(), "", NULL));
117 if (oat_file.get() == NULL) {
Ian Rogers725aee52012-01-11 11:56:56 -0800118 LOG(WARNING) << "unable to open oat file: " << outputName.c_str();
Brian Carlstromd601af82012-01-06 10:15:19 -0800119 jniThrowExceptionFmt(env, "java/io/IOException", "unable to open oat file: %s",
120 outputName.c_str());
121 return 0;
122 }
123 const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(sourceName.c_str());
124 if (oat_dex_file == NULL) {
Ian Rogers725aee52012-01-11 11:56:56 -0800125 LOG(WARNING) << "unable to find dex file in oat file: " << outputName.c_str();
Brian Carlstromd601af82012-01-06 10:15:19 -0800126 jniThrowExceptionFmt(env, "java/io/IOException", "unable to find dex file in oat file: %s",
127 outputName.c_str());
128 return 0;
129 }
Ian Rogers725aee52012-01-11 11:56:56 -0800130 Runtime::Current()->GetClassLinker()->RegisterOatFile(*oat_file.release());
Brian Carlstromd601af82012-01-06 10:15:19 -0800131 dex_file = oat_dex_file->OpenDexFile();
jeffhaoc393a4f2011-10-19 13:46:09 -0700132 }
jeffhao262bf462011-10-20 18:36:32 -0700133
Brian Carlstromaded5f72011-10-07 17:15:04 -0700134 if (dex_file == NULL) {
Ian Rogers725aee52012-01-11 11:56:56 -0800135 LOG(WARNING) << "unable to open dex file: " << sourceName.c_str();
Brian Carlstromd601af82012-01-06 10:15:19 -0800136 jniThrowExceptionFmt(env, "java/io/IOException", "unable to open dex file: %s",
Brian Carlstromaded5f72011-10-07 17:15:04 -0700137 sourceName.c_str());
jeffhaoc393a4f2011-10-19 13:46:09 -0700138 return 0;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700139 }
140 return static_cast<jint>(reinterpret_cast<uintptr_t>(dex_file));
141}
142
143static const DexFile* toDexFile(JNIEnv* env, int dex_file_address) {
144 const DexFile* dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(dex_file_address));
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800145 if (dex_file == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700146 jniThrowNullPointerException(env, "dex_file == null");
147 }
148 return dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700149}
150
151void DexFile_closeDexFile(JNIEnv* env, jclass, jint cookie) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700152 const DexFile* dex_file = toDexFile(env, cookie);
153 if (dex_file == NULL) {
154 return;
155 }
156 if (Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) {
157 return;
158 }
159 delete dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700160}
161
162jclass DexFile_defineClass(JNIEnv* env, jclass, jstring javaName, jobject javaLoader, jint cookie) {
Brian Carlstromb82b6872011-10-26 17:18:07 -0700163 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700164 const DexFile* dex_file = toDexFile(env, cookie);
165 if (dex_file == NULL) {
166 return NULL;
167 }
Brian Carlstromdf143242011-10-10 18:05:34 -0700168 ScopedUtfChars class_name(env, javaName);
169 if (class_name.c_str() == NULL) {
170 return NULL;
171 }
Elliott Hughes95572412011-12-13 18:14:20 -0800172 const std::string descriptor(DotToDescriptor(class_name.c_str()));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700173 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
174 if (dex_class_def == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700175 return NULL;
176 }
177
178 Object* class_loader_object = Decode<Object*>(env, javaLoader);
179 ClassLoader* class_loader = down_cast<ClassLoader*>(class_loader_object);
180 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
181 class_linker->RegisterDexFile(*dex_file);
182 Class* result = class_linker->DefineClass(descriptor, class_loader, *dex_file, *dex_class_def);
Brian Carlstrom41ec4772011-10-11 01:35:58 -0700183 if (env->ExceptionCheck()) {
Elliott Hughes7bfc9632012-01-26 12:02:54 -0800184 // Swallow any ClassNotFoundException or NoClassDefFoundError; the contract with the caller
185 // is that we return null if the class is not found.
Ian Rogerscab01012012-01-10 17:35:46 -0800186 jthrowable exception = env->ExceptionOccurred();
Brian Carlstrom41ec4772011-10-11 01:35:58 -0700187 env->ExceptionClear();
Elliott Hughes7bfc9632012-01-26 12:02:54 -0800188
189 static jclass ClassNotFoundException_class = CacheClass(env, "java/lang/ClassNotFoundException");
190 static jclass NoClassDefFoundError_class = CacheClass(env, "java/lang/NoClassDefFoundError");
191
192 if (!env->IsInstanceOf(exception, ClassNotFoundException_class) && !env->IsInstanceOf(exception, NoClassDefFoundError_class)) {
Ian Rogerscab01012012-01-10 17:35:46 -0800193 env->Throw(exception);
194 }
Brian Carlstrom41ec4772011-10-11 01:35:58 -0700195 return NULL;
196 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700197 return AddLocalReference<jclass>(env, result);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700198}
199
200jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jint cookie) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700201 const DexFile* dex_file = toDexFile(env, cookie);
202 if (dex_file == NULL) {
203 return NULL;
204 }
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700205
206 std::vector<std::string> class_names;
207 for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
208 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
209 const char* descriptor = dex_file->GetClassDescriptor(class_def);
210 class_names.push_back(DescriptorToDot(descriptor));
211 }
212 return toStringArray(env, class_names);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700213}
214
215jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700216 ScopedUtfChars filename(env, javaFilename);
217 if (filename.c_str() == NULL) {
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700218 return JNI_TRUE;
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700219 }
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700220
221 if (!OS::FileExists(filename.c_str())) {
222 jniThrowExceptionFmt(env, "java/io/FileNotFoundException", "%s", filename.c_str());
223 return JNI_TRUE;
224 }
225
226 // Always treat elements of the bootclasspath as up-to-date. The
227 // fact that code is running at all means that this should be true.
228 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
229 const std::vector<const DexFile*>& boot_class_path = class_linker->GetBootClassPath();
230 for (size_t i = 0; i < boot_class_path.size(); i++) {
231 if (boot_class_path[i]->GetLocation() == filename.c_str()) {
232 return JNI_FALSE;
233 }
234 }
235
236 UniquePtr<const DexFile> dex_file(DexFile::Open(filename.c_str(), ""));
237 if (dex_file.get() == NULL) {
238 return JNI_TRUE;
239 }
240
Brian Carlstromae826982011-11-09 01:33:42 -0800241 const OatFile* oat_file = class_linker->FindOatFileForDexFile(*dex_file.get());
Brian Carlstromfad71432011-10-16 20:25:10 -0700242 if (oat_file == NULL) {
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700243 return JNI_TRUE;
244 }
Brian Carlstromfad71432011-10-16 20:25:10 -0700245 const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file->GetLocation());
246 if (oat_dex_file == NULL) {
247 return JNI_TRUE;
248 }
249 if (oat_dex_file->GetDexFileChecksum() != dex_file->GetHeader().checksum_) {
250 return JNI_TRUE;
251 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700252 return JNI_FALSE;
253}
254
255static JNINativeMethod gMethods[] = {
256 NATIVE_METHOD(DexFile, closeDexFile, "(I)V"),
257 NATIVE_METHOD(DexFile, defineClass, "(Ljava/lang/String;Ljava/lang/ClassLoader;I)Ljava/lang/Class;"),
258 NATIVE_METHOD(DexFile, getClassNameList, "(I)[Ljava/lang/String;"),
259 NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
260 NATIVE_METHOD(DexFile, openDexFile, "(Ljava/lang/String;Ljava/lang/String;I)I"),
261};
262
263} // namespace
264
265void register_dalvik_system_DexFile(JNIEnv* env) {
266 jniRegisterNativeMethods(env, "dalvik/system/DexFile", gMethods, NELEM(gMethods));
267}
268
269} // namespace art