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 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 21 | #include "android-base/stringprintf.h" |
| 22 | |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 23 | #include "base/file_utils.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 24 | #include "base/logging.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 25 | #include "base/os.h" |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 26 | #include "base/stl_util.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 27 | #include "base/utils.h" |
David Sehr | 79e2607 | 2018-04-06 17:58:50 -0700 | [diff] [blame] | 28 | #include "base/zip_archive.h" |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 29 | #include "class_linker.h" |
Calin Juravle | 20c4644 | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 30 | #include <class_loader_context.h> |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 31 | #include "common_throws.h" |
Andreas Gampe | c38be81 | 2016-03-23 15:03:46 -0700 | [diff] [blame] | 32 | #include "compiler_filter.h" |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 33 | #include "dex/art_dex_file_loader.h" |
David Sehr | b2ec9f5 | 2018-02-21 13:20:31 -0800 | [diff] [blame] | 34 | #include "dex/descriptors_names.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 35 | #include "dex/dex_file-inl.h" |
| 36 | #include "dex/dex_file_loader.h" |
David Srbecky | fb3de3d | 2018-01-29 16:11:49 +0000 | [diff] [blame] | 37 | #include "jit/debugger_interface.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame^] | 38 | #include "jni/jni_internal.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 39 | #include "mirror/class_loader.h" |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 40 | #include "mirror/object-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 41 | #include "mirror/string.h" |
Andreas Gampe | 87583b3 | 2017-05-25 11:22:18 -0700 | [diff] [blame] | 42 | #include "native_util.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 43 | #include "nativehelper/jni_macros.h" |
Andreas Gampe | 373a9b5 | 2017-10-18 09:01:57 -0700 | [diff] [blame] | 44 | #include "nativehelper/scoped_local_ref.h" |
| 45 | #include "nativehelper/scoped_utf_chars.h" |
Narayan Kamath | 8943c1d | 2016-05-02 13:14:48 +0100 | [diff] [blame] | 46 | #include "oat_file.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 47 | #include "oat_file_assistant.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 48 | #include "oat_file_manager.h" |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 49 | #include "runtime.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 50 | #include "scoped_thread_state_change-inl.h" |
Ian Rogers | dd157d7 | 2014-05-15 14:47:50 -0700 | [diff] [blame] | 51 | #include "well_known_classes.h" |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 52 | |
| 53 | namespace art { |
| 54 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 55 | using android::base::StringPrintf; |
| 56 | |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 57 | static bool ConvertJavaArrayToDexFiles( |
| 58 | JNIEnv* env, |
| 59 | jobject arrayObject, |
| 60 | /*out*/ std::vector<const DexFile*>& dex_files, |
| 61 | /*out*/ const OatFile*& oat_file) { |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 62 | jarray array = reinterpret_cast<jarray>(arrayObject); |
| 63 | |
| 64 | jsize array_size = env->GetArrayLength(array); |
| 65 | if (env->ExceptionCheck() == JNI_TRUE) { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 66 | return false; |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // TODO: Optimize. On 32bit we can use an int array. |
| 70 | jboolean is_long_data_copied; |
| 71 | jlong* long_data = env->GetLongArrayElements(reinterpret_cast<jlongArray>(array), |
| 72 | &is_long_data_copied); |
| 73 | if (env->ExceptionCheck() == JNI_TRUE) { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 74 | return false; |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 77 | oat_file = reinterpret_cast<const OatFile*>(static_cast<uintptr_t>(long_data[kOatFileIndex])); |
| 78 | dex_files.reserve(array_size - 1); |
| 79 | for (jsize i = kDexFileIndexStart; i < array_size; ++i) { |
| 80 | 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] | 81 | } |
| 82 | |
| 83 | env->ReleaseLongArrayElements(reinterpret_cast<jlongArray>(array), long_data, JNI_ABORT); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 84 | return env->ExceptionCheck() != JNI_TRUE; |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 87 | static jlongArray ConvertDexFilesToJavaArray(JNIEnv* env, |
| 88 | const OatFile* oat_file, |
| 89 | std::vector<std::unique_ptr<const DexFile>>& vec) { |
| 90 | // Add one for the oat file. |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 91 | jlongArray long_array = env->NewLongArray(static_cast<jsize>(kDexFileIndexStart + vec.size())); |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 92 | if (env->ExceptionCheck() == JNI_TRUE) { |
| 93 | return nullptr; |
| 94 | } |
| 95 | |
| 96 | jboolean is_long_data_copied; |
| 97 | jlong* long_data = env->GetLongArrayElements(long_array, &is_long_data_copied); |
| 98 | if (env->ExceptionCheck() == JNI_TRUE) { |
| 99 | return nullptr; |
| 100 | } |
| 101 | |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 102 | long_data[kOatFileIndex] = reinterpret_cast<uintptr_t>(oat_file); |
| 103 | for (size_t i = 0; i < vec.size(); ++i) { |
| 104 | long_data[kDexFileIndexStart + i] = reinterpret_cast<uintptr_t>(vec[i].get()); |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | env->ReleaseLongArrayElements(long_array, long_data, 0); |
| 108 | if (env->ExceptionCheck() == JNI_TRUE) { |
| 109 | return nullptr; |
| 110 | } |
| 111 | |
| 112 | // Now release all the unique_ptrs. |
| 113 | for (auto& dex_file : vec) { |
| 114 | dex_file.release(); |
| 115 | } |
| 116 | |
| 117 | return long_array; |
| 118 | } |
| 119 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 120 | // A smart pointer that provides read-only access to a Java string's UTF chars. |
| 121 | // Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if |
| 122 | // passed a null jstring. The correct idiom is: |
| 123 | // |
| 124 | // NullableScopedUtfChars name(env, javaName); |
Brian Carlstrom | c252c3e | 2011-10-16 23:21:02 -0700 | [diff] [blame] | 125 | // if (env->ExceptionCheck()) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 126 | // return null; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 127 | // } |
| 128 | // // ... use name.c_str() |
| 129 | // |
| 130 | // TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option. |
| 131 | class NullableScopedUtfChars { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 132 | public: |
| 133 | NullableScopedUtfChars(JNIEnv* env, jstring s) : mEnv(env), mString(s) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 134 | mUtfChars = (s != nullptr) ? env->GetStringUTFChars(s, nullptr) : nullptr; |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | ~NullableScopedUtfChars() { |
| 138 | if (mUtfChars) { |
| 139 | mEnv->ReleaseStringUTFChars(mString, mUtfChars); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 140 | } |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 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 | const char* c_str() const { |
| 144 | return mUtfChars; |
| 145 | } |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 146 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 147 | size_t size() const { |
| 148 | return strlen(mUtfChars); |
| 149 | } |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 150 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 151 | // Element access. |
| 152 | const char& operator[](size_t n) const { |
| 153 | return mUtfChars[n]; |
| 154 | } |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 155 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 156 | private: |
| 157 | JNIEnv* mEnv; |
| 158 | jstring mString; |
| 159 | const char* mUtfChars; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 160 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 161 | // Disallow copy and assignment. |
| 162 | NullableScopedUtfChars(const NullableScopedUtfChars&); |
| 163 | void operator=(const NullableScopedUtfChars&); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 164 | }; |
| 165 | |
Alex Light | ea9465e | 2017-02-16 15:38:35 -0800 | [diff] [blame] | 166 | static std::unique_ptr<MemMap> AllocateDexMemoryMap(JNIEnv* env, jint start, jint end) { |
| 167 | if (end <= start) { |
| 168 | ScopedObjectAccess soa(env); |
| 169 | ThrowWrappedIOException("Bad range"); |
| 170 | return nullptr; |
| 171 | } |
| 172 | |
| 173 | std::string error_message; |
| 174 | size_t length = static_cast<size_t>(end - start); |
| 175 | std::unique_ptr<MemMap> dex_mem_map(MemMap::MapAnonymous("DEX data", |
| 176 | nullptr, |
| 177 | length, |
| 178 | PROT_READ | PROT_WRITE, |
| 179 | /* low_4gb */ false, |
| 180 | /* reuse */ false, |
| 181 | &error_message)); |
| 182 | if (dex_mem_map == nullptr) { |
| 183 | ScopedObjectAccess soa(env); |
| 184 | ThrowWrappedIOException("%s", error_message.c_str()); |
| 185 | } |
| 186 | return dex_mem_map; |
| 187 | } |
| 188 | |
| 189 | static const DexFile* CreateDexFile(JNIEnv* env, std::unique_ptr<MemMap> dex_mem_map) { |
| 190 | std::string location = StringPrintf("Anonymous-DexFile@%p-%p", |
| 191 | dex_mem_map->Begin(), |
| 192 | dex_mem_map->End()); |
| 193 | std::string error_message; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 194 | const ArtDexFileLoader dex_file_loader; |
| 195 | std::unique_ptr<const DexFile> dex_file(dex_file_loader.Open(location, |
| 196 | 0, |
| 197 | std::move(dex_mem_map), |
| 198 | /* verify */ true, |
| 199 | /* verify_location */ true, |
| 200 | &error_message)); |
Alex Light | ea9465e | 2017-02-16 15:38:35 -0800 | [diff] [blame] | 201 | if (dex_file == nullptr) { |
| 202 | ScopedObjectAccess soa(env); |
| 203 | ThrowWrappedIOException("%s", error_message.c_str()); |
| 204 | return nullptr; |
| 205 | } |
| 206 | |
| 207 | if (!dex_file->DisableWrite()) { |
| 208 | ScopedObjectAccess soa(env); |
| 209 | ThrowWrappedIOException("Failed to make dex file read-only"); |
| 210 | return nullptr; |
| 211 | } |
| 212 | |
| 213 | return dex_file.release(); |
| 214 | } |
| 215 | |
| 216 | static jobject CreateSingleDexFileCookie(JNIEnv* env, std::unique_ptr<MemMap> data) { |
| 217 | std::unique_ptr<const DexFile> dex_file(CreateDexFile(env, std::move(data))); |
| 218 | if (dex_file.get() == nullptr) { |
| 219 | DCHECK(env->ExceptionCheck()); |
| 220 | return nullptr; |
| 221 | } |
| 222 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 223 | dex_files.push_back(std::move(dex_file)); |
| 224 | return ConvertDexFilesToJavaArray(env, nullptr, dex_files); |
| 225 | } |
| 226 | |
| 227 | static jobject DexFile_createCookieWithDirectBuffer(JNIEnv* env, |
| 228 | jclass, |
| 229 | jobject buffer, |
| 230 | jint start, |
| 231 | jint end) { |
| 232 | uint8_t* base_address = reinterpret_cast<uint8_t*>(env->GetDirectBufferAddress(buffer)); |
| 233 | if (base_address == nullptr) { |
| 234 | ScopedObjectAccess soa(env); |
| 235 | ThrowWrappedIOException("dexFileBuffer not direct"); |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | std::unique_ptr<MemMap> dex_mem_map(AllocateDexMemoryMap(env, start, end)); |
| 240 | if (dex_mem_map == nullptr) { |
| 241 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | size_t length = static_cast<size_t>(end - start); |
| 246 | memcpy(dex_mem_map->Begin(), base_address, length); |
| 247 | return CreateSingleDexFileCookie(env, std::move(dex_mem_map)); |
| 248 | } |
| 249 | |
| 250 | static jobject DexFile_createCookieWithArray(JNIEnv* env, |
| 251 | jclass, |
| 252 | jbyteArray buffer, |
| 253 | jint start, |
| 254 | jint end) { |
| 255 | std::unique_ptr<MemMap> dex_mem_map(AllocateDexMemoryMap(env, start, end)); |
| 256 | if (dex_mem_map == nullptr) { |
| 257 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | auto destination = reinterpret_cast<jbyte*>(dex_mem_map.get()->Begin()); |
| 262 | env->GetByteArrayRegion(buffer, start, end - start, destination); |
| 263 | return CreateSingleDexFileCookie(env, std::move(dex_mem_map)); |
| 264 | } |
| 265 | |
Calin Juravle | 1f7079b | 2017-04-18 21:25:37 -0700 | [diff] [blame] | 266 | // TODO(calin): clean up the unused parameters (here and in libcore). |
Mathieu Chartier | b190d94 | 2015-11-12 10:00:58 -0800 | [diff] [blame] | 267 | static jobject DexFile_openDexFileNative(JNIEnv* env, |
| 268 | jclass, |
| 269 | jstring javaSourceName, |
Calin Juravle | 1f7079b | 2017-04-18 21:25:37 -0700 | [diff] [blame] | 270 | jstring javaOutputName ATTRIBUTE_UNUSED, |
Mathieu Chartier | b190d94 | 2015-11-12 10:00:58 -0800 | [diff] [blame] | 271 | jint flags ATTRIBUTE_UNUSED, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 272 | jobject class_loader, |
| 273 | jobjectArray dex_elements) { |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 274 | ScopedUtfChars sourceName(env, javaSourceName); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 275 | if (sourceName.c_str() == nullptr) { |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 276 | return 0; |
| 277 | } |
Calin Juravle | 1f7079b | 2017-04-18 21:25:37 -0700 | [diff] [blame] | 278 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 279 | Runtime* const runtime = Runtime::Current(); |
| 280 | ClassLinker* linker = runtime->GetClassLinker(); |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 281 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 282 | std::vector<std::string> error_msgs; |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 283 | const OatFile* oat_file = nullptr; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 284 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 285 | dex_files = runtime->GetOatFileManager().OpenDexFilesFromOat(sourceName.c_str(), |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 286 | class_loader, |
| 287 | dex_elements, |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 288 | /*out*/ &oat_file, |
| 289 | /*out*/ &error_msgs); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 290 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 291 | if (!dex_files.empty()) { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 292 | jlongArray array = ConvertDexFilesToJavaArray(env, oat_file, dex_files); |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 293 | if (array == nullptr) { |
| 294 | ScopedObjectAccess soa(env); |
| 295 | for (auto& dex_file : dex_files) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 296 | if (linker->IsDexFileRegistered(soa.Self(), *dex_file)) { |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 297 | dex_file.release(); |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | return array; |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 302 | } else { |
Vladimir Marko | 60836d5 | 2014-01-16 15:53:38 +0000 | [diff] [blame] | 303 | ScopedObjectAccess soa(env); |
Andreas Gampe | 329d188 | 2014-04-08 10:32:19 -0700 | [diff] [blame] | 304 | CHECK(!error_msgs.empty()); |
| 305 | // The most important message is at the end. So set up nesting by going forward, which will |
| 306 | // wrap the existing exception as a cause for the following one. |
| 307 | auto it = error_msgs.begin(); |
| 308 | auto itEnd = error_msgs.end(); |
| 309 | for ( ; it != itEnd; ++it) { |
| 310 | ThrowWrappedIOException("%s", it->c_str()); |
| 311 | } |
| 312 | |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 313 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 314 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Mathieu Chartier | 1d7d7f1 | 2015-09-25 16:48:57 -0700 | [diff] [blame] | 317 | static jboolean DexFile_closeDexFile(JNIEnv* env, jclass, jobject cookie) { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 318 | std::vector<const DexFile*> dex_files; |
| 319 | const OatFile* oat_file; |
| 320 | if (!ConvertJavaArrayToDexFiles(env, cookie, dex_files, oat_file)) { |
| 321 | Thread::Current()->AssertPendingException(); |
Mathieu Chartier | 1d7d7f1 | 2015-09-25 16:48:57 -0700 | [diff] [blame] | 322 | return JNI_FALSE; |
| 323 | } |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 324 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | 1d7d7f1 | 2015-09-25 16:48:57 -0700 | [diff] [blame] | 325 | bool all_deleted = true; |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 326 | { |
| 327 | ScopedObjectAccess soa(env); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 328 | ObjPtr<mirror::Object> dex_files_object = soa.Decode<mirror::Object>(cookie); |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 329 | ObjPtr<mirror::LongArray> long_dex_files = dex_files_object->AsLongArray(); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 330 | // Delete dex files associated with this dalvik.system.DexFile since there should not be running |
| 331 | // code using it. dex_files is a vector due to multidex. |
| 332 | ClassLinker* const class_linker = runtime->GetClassLinker(); |
| 333 | int32_t i = kDexFileIndexStart; // Oat file is at index 0. |
| 334 | for (const DexFile* dex_file : dex_files) { |
| 335 | if (dex_file != nullptr) { |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 336 | RemoveNativeDebugInfoForDex(soa.Self(), ArrayRef<const uint8_t>(dex_file->Begin(), |
| 337 | dex_file->Size())); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 338 | // Only delete the dex file if the dex cache is not found to prevent runtime crashes if there |
| 339 | // are calls to DexFile.close while the ART DexFile is still in use. |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 340 | if (!class_linker->IsDexFileRegistered(soa.Self(), *dex_file)) { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 341 | // Clear the element in the array so that we can call close again. |
| 342 | long_dex_files->Set(i, 0); |
| 343 | delete dex_file; |
| 344 | } else { |
| 345 | all_deleted = false; |
| 346 | } |
| 347 | } |
| 348 | ++i; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 349 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 350 | } |
Mathieu Chartier | 1d7d7f1 | 2015-09-25 16:48:57 -0700 | [diff] [blame] | 351 | |
Mathieu Chartier | fdccbd4 | 2015-10-14 10:58:41 -0700 | [diff] [blame] | 352 | // oat_file can be null if we are running without dex2oat. |
| 353 | if (all_deleted && oat_file != nullptr) { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 354 | // If all of the dex files are no longer in use we can unmap the corresponding oat file. |
| 355 | VLOG(class_linker) << "Unregistering " << oat_file; |
| 356 | runtime->GetOatFileManager().UnRegisterAndDeleteOatFile(oat_file); |
| 357 | } |
Mathieu Chartier | 1d7d7f1 | 2015-09-25 16:48:57 -0700 | [diff] [blame] | 358 | return all_deleted ? JNI_TRUE : JNI_FALSE; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 361 | static jclass DexFile_defineClassNative(JNIEnv* env, |
| 362 | jclass, |
| 363 | jstring javaName, |
| 364 | jobject javaLoader, |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 365 | jobject cookie, |
| 366 | jobject dexFile) { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 367 | std::vector<const DexFile*> dex_files; |
| 368 | const OatFile* oat_file; |
| 369 | if (!ConvertJavaArrayToDexFiles(env, cookie, /*out*/ dex_files, /*out*/ oat_file)) { |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 370 | VLOG(class_linker) << "Failed to find dex_file"; |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 371 | DCHECK(env->ExceptionCheck()); |
| 372 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 373 | } |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 374 | |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 375 | ScopedUtfChars class_name(env, javaName); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 376 | if (class_name.c_str() == nullptr) { |
Brian Carlstrom | 2e450bf | 2013-09-06 15:39:46 -0700 | [diff] [blame] | 377 | VLOG(class_linker) << "Failed to find class_name"; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 378 | return nullptr; |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 379 | } |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 380 | const std::string descriptor(DotToDescriptor(class_name.c_str())); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 381 | const size_t hash(ComputeModifiedUtf8Hash(descriptor.c_str())); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 382 | for (auto& dex_file : dex_files) { |
David Sehr | 9aa352e | 2016-09-15 18:13:52 -0700 | [diff] [blame] | 383 | const DexFile::ClassDef* dex_class_def = |
| 384 | OatDexFile::FindClassDef(*dex_file, descriptor.c_str(), hash); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 385 | if (dex_class_def != nullptr) { |
| 386 | ScopedObjectAccess soa(env); |
| 387 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 388 | StackHandleScope<1> hs(soa.Self()); |
| 389 | Handle<mirror::ClassLoader> class_loader( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 390 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(javaLoader))); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 391 | ObjPtr<mirror::DexCache> dex_cache = |
| 392 | class_linker->RegisterDexFile(*dex_file, class_loader.Get()); |
| 393 | if (dex_cache == nullptr) { |
| 394 | // OOME or InternalError (dexFile already registered with a different class loader). |
| 395 | soa.Self()->AssertPendingException(); |
| 396 | return nullptr; |
| 397 | } |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 398 | ObjPtr<mirror::Class> result = class_linker->DefineClass(soa.Self(), |
| 399 | descriptor.c_str(), |
| 400 | hash, |
| 401 | class_loader, |
| 402 | *dex_file, |
| 403 | *dex_class_def); |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 404 | // Add the used dex file. This only required for the DexFile.loadClass API since normal |
| 405 | // class loaders already keep their dex files live. |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 406 | class_linker->InsertDexFileInToClassLoader(soa.Decode<mirror::Object>(dexFile), |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 407 | class_loader.Get()); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 408 | if (result != nullptr) { |
Brian Carlstrom | 667ab7c | 2014-10-16 19:12:28 -0700 | [diff] [blame] | 409 | VLOG(class_linker) << "DexFile_defineClassNative returning " << result |
| 410 | << " for " << class_name.c_str(); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 411 | return soa.AddLocalReference<jclass>(result); |
| 412 | } |
| 413 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 414 | } |
Brian Carlstrom | 667ab7c | 2014-10-16 19:12:28 -0700 | [diff] [blame] | 415 | 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] | 416 | return nullptr; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 419 | // Needed as a compare functor for sets of const char |
| 420 | struct CharPointerComparator { |
| 421 | bool operator()(const char *str1, const char *str2) const { |
| 422 | return strcmp(str1, str2) < 0; |
| 423 | } |
| 424 | }; |
| 425 | |
| 426 | // 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] | 427 | static jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jobject cookie) { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 428 | const OatFile* oat_file = nullptr; |
| 429 | std::vector<const DexFile*> dex_files; |
| 430 | if (!ConvertJavaArrayToDexFiles(env, cookie, /*out */ dex_files, /* out */ oat_file)) { |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 431 | DCHECK(env->ExceptionCheck()); |
| 432 | return nullptr; |
| 433 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 434 | |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 435 | // Push all class descriptors into a set. Use set instead of unordered_set as we want to |
| 436 | // retrieve all in the end. |
| 437 | std::set<const char*, CharPointerComparator> descriptors; |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 438 | for (auto& dex_file : dex_files) { |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 439 | for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) { |
| 440 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(i); |
| 441 | const char* descriptor = dex_file->GetClassDescriptor(class_def); |
| 442 | descriptors.insert(descriptor); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 443 | } |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 444 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 445 | |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 446 | // Now create output array and copy the set into it. |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 447 | jobjectArray result = env->NewObjectArray(descriptors.size(), |
| 448 | WellKnownClasses::java_lang_String, |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 449 | nullptr); |
| 450 | if (result != nullptr) { |
| 451 | auto it = descriptors.begin(); |
| 452 | auto it_end = descriptors.end(); |
| 453 | jsize i = 0; |
| 454 | for (; it != it_end; it++, ++i) { |
| 455 | std::string descriptor(DescriptorToDot(*it)); |
| 456 | ScopedLocalRef<jstring> jdescriptor(env, env->NewStringUTF(descriptor.c_str())); |
| 457 | if (jdescriptor.get() == nullptr) { |
| 458 | return nullptr; |
Ian Rogers | dd157d7 | 2014-05-15 14:47:50 -0700 | [diff] [blame] | 459 | } |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 460 | env->SetObjectArrayElement(result, i, jdescriptor.get()); |
Ian Rogers | dd157d7 | 2014-05-15 14:47:50 -0700 | [diff] [blame] | 461 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 462 | } |
Ian Rogers | dd157d7 | 2014-05-15 14:47:50 -0700 | [diff] [blame] | 463 | return result; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 466 | static jint GetDexOptNeeded(JNIEnv* env, |
| 467 | const char* filename, |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 468 | const char* instruction_set, |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 469 | const char* compiler_filter_name, |
Calin Juravle | 20c4644 | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 470 | const char* class_loader_context, |
Shubham Ajmera | e4e812a | 2017-05-25 20:09:58 -0700 | [diff] [blame] | 471 | bool profile_changed, |
| 472 | bool downgrade) { |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 473 | if ((filename == nullptr) || !OS::FileExists(filename)) { |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 474 | LOG(ERROR) << "DexFile_getDexOptNeeded file '" << filename << "' does not exist"; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 475 | ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException")); |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 476 | const char* message = (filename == nullptr) ? "<empty file name>" : filename; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 477 | env->ThrowNew(fnfe.get(), message); |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 478 | return -1; |
Brian Carlstrom | 1d9f52b | 2011-10-13 10:50:45 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 481 | const InstructionSet target_instruction_set = GetInstructionSetFromString(instruction_set); |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 482 | if (target_instruction_set == InstructionSet::kNone) { |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 483 | ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException")); |
| 484 | std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set)); |
| 485 | env->ThrowNew(iae.get(), message.c_str()); |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 486 | return -1; |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 487 | } |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 488 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 489 | CompilerFilter::Filter filter; |
| 490 | if (!CompilerFilter::ParseCompilerFilter(compiler_filter_name, &filter)) { |
| 491 | ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException")); |
| 492 | std::string message(StringPrintf("Compiler filter %s is invalid.", compiler_filter_name)); |
| 493 | env->ThrowNew(iae.get(), message.c_str()); |
| 494 | return -1; |
| 495 | } |
| 496 | |
Calin Juravle | 20c4644 | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 497 | std::unique_ptr<ClassLoaderContext> context = nullptr; |
| 498 | if (class_loader_context != nullptr) { |
| 499 | context = ClassLoaderContext::Create(class_loader_context); |
| 500 | |
| 501 | if (context == nullptr) { |
| 502 | ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException")); |
| 503 | std::string message(StringPrintf("Class loader context '%s' is invalid.", |
| 504 | class_loader_context)); |
| 505 | env->ThrowNew(iae.get(), message.c_str()); |
| 506 | return -1; |
| 507 | } |
| 508 | } |
| 509 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 510 | // TODO: Verify the dex location is well formed, and throw an IOException if |
| 511 | // not? |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 512 | |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 513 | OatFileAssistant oat_file_assistant(filename, target_instruction_set, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 514 | |
| 515 | // Always treat elements of the bootclasspath as up-to-date. |
| 516 | if (oat_file_assistant.IsInBootClassPath()) { |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 517 | return OatFileAssistant::kNoDexOptNeeded; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 518 | } |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 519 | |
Calin Juravle | 20c4644 | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 520 | return oat_file_assistant.GetDexOptNeeded(filter, |
| 521 | profile_changed, |
| 522 | downgrade, |
| 523 | context.get()); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 524 | } |
| 525 | |
Narayan Kamath | 8943c1d | 2016-05-02 13:14:48 +0100 | [diff] [blame] | 526 | static jstring DexFile_getDexFileStatus(JNIEnv* env, |
| 527 | jclass, |
| 528 | jstring javaFilename, |
| 529 | jstring javaInstructionSet) { |
| 530 | ScopedUtfChars filename(env, javaFilename); |
| 531 | if (env->ExceptionCheck()) { |
| 532 | return nullptr; |
| 533 | } |
| 534 | |
| 535 | ScopedUtfChars instruction_set(env, javaInstructionSet); |
| 536 | if (env->ExceptionCheck()) { |
| 537 | return nullptr; |
| 538 | } |
| 539 | |
| 540 | const InstructionSet target_instruction_set = GetInstructionSetFromString( |
| 541 | instruction_set.c_str()); |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 542 | if (target_instruction_set == InstructionSet::kNone) { |
Narayan Kamath | 8943c1d | 2016-05-02 13:14:48 +0100 | [diff] [blame] | 543 | ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException")); |
| 544 | std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set.c_str())); |
| 545 | env->ThrowNew(iae.get(), message.c_str()); |
| 546 | return nullptr; |
| 547 | } |
| 548 | |
| 549 | OatFileAssistant oat_file_assistant(filename.c_str(), target_instruction_set, |
Narayan Kamath | 8943c1d | 2016-05-02 13:14:48 +0100 | [diff] [blame] | 550 | false /* load_executable */); |
Richard Uhler | 46cc64f | 2016-11-14 14:53:55 +0000 | [diff] [blame] | 551 | return env->NewStringUTF(oat_file_assistant.GetStatusDump().c_str()); |
Narayan Kamath | 8943c1d | 2016-05-02 13:14:48 +0100 | [diff] [blame] | 552 | } |
| 553 | |
Calin Juravle | 5f9a801 | 2018-02-12 20:27:46 -0800 | [diff] [blame] | 554 | // Return an array specifying the optimization status of the given file. |
| 555 | // The array specification is [compiler_filter, compiler_reason]. |
| 556 | static jobjectArray DexFile_getDexFileOptimizationStatus(JNIEnv* env, |
| 557 | jclass, |
| 558 | jstring javaFilename, |
| 559 | jstring javaInstructionSet) { |
| 560 | ScopedUtfChars filename(env, javaFilename); |
| 561 | if (env->ExceptionCheck()) { |
| 562 | return nullptr; |
| 563 | } |
| 564 | |
| 565 | ScopedUtfChars instruction_set(env, javaInstructionSet); |
| 566 | if (env->ExceptionCheck()) { |
| 567 | return nullptr; |
| 568 | } |
| 569 | |
| 570 | const InstructionSet target_instruction_set = GetInstructionSetFromString( |
| 571 | instruction_set.c_str()); |
| 572 | if (target_instruction_set == InstructionSet::kNone) { |
| 573 | ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException")); |
| 574 | std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set.c_str())); |
| 575 | env->ThrowNew(iae.get(), message.c_str()); |
| 576 | return nullptr; |
| 577 | } |
| 578 | |
| 579 | std::string compilation_filter; |
| 580 | std::string compilation_reason; |
| 581 | OatFileAssistant::GetOptimizationStatus( |
| 582 | filename.c_str(), target_instruction_set, &compilation_filter, &compilation_reason); |
| 583 | |
| 584 | ScopedLocalRef<jstring> j_compilation_filter(env, env->NewStringUTF(compilation_filter.c_str())); |
| 585 | if (j_compilation_filter.get() == nullptr) { |
| 586 | return nullptr; |
| 587 | } |
| 588 | ScopedLocalRef<jstring> j_compilation_reason(env, env->NewStringUTF(compilation_reason.c_str())); |
| 589 | if (j_compilation_reason.get() == nullptr) { |
| 590 | return nullptr; |
| 591 | } |
| 592 | |
| 593 | // Now create output array and copy the set into it. |
| 594 | jobjectArray result = env->NewObjectArray(2, |
| 595 | WellKnownClasses::java_lang_String, |
| 596 | nullptr); |
| 597 | env->SetObjectArrayElement(result, 0, j_compilation_filter.get()); |
| 598 | env->SetObjectArrayElement(result, 1, j_compilation_reason.get()); |
| 599 | |
| 600 | return result; |
| 601 | } |
| 602 | |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 603 | static jint DexFile_getDexOptNeeded(JNIEnv* env, |
| 604 | jclass, |
| 605 | jstring javaFilename, |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 606 | jstring javaInstructionSet, |
Andreas Gampe | c38be81 | 2016-03-23 15:03:46 -0700 | [diff] [blame] | 607 | jstring javaTargetCompilerFilter, |
Calin Juravle | 20c4644 | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 608 | jstring javaClassLoaderContext, |
Shubham Ajmera | e4e812a | 2017-05-25 20:09:58 -0700 | [diff] [blame] | 609 | jboolean newProfile, |
| 610 | jboolean downgrade) { |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 611 | ScopedUtfChars filename(env, javaFilename); |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 612 | if (env->ExceptionCheck()) { |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 613 | return -1; |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 616 | ScopedUtfChars instruction_set(env, javaInstructionSet); |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 617 | if (env->ExceptionCheck()) { |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 618 | return -1; |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 619 | } |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 620 | |
Andreas Gampe | c38be81 | 2016-03-23 15:03:46 -0700 | [diff] [blame] | 621 | ScopedUtfChars target_compiler_filter(env, javaTargetCompilerFilter); |
| 622 | if (env->ExceptionCheck()) { |
| 623 | return -1; |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 624 | } |
Andreas Gampe | c38be81 | 2016-03-23 15:03:46 -0700 | [diff] [blame] | 625 | |
Calin Juravle | 20c4644 | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 626 | NullableScopedUtfChars class_loader_context(env, javaClassLoaderContext); |
| 627 | if (env->ExceptionCheck()) { |
| 628 | return -1; |
| 629 | } |
| 630 | |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 631 | return GetDexOptNeeded(env, |
| 632 | filename.c_str(), |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 633 | instruction_set.c_str(), |
Andreas Gampe | c38be81 | 2016-03-23 15:03:46 -0700 | [diff] [blame] | 634 | target_compiler_filter.c_str(), |
Calin Juravle | 20c4644 | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 635 | class_loader_context.c_str(), |
Shubham Ajmera | e4e812a | 2017-05-25 20:09:58 -0700 | [diff] [blame] | 636 | newProfile == JNI_TRUE, |
| 637 | downgrade == JNI_TRUE); |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 638 | } |
| 639 | |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 640 | // public API |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 641 | static jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) { |
Richard Uhler | 06e3f4f | 2016-05-24 15:42:37 -0700 | [diff] [blame] | 642 | ScopedUtfChars filename_utf(env, javaFilename); |
| 643 | if (env->ExceptionCheck()) { |
| 644 | return JNI_FALSE; |
| 645 | } |
| 646 | |
| 647 | const char* filename = filename_utf.c_str(); |
| 648 | if ((filename == nullptr) || !OS::FileExists(filename)) { |
| 649 | LOG(ERROR) << "DexFile_isDexOptNeeded file '" << filename << "' does not exist"; |
| 650 | ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException")); |
| 651 | const char* message = (filename == nullptr) ? "<empty file name>" : filename; |
| 652 | env->ThrowNew(fnfe.get(), message); |
| 653 | return JNI_FALSE; |
| 654 | } |
| 655 | |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 656 | OatFileAssistant oat_file_assistant(filename, kRuntimeISA, false); |
Richard Uhler | 06e3f4f | 2016-05-24 15:42:37 -0700 | [diff] [blame] | 657 | return oat_file_assistant.IsUpToDate() ? JNI_FALSE : JNI_TRUE; |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 658 | } |
| 659 | |
Andreas Gampe | c38be81 | 2016-03-23 15:03:46 -0700 | [diff] [blame] | 660 | static jboolean DexFile_isValidCompilerFilter(JNIEnv* env, |
| 661 | jclass javeDexFileClass ATTRIBUTE_UNUSED, |
| 662 | jstring javaCompilerFilter) { |
| 663 | ScopedUtfChars compiler_filter(env, javaCompilerFilter); |
| 664 | if (env->ExceptionCheck()) { |
| 665 | return -1; |
| 666 | } |
| 667 | |
| 668 | CompilerFilter::Filter filter; |
| 669 | return CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter) |
| 670 | ? JNI_TRUE : JNI_FALSE; |
| 671 | } |
| 672 | |
| 673 | static jboolean DexFile_isProfileGuidedCompilerFilter(JNIEnv* env, |
| 674 | jclass javeDexFileClass ATTRIBUTE_UNUSED, |
| 675 | jstring javaCompilerFilter) { |
| 676 | ScopedUtfChars compiler_filter(env, javaCompilerFilter); |
| 677 | if (env->ExceptionCheck()) { |
| 678 | return -1; |
| 679 | } |
| 680 | |
| 681 | CompilerFilter::Filter filter; |
| 682 | if (!CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)) { |
| 683 | return JNI_FALSE; |
| 684 | } |
| 685 | return CompilerFilter::DependsOnProfile(filter) ? JNI_TRUE : JNI_FALSE; |
| 686 | } |
| 687 | |
Andreas Gampe | 86a785d | 2016-03-30 17:19:48 -0700 | [diff] [blame] | 688 | static jstring DexFile_getNonProfileGuidedCompilerFilter(JNIEnv* env, |
| 689 | jclass javeDexFileClass ATTRIBUTE_UNUSED, |
| 690 | jstring javaCompilerFilter) { |
| 691 | ScopedUtfChars compiler_filter(env, javaCompilerFilter); |
| 692 | if (env->ExceptionCheck()) { |
| 693 | return nullptr; |
| 694 | } |
| 695 | |
| 696 | CompilerFilter::Filter filter; |
| 697 | if (!CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)) { |
| 698 | return javaCompilerFilter; |
| 699 | } |
| 700 | |
| 701 | CompilerFilter::Filter new_filter = CompilerFilter::GetNonProfileDependentFilterFrom(filter); |
| 702 | |
| 703 | // Filter stayed the same, return input. |
| 704 | if (filter == new_filter) { |
| 705 | return javaCompilerFilter; |
| 706 | } |
| 707 | |
| 708 | // Create a new string object and return. |
| 709 | std::string new_filter_str = CompilerFilter::NameOfFilter(new_filter); |
| 710 | return env->NewStringUTF(new_filter_str.c_str()); |
| 711 | } |
| 712 | |
Nicolas Geoffray | 741d426 | 2017-05-03 13:08:36 +0100 | [diff] [blame] | 713 | static jstring DexFile_getSafeModeCompilerFilter(JNIEnv* env, |
| 714 | jclass javeDexFileClass ATTRIBUTE_UNUSED, |
| 715 | jstring javaCompilerFilter) { |
| 716 | ScopedUtfChars compiler_filter(env, javaCompilerFilter); |
| 717 | if (env->ExceptionCheck()) { |
| 718 | return nullptr; |
| 719 | } |
| 720 | |
| 721 | CompilerFilter::Filter filter; |
| 722 | if (!CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)) { |
| 723 | return javaCompilerFilter; |
| 724 | } |
| 725 | |
| 726 | CompilerFilter::Filter new_filter = CompilerFilter::GetSafeModeFilterFrom(filter); |
| 727 | |
| 728 | // Filter stayed the same, return input. |
| 729 | if (filter == new_filter) { |
| 730 | return javaCompilerFilter; |
| 731 | } |
| 732 | |
| 733 | // Create a new string object and return. |
| 734 | std::string new_filter_str = CompilerFilter::NameOfFilter(new_filter); |
| 735 | return env->NewStringUTF(new_filter_str.c_str()); |
| 736 | } |
| 737 | |
Jeff Hao | 90671be | 2016-04-22 14:00:06 -0700 | [diff] [blame] | 738 | static jboolean DexFile_isBackedByOatFile(JNIEnv* env, jclass, jobject cookie) { |
| 739 | const OatFile* oat_file = nullptr; |
| 740 | std::vector<const DexFile*> dex_files; |
| 741 | if (!ConvertJavaArrayToDexFiles(env, cookie, /*out */ dex_files, /* out */ oat_file)) { |
| 742 | DCHECK(env->ExceptionCheck()); |
| 743 | return false; |
| 744 | } |
| 745 | return oat_file != nullptr; |
| 746 | } |
| 747 | |
Calin Juravle | 367b9d8 | 2017-05-15 18:18:39 -0700 | [diff] [blame] | 748 | static jobjectArray DexFile_getDexFileOutputPaths(JNIEnv* env, |
Philip Cuadra | b55ad7c | 2016-07-12 16:37:40 -0700 | [diff] [blame] | 749 | jclass, |
| 750 | jstring javaFilename, |
| 751 | jstring javaInstructionSet) { |
| 752 | ScopedUtfChars filename(env, javaFilename); |
| 753 | if (env->ExceptionCheck()) { |
| 754 | return nullptr; |
| 755 | } |
| 756 | |
| 757 | ScopedUtfChars instruction_set(env, javaInstructionSet); |
| 758 | if (env->ExceptionCheck()) { |
| 759 | return nullptr; |
| 760 | } |
| 761 | |
| 762 | const InstructionSet target_instruction_set = GetInstructionSetFromString( |
| 763 | instruction_set.c_str()); |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 764 | if (target_instruction_set == InstructionSet::kNone) { |
Philip Cuadra | b55ad7c | 2016-07-12 16:37:40 -0700 | [diff] [blame] | 765 | ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException")); |
| 766 | std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set.c_str())); |
| 767 | env->ThrowNew(iae.get(), message.c_str()); |
| 768 | return nullptr; |
| 769 | } |
| 770 | |
| 771 | OatFileAssistant oat_file_assistant(filename.c_str(), |
| 772 | target_instruction_set, |
Philip Cuadra | b55ad7c | 2016-07-12 16:37:40 -0700 | [diff] [blame] | 773 | false /* load_executable */); |
| 774 | |
| 775 | std::unique_ptr<OatFile> best_oat_file = oat_file_assistant.GetBestOatFile(); |
| 776 | if (best_oat_file == nullptr) { |
| 777 | return nullptr; |
| 778 | } |
| 779 | |
Calin Juravle | 367b9d8 | 2017-05-15 18:18:39 -0700 | [diff] [blame] | 780 | std::string oat_filename = best_oat_file->GetLocation(); |
| 781 | std::string vdex_filename = GetVdexFilename(best_oat_file->GetLocation()); |
| 782 | |
| 783 | ScopedLocalRef<jstring> jvdexFilename(env, env->NewStringUTF(vdex_filename.c_str())); |
| 784 | if (jvdexFilename.get() == nullptr) { |
| 785 | return nullptr; |
| 786 | } |
| 787 | ScopedLocalRef<jstring> joatFilename(env, env->NewStringUTF(oat_filename.c_str())); |
| 788 | if (joatFilename.get() == nullptr) { |
| 789 | return nullptr; |
| 790 | } |
| 791 | |
| 792 | // Now create output array and copy the set into it. |
| 793 | jobjectArray result = env->NewObjectArray(2, |
| 794 | WellKnownClasses::java_lang_String, |
| 795 | nullptr); |
| 796 | env->SetObjectArrayElement(result, 0, jvdexFilename.get()); |
| 797 | env->SetObjectArrayElement(result, 1, joatFilename.get()); |
| 798 | |
| 799 | return result; |
Philip Cuadra | b55ad7c | 2016-07-12 16:37:40 -0700 | [diff] [blame] | 800 | } |
| 801 | |
Richard Uhler | 24ed94f | 2017-11-16 11:54:29 +0000 | [diff] [blame] | 802 | static jlong DexFile_getStaticSizeOfDexFile(JNIEnv* env, jclass, jobject cookie) { |
| 803 | const OatFile* oat_file = nullptr; |
| 804 | std::vector<const DexFile*> dex_files; |
| 805 | if (!ConvertJavaArrayToDexFiles(env, cookie, /*out */ dex_files, /* out */ oat_file)) { |
| 806 | DCHECK(env->ExceptionCheck()); |
| 807 | return 0; |
| 808 | } |
| 809 | |
| 810 | uint64_t file_size = 0; |
| 811 | for (auto& dex_file : dex_files) { |
| 812 | if (dex_file) { |
| 813 | file_size += dex_file->GetHeader().file_size_; |
| 814 | } |
| 815 | } |
| 816 | return static_cast<jlong>(file_size); |
| 817 | } |
| 818 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 819 | static JNINativeMethod gMethods[] = { |
Mathieu Chartier | 1d7d7f1 | 2015-09-25 16:48:57 -0700 | [diff] [blame] | 820 | NATIVE_METHOD(DexFile, closeDexFile, "(Ljava/lang/Object;)Z"), |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 821 | NATIVE_METHOD(DexFile, |
| 822 | defineClassNative, |
| 823 | "(Ljava/lang/String;" |
| 824 | "Ljava/lang/ClassLoader;" |
| 825 | "Ljava/lang/Object;" |
| 826 | "Ldalvik/system/DexFile;" |
| 827 | ")Ljava/lang/Class;"), |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 828 | NATIVE_METHOD(DexFile, getClassNameList, "(Ljava/lang/Object;)[Ljava/lang/String;"), |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 829 | NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"), |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 830 | NATIVE_METHOD(DexFile, getDexOptNeeded, |
Calin Juravle | 20c4644 | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 831 | "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ZZ)I"), |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 832 | NATIVE_METHOD(DexFile, openDexFileNative, |
Mathieu Chartier | 689a700 | 2015-11-20 10:29:42 -0800 | [diff] [blame] | 833 | "(Ljava/lang/String;" |
| 834 | "Ljava/lang/String;" |
| 835 | "I" |
| 836 | "Ljava/lang/ClassLoader;" |
| 837 | "[Ldalvik/system/DexPathList$Element;" |
| 838 | ")Ljava/lang/Object;"), |
Alex Light | ea9465e | 2017-02-16 15:38:35 -0800 | [diff] [blame] | 839 | NATIVE_METHOD(DexFile, createCookieWithDirectBuffer, |
| 840 | "(Ljava/nio/ByteBuffer;II)Ljava/lang/Object;"), |
| 841 | NATIVE_METHOD(DexFile, createCookieWithArray, "([BII)Ljava/lang/Object;"), |
Andreas Gampe | c38be81 | 2016-03-23 15:03:46 -0700 | [diff] [blame] | 842 | NATIVE_METHOD(DexFile, isValidCompilerFilter, "(Ljava/lang/String;)Z"), |
| 843 | NATIVE_METHOD(DexFile, isProfileGuidedCompilerFilter, "(Ljava/lang/String;)Z"), |
Andreas Gampe | 86a785d | 2016-03-30 17:19:48 -0700 | [diff] [blame] | 844 | NATIVE_METHOD(DexFile, |
| 845 | getNonProfileGuidedCompilerFilter, |
| 846 | "(Ljava/lang/String;)Ljava/lang/String;"), |
Nicolas Geoffray | 741d426 | 2017-05-03 13:08:36 +0100 | [diff] [blame] | 847 | NATIVE_METHOD(DexFile, |
| 848 | getSafeModeCompilerFilter, |
| 849 | "(Ljava/lang/String;)Ljava/lang/String;"), |
Jeff Hao | 90671be | 2016-04-22 14:00:06 -0700 | [diff] [blame] | 850 | NATIVE_METHOD(DexFile, isBackedByOatFile, "(Ljava/lang/Object;)Z"), |
Narayan Kamath | 8943c1d | 2016-05-02 13:14:48 +0100 | [diff] [blame] | 851 | NATIVE_METHOD(DexFile, getDexFileStatus, |
Philip Cuadra | b55ad7c | 2016-07-12 16:37:40 -0700 | [diff] [blame] | 852 | "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"), |
Calin Juravle | 367b9d8 | 2017-05-15 18:18:39 -0700 | [diff] [blame] | 853 | NATIVE_METHOD(DexFile, getDexFileOutputPaths, |
Richard Uhler | 24ed94f | 2017-11-16 11:54:29 +0000 | [diff] [blame] | 854 | "(Ljava/lang/String;Ljava/lang/String;)[Ljava/lang/String;"), |
Calin Juravle | 5f9a801 | 2018-02-12 20:27:46 -0800 | [diff] [blame] | 855 | NATIVE_METHOD(DexFile, getStaticSizeOfDexFile, "(Ljava/lang/Object;)J"), |
| 856 | NATIVE_METHOD(DexFile, getDexFileOptimizationStatus, |
| 857 | "(Ljava/lang/String;Ljava/lang/String;)[Ljava/lang/String;") |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 858 | }; |
| 859 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 860 | void register_dalvik_system_DexFile(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 861 | REGISTER_NATIVE_METHODS("dalvik/system/DexFile"); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | } // namespace art |