blob: e1fe3eb9184de1b3902ef183c3c502195542ad75 [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
Andreas Gampe324b9bb2015-02-23 16:33:22 -080061static std::unique_ptr<std::vector<const DexFile*>>
62ConvertJavaArrayToNative(JNIEnv* env, jobject arrayObject) {
63 jarray array = reinterpret_cast<jarray>(arrayObject);
64
65 jsize array_size = env->GetArrayLength(array);
66 if (env->ExceptionCheck() == JNI_TRUE) {
67 return std::unique_ptr<std::vector<const DexFile*>>();
68 }
69
70 // TODO: Optimize. On 32bit we can use an int array.
71 jboolean is_long_data_copied;
72 jlong* long_data = env->GetLongArrayElements(reinterpret_cast<jlongArray>(array),
73 &is_long_data_copied);
74 if (env->ExceptionCheck() == JNI_TRUE) {
75 return std::unique_ptr<std::vector<const DexFile*>>();
76 }
77
78 std::unique_ptr<std::vector<const DexFile*>> ret(new std::vector<const DexFile*>());
79 ret->reserve(array_size);
80 for (jsize i = 0; i < array_size; ++i) {
81 ret->push_back(reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(*(long_data + i))));
82 }
83
84 env->ReleaseLongArrayElements(reinterpret_cast<jlongArray>(array), long_data, JNI_ABORT);
85 if (env->ExceptionCheck() == JNI_TRUE) {
86 return std::unique_ptr<std::vector<const DexFile*>>();
87 }
88
89 return ret;
90}
91
92static jlongArray ConvertNativeToJavaArray(JNIEnv* env,
93 std::vector<std::unique_ptr<const DexFile>>& vec) {
94 size_t vec_size = vec.size();
95 jlongArray long_array = env->NewLongArray(static_cast<jsize>(vec_size));
96 if (env->ExceptionCheck() == JNI_TRUE) {
97 return nullptr;
98 }
99
100 jboolean is_long_data_copied;
101 jlong* long_data = env->GetLongArrayElements(long_array, &is_long_data_copied);
102 if (env->ExceptionCheck() == JNI_TRUE) {
103 return nullptr;
104 }
105
106 jlong* tmp = long_data;
107 for (auto& dex_file : vec) {
108 *tmp = reinterpret_cast<uintptr_t>(dex_file.get());
109 tmp++;
110 }
111
112 env->ReleaseLongArrayElements(long_array, long_data, 0);
113 if (env->ExceptionCheck() == JNI_TRUE) {
114 return nullptr;
115 }
116
117 // Now release all the unique_ptrs.
118 for (auto& dex_file : vec) {
119 dex_file.release();
120 }
121
122 return long_array;
123}
124
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700125// A smart pointer that provides read-only access to a Java string's UTF chars.
126// Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if
127// passed a null jstring. The correct idiom is:
128//
129// NullableScopedUtfChars name(env, javaName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -0700130// if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700131// return NULL;
132// }
133// // ... use name.c_str()
134//
135// TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option.
136class NullableScopedUtfChars {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800137 public:
138 NullableScopedUtfChars(JNIEnv* env, jstring s) : mEnv(env), mString(s) {
139 mUtfChars = (s != NULL) ? env->GetStringUTFChars(s, NULL) : NULL;
140 }
141
142 ~NullableScopedUtfChars() {
143 if (mUtfChars) {
144 mEnv->ReleaseStringUTFChars(mString, mUtfChars);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700145 }
Elliott Hughesba8eee12012-01-24 20:25:24 -0800146 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700147
Elliott Hughesba8eee12012-01-24 20:25:24 -0800148 const char* c_str() const {
149 return mUtfChars;
150 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700151
Elliott Hughesba8eee12012-01-24 20:25:24 -0800152 size_t size() const {
153 return strlen(mUtfChars);
154 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700155
Elliott Hughesba8eee12012-01-24 20:25:24 -0800156 // Element access.
157 const char& operator[](size_t n) const {
158 return mUtfChars[n];
159 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700160
Elliott Hughesba8eee12012-01-24 20:25:24 -0800161 private:
162 JNIEnv* mEnv;
163 jstring mString;
164 const char* mUtfChars;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700165
Elliott Hughesba8eee12012-01-24 20:25:24 -0800166 // Disallow copy and assignment.
167 NullableScopedUtfChars(const NullableScopedUtfChars&);
168 void operator=(const NullableScopedUtfChars&);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700169};
170
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800171static jobject DexFile_openDexFileNative(JNIEnv* env, jclass, jstring javaSourceName, jstring javaOutputName, jint) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700172 ScopedUtfChars sourceName(env, javaSourceName);
173 if (sourceName.c_str() == NULL) {
174 return 0;
175 }
176 NullableScopedUtfChars outputName(env, javaOutputName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -0700177 if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700178 return 0;
179 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700180
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700181 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800182 std::vector<std::unique_ptr<const DexFile>> dex_files;
Andreas Gampe833a4852014-05-21 18:46:59 -0700183 std::vector<std::string> error_msgs;
184
185 bool success = linker->OpenDexFilesFromOat(sourceName.c_str(), outputName.c_str(), &error_msgs,
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800186 &dex_files);
Andreas Gampe833a4852014-05-21 18:46:59 -0700187
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800188 if (success || !dex_files.empty()) {
189 jlongArray array = ConvertNativeToJavaArray(env, dex_files);
190 if (array == nullptr) {
191 ScopedObjectAccess soa(env);
192 for (auto& dex_file : dex_files) {
193 if (Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) {
194 dex_file.release();
195 }
196 }
197 }
198 return array;
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700199 } else {
Andreas Gampe833a4852014-05-21 18:46:59 -0700200 // The vector should be empty after a failed loading attempt.
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800201 DCHECK_EQ(0U, dex_files.size());
Andreas Gampe833a4852014-05-21 18:46:59 -0700202
Vladimir Marko60836d52014-01-16 15:53:38 +0000203 ScopedObjectAccess soa(env);
Andreas Gampe329d1882014-04-08 10:32:19 -0700204 CHECK(!error_msgs.empty());
205 // The most important message is at the end. So set up nesting by going forward, which will
206 // wrap the existing exception as a cause for the following one.
207 auto it = error_msgs.begin();
208 auto itEnd = error_msgs.end();
209 for ( ; it != itEnd; ++it) {
210 ThrowWrappedIOException("%s", it->c_str());
211 }
212
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800213 return nullptr;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700214 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700215}
216
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800217static void DexFile_closeDexFile(JNIEnv* env, jclass, jobject cookie) {
218 std::unique_ptr<std::vector<const DexFile*>> dex_files = ConvertJavaArrayToNative(env, cookie);
Andreas Gampe833a4852014-05-21 18:46:59 -0700219 if (dex_files.get() == nullptr) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800220 DCHECK(env->ExceptionCheck());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700221 return;
222 }
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800223
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700224 ScopedObjectAccess soa(env);
Andreas Gampe833a4852014-05-21 18:46:59 -0700225
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800226 // The Runtime currently never unloads classes, which means any registered
227 // dex files must be kept around forever in case they are used. We
228 // accomplish this here by explicitly leaking those dex files that are
229 // registered.
230 //
231 // TODO: The Runtime should support unloading of classes and freeing of the
232 // dex files for those unloaded classes rather than leaking dex files here.
233 for (auto& dex_file : *dex_files) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800234 if (!Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) {
235 delete dex_file;
Andreas Gampe833a4852014-05-21 18:46:59 -0700236 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700237 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700238}
239
Elliott Hughes0512f022012-03-15 22:10:52 -0700240static jclass DexFile_defineClassNative(JNIEnv* env, jclass, jstring javaName, jobject javaLoader,
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800241 jobject cookie) {
242 std::unique_ptr<std::vector<const DexFile*>> dex_files = ConvertJavaArrayToNative(env, cookie);
243 if (dex_files.get() == nullptr) {
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700244 VLOG(class_linker) << "Failed to find dex_file";
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800245 DCHECK(env->ExceptionCheck());
246 return nullptr;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700247 }
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800248
Brian Carlstromdf143242011-10-10 18:05:34 -0700249 ScopedUtfChars class_name(env, javaName);
250 if (class_name.c_str() == NULL) {
Brian Carlstrom2e450bf2013-09-06 15:39:46 -0700251 VLOG(class_linker) << "Failed to find class_name";
Brian Carlstromdf143242011-10-10 18:05:34 -0700252 return NULL;
253 }
Elliott Hughes95572412011-12-13 18:14:20 -0800254 const std::string descriptor(DotToDescriptor(class_name.c_str()));
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800255 const size_t hash(ComputeModifiedUtf8Hash(descriptor.c_str()));
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800256 for (auto& dex_file : *dex_files) {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800257 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor.c_str(), hash);
Andreas Gampe833a4852014-05-21 18:46:59 -0700258 if (dex_class_def != nullptr) {
259 ScopedObjectAccess soa(env);
260 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
261 class_linker->RegisterDexFile(*dex_file);
262 StackHandleScope<1> hs(soa.Self());
263 Handle<mirror::ClassLoader> class_loader(
264 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(javaLoader)));
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800265 mirror::Class* result = class_linker->DefineClass(soa.Self(), descriptor.c_str(), hash,
Ian Rogers7b078e82014-09-10 14:44:24 -0700266 class_loader, *dex_file, *dex_class_def);
Andreas Gampe833a4852014-05-21 18:46:59 -0700267 if (result != nullptr) {
Brian Carlstrom667ab7c2014-10-16 19:12:28 -0700268 VLOG(class_linker) << "DexFile_defineClassNative returning " << result
269 << " for " << class_name.c_str();
Andreas Gampe833a4852014-05-21 18:46:59 -0700270 return soa.AddLocalReference<jclass>(result);
271 }
272 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700273 }
Brian Carlstrom667ab7c2014-10-16 19:12:28 -0700274 VLOG(class_linker) << "Failed to find dex_class_def " << class_name.c_str();
Andreas Gampe833a4852014-05-21 18:46:59 -0700275 return nullptr;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700276}
277
Andreas Gampe833a4852014-05-21 18:46:59 -0700278// Needed as a compare functor for sets of const char
279struct CharPointerComparator {
280 bool operator()(const char *str1, const char *str2) const {
281 return strcmp(str1, str2) < 0;
282 }
283};
284
285// Note: this can be an expensive call, as we sort out duplicates in MultiDex files.
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800286static jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jobject cookie) {
287 std::unique_ptr<std::vector<const DexFile*>> dex_files = ConvertJavaArrayToNative(env, cookie);
288 if (dex_files.get() == nullptr) {
289 DCHECK(env->ExceptionCheck());
290 return nullptr;
291 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700292
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800293 // Push all class descriptors into a set. Use set instead of unordered_set as we want to
294 // retrieve all in the end.
295 std::set<const char*, CharPointerComparator> descriptors;
296 for (auto& dex_file : *dex_files) {
297 for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
298 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
299 const char* descriptor = dex_file->GetClassDescriptor(class_def);
300 descriptors.insert(descriptor);
Andreas Gampe833a4852014-05-21 18:46:59 -0700301 }
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800302 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700303
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800304 // Now create output array and copy the set into it.
305 jobjectArray result = env->NewObjectArray(descriptors.size(), WellKnownClasses::java_lang_String,
306 nullptr);
307 if (result != nullptr) {
308 auto it = descriptors.begin();
309 auto it_end = descriptors.end();
310 jsize i = 0;
311 for (; it != it_end; it++, ++i) {
312 std::string descriptor(DescriptorToDot(*it));
313 ScopedLocalRef<jstring> jdescriptor(env, env->NewStringUTF(descriptor.c_str()));
314 if (jdescriptor.get() == nullptr) {
315 return nullptr;
Ian Rogersdd157d72014-05-15 14:47:50 -0700316 }
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800317 env->SetObjectArrayElement(result, i, jdescriptor.get());
Ian Rogersdd157d72014-05-15 14:47:50 -0700318 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700319 }
Ian Rogersdd157d72014-05-15 14:47:50 -0700320 return result;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700321}
322
Dave Allison39c3bfb2014-01-28 18:33:52 -0800323static void CopyProfileFile(const char* oldfile, const char* newfile) {
Calin Juravle52214102014-06-04 12:01:50 +0100324 ScopedFd src(open(oldfile, O_RDONLY));
325 if (src.get() == -1) {
326 PLOG(ERROR) << "Failed to open profile file " << oldfile
327 << ". My uid:gid is " << getuid() << ":" << getgid();
328 return;
329 }
330
331 struct stat stat_src;
332 if (fstat(src.get(), &stat_src) == -1) {
333 PLOG(ERROR) << "Failed to get stats for profile file " << oldfile
334 << ". My uid:gid is " << getuid() << ":" << getgid();
Dave Allison39c3bfb2014-01-28 18:33:52 -0800335 return;
336 }
337
338 // Create the copy with rw------- (only accessible by system)
Calin Juravle52214102014-06-04 12:01:50 +0100339 ScopedFd dst(open(newfile, O_WRONLY|O_CREAT|O_TRUNC, 0600));
340 if (dst.get() == -1) {
341 PLOG(ERROR) << "Failed to create/write prev profile file " << newfile
342 << ". My uid:gid is " << getuid() << ":" << getgid();
Dave Allison39c3bfb2014-01-28 18:33:52 -0800343 return;
344 }
Calin Juravle52214102014-06-04 12:01:50 +0100345
Ian Rogersc5f17732014-06-05 20:48:42 -0700346#ifdef __linux__
Calin Juravle52214102014-06-04 12:01:50 +0100347 if (sendfile(dst.get(), src.get(), nullptr, stat_src.st_size) == -1) {
Ian Rogersc5f17732014-06-05 20:48:42 -0700348#else
349 off_t len;
350 if (sendfile(dst.get(), src.get(), 0, &len, nullptr, 0) == -1) {
351#endif
Calin Juravle52214102014-06-04 12:01:50 +0100352 PLOG(ERROR) << "Failed to copy profile file " << oldfile << " to " << newfile
353 << ". My uid:gid is " << getuid() << ":" << getgid();
Dave Allison39c3bfb2014-01-28 18:33:52 -0800354 }
Dave Allison39c3bfb2014-01-28 18:33:52 -0800355}
356
Alex Light6e183f22014-07-18 14:57:04 -0700357// Java: dalvik.system.DexFile.UP_TO_DATE
358static const jbyte kUpToDate = 0;
359// Java: dalvik.system.DexFile.DEXOPT_NEEDED
360static const jbyte kPatchoatNeeded = 1;
361// Java: dalvik.system.DexFile.PATCHOAT_NEEDED
362static const jbyte kDexoptNeeded = 2;
363
364template <const bool kVerboseLogging, const bool kReasonLogging>
365static jbyte IsDexOptNeededForFile(const std::string& oat_filename, const char* filename,
Igor Murashkin46774762014-10-22 11:37:02 -0700366 InstructionSet target_instruction_set,
367 bool* oat_is_pic) {
Alex Light6e183f22014-07-18 14:57:04 -0700368 std::string error_msg;
369 std::unique_ptr<const OatFile> oat_file(OatFile::Open(oat_filename, oat_filename, nullptr,
Igor Murashkin46774762014-10-22 11:37:02 -0700370 nullptr,
Alex Light6e183f22014-07-18 14:57:04 -0700371 false, &error_msg));
372 if (oat_file.get() == nullptr) {
Brian Carlstrom98515952015-01-06 12:05:34 -0800373 // Note that even though this is kDexoptNeeded, we use
374 // kVerboseLogging instead of the usual kReasonLogging since it is
375 // the common case on first boot and very spammy.
376 if (kVerboseLogging) {
Alex Light6e183f22014-07-18 14:57:04 -0700377 LOG(INFO) << "DexFile_isDexOptNeeded failed to open oat file '" << oat_filename
378 << "' for file location '" << filename << "': " << error_msg;
379 }
380 error_msg.clear();
381 return kDexoptNeeded;
382 }
Igor Murashkin46774762014-10-22 11:37:02 -0700383
384 // Pass-up the information about if this is PIC.
385 // TODO: Refactor this function to be less complicated.
386 *oat_is_pic = oat_file->IsPic();
387
Alex Light6e183f22014-07-18 14:57:04 -0700388 bool should_relocate_if_possible = Runtime::Current()->ShouldRelocate();
389 uint32_t location_checksum = 0;
390 const art::OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(filename, nullptr,
391 kReasonLogging);
392 if (oat_dex_file != nullptr) {
393 // If its not possible to read the classes.dex assume up-to-date as we won't be able to
394 // compile it anyway.
395 if (!DexFile::GetChecksum(filename, &location_checksum, &error_msg)) {
396 if (kVerboseLogging) {
397 LOG(INFO) << "DexFile_isDexOptNeeded found precompiled stripped file: "
398 << filename << " for " << oat_filename << ": " << error_msg;
399 }
400 if (ClassLinker::VerifyOatChecksums(oat_file.get(), target_instruction_set, &error_msg)) {
401 if (kVerboseLogging) {
402 LOG(INFO) << "DexFile_isDexOptNeeded file " << oat_filename
403 << " is up-to-date for " << filename;
404 }
405 return kUpToDate;
406 } else if (should_relocate_if_possible &&
407 ClassLinker::VerifyOatImageChecksum(oat_file.get(), target_instruction_set)) {
Brian Carlstrome8e39892014-09-23 13:53:28 -0700408 if (kReasonLogging) {
Alex Light6e183f22014-07-18 14:57:04 -0700409 LOG(INFO) << "DexFile_isDexOptNeeded file " << oat_filename
410 << " needs to be relocated for " << filename;
411 }
412 return kPatchoatNeeded;
413 } else {
Brian Carlstrome8e39892014-09-23 13:53:28 -0700414 if (kReasonLogging) {
Alex Light6e183f22014-07-18 14:57:04 -0700415 LOG(INFO) << "DexFile_isDexOptNeeded file " << oat_filename
416 << " is out of date for " << filename;
417 }
418 return kDexoptNeeded;
419 }
420 // If we get here the file is out of date and we should use the system one to relocate.
421 } else {
422 if (ClassLinker::VerifyOatAndDexFileChecksums(oat_file.get(), filename, location_checksum,
423 target_instruction_set, &error_msg)) {
424 if (kVerboseLogging) {
425 LOG(INFO) << "DexFile_isDexOptNeeded file " << oat_filename
426 << " is up-to-date for " << filename;
427 }
428 return kUpToDate;
429 } else if (location_checksum == oat_dex_file->GetDexFileLocationChecksum()
430 && should_relocate_if_possible
431 && ClassLinker::VerifyOatImageChecksum(oat_file.get(), target_instruction_set)) {
Brian Carlstrome8e39892014-09-23 13:53:28 -0700432 if (kReasonLogging) {
Alex Light6e183f22014-07-18 14:57:04 -0700433 LOG(INFO) << "DexFile_isDexOptNeeded file " << oat_filename
434 << " needs to be relocated for " << filename;
435 }
436 return kPatchoatNeeded;
437 } else {
Brian Carlstrome8e39892014-09-23 13:53:28 -0700438 if (kReasonLogging) {
Alex Light6e183f22014-07-18 14:57:04 -0700439 LOG(INFO) << "DexFile_isDexOptNeeded file " << oat_filename
440 << " is out of date for " << filename;
441 }
442 return kDexoptNeeded;
443 }
444 }
445 } else {
Brian Carlstrome8e39892014-09-23 13:53:28 -0700446 if (kReasonLogging) {
Alex Light6e183f22014-07-18 14:57:04 -0700447 LOG(INFO) << "DexFile_isDexOptNeeded file " << oat_filename
448 << " does not contain " << filename;
449 }
450 return kDexoptNeeded;
451 }
452}
453
454static jbyte IsDexOptNeededInternal(JNIEnv* env, const char* filename,
Narayan Kamath11d9f062014-04-23 20:24:57 +0100455 const char* pkgname, const char* instruction_set, const jboolean defer) {
Brian Carlstrome8e39892014-09-23 13:53:28 -0700456 // Spammy logging for kUpToDate
457 const bool kVerboseLogging = false;
458 // Logging of reason for returning kDexoptNeeded or kPatchoatNeeded.
459 const bool kReasonLogging = true;
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800460
Narayan Kamath11d9f062014-04-23 20:24:57 +0100461 if ((filename == nullptr) || !OS::FileExists(filename)) {
462 LOG(ERROR) << "DexFile_isDexOptNeeded file '" << filename << "' does not exist";
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700463 ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException"));
Narayan Kamath11d9f062014-04-23 20:24:57 +0100464 const char* message = (filename == nullptr) ? "<empty file name>" : filename;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700465 env->ThrowNew(fnfe.get(), message);
Alex Light6e183f22014-07-18 14:57:04 -0700466 return kUpToDate;
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700467 }
468
469 // Always treat elements of the bootclasspath as up-to-date. The
470 // fact that code is running at all means that this should be true.
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700471 Runtime* runtime = Runtime::Current();
472 ClassLinker* class_linker = runtime->GetClassLinker();
Narayan Kamath11d9f062014-04-23 20:24:57 +0100473 // TODO: We're assuming that the 64 and 32 bit runtimes have identical
474 // class paths. isDexOptNeeded will not necessarily be called on a runtime
475 // that has the same instruction set as the file being dexopted.
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700476 const std::vector<const DexFile*>& boot_class_path = class_linker->GetBootClassPath();
477 for (size_t i = 0; i < boot_class_path.size(); i++) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100478 if (boot_class_path[i]->GetLocation() == filename) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700479 if (kVerboseLogging) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100480 LOG(INFO) << "DexFile_isDexOptNeeded ignoring boot class path file: " << filename;
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800481 }
Alex Light6e183f22014-07-18 14:57:04 -0700482 return kUpToDate;
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700483 }
484 }
485
Alex Light6e183f22014-07-18 14:57:04 -0700486 bool force_system_only = false;
487 bool require_system_version = false;
Brian Carlstrome1ff1992014-05-18 22:37:51 -0700488
Dave Allison39c3bfb2014-01-28 18:33:52 -0800489 // Check the profile file. We need to rerun dex2oat if the profile has changed significantly
490 // since the last time, or it's new.
491 // If the 'defer' argument is true then this will be retried later. In this case we
492 // need to make sure that the profile file copy is not made so that we will get the
493 // same result second time.
Alex Light6e183f22014-07-18 14:57:04 -0700494 std::string profile_file;
495 std::string prev_profile_file;
496 bool should_copy_profile = false;
Calin Juravlec1b643c2014-05-30 23:44:11 +0100497 if (Runtime::Current()->GetProfilerOptions().IsEnabled() && (pkgname != nullptr)) {
Alex Light6e183f22014-07-18 14:57:04 -0700498 profile_file = GetDalvikCacheOrDie("profiles", false /* create_if_absent */)
Narayan Kamath11d9f062014-04-23 20:24:57 +0100499 + std::string("/") + pkgname;
Alex Light6e183f22014-07-18 14:57:04 -0700500 prev_profile_file = profile_file + std::string("@old");
Dave Allison39c3bfb2014-01-28 18:33:52 -0800501
502 struct stat profstat, prevstat;
503 int e1 = stat(profile_file.c_str(), &profstat);
Alex Light6e183f22014-07-18 14:57:04 -0700504 int e1_errno = errno;
Dave Allison39c3bfb2014-01-28 18:33:52 -0800505 int e2 = stat(prev_profile_file.c_str(), &prevstat);
Alex Light6e183f22014-07-18 14:57:04 -0700506 int e2_errno = errno;
Dave Allison39c3bfb2014-01-28 18:33:52 -0800507 if (e1 < 0) {
Alex Light6e183f22014-07-18 14:57:04 -0700508 if (e1_errno != EACCES) {
509 // No profile file, need to run dex2oat, unless we find a file in system
510 if (kReasonLogging) {
511 LOG(INFO) << "DexFile_isDexOptNeededInternal profile file " << profile_file << " doesn't exist. "
512 << "Will check odex to see if we can find a working version.";
513 }
514 // Force it to only accept system files/files with versions in system.
515 require_system_version = true;
516 } else {
517 LOG(INFO) << "DexFile_isDexOptNeededInternal recieved EACCES trying to stat profile file "
518 << profile_file;
Dave Allison39c3bfb2014-01-28 18:33:52 -0800519 }
Alex Light6e183f22014-07-18 14:57:04 -0700520 } else if (e2 == 0) {
Dave Allison39c3bfb2014-01-28 18:33:52 -0800521 // There is a previous profile file. Check if the profile has changed significantly.
Calin Juravle9dae5b42014-04-07 16:36:21 +0300522 // A change in profile is considered significant if X% (change_thr property) of the top K%
523 // (compile_thr property) samples has changed.
Calin Juravlec1b643c2014-05-30 23:44:11 +0100524 double top_k_threshold = Runtime::Current()->GetProfilerOptions().GetTopKThreshold();
525 double change_threshold = Runtime::Current()->GetProfilerOptions().GetTopKChangeThreshold();
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100526 double change_percent = 0.0;
527 ProfileFile new_profile, old_profile;
528 bool new_ok = new_profile.LoadFile(profile_file);
529 bool old_ok = old_profile.LoadFile(prev_profile_file);
530 if (!new_ok || !old_ok) {
Brian Carlstrom09881a82014-04-18 17:44:01 -0700531 if (kVerboseLogging) {
Alex Light6e183f22014-07-18 14:57:04 -0700532 LOG(INFO) << "DexFile_isDexOptNeededInternal Ignoring invalid profiles: "
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100533 << (new_ok ? "" : profile_file) << " " << (old_ok ? "" : prev_profile_file);
Calin Juravle9dae5b42014-04-07 16:36:21 +0300534 }
Calin Juravle9dae5b42014-04-07 16:36:21 +0300535 } else {
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100536 std::set<std::string> new_top_k, old_top_k;
537 new_profile.GetTopKSamples(new_top_k, top_k_threshold);
538 old_profile.GetTopKSamples(old_top_k, top_k_threshold);
539 if (new_top_k.empty()) {
540 if (kVerboseLogging) {
Alex Light6e183f22014-07-18 14:57:04 -0700541 LOG(INFO) << "DexFile_isDexOptNeededInternal empty profile: " << profile_file;
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100542 }
543 // If the new topK is empty we shouldn't optimize so we leave the change_percent at 0.0.
544 } else {
545 std::set<std::string> diff;
546 std::set_difference(new_top_k.begin(), new_top_k.end(), old_top_k.begin(), old_top_k.end(),
547 std::inserter(diff, diff.end()));
548 // TODO: consider using the usedPercentage instead of the plain diff count.
549 change_percent = 100.0 * static_cast<double>(diff.size()) / static_cast<double>(new_top_k.size());
550 if (kVerboseLogging) {
551 std::set<std::string>::iterator end = diff.end();
552 for (std::set<std::string>::iterator it = diff.begin(); it != end; it++) {
Alex Light6e183f22014-07-18 14:57:04 -0700553 LOG(INFO) << "DexFile_isDexOptNeededInternal new in topK: " << *it;
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100554 }
Calin Juravle9dae5b42014-04-07 16:36:21 +0300555 }
556 }
Dave Allison39c3bfb2014-01-28 18:33:52 -0800557 }
558
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100559 if (change_percent > change_threshold) {
Brian Carlstrom09881a82014-04-18 17:44:01 -0700560 if (kReasonLogging) {
Alex Light6e183f22014-07-18 14:57:04 -0700561 LOG(INFO) << "DexFile_isDexOptNeededInternal size of new profile file " << profile_file <<
Calin Juravle9dae5b42014-04-07 16:36:21 +0300562 " is significantly different from old profile file " << prev_profile_file << " (top "
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100563 << top_k_threshold << "% samples changed in proportion of " << change_percent << "%)";
Dave Allison39c3bfb2014-01-28 18:33:52 -0800564 }
Alex Light6e183f22014-07-18 14:57:04 -0700565 should_copy_profile = !defer;
566 // Force us to only accept system files.
567 force_system_only = true;
Dave Allison39c3bfb2014-01-28 18:33:52 -0800568 }
Alex Light6e183f22014-07-18 14:57:04 -0700569 } else if (e2_errno == ENOENT) {
Dave Allison39c3bfb2014-01-28 18:33:52 -0800570 // Previous profile does not exist. Make a copy of the current one.
Brian Carlstrom09881a82014-04-18 17:44:01 -0700571 if (kVerboseLogging) {
Alex Light6e183f22014-07-18 14:57:04 -0700572 LOG(INFO) << "DexFile_isDexOptNeededInternal previous profile doesn't exist: " << prev_profile_file;
Dave Allison39c3bfb2014-01-28 18:33:52 -0800573 }
Alex Light6e183f22014-07-18 14:57:04 -0700574 should_copy_profile = !defer;
575 } else {
576 PLOG(INFO) << "Unable to stat previous profile file " << prev_profile_file;
Dave Allison39c3bfb2014-01-28 18:33:52 -0800577 }
578 }
579
Alex Light6e183f22014-07-18 14:57:04 -0700580 const InstructionSet target_instruction_set = GetInstructionSetFromString(instruction_set);
Andreas Gampe20c89302014-08-19 17:28:06 -0700581 if (target_instruction_set == kNone) {
582 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
583 std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set));
584 env->ThrowNew(iae.get(), message.c_str());
585 return 0;
586 }
Alex Light6e183f22014-07-18 14:57:04 -0700587
588 // Get the filename for odex file next to the dex file.
589 std::string odex_filename(DexFilenameToOdexFilename(filename, target_instruction_set));
590 // Get the filename for the dalvik-cache file
591 std::string cache_dir;
592 bool have_android_data = false;
593 bool dalvik_cache_exists = false;
Andreas Gampe3c13a792014-09-18 20:56:04 -0700594 bool is_global_cache = false;
595 GetDalvikCache(instruction_set, false, &cache_dir, &have_android_data, &dalvik_cache_exists,
596 &is_global_cache);
Alex Light6e183f22014-07-18 14:57:04 -0700597 std::string cache_filename; // was cache_location
598 bool have_cache_filename = false;
599 if (dalvik_cache_exists) {
600 std::string error_msg;
601 have_cache_filename = GetDalvikCacheFilename(filename, cache_dir.c_str(), &cache_filename,
602 &error_msg);
603 if (!have_cache_filename && kVerboseLogging) {
604 LOG(INFO) << "DexFile_isDexOptNeededInternal failed to find cache file for dex file " << filename
605 << ": " << error_msg;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700606 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800607 }
608
Alex Light6e183f22014-07-18 14:57:04 -0700609 bool should_relocate_if_possible = Runtime::Current()->ShouldRelocate();
610
Alex Light6e183f22014-07-18 14:57:04 -0700611 jbyte dalvik_cache_decision = -1;
612 // Lets try the cache first (since we want to load from there since thats where the relocated
613 // versions will be).
614 if (have_cache_filename && !force_system_only) {
Igor Murashkin46774762014-10-22 11:37:02 -0700615 bool oat_is_pic;
Alex Light6e183f22014-07-18 14:57:04 -0700616 // We can use the dalvik-cache if we find a good file.
617 dalvik_cache_decision =
Narayan Kamath202d1f02014-08-08 16:19:44 +0100618 IsDexOptNeededForFile<kVerboseLogging, kReasonLogging>(cache_filename, filename,
Igor Murashkin46774762014-10-22 11:37:02 -0700619 target_instruction_set, &oat_is_pic);
620
621 // Apps that are compiled with --compile-pic never need to be patchoat-d
622 if (oat_is_pic && dalvik_cache_decision == kPatchoatNeeded) {
623 dalvik_cache_decision = kUpToDate;
624 }
Alex Light6e183f22014-07-18 14:57:04 -0700625 // We will only return DexOptNeeded if both the cache and system return it.
626 if (dalvik_cache_decision != kDexoptNeeded && !require_system_version) {
627 CHECK(!(dalvik_cache_decision == kPatchoatNeeded && !should_relocate_if_possible))
628 << "May not return PatchoatNeeded when patching is disabled.";
629 return dalvik_cache_decision;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700630 }
Alex Light6e183f22014-07-18 14:57:04 -0700631 // We couldn't find one thats easy. We should now try the system.
Brian Carlstroma004aa92012-02-08 18:05:09 -0800632 }
633
Igor Murashkin46774762014-10-22 11:37:02 -0700634 bool oat_is_pic;
Alex Light6e183f22014-07-18 14:57:04 -0700635 jbyte system_decision =
Narayan Kamath202d1f02014-08-08 16:19:44 +0100636 IsDexOptNeededForFile<kVerboseLogging, kReasonLogging>(odex_filename, filename,
Igor Murashkin46774762014-10-22 11:37:02 -0700637 target_instruction_set, &oat_is_pic);
Alex Light6e183f22014-07-18 14:57:04 -0700638 CHECK(!(system_decision == kPatchoatNeeded && !should_relocate_if_possible))
639 << "May not return PatchoatNeeded when patching is disabled.";
640
Igor Murashkin46774762014-10-22 11:37:02 -0700641 // Apps that are compiled with --compile-pic never need to be patchoat-d
642 if (oat_is_pic && system_decision == kPatchoatNeeded) {
643 system_decision = kUpToDate;
644 }
645
Alex Light6e183f22014-07-18 14:57:04 -0700646 if (require_system_version && system_decision == kPatchoatNeeded
647 && dalvik_cache_decision == kUpToDate) {
648 // We have a version from system relocated to the cache. Return it.
649 return dalvik_cache_decision;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800650 }
651
Alex Light6e183f22014-07-18 14:57:04 -0700652 if (should_copy_profile && system_decision == kDexoptNeeded) {
653 CopyProfileFile(profile_file.c_str(), prev_profile_file.c_str());
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800654 }
Alex Light6e183f22014-07-18 14:57:04 -0700655
656 return system_decision;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700657}
658
Alex Light6e183f22014-07-18 14:57:04 -0700659static jbyte DexFile_isDexOptNeededInternal(JNIEnv* env, jclass, jstring javaFilename,
Narayan Kamath11d9f062014-04-23 20:24:57 +0100660 jstring javaPkgname, jstring javaInstructionSet, jboolean defer) {
661 ScopedUtfChars filename(env, javaFilename);
Andreas Gampe20c89302014-08-19 17:28:06 -0700662 if (env->ExceptionCheck()) {
663 return 0;
664 }
665
Narayan Kamath11d9f062014-04-23 20:24:57 +0100666 NullableScopedUtfChars pkgname(env, javaPkgname);
Andreas Gampe20c89302014-08-19 17:28:06 -0700667
Narayan Kamath11d9f062014-04-23 20:24:57 +0100668 ScopedUtfChars instruction_set(env, javaInstructionSet);
Andreas Gampe20c89302014-08-19 17:28:06 -0700669 if (env->ExceptionCheck()) {
670 return 0;
671 }
Narayan Kamath11d9f062014-04-23 20:24:57 +0100672
673 return IsDexOptNeededInternal(env, filename.c_str(), pkgname.c_str(),
674 instruction_set.c_str(), defer);
675}
676
Dave Allison39c3bfb2014-01-28 18:33:52 -0800677// public API, NULL pkgname
Narayan Kamath11d9f062014-04-23 20:24:57 +0100678static jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
679 const char* instruction_set = GetInstructionSetString(kRuntimeISA);
680 ScopedUtfChars filename(env, javaFilename);
Alex Light6e183f22014-07-18 14:57:04 -0700681 return kUpToDate != IsDexOptNeededInternal(env, filename.c_str(), nullptr /* pkgname */,
682 instruction_set, false /* defer */);
Dave Allison39c3bfb2014-01-28 18:33:52 -0800683}
684
685
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700686static JNINativeMethod gMethods[] = {
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800687 NATIVE_METHOD(DexFile, closeDexFile, "(Ljava/lang/Object;)V"),
688 NATIVE_METHOD(DexFile, defineClassNative, "(Ljava/lang/String;Ljava/lang/ClassLoader;Ljava/lang/Object;)Ljava/lang/Class;"),
689 NATIVE_METHOD(DexFile, getClassNameList, "(Ljava/lang/Object;)[Ljava/lang/String;"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700690 NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
Alex Light6e183f22014-07-18 14:57:04 -0700691 NATIVE_METHOD(DexFile, isDexOptNeededInternal, "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Z)B"),
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800692 NATIVE_METHOD(DexFile, openDexFileNative, "(Ljava/lang/String;Ljava/lang/String;I)Ljava/lang/Object;"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700693};
694
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700695void register_dalvik_system_DexFile(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700696 REGISTER_NATIVE_METHODS("dalvik/system/DexFile");
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700697}
698
699} // namespace art