blob: d40e6d94c932346166b5a79f1a694522f3006fa4 [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 }
Calin Juravle44e5efa2017-09-12 00:54:26 -0700496
497 // TODO(calin): Extend DexFile.getDexOptNeeded to accept the class loader context. b/62269291.
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700498 return oat_file_assistant.GetDexOptNeeded(filter, profile_changed, downgrade);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700499}
500
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100501static jstring DexFile_getDexFileStatus(JNIEnv* env,
502 jclass,
503 jstring javaFilename,
504 jstring javaInstructionSet) {
505 ScopedUtfChars filename(env, javaFilename);
506 if (env->ExceptionCheck()) {
507 return nullptr;
508 }
509
510 ScopedUtfChars instruction_set(env, javaInstructionSet);
511 if (env->ExceptionCheck()) {
512 return nullptr;
513 }
514
515 const InstructionSet target_instruction_set = GetInstructionSetFromString(
516 instruction_set.c_str());
517 if (target_instruction_set == kNone) {
518 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
519 std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set.c_str()));
520 env->ThrowNew(iae.get(), message.c_str());
521 return nullptr;
522 }
523
524 OatFileAssistant oat_file_assistant(filename.c_str(), target_instruction_set,
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100525 false /* load_executable */);
Richard Uhler46cc64f2016-11-14 14:53:55 +0000526 return env->NewStringUTF(oat_file_assistant.GetStatusDump().c_str());
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100527}
528
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700529static jint DexFile_getDexOptNeeded(JNIEnv* env,
530 jclass,
531 jstring javaFilename,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700532 jstring javaInstructionSet,
Andreas Gampec38be812016-03-23 15:03:46 -0700533 jstring javaTargetCompilerFilter,
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700534 jboolean newProfile,
535 jboolean downgrade) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100536 ScopedUtfChars filename(env, javaFilename);
Andreas Gampe20c89302014-08-19 17:28:06 -0700537 if (env->ExceptionCheck()) {
Calin Juravleb077e152016-02-18 18:47:37 +0000538 return -1;
Andreas Gampe20c89302014-08-19 17:28:06 -0700539 }
540
Narayan Kamath11d9f062014-04-23 20:24:57 +0100541 ScopedUtfChars instruction_set(env, javaInstructionSet);
Andreas Gampe20c89302014-08-19 17:28:06 -0700542 if (env->ExceptionCheck()) {
Calin Juravleb077e152016-02-18 18:47:37 +0000543 return -1;
Andreas Gampe20c89302014-08-19 17:28:06 -0700544 }
Narayan Kamath11d9f062014-04-23 20:24:57 +0100545
Andreas Gampec38be812016-03-23 15:03:46 -0700546 ScopedUtfChars target_compiler_filter(env, javaTargetCompilerFilter);
547 if (env->ExceptionCheck()) {
548 return -1;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000549 }
Andreas Gampec38be812016-03-23 15:03:46 -0700550
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700551 return GetDexOptNeeded(env,
552 filename.c_str(),
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700553 instruction_set.c_str(),
Andreas Gampec38be812016-03-23 15:03:46 -0700554 target_compiler_filter.c_str(),
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700555 newProfile == JNI_TRUE,
556 downgrade == JNI_TRUE);
Narayan Kamath11d9f062014-04-23 20:24:57 +0100557}
558
Calin Juravleb077e152016-02-18 18:47:37 +0000559// public API
Narayan Kamath11d9f062014-04-23 20:24:57 +0100560static jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
Richard Uhler06e3f4f2016-05-24 15:42:37 -0700561 ScopedUtfChars filename_utf(env, javaFilename);
562 if (env->ExceptionCheck()) {
563 return JNI_FALSE;
564 }
565
566 const char* filename = filename_utf.c_str();
567 if ((filename == nullptr) || !OS::FileExists(filename)) {
568 LOG(ERROR) << "DexFile_isDexOptNeeded file '" << filename << "' does not exist";
569 ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException"));
570 const char* message = (filename == nullptr) ? "<empty file name>" : filename;
571 env->ThrowNew(fnfe.get(), message);
572 return JNI_FALSE;
573 }
574
Richard Uhlerd1472a22016-04-15 15:18:56 -0700575 OatFileAssistant oat_file_assistant(filename, kRuntimeISA, false);
Richard Uhler06e3f4f2016-05-24 15:42:37 -0700576 return oat_file_assistant.IsUpToDate() ? JNI_FALSE : JNI_TRUE;
Dave Allison39c3bfb2014-01-28 18:33:52 -0800577}
578
Andreas Gampec38be812016-03-23 15:03:46 -0700579static jboolean DexFile_isValidCompilerFilter(JNIEnv* env,
580 jclass javeDexFileClass ATTRIBUTE_UNUSED,
581 jstring javaCompilerFilter) {
582 ScopedUtfChars compiler_filter(env, javaCompilerFilter);
583 if (env->ExceptionCheck()) {
584 return -1;
585 }
586
587 CompilerFilter::Filter filter;
588 return CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)
589 ? JNI_TRUE : JNI_FALSE;
590}
591
592static jboolean DexFile_isProfileGuidedCompilerFilter(JNIEnv* env,
593 jclass javeDexFileClass ATTRIBUTE_UNUSED,
594 jstring javaCompilerFilter) {
595 ScopedUtfChars compiler_filter(env, javaCompilerFilter);
596 if (env->ExceptionCheck()) {
597 return -1;
598 }
599
600 CompilerFilter::Filter filter;
601 if (!CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)) {
602 return JNI_FALSE;
603 }
604 return CompilerFilter::DependsOnProfile(filter) ? JNI_TRUE : JNI_FALSE;
605}
606
Andreas Gampe86a785d2016-03-30 17:19:48 -0700607static jstring DexFile_getNonProfileGuidedCompilerFilter(JNIEnv* env,
608 jclass javeDexFileClass ATTRIBUTE_UNUSED,
609 jstring javaCompilerFilter) {
610 ScopedUtfChars compiler_filter(env, javaCompilerFilter);
611 if (env->ExceptionCheck()) {
612 return nullptr;
613 }
614
615 CompilerFilter::Filter filter;
616 if (!CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)) {
617 return javaCompilerFilter;
618 }
619
620 CompilerFilter::Filter new_filter = CompilerFilter::GetNonProfileDependentFilterFrom(filter);
621
622 // Filter stayed the same, return input.
623 if (filter == new_filter) {
624 return javaCompilerFilter;
625 }
626
627 // Create a new string object and return.
628 std::string new_filter_str = CompilerFilter::NameOfFilter(new_filter);
629 return env->NewStringUTF(new_filter_str.c_str());
630}
631
Nicolas Geoffray741d4262017-05-03 13:08:36 +0100632static jstring DexFile_getSafeModeCompilerFilter(JNIEnv* env,
633 jclass javeDexFileClass ATTRIBUTE_UNUSED,
634 jstring javaCompilerFilter) {
635 ScopedUtfChars compiler_filter(env, javaCompilerFilter);
636 if (env->ExceptionCheck()) {
637 return nullptr;
638 }
639
640 CompilerFilter::Filter filter;
641 if (!CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)) {
642 return javaCompilerFilter;
643 }
644
645 CompilerFilter::Filter new_filter = CompilerFilter::GetSafeModeFilterFrom(filter);
646
647 // Filter stayed the same, return input.
648 if (filter == new_filter) {
649 return javaCompilerFilter;
650 }
651
652 // Create a new string object and return.
653 std::string new_filter_str = CompilerFilter::NameOfFilter(new_filter);
654 return env->NewStringUTF(new_filter_str.c_str());
655}
656
Jeff Hao90671be2016-04-22 14:00:06 -0700657static jboolean DexFile_isBackedByOatFile(JNIEnv* env, jclass, jobject cookie) {
658 const OatFile* oat_file = nullptr;
659 std::vector<const DexFile*> dex_files;
660 if (!ConvertJavaArrayToDexFiles(env, cookie, /*out */ dex_files, /* out */ oat_file)) {
661 DCHECK(env->ExceptionCheck());
662 return false;
663 }
664 return oat_file != nullptr;
665}
666
Calin Juravle367b9d82017-05-15 18:18:39 -0700667static jobjectArray DexFile_getDexFileOutputPaths(JNIEnv* env,
Philip Cuadrab55ad7c2016-07-12 16:37:40 -0700668 jclass,
669 jstring javaFilename,
670 jstring javaInstructionSet) {
671 ScopedUtfChars filename(env, javaFilename);
672 if (env->ExceptionCheck()) {
673 return nullptr;
674 }
675
676 ScopedUtfChars instruction_set(env, javaInstructionSet);
677 if (env->ExceptionCheck()) {
678 return nullptr;
679 }
680
681 const InstructionSet target_instruction_set = GetInstructionSetFromString(
682 instruction_set.c_str());
683 if (target_instruction_set == kNone) {
684 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
685 std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set.c_str()));
686 env->ThrowNew(iae.get(), message.c_str());
687 return nullptr;
688 }
689
690 OatFileAssistant oat_file_assistant(filename.c_str(),
691 target_instruction_set,
Philip Cuadrab55ad7c2016-07-12 16:37:40 -0700692 false /* load_executable */);
693
694 std::unique_ptr<OatFile> best_oat_file = oat_file_assistant.GetBestOatFile();
695 if (best_oat_file == nullptr) {
696 return nullptr;
697 }
698
Calin Juravle367b9d82017-05-15 18:18:39 -0700699 std::string oat_filename = best_oat_file->GetLocation();
700 std::string vdex_filename = GetVdexFilename(best_oat_file->GetLocation());
701
702 ScopedLocalRef<jstring> jvdexFilename(env, env->NewStringUTF(vdex_filename.c_str()));
703 if (jvdexFilename.get() == nullptr) {
704 return nullptr;
705 }
706 ScopedLocalRef<jstring> joatFilename(env, env->NewStringUTF(oat_filename.c_str()));
707 if (joatFilename.get() == nullptr) {
708 return nullptr;
709 }
710
711 // Now create output array and copy the set into it.
712 jobjectArray result = env->NewObjectArray(2,
713 WellKnownClasses::java_lang_String,
714 nullptr);
715 env->SetObjectArrayElement(result, 0, jvdexFilename.get());
716 env->SetObjectArrayElement(result, 1, joatFilename.get());
717
718 return result;
Philip Cuadrab55ad7c2016-07-12 16:37:40 -0700719}
720
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700721static JNINativeMethod gMethods[] = {
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700722 NATIVE_METHOD(DexFile, closeDexFile, "(Ljava/lang/Object;)Z"),
Mathieu Chartier00310e02015-10-17 12:46:42 -0700723 NATIVE_METHOD(DexFile,
724 defineClassNative,
725 "(Ljava/lang/String;"
726 "Ljava/lang/ClassLoader;"
727 "Ljava/lang/Object;"
728 "Ldalvik/system/DexFile;"
729 ")Ljava/lang/Class;"),
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800730 NATIVE_METHOD(DexFile, getClassNameList, "(Ljava/lang/Object;)[Ljava/lang/String;"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700731 NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700732 NATIVE_METHOD(DexFile, getDexOptNeeded,
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700733 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ZZ)I"),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700734 NATIVE_METHOD(DexFile, openDexFileNative,
Mathieu Chartier689a7002015-11-20 10:29:42 -0800735 "(Ljava/lang/String;"
736 "Ljava/lang/String;"
737 "I"
738 "Ljava/lang/ClassLoader;"
739 "[Ldalvik/system/DexPathList$Element;"
740 ")Ljava/lang/Object;"),
Alex Lightea9465e2017-02-16 15:38:35 -0800741 NATIVE_METHOD(DexFile, createCookieWithDirectBuffer,
742 "(Ljava/nio/ByteBuffer;II)Ljava/lang/Object;"),
743 NATIVE_METHOD(DexFile, createCookieWithArray, "([BII)Ljava/lang/Object;"),
Andreas Gampec38be812016-03-23 15:03:46 -0700744 NATIVE_METHOD(DexFile, isValidCompilerFilter, "(Ljava/lang/String;)Z"),
745 NATIVE_METHOD(DexFile, isProfileGuidedCompilerFilter, "(Ljava/lang/String;)Z"),
Andreas Gampe86a785d2016-03-30 17:19:48 -0700746 NATIVE_METHOD(DexFile,
747 getNonProfileGuidedCompilerFilter,
748 "(Ljava/lang/String;)Ljava/lang/String;"),
Nicolas Geoffray741d4262017-05-03 13:08:36 +0100749 NATIVE_METHOD(DexFile,
750 getSafeModeCompilerFilter,
751 "(Ljava/lang/String;)Ljava/lang/String;"),
Jeff Hao90671be2016-04-22 14:00:06 -0700752 NATIVE_METHOD(DexFile, isBackedByOatFile, "(Ljava/lang/Object;)Z"),
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100753 NATIVE_METHOD(DexFile, getDexFileStatus,
Philip Cuadrab55ad7c2016-07-12 16:37:40 -0700754 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
Calin Juravle367b9d82017-05-15 18:18:39 -0700755 NATIVE_METHOD(DexFile, getDexFileOutputPaths,
756 "(Ljava/lang/String;Ljava/lang/String;)[Ljava/lang/String;")
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700757};
758
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700759void register_dalvik_system_DexFile(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700760 REGISTER_NATIVE_METHODS("dalvik/system/DexFile");
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700761}
762
763} // namespace art