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