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