blob: 07dfb659724c81dee7f315c9e17c2b46f59edd65 [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"
Andreas Gampe87583b32017-05-25 11:22:18 -070033#include "native_util.h"
Steven Morelande431e272017-07-18 16:53:49 -070034#include "nativehelper/jni_macros.h"
35#include "nativehelper/ScopedLocalRef.h"
36#include "nativehelper/ScopedUtfChars.h"
Narayan Kamath8943c1d2016-05-02 13:14:48 +010037#include "oat_file.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080038#include "oat_file_assistant.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070039#include "oat_file_manager.h"
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -070040#include "os.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070041#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070042#include "scoped_thread_state_change-inl.h"
Calin Juravlebb0b53f2014-05-23 17:33:29 +010043#include "utils.h"
Ian Rogersdd157d72014-05-15 14:47:50 -070044#include "well_known_classes.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070045#include "zip_archive.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070046
47namespace art {
48
Andreas Gampe46ee31b2016-12-14 10:11:49 -080049using android::base::StringPrintf;
50
Mathieu Chartiere58991b2015-10-13 07:59:34 -070051static bool ConvertJavaArrayToDexFiles(
52 JNIEnv* env,
53 jobject arrayObject,
54 /*out*/ std::vector<const DexFile*>& dex_files,
55 /*out*/ const OatFile*& oat_file) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -080056 jarray array = reinterpret_cast<jarray>(arrayObject);
57
58 jsize array_size = env->GetArrayLength(array);
59 if (env->ExceptionCheck() == JNI_TRUE) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -070060 return false;
Andreas Gampe324b9bb2015-02-23 16:33:22 -080061 }
62
63 // TODO: Optimize. On 32bit we can use an int array.
64 jboolean is_long_data_copied;
65 jlong* long_data = env->GetLongArrayElements(reinterpret_cast<jlongArray>(array),
66 &is_long_data_copied);
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
Mathieu Chartiere58991b2015-10-13 07:59:34 -070071 oat_file = reinterpret_cast<const OatFile*>(static_cast<uintptr_t>(long_data[kOatFileIndex]));
72 dex_files.reserve(array_size - 1);
73 for (jsize i = kDexFileIndexStart; i < array_size; ++i) {
74 dex_files.push_back(reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(long_data[i])));
Andreas Gampe324b9bb2015-02-23 16:33:22 -080075 }
76
77 env->ReleaseLongArrayElements(reinterpret_cast<jlongArray>(array), long_data, JNI_ABORT);
Mathieu Chartiere58991b2015-10-13 07:59:34 -070078 return env->ExceptionCheck() != JNI_TRUE;
Andreas Gampe324b9bb2015-02-23 16:33:22 -080079}
80
Mathieu Chartiere58991b2015-10-13 07:59:34 -070081static jlongArray ConvertDexFilesToJavaArray(JNIEnv* env,
82 const OatFile* oat_file,
83 std::vector<std::unique_ptr<const DexFile>>& vec) {
84 // Add one for the oat file.
Mathieu Chartier80b37b72015-10-12 18:13:39 -070085 jlongArray long_array = env->NewLongArray(static_cast<jsize>(kDexFileIndexStart + vec.size()));
Andreas Gampe324b9bb2015-02-23 16:33:22 -080086 if (env->ExceptionCheck() == JNI_TRUE) {
87 return nullptr;
88 }
89
90 jboolean is_long_data_copied;
91 jlong* long_data = env->GetLongArrayElements(long_array, &is_long_data_copied);
92 if (env->ExceptionCheck() == JNI_TRUE) {
93 return nullptr;
94 }
95
Mathieu Chartiere58991b2015-10-13 07:59:34 -070096 long_data[kOatFileIndex] = reinterpret_cast<uintptr_t>(oat_file);
97 for (size_t i = 0; i < vec.size(); ++i) {
98 long_data[kDexFileIndexStart + i] = reinterpret_cast<uintptr_t>(vec[i].get());
Andreas Gampe324b9bb2015-02-23 16:33:22 -080099 }
100
101 env->ReleaseLongArrayElements(long_array, long_data, 0);
102 if (env->ExceptionCheck() == JNI_TRUE) {
103 return nullptr;
104 }
105
106 // Now release all the unique_ptrs.
107 for (auto& dex_file : vec) {
108 dex_file.release();
109 }
110
111 return long_array;
112}
113
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700114// A smart pointer that provides read-only access to a Java string's UTF chars.
115// Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if
116// passed a null jstring. The correct idiom is:
117//
118// NullableScopedUtfChars name(env, javaName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -0700119// if (env->ExceptionCheck()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700120// return null;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700121// }
122// // ... use name.c_str()
123//
124// TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option.
125class NullableScopedUtfChars {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800126 public:
127 NullableScopedUtfChars(JNIEnv* env, jstring s) : mEnv(env), mString(s) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700128 mUtfChars = (s != nullptr) ? env->GetStringUTFChars(s, nullptr) : nullptr;
Elliott Hughesba8eee12012-01-24 20:25:24 -0800129 }
130
131 ~NullableScopedUtfChars() {
132 if (mUtfChars) {
133 mEnv->ReleaseStringUTFChars(mString, mUtfChars);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700134 }
Elliott Hughesba8eee12012-01-24 20:25:24 -0800135 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700136
Elliott Hughesba8eee12012-01-24 20:25:24 -0800137 const char* c_str() const {
138 return mUtfChars;
139 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700140
Elliott Hughesba8eee12012-01-24 20:25:24 -0800141 size_t size() const {
142 return strlen(mUtfChars);
143 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700144
Elliott Hughesba8eee12012-01-24 20:25:24 -0800145 // Element access.
146 const char& operator[](size_t n) const {
147 return mUtfChars[n];
148 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700149
Elliott Hughesba8eee12012-01-24 20:25:24 -0800150 private:
151 JNIEnv* mEnv;
152 jstring mString;
153 const char* mUtfChars;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700154
Elliott Hughesba8eee12012-01-24 20:25:24 -0800155 // Disallow copy and assignment.
156 NullableScopedUtfChars(const NullableScopedUtfChars&);
157 void operator=(const NullableScopedUtfChars&);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700158};
159
Alex Lightea9465e2017-02-16 15:38:35 -0800160static std::unique_ptr<MemMap> AllocateDexMemoryMap(JNIEnv* env, jint start, jint end) {
161 if (end <= start) {
162 ScopedObjectAccess soa(env);
163 ThrowWrappedIOException("Bad range");
164 return nullptr;
165 }
166
167 std::string error_message;
168 size_t length = static_cast<size_t>(end - start);
169 std::unique_ptr<MemMap> dex_mem_map(MemMap::MapAnonymous("DEX data",
170 nullptr,
171 length,
172 PROT_READ | PROT_WRITE,
173 /* low_4gb */ false,
174 /* reuse */ false,
175 &error_message));
176 if (dex_mem_map == nullptr) {
177 ScopedObjectAccess soa(env);
178 ThrowWrappedIOException("%s", error_message.c_str());
179 }
180 return dex_mem_map;
181}
182
183static const DexFile* CreateDexFile(JNIEnv* env, std::unique_ptr<MemMap> dex_mem_map) {
184 std::string location = StringPrintf("Anonymous-DexFile@%p-%p",
185 dex_mem_map->Begin(),
186 dex_mem_map->End());
187 std::string error_message;
188 std::unique_ptr<const DexFile> dex_file(DexFile::Open(location,
189 0,
190 std::move(dex_mem_map),
191 /* verify */ true,
192 /* verify_location */ true,
193 &error_message));
194 if (dex_file == nullptr) {
195 ScopedObjectAccess soa(env);
196 ThrowWrappedIOException("%s", error_message.c_str());
197 return nullptr;
198 }
199
200 if (!dex_file->DisableWrite()) {
201 ScopedObjectAccess soa(env);
202 ThrowWrappedIOException("Failed to make dex file read-only");
203 return nullptr;
204 }
205
206 return dex_file.release();
207}
208
209static jobject CreateSingleDexFileCookie(JNIEnv* env, std::unique_ptr<MemMap> data) {
210 std::unique_ptr<const DexFile> dex_file(CreateDexFile(env, std::move(data)));
211 if (dex_file.get() == nullptr) {
212 DCHECK(env->ExceptionCheck());
213 return nullptr;
214 }
215 std::vector<std::unique_ptr<const DexFile>> dex_files;
216 dex_files.push_back(std::move(dex_file));
217 return ConvertDexFilesToJavaArray(env, nullptr, dex_files);
218}
219
220static jobject DexFile_createCookieWithDirectBuffer(JNIEnv* env,
221 jclass,
222 jobject buffer,
223 jint start,
224 jint end) {
225 uint8_t* base_address = reinterpret_cast<uint8_t*>(env->GetDirectBufferAddress(buffer));
226 if (base_address == nullptr) {
227 ScopedObjectAccess soa(env);
228 ThrowWrappedIOException("dexFileBuffer not direct");
229 return 0;
230 }
231
232 std::unique_ptr<MemMap> dex_mem_map(AllocateDexMemoryMap(env, start, end));
233 if (dex_mem_map == nullptr) {
234 DCHECK(Thread::Current()->IsExceptionPending());
235 return 0;
236 }
237
238 size_t length = static_cast<size_t>(end - start);
239 memcpy(dex_mem_map->Begin(), base_address, length);
240 return CreateSingleDexFileCookie(env, std::move(dex_mem_map));
241}
242
243static jobject DexFile_createCookieWithArray(JNIEnv* env,
244 jclass,
245 jbyteArray buffer,
246 jint start,
247 jint end) {
248 std::unique_ptr<MemMap> dex_mem_map(AllocateDexMemoryMap(env, start, end));
249 if (dex_mem_map == nullptr) {
250 DCHECK(Thread::Current()->IsExceptionPending());
251 return 0;
252 }
253
254 auto destination = reinterpret_cast<jbyte*>(dex_mem_map.get()->Begin());
255 env->GetByteArrayRegion(buffer, start, end - start, destination);
256 return CreateSingleDexFileCookie(env, std::move(dex_mem_map));
257}
258
Calin Juravle1f7079b2017-04-18 21:25:37 -0700259// TODO(calin): clean up the unused parameters (here and in libcore).
Mathieu Chartierb190d942015-11-12 10:00:58 -0800260static jobject DexFile_openDexFileNative(JNIEnv* env,
261 jclass,
262 jstring javaSourceName,
Calin Juravle1f7079b2017-04-18 21:25:37 -0700263 jstring javaOutputName ATTRIBUTE_UNUSED,
Mathieu Chartierb190d942015-11-12 10:00:58 -0800264 jint flags ATTRIBUTE_UNUSED,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800265 jobject class_loader,
266 jobjectArray dex_elements) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700267 ScopedUtfChars sourceName(env, javaSourceName);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700268 if (sourceName.c_str() == nullptr) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700269 return 0;
270 }
Calin Juravle1f7079b2017-04-18 21:25:37 -0700271
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700272 Runtime* const runtime = Runtime::Current();
273 ClassLinker* linker = runtime->GetClassLinker();
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800274 std::vector<std::unique_ptr<const DexFile>> dex_files;
Andreas Gampe833a4852014-05-21 18:46:59 -0700275 std::vector<std::string> error_msgs;
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700276 const OatFile* oat_file = nullptr;
Andreas Gampe833a4852014-05-21 18:46:59 -0700277
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700278 dex_files = runtime->GetOatFileManager().OpenDexFilesFromOat(sourceName.c_str(),
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800279 class_loader,
280 dex_elements,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700281 /*out*/ &oat_file,
282 /*out*/ &error_msgs);
Andreas Gampe833a4852014-05-21 18:46:59 -0700283
Richard Uhler66d874d2015-01-15 09:37:19 -0800284 if (!dex_files.empty()) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700285 jlongArray array = ConvertDexFilesToJavaArray(env, oat_file, dex_files);
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800286 if (array == nullptr) {
287 ScopedObjectAccess soa(env);
288 for (auto& dex_file : dex_files) {
Vladimir Markocd556b02017-02-03 11:47:34 +0000289 if (linker->IsDexFileRegistered(soa.Self(), *dex_file)) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800290 dex_file.release();
291 }
292 }
293 }
294 return array;
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700295 } else {
Vladimir Marko60836d52014-01-16 15:53:38 +0000296 ScopedObjectAccess soa(env);
Andreas Gampe329d1882014-04-08 10:32:19 -0700297 CHECK(!error_msgs.empty());
298 // The most important message is at the end. So set up nesting by going forward, which will
299 // wrap the existing exception as a cause for the following one.
300 auto it = error_msgs.begin();
301 auto itEnd = error_msgs.end();
302 for ( ; it != itEnd; ++it) {
303 ThrowWrappedIOException("%s", it->c_str());
304 }
305
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800306 return nullptr;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700307 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700308}
309
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700310static jboolean DexFile_closeDexFile(JNIEnv* env, jclass, jobject cookie) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700311 std::vector<const DexFile*> dex_files;
312 const OatFile* oat_file;
313 if (!ConvertJavaArrayToDexFiles(env, cookie, dex_files, oat_file)) {
314 Thread::Current()->AssertPendingException();
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700315 return JNI_FALSE;
316 }
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700317 Runtime* const runtime = Runtime::Current();
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700318 bool all_deleted = true;
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700319 {
320 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700321 ObjPtr<mirror::Object> dex_files_object = soa.Decode<mirror::Object>(cookie);
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700322 ObjPtr<mirror::LongArray> long_dex_files = dex_files_object->AsLongArray();
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700323 // Delete dex files associated with this dalvik.system.DexFile since there should not be running
324 // code using it. dex_files is a vector due to multidex.
325 ClassLinker* const class_linker = runtime->GetClassLinker();
326 int32_t i = kDexFileIndexStart; // Oat file is at index 0.
327 for (const DexFile* dex_file : dex_files) {
328 if (dex_file != nullptr) {
329 // Only delete the dex file if the dex cache is not found to prevent runtime crashes if there
330 // are calls to DexFile.close while the ART DexFile is still in use.
Vladimir Markocd556b02017-02-03 11:47:34 +0000331 if (!class_linker->IsDexFileRegistered(soa.Self(), *dex_file)) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700332 // Clear the element in the array so that we can call close again.
333 long_dex_files->Set(i, 0);
334 delete dex_file;
335 } else {
336 all_deleted = false;
337 }
338 }
339 ++i;
Andreas Gampe833a4852014-05-21 18:46:59 -0700340 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700341 }
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700342
Mathieu Chartierfdccbd42015-10-14 10:58:41 -0700343 // oat_file can be null if we are running without dex2oat.
344 if (all_deleted && oat_file != nullptr) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700345 // If all of the dex files are no longer in use we can unmap the corresponding oat file.
346 VLOG(class_linker) << "Unregistering " << oat_file;
347 runtime->GetOatFileManager().UnRegisterAndDeleteOatFile(oat_file);
348 }
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700349 return all_deleted ? JNI_TRUE : JNI_FALSE;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700350}
351
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700352static jclass DexFile_defineClassNative(JNIEnv* env,
353 jclass,
354 jstring javaName,
355 jobject javaLoader,
Mathieu Chartier00310e02015-10-17 12:46:42 -0700356 jobject cookie,
357 jobject dexFile) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700358 std::vector<const DexFile*> dex_files;
359 const OatFile* oat_file;
360 if (!ConvertJavaArrayToDexFiles(env, cookie, /*out*/ dex_files, /*out*/ oat_file)) {
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700361 VLOG(class_linker) << "Failed to find dex_file";
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800362 DCHECK(env->ExceptionCheck());
363 return nullptr;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700364 }
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800365
Brian Carlstromdf143242011-10-10 18:05:34 -0700366 ScopedUtfChars class_name(env, javaName);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700367 if (class_name.c_str() == nullptr) {
Brian Carlstrom2e450bf2013-09-06 15:39:46 -0700368 VLOG(class_linker) << "Failed to find class_name";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700369 return nullptr;
Brian Carlstromdf143242011-10-10 18:05:34 -0700370 }
Elliott Hughes95572412011-12-13 18:14:20 -0800371 const std::string descriptor(DotToDescriptor(class_name.c_str()));
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800372 const size_t hash(ComputeModifiedUtf8Hash(descriptor.c_str()));
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700373 for (auto& dex_file : dex_files) {
David Sehr9aa352e2016-09-15 18:13:52 -0700374 const DexFile::ClassDef* dex_class_def =
375 OatDexFile::FindClassDef(*dex_file, descriptor.c_str(), hash);
Andreas Gampe833a4852014-05-21 18:46:59 -0700376 if (dex_class_def != nullptr) {
377 ScopedObjectAccess soa(env);
378 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Andreas Gampe833a4852014-05-21 18:46:59 -0700379 StackHandleScope<1> hs(soa.Self());
380 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700381 hs.NewHandle(soa.Decode<mirror::ClassLoader>(javaLoader)));
Vladimir Markocd556b02017-02-03 11:47:34 +0000382 ObjPtr<mirror::DexCache> dex_cache =
383 class_linker->RegisterDexFile(*dex_file, class_loader.Get());
384 if (dex_cache == nullptr) {
385 // OOME or InternalError (dexFile already registered with a different class loader).
386 soa.Self()->AssertPendingException();
387 return nullptr;
388 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700389 ObjPtr<mirror::Class> result = class_linker->DefineClass(soa.Self(),
390 descriptor.c_str(),
391 hash,
392 class_loader,
393 *dex_file,
394 *dex_class_def);
Mathieu Chartier00310e02015-10-17 12:46:42 -0700395 // Add the used dex file. This only required for the DexFile.loadClass API since normal
396 // class loaders already keep their dex files live.
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700397 class_linker->InsertDexFileInToClassLoader(soa.Decode<mirror::Object>(dexFile),
Mathieu Chartier00310e02015-10-17 12:46:42 -0700398 class_loader.Get());
Andreas Gampe833a4852014-05-21 18:46:59 -0700399 if (result != nullptr) {
Brian Carlstrom667ab7c2014-10-16 19:12:28 -0700400 VLOG(class_linker) << "DexFile_defineClassNative returning " << result
401 << " for " << class_name.c_str();
Andreas Gampe833a4852014-05-21 18:46:59 -0700402 return soa.AddLocalReference<jclass>(result);
403 }
404 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700405 }
Brian Carlstrom667ab7c2014-10-16 19:12:28 -0700406 VLOG(class_linker) << "Failed to find dex_class_def " << class_name.c_str();
Andreas Gampe833a4852014-05-21 18:46:59 -0700407 return nullptr;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700408}
409
Andreas Gampe833a4852014-05-21 18:46:59 -0700410// Needed as a compare functor for sets of const char
411struct CharPointerComparator {
412 bool operator()(const char *str1, const char *str2) const {
413 return strcmp(str1, str2) < 0;
414 }
415};
416
417// Note: this can be an expensive call, as we sort out duplicates in MultiDex files.
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800418static jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jobject cookie) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700419 const OatFile* oat_file = nullptr;
420 std::vector<const DexFile*> dex_files;
421 if (!ConvertJavaArrayToDexFiles(env, cookie, /*out */ dex_files, /* out */ oat_file)) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800422 DCHECK(env->ExceptionCheck());
423 return nullptr;
424 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700425
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800426 // Push all class descriptors into a set. Use set instead of unordered_set as we want to
427 // retrieve all in the end.
428 std::set<const char*, CharPointerComparator> descriptors;
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700429 for (auto& dex_file : dex_files) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800430 for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
431 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
432 const char* descriptor = dex_file->GetClassDescriptor(class_def);
433 descriptors.insert(descriptor);
Andreas Gampe833a4852014-05-21 18:46:59 -0700434 }
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800435 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700436
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800437 // Now create output array and copy the set into it.
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700438 jobjectArray result = env->NewObjectArray(descriptors.size(),
439 WellKnownClasses::java_lang_String,
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800440 nullptr);
441 if (result != nullptr) {
442 auto it = descriptors.begin();
443 auto it_end = descriptors.end();
444 jsize i = 0;
445 for (; it != it_end; it++, ++i) {
446 std::string descriptor(DescriptorToDot(*it));
447 ScopedLocalRef<jstring> jdescriptor(env, env->NewStringUTF(descriptor.c_str()));
448 if (jdescriptor.get() == nullptr) {
449 return nullptr;
Ian Rogersdd157d72014-05-15 14:47:50 -0700450 }
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800451 env->SetObjectArrayElement(result, i, jdescriptor.get());
Ian Rogersdd157d72014-05-15 14:47:50 -0700452 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700453 }
Ian Rogersdd157d72014-05-15 14:47:50 -0700454 return result;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700455}
456
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700457static jint GetDexOptNeeded(JNIEnv* env,
458 const char* filename,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700459 const char* instruction_set,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000460 const char* compiler_filter_name,
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700461 bool profile_changed,
462 bool downgrade) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100463 if ((filename == nullptr) || !OS::FileExists(filename)) {
Richard Uhler95abd042015-03-24 09:51:28 -0700464 LOG(ERROR) << "DexFile_getDexOptNeeded file '" << filename << "' does not exist";
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700465 ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException"));
Narayan Kamath11d9f062014-04-23 20:24:57 +0100466 const char* message = (filename == nullptr) ? "<empty file name>" : filename;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700467 env->ThrowNew(fnfe.get(), message);
Calin Juravleb077e152016-02-18 18:47:37 +0000468 return -1;
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700469 }
470
Alex Light6e183f22014-07-18 14:57:04 -0700471 const InstructionSet target_instruction_set = GetInstructionSetFromString(instruction_set);
Andreas Gampe20c89302014-08-19 17:28:06 -0700472 if (target_instruction_set == kNone) {
473 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
474 std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set));
475 env->ThrowNew(iae.get(), message.c_str());
Calin Juravleb077e152016-02-18 18:47:37 +0000476 return -1;
Andreas Gampe20c89302014-08-19 17:28:06 -0700477 }
Alex Light6e183f22014-07-18 14:57:04 -0700478
Andreas Gampe29d38e72016-03-23 15:31:51 +0000479 CompilerFilter::Filter filter;
480 if (!CompilerFilter::ParseCompilerFilter(compiler_filter_name, &filter)) {
481 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
482 std::string message(StringPrintf("Compiler filter %s is invalid.", compiler_filter_name));
483 env->ThrowNew(iae.get(), message.c_str());
484 return -1;
485 }
486
Richard Uhler66d874d2015-01-15 09:37:19 -0800487 // TODO: Verify the dex location is well formed, and throw an IOException if
488 // not?
Andreas Gampe29d38e72016-03-23 15:31:51 +0000489
Richard Uhlerd1472a22016-04-15 15:18:56 -0700490 OatFileAssistant oat_file_assistant(filename, target_instruction_set, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800491
492 // Always treat elements of the bootclasspath as up-to-date.
493 if (oat_file_assistant.IsInBootClassPath()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700494 return OatFileAssistant::kNoDexOptNeeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800495 }
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700496 return oat_file_assistant.GetDexOptNeeded(filter, profile_changed, downgrade);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700497}
498
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100499static jstring DexFile_getDexFileStatus(JNIEnv* env,
500 jclass,
501 jstring javaFilename,
502 jstring javaInstructionSet) {
503 ScopedUtfChars filename(env, javaFilename);
504 if (env->ExceptionCheck()) {
505 return nullptr;
506 }
507
508 ScopedUtfChars instruction_set(env, javaInstructionSet);
509 if (env->ExceptionCheck()) {
510 return nullptr;
511 }
512
513 const InstructionSet target_instruction_set = GetInstructionSetFromString(
514 instruction_set.c_str());
515 if (target_instruction_set == kNone) {
516 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
517 std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set.c_str()));
518 env->ThrowNew(iae.get(), message.c_str());
519 return nullptr;
520 }
521
522 OatFileAssistant oat_file_assistant(filename.c_str(), target_instruction_set,
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100523 false /* load_executable */);
Richard Uhler46cc64f2016-11-14 14:53:55 +0000524 return env->NewStringUTF(oat_file_assistant.GetStatusDump().c_str());
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100525}
526
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700527static jint DexFile_getDexOptNeeded(JNIEnv* env,
528 jclass,
529 jstring javaFilename,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700530 jstring javaInstructionSet,
Andreas Gampec38be812016-03-23 15:03:46 -0700531 jstring javaTargetCompilerFilter,
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700532 jboolean newProfile,
533 jboolean downgrade) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100534 ScopedUtfChars filename(env, javaFilename);
Andreas Gampe20c89302014-08-19 17:28:06 -0700535 if (env->ExceptionCheck()) {
Calin Juravleb077e152016-02-18 18:47:37 +0000536 return -1;
Andreas Gampe20c89302014-08-19 17:28:06 -0700537 }
538
Narayan Kamath11d9f062014-04-23 20:24:57 +0100539 ScopedUtfChars instruction_set(env, javaInstructionSet);
Andreas Gampe20c89302014-08-19 17:28:06 -0700540 if (env->ExceptionCheck()) {
Calin Juravleb077e152016-02-18 18:47:37 +0000541 return -1;
Andreas Gampe20c89302014-08-19 17:28:06 -0700542 }
Narayan Kamath11d9f062014-04-23 20:24:57 +0100543
Andreas Gampec38be812016-03-23 15:03:46 -0700544 ScopedUtfChars target_compiler_filter(env, javaTargetCompilerFilter);
545 if (env->ExceptionCheck()) {
546 return -1;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000547 }
Andreas Gampec38be812016-03-23 15:03:46 -0700548
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700549 return GetDexOptNeeded(env,
550 filename.c_str(),
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700551 instruction_set.c_str(),
Andreas Gampec38be812016-03-23 15:03:46 -0700552 target_compiler_filter.c_str(),
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700553 newProfile == JNI_TRUE,
554 downgrade == JNI_TRUE);
Narayan Kamath11d9f062014-04-23 20:24:57 +0100555}
556
Calin Juravleb077e152016-02-18 18:47:37 +0000557// public API
Narayan Kamath11d9f062014-04-23 20:24:57 +0100558static jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
Richard Uhler06e3f4f2016-05-24 15:42:37 -0700559 ScopedUtfChars filename_utf(env, javaFilename);
560 if (env->ExceptionCheck()) {
561 return JNI_FALSE;
562 }
563
564 const char* filename = filename_utf.c_str();
565 if ((filename == nullptr) || !OS::FileExists(filename)) {
566 LOG(ERROR) << "DexFile_isDexOptNeeded file '" << filename << "' does not exist";
567 ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException"));
568 const char* message = (filename == nullptr) ? "<empty file name>" : filename;
569 env->ThrowNew(fnfe.get(), message);
570 return JNI_FALSE;
571 }
572
Richard Uhlerd1472a22016-04-15 15:18:56 -0700573 OatFileAssistant oat_file_assistant(filename, kRuntimeISA, false);
Richard Uhler06e3f4f2016-05-24 15:42:37 -0700574 return oat_file_assistant.IsUpToDate() ? JNI_FALSE : JNI_TRUE;
Dave Allison39c3bfb2014-01-28 18:33:52 -0800575}
576
Andreas Gampec38be812016-03-23 15:03:46 -0700577static jboolean DexFile_isValidCompilerFilter(JNIEnv* env,
578 jclass javeDexFileClass ATTRIBUTE_UNUSED,
579 jstring javaCompilerFilter) {
580 ScopedUtfChars compiler_filter(env, javaCompilerFilter);
581 if (env->ExceptionCheck()) {
582 return -1;
583 }
584
585 CompilerFilter::Filter filter;
586 return CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)
587 ? JNI_TRUE : JNI_FALSE;
588}
589
590static jboolean DexFile_isProfileGuidedCompilerFilter(JNIEnv* env,
591 jclass javeDexFileClass ATTRIBUTE_UNUSED,
592 jstring javaCompilerFilter) {
593 ScopedUtfChars compiler_filter(env, javaCompilerFilter);
594 if (env->ExceptionCheck()) {
595 return -1;
596 }
597
598 CompilerFilter::Filter filter;
599 if (!CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)) {
600 return JNI_FALSE;
601 }
602 return CompilerFilter::DependsOnProfile(filter) ? JNI_TRUE : JNI_FALSE;
603}
604
Andreas Gampe86a785d2016-03-30 17:19:48 -0700605static jstring DexFile_getNonProfileGuidedCompilerFilter(JNIEnv* env,
606 jclass javeDexFileClass ATTRIBUTE_UNUSED,
607 jstring javaCompilerFilter) {
608 ScopedUtfChars compiler_filter(env, javaCompilerFilter);
609 if (env->ExceptionCheck()) {
610 return nullptr;
611 }
612
613 CompilerFilter::Filter filter;
614 if (!CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)) {
615 return javaCompilerFilter;
616 }
617
618 CompilerFilter::Filter new_filter = CompilerFilter::GetNonProfileDependentFilterFrom(filter);
619
620 // Filter stayed the same, return input.
621 if (filter == new_filter) {
622 return javaCompilerFilter;
623 }
624
625 // Create a new string object and return.
626 std::string new_filter_str = CompilerFilter::NameOfFilter(new_filter);
627 return env->NewStringUTF(new_filter_str.c_str());
628}
629
Nicolas Geoffray741d4262017-05-03 13:08:36 +0100630static jstring DexFile_getSafeModeCompilerFilter(JNIEnv* env,
631 jclass javeDexFileClass ATTRIBUTE_UNUSED,
632 jstring javaCompilerFilter) {
633 ScopedUtfChars compiler_filter(env, javaCompilerFilter);
634 if (env->ExceptionCheck()) {
635 return nullptr;
636 }
637
638 CompilerFilter::Filter filter;
639 if (!CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)) {
640 return javaCompilerFilter;
641 }
642
643 CompilerFilter::Filter new_filter = CompilerFilter::GetSafeModeFilterFrom(filter);
644
645 // Filter stayed the same, return input.
646 if (filter == new_filter) {
647 return javaCompilerFilter;
648 }
649
650 // Create a new string object and return.
651 std::string new_filter_str = CompilerFilter::NameOfFilter(new_filter);
652 return env->NewStringUTF(new_filter_str.c_str());
653}
654
Jeff Hao90671be2016-04-22 14:00:06 -0700655static jboolean DexFile_isBackedByOatFile(JNIEnv* env, jclass, jobject cookie) {
656 const OatFile* oat_file = nullptr;
657 std::vector<const DexFile*> dex_files;
658 if (!ConvertJavaArrayToDexFiles(env, cookie, /*out */ dex_files, /* out */ oat_file)) {
659 DCHECK(env->ExceptionCheck());
660 return false;
661 }
662 return oat_file != nullptr;
663}
664
Calin Juravle367b9d82017-05-15 18:18:39 -0700665static jobjectArray DexFile_getDexFileOutputPaths(JNIEnv* env,
Philip Cuadrab55ad7c2016-07-12 16:37:40 -0700666 jclass,
667 jstring javaFilename,
668 jstring javaInstructionSet) {
669 ScopedUtfChars filename(env, javaFilename);
670 if (env->ExceptionCheck()) {
671 return nullptr;
672 }
673
674 ScopedUtfChars instruction_set(env, javaInstructionSet);
675 if (env->ExceptionCheck()) {
676 return nullptr;
677 }
678
679 const InstructionSet target_instruction_set = GetInstructionSetFromString(
680 instruction_set.c_str());
681 if (target_instruction_set == kNone) {
682 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
683 std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set.c_str()));
684 env->ThrowNew(iae.get(), message.c_str());
685 return nullptr;
686 }
687
688 OatFileAssistant oat_file_assistant(filename.c_str(),
689 target_instruction_set,
Philip Cuadrab55ad7c2016-07-12 16:37:40 -0700690 false /* load_executable */);
691
692 std::unique_ptr<OatFile> best_oat_file = oat_file_assistant.GetBestOatFile();
693 if (best_oat_file == nullptr) {
694 return nullptr;
695 }
696
Calin Juravle367b9d82017-05-15 18:18:39 -0700697 std::string oat_filename = best_oat_file->GetLocation();
698 std::string vdex_filename = GetVdexFilename(best_oat_file->GetLocation());
699
700 ScopedLocalRef<jstring> jvdexFilename(env, env->NewStringUTF(vdex_filename.c_str()));
701 if (jvdexFilename.get() == nullptr) {
702 return nullptr;
703 }
704 ScopedLocalRef<jstring> joatFilename(env, env->NewStringUTF(oat_filename.c_str()));
705 if (joatFilename.get() == nullptr) {
706 return nullptr;
707 }
708
709 // Now create output array and copy the set into it.
710 jobjectArray result = env->NewObjectArray(2,
711 WellKnownClasses::java_lang_String,
712 nullptr);
713 env->SetObjectArrayElement(result, 0, jvdexFilename.get());
714 env->SetObjectArrayElement(result, 1, joatFilename.get());
715
716 return result;
Philip Cuadrab55ad7c2016-07-12 16:37:40 -0700717}
718
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700719static JNINativeMethod gMethods[] = {
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700720 NATIVE_METHOD(DexFile, closeDexFile, "(Ljava/lang/Object;)Z"),
Mathieu Chartier00310e02015-10-17 12:46:42 -0700721 NATIVE_METHOD(DexFile,
722 defineClassNative,
723 "(Ljava/lang/String;"
724 "Ljava/lang/ClassLoader;"
725 "Ljava/lang/Object;"
726 "Ldalvik/system/DexFile;"
727 ")Ljava/lang/Class;"),
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800728 NATIVE_METHOD(DexFile, getClassNameList, "(Ljava/lang/Object;)[Ljava/lang/String;"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700729 NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700730 NATIVE_METHOD(DexFile, getDexOptNeeded,
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700731 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ZZ)I"),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700732 NATIVE_METHOD(DexFile, openDexFileNative,
Mathieu Chartier689a7002015-11-20 10:29:42 -0800733 "(Ljava/lang/String;"
734 "Ljava/lang/String;"
735 "I"
736 "Ljava/lang/ClassLoader;"
737 "[Ldalvik/system/DexPathList$Element;"
738 ")Ljava/lang/Object;"),
Alex Lightea9465e2017-02-16 15:38:35 -0800739 NATIVE_METHOD(DexFile, createCookieWithDirectBuffer,
740 "(Ljava/nio/ByteBuffer;II)Ljava/lang/Object;"),
741 NATIVE_METHOD(DexFile, createCookieWithArray, "([BII)Ljava/lang/Object;"),
Andreas Gampec38be812016-03-23 15:03:46 -0700742 NATIVE_METHOD(DexFile, isValidCompilerFilter, "(Ljava/lang/String;)Z"),
743 NATIVE_METHOD(DexFile, isProfileGuidedCompilerFilter, "(Ljava/lang/String;)Z"),
Andreas Gampe86a785d2016-03-30 17:19:48 -0700744 NATIVE_METHOD(DexFile,
745 getNonProfileGuidedCompilerFilter,
746 "(Ljava/lang/String;)Ljava/lang/String;"),
Nicolas Geoffray741d4262017-05-03 13:08:36 +0100747 NATIVE_METHOD(DexFile,
748 getSafeModeCompilerFilter,
749 "(Ljava/lang/String;)Ljava/lang/String;"),
Jeff Hao90671be2016-04-22 14:00:06 -0700750 NATIVE_METHOD(DexFile, isBackedByOatFile, "(Ljava/lang/Object;)Z"),
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100751 NATIVE_METHOD(DexFile, getDexFileStatus,
Philip Cuadrab55ad7c2016-07-12 16:37:40 -0700752 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
Calin Juravle367b9d82017-05-15 18:18:39 -0700753 NATIVE_METHOD(DexFile, getDexFileOutputPaths,
754 "(Ljava/lang/String;Ljava/lang/String;)[Ljava/lang/String;")
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700755};
756
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700757void register_dalvik_system_DexFile(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700758 REGISTER_NATIVE_METHODS("dalvik/system/DexFile");
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700759}
760
761} // namespace art