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