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 | |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 17 | #include <algorithm> |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 18 | #include <set> |
Ian Rogers | dd157d7 | 2014-05-15 14:47:50 -0700 | [diff] [blame] | 19 | #include <fcntl.h> |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 20 | #ifdef __linux__ |
Calin Juravle | 5221410 | 2014-06-04 12:01:50 +0100 | [diff] [blame] | 21 | #include <sys/sendfile.h> |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 22 | #else |
| 23 | #include <sys/socket.h> |
| 24 | #endif |
Calin Juravle | 5221410 | 2014-06-04 12:01:50 +0100 | [diff] [blame] | 25 | #include <sys/stat.h> |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 26 | #include <unistd.h> |
Brian Carlstrom | 1d9f52b | 2011-10-13 10:50:45 -0700 | [diff] [blame] | 27 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 28 | #include "base/logging.h" |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 29 | #include "base/stl_util.h" |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 30 | #include "class_linker.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 31 | #include "common_throws.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 32 | #include "dex_file-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 33 | #include "gc/space/image_space.h" |
| 34 | #include "gc/space/space-inl.h" |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 35 | #include "image.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 36 | #include "jni_internal.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 37 | #include "mirror/class_loader.h" |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 38 | #include "mirror/object-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 39 | #include "mirror/string.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 40 | #include "oat.h" |
Brian Carlstrom | 1d9f52b | 2011-10-13 10:50:45 -0700 | [diff] [blame] | 41 | #include "os.h" |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 42 | #include "profiler.h" |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 43 | #include "runtime.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 44 | #include "scoped_thread_state_change.h" |
Calin Juravle | 177b429 | 2014-06-03 16:30:39 +0100 | [diff] [blame] | 45 | #include "ScopedFd.h" |
Ian Rogers | c981848 | 2012-01-11 08:52:51 -0800 | [diff] [blame] | 46 | #include "ScopedLocalRef.h" |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 47 | #include "ScopedUtfChars.h" |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 48 | #include "utils.h" |
Ian Rogers | dd157d7 | 2014-05-15 14:47:50 -0700 | [diff] [blame] | 49 | #include "well_known_classes.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 50 | #include "zip_archive.h" |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 51 | |
| 52 | namespace art { |
| 53 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 54 | // A smart pointer that provides read-only access to a Java string's UTF chars. |
| 55 | // Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if |
| 56 | // passed a null jstring. The correct idiom is: |
| 57 | // |
| 58 | // NullableScopedUtfChars name(env, javaName); |
Brian Carlstrom | c252c3e | 2011-10-16 23:21:02 -0700 | [diff] [blame] | 59 | // if (env->ExceptionCheck()) { |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 60 | // return NULL; |
| 61 | // } |
| 62 | // // ... use name.c_str() |
| 63 | // |
| 64 | // TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option. |
| 65 | class NullableScopedUtfChars { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 66 | public: |
| 67 | NullableScopedUtfChars(JNIEnv* env, jstring s) : mEnv(env), mString(s) { |
| 68 | mUtfChars = (s != NULL) ? env->GetStringUTFChars(s, NULL) : NULL; |
| 69 | } |
| 70 | |
| 71 | ~NullableScopedUtfChars() { |
| 72 | if (mUtfChars) { |
| 73 | mEnv->ReleaseStringUTFChars(mString, mUtfChars); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 74 | } |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 75 | } |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 76 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 77 | const char* c_str() const { |
| 78 | return mUtfChars; |
| 79 | } |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 80 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 81 | size_t size() const { |
| 82 | return strlen(mUtfChars); |
| 83 | } |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 84 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 85 | // Element access. |
| 86 | const char& operator[](size_t n) const { |
| 87 | return mUtfChars[n]; |
| 88 | } |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 89 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 90 | private: |
| 91 | JNIEnv* mEnv; |
| 92 | jstring mString; |
| 93 | const char* mUtfChars; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 94 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 95 | // Disallow copy and assignment. |
| 96 | NullableScopedUtfChars(const NullableScopedUtfChars&); |
| 97 | void operator=(const NullableScopedUtfChars&); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
Elliott Hughes | 2d98390 | 2014-02-04 16:17:13 -0800 | [diff] [blame] | 100 | static jlong DexFile_openDexFileNative(JNIEnv* env, jclass, jstring javaSourceName, jstring javaOutputName, jint) { |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 101 | ScopedUtfChars sourceName(env, javaSourceName); |
| 102 | if (sourceName.c_str() == NULL) { |
| 103 | return 0; |
| 104 | } |
| 105 | NullableScopedUtfChars outputName(env, javaOutputName); |
Brian Carlstrom | c252c3e | 2011-10-16 23:21:02 -0700 | [diff] [blame] | 106 | if (env->ExceptionCheck()) { |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 107 | return 0; |
| 108 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 109 | |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 110 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 111 | std::unique_ptr<std::vector<const DexFile*>> dex_files(new std::vector<const DexFile*>()); |
| 112 | std::vector<std::string> error_msgs; |
| 113 | |
| 114 | bool success = linker->OpenDexFilesFromOat(sourceName.c_str(), outputName.c_str(), &error_msgs, |
| 115 | dex_files.get()); |
| 116 | |
| 117 | if (success) { |
| 118 | return static_cast<jlong>(reinterpret_cast<uintptr_t>(dex_files.release())); |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 119 | } else { |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 120 | // The vector should be empty after a failed loading attempt. |
| 121 | DCHECK_EQ(0U, dex_files->size()); |
| 122 | |
Vladimir Marko | 60836d5 | 2014-01-16 15:53:38 +0000 | [diff] [blame] | 123 | ScopedObjectAccess soa(env); |
Andreas Gampe | 329d188 | 2014-04-08 10:32:19 -0700 | [diff] [blame] | 124 | CHECK(!error_msgs.empty()); |
| 125 | // The most important message is at the end. So set up nesting by going forward, which will |
| 126 | // wrap the existing exception as a cause for the following one. |
| 127 | auto it = error_msgs.begin(); |
| 128 | auto itEnd = error_msgs.end(); |
| 129 | for ( ; it != itEnd; ++it) { |
| 130 | ThrowWrappedIOException("%s", it->c_str()); |
| 131 | } |
| 132 | |
jeffhao | c393a4f | 2011-10-19 13:46:09 -0700 | [diff] [blame] | 133 | return 0; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 134 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 137 | static std::vector<const DexFile*>* toDexFiles(jlong dex_file_address, JNIEnv* env) { |
| 138 | std::vector<const DexFile*>* dex_files = reinterpret_cast<std::vector<const DexFile*>*>( |
| 139 | static_cast<uintptr_t>(dex_file_address)); |
| 140 | if (UNLIKELY(dex_files == nullptr)) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 141 | ScopedObjectAccess soa(env); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 142 | ThrowNullPointerException(NULL, "dex_file == null"); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 143 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 144 | return dex_files; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Elliott Hughes | 2d98390 | 2014-02-04 16:17:13 -0800 | [diff] [blame] | 147 | static void DexFile_closeDexFile(JNIEnv* env, jclass, jlong cookie) { |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 148 | std::unique_ptr<std::vector<const DexFile*>> dex_files(toDexFiles(cookie, env)); |
| 149 | if (dex_files.get() == nullptr) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 150 | return; |
| 151 | } |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 152 | ScopedObjectAccess soa(env); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 153 | |
| 154 | size_t index = 0; |
| 155 | for (const DexFile* dex_file : *dex_files) { |
| 156 | if (Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) { |
| 157 | (*dex_files)[index] = nullptr; |
| 158 | } |
| 159 | index++; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 160 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 161 | |
| 162 | STLDeleteElements(dex_files.get()); |
| 163 | // Unique_ptr will delete the vector itself. |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 166 | static jclass DexFile_defineClassNative(JNIEnv* env, jclass, jstring javaName, jobject javaLoader, |
Elliott Hughes | 2d98390 | 2014-02-04 16:17:13 -0800 | [diff] [blame] | 167 | jlong cookie) { |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 168 | std::vector<const DexFile*>* dex_files = toDexFiles(cookie, env); |
| 169 | if (dex_files == NULL) { |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 170 | VLOG(class_linker) << "Failed to find dex_file"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 171 | return NULL; |
| 172 | } |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 173 | ScopedUtfChars class_name(env, javaName); |
| 174 | if (class_name.c_str() == NULL) { |
Brian Carlstrom | 2e450bf | 2013-09-06 15:39:46 -0700 | [diff] [blame] | 175 | VLOG(class_linker) << "Failed to find class_name"; |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 176 | return NULL; |
| 177 | } |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 178 | const std::string descriptor(DotToDescriptor(class_name.c_str())); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 179 | |
| 180 | for (const DexFile* dex_file : *dex_files) { |
| 181 | const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor.c_str()); |
| 182 | if (dex_class_def != nullptr) { |
| 183 | ScopedObjectAccess soa(env); |
| 184 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 185 | class_linker->RegisterDexFile(*dex_file); |
| 186 | StackHandleScope<1> hs(soa.Self()); |
| 187 | Handle<mirror::ClassLoader> class_loader( |
| 188 | hs.NewHandle(soa.Decode<mirror::ClassLoader*>(javaLoader))); |
| 189 | mirror::Class* result = class_linker->DefineClass(descriptor.c_str(), class_loader, *dex_file, |
| 190 | *dex_class_def); |
| 191 | if (result != nullptr) { |
| 192 | VLOG(class_linker) << "DexFile_defineClassNative returning " << result; |
| 193 | return soa.AddLocalReference<jclass>(result); |
| 194 | } |
| 195 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 196 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 197 | VLOG(class_linker) << "Failed to find dex_class_def"; |
| 198 | return nullptr; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 201 | // Needed as a compare functor for sets of const char |
| 202 | struct CharPointerComparator { |
| 203 | bool operator()(const char *str1, const char *str2) const { |
| 204 | return strcmp(str1, str2) < 0; |
| 205 | } |
| 206 | }; |
| 207 | |
| 208 | // 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] | 209 | static jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jlong cookie) { |
Ian Rogers | dd157d7 | 2014-05-15 14:47:50 -0700 | [diff] [blame] | 210 | jobjectArray result = nullptr; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 211 | std::vector<const DexFile*>* dex_files = toDexFiles(cookie, env); |
| 212 | |
| 213 | if (dex_files != nullptr) { |
| 214 | // Push all class descriptors into a set. Use set instead of unordered_set as we want to |
| 215 | // retrieve all in the end. |
| 216 | std::set<const char*, CharPointerComparator> descriptors; |
| 217 | for (const DexFile* dex_file : *dex_files) { |
Ian Rogers | dd157d7 | 2014-05-15 14:47:50 -0700 | [diff] [blame] | 218 | for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) { |
| 219 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(i); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 220 | const char* descriptor = dex_file->GetClassDescriptor(class_def); |
| 221 | descriptors.insert(descriptor); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | // Now create output array and copy the set into it. |
| 226 | result = env->NewObjectArray(descriptors.size(), WellKnownClasses::java_lang_String, nullptr); |
| 227 | if (result != nullptr) { |
| 228 | auto it = descriptors.begin(); |
| 229 | auto it_end = descriptors.end(); |
| 230 | jsize i = 0; |
| 231 | for (; it != it_end; it++, ++i) { |
| 232 | std::string descriptor(DescriptorToDot(*it)); |
Brian Carlstrom | cf790bb | 2014-05-28 11:09:10 -0700 | [diff] [blame] | 233 | ScopedLocalRef<jstring> jdescriptor(env, env->NewStringUTF(descriptor.c_str())); |
Ian Rogers | dd157d7 | 2014-05-15 14:47:50 -0700 | [diff] [blame] | 234 | if (jdescriptor.get() == nullptr) { |
| 235 | return nullptr; |
| 236 | } |
| 237 | env->SetObjectArrayElement(result, i, jdescriptor.get()); |
| 238 | } |
| 239 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 240 | } |
Ian Rogers | dd157d7 | 2014-05-15 14:47:50 -0700 | [diff] [blame] | 241 | return result; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 244 | static void CopyProfileFile(const char* oldfile, const char* newfile) { |
Calin Juravle | 5221410 | 2014-06-04 12:01:50 +0100 | [diff] [blame] | 245 | ScopedFd src(open(oldfile, O_RDONLY)); |
| 246 | if (src.get() == -1) { |
| 247 | PLOG(ERROR) << "Failed to open profile file " << oldfile |
| 248 | << ". My uid:gid is " << getuid() << ":" << getgid(); |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | struct stat stat_src; |
| 253 | if (fstat(src.get(), &stat_src) == -1) { |
| 254 | PLOG(ERROR) << "Failed to get stats for profile file " << oldfile |
| 255 | << ". My uid:gid is " << getuid() << ":" << getgid(); |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 256 | return; |
| 257 | } |
| 258 | |
| 259 | // Create the copy with rw------- (only accessible by system) |
Calin Juravle | 5221410 | 2014-06-04 12:01:50 +0100 | [diff] [blame] | 260 | ScopedFd dst(open(newfile, O_WRONLY|O_CREAT|O_TRUNC, 0600)); |
| 261 | if (dst.get() == -1) { |
| 262 | PLOG(ERROR) << "Failed to create/write prev profile file " << newfile |
| 263 | << ". My uid:gid is " << getuid() << ":" << getgid(); |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 264 | return; |
| 265 | } |
Calin Juravle | 5221410 | 2014-06-04 12:01:50 +0100 | [diff] [blame] | 266 | |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 267 | #ifdef __linux__ |
Calin Juravle | 5221410 | 2014-06-04 12:01:50 +0100 | [diff] [blame] | 268 | if (sendfile(dst.get(), src.get(), nullptr, stat_src.st_size) == -1) { |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 269 | #else |
| 270 | off_t len; |
| 271 | if (sendfile(dst.get(), src.get(), 0, &len, nullptr, 0) == -1) { |
| 272 | #endif |
Calin Juravle | 5221410 | 2014-06-04 12:01:50 +0100 | [diff] [blame] | 273 | PLOG(ERROR) << "Failed to copy profile file " << oldfile << " to " << newfile |
| 274 | << ". My uid:gid is " << getuid() << ":" << getgid(); |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 275 | } |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 276 | } |
| 277 | |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 278 | static jboolean IsDexOptNeededInternal(JNIEnv* env, const char* filename, |
| 279 | const char* pkgname, const char* instruction_set, const jboolean defer) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 280 | const bool kVerboseLogging = false; // Spammy logging. |
Brian Carlstrom | 09881a8 | 2014-04-18 17:44:01 -0700 | [diff] [blame] | 281 | const bool kReasonLogging = true; // Logging of reason for returning JNI_TRUE. |
Brian Carlstrom | bf2cb16 | 2012-02-27 17:49:19 -0800 | [diff] [blame] | 282 | |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 283 | if ((filename == nullptr) || !OS::FileExists(filename)) { |
| 284 | LOG(ERROR) << "DexFile_isDexOptNeeded file '" << filename << "' does not exist"; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 285 | ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException")); |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 286 | const char* message = (filename == nullptr) ? "<empty file name>" : filename; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 287 | env->ThrowNew(fnfe.get(), message); |
| 288 | return JNI_FALSE; |
Brian Carlstrom | 1d9f52b | 2011-10-13 10:50:45 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | // Always treat elements of the bootclasspath as up-to-date. The |
| 292 | // 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] | 293 | Runtime* runtime = Runtime::Current(); |
| 294 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 295 | // TODO: We're assuming that the 64 and 32 bit runtimes have identical |
| 296 | // class paths. isDexOptNeeded will not necessarily be called on a runtime |
| 297 | // that has the same instruction set as the file being dexopted. |
Brian Carlstrom | 1d9f52b | 2011-10-13 10:50:45 -0700 | [diff] [blame] | 298 | const std::vector<const DexFile*>& boot_class_path = class_linker->GetBootClassPath(); |
| 299 | for (size_t i = 0; i < boot_class_path.size(); i++) { |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 300 | if (boot_class_path[i]->GetLocation() == filename) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 301 | if (kVerboseLogging) { |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 302 | LOG(INFO) << "DexFile_isDexOptNeeded ignoring boot class path file: " << filename; |
Brian Carlstrom | bf2cb16 | 2012-02-27 17:49:19 -0800 | [diff] [blame] | 303 | } |
Brian Carlstrom | 1d9f52b | 2011-10-13 10:50:45 -0700 | [diff] [blame] | 304 | return JNI_FALSE; |
| 305 | } |
| 306 | } |
| 307 | |
Brian Carlstrom | e1ff199 | 2014-05-18 22:37:51 -0700 | [diff] [blame] | 308 | const InstructionSet target_instruction_set = GetInstructionSetFromString(instruction_set); |
| 309 | |
| 310 | // Check if we have an odex file next to the dex file. |
| 311 | std::string odex_filename(DexFilenameToOdexFilename(filename, kRuntimeISA)); |
| 312 | std::string error_msg; |
| 313 | std::unique_ptr<const OatFile> oat_file(OatFile::Open(odex_filename, odex_filename, NULL, false, |
| 314 | &error_msg)); |
| 315 | if (oat_file.get() == nullptr) { |
| 316 | if (kVerboseLogging) { |
| 317 | LOG(INFO) << "DexFile_isDexOptNeeded failed to open oat file '" << filename |
| 318 | << "': " << error_msg; |
| 319 | } |
| 320 | error_msg.clear(); |
| 321 | } else { |
| 322 | const art::OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(filename, NULL, |
| 323 | kReasonLogging); |
| 324 | if (oat_dex_file != nullptr) { |
| 325 | uint32_t location_checksum; |
| 326 | // If its not possible to read the classes.dex assume up-to-date as we won't be able to |
| 327 | // compile it anyway. |
| 328 | if (!DexFile::GetChecksum(filename, &location_checksum, &error_msg)) { |
| 329 | if (kVerboseLogging) { |
| 330 | LOG(INFO) << "DexFile_isDexOptNeeded ignoring precompiled stripped file: " |
| 331 | << filename << ": " << error_msg; |
| 332 | } |
| 333 | return JNI_FALSE; |
| 334 | } |
| 335 | if (ClassLinker::VerifyOatFileChecksums(oat_file.get(), filename, location_checksum, |
| 336 | target_instruction_set, |
| 337 | &error_msg)) { |
| 338 | if (kVerboseLogging) { |
| 339 | LOG(INFO) << "DexFile_isDexOptNeeded precompiled file " << odex_filename |
| 340 | << " has an up-to-date checksum compared to " << filename; |
| 341 | } |
| 342 | return JNI_FALSE; |
| 343 | } else { |
| 344 | if (kVerboseLogging) { |
| 345 | LOG(INFO) << "DexFile_isDexOptNeeded found precompiled file " << odex_filename |
| 346 | << " with an out-of-date checksum compared to " << filename |
| 347 | << ": " << error_msg; |
| 348 | } |
| 349 | error_msg.clear(); |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 354 | // Check the profile file. We need to rerun dex2oat if the profile has changed significantly |
| 355 | // since the last time, or it's new. |
| 356 | // If the 'defer' argument is true then this will be retried later. In this case we |
| 357 | // need to make sure that the profile file copy is not made so that we will get the |
| 358 | // same result second time. |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 359 | if (Runtime::Current()->GetProfilerOptions().IsEnabled() && (pkgname != nullptr)) { |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 360 | const std::string profile_file = GetDalvikCacheOrDie("profiles", false /* create_if_absent */) |
| 361 | + std::string("/") + pkgname; |
Nick Kralevich | 7bfa86d | 2014-06-11 18:29:07 -0700 | [diff] [blame] | 362 | const std::string prev_profile_file = profile_file + std::string("@old"); |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 363 | |
| 364 | struct stat profstat, prevstat; |
| 365 | int e1 = stat(profile_file.c_str(), &profstat); |
| 366 | int e2 = stat(prev_profile_file.c_str(), &prevstat); |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 367 | if (e1 < 0) { |
| 368 | // No profile file, need to run dex2oat |
Brian Carlstrom | 09881a8 | 2014-04-18 17:44:01 -0700 | [diff] [blame] | 369 | if (kReasonLogging) { |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 370 | LOG(INFO) << "DexFile_isDexOptNeeded profile file " << profile_file << " doesn't exist"; |
| 371 | } |
| 372 | return JNI_TRUE; |
| 373 | } |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 374 | |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 375 | if (e2 == 0) { |
| 376 | // 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] | 377 | // A change in profile is considered significant if X% (change_thr property) of the top K% |
| 378 | // (compile_thr property) samples has changed. |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 379 | double top_k_threshold = Runtime::Current()->GetProfilerOptions().GetTopKThreshold(); |
| 380 | double change_threshold = Runtime::Current()->GetProfilerOptions().GetTopKChangeThreshold(); |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 381 | double change_percent = 0.0; |
| 382 | ProfileFile new_profile, old_profile; |
| 383 | bool new_ok = new_profile.LoadFile(profile_file); |
| 384 | bool old_ok = old_profile.LoadFile(prev_profile_file); |
| 385 | if (!new_ok || !old_ok) { |
Brian Carlstrom | 09881a8 | 2014-04-18 17:44:01 -0700 | [diff] [blame] | 386 | if (kVerboseLogging) { |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 387 | LOG(INFO) << "DexFile_isDexOptNeeded Ignoring invalid profiles: " |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 388 | << (new_ok ? "" : profile_file) << " " << (old_ok ? "" : prev_profile_file); |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 389 | } |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 390 | } else { |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 391 | std::set<std::string> new_top_k, old_top_k; |
| 392 | new_profile.GetTopKSamples(new_top_k, top_k_threshold); |
| 393 | old_profile.GetTopKSamples(old_top_k, top_k_threshold); |
| 394 | if (new_top_k.empty()) { |
| 395 | if (kVerboseLogging) { |
| 396 | LOG(INFO) << "DexFile_isDexOptNeeded empty profile: " << profile_file; |
| 397 | } |
| 398 | // If the new topK is empty we shouldn't optimize so we leave the change_percent at 0.0. |
| 399 | } else { |
| 400 | std::set<std::string> diff; |
| 401 | std::set_difference(new_top_k.begin(), new_top_k.end(), old_top_k.begin(), old_top_k.end(), |
| 402 | std::inserter(diff, diff.end())); |
| 403 | // TODO: consider using the usedPercentage instead of the plain diff count. |
| 404 | change_percent = 100.0 * static_cast<double>(diff.size()) / static_cast<double>(new_top_k.size()); |
| 405 | if (kVerboseLogging) { |
| 406 | std::set<std::string>::iterator end = diff.end(); |
| 407 | for (std::set<std::string>::iterator it = diff.begin(); it != end; it++) { |
| 408 | LOG(INFO) << "DexFile_isDexOptNeeded new in topK: " << *it; |
| 409 | } |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 410 | } |
| 411 | } |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 412 | } |
| 413 | |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 414 | if (change_percent > change_threshold) { |
Brian Carlstrom | 09881a8 | 2014-04-18 17:44:01 -0700 | [diff] [blame] | 415 | if (kReasonLogging) { |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 416 | LOG(INFO) << "DexFile_isDexOptNeeded size of new profile file " << profile_file << |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 417 | " is significantly different from old profile file " << prev_profile_file << " (top " |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 418 | << top_k_threshold << "% samples changed in proportion of " << change_percent << "%)"; |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 419 | } |
| 420 | if (!defer) { |
| 421 | CopyProfileFile(profile_file.c_str(), prev_profile_file.c_str()); |
| 422 | } |
| 423 | return JNI_TRUE; |
| 424 | } |
| 425 | } else { |
| 426 | // Previous profile does not exist. Make a copy of the current one. |
Brian Carlstrom | 09881a8 | 2014-04-18 17:44:01 -0700 | [diff] [blame] | 427 | if (kVerboseLogging) { |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 428 | LOG(INFO) << "DexFile_isDexOptNeeded previous profile doesn't exist: " << prev_profile_file; |
| 429 | } |
| 430 | if (!defer) { |
| 431 | CopyProfileFile(profile_file.c_str(), prev_profile_file.c_str()); |
| 432 | } |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 436 | // Check if we have an oat file in the cache |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 437 | const std::string cache_dir(GetDalvikCacheOrDie(instruction_set)); |
| 438 | const std::string cache_location( |
| 439 | GetDalvikCacheFilenameOrDie(filename, cache_dir.c_str())); |
| 440 | oat_file.reset(OatFile::Open(cache_location, filename, NULL, false, &error_msg)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 441 | if (oat_file.get() == nullptr) { |
Brian Carlstrom | 09881a8 | 2014-04-18 17:44:01 -0700 | [diff] [blame] | 442 | if (kReasonLogging) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 443 | LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 444 | << " does not exist for " << filename << ": " << error_msg; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 445 | } |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 446 | return JNI_TRUE; |
| 447 | } |
| 448 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 449 | uint32_t location_checksum; |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 450 | if (!DexFile::GetChecksum(filename, &location_checksum, &error_msg)) { |
Brian Carlstrom | 09881a8 | 2014-04-18 17:44:01 -0700 | [diff] [blame] | 451 | if (kReasonLogging) { |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 452 | LOG(ERROR) << "DexFile_isDexOptNeeded failed to compute checksum of " << filename |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 453 | << " (error " << error_msg << ")"; |
| 454 | } |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 455 | return JNI_TRUE; |
| 456 | } |
| 457 | |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 458 | if (!ClassLinker::VerifyOatFileChecksums(oat_file.get(), filename, location_checksum, |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 459 | target_instruction_set, &error_msg)) { |
Brian Carlstrom | 09881a8 | 2014-04-18 17:44:01 -0700 | [diff] [blame] | 460 | if (kReasonLogging) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 461 | LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 462 | << " has out-of-date checksum compared to " << filename |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 463 | << " (error " << error_msg << ")"; |
| 464 | } |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 465 | return JNI_TRUE; |
| 466 | } |
| 467 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 468 | if (kVerboseLogging) { |
Brian Carlstrom | bf2cb16 | 2012-02-27 17:49:19 -0800 | [diff] [blame] | 469 | LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 470 | << " is up-to-date for " << filename; |
Brian Carlstrom | bf2cb16 | 2012-02-27 17:49:19 -0800 | [diff] [blame] | 471 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 472 | CHECK(error_msg.empty()) << error_msg; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 473 | return JNI_FALSE; |
| 474 | } |
| 475 | |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 476 | static jboolean DexFile_isDexOptNeededInternal(JNIEnv* env, jclass, jstring javaFilename, |
| 477 | jstring javaPkgname, jstring javaInstructionSet, jboolean defer) { |
| 478 | ScopedUtfChars filename(env, javaFilename); |
| 479 | NullableScopedUtfChars pkgname(env, javaPkgname); |
| 480 | ScopedUtfChars instruction_set(env, javaInstructionSet); |
| 481 | |
| 482 | return IsDexOptNeededInternal(env, filename.c_str(), pkgname.c_str(), |
| 483 | instruction_set.c_str(), defer); |
| 484 | } |
| 485 | |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 486 | // public API, NULL pkgname |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 487 | static jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) { |
| 488 | const char* instruction_set = GetInstructionSetString(kRuntimeISA); |
| 489 | ScopedUtfChars filename(env, javaFilename); |
| 490 | return IsDexOptNeededInternal(env, filename.c_str(), nullptr /* pkgname */, |
| 491 | instruction_set, false /* defer */); |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 495 | static JNINativeMethod gMethods[] = { |
Elliott Hughes | 2d98390 | 2014-02-04 16:17:13 -0800 | [diff] [blame] | 496 | NATIVE_METHOD(DexFile, closeDexFile, "(J)V"), |
| 497 | NATIVE_METHOD(DexFile, defineClassNative, "(Ljava/lang/String;Ljava/lang/ClassLoader;J)Ljava/lang/Class;"), |
| 498 | NATIVE_METHOD(DexFile, getClassNameList, "(J)[Ljava/lang/String;"), |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 499 | NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"), |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 500 | NATIVE_METHOD(DexFile, isDexOptNeededInternal, "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Z)Z"), |
Elliott Hughes | 2d98390 | 2014-02-04 16:17:13 -0800 | [diff] [blame] | 501 | NATIVE_METHOD(DexFile, openDexFileNative, "(Ljava/lang/String;Ljava/lang/String;I)J"), |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 502 | }; |
| 503 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 504 | void register_dalvik_system_DexFile(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 505 | REGISTER_NATIVE_METHODS("dalvik/system/DexFile"); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | } // namespace art |