blob: f30f7a641e33b5bed0ea7c8a45357b366a88e24a [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
Narayan Kamath8943c1d2016-05-02 13:14:48 +010019#include <sstream>
20
Elliott Hughes07ed66b2012-12-12 18:34:25 -080021#include "base/logging.h"
Andreas Gampe833a4852014-05-21 18:46:59 -070022#include "base/stl_util.h"
Andreas Gampe20c89302014-08-19 17:28:06 -070023#include "base/stringprintf.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070024#include "class_linker.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080025#include "common_throws.h"
Andreas Gampec38be812016-03-23 15:03:46 -070026#include "compiler_filter.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070027#include "dex_file-inl.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070028#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "mirror/class_loader.h"
Ian Rogers05f30572013-02-20 12:13:11 -080030#include "mirror/object-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031#include "mirror/string.h"
Narayan Kamath8943c1d2016-05-02 13:14:48 +010032#include "oat_file.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080033#include "oat_file_assistant.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070034#include "oat_file_manager.h"
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -070035#include "os.h"
Calin Juravle9dae5b42014-04-07 16:36:21 +030036#include "profiler.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070037#include "runtime.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070038#include "scoped_thread_state_change.h"
Ian Rogersc9818482012-01-11 08:52:51 -080039#include "ScopedLocalRef.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070040#include "ScopedUtfChars.h"
Calin Juravlebb0b53f2014-05-23 17:33:29 +010041#include "utils.h"
Ian Rogersdd157d72014-05-15 14:47:50 -070042#include "well_known_classes.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070043#include "zip_archive.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070044
45namespace art {
46
Mathieu Chartiere58991b2015-10-13 07:59:34 -070047static bool ConvertJavaArrayToDexFiles(
48 JNIEnv* env,
49 jobject arrayObject,
50 /*out*/ std::vector<const DexFile*>& dex_files,
51 /*out*/ const OatFile*& oat_file) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -080052 jarray array = reinterpret_cast<jarray>(arrayObject);
53
54 jsize array_size = env->GetArrayLength(array);
55 if (env->ExceptionCheck() == JNI_TRUE) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -070056 return false;
Andreas Gampe324b9bb2015-02-23 16:33:22 -080057 }
58
59 // TODO: Optimize. On 32bit we can use an int array.
60 jboolean is_long_data_copied;
61 jlong* long_data = env->GetLongArrayElements(reinterpret_cast<jlongArray>(array),
62 &is_long_data_copied);
63 if (env->ExceptionCheck() == JNI_TRUE) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -070064 return false;
Andreas Gampe324b9bb2015-02-23 16:33:22 -080065 }
66
Mathieu Chartiere58991b2015-10-13 07:59:34 -070067 oat_file = reinterpret_cast<const OatFile*>(static_cast<uintptr_t>(long_data[kOatFileIndex]));
68 dex_files.reserve(array_size - 1);
69 for (jsize i = kDexFileIndexStart; i < array_size; ++i) {
70 dex_files.push_back(reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(long_data[i])));
Andreas Gampe324b9bb2015-02-23 16:33:22 -080071 }
72
73 env->ReleaseLongArrayElements(reinterpret_cast<jlongArray>(array), long_data, JNI_ABORT);
Mathieu Chartiere58991b2015-10-13 07:59:34 -070074 return env->ExceptionCheck() != JNI_TRUE;
Andreas Gampe324b9bb2015-02-23 16:33:22 -080075}
76
Mathieu Chartiere58991b2015-10-13 07:59:34 -070077static jlongArray ConvertDexFilesToJavaArray(JNIEnv* env,
78 const OatFile* oat_file,
79 std::vector<std::unique_ptr<const DexFile>>& vec) {
80 // Add one for the oat file.
Mathieu Chartier80b37b72015-10-12 18:13:39 -070081 jlongArray long_array = env->NewLongArray(static_cast<jsize>(kDexFileIndexStart + vec.size()));
Andreas Gampe324b9bb2015-02-23 16:33:22 -080082 if (env->ExceptionCheck() == JNI_TRUE) {
83 return nullptr;
84 }
85
86 jboolean is_long_data_copied;
87 jlong* long_data = env->GetLongArrayElements(long_array, &is_long_data_copied);
88 if (env->ExceptionCheck() == JNI_TRUE) {
89 return nullptr;
90 }
91
Mathieu Chartiere58991b2015-10-13 07:59:34 -070092 long_data[kOatFileIndex] = reinterpret_cast<uintptr_t>(oat_file);
93 for (size_t i = 0; i < vec.size(); ++i) {
94 long_data[kDexFileIndexStart + i] = reinterpret_cast<uintptr_t>(vec[i].get());
Andreas Gampe324b9bb2015-02-23 16:33:22 -080095 }
96
97 env->ReleaseLongArrayElements(long_array, long_data, 0);
98 if (env->ExceptionCheck() == JNI_TRUE) {
99 return nullptr;
100 }
101
102 // Now release all the unique_ptrs.
103 for (auto& dex_file : vec) {
104 dex_file.release();
105 }
106
107 return long_array;
108}
109
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700110// A smart pointer that provides read-only access to a Java string's UTF chars.
111// Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if
112// passed a null jstring. The correct idiom is:
113//
114// NullableScopedUtfChars name(env, javaName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -0700115// if (env->ExceptionCheck()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700116// return null;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700117// }
118// // ... use name.c_str()
119//
120// TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option.
121class NullableScopedUtfChars {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800122 public:
123 NullableScopedUtfChars(JNIEnv* env, jstring s) : mEnv(env), mString(s) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700124 mUtfChars = (s != nullptr) ? env->GetStringUTFChars(s, nullptr) : nullptr;
Elliott Hughesba8eee12012-01-24 20:25:24 -0800125 }
126
127 ~NullableScopedUtfChars() {
128 if (mUtfChars) {
129 mEnv->ReleaseStringUTFChars(mString, mUtfChars);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700130 }
Elliott Hughesba8eee12012-01-24 20:25:24 -0800131 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700132
Elliott Hughesba8eee12012-01-24 20:25:24 -0800133 const char* c_str() const {
134 return mUtfChars;
135 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700136
Elliott Hughesba8eee12012-01-24 20:25:24 -0800137 size_t size() const {
138 return strlen(mUtfChars);
139 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700140
Elliott Hughesba8eee12012-01-24 20:25:24 -0800141 // Element access.
142 const char& operator[](size_t n) const {
143 return mUtfChars[n];
144 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700145
Elliott Hughesba8eee12012-01-24 20:25:24 -0800146 private:
147 JNIEnv* mEnv;
148 jstring mString;
149 const char* mUtfChars;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700150
Elliott Hughesba8eee12012-01-24 20:25:24 -0800151 // Disallow copy and assignment.
152 NullableScopedUtfChars(const NullableScopedUtfChars&);
153 void operator=(const NullableScopedUtfChars&);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700154};
155
Mathieu Chartierb190d942015-11-12 10:00:58 -0800156static jobject DexFile_openDexFileNative(JNIEnv* env,
157 jclass,
158 jstring javaSourceName,
159 jstring javaOutputName,
160 jint flags ATTRIBUTE_UNUSED,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800161 jobject class_loader,
162 jobjectArray dex_elements) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700163 ScopedUtfChars sourceName(env, javaSourceName);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700164 if (sourceName.c_str() == nullptr) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700165 return 0;
166 }
167 NullableScopedUtfChars outputName(env, javaOutputName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -0700168 if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700169 return 0;
170 }
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700171 Runtime* const runtime = Runtime::Current();
172 ClassLinker* linker = runtime->GetClassLinker();
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800173 std::vector<std::unique_ptr<const DexFile>> dex_files;
Andreas Gampe833a4852014-05-21 18:46:59 -0700174 std::vector<std::string> error_msgs;
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700175 const OatFile* oat_file = nullptr;
Andreas Gampe833a4852014-05-21 18:46:59 -0700176
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700177 dex_files = runtime->GetOatFileManager().OpenDexFilesFromOat(sourceName.c_str(),
178 outputName.c_str(),
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800179 class_loader,
180 dex_elements,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700181 /*out*/ &oat_file,
182 /*out*/ &error_msgs);
Andreas Gampe833a4852014-05-21 18:46:59 -0700183
Richard Uhler66d874d2015-01-15 09:37:19 -0800184 if (!dex_files.empty()) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700185 jlongArray array = ConvertDexFilesToJavaArray(env, oat_file, dex_files);
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800186 if (array == nullptr) {
187 ScopedObjectAccess soa(env);
188 for (auto& dex_file : dex_files) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700189 if (linker->FindDexCache(soa.Self(), *dex_file, true) != nullptr) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800190 dex_file.release();
191 }
192 }
193 }
194 return array;
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700195 } else {
Vladimir Marko60836d52014-01-16 15:53:38 +0000196 ScopedObjectAccess soa(env);
Andreas Gampe329d1882014-04-08 10:32:19 -0700197 CHECK(!error_msgs.empty());
198 // The most important message is at the end. So set up nesting by going forward, which will
199 // wrap the existing exception as a cause for the following one.
200 auto it = error_msgs.begin();
201 auto itEnd = error_msgs.end();
202 for ( ; it != itEnd; ++it) {
203 ThrowWrappedIOException("%s", it->c_str());
204 }
205
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800206 return nullptr;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700207 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700208}
209
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700210static jboolean DexFile_closeDexFile(JNIEnv* env, jclass, jobject cookie) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700211 std::vector<const DexFile*> dex_files;
212 const OatFile* oat_file;
213 if (!ConvertJavaArrayToDexFiles(env, cookie, dex_files, oat_file)) {
214 Thread::Current()->AssertPendingException();
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700215 return JNI_FALSE;
216 }
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700217 Runtime* const runtime = Runtime::Current();
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700218 bool all_deleted = true;
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700219 {
220 ScopedObjectAccess soa(env);
221 mirror::Object* dex_files_object = soa.Decode<mirror::Object*>(cookie);
222 mirror::LongArray* long_dex_files = dex_files_object->AsLongArray();
223 // Delete dex files associated with this dalvik.system.DexFile since there should not be running
224 // code using it. dex_files is a vector due to multidex.
225 ClassLinker* const class_linker = runtime->GetClassLinker();
226 int32_t i = kDexFileIndexStart; // Oat file is at index 0.
227 for (const DexFile* dex_file : dex_files) {
228 if (dex_file != nullptr) {
229 // Only delete the dex file if the dex cache is not found to prevent runtime crashes if there
230 // are calls to DexFile.close while the ART DexFile is still in use.
231 if (class_linker->FindDexCache(soa.Self(), *dex_file, true) == nullptr) {
232 // Clear the element in the array so that we can call close again.
233 long_dex_files->Set(i, 0);
234 delete dex_file;
235 } else {
236 all_deleted = false;
237 }
238 }
239 ++i;
Andreas Gampe833a4852014-05-21 18:46:59 -0700240 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700241 }
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700242
Mathieu Chartierfdccbd42015-10-14 10:58:41 -0700243 // oat_file can be null if we are running without dex2oat.
244 if (all_deleted && oat_file != nullptr) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700245 // If all of the dex files are no longer in use we can unmap the corresponding oat file.
246 VLOG(class_linker) << "Unregistering " << oat_file;
247 runtime->GetOatFileManager().UnRegisterAndDeleteOatFile(oat_file);
248 }
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700249 return all_deleted ? JNI_TRUE : JNI_FALSE;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700250}
251
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700252static jclass DexFile_defineClassNative(JNIEnv* env,
253 jclass,
254 jstring javaName,
255 jobject javaLoader,
Mathieu Chartier00310e02015-10-17 12:46:42 -0700256 jobject cookie,
257 jobject dexFile) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700258 std::vector<const DexFile*> dex_files;
259 const OatFile* oat_file;
260 if (!ConvertJavaArrayToDexFiles(env, cookie, /*out*/ dex_files, /*out*/ oat_file)) {
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700261 VLOG(class_linker) << "Failed to find dex_file";
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800262 DCHECK(env->ExceptionCheck());
263 return nullptr;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700264 }
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800265
Brian Carlstromdf143242011-10-10 18:05:34 -0700266 ScopedUtfChars class_name(env, javaName);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700267 if (class_name.c_str() == nullptr) {
Brian Carlstrom2e450bf2013-09-06 15:39:46 -0700268 VLOG(class_linker) << "Failed to find class_name";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700269 return nullptr;
Brian Carlstromdf143242011-10-10 18:05:34 -0700270 }
Elliott Hughes95572412011-12-13 18:14:20 -0800271 const std::string descriptor(DotToDescriptor(class_name.c_str()));
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800272 const size_t hash(ComputeModifiedUtf8Hash(descriptor.c_str()));
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700273 for (auto& dex_file : dex_files) {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800274 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor.c_str(), hash);
Andreas Gampe833a4852014-05-21 18:46:59 -0700275 if (dex_class_def != nullptr) {
276 ScopedObjectAccess soa(env);
277 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Andreas Gampe833a4852014-05-21 18:46:59 -0700278 StackHandleScope<1> hs(soa.Self());
279 Handle<mirror::ClassLoader> class_loader(
280 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(javaLoader)));
Brian Carlstrom6d335632016-06-04 00:22:32 +0000281 class_linker->RegisterDexFile(
282 *dex_file,
283 class_linker->GetOrCreateAllocatorForClassLoader(class_loader.Get()));
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700284 mirror::Class* result = class_linker->DefineClass(soa.Self(),
285 descriptor.c_str(),
286 hash,
287 class_loader,
288 *dex_file,
289 *dex_class_def);
Mathieu Chartier00310e02015-10-17 12:46:42 -0700290 // Add the used dex file. This only required for the DexFile.loadClass API since normal
291 // class loaders already keep their dex files live.
292 class_linker->InsertDexFileInToClassLoader(soa.Decode<mirror::Object*>(dexFile),
293 class_loader.Get());
Andreas Gampe833a4852014-05-21 18:46:59 -0700294 if (result != nullptr) {
Brian Carlstrom667ab7c2014-10-16 19:12:28 -0700295 VLOG(class_linker) << "DexFile_defineClassNative returning " << result
296 << " for " << class_name.c_str();
Andreas Gampe833a4852014-05-21 18:46:59 -0700297 return soa.AddLocalReference<jclass>(result);
298 }
299 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700300 }
Brian Carlstrom667ab7c2014-10-16 19:12:28 -0700301 VLOG(class_linker) << "Failed to find dex_class_def " << class_name.c_str();
Andreas Gampe833a4852014-05-21 18:46:59 -0700302 return nullptr;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700303}
304
Andreas Gampe833a4852014-05-21 18:46:59 -0700305// Needed as a compare functor for sets of const char
306struct CharPointerComparator {
307 bool operator()(const char *str1, const char *str2) const {
308 return strcmp(str1, str2) < 0;
309 }
310};
311
312// Note: this can be an expensive call, as we sort out duplicates in MultiDex files.
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800313static jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jobject cookie) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700314 const OatFile* oat_file = nullptr;
315 std::vector<const DexFile*> dex_files;
316 if (!ConvertJavaArrayToDexFiles(env, cookie, /*out */ dex_files, /* out */ oat_file)) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800317 DCHECK(env->ExceptionCheck());
318 return nullptr;
319 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700320
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800321 // Push all class descriptors into a set. Use set instead of unordered_set as we want to
322 // retrieve all in the end.
323 std::set<const char*, CharPointerComparator> descriptors;
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700324 for (auto& dex_file : dex_files) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800325 for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
326 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
327 const char* descriptor = dex_file->GetClassDescriptor(class_def);
328 descriptors.insert(descriptor);
Andreas Gampe833a4852014-05-21 18:46:59 -0700329 }
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800330 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700331
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800332 // Now create output array and copy the set into it.
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700333 jobjectArray result = env->NewObjectArray(descriptors.size(),
334 WellKnownClasses::java_lang_String,
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800335 nullptr);
336 if (result != nullptr) {
337 auto it = descriptors.begin();
338 auto it_end = descriptors.end();
339 jsize i = 0;
340 for (; it != it_end; it++, ++i) {
341 std::string descriptor(DescriptorToDot(*it));
342 ScopedLocalRef<jstring> jdescriptor(env, env->NewStringUTF(descriptor.c_str()));
343 if (jdescriptor.get() == nullptr) {
344 return nullptr;
Ian Rogersdd157d72014-05-15 14:47:50 -0700345 }
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800346 env->SetObjectArrayElement(result, i, jdescriptor.get());
Ian Rogersdd157d72014-05-15 14:47:50 -0700347 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700348 }
Ian Rogersdd157d72014-05-15 14:47:50 -0700349 return result;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700350}
351
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700352static jint GetDexOptNeeded(JNIEnv* env,
353 const char* filename,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700354 const char* instruction_set,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000355 const char* compiler_filter_name,
356 bool profile_changed) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100357 if ((filename == nullptr) || !OS::FileExists(filename)) {
Richard Uhler95abd042015-03-24 09:51:28 -0700358 LOG(ERROR) << "DexFile_getDexOptNeeded file '" << filename << "' does not exist";
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700359 ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException"));
Narayan Kamath11d9f062014-04-23 20:24:57 +0100360 const char* message = (filename == nullptr) ? "<empty file name>" : filename;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700361 env->ThrowNew(fnfe.get(), message);
Calin Juravleb077e152016-02-18 18:47:37 +0000362 return -1;
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700363 }
364
Alex Light6e183f22014-07-18 14:57:04 -0700365 const InstructionSet target_instruction_set = GetInstructionSetFromString(instruction_set);
Andreas Gampe20c89302014-08-19 17:28:06 -0700366 if (target_instruction_set == kNone) {
367 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
368 std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set));
369 env->ThrowNew(iae.get(), message.c_str());
Calin Juravleb077e152016-02-18 18:47:37 +0000370 return -1;
Andreas Gampe20c89302014-08-19 17:28:06 -0700371 }
Alex Light6e183f22014-07-18 14:57:04 -0700372
Andreas Gampe29d38e72016-03-23 15:31:51 +0000373 CompilerFilter::Filter filter;
374 if (!CompilerFilter::ParseCompilerFilter(compiler_filter_name, &filter)) {
375 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
376 std::string message(StringPrintf("Compiler filter %s is invalid.", compiler_filter_name));
377 env->ThrowNew(iae.get(), message.c_str());
378 return -1;
379 }
380
Richard Uhler66d874d2015-01-15 09:37:19 -0800381 // TODO: Verify the dex location is well formed, and throw an IOException if
382 // not?
Andreas Gampe29d38e72016-03-23 15:31:51 +0000383
384 OatFileAssistant oat_file_assistant(filename, target_instruction_set, profile_changed, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800385
386 // Always treat elements of the bootclasspath as up-to-date.
387 if (oat_file_assistant.IsInBootClassPath()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700388 return OatFileAssistant::kNoDexOptNeeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800389 }
Andreas Gampe29d38e72016-03-23 15:31:51 +0000390 return oat_file_assistant.GetDexOptNeeded(filter);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700391}
392
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100393static jstring DexFile_getDexFileStatus(JNIEnv* env,
394 jclass,
395 jstring javaFilename,
396 jstring javaInstructionSet) {
397 ScopedUtfChars filename(env, javaFilename);
398 if (env->ExceptionCheck()) {
399 return nullptr;
400 }
401
402 ScopedUtfChars instruction_set(env, javaInstructionSet);
403 if (env->ExceptionCheck()) {
404 return nullptr;
405 }
406
407 const InstructionSet target_instruction_set = GetInstructionSetFromString(
408 instruction_set.c_str());
409 if (target_instruction_set == kNone) {
410 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
411 std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set.c_str()));
412 env->ThrowNew(iae.get(), message.c_str());
413 return nullptr;
414 }
415
416 OatFileAssistant oat_file_assistant(filename.c_str(), target_instruction_set,
417 false /* profile_changed */,
418 false /* load_executable */);
419
420 std::ostringstream status;
421 bool oat_file_exists = false;
422 bool odex_file_exists = false;
423 if (oat_file_assistant.OatFileExists()) {
424 oat_file_exists = true;
425 status << *oat_file_assistant.OatFileName() << " [compilation_filter=";
426 status << CompilerFilter::NameOfFilter(oat_file_assistant.OatFileCompilerFilter());
427 status << ", status=" << oat_file_assistant.OatFileStatus();
428 }
429
430 if (oat_file_assistant.OdexFileExists()) {
431 odex_file_exists = true;
432 if (oat_file_exists) {
433 status << "] ";
434 }
435 status << *oat_file_assistant.OdexFileName() << " [compilation_filter=";
436 status << CompilerFilter::NameOfFilter(oat_file_assistant.OdexFileCompilerFilter());
437 status << ", status=" << oat_file_assistant.OdexFileStatus();
438 }
439
440 if (!oat_file_exists && !odex_file_exists) {
441 status << "invalid[";
442 }
443
444 status << "]";
445 return env->NewStringUTF(status.str().c_str());
446}
447
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700448static jint DexFile_getDexOptNeeded(JNIEnv* env,
449 jclass,
450 jstring javaFilename,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700451 jstring javaInstructionSet,
Andreas Gampec38be812016-03-23 15:03:46 -0700452 jstring javaTargetCompilerFilter,
453 jboolean newProfile) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100454 ScopedUtfChars filename(env, javaFilename);
Andreas Gampe20c89302014-08-19 17:28:06 -0700455 if (env->ExceptionCheck()) {
Calin Juravleb077e152016-02-18 18:47:37 +0000456 return -1;
Andreas Gampe20c89302014-08-19 17:28:06 -0700457 }
458
Narayan Kamath11d9f062014-04-23 20:24:57 +0100459 ScopedUtfChars instruction_set(env, javaInstructionSet);
Andreas Gampe20c89302014-08-19 17:28:06 -0700460 if (env->ExceptionCheck()) {
Calin Juravleb077e152016-02-18 18:47:37 +0000461 return -1;
Andreas Gampe20c89302014-08-19 17:28:06 -0700462 }
Narayan Kamath11d9f062014-04-23 20:24:57 +0100463
Andreas Gampec38be812016-03-23 15:03:46 -0700464 ScopedUtfChars target_compiler_filter(env, javaTargetCompilerFilter);
465 if (env->ExceptionCheck()) {
466 return -1;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000467 }
Andreas Gampec38be812016-03-23 15:03:46 -0700468
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700469 return GetDexOptNeeded(env,
470 filename.c_str(),
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700471 instruction_set.c_str(),
Andreas Gampec38be812016-03-23 15:03:46 -0700472 target_compiler_filter.c_str(),
473 newProfile == JNI_TRUE);
Narayan Kamath11d9f062014-04-23 20:24:57 +0100474}
475
Calin Juravleb077e152016-02-18 18:47:37 +0000476// public API
Narayan Kamath11d9f062014-04-23 20:24:57 +0100477static jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
Richard Uhler06e3f4f2016-05-24 15:42:37 -0700478 ScopedUtfChars filename_utf(env, javaFilename);
479 if (env->ExceptionCheck()) {
480 return JNI_FALSE;
481 }
482
483 const char* filename = filename_utf.c_str();
484 if ((filename == nullptr) || !OS::FileExists(filename)) {
485 LOG(ERROR) << "DexFile_isDexOptNeeded file '" << filename << "' does not exist";
486 ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException"));
487 const char* message = (filename == nullptr) ? "<empty file name>" : filename;
488 env->ThrowNew(fnfe.get(), message);
489 return JNI_FALSE;
490 }
491
492 OatFileAssistant oat_file_assistant(filename, kRuntimeISA, false, false);
493 return oat_file_assistant.IsUpToDate() ? JNI_FALSE : JNI_TRUE;
Dave Allison39c3bfb2014-01-28 18:33:52 -0800494}
495
Andreas Gampec38be812016-03-23 15:03:46 -0700496static jboolean DexFile_isValidCompilerFilter(JNIEnv* env,
497 jclass javeDexFileClass ATTRIBUTE_UNUSED,
498 jstring javaCompilerFilter) {
499 ScopedUtfChars compiler_filter(env, javaCompilerFilter);
500 if (env->ExceptionCheck()) {
501 return -1;
502 }
503
504 CompilerFilter::Filter filter;
505 return CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)
506 ? JNI_TRUE : JNI_FALSE;
507}
508
509static jboolean DexFile_isProfileGuidedCompilerFilter(JNIEnv* env,
510 jclass javeDexFileClass ATTRIBUTE_UNUSED,
511 jstring javaCompilerFilter) {
512 ScopedUtfChars compiler_filter(env, javaCompilerFilter);
513 if (env->ExceptionCheck()) {
514 return -1;
515 }
516
517 CompilerFilter::Filter filter;
518 if (!CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)) {
519 return JNI_FALSE;
520 }
521 return CompilerFilter::DependsOnProfile(filter) ? JNI_TRUE : JNI_FALSE;
522}
523
Andreas Gampe86a785d2016-03-30 17:19:48 -0700524static jstring DexFile_getNonProfileGuidedCompilerFilter(JNIEnv* env,
525 jclass javeDexFileClass ATTRIBUTE_UNUSED,
526 jstring javaCompilerFilter) {
527 ScopedUtfChars compiler_filter(env, javaCompilerFilter);
528 if (env->ExceptionCheck()) {
529 return nullptr;
530 }
531
532 CompilerFilter::Filter filter;
533 if (!CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)) {
534 return javaCompilerFilter;
535 }
536
537 CompilerFilter::Filter new_filter = CompilerFilter::GetNonProfileDependentFilterFrom(filter);
538
539 // Filter stayed the same, return input.
540 if (filter == new_filter) {
541 return javaCompilerFilter;
542 }
543
544 // Create a new string object and return.
545 std::string new_filter_str = CompilerFilter::NameOfFilter(new_filter);
546 return env->NewStringUTF(new_filter_str.c_str());
547}
548
Jeff Hao90671be2016-04-22 14:00:06 -0700549static jboolean DexFile_isBackedByOatFile(JNIEnv* env, jclass, jobject cookie) {
550 const OatFile* oat_file = nullptr;
551 std::vector<const DexFile*> dex_files;
552 if (!ConvertJavaArrayToDexFiles(env, cookie, /*out */ dex_files, /* out */ oat_file)) {
553 DCHECK(env->ExceptionCheck());
554 return false;
555 }
556 return oat_file != nullptr;
557}
558
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700559static JNINativeMethod gMethods[] = {
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700560 NATIVE_METHOD(DexFile, closeDexFile, "(Ljava/lang/Object;)Z"),
Mathieu Chartier00310e02015-10-17 12:46:42 -0700561 NATIVE_METHOD(DexFile,
562 defineClassNative,
563 "(Ljava/lang/String;"
564 "Ljava/lang/ClassLoader;"
565 "Ljava/lang/Object;"
566 "Ldalvik/system/DexFile;"
567 ")Ljava/lang/Class;"),
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800568 NATIVE_METHOD(DexFile, getClassNameList, "(Ljava/lang/Object;)[Ljava/lang/String;"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700569 NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700570 NATIVE_METHOD(DexFile, getDexOptNeeded,
Andreas Gampec38be812016-03-23 15:03:46 -0700571 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Z)I"),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700572 NATIVE_METHOD(DexFile, openDexFileNative,
Mathieu Chartier689a7002015-11-20 10:29:42 -0800573 "(Ljava/lang/String;"
574 "Ljava/lang/String;"
575 "I"
576 "Ljava/lang/ClassLoader;"
577 "[Ldalvik/system/DexPathList$Element;"
578 ")Ljava/lang/Object;"),
Andreas Gampec38be812016-03-23 15:03:46 -0700579 NATIVE_METHOD(DexFile, isValidCompilerFilter, "(Ljava/lang/String;)Z"),
580 NATIVE_METHOD(DexFile, isProfileGuidedCompilerFilter, "(Ljava/lang/String;)Z"),
Andreas Gampe86a785d2016-03-30 17:19:48 -0700581 NATIVE_METHOD(DexFile,
582 getNonProfileGuidedCompilerFilter,
583 "(Ljava/lang/String;)Ljava/lang/String;"),
Jeff Hao90671be2016-04-22 14:00:06 -0700584 NATIVE_METHOD(DexFile, isBackedByOatFile, "(Ljava/lang/Object;)Z"),
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100585 NATIVE_METHOD(DexFile, getDexFileStatus,
586 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;")
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700587};
588
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700589void register_dalvik_system_DexFile(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700590 REGISTER_NATIVE_METHODS("dalvik/system/DexFile");
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700591}
592
593} // namespace art