blob: 14d6cd950efd21e13aeea55e68ba785ff2f9fae8 [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>
Calin Juravle9dae5b42014-04-07 16:36:21 +030018#include <set>
Ian Rogersdd157d72014-05-15 14:47:50 -070019#include <fcntl.h>
Ian Rogersc5f17732014-06-05 20:48:42 -070020#ifdef __linux__
Calin Juravle52214102014-06-04 12:01:50 +010021#include <sys/sendfile.h>
Ian Rogersc5f17732014-06-05 20:48:42 -070022#else
23#include <sys/socket.h>
24#endif
Calin Juravle52214102014-06-04 12:01:50 +010025#include <sys/stat.h>
Calin Juravle9dae5b42014-04-07 16:36:21 +030026#include <unistd.h>
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -070027
Elliott Hughes07ed66b2012-12-12 18:34:25 -080028#include "base/logging.h"
Andreas Gampe833a4852014-05-21 18:46:59 -070029#include "base/stl_util.h"
Andreas Gampe20c89302014-08-19 17:28:06 -070030#include "base/stringprintf.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070031#include "class_linker.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080032#include "common_throws.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070033#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070034#include "gc/space/image_space.h"
35#include "gc/space/space-inl.h"
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070036#include "image.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070037#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#include "mirror/class_loader.h"
Ian Rogers05f30572013-02-20 12:13:11 -080039#include "mirror/object-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040#include "mirror/string.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080041#include "oat.h"
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -070042#include "os.h"
Calin Juravle9dae5b42014-04-07 16:36:21 +030043#include "profiler.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070044#include "runtime.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070045#include "scoped_thread_state_change.h"
Calin Juravle177b4292014-06-03 16:30:39 +010046#include "ScopedFd.h"
Ian Rogersc9818482012-01-11 08:52:51 -080047#include "ScopedLocalRef.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070048#include "ScopedUtfChars.h"
Calin Juravlebb0b53f2014-05-23 17:33:29 +010049#include "utils.h"
Ian Rogersdd157d72014-05-15 14:47:50 -070050#include "well_known_classes.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070051#include "zip_archive.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070052
53namespace art {
54
Brian Carlstromf91c8c32011-09-21 17:30:34 -070055// A smart pointer that provides read-only access to a Java string's UTF chars.
56// Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if
57// passed a null jstring. The correct idiom is:
58//
59// NullableScopedUtfChars name(env, javaName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -070060// if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070061// return NULL;
62// }
63// // ... use name.c_str()
64//
65// TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option.
66class NullableScopedUtfChars {
Elliott Hughesba8eee12012-01-24 20:25:24 -080067 public:
68 NullableScopedUtfChars(JNIEnv* env, jstring s) : mEnv(env), mString(s) {
69 mUtfChars = (s != NULL) ? env->GetStringUTFChars(s, NULL) : NULL;
70 }
71
72 ~NullableScopedUtfChars() {
73 if (mUtfChars) {
74 mEnv->ReleaseStringUTFChars(mString, mUtfChars);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070075 }
Elliott Hughesba8eee12012-01-24 20:25:24 -080076 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070077
Elliott Hughesba8eee12012-01-24 20:25:24 -080078 const char* c_str() const {
79 return mUtfChars;
80 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070081
Elliott Hughesba8eee12012-01-24 20:25:24 -080082 size_t size() const {
83 return strlen(mUtfChars);
84 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070085
Elliott Hughesba8eee12012-01-24 20:25:24 -080086 // Element access.
87 const char& operator[](size_t n) const {
88 return mUtfChars[n];
89 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070090
Elliott Hughesba8eee12012-01-24 20:25:24 -080091 private:
92 JNIEnv* mEnv;
93 jstring mString;
94 const char* mUtfChars;
Brian Carlstromf91c8c32011-09-21 17:30:34 -070095
Elliott Hughesba8eee12012-01-24 20:25:24 -080096 // Disallow copy and assignment.
97 NullableScopedUtfChars(const NullableScopedUtfChars&);
98 void operator=(const NullableScopedUtfChars&);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070099};
100
Calin Juravlea2069c72014-08-06 19:07:41 +0000101static jlong DexFile_openDexFileNative(JNIEnv* env, jclass, jstring javaSourceName, jstring javaOutputName, jint) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700102 ScopedUtfChars sourceName(env, javaSourceName);
103 if (sourceName.c_str() == NULL) {
104 return 0;
105 }
106 NullableScopedUtfChars outputName(env, javaOutputName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -0700107 if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700108 return 0;
109 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700110
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700111 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Andreas Gampe833a4852014-05-21 18:46:59 -0700112 std::unique_ptr<std::vector<const DexFile*>> dex_files(new std::vector<const DexFile*>());
113 std::vector<std::string> error_msgs;
114
115 bool success = linker->OpenDexFilesFromOat(sourceName.c_str(), outputName.c_str(), &error_msgs,
116 dex_files.get());
117
Nicolas Geoffray4fcdc942014-07-22 10:48:00 +0100118 if (success || !dex_files->empty()) {
119 // In the case of non-success, we have not found or could not generate the oat file.
120 // But we may still have found a dex file that we can use.
Andreas Gampe833a4852014-05-21 18:46:59 -0700121 return static_cast<jlong>(reinterpret_cast<uintptr_t>(dex_files.release()));
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700122 } else {
Andreas Gampe833a4852014-05-21 18:46:59 -0700123 // The vector should be empty after a failed loading attempt.
124 DCHECK_EQ(0U, dex_files->size());
125
Vladimir Marko60836d52014-01-16 15:53:38 +0000126 ScopedObjectAccess soa(env);
Andreas Gampe329d1882014-04-08 10:32:19 -0700127 CHECK(!error_msgs.empty());
128 // The most important message is at the end. So set up nesting by going forward, which will
129 // wrap the existing exception as a cause for the following one.
130 auto it = error_msgs.begin();
131 auto itEnd = error_msgs.end();
132 for ( ; it != itEnd; ++it) {
133 ThrowWrappedIOException("%s", it->c_str());
134 }
135
jeffhaoc393a4f2011-10-19 13:46:09 -0700136 return 0;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700137 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700138}
139
Andreas Gampe833a4852014-05-21 18:46:59 -0700140static std::vector<const DexFile*>* toDexFiles(jlong dex_file_address, JNIEnv* env) {
141 std::vector<const DexFile*>* dex_files = reinterpret_cast<std::vector<const DexFile*>*>(
142 static_cast<uintptr_t>(dex_file_address));
143 if (UNLIKELY(dex_files == nullptr)) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700144 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800145 ThrowNullPointerException(NULL, "dex_file == null");
Brian Carlstromaded5f72011-10-07 17:15:04 -0700146 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700147 return dex_files;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700148}
149
Elliott Hughes2d983902014-02-04 16:17:13 -0800150static void DexFile_closeDexFile(JNIEnv* env, jclass, jlong cookie) {
Andreas Gampe833a4852014-05-21 18:46:59 -0700151 std::unique_ptr<std::vector<const DexFile*>> dex_files(toDexFiles(cookie, env));
152 if (dex_files.get() == nullptr) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700153 return;
154 }
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700155 ScopedObjectAccess soa(env);
Andreas Gampe833a4852014-05-21 18:46:59 -0700156
157 size_t index = 0;
158 for (const DexFile* dex_file : *dex_files) {
159 if (Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) {
160 (*dex_files)[index] = nullptr;
161 }
162 index++;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700163 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700164
165 STLDeleteElements(dex_files.get());
166 // Unique_ptr will delete the vector itself.
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700167}
168
Elliott Hughes0512f022012-03-15 22:10:52 -0700169static jclass DexFile_defineClassNative(JNIEnv* env, jclass, jstring javaName, jobject javaLoader,
Elliott Hughes2d983902014-02-04 16:17:13 -0800170 jlong cookie) {
Andreas Gampe833a4852014-05-21 18:46:59 -0700171 std::vector<const DexFile*>* dex_files = toDexFiles(cookie, env);
172 if (dex_files == NULL) {
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700173 VLOG(class_linker) << "Failed to find dex_file";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700174 return NULL;
175 }
Brian Carlstromdf143242011-10-10 18:05:34 -0700176 ScopedUtfChars class_name(env, javaName);
177 if (class_name.c_str() == NULL) {
Brian Carlstrom2e450bf2013-09-06 15:39:46 -0700178 VLOG(class_linker) << "Failed to find class_name";
Brian Carlstromdf143242011-10-10 18:05:34 -0700179 return NULL;
180 }
Elliott Hughes95572412011-12-13 18:14:20 -0800181 const std::string descriptor(DotToDescriptor(class_name.c_str()));
Andreas Gampe833a4852014-05-21 18:46:59 -0700182
183 for (const DexFile* dex_file : *dex_files) {
184 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor.c_str());
185 if (dex_class_def != nullptr) {
186 ScopedObjectAccess soa(env);
187 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
188 class_linker->RegisterDexFile(*dex_file);
189 StackHandleScope<1> hs(soa.Self());
190 Handle<mirror::ClassLoader> class_loader(
191 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(javaLoader)));
192 mirror::Class* result = class_linker->DefineClass(descriptor.c_str(), class_loader, *dex_file,
193 *dex_class_def);
194 if (result != nullptr) {
195 VLOG(class_linker) << "DexFile_defineClassNative returning " << result;
196 return soa.AddLocalReference<jclass>(result);
197 }
198 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700199 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700200 VLOG(class_linker) << "Failed to find dex_class_def";
201 return nullptr;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700202}
203
Andreas Gampe833a4852014-05-21 18:46:59 -0700204// Needed as a compare functor for sets of const char
205struct CharPointerComparator {
206 bool operator()(const char *str1, const char *str2) const {
207 return strcmp(str1, str2) < 0;
208 }
209};
210
211// Note: this can be an expensive call, as we sort out duplicates in MultiDex files.
Elliott Hughes2d983902014-02-04 16:17:13 -0800212static jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jlong cookie) {
Ian Rogersdd157d72014-05-15 14:47:50 -0700213 jobjectArray result = nullptr;
Andreas Gampe833a4852014-05-21 18:46:59 -0700214 std::vector<const DexFile*>* dex_files = toDexFiles(cookie, env);
215
216 if (dex_files != nullptr) {
217 // Push all class descriptors into a set. Use set instead of unordered_set as we want to
218 // retrieve all in the end.
219 std::set<const char*, CharPointerComparator> descriptors;
220 for (const DexFile* dex_file : *dex_files) {
Ian Rogersdd157d72014-05-15 14:47:50 -0700221 for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
222 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
Andreas Gampe833a4852014-05-21 18:46:59 -0700223 const char* descriptor = dex_file->GetClassDescriptor(class_def);
224 descriptors.insert(descriptor);
225 }
226 }
227
228 // Now create output array and copy the set into it.
229 result = env->NewObjectArray(descriptors.size(), WellKnownClasses::java_lang_String, nullptr);
230 if (result != nullptr) {
231 auto it = descriptors.begin();
232 auto it_end = descriptors.end();
233 jsize i = 0;
234 for (; it != it_end; it++, ++i) {
235 std::string descriptor(DescriptorToDot(*it));
Brian Carlstromcf790bb2014-05-28 11:09:10 -0700236 ScopedLocalRef<jstring> jdescriptor(env, env->NewStringUTF(descriptor.c_str()));
Ian Rogersdd157d72014-05-15 14:47:50 -0700237 if (jdescriptor.get() == nullptr) {
238 return nullptr;
239 }
240 env->SetObjectArrayElement(result, i, jdescriptor.get());
241 }
242 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700243 }
Ian Rogersdd157d72014-05-15 14:47:50 -0700244 return result;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700245}
246
Dave Allison39c3bfb2014-01-28 18:33:52 -0800247static void CopyProfileFile(const char* oldfile, const char* newfile) {
Calin Juravle52214102014-06-04 12:01:50 +0100248 ScopedFd src(open(oldfile, O_RDONLY));
249 if (src.get() == -1) {
250 PLOG(ERROR) << "Failed to open profile file " << oldfile
251 << ". My uid:gid is " << getuid() << ":" << getgid();
252 return;
253 }
254
255 struct stat stat_src;
256 if (fstat(src.get(), &stat_src) == -1) {
257 PLOG(ERROR) << "Failed to get stats for profile file " << oldfile
258 << ". My uid:gid is " << getuid() << ":" << getgid();
Dave Allison39c3bfb2014-01-28 18:33:52 -0800259 return;
260 }
261
262 // Create the copy with rw------- (only accessible by system)
Calin Juravle52214102014-06-04 12:01:50 +0100263 ScopedFd dst(open(newfile, O_WRONLY|O_CREAT|O_TRUNC, 0600));
264 if (dst.get() == -1) {
265 PLOG(ERROR) << "Failed to create/write prev profile file " << newfile
266 << ". My uid:gid is " << getuid() << ":" << getgid();
Dave Allison39c3bfb2014-01-28 18:33:52 -0800267 return;
268 }
Calin Juravle52214102014-06-04 12:01:50 +0100269
Ian Rogersc5f17732014-06-05 20:48:42 -0700270#ifdef __linux__
Calin Juravle52214102014-06-04 12:01:50 +0100271 if (sendfile(dst.get(), src.get(), nullptr, stat_src.st_size) == -1) {
Ian Rogersc5f17732014-06-05 20:48:42 -0700272#else
273 off_t len;
274 if (sendfile(dst.get(), src.get(), 0, &len, nullptr, 0) == -1) {
275#endif
Calin Juravle52214102014-06-04 12:01:50 +0100276 PLOG(ERROR) << "Failed to copy profile file " << oldfile << " to " << newfile
277 << ". My uid:gid is " << getuid() << ":" << getgid();
Dave Allison39c3bfb2014-01-28 18:33:52 -0800278 }
Dave Allison39c3bfb2014-01-28 18:33:52 -0800279}
280
Alex Light6e183f22014-07-18 14:57:04 -0700281// Java: dalvik.system.DexFile.UP_TO_DATE
282static const jbyte kUpToDate = 0;
283// Java: dalvik.system.DexFile.DEXOPT_NEEDED
284static const jbyte kPatchoatNeeded = 1;
285// Java: dalvik.system.DexFile.PATCHOAT_NEEDED
286static const jbyte kDexoptNeeded = 2;
287
288template <const bool kVerboseLogging, const bool kReasonLogging>
289static jbyte IsDexOptNeededForFile(const std::string& oat_filename, const char* filename,
290 InstructionSet target_instruction_set) {
291 std::string error_msg;
292 std::unique_ptr<const OatFile> oat_file(OatFile::Open(oat_filename, oat_filename, nullptr,
293 false, &error_msg));
294 if (oat_file.get() == nullptr) {
295 if (kVerboseLogging) {
296 LOG(INFO) << "DexFile_isDexOptNeeded failed to open oat file '" << oat_filename
297 << "' for file location '" << filename << "': " << error_msg;
298 }
299 error_msg.clear();
300 return kDexoptNeeded;
301 }
302 bool should_relocate_if_possible = Runtime::Current()->ShouldRelocate();
303 uint32_t location_checksum = 0;
304 const art::OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(filename, nullptr,
305 kReasonLogging);
306 if (oat_dex_file != nullptr) {
307 // If its not possible to read the classes.dex assume up-to-date as we won't be able to
308 // compile it anyway.
309 if (!DexFile::GetChecksum(filename, &location_checksum, &error_msg)) {
310 if (kVerboseLogging) {
311 LOG(INFO) << "DexFile_isDexOptNeeded found precompiled stripped file: "
312 << filename << " for " << oat_filename << ": " << error_msg;
313 }
314 if (ClassLinker::VerifyOatChecksums(oat_file.get(), target_instruction_set, &error_msg)) {
315 if (kVerboseLogging) {
316 LOG(INFO) << "DexFile_isDexOptNeeded file " << oat_filename
317 << " is up-to-date for " << filename;
318 }
319 return kUpToDate;
320 } else if (should_relocate_if_possible &&
321 ClassLinker::VerifyOatImageChecksum(oat_file.get(), target_instruction_set)) {
322 if (kVerboseLogging) {
323 LOG(INFO) << "DexFile_isDexOptNeeded file " << oat_filename
324 << " needs to be relocated for " << filename;
325 }
326 return kPatchoatNeeded;
327 } else {
328 if (kVerboseLogging) {
329 LOG(INFO) << "DexFile_isDexOptNeeded file " << oat_filename
330 << " is out of date for " << filename;
331 }
332 return kDexoptNeeded;
333 }
334 // If we get here the file is out of date and we should use the system one to relocate.
335 } else {
336 if (ClassLinker::VerifyOatAndDexFileChecksums(oat_file.get(), filename, location_checksum,
337 target_instruction_set, &error_msg)) {
338 if (kVerboseLogging) {
339 LOG(INFO) << "DexFile_isDexOptNeeded file " << oat_filename
340 << " is up-to-date for " << filename;
341 }
342 return kUpToDate;
343 } else if (location_checksum == oat_dex_file->GetDexFileLocationChecksum()
344 && should_relocate_if_possible
345 && ClassLinker::VerifyOatImageChecksum(oat_file.get(), target_instruction_set)) {
346 if (kVerboseLogging) {
347 LOG(INFO) << "DexFile_isDexOptNeeded file " << oat_filename
348 << " needs to be relocated for " << filename;
349 }
350 return kPatchoatNeeded;
351 } else {
352 if (kVerboseLogging) {
353 LOG(INFO) << "DexFile_isDexOptNeeded file " << oat_filename
354 << " is out of date for " << filename;
355 }
356 return kDexoptNeeded;
357 }
358 }
359 } else {
360 if (kVerboseLogging) {
361 LOG(INFO) << "DexFile_isDexOptNeeded file " << oat_filename
362 << " does not contain " << filename;
363 }
364 return kDexoptNeeded;
365 }
366}
367
368static jbyte IsDexOptNeededInternal(JNIEnv* env, const char* filename,
Narayan Kamath11d9f062014-04-23 20:24:57 +0100369 const char* pkgname, const char* instruction_set, const jboolean defer) {
Alex Light6e183f22014-07-18 14:57:04 -0700370 // TODO disable this logging.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700371 const bool kVerboseLogging = false; // Spammy logging.
Brian Carlstrom09881a82014-04-18 17:44:01 -0700372 const bool kReasonLogging = true; // Logging of reason for returning JNI_TRUE.
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800373
Narayan Kamath11d9f062014-04-23 20:24:57 +0100374 if ((filename == nullptr) || !OS::FileExists(filename)) {
375 LOG(ERROR) << "DexFile_isDexOptNeeded file '" << filename << "' does not exist";
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700376 ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException"));
Narayan Kamath11d9f062014-04-23 20:24:57 +0100377 const char* message = (filename == nullptr) ? "<empty file name>" : filename;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700378 env->ThrowNew(fnfe.get(), message);
Alex Light6e183f22014-07-18 14:57:04 -0700379 return kUpToDate;
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700380 }
381
382 // Always treat elements of the bootclasspath as up-to-date. The
383 // fact that code is running at all means that this should be true.
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700384 Runtime* runtime = Runtime::Current();
385 ClassLinker* class_linker = runtime->GetClassLinker();
Narayan Kamath11d9f062014-04-23 20:24:57 +0100386 // TODO: We're assuming that the 64 and 32 bit runtimes have identical
387 // class paths. isDexOptNeeded will not necessarily be called on a runtime
388 // that has the same instruction set as the file being dexopted.
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700389 const std::vector<const DexFile*>& boot_class_path = class_linker->GetBootClassPath();
390 for (size_t i = 0; i < boot_class_path.size(); i++) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100391 if (boot_class_path[i]->GetLocation() == filename) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700392 if (kVerboseLogging) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100393 LOG(INFO) << "DexFile_isDexOptNeeded ignoring boot class path file: " << filename;
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800394 }
Alex Light6e183f22014-07-18 14:57:04 -0700395 return kUpToDate;
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700396 }
397 }
398
Alex Light6e183f22014-07-18 14:57:04 -0700399 bool force_system_only = false;
400 bool require_system_version = false;
Brian Carlstrome1ff1992014-05-18 22:37:51 -0700401
Dave Allison39c3bfb2014-01-28 18:33:52 -0800402 // Check the profile file. We need to rerun dex2oat if the profile has changed significantly
403 // since the last time, or it's new.
404 // If the 'defer' argument is true then this will be retried later. In this case we
405 // need to make sure that the profile file copy is not made so that we will get the
406 // same result second time.
Alex Light6e183f22014-07-18 14:57:04 -0700407 std::string profile_file;
408 std::string prev_profile_file;
409 bool should_copy_profile = false;
Calin Juravlec1b643c2014-05-30 23:44:11 +0100410 if (Runtime::Current()->GetProfilerOptions().IsEnabled() && (pkgname != nullptr)) {
Alex Light6e183f22014-07-18 14:57:04 -0700411 profile_file = GetDalvikCacheOrDie("profiles", false /* create_if_absent */)
Narayan Kamath11d9f062014-04-23 20:24:57 +0100412 + std::string("/") + pkgname;
Alex Light6e183f22014-07-18 14:57:04 -0700413 prev_profile_file = profile_file + std::string("@old");
Dave Allison39c3bfb2014-01-28 18:33:52 -0800414
415 struct stat profstat, prevstat;
416 int e1 = stat(profile_file.c_str(), &profstat);
Alex Light6e183f22014-07-18 14:57:04 -0700417 int e1_errno = errno;
Dave Allison39c3bfb2014-01-28 18:33:52 -0800418 int e2 = stat(prev_profile_file.c_str(), &prevstat);
Alex Light6e183f22014-07-18 14:57:04 -0700419 int e2_errno = errno;
Dave Allison39c3bfb2014-01-28 18:33:52 -0800420 if (e1 < 0) {
Alex Light6e183f22014-07-18 14:57:04 -0700421 if (e1_errno != EACCES) {
422 // No profile file, need to run dex2oat, unless we find a file in system
423 if (kReasonLogging) {
424 LOG(INFO) << "DexFile_isDexOptNeededInternal profile file " << profile_file << " doesn't exist. "
425 << "Will check odex to see if we can find a working version.";
426 }
427 // Force it to only accept system files/files with versions in system.
428 require_system_version = true;
429 } else {
430 LOG(INFO) << "DexFile_isDexOptNeededInternal recieved EACCES trying to stat profile file "
431 << profile_file;
Dave Allison39c3bfb2014-01-28 18:33:52 -0800432 }
Alex Light6e183f22014-07-18 14:57:04 -0700433 } else if (e2 == 0) {
Dave Allison39c3bfb2014-01-28 18:33:52 -0800434 // There is a previous profile file. Check if the profile has changed significantly.
Calin Juravle9dae5b42014-04-07 16:36:21 +0300435 // A change in profile is considered significant if X% (change_thr property) of the top K%
436 // (compile_thr property) samples has changed.
Calin Juravlec1b643c2014-05-30 23:44:11 +0100437 double top_k_threshold = Runtime::Current()->GetProfilerOptions().GetTopKThreshold();
438 double change_threshold = Runtime::Current()->GetProfilerOptions().GetTopKChangeThreshold();
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100439 double change_percent = 0.0;
440 ProfileFile new_profile, old_profile;
441 bool new_ok = new_profile.LoadFile(profile_file);
442 bool old_ok = old_profile.LoadFile(prev_profile_file);
443 if (!new_ok || !old_ok) {
Brian Carlstrom09881a82014-04-18 17:44:01 -0700444 if (kVerboseLogging) {
Alex Light6e183f22014-07-18 14:57:04 -0700445 LOG(INFO) << "DexFile_isDexOptNeededInternal Ignoring invalid profiles: "
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100446 << (new_ok ? "" : profile_file) << " " << (old_ok ? "" : prev_profile_file);
Calin Juravle9dae5b42014-04-07 16:36:21 +0300447 }
Calin Juravle9dae5b42014-04-07 16:36:21 +0300448 } else {
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100449 std::set<std::string> new_top_k, old_top_k;
450 new_profile.GetTopKSamples(new_top_k, top_k_threshold);
451 old_profile.GetTopKSamples(old_top_k, top_k_threshold);
452 if (new_top_k.empty()) {
453 if (kVerboseLogging) {
Alex Light6e183f22014-07-18 14:57:04 -0700454 LOG(INFO) << "DexFile_isDexOptNeededInternal empty profile: " << profile_file;
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100455 }
456 // If the new topK is empty we shouldn't optimize so we leave the change_percent at 0.0.
457 } else {
458 std::set<std::string> diff;
459 std::set_difference(new_top_k.begin(), new_top_k.end(), old_top_k.begin(), old_top_k.end(),
460 std::inserter(diff, diff.end()));
461 // TODO: consider using the usedPercentage instead of the plain diff count.
462 change_percent = 100.0 * static_cast<double>(diff.size()) / static_cast<double>(new_top_k.size());
463 if (kVerboseLogging) {
464 std::set<std::string>::iterator end = diff.end();
465 for (std::set<std::string>::iterator it = diff.begin(); it != end; it++) {
Alex Light6e183f22014-07-18 14:57:04 -0700466 LOG(INFO) << "DexFile_isDexOptNeededInternal new in topK: " << *it;
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100467 }
Calin Juravle9dae5b42014-04-07 16:36:21 +0300468 }
469 }
Dave Allison39c3bfb2014-01-28 18:33:52 -0800470 }
471
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100472 if (change_percent > change_threshold) {
Brian Carlstrom09881a82014-04-18 17:44:01 -0700473 if (kReasonLogging) {
Alex Light6e183f22014-07-18 14:57:04 -0700474 LOG(INFO) << "DexFile_isDexOptNeededInternal size of new profile file " << profile_file <<
Calin Juravle9dae5b42014-04-07 16:36:21 +0300475 " is significantly different from old profile file " << prev_profile_file << " (top "
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100476 << top_k_threshold << "% samples changed in proportion of " << change_percent << "%)";
Dave Allison39c3bfb2014-01-28 18:33:52 -0800477 }
Alex Light6e183f22014-07-18 14:57:04 -0700478 should_copy_profile = !defer;
479 // Force us to only accept system files.
480 force_system_only = true;
Dave Allison39c3bfb2014-01-28 18:33:52 -0800481 }
Alex Light6e183f22014-07-18 14:57:04 -0700482 } else if (e2_errno == ENOENT) {
Dave Allison39c3bfb2014-01-28 18:33:52 -0800483 // Previous profile does not exist. Make a copy of the current one.
Brian Carlstrom09881a82014-04-18 17:44:01 -0700484 if (kVerboseLogging) {
Alex Light6e183f22014-07-18 14:57:04 -0700485 LOG(INFO) << "DexFile_isDexOptNeededInternal previous profile doesn't exist: " << prev_profile_file;
Dave Allison39c3bfb2014-01-28 18:33:52 -0800486 }
Alex Light6e183f22014-07-18 14:57:04 -0700487 should_copy_profile = !defer;
488 } else {
489 PLOG(INFO) << "Unable to stat previous profile file " << prev_profile_file;
Dave Allison39c3bfb2014-01-28 18:33:52 -0800490 }
491 }
492
Alex Light6e183f22014-07-18 14:57:04 -0700493 const InstructionSet target_instruction_set = GetInstructionSetFromString(instruction_set);
Andreas Gampe20c89302014-08-19 17:28:06 -0700494 if (target_instruction_set == kNone) {
495 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
496 std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set));
497 env->ThrowNew(iae.get(), message.c_str());
498 return 0;
499 }
Alex Light6e183f22014-07-18 14:57:04 -0700500
501 // Get the filename for odex file next to the dex file.
502 std::string odex_filename(DexFilenameToOdexFilename(filename, target_instruction_set));
503 // Get the filename for the dalvik-cache file
504 std::string cache_dir;
505 bool have_android_data = false;
506 bool dalvik_cache_exists = false;
507 GetDalvikCache(instruction_set, false, &cache_dir, &have_android_data, &dalvik_cache_exists);
508 std::string cache_filename; // was cache_location
509 bool have_cache_filename = false;
510 if (dalvik_cache_exists) {
511 std::string error_msg;
512 have_cache_filename = GetDalvikCacheFilename(filename, cache_dir.c_str(), &cache_filename,
513 &error_msg);
514 if (!have_cache_filename && kVerboseLogging) {
515 LOG(INFO) << "DexFile_isDexOptNeededInternal failed to find cache file for dex file " << filename
516 << ": " << error_msg;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700517 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800518 }
519
Alex Light6e183f22014-07-18 14:57:04 -0700520 bool should_relocate_if_possible = Runtime::Current()->ShouldRelocate();
521
Alex Light6e183f22014-07-18 14:57:04 -0700522 jbyte dalvik_cache_decision = -1;
523 // Lets try the cache first (since we want to load from there since thats where the relocated
524 // versions will be).
525 if (have_cache_filename && !force_system_only) {
526 // We can use the dalvik-cache if we find a good file.
527 dalvik_cache_decision =
Narayan Kamath202d1f02014-08-08 16:19:44 +0100528 IsDexOptNeededForFile<kVerboseLogging, kReasonLogging>(cache_filename, filename,
529 target_instruction_set);
Alex Light6e183f22014-07-18 14:57:04 -0700530 // We will only return DexOptNeeded if both the cache and system return it.
531 if (dalvik_cache_decision != kDexoptNeeded && !require_system_version) {
532 CHECK(!(dalvik_cache_decision == kPatchoatNeeded && !should_relocate_if_possible))
533 << "May not return PatchoatNeeded when patching is disabled.";
534 return dalvik_cache_decision;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700535 }
Alex Light6e183f22014-07-18 14:57:04 -0700536 // We couldn't find one thats easy. We should now try the system.
Brian Carlstroma004aa92012-02-08 18:05:09 -0800537 }
538
Alex Light6e183f22014-07-18 14:57:04 -0700539 jbyte system_decision =
Narayan Kamath202d1f02014-08-08 16:19:44 +0100540 IsDexOptNeededForFile<kVerboseLogging, kReasonLogging>(odex_filename, filename,
541 target_instruction_set);
Alex Light6e183f22014-07-18 14:57:04 -0700542 CHECK(!(system_decision == kPatchoatNeeded && !should_relocate_if_possible))
543 << "May not return PatchoatNeeded when patching is disabled.";
544
545 if (require_system_version && system_decision == kPatchoatNeeded
546 && dalvik_cache_decision == kUpToDate) {
547 // We have a version from system relocated to the cache. Return it.
548 return dalvik_cache_decision;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800549 }
550
Alex Light6e183f22014-07-18 14:57:04 -0700551 if (should_copy_profile && system_decision == kDexoptNeeded) {
552 CopyProfileFile(profile_file.c_str(), prev_profile_file.c_str());
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800553 }
Alex Light6e183f22014-07-18 14:57:04 -0700554
555 return system_decision;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700556}
557
Alex Light6e183f22014-07-18 14:57:04 -0700558static jbyte DexFile_isDexOptNeededInternal(JNIEnv* env, jclass, jstring javaFilename,
Narayan Kamath11d9f062014-04-23 20:24:57 +0100559 jstring javaPkgname, jstring javaInstructionSet, jboolean defer) {
560 ScopedUtfChars filename(env, javaFilename);
Andreas Gampe20c89302014-08-19 17:28:06 -0700561 if (env->ExceptionCheck()) {
562 return 0;
563 }
564
Narayan Kamath11d9f062014-04-23 20:24:57 +0100565 NullableScopedUtfChars pkgname(env, javaPkgname);
Andreas Gampe20c89302014-08-19 17:28:06 -0700566
Narayan Kamath11d9f062014-04-23 20:24:57 +0100567 ScopedUtfChars instruction_set(env, javaInstructionSet);
Andreas Gampe20c89302014-08-19 17:28:06 -0700568 if (env->ExceptionCheck()) {
569 return 0;
570 }
Narayan Kamath11d9f062014-04-23 20:24:57 +0100571
572 return IsDexOptNeededInternal(env, filename.c_str(), pkgname.c_str(),
573 instruction_set.c_str(), defer);
574}
575
Dave Allison39c3bfb2014-01-28 18:33:52 -0800576// public API, NULL pkgname
Narayan Kamath11d9f062014-04-23 20:24:57 +0100577static jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
578 const char* instruction_set = GetInstructionSetString(kRuntimeISA);
579 ScopedUtfChars filename(env, javaFilename);
Alex Light6e183f22014-07-18 14:57:04 -0700580 return kUpToDate != IsDexOptNeededInternal(env, filename.c_str(), nullptr /* pkgname */,
581 instruction_set, false /* defer */);
Dave Allison39c3bfb2014-01-28 18:33:52 -0800582}
583
584
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700585static JNINativeMethod gMethods[] = {
Elliott Hughes2d983902014-02-04 16:17:13 -0800586 NATIVE_METHOD(DexFile, closeDexFile, "(J)V"),
587 NATIVE_METHOD(DexFile, defineClassNative, "(Ljava/lang/String;Ljava/lang/ClassLoader;J)Ljava/lang/Class;"),
588 NATIVE_METHOD(DexFile, getClassNameList, "(J)[Ljava/lang/String;"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700589 NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
Alex Light6e183f22014-07-18 14:57:04 -0700590 NATIVE_METHOD(DexFile, isDexOptNeededInternal, "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Z)B"),
Calin Juravlea2069c72014-08-06 19:07:41 +0000591 NATIVE_METHOD(DexFile, openDexFileNative, "(Ljava/lang/String;Ljava/lang/String;I)J"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700592};
593
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700594void register_dalvik_system_DexFile(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700595 REGISTER_NATIVE_METHODS("dalvik/system/DexFile");
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700596}
597
598} // namespace art