blob: 6ad92ba07599882891187db2b03191ae3bbc15d4 [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"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070027#include "ScopedUtfChars.h"
28
29#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
30
31namespace art {
32
33namespace {
34
35// A smart pointer that provides read-only access to a Java string's UTF chars.
36// Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if
37// passed a null jstring. The correct idiom is:
38//
39// NullableScopedUtfChars name(env, javaName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -070040// if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070041// return NULL;
42// }
43// // ... use name.c_str()
44//
45// TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option.
46class NullableScopedUtfChars {
47public:
48 NullableScopedUtfChars(JNIEnv* env, jstring s)
49 : mEnv(env), mString(s)
50 {
51 mUtfChars = (s != NULL) ? env->GetStringUTFChars(s, NULL) : NULL;
52 }
53
54 ~NullableScopedUtfChars() {
55 if (mUtfChars) {
56 mEnv->ReleaseStringUTFChars(mString, mUtfChars);
57 }
58 }
59
60 const char* c_str() const {
61 return mUtfChars;
62 }
63
64 size_t size() const {
65 return strlen(mUtfChars);
66 }
67
68 // Element access.
69 const char& operator[](size_t n) const {
70 return mUtfChars[n];
71 }
72
73private:
74 JNIEnv* mEnv;
75 jstring mString;
76 const char* mUtfChars;
77
78 // Disallow copy and assignment.
79 NullableScopedUtfChars(const NullableScopedUtfChars&);
80 void operator=(const NullableScopedUtfChars&);
81};
82
jeffhaoc393a4f2011-10-19 13:46:09 -070083static jint DexFile_openDexFile(JNIEnv* env, jclass, jstring javaSourceName, jstring javaOutputName, jint) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070084 ScopedUtfChars sourceName(env, javaSourceName);
85 if (sourceName.c_str() == NULL) {
86 return 0;
87 }
88 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) {
94 dex_file = DexFile::Open(sourceName.c_str(), "");
95 } else {
jeffhao262bf462011-10-20 18:36:32 -070096 // Sanity check the arguments.
97 if (!IsValidZipFilename(sourceName.c_str()) || !IsValidDexFilename(outputName.c_str())) {
98 LOG(ERROR) << "Bad filenames extracting dex '" << outputName.c_str()
99 << "' from zip '" << sourceName.c_str() << "'";
100 return NULL;
101 }
102
103 // Extract classes.dex from the input zip file to the output dex file
104 UniquePtr<ZipArchive> zip_archive(ZipArchive::Open(sourceName.c_str()));
105 if (zip_archive.get() == NULL) {
106 LOG(ERROR) << "Failed to open " << sourceName.c_str() << " when looking for classes.dex";
107 return NULL;
108 }
109 UniquePtr<ZipEntry> zip_entry(zip_archive->Find(DexFile::kClassesDex));
110 if (zip_entry.get() == NULL) {
111 LOG(ERROR) << "Failed to find classes.dex within " << sourceName.c_str();
112 return NULL;
113 }
114 UniquePtr<File> file(OS::OpenFile(outputName.c_str(), true));
115 bool success = zip_entry->Extract(*file);
116 if (!success) {
117 LOG(ERROR) << "Failed to extract classes.dex from " << sourceName.c_str();
118 return NULL;
119 }
jeffhaoc393a4f2011-10-19 13:46:09 -0700120 dex_file = DexFile::Open(outputName.c_str(), "");
jeffhao262bf462011-10-20 18:36:32 -0700121
122 // Generate the oat file for the newly extracted dex file
123 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
124 if (class_linker->GenerateOatFile(outputName.c_str()) == NULL) {
125 return 0;
126 }
jeffhaoc393a4f2011-10-19 13:46:09 -0700127 }
jeffhao262bf462011-10-20 18:36:32 -0700128
Brian Carlstromaded5f72011-10-07 17:15:04 -0700129 if (dex_file == NULL) {
130 jniThrowExceptionFmt(env, "java/io/IOException", "unable to open DEX file: %s",
131 sourceName.c_str());
jeffhaoc393a4f2011-10-19 13:46:09 -0700132 return 0;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700133 }
134 return static_cast<jint>(reinterpret_cast<uintptr_t>(dex_file));
135}
136
137static const DexFile* toDexFile(JNIEnv* env, int dex_file_address) {
138 const DexFile* dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(dex_file_address));
139 if ((dex_file == NULL)) {
140 jniThrowNullPointerException(env, "dex_file == null");
141 }
142 return dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700143}
144
145void DexFile_closeDexFile(JNIEnv* env, jclass, jint cookie) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700146 const DexFile* dex_file = toDexFile(env, cookie);
147 if (dex_file == NULL) {
148 return;
149 }
150 if (Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) {
151 return;
152 }
153 delete dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700154}
155
156jclass DexFile_defineClass(JNIEnv* env, jclass, jstring javaName, jobject javaLoader, jint cookie) {
Brian Carlstromb82b6872011-10-26 17:18:07 -0700157 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700158 const DexFile* dex_file = toDexFile(env, cookie);
159 if (dex_file == NULL) {
160 return NULL;
161 }
Brian Carlstromdf143242011-10-10 18:05:34 -0700162 ScopedUtfChars class_name(env, javaName);
163 if (class_name.c_str() == NULL) {
164 return NULL;
165 }
166 const std::string descriptor = DotToDescriptor(class_name.c_str());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700167 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
168 if (dex_class_def == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700169 return NULL;
170 }
171
172 Object* class_loader_object = Decode<Object*>(env, javaLoader);
173 ClassLoader* class_loader = down_cast<ClassLoader*>(class_loader_object);
174 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
175 class_linker->RegisterDexFile(*dex_file);
176 Class* result = class_linker->DefineClass(descriptor, class_loader, *dex_file, *dex_class_def);
Brian Carlstrom41ec4772011-10-11 01:35:58 -0700177 if (env->ExceptionCheck()) {
178 env->ExceptionClear();
179 return NULL;
180 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700181 return AddLocalReference<jclass>(env, result);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700182}
183
184jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jint cookie) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700185 const DexFile* dex_file = toDexFile(env, cookie);
186 if (dex_file == NULL) {
187 return NULL;
188 }
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700189
190 std::vector<std::string> class_names;
191 for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
192 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
193 const char* descriptor = dex_file->GetClassDescriptor(class_def);
194 class_names.push_back(DescriptorToDot(descriptor));
195 }
196 return toStringArray(env, class_names);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700197}
198
199jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700200 ScopedUtfChars filename(env, javaFilename);
201 if (filename.c_str() == NULL) {
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700202 return JNI_TRUE;
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700203 }
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700204
205 if (!OS::FileExists(filename.c_str())) {
206 jniThrowExceptionFmt(env, "java/io/FileNotFoundException", "%s", filename.c_str());
207 return JNI_TRUE;
208 }
209
210 // Always treat elements of the bootclasspath as up-to-date. The
211 // fact that code is running at all means that this should be true.
212 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
213 const std::vector<const DexFile*>& boot_class_path = class_linker->GetBootClassPath();
214 for (size_t i = 0; i < boot_class_path.size(); i++) {
215 if (boot_class_path[i]->GetLocation() == filename.c_str()) {
216 return JNI_FALSE;
217 }
218 }
219
220 UniquePtr<const DexFile> dex_file(DexFile::Open(filename.c_str(), ""));
221 if (dex_file.get() == NULL) {
222 return JNI_TRUE;
223 }
224
Brian Carlstromfad71432011-10-16 20:25:10 -0700225 const OatFile* oat_file = class_linker->FindOatFile(*dex_file.get());
226 if (oat_file == NULL) {
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700227 return JNI_TRUE;
228 }
Brian Carlstromfad71432011-10-16 20:25:10 -0700229 const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file->GetLocation());
230 if (oat_dex_file == NULL) {
231 return JNI_TRUE;
232 }
233 if (oat_dex_file->GetDexFileChecksum() != dex_file->GetHeader().checksum_) {
234 return JNI_TRUE;
235 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700236 return JNI_FALSE;
237}
238
239static JNINativeMethod gMethods[] = {
240 NATIVE_METHOD(DexFile, closeDexFile, "(I)V"),
241 NATIVE_METHOD(DexFile, defineClass, "(Ljava/lang/String;Ljava/lang/ClassLoader;I)Ljava/lang/Class;"),
242 NATIVE_METHOD(DexFile, getClassNameList, "(I)[Ljava/lang/String;"),
243 NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
244 NATIVE_METHOD(DexFile, openDexFile, "(Ljava/lang/String;Ljava/lang/String;I)I"),
245};
246
247} // namespace
248
249void register_dalvik_system_DexFile(JNIEnv* env) {
250 jniRegisterNativeMethods(env, "dalvik/system/DexFile", gMethods, NELEM(gMethods));
251}
252
253} // namespace art