blob: 67cb49bc9d9f30a4c98480cab230b0843de95803 [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) {
jeffhaof6174e82012-01-31 16:14:17 -080093 dex_file = Runtime::Current()->GetClassLinker()->FindDexFileFromDexLocation(sourceName.c_str());
94 if (dex_file == NULL) {
95 dex_file = DexFile::Open(sourceName.c_str(), "");
96 }
jeffhaoc393a4f2011-10-19 13:46:09 -070097 } else {
jeffhao262bf462011-10-20 18:36:32 -070098 // Sanity check the arguments.
99 if (!IsValidZipFilename(sourceName.c_str()) || !IsValidDexFilename(outputName.c_str())) {
100 LOG(ERROR) << "Bad filenames extracting dex '" << outputName.c_str()
101 << "' from zip '" << sourceName.c_str() << "'";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -0800102 return 0;
jeffhao262bf462011-10-20 18:36:32 -0700103 }
Brian Carlstromd601af82012-01-06 10:15:19 -0800104 // Generate the output oat file for the source dex file
jeffhao262bf462011-10-20 18:36:32 -0700105 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromd601af82012-01-06 10:15:19 -0800106 UniquePtr<File> file(OS::OpenFile(outputName.c_str(), true));
107 if (file.get() == NULL) {
Ian Rogers725aee52012-01-11 11:56:56 -0800108 LOG(WARNING) << "unable to create oat file: " << outputName.c_str();
Brian Carlstromd601af82012-01-06 10:15:19 -0800109 jniThrowExceptionFmt(env, "java/io/IOException", "unable to create oat file: %s",
110 outputName.c_str());
jeffhao262bf462011-10-20 18:36:32 -0700111 return 0;
112 }
Brian Carlstromd601af82012-01-06 10:15:19 -0800113 if (!class_linker->GenerateOatFile(sourceName.c_str(), file->Fd(), outputName.c_str())) {
Ian Rogers725aee52012-01-11 11:56:56 -0800114 LOG(WARNING) << "unable to generate oat file: " << outputName.c_str();
Brian Carlstromd601af82012-01-06 10:15:19 -0800115 jniThrowExceptionFmt(env, "java/io/IOException", "unable to generate oat file: %s",
116 outputName.c_str());
117 return 0;
118 }
119 UniquePtr<OatFile> oat_file(OatFile::Open(outputName.c_str(), "", NULL));
120 if (oat_file.get() == NULL) {
Ian Rogers725aee52012-01-11 11:56:56 -0800121 LOG(WARNING) << "unable to open oat file: " << outputName.c_str();
Brian Carlstromd601af82012-01-06 10:15:19 -0800122 jniThrowExceptionFmt(env, "java/io/IOException", "unable to open oat file: %s",
123 outputName.c_str());
124 return 0;
125 }
126 const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(sourceName.c_str());
127 if (oat_dex_file == NULL) {
Ian Rogers725aee52012-01-11 11:56:56 -0800128 LOG(WARNING) << "unable to find dex file in oat file: " << outputName.c_str();
Brian Carlstromd601af82012-01-06 10:15:19 -0800129 jniThrowExceptionFmt(env, "java/io/IOException", "unable to find dex file in oat file: %s",
130 outputName.c_str());
131 return 0;
132 }
Ian Rogers725aee52012-01-11 11:56:56 -0800133 Runtime::Current()->GetClassLinker()->RegisterOatFile(*oat_file.release());
Brian Carlstromd601af82012-01-06 10:15:19 -0800134 dex_file = oat_dex_file->OpenDexFile();
jeffhaoc393a4f2011-10-19 13:46:09 -0700135 }
jeffhao262bf462011-10-20 18:36:32 -0700136
Brian Carlstromaded5f72011-10-07 17:15:04 -0700137 if (dex_file == NULL) {
Ian Rogers725aee52012-01-11 11:56:56 -0800138 LOG(WARNING) << "unable to open dex file: " << sourceName.c_str();
Brian Carlstromd601af82012-01-06 10:15:19 -0800139 jniThrowExceptionFmt(env, "java/io/IOException", "unable to open dex file: %s",
Brian Carlstromaded5f72011-10-07 17:15:04 -0700140 sourceName.c_str());
jeffhaoc393a4f2011-10-19 13:46:09 -0700141 return 0;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700142 }
143 return static_cast<jint>(reinterpret_cast<uintptr_t>(dex_file));
144}
145
146static const DexFile* toDexFile(JNIEnv* env, int dex_file_address) {
147 const DexFile* dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(dex_file_address));
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800148 if (dex_file == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700149 jniThrowNullPointerException(env, "dex_file == null");
150 }
151 return dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700152}
153
154void DexFile_closeDexFile(JNIEnv* env, jclass, jint cookie) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700155 const DexFile* dex_file = toDexFile(env, cookie);
156 if (dex_file == NULL) {
157 return;
158 }
159 if (Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) {
160 return;
161 }
162 delete dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700163}
164
165jclass DexFile_defineClass(JNIEnv* env, jclass, jstring javaName, jobject javaLoader, jint cookie) {
Brian Carlstromb82b6872011-10-26 17:18:07 -0700166 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700167 const DexFile* dex_file = toDexFile(env, cookie);
168 if (dex_file == NULL) {
169 return NULL;
170 }
Brian Carlstromdf143242011-10-10 18:05:34 -0700171 ScopedUtfChars class_name(env, javaName);
172 if (class_name.c_str() == NULL) {
173 return NULL;
174 }
Elliott Hughes95572412011-12-13 18:14:20 -0800175 const std::string descriptor(DotToDescriptor(class_name.c_str()));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700176 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
177 if (dex_class_def == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700178 return NULL;
179 }
180
181 Object* class_loader_object = Decode<Object*>(env, javaLoader);
182 ClassLoader* class_loader = down_cast<ClassLoader*>(class_loader_object);
183 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
184 class_linker->RegisterDexFile(*dex_file);
185 Class* result = class_linker->DefineClass(descriptor, class_loader, *dex_file, *dex_class_def);
Brian Carlstrom41ec4772011-10-11 01:35:58 -0700186 if (env->ExceptionCheck()) {
Elliott Hughes7bfc9632012-01-26 12:02:54 -0800187 // Swallow any ClassNotFoundException or NoClassDefFoundError; the contract with the caller
188 // is that we return null if the class is not found.
Ian Rogerscab01012012-01-10 17:35:46 -0800189 jthrowable exception = env->ExceptionOccurred();
Brian Carlstrom41ec4772011-10-11 01:35:58 -0700190 env->ExceptionClear();
Elliott Hughes7bfc9632012-01-26 12:02:54 -0800191
192 static jclass ClassNotFoundException_class = CacheClass(env, "java/lang/ClassNotFoundException");
193 static jclass NoClassDefFoundError_class = CacheClass(env, "java/lang/NoClassDefFoundError");
194
195 if (!env->IsInstanceOf(exception, ClassNotFoundException_class) && !env->IsInstanceOf(exception, NoClassDefFoundError_class)) {
Ian Rogerscab01012012-01-10 17:35:46 -0800196 env->Throw(exception);
197 }
Brian Carlstrom41ec4772011-10-11 01:35:58 -0700198 return NULL;
199 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700200 return AddLocalReference<jclass>(env, result);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700201}
202
203jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jint cookie) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700204 const DexFile* dex_file = toDexFile(env, cookie);
205 if (dex_file == NULL) {
206 return NULL;
207 }
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700208
209 std::vector<std::string> class_names;
210 for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
211 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
212 const char* descriptor = dex_file->GetClassDescriptor(class_def);
213 class_names.push_back(DescriptorToDot(descriptor));
214 }
215 return toStringArray(env, class_names);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700216}
217
218jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700219 ScopedUtfChars filename(env, javaFilename);
220 if (filename.c_str() == NULL) {
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700221 return JNI_TRUE;
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700222 }
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700223
224 if (!OS::FileExists(filename.c_str())) {
225 jniThrowExceptionFmt(env, "java/io/FileNotFoundException", "%s", filename.c_str());
226 return JNI_TRUE;
227 }
228
229 // Always treat elements of the bootclasspath as up-to-date. The
230 // fact that code is running at all means that this should be true.
231 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
232 const std::vector<const DexFile*>& boot_class_path = class_linker->GetBootClassPath();
233 for (size_t i = 0; i < boot_class_path.size(); i++) {
234 if (boot_class_path[i]->GetLocation() == filename.c_str()) {
235 return JNI_FALSE;
236 }
237 }
238
jeffhaof6174e82012-01-31 16:14:17 -0800239 const DexFile* dex_file = class_linker->FindDexFileFromDexLocation(filename.c_str());
240 if (dex_file == NULL) {
Brian Carlstromfad71432011-10-16 20:25:10 -0700241 return JNI_TRUE;
242 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700243 return JNI_FALSE;
244}
245
246static JNINativeMethod gMethods[] = {
247 NATIVE_METHOD(DexFile, closeDexFile, "(I)V"),
248 NATIVE_METHOD(DexFile, defineClass, "(Ljava/lang/String;Ljava/lang/ClassLoader;I)Ljava/lang/Class;"),
249 NATIVE_METHOD(DexFile, getClassNameList, "(I)[Ljava/lang/String;"),
250 NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
251 NATIVE_METHOD(DexFile, openDexFile, "(Ljava/lang/String;Ljava/lang/String;I)I"),
252};
253
254} // namespace
255
256void register_dalvik_system_DexFile(JNIEnv* env) {
257 jniRegisterNativeMethods(env, "dalvik/system/DexFile", gMethods, NELEM(gMethods));
258}
259
260} // namespace art