blob: b5e1c19d8ed67fa67d49a52b3d33e65e46caa44c [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 Rogers00f7d0e2012-07-19 15:28:27 -070027#include "scoped_thread_state_change.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 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070092 ScopedObjectAccess soa(env);
jeffhaoc393a4f2011-10-19 13:46:09 -070093 const DexFile* dex_file;
94 if (outputName.c_str() == NULL) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -080095 dex_file = Runtime::Current()->GetClassLinker()->FindDexFileInOatFileFromDexLocation(source);
jeffhaoc393a4f2011-10-19 13:46:09 -070096 } else {
Brian Carlstrom5b332c82012-02-01 15:02:31 -080097 std::string output(outputName.c_str());
Ian Rogers00f7d0e2012-07-19 15:28:27 -070098 dex_file =
99 Runtime::Current()->GetClassLinker()->FindOrCreateOatFileForDexLocation(source, output);
jeffhaoc393a4f2011-10-19 13:46:09 -0700100 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700101 if (dex_file == NULL) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800102 LOG(WARNING) << "Failed to open dex file: " << source;
Elliott Hugheseac76672012-05-24 21:56:51 -0700103 Thread::Current()->ThrowNewExceptionF("Ljava/io/IOException;", "Unable to open dex file: %s",
104 source.c_str());
jeffhaoc393a4f2011-10-19 13:46:09 -0700105 return 0;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700106 }
107 return static_cast<jint>(reinterpret_cast<uintptr_t>(dex_file));
108}
109
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700110static const DexFile* toDexFile(int dex_file_address)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700111 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700112 const DexFile* dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(dex_file_address));
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800113 if (dex_file == NULL) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700114 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NullPointerException;", "dex_file == null");
Brian Carlstromaded5f72011-10-07 17:15:04 -0700115 }
116 return dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700117}
118
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700119static void DexFile_closeDexFile(JNIEnv* env, jclass, jint cookie) {
120 const DexFile* dex_file;
121 {
122 ScopedObjectAccess soa(env);
123 dex_file = toDexFile(cookie);
124 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700125 if (dex_file == NULL) {
126 return;
127 }
128 if (Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) {
129 return;
130 }
131 delete dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700132}
133
Elliott Hughes0512f022012-03-15 22:10:52 -0700134static jclass DexFile_defineClassNative(JNIEnv* env, jclass, jstring javaName, jobject javaLoader,
135 jint cookie) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700136 ScopedObjectAccess soa(env);
Elliott Hugheseac76672012-05-24 21:56:51 -0700137 const DexFile* dex_file = toDexFile(cookie);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700138 if (dex_file == NULL) {
139 return NULL;
140 }
Brian Carlstromdf143242011-10-10 18:05:34 -0700141 ScopedUtfChars class_name(env, javaName);
142 if (class_name.c_str() == NULL) {
143 return NULL;
144 }
Elliott Hughes95572412011-12-13 18:14:20 -0800145 const std::string descriptor(DotToDescriptor(class_name.c_str()));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700146 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
147 if (dex_class_def == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700148 return NULL;
149 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700150 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
151 class_linker->RegisterDexFile(*dex_file);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700152 Object* class_loader_object = soa.Decode<Object*>(javaLoader);
Ian Rogers0399dde2012-06-06 17:09:28 -0700153 ClassLoader* class_loader = down_cast<ClassLoader*>(class_loader_object);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700154 Class* result = class_linker->DefineClass(descriptor, class_loader, *dex_file, *dex_class_def);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700155 return soa.AddLocalReference<jclass>(result);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700156}
157
Elliott Hughes0512f022012-03-15 22:10:52 -0700158static jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jint cookie) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700159 const DexFile* dex_file;
160 {
161 ScopedObjectAccess soa(env);
162 dex_file = toDexFile(cookie);
163 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700164 if (dex_file == NULL) {
165 return NULL;
166 }
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700167
168 std::vector<std::string> class_names;
169 for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
170 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
171 const char* descriptor = dex_file->GetClassDescriptor(class_def);
172 class_names.push_back(DescriptorToDot(descriptor));
173 }
174 return toStringArray(env, class_names);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700175}
176
Elliott Hughes0512f022012-03-15 22:10:52 -0700177static jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800178 bool debug_logging = false;
179
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700180 ScopedUtfChars filename(env, javaFilename);
181 if (filename.c_str() == NULL) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800182 LOG(ERROR) << "DexFile_isDexOptNeeded null filename";
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700183 return JNI_TRUE;
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700184 }
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700185
186 if (!OS::FileExists(filename.c_str())) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800187 LOG(ERROR) << "DexFile_isDexOptNeeded file '" << filename.c_str() << "' does not exist";
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700188 ScopedObjectAccess soa(env);
Elliott Hugheseac76672012-05-24 21:56:51 -0700189 Thread::Current()->ThrowNewExceptionF("Ljava/io/FileNotFoundException;", "%s", filename.c_str());
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700190 return JNI_TRUE;
191 }
192
193 // Always treat elements of the bootclasspath as up-to-date. The
194 // fact that code is running at all means that this should be true.
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700195 Runtime* runtime = Runtime::Current();
196 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700197 const std::vector<const DexFile*>& boot_class_path = class_linker->GetBootClassPath();
198 for (size_t i = 0; i < boot_class_path.size(); i++) {
199 if (boot_class_path[i]->GetLocation() == filename.c_str()) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800200 if (debug_logging) {
201 LOG(INFO) << "DexFile_isDexOptNeeded ignoring boot class path file: " << filename.c_str();
202 }
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700203 return JNI_FALSE;
204 }
205 }
206
Brian Carlstromafe25512012-06-27 17:02:58 -0700207 // Check if we have an oat file next to the dex file.
Brian Carlstroma004aa92012-02-08 18:05:09 -0800208 std::string oat_filename(OatFile::DexFilenameToOatFilename(filename.c_str()));
Logan Chien0c717dd2012-03-28 18:31:07 +0800209 UniquePtr<const OatFile> oat_file(
210 OatFile::Open(oat_filename, oat_filename, NULL, OatFile::kRelocNone));
Brian Carlstrom58cbbc22012-03-29 17:52:00 -0700211 if (oat_file.get() != NULL && oat_file->GetOatDexFile(filename.c_str()) != NULL) {
Brian Carlstromafe25512012-06-27 17:02:58 -0700212 uint32_t location_checksum;
213 // If we have no classes.dex checksum such as in a user build, assume up-to-date.
214 if (!DexFile::GetChecksum(filename.c_str(), location_checksum)) {
215 if (debug_logging) {
216 LOG(INFO) << "DexFile_isDexOptNeeded ignoring precompiled stripped file: " << filename.c_str();
217 }
218 return JNI_FALSE;
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800219 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700220 ScopedObjectAccess soa(env);
Brian Carlstromafe25512012-06-27 17:02:58 -0700221 if (ClassLinker::VerifyOatFileChecksums(oat_file.get(), filename.c_str(), location_checksum)) {
222 if (debug_logging) {
223 LOG(INFO) << "DexFile_isDexOptNeeded precompiled file " << oat_filename
224 << " is up-to-date checksum compared to " << filename.c_str();
225 }
226 return JNI_FALSE;
227 }
Brian Carlstromfad71432011-10-16 20:25:10 -0700228 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800229
Brian Carlstroma004aa92012-02-08 18:05:09 -0800230 // Check if we have an oat file in the cache
231 std::string cache_location(GetArtCacheFilenameOrDie(oat_filename));
Logan Chien0c717dd2012-03-28 18:31:07 +0800232 oat_file.reset(
233 OatFile::Open(cache_location, oat_filename, NULL, OatFile::kRelocNone));
Brian Carlstrom58cbbc22012-03-29 17:52:00 -0700234 if (oat_file.get() == NULL) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800235 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
236 << " does not exist for " << filename.c_str();
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800237 return JNI_TRUE;
238 }
239
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700240 Heap* heap = runtime->GetHeap();
241 const Spaces& spaces = heap->GetSpaces();
242 // TODO: C++0x auto
243 for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) {
244 if ((*cur)->IsImageSpace()) {
245 // TODO: Ensure this works with multiple image spaces.
246 const ImageHeader& image_header = (*cur)->AsImageSpace()->GetImageHeader();
247 if (oat_file->GetOatHeader().GetImageFileLocationChecksum() != image_header.GetOatChecksum()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700248 ScopedObjectAccess soa(env);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700249 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
250 << " has out-of-date checksum compared to "
251 << image_header.GetImageRoot(ImageHeader::kOatLocation)->AsString()->ToModifiedUtf8();
252 return JNI_TRUE;
253 }
254 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700255 }
256
Brian Carlstroma004aa92012-02-08 18:05:09 -0800257 uint32_t location_checksum;
258 if (!DexFile::GetChecksum(filename.c_str(), location_checksum)) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800259 LOG(ERROR) << "DexFile_isDexOptNeeded failed to compute checksum of " << filename.c_str();
Brian Carlstroma004aa92012-02-08 18:05:09 -0800260 return JNI_TRUE;
261 }
262
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700263 ScopedObjectAccess soa(env);
Brian Carlstromafe25512012-06-27 17:02:58 -0700264 if (!ClassLinker::VerifyOatFileChecksums(oat_file.get(), filename.c_str(), location_checksum)) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800265 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700266 << " has out-of-date checksum compared to " << filename.c_str();
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800267 return JNI_TRUE;
268 }
269
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800270 if (debug_logging) {
271 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
272 << " is up-to-date for " << filename.c_str();
273 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700274 return JNI_FALSE;
275}
276
277static JNINativeMethod gMethods[] = {
278 NATIVE_METHOD(DexFile, closeDexFile, "(I)V"),
Ian Rogers66a556f2012-02-14 00:05:38 -0800279 NATIVE_METHOD(DexFile, defineClassNative, "(Ljava/lang/String;Ljava/lang/ClassLoader;I)Ljava/lang/Class;"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700280 NATIVE_METHOD(DexFile, getClassNameList, "(I)[Ljava/lang/String;"),
281 NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
282 NATIVE_METHOD(DexFile, openDexFile, "(Ljava/lang/String;Ljava/lang/String;I)I"),
283};
284
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700285void register_dalvik_system_DexFile(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700286 REGISTER_NATIVE_METHODS("dalvik/system/DexFile");
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700287}
288
289} // namespace art