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 | |
Narayan Kamath | 8943c1d | 2016-05-02 13:14:48 +0100 | [diff] [blame] | 19 | #include <sstream> |
| 20 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 21 | #include "base/logging.h" |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 22 | #include "base/stl_util.h" |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 23 | #include "base/stringprintf.h" |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 24 | #include "class_linker.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 25 | #include "common_throws.h" |
Andreas Gampe | c38be81 | 2016-03-23 15:03:46 -0700 | [diff] [blame] | 26 | #include "compiler_filter.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 27 | #include "dex_file-inl.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 28 | #include "jni_internal.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 29 | #include "mirror/class_loader.h" |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 30 | #include "mirror/object-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 31 | #include "mirror/string.h" |
Narayan Kamath | 8943c1d | 2016-05-02 13:14:48 +0100 | [diff] [blame] | 32 | #include "oat_file.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 33 | #include "oat_file_assistant.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 34 | #include "oat_file_manager.h" |
Brian Carlstrom | 1d9f52b | 2011-10-13 10:50:45 -0700 | [diff] [blame] | 35 | #include "os.h" |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 36 | #include "runtime.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 37 | #include "scoped_thread_state_change.h" |
Ian Rogers | c981848 | 2012-01-11 08:52:51 -0800 | [diff] [blame] | 38 | #include "ScopedLocalRef.h" |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 39 | #include "ScopedUtfChars.h" |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 40 | #include "utils.h" |
Ian Rogers | dd157d7 | 2014-05-15 14:47:50 -0700 | [diff] [blame] | 41 | #include "well_known_classes.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 42 | #include "zip_archive.h" |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 43 | |
| 44 | namespace art { |
| 45 | |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 46 | static bool ConvertJavaArrayToDexFiles( |
| 47 | JNIEnv* env, |
| 48 | jobject arrayObject, |
| 49 | /*out*/ std::vector<const DexFile*>& dex_files, |
| 50 | /*out*/ const OatFile*& oat_file) { |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 51 | jarray array = reinterpret_cast<jarray>(arrayObject); |
| 52 | |
| 53 | jsize array_size = env->GetArrayLength(array); |
| 54 | if (env->ExceptionCheck() == JNI_TRUE) { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 55 | return false; |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | // TODO: Optimize. On 32bit we can use an int array. |
| 59 | jboolean is_long_data_copied; |
| 60 | jlong* long_data = env->GetLongArrayElements(reinterpret_cast<jlongArray>(array), |
| 61 | &is_long_data_copied); |
| 62 | if (env->ExceptionCheck() == JNI_TRUE) { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 63 | return false; |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 66 | oat_file = reinterpret_cast<const OatFile*>(static_cast<uintptr_t>(long_data[kOatFileIndex])); |
| 67 | dex_files.reserve(array_size - 1); |
| 68 | for (jsize i = kDexFileIndexStart; i < array_size; ++i) { |
| 69 | dex_files.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] | 70 | } |
| 71 | |
| 72 | env->ReleaseLongArrayElements(reinterpret_cast<jlongArray>(array), long_data, JNI_ABORT); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 73 | return env->ExceptionCheck() != JNI_TRUE; |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 76 | static jlongArray ConvertDexFilesToJavaArray(JNIEnv* env, |
| 77 | const OatFile* oat_file, |
| 78 | std::vector<std::unique_ptr<const DexFile>>& vec) { |
| 79 | // Add one for the oat file. |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 80 | jlongArray long_array = env->NewLongArray(static_cast<jsize>(kDexFileIndexStart + vec.size())); |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 81 | if (env->ExceptionCheck() == JNI_TRUE) { |
| 82 | return nullptr; |
| 83 | } |
| 84 | |
| 85 | jboolean is_long_data_copied; |
| 86 | jlong* long_data = env->GetLongArrayElements(long_array, &is_long_data_copied); |
| 87 | if (env->ExceptionCheck() == JNI_TRUE) { |
| 88 | return nullptr; |
| 89 | } |
| 90 | |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 91 | long_data[kOatFileIndex] = reinterpret_cast<uintptr_t>(oat_file); |
| 92 | for (size_t i = 0; i < vec.size(); ++i) { |
| 93 | long_data[kDexFileIndexStart + i] = reinterpret_cast<uintptr_t>(vec[i].get()); |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | env->ReleaseLongArrayElements(long_array, long_data, 0); |
| 97 | if (env->ExceptionCheck() == JNI_TRUE) { |
| 98 | return nullptr; |
| 99 | } |
| 100 | |
| 101 | // Now release all the unique_ptrs. |
| 102 | for (auto& dex_file : vec) { |
| 103 | dex_file.release(); |
| 104 | } |
| 105 | |
| 106 | return long_array; |
| 107 | } |
| 108 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 109 | // A smart pointer that provides read-only access to a Java string's UTF chars. |
| 110 | // Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if |
| 111 | // passed a null jstring. The correct idiom is: |
| 112 | // |
| 113 | // NullableScopedUtfChars name(env, javaName); |
Brian Carlstrom | c252c3e | 2011-10-16 23:21:02 -0700 | [diff] [blame] | 114 | // if (env->ExceptionCheck()) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 115 | // return null; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 116 | // } |
| 117 | // // ... use name.c_str() |
| 118 | // |
| 119 | // TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option. |
| 120 | class NullableScopedUtfChars { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 121 | public: |
| 122 | NullableScopedUtfChars(JNIEnv* env, jstring s) : mEnv(env), mString(s) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 123 | mUtfChars = (s != nullptr) ? env->GetStringUTFChars(s, nullptr) : nullptr; |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | ~NullableScopedUtfChars() { |
| 127 | if (mUtfChars) { |
| 128 | mEnv->ReleaseStringUTFChars(mString, mUtfChars); |
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 | } |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 131 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 132 | const char* c_str() const { |
| 133 | return mUtfChars; |
| 134 | } |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 135 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 136 | size_t size() const { |
| 137 | return strlen(mUtfChars); |
| 138 | } |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 139 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 140 | // Element access. |
| 141 | const char& operator[](size_t n) const { |
| 142 | return mUtfChars[n]; |
| 143 | } |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 144 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 145 | private: |
| 146 | JNIEnv* mEnv; |
| 147 | jstring mString; |
| 148 | const char* mUtfChars; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 149 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 150 | // Disallow copy and assignment. |
| 151 | NullableScopedUtfChars(const NullableScopedUtfChars&); |
| 152 | void operator=(const NullableScopedUtfChars&); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 153 | }; |
| 154 | |
Mathieu Chartier | b190d94 | 2015-11-12 10:00:58 -0800 | [diff] [blame] | 155 | static jobject DexFile_openDexFileNative(JNIEnv* env, |
| 156 | jclass, |
| 157 | jstring javaSourceName, |
| 158 | jstring javaOutputName, |
| 159 | jint flags ATTRIBUTE_UNUSED, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 160 | jobject class_loader, |
| 161 | jobjectArray dex_elements) { |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 162 | ScopedUtfChars sourceName(env, javaSourceName); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 163 | if (sourceName.c_str() == nullptr) { |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 164 | return 0; |
| 165 | } |
| 166 | NullableScopedUtfChars outputName(env, javaOutputName); |
Brian Carlstrom | c252c3e | 2011-10-16 23:21:02 -0700 | [diff] [blame] | 167 | if (env->ExceptionCheck()) { |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 168 | return 0; |
| 169 | } |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 170 | Runtime* const runtime = Runtime::Current(); |
| 171 | ClassLinker* linker = runtime->GetClassLinker(); |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 172 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 173 | std::vector<std::string> error_msgs; |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 174 | const OatFile* oat_file = nullptr; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 175 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 176 | dex_files = runtime->GetOatFileManager().OpenDexFilesFromOat(sourceName.c_str(), |
| 177 | outputName.c_str(), |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 178 | class_loader, |
| 179 | dex_elements, |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 180 | /*out*/ &oat_file, |
| 181 | /*out*/ &error_msgs); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 182 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 183 | if (!dex_files.empty()) { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 184 | jlongArray array = ConvertDexFilesToJavaArray(env, oat_file, dex_files); |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 185 | if (array == nullptr) { |
| 186 | ScopedObjectAccess soa(env); |
| 187 | for (auto& dex_file : dex_files) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 188 | if (linker->FindDexCache(soa.Self(), *dex_file, true) != nullptr) { |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 189 | dex_file.release(); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | return array; |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 194 | } else { |
Vladimir Marko | 60836d5 | 2014-01-16 15:53:38 +0000 | [diff] [blame] | 195 | ScopedObjectAccess soa(env); |
Andreas Gampe | 329d188 | 2014-04-08 10:32:19 -0700 | [diff] [blame] | 196 | CHECK(!error_msgs.empty()); |
| 197 | // The most important message is at the end. So set up nesting by going forward, which will |
| 198 | // wrap the existing exception as a cause for the following one. |
| 199 | auto it = error_msgs.begin(); |
| 200 | auto itEnd = error_msgs.end(); |
| 201 | for ( ; it != itEnd; ++it) { |
| 202 | ThrowWrappedIOException("%s", it->c_str()); |
| 203 | } |
| 204 | |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 205 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 206 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Mathieu Chartier | 1d7d7f1 | 2015-09-25 16:48:57 -0700 | [diff] [blame] | 209 | static jboolean DexFile_closeDexFile(JNIEnv* env, jclass, jobject cookie) { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 210 | std::vector<const DexFile*> dex_files; |
| 211 | const OatFile* oat_file; |
| 212 | if (!ConvertJavaArrayToDexFiles(env, cookie, dex_files, oat_file)) { |
| 213 | Thread::Current()->AssertPendingException(); |
Mathieu Chartier | 1d7d7f1 | 2015-09-25 16:48:57 -0700 | [diff] [blame] | 214 | return JNI_FALSE; |
| 215 | } |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 216 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | 1d7d7f1 | 2015-09-25 16:48:57 -0700 | [diff] [blame] | 217 | bool all_deleted = true; |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 218 | { |
| 219 | ScopedObjectAccess soa(env); |
| 220 | mirror::Object* dex_files_object = soa.Decode<mirror::Object*>(cookie); |
| 221 | mirror::LongArray* long_dex_files = dex_files_object->AsLongArray(); |
| 222 | // Delete dex files associated with this dalvik.system.DexFile since there should not be running |
| 223 | // code using it. dex_files is a vector due to multidex. |
| 224 | ClassLinker* const class_linker = runtime->GetClassLinker(); |
| 225 | int32_t i = kDexFileIndexStart; // Oat file is at index 0. |
| 226 | for (const DexFile* dex_file : dex_files) { |
| 227 | if (dex_file != nullptr) { |
| 228 | // Only delete the dex file if the dex cache is not found to prevent runtime crashes if there |
| 229 | // are calls to DexFile.close while the ART DexFile is still in use. |
| 230 | if (class_linker->FindDexCache(soa.Self(), *dex_file, true) == nullptr) { |
| 231 | // Clear the element in the array so that we can call close again. |
| 232 | long_dex_files->Set(i, 0); |
| 233 | delete dex_file; |
| 234 | } else { |
| 235 | all_deleted = false; |
| 236 | } |
| 237 | } |
| 238 | ++i; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 239 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 240 | } |
Mathieu Chartier | 1d7d7f1 | 2015-09-25 16:48:57 -0700 | [diff] [blame] | 241 | |
Mathieu Chartier | fdccbd4 | 2015-10-14 10:58:41 -0700 | [diff] [blame] | 242 | // oat_file can be null if we are running without dex2oat. |
| 243 | if (all_deleted && oat_file != nullptr) { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 244 | // If all of the dex files are no longer in use we can unmap the corresponding oat file. |
| 245 | VLOG(class_linker) << "Unregistering " << oat_file; |
| 246 | runtime->GetOatFileManager().UnRegisterAndDeleteOatFile(oat_file); |
| 247 | } |
Mathieu Chartier | 1d7d7f1 | 2015-09-25 16:48:57 -0700 | [diff] [blame] | 248 | return all_deleted ? JNI_TRUE : JNI_FALSE; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 251 | static jclass DexFile_defineClassNative(JNIEnv* env, |
| 252 | jclass, |
| 253 | jstring javaName, |
| 254 | jobject javaLoader, |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 255 | jobject cookie, |
| 256 | jobject dexFile) { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 257 | std::vector<const DexFile*> dex_files; |
| 258 | const OatFile* oat_file; |
| 259 | if (!ConvertJavaArrayToDexFiles(env, cookie, /*out*/ dex_files, /*out*/ oat_file)) { |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 260 | VLOG(class_linker) << "Failed to find dex_file"; |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 261 | DCHECK(env->ExceptionCheck()); |
| 262 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 263 | } |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 264 | |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 265 | ScopedUtfChars class_name(env, javaName); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 266 | if (class_name.c_str() == nullptr) { |
Brian Carlstrom | 2e450bf | 2013-09-06 15:39:46 -0700 | [diff] [blame] | 267 | VLOG(class_linker) << "Failed to find class_name"; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 268 | return nullptr; |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 269 | } |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 270 | const std::string descriptor(DotToDescriptor(class_name.c_str())); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 271 | const size_t hash(ComputeModifiedUtf8Hash(descriptor.c_str())); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 272 | for (auto& dex_file : dex_files) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 273 | 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] | 274 | if (dex_class_def != nullptr) { |
| 275 | ScopedObjectAccess soa(env); |
| 276 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 277 | StackHandleScope<1> hs(soa.Self()); |
| 278 | Handle<mirror::ClassLoader> class_loader( |
| 279 | hs.NewHandle(soa.Decode<mirror::ClassLoader*>(javaLoader))); |
Mathieu Chartier | f284d44 | 2016-06-02 11:48:30 -0700 | [diff] [blame] | 280 | class_linker->RegisterDexFile(*dex_file, class_loader.Get()); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 281 | mirror::Class* result = class_linker->DefineClass(soa.Self(), |
| 282 | descriptor.c_str(), |
| 283 | hash, |
| 284 | class_loader, |
| 285 | *dex_file, |
| 286 | *dex_class_def); |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 287 | // Add the used dex file. This only required for the DexFile.loadClass API since normal |
| 288 | // class loaders already keep their dex files live. |
| 289 | class_linker->InsertDexFileInToClassLoader(soa.Decode<mirror::Object*>(dexFile), |
| 290 | class_loader.Get()); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 291 | if (result != nullptr) { |
Brian Carlstrom | 667ab7c | 2014-10-16 19:12:28 -0700 | [diff] [blame] | 292 | VLOG(class_linker) << "DexFile_defineClassNative returning " << result |
| 293 | << " for " << class_name.c_str(); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 294 | return soa.AddLocalReference<jclass>(result); |
| 295 | } |
| 296 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 297 | } |
Brian Carlstrom | 667ab7c | 2014-10-16 19:12:28 -0700 | [diff] [blame] | 298 | 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] | 299 | return nullptr; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 302 | // Needed as a compare functor for sets of const char |
| 303 | struct CharPointerComparator { |
| 304 | bool operator()(const char *str1, const char *str2) const { |
| 305 | return strcmp(str1, str2) < 0; |
| 306 | } |
| 307 | }; |
| 308 | |
| 309 | // 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] | 310 | static jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jobject cookie) { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 311 | const OatFile* oat_file = nullptr; |
| 312 | std::vector<const DexFile*> dex_files; |
| 313 | if (!ConvertJavaArrayToDexFiles(env, cookie, /*out */ dex_files, /* out */ oat_file)) { |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 314 | DCHECK(env->ExceptionCheck()); |
| 315 | return nullptr; |
| 316 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 317 | |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 318 | // Push all class descriptors into a set. Use set instead of unordered_set as we want to |
| 319 | // retrieve all in the end. |
| 320 | std::set<const char*, CharPointerComparator> descriptors; |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 321 | for (auto& dex_file : dex_files) { |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 322 | for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) { |
| 323 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(i); |
| 324 | const char* descriptor = dex_file->GetClassDescriptor(class_def); |
| 325 | descriptors.insert(descriptor); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 326 | } |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 327 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 328 | |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 329 | // Now create output array and copy the set into it. |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 330 | jobjectArray result = env->NewObjectArray(descriptors.size(), |
| 331 | WellKnownClasses::java_lang_String, |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 332 | nullptr); |
| 333 | if (result != nullptr) { |
| 334 | auto it = descriptors.begin(); |
| 335 | auto it_end = descriptors.end(); |
| 336 | jsize i = 0; |
| 337 | for (; it != it_end; it++, ++i) { |
| 338 | std::string descriptor(DescriptorToDot(*it)); |
| 339 | ScopedLocalRef<jstring> jdescriptor(env, env->NewStringUTF(descriptor.c_str())); |
| 340 | if (jdescriptor.get() == nullptr) { |
| 341 | return nullptr; |
Ian Rogers | dd157d7 | 2014-05-15 14:47:50 -0700 | [diff] [blame] | 342 | } |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 343 | env->SetObjectArrayElement(result, i, jdescriptor.get()); |
Ian Rogers | dd157d7 | 2014-05-15 14:47:50 -0700 | [diff] [blame] | 344 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 345 | } |
Ian Rogers | dd157d7 | 2014-05-15 14:47:50 -0700 | [diff] [blame] | 346 | return result; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 349 | static jint GetDexOptNeeded(JNIEnv* env, |
| 350 | const char* filename, |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 351 | const char* instruction_set, |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 352 | const char* compiler_filter_name, |
| 353 | bool profile_changed) { |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 354 | if ((filename == nullptr) || !OS::FileExists(filename)) { |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 355 | LOG(ERROR) << "DexFile_getDexOptNeeded file '" << filename << "' does not exist"; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 356 | ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException")); |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 357 | const char* message = (filename == nullptr) ? "<empty file name>" : filename; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 358 | env->ThrowNew(fnfe.get(), message); |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 359 | return -1; |
Brian Carlstrom | 1d9f52b | 2011-10-13 10:50:45 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 362 | const InstructionSet target_instruction_set = GetInstructionSetFromString(instruction_set); |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 363 | if (target_instruction_set == kNone) { |
| 364 | ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException")); |
| 365 | std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set)); |
| 366 | env->ThrowNew(iae.get(), message.c_str()); |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 367 | return -1; |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 368 | } |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 369 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 370 | CompilerFilter::Filter filter; |
| 371 | if (!CompilerFilter::ParseCompilerFilter(compiler_filter_name, &filter)) { |
| 372 | ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException")); |
| 373 | std::string message(StringPrintf("Compiler filter %s is invalid.", compiler_filter_name)); |
| 374 | env->ThrowNew(iae.get(), message.c_str()); |
| 375 | return -1; |
| 376 | } |
| 377 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 378 | // TODO: Verify the dex location is well formed, and throw an IOException if |
| 379 | // not? |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 380 | |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 381 | OatFileAssistant oat_file_assistant(filename, target_instruction_set, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 382 | |
| 383 | // Always treat elements of the bootclasspath as up-to-date. |
| 384 | if (oat_file_assistant.IsInBootClassPath()) { |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 385 | return OatFileAssistant::kNoDexOptNeeded; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 386 | } |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 387 | return oat_file_assistant.GetDexOptNeeded(filter, profile_changed); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Narayan Kamath | 8943c1d | 2016-05-02 13:14:48 +0100 | [diff] [blame] | 390 | static jstring DexFile_getDexFileStatus(JNIEnv* env, |
| 391 | jclass, |
| 392 | jstring javaFilename, |
| 393 | jstring javaInstructionSet) { |
| 394 | ScopedUtfChars filename(env, javaFilename); |
| 395 | if (env->ExceptionCheck()) { |
| 396 | return nullptr; |
| 397 | } |
| 398 | |
| 399 | ScopedUtfChars instruction_set(env, javaInstructionSet); |
| 400 | if (env->ExceptionCheck()) { |
| 401 | return nullptr; |
| 402 | } |
| 403 | |
| 404 | const InstructionSet target_instruction_set = GetInstructionSetFromString( |
| 405 | instruction_set.c_str()); |
| 406 | if (target_instruction_set == kNone) { |
| 407 | ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException")); |
| 408 | std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set.c_str())); |
| 409 | env->ThrowNew(iae.get(), message.c_str()); |
| 410 | return nullptr; |
| 411 | } |
| 412 | |
| 413 | OatFileAssistant oat_file_assistant(filename.c_str(), target_instruction_set, |
Narayan Kamath | 8943c1d | 2016-05-02 13:14:48 +0100 | [diff] [blame] | 414 | false /* load_executable */); |
| 415 | |
| 416 | std::ostringstream status; |
| 417 | bool oat_file_exists = false; |
| 418 | bool odex_file_exists = false; |
| 419 | if (oat_file_assistant.OatFileExists()) { |
| 420 | oat_file_exists = true; |
| 421 | status << *oat_file_assistant.OatFileName() << " [compilation_filter="; |
| 422 | status << CompilerFilter::NameOfFilter(oat_file_assistant.OatFileCompilerFilter()); |
| 423 | status << ", status=" << oat_file_assistant.OatFileStatus(); |
| 424 | } |
| 425 | |
| 426 | if (oat_file_assistant.OdexFileExists()) { |
| 427 | odex_file_exists = true; |
| 428 | if (oat_file_exists) { |
| 429 | status << "] "; |
| 430 | } |
| 431 | status << *oat_file_assistant.OdexFileName() << " [compilation_filter="; |
| 432 | status << CompilerFilter::NameOfFilter(oat_file_assistant.OdexFileCompilerFilter()); |
| 433 | status << ", status=" << oat_file_assistant.OdexFileStatus(); |
| 434 | } |
| 435 | |
| 436 | if (!oat_file_exists && !odex_file_exists) { |
| 437 | status << "invalid["; |
| 438 | } |
| 439 | |
| 440 | status << "]"; |
| 441 | return env->NewStringUTF(status.str().c_str()); |
| 442 | } |
| 443 | |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 444 | static jint DexFile_getDexOptNeeded(JNIEnv* env, |
| 445 | jclass, |
| 446 | jstring javaFilename, |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 447 | jstring javaInstructionSet, |
Andreas Gampe | c38be81 | 2016-03-23 15:03:46 -0700 | [diff] [blame] | 448 | jstring javaTargetCompilerFilter, |
| 449 | jboolean newProfile) { |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 450 | ScopedUtfChars filename(env, javaFilename); |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 451 | if (env->ExceptionCheck()) { |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 452 | return -1; |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 455 | ScopedUtfChars instruction_set(env, javaInstructionSet); |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 456 | if (env->ExceptionCheck()) { |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 457 | return -1; |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 458 | } |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 459 | |
Andreas Gampe | c38be81 | 2016-03-23 15:03:46 -0700 | [diff] [blame] | 460 | ScopedUtfChars target_compiler_filter(env, javaTargetCompilerFilter); |
| 461 | if (env->ExceptionCheck()) { |
| 462 | return -1; |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 463 | } |
Andreas Gampe | c38be81 | 2016-03-23 15:03:46 -0700 | [diff] [blame] | 464 | |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 465 | return GetDexOptNeeded(env, |
| 466 | filename.c_str(), |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 467 | instruction_set.c_str(), |
Andreas Gampe | c38be81 | 2016-03-23 15:03:46 -0700 | [diff] [blame] | 468 | target_compiler_filter.c_str(), |
| 469 | newProfile == JNI_TRUE); |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 470 | } |
| 471 | |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 472 | // public API |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 473 | static jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) { |
Richard Uhler | 06e3f4f | 2016-05-24 15:42:37 -0700 | [diff] [blame] | 474 | ScopedUtfChars filename_utf(env, javaFilename); |
| 475 | if (env->ExceptionCheck()) { |
| 476 | return JNI_FALSE; |
| 477 | } |
| 478 | |
| 479 | const char* filename = filename_utf.c_str(); |
| 480 | if ((filename == nullptr) || !OS::FileExists(filename)) { |
| 481 | LOG(ERROR) << "DexFile_isDexOptNeeded file '" << filename << "' does not exist"; |
| 482 | ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException")); |
| 483 | const char* message = (filename == nullptr) ? "<empty file name>" : filename; |
| 484 | env->ThrowNew(fnfe.get(), message); |
| 485 | return JNI_FALSE; |
| 486 | } |
| 487 | |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 488 | OatFileAssistant oat_file_assistant(filename, kRuntimeISA, false); |
Richard Uhler | 06e3f4f | 2016-05-24 15:42:37 -0700 | [diff] [blame] | 489 | return oat_file_assistant.IsUpToDate() ? JNI_FALSE : JNI_TRUE; |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 490 | } |
| 491 | |
Andreas Gampe | c38be81 | 2016-03-23 15:03:46 -0700 | [diff] [blame] | 492 | static jboolean DexFile_isValidCompilerFilter(JNIEnv* env, |
| 493 | jclass javeDexFileClass ATTRIBUTE_UNUSED, |
| 494 | jstring javaCompilerFilter) { |
| 495 | ScopedUtfChars compiler_filter(env, javaCompilerFilter); |
| 496 | if (env->ExceptionCheck()) { |
| 497 | return -1; |
| 498 | } |
| 499 | |
| 500 | CompilerFilter::Filter filter; |
| 501 | return CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter) |
| 502 | ? JNI_TRUE : JNI_FALSE; |
| 503 | } |
| 504 | |
| 505 | static jboolean DexFile_isProfileGuidedCompilerFilter(JNIEnv* env, |
| 506 | jclass javeDexFileClass ATTRIBUTE_UNUSED, |
| 507 | jstring javaCompilerFilter) { |
| 508 | ScopedUtfChars compiler_filter(env, javaCompilerFilter); |
| 509 | if (env->ExceptionCheck()) { |
| 510 | return -1; |
| 511 | } |
| 512 | |
| 513 | CompilerFilter::Filter filter; |
| 514 | if (!CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)) { |
| 515 | return JNI_FALSE; |
| 516 | } |
| 517 | return CompilerFilter::DependsOnProfile(filter) ? JNI_TRUE : JNI_FALSE; |
| 518 | } |
| 519 | |
Andreas Gampe | 86a785d | 2016-03-30 17:19:48 -0700 | [diff] [blame] | 520 | static jstring DexFile_getNonProfileGuidedCompilerFilter(JNIEnv* env, |
| 521 | jclass javeDexFileClass ATTRIBUTE_UNUSED, |
| 522 | jstring javaCompilerFilter) { |
| 523 | ScopedUtfChars compiler_filter(env, javaCompilerFilter); |
| 524 | if (env->ExceptionCheck()) { |
| 525 | return nullptr; |
| 526 | } |
| 527 | |
| 528 | CompilerFilter::Filter filter; |
| 529 | if (!CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)) { |
| 530 | return javaCompilerFilter; |
| 531 | } |
| 532 | |
| 533 | CompilerFilter::Filter new_filter = CompilerFilter::GetNonProfileDependentFilterFrom(filter); |
| 534 | |
| 535 | // Filter stayed the same, return input. |
| 536 | if (filter == new_filter) { |
| 537 | return javaCompilerFilter; |
| 538 | } |
| 539 | |
| 540 | // Create a new string object and return. |
| 541 | std::string new_filter_str = CompilerFilter::NameOfFilter(new_filter); |
| 542 | return env->NewStringUTF(new_filter_str.c_str()); |
| 543 | } |
| 544 | |
Jeff Hao | 90671be | 2016-04-22 14:00:06 -0700 | [diff] [blame] | 545 | static jboolean DexFile_isBackedByOatFile(JNIEnv* env, jclass, jobject cookie) { |
| 546 | const OatFile* oat_file = nullptr; |
| 547 | std::vector<const DexFile*> dex_files; |
| 548 | if (!ConvertJavaArrayToDexFiles(env, cookie, /*out */ dex_files, /* out */ oat_file)) { |
| 549 | DCHECK(env->ExceptionCheck()); |
| 550 | return false; |
| 551 | } |
| 552 | return oat_file != nullptr; |
| 553 | } |
| 554 | |
Philip Cuadra | b55ad7c | 2016-07-12 16:37:40 -0700 | [diff] [blame] | 555 | static jstring DexFile_getDexFileOutputPath(JNIEnv* env, |
| 556 | jclass, |
| 557 | jstring javaFilename, |
| 558 | jstring javaInstructionSet) { |
| 559 | ScopedUtfChars filename(env, javaFilename); |
| 560 | if (env->ExceptionCheck()) { |
| 561 | return nullptr; |
| 562 | } |
| 563 | |
| 564 | ScopedUtfChars instruction_set(env, javaInstructionSet); |
| 565 | if (env->ExceptionCheck()) { |
| 566 | return nullptr; |
| 567 | } |
| 568 | |
| 569 | const InstructionSet target_instruction_set = GetInstructionSetFromString( |
| 570 | instruction_set.c_str()); |
| 571 | if (target_instruction_set == kNone) { |
| 572 | ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException")); |
| 573 | std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set.c_str())); |
| 574 | env->ThrowNew(iae.get(), message.c_str()); |
| 575 | return nullptr; |
| 576 | } |
| 577 | |
| 578 | OatFileAssistant oat_file_assistant(filename.c_str(), |
| 579 | target_instruction_set, |
Philip Cuadra | b55ad7c | 2016-07-12 16:37:40 -0700 | [diff] [blame] | 580 | false /* load_executable */); |
| 581 | |
| 582 | std::unique_ptr<OatFile> best_oat_file = oat_file_assistant.GetBestOatFile(); |
| 583 | if (best_oat_file == nullptr) { |
| 584 | return nullptr; |
| 585 | } |
| 586 | |
| 587 | return env->NewStringUTF(best_oat_file->GetLocation().c_str()); |
| 588 | } |
| 589 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 590 | static JNINativeMethod gMethods[] = { |
Mathieu Chartier | 1d7d7f1 | 2015-09-25 16:48:57 -0700 | [diff] [blame] | 591 | NATIVE_METHOD(DexFile, closeDexFile, "(Ljava/lang/Object;)Z"), |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 592 | NATIVE_METHOD(DexFile, |
| 593 | defineClassNative, |
| 594 | "(Ljava/lang/String;" |
| 595 | "Ljava/lang/ClassLoader;" |
| 596 | "Ljava/lang/Object;" |
| 597 | "Ldalvik/system/DexFile;" |
| 598 | ")Ljava/lang/Class;"), |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 599 | NATIVE_METHOD(DexFile, getClassNameList, "(Ljava/lang/Object;)[Ljava/lang/String;"), |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 600 | NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"), |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 601 | NATIVE_METHOD(DexFile, getDexOptNeeded, |
Andreas Gampe | c38be81 | 2016-03-23 15:03:46 -0700 | [diff] [blame] | 602 | "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Z)I"), |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 603 | NATIVE_METHOD(DexFile, openDexFileNative, |
Mathieu Chartier | 689a700 | 2015-11-20 10:29:42 -0800 | [diff] [blame] | 604 | "(Ljava/lang/String;" |
| 605 | "Ljava/lang/String;" |
| 606 | "I" |
| 607 | "Ljava/lang/ClassLoader;" |
| 608 | "[Ldalvik/system/DexPathList$Element;" |
| 609 | ")Ljava/lang/Object;"), |
Andreas Gampe | c38be81 | 2016-03-23 15:03:46 -0700 | [diff] [blame] | 610 | NATIVE_METHOD(DexFile, isValidCompilerFilter, "(Ljava/lang/String;)Z"), |
| 611 | NATIVE_METHOD(DexFile, isProfileGuidedCompilerFilter, "(Ljava/lang/String;)Z"), |
Andreas Gampe | 86a785d | 2016-03-30 17:19:48 -0700 | [diff] [blame] | 612 | NATIVE_METHOD(DexFile, |
| 613 | getNonProfileGuidedCompilerFilter, |
| 614 | "(Ljava/lang/String;)Ljava/lang/String;"), |
Jeff Hao | 90671be | 2016-04-22 14:00:06 -0700 | [diff] [blame] | 615 | NATIVE_METHOD(DexFile, isBackedByOatFile, "(Ljava/lang/Object;)Z"), |
Narayan Kamath | 8943c1d | 2016-05-02 13:14:48 +0100 | [diff] [blame] | 616 | NATIVE_METHOD(DexFile, getDexFileStatus, |
Philip Cuadra | b55ad7c | 2016-07-12 16:37:40 -0700 | [diff] [blame] | 617 | "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"), |
| 618 | NATIVE_METHOD(DexFile, getDexFileOutputPath, |
Narayan Kamath | 8943c1d | 2016-05-02 13:14:48 +0100 | [diff] [blame] | 619 | "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;") |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 620 | }; |
| 621 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 622 | void register_dalvik_system_DexFile(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 623 | REGISTER_NATIVE_METHODS("dalvik/system/DexFile"); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | } // namespace art |