Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 1 | /* |
| 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 Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 17 | #include "dalvik_system_DexFile.h" |
| 18 | |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 19 | #include <algorithm> |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 20 | #include <set> |
Ian Rogers | dd157d7 | 2014-05-15 14:47:50 -0700 | [diff] [blame] | 21 | #include <fcntl.h> |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 22 | #ifdef __linux__ |
Calin Juravle | 5221410 | 2014-06-04 12:01:50 +0100 | [diff] [blame] | 23 | #include <sys/sendfile.h> |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 24 | #else |
| 25 | #include <sys/socket.h> |
| 26 | #endif |
Calin Juravle | 5221410 | 2014-06-04 12:01:50 +0100 | [diff] [blame] | 27 | #include <sys/stat.h> |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 28 | #include <unistd.h> |
Brian Carlstrom | 1d9f52b | 2011-10-13 10:50:45 -0700 | [diff] [blame] | 29 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 30 | #include "base/logging.h" |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 31 | #include "base/stl_util.h" |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 32 | #include "base/stringprintf.h" |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 33 | #include "class_linker.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 34 | #include "common_throws.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 35 | #include "dex_file-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 36 | #include "gc/space/image_space.h" |
| 37 | #include "gc/space/space-inl.h" |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 38 | #include "image.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 39 | #include "jni_internal.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 40 | #include "mirror/class_loader.h" |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 41 | #include "mirror/object-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 42 | #include "mirror/string.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 43 | #include "oat.h" |
Brian Carlstrom | 1d9f52b | 2011-10-13 10:50:45 -0700 | [diff] [blame] | 44 | #include "os.h" |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 45 | #include "profiler.h" |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 46 | #include "runtime.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 47 | #include "scoped_thread_state_change.h" |
Ian Rogers | c981848 | 2012-01-11 08:52:51 -0800 | [diff] [blame] | 48 | #include "ScopedLocalRef.h" |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 49 | #include "ScopedUtfChars.h" |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 50 | #include "utils.h" |
Ian Rogers | dd157d7 | 2014-05-15 14:47:50 -0700 | [diff] [blame] | 51 | #include "well_known_classes.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 52 | #include "zip_archive.h" |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 53 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 54 | #pragma GCC diagnostic push |
| 55 | #pragma GCC diagnostic ignored "-Wshadow" |
| 56 | #include "ScopedFd.h" |
| 57 | #pragma GCC diagnostic pop |
| 58 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 59 | namespace art { |
| 60 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 61 | // 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 Carlstrom | c252c3e | 2011-10-16 23:21:02 -0700 | [diff] [blame] | 66 | // if (env->ExceptionCheck()) { |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 67 | // return NULL; |
| 68 | // } |
| 69 | // // ... use name.c_str() |
| 70 | // |
| 71 | // TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option. |
| 72 | class NullableScopedUtfChars { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 73 | 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 Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 81 | } |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 82 | } |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 83 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 84 | const char* c_str() const { |
| 85 | return mUtfChars; |
| 86 | } |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 87 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 88 | size_t size() const { |
| 89 | return strlen(mUtfChars); |
| 90 | } |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 91 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 92 | // Element access. |
| 93 | const char& operator[](size_t n) const { |
| 94 | return mUtfChars[n]; |
| 95 | } |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 96 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 97 | private: |
| 98 | JNIEnv* mEnv; |
| 99 | jstring mString; |
| 100 | const char* mUtfChars; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 101 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 102 | // Disallow copy and assignment. |
| 103 | NullableScopedUtfChars(const NullableScopedUtfChars&); |
| 104 | void operator=(const NullableScopedUtfChars&); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
Calin Juravle | a2069c7 | 2014-08-06 19:07:41 +0000 | [diff] [blame] | 107 | static jlong DexFile_openDexFileNative(JNIEnv* env, jclass, jstring javaSourceName, jstring javaOutputName, jint) { |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 108 | ScopedUtfChars sourceName(env, javaSourceName); |
| 109 | if (sourceName.c_str() == NULL) { |
| 110 | return 0; |
| 111 | } |
| 112 | NullableScopedUtfChars outputName(env, javaOutputName); |
Brian Carlstrom | c252c3e | 2011-10-16 23:21:02 -0700 | [diff] [blame] | 113 | if (env->ExceptionCheck()) { |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 114 | return 0; |
| 115 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 116 | |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 117 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 118 | std::unique_ptr<std::vector<const DexFile*>> dex_files(new std::vector<const DexFile*>()); |
| 119 | std::vector<std::string> error_msgs; |
| 120 | |
| 121 | bool success = linker->OpenDexFilesFromOat(sourceName.c_str(), outputName.c_str(), &error_msgs, |
| 122 | dex_files.get()); |
| 123 | |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 124 | if (success || !dex_files->empty()) { |
| 125 | // In the case of non-success, we have not found or could not generate the oat file. |
| 126 | // But we may still have found a dex file that we can use. |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 127 | return static_cast<jlong>(reinterpret_cast<uintptr_t>(dex_files.release())); |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 128 | } else { |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 129 | // The vector should be empty after a failed loading attempt. |
| 130 | DCHECK_EQ(0U, dex_files->size()); |
| 131 | |
Vladimir Marko | 60836d5 | 2014-01-16 15:53:38 +0000 | [diff] [blame] | 132 | ScopedObjectAccess soa(env); |
Andreas Gampe | 329d188 | 2014-04-08 10:32:19 -0700 | [diff] [blame] | 133 | CHECK(!error_msgs.empty()); |
| 134 | // The most important message is at the end. So set up nesting by going forward, which will |
| 135 | // wrap the existing exception as a cause for the following one. |
| 136 | auto it = error_msgs.begin(); |
| 137 | auto itEnd = error_msgs.end(); |
| 138 | for ( ; it != itEnd; ++it) { |
| 139 | ThrowWrappedIOException("%s", it->c_str()); |
| 140 | } |
| 141 | |
jeffhao | c393a4f | 2011-10-19 13:46:09 -0700 | [diff] [blame] | 142 | return 0; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 143 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 146 | static std::vector<const DexFile*>* toDexFiles(jlong dex_file_address, JNIEnv* env) { |
| 147 | std::vector<const DexFile*>* dex_files = reinterpret_cast<std::vector<const DexFile*>*>( |
| 148 | static_cast<uintptr_t>(dex_file_address)); |
| 149 | if (UNLIKELY(dex_files == nullptr)) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 150 | ScopedObjectAccess soa(env); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 151 | ThrowNullPointerException(NULL, "dex_file == null"); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 152 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 153 | return dex_files; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Elliott Hughes | 2d98390 | 2014-02-04 16:17:13 -0800 | [diff] [blame] | 156 | static void DexFile_closeDexFile(JNIEnv* env, jclass, jlong cookie) { |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 157 | std::unique_ptr<std::vector<const DexFile*>> dex_files(toDexFiles(cookie, env)); |
| 158 | if (dex_files.get() == nullptr) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 159 | return; |
| 160 | } |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 161 | ScopedObjectAccess soa(env); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 162 | |
| 163 | size_t index = 0; |
| 164 | for (const DexFile* dex_file : *dex_files) { |
| 165 | if (Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) { |
| 166 | (*dex_files)[index] = nullptr; |
| 167 | } |
| 168 | index++; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 169 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 170 | |
| 171 | STLDeleteElements(dex_files.get()); |
| 172 | // Unique_ptr will delete the vector itself. |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 175 | static jclass DexFile_defineClassNative(JNIEnv* env, jclass, jstring javaName, jobject javaLoader, |
Elliott Hughes | 2d98390 | 2014-02-04 16:17:13 -0800 | [diff] [blame] | 176 | jlong cookie) { |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 177 | std::vector<const DexFile*>* dex_files = toDexFiles(cookie, env); |
| 178 | if (dex_files == NULL) { |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 179 | VLOG(class_linker) << "Failed to find dex_file"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 180 | return NULL; |
| 181 | } |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 182 | ScopedUtfChars class_name(env, javaName); |
| 183 | if (class_name.c_str() == NULL) { |
Brian Carlstrom | 2e450bf | 2013-09-06 15:39:46 -0700 | [diff] [blame] | 184 | VLOG(class_linker) << "Failed to find class_name"; |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 185 | return NULL; |
| 186 | } |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 187 | const std::string descriptor(DotToDescriptor(class_name.c_str())); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 188 | const size_t hash(ComputeModifiedUtf8Hash(descriptor.c_str())); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 189 | for (const DexFile* dex_file : *dex_files) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 190 | const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor.c_str(), hash); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 191 | if (dex_class_def != nullptr) { |
| 192 | ScopedObjectAccess soa(env); |
| 193 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 194 | class_linker->RegisterDexFile(*dex_file); |
| 195 | StackHandleScope<1> hs(soa.Self()); |
| 196 | Handle<mirror::ClassLoader> class_loader( |
| 197 | hs.NewHandle(soa.Decode<mirror::ClassLoader*>(javaLoader))); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 198 | mirror::Class* result = class_linker->DefineClass(soa.Self(), descriptor.c_str(), hash, |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 199 | class_loader, *dex_file, *dex_class_def); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 200 | if (result != nullptr) { |
Brian Carlstrom | 667ab7c | 2014-10-16 19:12:28 -0700 | [diff] [blame] | 201 | VLOG(class_linker) << "DexFile_defineClassNative returning " << result |
| 202 | << " for " << class_name.c_str(); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 203 | return soa.AddLocalReference<jclass>(result); |
| 204 | } |
| 205 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 206 | } |
Brian Carlstrom | 667ab7c | 2014-10-16 19:12:28 -0700 | [diff] [blame] | 207 | VLOG(class_linker) << "Failed to find dex_class_def " << class_name.c_str(); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 208 | return nullptr; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 211 | // Needed as a compare functor for sets of const char |
| 212 | struct CharPointerComparator { |
| 213 | bool operator()(const char *str1, const char *str2) const { |
| 214 | return strcmp(str1, str2) < 0; |
| 215 | } |
| 216 | }; |
| 217 | |
| 218 | // Note: this can be an expensive call, as we sort out duplicates in MultiDex files. |
Elliott Hughes | 2d98390 | 2014-02-04 16:17:13 -0800 | [diff] [blame] | 219 | static jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jlong cookie) { |
Ian Rogers | dd157d7 | 2014-05-15 14:47:50 -0700 | [diff] [blame] | 220 | jobjectArray result = nullptr; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 221 | std::vector<const DexFile*>* dex_files = toDexFiles(cookie, env); |
| 222 | |
| 223 | if (dex_files != nullptr) { |
| 224 | // Push all class descriptors into a set. Use set instead of unordered_set as we want to |
| 225 | // retrieve all in the end. |
| 226 | std::set<const char*, CharPointerComparator> descriptors; |
| 227 | for (const DexFile* dex_file : *dex_files) { |
Ian Rogers | dd157d7 | 2014-05-15 14:47:50 -0700 | [diff] [blame] | 228 | for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) { |
| 229 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(i); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 230 | const char* descriptor = dex_file->GetClassDescriptor(class_def); |
| 231 | descriptors.insert(descriptor); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | // Now create output array and copy the set into it. |
| 236 | result = env->NewObjectArray(descriptors.size(), WellKnownClasses::java_lang_String, nullptr); |
| 237 | if (result != nullptr) { |
| 238 | auto it = descriptors.begin(); |
| 239 | auto it_end = descriptors.end(); |
| 240 | jsize i = 0; |
| 241 | for (; it != it_end; it++, ++i) { |
| 242 | std::string descriptor(DescriptorToDot(*it)); |
Brian Carlstrom | cf790bb | 2014-05-28 11:09:10 -0700 | [diff] [blame] | 243 | ScopedLocalRef<jstring> jdescriptor(env, env->NewStringUTF(descriptor.c_str())); |
Ian Rogers | dd157d7 | 2014-05-15 14:47:50 -0700 | [diff] [blame] | 244 | if (jdescriptor.get() == nullptr) { |
| 245 | return nullptr; |
| 246 | } |
| 247 | env->SetObjectArrayElement(result, i, jdescriptor.get()); |
| 248 | } |
| 249 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 250 | } |
Ian Rogers | dd157d7 | 2014-05-15 14:47:50 -0700 | [diff] [blame] | 251 | return result; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 254 | static void CopyProfileFile(const char* oldfile, const char* newfile) { |
Calin Juravle | 5221410 | 2014-06-04 12:01:50 +0100 | [diff] [blame] | 255 | ScopedFd src(open(oldfile, O_RDONLY)); |
| 256 | if (src.get() == -1) { |
| 257 | PLOG(ERROR) << "Failed to open profile file " << oldfile |
| 258 | << ". My uid:gid is " << getuid() << ":" << getgid(); |
| 259 | return; |
| 260 | } |
| 261 | |
| 262 | struct stat stat_src; |
| 263 | if (fstat(src.get(), &stat_src) == -1) { |
| 264 | PLOG(ERROR) << "Failed to get stats for profile file " << oldfile |
| 265 | << ". My uid:gid is " << getuid() << ":" << getgid(); |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 266 | return; |
| 267 | } |
| 268 | |
| 269 | // Create the copy with rw------- (only accessible by system) |
Calin Juravle | 5221410 | 2014-06-04 12:01:50 +0100 | [diff] [blame] | 270 | ScopedFd dst(open(newfile, O_WRONLY|O_CREAT|O_TRUNC, 0600)); |
| 271 | if (dst.get() == -1) { |
| 272 | PLOG(ERROR) << "Failed to create/write prev profile file " << newfile |
| 273 | << ". My uid:gid is " << getuid() << ":" << getgid(); |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 274 | return; |
| 275 | } |
Calin Juravle | 5221410 | 2014-06-04 12:01:50 +0100 | [diff] [blame] | 276 | |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 277 | #ifdef __linux__ |
Calin Juravle | 5221410 | 2014-06-04 12:01:50 +0100 | [diff] [blame] | 278 | if (sendfile(dst.get(), src.get(), nullptr, stat_src.st_size) == -1) { |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 279 | #else |
| 280 | off_t len; |
| 281 | if (sendfile(dst.get(), src.get(), 0, &len, nullptr, 0) == -1) { |
| 282 | #endif |
Calin Juravle | 5221410 | 2014-06-04 12:01:50 +0100 | [diff] [blame] | 283 | PLOG(ERROR) << "Failed to copy profile file " << oldfile << " to " << newfile |
| 284 | << ". My uid:gid is " << getuid() << ":" << getgid(); |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 285 | } |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 286 | } |
| 287 | |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 288 | // Java: dalvik.system.DexFile.UP_TO_DATE |
| 289 | static const jbyte kUpToDate = 0; |
| 290 | // Java: dalvik.system.DexFile.DEXOPT_NEEDED |
| 291 | static const jbyte kPatchoatNeeded = 1; |
| 292 | // Java: dalvik.system.DexFile.PATCHOAT_NEEDED |
| 293 | static const jbyte kDexoptNeeded = 2; |
| 294 | |
| 295 | template <const bool kVerboseLogging, const bool kReasonLogging> |
| 296 | static jbyte IsDexOptNeededForFile(const std::string& oat_filename, const char* filename, |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 297 | InstructionSet target_instruction_set, |
| 298 | bool* oat_is_pic) { |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 299 | std::string error_msg; |
| 300 | std::unique_ptr<const OatFile> oat_file(OatFile::Open(oat_filename, oat_filename, nullptr, |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 301 | nullptr, |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 302 | false, &error_msg)); |
| 303 | if (oat_file.get() == nullptr) { |
Brian Carlstrom | e8e3989 | 2014-09-23 13:53:28 -0700 | [diff] [blame] | 304 | if (kReasonLogging) { |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 305 | LOG(INFO) << "DexFile_isDexOptNeeded failed to open oat file '" << oat_filename |
| 306 | << "' for file location '" << filename << "': " << error_msg; |
| 307 | } |
| 308 | error_msg.clear(); |
| 309 | return kDexoptNeeded; |
| 310 | } |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 311 | |
| 312 | // Pass-up the information about if this is PIC. |
| 313 | // TODO: Refactor this function to be less complicated. |
| 314 | *oat_is_pic = oat_file->IsPic(); |
| 315 | |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 316 | bool should_relocate_if_possible = Runtime::Current()->ShouldRelocate(); |
| 317 | uint32_t location_checksum = 0; |
| 318 | const art::OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(filename, nullptr, |
| 319 | kReasonLogging); |
| 320 | if (oat_dex_file != nullptr) { |
| 321 | // If its not possible to read the classes.dex assume up-to-date as we won't be able to |
| 322 | // compile it anyway. |
| 323 | if (!DexFile::GetChecksum(filename, &location_checksum, &error_msg)) { |
| 324 | if (kVerboseLogging) { |
| 325 | LOG(INFO) << "DexFile_isDexOptNeeded found precompiled stripped file: " |
| 326 | << filename << " for " << oat_filename << ": " << error_msg; |
| 327 | } |
| 328 | if (ClassLinker::VerifyOatChecksums(oat_file.get(), target_instruction_set, &error_msg)) { |
| 329 | if (kVerboseLogging) { |
| 330 | LOG(INFO) << "DexFile_isDexOptNeeded file " << oat_filename |
| 331 | << " is up-to-date for " << filename; |
| 332 | } |
| 333 | return kUpToDate; |
| 334 | } else if (should_relocate_if_possible && |
| 335 | ClassLinker::VerifyOatImageChecksum(oat_file.get(), target_instruction_set)) { |
Brian Carlstrom | e8e3989 | 2014-09-23 13:53:28 -0700 | [diff] [blame] | 336 | if (kReasonLogging) { |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 337 | LOG(INFO) << "DexFile_isDexOptNeeded file " << oat_filename |
| 338 | << " needs to be relocated for " << filename; |
| 339 | } |
| 340 | return kPatchoatNeeded; |
| 341 | } else { |
Brian Carlstrom | e8e3989 | 2014-09-23 13:53:28 -0700 | [diff] [blame] | 342 | if (kReasonLogging) { |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 343 | LOG(INFO) << "DexFile_isDexOptNeeded file " << oat_filename |
| 344 | << " is out of date for " << filename; |
| 345 | } |
| 346 | return kDexoptNeeded; |
| 347 | } |
| 348 | // If we get here the file is out of date and we should use the system one to relocate. |
| 349 | } else { |
| 350 | if (ClassLinker::VerifyOatAndDexFileChecksums(oat_file.get(), filename, location_checksum, |
| 351 | target_instruction_set, &error_msg)) { |
| 352 | if (kVerboseLogging) { |
| 353 | LOG(INFO) << "DexFile_isDexOptNeeded file " << oat_filename |
| 354 | << " is up-to-date for " << filename; |
| 355 | } |
| 356 | return kUpToDate; |
| 357 | } else if (location_checksum == oat_dex_file->GetDexFileLocationChecksum() |
| 358 | && should_relocate_if_possible |
| 359 | && ClassLinker::VerifyOatImageChecksum(oat_file.get(), target_instruction_set)) { |
Brian Carlstrom | e8e3989 | 2014-09-23 13:53:28 -0700 | [diff] [blame] | 360 | if (kReasonLogging) { |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 361 | LOG(INFO) << "DexFile_isDexOptNeeded file " << oat_filename |
| 362 | << " needs to be relocated for " << filename; |
| 363 | } |
| 364 | return kPatchoatNeeded; |
| 365 | } else { |
Brian Carlstrom | e8e3989 | 2014-09-23 13:53:28 -0700 | [diff] [blame] | 366 | if (kReasonLogging) { |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 367 | LOG(INFO) << "DexFile_isDexOptNeeded file " << oat_filename |
| 368 | << " is out of date for " << filename; |
| 369 | } |
| 370 | return kDexoptNeeded; |
| 371 | } |
| 372 | } |
| 373 | } else { |
Brian Carlstrom | e8e3989 | 2014-09-23 13:53:28 -0700 | [diff] [blame] | 374 | if (kReasonLogging) { |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 375 | LOG(INFO) << "DexFile_isDexOptNeeded file " << oat_filename |
| 376 | << " does not contain " << filename; |
| 377 | } |
| 378 | return kDexoptNeeded; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | static jbyte IsDexOptNeededInternal(JNIEnv* env, const char* filename, |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 383 | const char* pkgname, const char* instruction_set, const jboolean defer) { |
Brian Carlstrom | e8e3989 | 2014-09-23 13:53:28 -0700 | [diff] [blame] | 384 | // Spammy logging for kUpToDate |
| 385 | const bool kVerboseLogging = false; |
| 386 | // Logging of reason for returning kDexoptNeeded or kPatchoatNeeded. |
| 387 | const bool kReasonLogging = true; |
Brian Carlstrom | bf2cb16 | 2012-02-27 17:49:19 -0800 | [diff] [blame] | 388 | |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 389 | if ((filename == nullptr) || !OS::FileExists(filename)) { |
| 390 | LOG(ERROR) << "DexFile_isDexOptNeeded file '" << filename << "' does not exist"; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 391 | ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException")); |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 392 | const char* message = (filename == nullptr) ? "<empty file name>" : filename; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 393 | env->ThrowNew(fnfe.get(), message); |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 394 | return kUpToDate; |
Brian Carlstrom | 1d9f52b | 2011-10-13 10:50:45 -0700 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | // Always treat elements of the bootclasspath as up-to-date. The |
| 398 | // fact that code is running at all means that this should be true. |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 399 | Runtime* runtime = Runtime::Current(); |
| 400 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 401 | // TODO: We're assuming that the 64 and 32 bit runtimes have identical |
| 402 | // class paths. isDexOptNeeded will not necessarily be called on a runtime |
| 403 | // that has the same instruction set as the file being dexopted. |
Brian Carlstrom | 1d9f52b | 2011-10-13 10:50:45 -0700 | [diff] [blame] | 404 | const std::vector<const DexFile*>& boot_class_path = class_linker->GetBootClassPath(); |
| 405 | for (size_t i = 0; i < boot_class_path.size(); i++) { |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 406 | if (boot_class_path[i]->GetLocation() == filename) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 407 | if (kVerboseLogging) { |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 408 | LOG(INFO) << "DexFile_isDexOptNeeded ignoring boot class path file: " << filename; |
Brian Carlstrom | bf2cb16 | 2012-02-27 17:49:19 -0800 | [diff] [blame] | 409 | } |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 410 | return kUpToDate; |
Brian Carlstrom | 1d9f52b | 2011-10-13 10:50:45 -0700 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 414 | bool force_system_only = false; |
| 415 | bool require_system_version = false; |
Brian Carlstrom | e1ff199 | 2014-05-18 22:37:51 -0700 | [diff] [blame] | 416 | |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 417 | // Check the profile file. We need to rerun dex2oat if the profile has changed significantly |
| 418 | // since the last time, or it's new. |
| 419 | // If the 'defer' argument is true then this will be retried later. In this case we |
| 420 | // need to make sure that the profile file copy is not made so that we will get the |
| 421 | // same result second time. |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 422 | std::string profile_file; |
| 423 | std::string prev_profile_file; |
| 424 | bool should_copy_profile = false; |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 425 | if (Runtime::Current()->GetProfilerOptions().IsEnabled() && (pkgname != nullptr)) { |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 426 | profile_file = GetDalvikCacheOrDie("profiles", false /* create_if_absent */) |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 427 | + std::string("/") + pkgname; |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 428 | prev_profile_file = profile_file + std::string("@old"); |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 429 | |
| 430 | struct stat profstat, prevstat; |
| 431 | int e1 = stat(profile_file.c_str(), &profstat); |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 432 | int e1_errno = errno; |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 433 | int e2 = stat(prev_profile_file.c_str(), &prevstat); |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 434 | int e2_errno = errno; |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 435 | if (e1 < 0) { |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 436 | if (e1_errno != EACCES) { |
| 437 | // No profile file, need to run dex2oat, unless we find a file in system |
| 438 | if (kReasonLogging) { |
| 439 | LOG(INFO) << "DexFile_isDexOptNeededInternal profile file " << profile_file << " doesn't exist. " |
| 440 | << "Will check odex to see if we can find a working version."; |
| 441 | } |
| 442 | // Force it to only accept system files/files with versions in system. |
| 443 | require_system_version = true; |
| 444 | } else { |
| 445 | LOG(INFO) << "DexFile_isDexOptNeededInternal recieved EACCES trying to stat profile file " |
| 446 | << profile_file; |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 447 | } |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 448 | } else if (e2 == 0) { |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 449 | // There is a previous profile file. Check if the profile has changed significantly. |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 450 | // A change in profile is considered significant if X% (change_thr property) of the top K% |
| 451 | // (compile_thr property) samples has changed. |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 452 | double top_k_threshold = Runtime::Current()->GetProfilerOptions().GetTopKThreshold(); |
| 453 | double change_threshold = Runtime::Current()->GetProfilerOptions().GetTopKChangeThreshold(); |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 454 | double change_percent = 0.0; |
| 455 | ProfileFile new_profile, old_profile; |
| 456 | bool new_ok = new_profile.LoadFile(profile_file); |
| 457 | bool old_ok = old_profile.LoadFile(prev_profile_file); |
| 458 | if (!new_ok || !old_ok) { |
Brian Carlstrom | 09881a8 | 2014-04-18 17:44:01 -0700 | [diff] [blame] | 459 | if (kVerboseLogging) { |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 460 | LOG(INFO) << "DexFile_isDexOptNeededInternal Ignoring invalid profiles: " |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 461 | << (new_ok ? "" : profile_file) << " " << (old_ok ? "" : prev_profile_file); |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 462 | } |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 463 | } else { |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 464 | std::set<std::string> new_top_k, old_top_k; |
| 465 | new_profile.GetTopKSamples(new_top_k, top_k_threshold); |
| 466 | old_profile.GetTopKSamples(old_top_k, top_k_threshold); |
| 467 | if (new_top_k.empty()) { |
| 468 | if (kVerboseLogging) { |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 469 | LOG(INFO) << "DexFile_isDexOptNeededInternal empty profile: " << profile_file; |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 470 | } |
| 471 | // If the new topK is empty we shouldn't optimize so we leave the change_percent at 0.0. |
| 472 | } else { |
| 473 | std::set<std::string> diff; |
| 474 | std::set_difference(new_top_k.begin(), new_top_k.end(), old_top_k.begin(), old_top_k.end(), |
| 475 | std::inserter(diff, diff.end())); |
| 476 | // TODO: consider using the usedPercentage instead of the plain diff count. |
| 477 | change_percent = 100.0 * static_cast<double>(diff.size()) / static_cast<double>(new_top_k.size()); |
| 478 | if (kVerboseLogging) { |
| 479 | std::set<std::string>::iterator end = diff.end(); |
| 480 | for (std::set<std::string>::iterator it = diff.begin(); it != end; it++) { |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 481 | LOG(INFO) << "DexFile_isDexOptNeededInternal new in topK: " << *it; |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 482 | } |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 483 | } |
| 484 | } |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 485 | } |
| 486 | |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 487 | if (change_percent > change_threshold) { |
Brian Carlstrom | 09881a8 | 2014-04-18 17:44:01 -0700 | [diff] [blame] | 488 | if (kReasonLogging) { |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 489 | LOG(INFO) << "DexFile_isDexOptNeededInternal size of new profile file " << profile_file << |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 490 | " is significantly different from old profile file " << prev_profile_file << " (top " |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 491 | << top_k_threshold << "% samples changed in proportion of " << change_percent << "%)"; |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 492 | } |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 493 | should_copy_profile = !defer; |
| 494 | // Force us to only accept system files. |
| 495 | force_system_only = true; |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 496 | } |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 497 | } else if (e2_errno == ENOENT) { |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 498 | // Previous profile does not exist. Make a copy of the current one. |
Brian Carlstrom | 09881a8 | 2014-04-18 17:44:01 -0700 | [diff] [blame] | 499 | if (kVerboseLogging) { |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 500 | LOG(INFO) << "DexFile_isDexOptNeededInternal previous profile doesn't exist: " << prev_profile_file; |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 501 | } |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 502 | should_copy_profile = !defer; |
| 503 | } else { |
| 504 | PLOG(INFO) << "Unable to stat previous profile file " << prev_profile_file; |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 508 | const InstructionSet target_instruction_set = GetInstructionSetFromString(instruction_set); |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 509 | if (target_instruction_set == kNone) { |
| 510 | ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException")); |
| 511 | std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set)); |
| 512 | env->ThrowNew(iae.get(), message.c_str()); |
| 513 | return 0; |
| 514 | } |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 515 | |
| 516 | // Get the filename for odex file next to the dex file. |
| 517 | std::string odex_filename(DexFilenameToOdexFilename(filename, target_instruction_set)); |
| 518 | // Get the filename for the dalvik-cache file |
| 519 | std::string cache_dir; |
| 520 | bool have_android_data = false; |
| 521 | bool dalvik_cache_exists = false; |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 522 | bool is_global_cache = false; |
| 523 | GetDalvikCache(instruction_set, false, &cache_dir, &have_android_data, &dalvik_cache_exists, |
| 524 | &is_global_cache); |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 525 | std::string cache_filename; // was cache_location |
| 526 | bool have_cache_filename = false; |
| 527 | if (dalvik_cache_exists) { |
| 528 | std::string error_msg; |
| 529 | have_cache_filename = GetDalvikCacheFilename(filename, cache_dir.c_str(), &cache_filename, |
| 530 | &error_msg); |
| 531 | if (!have_cache_filename && kVerboseLogging) { |
| 532 | LOG(INFO) << "DexFile_isDexOptNeededInternal failed to find cache file for dex file " << filename |
| 533 | << ": " << error_msg; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 534 | } |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 535 | } |
| 536 | |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 537 | bool should_relocate_if_possible = Runtime::Current()->ShouldRelocate(); |
| 538 | |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 539 | jbyte dalvik_cache_decision = -1; |
| 540 | // Lets try the cache first (since we want to load from there since thats where the relocated |
| 541 | // versions will be). |
| 542 | if (have_cache_filename && !force_system_only) { |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 543 | bool oat_is_pic; |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 544 | // We can use the dalvik-cache if we find a good file. |
| 545 | dalvik_cache_decision = |
Narayan Kamath | 202d1f0 | 2014-08-08 16:19:44 +0100 | [diff] [blame] | 546 | IsDexOptNeededForFile<kVerboseLogging, kReasonLogging>(cache_filename, filename, |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 547 | target_instruction_set, &oat_is_pic); |
| 548 | |
| 549 | // Apps that are compiled with --compile-pic never need to be patchoat-d |
| 550 | if (oat_is_pic && dalvik_cache_decision == kPatchoatNeeded) { |
| 551 | dalvik_cache_decision = kUpToDate; |
| 552 | } |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 553 | // We will only return DexOptNeeded if both the cache and system return it. |
| 554 | if (dalvik_cache_decision != kDexoptNeeded && !require_system_version) { |
| 555 | CHECK(!(dalvik_cache_decision == kPatchoatNeeded && !should_relocate_if_possible)) |
| 556 | << "May not return PatchoatNeeded when patching is disabled."; |
| 557 | return dalvik_cache_decision; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 558 | } |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 559 | // We couldn't find one thats easy. We should now try the system. |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 560 | } |
| 561 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 562 | bool oat_is_pic; |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 563 | jbyte system_decision = |
Narayan Kamath | 202d1f0 | 2014-08-08 16:19:44 +0100 | [diff] [blame] | 564 | IsDexOptNeededForFile<kVerboseLogging, kReasonLogging>(odex_filename, filename, |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 565 | target_instruction_set, &oat_is_pic); |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 566 | CHECK(!(system_decision == kPatchoatNeeded && !should_relocate_if_possible)) |
| 567 | << "May not return PatchoatNeeded when patching is disabled."; |
| 568 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 569 | // Apps that are compiled with --compile-pic never need to be patchoat-d |
| 570 | if (oat_is_pic && system_decision == kPatchoatNeeded) { |
| 571 | system_decision = kUpToDate; |
| 572 | } |
| 573 | |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 574 | if (require_system_version && system_decision == kPatchoatNeeded |
| 575 | && dalvik_cache_decision == kUpToDate) { |
| 576 | // We have a version from system relocated to the cache. Return it. |
| 577 | return dalvik_cache_decision; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 578 | } |
| 579 | |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 580 | if (should_copy_profile && system_decision == kDexoptNeeded) { |
| 581 | CopyProfileFile(profile_file.c_str(), prev_profile_file.c_str()); |
Brian Carlstrom | bf2cb16 | 2012-02-27 17:49:19 -0800 | [diff] [blame] | 582 | } |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 583 | |
| 584 | return system_decision; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 585 | } |
| 586 | |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 587 | static jbyte DexFile_isDexOptNeededInternal(JNIEnv* env, jclass, jstring javaFilename, |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 588 | jstring javaPkgname, jstring javaInstructionSet, jboolean defer) { |
| 589 | ScopedUtfChars filename(env, javaFilename); |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 590 | if (env->ExceptionCheck()) { |
| 591 | return 0; |
| 592 | } |
| 593 | |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 594 | NullableScopedUtfChars pkgname(env, javaPkgname); |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 595 | |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 596 | ScopedUtfChars instruction_set(env, javaInstructionSet); |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 597 | if (env->ExceptionCheck()) { |
| 598 | return 0; |
| 599 | } |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 600 | |
| 601 | return IsDexOptNeededInternal(env, filename.c_str(), pkgname.c_str(), |
| 602 | instruction_set.c_str(), defer); |
| 603 | } |
| 604 | |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 605 | // public API, NULL pkgname |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 606 | static jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) { |
| 607 | const char* instruction_set = GetInstructionSetString(kRuntimeISA); |
| 608 | ScopedUtfChars filename(env, javaFilename); |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 609 | return kUpToDate != IsDexOptNeededInternal(env, filename.c_str(), nullptr /* pkgname */, |
| 610 | instruction_set, false /* defer */); |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 614 | static JNINativeMethod gMethods[] = { |
Elliott Hughes | 2d98390 | 2014-02-04 16:17:13 -0800 | [diff] [blame] | 615 | NATIVE_METHOD(DexFile, closeDexFile, "(J)V"), |
| 616 | NATIVE_METHOD(DexFile, defineClassNative, "(Ljava/lang/String;Ljava/lang/ClassLoader;J)Ljava/lang/Class;"), |
| 617 | NATIVE_METHOD(DexFile, getClassNameList, "(J)[Ljava/lang/String;"), |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 618 | NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"), |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 619 | NATIVE_METHOD(DexFile, isDexOptNeededInternal, "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Z)B"), |
Calin Juravle | a2069c7 | 2014-08-06 19:07:41 +0000 | [diff] [blame] | 620 | NATIVE_METHOD(DexFile, openDexFileNative, "(Ljava/lang/String;Ljava/lang/String;I)J"), |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 621 | }; |
| 622 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 623 | void register_dalvik_system_DexFile(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 624 | REGISTER_NATIVE_METHODS("dalvik/system/DexFile"); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | } // namespace art |