blob: 15a57793ee669ddd2d90d2260af62bb8414a8098 [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
Calin Juravle9dae5b42014-04-07 16:36:21 +030017#include <algorithm>
Dave Allison39c3bfb2014-01-28 18:33:52 -080018#include <fcntl.h>
Calin Juravle9dae5b42014-04-07 16:36:21 +030019#include <set>
20#include <unistd.h>
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -070021
Elliott Hughes07ed66b2012-12-12 18:34:25 -080022#include "base/logging.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070023#include "class_linker.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080024#include "common_throws.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070026#include "gc/space/image_space.h"
27#include "gc/space/space-inl.h"
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070028#include "image.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070029#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030#include "mirror/class_loader.h"
Ian Rogers05f30572013-02-20 12:13:11 -080031#include "mirror/object-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032#include "mirror/string.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080033#include "oat.h"
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -070034#include "os.h"
Calin Juravle9dae5b42014-04-07 16:36:21 +030035#include "profiler.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070036#include "runtime.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070037#include "scoped_thread_state_change.h"
Ian Rogersc9818482012-01-11 08:52:51 -080038#include "ScopedLocalRef.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070039#include "ScopedUtfChars.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070040#include "toStringArray.h"
41#include "zip_archive.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070042
Dave Allison39c3bfb2014-01-28 18:33:52 -080043#ifdef HAVE_ANDROID_OS
44#include "cutils/properties.h"
45#endif
46
Brian Carlstromf91c8c32011-09-21 17:30:34 -070047namespace art {
48
Brian Carlstromf91c8c32011-09-21 17:30:34 -070049// A smart pointer that provides read-only access to a Java string's UTF chars.
50// Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if
51// passed a null jstring. The correct idiom is:
52//
53// NullableScopedUtfChars name(env, javaName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -070054// if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070055// return NULL;
56// }
57// // ... use name.c_str()
58//
59// TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option.
60class NullableScopedUtfChars {
Elliott Hughesba8eee12012-01-24 20:25:24 -080061 public:
62 NullableScopedUtfChars(JNIEnv* env, jstring s) : mEnv(env), mString(s) {
63 mUtfChars = (s != NULL) ? env->GetStringUTFChars(s, NULL) : NULL;
64 }
65
66 ~NullableScopedUtfChars() {
67 if (mUtfChars) {
68 mEnv->ReleaseStringUTFChars(mString, mUtfChars);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070069 }
Elliott Hughesba8eee12012-01-24 20:25:24 -080070 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070071
Elliott Hughesba8eee12012-01-24 20:25:24 -080072 const char* c_str() const {
73 return mUtfChars;
74 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070075
Elliott Hughesba8eee12012-01-24 20:25:24 -080076 size_t size() const {
77 return strlen(mUtfChars);
78 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070079
Elliott Hughesba8eee12012-01-24 20:25:24 -080080 // Element access.
81 const char& operator[](size_t n) const {
82 return mUtfChars[n];
83 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070084
Elliott Hughesba8eee12012-01-24 20:25:24 -080085 private:
86 JNIEnv* mEnv;
87 jstring mString;
88 const char* mUtfChars;
Brian Carlstromf91c8c32011-09-21 17:30:34 -070089
Elliott Hughesba8eee12012-01-24 20:25:24 -080090 // Disallow copy and assignment.
91 NullableScopedUtfChars(const NullableScopedUtfChars&);
92 void operator=(const NullableScopedUtfChars&);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070093};
94
Elliott Hughes2d983902014-02-04 16:17:13 -080095static jlong DexFile_openDexFileNative(JNIEnv* env, jclass, jstring javaSourceName, jstring javaOutputName, jint) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070096 ScopedUtfChars sourceName(env, javaSourceName);
97 if (sourceName.c_str() == NULL) {
98 return 0;
99 }
100 NullableScopedUtfChars outputName(env, javaOutputName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -0700101 if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700102 return 0;
103 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700104
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700105 uint32_t dex_location_checksum;
Brian Carlstrom08cbf662013-12-10 16:52:57 -0800106 uint32_t* dex_location_checksum_pointer = &dex_location_checksum;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700107 std::string error_msg;
Brian Carlstrom08cbf662013-12-10 16:52:57 -0800108 if (!DexFile::GetChecksum(sourceName.c_str(), dex_location_checksum_pointer, &error_msg)) {
109 dex_location_checksum_pointer = NULL;
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700110 }
111
112 ClassLinker* linker = Runtime::Current()->GetClassLinker();
113 const DexFile* dex_file;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700114 if (outputName.c_str() == nullptr) {
Brian Carlstrom08cbf662013-12-10 16:52:57 -0800115 // FindOrCreateOatFileForDexLocation can tolerate a missing dex_location_checksum
116 error_msg.clear();
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700117 dex_file = linker->FindDexFileInOatFileFromDexLocation(sourceName.c_str(),
Brian Carlstrom08cbf662013-12-10 16:52:57 -0800118 dex_location_checksum_pointer, &error_msg);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700119 } else {
Brian Carlstrom08cbf662013-12-10 16:52:57 -0800120 // FindOrCreateOatFileForDexLocation requires the dex_location_checksum
121 if (dex_location_checksum_pointer == NULL) {
122 ScopedObjectAccess soa(env);
123 DCHECK(!error_msg.empty());
124 ThrowIOException("%s", error_msg.c_str());
125 return 0;
126 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700127 dex_file = linker->FindOrCreateOatFileForDexLocation(sourceName.c_str(), dex_location_checksum,
128 outputName.c_str(), &error_msg);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700129 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700130 if (dex_file == nullptr) {
Vladimir Marko60836d52014-01-16 15:53:38 +0000131 ScopedObjectAccess soa(env);
132 CHECK(!error_msg.empty());
133 ThrowIOException("%s", error_msg.c_str());
jeffhaoc393a4f2011-10-19 13:46:09 -0700134 return 0;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700135 }
Elliott Hughes2d983902014-02-04 16:17:13 -0800136 return static_cast<jlong>(reinterpret_cast<uintptr_t>(dex_file));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700137}
138
Elliott Hughes2d983902014-02-04 16:17:13 -0800139static const DexFile* toDexFile(jlong dex_file_address, JNIEnv* env) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700140 const DexFile* dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(dex_file_address));
Ian Rogers1eb512d2013-10-18 15:42:20 -0700141 if (UNLIKELY(dex_file == nullptr)) {
142 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800143 ThrowNullPointerException(NULL, "dex_file == null");
Brian Carlstromaded5f72011-10-07 17:15:04 -0700144 }
145 return dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700146}
147
Elliott Hughes2d983902014-02-04 16:17:13 -0800148static void DexFile_closeDexFile(JNIEnv* env, jclass, jlong cookie) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700149 const DexFile* dex_file;
Ian Rogers1eb512d2013-10-18 15:42:20 -0700150 dex_file = toDexFile(cookie, env);
151 if (dex_file == nullptr) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700152 return;
153 }
154 if (Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) {
155 return;
156 }
157 delete dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700158}
159
Elliott Hughes0512f022012-03-15 22:10:52 -0700160static jclass DexFile_defineClassNative(JNIEnv* env, jclass, jstring javaName, jobject javaLoader,
Elliott Hughes2d983902014-02-04 16:17:13 -0800161 jlong cookie) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700162 const DexFile* dex_file = toDexFile(cookie, env);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700163 if (dex_file == NULL) {
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700164 VLOG(class_linker) << "Failed to find dex_file";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700165 return NULL;
166 }
Brian Carlstromdf143242011-10-10 18:05:34 -0700167 ScopedUtfChars class_name(env, javaName);
168 if (class_name.c_str() == NULL) {
Brian Carlstrom2e450bf2013-09-06 15:39:46 -0700169 VLOG(class_linker) << "Failed to find class_name";
Brian Carlstromdf143242011-10-10 18:05:34 -0700170 return NULL;
171 }
Elliott Hughes95572412011-12-13 18:14:20 -0800172 const std::string descriptor(DotToDescriptor(class_name.c_str()));
Ian Rogersee39a102013-09-19 02:56:49 -0700173 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor.c_str());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700174 if (dex_class_def == NULL) {
Brian Carlstrom2e450bf2013-09-06 15:39:46 -0700175 VLOG(class_linker) << "Failed to find dex_class_def";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700176 return NULL;
177 }
Ian Rogers1eb512d2013-10-18 15:42:20 -0700178 ScopedObjectAccess soa(env);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700179 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
180 class_linker->RegisterDexFile(*dex_file);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700181 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), soa.Decode<mirror::ClassLoader*>(javaLoader));
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700182 mirror::Class* result = class_linker->DefineClass(descriptor.c_str(), class_loader, *dex_file,
183 *dex_class_def);
Brian Carlstrom2e450bf2013-09-06 15:39:46 -0700184 VLOG(class_linker) << "DexFile_defineClassNative returning " << result;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700185 return soa.AddLocalReference<jclass>(result);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700186}
187
Elliott Hughes2d983902014-02-04 16:17:13 -0800188static jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jlong cookie) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700189 const DexFile* dex_file;
Ian Rogers1eb512d2013-10-18 15:42:20 -0700190 dex_file = toDexFile(cookie, env);
191 if (dex_file == nullptr) {
192 return nullptr;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700193 }
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700194
195 std::vector<std::string> class_names;
196 for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
197 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
198 const char* descriptor = dex_file->GetClassDescriptor(class_def);
199 class_names.push_back(DescriptorToDot(descriptor));
200 }
201 return toStringArray(env, class_names);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700202}
203
Dave Allison39c3bfb2014-01-28 18:33:52 -0800204// Copy a profile file
205static void CopyProfileFile(const char* oldfile, const char* newfile) {
206 int fd = open(oldfile, O_RDONLY);
207 if (fd < 0) {
208 // If we can't open the file show the uid:gid of the this process to allow
209 // diagnosis of the problem.
210 LOG(ERROR) << "Failed to open profile file " << oldfile<< ". My uid:gid is "
211 << getuid() << ":" << getgid();
212 return;
213 }
214
215 // Create the copy with rw------- (only accessible by system)
216 int fd2 = open(newfile, O_WRONLY|O_CREAT|O_TRUNC, 0600);
217 if (fd2 < 0) {
218 // If we can't open the file show the uid:gid of the this process to allow
219 // diagnosis of the problem.
220 LOG(ERROR) << "Failed to create/write prev profile file " << newfile << ". My uid:gid is "
221 << getuid() << ":" << getgid();
222 return;
223 }
224 char buf[4096];
225 while (true) {
226 int n = read(fd, buf, sizeof(buf));
227 if (n <= 0) {
228 break;
229 }
230 write(fd2, buf, n);
231 }
232 close(fd);
233 close(fd2);
234}
235
Calin Juravle9dae5b42014-04-07 16:36:21 +0300236static double GetDoubleProperty(const char* property, double minValue, double maxValue, double defaultValue) {
237#ifndef HAVE_ANDROID_OS
238 return defaultValue;
239#else
240 char buf[PROP_VALUE_MAX];
241 char* endptr;
242
243 property_get(property, buf, "");
244 double value = strtod(buf, &endptr);
245
246 if (value == 0 && endptr == buf) {
247 value = defaultValue;
248 } else if (value < minValue || value > maxValue) {
249 value = defaultValue;
250 }
251 return value;
252#endif
253}
254
Dave Allison39c3bfb2014-01-28 18:33:52 -0800255static jboolean DexFile_isDexOptNeededInternal(JNIEnv* env, jclass, jstring javaFilename,
256 jstring javaPkgname, jboolean defer) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700257 const bool kVerboseLogging = false; // Spammy logging.
258 const bool kDebugLogging = true; // Logging useful for debugging.
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800259
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700260 ScopedUtfChars filename(env, javaFilename);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700261 if ((filename.c_str() == nullptr) || !OS::FileExists(filename.c_str())) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800262 LOG(ERROR) << "DexFile_isDexOptNeeded file '" << filename.c_str() << "' does not exist";
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700263 ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException"));
264 const char* message = (filename.c_str() == nullptr) ? "<empty file name>" : filename.c_str();
265 env->ThrowNew(fnfe.get(), message);
266 return JNI_FALSE;
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700267 }
268
269 // Always treat elements of the bootclasspath as up-to-date. The
270 // fact that code is running at all means that this should be true.
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700271 Runtime* runtime = Runtime::Current();
272 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700273 const std::vector<const DexFile*>& boot_class_path = class_linker->GetBootClassPath();
274 for (size_t i = 0; i < boot_class_path.size(); i++) {
275 if (boot_class_path[i]->GetLocation() == filename.c_str()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700276 if (kVerboseLogging) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800277 LOG(INFO) << "DexFile_isDexOptNeeded ignoring boot class path file: " << filename.c_str();
278 }
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700279 return JNI_FALSE;
280 }
281 }
282
Dave Allison39c3bfb2014-01-28 18:33:52 -0800283 // Check the profile file. We need to rerun dex2oat if the profile has changed significantly
284 // since the last time, or it's new.
285 // If the 'defer' argument is true then this will be retried later. In this case we
286 // need to make sure that the profile file copy is not made so that we will get the
287 // same result second time.
288 if (javaPkgname != NULL) {
289 ScopedUtfChars pkgname(env, javaPkgname);
290 std::string profile_file = GetDalvikCacheOrDie(GetAndroidData()) + std::string("/profiles/") +
291 pkgname.c_str();
292
293 std::string profile_cache_dir = GetDalvikCacheOrDie(GetAndroidData()) + "/profile-cache";
294
295 // Make the profile cache if it doesn't exist.
296 mkdir(profile_cache_dir.c_str(), 0700);
297
298 // The previous profile file (a copy of the profile the last time this was run) is
299 // in the dalvik-cache directory because this is owned by system. The profiles
300 // directory is owned by install so system cannot write files in there.
301 std::string prev_profile_file = profile_cache_dir + std::string("/") + pkgname.c_str();
302
303 struct stat profstat, prevstat;
304 int e1 = stat(profile_file.c_str(), &profstat);
305 int e2 = stat(prev_profile_file.c_str(), &prevstat);
Dave Allison39c3bfb2014-01-28 18:33:52 -0800306 if (e1 < 0) {
307 // No profile file, need to run dex2oat
308 if (kDebugLogging) {
309 LOG(INFO) << "DexFile_isDexOptNeeded profile file " << profile_file << " doesn't exist";
310 }
311 return JNI_TRUE;
312 }
Calin Juravle9dae5b42014-04-07 16:36:21 +0300313
Dave Allison39c3bfb2014-01-28 18:33:52 -0800314 if (e2 == 0) {
315 // There is a previous profile file. Check if the profile has changed significantly.
Calin Juravle9dae5b42014-04-07 16:36:21 +0300316 // A change in profile is considered significant if X% (change_thr property) of the top K%
317 // (compile_thr property) samples has changed.
Dave Allison39c3bfb2014-01-28 18:33:52 -0800318
Calin Juravle9dae5b42014-04-07 16:36:21 +0300319 double topKThreshold = GetDoubleProperty("dalvik.vm.profiler.dex2oat.compile_thr", 10.0, 90.0, 90.0);
320 double changeThreshold = GetDoubleProperty("dalvik.vm.profiler.dex2oat.change_thr", 1.0, 90.0, 10.0);
321 double changePercent = 0.0;
322 std::set<std::string> newTopK, oldTopK;
323 bool newOk = ProfileHelper::LoadTopKSamples(newTopK, profile_file, topKThreshold);
324 bool oldOk = ProfileHelper::LoadTopKSamples(oldTopK, prev_profile_file, topKThreshold);
325 if (!newOk || !oldOk) {
326 if (kDebugLogging) {
327 LOG(INFO) << "DexFile_isDexOptNeeded Ignoring invalid profiles: "
328 << (newOk ? "" : profile_file) << " " << (oldOk ? "" : prev_profile_file);
329 }
330 } else if (newTopK.empty()) {
331 if (kDebugLogging && kVerboseLogging) {
332 LOG(INFO) << "DexFile_isDexOptNeeded empty profile: " << profile_file;
333 }
334 // If the new topK is empty we shouldn't optimize so we leave the changePercent at 0.0.
335 } else {
336 std::set<std::string> diff;
337 std::set_difference(newTopK.begin(), newTopK.end(), oldTopK.begin(), oldTopK.end(),
338 std::inserter(diff, diff.end()));
339 // TODO: consider using the usedPercentage instead of the plain diff count.
340 changePercent = 100.0 * static_cast<double>(diff.size()) / static_cast<double>(newTopK.size());
341 if (kDebugLogging && kVerboseLogging) {
342 std::set<std::string>::iterator end = diff.end();
343 for (std::set<std::string>::iterator it = diff.begin(); it != end; it++) {
344 LOG(INFO) << "DexFile_isDexOptNeeded new in topK: " << *it;
345 }
346 }
Dave Allison39c3bfb2014-01-28 18:33:52 -0800347 }
348
Calin Juravle9dae5b42014-04-07 16:36:21 +0300349 if (changePercent > changeThreshold) {
Dave Allison39c3bfb2014-01-28 18:33:52 -0800350 if (kDebugLogging) {
351 LOG(INFO) << "DexFile_isDexOptNeeded size of new profile file " << profile_file <<
Calin Juravle9dae5b42014-04-07 16:36:21 +0300352 " is significantly different from old profile file " << prev_profile_file << " (top "
353 << topKThreshold << "% samples changed in proportion of " << changePercent << "%)";
Dave Allison39c3bfb2014-01-28 18:33:52 -0800354 }
355 if (!defer) {
356 CopyProfileFile(profile_file.c_str(), prev_profile_file.c_str());
357 }
358 return JNI_TRUE;
359 }
360 } else {
361 // Previous profile does not exist. Make a copy of the current one.
362 if (kDebugLogging) {
363 LOG(INFO) << "DexFile_isDexOptNeeded previous profile doesn't exist: " << prev_profile_file;
364 }
365 if (!defer) {
366 CopyProfileFile(profile_file.c_str(), prev_profile_file.c_str());
367 }
368 return JNI_TRUE;
369 }
370 }
371
Brian Carlstrom30e2ea42013-06-19 23:25:37 -0700372 // Check if we have an odex file next to the dex file.
373 std::string odex_filename(OatFile::DexFilenameToOdexFilename(filename.c_str()));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700374 std::string error_msg;
375 UniquePtr<const OatFile> oat_file(OatFile::Open(odex_filename, odex_filename, NULL, false,
376 &error_msg));
377 if (oat_file.get() == nullptr) {
378 if (kVerboseLogging) {
379 LOG(INFO) << "DexFile_isDexOptNeeded failed to open oat file '" << filename.c_str()
380 << "': " << error_msg;
381 }
382 error_msg.clear();
383 } else {
384 const art::OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(filename.c_str(), NULL,
385 kDebugLogging);
386 if (oat_dex_file != nullptr) {
Ian Rogers33e95662013-05-20 20:29:14 -0700387 uint32_t location_checksum;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700388 // If its not possible to read the classes.dex assume up-to-date as we won't be able to
389 // compile it anyway.
390 if (!DexFile::GetChecksum(filename.c_str(), &location_checksum, &error_msg)) {
391 if (kVerboseLogging) {
Ian Rogers33e95662013-05-20 20:29:14 -0700392 LOG(INFO) << "DexFile_isDexOptNeeded ignoring precompiled stripped file: "
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700393 << filename.c_str() << ": " << error_msg;
Ian Rogers33e95662013-05-20 20:29:14 -0700394 }
395 return JNI_FALSE;
396 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700397 if (ClassLinker::VerifyOatFileChecksums(oat_file.get(), filename.c_str(), location_checksum,
398 &error_msg)) {
399 if (kVerboseLogging) {
Brian Carlstrom30e2ea42013-06-19 23:25:37 -0700400 LOG(INFO) << "DexFile_isDexOptNeeded precompiled file " << odex_filename
Brian Carlstrom0d3bbff2013-10-28 15:21:32 -0700401 << " has an up-to-date checksum compared to " << filename.c_str();
Ian Rogers33e95662013-05-20 20:29:14 -0700402 }
403 return JNI_FALSE;
Brian Carlstrom0d3bbff2013-10-28 15:21:32 -0700404 } else {
405 if (kVerboseLogging) {
406 LOG(INFO) << "DexFile_isDexOptNeeded found precompiled file " << odex_filename
407 << " with an out-of-date checksum compared to " << filename.c_str()
408 << ": " << error_msg;
409 }
410 error_msg.clear();
Ian Rogers33e95662013-05-20 20:29:14 -0700411 }
Brian Carlstromafe25512012-06-27 17:02:58 -0700412 }
Brian Carlstromfad71432011-10-16 20:25:10 -0700413 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800414
Brian Carlstroma004aa92012-02-08 18:05:09 -0800415 // Check if we have an oat file in the cache
Brian Carlstrom30e2ea42013-06-19 23:25:37 -0700416 std::string cache_location(GetDalvikCacheFilenameOrDie(filename.c_str()));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700417 oat_file.reset(OatFile::Open(cache_location, filename.c_str(), NULL, false, &error_msg));
418 if (oat_file.get() == nullptr) {
419 if (kDebugLogging) {
420 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
421 << " does not exist for " << filename.c_str() << ": " << error_msg;
422 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800423 return JNI_TRUE;
424 }
425
Mathieu Chartier02e25112013-08-14 16:14:24 -0700426 for (const auto& space : runtime->GetHeap()->GetContinuousSpaces()) {
427 if (space->IsImageSpace()) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700428 // TODO: Ensure this works with multiple image spaces.
Mathieu Chartier02e25112013-08-14 16:14:24 -0700429 const ImageHeader& image_header = space->AsImageSpace()->GetImageHeader();
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700430 if (oat_file->GetOatHeader().GetImageFileLocationOatChecksum() !=
431 image_header.GetOatChecksum()) {
432 if (kDebugLogging) {
433 ScopedObjectAccess soa(env);
434 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
435 << " has out-of-date oat checksum compared to "
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000436 << oat_file->GetLocation();
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700437 }
Brian Carlstrom28db0122012-10-18 16:20:41 -0700438 return JNI_TRUE;
439 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800440 if (oat_file->GetOatHeader().GetImageFileLocationOatDataBegin()
Ian Rogersef7d42f2014-01-06 12:55:46 -0800441 != reinterpret_cast<uintptr_t>(image_header.GetOatDataBegin())) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700442 if (kDebugLogging) {
443 ScopedObjectAccess soa(env);
444 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
445 << " has out-of-date oat begin compared to "
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000446 << oat_file->GetLocation();
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700447 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700448 return JNI_TRUE;
449 }
450 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700451 }
452
Brian Carlstroma004aa92012-02-08 18:05:09 -0800453 uint32_t location_checksum;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700454 if (!DexFile::GetChecksum(filename.c_str(), &location_checksum, &error_msg)) {
455 if (kDebugLogging) {
456 LOG(ERROR) << "DexFile_isDexOptNeeded failed to compute checksum of " << filename.c_str()
457 << " (error " << error_msg << ")";
458 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800459 return JNI_TRUE;
460 }
461
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700462 if (!ClassLinker::VerifyOatFileChecksums(oat_file.get(), filename.c_str(), location_checksum,
463 &error_msg)) {
464 if (kDebugLogging) {
465 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
466 << " has out-of-date checksum compared to " << filename.c_str()
467 << " (error " << error_msg << ")";
468 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800469 return JNI_TRUE;
470 }
471
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700472 if (kVerboseLogging) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800473 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
474 << " is up-to-date for " << filename.c_str();
475 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700476 CHECK(error_msg.empty()) << error_msg;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700477 return JNI_FALSE;
478}
479
Dave Allison39c3bfb2014-01-28 18:33:52 -0800480// public API, NULL pkgname
481static jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass c, jstring javaFilename) {
482 return DexFile_isDexOptNeededInternal(env, c, javaFilename, NULL, false);
483}
484
485
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700486static JNINativeMethod gMethods[] = {
Elliott Hughes2d983902014-02-04 16:17:13 -0800487 NATIVE_METHOD(DexFile, closeDexFile, "(J)V"),
488 NATIVE_METHOD(DexFile, defineClassNative, "(Ljava/lang/String;Ljava/lang/ClassLoader;J)Ljava/lang/Class;"),
489 NATIVE_METHOD(DexFile, getClassNameList, "(J)[Ljava/lang/String;"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700490 NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
Dave Allison39c3bfb2014-01-28 18:33:52 -0800491 NATIVE_METHOD(DexFile, isDexOptNeededInternal, "(Ljava/lang/String;Ljava/lang/String;Z)Z"),
Elliott Hughes2d983902014-02-04 16:17:13 -0800492 NATIVE_METHOD(DexFile, openDexFileNative, "(Ljava/lang/String;Ljava/lang/String;I)J"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700493};
494
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700495void register_dalvik_system_DexFile(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700496 REGISTER_NATIVE_METHODS("dalvik/system/DexFile");
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700497}
498
499} // namespace art