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