blob: 2b86b767015838e2841166c741ab21d4f14af15b [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
Brian Carlstromf91c8c32011-09-21 17:30:34 -070034// A smart pointer that provides read-only access to a Java string's UTF chars.
35// Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if
36// passed a null jstring. The correct idiom is:
37//
38// NullableScopedUtfChars name(env, javaName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -070039// if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070040// return NULL;
41// }
42// // ... use name.c_str()
43//
44// TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option.
45class NullableScopedUtfChars {
Elliott Hughesba8eee12012-01-24 20:25:24 -080046 public:
47 NullableScopedUtfChars(JNIEnv* env, jstring s) : mEnv(env), mString(s) {
48 mUtfChars = (s != NULL) ? env->GetStringUTFChars(s, NULL) : NULL;
49 }
50
51 ~NullableScopedUtfChars() {
52 if (mUtfChars) {
53 mEnv->ReleaseStringUTFChars(mString, mUtfChars);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070054 }
Elliott Hughesba8eee12012-01-24 20:25:24 -080055 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070056
Elliott Hughesba8eee12012-01-24 20:25:24 -080057 const char* c_str() const {
58 return mUtfChars;
59 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070060
Elliott Hughesba8eee12012-01-24 20:25:24 -080061 size_t size() const {
62 return strlen(mUtfChars);
63 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070064
Elliott Hughesba8eee12012-01-24 20:25:24 -080065 // Element access.
66 const char& operator[](size_t n) const {
67 return mUtfChars[n];
68 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070069
Elliott Hughesba8eee12012-01-24 20:25:24 -080070 private:
71 JNIEnv* mEnv;
72 jstring mString;
73 const char* mUtfChars;
Brian Carlstromf91c8c32011-09-21 17:30:34 -070074
Elliott Hughesba8eee12012-01-24 20:25:24 -080075 // Disallow copy and assignment.
76 NullableScopedUtfChars(const NullableScopedUtfChars&);
77 void operator=(const NullableScopedUtfChars&);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070078};
79
jeffhaoc393a4f2011-10-19 13:46:09 -070080static jint DexFile_openDexFile(JNIEnv* env, jclass, jstring javaSourceName, jstring javaOutputName, jint) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070081 ScopedUtfChars sourceName(env, javaSourceName);
82 if (sourceName.c_str() == NULL) {
83 return 0;
84 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -080085 std::string source(sourceName.c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -070086 NullableScopedUtfChars outputName(env, javaOutputName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -070087 if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070088 return 0;
89 }
jeffhaoc393a4f2011-10-19 13:46:09 -070090 const DexFile* dex_file;
91 if (outputName.c_str() == NULL) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -080092 dex_file = Runtime::Current()->GetClassLinker()->FindDexFileInOatFileFromDexLocation(source);
jeffhaoc393a4f2011-10-19 13:46:09 -070093 } else {
Brian Carlstrom5b332c82012-02-01 15:02:31 -080094 std::string output(outputName.c_str());
95 dex_file = Runtime::Current()->GetClassLinker()->FindOrCreateOatFileForDexLocation(source, output);
jeffhaoc393a4f2011-10-19 13:46:09 -070096 }
Brian Carlstromaded5f72011-10-07 17:15:04 -070097 if (dex_file == NULL) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -080098 LOG(WARNING) << "Failed to open dex file: " << source;
Brian Carlstromd601af82012-01-06 10:15:19 -080099 jniThrowExceptionFmt(env, "java/io/IOException", "unable to open dex file: %s",
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800100 source.c_str());
jeffhaoc393a4f2011-10-19 13:46:09 -0700101 return 0;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700102 }
103 return static_cast<jint>(reinterpret_cast<uintptr_t>(dex_file));
104}
105
106static const DexFile* toDexFile(JNIEnv* env, int dex_file_address) {
107 const DexFile* dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(dex_file_address));
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800108 if (dex_file == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700109 jniThrowNullPointerException(env, "dex_file == null");
110 }
111 return dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700112}
113
Elliott Hughes0512f022012-03-15 22:10:52 -0700114static void DexFile_closeDexFile(JNIEnv* env, jclass, jint cookie) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700115 const DexFile* dex_file = toDexFile(env, cookie);
116 if (dex_file == NULL) {
117 return;
118 }
119 if (Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) {
120 return;
121 }
122 delete dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700123}
124
Elliott Hughes0512f022012-03-15 22:10:52 -0700125static jclass DexFile_defineClassNative(JNIEnv* env, jclass, jstring javaName, jobject javaLoader,
126 jint cookie) {
Brian Carlstromb82b6872011-10-26 17:18:07 -0700127 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700128 const DexFile* dex_file = toDexFile(env, cookie);
129 if (dex_file == NULL) {
130 return NULL;
131 }
Brian Carlstromdf143242011-10-10 18:05:34 -0700132 ScopedUtfChars class_name(env, javaName);
133 if (class_name.c_str() == NULL) {
134 return NULL;
135 }
Elliott Hughes95572412011-12-13 18:14:20 -0800136 const std::string descriptor(DotToDescriptor(class_name.c_str()));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700137 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
138 if (dex_class_def == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700139 return NULL;
140 }
141
142 Object* class_loader_object = Decode<Object*>(env, javaLoader);
143 ClassLoader* class_loader = down_cast<ClassLoader*>(class_loader_object);
144 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
145 class_linker->RegisterDexFile(*dex_file);
146 Class* result = class_linker->DefineClass(descriptor, class_loader, *dex_file, *dex_class_def);
147 return AddLocalReference<jclass>(env, result);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700148}
149
Elliott Hughes0512f022012-03-15 22:10:52 -0700150static jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jint cookie) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700151 const DexFile* dex_file = toDexFile(env, cookie);
152 if (dex_file == NULL) {
153 return NULL;
154 }
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700155
156 std::vector<std::string> class_names;
157 for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
158 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
159 const char* descriptor = dex_file->GetClassDescriptor(class_def);
160 class_names.push_back(DescriptorToDot(descriptor));
161 }
162 return toStringArray(env, class_names);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700163}
164
Elliott Hughes0512f022012-03-15 22:10:52 -0700165static jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800166 bool debug_logging = false;
167
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700168 ScopedUtfChars filename(env, javaFilename);
169 if (filename.c_str() == NULL) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800170 LOG(ERROR) << "DexFile_isDexOptNeeded null filename";
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700171 return JNI_TRUE;
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700172 }
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700173
174 if (!OS::FileExists(filename.c_str())) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800175 LOG(ERROR) << "DexFile_isDexOptNeeded file '" << filename.c_str() << "' does not exist";
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700176 jniThrowExceptionFmt(env, "java/io/FileNotFoundException", "%s", filename.c_str());
177 return JNI_TRUE;
178 }
179
180 // Always treat elements of the bootclasspath as up-to-date. The
181 // fact that code is running at all means that this should be true.
182 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
183 const std::vector<const DexFile*>& boot_class_path = class_linker->GetBootClassPath();
184 for (size_t i = 0; i < boot_class_path.size(); i++) {
185 if (boot_class_path[i]->GetLocation() == filename.c_str()) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800186 if (debug_logging) {
187 LOG(INFO) << "DexFile_isDexOptNeeded ignoring boot class path file: " << filename.c_str();
188 }
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700189 return JNI_FALSE;
190 }
191 }
192
Brian Carlstroma004aa92012-02-08 18:05:09 -0800193 // If we have an oat file next to the dex file, assume up-to-date.
194 // A user build looks like this, and it will have no classes.dex in
195 // the input for checksum validation.
196 std::string oat_filename(OatFile::DexFilenameToOatFilename(filename.c_str()));
197 const OatFile* oat_file = class_linker->FindOatFileFromOatLocation(oat_filename);
198 if (oat_file != NULL && oat_file->GetOatDexFile(filename.c_str()) != NULL) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800199 if (debug_logging) {
200 LOG(INFO) << "DexFile_isDexOptNeeded ignoring precompiled file: " << filename.c_str();
201 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800202 return JNI_FALSE;
Brian Carlstromfad71432011-10-16 20:25:10 -0700203 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800204
Brian Carlstroma004aa92012-02-08 18:05:09 -0800205 // Check if we have an oat file in the cache
206 std::string cache_location(GetArtCacheFilenameOrDie(oat_filename));
207 oat_file = class_linker->FindOatFileFromOatLocation(cache_location);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800208 if (oat_file == NULL) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800209 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
210 << " does not exist for " << filename.c_str();
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800211 return JNI_TRUE;
212 }
213
214 const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(filename.c_str());
215 if (oat_dex_file == NULL) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800216 LOG(ERROR) << "DexFile_isDexOptNeeded cache file " << cache_location
217 << " does not contain contents for " << filename.c_str();
218 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file->GetOatDexFiles();
219 for (size_t i = 0; i < oat_dex_files.size(); i++) {
220 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
221 LOG(ERROR) << "DexFile_isDexOptNeeded cache file " << cache_location
222 << " contains contents for " << oat_dex_file->GetDexFileLocation();
223 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800224 return JNI_TRUE;
225 }
226
Brian Carlstroma004aa92012-02-08 18:05:09 -0800227 uint32_t location_checksum;
228 if (!DexFile::GetChecksum(filename.c_str(), location_checksum)) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800229 LOG(ERROR) << "DexFile_isDexOptNeeded failed to compute checksum of " << filename.c_str();
Brian Carlstroma004aa92012-02-08 18:05:09 -0800230 return JNI_TRUE;
231 }
232
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800233 if (location_checksum != oat_dex_file->GetDexFileLocationChecksum()) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800234 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
235 << " has out-of-date checksum compared to " << filename.c_str();
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800236 return JNI_TRUE;
237 }
238
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800239 if (debug_logging) {
240 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
241 << " is up-to-date for " << filename.c_str();
242 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700243 return JNI_FALSE;
244}
245
246static JNINativeMethod gMethods[] = {
247 NATIVE_METHOD(DexFile, closeDexFile, "(I)V"),
Ian Rogers66a556f2012-02-14 00:05:38 -0800248 NATIVE_METHOD(DexFile, defineClassNative, "(Ljava/lang/String;Ljava/lang/ClassLoader;I)Ljava/lang/Class;"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700249 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
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700254void register_dalvik_system_DexFile(JNIEnv* env) {
255 jniRegisterNativeMethods(env, "dalvik/system/DexFile", gMethods, NELEM(gMethods));
256}
257
258} // namespace art