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 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 19 | #include "base/logging.h" |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 20 | #include "base/stl_util.h" |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 21 | #include "base/stringprintf.h" |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 22 | #include "class_linker.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 23 | #include "common_throws.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 24 | #include "dex_file-inl.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 25 | #include "jni_internal.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 26 | #include "mirror/class_loader.h" |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 27 | #include "mirror/object-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 28 | #include "mirror/string.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 29 | #include "oat_file_assistant.h" |
Brian Carlstrom | 1d9f52b | 2011-10-13 10:50:45 -0700 | [diff] [blame] | 30 | #include "os.h" |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 31 | #include "profiler.h" |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 32 | #include "runtime.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 33 | #include "scoped_thread_state_change.h" |
Ian Rogers | c981848 | 2012-01-11 08:52:51 -0800 | [diff] [blame] | 34 | #include "ScopedLocalRef.h" |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 35 | #include "ScopedUtfChars.h" |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 36 | #include "utils.h" |
Ian Rogers | dd157d7 | 2014-05-15 14:47:50 -0700 | [diff] [blame] | 37 | #include "well_known_classes.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 38 | #include "zip_archive.h" |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 39 | |
| 40 | namespace art { |
| 41 | |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 42 | static std::unique_ptr<std::vector<const DexFile*>> |
| 43 | ConvertJavaArrayToNative(JNIEnv* env, jobject arrayObject) { |
| 44 | jarray array = reinterpret_cast<jarray>(arrayObject); |
| 45 | |
| 46 | jsize array_size = env->GetArrayLength(array); |
| 47 | if (env->ExceptionCheck() == JNI_TRUE) { |
| 48 | return std::unique_ptr<std::vector<const DexFile*>>(); |
| 49 | } |
| 50 | |
| 51 | // TODO: Optimize. On 32bit we can use an int array. |
| 52 | jboolean is_long_data_copied; |
| 53 | jlong* long_data = env->GetLongArrayElements(reinterpret_cast<jlongArray>(array), |
| 54 | &is_long_data_copied); |
| 55 | if (env->ExceptionCheck() == JNI_TRUE) { |
| 56 | return std::unique_ptr<std::vector<const DexFile*>>(); |
| 57 | } |
| 58 | |
| 59 | std::unique_ptr<std::vector<const DexFile*>> ret(new std::vector<const DexFile*>()); |
| 60 | ret->reserve(array_size); |
| 61 | for (jsize i = 0; i < array_size; ++i) { |
| 62 | ret->push_back(reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(*(long_data + i)))); |
| 63 | } |
| 64 | |
| 65 | env->ReleaseLongArrayElements(reinterpret_cast<jlongArray>(array), long_data, JNI_ABORT); |
| 66 | if (env->ExceptionCheck() == JNI_TRUE) { |
| 67 | return std::unique_ptr<std::vector<const DexFile*>>(); |
| 68 | } |
| 69 | |
| 70 | return ret; |
| 71 | } |
| 72 | |
| 73 | static jlongArray ConvertNativeToJavaArray(JNIEnv* env, |
| 74 | std::vector<std::unique_ptr<const DexFile>>& vec) { |
| 75 | size_t vec_size = vec.size(); |
| 76 | jlongArray long_array = env->NewLongArray(static_cast<jsize>(vec_size)); |
| 77 | if (env->ExceptionCheck() == JNI_TRUE) { |
| 78 | return nullptr; |
| 79 | } |
| 80 | |
| 81 | jboolean is_long_data_copied; |
| 82 | jlong* long_data = env->GetLongArrayElements(long_array, &is_long_data_copied); |
| 83 | if (env->ExceptionCheck() == JNI_TRUE) { |
| 84 | return nullptr; |
| 85 | } |
| 86 | |
| 87 | jlong* tmp = long_data; |
| 88 | for (auto& dex_file : vec) { |
| 89 | *tmp = reinterpret_cast<uintptr_t>(dex_file.get()); |
| 90 | tmp++; |
| 91 | } |
| 92 | |
| 93 | env->ReleaseLongArrayElements(long_array, long_data, 0); |
| 94 | if (env->ExceptionCheck() == JNI_TRUE) { |
| 95 | return nullptr; |
| 96 | } |
| 97 | |
| 98 | // Now release all the unique_ptrs. |
| 99 | for (auto& dex_file : vec) { |
| 100 | dex_file.release(); |
| 101 | } |
| 102 | |
| 103 | return long_array; |
| 104 | } |
| 105 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 106 | // A smart pointer that provides read-only access to a Java string's UTF chars. |
| 107 | // Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if |
| 108 | // passed a null jstring. The correct idiom is: |
| 109 | // |
| 110 | // NullableScopedUtfChars name(env, javaName); |
Brian Carlstrom | c252c3e | 2011-10-16 23:21:02 -0700 | [diff] [blame] | 111 | // if (env->ExceptionCheck()) { |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 112 | // return NULL; |
| 113 | // } |
| 114 | // // ... use name.c_str() |
| 115 | // |
| 116 | // TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option. |
| 117 | class NullableScopedUtfChars { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 118 | public: |
| 119 | NullableScopedUtfChars(JNIEnv* env, jstring s) : mEnv(env), mString(s) { |
| 120 | mUtfChars = (s != NULL) ? env->GetStringUTFChars(s, NULL) : NULL; |
| 121 | } |
| 122 | |
| 123 | ~NullableScopedUtfChars() { |
| 124 | if (mUtfChars) { |
| 125 | mEnv->ReleaseStringUTFChars(mString, mUtfChars); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 126 | } |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 127 | } |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 128 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 129 | const char* c_str() const { |
| 130 | return mUtfChars; |
| 131 | } |
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 | size_t size() const { |
| 134 | return strlen(mUtfChars); |
| 135 | } |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 136 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 137 | // Element access. |
| 138 | const char& operator[](size_t n) const { |
| 139 | return mUtfChars[n]; |
| 140 | } |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 141 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 142 | private: |
| 143 | JNIEnv* mEnv; |
| 144 | jstring mString; |
| 145 | const char* mUtfChars; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 146 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 147 | // Disallow copy and assignment. |
| 148 | NullableScopedUtfChars(const NullableScopedUtfChars&); |
| 149 | void operator=(const NullableScopedUtfChars&); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 150 | }; |
| 151 | |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 152 | static jobject DexFile_openDexFileNative(JNIEnv* env, jclass, jstring javaSourceName, jstring javaOutputName, jint) { |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 153 | ScopedUtfChars sourceName(env, javaSourceName); |
| 154 | if (sourceName.c_str() == NULL) { |
| 155 | return 0; |
| 156 | } |
| 157 | NullableScopedUtfChars outputName(env, javaOutputName); |
Brian Carlstrom | c252c3e | 2011-10-16 23:21:02 -0700 | [diff] [blame] | 158 | if (env->ExceptionCheck()) { |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 159 | return 0; |
| 160 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 161 | |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 162 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 163 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 164 | std::vector<std::string> error_msgs; |
| 165 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 166 | dex_files = linker->OpenDexFilesFromOat(sourceName.c_str(), outputName.c_str(), &error_msgs); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 167 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 168 | if (!dex_files.empty()) { |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 169 | jlongArray array = ConvertNativeToJavaArray(env, dex_files); |
| 170 | if (array == nullptr) { |
| 171 | ScopedObjectAccess soa(env); |
| 172 | for (auto& dex_file : dex_files) { |
| 173 | if (Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) { |
| 174 | dex_file.release(); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | return array; |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 179 | } else { |
Vladimir Marko | 60836d5 | 2014-01-16 15:53:38 +0000 | [diff] [blame] | 180 | ScopedObjectAccess soa(env); |
Andreas Gampe | 329d188 | 2014-04-08 10:32:19 -0700 | [diff] [blame] | 181 | CHECK(!error_msgs.empty()); |
| 182 | // The most important message is at the end. So set up nesting by going forward, which will |
| 183 | // wrap the existing exception as a cause for the following one. |
| 184 | auto it = error_msgs.begin(); |
| 185 | auto itEnd = error_msgs.end(); |
| 186 | for ( ; it != itEnd; ++it) { |
| 187 | ThrowWrappedIOException("%s", it->c_str()); |
| 188 | } |
| 189 | |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 190 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 191 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 194 | static void DexFile_closeDexFile(JNIEnv* env, jclass, jobject cookie) { |
| 195 | std::unique_ptr<std::vector<const DexFile*>> dex_files = ConvertJavaArrayToNative(env, cookie); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 196 | if (dex_files.get() == nullptr) { |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 197 | DCHECK(env->ExceptionCheck()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 198 | return; |
| 199 | } |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 200 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 201 | ScopedObjectAccess soa(env); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 202 | |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 203 | // The Runtime currently never unloads classes, which means any registered |
| 204 | // dex files must be kept around forever in case they are used. We |
| 205 | // accomplish this here by explicitly leaking those dex files that are |
| 206 | // registered. |
| 207 | // |
| 208 | // TODO: The Runtime should support unloading of classes and freeing of the |
| 209 | // dex files for those unloaded classes rather than leaking dex files here. |
| 210 | for (auto& dex_file : *dex_files) { |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 211 | if (!Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) { |
| 212 | delete dex_file; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 213 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 214 | } |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 217 | static jclass DexFile_defineClassNative(JNIEnv* env, jclass, jstring javaName, jobject javaLoader, |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 218 | jobject cookie) { |
| 219 | std::unique_ptr<std::vector<const DexFile*>> dex_files = ConvertJavaArrayToNative(env, cookie); |
| 220 | if (dex_files.get() == nullptr) { |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 221 | VLOG(class_linker) << "Failed to find dex_file"; |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 222 | DCHECK(env->ExceptionCheck()); |
| 223 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 224 | } |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 225 | |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 226 | ScopedUtfChars class_name(env, javaName); |
| 227 | if (class_name.c_str() == NULL) { |
Brian Carlstrom | 2e450bf | 2013-09-06 15:39:46 -0700 | [diff] [blame] | 228 | VLOG(class_linker) << "Failed to find class_name"; |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 229 | return NULL; |
| 230 | } |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 231 | const std::string descriptor(DotToDescriptor(class_name.c_str())); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 232 | const size_t hash(ComputeModifiedUtf8Hash(descriptor.c_str())); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 233 | for (auto& dex_file : *dex_files) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 234 | const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor.c_str(), hash); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 235 | if (dex_class_def != nullptr) { |
| 236 | ScopedObjectAccess soa(env); |
| 237 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 238 | class_linker->RegisterDexFile(*dex_file); |
| 239 | StackHandleScope<1> hs(soa.Self()); |
| 240 | Handle<mirror::ClassLoader> class_loader( |
| 241 | hs.NewHandle(soa.Decode<mirror::ClassLoader*>(javaLoader))); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 242 | mirror::Class* result = class_linker->DefineClass(soa.Self(), descriptor.c_str(), hash, |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 243 | class_loader, *dex_file, *dex_class_def); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 244 | if (result != nullptr) { |
Brian Carlstrom | 667ab7c | 2014-10-16 19:12:28 -0700 | [diff] [blame] | 245 | VLOG(class_linker) << "DexFile_defineClassNative returning " << result |
| 246 | << " for " << class_name.c_str(); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 247 | return soa.AddLocalReference<jclass>(result); |
| 248 | } |
| 249 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 250 | } |
Brian Carlstrom | 667ab7c | 2014-10-16 19:12:28 -0700 | [diff] [blame] | 251 | 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] | 252 | return nullptr; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 255 | // Needed as a compare functor for sets of const char |
| 256 | struct CharPointerComparator { |
| 257 | bool operator()(const char *str1, const char *str2) const { |
| 258 | return strcmp(str1, str2) < 0; |
| 259 | } |
| 260 | }; |
| 261 | |
| 262 | // 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] | 263 | static jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jobject cookie) { |
| 264 | std::unique_ptr<std::vector<const DexFile*>> dex_files = ConvertJavaArrayToNative(env, cookie); |
| 265 | if (dex_files.get() == nullptr) { |
| 266 | DCHECK(env->ExceptionCheck()); |
| 267 | return nullptr; |
| 268 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 269 | |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 270 | // Push all class descriptors into a set. Use set instead of unordered_set as we want to |
| 271 | // retrieve all in the end. |
| 272 | std::set<const char*, CharPointerComparator> descriptors; |
| 273 | for (auto& dex_file : *dex_files) { |
| 274 | for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) { |
| 275 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(i); |
| 276 | const char* descriptor = dex_file->GetClassDescriptor(class_def); |
| 277 | descriptors.insert(descriptor); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 278 | } |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 279 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 280 | |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 281 | // Now create output array and copy the set into it. |
| 282 | jobjectArray result = env->NewObjectArray(descriptors.size(), WellKnownClasses::java_lang_String, |
| 283 | nullptr); |
| 284 | if (result != nullptr) { |
| 285 | auto it = descriptors.begin(); |
| 286 | auto it_end = descriptors.end(); |
| 287 | jsize i = 0; |
| 288 | for (; it != it_end; it++, ++i) { |
| 289 | std::string descriptor(DescriptorToDot(*it)); |
| 290 | ScopedLocalRef<jstring> jdescriptor(env, env->NewStringUTF(descriptor.c_str())); |
| 291 | if (jdescriptor.get() == nullptr) { |
| 292 | return nullptr; |
Ian Rogers | dd157d7 | 2014-05-15 14:47:50 -0700 | [diff] [blame] | 293 | } |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 294 | env->SetObjectArrayElement(result, i, jdescriptor.get()); |
Ian Rogers | dd157d7 | 2014-05-15 14:47:50 -0700 | [diff] [blame] | 295 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 296 | } |
Ian Rogers | dd157d7 | 2014-05-15 14:47:50 -0700 | [diff] [blame] | 297 | return result; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 300 | static jint GetDexOptNeeded(JNIEnv* env, const char* filename, |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 301 | const char* pkgname, const char* instruction_set, const jboolean defer) { |
Brian Carlstrom | bf2cb16 | 2012-02-27 17:49:19 -0800 | [diff] [blame] | 302 | |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 303 | if ((filename == nullptr) || !OS::FileExists(filename)) { |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 304 | LOG(ERROR) << "DexFile_getDexOptNeeded file '" << filename << "' does not exist"; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 305 | ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException")); |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 306 | const char* message = (filename == nullptr) ? "<empty file name>" : filename; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 307 | env->ThrowNew(fnfe.get(), message); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 308 | return OatFileAssistant::kNoDexOptNeeded; |
Brian Carlstrom | 1d9f52b | 2011-10-13 10:50:45 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 311 | const InstructionSet target_instruction_set = GetInstructionSetFromString(instruction_set); |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 312 | if (target_instruction_set == kNone) { |
| 313 | ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException")); |
| 314 | std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set)); |
| 315 | env->ThrowNew(iae.get(), message.c_str()); |
| 316 | return 0; |
| 317 | } |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 318 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 319 | // TODO: Verify the dex location is well formed, and throw an IOException if |
| 320 | // not? |
| 321 | |
| 322 | OatFileAssistant oat_file_assistant(filename, target_instruction_set, false, pkgname); |
| 323 | |
| 324 | // Always treat elements of the bootclasspath as up-to-date. |
| 325 | if (oat_file_assistant.IsInBootClassPath()) { |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 326 | return OatFileAssistant::kNoDexOptNeeded; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | // TODO: Checking the profile should probably be done in the GetStatus() |
| 330 | // function. We have it here because GetStatus() should not be copying |
| 331 | // profile files. But who should be copying profile files? |
| 332 | if (oat_file_assistant.OdexFileIsOutOfDate()) { |
| 333 | // Needs recompile if profile has changed significantly. |
| 334 | if (Runtime::Current()->GetProfilerOptions().IsEnabled()) { |
| 335 | if (oat_file_assistant.IsProfileChangeSignificant()) { |
| 336 | if (!defer) { |
| 337 | oat_file_assistant.CopyProfileFile(); |
| 338 | } |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 339 | return OatFileAssistant::kDex2OatNeeded; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 340 | } else if (oat_file_assistant.ProfileExists() |
| 341 | && !oat_file_assistant.OldProfileExists()) { |
| 342 | if (!defer) { |
| 343 | oat_file_assistant.CopyProfileFile(); |
| 344 | } |
| 345 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 346 | } |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 347 | } |
| 348 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 349 | return oat_file_assistant.GetDexOptNeeded(); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 352 | static jint DexFile_getDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename, |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 353 | jstring javaPkgname, jstring javaInstructionSet, jboolean defer) { |
| 354 | ScopedUtfChars filename(env, javaFilename); |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 355 | if (env->ExceptionCheck()) { |
| 356 | return 0; |
| 357 | } |
| 358 | |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 359 | NullableScopedUtfChars pkgname(env, javaPkgname); |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 360 | |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 361 | ScopedUtfChars instruction_set(env, javaInstructionSet); |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 362 | if (env->ExceptionCheck()) { |
| 363 | return 0; |
| 364 | } |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 365 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 366 | return GetDexOptNeeded(env, filename.c_str(), pkgname.c_str(), |
| 367 | instruction_set.c_str(), defer); |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 368 | } |
| 369 | |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 370 | // public API, NULL pkgname |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 371 | static jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) { |
| 372 | const char* instruction_set = GetInstructionSetString(kRuntimeISA); |
| 373 | ScopedUtfChars filename(env, javaFilename); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 374 | jint status = GetDexOptNeeded(env, filename.c_str(), nullptr /* pkgname */, |
| 375 | instruction_set, false /* defer */); |
| 376 | return (status != OatFileAssistant::kNoDexOptNeeded) ? JNI_TRUE : JNI_FALSE; |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 377 | } |
| 378 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 379 | static JNINativeMethod gMethods[] = { |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 380 | NATIVE_METHOD(DexFile, closeDexFile, "(Ljava/lang/Object;)V"), |
| 381 | NATIVE_METHOD(DexFile, defineClassNative, "(Ljava/lang/String;Ljava/lang/ClassLoader;Ljava/lang/Object;)Ljava/lang/Class;"), |
| 382 | NATIVE_METHOD(DexFile, getClassNameList, "(Ljava/lang/Object;)[Ljava/lang/String;"), |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 383 | NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"), |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 384 | NATIVE_METHOD(DexFile, getDexOptNeeded, "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Z)I"), |
Andreas Gampe | 324b9bb | 2015-02-23 16:33:22 -0800 | [diff] [blame] | 385 | NATIVE_METHOD(DexFile, openDexFileNative, "(Ljava/lang/String;Ljava/lang/String;I)Ljava/lang/Object;"), |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 386 | }; |
| 387 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 388 | void register_dalvik_system_DexFile(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 389 | REGISTER_NATIVE_METHODS("dalvik/system/DexFile"); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | } // namespace art |