blob: e549a8bf74ce31330186b04ed32507cc220667b2 [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"
26#include "mirror/string.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080027#include "oat.h"
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -070028#include "os.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070029#include "runtime.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070030#include "scoped_thread_state_change.h"
Ian Rogersc9818482012-01-11 08:52:51 -080031#include "ScopedLocalRef.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070032#include "ScopedUtfChars.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070033#include "toStringArray.h"
34#include "zip_archive.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070035
36namespace art {
37
Brian Carlstromf91c8c32011-09-21 17:30:34 -070038// A smart pointer that provides read-only access to a Java string's UTF chars.
39// Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if
40// passed a null jstring. The correct idiom is:
41//
42// NullableScopedUtfChars name(env, javaName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -070043// if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070044// return NULL;
45// }
46// // ... use name.c_str()
47//
48// TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option.
49class NullableScopedUtfChars {
Elliott Hughesba8eee12012-01-24 20:25:24 -080050 public:
51 NullableScopedUtfChars(JNIEnv* env, jstring s) : mEnv(env), mString(s) {
52 mUtfChars = (s != NULL) ? env->GetStringUTFChars(s, NULL) : NULL;
53 }
54
55 ~NullableScopedUtfChars() {
56 if (mUtfChars) {
57 mEnv->ReleaseStringUTFChars(mString, mUtfChars);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070058 }
Elliott Hughesba8eee12012-01-24 20:25:24 -080059 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070060
Elliott Hughesba8eee12012-01-24 20:25:24 -080061 const char* c_str() const {
62 return mUtfChars;
63 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070064
Elliott Hughesba8eee12012-01-24 20:25:24 -080065 size_t size() const {
66 return strlen(mUtfChars);
67 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070068
Elliott Hughesba8eee12012-01-24 20:25:24 -080069 // Element access.
70 const char& operator[](size_t n) const {
71 return mUtfChars[n];
72 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070073
Elliott Hughesba8eee12012-01-24 20:25:24 -080074 private:
75 JNIEnv* mEnv;
76 jstring mString;
77 const char* mUtfChars;
Brian Carlstromf91c8c32011-09-21 17:30:34 -070078
Elliott Hughesba8eee12012-01-24 20:25:24 -080079 // Disallow copy and assignment.
80 NullableScopedUtfChars(const NullableScopedUtfChars&);
81 void operator=(const NullableScopedUtfChars&);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070082};
83
jeffhaoc393a4f2011-10-19 13:46:09 -070084static jint DexFile_openDexFile(JNIEnv* env, jclass, jstring javaSourceName, jstring javaOutputName, jint) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070085 ScopedUtfChars sourceName(env, javaSourceName);
86 if (sourceName.c_str() == NULL) {
87 return 0;
88 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -080089 std::string source(sourceName.c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -070090 NullableScopedUtfChars outputName(env, javaOutputName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -070091 if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070092 return 0;
93 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070094 ScopedObjectAccess soa(env);
jeffhaoc393a4f2011-10-19 13:46:09 -070095 const DexFile* dex_file;
96 if (outputName.c_str() == NULL) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -080097 dex_file = Runtime::Current()->GetClassLinker()->FindDexFileInOatFileFromDexLocation(source);
jeffhaoc393a4f2011-10-19 13:46:09 -070098 } else {
Brian Carlstrom5b332c82012-02-01 15:02:31 -080099 std::string output(outputName.c_str());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700100 dex_file =
101 Runtime::Current()->GetClassLinker()->FindOrCreateOatFileForDexLocation(source, output);
jeffhaoc393a4f2011-10-19 13:46:09 -0700102 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700103 if (dex_file == NULL) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800104 LOG(WARNING) << "Failed to open dex file: " << source;
Elliott Hugheseac76672012-05-24 21:56:51 -0700105 Thread::Current()->ThrowNewExceptionF("Ljava/io/IOException;", "Unable to open dex file: %s",
106 source.c_str());
jeffhaoc393a4f2011-10-19 13:46:09 -0700107 return 0;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700108 }
109 return static_cast<jint>(reinterpret_cast<uintptr_t>(dex_file));
110}
111
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700112static const DexFile* toDexFile(int dex_file_address)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700113 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700114 const DexFile* dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(dex_file_address));
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800115 if (dex_file == NULL) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700116 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NullPointerException;", "dex_file == null");
Brian Carlstromaded5f72011-10-07 17:15:04 -0700117 }
118 return dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700119}
120
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700121static void DexFile_closeDexFile(JNIEnv* env, jclass, jint cookie) {
122 const DexFile* dex_file;
123 {
124 ScopedObjectAccess soa(env);
125 dex_file = toDexFile(cookie);
126 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700127 if (dex_file == NULL) {
128 return;
129 }
130 if (Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) {
131 return;
132 }
133 delete dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700134}
135
Elliott Hughes0512f022012-03-15 22:10:52 -0700136static jclass DexFile_defineClassNative(JNIEnv* env, jclass, jstring javaName, jobject javaLoader,
137 jint cookie) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700138 ScopedObjectAccess soa(env);
Elliott Hugheseac76672012-05-24 21:56:51 -0700139 const DexFile* dex_file = toDexFile(cookie);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700140 if (dex_file == NULL) {
141 return NULL;
142 }
Brian Carlstromdf143242011-10-10 18:05:34 -0700143 ScopedUtfChars class_name(env, javaName);
144 if (class_name.c_str() == NULL) {
145 return NULL;
146 }
Elliott Hughes95572412011-12-13 18:14:20 -0800147 const std::string descriptor(DotToDescriptor(class_name.c_str()));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700148 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
149 if (dex_class_def == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700150 return NULL;
151 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700152 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
153 class_linker->RegisterDexFile(*dex_file);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800154 mirror::ClassLoader* class_loader = soa.Decode<mirror::ClassLoader*>(javaLoader);
155 mirror::Class* result = class_linker->DefineClass(descriptor, class_loader, *dex_file, *dex_class_def);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700156 return soa.AddLocalReference<jclass>(result);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700157}
158
Elliott Hughes0512f022012-03-15 22:10:52 -0700159static jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jint cookie) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700160 const DexFile* dex_file;
161 {
162 ScopedObjectAccess soa(env);
163 dex_file = toDexFile(cookie);
164 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700165 if (dex_file == NULL) {
166 return NULL;
167 }
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700168
169 std::vector<std::string> class_names;
170 for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
171 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
172 const char* descriptor = dex_file->GetClassDescriptor(class_def);
173 class_names.push_back(DescriptorToDot(descriptor));
174 }
175 return toStringArray(env, class_names);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700176}
177
Elliott Hughes0512f022012-03-15 22:10:52 -0700178static jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800179 bool debug_logging = false;
180
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700181 ScopedUtfChars filename(env, javaFilename);
182 if (filename.c_str() == NULL) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800183 LOG(ERROR) << "DexFile_isDexOptNeeded null filename";
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700184 return JNI_TRUE;
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700185 }
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700186
187 if (!OS::FileExists(filename.c_str())) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800188 LOG(ERROR) << "DexFile_isDexOptNeeded file '" << filename.c_str() << "' does not exist";
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700189 ScopedObjectAccess soa(env);
Elliott Hugheseac76672012-05-24 21:56:51 -0700190 Thread::Current()->ThrowNewExceptionF("Ljava/io/FileNotFoundException;", "%s", filename.c_str());
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700191 return JNI_TRUE;
192 }
193
194 // Always treat elements of the bootclasspath as up-to-date. The
195 // fact that code is running at all means that this should be true.
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700196 Runtime* runtime = Runtime::Current();
197 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700198 const std::vector<const DexFile*>& boot_class_path = class_linker->GetBootClassPath();
199 for (size_t i = 0; i < boot_class_path.size(); i++) {
200 if (boot_class_path[i]->GetLocation() == filename.c_str()) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800201 if (debug_logging) {
202 LOG(INFO) << "DexFile_isDexOptNeeded ignoring boot class path file: " << filename.c_str();
203 }
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700204 return JNI_FALSE;
205 }
206 }
207
Brian Carlstromafe25512012-06-27 17:02:58 -0700208 // Check if we have an oat file next to the dex file.
Brian Carlstroma004aa92012-02-08 18:05:09 -0800209 std::string oat_filename(OatFile::DexFilenameToOatFilename(filename.c_str()));
Brian Carlstrom1cac3432012-12-12 10:56:22 -0800210 UniquePtr<const OatFile> oat_file(OatFile::Open(oat_filename, oat_filename, NULL));
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));
Brian Carlstrom1cac3432012-12-12 10:56:22 -0800232 oat_file.reset(OatFile::Open(cache_location, oat_filename, NULL));
Brian Carlstrom58cbbc22012-03-29 17:52:00 -0700233 if (oat_file.get() == NULL) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800234 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
235 << " does not exist for " << filename.c_str();
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800236 return JNI_TRUE;
237 }
238
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700239 Heap* heap = runtime->GetHeap();
240 const Spaces& spaces = heap->GetSpaces();
241 // TODO: C++0x auto
242 for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) {
243 if ((*cur)->IsImageSpace()) {
244 // TODO: Ensure this works with multiple image spaces.
245 const ImageHeader& image_header = (*cur)->AsImageSpace()->GetImageHeader();
Brian Carlstrom28db0122012-10-18 16:20:41 -0700246 if (oat_file->GetOatHeader().GetImageFileLocationOatChecksum() != image_header.GetOatChecksum()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700247 ScopedObjectAccess soa(env);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700248 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
Brian Carlstrom28db0122012-10-18 16:20:41 -0700249 << " has out-of-date oat checksum compared to "
250 << image_header.GetImageRoot(ImageHeader::kOatLocation)->AsString()->ToModifiedUtf8();
251 return JNI_TRUE;
252 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800253 if (oat_file->GetOatHeader().GetImageFileLocationOatDataBegin()
254 != reinterpret_cast<uint32_t>(image_header.GetOatDataBegin())) {
Brian Carlstrom28db0122012-10-18 16:20:41 -0700255 ScopedObjectAccess soa(env);
256 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
257 << " has out-of-date oat begin compared to "
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700258 << image_header.GetImageRoot(ImageHeader::kOatLocation)->AsString()->ToModifiedUtf8();
259 return JNI_TRUE;
260 }
261 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700262 }
263
Brian Carlstroma004aa92012-02-08 18:05:09 -0800264 uint32_t location_checksum;
265 if (!DexFile::GetChecksum(filename.c_str(), location_checksum)) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800266 LOG(ERROR) << "DexFile_isDexOptNeeded failed to compute checksum of " << filename.c_str();
Brian Carlstroma004aa92012-02-08 18:05:09 -0800267 return JNI_TRUE;
268 }
269
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700270 ScopedObjectAccess soa(env);
Brian Carlstromafe25512012-06-27 17:02:58 -0700271 if (!ClassLinker::VerifyOatFileChecksums(oat_file.get(), filename.c_str(), location_checksum)) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800272 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700273 << " has out-of-date checksum compared to " << filename.c_str();
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800274 return JNI_TRUE;
275 }
276
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800277 if (debug_logging) {
278 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
279 << " is up-to-date for " << filename.c_str();
280 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700281 return JNI_FALSE;
282}
283
284static JNINativeMethod gMethods[] = {
285 NATIVE_METHOD(DexFile, closeDexFile, "(I)V"),
Ian Rogers66a556f2012-02-14 00:05:38 -0800286 NATIVE_METHOD(DexFile, defineClassNative, "(Ljava/lang/String;Ljava/lang/ClassLoader;I)Ljava/lang/Class;"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700287 NATIVE_METHOD(DexFile, getClassNameList, "(I)[Ljava/lang/String;"),
288 NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
289 NATIVE_METHOD(DexFile, openDexFile, "(Ljava/lang/String;Ljava/lang/String;I)I"),
290};
291
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700292void register_dalvik_system_DexFile(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700293 REGISTER_NATIVE_METHODS("dalvik/system/DexFile");
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700294}
295
296} // namespace art