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