blob: 6643ac223183f443e27ba441c4bf84b7d5d2ea2c [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"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070024#include "dex_file-inl.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070025#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "mirror/class_loader.h"
Ian Rogers05f30572013-02-20 12:13:11 -080027#include "mirror/object-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "mirror/string.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080029#include "oat_file_assistant.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070030#include "oat_file_manager.h"
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -070031#include "os.h"
Calin Juravle9dae5b42014-04-07 16:36:21 +030032#include "profiler.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070033#include "runtime.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070034#include "scoped_thread_state_change.h"
Ian Rogersc9818482012-01-11 08:52:51 -080035#include "ScopedLocalRef.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070036#include "ScopedUtfChars.h"
Calin Juravlebb0b53f2014-05-23 17:33:29 +010037#include "utils.h"
Ian Rogersdd157d72014-05-15 14:47:50 -070038#include "well_known_classes.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070039#include "zip_archive.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070040
41namespace art {
42
Mathieu Chartiere58991b2015-10-13 07:59:34 -070043static bool ConvertJavaArrayToDexFiles(
44 JNIEnv* env,
45 jobject arrayObject,
46 /*out*/ std::vector<const DexFile*>& dex_files,
47 /*out*/ const OatFile*& oat_file) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -080048 jarray array = reinterpret_cast<jarray>(arrayObject);
49
50 jsize array_size = env->GetArrayLength(array);
51 if (env->ExceptionCheck() == JNI_TRUE) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -070052 return false;
Andreas Gampe324b9bb2015-02-23 16:33:22 -080053 }
54
55 // TODO: Optimize. On 32bit we can use an int array.
56 jboolean is_long_data_copied;
57 jlong* long_data = env->GetLongArrayElements(reinterpret_cast<jlongArray>(array),
58 &is_long_data_copied);
59 if (env->ExceptionCheck() == JNI_TRUE) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -070060 return false;
Andreas Gampe324b9bb2015-02-23 16:33:22 -080061 }
62
Mathieu Chartiere58991b2015-10-13 07:59:34 -070063 oat_file = reinterpret_cast<const OatFile*>(static_cast<uintptr_t>(long_data[kOatFileIndex]));
64 dex_files.reserve(array_size - 1);
65 for (jsize i = kDexFileIndexStart; i < array_size; ++i) {
66 dex_files.push_back(reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(long_data[i])));
Andreas Gampe324b9bb2015-02-23 16:33:22 -080067 }
68
69 env->ReleaseLongArrayElements(reinterpret_cast<jlongArray>(array), long_data, JNI_ABORT);
Mathieu Chartiere58991b2015-10-13 07:59:34 -070070 return env->ExceptionCheck() != JNI_TRUE;
Andreas Gampe324b9bb2015-02-23 16:33:22 -080071}
72
Mathieu Chartiere58991b2015-10-13 07:59:34 -070073static jlongArray ConvertDexFilesToJavaArray(JNIEnv* env,
74 const OatFile* oat_file,
75 std::vector<std::unique_ptr<const DexFile>>& vec) {
76 // Add one for the oat file.
Mathieu Chartier80b37b72015-10-12 18:13:39 -070077 jlongArray long_array = env->NewLongArray(static_cast<jsize>(kDexFileIndexStart + vec.size()));
Andreas Gampe324b9bb2015-02-23 16:33:22 -080078 if (env->ExceptionCheck() == JNI_TRUE) {
79 return nullptr;
80 }
81
82 jboolean is_long_data_copied;
83 jlong* long_data = env->GetLongArrayElements(long_array, &is_long_data_copied);
84 if (env->ExceptionCheck() == JNI_TRUE) {
85 return nullptr;
86 }
87
Mathieu Chartiere58991b2015-10-13 07:59:34 -070088 long_data[kOatFileIndex] = reinterpret_cast<uintptr_t>(oat_file);
89 for (size_t i = 0; i < vec.size(); ++i) {
90 long_data[kDexFileIndexStart + i] = reinterpret_cast<uintptr_t>(vec[i].get());
Andreas Gampe324b9bb2015-02-23 16:33:22 -080091 }
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 Carlstromf91c8c32011-09-21 17:30:34 -0700106// 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 Carlstromc252c3e2011-10-16 23:21:02 -0700111// if (env->ExceptionCheck()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700112// return null;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700113// }
114// // ... use name.c_str()
115//
116// TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option.
117class NullableScopedUtfChars {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800118 public:
119 NullableScopedUtfChars(JNIEnv* env, jstring s) : mEnv(env), mString(s) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700120 mUtfChars = (s != nullptr) ? env->GetStringUTFChars(s, nullptr) : nullptr;
Elliott Hughesba8eee12012-01-24 20:25:24 -0800121 }
122
123 ~NullableScopedUtfChars() {
124 if (mUtfChars) {
125 mEnv->ReleaseStringUTFChars(mString, mUtfChars);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700126 }
Elliott Hughesba8eee12012-01-24 20:25:24 -0800127 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700128
Elliott Hughesba8eee12012-01-24 20:25:24 -0800129 const char* c_str() const {
130 return mUtfChars;
131 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700132
Elliott Hughesba8eee12012-01-24 20:25:24 -0800133 size_t size() const {
134 return strlen(mUtfChars);
135 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700136
Elliott Hughesba8eee12012-01-24 20:25:24 -0800137 // Element access.
138 const char& operator[](size_t n) const {
139 return mUtfChars[n];
140 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700141
Elliott Hughesba8eee12012-01-24 20:25:24 -0800142 private:
143 JNIEnv* mEnv;
144 jstring mString;
145 const char* mUtfChars;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700146
Elliott Hughesba8eee12012-01-24 20:25:24 -0800147 // Disallow copy and assignment.
148 NullableScopedUtfChars(const NullableScopedUtfChars&);
149 void operator=(const NullableScopedUtfChars&);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700150};
151
Mathieu Chartierb190d942015-11-12 10:00:58 -0800152static jobject DexFile_openDexFileNative(JNIEnv* env,
153 jclass,
154 jstring javaSourceName,
155 jstring javaOutputName,
156 jint flags ATTRIBUTE_UNUSED,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800157 jobject class_loader,
158 jobjectArray dex_elements) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700159 ScopedUtfChars sourceName(env, javaSourceName);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700160 if (sourceName.c_str() == nullptr) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700161 return 0;
162 }
163 NullableScopedUtfChars outputName(env, javaOutputName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -0700164 if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700165 return 0;
166 }
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700167 Runtime* const runtime = Runtime::Current();
168 ClassLinker* linker = runtime->GetClassLinker();
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800169 std::vector<std::unique_ptr<const DexFile>> dex_files;
Andreas Gampe833a4852014-05-21 18:46:59 -0700170 std::vector<std::string> error_msgs;
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700171 const OatFile* oat_file = nullptr;
Andreas Gampe833a4852014-05-21 18:46:59 -0700172
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700173 dex_files = runtime->GetOatFileManager().OpenDexFilesFromOat(sourceName.c_str(),
174 outputName.c_str(),
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800175 class_loader,
176 dex_elements,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700177 /*out*/ &oat_file,
178 /*out*/ &error_msgs);
Andreas Gampe833a4852014-05-21 18:46:59 -0700179
Richard Uhler66d874d2015-01-15 09:37:19 -0800180 if (!dex_files.empty()) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700181 jlongArray array = ConvertDexFilesToJavaArray(env, oat_file, dex_files);
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800182 if (array == nullptr) {
183 ScopedObjectAccess soa(env);
184 for (auto& dex_file : dex_files) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700185 if (linker->FindDexCache(soa.Self(), *dex_file, true) != nullptr) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800186 dex_file.release();
187 }
188 }
189 }
190 return array;
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700191 } else {
Vladimir Marko60836d52014-01-16 15:53:38 +0000192 ScopedObjectAccess soa(env);
Andreas Gampe329d1882014-04-08 10:32:19 -0700193 CHECK(!error_msgs.empty());
194 // The most important message is at the end. So set up nesting by going forward, which will
195 // wrap the existing exception as a cause for the following one.
196 auto it = error_msgs.begin();
197 auto itEnd = error_msgs.end();
198 for ( ; it != itEnd; ++it) {
199 ThrowWrappedIOException("%s", it->c_str());
200 }
201
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800202 return nullptr;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700203 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700204}
205
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700206static jboolean DexFile_closeDexFile(JNIEnv* env, jclass, jobject cookie) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700207 std::vector<const DexFile*> dex_files;
208 const OatFile* oat_file;
209 if (!ConvertJavaArrayToDexFiles(env, cookie, dex_files, oat_file)) {
210 Thread::Current()->AssertPendingException();
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700211 return JNI_FALSE;
212 }
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700213 Runtime* const runtime = Runtime::Current();
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700214 bool all_deleted = true;
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700215 {
216 ScopedObjectAccess soa(env);
217 mirror::Object* dex_files_object = soa.Decode<mirror::Object*>(cookie);
218 mirror::LongArray* long_dex_files = dex_files_object->AsLongArray();
219 // Delete dex files associated with this dalvik.system.DexFile since there should not be running
220 // code using it. dex_files is a vector due to multidex.
221 ClassLinker* const class_linker = runtime->GetClassLinker();
222 int32_t i = kDexFileIndexStart; // Oat file is at index 0.
223 for (const DexFile* dex_file : dex_files) {
224 if (dex_file != nullptr) {
225 // Only delete the dex file if the dex cache is not found to prevent runtime crashes if there
226 // are calls to DexFile.close while the ART DexFile is still in use.
227 if (class_linker->FindDexCache(soa.Self(), *dex_file, true) == nullptr) {
228 // Clear the element in the array so that we can call close again.
229 long_dex_files->Set(i, 0);
230 delete dex_file;
231 } else {
232 all_deleted = false;
233 }
234 }
235 ++i;
Andreas Gampe833a4852014-05-21 18:46:59 -0700236 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700237 }
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700238
Mathieu Chartierfdccbd42015-10-14 10:58:41 -0700239 // oat_file can be null if we are running without dex2oat.
240 if (all_deleted && oat_file != nullptr) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700241 // If all of the dex files are no longer in use we can unmap the corresponding oat file.
242 VLOG(class_linker) << "Unregistering " << oat_file;
243 runtime->GetOatFileManager().UnRegisterAndDeleteOatFile(oat_file);
244 }
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700245 return all_deleted ? JNI_TRUE : JNI_FALSE;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700246}
247
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700248static jclass DexFile_defineClassNative(JNIEnv* env,
249 jclass,
250 jstring javaName,
251 jobject javaLoader,
Mathieu Chartier00310e02015-10-17 12:46:42 -0700252 jobject cookie,
253 jobject dexFile) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700254 std::vector<const DexFile*> dex_files;
255 const OatFile* oat_file;
256 if (!ConvertJavaArrayToDexFiles(env, cookie, /*out*/ dex_files, /*out*/ oat_file)) {
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700257 VLOG(class_linker) << "Failed to find dex_file";
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800258 DCHECK(env->ExceptionCheck());
259 return nullptr;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700260 }
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800261
Brian Carlstromdf143242011-10-10 18:05:34 -0700262 ScopedUtfChars class_name(env, javaName);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700263 if (class_name.c_str() == nullptr) {
Brian Carlstrom2e450bf2013-09-06 15:39:46 -0700264 VLOG(class_linker) << "Failed to find class_name";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700265 return nullptr;
Brian Carlstromdf143242011-10-10 18:05:34 -0700266 }
Elliott Hughes95572412011-12-13 18:14:20 -0800267 const std::string descriptor(DotToDescriptor(class_name.c_str()));
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800268 const size_t hash(ComputeModifiedUtf8Hash(descriptor.c_str()));
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700269 for (auto& dex_file : dex_files) {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800270 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor.c_str(), hash);
Andreas Gampe833a4852014-05-21 18:46:59 -0700271 if (dex_class_def != nullptr) {
272 ScopedObjectAccess soa(env);
273 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Andreas Gampe833a4852014-05-21 18:46:59 -0700274 StackHandleScope<1> hs(soa.Self());
275 Handle<mirror::ClassLoader> class_loader(
276 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(javaLoader)));
Mathieu Chartierd57d4542015-10-14 10:55:30 -0700277 class_linker->RegisterDexFile(
278 *dex_file,
279 class_linker->GetOrCreateAllocatorForClassLoader(class_loader.Get()));
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700280 mirror::Class* result = class_linker->DefineClass(soa.Self(),
281 descriptor.c_str(),
282 hash,
283 class_loader,
284 *dex_file,
285 *dex_class_def);
Mathieu Chartier00310e02015-10-17 12:46:42 -0700286 // Add the used dex file. This only required for the DexFile.loadClass API since normal
287 // class loaders already keep their dex files live.
288 class_linker->InsertDexFileInToClassLoader(soa.Decode<mirror::Object*>(dexFile),
289 class_loader.Get());
Andreas Gampe833a4852014-05-21 18:46:59 -0700290 if (result != nullptr) {
Brian Carlstrom667ab7c2014-10-16 19:12:28 -0700291 VLOG(class_linker) << "DexFile_defineClassNative returning " << result
292 << " for " << class_name.c_str();
Andreas Gampe833a4852014-05-21 18:46:59 -0700293 return soa.AddLocalReference<jclass>(result);
294 }
295 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700296 }
Brian Carlstrom667ab7c2014-10-16 19:12:28 -0700297 VLOG(class_linker) << "Failed to find dex_class_def " << class_name.c_str();
Andreas Gampe833a4852014-05-21 18:46:59 -0700298 return nullptr;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700299}
300
Andreas Gampe833a4852014-05-21 18:46:59 -0700301// Needed as a compare functor for sets of const char
302struct CharPointerComparator {
303 bool operator()(const char *str1, const char *str2) const {
304 return strcmp(str1, str2) < 0;
305 }
306};
307
308// Note: this can be an expensive call, as we sort out duplicates in MultiDex files.
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800309static jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jobject cookie) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700310 const OatFile* oat_file = nullptr;
311 std::vector<const DexFile*> dex_files;
312 if (!ConvertJavaArrayToDexFiles(env, cookie, /*out */ dex_files, /* out */ oat_file)) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800313 DCHECK(env->ExceptionCheck());
314 return nullptr;
315 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700316
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800317 // Push all class descriptors into a set. Use set instead of unordered_set as we want to
318 // retrieve all in the end.
319 std::set<const char*, CharPointerComparator> descriptors;
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700320 for (auto& dex_file : dex_files) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800321 for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
322 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
323 const char* descriptor = dex_file->GetClassDescriptor(class_def);
324 descriptors.insert(descriptor);
Andreas Gampe833a4852014-05-21 18:46:59 -0700325 }
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800326 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700327
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800328 // Now create output array and copy the set into it.
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700329 jobjectArray result = env->NewObjectArray(descriptors.size(),
330 WellKnownClasses::java_lang_String,
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800331 nullptr);
332 if (result != nullptr) {
333 auto it = descriptors.begin();
334 auto it_end = descriptors.end();
335 jsize i = 0;
336 for (; it != it_end; it++, ++i) {
337 std::string descriptor(DescriptorToDot(*it));
338 ScopedLocalRef<jstring> jdescriptor(env, env->NewStringUTF(descriptor.c_str()));
339 if (jdescriptor.get() == nullptr) {
340 return nullptr;
Ian Rogersdd157d72014-05-15 14:47:50 -0700341 }
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800342 env->SetObjectArrayElement(result, i, jdescriptor.get());
Ian Rogersdd157d72014-05-15 14:47:50 -0700343 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700344 }
Ian Rogersdd157d72014-05-15 14:47:50 -0700345 return result;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700346}
347
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700348static jint GetDexOptNeeded(JNIEnv* env,
349 const char* filename,
350 const char* pkgname,
351 const char* instruction_set,
352 const jboolean defer) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100353 if ((filename == nullptr) || !OS::FileExists(filename)) {
Richard Uhler95abd042015-03-24 09:51:28 -0700354 LOG(ERROR) << "DexFile_getDexOptNeeded file '" << filename << "' does not exist";
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700355 ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException"));
Narayan Kamath11d9f062014-04-23 20:24:57 +0100356 const char* message = (filename == nullptr) ? "<empty file name>" : filename;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700357 env->ThrowNew(fnfe.get(), message);
Richard Uhler95abd042015-03-24 09:51:28 -0700358 return OatFileAssistant::kNoDexOptNeeded;
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700359 }
360
Alex Light6e183f22014-07-18 14:57:04 -0700361 const InstructionSet target_instruction_set = GetInstructionSetFromString(instruction_set);
Andreas Gampe20c89302014-08-19 17:28:06 -0700362 if (target_instruction_set == kNone) {
363 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
364 std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set));
365 env->ThrowNew(iae.get(), message.c_str());
366 return 0;
367 }
Alex Light6e183f22014-07-18 14:57:04 -0700368
Richard Uhler66d874d2015-01-15 09:37:19 -0800369 // TODO: Verify the dex location is well formed, and throw an IOException if
370 // not?
371
372 OatFileAssistant oat_file_assistant(filename, target_instruction_set, false, pkgname);
373
374 // Always treat elements of the bootclasspath as up-to-date.
375 if (oat_file_assistant.IsInBootClassPath()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700376 return OatFileAssistant::kNoDexOptNeeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800377 }
378
379 // TODO: Checking the profile should probably be done in the GetStatus()
380 // function. We have it here because GetStatus() should not be copying
381 // profile files. But who should be copying profile files?
382 if (oat_file_assistant.OdexFileIsOutOfDate()) {
383 // Needs recompile if profile has changed significantly.
384 if (Runtime::Current()->GetProfilerOptions().IsEnabled()) {
385 if (oat_file_assistant.IsProfileChangeSignificant()) {
386 if (!defer) {
387 oat_file_assistant.CopyProfileFile();
388 }
Richard Uhler95abd042015-03-24 09:51:28 -0700389 return OatFileAssistant::kDex2OatNeeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800390 } else if (oat_file_assistant.ProfileExists()
391 && !oat_file_assistant.OldProfileExists()) {
392 if (!defer) {
393 oat_file_assistant.CopyProfileFile();
394 }
395 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700396 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800397 }
398
Richard Uhler95abd042015-03-24 09:51:28 -0700399 return oat_file_assistant.GetDexOptNeeded();
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700400}
401
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700402static jint DexFile_getDexOptNeeded(JNIEnv* env,
403 jclass,
404 jstring javaFilename,
405 jstring javaPkgname,
406 jstring javaInstructionSet,
407 jboolean defer) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100408 ScopedUtfChars filename(env, javaFilename);
Andreas Gampe20c89302014-08-19 17:28:06 -0700409 if (env->ExceptionCheck()) {
410 return 0;
411 }
412
Narayan Kamath11d9f062014-04-23 20:24:57 +0100413 NullableScopedUtfChars pkgname(env, javaPkgname);
Andreas Gampe20c89302014-08-19 17:28:06 -0700414
Narayan Kamath11d9f062014-04-23 20:24:57 +0100415 ScopedUtfChars instruction_set(env, javaInstructionSet);
Andreas Gampe20c89302014-08-19 17:28:06 -0700416 if (env->ExceptionCheck()) {
417 return 0;
418 }
Narayan Kamath11d9f062014-04-23 20:24:57 +0100419
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700420 return GetDexOptNeeded(env,
421 filename.c_str(),
422 pkgname.c_str(),
423 instruction_set.c_str(),
424 defer);
Narayan Kamath11d9f062014-04-23 20:24:57 +0100425}
426
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700427// public API, null pkgname
Narayan Kamath11d9f062014-04-23 20:24:57 +0100428static jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
429 const char* instruction_set = GetInstructionSetString(kRuntimeISA);
430 ScopedUtfChars filename(env, javaFilename);
Richard Uhler95abd042015-03-24 09:51:28 -0700431 jint status = GetDexOptNeeded(env, filename.c_str(), nullptr /* pkgname */,
432 instruction_set, false /* defer */);
433 return (status != OatFileAssistant::kNoDexOptNeeded) ? JNI_TRUE : JNI_FALSE;
Dave Allison39c3bfb2014-01-28 18:33:52 -0800434}
435
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700436static JNINativeMethod gMethods[] = {
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700437 NATIVE_METHOD(DexFile, closeDexFile, "(Ljava/lang/Object;)Z"),
Mathieu Chartier00310e02015-10-17 12:46:42 -0700438 NATIVE_METHOD(DexFile,
439 defineClassNative,
440 "(Ljava/lang/String;"
441 "Ljava/lang/ClassLoader;"
442 "Ljava/lang/Object;"
443 "Ldalvik/system/DexFile;"
444 ")Ljava/lang/Class;"),
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800445 NATIVE_METHOD(DexFile, getClassNameList, "(Ljava/lang/Object;)[Ljava/lang/String;"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700446 NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700447 NATIVE_METHOD(DexFile, getDexOptNeeded,
448 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Z)I"),
449 NATIVE_METHOD(DexFile, openDexFileNative,
Mathieu Chartier689a7002015-11-20 10:29:42 -0800450 "(Ljava/lang/String;"
451 "Ljava/lang/String;"
452 "I"
453 "Ljava/lang/ClassLoader;"
454 "[Ldalvik/system/DexPathList$Element;"
455 ")Ljava/lang/Object;"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700456};
457
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700458void register_dalvik_system_DexFile(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700459 REGISTER_NATIVE_METHODS("dalvik/system/DexFile");
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700460}
461
462} // namespace art