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 | |
Brian Carlstrom | 1d9f52b | 2011-10-13 10:50:45 -0700 | [diff] [blame] | 17 | #include <unistd.h> |
| 18 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 19 | #include "base/logging.h" |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 20 | #include "class_linker.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 21 | #include "common_throws.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 22 | #include "dex_file-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 23 | #include "gc/space/image_space.h" |
| 24 | #include "gc/space/space-inl.h" |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 25 | #include "image.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 26 | #include "jni_internal.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 27 | #include "mirror/class_loader.h" |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 28 | #include "mirror/object-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 29 | #include "mirror/string.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 30 | #include "oat.h" |
Brian Carlstrom | 1d9f52b | 2011-10-13 10:50:45 -0700 | [diff] [blame] | 31 | #include "os.h" |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 32 | #include "runtime.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 33 | #include "scoped_thread_state_change.h" |
Ian Rogers | c981848 | 2012-01-11 08:52:51 -0800 | [diff] [blame] | 34 | #include "ScopedLocalRef.h" |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 35 | #include "ScopedUtfChars.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 36 | #include "toStringArray.h" |
| 37 | #include "zip_archive.h" |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 38 | |
| 39 | namespace art { |
| 40 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 41 | // A smart pointer that provides read-only access to a Java string's UTF chars. |
| 42 | // Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if |
| 43 | // passed a null jstring. The correct idiom is: |
| 44 | // |
| 45 | // NullableScopedUtfChars name(env, javaName); |
Brian Carlstrom | c252c3e | 2011-10-16 23:21:02 -0700 | [diff] [blame] | 46 | // if (env->ExceptionCheck()) { |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 47 | // return NULL; |
| 48 | // } |
| 49 | // // ... use name.c_str() |
| 50 | // |
| 51 | // TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option. |
| 52 | class NullableScopedUtfChars { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 53 | public: |
| 54 | NullableScopedUtfChars(JNIEnv* env, jstring s) : mEnv(env), mString(s) { |
| 55 | mUtfChars = (s != NULL) ? env->GetStringUTFChars(s, NULL) : NULL; |
| 56 | } |
| 57 | |
| 58 | ~NullableScopedUtfChars() { |
| 59 | if (mUtfChars) { |
| 60 | mEnv->ReleaseStringUTFChars(mString, mUtfChars); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 61 | } |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 62 | } |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 63 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 64 | const char* c_str() const { |
| 65 | return mUtfChars; |
| 66 | } |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 67 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 68 | size_t size() const { |
| 69 | return strlen(mUtfChars); |
| 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 | // Element access. |
| 73 | const char& operator[](size_t n) const { |
| 74 | return mUtfChars[n]; |
| 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 | private: |
| 78 | JNIEnv* mEnv; |
| 79 | jstring mString; |
| 80 | const char* 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 | // Disallow copy and assignment. |
| 83 | NullableScopedUtfChars(const NullableScopedUtfChars&); |
| 84 | void operator=(const NullableScopedUtfChars&); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 87 | static jint DexFile_openDexFileNative(JNIEnv* env, jclass, jstring javaSourceName, jstring javaOutputName, jint) { |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 88 | ScopedUtfChars sourceName(env, javaSourceName); |
| 89 | if (sourceName.c_str() == NULL) { |
| 90 | return 0; |
| 91 | } |
| 92 | NullableScopedUtfChars outputName(env, javaOutputName); |
Brian Carlstrom | c252c3e | 2011-10-16 23:21:02 -0700 | [diff] [blame] | 93 | if (env->ExceptionCheck()) { |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 94 | return 0; |
| 95 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 96 | |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 97 | uint32_t dex_location_checksum; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 98 | std::string error_msg; |
| 99 | if (!DexFile::GetChecksum(sourceName.c_str(), &dex_location_checksum, &error_msg)) { |
| 100 | ScopedObjectAccess soa(env); |
| 101 | DCHECK(!error_msg.empty()); |
| 102 | ThrowIOException("%s", error_msg.c_str()); |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 107 | const DexFile* dex_file; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 108 | if (outputName.c_str() == nullptr) { |
| 109 | dex_file = linker->FindDexFileInOatFileFromDexLocation(sourceName.c_str(), |
| 110 | dex_location_checksum, &error_msg); |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 111 | } else { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 112 | dex_file = linker->FindOrCreateOatFileForDexLocation(sourceName.c_str(), dex_location_checksum, |
| 113 | outputName.c_str(), &error_msg); |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 114 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 115 | if (dex_file == nullptr) { |
| 116 | ScopedObjectAccess soa(env); |
| 117 | ThrowIOException("%s", error_msg.c_str()); |
jeffhao | c393a4f | 2011-10-19 13:46:09 -0700 | [diff] [blame] | 118 | return 0; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 119 | } |
| 120 | return static_cast<jint>(reinterpret_cast<uintptr_t>(dex_file)); |
| 121 | } |
| 122 | |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 123 | static const DexFile* toDexFile(int dex_file_address, JNIEnv* env) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 124 | 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] | 125 | if (UNLIKELY(dex_file == nullptr)) { |
| 126 | ScopedObjectAccess soa(env); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 127 | ThrowNullPointerException(NULL, "dex_file == null"); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 128 | } |
| 129 | return dex_file; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 132 | static void DexFile_closeDexFile(JNIEnv* env, jclass, jint cookie) { |
| 133 | const DexFile* dex_file; |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 134 | dex_file = toDexFile(cookie, env); |
| 135 | if (dex_file == nullptr) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 136 | return; |
| 137 | } |
| 138 | if (Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) { |
| 139 | return; |
| 140 | } |
| 141 | delete dex_file; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 144 | static jclass DexFile_defineClassNative(JNIEnv* env, jclass, jstring javaName, jobject javaLoader, |
| 145 | jint cookie) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 146 | const DexFile* dex_file = toDexFile(cookie, env); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 147 | if (dex_file == NULL) { |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 148 | VLOG(class_linker) << "Failed to find dex_file"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 149 | return NULL; |
| 150 | } |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 151 | ScopedUtfChars class_name(env, javaName); |
| 152 | if (class_name.c_str() == NULL) { |
Brian Carlstrom | 2e450bf | 2013-09-06 15:39:46 -0700 | [diff] [blame] | 153 | VLOG(class_linker) << "Failed to find class_name"; |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 154 | return NULL; |
| 155 | } |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 156 | const std::string descriptor(DotToDescriptor(class_name.c_str())); |
Ian Rogers | ee39a10 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 157 | const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor.c_str()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 158 | if (dex_class_def == NULL) { |
Brian Carlstrom | 2e450bf | 2013-09-06 15:39:46 -0700 | [diff] [blame] | 159 | VLOG(class_linker) << "Failed to find dex_class_def"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 160 | return NULL; |
| 161 | } |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 162 | ScopedObjectAccess soa(env); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 163 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 164 | class_linker->RegisterDexFile(*dex_file); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 165 | mirror::ClassLoader* class_loader = soa.Decode<mirror::ClassLoader*>(javaLoader); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 166 | mirror::Class* result = class_linker->DefineClass(descriptor.c_str(), class_loader, *dex_file, |
| 167 | *dex_class_def); |
Brian Carlstrom | 2e450bf | 2013-09-06 15:39:46 -0700 | [diff] [blame] | 168 | VLOG(class_linker) << "DexFile_defineClassNative returning " << result; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 169 | return soa.AddLocalReference<jclass>(result); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 172 | static jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jint cookie) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 173 | const DexFile* dex_file; |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 174 | dex_file = toDexFile(cookie, env); |
| 175 | if (dex_file == nullptr) { |
| 176 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 177 | } |
Brian Carlstrom | 03a20ba | 2011-10-13 10:24:13 -0700 | [diff] [blame] | 178 | |
| 179 | std::vector<std::string> class_names; |
| 180 | for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) { |
| 181 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(i); |
| 182 | const char* descriptor = dex_file->GetClassDescriptor(class_def); |
| 183 | class_names.push_back(DescriptorToDot(descriptor)); |
| 184 | } |
| 185 | return toStringArray(env, class_names); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 188 | static jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 189 | const bool kVerboseLogging = false; // Spammy logging. |
| 190 | const bool kDebugLogging = true; // Logging useful for debugging. |
Brian Carlstrom | bf2cb16 | 2012-02-27 17:49:19 -0800 | [diff] [blame] | 191 | |
Brian Carlstrom | 03a20ba | 2011-10-13 10:24:13 -0700 | [diff] [blame] | 192 | ScopedUtfChars filename(env, javaFilename); |
Brian Carlstrom | 1d9f52b | 2011-10-13 10:50:45 -0700 | [diff] [blame] | 193 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 194 | if ((filename.c_str() == nullptr) || !OS::FileExists(filename.c_str())) { |
Brian Carlstrom | bf2cb16 | 2012-02-27 17:49:19 -0800 | [diff] [blame] | 195 | LOG(ERROR) << "DexFile_isDexOptNeeded file '" << filename.c_str() << "' does not exist"; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 196 | ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException")); |
| 197 | const char* message = (filename.c_str() == nullptr) ? "<empty file name>" : filename.c_str(); |
| 198 | env->ThrowNew(fnfe.get(), message); |
| 199 | return JNI_FALSE; |
Brian Carlstrom | 1d9f52b | 2011-10-13 10:50:45 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | // Always treat elements of the bootclasspath as up-to-date. The |
| 203 | // 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] | 204 | Runtime* runtime = Runtime::Current(); |
| 205 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Brian Carlstrom | 1d9f52b | 2011-10-13 10:50:45 -0700 | [diff] [blame] | 206 | const std::vector<const DexFile*>& boot_class_path = class_linker->GetBootClassPath(); |
| 207 | for (size_t i = 0; i < boot_class_path.size(); i++) { |
| 208 | if (boot_class_path[i]->GetLocation() == filename.c_str()) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 209 | if (kVerboseLogging) { |
Brian Carlstrom | bf2cb16 | 2012-02-27 17:49:19 -0800 | [diff] [blame] | 210 | LOG(INFO) << "DexFile_isDexOptNeeded ignoring boot class path file: " << filename.c_str(); |
| 211 | } |
Brian Carlstrom | 1d9f52b | 2011-10-13 10:50:45 -0700 | [diff] [blame] | 212 | return JNI_FALSE; |
| 213 | } |
| 214 | } |
| 215 | |
Brian Carlstrom | 30e2ea4 | 2013-06-19 23:25:37 -0700 | [diff] [blame] | 216 | // Check if we have an odex file next to the dex file. |
| 217 | std::string odex_filename(OatFile::DexFilenameToOdexFilename(filename.c_str())); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 218 | std::string error_msg; |
| 219 | UniquePtr<const OatFile> oat_file(OatFile::Open(odex_filename, odex_filename, NULL, false, |
| 220 | &error_msg)); |
| 221 | if (oat_file.get() == nullptr) { |
| 222 | if (kVerboseLogging) { |
| 223 | LOG(INFO) << "DexFile_isDexOptNeeded failed to open oat file '" << filename.c_str() |
| 224 | << "': " << error_msg; |
| 225 | } |
| 226 | error_msg.clear(); |
| 227 | } else { |
| 228 | const art::OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(filename.c_str(), NULL, |
| 229 | kDebugLogging); |
| 230 | if (oat_dex_file != nullptr) { |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 231 | uint32_t location_checksum; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 232 | // If its not possible to read the classes.dex assume up-to-date as we won't be able to |
| 233 | // compile it anyway. |
| 234 | if (!DexFile::GetChecksum(filename.c_str(), &location_checksum, &error_msg)) { |
| 235 | if (kVerboseLogging) { |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 236 | LOG(INFO) << "DexFile_isDexOptNeeded ignoring precompiled stripped file: " |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 237 | << filename.c_str() << ": " << error_msg; |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 238 | } |
| 239 | return JNI_FALSE; |
| 240 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 241 | if (ClassLinker::VerifyOatFileChecksums(oat_file.get(), filename.c_str(), location_checksum, |
| 242 | &error_msg)) { |
| 243 | if (kVerboseLogging) { |
Brian Carlstrom | 30e2ea4 | 2013-06-19 23:25:37 -0700 | [diff] [blame] | 244 | LOG(INFO) << "DexFile_isDexOptNeeded precompiled file " << odex_filename |
Brian Carlstrom | 0d3bbff | 2013-10-28 15:21:32 -0700 | [diff] [blame] | 245 | << " has an up-to-date checksum compared to " << filename.c_str(); |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 246 | } |
| 247 | return JNI_FALSE; |
Brian Carlstrom | 0d3bbff | 2013-10-28 15:21:32 -0700 | [diff] [blame] | 248 | } else { |
| 249 | if (kVerboseLogging) { |
| 250 | LOG(INFO) << "DexFile_isDexOptNeeded found precompiled file " << odex_filename |
| 251 | << " with an out-of-date checksum compared to " << filename.c_str() |
| 252 | << ": " << error_msg; |
| 253 | } |
| 254 | error_msg.clear(); |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 255 | } |
Brian Carlstrom | afe2551 | 2012-06-27 17:02:58 -0700 | [diff] [blame] | 256 | } |
Brian Carlstrom | fad7143 | 2011-10-16 20:25:10 -0700 | [diff] [blame] | 257 | } |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 258 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 259 | // Check if we have an oat file in the cache |
Brian Carlstrom | 30e2ea4 | 2013-06-19 23:25:37 -0700 | [diff] [blame] | 260 | std::string cache_location(GetDalvikCacheFilenameOrDie(filename.c_str())); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 261 | oat_file.reset(OatFile::Open(cache_location, filename.c_str(), NULL, false, &error_msg)); |
| 262 | if (oat_file.get() == nullptr) { |
| 263 | if (kDebugLogging) { |
| 264 | LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location |
| 265 | << " does not exist for " << filename.c_str() << ": " << error_msg; |
| 266 | } |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 267 | return JNI_TRUE; |
| 268 | } |
| 269 | |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 270 | for (const auto& space : runtime->GetHeap()->GetContinuousSpaces()) { |
| 271 | if (space->IsImageSpace()) { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 272 | // TODO: Ensure this works with multiple image spaces. |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 273 | const ImageHeader& image_header = space->AsImageSpace()->GetImageHeader(); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 274 | if (oat_file->GetOatHeader().GetImageFileLocationOatChecksum() != |
| 275 | image_header.GetOatChecksum()) { |
| 276 | if (kDebugLogging) { |
| 277 | ScopedObjectAccess soa(env); |
| 278 | LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location |
| 279 | << " has out-of-date oat checksum compared to " |
| 280 | << image_header.GetImageRoot(ImageHeader::kOatLocation)->AsString()->ToModifiedUtf8(); |
| 281 | } |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 282 | return JNI_TRUE; |
| 283 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 284 | if (oat_file->GetOatHeader().GetImageFileLocationOatDataBegin() |
| 285 | != reinterpret_cast<uint32_t>(image_header.GetOatDataBegin())) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 286 | if (kDebugLogging) { |
| 287 | ScopedObjectAccess soa(env); |
| 288 | LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location |
| 289 | << " has out-of-date oat begin compared to " |
| 290 | << image_header.GetImageRoot(ImageHeader::kOatLocation)->AsString()->ToModifiedUtf8(); |
| 291 | } |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 292 | return JNI_TRUE; |
| 293 | } |
| 294 | } |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 297 | uint32_t location_checksum; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 298 | if (!DexFile::GetChecksum(filename.c_str(), &location_checksum, &error_msg)) { |
| 299 | if (kDebugLogging) { |
| 300 | LOG(ERROR) << "DexFile_isDexOptNeeded failed to compute checksum of " << filename.c_str() |
| 301 | << " (error " << error_msg << ")"; |
| 302 | } |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 303 | return JNI_TRUE; |
| 304 | } |
| 305 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 306 | if (!ClassLinker::VerifyOatFileChecksums(oat_file.get(), filename.c_str(), location_checksum, |
| 307 | &error_msg)) { |
| 308 | if (kDebugLogging) { |
| 309 | LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location |
| 310 | << " has out-of-date checksum compared to " << filename.c_str() |
| 311 | << " (error " << error_msg << ")"; |
| 312 | } |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 313 | return JNI_TRUE; |
| 314 | } |
| 315 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 316 | if (kVerboseLogging) { |
Brian Carlstrom | bf2cb16 | 2012-02-27 17:49:19 -0800 | [diff] [blame] | 317 | LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location |
| 318 | << " is up-to-date for " << filename.c_str(); |
| 319 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 320 | CHECK(error_msg.empty()) << error_msg; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 321 | return JNI_FALSE; |
| 322 | } |
| 323 | |
| 324 | static JNINativeMethod gMethods[] = { |
| 325 | NATIVE_METHOD(DexFile, closeDexFile, "(I)V"), |
Ian Rogers | 66a556f | 2012-02-14 00:05:38 -0800 | [diff] [blame] | 326 | NATIVE_METHOD(DexFile, defineClassNative, "(Ljava/lang/String;Ljava/lang/ClassLoader;I)Ljava/lang/Class;"), |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 327 | NATIVE_METHOD(DexFile, getClassNameList, "(I)[Ljava/lang/String;"), |
| 328 | NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"), |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 329 | NATIVE_METHOD(DexFile, openDexFileNative, "(Ljava/lang/String;Ljava/lang/String;I)I"), |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 330 | }; |
| 331 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 332 | void register_dalvik_system_DexFile(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 333 | REGISTER_NATIVE_METHODS("dalvik/system/DexFile"); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | } // namespace art |