blob: 037072d8d938345626d70b10c29cbb18a2db8240 [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
Andreas Gampe277ccbd2014-11-03 21:36:10 -080017#include "dalvik_system_DexFile.h"
18
Calin Juravle9dae5b42014-04-07 16:36:21 +030019#include <algorithm>
Calin Juravle9dae5b42014-04-07 16:36:21 +030020#include <set>
Ian Rogersdd157d72014-05-15 14:47:50 -070021#include <fcntl.h>
Ian Rogersc5f17732014-06-05 20:48:42 -070022#ifdef __linux__
Calin Juravle52214102014-06-04 12:01:50 +010023#include <sys/sendfile.h>
Ian Rogersc5f17732014-06-05 20:48:42 -070024#else
25#include <sys/socket.h>
26#endif
Calin Juravle52214102014-06-04 12:01:50 +010027#include <sys/stat.h>
Calin Juravle9dae5b42014-04-07 16:36:21 +030028#include <unistd.h>
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -070029
Elliott Hughes07ed66b2012-12-12 18:34:25 -080030#include "base/logging.h"
Andreas Gampe833a4852014-05-21 18:46:59 -070031#include "base/stl_util.h"
Andreas Gampe20c89302014-08-19 17:28:06 -070032#include "base/stringprintf.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070033#include "class_linker.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080034#include "common_throws.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070035#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070036#include "gc/space/image_space.h"
37#include "gc/space/space-inl.h"
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070038#include "image.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070039#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040#include "mirror/class_loader.h"
Ian Rogers05f30572013-02-20 12:13:11 -080041#include "mirror/object-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042#include "mirror/string.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080043#include "oat.h"
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -070044#include "os.h"
Calin Juravle9dae5b42014-04-07 16:36:21 +030045#include "profiler.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070046#include "runtime.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070047#include "scoped_thread_state_change.h"
Ian Rogersc9818482012-01-11 08:52:51 -080048#include "ScopedLocalRef.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070049#include "ScopedUtfChars.h"
Calin Juravlebb0b53f2014-05-23 17:33:29 +010050#include "utils.h"
Ian Rogersdd157d72014-05-15 14:47:50 -070051#include "well_known_classes.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070052#include "zip_archive.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070053
Andreas Gampe277ccbd2014-11-03 21:36:10 -080054#pragma GCC diagnostic push
55#pragma GCC diagnostic ignored "-Wshadow"
56#include "ScopedFd.h"
57#pragma GCC diagnostic pop
58
Brian Carlstromf91c8c32011-09-21 17:30:34 -070059namespace art {
60
Brian Carlstromf91c8c32011-09-21 17:30:34 -070061// A smart pointer that provides read-only access to a Java string's UTF chars.
62// Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if
63// passed a null jstring. The correct idiom is:
64//
65// NullableScopedUtfChars name(env, javaName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -070066// if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070067// return NULL;
68// }
69// // ... use name.c_str()
70//
71// TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option.
72class NullableScopedUtfChars {
Elliott Hughesba8eee12012-01-24 20:25:24 -080073 public:
74 NullableScopedUtfChars(JNIEnv* env, jstring s) : mEnv(env), mString(s) {
75 mUtfChars = (s != NULL) ? env->GetStringUTFChars(s, NULL) : NULL;
76 }
77
78 ~NullableScopedUtfChars() {
79 if (mUtfChars) {
80 mEnv->ReleaseStringUTFChars(mString, mUtfChars);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070081 }
Elliott Hughesba8eee12012-01-24 20:25:24 -080082 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070083
Elliott Hughesba8eee12012-01-24 20:25:24 -080084 const char* c_str() const {
85 return mUtfChars;
86 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070087
Elliott Hughesba8eee12012-01-24 20:25:24 -080088 size_t size() const {
89 return strlen(mUtfChars);
90 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070091
Elliott Hughesba8eee12012-01-24 20:25:24 -080092 // Element access.
93 const char& operator[](size_t n) const {
94 return mUtfChars[n];
95 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070096
Elliott Hughesba8eee12012-01-24 20:25:24 -080097 private:
98 JNIEnv* mEnv;
99 jstring mString;
100 const char* mUtfChars;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700101
Elliott Hughesba8eee12012-01-24 20:25:24 -0800102 // Disallow copy and assignment.
103 NullableScopedUtfChars(const NullableScopedUtfChars&);
104 void operator=(const NullableScopedUtfChars&);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700105};
106
Calin Juravlea2069c72014-08-06 19:07:41 +0000107static jlong DexFile_openDexFileNative(JNIEnv* env, jclass, jstring javaSourceName, jstring javaOutputName, jint) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700108 ScopedUtfChars sourceName(env, javaSourceName);
109 if (sourceName.c_str() == NULL) {
110 return 0;
111 }
112 NullableScopedUtfChars outputName(env, javaOutputName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -0700113 if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700114 return 0;
115 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700116
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700117 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800118 std::unique_ptr<std::vector<std::unique_ptr<const DexFile>>> dex_files(
119 new std::vector<std::unique_ptr<const DexFile>>());
Andreas Gampe833a4852014-05-21 18:46:59 -0700120 std::vector<std::string> error_msgs;
121
122 bool success = linker->OpenDexFilesFromOat(sourceName.c_str(), outputName.c_str(), &error_msgs,
123 dex_files.get());
124
Nicolas Geoffray4fcdc942014-07-22 10:48:00 +0100125 if (success || !dex_files->empty()) {
126 // In the case of non-success, we have not found or could not generate the oat file.
127 // But we may still have found a dex file that we can use.
Andreas Gampe833a4852014-05-21 18:46:59 -0700128 return static_cast<jlong>(reinterpret_cast<uintptr_t>(dex_files.release()));
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700129 } else {
Andreas Gampe833a4852014-05-21 18:46:59 -0700130 // The vector should be empty after a failed loading attempt.
131 DCHECK_EQ(0U, dex_files->size());
132
Vladimir Marko60836d52014-01-16 15:53:38 +0000133 ScopedObjectAccess soa(env);
Andreas Gampe329d1882014-04-08 10:32:19 -0700134 CHECK(!error_msgs.empty());
135 // The most important message is at the end. So set up nesting by going forward, which will
136 // wrap the existing exception as a cause for the following one.
137 auto it = error_msgs.begin();
138 auto itEnd = error_msgs.end();
139 for ( ; it != itEnd; ++it) {
140 ThrowWrappedIOException("%s", it->c_str());
141 }
142
jeffhaoc393a4f2011-10-19 13:46:09 -0700143 return 0;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700144 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700145}
146
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800147static std::vector<std::unique_ptr<const DexFile>>*
148toDexFiles(jlong dex_file_address, JNIEnv* env) {
149 std::vector<std::unique_ptr<const DexFile>>* dex_files
150 = reinterpret_cast<std::vector<std::unique_ptr<const DexFile>>*>(
151 static_cast<uintptr_t>(dex_file_address));
Andreas Gampe833a4852014-05-21 18:46:59 -0700152 if (UNLIKELY(dex_files == nullptr)) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700153 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800154 ThrowNullPointerException(NULL, "dex_file == null");
Brian Carlstromaded5f72011-10-07 17:15:04 -0700155 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700156 return dex_files;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700157}
158
Elliott Hughes2d983902014-02-04 16:17:13 -0800159static void DexFile_closeDexFile(JNIEnv* env, jclass, jlong cookie) {
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800160 std::unique_ptr<std::vector<std::unique_ptr<const DexFile>>> dex_files(toDexFiles(cookie, env));
Andreas Gampe833a4852014-05-21 18:46:59 -0700161 if (dex_files.get() == nullptr) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700162 return;
163 }
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700164 ScopedObjectAccess soa(env);
Andreas Gampe833a4852014-05-21 18:46:59 -0700165
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800166 // The Runtime currently never unloads classes, which means any registered
167 // dex files must be kept around forever in case they are used. We
168 // accomplish this here by explicitly leaking those dex files that are
169 // registered.
170 //
171 // TODO: The Runtime should support unloading of classes and freeing of the
172 // dex files for those unloaded classes rather than leaking dex files here.
173 for (auto& dex_file : *dex_files) {
Andreas Gampe833a4852014-05-21 18:46:59 -0700174 if (Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) {
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800175 dex_file.release();
Andreas Gampe833a4852014-05-21 18:46:59 -0700176 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700177 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700178}
179
Elliott Hughes0512f022012-03-15 22:10:52 -0700180static jclass DexFile_defineClassNative(JNIEnv* env, jclass, jstring javaName, jobject javaLoader,
Elliott Hughes2d983902014-02-04 16:17:13 -0800181 jlong cookie) {
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800182 std::vector<std::unique_ptr<const DexFile>>* dex_files = toDexFiles(cookie, env);
Andreas Gampe833a4852014-05-21 18:46:59 -0700183 if (dex_files == NULL) {
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700184 VLOG(class_linker) << "Failed to find dex_file";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700185 return NULL;
186 }
Brian Carlstromdf143242011-10-10 18:05:34 -0700187 ScopedUtfChars class_name(env, javaName);
188 if (class_name.c_str() == NULL) {
Brian Carlstrom2e450bf2013-09-06 15:39:46 -0700189 VLOG(class_linker) << "Failed to find class_name";
Brian Carlstromdf143242011-10-10 18:05:34 -0700190 return NULL;
191 }
Elliott Hughes95572412011-12-13 18:14:20 -0800192 const std::string descriptor(DotToDescriptor(class_name.c_str()));
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800193 const size_t hash(ComputeModifiedUtf8Hash(descriptor.c_str()));
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800194 for (auto& dex_file : *dex_files) {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800195 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor.c_str(), hash);
Andreas Gampe833a4852014-05-21 18:46:59 -0700196 if (dex_class_def != nullptr) {
197 ScopedObjectAccess soa(env);
198 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
199 class_linker->RegisterDexFile(*dex_file);
200 StackHandleScope<1> hs(soa.Self());
201 Handle<mirror::ClassLoader> class_loader(
202 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(javaLoader)));
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800203 mirror::Class* result = class_linker->DefineClass(soa.Self(), descriptor.c_str(), hash,
Ian Rogers7b078e82014-09-10 14:44:24 -0700204 class_loader, *dex_file, *dex_class_def);
Andreas Gampe833a4852014-05-21 18:46:59 -0700205 if (result != nullptr) {
Brian Carlstrom667ab7c2014-10-16 19:12:28 -0700206 VLOG(class_linker) << "DexFile_defineClassNative returning " << result
207 << " for " << class_name.c_str();
Andreas Gampe833a4852014-05-21 18:46:59 -0700208 return soa.AddLocalReference<jclass>(result);
209 }
210 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700211 }
Brian Carlstrom667ab7c2014-10-16 19:12:28 -0700212 VLOG(class_linker) << "Failed to find dex_class_def " << class_name.c_str();
Andreas Gampe833a4852014-05-21 18:46:59 -0700213 return nullptr;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700214}
215
Andreas Gampe833a4852014-05-21 18:46:59 -0700216// Needed as a compare functor for sets of const char
217struct CharPointerComparator {
218 bool operator()(const char *str1, const char *str2) const {
219 return strcmp(str1, str2) < 0;
220 }
221};
222
223// Note: this can be an expensive call, as we sort out duplicates in MultiDex files.
Elliott Hughes2d983902014-02-04 16:17:13 -0800224static jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jlong cookie) {
Ian Rogersdd157d72014-05-15 14:47:50 -0700225 jobjectArray result = nullptr;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800226 std::vector<std::unique_ptr<const DexFile>>* dex_files = toDexFiles(cookie, env);
Andreas Gampe833a4852014-05-21 18:46:59 -0700227
228 if (dex_files != nullptr) {
229 // Push all class descriptors into a set. Use set instead of unordered_set as we want to
230 // retrieve all in the end.
231 std::set<const char*, CharPointerComparator> descriptors;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800232 for (auto& dex_file : *dex_files) {
Ian Rogersdd157d72014-05-15 14:47:50 -0700233 for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
234 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
Andreas Gampe833a4852014-05-21 18:46:59 -0700235 const char* descriptor = dex_file->GetClassDescriptor(class_def);
236 descriptors.insert(descriptor);
237 }
238 }
239
240 // Now create output array and copy the set into it.
241 result = env->NewObjectArray(descriptors.size(), WellKnownClasses::java_lang_String, nullptr);
242 if (result != nullptr) {
243 auto it = descriptors.begin();
244 auto it_end = descriptors.end();
245 jsize i = 0;
246 for (; it != it_end; it++, ++i) {
247 std::string descriptor(DescriptorToDot(*it));
Brian Carlstromcf790bb2014-05-28 11:09:10 -0700248 ScopedLocalRef<jstring> jdescriptor(env, env->NewStringUTF(descriptor.c_str()));
Ian Rogersdd157d72014-05-15 14:47:50 -0700249 if (jdescriptor.get() == nullptr) {
250 return nullptr;
251 }
252 env->SetObjectArrayElement(result, i, jdescriptor.get());
253 }
254 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700255 }
Ian Rogersdd157d72014-05-15 14:47:50 -0700256 return result;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700257}
258
Dave Allison39c3bfb2014-01-28 18:33:52 -0800259static void CopyProfileFile(const char* oldfile, const char* newfile) {
Calin Juravle52214102014-06-04 12:01:50 +0100260 ScopedFd src(open(oldfile, O_RDONLY));
261 if (src.get() == -1) {
262 PLOG(ERROR) << "Failed to open profile file " << oldfile
263 << ". My uid:gid is " << getuid() << ":" << getgid();
264 return;
265 }
266
267 struct stat stat_src;
268 if (fstat(src.get(), &stat_src) == -1) {
269 PLOG(ERROR) << "Failed to get stats for profile file " << oldfile
270 << ". My uid:gid is " << getuid() << ":" << getgid();
Dave Allison39c3bfb2014-01-28 18:33:52 -0800271 return;
272 }
273
274 // Create the copy with rw------- (only accessible by system)
Calin Juravle52214102014-06-04 12:01:50 +0100275 ScopedFd dst(open(newfile, O_WRONLY|O_CREAT|O_TRUNC, 0600));
276 if (dst.get() == -1) {
277 PLOG(ERROR) << "Failed to create/write prev profile file " << newfile
278 << ". My uid:gid is " << getuid() << ":" << getgid();
Dave Allison39c3bfb2014-01-28 18:33:52 -0800279 return;
280 }
Calin Juravle52214102014-06-04 12:01:50 +0100281
Ian Rogersc5f17732014-06-05 20:48:42 -0700282#ifdef __linux__
Calin Juravle52214102014-06-04 12:01:50 +0100283 if (sendfile(dst.get(), src.get(), nullptr, stat_src.st_size) == -1) {
Ian Rogersc5f17732014-06-05 20:48:42 -0700284#else
285 off_t len;
286 if (sendfile(dst.get(), src.get(), 0, &len, nullptr, 0) == -1) {
287#endif
Calin Juravle52214102014-06-04 12:01:50 +0100288 PLOG(ERROR) << "Failed to copy profile file " << oldfile << " to " << newfile
289 << ". My uid:gid is " << getuid() << ":" << getgid();
Dave Allison39c3bfb2014-01-28 18:33:52 -0800290 }
Dave Allison39c3bfb2014-01-28 18:33:52 -0800291}
292
Alex Light6e183f22014-07-18 14:57:04 -0700293// Java: dalvik.system.DexFile.UP_TO_DATE
294static const jbyte kUpToDate = 0;
295// Java: dalvik.system.DexFile.DEXOPT_NEEDED
296static const jbyte kPatchoatNeeded = 1;
297// Java: dalvik.system.DexFile.PATCHOAT_NEEDED
298static const jbyte kDexoptNeeded = 2;
299
300template <const bool kVerboseLogging, const bool kReasonLogging>
301static jbyte IsDexOptNeededForFile(const std::string& oat_filename, const char* filename,
Igor Murashkin46774762014-10-22 11:37:02 -0700302 InstructionSet target_instruction_set,
303 bool* oat_is_pic) {
Alex Light6e183f22014-07-18 14:57:04 -0700304 std::string error_msg;
305 std::unique_ptr<const OatFile> oat_file(OatFile::Open(oat_filename, oat_filename, nullptr,
Igor Murashkin46774762014-10-22 11:37:02 -0700306 nullptr,
Alex Light6e183f22014-07-18 14:57:04 -0700307 false, &error_msg));
308 if (oat_file.get() == nullptr) {
Brian Carlstrom98515952015-01-06 12:05:34 -0800309 // Note that even though this is kDexoptNeeded, we use
310 // kVerboseLogging instead of the usual kReasonLogging since it is
311 // the common case on first boot and very spammy.
312 if (kVerboseLogging) {
Alex Light6e183f22014-07-18 14:57:04 -0700313 LOG(INFO) << "DexFile_isDexOptNeeded failed to open oat file '" << oat_filename
314 << "' for file location '" << filename << "': " << error_msg;
315 }
316 error_msg.clear();
317 return kDexoptNeeded;
318 }
Igor Murashkin46774762014-10-22 11:37:02 -0700319
320 // Pass-up the information about if this is PIC.
321 // TODO: Refactor this function to be less complicated.
322 *oat_is_pic = oat_file->IsPic();
323
Alex Light6e183f22014-07-18 14:57:04 -0700324 bool should_relocate_if_possible = Runtime::Current()->ShouldRelocate();
325 uint32_t location_checksum = 0;
326 const art::OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(filename, nullptr,
327 kReasonLogging);
328 if (oat_dex_file != nullptr) {
329 // If its not possible to read the classes.dex assume up-to-date as we won't be able to
330 // compile it anyway.
331 if (!DexFile::GetChecksum(filename, &location_checksum, &error_msg)) {
332 if (kVerboseLogging) {
333 LOG(INFO) << "DexFile_isDexOptNeeded found precompiled stripped file: "
334 << filename << " for " << oat_filename << ": " << error_msg;
335 }
336 if (ClassLinker::VerifyOatChecksums(oat_file.get(), target_instruction_set, &error_msg)) {
337 if (kVerboseLogging) {
338 LOG(INFO) << "DexFile_isDexOptNeeded file " << oat_filename
339 << " is up-to-date for " << filename;
340 }
341 return kUpToDate;
342 } else if (should_relocate_if_possible &&
343 ClassLinker::VerifyOatImageChecksum(oat_file.get(), target_instruction_set)) {
Brian Carlstrome8e39892014-09-23 13:53:28 -0700344 if (kReasonLogging) {
Alex Light6e183f22014-07-18 14:57:04 -0700345 LOG(INFO) << "DexFile_isDexOptNeeded file " << oat_filename
346 << " needs to be relocated for " << filename;
347 }
348 return kPatchoatNeeded;
349 } else {
Brian Carlstrome8e39892014-09-23 13:53:28 -0700350 if (kReasonLogging) {
Alex Light6e183f22014-07-18 14:57:04 -0700351 LOG(INFO) << "DexFile_isDexOptNeeded file " << oat_filename
352 << " is out of date for " << filename;
353 }
354 return kDexoptNeeded;
355 }
356 // If we get here the file is out of date and we should use the system one to relocate.
357 } else {
358 if (ClassLinker::VerifyOatAndDexFileChecksums(oat_file.get(), filename, location_checksum,
359 target_instruction_set, &error_msg)) {
360 if (kVerboseLogging) {
361 LOG(INFO) << "DexFile_isDexOptNeeded file " << oat_filename
362 << " is up-to-date for " << filename;
363 }
364 return kUpToDate;
365 } else if (location_checksum == oat_dex_file->GetDexFileLocationChecksum()
366 && should_relocate_if_possible
367 && ClassLinker::VerifyOatImageChecksum(oat_file.get(), target_instruction_set)) {
Brian Carlstrome8e39892014-09-23 13:53:28 -0700368 if (kReasonLogging) {
Alex Light6e183f22014-07-18 14:57:04 -0700369 LOG(INFO) << "DexFile_isDexOptNeeded file " << oat_filename
370 << " needs to be relocated for " << filename;
371 }
372 return kPatchoatNeeded;
373 } else {
Brian Carlstrome8e39892014-09-23 13:53:28 -0700374 if (kReasonLogging) {
Alex Light6e183f22014-07-18 14:57:04 -0700375 LOG(INFO) << "DexFile_isDexOptNeeded file " << oat_filename
376 << " is out of date for " << filename;
377 }
378 return kDexoptNeeded;
379 }
380 }
381 } else {
Brian Carlstrome8e39892014-09-23 13:53:28 -0700382 if (kReasonLogging) {
Alex Light6e183f22014-07-18 14:57:04 -0700383 LOG(INFO) << "DexFile_isDexOptNeeded file " << oat_filename
384 << " does not contain " << filename;
385 }
386 return kDexoptNeeded;
387 }
388}
389
390static jbyte IsDexOptNeededInternal(JNIEnv* env, const char* filename,
Narayan Kamath11d9f062014-04-23 20:24:57 +0100391 const char* pkgname, const char* instruction_set, const jboolean defer) {
Brian Carlstrome8e39892014-09-23 13:53:28 -0700392 // Spammy logging for kUpToDate
393 const bool kVerboseLogging = false;
394 // Logging of reason for returning kDexoptNeeded or kPatchoatNeeded.
395 const bool kReasonLogging = true;
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800396
Narayan Kamath11d9f062014-04-23 20:24:57 +0100397 if ((filename == nullptr) || !OS::FileExists(filename)) {
398 LOG(ERROR) << "DexFile_isDexOptNeeded file '" << filename << "' does not exist";
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700399 ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException"));
Narayan Kamath11d9f062014-04-23 20:24:57 +0100400 const char* message = (filename == nullptr) ? "<empty file name>" : filename;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700401 env->ThrowNew(fnfe.get(), message);
Alex Light6e183f22014-07-18 14:57:04 -0700402 return kUpToDate;
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700403 }
404
405 // Always treat elements of the bootclasspath as up-to-date. The
406 // fact that code is running at all means that this should be true.
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700407 Runtime* runtime = Runtime::Current();
408 ClassLinker* class_linker = runtime->GetClassLinker();
Narayan Kamath11d9f062014-04-23 20:24:57 +0100409 // TODO: We're assuming that the 64 and 32 bit runtimes have identical
410 // class paths. isDexOptNeeded will not necessarily be called on a runtime
411 // that has the same instruction set as the file being dexopted.
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700412 const std::vector<const DexFile*>& boot_class_path = class_linker->GetBootClassPath();
413 for (size_t i = 0; i < boot_class_path.size(); i++) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100414 if (boot_class_path[i]->GetLocation() == filename) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700415 if (kVerboseLogging) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100416 LOG(INFO) << "DexFile_isDexOptNeeded ignoring boot class path file: " << filename;
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800417 }
Alex Light6e183f22014-07-18 14:57:04 -0700418 return kUpToDate;
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700419 }
420 }
421
Alex Light6e183f22014-07-18 14:57:04 -0700422 bool force_system_only = false;
423 bool require_system_version = false;
Brian Carlstrome1ff1992014-05-18 22:37:51 -0700424
Dave Allison39c3bfb2014-01-28 18:33:52 -0800425 // Check the profile file. We need to rerun dex2oat if the profile has changed significantly
426 // since the last time, or it's new.
427 // If the 'defer' argument is true then this will be retried later. In this case we
428 // need to make sure that the profile file copy is not made so that we will get the
429 // same result second time.
Alex Light6e183f22014-07-18 14:57:04 -0700430 std::string profile_file;
431 std::string prev_profile_file;
432 bool should_copy_profile = false;
Calin Juravlec1b643c2014-05-30 23:44:11 +0100433 if (Runtime::Current()->GetProfilerOptions().IsEnabled() && (pkgname != nullptr)) {
Alex Light6e183f22014-07-18 14:57:04 -0700434 profile_file = GetDalvikCacheOrDie("profiles", false /* create_if_absent */)
Narayan Kamath11d9f062014-04-23 20:24:57 +0100435 + std::string("/") + pkgname;
Alex Light6e183f22014-07-18 14:57:04 -0700436 prev_profile_file = profile_file + std::string("@old");
Dave Allison39c3bfb2014-01-28 18:33:52 -0800437
438 struct stat profstat, prevstat;
439 int e1 = stat(profile_file.c_str(), &profstat);
Alex Light6e183f22014-07-18 14:57:04 -0700440 int e1_errno = errno;
Dave Allison39c3bfb2014-01-28 18:33:52 -0800441 int e2 = stat(prev_profile_file.c_str(), &prevstat);
Alex Light6e183f22014-07-18 14:57:04 -0700442 int e2_errno = errno;
Dave Allison39c3bfb2014-01-28 18:33:52 -0800443 if (e1 < 0) {
Alex Light6e183f22014-07-18 14:57:04 -0700444 if (e1_errno != EACCES) {
445 // No profile file, need to run dex2oat, unless we find a file in system
446 if (kReasonLogging) {
447 LOG(INFO) << "DexFile_isDexOptNeededInternal profile file " << profile_file << " doesn't exist. "
448 << "Will check odex to see if we can find a working version.";
449 }
450 // Force it to only accept system files/files with versions in system.
451 require_system_version = true;
452 } else {
453 LOG(INFO) << "DexFile_isDexOptNeededInternal recieved EACCES trying to stat profile file "
454 << profile_file;
Dave Allison39c3bfb2014-01-28 18:33:52 -0800455 }
Alex Light6e183f22014-07-18 14:57:04 -0700456 } else if (e2 == 0) {
Dave Allison39c3bfb2014-01-28 18:33:52 -0800457 // There is a previous profile file. Check if the profile has changed significantly.
Calin Juravle9dae5b42014-04-07 16:36:21 +0300458 // A change in profile is considered significant if X% (change_thr property) of the top K%
459 // (compile_thr property) samples has changed.
Calin Juravlec1b643c2014-05-30 23:44:11 +0100460 double top_k_threshold = Runtime::Current()->GetProfilerOptions().GetTopKThreshold();
461 double change_threshold = Runtime::Current()->GetProfilerOptions().GetTopKChangeThreshold();
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100462 double change_percent = 0.0;
463 ProfileFile new_profile, old_profile;
464 bool new_ok = new_profile.LoadFile(profile_file);
465 bool old_ok = old_profile.LoadFile(prev_profile_file);
466 if (!new_ok || !old_ok) {
Brian Carlstrom09881a82014-04-18 17:44:01 -0700467 if (kVerboseLogging) {
Alex Light6e183f22014-07-18 14:57:04 -0700468 LOG(INFO) << "DexFile_isDexOptNeededInternal Ignoring invalid profiles: "
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100469 << (new_ok ? "" : profile_file) << " " << (old_ok ? "" : prev_profile_file);
Calin Juravle9dae5b42014-04-07 16:36:21 +0300470 }
Calin Juravle9dae5b42014-04-07 16:36:21 +0300471 } else {
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100472 std::set<std::string> new_top_k, old_top_k;
473 new_profile.GetTopKSamples(new_top_k, top_k_threshold);
474 old_profile.GetTopKSamples(old_top_k, top_k_threshold);
475 if (new_top_k.empty()) {
476 if (kVerboseLogging) {
Alex Light6e183f22014-07-18 14:57:04 -0700477 LOG(INFO) << "DexFile_isDexOptNeededInternal empty profile: " << profile_file;
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100478 }
479 // If the new topK is empty we shouldn't optimize so we leave the change_percent at 0.0.
480 } else {
481 std::set<std::string> diff;
482 std::set_difference(new_top_k.begin(), new_top_k.end(), old_top_k.begin(), old_top_k.end(),
483 std::inserter(diff, diff.end()));
484 // TODO: consider using the usedPercentage instead of the plain diff count.
485 change_percent = 100.0 * static_cast<double>(diff.size()) / static_cast<double>(new_top_k.size());
486 if (kVerboseLogging) {
487 std::set<std::string>::iterator end = diff.end();
488 for (std::set<std::string>::iterator it = diff.begin(); it != end; it++) {
Alex Light6e183f22014-07-18 14:57:04 -0700489 LOG(INFO) << "DexFile_isDexOptNeededInternal new in topK: " << *it;
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100490 }
Calin Juravle9dae5b42014-04-07 16:36:21 +0300491 }
492 }
Dave Allison39c3bfb2014-01-28 18:33:52 -0800493 }
494
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100495 if (change_percent > change_threshold) {
Brian Carlstrom09881a82014-04-18 17:44:01 -0700496 if (kReasonLogging) {
Alex Light6e183f22014-07-18 14:57:04 -0700497 LOG(INFO) << "DexFile_isDexOptNeededInternal size of new profile file " << profile_file <<
Calin Juravle9dae5b42014-04-07 16:36:21 +0300498 " is significantly different from old profile file " << prev_profile_file << " (top "
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100499 << top_k_threshold << "% samples changed in proportion of " << change_percent << "%)";
Dave Allison39c3bfb2014-01-28 18:33:52 -0800500 }
Alex Light6e183f22014-07-18 14:57:04 -0700501 should_copy_profile = !defer;
502 // Force us to only accept system files.
503 force_system_only = true;
Dave Allison39c3bfb2014-01-28 18:33:52 -0800504 }
Alex Light6e183f22014-07-18 14:57:04 -0700505 } else if (e2_errno == ENOENT) {
Dave Allison39c3bfb2014-01-28 18:33:52 -0800506 // Previous profile does not exist. Make a copy of the current one.
Brian Carlstrom09881a82014-04-18 17:44:01 -0700507 if (kVerboseLogging) {
Alex Light6e183f22014-07-18 14:57:04 -0700508 LOG(INFO) << "DexFile_isDexOptNeededInternal previous profile doesn't exist: " << prev_profile_file;
Dave Allison39c3bfb2014-01-28 18:33:52 -0800509 }
Alex Light6e183f22014-07-18 14:57:04 -0700510 should_copy_profile = !defer;
511 } else {
512 PLOG(INFO) << "Unable to stat previous profile file " << prev_profile_file;
Dave Allison39c3bfb2014-01-28 18:33:52 -0800513 }
514 }
515
Alex Light6e183f22014-07-18 14:57:04 -0700516 const InstructionSet target_instruction_set = GetInstructionSetFromString(instruction_set);
Andreas Gampe20c89302014-08-19 17:28:06 -0700517 if (target_instruction_set == kNone) {
518 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
519 std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set));
520 env->ThrowNew(iae.get(), message.c_str());
521 return 0;
522 }
Alex Light6e183f22014-07-18 14:57:04 -0700523
524 // Get the filename for odex file next to the dex file.
525 std::string odex_filename(DexFilenameToOdexFilename(filename, target_instruction_set));
526 // Get the filename for the dalvik-cache file
527 std::string cache_dir;
528 bool have_android_data = false;
529 bool dalvik_cache_exists = false;
Andreas Gampe3c13a792014-09-18 20:56:04 -0700530 bool is_global_cache = false;
531 GetDalvikCache(instruction_set, false, &cache_dir, &have_android_data, &dalvik_cache_exists,
532 &is_global_cache);
Alex Light6e183f22014-07-18 14:57:04 -0700533 std::string cache_filename; // was cache_location
534 bool have_cache_filename = false;
535 if (dalvik_cache_exists) {
536 std::string error_msg;
537 have_cache_filename = GetDalvikCacheFilename(filename, cache_dir.c_str(), &cache_filename,
538 &error_msg);
539 if (!have_cache_filename && kVerboseLogging) {
540 LOG(INFO) << "DexFile_isDexOptNeededInternal failed to find cache file for dex file " << filename
541 << ": " << error_msg;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700542 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800543 }
544
Alex Light6e183f22014-07-18 14:57:04 -0700545 bool should_relocate_if_possible = Runtime::Current()->ShouldRelocate();
546
Alex Light6e183f22014-07-18 14:57:04 -0700547 jbyte dalvik_cache_decision = -1;
548 // Lets try the cache first (since we want to load from there since thats where the relocated
549 // versions will be).
550 if (have_cache_filename && !force_system_only) {
Igor Murashkin46774762014-10-22 11:37:02 -0700551 bool oat_is_pic;
Alex Light6e183f22014-07-18 14:57:04 -0700552 // We can use the dalvik-cache if we find a good file.
553 dalvik_cache_decision =
Narayan Kamath202d1f02014-08-08 16:19:44 +0100554 IsDexOptNeededForFile<kVerboseLogging, kReasonLogging>(cache_filename, filename,
Igor Murashkin46774762014-10-22 11:37:02 -0700555 target_instruction_set, &oat_is_pic);
556
557 // Apps that are compiled with --compile-pic never need to be patchoat-d
558 if (oat_is_pic && dalvik_cache_decision == kPatchoatNeeded) {
559 dalvik_cache_decision = kUpToDate;
560 }
Alex Light6e183f22014-07-18 14:57:04 -0700561 // We will only return DexOptNeeded if both the cache and system return it.
562 if (dalvik_cache_decision != kDexoptNeeded && !require_system_version) {
563 CHECK(!(dalvik_cache_decision == kPatchoatNeeded && !should_relocate_if_possible))
564 << "May not return PatchoatNeeded when patching is disabled.";
565 return dalvik_cache_decision;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700566 }
Alex Light6e183f22014-07-18 14:57:04 -0700567 // We couldn't find one thats easy. We should now try the system.
Brian Carlstroma004aa92012-02-08 18:05:09 -0800568 }
569
Igor Murashkin46774762014-10-22 11:37:02 -0700570 bool oat_is_pic;
Alex Light6e183f22014-07-18 14:57:04 -0700571 jbyte system_decision =
Narayan Kamath202d1f02014-08-08 16:19:44 +0100572 IsDexOptNeededForFile<kVerboseLogging, kReasonLogging>(odex_filename, filename,
Igor Murashkin46774762014-10-22 11:37:02 -0700573 target_instruction_set, &oat_is_pic);
Alex Light6e183f22014-07-18 14:57:04 -0700574 CHECK(!(system_decision == kPatchoatNeeded && !should_relocate_if_possible))
575 << "May not return PatchoatNeeded when patching is disabled.";
576
Igor Murashkin46774762014-10-22 11:37:02 -0700577 // Apps that are compiled with --compile-pic never need to be patchoat-d
578 if (oat_is_pic && system_decision == kPatchoatNeeded) {
579 system_decision = kUpToDate;
580 }
581
Alex Light6e183f22014-07-18 14:57:04 -0700582 if (require_system_version && system_decision == kPatchoatNeeded
583 && dalvik_cache_decision == kUpToDate) {
584 // We have a version from system relocated to the cache. Return it.
585 return dalvik_cache_decision;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800586 }
587
Alex Light6e183f22014-07-18 14:57:04 -0700588 if (should_copy_profile && system_decision == kDexoptNeeded) {
589 CopyProfileFile(profile_file.c_str(), prev_profile_file.c_str());
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800590 }
Alex Light6e183f22014-07-18 14:57:04 -0700591
592 return system_decision;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700593}
594
Alex Light6e183f22014-07-18 14:57:04 -0700595static jbyte DexFile_isDexOptNeededInternal(JNIEnv* env, jclass, jstring javaFilename,
Narayan Kamath11d9f062014-04-23 20:24:57 +0100596 jstring javaPkgname, jstring javaInstructionSet, jboolean defer) {
597 ScopedUtfChars filename(env, javaFilename);
Andreas Gampe20c89302014-08-19 17:28:06 -0700598 if (env->ExceptionCheck()) {
599 return 0;
600 }
601
Narayan Kamath11d9f062014-04-23 20:24:57 +0100602 NullableScopedUtfChars pkgname(env, javaPkgname);
Andreas Gampe20c89302014-08-19 17:28:06 -0700603
Narayan Kamath11d9f062014-04-23 20:24:57 +0100604 ScopedUtfChars instruction_set(env, javaInstructionSet);
Andreas Gampe20c89302014-08-19 17:28:06 -0700605 if (env->ExceptionCheck()) {
606 return 0;
607 }
Narayan Kamath11d9f062014-04-23 20:24:57 +0100608
609 return IsDexOptNeededInternal(env, filename.c_str(), pkgname.c_str(),
610 instruction_set.c_str(), defer);
611}
612
Dave Allison39c3bfb2014-01-28 18:33:52 -0800613// public API, NULL pkgname
Narayan Kamath11d9f062014-04-23 20:24:57 +0100614static jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
615 const char* instruction_set = GetInstructionSetString(kRuntimeISA);
616 ScopedUtfChars filename(env, javaFilename);
Alex Light6e183f22014-07-18 14:57:04 -0700617 return kUpToDate != IsDexOptNeededInternal(env, filename.c_str(), nullptr /* pkgname */,
618 instruction_set, false /* defer */);
Dave Allison39c3bfb2014-01-28 18:33:52 -0800619}
620
621
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700622static JNINativeMethod gMethods[] = {
Elliott Hughes2d983902014-02-04 16:17:13 -0800623 NATIVE_METHOD(DexFile, closeDexFile, "(J)V"),
624 NATIVE_METHOD(DexFile, defineClassNative, "(Ljava/lang/String;Ljava/lang/ClassLoader;J)Ljava/lang/Class;"),
625 NATIVE_METHOD(DexFile, getClassNameList, "(J)[Ljava/lang/String;"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700626 NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
Alex Light6e183f22014-07-18 14:57:04 -0700627 NATIVE_METHOD(DexFile, isDexOptNeededInternal, "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Z)B"),
Calin Juravlea2069c72014-08-06 19:07:41 +0000628 NATIVE_METHOD(DexFile, openDexFileNative, "(Ljava/lang/String;Ljava/lang/String;I)J"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700629};
630
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700631void register_dalvik_system_DexFile(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700632 REGISTER_NATIVE_METHODS("dalvik/system/DexFile");
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700633}
634
635} // namespace art