blob: 823013a9509512a4e213bb9fd3cf8cb6e30fc855 [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"
Ian Rogers62d6c772013-02-27 08:32:07 -080021#include "common_throws.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070022#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070023#include "gc/space/image_space.h"
24#include "gc/space/space-inl.h"
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070025#include "image.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070026#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "mirror/class_loader.h"
Ian Rogers05f30572013-02-20 12:13:11 -080028#include "mirror/object-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "mirror/string.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080030#include "oat.h"
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -070031#include "os.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070032#include "runtime.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070033#include "scoped_thread_state_change.h"
Ian Rogersc9818482012-01-11 08:52:51 -080034#include "ScopedLocalRef.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070035#include "ScopedUtfChars.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070036#include "toStringArray.h"
37#include "zip_archive.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070038
39namespace art {
40
Brian Carlstromf91c8c32011-09-21 17:30:34 -070041// A smart pointer that provides read-only access to a Java string's UTF chars.
42// Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if
43// passed a null jstring. The correct idiom is:
44//
45// NullableScopedUtfChars name(env, javaName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -070046// if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070047// return NULL;
48// }
49// // ... use name.c_str()
50//
51// TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option.
52class NullableScopedUtfChars {
Elliott Hughesba8eee12012-01-24 20:25:24 -080053 public:
54 NullableScopedUtfChars(JNIEnv* env, jstring s) : mEnv(env), mString(s) {
55 mUtfChars = (s != NULL) ? env->GetStringUTFChars(s, NULL) : NULL;
56 }
57
58 ~NullableScopedUtfChars() {
59 if (mUtfChars) {
60 mEnv->ReleaseStringUTFChars(mString, mUtfChars);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070061 }
Elliott Hughesba8eee12012-01-24 20:25:24 -080062 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070063
Elliott Hughesba8eee12012-01-24 20:25:24 -080064 const char* c_str() const {
65 return mUtfChars;
66 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070067
Elliott Hughesba8eee12012-01-24 20:25:24 -080068 size_t size() const {
69 return strlen(mUtfChars);
70 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070071
Elliott Hughesba8eee12012-01-24 20:25:24 -080072 // Element access.
73 const char& operator[](size_t n) const {
74 return mUtfChars[n];
75 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070076
Elliott Hughesba8eee12012-01-24 20:25:24 -080077 private:
78 JNIEnv* mEnv;
79 jstring mString;
80 const char* mUtfChars;
Brian Carlstromf91c8c32011-09-21 17:30:34 -070081
Elliott Hughesba8eee12012-01-24 20:25:24 -080082 // Disallow copy and assignment.
83 NullableScopedUtfChars(const NullableScopedUtfChars&);
84 void operator=(const NullableScopedUtfChars&);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070085};
86
Brian Carlstrom7571e8b2013-08-12 17:04:14 -070087static jint DexFile_openDexFileNative(JNIEnv* env, jclass, jstring javaSourceName, jstring javaOutputName, jint) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070088 ScopedUtfChars sourceName(env, javaSourceName);
89 if (sourceName.c_str() == NULL) {
90 return 0;
91 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -070092 std::string dex_location(sourceName.c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -070093 NullableScopedUtfChars outputName(env, javaOutputName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -070094 if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070095 return 0;
96 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070097 ScopedObjectAccess soa(env);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -070098
99 uint32_t dex_location_checksum;
100 if (!DexFile::GetChecksum(dex_location, &dex_location_checksum)) {
101 LOG(WARNING) << "Failed to compute checksum: " << dex_location;
Ian Rogers62d6c772013-02-27 08:32:07 -0800102 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
103 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/io/IOException;",
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700104 "Unable to get checksum of dex file: %s", dex_location.c_str());
105 return 0;
106 }
107
108 ClassLinker* linker = Runtime::Current()->GetClassLinker();
109 const DexFile* dex_file;
110 if (outputName.c_str() == NULL) {
111 dex_file = linker->FindDexFileInOatFileFromDexLocation(dex_location, dex_location_checksum);
112 } else {
113 std::string oat_location(outputName.c_str());
114 dex_file = linker->FindOrCreateOatFileForDexLocation(dex_location, dex_location_checksum, oat_location);
115 }
116 if (dex_file == NULL) {
117 LOG(WARNING) << "Failed to open dex file: " << dex_location;
118 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
119 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/io/IOException;",
120 "Unable to open dex file: %s", dex_location.c_str());
jeffhaoc393a4f2011-10-19 13:46:09 -0700121 return 0;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700122 }
123 return static_cast<jint>(reinterpret_cast<uintptr_t>(dex_file));
124}
125
Ian Rogers62d6c772013-02-27 08:32:07 -0800126static const DexFile* toDexFile(int dex_file_address) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700127 const DexFile* dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(dex_file_address));
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800128 if (dex_file == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800129 ThrowNullPointerException(NULL, "dex_file == null");
Brian Carlstromaded5f72011-10-07 17:15:04 -0700130 }
131 return dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700132}
133
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700134static void DexFile_closeDexFile(JNIEnv* env, jclass, jint cookie) {
135 const DexFile* dex_file;
136 {
137 ScopedObjectAccess soa(env);
138 dex_file = toDexFile(cookie);
139 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700140 if (dex_file == NULL) {
141 return;
142 }
143 if (Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) {
144 return;
145 }
146 delete dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700147}
148
Elliott Hughes0512f022012-03-15 22:10:52 -0700149static jclass DexFile_defineClassNative(JNIEnv* env, jclass, jstring javaName, jobject javaLoader,
150 jint cookie) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700151 ScopedObjectAccess soa(env);
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) {
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700154 VLOG(class_linker) << "Failed to find dex_file";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700155 return NULL;
156 }
Brian Carlstromdf143242011-10-10 18:05:34 -0700157 ScopedUtfChars class_name(env, javaName);
158 if (class_name.c_str() == NULL) {
Brian Carlstrom2e450bf2013-09-06 15:39:46 -0700159 VLOG(class_linker) << "Failed to find class_name";
Brian Carlstromdf143242011-10-10 18:05:34 -0700160 return NULL;
161 }
Elliott Hughes95572412011-12-13 18:14:20 -0800162 const std::string descriptor(DotToDescriptor(class_name.c_str()));
Ian Rogersee39a102013-09-19 02:56:49 -0700163 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor.c_str());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700164 if (dex_class_def == NULL) {
Brian Carlstrom2e450bf2013-09-06 15:39:46 -0700165 VLOG(class_linker) << "Failed to find dex_class_def";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700166 return NULL;
167 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700168 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
169 class_linker->RegisterDexFile(*dex_file);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800170 mirror::ClassLoader* class_loader = soa.Decode<mirror::ClassLoader*>(javaLoader);
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700171 mirror::Class* result = class_linker->DefineClass(descriptor.c_str(), class_loader, *dex_file,
172 *dex_class_def);
Brian Carlstrom2e450bf2013-09-06 15:39:46 -0700173 VLOG(class_linker) << "DexFile_defineClassNative returning " << result;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700174 return soa.AddLocalReference<jclass>(result);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700175}
176
Elliott Hughes0512f022012-03-15 22:10:52 -0700177static jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jint cookie) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700178 const DexFile* dex_file;
179 {
180 ScopedObjectAccess soa(env);
181 dex_file = toDexFile(cookie);
182 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700183 if (dex_file == NULL) {
184 return NULL;
185 }
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700186
187 std::vector<std::string> class_names;
188 for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
189 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
190 const char* descriptor = dex_file->GetClassDescriptor(class_def);
191 class_names.push_back(DescriptorToDot(descriptor));
192 }
193 return toStringArray(env, class_names);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700194}
195
Elliott Hughes0512f022012-03-15 22:10:52 -0700196static jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800197 bool debug_logging = false;
198
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700199 ScopedUtfChars filename(env, javaFilename);
200 if (filename.c_str() == NULL) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800201 LOG(ERROR) << "DexFile_isDexOptNeeded null filename";
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700202 return JNI_TRUE;
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700203 }
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700204
205 if (!OS::FileExists(filename.c_str())) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800206 LOG(ERROR) << "DexFile_isDexOptNeeded file '" << filename.c_str() << "' does not exist";
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700207 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800208 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
209 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/io/FileNotFoundException;",
210 "%s", filename.c_str());
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700211 return JNI_TRUE;
212 }
213
214 // Always treat elements of the bootclasspath as up-to-date. The
215 // fact that code is running at all means that this should be true.
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700216 Runtime* runtime = Runtime::Current();
217 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700218 const std::vector<const DexFile*>& boot_class_path = class_linker->GetBootClassPath();
219 for (size_t i = 0; i < boot_class_path.size(); i++) {
220 if (boot_class_path[i]->GetLocation() == filename.c_str()) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800221 if (debug_logging) {
222 LOG(INFO) << "DexFile_isDexOptNeeded ignoring boot class path file: " << filename.c_str();
223 }
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700224 return JNI_FALSE;
225 }
226 }
227
Brian Carlstrom30e2ea42013-06-19 23:25:37 -0700228 // Check if we have an odex file next to the dex file.
229 std::string odex_filename(OatFile::DexFilenameToOdexFilename(filename.c_str()));
Brian Carlstromf1d34552013-07-12 20:22:23 -0700230 UniquePtr<const OatFile> oat_file(OatFile::Open(odex_filename, odex_filename, NULL, false));
Ian Rogers33e95662013-05-20 20:29:14 -0700231 if (oat_file.get() != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700232 ScopedObjectAccess soa(env);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700233 const art::OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(filename.c_str(), NULL);
Ian Rogers33e95662013-05-20 20:29:14 -0700234 if (oat_dex_file == NULL) {
Brian Carlstromafe25512012-06-27 17:02:58 -0700235 if (debug_logging) {
Ian Rogers33e95662013-05-20 20:29:14 -0700236 LOG(INFO) << "DexFile_isDexOptNeeded GetOatDexFile failed";
Brian Carlstromafe25512012-06-27 17:02:58 -0700237 }
Ian Rogers33e95662013-05-20 20:29:14 -0700238 } else {
239 uint32_t location_checksum;
240 // If we have no classes.dex checksum such as in a user build, assume up-to-date.
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700241 if (!DexFile::GetChecksum(filename.c_str(), &location_checksum)) {
Ian Rogers33e95662013-05-20 20:29:14 -0700242 if (debug_logging) {
243 LOG(INFO) << "DexFile_isDexOptNeeded ignoring precompiled stripped file: "
244 << filename.c_str();
245 }
246 return JNI_FALSE;
247 }
248 if (ClassLinker::VerifyOatFileChecksums(oat_file.get(), filename.c_str(), location_checksum)) {
249 if (debug_logging) {
Brian Carlstrom30e2ea42013-06-19 23:25:37 -0700250 LOG(INFO) << "DexFile_isDexOptNeeded precompiled file " << odex_filename
Ian Rogers33e95662013-05-20 20:29:14 -0700251 << " is up-to-date checksum compared to " << filename.c_str();
252 }
253 return JNI_FALSE;
254 }
Brian Carlstromafe25512012-06-27 17:02:58 -0700255 }
Brian Carlstromfad71432011-10-16 20:25:10 -0700256 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800257
Brian Carlstroma004aa92012-02-08 18:05:09 -0800258 // Check if we have an oat file in the cache
Brian Carlstrom30e2ea42013-06-19 23:25:37 -0700259 std::string cache_location(GetDalvikCacheFilenameOrDie(filename.c_str()));
Brian Carlstromf1d34552013-07-12 20:22:23 -0700260 oat_file.reset(OatFile::Open(cache_location, filename.c_str(), NULL, false));
Brian Carlstrom58cbbc22012-03-29 17:52:00 -0700261 if (oat_file.get() == NULL) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800262 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
263 << " does not exist for " << filename.c_str();
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800264 return JNI_TRUE;
265 }
266
Mathieu Chartier02e25112013-08-14 16:14:24 -0700267 for (const auto& space : runtime->GetHeap()->GetContinuousSpaces()) {
268 if (space->IsImageSpace()) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700269 // TODO: Ensure this works with multiple image spaces.
Mathieu Chartier02e25112013-08-14 16:14:24 -0700270 const ImageHeader& image_header = space->AsImageSpace()->GetImageHeader();
Brian Carlstrom28db0122012-10-18 16:20:41 -0700271 if (oat_file->GetOatHeader().GetImageFileLocationOatChecksum() != image_header.GetOatChecksum()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700272 ScopedObjectAccess soa(env);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700273 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
Brian Carlstrom28db0122012-10-18 16:20:41 -0700274 << " has out-of-date oat checksum compared to "
275 << image_header.GetImageRoot(ImageHeader::kOatLocation)->AsString()->ToModifiedUtf8();
276 return JNI_TRUE;
277 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800278 if (oat_file->GetOatHeader().GetImageFileLocationOatDataBegin()
279 != reinterpret_cast<uint32_t>(image_header.GetOatDataBegin())) {
Brian Carlstrom28db0122012-10-18 16:20:41 -0700280 ScopedObjectAccess soa(env);
281 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
282 << " has out-of-date oat begin compared to "
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700283 << image_header.GetImageRoot(ImageHeader::kOatLocation)->AsString()->ToModifiedUtf8();
284 return JNI_TRUE;
285 }
286 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700287 }
288
Ian Rogers33e95662013-05-20 20:29:14 -0700289 ScopedObjectAccess soa(env);
Brian Carlstroma004aa92012-02-08 18:05:09 -0800290 uint32_t location_checksum;
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700291 if (!DexFile::GetChecksum(filename.c_str(), &location_checksum)) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800292 LOG(ERROR) << "DexFile_isDexOptNeeded failed to compute checksum of " << filename.c_str();
Brian Carlstroma004aa92012-02-08 18:05:09 -0800293 return JNI_TRUE;
294 }
295
Brian Carlstromafe25512012-06-27 17:02:58 -0700296 if (!ClassLinker::VerifyOatFileChecksums(oat_file.get(), filename.c_str(), location_checksum)) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800297 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700298 << " has out-of-date checksum compared to " << filename.c_str();
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800299 return JNI_TRUE;
300 }
301
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800302 if (debug_logging) {
303 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
304 << " is up-to-date for " << filename.c_str();
305 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700306 return JNI_FALSE;
307}
308
309static JNINativeMethod gMethods[] = {
310 NATIVE_METHOD(DexFile, closeDexFile, "(I)V"),
Ian Rogers66a556f2012-02-14 00:05:38 -0800311 NATIVE_METHOD(DexFile, defineClassNative, "(Ljava/lang/String;Ljava/lang/ClassLoader;I)Ljava/lang/Class;"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700312 NATIVE_METHOD(DexFile, getClassNameList, "(I)[Ljava/lang/String;"),
313 NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700314 NATIVE_METHOD(DexFile, openDexFileNative, "(Ljava/lang/String;Ljava/lang/String;I)I"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700315};
316
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700317void register_dalvik_system_DexFile(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700318 REGISTER_NATIVE_METHODS("dalvik/system/DexFile");
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700319}
320
321} // namespace art