blob: 4f9cf70efd661c57e39e33d7b659425a9b9af2c2 [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
Vladimir Marko78baed52018-10-11 10:44:58 +010023#include "base/casts.h"
David Sehr891a50e2017-10-27 17:01:07 -070024#include "base/file_utils.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080025#include "base/logging.h"
David Sehrc431b9d2018-03-02 12:01:51 -080026#include "base/os.h"
Andreas Gampe833a4852014-05-21 18:46:59 -070027#include "base/stl_util.h"
David Sehrc431b9d2018-03-02 12:01:51 -080028#include "base/utils.h"
David Sehr79e26072018-04-06 17:58:50 -070029#include "base/zip_archive.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070030#include "class_linker.h"
Calin Juravle20c46442017-09-12 00:54:26 -070031#include <class_loader_context.h>
Ian Rogers62d6c772013-02-27 08:32:07 -080032#include "common_throws.h"
Andreas Gampec38be812016-03-23 15:03:46 -070033#include "compiler_filter.h"
David Sehr013fd802018-01-11 22:55:24 -080034#include "dex/art_dex_file_loader.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080035#include "dex/descriptors_names.h"
David Sehr9e734c72018-01-04 17:56:19 -080036#include "dex/dex_file-inl.h"
37#include "dex/dex_file_loader.h"
Andreas Gampe88dbad32018-06-26 19:54:12 -070038#include "handle_scope-inl.h"
David Srbeckyfb3de3d2018-01-29 16:11:49 +000039#include "jit/debugger_interface.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010040#include "jni/jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041#include "mirror/class_loader.h"
Ian Rogers05f30572013-02-20 12:13:11 -080042#include "mirror/object-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080043#include "mirror/string.h"
Andreas Gampe87583b32017-05-25 11:22:18 -070044#include "native_util.h"
Steven Morelande431e272017-07-18 16:53:49 -070045#include "nativehelper/jni_macros.h"
Andreas Gampe373a9b52017-10-18 09:01:57 -070046#include "nativehelper/scoped_local_ref.h"
47#include "nativehelper/scoped_utf_chars.h"
Narayan Kamath8943c1d2016-05-02 13:14:48 +010048#include "oat_file.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080049#include "oat_file_assistant.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070050#include "oat_file_manager.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070051#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070052#include "scoped_thread_state_change-inl.h"
Ian Rogersdd157d72014-05-15 14:47:50 -070053#include "well_known_classes.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070054
55namespace art {
56
Andreas Gampe46ee31b2016-12-14 10:11:49 -080057using android::base::StringPrintf;
58
Mathieu Chartiere58991b2015-10-13 07:59:34 -070059static bool ConvertJavaArrayToDexFiles(
60 JNIEnv* env,
61 jobject arrayObject,
62 /*out*/ std::vector<const DexFile*>& dex_files,
63 /*out*/ const OatFile*& oat_file) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -080064 jarray array = reinterpret_cast<jarray>(arrayObject);
65
66 jsize array_size = env->GetArrayLength(array);
67 if (env->ExceptionCheck() == JNI_TRUE) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -070068 return false;
Andreas Gampe324b9bb2015-02-23 16:33:22 -080069 }
70
71 // TODO: Optimize. On 32bit we can use an int array.
72 jboolean is_long_data_copied;
73 jlong* long_data = env->GetLongArrayElements(reinterpret_cast<jlongArray>(array),
74 &is_long_data_copied);
75 if (env->ExceptionCheck() == JNI_TRUE) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -070076 return false;
Andreas Gampe324b9bb2015-02-23 16:33:22 -080077 }
78
Vladimir Marko78baed52018-10-11 10:44:58 +010079 oat_file = reinterpret_cast64<const OatFile*>(long_data[kOatFileIndex]);
Mathieu Chartiere58991b2015-10-13 07:59:34 -070080 dex_files.reserve(array_size - 1);
81 for (jsize i = kDexFileIndexStart; i < array_size; ++i) {
Vladimir Marko78baed52018-10-11 10:44:58 +010082 dex_files.push_back(reinterpret_cast64<const DexFile*>(long_data[i]));
Andreas Gampe324b9bb2015-02-23 16:33:22 -080083 }
84
85 env->ReleaseLongArrayElements(reinterpret_cast<jlongArray>(array), long_data, JNI_ABORT);
Mathieu Chartiere58991b2015-10-13 07:59:34 -070086 return env->ExceptionCheck() != JNI_TRUE;
Andreas Gampe324b9bb2015-02-23 16:33:22 -080087}
88
Mathieu Chartiere58991b2015-10-13 07:59:34 -070089static jlongArray ConvertDexFilesToJavaArray(JNIEnv* env,
90 const OatFile* oat_file,
91 std::vector<std::unique_ptr<const DexFile>>& vec) {
92 // Add one for the oat file.
Mathieu Chartier80b37b72015-10-12 18:13:39 -070093 jlongArray long_array = env->NewLongArray(static_cast<jsize>(kDexFileIndexStart + vec.size()));
Andreas Gampe324b9bb2015-02-23 16:33:22 -080094 if (env->ExceptionCheck() == JNI_TRUE) {
95 return nullptr;
96 }
97
98 jboolean is_long_data_copied;
99 jlong* long_data = env->GetLongArrayElements(long_array, &is_long_data_copied);
100 if (env->ExceptionCheck() == JNI_TRUE) {
101 return nullptr;
102 }
103
Vladimir Marko78baed52018-10-11 10:44:58 +0100104 long_data[kOatFileIndex] = reinterpret_cast64<jlong>(oat_file);
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700105 for (size_t i = 0; i < vec.size(); ++i) {
Vladimir Marko78baed52018-10-11 10:44:58 +0100106 long_data[kDexFileIndexStart + i] = reinterpret_cast64<jlong>(vec[i].get());
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800107 }
108
109 env->ReleaseLongArrayElements(long_array, long_data, 0);
110 if (env->ExceptionCheck() == JNI_TRUE) {
111 return nullptr;
112 }
113
114 // Now release all the unique_ptrs.
115 for (auto& dex_file : vec) {
Andreas Gampeafaf7f82018-10-16 11:32:38 -0700116 dex_file.release(); // NOLINT
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800117 }
118
119 return long_array;
120}
121
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700122// A smart pointer that provides read-only access to a Java string's UTF chars.
123// Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if
124// passed a null jstring. The correct idiom is:
125//
126// NullableScopedUtfChars name(env, javaName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -0700127// if (env->ExceptionCheck()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700128// return null;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700129// }
130// // ... use name.c_str()
131//
132// TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option.
133class NullableScopedUtfChars {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800134 public:
135 NullableScopedUtfChars(JNIEnv* env, jstring s) : mEnv(env), mString(s) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700136 mUtfChars = (s != nullptr) ? env->GetStringUTFChars(s, nullptr) : nullptr;
Elliott Hughesba8eee12012-01-24 20:25:24 -0800137 }
138
139 ~NullableScopedUtfChars() {
140 if (mUtfChars) {
141 mEnv->ReleaseStringUTFChars(mString, mUtfChars);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700142 }
Elliott Hughesba8eee12012-01-24 20:25:24 -0800143 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700144
Elliott Hughesba8eee12012-01-24 20:25:24 -0800145 const char* c_str() const {
146 return mUtfChars;
147 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700148
Elliott Hughesba8eee12012-01-24 20:25:24 -0800149 size_t size() const {
150 return strlen(mUtfChars);
151 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700152
Elliott Hughesba8eee12012-01-24 20:25:24 -0800153 // Element access.
154 const char& operator[](size_t n) const {
155 return mUtfChars[n];
156 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700157
Elliott Hughesba8eee12012-01-24 20:25:24 -0800158 private:
159 JNIEnv* mEnv;
160 jstring mString;
161 const char* mUtfChars;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700162
Elliott Hughesba8eee12012-01-24 20:25:24 -0800163 // Disallow copy and assignment.
164 NullableScopedUtfChars(const NullableScopedUtfChars&);
165 void operator=(const NullableScopedUtfChars&);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700166};
167
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100168static MemMap AllocateDexMemoryMap(JNIEnv* env, jint start, jint end) {
Alex Lightea9465e2017-02-16 15:38:35 -0800169 if (end <= start) {
170 ScopedObjectAccess soa(env);
171 ThrowWrappedIOException("Bad range");
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100172 return MemMap::Invalid();
Alex Lightea9465e2017-02-16 15:38:35 -0800173 }
174
175 std::string error_message;
176 size_t length = static_cast<size_t>(end - start);
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100177 MemMap dex_mem_map = MemMap::MapAnonymous("DEX data",
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100178 length,
179 PROT_READ | PROT_WRITE,
Vladimir Marko11306592018-10-26 14:22:59 +0100180 /*low_4gb=*/ false,
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100181 &error_message);
182 if (!dex_mem_map.IsValid()) {
Alex Lightea9465e2017-02-16 15:38:35 -0800183 ScopedObjectAccess soa(env);
184 ThrowWrappedIOException("%s", error_message.c_str());
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100185 return MemMap::Invalid();
Alex Lightea9465e2017-02-16 15:38:35 -0800186 }
187 return dex_mem_map;
188}
189
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100190static const DexFile* CreateDexFile(JNIEnv* env, MemMap&& dex_mem_map) {
Alex Lightea9465e2017-02-16 15:38:35 -0800191 std::string location = StringPrintf("Anonymous-DexFile@%p-%p",
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100192 dex_mem_map.Begin(),
193 dex_mem_map.End());
Alex Lightea9465e2017-02-16 15:38:35 -0800194 std::string error_message;
David Sehr013fd802018-01-11 22:55:24 -0800195 const ArtDexFileLoader dex_file_loader;
196 std::unique_ptr<const DexFile> dex_file(dex_file_loader.Open(location,
197 0,
198 std::move(dex_mem_map),
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700199 /* verify= */ true,
200 /* verify_checksum= */ true,
David Sehr013fd802018-01-11 22:55:24 -0800201 &error_message));
Alex Lightea9465e2017-02-16 15:38:35 -0800202 if (dex_file == nullptr) {
203 ScopedObjectAccess soa(env);
204 ThrowWrappedIOException("%s", error_message.c_str());
205 return nullptr;
206 }
207
208 if (!dex_file->DisableWrite()) {
209 ScopedObjectAccess soa(env);
210 ThrowWrappedIOException("Failed to make dex file read-only");
211 return nullptr;
212 }
213
214 return dex_file.release();
215}
216
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100217static jobject CreateSingleDexFileCookie(JNIEnv* env, MemMap&& data) {
Alex Lightea9465e2017-02-16 15:38:35 -0800218 std::unique_ptr<const DexFile> dex_file(CreateDexFile(env, std::move(data)));
219 if (dex_file.get() == nullptr) {
220 DCHECK(env->ExceptionCheck());
221 return nullptr;
222 }
223 std::vector<std::unique_ptr<const DexFile>> dex_files;
224 dex_files.push_back(std::move(dex_file));
225 return ConvertDexFilesToJavaArray(env, nullptr, dex_files);
226}
227
228static jobject DexFile_createCookieWithDirectBuffer(JNIEnv* env,
229 jclass,
230 jobject buffer,
231 jint start,
232 jint end) {
233 uint8_t* base_address = reinterpret_cast<uint8_t*>(env->GetDirectBufferAddress(buffer));
234 if (base_address == nullptr) {
235 ScopedObjectAccess soa(env);
236 ThrowWrappedIOException("dexFileBuffer not direct");
Yi Kong4b22b342018-08-02 14:43:21 -0700237 return nullptr;
Alex Lightea9465e2017-02-16 15:38:35 -0800238 }
239
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100240 MemMap dex_mem_map = AllocateDexMemoryMap(env, start, end);
241 if (!dex_mem_map.IsValid()) {
Alex Lightea9465e2017-02-16 15:38:35 -0800242 DCHECK(Thread::Current()->IsExceptionPending());
Yi Kong4b22b342018-08-02 14:43:21 -0700243 return nullptr;
Alex Lightea9465e2017-02-16 15:38:35 -0800244 }
245
246 size_t length = static_cast<size_t>(end - start);
David Brazdil1a041482018-12-18 17:55:08 +0000247 memcpy(dex_mem_map.Begin(), base_address + start, length);
Alex Lightea9465e2017-02-16 15:38:35 -0800248 return CreateSingleDexFileCookie(env, std::move(dex_mem_map));
249}
250
251static jobject DexFile_createCookieWithArray(JNIEnv* env,
252 jclass,
253 jbyteArray buffer,
254 jint start,
255 jint end) {
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100256 MemMap dex_mem_map = AllocateDexMemoryMap(env, start, end);
257 if (!dex_mem_map.IsValid()) {
Alex Lightea9465e2017-02-16 15:38:35 -0800258 DCHECK(Thread::Current()->IsExceptionPending());
Yi Kong4b22b342018-08-02 14:43:21 -0700259 return nullptr;
Alex Lightea9465e2017-02-16 15:38:35 -0800260 }
261
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100262 auto destination = reinterpret_cast<jbyte*>(dex_mem_map.Begin());
Alex Lightea9465e2017-02-16 15:38:35 -0800263 env->GetByteArrayRegion(buffer, start, end - start, destination);
264 return CreateSingleDexFileCookie(env, std::move(dex_mem_map));
265}
266
Calin Juravle1f7079b2017-04-18 21:25:37 -0700267// TODO(calin): clean up the unused parameters (here and in libcore).
Mathieu Chartierb190d942015-11-12 10:00:58 -0800268static jobject DexFile_openDexFileNative(JNIEnv* env,
269 jclass,
270 jstring javaSourceName,
Calin Juravle1f7079b2017-04-18 21:25:37 -0700271 jstring javaOutputName ATTRIBUTE_UNUSED,
Mathieu Chartierb190d942015-11-12 10:00:58 -0800272 jint flags ATTRIBUTE_UNUSED,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800273 jobject class_loader,
274 jobjectArray dex_elements) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700275 ScopedUtfChars sourceName(env, javaSourceName);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700276 if (sourceName.c_str() == nullptr) {
Yi Kong4b22b342018-08-02 14:43:21 -0700277 return nullptr;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700278 }
Calin Juravle1f7079b2017-04-18 21:25:37 -0700279
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700280 Runtime* const runtime = Runtime::Current();
281 ClassLinker* linker = runtime->GetClassLinker();
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800282 std::vector<std::unique_ptr<const DexFile>> dex_files;
Andreas Gampe833a4852014-05-21 18:46:59 -0700283 std::vector<std::string> error_msgs;
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700284 const OatFile* oat_file = nullptr;
Andreas Gampe833a4852014-05-21 18:46:59 -0700285
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700286 dex_files = runtime->GetOatFileManager().OpenDexFilesFromOat(sourceName.c_str(),
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800287 class_loader,
288 dex_elements,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700289 /*out*/ &oat_file,
290 /*out*/ &error_msgs);
Andreas Gampe833a4852014-05-21 18:46:59 -0700291
Richard Uhler66d874d2015-01-15 09:37:19 -0800292 if (!dex_files.empty()) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700293 jlongArray array = ConvertDexFilesToJavaArray(env, oat_file, dex_files);
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800294 if (array == nullptr) {
295 ScopedObjectAccess soa(env);
296 for (auto& dex_file : dex_files) {
Vladimir Markocd556b02017-02-03 11:47:34 +0000297 if (linker->IsDexFileRegistered(soa.Self(), *dex_file)) {
Andreas Gampeafaf7f82018-10-16 11:32:38 -0700298 dex_file.release(); // NOLINT
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800299 }
300 }
301 }
302 return array;
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700303 } else {
Vladimir Marko60836d52014-01-16 15:53:38 +0000304 ScopedObjectAccess soa(env);
Andreas Gampe329d1882014-04-08 10:32:19 -0700305 CHECK(!error_msgs.empty());
306 // The most important message is at the end. So set up nesting by going forward, which will
307 // wrap the existing exception as a cause for the following one.
308 auto it = error_msgs.begin();
309 auto itEnd = error_msgs.end();
310 for ( ; it != itEnd; ++it) {
311 ThrowWrappedIOException("%s", it->c_str());
312 }
313
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800314 return nullptr;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700315 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700316}
317
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700318static jboolean DexFile_closeDexFile(JNIEnv* env, jclass, jobject cookie) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700319 std::vector<const DexFile*> dex_files;
320 const OatFile* oat_file;
321 if (!ConvertJavaArrayToDexFiles(env, cookie, dex_files, oat_file)) {
322 Thread::Current()->AssertPendingException();
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700323 return JNI_FALSE;
324 }
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700325 Runtime* const runtime = Runtime::Current();
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700326 bool all_deleted = true;
David Srbecky912f36c2018-09-08 12:22:58 +0100327 // We need to clear the caches since they may contain pointers to the dex instructions.
328 // Different dex file can be loaded at the same memory location later by chance.
329 Thread::ClearAllInterpreterCaches();
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700330 {
331 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700332 ObjPtr<mirror::Object> dex_files_object = soa.Decode<mirror::Object>(cookie);
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700333 ObjPtr<mirror::LongArray> long_dex_files = dex_files_object->AsLongArray();
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700334 // Delete dex files associated with this dalvik.system.DexFile since there should not be running
335 // code using it. dex_files is a vector due to multidex.
336 ClassLinker* const class_linker = runtime->GetClassLinker();
337 int32_t i = kDexFileIndexStart; // Oat file is at index 0.
338 for (const DexFile* dex_file : dex_files) {
339 if (dex_file != nullptr) {
David Srbeckyafc60cd2018-12-05 11:59:31 +0000340 RemoveNativeDebugInfoForDex(soa.Self(), dex_file);
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700341 // Only delete the dex file if the dex cache is not found to prevent runtime crashes if there
342 // are calls to DexFile.close while the ART DexFile is still in use.
Vladimir Markocd556b02017-02-03 11:47:34 +0000343 if (!class_linker->IsDexFileRegistered(soa.Self(), *dex_file)) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700344 // Clear the element in the array so that we can call close again.
345 long_dex_files->Set(i, 0);
346 delete dex_file;
347 } else {
348 all_deleted = false;
349 }
350 }
351 ++i;
Andreas Gampe833a4852014-05-21 18:46:59 -0700352 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700353 }
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700354
Mathieu Chartierfdccbd42015-10-14 10:58:41 -0700355 // oat_file can be null if we are running without dex2oat.
356 if (all_deleted && oat_file != nullptr) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700357 // If all of the dex files are no longer in use we can unmap the corresponding oat file.
358 VLOG(class_linker) << "Unregistering " << oat_file;
359 runtime->GetOatFileManager().UnRegisterAndDeleteOatFile(oat_file);
360 }
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700361 return all_deleted ? JNI_TRUE : JNI_FALSE;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700362}
363
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700364static jclass DexFile_defineClassNative(JNIEnv* env,
365 jclass,
366 jstring javaName,
367 jobject javaLoader,
Mathieu Chartier00310e02015-10-17 12:46:42 -0700368 jobject cookie,
369 jobject dexFile) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700370 std::vector<const DexFile*> dex_files;
371 const OatFile* oat_file;
372 if (!ConvertJavaArrayToDexFiles(env, cookie, /*out*/ dex_files, /*out*/ oat_file)) {
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700373 VLOG(class_linker) << "Failed to find dex_file";
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800374 DCHECK(env->ExceptionCheck());
375 return nullptr;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700376 }
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800377
Brian Carlstromdf143242011-10-10 18:05:34 -0700378 ScopedUtfChars class_name(env, javaName);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700379 if (class_name.c_str() == nullptr) {
Brian Carlstrom2e450bf2013-09-06 15:39:46 -0700380 VLOG(class_linker) << "Failed to find class_name";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700381 return nullptr;
Brian Carlstromdf143242011-10-10 18:05:34 -0700382 }
Elliott Hughes95572412011-12-13 18:14:20 -0800383 const std::string descriptor(DotToDescriptor(class_name.c_str()));
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800384 const size_t hash(ComputeModifiedUtf8Hash(descriptor.c_str()));
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700385 for (auto& dex_file : dex_files) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800386 const dex::ClassDef* dex_class_def =
David Sehr9aa352e2016-09-15 18:13:52 -0700387 OatDexFile::FindClassDef(*dex_file, descriptor.c_str(), hash);
Andreas Gampe833a4852014-05-21 18:46:59 -0700388 if (dex_class_def != nullptr) {
389 ScopedObjectAccess soa(env);
390 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Andreas Gampe833a4852014-05-21 18:46:59 -0700391 StackHandleScope<1> hs(soa.Self());
392 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700393 hs.NewHandle(soa.Decode<mirror::ClassLoader>(javaLoader)));
Vladimir Markocd556b02017-02-03 11:47:34 +0000394 ObjPtr<mirror::DexCache> dex_cache =
395 class_linker->RegisterDexFile(*dex_file, class_loader.Get());
396 if (dex_cache == nullptr) {
397 // OOME or InternalError (dexFile already registered with a different class loader).
398 soa.Self()->AssertPendingException();
399 return nullptr;
400 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700401 ObjPtr<mirror::Class> result = class_linker->DefineClass(soa.Self(),
402 descriptor.c_str(),
403 hash,
404 class_loader,
405 *dex_file,
406 *dex_class_def);
Mathieu Chartier00310e02015-10-17 12:46:42 -0700407 // Add the used dex file. This only required for the DexFile.loadClass API since normal
408 // class loaders already keep their dex files live.
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700409 class_linker->InsertDexFileInToClassLoader(soa.Decode<mirror::Object>(dexFile),
Mathieu Chartier00310e02015-10-17 12:46:42 -0700410 class_loader.Get());
Andreas Gampe833a4852014-05-21 18:46:59 -0700411 if (result != nullptr) {
Brian Carlstrom667ab7c2014-10-16 19:12:28 -0700412 VLOG(class_linker) << "DexFile_defineClassNative returning " << result
413 << " for " << class_name.c_str();
Andreas Gampe833a4852014-05-21 18:46:59 -0700414 return soa.AddLocalReference<jclass>(result);
415 }
416 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700417 }
Brian Carlstrom667ab7c2014-10-16 19:12:28 -0700418 VLOG(class_linker) << "Failed to find dex_class_def " << class_name.c_str();
Andreas Gampe833a4852014-05-21 18:46:59 -0700419 return nullptr;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700420}
421
Andreas Gampe833a4852014-05-21 18:46:59 -0700422// Needed as a compare functor for sets of const char
423struct CharPointerComparator {
424 bool operator()(const char *str1, const char *str2) const {
425 return strcmp(str1, str2) < 0;
426 }
427};
428
429// Note: this can be an expensive call, as we sort out duplicates in MultiDex files.
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800430static jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jobject cookie) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700431 const OatFile* oat_file = nullptr;
432 std::vector<const DexFile*> dex_files;
433 if (!ConvertJavaArrayToDexFiles(env, cookie, /*out */ dex_files, /* out */ oat_file)) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800434 DCHECK(env->ExceptionCheck());
435 return nullptr;
436 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700437
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800438 // Push all class descriptors into a set. Use set instead of unordered_set as we want to
439 // retrieve all in the end.
440 std::set<const char*, CharPointerComparator> descriptors;
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700441 for (auto& dex_file : dex_files) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800442 for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800443 const dex::ClassDef& class_def = dex_file->GetClassDef(i);
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800444 const char* descriptor = dex_file->GetClassDescriptor(class_def);
445 descriptors.insert(descriptor);
Andreas Gampe833a4852014-05-21 18:46:59 -0700446 }
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800447 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700448
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800449 // Now create output array and copy the set into it.
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700450 jobjectArray result = env->NewObjectArray(descriptors.size(),
451 WellKnownClasses::java_lang_String,
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800452 nullptr);
453 if (result != nullptr) {
454 auto it = descriptors.begin();
455 auto it_end = descriptors.end();
456 jsize i = 0;
457 for (; it != it_end; it++, ++i) {
458 std::string descriptor(DescriptorToDot(*it));
459 ScopedLocalRef<jstring> jdescriptor(env, env->NewStringUTF(descriptor.c_str()));
460 if (jdescriptor.get() == nullptr) {
461 return nullptr;
Ian Rogersdd157d72014-05-15 14:47:50 -0700462 }
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800463 env->SetObjectArrayElement(result, i, jdescriptor.get());
Ian Rogersdd157d72014-05-15 14:47:50 -0700464 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700465 }
Ian Rogersdd157d72014-05-15 14:47:50 -0700466 return result;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700467}
468
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700469static jint GetDexOptNeeded(JNIEnv* env,
470 const char* filename,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700471 const char* instruction_set,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000472 const char* compiler_filter_name,
Calin Juravle20c46442017-09-12 00:54:26 -0700473 const char* class_loader_context,
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700474 bool profile_changed,
475 bool downgrade) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100476 if ((filename == nullptr) || !OS::FileExists(filename)) {
Richard Uhler95abd042015-03-24 09:51:28 -0700477 LOG(ERROR) << "DexFile_getDexOptNeeded file '" << filename << "' does not exist";
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700478 ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException"));
Narayan Kamath11d9f062014-04-23 20:24:57 +0100479 const char* message = (filename == nullptr) ? "<empty file name>" : filename;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700480 env->ThrowNew(fnfe.get(), message);
Calin Juravleb077e152016-02-18 18:47:37 +0000481 return -1;
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700482 }
483
Alex Light6e183f22014-07-18 14:57:04 -0700484 const InstructionSet target_instruction_set = GetInstructionSetFromString(instruction_set);
Vladimir Marko33bff252017-11-01 14:35:42 +0000485 if (target_instruction_set == InstructionSet::kNone) {
Andreas Gampe20c89302014-08-19 17:28:06 -0700486 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
487 std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set));
488 env->ThrowNew(iae.get(), message.c_str());
Calin Juravleb077e152016-02-18 18:47:37 +0000489 return -1;
Andreas Gampe20c89302014-08-19 17:28:06 -0700490 }
Alex Light6e183f22014-07-18 14:57:04 -0700491
Andreas Gampe29d38e72016-03-23 15:31:51 +0000492 CompilerFilter::Filter filter;
493 if (!CompilerFilter::ParseCompilerFilter(compiler_filter_name, &filter)) {
494 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
495 std::string message(StringPrintf("Compiler filter %s is invalid.", compiler_filter_name));
496 env->ThrowNew(iae.get(), message.c_str());
497 return -1;
498 }
499
Calin Juravle20c46442017-09-12 00:54:26 -0700500 std::unique_ptr<ClassLoaderContext> context = nullptr;
501 if (class_loader_context != nullptr) {
502 context = ClassLoaderContext::Create(class_loader_context);
503
504 if (context == nullptr) {
505 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
506 std::string message(StringPrintf("Class loader context '%s' is invalid.",
507 class_loader_context));
508 env->ThrowNew(iae.get(), message.c_str());
509 return -1;
510 }
511 }
512
Richard Uhler66d874d2015-01-15 09:37:19 -0800513 // TODO: Verify the dex location is well formed, and throw an IOException if
514 // not?
Andreas Gampe29d38e72016-03-23 15:31:51 +0000515
Richard Uhlerd1472a22016-04-15 15:18:56 -0700516 OatFileAssistant oat_file_assistant(filename, target_instruction_set, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800517
518 // Always treat elements of the bootclasspath as up-to-date.
519 if (oat_file_assistant.IsInBootClassPath()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700520 return OatFileAssistant::kNoDexOptNeeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800521 }
Calin Juravle44e5efa2017-09-12 00:54:26 -0700522
Calin Juravle20c46442017-09-12 00:54:26 -0700523 return oat_file_assistant.GetDexOptNeeded(filter,
524 profile_changed,
525 downgrade,
526 context.get());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700527}
528
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100529static jstring DexFile_getDexFileStatus(JNIEnv* env,
530 jclass,
531 jstring javaFilename,
532 jstring javaInstructionSet) {
533 ScopedUtfChars filename(env, javaFilename);
534 if (env->ExceptionCheck()) {
535 return nullptr;
536 }
537
538 ScopedUtfChars instruction_set(env, javaInstructionSet);
539 if (env->ExceptionCheck()) {
540 return nullptr;
541 }
542
543 const InstructionSet target_instruction_set = GetInstructionSetFromString(
544 instruction_set.c_str());
Vladimir Marko33bff252017-11-01 14:35:42 +0000545 if (target_instruction_set == InstructionSet::kNone) {
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100546 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
547 std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set.c_str()));
548 env->ThrowNew(iae.get(), message.c_str());
549 return nullptr;
550 }
551
552 OatFileAssistant oat_file_assistant(filename.c_str(), target_instruction_set,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700553 /* load_executable= */ false);
Richard Uhler46cc64f2016-11-14 14:53:55 +0000554 return env->NewStringUTF(oat_file_assistant.GetStatusDump().c_str());
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100555}
556
Calin Juravle5f9a8012018-02-12 20:27:46 -0800557// Return an array specifying the optimization status of the given file.
558// The array specification is [compiler_filter, compiler_reason].
559static jobjectArray DexFile_getDexFileOptimizationStatus(JNIEnv* env,
560 jclass,
561 jstring javaFilename,
562 jstring javaInstructionSet) {
563 ScopedUtfChars filename(env, javaFilename);
564 if (env->ExceptionCheck()) {
565 return nullptr;
566 }
567
568 ScopedUtfChars instruction_set(env, javaInstructionSet);
569 if (env->ExceptionCheck()) {
570 return nullptr;
571 }
572
573 const InstructionSet target_instruction_set = GetInstructionSetFromString(
574 instruction_set.c_str());
575 if (target_instruction_set == InstructionSet::kNone) {
576 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
577 std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set.c_str()));
578 env->ThrowNew(iae.get(), message.c_str());
579 return nullptr;
580 }
581
582 std::string compilation_filter;
583 std::string compilation_reason;
584 OatFileAssistant::GetOptimizationStatus(
585 filename.c_str(), target_instruction_set, &compilation_filter, &compilation_reason);
586
587 ScopedLocalRef<jstring> j_compilation_filter(env, env->NewStringUTF(compilation_filter.c_str()));
588 if (j_compilation_filter.get() == nullptr) {
589 return nullptr;
590 }
591 ScopedLocalRef<jstring> j_compilation_reason(env, env->NewStringUTF(compilation_reason.c_str()));
592 if (j_compilation_reason.get() == nullptr) {
593 return nullptr;
594 }
595
596 // Now create output array and copy the set into it.
597 jobjectArray result = env->NewObjectArray(2,
598 WellKnownClasses::java_lang_String,
599 nullptr);
600 env->SetObjectArrayElement(result, 0, j_compilation_filter.get());
601 env->SetObjectArrayElement(result, 1, j_compilation_reason.get());
602
603 return result;
604}
605
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700606static jint DexFile_getDexOptNeeded(JNIEnv* env,
607 jclass,
608 jstring javaFilename,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700609 jstring javaInstructionSet,
Andreas Gampec38be812016-03-23 15:03:46 -0700610 jstring javaTargetCompilerFilter,
Calin Juravle20c46442017-09-12 00:54:26 -0700611 jstring javaClassLoaderContext,
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700612 jboolean newProfile,
613 jboolean downgrade) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100614 ScopedUtfChars filename(env, javaFilename);
Andreas Gampe20c89302014-08-19 17:28:06 -0700615 if (env->ExceptionCheck()) {
Calin Juravleb077e152016-02-18 18:47:37 +0000616 return -1;
Andreas Gampe20c89302014-08-19 17:28:06 -0700617 }
618
Narayan Kamath11d9f062014-04-23 20:24:57 +0100619 ScopedUtfChars instruction_set(env, javaInstructionSet);
Andreas Gampe20c89302014-08-19 17:28:06 -0700620 if (env->ExceptionCheck()) {
Calin Juravleb077e152016-02-18 18:47:37 +0000621 return -1;
Andreas Gampe20c89302014-08-19 17:28:06 -0700622 }
Narayan Kamath11d9f062014-04-23 20:24:57 +0100623
Andreas Gampec38be812016-03-23 15:03:46 -0700624 ScopedUtfChars target_compiler_filter(env, javaTargetCompilerFilter);
625 if (env->ExceptionCheck()) {
626 return -1;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000627 }
Andreas Gampec38be812016-03-23 15:03:46 -0700628
Calin Juravle20c46442017-09-12 00:54:26 -0700629 NullableScopedUtfChars class_loader_context(env, javaClassLoaderContext);
630 if (env->ExceptionCheck()) {
631 return -1;
632 }
633
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700634 return GetDexOptNeeded(env,
635 filename.c_str(),
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700636 instruction_set.c_str(),
Andreas Gampec38be812016-03-23 15:03:46 -0700637 target_compiler_filter.c_str(),
Calin Juravle20c46442017-09-12 00:54:26 -0700638 class_loader_context.c_str(),
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700639 newProfile == JNI_TRUE,
640 downgrade == JNI_TRUE);
Narayan Kamath11d9f062014-04-23 20:24:57 +0100641}
642
Calin Juravleb077e152016-02-18 18:47:37 +0000643// public API
Narayan Kamath11d9f062014-04-23 20:24:57 +0100644static jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
Richard Uhler06e3f4f2016-05-24 15:42:37 -0700645 ScopedUtfChars filename_utf(env, javaFilename);
646 if (env->ExceptionCheck()) {
647 return JNI_FALSE;
648 }
649
650 const char* filename = filename_utf.c_str();
651 if ((filename == nullptr) || !OS::FileExists(filename)) {
652 LOG(ERROR) << "DexFile_isDexOptNeeded file '" << filename << "' does not exist";
653 ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException"));
654 const char* message = (filename == nullptr) ? "<empty file name>" : filename;
655 env->ThrowNew(fnfe.get(), message);
656 return JNI_FALSE;
657 }
658
Richard Uhlerd1472a22016-04-15 15:18:56 -0700659 OatFileAssistant oat_file_assistant(filename, kRuntimeISA, false);
Richard Uhler06e3f4f2016-05-24 15:42:37 -0700660 return oat_file_assistant.IsUpToDate() ? JNI_FALSE : JNI_TRUE;
Dave Allison39c3bfb2014-01-28 18:33:52 -0800661}
662
Andreas Gampec38be812016-03-23 15:03:46 -0700663static jboolean DexFile_isValidCompilerFilter(JNIEnv* env,
664 jclass javeDexFileClass ATTRIBUTE_UNUSED,
665 jstring javaCompilerFilter) {
666 ScopedUtfChars compiler_filter(env, javaCompilerFilter);
667 if (env->ExceptionCheck()) {
668 return -1;
669 }
670
671 CompilerFilter::Filter filter;
672 return CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)
673 ? JNI_TRUE : JNI_FALSE;
674}
675
676static jboolean DexFile_isProfileGuidedCompilerFilter(JNIEnv* env,
677 jclass javeDexFileClass ATTRIBUTE_UNUSED,
678 jstring javaCompilerFilter) {
679 ScopedUtfChars compiler_filter(env, javaCompilerFilter);
680 if (env->ExceptionCheck()) {
681 return -1;
682 }
683
684 CompilerFilter::Filter filter;
685 if (!CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)) {
686 return JNI_FALSE;
687 }
688 return CompilerFilter::DependsOnProfile(filter) ? JNI_TRUE : JNI_FALSE;
689}
690
Andreas Gampe86a785d2016-03-30 17:19:48 -0700691static jstring DexFile_getNonProfileGuidedCompilerFilter(JNIEnv* env,
692 jclass javeDexFileClass ATTRIBUTE_UNUSED,
693 jstring javaCompilerFilter) {
694 ScopedUtfChars compiler_filter(env, javaCompilerFilter);
695 if (env->ExceptionCheck()) {
696 return nullptr;
697 }
698
699 CompilerFilter::Filter filter;
700 if (!CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)) {
701 return javaCompilerFilter;
702 }
703
704 CompilerFilter::Filter new_filter = CompilerFilter::GetNonProfileDependentFilterFrom(filter);
705
706 // Filter stayed the same, return input.
707 if (filter == new_filter) {
708 return javaCompilerFilter;
709 }
710
711 // Create a new string object and return.
712 std::string new_filter_str = CompilerFilter::NameOfFilter(new_filter);
713 return env->NewStringUTF(new_filter_str.c_str());
714}
715
Nicolas Geoffray741d4262017-05-03 13:08:36 +0100716static jstring DexFile_getSafeModeCompilerFilter(JNIEnv* env,
717 jclass javeDexFileClass ATTRIBUTE_UNUSED,
718 jstring javaCompilerFilter) {
719 ScopedUtfChars compiler_filter(env, javaCompilerFilter);
720 if (env->ExceptionCheck()) {
721 return nullptr;
722 }
723
724 CompilerFilter::Filter filter;
725 if (!CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)) {
726 return javaCompilerFilter;
727 }
728
729 CompilerFilter::Filter new_filter = CompilerFilter::GetSafeModeFilterFrom(filter);
730
731 // Filter stayed the same, return input.
732 if (filter == new_filter) {
733 return javaCompilerFilter;
734 }
735
736 // Create a new string object and return.
737 std::string new_filter_str = CompilerFilter::NameOfFilter(new_filter);
738 return env->NewStringUTF(new_filter_str.c_str());
739}
740
Jeff Hao90671be2016-04-22 14:00:06 -0700741static jboolean DexFile_isBackedByOatFile(JNIEnv* env, jclass, jobject cookie) {
742 const OatFile* oat_file = nullptr;
743 std::vector<const DexFile*> dex_files;
744 if (!ConvertJavaArrayToDexFiles(env, cookie, /*out */ dex_files, /* out */ oat_file)) {
745 DCHECK(env->ExceptionCheck());
746 return false;
747 }
748 return oat_file != nullptr;
749}
750
Calin Juravle367b9d82017-05-15 18:18:39 -0700751static jobjectArray DexFile_getDexFileOutputPaths(JNIEnv* env,
Philip Cuadrab55ad7c2016-07-12 16:37:40 -0700752 jclass,
753 jstring javaFilename,
754 jstring javaInstructionSet) {
755 ScopedUtfChars filename(env, javaFilename);
756 if (env->ExceptionCheck()) {
757 return nullptr;
758 }
759
760 ScopedUtfChars instruction_set(env, javaInstructionSet);
761 if (env->ExceptionCheck()) {
762 return nullptr;
763 }
764
765 const InstructionSet target_instruction_set = GetInstructionSetFromString(
766 instruction_set.c_str());
Vladimir Marko33bff252017-11-01 14:35:42 +0000767 if (target_instruction_set == InstructionSet::kNone) {
Philip Cuadrab55ad7c2016-07-12 16:37:40 -0700768 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
769 std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set.c_str()));
770 env->ThrowNew(iae.get(), message.c_str());
771 return nullptr;
772 }
773
774 OatFileAssistant oat_file_assistant(filename.c_str(),
775 target_instruction_set,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700776 /* load_executable= */ false);
Philip Cuadrab55ad7c2016-07-12 16:37:40 -0700777
778 std::unique_ptr<OatFile> best_oat_file = oat_file_assistant.GetBestOatFile();
779 if (best_oat_file == nullptr) {
780 return nullptr;
781 }
782
Calin Juravle367b9d82017-05-15 18:18:39 -0700783 std::string oat_filename = best_oat_file->GetLocation();
784 std::string vdex_filename = GetVdexFilename(best_oat_file->GetLocation());
785
786 ScopedLocalRef<jstring> jvdexFilename(env, env->NewStringUTF(vdex_filename.c_str()));
787 if (jvdexFilename.get() == nullptr) {
788 return nullptr;
789 }
790 ScopedLocalRef<jstring> joatFilename(env, env->NewStringUTF(oat_filename.c_str()));
791 if (joatFilename.get() == nullptr) {
792 return nullptr;
793 }
794
795 // Now create output array and copy the set into it.
796 jobjectArray result = env->NewObjectArray(2,
797 WellKnownClasses::java_lang_String,
798 nullptr);
799 env->SetObjectArrayElement(result, 0, jvdexFilename.get());
800 env->SetObjectArrayElement(result, 1, joatFilename.get());
801
802 return result;
Philip Cuadrab55ad7c2016-07-12 16:37:40 -0700803}
804
Richard Uhler24ed94f2017-11-16 11:54:29 +0000805static jlong DexFile_getStaticSizeOfDexFile(JNIEnv* env, jclass, jobject cookie) {
806 const OatFile* oat_file = nullptr;
807 std::vector<const DexFile*> dex_files;
808 if (!ConvertJavaArrayToDexFiles(env, cookie, /*out */ dex_files, /* out */ oat_file)) {
809 DCHECK(env->ExceptionCheck());
810 return 0;
811 }
812
813 uint64_t file_size = 0;
814 for (auto& dex_file : dex_files) {
815 if (dex_file) {
816 file_size += dex_file->GetHeader().file_size_;
817 }
818 }
819 return static_cast<jlong>(file_size);
820}
821
Nicolas Geoffray35a4f482018-05-09 14:49:54 +0100822static void DexFile_setTrusted(JNIEnv* env, jclass, jobject j_cookie) {
823 Runtime* runtime = Runtime::Current();
824 ScopedObjectAccess soa(env);
825
826 // Currently only allow this for debuggable apps.
827 if (!runtime->IsJavaDebuggable()) {
828 ThrowSecurityException("Can't exempt class, process is not debuggable.");
829 return;
830 }
831
832 std::vector<const DexFile*> dex_files;
833 const OatFile* oat_file;
834 if (!ConvertJavaArrayToDexFiles(env, j_cookie, dex_files, oat_file)) {
835 Thread::Current()->AssertPendingException();
836 return;
837 }
838
David Brazdile7681822018-12-14 16:25:33 +0000839 // Assign core platform domain as the dex files are allowed to access all the other domains.
Nicolas Geoffray35a4f482018-05-09 14:49:54 +0100840 for (const DexFile* dex_file : dex_files) {
David Brazdile7681822018-12-14 16:25:33 +0000841 const_cast<DexFile*>(dex_file)->SetHiddenapiDomain(hiddenapi::Domain::kCorePlatform);
Nicolas Geoffray35a4f482018-05-09 14:49:54 +0100842 }
843}
844
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700845static JNINativeMethod gMethods[] = {
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700846 NATIVE_METHOD(DexFile, closeDexFile, "(Ljava/lang/Object;)Z"),
Mathieu Chartier00310e02015-10-17 12:46:42 -0700847 NATIVE_METHOD(DexFile,
848 defineClassNative,
849 "(Ljava/lang/String;"
850 "Ljava/lang/ClassLoader;"
851 "Ljava/lang/Object;"
852 "Ldalvik/system/DexFile;"
853 ")Ljava/lang/Class;"),
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800854 NATIVE_METHOD(DexFile, getClassNameList, "(Ljava/lang/Object;)[Ljava/lang/String;"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700855 NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700856 NATIVE_METHOD(DexFile, getDexOptNeeded,
Calin Juravle20c46442017-09-12 00:54:26 -0700857 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ZZ)I"),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700858 NATIVE_METHOD(DexFile, openDexFileNative,
Mathieu Chartier689a7002015-11-20 10:29:42 -0800859 "(Ljava/lang/String;"
860 "Ljava/lang/String;"
861 "I"
862 "Ljava/lang/ClassLoader;"
863 "[Ldalvik/system/DexPathList$Element;"
864 ")Ljava/lang/Object;"),
Alex Lightea9465e2017-02-16 15:38:35 -0800865 NATIVE_METHOD(DexFile, createCookieWithDirectBuffer,
866 "(Ljava/nio/ByteBuffer;II)Ljava/lang/Object;"),
867 NATIVE_METHOD(DexFile, createCookieWithArray, "([BII)Ljava/lang/Object;"),
Andreas Gampec38be812016-03-23 15:03:46 -0700868 NATIVE_METHOD(DexFile, isValidCompilerFilter, "(Ljava/lang/String;)Z"),
869 NATIVE_METHOD(DexFile, isProfileGuidedCompilerFilter, "(Ljava/lang/String;)Z"),
Andreas Gampe86a785d2016-03-30 17:19:48 -0700870 NATIVE_METHOD(DexFile,
871 getNonProfileGuidedCompilerFilter,
872 "(Ljava/lang/String;)Ljava/lang/String;"),
Nicolas Geoffray741d4262017-05-03 13:08:36 +0100873 NATIVE_METHOD(DexFile,
874 getSafeModeCompilerFilter,
875 "(Ljava/lang/String;)Ljava/lang/String;"),
Jeff Hao90671be2016-04-22 14:00:06 -0700876 NATIVE_METHOD(DexFile, isBackedByOatFile, "(Ljava/lang/Object;)Z"),
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100877 NATIVE_METHOD(DexFile, getDexFileStatus,
Philip Cuadrab55ad7c2016-07-12 16:37:40 -0700878 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
Calin Juravle367b9d82017-05-15 18:18:39 -0700879 NATIVE_METHOD(DexFile, getDexFileOutputPaths,
Richard Uhler24ed94f2017-11-16 11:54:29 +0000880 "(Ljava/lang/String;Ljava/lang/String;)[Ljava/lang/String;"),
Calin Juravle5f9a8012018-02-12 20:27:46 -0800881 NATIVE_METHOD(DexFile, getStaticSizeOfDexFile, "(Ljava/lang/Object;)J"),
882 NATIVE_METHOD(DexFile, getDexFileOptimizationStatus,
Nicolas Geoffray35a4f482018-05-09 14:49:54 +0100883 "(Ljava/lang/String;Ljava/lang/String;)[Ljava/lang/String;"),
884 NATIVE_METHOD(DexFile, setTrusted, "(Ljava/lang/Object;)V")
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700885};
886
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700887void register_dalvik_system_DexFile(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700888 REGISTER_NATIVE_METHODS("dalvik/system/DexFile");
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700889}
890
891} // namespace art