blob: 6059b89a76f60c90453da197ee8f61f216f2d44f [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
David Sehr891a50e2017-10-27 17:01:07 -070023#include "base/file_utils.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080024#include "base/logging.h"
Andreas Gampe833a4852014-05-21 18:46:59 -070025#include "base/stl_util.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070026#include "class_linker.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080027#include "common_throws.h"
Andreas Gampec38be812016-03-23 15:03:46 -070028#include "compiler_filter.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070029#include "dex_file-inl.h"
Mathieu Chartier79c87da2017-10-10 11:54:29 -070030#include "dex_file_loader.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070031#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032#include "mirror/class_loader.h"
Ian Rogers05f30572013-02-20 12:13:11 -080033#include "mirror/object-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "mirror/string.h"
Andreas Gampe87583b32017-05-25 11:22:18 -070035#include "native_util.h"
Steven Morelande431e272017-07-18 16:53:49 -070036#include "nativehelper/jni_macros.h"
Andreas Gampe373a9b52017-10-18 09:01:57 -070037#include "nativehelper/scoped_local_ref.h"
38#include "nativehelper/scoped_utf_chars.h"
Narayan Kamath8943c1d2016-05-02 13:14:48 +010039#include "oat_file.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080040#include "oat_file_assistant.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070041#include "oat_file_manager.h"
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -070042#include "os.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070043#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070044#include "scoped_thread_state_change-inl.h"
Calin Juravlebb0b53f2014-05-23 17:33:29 +010045#include "utils.h"
Ian Rogersdd157d72014-05-15 14:47:50 -070046#include "well_known_classes.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070047#include "zip_archive.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070048
49namespace art {
50
Andreas Gampe46ee31b2016-12-14 10:11:49 -080051using android::base::StringPrintf;
52
Mathieu Chartiere58991b2015-10-13 07:59:34 -070053static bool ConvertJavaArrayToDexFiles(
54 JNIEnv* env,
55 jobject arrayObject,
56 /*out*/ std::vector<const DexFile*>& dex_files,
57 /*out*/ const OatFile*& oat_file) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -080058 jarray array = reinterpret_cast<jarray>(arrayObject);
59
60 jsize array_size = env->GetArrayLength(array);
61 if (env->ExceptionCheck() == JNI_TRUE) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -070062 return false;
Andreas Gampe324b9bb2015-02-23 16:33:22 -080063 }
64
65 // TODO: Optimize. On 32bit we can use an int array.
66 jboolean is_long_data_copied;
67 jlong* long_data = env->GetLongArrayElements(reinterpret_cast<jlongArray>(array),
68 &is_long_data_copied);
69 if (env->ExceptionCheck() == JNI_TRUE) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -070070 return false;
Andreas Gampe324b9bb2015-02-23 16:33:22 -080071 }
72
Mathieu Chartiere58991b2015-10-13 07:59:34 -070073 oat_file = reinterpret_cast<const OatFile*>(static_cast<uintptr_t>(long_data[kOatFileIndex]));
74 dex_files.reserve(array_size - 1);
75 for (jsize i = kDexFileIndexStart; i < array_size; ++i) {
76 dex_files.push_back(reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(long_data[i])));
Andreas Gampe324b9bb2015-02-23 16:33:22 -080077 }
78
79 env->ReleaseLongArrayElements(reinterpret_cast<jlongArray>(array), long_data, JNI_ABORT);
Mathieu Chartiere58991b2015-10-13 07:59:34 -070080 return env->ExceptionCheck() != JNI_TRUE;
Andreas Gampe324b9bb2015-02-23 16:33:22 -080081}
82
Mathieu Chartiere58991b2015-10-13 07:59:34 -070083static jlongArray ConvertDexFilesToJavaArray(JNIEnv* env,
84 const OatFile* oat_file,
85 std::vector<std::unique_ptr<const DexFile>>& vec) {
86 // Add one for the oat file.
Mathieu Chartier80b37b72015-10-12 18:13:39 -070087 jlongArray long_array = env->NewLongArray(static_cast<jsize>(kDexFileIndexStart + vec.size()));
Andreas Gampe324b9bb2015-02-23 16:33:22 -080088 if (env->ExceptionCheck() == JNI_TRUE) {
89 return nullptr;
90 }
91
92 jboolean is_long_data_copied;
93 jlong* long_data = env->GetLongArrayElements(long_array, &is_long_data_copied);
94 if (env->ExceptionCheck() == JNI_TRUE) {
95 return nullptr;
96 }
97
Mathieu Chartiere58991b2015-10-13 07:59:34 -070098 long_data[kOatFileIndex] = reinterpret_cast<uintptr_t>(oat_file);
99 for (size_t i = 0; i < vec.size(); ++i) {
100 long_data[kDexFileIndexStart + i] = reinterpret_cast<uintptr_t>(vec[i].get());
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800101 }
102
103 env->ReleaseLongArrayElements(long_array, long_data, 0);
104 if (env->ExceptionCheck() == JNI_TRUE) {
105 return nullptr;
106 }
107
108 // Now release all the unique_ptrs.
109 for (auto& dex_file : vec) {
110 dex_file.release();
111 }
112
113 return long_array;
114}
115
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700116// A smart pointer that provides read-only access to a Java string's UTF chars.
117// Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if
118// passed a null jstring. The correct idiom is:
119//
120// NullableScopedUtfChars name(env, javaName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -0700121// if (env->ExceptionCheck()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700122// return null;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700123// }
124// // ... use name.c_str()
125//
126// TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option.
127class NullableScopedUtfChars {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800128 public:
129 NullableScopedUtfChars(JNIEnv* env, jstring s) : mEnv(env), mString(s) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700130 mUtfChars = (s != nullptr) ? env->GetStringUTFChars(s, nullptr) : nullptr;
Elliott Hughesba8eee12012-01-24 20:25:24 -0800131 }
132
133 ~NullableScopedUtfChars() {
134 if (mUtfChars) {
135 mEnv->ReleaseStringUTFChars(mString, mUtfChars);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700136 }
Elliott Hughesba8eee12012-01-24 20:25:24 -0800137 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700138
Elliott Hughesba8eee12012-01-24 20:25:24 -0800139 const char* c_str() const {
140 return mUtfChars;
141 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700142
Elliott Hughesba8eee12012-01-24 20:25:24 -0800143 size_t size() const {
144 return strlen(mUtfChars);
145 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700146
Elliott Hughesba8eee12012-01-24 20:25:24 -0800147 // Element access.
148 const char& operator[](size_t n) const {
149 return mUtfChars[n];
150 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700151
Elliott Hughesba8eee12012-01-24 20:25:24 -0800152 private:
153 JNIEnv* mEnv;
154 jstring mString;
155 const char* mUtfChars;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700156
Elliott Hughesba8eee12012-01-24 20:25:24 -0800157 // Disallow copy and assignment.
158 NullableScopedUtfChars(const NullableScopedUtfChars&);
159 void operator=(const NullableScopedUtfChars&);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700160};
161
Alex Lightea9465e2017-02-16 15:38:35 -0800162static std::unique_ptr<MemMap> AllocateDexMemoryMap(JNIEnv* env, jint start, jint end) {
163 if (end <= start) {
164 ScopedObjectAccess soa(env);
165 ThrowWrappedIOException("Bad range");
166 return nullptr;
167 }
168
169 std::string error_message;
170 size_t length = static_cast<size_t>(end - start);
171 std::unique_ptr<MemMap> dex_mem_map(MemMap::MapAnonymous("DEX data",
172 nullptr,
173 length,
174 PROT_READ | PROT_WRITE,
175 /* low_4gb */ false,
176 /* reuse */ false,
177 &error_message));
178 if (dex_mem_map == nullptr) {
179 ScopedObjectAccess soa(env);
180 ThrowWrappedIOException("%s", error_message.c_str());
181 }
182 return dex_mem_map;
183}
184
185static const DexFile* CreateDexFile(JNIEnv* env, std::unique_ptr<MemMap> dex_mem_map) {
186 std::string location = StringPrintf("Anonymous-DexFile@%p-%p",
187 dex_mem_map->Begin(),
188 dex_mem_map->End());
189 std::string error_message;
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700190 std::unique_ptr<const DexFile> dex_file(DexFileLoader::Open(location,
191 0,
192 std::move(dex_mem_map),
193 /* verify */ true,
194 /* verify_location */ true,
195 &error_message));
Alex Lightea9465e2017-02-16 15:38:35 -0800196 if (dex_file == nullptr) {
197 ScopedObjectAccess soa(env);
198 ThrowWrappedIOException("%s", error_message.c_str());
199 return nullptr;
200 }
201
202 if (!dex_file->DisableWrite()) {
203 ScopedObjectAccess soa(env);
204 ThrowWrappedIOException("Failed to make dex file read-only");
205 return nullptr;
206 }
207
208 return dex_file.release();
209}
210
211static jobject CreateSingleDexFileCookie(JNIEnv* env, std::unique_ptr<MemMap> data) {
212 std::unique_ptr<const DexFile> dex_file(CreateDexFile(env, std::move(data)));
213 if (dex_file.get() == nullptr) {
214 DCHECK(env->ExceptionCheck());
215 return nullptr;
216 }
217 std::vector<std::unique_ptr<const DexFile>> dex_files;
218 dex_files.push_back(std::move(dex_file));
219 return ConvertDexFilesToJavaArray(env, nullptr, dex_files);
220}
221
222static jobject DexFile_createCookieWithDirectBuffer(JNIEnv* env,
223 jclass,
224 jobject buffer,
225 jint start,
226 jint end) {
227 uint8_t* base_address = reinterpret_cast<uint8_t*>(env->GetDirectBufferAddress(buffer));
228 if (base_address == nullptr) {
229 ScopedObjectAccess soa(env);
230 ThrowWrappedIOException("dexFileBuffer not direct");
231 return 0;
232 }
233
234 std::unique_ptr<MemMap> dex_mem_map(AllocateDexMemoryMap(env, start, end));
235 if (dex_mem_map == nullptr) {
236 DCHECK(Thread::Current()->IsExceptionPending());
237 return 0;
238 }
239
240 size_t length = static_cast<size_t>(end - start);
241 memcpy(dex_mem_map->Begin(), base_address, length);
242 return CreateSingleDexFileCookie(env, std::move(dex_mem_map));
243}
244
245static jobject DexFile_createCookieWithArray(JNIEnv* env,
246 jclass,
247 jbyteArray buffer,
248 jint start,
249 jint end) {
250 std::unique_ptr<MemMap> dex_mem_map(AllocateDexMemoryMap(env, start, end));
251 if (dex_mem_map == nullptr) {
252 DCHECK(Thread::Current()->IsExceptionPending());
253 return 0;
254 }
255
256 auto destination = reinterpret_cast<jbyte*>(dex_mem_map.get()->Begin());
257 env->GetByteArrayRegion(buffer, start, end - start, destination);
258 return CreateSingleDexFileCookie(env, std::move(dex_mem_map));
259}
260
Calin Juravle1f7079b2017-04-18 21:25:37 -0700261// TODO(calin): clean up the unused parameters (here and in libcore).
Mathieu Chartierb190d942015-11-12 10:00:58 -0800262static jobject DexFile_openDexFileNative(JNIEnv* env,
263 jclass,
264 jstring javaSourceName,
Calin Juravle1f7079b2017-04-18 21:25:37 -0700265 jstring javaOutputName ATTRIBUTE_UNUSED,
Mathieu Chartierb190d942015-11-12 10:00:58 -0800266 jint flags ATTRIBUTE_UNUSED,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800267 jobject class_loader,
268 jobjectArray dex_elements) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700269 ScopedUtfChars sourceName(env, javaSourceName);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700270 if (sourceName.c_str() == nullptr) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700271 return 0;
272 }
Calin Juravle1f7079b2017-04-18 21:25:37 -0700273
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700274 Runtime* const runtime = Runtime::Current();
275 ClassLinker* linker = runtime->GetClassLinker();
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800276 std::vector<std::unique_ptr<const DexFile>> dex_files;
Andreas Gampe833a4852014-05-21 18:46:59 -0700277 std::vector<std::string> error_msgs;
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700278 const OatFile* oat_file = nullptr;
Andreas Gampe833a4852014-05-21 18:46:59 -0700279
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700280 dex_files = runtime->GetOatFileManager().OpenDexFilesFromOat(sourceName.c_str(),
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800281 class_loader,
282 dex_elements,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700283 /*out*/ &oat_file,
284 /*out*/ &error_msgs);
Andreas Gampe833a4852014-05-21 18:46:59 -0700285
Richard Uhler66d874d2015-01-15 09:37:19 -0800286 if (!dex_files.empty()) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700287 jlongArray array = ConvertDexFilesToJavaArray(env, oat_file, dex_files);
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800288 if (array == nullptr) {
289 ScopedObjectAccess soa(env);
290 for (auto& dex_file : dex_files) {
Vladimir Markocd556b02017-02-03 11:47:34 +0000291 if (linker->IsDexFileRegistered(soa.Self(), *dex_file)) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800292 dex_file.release();
293 }
294 }
295 }
296 return array;
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700297 } else {
Vladimir Marko60836d52014-01-16 15:53:38 +0000298 ScopedObjectAccess soa(env);
Andreas Gampe329d1882014-04-08 10:32:19 -0700299 CHECK(!error_msgs.empty());
300 // The most important message is at the end. So set up nesting by going forward, which will
301 // wrap the existing exception as a cause for the following one.
302 auto it = error_msgs.begin();
303 auto itEnd = error_msgs.end();
304 for ( ; it != itEnd; ++it) {
305 ThrowWrappedIOException("%s", it->c_str());
306 }
307
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800308 return nullptr;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700309 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700310}
311
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700312static jboolean DexFile_closeDexFile(JNIEnv* env, jclass, jobject cookie) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700313 std::vector<const DexFile*> dex_files;
314 const OatFile* oat_file;
315 if (!ConvertJavaArrayToDexFiles(env, cookie, dex_files, oat_file)) {
316 Thread::Current()->AssertPendingException();
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700317 return JNI_FALSE;
318 }
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700319 Runtime* const runtime = Runtime::Current();
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700320 bool all_deleted = true;
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700321 {
322 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700323 ObjPtr<mirror::Object> dex_files_object = soa.Decode<mirror::Object>(cookie);
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700324 ObjPtr<mirror::LongArray> long_dex_files = dex_files_object->AsLongArray();
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700325 // Delete dex files associated with this dalvik.system.DexFile since there should not be running
326 // code using it. dex_files is a vector due to multidex.
327 ClassLinker* const class_linker = runtime->GetClassLinker();
328 int32_t i = kDexFileIndexStart; // Oat file is at index 0.
329 for (const DexFile* dex_file : dex_files) {
330 if (dex_file != nullptr) {
331 // Only delete the dex file if the dex cache is not found to prevent runtime crashes if there
332 // are calls to DexFile.close while the ART DexFile is still in use.
Vladimir Markocd556b02017-02-03 11:47:34 +0000333 if (!class_linker->IsDexFileRegistered(soa.Self(), *dex_file)) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700334 // Clear the element in the array so that we can call close again.
335 long_dex_files->Set(i, 0);
336 delete dex_file;
337 } else {
338 all_deleted = false;
339 }
340 }
341 ++i;
Andreas Gampe833a4852014-05-21 18:46:59 -0700342 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700343 }
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700344
Mathieu Chartierfdccbd42015-10-14 10:58:41 -0700345 // oat_file can be null if we are running without dex2oat.
346 if (all_deleted && oat_file != nullptr) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700347 // If all of the dex files are no longer in use we can unmap the corresponding oat file.
348 VLOG(class_linker) << "Unregistering " << oat_file;
349 runtime->GetOatFileManager().UnRegisterAndDeleteOatFile(oat_file);
350 }
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700351 return all_deleted ? JNI_TRUE : JNI_FALSE;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700352}
353
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700354static jclass DexFile_defineClassNative(JNIEnv* env,
355 jclass,
356 jstring javaName,
357 jobject javaLoader,
Mathieu Chartier00310e02015-10-17 12:46:42 -0700358 jobject cookie,
359 jobject dexFile) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700360 std::vector<const DexFile*> dex_files;
361 const OatFile* oat_file;
362 if (!ConvertJavaArrayToDexFiles(env, cookie, /*out*/ dex_files, /*out*/ oat_file)) {
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700363 VLOG(class_linker) << "Failed to find dex_file";
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800364 DCHECK(env->ExceptionCheck());
365 return nullptr;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700366 }
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800367
Brian Carlstromdf143242011-10-10 18:05:34 -0700368 ScopedUtfChars class_name(env, javaName);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700369 if (class_name.c_str() == nullptr) {
Brian Carlstrom2e450bf2013-09-06 15:39:46 -0700370 VLOG(class_linker) << "Failed to find class_name";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700371 return nullptr;
Brian Carlstromdf143242011-10-10 18:05:34 -0700372 }
Elliott Hughes95572412011-12-13 18:14:20 -0800373 const std::string descriptor(DotToDescriptor(class_name.c_str()));
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800374 const size_t hash(ComputeModifiedUtf8Hash(descriptor.c_str()));
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700375 for (auto& dex_file : dex_files) {
David Sehr9aa352e2016-09-15 18:13:52 -0700376 const DexFile::ClassDef* dex_class_def =
377 OatDexFile::FindClassDef(*dex_file, descriptor.c_str(), hash);
Andreas Gampe833a4852014-05-21 18:46:59 -0700378 if (dex_class_def != nullptr) {
379 ScopedObjectAccess soa(env);
380 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Andreas Gampe833a4852014-05-21 18:46:59 -0700381 StackHandleScope<1> hs(soa.Self());
382 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700383 hs.NewHandle(soa.Decode<mirror::ClassLoader>(javaLoader)));
Vladimir Markocd556b02017-02-03 11:47:34 +0000384 ObjPtr<mirror::DexCache> dex_cache =
385 class_linker->RegisterDexFile(*dex_file, class_loader.Get());
386 if (dex_cache == nullptr) {
387 // OOME or InternalError (dexFile already registered with a different class loader).
388 soa.Self()->AssertPendingException();
389 return nullptr;
390 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700391 ObjPtr<mirror::Class> result = class_linker->DefineClass(soa.Self(),
392 descriptor.c_str(),
393 hash,
394 class_loader,
395 *dex_file,
396 *dex_class_def);
Mathieu Chartier00310e02015-10-17 12:46:42 -0700397 // Add the used dex file. This only required for the DexFile.loadClass API since normal
398 // class loaders already keep their dex files live.
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700399 class_linker->InsertDexFileInToClassLoader(soa.Decode<mirror::Object>(dexFile),
Mathieu Chartier00310e02015-10-17 12:46:42 -0700400 class_loader.Get());
Andreas Gampe833a4852014-05-21 18:46:59 -0700401 if (result != nullptr) {
Brian Carlstrom667ab7c2014-10-16 19:12:28 -0700402 VLOG(class_linker) << "DexFile_defineClassNative returning " << result
403 << " for " << class_name.c_str();
Andreas Gampe833a4852014-05-21 18:46:59 -0700404 return soa.AddLocalReference<jclass>(result);
405 }
406 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700407 }
Brian Carlstrom667ab7c2014-10-16 19:12:28 -0700408 VLOG(class_linker) << "Failed to find dex_class_def " << class_name.c_str();
Andreas Gampe833a4852014-05-21 18:46:59 -0700409 return nullptr;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700410}
411
Andreas Gampe833a4852014-05-21 18:46:59 -0700412// Needed as a compare functor for sets of const char
413struct CharPointerComparator {
414 bool operator()(const char *str1, const char *str2) const {
415 return strcmp(str1, str2) < 0;
416 }
417};
418
419// Note: this can be an expensive call, as we sort out duplicates in MultiDex files.
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800420static jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jobject cookie) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700421 const OatFile* oat_file = nullptr;
422 std::vector<const DexFile*> dex_files;
423 if (!ConvertJavaArrayToDexFiles(env, cookie, /*out */ dex_files, /* out */ oat_file)) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800424 DCHECK(env->ExceptionCheck());
425 return nullptr;
426 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700427
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800428 // Push all class descriptors into a set. Use set instead of unordered_set as we want to
429 // retrieve all in the end.
430 std::set<const char*, CharPointerComparator> descriptors;
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700431 for (auto& dex_file : dex_files) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800432 for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
433 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
434 const char* descriptor = dex_file->GetClassDescriptor(class_def);
435 descriptors.insert(descriptor);
Andreas Gampe833a4852014-05-21 18:46:59 -0700436 }
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800437 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700438
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800439 // Now create output array and copy the set into it.
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700440 jobjectArray result = env->NewObjectArray(descriptors.size(),
441 WellKnownClasses::java_lang_String,
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800442 nullptr);
443 if (result != nullptr) {
444 auto it = descriptors.begin();
445 auto it_end = descriptors.end();
446 jsize i = 0;
447 for (; it != it_end; it++, ++i) {
448 std::string descriptor(DescriptorToDot(*it));
449 ScopedLocalRef<jstring> jdescriptor(env, env->NewStringUTF(descriptor.c_str()));
450 if (jdescriptor.get() == nullptr) {
451 return nullptr;
Ian Rogersdd157d72014-05-15 14:47:50 -0700452 }
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800453 env->SetObjectArrayElement(result, i, jdescriptor.get());
Ian Rogersdd157d72014-05-15 14:47:50 -0700454 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700455 }
Ian Rogersdd157d72014-05-15 14:47:50 -0700456 return result;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700457}
458
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700459static jint GetDexOptNeeded(JNIEnv* env,
460 const char* filename,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700461 const char* instruction_set,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000462 const char* compiler_filter_name,
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700463 bool profile_changed,
464 bool downgrade) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100465 if ((filename == nullptr) || !OS::FileExists(filename)) {
Richard Uhler95abd042015-03-24 09:51:28 -0700466 LOG(ERROR) << "DexFile_getDexOptNeeded file '" << filename << "' does not exist";
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700467 ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException"));
Narayan Kamath11d9f062014-04-23 20:24:57 +0100468 const char* message = (filename == nullptr) ? "<empty file name>" : filename;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700469 env->ThrowNew(fnfe.get(), message);
Calin Juravleb077e152016-02-18 18:47:37 +0000470 return -1;
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700471 }
472
Alex Light6e183f22014-07-18 14:57:04 -0700473 const InstructionSet target_instruction_set = GetInstructionSetFromString(instruction_set);
Andreas Gampe20c89302014-08-19 17:28:06 -0700474 if (target_instruction_set == kNone) {
475 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
476 std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set));
477 env->ThrowNew(iae.get(), message.c_str());
Calin Juravleb077e152016-02-18 18:47:37 +0000478 return -1;
Andreas Gampe20c89302014-08-19 17:28:06 -0700479 }
Alex Light6e183f22014-07-18 14:57:04 -0700480
Andreas Gampe29d38e72016-03-23 15:31:51 +0000481 CompilerFilter::Filter filter;
482 if (!CompilerFilter::ParseCompilerFilter(compiler_filter_name, &filter)) {
483 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
484 std::string message(StringPrintf("Compiler filter %s is invalid.", compiler_filter_name));
485 env->ThrowNew(iae.get(), message.c_str());
486 return -1;
487 }
488
Richard Uhler66d874d2015-01-15 09:37:19 -0800489 // TODO: Verify the dex location is well formed, and throw an IOException if
490 // not?
Andreas Gampe29d38e72016-03-23 15:31:51 +0000491
Richard Uhlerd1472a22016-04-15 15:18:56 -0700492 OatFileAssistant oat_file_assistant(filename, target_instruction_set, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800493
494 // Always treat elements of the bootclasspath as up-to-date.
495 if (oat_file_assistant.IsInBootClassPath()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700496 return OatFileAssistant::kNoDexOptNeeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800497 }
Calin Juravle44e5efa2017-09-12 00:54:26 -0700498
499 // TODO(calin): Extend DexFile.getDexOptNeeded to accept the class loader context. b/62269291.
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700500 return oat_file_assistant.GetDexOptNeeded(filter, profile_changed, downgrade);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700501}
502
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100503static jstring DexFile_getDexFileStatus(JNIEnv* env,
504 jclass,
505 jstring javaFilename,
506 jstring javaInstructionSet) {
507 ScopedUtfChars filename(env, javaFilename);
508 if (env->ExceptionCheck()) {
509 return nullptr;
510 }
511
512 ScopedUtfChars instruction_set(env, javaInstructionSet);
513 if (env->ExceptionCheck()) {
514 return nullptr;
515 }
516
517 const InstructionSet target_instruction_set = GetInstructionSetFromString(
518 instruction_set.c_str());
519 if (target_instruction_set == kNone) {
520 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
521 std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set.c_str()));
522 env->ThrowNew(iae.get(), message.c_str());
523 return nullptr;
524 }
525
526 OatFileAssistant oat_file_assistant(filename.c_str(), target_instruction_set,
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100527 false /* load_executable */);
Richard Uhler46cc64f2016-11-14 14:53:55 +0000528 return env->NewStringUTF(oat_file_assistant.GetStatusDump().c_str());
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100529}
530
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700531static jint DexFile_getDexOptNeeded(JNIEnv* env,
532 jclass,
533 jstring javaFilename,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700534 jstring javaInstructionSet,
Andreas Gampec38be812016-03-23 15:03:46 -0700535 jstring javaTargetCompilerFilter,
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700536 jboolean newProfile,
537 jboolean downgrade) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100538 ScopedUtfChars filename(env, javaFilename);
Andreas Gampe20c89302014-08-19 17:28:06 -0700539 if (env->ExceptionCheck()) {
Calin Juravleb077e152016-02-18 18:47:37 +0000540 return -1;
Andreas Gampe20c89302014-08-19 17:28:06 -0700541 }
542
Narayan Kamath11d9f062014-04-23 20:24:57 +0100543 ScopedUtfChars instruction_set(env, javaInstructionSet);
Andreas Gampe20c89302014-08-19 17:28:06 -0700544 if (env->ExceptionCheck()) {
Calin Juravleb077e152016-02-18 18:47:37 +0000545 return -1;
Andreas Gampe20c89302014-08-19 17:28:06 -0700546 }
Narayan Kamath11d9f062014-04-23 20:24:57 +0100547
Andreas Gampec38be812016-03-23 15:03:46 -0700548 ScopedUtfChars target_compiler_filter(env, javaTargetCompilerFilter);
549 if (env->ExceptionCheck()) {
550 return -1;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000551 }
Andreas Gampec38be812016-03-23 15:03:46 -0700552
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700553 return GetDexOptNeeded(env,
554 filename.c_str(),
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700555 instruction_set.c_str(),
Andreas Gampec38be812016-03-23 15:03:46 -0700556 target_compiler_filter.c_str(),
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700557 newProfile == JNI_TRUE,
558 downgrade == JNI_TRUE);
Narayan Kamath11d9f062014-04-23 20:24:57 +0100559}
560
Calin Juravleb077e152016-02-18 18:47:37 +0000561// public API
Narayan Kamath11d9f062014-04-23 20:24:57 +0100562static jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
Richard Uhler06e3f4f2016-05-24 15:42:37 -0700563 ScopedUtfChars filename_utf(env, javaFilename);
564 if (env->ExceptionCheck()) {
565 return JNI_FALSE;
566 }
567
568 const char* filename = filename_utf.c_str();
569 if ((filename == nullptr) || !OS::FileExists(filename)) {
570 LOG(ERROR) << "DexFile_isDexOptNeeded file '" << filename << "' does not exist";
571 ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException"));
572 const char* message = (filename == nullptr) ? "<empty file name>" : filename;
573 env->ThrowNew(fnfe.get(), message);
574 return JNI_FALSE;
575 }
576
Richard Uhlerd1472a22016-04-15 15:18:56 -0700577 OatFileAssistant oat_file_assistant(filename, kRuntimeISA, false);
Richard Uhler06e3f4f2016-05-24 15:42:37 -0700578 return oat_file_assistant.IsUpToDate() ? JNI_FALSE : JNI_TRUE;
Dave Allison39c3bfb2014-01-28 18:33:52 -0800579}
580
Andreas Gampec38be812016-03-23 15:03:46 -0700581static jboolean DexFile_isValidCompilerFilter(JNIEnv* env,
582 jclass javeDexFileClass ATTRIBUTE_UNUSED,
583 jstring javaCompilerFilter) {
584 ScopedUtfChars compiler_filter(env, javaCompilerFilter);
585 if (env->ExceptionCheck()) {
586 return -1;
587 }
588
589 CompilerFilter::Filter filter;
590 return CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)
591 ? JNI_TRUE : JNI_FALSE;
592}
593
594static jboolean DexFile_isProfileGuidedCompilerFilter(JNIEnv* env,
595 jclass javeDexFileClass ATTRIBUTE_UNUSED,
596 jstring javaCompilerFilter) {
597 ScopedUtfChars compiler_filter(env, javaCompilerFilter);
598 if (env->ExceptionCheck()) {
599 return -1;
600 }
601
602 CompilerFilter::Filter filter;
603 if (!CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)) {
604 return JNI_FALSE;
605 }
606 return CompilerFilter::DependsOnProfile(filter) ? JNI_TRUE : JNI_FALSE;
607}
608
Andreas Gampe86a785d2016-03-30 17:19:48 -0700609static jstring DexFile_getNonProfileGuidedCompilerFilter(JNIEnv* env,
610 jclass javeDexFileClass ATTRIBUTE_UNUSED,
611 jstring javaCompilerFilter) {
612 ScopedUtfChars compiler_filter(env, javaCompilerFilter);
613 if (env->ExceptionCheck()) {
614 return nullptr;
615 }
616
617 CompilerFilter::Filter filter;
618 if (!CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)) {
619 return javaCompilerFilter;
620 }
621
622 CompilerFilter::Filter new_filter = CompilerFilter::GetNonProfileDependentFilterFrom(filter);
623
624 // Filter stayed the same, return input.
625 if (filter == new_filter) {
626 return javaCompilerFilter;
627 }
628
629 // Create a new string object and return.
630 std::string new_filter_str = CompilerFilter::NameOfFilter(new_filter);
631 return env->NewStringUTF(new_filter_str.c_str());
632}
633
Nicolas Geoffray741d4262017-05-03 13:08:36 +0100634static jstring DexFile_getSafeModeCompilerFilter(JNIEnv* env,
635 jclass javeDexFileClass ATTRIBUTE_UNUSED,
636 jstring javaCompilerFilter) {
637 ScopedUtfChars compiler_filter(env, javaCompilerFilter);
638 if (env->ExceptionCheck()) {
639 return nullptr;
640 }
641
642 CompilerFilter::Filter filter;
643 if (!CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)) {
644 return javaCompilerFilter;
645 }
646
647 CompilerFilter::Filter new_filter = CompilerFilter::GetSafeModeFilterFrom(filter);
648
649 // Filter stayed the same, return input.
650 if (filter == new_filter) {
651 return javaCompilerFilter;
652 }
653
654 // Create a new string object and return.
655 std::string new_filter_str = CompilerFilter::NameOfFilter(new_filter);
656 return env->NewStringUTF(new_filter_str.c_str());
657}
658
Jeff Hao90671be2016-04-22 14:00:06 -0700659static jboolean DexFile_isBackedByOatFile(JNIEnv* env, jclass, jobject cookie) {
660 const OatFile* oat_file = nullptr;
661 std::vector<const DexFile*> dex_files;
662 if (!ConvertJavaArrayToDexFiles(env, cookie, /*out */ dex_files, /* out */ oat_file)) {
663 DCHECK(env->ExceptionCheck());
664 return false;
665 }
666 return oat_file != nullptr;
667}
668
Calin Juravle367b9d82017-05-15 18:18:39 -0700669static jobjectArray DexFile_getDexFileOutputPaths(JNIEnv* env,
Philip Cuadrab55ad7c2016-07-12 16:37:40 -0700670 jclass,
671 jstring javaFilename,
672 jstring javaInstructionSet) {
673 ScopedUtfChars filename(env, javaFilename);
674 if (env->ExceptionCheck()) {
675 return nullptr;
676 }
677
678 ScopedUtfChars instruction_set(env, javaInstructionSet);
679 if (env->ExceptionCheck()) {
680 return nullptr;
681 }
682
683 const InstructionSet target_instruction_set = GetInstructionSetFromString(
684 instruction_set.c_str());
685 if (target_instruction_set == kNone) {
686 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
687 std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set.c_str()));
688 env->ThrowNew(iae.get(), message.c_str());
689 return nullptr;
690 }
691
692 OatFileAssistant oat_file_assistant(filename.c_str(),
693 target_instruction_set,
Philip Cuadrab55ad7c2016-07-12 16:37:40 -0700694 false /* load_executable */);
695
696 std::unique_ptr<OatFile> best_oat_file = oat_file_assistant.GetBestOatFile();
697 if (best_oat_file == nullptr) {
698 return nullptr;
699 }
700
Calin Juravle367b9d82017-05-15 18:18:39 -0700701 std::string oat_filename = best_oat_file->GetLocation();
702 std::string vdex_filename = GetVdexFilename(best_oat_file->GetLocation());
703
704 ScopedLocalRef<jstring> jvdexFilename(env, env->NewStringUTF(vdex_filename.c_str()));
705 if (jvdexFilename.get() == nullptr) {
706 return nullptr;
707 }
708 ScopedLocalRef<jstring> joatFilename(env, env->NewStringUTF(oat_filename.c_str()));
709 if (joatFilename.get() == nullptr) {
710 return nullptr;
711 }
712
713 // Now create output array and copy the set into it.
714 jobjectArray result = env->NewObjectArray(2,
715 WellKnownClasses::java_lang_String,
716 nullptr);
717 env->SetObjectArrayElement(result, 0, jvdexFilename.get());
718 env->SetObjectArrayElement(result, 1, joatFilename.get());
719
720 return result;
Philip Cuadrab55ad7c2016-07-12 16:37:40 -0700721}
722
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700723static JNINativeMethod gMethods[] = {
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700724 NATIVE_METHOD(DexFile, closeDexFile, "(Ljava/lang/Object;)Z"),
Mathieu Chartier00310e02015-10-17 12:46:42 -0700725 NATIVE_METHOD(DexFile,
726 defineClassNative,
727 "(Ljava/lang/String;"
728 "Ljava/lang/ClassLoader;"
729 "Ljava/lang/Object;"
730 "Ldalvik/system/DexFile;"
731 ")Ljava/lang/Class;"),
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800732 NATIVE_METHOD(DexFile, getClassNameList, "(Ljava/lang/Object;)[Ljava/lang/String;"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700733 NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700734 NATIVE_METHOD(DexFile, getDexOptNeeded,
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700735 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ZZ)I"),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700736 NATIVE_METHOD(DexFile, openDexFileNative,
Mathieu Chartier689a7002015-11-20 10:29:42 -0800737 "(Ljava/lang/String;"
738 "Ljava/lang/String;"
739 "I"
740 "Ljava/lang/ClassLoader;"
741 "[Ldalvik/system/DexPathList$Element;"
742 ")Ljava/lang/Object;"),
Alex Lightea9465e2017-02-16 15:38:35 -0800743 NATIVE_METHOD(DexFile, createCookieWithDirectBuffer,
744 "(Ljava/nio/ByteBuffer;II)Ljava/lang/Object;"),
745 NATIVE_METHOD(DexFile, createCookieWithArray, "([BII)Ljava/lang/Object;"),
Andreas Gampec38be812016-03-23 15:03:46 -0700746 NATIVE_METHOD(DexFile, isValidCompilerFilter, "(Ljava/lang/String;)Z"),
747 NATIVE_METHOD(DexFile, isProfileGuidedCompilerFilter, "(Ljava/lang/String;)Z"),
Andreas Gampe86a785d2016-03-30 17:19:48 -0700748 NATIVE_METHOD(DexFile,
749 getNonProfileGuidedCompilerFilter,
750 "(Ljava/lang/String;)Ljava/lang/String;"),
Nicolas Geoffray741d4262017-05-03 13:08:36 +0100751 NATIVE_METHOD(DexFile,
752 getSafeModeCompilerFilter,
753 "(Ljava/lang/String;)Ljava/lang/String;"),
Jeff Hao90671be2016-04-22 14:00:06 -0700754 NATIVE_METHOD(DexFile, isBackedByOatFile, "(Ljava/lang/Object;)Z"),
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100755 NATIVE_METHOD(DexFile, getDexFileStatus,
Philip Cuadrab55ad7c2016-07-12 16:37:40 -0700756 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
Calin Juravle367b9d82017-05-15 18:18:39 -0700757 NATIVE_METHOD(DexFile, getDexFileOutputPaths,
758 "(Ljava/lang/String;Ljava/lang/String;)[Ljava/lang/String;")
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700759};
760
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700761void register_dalvik_system_DexFile(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700762 REGISTER_NATIVE_METHODS("dalvik/system/DexFile");
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700763}
764
765} // namespace art