blob: c009abaf14204037e886479d0edf1e2b6cb29dee [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 {
48public:
49 NullableScopedUtfChars(JNIEnv* env, jstring s)
50 : mEnv(env), mString(s)
51 {
52 mUtfChars = (s != NULL) ? env->GetStringUTFChars(s, NULL) : NULL;
53 }
54
55 ~NullableScopedUtfChars() {
56 if (mUtfChars) {
57 mEnv->ReleaseStringUTFChars(mString, mUtfChars);
58 }
59 }
60
61 const char* c_str() const {
62 return mUtfChars;
63 }
64
65 size_t size() const {
66 return strlen(mUtfChars);
67 }
68
69 // Element access.
70 const char& operator[](size_t n) const {
71 return mUtfChars[n];
72 }
73
74private:
75 JNIEnv* mEnv;
76 jstring mString;
77 const char* mUtfChars;
78
79 // Disallow copy and assignment.
80 NullableScopedUtfChars(const NullableScopedUtfChars&);
81 void operator=(const NullableScopedUtfChars&);
82};
83
jeffhaoc393a4f2011-10-19 13:46:09 -070084static jint DexFile_openDexFile(JNIEnv* env, jclass, jstring javaSourceName, jstring javaOutputName, jint) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070085 ScopedUtfChars sourceName(env, javaSourceName);
86 if (sourceName.c_str() == NULL) {
87 return 0;
88 }
89 NullableScopedUtfChars outputName(env, javaOutputName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -070090 if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070091 return 0;
92 }
jeffhaoc393a4f2011-10-19 13:46:09 -070093 const DexFile* dex_file;
94 if (outputName.c_str() == NULL) {
95 dex_file = DexFile::Open(sourceName.c_str(), "");
96 } else {
jeffhao262bf462011-10-20 18:36:32 -070097 // Sanity check the arguments.
98 if (!IsValidZipFilename(sourceName.c_str()) || !IsValidDexFilename(outputName.c_str())) {
99 LOG(ERROR) << "Bad filenames extracting dex '" << outputName.c_str()
100 << "' from zip '" << sourceName.c_str() << "'";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -0800101 return 0;
jeffhao262bf462011-10-20 18:36:32 -0700102 }
Brian Carlstromd601af82012-01-06 10:15:19 -0800103 // Generate the output oat file for the source dex file
jeffhao262bf462011-10-20 18:36:32 -0700104 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromd601af82012-01-06 10:15:19 -0800105 UniquePtr<File> file(OS::OpenFile(outputName.c_str(), true));
106 if (file.get() == NULL) {
Ian Rogers725aee52012-01-11 11:56:56 -0800107 LOG(WARNING) << "unable to create oat file: " << outputName.c_str();
Brian Carlstromd601af82012-01-06 10:15:19 -0800108 jniThrowExceptionFmt(env, "java/io/IOException", "unable to create oat file: %s",
109 outputName.c_str());
jeffhao262bf462011-10-20 18:36:32 -0700110 return 0;
111 }
Brian Carlstromd601af82012-01-06 10:15:19 -0800112 if (!class_linker->GenerateOatFile(sourceName.c_str(), file->Fd(), outputName.c_str())) {
Ian Rogers725aee52012-01-11 11:56:56 -0800113 LOG(WARNING) << "unable to generate oat file: " << outputName.c_str();
Brian Carlstromd601af82012-01-06 10:15:19 -0800114 jniThrowExceptionFmt(env, "java/io/IOException", "unable to generate oat file: %s",
115 outputName.c_str());
116 return 0;
117 }
118 UniquePtr<OatFile> oat_file(OatFile::Open(outputName.c_str(), "", NULL));
119 if (oat_file.get() == NULL) {
Ian Rogers725aee52012-01-11 11:56:56 -0800120 LOG(WARNING) << "unable to open oat file: " << outputName.c_str();
Brian Carlstromd601af82012-01-06 10:15:19 -0800121 jniThrowExceptionFmt(env, "java/io/IOException", "unable to open oat file: %s",
122 outputName.c_str());
123 return 0;
124 }
125 const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(sourceName.c_str());
126 if (oat_dex_file == NULL) {
Ian Rogers725aee52012-01-11 11:56:56 -0800127 LOG(WARNING) << "unable to find dex file in oat file: " << outputName.c_str();
Brian Carlstromd601af82012-01-06 10:15:19 -0800128 jniThrowExceptionFmt(env, "java/io/IOException", "unable to find dex file in oat file: %s",
129 outputName.c_str());
130 return 0;
131 }
Ian Rogers725aee52012-01-11 11:56:56 -0800132 Runtime::Current()->GetClassLinker()->RegisterOatFile(*oat_file.release());
Brian Carlstromd601af82012-01-06 10:15:19 -0800133 dex_file = oat_dex_file->OpenDexFile();
jeffhaoc393a4f2011-10-19 13:46:09 -0700134 }
jeffhao262bf462011-10-20 18:36:32 -0700135
Brian Carlstromaded5f72011-10-07 17:15:04 -0700136 if (dex_file == NULL) {
Ian Rogers725aee52012-01-11 11:56:56 -0800137 LOG(WARNING) << "unable to open dex file: " << sourceName.c_str();
Brian Carlstromd601af82012-01-06 10:15:19 -0800138 jniThrowExceptionFmt(env, "java/io/IOException", "unable to open dex file: %s",
Brian Carlstromaded5f72011-10-07 17:15:04 -0700139 sourceName.c_str());
jeffhaoc393a4f2011-10-19 13:46:09 -0700140 return 0;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700141 }
142 return static_cast<jint>(reinterpret_cast<uintptr_t>(dex_file));
143}
144
145static const DexFile* toDexFile(JNIEnv* env, int dex_file_address) {
146 const DexFile* dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(dex_file_address));
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800147 if (dex_file == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700148 jniThrowNullPointerException(env, "dex_file == null");
149 }
150 return dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700151}
152
153void DexFile_closeDexFile(JNIEnv* env, jclass, jint cookie) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700154 const DexFile* dex_file = toDexFile(env, cookie);
155 if (dex_file == NULL) {
156 return;
157 }
158 if (Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) {
159 return;
160 }
161 delete dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700162}
163
164jclass DexFile_defineClass(JNIEnv* env, jclass, jstring javaName, jobject javaLoader, jint cookie) {
Brian Carlstromb82b6872011-10-26 17:18:07 -0700165 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
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 Carlstromdf143242011-10-10 18:05:34 -0700170 ScopedUtfChars class_name(env, javaName);
171 if (class_name.c_str() == NULL) {
172 return NULL;
173 }
Elliott Hughes95572412011-12-13 18:14:20 -0800174 const std::string descriptor(DotToDescriptor(class_name.c_str()));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700175 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
176 if (dex_class_def == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700177 return NULL;
178 }
179
180 Object* class_loader_object = Decode<Object*>(env, javaLoader);
181 ClassLoader* class_loader = down_cast<ClassLoader*>(class_loader_object);
182 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
183 class_linker->RegisterDexFile(*dex_file);
184 Class* result = class_linker->DefineClass(descriptor, class_loader, *dex_file, *dex_class_def);
Brian Carlstrom41ec4772011-10-11 01:35:58 -0700185 if (env->ExceptionCheck()) {
Elliott Hughese8f32122012-01-24 18:23:30 -0800186 // If we threw a ClassNotFoundException, stifle it, since the contract in the caller
187 // says we simply return null if the class is not found.
Ian Rogerscab01012012-01-10 17:35:46 -0800188 jthrowable exception = env->ExceptionOccurred();
Brian Carlstrom41ec4772011-10-11 01:35:58 -0700189 env->ExceptionClear();
Elliott Hughese8f32122012-01-24 18:23:30 -0800190 ScopedLocalRef<jclass> exception_class(env, env->FindClass("java/lang/ClassNotFoundException"));
191 if (!env->IsInstanceOf(exception, exception_class.get())) {
Ian Rogerscab01012012-01-10 17:35:46 -0800192 env->Throw(exception);
193 }
Brian Carlstrom41ec4772011-10-11 01:35:58 -0700194 return NULL;
195 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700196 return AddLocalReference<jclass>(env, result);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700197}
198
199jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jint cookie) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700200 const DexFile* dex_file = toDexFile(env, cookie);
201 if (dex_file == NULL) {
202 return NULL;
203 }
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700204
205 std::vector<std::string> class_names;
206 for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
207 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
208 const char* descriptor = dex_file->GetClassDescriptor(class_def);
209 class_names.push_back(DescriptorToDot(descriptor));
210 }
211 return toStringArray(env, class_names);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700212}
213
214jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700215 ScopedUtfChars filename(env, javaFilename);
216 if (filename.c_str() == NULL) {
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700217 return JNI_TRUE;
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700218 }
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700219
220 if (!OS::FileExists(filename.c_str())) {
221 jniThrowExceptionFmt(env, "java/io/FileNotFoundException", "%s", filename.c_str());
222 return JNI_TRUE;
223 }
224
225 // Always treat elements of the bootclasspath as up-to-date. The
226 // fact that code is running at all means that this should be true.
227 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
228 const std::vector<const DexFile*>& boot_class_path = class_linker->GetBootClassPath();
229 for (size_t i = 0; i < boot_class_path.size(); i++) {
230 if (boot_class_path[i]->GetLocation() == filename.c_str()) {
231 return JNI_FALSE;
232 }
233 }
234
235 UniquePtr<const DexFile> dex_file(DexFile::Open(filename.c_str(), ""));
236 if (dex_file.get() == NULL) {
237 return JNI_TRUE;
238 }
239
Brian Carlstromae826982011-11-09 01:33:42 -0800240 const OatFile* oat_file = class_linker->FindOatFileForDexFile(*dex_file.get());
Brian Carlstromfad71432011-10-16 20:25:10 -0700241 if (oat_file == NULL) {
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700242 return JNI_TRUE;
243 }
Brian Carlstromfad71432011-10-16 20:25:10 -0700244 const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file->GetLocation());
245 if (oat_dex_file == NULL) {
246 return JNI_TRUE;
247 }
248 if (oat_dex_file->GetDexFileChecksum() != dex_file->GetHeader().checksum_) {
249 return JNI_TRUE;
250 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700251 return JNI_FALSE;
252}
253
254static JNINativeMethod gMethods[] = {
255 NATIVE_METHOD(DexFile, closeDexFile, "(I)V"),
256 NATIVE_METHOD(DexFile, defineClass, "(Ljava/lang/String;Ljava/lang/ClassLoader;I)Ljava/lang/Class;"),
257 NATIVE_METHOD(DexFile, getClassNameList, "(I)[Ljava/lang/String;"),
258 NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
259 NATIVE_METHOD(DexFile, openDexFile, "(Ljava/lang/String;Ljava/lang/String;I)I"),
260};
261
262} // namespace
263
264void register_dalvik_system_DexFile(JNIEnv* env) {
265 jniRegisterNativeMethods(env, "dalvik/system/DexFile", gMethods, NELEM(gMethods));
266}
267
268} // namespace art