blob: 03907039144ec11e44eca6baeca4737425991dbd [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
Elliott Hughes07ed66b2012-12-12 18:34:25 -080019#include "base/logging.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070020#include "class_linker.h"
21#include "dex_file.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080022#include "gc/space.h"
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070023#include "image.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070024#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "mirror/class_loader.h"
Ian Rogers05f30572013-02-20 12:13:11 -080026#include "mirror/object-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "mirror/string.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080028#include "oat.h"
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -070029#include "os.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070030#include "runtime.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070031#include "scoped_thread_state_change.h"
Ian Rogersc9818482012-01-11 08:52:51 -080032#include "ScopedLocalRef.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070033#include "ScopedUtfChars.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070034#include "toStringArray.h"
35#include "zip_archive.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070036
37namespace art {
38
Brian Carlstromf91c8c32011-09-21 17:30:34 -070039// A smart pointer that provides read-only access to a Java string's UTF chars.
40// Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if
41// passed a null jstring. The correct idiom is:
42//
43// NullableScopedUtfChars name(env, javaName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -070044// if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070045// return NULL;
46// }
47// // ... use name.c_str()
48//
49// TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option.
50class NullableScopedUtfChars {
Elliott Hughesba8eee12012-01-24 20:25:24 -080051 public:
52 NullableScopedUtfChars(JNIEnv* env, jstring s) : mEnv(env), mString(s) {
53 mUtfChars = (s != NULL) ? env->GetStringUTFChars(s, NULL) : NULL;
54 }
55
56 ~NullableScopedUtfChars() {
57 if (mUtfChars) {
58 mEnv->ReleaseStringUTFChars(mString, mUtfChars);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070059 }
Elliott Hughesba8eee12012-01-24 20:25:24 -080060 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070061
Elliott Hughesba8eee12012-01-24 20:25:24 -080062 const char* c_str() const {
63 return mUtfChars;
64 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070065
Elliott Hughesba8eee12012-01-24 20:25:24 -080066 size_t size() const {
67 return strlen(mUtfChars);
68 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070069
Elliott Hughesba8eee12012-01-24 20:25:24 -080070 // Element access.
71 const char& operator[](size_t n) const {
72 return mUtfChars[n];
73 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070074
Elliott Hughesba8eee12012-01-24 20:25:24 -080075 private:
76 JNIEnv* mEnv;
77 jstring mString;
78 const char* mUtfChars;
Brian Carlstromf91c8c32011-09-21 17:30:34 -070079
Elliott Hughesba8eee12012-01-24 20:25:24 -080080 // Disallow copy and assignment.
81 NullableScopedUtfChars(const NullableScopedUtfChars&);
82 void operator=(const NullableScopedUtfChars&);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070083};
84
jeffhaoc393a4f2011-10-19 13:46:09 -070085static jint DexFile_openDexFile(JNIEnv* env, jclass, jstring javaSourceName, jstring javaOutputName, jint) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070086 ScopedUtfChars sourceName(env, javaSourceName);
87 if (sourceName.c_str() == NULL) {
88 return 0;
89 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -080090 std::string source(sourceName.c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -070091 NullableScopedUtfChars outputName(env, javaOutputName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -070092 if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070093 return 0;
94 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070095 ScopedObjectAccess soa(env);
jeffhaoc393a4f2011-10-19 13:46:09 -070096 const DexFile* dex_file;
97 if (outputName.c_str() == NULL) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -080098 dex_file = Runtime::Current()->GetClassLinker()->FindDexFileInOatFileFromDexLocation(source);
jeffhaoc393a4f2011-10-19 13:46:09 -070099 } else {
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800100 std::string output(outputName.c_str());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700101 dex_file =
102 Runtime::Current()->GetClassLinker()->FindOrCreateOatFileForDexLocation(source, output);
jeffhaoc393a4f2011-10-19 13:46:09 -0700103 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700104 if (dex_file == NULL) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800105 LOG(WARNING) << "Failed to open dex file: " << source;
Elliott Hugheseac76672012-05-24 21:56:51 -0700106 Thread::Current()->ThrowNewExceptionF("Ljava/io/IOException;", "Unable to open dex file: %s",
107 source.c_str());
jeffhaoc393a4f2011-10-19 13:46:09 -0700108 return 0;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700109 }
110 return static_cast<jint>(reinterpret_cast<uintptr_t>(dex_file));
111}
112
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700113static const DexFile* toDexFile(int dex_file_address)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700114 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700115 const DexFile* dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(dex_file_address));
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800116 if (dex_file == NULL) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700117 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NullPointerException;", "dex_file == null");
Brian Carlstromaded5f72011-10-07 17:15:04 -0700118 }
119 return dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700120}
121
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700122static void DexFile_closeDexFile(JNIEnv* env, jclass, jint cookie) {
123 const DexFile* dex_file;
124 {
125 ScopedObjectAccess soa(env);
126 dex_file = toDexFile(cookie);
127 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700128 if (dex_file == NULL) {
129 return;
130 }
131 if (Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) {
132 return;
133 }
134 delete dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700135}
136
Elliott Hughes0512f022012-03-15 22:10:52 -0700137static jclass DexFile_defineClassNative(JNIEnv* env, jclass, jstring javaName, jobject javaLoader,
138 jint cookie) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700139 ScopedObjectAccess soa(env);
Elliott Hugheseac76672012-05-24 21:56:51 -0700140 const DexFile* dex_file = toDexFile(cookie);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700141 if (dex_file == NULL) {
142 return NULL;
143 }
Brian Carlstromdf143242011-10-10 18:05:34 -0700144 ScopedUtfChars class_name(env, javaName);
145 if (class_name.c_str() == NULL) {
146 return NULL;
147 }
Elliott Hughes95572412011-12-13 18:14:20 -0800148 const std::string descriptor(DotToDescriptor(class_name.c_str()));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700149 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
150 if (dex_class_def == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700151 return NULL;
152 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700153 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
154 class_linker->RegisterDexFile(*dex_file);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800155 mirror::ClassLoader* class_loader = soa.Decode<mirror::ClassLoader*>(javaLoader);
156 mirror::Class* result = class_linker->DefineClass(descriptor, class_loader, *dex_file, *dex_class_def);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700157 return soa.AddLocalReference<jclass>(result);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700158}
159
Elliott Hughes0512f022012-03-15 22:10:52 -0700160static jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jint cookie) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700161 const DexFile* dex_file;
162 {
163 ScopedObjectAccess soa(env);
164 dex_file = toDexFile(cookie);
165 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700166 if (dex_file == NULL) {
167 return NULL;
168 }
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700169
170 std::vector<std::string> class_names;
171 for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
172 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
173 const char* descriptor = dex_file->GetClassDescriptor(class_def);
174 class_names.push_back(DescriptorToDot(descriptor));
175 }
176 return toStringArray(env, class_names);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700177}
178
Elliott Hughes0512f022012-03-15 22:10:52 -0700179static jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800180 bool debug_logging = false;
181
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700182 ScopedUtfChars filename(env, javaFilename);
183 if (filename.c_str() == NULL) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800184 LOG(ERROR) << "DexFile_isDexOptNeeded null filename";
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700185 return JNI_TRUE;
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700186 }
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700187
188 if (!OS::FileExists(filename.c_str())) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800189 LOG(ERROR) << "DexFile_isDexOptNeeded file '" << filename.c_str() << "' does not exist";
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700190 ScopedObjectAccess soa(env);
Elliott Hugheseac76672012-05-24 21:56:51 -0700191 Thread::Current()->ThrowNewExceptionF("Ljava/io/FileNotFoundException;", "%s", filename.c_str());
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700192 return JNI_TRUE;
193 }
194
195 // Always treat elements of the bootclasspath as up-to-date. The
196 // fact that code is running at all means that this should be true.
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700197 Runtime* runtime = Runtime::Current();
198 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700199 const std::vector<const DexFile*>& boot_class_path = class_linker->GetBootClassPath();
200 for (size_t i = 0; i < boot_class_path.size(); i++) {
201 if (boot_class_path[i]->GetLocation() == filename.c_str()) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800202 if (debug_logging) {
203 LOG(INFO) << "DexFile_isDexOptNeeded ignoring boot class path file: " << filename.c_str();
204 }
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700205 return JNI_FALSE;
206 }
207 }
208
Brian Carlstromafe25512012-06-27 17:02:58 -0700209 // Check if we have an oat file next to the dex file.
Brian Carlstroma004aa92012-02-08 18:05:09 -0800210 std::string oat_filename(OatFile::DexFilenameToOatFilename(filename.c_str()));
Brian Carlstrom1cac3432012-12-12 10:56:22 -0800211 UniquePtr<const OatFile> oat_file(OatFile::Open(oat_filename, oat_filename, NULL));
Brian Carlstrom58cbbc22012-03-29 17:52:00 -0700212 if (oat_file.get() != NULL && oat_file->GetOatDexFile(filename.c_str()) != NULL) {
Brian Carlstromafe25512012-06-27 17:02:58 -0700213 uint32_t location_checksum;
214 // If we have no classes.dex checksum such as in a user build, assume up-to-date.
215 if (!DexFile::GetChecksum(filename.c_str(), location_checksum)) {
216 if (debug_logging) {
217 LOG(INFO) << "DexFile_isDexOptNeeded ignoring precompiled stripped file: " << filename.c_str();
218 }
219 return JNI_FALSE;
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800220 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700221 ScopedObjectAccess soa(env);
Brian Carlstromafe25512012-06-27 17:02:58 -0700222 if (ClassLinker::VerifyOatFileChecksums(oat_file.get(), filename.c_str(), location_checksum)) {
223 if (debug_logging) {
224 LOG(INFO) << "DexFile_isDexOptNeeded precompiled file " << oat_filename
225 << " is up-to-date checksum compared to " << filename.c_str();
226 }
227 return JNI_FALSE;
228 }
Brian Carlstromfad71432011-10-16 20:25:10 -0700229 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800230
Brian Carlstroma004aa92012-02-08 18:05:09 -0800231 // Check if we have an oat file in the cache
232 std::string cache_location(GetArtCacheFilenameOrDie(oat_filename));
Brian Carlstrom1cac3432012-12-12 10:56:22 -0800233 oat_file.reset(OatFile::Open(cache_location, oat_filename, NULL));
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();
Brian Carlstrom28db0122012-10-18 16:20:41 -0700247 if (oat_file->GetOatHeader().GetImageFileLocationOatChecksum() != 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
Brian Carlstrom28db0122012-10-18 16:20:41 -0700250 << " has out-of-date oat checksum compared to "
251 << image_header.GetImageRoot(ImageHeader::kOatLocation)->AsString()->ToModifiedUtf8();
252 return JNI_TRUE;
253 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800254 if (oat_file->GetOatHeader().GetImageFileLocationOatDataBegin()
255 != reinterpret_cast<uint32_t>(image_header.GetOatDataBegin())) {
Brian Carlstrom28db0122012-10-18 16:20:41 -0700256 ScopedObjectAccess soa(env);
257 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
258 << " has out-of-date oat begin compared to "
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700259 << image_header.GetImageRoot(ImageHeader::kOatLocation)->AsString()->ToModifiedUtf8();
260 return JNI_TRUE;
261 }
262 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700263 }
264
Brian Carlstroma004aa92012-02-08 18:05:09 -0800265 uint32_t location_checksum;
266 if (!DexFile::GetChecksum(filename.c_str(), location_checksum)) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800267 LOG(ERROR) << "DexFile_isDexOptNeeded failed to compute checksum of " << filename.c_str();
Brian Carlstroma004aa92012-02-08 18:05:09 -0800268 return JNI_TRUE;
269 }
270
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700271 ScopedObjectAccess soa(env);
Brian Carlstromafe25512012-06-27 17:02:58 -0700272 if (!ClassLinker::VerifyOatFileChecksums(oat_file.get(), filename.c_str(), location_checksum)) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800273 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700274 << " has out-of-date checksum compared to " << filename.c_str();
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800275 return JNI_TRUE;
276 }
277
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800278 if (debug_logging) {
279 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
280 << " is up-to-date for " << filename.c_str();
281 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700282 return JNI_FALSE;
283}
284
285static JNINativeMethod gMethods[] = {
286 NATIVE_METHOD(DexFile, closeDexFile, "(I)V"),
Ian Rogers66a556f2012-02-14 00:05:38 -0800287 NATIVE_METHOD(DexFile, defineClassNative, "(Ljava/lang/String;Ljava/lang/ClassLoader;I)Ljava/lang/Class;"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700288 NATIVE_METHOD(DexFile, getClassNameList, "(I)[Ljava/lang/String;"),
289 NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
290 NATIVE_METHOD(DexFile, openDexFile, "(Ljava/lang/String;Ljava/lang/String;I)I"),
291};
292
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700293void register_dalvik_system_DexFile(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700294 REGISTER_NATIVE_METHODS("dalvik/system/DexFile");
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700295}
296
297} // namespace art