blob: ad009668bf14c03bf8fa4996b1c25c1c225dc4ef [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"
Andreas Gampea14100c2017-04-24 15:09:56 -070022#include "nativehelper/jni_macros.h"
Andreas Gampe46ee31b2016-12-14 10:11:49 -080023
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"
Elliott Hugheseac76672012-05-24 21:56:51 -070030#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031#include "mirror/class_loader.h"
Ian Rogers05f30572013-02-20 12:13:11 -080032#include "mirror/object-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "mirror/string.h"
Andreas Gampe87583b32017-05-25 11:22:18 -070034#include "native_util.h"
Narayan Kamath8943c1d2016-05-02 13:14:48 +010035#include "oat_file.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080036#include "oat_file_assistant.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070037#include "oat_file_manager.h"
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -070038#include "os.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070039#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070040#include "scoped_thread_state_change-inl.h"
Ian Rogersc9818482012-01-11 08:52:51 -080041#include "ScopedLocalRef.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070042#include "ScopedUtfChars.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,
461 bool profile_changed) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100462 if ((filename == nullptr) || !OS::FileExists(filename)) {
Richard Uhler95abd042015-03-24 09:51:28 -0700463 LOG(ERROR) << "DexFile_getDexOptNeeded file '" << filename << "' does not exist";
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700464 ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException"));
Narayan Kamath11d9f062014-04-23 20:24:57 +0100465 const char* message = (filename == nullptr) ? "<empty file name>" : filename;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700466 env->ThrowNew(fnfe.get(), message);
Calin Juravleb077e152016-02-18 18:47:37 +0000467 return -1;
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700468 }
469
Alex Light6e183f22014-07-18 14:57:04 -0700470 const InstructionSet target_instruction_set = GetInstructionSetFromString(instruction_set);
Andreas Gampe20c89302014-08-19 17:28:06 -0700471 if (target_instruction_set == kNone) {
472 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
473 std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set));
474 env->ThrowNew(iae.get(), message.c_str());
Calin Juravleb077e152016-02-18 18:47:37 +0000475 return -1;
Andreas Gampe20c89302014-08-19 17:28:06 -0700476 }
Alex Light6e183f22014-07-18 14:57:04 -0700477
Andreas Gampe29d38e72016-03-23 15:31:51 +0000478 CompilerFilter::Filter filter;
479 if (!CompilerFilter::ParseCompilerFilter(compiler_filter_name, &filter)) {
480 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
481 std::string message(StringPrintf("Compiler filter %s is invalid.", compiler_filter_name));
482 env->ThrowNew(iae.get(), message.c_str());
483 return -1;
484 }
485
Richard Uhler66d874d2015-01-15 09:37:19 -0800486 // TODO: Verify the dex location is well formed, and throw an IOException if
487 // not?
Andreas Gampe29d38e72016-03-23 15:31:51 +0000488
Richard Uhlerd1472a22016-04-15 15:18:56 -0700489 OatFileAssistant oat_file_assistant(filename, target_instruction_set, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800490
491 // Always treat elements of the bootclasspath as up-to-date.
492 if (oat_file_assistant.IsInBootClassPath()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700493 return OatFileAssistant::kNoDexOptNeeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800494 }
Richard Uhlerd1472a22016-04-15 15:18:56 -0700495 return oat_file_assistant.GetDexOptNeeded(filter, profile_changed);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700496}
497
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100498static jstring DexFile_getDexFileStatus(JNIEnv* env,
499 jclass,
500 jstring javaFilename,
501 jstring javaInstructionSet) {
502 ScopedUtfChars filename(env, javaFilename);
503 if (env->ExceptionCheck()) {
504 return nullptr;
505 }
506
507 ScopedUtfChars instruction_set(env, javaInstructionSet);
508 if (env->ExceptionCheck()) {
509 return nullptr;
510 }
511
512 const InstructionSet target_instruction_set = GetInstructionSetFromString(
513 instruction_set.c_str());
514 if (target_instruction_set == kNone) {
515 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
516 std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set.c_str()));
517 env->ThrowNew(iae.get(), message.c_str());
518 return nullptr;
519 }
520
521 OatFileAssistant oat_file_assistant(filename.c_str(), target_instruction_set,
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100522 false /* load_executable */);
Richard Uhler46cc64f2016-11-14 14:53:55 +0000523 return env->NewStringUTF(oat_file_assistant.GetStatusDump().c_str());
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100524}
525
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700526static jint DexFile_getDexOptNeeded(JNIEnv* env,
527 jclass,
528 jstring javaFilename,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700529 jstring javaInstructionSet,
Andreas Gampec38be812016-03-23 15:03:46 -0700530 jstring javaTargetCompilerFilter,
531 jboolean newProfile) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100532 ScopedUtfChars filename(env, javaFilename);
Andreas Gampe20c89302014-08-19 17:28:06 -0700533 if (env->ExceptionCheck()) {
Calin Juravleb077e152016-02-18 18:47:37 +0000534 return -1;
Andreas Gampe20c89302014-08-19 17:28:06 -0700535 }
536
Narayan Kamath11d9f062014-04-23 20:24:57 +0100537 ScopedUtfChars instruction_set(env, javaInstructionSet);
Andreas Gampe20c89302014-08-19 17:28:06 -0700538 if (env->ExceptionCheck()) {
Calin Juravleb077e152016-02-18 18:47:37 +0000539 return -1;
Andreas Gampe20c89302014-08-19 17:28:06 -0700540 }
Narayan Kamath11d9f062014-04-23 20:24:57 +0100541
Andreas Gampec38be812016-03-23 15:03:46 -0700542 ScopedUtfChars target_compiler_filter(env, javaTargetCompilerFilter);
543 if (env->ExceptionCheck()) {
544 return -1;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000545 }
Andreas Gampec38be812016-03-23 15:03:46 -0700546
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700547 return GetDexOptNeeded(env,
548 filename.c_str(),
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700549 instruction_set.c_str(),
Andreas Gampec38be812016-03-23 15:03:46 -0700550 target_compiler_filter.c_str(),
551 newProfile == JNI_TRUE);
Narayan Kamath11d9f062014-04-23 20:24:57 +0100552}
553
Calin Juravleb077e152016-02-18 18:47:37 +0000554// public API
Narayan Kamath11d9f062014-04-23 20:24:57 +0100555static jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
Richard Uhler06e3f4f2016-05-24 15:42:37 -0700556 ScopedUtfChars filename_utf(env, javaFilename);
557 if (env->ExceptionCheck()) {
558 return JNI_FALSE;
559 }
560
561 const char* filename = filename_utf.c_str();
562 if ((filename == nullptr) || !OS::FileExists(filename)) {
563 LOG(ERROR) << "DexFile_isDexOptNeeded file '" << filename << "' does not exist";
564 ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException"));
565 const char* message = (filename == nullptr) ? "<empty file name>" : filename;
566 env->ThrowNew(fnfe.get(), message);
567 return JNI_FALSE;
568 }
569
Richard Uhlerd1472a22016-04-15 15:18:56 -0700570 OatFileAssistant oat_file_assistant(filename, kRuntimeISA, false);
Richard Uhler06e3f4f2016-05-24 15:42:37 -0700571 return oat_file_assistant.IsUpToDate() ? JNI_FALSE : JNI_TRUE;
Dave Allison39c3bfb2014-01-28 18:33:52 -0800572}
573
Andreas Gampec38be812016-03-23 15:03:46 -0700574static jboolean DexFile_isValidCompilerFilter(JNIEnv* env,
575 jclass javeDexFileClass ATTRIBUTE_UNUSED,
576 jstring javaCompilerFilter) {
577 ScopedUtfChars compiler_filter(env, javaCompilerFilter);
578 if (env->ExceptionCheck()) {
579 return -1;
580 }
581
582 CompilerFilter::Filter filter;
583 return CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)
584 ? JNI_TRUE : JNI_FALSE;
585}
586
587static jboolean DexFile_isProfileGuidedCompilerFilter(JNIEnv* env,
588 jclass javeDexFileClass ATTRIBUTE_UNUSED,
589 jstring javaCompilerFilter) {
590 ScopedUtfChars compiler_filter(env, javaCompilerFilter);
591 if (env->ExceptionCheck()) {
592 return -1;
593 }
594
595 CompilerFilter::Filter filter;
596 if (!CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)) {
597 return JNI_FALSE;
598 }
599 return CompilerFilter::DependsOnProfile(filter) ? JNI_TRUE : JNI_FALSE;
600}
601
Andreas Gampe86a785d2016-03-30 17:19:48 -0700602static jstring DexFile_getNonProfileGuidedCompilerFilter(JNIEnv* env,
603 jclass javeDexFileClass ATTRIBUTE_UNUSED,
604 jstring javaCompilerFilter) {
605 ScopedUtfChars compiler_filter(env, javaCompilerFilter);
606 if (env->ExceptionCheck()) {
607 return nullptr;
608 }
609
610 CompilerFilter::Filter filter;
611 if (!CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)) {
612 return javaCompilerFilter;
613 }
614
615 CompilerFilter::Filter new_filter = CompilerFilter::GetNonProfileDependentFilterFrom(filter);
616
617 // Filter stayed the same, return input.
618 if (filter == new_filter) {
619 return javaCompilerFilter;
620 }
621
622 // Create a new string object and return.
623 std::string new_filter_str = CompilerFilter::NameOfFilter(new_filter);
624 return env->NewStringUTF(new_filter_str.c_str());
625}
626
Nicolas Geoffray741d4262017-05-03 13:08:36 +0100627static jstring DexFile_getSafeModeCompilerFilter(JNIEnv* env,
628 jclass javeDexFileClass ATTRIBUTE_UNUSED,
629 jstring javaCompilerFilter) {
630 ScopedUtfChars compiler_filter(env, javaCompilerFilter);
631 if (env->ExceptionCheck()) {
632 return nullptr;
633 }
634
635 CompilerFilter::Filter filter;
636 if (!CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)) {
637 return javaCompilerFilter;
638 }
639
640 CompilerFilter::Filter new_filter = CompilerFilter::GetSafeModeFilterFrom(filter);
641
642 // Filter stayed the same, return input.
643 if (filter == new_filter) {
644 return javaCompilerFilter;
645 }
646
647 // Create a new string object and return.
648 std::string new_filter_str = CompilerFilter::NameOfFilter(new_filter);
649 return env->NewStringUTF(new_filter_str.c_str());
650}
651
Jeff Hao90671be2016-04-22 14:00:06 -0700652static jboolean DexFile_isBackedByOatFile(JNIEnv* env, jclass, jobject cookie) {
653 const OatFile* oat_file = nullptr;
654 std::vector<const DexFile*> dex_files;
655 if (!ConvertJavaArrayToDexFiles(env, cookie, /*out */ dex_files, /* out */ oat_file)) {
656 DCHECK(env->ExceptionCheck());
657 return false;
658 }
659 return oat_file != nullptr;
660}
661
Calin Juravle367b9d82017-05-15 18:18:39 -0700662static jobjectArray DexFile_getDexFileOutputPaths(JNIEnv* env,
Philip Cuadrab55ad7c2016-07-12 16:37:40 -0700663 jclass,
664 jstring javaFilename,
665 jstring javaInstructionSet) {
666 ScopedUtfChars filename(env, javaFilename);
667 if (env->ExceptionCheck()) {
668 return nullptr;
669 }
670
671 ScopedUtfChars instruction_set(env, javaInstructionSet);
672 if (env->ExceptionCheck()) {
673 return nullptr;
674 }
675
676 const InstructionSet target_instruction_set = GetInstructionSetFromString(
677 instruction_set.c_str());
678 if (target_instruction_set == kNone) {
679 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
680 std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set.c_str()));
681 env->ThrowNew(iae.get(), message.c_str());
682 return nullptr;
683 }
684
685 OatFileAssistant oat_file_assistant(filename.c_str(),
686 target_instruction_set,
Philip Cuadrab55ad7c2016-07-12 16:37:40 -0700687 false /* load_executable */);
688
689 std::unique_ptr<OatFile> best_oat_file = oat_file_assistant.GetBestOatFile();
690 if (best_oat_file == nullptr) {
691 return nullptr;
692 }
693
Calin Juravle367b9d82017-05-15 18:18:39 -0700694 std::string oat_filename = best_oat_file->GetLocation();
695 std::string vdex_filename = GetVdexFilename(best_oat_file->GetLocation());
696
697 ScopedLocalRef<jstring> jvdexFilename(env, env->NewStringUTF(vdex_filename.c_str()));
698 if (jvdexFilename.get() == nullptr) {
699 return nullptr;
700 }
701 ScopedLocalRef<jstring> joatFilename(env, env->NewStringUTF(oat_filename.c_str()));
702 if (joatFilename.get() == nullptr) {
703 return nullptr;
704 }
705
706 // Now create output array and copy the set into it.
707 jobjectArray result = env->NewObjectArray(2,
708 WellKnownClasses::java_lang_String,
709 nullptr);
710 env->SetObjectArrayElement(result, 0, jvdexFilename.get());
711 env->SetObjectArrayElement(result, 1, joatFilename.get());
712
713 return result;
Philip Cuadrab55ad7c2016-07-12 16:37:40 -0700714}
715
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700716static JNINativeMethod gMethods[] = {
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700717 NATIVE_METHOD(DexFile, closeDexFile, "(Ljava/lang/Object;)Z"),
Mathieu Chartier00310e02015-10-17 12:46:42 -0700718 NATIVE_METHOD(DexFile,
719 defineClassNative,
720 "(Ljava/lang/String;"
721 "Ljava/lang/ClassLoader;"
722 "Ljava/lang/Object;"
723 "Ldalvik/system/DexFile;"
724 ")Ljava/lang/Class;"),
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800725 NATIVE_METHOD(DexFile, getClassNameList, "(Ljava/lang/Object;)[Ljava/lang/String;"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700726 NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700727 NATIVE_METHOD(DexFile, getDexOptNeeded,
Andreas Gampec38be812016-03-23 15:03:46 -0700728 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Z)I"),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700729 NATIVE_METHOD(DexFile, openDexFileNative,
Mathieu Chartier689a7002015-11-20 10:29:42 -0800730 "(Ljava/lang/String;"
731 "Ljava/lang/String;"
732 "I"
733 "Ljava/lang/ClassLoader;"
734 "[Ldalvik/system/DexPathList$Element;"
735 ")Ljava/lang/Object;"),
Alex Lightea9465e2017-02-16 15:38:35 -0800736 NATIVE_METHOD(DexFile, createCookieWithDirectBuffer,
737 "(Ljava/nio/ByteBuffer;II)Ljava/lang/Object;"),
738 NATIVE_METHOD(DexFile, createCookieWithArray, "([BII)Ljava/lang/Object;"),
Andreas Gampec38be812016-03-23 15:03:46 -0700739 NATIVE_METHOD(DexFile, isValidCompilerFilter, "(Ljava/lang/String;)Z"),
740 NATIVE_METHOD(DexFile, isProfileGuidedCompilerFilter, "(Ljava/lang/String;)Z"),
Andreas Gampe86a785d2016-03-30 17:19:48 -0700741 NATIVE_METHOD(DexFile,
742 getNonProfileGuidedCompilerFilter,
743 "(Ljava/lang/String;)Ljava/lang/String;"),
Nicolas Geoffray741d4262017-05-03 13:08:36 +0100744 NATIVE_METHOD(DexFile,
745 getSafeModeCompilerFilter,
746 "(Ljava/lang/String;)Ljava/lang/String;"),
Jeff Hao90671be2016-04-22 14:00:06 -0700747 NATIVE_METHOD(DexFile, isBackedByOatFile, "(Ljava/lang/Object;)Z"),
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100748 NATIVE_METHOD(DexFile, getDexFileStatus,
Philip Cuadrab55ad7c2016-07-12 16:37:40 -0700749 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
Calin Juravle367b9d82017-05-15 18:18:39 -0700750 NATIVE_METHOD(DexFile, getDexFileOutputPaths,
751 "(Ljava/lang/String;Ljava/lang/String;)[Ljava/lang/String;")
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700752};
753
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700754void register_dalvik_system_DexFile(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700755 REGISTER_NATIVE_METHODS("dalvik/system/DexFile");
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700756}
757
758} // namespace art