blob: bab0604d949485f0d49321f2e77912ef71182fdc [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>
Dave Allison39c3bfb2014-01-28 18:33:52 -080018#include <fcntl.h>
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -070019
Elliott Hughes07ed66b2012-12-12 18:34:25 -080020#include "base/logging.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070021#include "class_linker.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080022#include "common_throws.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070023#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070024#include "gc/space/image_space.h"
25#include "gc/space/space-inl.h"
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070026#include "image.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070027#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "mirror/class_loader.h"
Ian Rogers05f30572013-02-20 12:13:11 -080029#include "mirror/object-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030#include "mirror/string.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080031#include "oat.h"
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -070032#include "os.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070033#include "runtime.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070034#include "scoped_thread_state_change.h"
Ian Rogersc9818482012-01-11 08:52:51 -080035#include "ScopedLocalRef.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070036#include "ScopedUtfChars.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070037#include "toStringArray.h"
38#include "zip_archive.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070039
Dave Allison39c3bfb2014-01-28 18:33:52 -080040#ifdef HAVE_ANDROID_OS
41#include "cutils/properties.h"
42#endif
43
Brian Carlstromf91c8c32011-09-21 17:30:34 -070044namespace art {
45
Brian Carlstromf91c8c32011-09-21 17:30:34 -070046// A smart pointer that provides read-only access to a Java string's UTF chars.
47// Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if
48// passed a null jstring. The correct idiom is:
49//
50// NullableScopedUtfChars name(env, javaName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -070051// if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070052// return NULL;
53// }
54// // ... use name.c_str()
55//
56// TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option.
57class NullableScopedUtfChars {
Elliott Hughesba8eee12012-01-24 20:25:24 -080058 public:
59 NullableScopedUtfChars(JNIEnv* env, jstring s) : mEnv(env), mString(s) {
60 mUtfChars = (s != NULL) ? env->GetStringUTFChars(s, NULL) : NULL;
61 }
62
63 ~NullableScopedUtfChars() {
64 if (mUtfChars) {
65 mEnv->ReleaseStringUTFChars(mString, mUtfChars);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070066 }
Elliott Hughesba8eee12012-01-24 20:25:24 -080067 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070068
Elliott Hughesba8eee12012-01-24 20:25:24 -080069 const char* c_str() const {
70 return mUtfChars;
71 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070072
Elliott Hughesba8eee12012-01-24 20:25:24 -080073 size_t size() const {
74 return strlen(mUtfChars);
75 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070076
Elliott Hughesba8eee12012-01-24 20:25:24 -080077 // Element access.
78 const char& operator[](size_t n) const {
79 return mUtfChars[n];
80 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070081
Elliott Hughesba8eee12012-01-24 20:25:24 -080082 private:
83 JNIEnv* mEnv;
84 jstring mString;
85 const char* mUtfChars;
Brian Carlstromf91c8c32011-09-21 17:30:34 -070086
Elliott Hughesba8eee12012-01-24 20:25:24 -080087 // Disallow copy and assignment.
88 NullableScopedUtfChars(const NullableScopedUtfChars&);
89 void operator=(const NullableScopedUtfChars&);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070090};
91
Elliott Hughes2d983902014-02-04 16:17:13 -080092static jlong DexFile_openDexFileNative(JNIEnv* env, jclass, jstring javaSourceName, jstring javaOutputName, jint) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070093 ScopedUtfChars sourceName(env, javaSourceName);
94 if (sourceName.c_str() == NULL) {
95 return 0;
96 }
97 NullableScopedUtfChars outputName(env, javaOutputName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -070098 if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070099 return 0;
100 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700101
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700102 uint32_t dex_location_checksum;
Brian Carlstrom08cbf662013-12-10 16:52:57 -0800103 uint32_t* dex_location_checksum_pointer = &dex_location_checksum;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700104 std::string error_msg;
Brian Carlstrom08cbf662013-12-10 16:52:57 -0800105 if (!DexFile::GetChecksum(sourceName.c_str(), dex_location_checksum_pointer, &error_msg)) {
106 dex_location_checksum_pointer = NULL;
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700107 }
108
109 ClassLinker* linker = Runtime::Current()->GetClassLinker();
110 const DexFile* dex_file;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700111 if (outputName.c_str() == nullptr) {
Brian Carlstrom08cbf662013-12-10 16:52:57 -0800112 // FindOrCreateOatFileForDexLocation can tolerate a missing dex_location_checksum
113 error_msg.clear();
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700114 dex_file = linker->FindDexFileInOatFileFromDexLocation(sourceName.c_str(),
Brian Carlstrom08cbf662013-12-10 16:52:57 -0800115 dex_location_checksum_pointer, &error_msg);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700116 } else {
Brian Carlstrom08cbf662013-12-10 16:52:57 -0800117 // FindOrCreateOatFileForDexLocation requires the dex_location_checksum
118 if (dex_location_checksum_pointer == NULL) {
119 ScopedObjectAccess soa(env);
120 DCHECK(!error_msg.empty());
121 ThrowIOException("%s", error_msg.c_str());
122 return 0;
123 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700124 dex_file = linker->FindOrCreateOatFileForDexLocation(sourceName.c_str(), dex_location_checksum,
125 outputName.c_str(), &error_msg);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700126 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700127 if (dex_file == nullptr) {
Vladimir Marko60836d52014-01-16 15:53:38 +0000128 ScopedObjectAccess soa(env);
129 CHECK(!error_msg.empty());
130 ThrowIOException("%s", error_msg.c_str());
jeffhaoc393a4f2011-10-19 13:46:09 -0700131 return 0;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700132 }
Elliott Hughes2d983902014-02-04 16:17:13 -0800133 return static_cast<jlong>(reinterpret_cast<uintptr_t>(dex_file));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700134}
135
Elliott Hughes2d983902014-02-04 16:17:13 -0800136static const DexFile* toDexFile(jlong dex_file_address, JNIEnv* env) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700137 const DexFile* dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(dex_file_address));
Ian Rogers1eb512d2013-10-18 15:42:20 -0700138 if (UNLIKELY(dex_file == nullptr)) {
139 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800140 ThrowNullPointerException(NULL, "dex_file == null");
Brian Carlstromaded5f72011-10-07 17:15:04 -0700141 }
142 return dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700143}
144
Elliott Hughes2d983902014-02-04 16:17:13 -0800145static void DexFile_closeDexFile(JNIEnv* env, jclass, jlong cookie) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700146 const DexFile* dex_file;
Ian Rogers1eb512d2013-10-18 15:42:20 -0700147 dex_file = toDexFile(cookie, env);
148 if (dex_file == nullptr) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700149 return;
150 }
151 if (Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) {
152 return;
153 }
154 delete dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700155}
156
Elliott Hughes0512f022012-03-15 22:10:52 -0700157static jclass DexFile_defineClassNative(JNIEnv* env, jclass, jstring javaName, jobject javaLoader,
Elliott Hughes2d983902014-02-04 16:17:13 -0800158 jlong cookie) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700159 const DexFile* dex_file = toDexFile(cookie, env);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700160 if (dex_file == NULL) {
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700161 VLOG(class_linker) << "Failed to find dex_file";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700162 return NULL;
163 }
Brian Carlstromdf143242011-10-10 18:05:34 -0700164 ScopedUtfChars class_name(env, javaName);
165 if (class_name.c_str() == NULL) {
Brian Carlstrom2e450bf2013-09-06 15:39:46 -0700166 VLOG(class_linker) << "Failed to find class_name";
Brian Carlstromdf143242011-10-10 18:05:34 -0700167 return NULL;
168 }
Elliott Hughes95572412011-12-13 18:14:20 -0800169 const std::string descriptor(DotToDescriptor(class_name.c_str()));
Ian Rogersee39a102013-09-19 02:56:49 -0700170 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor.c_str());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700171 if (dex_class_def == NULL) {
Brian Carlstrom2e450bf2013-09-06 15:39:46 -0700172 VLOG(class_linker) << "Failed to find dex_class_def";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700173 return NULL;
174 }
Ian Rogers1eb512d2013-10-18 15:42:20 -0700175 ScopedObjectAccess soa(env);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700176 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
177 class_linker->RegisterDexFile(*dex_file);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700178 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), soa.Decode<mirror::ClassLoader*>(javaLoader));
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700179 mirror::Class* result = class_linker->DefineClass(descriptor.c_str(), class_loader, *dex_file,
180 *dex_class_def);
Brian Carlstrom2e450bf2013-09-06 15:39:46 -0700181 VLOG(class_linker) << "DexFile_defineClassNative returning " << result;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700182 return soa.AddLocalReference<jclass>(result);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700183}
184
Elliott Hughes2d983902014-02-04 16:17:13 -0800185static jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jlong cookie) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700186 const DexFile* dex_file;
Ian Rogers1eb512d2013-10-18 15:42:20 -0700187 dex_file = toDexFile(cookie, env);
188 if (dex_file == nullptr) {
189 return nullptr;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700190 }
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700191
192 std::vector<std::string> class_names;
193 for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
194 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
195 const char* descriptor = dex_file->GetClassDescriptor(class_def);
196 class_names.push_back(DescriptorToDot(descriptor));
197 }
198 return toStringArray(env, class_names);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700199}
200
Dave Allison39c3bfb2014-01-28 18:33:52 -0800201// Copy a profile file
202static void CopyProfileFile(const char* oldfile, const char* newfile) {
203 int fd = open(oldfile, O_RDONLY);
204 if (fd < 0) {
205 // If we can't open the file show the uid:gid of the this process to allow
206 // diagnosis of the problem.
207 LOG(ERROR) << "Failed to open profile file " << oldfile<< ". My uid:gid is "
208 << getuid() << ":" << getgid();
209 return;
210 }
211
212 // Create the copy with rw------- (only accessible by system)
213 int fd2 = open(newfile, O_WRONLY|O_CREAT|O_TRUNC, 0600);
214 if (fd2 < 0) {
215 // If we can't open the file show the uid:gid of the this process to allow
216 // diagnosis of the problem.
217 LOG(ERROR) << "Failed to create/write prev profile file " << newfile << ". My uid:gid is "
218 << getuid() << ":" << getgid();
219 return;
220 }
221 char buf[4096];
222 while (true) {
223 int n = read(fd, buf, sizeof(buf));
224 if (n <= 0) {
225 break;
226 }
227 write(fd2, buf, n);
228 }
229 close(fd);
230 close(fd2);
231}
232
233static jboolean DexFile_isDexOptNeededInternal(JNIEnv* env, jclass, jstring javaFilename,
234 jstring javaPkgname, jboolean defer) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700235 const bool kVerboseLogging = false; // Spammy logging.
236 const bool kDebugLogging = true; // Logging useful for debugging.
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800237
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700238 ScopedUtfChars filename(env, javaFilename);
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700239
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700240 if ((filename.c_str() == nullptr) || !OS::FileExists(filename.c_str())) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800241 LOG(ERROR) << "DexFile_isDexOptNeeded file '" << filename.c_str() << "' does not exist";
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700242 ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException"));
243 const char* message = (filename.c_str() == nullptr) ? "<empty file name>" : filename.c_str();
244 env->ThrowNew(fnfe.get(), message);
245 return JNI_FALSE;
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700246 }
247
248 // Always treat elements of the bootclasspath as up-to-date. The
249 // fact that code is running at all means that this should be true.
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700250 Runtime* runtime = Runtime::Current();
251 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700252 const std::vector<const DexFile*>& boot_class_path = class_linker->GetBootClassPath();
253 for (size_t i = 0; i < boot_class_path.size(); i++) {
254 if (boot_class_path[i]->GetLocation() == filename.c_str()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700255 if (kVerboseLogging) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800256 LOG(INFO) << "DexFile_isDexOptNeeded ignoring boot class path file: " << filename.c_str();
257 }
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700258 return JNI_FALSE;
259 }
260 }
261
Dave Allison39c3bfb2014-01-28 18:33:52 -0800262 // Check the profile file. We need to rerun dex2oat if the profile has changed significantly
263 // since the last time, or it's new.
264 // If the 'defer' argument is true then this will be retried later. In this case we
265 // need to make sure that the profile file copy is not made so that we will get the
266 // same result second time.
267 if (javaPkgname != NULL) {
268 ScopedUtfChars pkgname(env, javaPkgname);
269 std::string profile_file = GetDalvikCacheOrDie(GetAndroidData()) + std::string("/profiles/") +
270 pkgname.c_str();
271
272 std::string profile_cache_dir = GetDalvikCacheOrDie(GetAndroidData()) + "/profile-cache";
273
274 // Make the profile cache if it doesn't exist.
275 mkdir(profile_cache_dir.c_str(), 0700);
276
277 // The previous profile file (a copy of the profile the last time this was run) is
278 // in the dalvik-cache directory because this is owned by system. The profiles
279 // directory is owned by install so system cannot write files in there.
280 std::string prev_profile_file = profile_cache_dir + std::string("/") + pkgname.c_str();
281
282 struct stat profstat, prevstat;
283 int e1 = stat(profile_file.c_str(), &profstat);
284 int e2 = stat(prev_profile_file.c_str(), &prevstat);
285
286 if (e1 < 0) {
287 // No profile file, need to run dex2oat
288 if (kDebugLogging) {
289 LOG(INFO) << "DexFile_isDexOptNeeded profile file " << profile_file << " doesn't exist";
290 }
291 return JNI_TRUE;
292 }
293 if (e2 == 0) {
294 // There is a previous profile file. Check if the profile has changed significantly.
295 // Let's use the file size as a proxy for significance. If the new profile is 10%
296 // different in size than the the old profile then we run dex2oat.
297 double newsize = profstat.st_size;
298 double oldsize = prevstat.st_size;
299 bool need_profile = false;
300
301 double ratio = 0; // If the old file was empty and the new one not
302 if (oldsize > 0 && newsize > 0) {
303 ratio = newsize / oldsize;
304 } else if (oldsize == 0 && newsize > 0) {
305 need_profile = true;
306 } else if (oldsize > 0 && newsize == 0) {
307 // Unlikely to happen, but cover all the bases.
308 need_profile = true;
309 }
310
311 double significant_difference = 10.0;
312#ifdef HAVE_ANDROID_OS
313 // Switch off profiler if the dalvik.vm.profiler property has value 0.
314 char buf[PROP_VALUE_MAX];
315 property_get("dalvik.vm.profiler.dex2oat.threshold", buf, "10.0");
316 significant_difference = strtod(buf, nullptr);
317
318 // Something reasonable?
319 if (significant_difference < 1.0 || significant_difference > 90.0) {
320 significant_difference = 10.0;
321 }
322#endif // The percentage difference that we consider as being significant.
323 double diff_hwm = 1.0 + significant_difference/10.0;
324 double diff_lwm = 1.0 - significant_difference/10.0;
325
326 if (ratio > diff_hwm || ratio < diff_lwm) {
327 need_profile = true;
328 }
329
330 if (need_profile) {
331 if (kDebugLogging) {
332 LOG(INFO) << "DexFile_isDexOptNeeded size of new profile file " << profile_file <<
333 " is significantly different from old profile file " << prev_profile_file << " (new: " <<
334 newsize << ", old: " << oldsize << ", ratio: " << ratio << ")";
335 }
336 if (!defer) {
337 CopyProfileFile(profile_file.c_str(), prev_profile_file.c_str());
338 }
339 return JNI_TRUE;
340 }
341 } else {
342 // Previous profile does not exist. Make a copy of the current one.
343 if (kDebugLogging) {
344 LOG(INFO) << "DexFile_isDexOptNeeded previous profile doesn't exist: " << prev_profile_file;
345 }
346 if (!defer) {
347 CopyProfileFile(profile_file.c_str(), prev_profile_file.c_str());
348 }
349 return JNI_TRUE;
350 }
351 }
352
Brian Carlstrom30e2ea42013-06-19 23:25:37 -0700353 // Check if we have an odex file next to the dex file.
354 std::string odex_filename(OatFile::DexFilenameToOdexFilename(filename.c_str()));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700355 std::string error_msg;
356 UniquePtr<const OatFile> oat_file(OatFile::Open(odex_filename, odex_filename, NULL, false,
357 &error_msg));
358 if (oat_file.get() == nullptr) {
359 if (kVerboseLogging) {
360 LOG(INFO) << "DexFile_isDexOptNeeded failed to open oat file '" << filename.c_str()
361 << "': " << error_msg;
362 }
363 error_msg.clear();
364 } else {
365 const art::OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(filename.c_str(), NULL,
366 kDebugLogging);
367 if (oat_dex_file != nullptr) {
Ian Rogers33e95662013-05-20 20:29:14 -0700368 uint32_t location_checksum;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700369 // If its not possible to read the classes.dex assume up-to-date as we won't be able to
370 // compile it anyway.
371 if (!DexFile::GetChecksum(filename.c_str(), &location_checksum, &error_msg)) {
372 if (kVerboseLogging) {
Ian Rogers33e95662013-05-20 20:29:14 -0700373 LOG(INFO) << "DexFile_isDexOptNeeded ignoring precompiled stripped file: "
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700374 << filename.c_str() << ": " << error_msg;
Ian Rogers33e95662013-05-20 20:29:14 -0700375 }
376 return JNI_FALSE;
377 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700378 if (ClassLinker::VerifyOatFileChecksums(oat_file.get(), filename.c_str(), location_checksum,
379 &error_msg)) {
380 if (kVerboseLogging) {
Brian Carlstrom30e2ea42013-06-19 23:25:37 -0700381 LOG(INFO) << "DexFile_isDexOptNeeded precompiled file " << odex_filename
Brian Carlstrom0d3bbff2013-10-28 15:21:32 -0700382 << " has an up-to-date checksum compared to " << filename.c_str();
Ian Rogers33e95662013-05-20 20:29:14 -0700383 }
384 return JNI_FALSE;
Brian Carlstrom0d3bbff2013-10-28 15:21:32 -0700385 } else {
386 if (kVerboseLogging) {
387 LOG(INFO) << "DexFile_isDexOptNeeded found precompiled file " << odex_filename
388 << " with an out-of-date checksum compared to " << filename.c_str()
389 << ": " << error_msg;
390 }
391 error_msg.clear();
Ian Rogers33e95662013-05-20 20:29:14 -0700392 }
Brian Carlstromafe25512012-06-27 17:02:58 -0700393 }
Brian Carlstromfad71432011-10-16 20:25:10 -0700394 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800395
Brian Carlstroma004aa92012-02-08 18:05:09 -0800396 // Check if we have an oat file in the cache
Brian Carlstrom30e2ea42013-06-19 23:25:37 -0700397 std::string cache_location(GetDalvikCacheFilenameOrDie(filename.c_str()));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700398 oat_file.reset(OatFile::Open(cache_location, filename.c_str(), NULL, false, &error_msg));
399 if (oat_file.get() == nullptr) {
400 if (kDebugLogging) {
401 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
402 << " does not exist for " << filename.c_str() << ": " << error_msg;
403 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800404 return JNI_TRUE;
405 }
406
Mathieu Chartier02e25112013-08-14 16:14:24 -0700407 for (const auto& space : runtime->GetHeap()->GetContinuousSpaces()) {
408 if (space->IsImageSpace()) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700409 // TODO: Ensure this works with multiple image spaces.
Mathieu Chartier02e25112013-08-14 16:14:24 -0700410 const ImageHeader& image_header = space->AsImageSpace()->GetImageHeader();
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700411 if (oat_file->GetOatHeader().GetImageFileLocationOatChecksum() !=
412 image_header.GetOatChecksum()) {
413 if (kDebugLogging) {
414 ScopedObjectAccess soa(env);
415 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
416 << " has out-of-date oat checksum compared to "
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000417 << oat_file->GetLocation();
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700418 }
Brian Carlstrom28db0122012-10-18 16:20:41 -0700419 return JNI_TRUE;
420 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800421 if (oat_file->GetOatHeader().GetImageFileLocationOatDataBegin()
Ian Rogersef7d42f2014-01-06 12:55:46 -0800422 != reinterpret_cast<uintptr_t>(image_header.GetOatDataBegin())) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700423 if (kDebugLogging) {
424 ScopedObjectAccess soa(env);
425 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
426 << " has out-of-date oat begin compared to "
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000427 << oat_file->GetLocation();
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700428 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700429 return JNI_TRUE;
430 }
431 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700432 }
433
Brian Carlstroma004aa92012-02-08 18:05:09 -0800434 uint32_t location_checksum;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700435 if (!DexFile::GetChecksum(filename.c_str(), &location_checksum, &error_msg)) {
436 if (kDebugLogging) {
437 LOG(ERROR) << "DexFile_isDexOptNeeded failed to compute checksum of " << filename.c_str()
438 << " (error " << error_msg << ")";
439 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800440 return JNI_TRUE;
441 }
442
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700443 if (!ClassLinker::VerifyOatFileChecksums(oat_file.get(), filename.c_str(), location_checksum,
444 &error_msg)) {
445 if (kDebugLogging) {
446 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
447 << " has out-of-date checksum compared to " << filename.c_str()
448 << " (error " << error_msg << ")";
449 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800450 return JNI_TRUE;
451 }
452
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700453 if (kVerboseLogging) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800454 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
455 << " is up-to-date for " << filename.c_str();
456 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700457 CHECK(error_msg.empty()) << error_msg;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700458 return JNI_FALSE;
459}
460
Dave Allison39c3bfb2014-01-28 18:33:52 -0800461// public API, NULL pkgname
462static jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass c, jstring javaFilename) {
463 return DexFile_isDexOptNeededInternal(env, c, javaFilename, NULL, false);
464}
465
466
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700467static JNINativeMethod gMethods[] = {
Elliott Hughes2d983902014-02-04 16:17:13 -0800468 NATIVE_METHOD(DexFile, closeDexFile, "(J)V"),
469 NATIVE_METHOD(DexFile, defineClassNative, "(Ljava/lang/String;Ljava/lang/ClassLoader;J)Ljava/lang/Class;"),
470 NATIVE_METHOD(DexFile, getClassNameList, "(J)[Ljava/lang/String;"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700471 NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
Dave Allison39c3bfb2014-01-28 18:33:52 -0800472 NATIVE_METHOD(DexFile, isDexOptNeededInternal, "(Ljava/lang/String;Ljava/lang/String;Z)Z"),
Elliott Hughes2d983902014-02-04 16:17:13 -0800473 NATIVE_METHOD(DexFile, openDexFileNative, "(Ljava/lang/String;Ljava/lang/String;I)J"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700474};
475
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700476void register_dalvik_system_DexFile(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700477 REGISTER_NATIVE_METHODS("dalvik/system/DexFile");
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700478}
479
480} // namespace art