blob: 1e4b35f9652324e182e0a396d676d950dc976288 [file] [log] [blame]
Brian Carlstromf91c8c32011-09-21 17:30:34 -07001/*
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 Gampe277ccbd2014-11-03 21:36:10 -080017#include "dalvik_system_DexFile.h"
18
Elliott Hughes07ed66b2012-12-12 18:34:25 -080019#include "base/logging.h"
Andreas Gampe833a4852014-05-21 18:46:59 -070020#include "base/stl_util.h"
Andreas Gampe20c89302014-08-19 17:28:06 -070021#include "base/stringprintf.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070022#include "class_linker.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080023#include "common_throws.h"
Andreas Gampec38be812016-03-23 15:03:46 -070024#include "compiler_filter.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "dex_file-inl.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070026#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "mirror/class_loader.h"
Ian Rogers05f30572013-02-20 12:13:11 -080028#include "mirror/object-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "mirror/string.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080030#include "oat_file_assistant.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070031#include "oat_file_manager.h"
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -070032#include "os.h"
Calin Juravle9dae5b42014-04-07 16:36:21 +030033#include "profiler.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070034#include "runtime.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070035#include "scoped_thread_state_change.h"
Ian Rogersc9818482012-01-11 08:52:51 -080036#include "ScopedLocalRef.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070037#include "ScopedUtfChars.h"
Calin Juravlebb0b53f2014-05-23 17:33:29 +010038#include "utils.h"
Ian Rogersdd157d72014-05-15 14:47:50 -070039#include "well_known_classes.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070040#include "zip_archive.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070041
42namespace art {
43
Mathieu Chartiere58991b2015-10-13 07:59:34 -070044static bool ConvertJavaArrayToDexFiles(
45 JNIEnv* env,
46 jobject arrayObject,
47 /*out*/ std::vector<const DexFile*>& dex_files,
48 /*out*/ const OatFile*& oat_file) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -080049 jarray array = reinterpret_cast<jarray>(arrayObject);
50
51 jsize array_size = env->GetArrayLength(array);
52 if (env->ExceptionCheck() == JNI_TRUE) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -070053 return false;
Andreas Gampe324b9bb2015-02-23 16:33:22 -080054 }
55
56 // TODO: Optimize. On 32bit we can use an int array.
57 jboolean is_long_data_copied;
58 jlong* long_data = env->GetLongArrayElements(reinterpret_cast<jlongArray>(array),
59 &is_long_data_copied);
60 if (env->ExceptionCheck() == JNI_TRUE) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -070061 return false;
Andreas Gampe324b9bb2015-02-23 16:33:22 -080062 }
63
Mathieu Chartiere58991b2015-10-13 07:59:34 -070064 oat_file = reinterpret_cast<const OatFile*>(static_cast<uintptr_t>(long_data[kOatFileIndex]));
65 dex_files.reserve(array_size - 1);
66 for (jsize i = kDexFileIndexStart; i < array_size; ++i) {
67 dex_files.push_back(reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(long_data[i])));
Andreas Gampe324b9bb2015-02-23 16:33:22 -080068 }
69
70 env->ReleaseLongArrayElements(reinterpret_cast<jlongArray>(array), long_data, JNI_ABORT);
Mathieu Chartiere58991b2015-10-13 07:59:34 -070071 return env->ExceptionCheck() != JNI_TRUE;
Andreas Gampe324b9bb2015-02-23 16:33:22 -080072}
73
Mathieu Chartiere58991b2015-10-13 07:59:34 -070074static jlongArray ConvertDexFilesToJavaArray(JNIEnv* env,
75 const OatFile* oat_file,
76 std::vector<std::unique_ptr<const DexFile>>& vec) {
77 // Add one for the oat file.
Mathieu Chartier80b37b72015-10-12 18:13:39 -070078 jlongArray long_array = env->NewLongArray(static_cast<jsize>(kDexFileIndexStart + vec.size()));
Andreas Gampe324b9bb2015-02-23 16:33:22 -080079 if (env->ExceptionCheck() == JNI_TRUE) {
80 return nullptr;
81 }
82
83 jboolean is_long_data_copied;
84 jlong* long_data = env->GetLongArrayElements(long_array, &is_long_data_copied);
85 if (env->ExceptionCheck() == JNI_TRUE) {
86 return nullptr;
87 }
88
Mathieu Chartiere58991b2015-10-13 07:59:34 -070089 long_data[kOatFileIndex] = reinterpret_cast<uintptr_t>(oat_file);
90 for (size_t i = 0; i < vec.size(); ++i) {
91 long_data[kDexFileIndexStart + i] = reinterpret_cast<uintptr_t>(vec[i].get());
Andreas Gampe324b9bb2015-02-23 16:33:22 -080092 }
93
94 env->ReleaseLongArrayElements(long_array, long_data, 0);
95 if (env->ExceptionCheck() == JNI_TRUE) {
96 return nullptr;
97 }
98
99 // Now release all the unique_ptrs.
100 for (auto& dex_file : vec) {
101 dex_file.release();
102 }
103
104 return long_array;
105}
106
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700107// A smart pointer that provides read-only access to a Java string's UTF chars.
108// Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if
109// passed a null jstring. The correct idiom is:
110//
111// NullableScopedUtfChars name(env, javaName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -0700112// if (env->ExceptionCheck()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700113// return null;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700114// }
115// // ... use name.c_str()
116//
117// TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option.
118class NullableScopedUtfChars {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800119 public:
120 NullableScopedUtfChars(JNIEnv* env, jstring s) : mEnv(env), mString(s) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700121 mUtfChars = (s != nullptr) ? env->GetStringUTFChars(s, nullptr) : nullptr;
Elliott Hughesba8eee12012-01-24 20:25:24 -0800122 }
123
124 ~NullableScopedUtfChars() {
125 if (mUtfChars) {
126 mEnv->ReleaseStringUTFChars(mString, mUtfChars);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700127 }
Elliott Hughesba8eee12012-01-24 20:25:24 -0800128 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700129
Elliott Hughesba8eee12012-01-24 20:25:24 -0800130 const char* c_str() const {
131 return mUtfChars;
132 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700133
Elliott Hughesba8eee12012-01-24 20:25:24 -0800134 size_t size() const {
135 return strlen(mUtfChars);
136 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700137
Elliott Hughesba8eee12012-01-24 20:25:24 -0800138 // Element access.
139 const char& operator[](size_t n) const {
140 return mUtfChars[n];
141 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700142
Elliott Hughesba8eee12012-01-24 20:25:24 -0800143 private:
144 JNIEnv* mEnv;
145 jstring mString;
146 const char* mUtfChars;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700147
Elliott Hughesba8eee12012-01-24 20:25:24 -0800148 // Disallow copy and assignment.
149 NullableScopedUtfChars(const NullableScopedUtfChars&);
150 void operator=(const NullableScopedUtfChars&);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700151};
152
Mathieu Chartierb190d942015-11-12 10:00:58 -0800153static jobject DexFile_openDexFileNative(JNIEnv* env,
154 jclass,
155 jstring javaSourceName,
156 jstring javaOutputName,
157 jint flags ATTRIBUTE_UNUSED,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800158 jobject class_loader,
159 jobjectArray dex_elements) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700160 ScopedUtfChars sourceName(env, javaSourceName);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700161 if (sourceName.c_str() == nullptr) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700162 return 0;
163 }
164 NullableScopedUtfChars outputName(env, javaOutputName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -0700165 if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700166 return 0;
167 }
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700168 Runtime* const runtime = Runtime::Current();
169 ClassLinker* linker = runtime->GetClassLinker();
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800170 std::vector<std::unique_ptr<const DexFile>> dex_files;
Andreas Gampe833a4852014-05-21 18:46:59 -0700171 std::vector<std::string> error_msgs;
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700172 const OatFile* oat_file = nullptr;
Andreas Gampe833a4852014-05-21 18:46:59 -0700173
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700174 dex_files = runtime->GetOatFileManager().OpenDexFilesFromOat(sourceName.c_str(),
175 outputName.c_str(),
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800176 class_loader,
177 dex_elements,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700178 /*out*/ &oat_file,
179 /*out*/ &error_msgs);
Andreas Gampe833a4852014-05-21 18:46:59 -0700180
Richard Uhler66d874d2015-01-15 09:37:19 -0800181 if (!dex_files.empty()) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700182 jlongArray array = ConvertDexFilesToJavaArray(env, oat_file, dex_files);
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800183 if (array == nullptr) {
184 ScopedObjectAccess soa(env);
185 for (auto& dex_file : dex_files) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700186 if (linker->FindDexCache(soa.Self(), *dex_file, true) != nullptr) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800187 dex_file.release();
188 }
189 }
190 }
191 return array;
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700192 } else {
Vladimir Marko60836d52014-01-16 15:53:38 +0000193 ScopedObjectAccess soa(env);
Andreas Gampe329d1882014-04-08 10:32:19 -0700194 CHECK(!error_msgs.empty());
195 // The most important message is at the end. So set up nesting by going forward, which will
196 // wrap the existing exception as a cause for the following one.
197 auto it = error_msgs.begin();
198 auto itEnd = error_msgs.end();
199 for ( ; it != itEnd; ++it) {
200 ThrowWrappedIOException("%s", it->c_str());
201 }
202
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800203 return nullptr;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700204 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700205}
206
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700207static jboolean DexFile_closeDexFile(JNIEnv* env, jclass, jobject cookie) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700208 std::vector<const DexFile*> dex_files;
209 const OatFile* oat_file;
210 if (!ConvertJavaArrayToDexFiles(env, cookie, dex_files, oat_file)) {
211 Thread::Current()->AssertPendingException();
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700212 return JNI_FALSE;
213 }
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700214 Runtime* const runtime = Runtime::Current();
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700215 bool all_deleted = true;
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700216 {
217 ScopedObjectAccess soa(env);
218 mirror::Object* dex_files_object = soa.Decode<mirror::Object*>(cookie);
219 mirror::LongArray* long_dex_files = dex_files_object->AsLongArray();
220 // Delete dex files associated with this dalvik.system.DexFile since there should not be running
221 // code using it. dex_files is a vector due to multidex.
222 ClassLinker* const class_linker = runtime->GetClassLinker();
223 int32_t i = kDexFileIndexStart; // Oat file is at index 0.
224 for (const DexFile* dex_file : dex_files) {
225 if (dex_file != nullptr) {
226 // Only delete the dex file if the dex cache is not found to prevent runtime crashes if there
227 // are calls to DexFile.close while the ART DexFile is still in use.
228 if (class_linker->FindDexCache(soa.Self(), *dex_file, true) == nullptr) {
229 // Clear the element in the array so that we can call close again.
230 long_dex_files->Set(i, 0);
231 delete dex_file;
232 } else {
233 all_deleted = false;
234 }
235 }
236 ++i;
Andreas Gampe833a4852014-05-21 18:46:59 -0700237 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700238 }
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700239
Mathieu Chartierfdccbd42015-10-14 10:58:41 -0700240 // oat_file can be null if we are running without dex2oat.
241 if (all_deleted && oat_file != nullptr) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700242 // If all of the dex files are no longer in use we can unmap the corresponding oat file.
243 VLOG(class_linker) << "Unregistering " << oat_file;
244 runtime->GetOatFileManager().UnRegisterAndDeleteOatFile(oat_file);
245 }
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700246 return all_deleted ? JNI_TRUE : JNI_FALSE;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700247}
248
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700249static jclass DexFile_defineClassNative(JNIEnv* env,
250 jclass,
251 jstring javaName,
252 jobject javaLoader,
Mathieu Chartier00310e02015-10-17 12:46:42 -0700253 jobject cookie,
254 jobject dexFile) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700255 std::vector<const DexFile*> dex_files;
256 const OatFile* oat_file;
257 if (!ConvertJavaArrayToDexFiles(env, cookie, /*out*/ dex_files, /*out*/ oat_file)) {
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700258 VLOG(class_linker) << "Failed to find dex_file";
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800259 DCHECK(env->ExceptionCheck());
260 return nullptr;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700261 }
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800262
Brian Carlstromdf143242011-10-10 18:05:34 -0700263 ScopedUtfChars class_name(env, javaName);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700264 if (class_name.c_str() == nullptr) {
Brian Carlstrom2e450bf2013-09-06 15:39:46 -0700265 VLOG(class_linker) << "Failed to find class_name";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700266 return nullptr;
Brian Carlstromdf143242011-10-10 18:05:34 -0700267 }
Elliott Hughes95572412011-12-13 18:14:20 -0800268 const std::string descriptor(DotToDescriptor(class_name.c_str()));
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800269 const size_t hash(ComputeModifiedUtf8Hash(descriptor.c_str()));
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700270 for (auto& dex_file : dex_files) {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800271 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor.c_str(), hash);
Andreas Gampe833a4852014-05-21 18:46:59 -0700272 if (dex_class_def != nullptr) {
273 ScopedObjectAccess soa(env);
274 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Andreas Gampe833a4852014-05-21 18:46:59 -0700275 StackHandleScope<1> hs(soa.Self());
276 Handle<mirror::ClassLoader> class_loader(
277 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(javaLoader)));
Mathieu Chartierd57d4542015-10-14 10:55:30 -0700278 class_linker->RegisterDexFile(
279 *dex_file,
280 class_linker->GetOrCreateAllocatorForClassLoader(class_loader.Get()));
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700281 mirror::Class* result = class_linker->DefineClass(soa.Self(),
282 descriptor.c_str(),
283 hash,
284 class_loader,
285 *dex_file,
286 *dex_class_def);
Mathieu Chartier00310e02015-10-17 12:46:42 -0700287 // Add the used dex file. This only required for the DexFile.loadClass API since normal
288 // class loaders already keep their dex files live.
289 class_linker->InsertDexFileInToClassLoader(soa.Decode<mirror::Object*>(dexFile),
290 class_loader.Get());
Andreas Gampe833a4852014-05-21 18:46:59 -0700291 if (result != nullptr) {
Brian Carlstrom667ab7c2014-10-16 19:12:28 -0700292 VLOG(class_linker) << "DexFile_defineClassNative returning " << result
293 << " for " << class_name.c_str();
Andreas Gampe833a4852014-05-21 18:46:59 -0700294 return soa.AddLocalReference<jclass>(result);
295 }
296 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700297 }
Brian Carlstrom667ab7c2014-10-16 19:12:28 -0700298 VLOG(class_linker) << "Failed to find dex_class_def " << class_name.c_str();
Andreas Gampe833a4852014-05-21 18:46:59 -0700299 return nullptr;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700300}
301
Andreas Gampe833a4852014-05-21 18:46:59 -0700302// Needed as a compare functor for sets of const char
303struct CharPointerComparator {
304 bool operator()(const char *str1, const char *str2) const {
305 return strcmp(str1, str2) < 0;
306 }
307};
308
309// Note: this can be an expensive call, as we sort out duplicates in MultiDex files.
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800310static jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jobject cookie) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700311 const OatFile* oat_file = nullptr;
312 std::vector<const DexFile*> dex_files;
313 if (!ConvertJavaArrayToDexFiles(env, cookie, /*out */ dex_files, /* out */ oat_file)) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800314 DCHECK(env->ExceptionCheck());
315 return nullptr;
316 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700317
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800318 // Push all class descriptors into a set. Use set instead of unordered_set as we want to
319 // retrieve all in the end.
320 std::set<const char*, CharPointerComparator> descriptors;
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700321 for (auto& dex_file : dex_files) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800322 for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
323 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
324 const char* descriptor = dex_file->GetClassDescriptor(class_def);
325 descriptors.insert(descriptor);
Andreas Gampe833a4852014-05-21 18:46:59 -0700326 }
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800327 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700328
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800329 // Now create output array and copy the set into it.
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700330 jobjectArray result = env->NewObjectArray(descriptors.size(),
331 WellKnownClasses::java_lang_String,
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800332 nullptr);
333 if (result != nullptr) {
334 auto it = descriptors.begin();
335 auto it_end = descriptors.end();
336 jsize i = 0;
337 for (; it != it_end; it++, ++i) {
338 std::string descriptor(DescriptorToDot(*it));
339 ScopedLocalRef<jstring> jdescriptor(env, env->NewStringUTF(descriptor.c_str()));
340 if (jdescriptor.get() == nullptr) {
341 return nullptr;
Ian Rogersdd157d72014-05-15 14:47:50 -0700342 }
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800343 env->SetObjectArrayElement(result, i, jdescriptor.get());
Ian Rogersdd157d72014-05-15 14:47:50 -0700344 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700345 }
Ian Rogersdd157d72014-05-15 14:47:50 -0700346 return result;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700347}
348
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700349static jint GetDexOptNeeded(JNIEnv* env,
350 const char* filename,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700351 const char* instruction_set,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000352 const char* compiler_filter_name,
353 bool profile_changed) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100354 if ((filename == nullptr) || !OS::FileExists(filename)) {
Richard Uhler95abd042015-03-24 09:51:28 -0700355 LOG(ERROR) << "DexFile_getDexOptNeeded file '" << filename << "' does not exist";
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700356 ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException"));
Narayan Kamath11d9f062014-04-23 20:24:57 +0100357 const char* message = (filename == nullptr) ? "<empty file name>" : filename;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700358 env->ThrowNew(fnfe.get(), message);
Calin Juravleb077e152016-02-18 18:47:37 +0000359 return -1;
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700360 }
361
Alex Light6e183f22014-07-18 14:57:04 -0700362 const InstructionSet target_instruction_set = GetInstructionSetFromString(instruction_set);
Andreas Gampe20c89302014-08-19 17:28:06 -0700363 if (target_instruction_set == kNone) {
364 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
365 std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set));
366 env->ThrowNew(iae.get(), message.c_str());
Calin Juravleb077e152016-02-18 18:47:37 +0000367 return -1;
Andreas Gampe20c89302014-08-19 17:28:06 -0700368 }
Alex Light6e183f22014-07-18 14:57:04 -0700369
Andreas Gampe29d38e72016-03-23 15:31:51 +0000370 CompilerFilter::Filter filter;
371 if (!CompilerFilter::ParseCompilerFilter(compiler_filter_name, &filter)) {
372 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
373 std::string message(StringPrintf("Compiler filter %s is invalid.", compiler_filter_name));
374 env->ThrowNew(iae.get(), message.c_str());
375 return -1;
376 }
377
Richard Uhler66d874d2015-01-15 09:37:19 -0800378 // TODO: Verify the dex location is well formed, and throw an IOException if
379 // not?
Andreas Gampe29d38e72016-03-23 15:31:51 +0000380
381 OatFileAssistant oat_file_assistant(filename, target_instruction_set, profile_changed, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800382
383 // Always treat elements of the bootclasspath as up-to-date.
384 if (oat_file_assistant.IsInBootClassPath()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700385 return OatFileAssistant::kNoDexOptNeeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800386 }
Andreas Gampe29d38e72016-03-23 15:31:51 +0000387 return oat_file_assistant.GetDexOptNeeded(filter);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700388}
389
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700390static jint DexFile_getDexOptNeeded(JNIEnv* env,
391 jclass,
392 jstring javaFilename,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700393 jstring javaInstructionSet,
Andreas Gampec38be812016-03-23 15:03:46 -0700394 jstring javaTargetCompilerFilter,
395 jboolean newProfile) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100396 ScopedUtfChars filename(env, javaFilename);
Andreas Gampe20c89302014-08-19 17:28:06 -0700397 if (env->ExceptionCheck()) {
Calin Juravleb077e152016-02-18 18:47:37 +0000398 return -1;
Andreas Gampe20c89302014-08-19 17:28:06 -0700399 }
400
Narayan Kamath11d9f062014-04-23 20:24:57 +0100401 ScopedUtfChars instruction_set(env, javaInstructionSet);
Andreas Gampe20c89302014-08-19 17:28:06 -0700402 if (env->ExceptionCheck()) {
Calin Juravleb077e152016-02-18 18:47:37 +0000403 return -1;
Andreas Gampe20c89302014-08-19 17:28:06 -0700404 }
Narayan Kamath11d9f062014-04-23 20:24:57 +0100405
Andreas Gampec38be812016-03-23 15:03:46 -0700406 ScopedUtfChars target_compiler_filter(env, javaTargetCompilerFilter);
407 if (env->ExceptionCheck()) {
408 return -1;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000409 }
Andreas Gampec38be812016-03-23 15:03:46 -0700410
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700411 return GetDexOptNeeded(env,
412 filename.c_str(),
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700413 instruction_set.c_str(),
Andreas Gampec38be812016-03-23 15:03:46 -0700414 target_compiler_filter.c_str(),
415 newProfile == JNI_TRUE);
Narayan Kamath11d9f062014-04-23 20:24:57 +0100416}
417
Calin Juravleb077e152016-02-18 18:47:37 +0000418// public API
Narayan Kamath11d9f062014-04-23 20:24:57 +0100419static jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
420 const char* instruction_set = GetInstructionSetString(kRuntimeISA);
421 ScopedUtfChars filename(env, javaFilename);
Calin Juravleb077e152016-02-18 18:47:37 +0000422 jint status = GetDexOptNeeded(
423 env,
424 filename.c_str(),
425 instruction_set,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000426 "speed-profile",
427 /*profile_changed*/false);
Richard Uhler95abd042015-03-24 09:51:28 -0700428 return (status != OatFileAssistant::kNoDexOptNeeded) ? JNI_TRUE : JNI_FALSE;
Dave Allison39c3bfb2014-01-28 18:33:52 -0800429}
430
Andreas Gampec38be812016-03-23 15:03:46 -0700431static jboolean DexFile_isValidCompilerFilter(JNIEnv* env,
432 jclass javeDexFileClass ATTRIBUTE_UNUSED,
433 jstring javaCompilerFilter) {
434 ScopedUtfChars compiler_filter(env, javaCompilerFilter);
435 if (env->ExceptionCheck()) {
436 return -1;
437 }
438
439 CompilerFilter::Filter filter;
440 return CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)
441 ? JNI_TRUE : JNI_FALSE;
442}
443
444static jboolean DexFile_isProfileGuidedCompilerFilter(JNIEnv* env,
445 jclass javeDexFileClass ATTRIBUTE_UNUSED,
446 jstring javaCompilerFilter) {
447 ScopedUtfChars compiler_filter(env, javaCompilerFilter);
448 if (env->ExceptionCheck()) {
449 return -1;
450 }
451
452 CompilerFilter::Filter filter;
453 if (!CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)) {
454 return JNI_FALSE;
455 }
456 return CompilerFilter::DependsOnProfile(filter) ? JNI_TRUE : JNI_FALSE;
457}
458
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700459static JNINativeMethod gMethods[] = {
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700460 NATIVE_METHOD(DexFile, closeDexFile, "(Ljava/lang/Object;)Z"),
Mathieu Chartier00310e02015-10-17 12:46:42 -0700461 NATIVE_METHOD(DexFile,
462 defineClassNative,
463 "(Ljava/lang/String;"
464 "Ljava/lang/ClassLoader;"
465 "Ljava/lang/Object;"
466 "Ldalvik/system/DexFile;"
467 ")Ljava/lang/Class;"),
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800468 NATIVE_METHOD(DexFile, getClassNameList, "(Ljava/lang/Object;)[Ljava/lang/String;"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700469 NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700470 NATIVE_METHOD(DexFile, getDexOptNeeded,
Andreas Gampec38be812016-03-23 15:03:46 -0700471 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Z)I"),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700472 NATIVE_METHOD(DexFile, openDexFileNative,
Mathieu Chartier689a7002015-11-20 10:29:42 -0800473 "(Ljava/lang/String;"
474 "Ljava/lang/String;"
475 "I"
476 "Ljava/lang/ClassLoader;"
477 "[Ldalvik/system/DexPathList$Element;"
478 ")Ljava/lang/Object;"),
Andreas Gampec38be812016-03-23 15:03:46 -0700479 NATIVE_METHOD(DexFile, isValidCompilerFilter, "(Ljava/lang/String;)Z"),
480 NATIVE_METHOD(DexFile, isProfileGuidedCompilerFilter, "(Ljava/lang/String;)Z"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700481};
482
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700483void register_dalvik_system_DexFile(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700484 REGISTER_NATIVE_METHODS("dalvik/system/DexFile");
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700485}
486
487} // namespace art