blob: ee36f99394835f4b6a9a56689d6fa0e3c5021770 [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 Hughese8f32122012-01-24 18:23:30 -0800184 // If we threw a ClassNotFoundException, stifle it, since the contract in the caller
185 // says we simply 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 Hughese8f32122012-01-24 18:23:30 -0800188 ScopedLocalRef<jclass> exception_class(env, env->FindClass("java/lang/ClassNotFoundException"));
189 if (!env->IsInstanceOf(exception, exception_class.get())) {
Ian Rogerscab01012012-01-10 17:35:46 -0800190 env->Throw(exception);
191 }
Brian Carlstrom41ec4772011-10-11 01:35:58 -0700192 return NULL;
193 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700194 return AddLocalReference<jclass>(env, result);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700195}
196
197jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jint cookie) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700198 const DexFile* dex_file = toDexFile(env, cookie);
199 if (dex_file == NULL) {
200 return NULL;
201 }
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700202
203 std::vector<std::string> class_names;
204 for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
205 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
206 const char* descriptor = dex_file->GetClassDescriptor(class_def);
207 class_names.push_back(DescriptorToDot(descriptor));
208 }
209 return toStringArray(env, class_names);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700210}
211
212jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700213 ScopedUtfChars filename(env, javaFilename);
214 if (filename.c_str() == NULL) {
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700215 return JNI_TRUE;
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700216 }
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700217
218 if (!OS::FileExists(filename.c_str())) {
219 jniThrowExceptionFmt(env, "java/io/FileNotFoundException", "%s", filename.c_str());
220 return JNI_TRUE;
221 }
222
223 // Always treat elements of the bootclasspath as up-to-date. The
224 // fact that code is running at all means that this should be true.
225 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
226 const std::vector<const DexFile*>& boot_class_path = class_linker->GetBootClassPath();
227 for (size_t i = 0; i < boot_class_path.size(); i++) {
228 if (boot_class_path[i]->GetLocation() == filename.c_str()) {
229 return JNI_FALSE;
230 }
231 }
232
233 UniquePtr<const DexFile> dex_file(DexFile::Open(filename.c_str(), ""));
234 if (dex_file.get() == NULL) {
235 return JNI_TRUE;
236 }
237
Brian Carlstromae826982011-11-09 01:33:42 -0800238 const OatFile* oat_file = class_linker->FindOatFileForDexFile(*dex_file.get());
Brian Carlstromfad71432011-10-16 20:25:10 -0700239 if (oat_file == NULL) {
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700240 return JNI_TRUE;
241 }
Brian Carlstromfad71432011-10-16 20:25:10 -0700242 const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file->GetLocation());
243 if (oat_dex_file == NULL) {
244 return JNI_TRUE;
245 }
246 if (oat_dex_file->GetDexFileChecksum() != dex_file->GetHeader().checksum_) {
247 return JNI_TRUE;
248 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700249 return JNI_FALSE;
250}
251
252static JNINativeMethod gMethods[] = {
253 NATIVE_METHOD(DexFile, closeDexFile, "(I)V"),
254 NATIVE_METHOD(DexFile, defineClass, "(Ljava/lang/String;Ljava/lang/ClassLoader;I)Ljava/lang/Class;"),
255 NATIVE_METHOD(DexFile, getClassNameList, "(I)[Ljava/lang/String;"),
256 NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
257 NATIVE_METHOD(DexFile, openDexFile, "(Ljava/lang/String;Ljava/lang/String;I)I"),
258};
259
260} // namespace
261
262void register_dalvik_system_DexFile(JNIEnv* env) {
263 jniRegisterNativeMethods(env, "dalvik/system/DexFile", gMethods, NELEM(gMethods));
264}
265
266} // namespace art