blob: ef38f00561d6757d3080113e1df0bf21ad5b2228 [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_linker.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070020#include "class_loader.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070021#include "dex_file.h"
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070022#include "image.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070023#include "jni_internal.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070024#include "logging.h"
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -070025#include "os.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070026#include "runtime.h"
Ian Rogers0399dde2012-06-06 17:09:28 -070027#include "scoped_jni_thread_state.h"
Ian Rogersc9818482012-01-11 08:52:51 -080028#include "ScopedLocalRef.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070029#include "ScopedUtfChars.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070030#include "space.h"
31#include "toStringArray.h"
32#include "zip_archive.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070033
34namespace art {
35
Brian Carlstromf91c8c32011-09-21 17:30:34 -070036// 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 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -080087 std::string source(sourceName.c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -070088 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) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -080094 dex_file = Runtime::Current()->GetClassLinker()->FindDexFileInOatFileFromDexLocation(source);
jeffhaoc393a4f2011-10-19 13:46:09 -070095 } else {
Brian Carlstrom5b332c82012-02-01 15:02:31 -080096 std::string output(outputName.c_str());
97 dex_file = Runtime::Current()->GetClassLinker()->FindOrCreateOatFileForDexLocation(source, output);
jeffhaoc393a4f2011-10-19 13:46:09 -070098 }
Brian Carlstromaded5f72011-10-07 17:15:04 -070099 if (dex_file == NULL) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800100 LOG(WARNING) << "Failed to open dex file: " << source;
Elliott Hugheseac76672012-05-24 21:56:51 -0700101 Thread::Current()->ThrowNewExceptionF("Ljava/io/IOException;", "Unable to open dex file: %s",
102 source.c_str());
jeffhaoc393a4f2011-10-19 13:46:09 -0700103 return 0;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700104 }
105 return static_cast<jint>(reinterpret_cast<uintptr_t>(dex_file));
106}
107
Elliott Hugheseac76672012-05-24 21:56:51 -0700108static const DexFile* toDexFile(int dex_file_address) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700109 const DexFile* dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(dex_file_address));
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800110 if (dex_file == NULL) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700111 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NullPointerException;", "dex_file == null");
Brian Carlstromaded5f72011-10-07 17:15:04 -0700112 }
113 return dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700114}
115
Elliott Hugheseac76672012-05-24 21:56:51 -0700116static void DexFile_closeDexFile(JNIEnv*, jclass, jint cookie) {
117 const DexFile* dex_file = toDexFile(cookie);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700118 if (dex_file == NULL) {
119 return;
120 }
121 if (Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) {
122 return;
123 }
124 delete dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700125}
126
Elliott Hughes0512f022012-03-15 22:10:52 -0700127static jclass DexFile_defineClassNative(JNIEnv* env, jclass, jstring javaName, jobject javaLoader,
128 jint cookie) {
Ian Rogers365c1022012-06-22 15:05:28 -0700129 ScopedJniThreadState ts(env);
Elliott Hugheseac76672012-05-24 21:56:51 -0700130 const DexFile* dex_file = toDexFile(cookie);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700131 if (dex_file == NULL) {
132 return NULL;
133 }
Brian Carlstromdf143242011-10-10 18:05:34 -0700134 ScopedUtfChars class_name(env, javaName);
135 if (class_name.c_str() == NULL) {
136 return NULL;
137 }
Elliott Hughes95572412011-12-13 18:14:20 -0800138 const std::string descriptor(DotToDescriptor(class_name.c_str()));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700139 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
140 if (dex_class_def == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700141 return NULL;
142 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700143 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
144 class_linker->RegisterDexFile(*dex_file);
Ian Rogers365c1022012-06-22 15:05:28 -0700145 Object* class_loader_object = ts.Decode<Object*>(javaLoader);
Ian Rogers0399dde2012-06-06 17:09:28 -0700146 ClassLoader* class_loader = down_cast<ClassLoader*>(class_loader_object);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700147 Class* result = class_linker->DefineClass(descriptor, class_loader, *dex_file, *dex_class_def);
Ian Rogers365c1022012-06-22 15:05:28 -0700148 return ts.AddLocalReference<jclass>(result);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700149}
150
Elliott Hughes0512f022012-03-15 22:10:52 -0700151static jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jint cookie) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700152 const DexFile* dex_file = toDexFile(cookie);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700153 if (dex_file == NULL) {
154 return NULL;
155 }
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700156
157 std::vector<std::string> class_names;
158 for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
159 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
160 const char* descriptor = dex_file->GetClassDescriptor(class_def);
161 class_names.push_back(DescriptorToDot(descriptor));
162 }
163 return toStringArray(env, class_names);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700164}
165
Elliott Hughes0512f022012-03-15 22:10:52 -0700166static jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800167 bool debug_logging = false;
168
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700169 ScopedUtfChars filename(env, javaFilename);
170 if (filename.c_str() == NULL) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800171 LOG(ERROR) << "DexFile_isDexOptNeeded null filename";
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700172 return JNI_TRUE;
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700173 }
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700174
175 if (!OS::FileExists(filename.c_str())) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800176 LOG(ERROR) << "DexFile_isDexOptNeeded file '" << filename.c_str() << "' does not exist";
Elliott Hugheseac76672012-05-24 21:56:51 -0700177 Thread::Current()->ThrowNewExceptionF("Ljava/io/FileNotFoundException;", "%s", filename.c_str());
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700178 return JNI_TRUE;
179 }
180
181 // Always treat elements of the bootclasspath as up-to-date. The
182 // fact that code is running at all means that this should be true.
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700183 Runtime* runtime = Runtime::Current();
184 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700185 const std::vector<const DexFile*>& boot_class_path = class_linker->GetBootClassPath();
186 for (size_t i = 0; i < boot_class_path.size(); i++) {
187 if (boot_class_path[i]->GetLocation() == filename.c_str()) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800188 if (debug_logging) {
189 LOG(INFO) << "DexFile_isDexOptNeeded ignoring boot class path file: " << filename.c_str();
190 }
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700191 return JNI_FALSE;
192 }
193 }
194
Brian Carlstromafe25512012-06-27 17:02:58 -0700195 // Check if we have an oat file next to the dex file.
Brian Carlstroma004aa92012-02-08 18:05:09 -0800196 std::string oat_filename(OatFile::DexFilenameToOatFilename(filename.c_str()));
Logan Chien0c717dd2012-03-28 18:31:07 +0800197 UniquePtr<const OatFile> oat_file(
198 OatFile::Open(oat_filename, oat_filename, NULL, OatFile::kRelocNone));
Brian Carlstrom58cbbc22012-03-29 17:52:00 -0700199 if (oat_file.get() != NULL && oat_file->GetOatDexFile(filename.c_str()) != NULL) {
Brian Carlstromafe25512012-06-27 17:02:58 -0700200 uint32_t location_checksum;
201 // If we have no classes.dex checksum such as in a user build, assume up-to-date.
202 if (!DexFile::GetChecksum(filename.c_str(), location_checksum)) {
203 if (debug_logging) {
204 LOG(INFO) << "DexFile_isDexOptNeeded ignoring precompiled stripped file: " << filename.c_str();
205 }
206 return JNI_FALSE;
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800207 }
Brian Carlstromafe25512012-06-27 17:02:58 -0700208 if (ClassLinker::VerifyOatFileChecksums(oat_file.get(), filename.c_str(), location_checksum)) {
209 if (debug_logging) {
210 LOG(INFO) << "DexFile_isDexOptNeeded precompiled file " << oat_filename
211 << " is up-to-date checksum compared to " << filename.c_str();
212 }
213 return JNI_FALSE;
214 }
Brian Carlstromfad71432011-10-16 20:25:10 -0700215 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800216
Brian Carlstroma004aa92012-02-08 18:05:09 -0800217 // Check if we have an oat file in the cache
218 std::string cache_location(GetArtCacheFilenameOrDie(oat_filename));
Logan Chien0c717dd2012-03-28 18:31:07 +0800219 oat_file.reset(
220 OatFile::Open(cache_location, oat_filename, NULL, OatFile::kRelocNone));
Brian Carlstrom58cbbc22012-03-29 17:52:00 -0700221 if (oat_file.get() == NULL) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800222 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
223 << " does not exist for " << filename.c_str();
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800224 return JNI_TRUE;
225 }
226
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700227 const ImageHeader& image_header = runtime->GetHeap()->GetImageSpace()->GetImageHeader();
228 if (oat_file->GetOatHeader().GetImageFileLocationChecksum() != image_header.GetOatChecksum()) {
229 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
230 << " has out-of-date checksum compared to "
231 << image_header.GetImageRoot(ImageHeader::kOatLocation)->AsString()->ToModifiedUtf8();
232 return JNI_TRUE;
233 }
234
Brian Carlstroma004aa92012-02-08 18:05:09 -0800235 uint32_t location_checksum;
236 if (!DexFile::GetChecksum(filename.c_str(), location_checksum)) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800237 LOG(ERROR) << "DexFile_isDexOptNeeded failed to compute checksum of " << filename.c_str();
Brian Carlstroma004aa92012-02-08 18:05:09 -0800238 return JNI_TRUE;
239 }
240
Brian Carlstromafe25512012-06-27 17:02:58 -0700241 if (!ClassLinker::VerifyOatFileChecksums(oat_file.get(), filename.c_str(), location_checksum)) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800242 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
243 << " has out-of-date checksum compared to " << filename.c_str();
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800244 return JNI_TRUE;
245 }
246
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800247 if (debug_logging) {
248 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
249 << " is up-to-date for " << filename.c_str();
250 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700251 return JNI_FALSE;
252}
253
254static JNINativeMethod gMethods[] = {
255 NATIVE_METHOD(DexFile, closeDexFile, "(I)V"),
Ian Rogers66a556f2012-02-14 00:05:38 -0800256 NATIVE_METHOD(DexFile, defineClassNative, "(Ljava/lang/String;Ljava/lang/ClassLoader;I)Ljava/lang/Class;"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700257 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
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700262void register_dalvik_system_DexFile(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700263 REGISTER_NATIVE_METHODS("dalvik/system/DexFile");
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700264}
265
266} // namespace art