Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 17 | #include "dalvik_system_DexFile.h" |
| 18 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 19 | #include "base/logging.h" |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 20 | #include "base/stl_util.h" |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 21 | #include "base/stringprintf.h" |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 22 | #include "class_linker.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 23 | #include "common_throws.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 24 | #include "dex_file-inl.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" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 29 | #include "oat_file_assistant.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 30 | #include "oat_file_manager.h" |
Brian Carlstrom | 1d9f52b | 2011-10-13 10:50:45 -0700 | [diff] [blame] | 31 | #include "os.h" |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 32 | #include "profiler.h" |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 33 | #include "runtime.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 34 | #include "scoped_thread_state_change.h" |
Ian Rogers | c981848 | 2012-01-11 08:52:51 -0800 | [diff] [blame] | 35 | #include "ScopedLocalRef.h" |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 36 | #include "ScopedUtfChars.h" |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 37 | #include "utils.h" |
Ian Rogers | dd157d7 | 2014-05-15 14:47:50 -0700 | [diff] [blame] | 38 | #include "well_known_classes.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 39 | #include "zip_archive.h" |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 40 | |
| 41 | namespace art { |
| 42 | |
Nicolas Geoffray | 72da5e7 | 2015-10-13 07:26:45 +0000 | [diff] [blame] | 43 | static std::unique_ptr<std::vector<const DexFile*>> |
| 44 | ConvertJavaArrayToNative(JNIEnv* env, jobject arrayObject) { |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 45 | jarray array = reinterpret_cast<jarray>(arrayObject); |
| 46 | |
| 47 | jsize array_size = env->GetArrayLength(array); |
| 48 | if (env->ExceptionCheck() == JNI_TRUE) { |
Nicolas Geoffray | 72da5e7 | 2015-10-13 07:26:45 +0000 | [diff] [blame] | 49 | return std::unique_ptr<std::vector<const DexFile*>>(); |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | // TODO: Optimize. On 32bit we can use an int array. |
| 53 | jboolean is_long_data_copied; |
| 54 | jlong* long_data = env->GetLongArrayElements(reinterpret_cast<jlongArray>(array), |
| 55 | &is_long_data_copied); |
| 56 | if (env->ExceptionCheck() == JNI_TRUE) { |
Nicolas Geoffray | 72da5e7 | 2015-10-13 07:26:45 +0000 | [diff] [blame] | 57 | return std::unique_ptr<std::vector<const DexFile*>>(); |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Nicolas Geoffray | 72da5e7 | 2015-10-13 07:26:45 +0000 | [diff] [blame] | 60 | std::unique_ptr<std::vector<const DexFile*>> ret(new std::vector<const DexFile*>()); |
| 61 | ret->reserve(array_size); |
| 62 | for (jsize i = 0; i < array_size; ++i) { |
| 63 | ret->push_back(reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(*(long_data + i)))); |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | env->ReleaseLongArrayElements(reinterpret_cast<jlongArray>(array), long_data, JNI_ABORT); |
Nicolas Geoffray | 72da5e7 | 2015-10-13 07:26:45 +0000 | [diff] [blame] | 67 | if (env->ExceptionCheck() == JNI_TRUE) { |
| 68 | return std::unique_ptr<std::vector<const DexFile*>>(); |
| 69 | } |
| 70 | |
| 71 | return ret; |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Nicolas Geoffray | 72da5e7 | 2015-10-13 07:26:45 +0000 | [diff] [blame] | 74 | static jlongArray ConvertNativeToJavaArray(JNIEnv* env, |
| 75 | std::vector<std::unique_ptr<const DexFile>>& vec) { |
| 76 | size_t vec_size = vec.size(); |
| 77 | jlongArray long_array = env->NewLongArray(static_cast<jsize>(vec_size)); |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 78 | if (env->ExceptionCheck() == JNI_TRUE) { |
| 79 | return nullptr; |
| 80 | } |
| 81 | |
| 82 | jboolean is_long_data_copied; |
| 83 | jlong* long_data = env->GetLongArrayElements(long_array, &is_long_data_copied); |
| 84 | if (env->ExceptionCheck() == JNI_TRUE) { |
| 85 | return nullptr; |
| 86 | } |
| 87 | |
Nicolas Geoffray | 72da5e7 | 2015-10-13 07:26:45 +0000 | [diff] [blame] | 88 | jlong* tmp = long_data; |
| 89 | for (auto& dex_file : vec) { |
| 90 | *tmp = reinterpret_cast<uintptr_t>(dex_file.get()); |
| 91 | tmp++; |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | env->ReleaseLongArrayElements(long_array, long_data, 0); |
| 95 | if (env->ExceptionCheck() == JNI_TRUE) { |
| 96 | return nullptr; |
| 97 | } |
| 98 | |
| 99 | // Now release all the unique_ptrs. |
| 100 | for (auto& dex_file : vec) { |
| 101 | dex_file.release(); |
| 102 | } |
| 103 | |
| 104 | return long_array; |
| 105 | } |
| 106 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 107 | // A smart pointer that provides read-only access to a Java string's UTF chars. |
| 108 | // Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if |
| 109 | // passed a null jstring. The correct idiom is: |
| 110 | // |
| 111 | // NullableScopedUtfChars name(env, javaName); |
Brian Carlstrom | c252c3e | 2011-10-16 23:21:02 -0700 | [diff] [blame] | 112 | // if (env->ExceptionCheck()) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 113 | // return null; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 114 | // } |
| 115 | // // ... use name.c_str() |
| 116 | // |
| 117 | // TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option. |
| 118 | class NullableScopedUtfChars { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 119 | public: |
| 120 | NullableScopedUtfChars(JNIEnv* env, jstring s) : mEnv(env), mString(s) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 121 | mUtfChars = (s != nullptr) ? env->GetStringUTFChars(s, nullptr) : nullptr; |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | ~NullableScopedUtfChars() { |
| 125 | if (mUtfChars) { |
| 126 | mEnv->ReleaseStringUTFChars(mString, mUtfChars); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 127 | } |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 128 | } |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 129 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 130 | const char* c_str() const { |
| 131 | return mUtfChars; |
| 132 | } |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 133 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 134 | size_t size() const { |
| 135 | return strlen(mUtfChars); |
| 136 | } |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 137 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 138 | // Element access. |
| 139 | const char& operator[](size_t n) const { |
| 140 | return mUtfChars[n]; |
| 141 | } |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 142 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 143 | private: |
| 144 | JNIEnv* mEnv; |
| 145 | jstring mString; |
| 146 | const char* mUtfChars; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 147 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 148 | // Disallow copy and assignment. |
| 149 | NullableScopedUtfChars(const NullableScopedUtfChars&); |
| 150 | void operator=(const NullableScopedUtfChars&); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 151 | }; |
| 152 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 153 | static jobject DexFile_openDexFileNative( |
| 154 | JNIEnv* env, jclass, jstring javaSourceName, jstring javaOutputName, jint) { |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 155 | ScopedUtfChars sourceName(env, javaSourceName); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 156 | if (sourceName.c_str() == nullptr) { |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 157 | return 0; |
| 158 | } |
| 159 | NullableScopedUtfChars outputName(env, javaOutputName); |
Brian Carlstrom | c252c3e | 2011-10-16 23:21:02 -0700 | [diff] [blame] | 160 | if (env->ExceptionCheck()) { |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 161 | return 0; |
| 162 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 163 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 164 | Runtime* const runtime = Runtime::Current(); |
| 165 | ClassLinker* linker = runtime->GetClassLinker(); |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 166 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 167 | std::vector<std::string> error_msgs; |
| 168 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 169 | dex_files = runtime->GetOatFileManager().OpenDexFilesFromOat(sourceName.c_str(), |
| 170 | outputName.c_str(), |
Nicolas Geoffray | 72da5e7 | 2015-10-13 07:26:45 +0000 | [diff] [blame] | 171 | &error_msgs); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 172 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 173 | if (!dex_files.empty()) { |
Nicolas Geoffray | 72da5e7 | 2015-10-13 07:26:45 +0000 | [diff] [blame] | 174 | jlongArray array = ConvertNativeToJavaArray(env, dex_files); |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 175 | if (array == nullptr) { |
| 176 | ScopedObjectAccess soa(env); |
| 177 | for (auto& dex_file : dex_files) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 178 | if (linker->FindDexCache(soa.Self(), *dex_file, true) != nullptr) { |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 179 | dex_file.release(); |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | return array; |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 184 | } else { |
Vladimir Marko | 60836d5 | 2014-01-16 15:53:38 +0000 | [diff] [blame] | 185 | ScopedObjectAccess soa(env); |
Andreas Gampe | 329d188 | 2014-04-08 10:32:19 -0700 | [diff] [blame] | 186 | CHECK(!error_msgs.empty()); |
| 187 | // The most important message is at the end. So set up nesting by going forward, which will |
| 188 | // wrap the existing exception as a cause for the following one. |
| 189 | auto it = error_msgs.begin(); |
| 190 | auto itEnd = error_msgs.end(); |
| 191 | for ( ; it != itEnd; ++it) { |
| 192 | ThrowWrappedIOException("%s", it->c_str()); |
| 193 | } |
| 194 | |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 195 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 196 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Mathieu Chartier | 1d7d7f1 | 2015-09-25 16:48:57 -0700 | [diff] [blame] | 199 | static jboolean DexFile_closeDexFile(JNIEnv* env, jclass, jobject cookie) { |
Nicolas Geoffray | 72da5e7 | 2015-10-13 07:26:45 +0000 | [diff] [blame] | 200 | ScopedObjectAccess soa(env); |
| 201 | mirror::Object* dex_files_object = soa.Decode<mirror::Object*>(cookie); |
| 202 | if (dex_files_object == nullptr) { |
| 203 | ThrowNullPointerException("cookie == null"); |
Mathieu Chartier | 1d7d7f1 | 2015-09-25 16:48:57 -0700 | [diff] [blame] | 204 | return JNI_FALSE; |
| 205 | } |
Nicolas Geoffray | 72da5e7 | 2015-10-13 07:26:45 +0000 | [diff] [blame] | 206 | mirror::LongArray* dex_files = dex_files_object->AsLongArray(); |
| 207 | |
| 208 | // Delete dex files associated with this dalvik.system.DexFile since there should not be running |
| 209 | // code using it. dex_files is a vector due to multidex. |
| 210 | ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); |
Mathieu Chartier | 1d7d7f1 | 2015-09-25 16:48:57 -0700 | [diff] [blame] | 211 | bool all_deleted = true; |
Nicolas Geoffray | 72da5e7 | 2015-10-13 07:26:45 +0000 | [diff] [blame] | 212 | for (int32_t i = 0, count = dex_files->GetLength(); i < count; ++i) { |
| 213 | auto* dex_file = reinterpret_cast<DexFile*>(dex_files->Get(i)); |
| 214 | if (dex_file == nullptr) { |
| 215 | continue; |
| 216 | } |
| 217 | // Only delete the dex file if the dex cache is not found to prevent runtime crashes if there |
| 218 | // are calls to DexFile.close while the ART DexFile is still in use. |
| 219 | if (class_linker->FindDexCache(soa.Self(), *dex_file, true) == nullptr) { |
| 220 | // Clear the element in the array so that we can call close again. |
| 221 | dex_files->Set(i, 0); |
| 222 | delete dex_file; |
| 223 | } else { |
| 224 | all_deleted = false; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 225 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 226 | } |
Mathieu Chartier | 1d7d7f1 | 2015-09-25 16:48:57 -0700 | [diff] [blame] | 227 | |
Nicolas Geoffray | 72da5e7 | 2015-10-13 07:26:45 +0000 | [diff] [blame] | 228 | // TODO: Also unmap the OatFile for this dalvik.system.DexFile. |
| 229 | |
Mathieu Chartier | 1d7d7f1 | 2015-09-25 16:48:57 -0700 | [diff] [blame] | 230 | return all_deleted ? JNI_TRUE : JNI_FALSE; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Nicolas Geoffray | 72da5e7 | 2015-10-13 07:26:45 +0000 | [diff] [blame] | 233 | static jclass DexFile_defineClassNative(JNIEnv* env, jclass, jstring javaName, jobject javaLoader, |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 234 | jobject cookie) { |
Nicolas Geoffray | 72da5e7 | 2015-10-13 07:26:45 +0000 | [diff] [blame] | 235 | std::unique_ptr<std::vector<const DexFile*>> dex_files = ConvertJavaArrayToNative(env, cookie); |
| 236 | if (dex_files.get() == nullptr) { |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 237 | VLOG(class_linker) << "Failed to find dex_file"; |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 238 | DCHECK(env->ExceptionCheck()); |
| 239 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 240 | } |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 241 | |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 242 | ScopedUtfChars class_name(env, javaName); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 243 | if (class_name.c_str() == nullptr) { |
Brian Carlstrom | 2e450bf | 2013-09-06 15:39:46 -0700 | [diff] [blame] | 244 | VLOG(class_linker) << "Failed to find class_name"; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 245 | return nullptr; |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 246 | } |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 247 | const std::string descriptor(DotToDescriptor(class_name.c_str())); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 248 | const size_t hash(ComputeModifiedUtf8Hash(descriptor.c_str())); |
Nicolas Geoffray | 72da5e7 | 2015-10-13 07:26:45 +0000 | [diff] [blame] | 249 | for (auto& dex_file : *dex_files) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 250 | const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor.c_str(), hash); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 251 | if (dex_class_def != nullptr) { |
| 252 | ScopedObjectAccess soa(env); |
| 253 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 254 | class_linker->RegisterDexFile(*dex_file); |
| 255 | StackHandleScope<1> hs(soa.Self()); |
| 256 | Handle<mirror::ClassLoader> class_loader( |
| 257 | hs.NewHandle(soa.Decode<mirror::ClassLoader*>(javaLoader))); |
Nicolas Geoffray | 72da5e7 | 2015-10-13 07:26:45 +0000 | [diff] [blame] | 258 | mirror::Class* result = class_linker->DefineClass(soa.Self(), descriptor.c_str(), hash, |
| 259 | class_loader, *dex_file, *dex_class_def); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 260 | if (result != nullptr) { |
Brian Carlstrom | 667ab7c | 2014-10-16 19:12:28 -0700 | [diff] [blame] | 261 | VLOG(class_linker) << "DexFile_defineClassNative returning " << result |
| 262 | << " for " << class_name.c_str(); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 263 | return soa.AddLocalReference<jclass>(result); |
| 264 | } |
| 265 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 266 | } |
Brian Carlstrom | 667ab7c | 2014-10-16 19:12:28 -0700 | [diff] [blame] | 267 | VLOG(class_linker) << "Failed to find dex_class_def " << class_name.c_str(); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 268 | return nullptr; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 271 | // Needed as a compare functor for sets of const char |
| 272 | struct CharPointerComparator { |
| 273 | bool operator()(const char *str1, const char *str2) const { |
| 274 | return strcmp(str1, str2) < 0; |
| 275 | } |
| 276 | }; |
| 277 | |
| 278 | // Note: this can be an expensive call, as we sort out duplicates in MultiDex files. |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 279 | static jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jobject cookie) { |
Nicolas Geoffray | 72da5e7 | 2015-10-13 07:26:45 +0000 | [diff] [blame] | 280 | std::unique_ptr<std::vector<const DexFile*>> dex_files = ConvertJavaArrayToNative(env, cookie); |
| 281 | if (dex_files.get() == nullptr) { |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 282 | DCHECK(env->ExceptionCheck()); |
| 283 | return nullptr; |
| 284 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 285 | |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 286 | // Push all class descriptors into a set. Use set instead of unordered_set as we want to |
| 287 | // retrieve all in the end. |
| 288 | std::set<const char*, CharPointerComparator> descriptors; |
Nicolas Geoffray | 72da5e7 | 2015-10-13 07:26:45 +0000 | [diff] [blame] | 289 | for (auto& dex_file : *dex_files) { |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 290 | for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) { |
| 291 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(i); |
| 292 | const char* descriptor = dex_file->GetClassDescriptor(class_def); |
| 293 | descriptors.insert(descriptor); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 294 | } |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 295 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 296 | |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 297 | // Now create output array and copy the set into it. |
Nicolas Geoffray | 72da5e7 | 2015-10-13 07:26:45 +0000 | [diff] [blame] | 298 | jobjectArray result = env->NewObjectArray(descriptors.size(), WellKnownClasses::java_lang_String, |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 299 | nullptr); |
| 300 | if (result != nullptr) { |
| 301 | auto it = descriptors.begin(); |
| 302 | auto it_end = descriptors.end(); |
| 303 | jsize i = 0; |
| 304 | for (; it != it_end; it++, ++i) { |
| 305 | std::string descriptor(DescriptorToDot(*it)); |
| 306 | ScopedLocalRef<jstring> jdescriptor(env, env->NewStringUTF(descriptor.c_str())); |
| 307 | if (jdescriptor.get() == nullptr) { |
| 308 | return nullptr; |
Ian Rogers | dd157d7 | 2014-05-15 14:47:50 -0700 | [diff] [blame] | 309 | } |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 310 | env->SetObjectArrayElement(result, i, jdescriptor.get()); |
Ian Rogers | dd157d7 | 2014-05-15 14:47:50 -0700 | [diff] [blame] | 311 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 312 | } |
Ian Rogers | dd157d7 | 2014-05-15 14:47:50 -0700 | [diff] [blame] | 313 | return result; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Nicolas Geoffray | 72da5e7 | 2015-10-13 07:26:45 +0000 | [diff] [blame] | 316 | static jint GetDexOptNeeded(JNIEnv* env, const char* filename, |
| 317 | const char* pkgname, const char* instruction_set, const jboolean defer) { |
| 318 | |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 319 | if ((filename == nullptr) || !OS::FileExists(filename)) { |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 320 | LOG(ERROR) << "DexFile_getDexOptNeeded file '" << filename << "' does not exist"; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 321 | ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException")); |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 322 | const char* message = (filename == nullptr) ? "<empty file name>" : filename; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 323 | env->ThrowNew(fnfe.get(), message); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 324 | return OatFileAssistant::kNoDexOptNeeded; |
Brian Carlstrom | 1d9f52b | 2011-10-13 10:50:45 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 327 | const InstructionSet target_instruction_set = GetInstructionSetFromString(instruction_set); |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 328 | if (target_instruction_set == kNone) { |
| 329 | ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException")); |
| 330 | std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set)); |
| 331 | env->ThrowNew(iae.get(), message.c_str()); |
| 332 | return 0; |
| 333 | } |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 334 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 335 | // TODO: Verify the dex location is well formed, and throw an IOException if |
| 336 | // not? |
| 337 | |
| 338 | OatFileAssistant oat_file_assistant(filename, target_instruction_set, false, pkgname); |
| 339 | |
| 340 | // Always treat elements of the bootclasspath as up-to-date. |
| 341 | if (oat_file_assistant.IsInBootClassPath()) { |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 342 | return OatFileAssistant::kNoDexOptNeeded; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | // TODO: Checking the profile should probably be done in the GetStatus() |
| 346 | // function. We have it here because GetStatus() should not be copying |
| 347 | // profile files. But who should be copying profile files? |
| 348 | if (oat_file_assistant.OdexFileIsOutOfDate()) { |
| 349 | // Needs recompile if profile has changed significantly. |
| 350 | if (Runtime::Current()->GetProfilerOptions().IsEnabled()) { |
| 351 | if (oat_file_assistant.IsProfileChangeSignificant()) { |
| 352 | if (!defer) { |
| 353 | oat_file_assistant.CopyProfileFile(); |
| 354 | } |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 355 | return OatFileAssistant::kDex2OatNeeded; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 356 | } else if (oat_file_assistant.ProfileExists() |
| 357 | && !oat_file_assistant.OldProfileExists()) { |
| 358 | if (!defer) { |
| 359 | oat_file_assistant.CopyProfileFile(); |
| 360 | } |
| 361 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 362 | } |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 363 | } |
| 364 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 365 | return oat_file_assistant.GetDexOptNeeded(); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Nicolas Geoffray | 72da5e7 | 2015-10-13 07:26:45 +0000 | [diff] [blame] | 368 | static jint DexFile_getDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename, |
| 369 | jstring javaPkgname, jstring javaInstructionSet, jboolean defer) { |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 370 | ScopedUtfChars filename(env, javaFilename); |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 371 | if (env->ExceptionCheck()) { |
| 372 | return 0; |
| 373 | } |
| 374 | |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 375 | NullableScopedUtfChars pkgname(env, javaPkgname); |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 376 | |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 377 | ScopedUtfChars instruction_set(env, javaInstructionSet); |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 378 | if (env->ExceptionCheck()) { |
| 379 | return 0; |
| 380 | } |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 381 | |
Nicolas Geoffray | 72da5e7 | 2015-10-13 07:26:45 +0000 | [diff] [blame] | 382 | return GetDexOptNeeded(env, filename.c_str(), pkgname.c_str(), |
| 383 | instruction_set.c_str(), defer); |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 384 | } |
| 385 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 386 | // public API, null pkgname |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 387 | static jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) { |
| 388 | const char* instruction_set = GetInstructionSetString(kRuntimeISA); |
| 389 | ScopedUtfChars filename(env, javaFilename); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 390 | jint status = GetDexOptNeeded(env, filename.c_str(), nullptr /* pkgname */, |
| 391 | instruction_set, false /* defer */); |
| 392 | return (status != OatFileAssistant::kNoDexOptNeeded) ? JNI_TRUE : JNI_FALSE; |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 393 | } |
| 394 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 395 | static JNINativeMethod gMethods[] = { |
Mathieu Chartier | 1d7d7f1 | 2015-09-25 16:48:57 -0700 | [diff] [blame] | 396 | NATIVE_METHOD(DexFile, closeDexFile, "(Ljava/lang/Object;)Z"), |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 397 | NATIVE_METHOD(DexFile, defineClassNative, |
| 398 | "(Ljava/lang/String;Ljava/lang/ClassLoader;Ljava/lang/Object;)Ljava/lang/Class;"), |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 399 | NATIVE_METHOD(DexFile, getClassNameList, "(Ljava/lang/Object;)[Ljava/lang/String;"), |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 400 | NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"), |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 401 | NATIVE_METHOD(DexFile, getDexOptNeeded, |
| 402 | "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Z)I"), |
| 403 | NATIVE_METHOD(DexFile, openDexFileNative, |
| 404 | "(Ljava/lang/String;Ljava/lang/String;I)Ljava/lang/Object;"), |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 405 | }; |
| 406 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 407 | void register_dalvik_system_DexFile(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 408 | REGISTER_NATIVE_METHODS("dalvik/system/DexFile"); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | } // namespace art |