blob: cd0e55f261acc3e5d45a8384a80f8b91edbe2ee9 [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
Andreas Gampe46ee31b2016-12-14 10:11:49 -080021#include "android-base/stringprintf.h"
22
Elliott Hughes07ed66b2012-12-12 18:34:25 -080023#include "base/logging.h"
Andreas Gampe833a4852014-05-21 18:46:59 -070024#include "base/stl_util.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070025#include "class_linker.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080026#include "common_throws.h"
Andreas Gampec38be812016-03-23 15:03:46 -070027#include "compiler_filter.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070028#include "dex_file-inl.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070029#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030#include "mirror/class_loader.h"
Ian Rogers05f30572013-02-20 12:13:11 -080031#include "mirror/object-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032#include "mirror/string.h"
Narayan Kamath8943c1d2016-05-02 13:14:48 +010033#include "oat_file.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080034#include "oat_file_assistant.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070035#include "oat_file_manager.h"
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -070036#include "os.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070037#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070038#include "scoped_thread_state_change-inl.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
Andreas Gampe46ee31b2016-12-14 10:11:49 -080047using android::base::StringPrintf;
48
Mathieu Chartiere58991b2015-10-13 07:59:34 -070049static bool ConvertJavaArrayToDexFiles(
50 JNIEnv* env,
51 jobject arrayObject,
52 /*out*/ std::vector<const DexFile*>& dex_files,
53 /*out*/ const OatFile*& oat_file) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -080054 jarray array = reinterpret_cast<jarray>(arrayObject);
55
56 jsize array_size = env->GetArrayLength(array);
57 if (env->ExceptionCheck() == JNI_TRUE) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -070058 return false;
Andreas Gampe324b9bb2015-02-23 16:33:22 -080059 }
60
61 // TODO: Optimize. On 32bit we can use an int array.
62 jboolean is_long_data_copied;
63 jlong* long_data = env->GetLongArrayElements(reinterpret_cast<jlongArray>(array),
64 &is_long_data_copied);
65 if (env->ExceptionCheck() == JNI_TRUE) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -070066 return false;
Andreas Gampe324b9bb2015-02-23 16:33:22 -080067 }
68
Mathieu Chartiere58991b2015-10-13 07:59:34 -070069 oat_file = reinterpret_cast<const OatFile*>(static_cast<uintptr_t>(long_data[kOatFileIndex]));
70 dex_files.reserve(array_size - 1);
71 for (jsize i = kDexFileIndexStart; i < array_size; ++i) {
72 dex_files.push_back(reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(long_data[i])));
Andreas Gampe324b9bb2015-02-23 16:33:22 -080073 }
74
75 env->ReleaseLongArrayElements(reinterpret_cast<jlongArray>(array), long_data, JNI_ABORT);
Mathieu Chartiere58991b2015-10-13 07:59:34 -070076 return env->ExceptionCheck() != JNI_TRUE;
Andreas Gampe324b9bb2015-02-23 16:33:22 -080077}
78
Mathieu Chartiere58991b2015-10-13 07:59:34 -070079static jlongArray ConvertDexFilesToJavaArray(JNIEnv* env,
80 const OatFile* oat_file,
81 std::vector<std::unique_ptr<const DexFile>>& vec) {
82 // Add one for the oat file.
Mathieu Chartier80b37b72015-10-12 18:13:39 -070083 jlongArray long_array = env->NewLongArray(static_cast<jsize>(kDexFileIndexStart + vec.size()));
Andreas Gampe324b9bb2015-02-23 16:33:22 -080084 if (env->ExceptionCheck() == JNI_TRUE) {
85 return nullptr;
86 }
87
88 jboolean is_long_data_copied;
89 jlong* long_data = env->GetLongArrayElements(long_array, &is_long_data_copied);
90 if (env->ExceptionCheck() == JNI_TRUE) {
91 return nullptr;
92 }
93
Mathieu Chartiere58991b2015-10-13 07:59:34 -070094 long_data[kOatFileIndex] = reinterpret_cast<uintptr_t>(oat_file);
95 for (size_t i = 0; i < vec.size(); ++i) {
96 long_data[kDexFileIndexStart + i] = reinterpret_cast<uintptr_t>(vec[i].get());
Andreas Gampe324b9bb2015-02-23 16:33:22 -080097 }
98
99 env->ReleaseLongArrayElements(long_array, long_data, 0);
100 if (env->ExceptionCheck() == JNI_TRUE) {
101 return nullptr;
102 }
103
104 // Now release all the unique_ptrs.
105 for (auto& dex_file : vec) {
106 dex_file.release();
107 }
108
109 return long_array;
110}
111
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700112// A smart pointer that provides read-only access to a Java string's UTF chars.
113// Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if
114// passed a null jstring. The correct idiom is:
115//
116// NullableScopedUtfChars name(env, javaName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -0700117// if (env->ExceptionCheck()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700118// return null;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700119// }
120// // ... use name.c_str()
121//
122// TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option.
123class NullableScopedUtfChars {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800124 public:
125 NullableScopedUtfChars(JNIEnv* env, jstring s) : mEnv(env), mString(s) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700126 mUtfChars = (s != nullptr) ? env->GetStringUTFChars(s, nullptr) : nullptr;
Elliott Hughesba8eee12012-01-24 20:25:24 -0800127 }
128
129 ~NullableScopedUtfChars() {
130 if (mUtfChars) {
131 mEnv->ReleaseStringUTFChars(mString, mUtfChars);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700132 }
Elliott Hughesba8eee12012-01-24 20:25:24 -0800133 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700134
Elliott Hughesba8eee12012-01-24 20:25:24 -0800135 const char* c_str() const {
136 return mUtfChars;
137 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700138
Elliott Hughesba8eee12012-01-24 20:25:24 -0800139 size_t size() const {
140 return strlen(mUtfChars);
141 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700142
Elliott Hughesba8eee12012-01-24 20:25:24 -0800143 // Element access.
144 const char& operator[](size_t n) const {
145 return mUtfChars[n];
146 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700147
Elliott Hughesba8eee12012-01-24 20:25:24 -0800148 private:
149 JNIEnv* mEnv;
150 jstring mString;
151 const char* mUtfChars;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700152
Elliott Hughesba8eee12012-01-24 20:25:24 -0800153 // Disallow copy and assignment.
154 NullableScopedUtfChars(const NullableScopedUtfChars&);
155 void operator=(const NullableScopedUtfChars&);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700156};
157
Mathieu Chartierb190d942015-11-12 10:00:58 -0800158static jobject DexFile_openDexFileNative(JNIEnv* env,
159 jclass,
160 jstring javaSourceName,
161 jstring javaOutputName,
162 jint flags ATTRIBUTE_UNUSED,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800163 jobject class_loader,
164 jobjectArray dex_elements) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700165 ScopedUtfChars sourceName(env, javaSourceName);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700166 if (sourceName.c_str() == nullptr) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700167 return 0;
168 }
169 NullableScopedUtfChars outputName(env, javaOutputName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -0700170 if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700171 return 0;
172 }
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700173 Runtime* const runtime = Runtime::Current();
174 ClassLinker* linker = runtime->GetClassLinker();
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800175 std::vector<std::unique_ptr<const DexFile>> dex_files;
Andreas Gampe833a4852014-05-21 18:46:59 -0700176 std::vector<std::string> error_msgs;
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700177 const OatFile* oat_file = nullptr;
Andreas Gampe833a4852014-05-21 18:46:59 -0700178
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700179 dex_files = runtime->GetOatFileManager().OpenDexFilesFromOat(sourceName.c_str(),
180 outputName.c_str(),
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800181 class_loader,
182 dex_elements,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700183 /*out*/ &oat_file,
184 /*out*/ &error_msgs);
Andreas Gampe833a4852014-05-21 18:46:59 -0700185
Richard Uhler66d874d2015-01-15 09:37:19 -0800186 if (!dex_files.empty()) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700187 jlongArray array = ConvertDexFilesToJavaArray(env, oat_file, dex_files);
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800188 if (array == nullptr) {
189 ScopedObjectAccess soa(env);
190 for (auto& dex_file : dex_files) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700191 if (linker->FindDexCache(soa.Self(), *dex_file, true) != nullptr) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800192 dex_file.release();
193 }
194 }
195 }
196 return array;
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700197 } else {
Vladimir Marko60836d52014-01-16 15:53:38 +0000198 ScopedObjectAccess soa(env);
Andreas Gampe329d1882014-04-08 10:32:19 -0700199 CHECK(!error_msgs.empty());
200 // The most important message is at the end. So set up nesting by going forward, which will
201 // wrap the existing exception as a cause for the following one.
202 auto it = error_msgs.begin();
203 auto itEnd = error_msgs.end();
204 for ( ; it != itEnd; ++it) {
205 ThrowWrappedIOException("%s", it->c_str());
206 }
207
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800208 return nullptr;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700209 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700210}
211
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700212static jboolean DexFile_closeDexFile(JNIEnv* env, jclass, jobject cookie) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700213 std::vector<const DexFile*> dex_files;
214 const OatFile* oat_file;
215 if (!ConvertJavaArrayToDexFiles(env, cookie, dex_files, oat_file)) {
216 Thread::Current()->AssertPendingException();
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700217 return JNI_FALSE;
218 }
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700219 Runtime* const runtime = Runtime::Current();
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700220 bool all_deleted = true;
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700221 {
222 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700223 ObjPtr<mirror::Object> dex_files_object = soa.Decode<mirror::Object>(cookie);
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700224 ObjPtr<mirror::LongArray> long_dex_files = dex_files_object->AsLongArray();
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700225 // Delete dex files associated with this dalvik.system.DexFile since there should not be running
226 // code using it. dex_files is a vector due to multidex.
227 ClassLinker* const class_linker = runtime->GetClassLinker();
228 int32_t i = kDexFileIndexStart; // Oat file is at index 0.
229 for (const DexFile* dex_file : dex_files) {
230 if (dex_file != nullptr) {
231 // Only delete the dex file if the dex cache is not found to prevent runtime crashes if there
232 // are calls to DexFile.close while the ART DexFile is still in use.
233 if (class_linker->FindDexCache(soa.Self(), *dex_file, true) == nullptr) {
234 // Clear the element in the array so that we can call close again.
235 long_dex_files->Set(i, 0);
236 delete dex_file;
237 } else {
238 all_deleted = false;
239 }
240 }
241 ++i;
Andreas Gampe833a4852014-05-21 18:46:59 -0700242 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700243 }
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700244
Mathieu Chartierfdccbd42015-10-14 10:58:41 -0700245 // oat_file can be null if we are running without dex2oat.
246 if (all_deleted && oat_file != nullptr) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700247 // If all of the dex files are no longer in use we can unmap the corresponding oat file.
248 VLOG(class_linker) << "Unregistering " << oat_file;
249 runtime->GetOatFileManager().UnRegisterAndDeleteOatFile(oat_file);
250 }
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700251 return all_deleted ? JNI_TRUE : JNI_FALSE;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700252}
253
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700254static jclass DexFile_defineClassNative(JNIEnv* env,
255 jclass,
256 jstring javaName,
257 jobject javaLoader,
Mathieu Chartier00310e02015-10-17 12:46:42 -0700258 jobject cookie,
259 jobject dexFile) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700260 std::vector<const DexFile*> dex_files;
261 const OatFile* oat_file;
262 if (!ConvertJavaArrayToDexFiles(env, cookie, /*out*/ dex_files, /*out*/ oat_file)) {
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700263 VLOG(class_linker) << "Failed to find dex_file";
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800264 DCHECK(env->ExceptionCheck());
265 return nullptr;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700266 }
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800267
Brian Carlstromdf143242011-10-10 18:05:34 -0700268 ScopedUtfChars class_name(env, javaName);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700269 if (class_name.c_str() == nullptr) {
Brian Carlstrom2e450bf2013-09-06 15:39:46 -0700270 VLOG(class_linker) << "Failed to find class_name";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700271 return nullptr;
Brian Carlstromdf143242011-10-10 18:05:34 -0700272 }
Elliott Hughes95572412011-12-13 18:14:20 -0800273 const std::string descriptor(DotToDescriptor(class_name.c_str()));
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800274 const size_t hash(ComputeModifiedUtf8Hash(descriptor.c_str()));
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700275 for (auto& dex_file : dex_files) {
David Sehr9aa352e2016-09-15 18:13:52 -0700276 const DexFile::ClassDef* dex_class_def =
277 OatDexFile::FindClassDef(*dex_file, descriptor.c_str(), hash);
Andreas Gampe833a4852014-05-21 18:46:59 -0700278 if (dex_class_def != nullptr) {
279 ScopedObjectAccess soa(env);
280 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Andreas Gampe833a4852014-05-21 18:46:59 -0700281 StackHandleScope<1> hs(soa.Self());
282 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700283 hs.NewHandle(soa.Decode<mirror::ClassLoader>(javaLoader)));
Mathieu Chartierf284d442016-06-02 11:48:30 -0700284 class_linker->RegisterDexFile(*dex_file, class_loader.Get());
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700285 ObjPtr<mirror::Class> result = class_linker->DefineClass(soa.Self(),
286 descriptor.c_str(),
287 hash,
288 class_loader,
289 *dex_file,
290 *dex_class_def);
Mathieu Chartier00310e02015-10-17 12:46:42 -0700291 // Add the used dex file. This only required for the DexFile.loadClass API since normal
292 // class loaders already keep their dex files live.
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700293 class_linker->InsertDexFileInToClassLoader(soa.Decode<mirror::Object>(dexFile),
Mathieu Chartier00310e02015-10-17 12:46:42 -0700294 class_loader.Get());
Andreas Gampe833a4852014-05-21 18:46:59 -0700295 if (result != nullptr) {
Brian Carlstrom667ab7c2014-10-16 19:12:28 -0700296 VLOG(class_linker) << "DexFile_defineClassNative returning " << result
297 << " for " << class_name.c_str();
Andreas Gampe833a4852014-05-21 18:46:59 -0700298 return soa.AddLocalReference<jclass>(result);
299 }
300 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700301 }
Brian Carlstrom667ab7c2014-10-16 19:12:28 -0700302 VLOG(class_linker) << "Failed to find dex_class_def " << class_name.c_str();
Andreas Gampe833a4852014-05-21 18:46:59 -0700303 return nullptr;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700304}
305
Andreas Gampe833a4852014-05-21 18:46:59 -0700306// Needed as a compare functor for sets of const char
307struct CharPointerComparator {
308 bool operator()(const char *str1, const char *str2) const {
309 return strcmp(str1, str2) < 0;
310 }
311};
312
313// Note: this can be an expensive call, as we sort out duplicates in MultiDex files.
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800314static jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jobject cookie) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700315 const OatFile* oat_file = nullptr;
316 std::vector<const DexFile*> dex_files;
317 if (!ConvertJavaArrayToDexFiles(env, cookie, /*out */ dex_files, /* out */ oat_file)) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800318 DCHECK(env->ExceptionCheck());
319 return nullptr;
320 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700321
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800322 // Push all class descriptors into a set. Use set instead of unordered_set as we want to
323 // retrieve all in the end.
324 std::set<const char*, CharPointerComparator> descriptors;
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700325 for (auto& dex_file : dex_files) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800326 for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
327 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
328 const char* descriptor = dex_file->GetClassDescriptor(class_def);
329 descriptors.insert(descriptor);
Andreas Gampe833a4852014-05-21 18:46:59 -0700330 }
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800331 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700332
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800333 // Now create output array and copy the set into it.
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700334 jobjectArray result = env->NewObjectArray(descriptors.size(),
335 WellKnownClasses::java_lang_String,
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800336 nullptr);
337 if (result != nullptr) {
338 auto it = descriptors.begin();
339 auto it_end = descriptors.end();
340 jsize i = 0;
341 for (; it != it_end; it++, ++i) {
342 std::string descriptor(DescriptorToDot(*it));
343 ScopedLocalRef<jstring> jdescriptor(env, env->NewStringUTF(descriptor.c_str()));
344 if (jdescriptor.get() == nullptr) {
345 return nullptr;
Ian Rogersdd157d72014-05-15 14:47:50 -0700346 }
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800347 env->SetObjectArrayElement(result, i, jdescriptor.get());
Ian Rogersdd157d72014-05-15 14:47:50 -0700348 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700349 }
Ian Rogersdd157d72014-05-15 14:47:50 -0700350 return result;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700351}
352
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700353static jint GetDexOptNeeded(JNIEnv* env,
354 const char* filename,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700355 const char* instruction_set,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000356 const char* compiler_filter_name,
357 bool profile_changed) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100358 if ((filename == nullptr) || !OS::FileExists(filename)) {
Richard Uhler95abd042015-03-24 09:51:28 -0700359 LOG(ERROR) << "DexFile_getDexOptNeeded file '" << filename << "' does not exist";
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700360 ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException"));
Narayan Kamath11d9f062014-04-23 20:24:57 +0100361 const char* message = (filename == nullptr) ? "<empty file name>" : filename;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700362 env->ThrowNew(fnfe.get(), message);
Calin Juravleb077e152016-02-18 18:47:37 +0000363 return -1;
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700364 }
365
Alex Light6e183f22014-07-18 14:57:04 -0700366 const InstructionSet target_instruction_set = GetInstructionSetFromString(instruction_set);
Andreas Gampe20c89302014-08-19 17:28:06 -0700367 if (target_instruction_set == kNone) {
368 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
369 std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set));
370 env->ThrowNew(iae.get(), message.c_str());
Calin Juravleb077e152016-02-18 18:47:37 +0000371 return -1;
Andreas Gampe20c89302014-08-19 17:28:06 -0700372 }
Alex Light6e183f22014-07-18 14:57:04 -0700373
Andreas Gampe29d38e72016-03-23 15:31:51 +0000374 CompilerFilter::Filter filter;
375 if (!CompilerFilter::ParseCompilerFilter(compiler_filter_name, &filter)) {
376 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
377 std::string message(StringPrintf("Compiler filter %s is invalid.", compiler_filter_name));
378 env->ThrowNew(iae.get(), message.c_str());
379 return -1;
380 }
381
Richard Uhler66d874d2015-01-15 09:37:19 -0800382 // TODO: Verify the dex location is well formed, and throw an IOException if
383 // not?
Andreas Gampe29d38e72016-03-23 15:31:51 +0000384
Richard Uhlerd1472a22016-04-15 15:18:56 -0700385 OatFileAssistant oat_file_assistant(filename, target_instruction_set, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800386
387 // Always treat elements of the bootclasspath as up-to-date.
388 if (oat_file_assistant.IsInBootClassPath()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700389 return OatFileAssistant::kNoDexOptNeeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800390 }
Richard Uhlerd1472a22016-04-15 15:18:56 -0700391 return oat_file_assistant.GetDexOptNeeded(filter, profile_changed);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700392}
393
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100394static jstring DexFile_getDexFileStatus(JNIEnv* env,
395 jclass,
396 jstring javaFilename,
397 jstring javaInstructionSet) {
398 ScopedUtfChars filename(env, javaFilename);
399 if (env->ExceptionCheck()) {
400 return nullptr;
401 }
402
403 ScopedUtfChars instruction_set(env, javaInstructionSet);
404 if (env->ExceptionCheck()) {
405 return nullptr;
406 }
407
408 const InstructionSet target_instruction_set = GetInstructionSetFromString(
409 instruction_set.c_str());
410 if (target_instruction_set == kNone) {
411 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
412 std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set.c_str()));
413 env->ThrowNew(iae.get(), message.c_str());
414 return nullptr;
415 }
416
417 OatFileAssistant oat_file_assistant(filename.c_str(), target_instruction_set,
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100418 false /* load_executable */);
Richard Uhler46cc64f2016-11-14 14:53:55 +0000419 return env->NewStringUTF(oat_file_assistant.GetStatusDump().c_str());
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100420}
421
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700422static jint DexFile_getDexOptNeeded(JNIEnv* env,
423 jclass,
424 jstring javaFilename,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700425 jstring javaInstructionSet,
Andreas Gampec38be812016-03-23 15:03:46 -0700426 jstring javaTargetCompilerFilter,
427 jboolean newProfile) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100428 ScopedUtfChars filename(env, javaFilename);
Andreas Gampe20c89302014-08-19 17:28:06 -0700429 if (env->ExceptionCheck()) {
Calin Juravleb077e152016-02-18 18:47:37 +0000430 return -1;
Andreas Gampe20c89302014-08-19 17:28:06 -0700431 }
432
Narayan Kamath11d9f062014-04-23 20:24:57 +0100433 ScopedUtfChars instruction_set(env, javaInstructionSet);
Andreas Gampe20c89302014-08-19 17:28:06 -0700434 if (env->ExceptionCheck()) {
Calin Juravleb077e152016-02-18 18:47:37 +0000435 return -1;
Andreas Gampe20c89302014-08-19 17:28:06 -0700436 }
Narayan Kamath11d9f062014-04-23 20:24:57 +0100437
Andreas Gampec38be812016-03-23 15:03:46 -0700438 ScopedUtfChars target_compiler_filter(env, javaTargetCompilerFilter);
439 if (env->ExceptionCheck()) {
440 return -1;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000441 }
Andreas Gampec38be812016-03-23 15:03:46 -0700442
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700443 return GetDexOptNeeded(env,
444 filename.c_str(),
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700445 instruction_set.c_str(),
Andreas Gampec38be812016-03-23 15:03:46 -0700446 target_compiler_filter.c_str(),
447 newProfile == JNI_TRUE);
Narayan Kamath11d9f062014-04-23 20:24:57 +0100448}
449
Calin Juravleb077e152016-02-18 18:47:37 +0000450// public API
Narayan Kamath11d9f062014-04-23 20:24:57 +0100451static jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
Richard Uhler06e3f4f2016-05-24 15:42:37 -0700452 ScopedUtfChars filename_utf(env, javaFilename);
453 if (env->ExceptionCheck()) {
454 return JNI_FALSE;
455 }
456
457 const char* filename = filename_utf.c_str();
458 if ((filename == nullptr) || !OS::FileExists(filename)) {
459 LOG(ERROR) << "DexFile_isDexOptNeeded file '" << filename << "' does not exist";
460 ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException"));
461 const char* message = (filename == nullptr) ? "<empty file name>" : filename;
462 env->ThrowNew(fnfe.get(), message);
463 return JNI_FALSE;
464 }
465
Richard Uhlerd1472a22016-04-15 15:18:56 -0700466 OatFileAssistant oat_file_assistant(filename, kRuntimeISA, false);
Richard Uhler06e3f4f2016-05-24 15:42:37 -0700467 return oat_file_assistant.IsUpToDate() ? JNI_FALSE : JNI_TRUE;
Dave Allison39c3bfb2014-01-28 18:33:52 -0800468}
469
Andreas Gampec38be812016-03-23 15:03:46 -0700470static jboolean DexFile_isValidCompilerFilter(JNIEnv* env,
471 jclass javeDexFileClass ATTRIBUTE_UNUSED,
472 jstring javaCompilerFilter) {
473 ScopedUtfChars compiler_filter(env, javaCompilerFilter);
474 if (env->ExceptionCheck()) {
475 return -1;
476 }
477
478 CompilerFilter::Filter filter;
479 return CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)
480 ? JNI_TRUE : JNI_FALSE;
481}
482
483static jboolean DexFile_isProfileGuidedCompilerFilter(JNIEnv* env,
484 jclass javeDexFileClass ATTRIBUTE_UNUSED,
485 jstring javaCompilerFilter) {
486 ScopedUtfChars compiler_filter(env, javaCompilerFilter);
487 if (env->ExceptionCheck()) {
488 return -1;
489 }
490
491 CompilerFilter::Filter filter;
492 if (!CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)) {
493 return JNI_FALSE;
494 }
495 return CompilerFilter::DependsOnProfile(filter) ? JNI_TRUE : JNI_FALSE;
496}
497
Andreas Gampe86a785d2016-03-30 17:19:48 -0700498static jstring DexFile_getNonProfileGuidedCompilerFilter(JNIEnv* env,
499 jclass javeDexFileClass ATTRIBUTE_UNUSED,
500 jstring javaCompilerFilter) {
501 ScopedUtfChars compiler_filter(env, javaCompilerFilter);
502 if (env->ExceptionCheck()) {
503 return nullptr;
504 }
505
506 CompilerFilter::Filter filter;
507 if (!CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)) {
508 return javaCompilerFilter;
509 }
510
511 CompilerFilter::Filter new_filter = CompilerFilter::GetNonProfileDependentFilterFrom(filter);
512
513 // Filter stayed the same, return input.
514 if (filter == new_filter) {
515 return javaCompilerFilter;
516 }
517
518 // Create a new string object and return.
519 std::string new_filter_str = CompilerFilter::NameOfFilter(new_filter);
520 return env->NewStringUTF(new_filter_str.c_str());
521}
522
Jeff Hao90671be2016-04-22 14:00:06 -0700523static jboolean DexFile_isBackedByOatFile(JNIEnv* env, jclass, jobject cookie) {
524 const OatFile* oat_file = nullptr;
525 std::vector<const DexFile*> dex_files;
526 if (!ConvertJavaArrayToDexFiles(env, cookie, /*out */ dex_files, /* out */ oat_file)) {
527 DCHECK(env->ExceptionCheck());
528 return false;
529 }
530 return oat_file != nullptr;
531}
532
Philip Cuadrab55ad7c2016-07-12 16:37:40 -0700533static jstring DexFile_getDexFileOutputPath(JNIEnv* env,
534 jclass,
535 jstring javaFilename,
536 jstring javaInstructionSet) {
537 ScopedUtfChars filename(env, javaFilename);
538 if (env->ExceptionCheck()) {
539 return nullptr;
540 }
541
542 ScopedUtfChars instruction_set(env, javaInstructionSet);
543 if (env->ExceptionCheck()) {
544 return nullptr;
545 }
546
547 const InstructionSet target_instruction_set = GetInstructionSetFromString(
548 instruction_set.c_str());
549 if (target_instruction_set == kNone) {
550 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
551 std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set.c_str()));
552 env->ThrowNew(iae.get(), message.c_str());
553 return nullptr;
554 }
555
556 OatFileAssistant oat_file_assistant(filename.c_str(),
557 target_instruction_set,
Philip Cuadrab55ad7c2016-07-12 16:37:40 -0700558 false /* load_executable */);
559
560 std::unique_ptr<OatFile> best_oat_file = oat_file_assistant.GetBestOatFile();
561 if (best_oat_file == nullptr) {
562 return nullptr;
563 }
564
565 return env->NewStringUTF(best_oat_file->GetLocation().c_str());
566}
567
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700568static JNINativeMethod gMethods[] = {
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700569 NATIVE_METHOD(DexFile, closeDexFile, "(Ljava/lang/Object;)Z"),
Mathieu Chartier00310e02015-10-17 12:46:42 -0700570 NATIVE_METHOD(DexFile,
571 defineClassNative,
572 "(Ljava/lang/String;"
573 "Ljava/lang/ClassLoader;"
574 "Ljava/lang/Object;"
575 "Ldalvik/system/DexFile;"
576 ")Ljava/lang/Class;"),
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800577 NATIVE_METHOD(DexFile, getClassNameList, "(Ljava/lang/Object;)[Ljava/lang/String;"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700578 NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700579 NATIVE_METHOD(DexFile, getDexOptNeeded,
Andreas Gampec38be812016-03-23 15:03:46 -0700580 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Z)I"),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700581 NATIVE_METHOD(DexFile, openDexFileNative,
Mathieu Chartier689a7002015-11-20 10:29:42 -0800582 "(Ljava/lang/String;"
583 "Ljava/lang/String;"
584 "I"
585 "Ljava/lang/ClassLoader;"
586 "[Ldalvik/system/DexPathList$Element;"
587 ")Ljava/lang/Object;"),
Andreas Gampec38be812016-03-23 15:03:46 -0700588 NATIVE_METHOD(DexFile, isValidCompilerFilter, "(Ljava/lang/String;)Z"),
589 NATIVE_METHOD(DexFile, isProfileGuidedCompilerFilter, "(Ljava/lang/String;)Z"),
Andreas Gampe86a785d2016-03-30 17:19:48 -0700590 NATIVE_METHOD(DexFile,
591 getNonProfileGuidedCompilerFilter,
592 "(Ljava/lang/String;)Ljava/lang/String;"),
Jeff Hao90671be2016-04-22 14:00:06 -0700593 NATIVE_METHOD(DexFile, isBackedByOatFile, "(Ljava/lang/Object;)Z"),
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100594 NATIVE_METHOD(DexFile, getDexFileStatus,
Philip Cuadrab55ad7c2016-07-12 16:37:40 -0700595 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
596 NATIVE_METHOD(DexFile, getDexFileOutputPath,
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100597 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;")
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700598};
599
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700600void register_dalvik_system_DexFile(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700601 REGISTER_NATIVE_METHODS("dalvik/system/DexFile");
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700602}
603
604} // namespace art