blob: e75d09722097214bfcdf6ca12a96c478b32c3c90 [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"
Mathieu Chartier79c87da2017-10-10 11:54:29 -070029#include "dex_file_loader.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"
Steven Morelande431e272017-07-18 16:53:49 -070035#include "nativehelper/jni_macros.h"
36#include "nativehelper/ScopedLocalRef.h"
37#include "nativehelper/ScopedUtfChars.h"
Narayan Kamath8943c1d2016-05-02 13:14:48 +010038#include "oat_file.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080039#include "oat_file_assistant.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070040#include "oat_file_manager.h"
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -070041#include "os.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070042#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070043#include "scoped_thread_state_change-inl.h"
Calin Juravlebb0b53f2014-05-23 17:33:29 +010044#include "utils.h"
Ian Rogersdd157d72014-05-15 14:47:50 -070045#include "well_known_classes.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070046#include "zip_archive.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070047
48namespace art {
49
Andreas Gampe46ee31b2016-12-14 10:11:49 -080050using android::base::StringPrintf;
51
Mathieu Chartiere58991b2015-10-13 07:59:34 -070052static bool ConvertJavaArrayToDexFiles(
53 JNIEnv* env,
54 jobject arrayObject,
55 /*out*/ std::vector<const DexFile*>& dex_files,
56 /*out*/ const OatFile*& oat_file) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -080057 jarray array = reinterpret_cast<jarray>(arrayObject);
58
59 jsize array_size = env->GetArrayLength(array);
60 if (env->ExceptionCheck() == JNI_TRUE) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -070061 return false;
Andreas Gampe324b9bb2015-02-23 16:33:22 -080062 }
63
64 // TODO: Optimize. On 32bit we can use an int array.
65 jboolean is_long_data_copied;
66 jlong* long_data = env->GetLongArrayElements(reinterpret_cast<jlongArray>(array),
67 &is_long_data_copied);
68 if (env->ExceptionCheck() == JNI_TRUE) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -070069 return false;
Andreas Gampe324b9bb2015-02-23 16:33:22 -080070 }
71
Mathieu Chartiere58991b2015-10-13 07:59:34 -070072 oat_file = reinterpret_cast<const OatFile*>(static_cast<uintptr_t>(long_data[kOatFileIndex]));
73 dex_files.reserve(array_size - 1);
74 for (jsize i = kDexFileIndexStart; i < array_size; ++i) {
75 dex_files.push_back(reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(long_data[i])));
Andreas Gampe324b9bb2015-02-23 16:33:22 -080076 }
77
78 env->ReleaseLongArrayElements(reinterpret_cast<jlongArray>(array), long_data, JNI_ABORT);
Mathieu Chartiere58991b2015-10-13 07:59:34 -070079 return env->ExceptionCheck() != JNI_TRUE;
Andreas Gampe324b9bb2015-02-23 16:33:22 -080080}
81
Mathieu Chartiere58991b2015-10-13 07:59:34 -070082static jlongArray ConvertDexFilesToJavaArray(JNIEnv* env,
83 const OatFile* oat_file,
84 std::vector<std::unique_ptr<const DexFile>>& vec) {
85 // Add one for the oat file.
Mathieu Chartier80b37b72015-10-12 18:13:39 -070086 jlongArray long_array = env->NewLongArray(static_cast<jsize>(kDexFileIndexStart + vec.size()));
Andreas Gampe324b9bb2015-02-23 16:33:22 -080087 if (env->ExceptionCheck() == JNI_TRUE) {
88 return nullptr;
89 }
90
91 jboolean is_long_data_copied;
92 jlong* long_data = env->GetLongArrayElements(long_array, &is_long_data_copied);
93 if (env->ExceptionCheck() == JNI_TRUE) {
94 return nullptr;
95 }
96
Mathieu Chartiere58991b2015-10-13 07:59:34 -070097 long_data[kOatFileIndex] = reinterpret_cast<uintptr_t>(oat_file);
98 for (size_t i = 0; i < vec.size(); ++i) {
99 long_data[kDexFileIndexStart + i] = reinterpret_cast<uintptr_t>(vec[i].get());
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800100 }
101
102 env->ReleaseLongArrayElements(long_array, long_data, 0);
103 if (env->ExceptionCheck() == JNI_TRUE) {
104 return nullptr;
105 }
106
107 // Now release all the unique_ptrs.
108 for (auto& dex_file : vec) {
109 dex_file.release();
110 }
111
112 return long_array;
113}
114
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700115// A smart pointer that provides read-only access to a Java string's UTF chars.
116// Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if
117// passed a null jstring. The correct idiom is:
118//
119// NullableScopedUtfChars name(env, javaName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -0700120// if (env->ExceptionCheck()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700121// return null;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700122// }
123// // ... use name.c_str()
124//
125// TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option.
126class NullableScopedUtfChars {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800127 public:
128 NullableScopedUtfChars(JNIEnv* env, jstring s) : mEnv(env), mString(s) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700129 mUtfChars = (s != nullptr) ? env->GetStringUTFChars(s, nullptr) : nullptr;
Elliott Hughesba8eee12012-01-24 20:25:24 -0800130 }
131
132 ~NullableScopedUtfChars() {
133 if (mUtfChars) {
134 mEnv->ReleaseStringUTFChars(mString, mUtfChars);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700135 }
Elliott Hughesba8eee12012-01-24 20:25:24 -0800136 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700137
Elliott Hughesba8eee12012-01-24 20:25:24 -0800138 const char* c_str() const {
139 return mUtfChars;
140 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700141
Elliott Hughesba8eee12012-01-24 20:25:24 -0800142 size_t size() const {
143 return strlen(mUtfChars);
144 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700145
Elliott Hughesba8eee12012-01-24 20:25:24 -0800146 // Element access.
147 const char& operator[](size_t n) const {
148 return mUtfChars[n];
149 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700150
Elliott Hughesba8eee12012-01-24 20:25:24 -0800151 private:
152 JNIEnv* mEnv;
153 jstring mString;
154 const char* mUtfChars;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700155
Elliott Hughesba8eee12012-01-24 20:25:24 -0800156 // Disallow copy and assignment.
157 NullableScopedUtfChars(const NullableScopedUtfChars&);
158 void operator=(const NullableScopedUtfChars&);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700159};
160
Alex Lightea9465e2017-02-16 15:38:35 -0800161static std::unique_ptr<MemMap> AllocateDexMemoryMap(JNIEnv* env, jint start, jint end) {
162 if (end <= start) {
163 ScopedObjectAccess soa(env);
164 ThrowWrappedIOException("Bad range");
165 return nullptr;
166 }
167
168 std::string error_message;
169 size_t length = static_cast<size_t>(end - start);
170 std::unique_ptr<MemMap> dex_mem_map(MemMap::MapAnonymous("DEX data",
171 nullptr,
172 length,
173 PROT_READ | PROT_WRITE,
174 /* low_4gb */ false,
175 /* reuse */ false,
176 &error_message));
177 if (dex_mem_map == nullptr) {
178 ScopedObjectAccess soa(env);
179 ThrowWrappedIOException("%s", error_message.c_str());
180 }
181 return dex_mem_map;
182}
183
184static const DexFile* CreateDexFile(JNIEnv* env, std::unique_ptr<MemMap> dex_mem_map) {
185 std::string location = StringPrintf("Anonymous-DexFile@%p-%p",
186 dex_mem_map->Begin(),
187 dex_mem_map->End());
188 std::string error_message;
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700189 std::unique_ptr<const DexFile> dex_file(DexFileLoader::Open(location,
190 0,
191 std::move(dex_mem_map),
192 /* verify */ true,
193 /* verify_location */ true,
194 &error_message));
Alex Lightea9465e2017-02-16 15:38:35 -0800195 if (dex_file == nullptr) {
196 ScopedObjectAccess soa(env);
197 ThrowWrappedIOException("%s", error_message.c_str());
198 return nullptr;
199 }
200
201 if (!dex_file->DisableWrite()) {
202 ScopedObjectAccess soa(env);
203 ThrowWrappedIOException("Failed to make dex file read-only");
204 return nullptr;
205 }
206
207 return dex_file.release();
208}
209
210static jobject CreateSingleDexFileCookie(JNIEnv* env, std::unique_ptr<MemMap> data) {
211 std::unique_ptr<const DexFile> dex_file(CreateDexFile(env, std::move(data)));
212 if (dex_file.get() == nullptr) {
213 DCHECK(env->ExceptionCheck());
214 return nullptr;
215 }
216 std::vector<std::unique_ptr<const DexFile>> dex_files;
217 dex_files.push_back(std::move(dex_file));
218 return ConvertDexFilesToJavaArray(env, nullptr, dex_files);
219}
220
221static jobject DexFile_createCookieWithDirectBuffer(JNIEnv* env,
222 jclass,
223 jobject buffer,
224 jint start,
225 jint end) {
226 uint8_t* base_address = reinterpret_cast<uint8_t*>(env->GetDirectBufferAddress(buffer));
227 if (base_address == nullptr) {
228 ScopedObjectAccess soa(env);
229 ThrowWrappedIOException("dexFileBuffer not direct");
230 return 0;
231 }
232
233 std::unique_ptr<MemMap> dex_mem_map(AllocateDexMemoryMap(env, start, end));
234 if (dex_mem_map == nullptr) {
235 DCHECK(Thread::Current()->IsExceptionPending());
236 return 0;
237 }
238
239 size_t length = static_cast<size_t>(end - start);
240 memcpy(dex_mem_map->Begin(), base_address, length);
241 return CreateSingleDexFileCookie(env, std::move(dex_mem_map));
242}
243
244static jobject DexFile_createCookieWithArray(JNIEnv* env,
245 jclass,
246 jbyteArray buffer,
247 jint start,
248 jint end) {
249 std::unique_ptr<MemMap> dex_mem_map(AllocateDexMemoryMap(env, start, end));
250 if (dex_mem_map == nullptr) {
251 DCHECK(Thread::Current()->IsExceptionPending());
252 return 0;
253 }
254
255 auto destination = reinterpret_cast<jbyte*>(dex_mem_map.get()->Begin());
256 env->GetByteArrayRegion(buffer, start, end - start, destination);
257 return CreateSingleDexFileCookie(env, std::move(dex_mem_map));
258}
259
Calin Juravle1f7079b2017-04-18 21:25:37 -0700260// TODO(calin): clean up the unused parameters (here and in libcore).
Mathieu Chartierb190d942015-11-12 10:00:58 -0800261static jobject DexFile_openDexFileNative(JNIEnv* env,
262 jclass,
263 jstring javaSourceName,
Calin Juravle1f7079b2017-04-18 21:25:37 -0700264 jstring javaOutputName ATTRIBUTE_UNUSED,
Mathieu Chartierb190d942015-11-12 10:00:58 -0800265 jint flags ATTRIBUTE_UNUSED,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800266 jobject class_loader,
267 jobjectArray dex_elements) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700268 ScopedUtfChars sourceName(env, javaSourceName);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700269 if (sourceName.c_str() == nullptr) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700270 return 0;
271 }
Calin Juravle1f7079b2017-04-18 21:25:37 -0700272
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700273 Runtime* const runtime = Runtime::Current();
274 ClassLinker* linker = runtime->GetClassLinker();
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800275 std::vector<std::unique_ptr<const DexFile>> dex_files;
Andreas Gampe833a4852014-05-21 18:46:59 -0700276 std::vector<std::string> error_msgs;
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700277 const OatFile* oat_file = nullptr;
Andreas Gampe833a4852014-05-21 18:46:59 -0700278
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700279 dex_files = runtime->GetOatFileManager().OpenDexFilesFromOat(sourceName.c_str(),
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800280 class_loader,
281 dex_elements,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700282 /*out*/ &oat_file,
283 /*out*/ &error_msgs);
Andreas Gampe833a4852014-05-21 18:46:59 -0700284
Richard Uhler66d874d2015-01-15 09:37:19 -0800285 if (!dex_files.empty()) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700286 jlongArray array = ConvertDexFilesToJavaArray(env, oat_file, dex_files);
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800287 if (array == nullptr) {
288 ScopedObjectAccess soa(env);
289 for (auto& dex_file : dex_files) {
Vladimir Markocd556b02017-02-03 11:47:34 +0000290 if (linker->IsDexFileRegistered(soa.Self(), *dex_file)) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800291 dex_file.release();
292 }
293 }
294 }
295 return array;
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700296 } else {
Vladimir Marko60836d52014-01-16 15:53:38 +0000297 ScopedObjectAccess soa(env);
Andreas Gampe329d1882014-04-08 10:32:19 -0700298 CHECK(!error_msgs.empty());
299 // The most important message is at the end. So set up nesting by going forward, which will
300 // wrap the existing exception as a cause for the following one.
301 auto it = error_msgs.begin();
302 auto itEnd = error_msgs.end();
303 for ( ; it != itEnd; ++it) {
304 ThrowWrappedIOException("%s", it->c_str());
305 }
306
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800307 return nullptr;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700308 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700309}
310
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700311static jboolean DexFile_closeDexFile(JNIEnv* env, jclass, jobject cookie) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700312 std::vector<const DexFile*> dex_files;
313 const OatFile* oat_file;
314 if (!ConvertJavaArrayToDexFiles(env, cookie, dex_files, oat_file)) {
315 Thread::Current()->AssertPendingException();
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700316 return JNI_FALSE;
317 }
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700318 Runtime* const runtime = Runtime::Current();
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700319 bool all_deleted = true;
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700320 {
321 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700322 ObjPtr<mirror::Object> dex_files_object = soa.Decode<mirror::Object>(cookie);
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700323 ObjPtr<mirror::LongArray> long_dex_files = dex_files_object->AsLongArray();
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700324 // Delete dex files associated with this dalvik.system.DexFile since there should not be running
325 // code using it. dex_files is a vector due to multidex.
326 ClassLinker* const class_linker = runtime->GetClassLinker();
327 int32_t i = kDexFileIndexStart; // Oat file is at index 0.
328 for (const DexFile* dex_file : dex_files) {
329 if (dex_file != nullptr) {
330 // Only delete the dex file if the dex cache is not found to prevent runtime crashes if there
331 // are calls to DexFile.close while the ART DexFile is still in use.
Vladimir Markocd556b02017-02-03 11:47:34 +0000332 if (!class_linker->IsDexFileRegistered(soa.Self(), *dex_file)) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700333 // Clear the element in the array so that we can call close again.
334 long_dex_files->Set(i, 0);
335 delete dex_file;
336 } else {
337 all_deleted = false;
338 }
339 }
340 ++i;
Andreas Gampe833a4852014-05-21 18:46:59 -0700341 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700342 }
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700343
Mathieu Chartierfdccbd42015-10-14 10:58:41 -0700344 // oat_file can be null if we are running without dex2oat.
345 if (all_deleted && oat_file != nullptr) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700346 // If all of the dex files are no longer in use we can unmap the corresponding oat file.
347 VLOG(class_linker) << "Unregistering " << oat_file;
348 runtime->GetOatFileManager().UnRegisterAndDeleteOatFile(oat_file);
349 }
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700350 return all_deleted ? JNI_TRUE : JNI_FALSE;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700351}
352
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700353static jclass DexFile_defineClassNative(JNIEnv* env,
354 jclass,
355 jstring javaName,
356 jobject javaLoader,
Mathieu Chartier00310e02015-10-17 12:46:42 -0700357 jobject cookie,
358 jobject dexFile) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700359 std::vector<const DexFile*> dex_files;
360 const OatFile* oat_file;
361 if (!ConvertJavaArrayToDexFiles(env, cookie, /*out*/ dex_files, /*out*/ oat_file)) {
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700362 VLOG(class_linker) << "Failed to find dex_file";
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800363 DCHECK(env->ExceptionCheck());
364 return nullptr;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700365 }
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800366
Brian Carlstromdf143242011-10-10 18:05:34 -0700367 ScopedUtfChars class_name(env, javaName);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700368 if (class_name.c_str() == nullptr) {
Brian Carlstrom2e450bf2013-09-06 15:39:46 -0700369 VLOG(class_linker) << "Failed to find class_name";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700370 return nullptr;
Brian Carlstromdf143242011-10-10 18:05:34 -0700371 }
Elliott Hughes95572412011-12-13 18:14:20 -0800372 const std::string descriptor(DotToDescriptor(class_name.c_str()));
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800373 const size_t hash(ComputeModifiedUtf8Hash(descriptor.c_str()));
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700374 for (auto& dex_file : dex_files) {
David Sehr9aa352e2016-09-15 18:13:52 -0700375 const DexFile::ClassDef* dex_class_def =
376 OatDexFile::FindClassDef(*dex_file, descriptor.c_str(), hash);
Andreas Gampe833a4852014-05-21 18:46:59 -0700377 if (dex_class_def != nullptr) {
378 ScopedObjectAccess soa(env);
379 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Andreas Gampe833a4852014-05-21 18:46:59 -0700380 StackHandleScope<1> hs(soa.Self());
381 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700382 hs.NewHandle(soa.Decode<mirror::ClassLoader>(javaLoader)));
Vladimir Markocd556b02017-02-03 11:47:34 +0000383 ObjPtr<mirror::DexCache> dex_cache =
384 class_linker->RegisterDexFile(*dex_file, class_loader.Get());
385 if (dex_cache == nullptr) {
386 // OOME or InternalError (dexFile already registered with a different class loader).
387 soa.Self()->AssertPendingException();
388 return nullptr;
389 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700390 ObjPtr<mirror::Class> result = class_linker->DefineClass(soa.Self(),
391 descriptor.c_str(),
392 hash,
393 class_loader,
394 *dex_file,
395 *dex_class_def);
Mathieu Chartier00310e02015-10-17 12:46:42 -0700396 // Add the used dex file. This only required for the DexFile.loadClass API since normal
397 // class loaders already keep their dex files live.
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700398 class_linker->InsertDexFileInToClassLoader(soa.Decode<mirror::Object>(dexFile),
Mathieu Chartier00310e02015-10-17 12:46:42 -0700399 class_loader.Get());
Andreas Gampe833a4852014-05-21 18:46:59 -0700400 if (result != nullptr) {
Brian Carlstrom667ab7c2014-10-16 19:12:28 -0700401 VLOG(class_linker) << "DexFile_defineClassNative returning " << result
402 << " for " << class_name.c_str();
Andreas Gampe833a4852014-05-21 18:46:59 -0700403 return soa.AddLocalReference<jclass>(result);
404 }
405 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700406 }
Brian Carlstrom667ab7c2014-10-16 19:12:28 -0700407 VLOG(class_linker) << "Failed to find dex_class_def " << class_name.c_str();
Andreas Gampe833a4852014-05-21 18:46:59 -0700408 return nullptr;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700409}
410
Andreas Gampe833a4852014-05-21 18:46:59 -0700411// Needed as a compare functor for sets of const char
412struct CharPointerComparator {
413 bool operator()(const char *str1, const char *str2) const {
414 return strcmp(str1, str2) < 0;
415 }
416};
417
418// Note: this can be an expensive call, as we sort out duplicates in MultiDex files.
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800419static jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jobject cookie) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700420 const OatFile* oat_file = nullptr;
421 std::vector<const DexFile*> dex_files;
422 if (!ConvertJavaArrayToDexFiles(env, cookie, /*out */ dex_files, /* out */ oat_file)) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800423 DCHECK(env->ExceptionCheck());
424 return nullptr;
425 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700426
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800427 // Push all class descriptors into a set. Use set instead of unordered_set as we want to
428 // retrieve all in the end.
429 std::set<const char*, CharPointerComparator> descriptors;
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700430 for (auto& dex_file : dex_files) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800431 for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
432 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
433 const char* descriptor = dex_file->GetClassDescriptor(class_def);
434 descriptors.insert(descriptor);
Andreas Gampe833a4852014-05-21 18:46:59 -0700435 }
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800436 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700437
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800438 // Now create output array and copy the set into it.
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700439 jobjectArray result = env->NewObjectArray(descriptors.size(),
440 WellKnownClasses::java_lang_String,
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800441 nullptr);
442 if (result != nullptr) {
443 auto it = descriptors.begin();
444 auto it_end = descriptors.end();
445 jsize i = 0;
446 for (; it != it_end; it++, ++i) {
447 std::string descriptor(DescriptorToDot(*it));
448 ScopedLocalRef<jstring> jdescriptor(env, env->NewStringUTF(descriptor.c_str()));
449 if (jdescriptor.get() == nullptr) {
450 return nullptr;
Ian Rogersdd157d72014-05-15 14:47:50 -0700451 }
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800452 env->SetObjectArrayElement(result, i, jdescriptor.get());
Ian Rogersdd157d72014-05-15 14:47:50 -0700453 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700454 }
Ian Rogersdd157d72014-05-15 14:47:50 -0700455 return result;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700456}
457
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700458static jint GetDexOptNeeded(JNIEnv* env,
459 const char* filename,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700460 const char* instruction_set,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000461 const char* compiler_filter_name,
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700462 bool profile_changed,
463 bool downgrade) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100464 if ((filename == nullptr) || !OS::FileExists(filename)) {
Richard Uhler95abd042015-03-24 09:51:28 -0700465 LOG(ERROR) << "DexFile_getDexOptNeeded file '" << filename << "' does not exist";
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700466 ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException"));
Narayan Kamath11d9f062014-04-23 20:24:57 +0100467 const char* message = (filename == nullptr) ? "<empty file name>" : filename;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700468 env->ThrowNew(fnfe.get(), message);
Calin Juravleb077e152016-02-18 18:47:37 +0000469 return -1;
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700470 }
471
Alex Light6e183f22014-07-18 14:57:04 -0700472 const InstructionSet target_instruction_set = GetInstructionSetFromString(instruction_set);
Andreas Gampe20c89302014-08-19 17:28:06 -0700473 if (target_instruction_set == kNone) {
474 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
475 std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set));
476 env->ThrowNew(iae.get(), message.c_str());
Calin Juravleb077e152016-02-18 18:47:37 +0000477 return -1;
Andreas Gampe20c89302014-08-19 17:28:06 -0700478 }
Alex Light6e183f22014-07-18 14:57:04 -0700479
Andreas Gampe29d38e72016-03-23 15:31:51 +0000480 CompilerFilter::Filter filter;
481 if (!CompilerFilter::ParseCompilerFilter(compiler_filter_name, &filter)) {
482 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
483 std::string message(StringPrintf("Compiler filter %s is invalid.", compiler_filter_name));
484 env->ThrowNew(iae.get(), message.c_str());
485 return -1;
486 }
487
Richard Uhler66d874d2015-01-15 09:37:19 -0800488 // TODO: Verify the dex location is well formed, and throw an IOException if
489 // not?
Andreas Gampe29d38e72016-03-23 15:31:51 +0000490
Richard Uhlerd1472a22016-04-15 15:18:56 -0700491 OatFileAssistant oat_file_assistant(filename, target_instruction_set, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800492
493 // Always treat elements of the bootclasspath as up-to-date.
494 if (oat_file_assistant.IsInBootClassPath()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700495 return OatFileAssistant::kNoDexOptNeeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800496 }
Calin Juravle44e5efa2017-09-12 00:54:26 -0700497
498 // TODO(calin): Extend DexFile.getDexOptNeeded to accept the class loader context. b/62269291.
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700499 return oat_file_assistant.GetDexOptNeeded(filter, profile_changed, downgrade);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700500}
501
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100502static jstring DexFile_getDexFileStatus(JNIEnv* env,
503 jclass,
504 jstring javaFilename,
505 jstring javaInstructionSet) {
506 ScopedUtfChars filename(env, javaFilename);
507 if (env->ExceptionCheck()) {
508 return nullptr;
509 }
510
511 ScopedUtfChars instruction_set(env, javaInstructionSet);
512 if (env->ExceptionCheck()) {
513 return nullptr;
514 }
515
516 const InstructionSet target_instruction_set = GetInstructionSetFromString(
517 instruction_set.c_str());
518 if (target_instruction_set == kNone) {
519 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
520 std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set.c_str()));
521 env->ThrowNew(iae.get(), message.c_str());
522 return nullptr;
523 }
524
525 OatFileAssistant oat_file_assistant(filename.c_str(), target_instruction_set,
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100526 false /* load_executable */);
Richard Uhler46cc64f2016-11-14 14:53:55 +0000527 return env->NewStringUTF(oat_file_assistant.GetStatusDump().c_str());
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100528}
529
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700530static jint DexFile_getDexOptNeeded(JNIEnv* env,
531 jclass,
532 jstring javaFilename,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700533 jstring javaInstructionSet,
Andreas Gampec38be812016-03-23 15:03:46 -0700534 jstring javaTargetCompilerFilter,
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700535 jboolean newProfile,
536 jboolean downgrade) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100537 ScopedUtfChars filename(env, javaFilename);
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 }
541
Narayan Kamath11d9f062014-04-23 20:24:57 +0100542 ScopedUtfChars instruction_set(env, javaInstructionSet);
Andreas Gampe20c89302014-08-19 17:28:06 -0700543 if (env->ExceptionCheck()) {
Calin Juravleb077e152016-02-18 18:47:37 +0000544 return -1;
Andreas Gampe20c89302014-08-19 17:28:06 -0700545 }
Narayan Kamath11d9f062014-04-23 20:24:57 +0100546
Andreas Gampec38be812016-03-23 15:03:46 -0700547 ScopedUtfChars target_compiler_filter(env, javaTargetCompilerFilter);
548 if (env->ExceptionCheck()) {
549 return -1;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000550 }
Andreas Gampec38be812016-03-23 15:03:46 -0700551
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700552 return GetDexOptNeeded(env,
553 filename.c_str(),
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700554 instruction_set.c_str(),
Andreas Gampec38be812016-03-23 15:03:46 -0700555 target_compiler_filter.c_str(),
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700556 newProfile == JNI_TRUE,
557 downgrade == JNI_TRUE);
Narayan Kamath11d9f062014-04-23 20:24:57 +0100558}
559
Calin Juravleb077e152016-02-18 18:47:37 +0000560// public API
Narayan Kamath11d9f062014-04-23 20:24:57 +0100561static jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
Richard Uhler06e3f4f2016-05-24 15:42:37 -0700562 ScopedUtfChars filename_utf(env, javaFilename);
563 if (env->ExceptionCheck()) {
564 return JNI_FALSE;
565 }
566
567 const char* filename = filename_utf.c_str();
568 if ((filename == nullptr) || !OS::FileExists(filename)) {
569 LOG(ERROR) << "DexFile_isDexOptNeeded file '" << filename << "' does not exist";
570 ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException"));
571 const char* message = (filename == nullptr) ? "<empty file name>" : filename;
572 env->ThrowNew(fnfe.get(), message);
573 return JNI_FALSE;
574 }
575
Richard Uhlerd1472a22016-04-15 15:18:56 -0700576 OatFileAssistant oat_file_assistant(filename, kRuntimeISA, false);
Richard Uhler06e3f4f2016-05-24 15:42:37 -0700577 return oat_file_assistant.IsUpToDate() ? JNI_FALSE : JNI_TRUE;
Dave Allison39c3bfb2014-01-28 18:33:52 -0800578}
579
Andreas Gampec38be812016-03-23 15:03:46 -0700580static jboolean DexFile_isValidCompilerFilter(JNIEnv* env,
581 jclass javeDexFileClass ATTRIBUTE_UNUSED,
582 jstring javaCompilerFilter) {
583 ScopedUtfChars compiler_filter(env, javaCompilerFilter);
584 if (env->ExceptionCheck()) {
585 return -1;
586 }
587
588 CompilerFilter::Filter filter;
589 return CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)
590 ? JNI_TRUE : JNI_FALSE;
591}
592
593static jboolean DexFile_isProfileGuidedCompilerFilter(JNIEnv* env,
594 jclass javeDexFileClass ATTRIBUTE_UNUSED,
595 jstring javaCompilerFilter) {
596 ScopedUtfChars compiler_filter(env, javaCompilerFilter);
597 if (env->ExceptionCheck()) {
598 return -1;
599 }
600
601 CompilerFilter::Filter filter;
602 if (!CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)) {
603 return JNI_FALSE;
604 }
605 return CompilerFilter::DependsOnProfile(filter) ? JNI_TRUE : JNI_FALSE;
606}
607
Andreas Gampe86a785d2016-03-30 17:19:48 -0700608static jstring DexFile_getNonProfileGuidedCompilerFilter(JNIEnv* env,
609 jclass javeDexFileClass ATTRIBUTE_UNUSED,
610 jstring javaCompilerFilter) {
611 ScopedUtfChars compiler_filter(env, javaCompilerFilter);
612 if (env->ExceptionCheck()) {
613 return nullptr;
614 }
615
616 CompilerFilter::Filter filter;
617 if (!CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)) {
618 return javaCompilerFilter;
619 }
620
621 CompilerFilter::Filter new_filter = CompilerFilter::GetNonProfileDependentFilterFrom(filter);
622
623 // Filter stayed the same, return input.
624 if (filter == new_filter) {
625 return javaCompilerFilter;
626 }
627
628 // Create a new string object and return.
629 std::string new_filter_str = CompilerFilter::NameOfFilter(new_filter);
630 return env->NewStringUTF(new_filter_str.c_str());
631}
632
Nicolas Geoffray741d4262017-05-03 13:08:36 +0100633static jstring DexFile_getSafeModeCompilerFilter(JNIEnv* env,
634 jclass javeDexFileClass ATTRIBUTE_UNUSED,
635 jstring javaCompilerFilter) {
636 ScopedUtfChars compiler_filter(env, javaCompilerFilter);
637 if (env->ExceptionCheck()) {
638 return nullptr;
639 }
640
641 CompilerFilter::Filter filter;
642 if (!CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)) {
643 return javaCompilerFilter;
644 }
645
646 CompilerFilter::Filter new_filter = CompilerFilter::GetSafeModeFilterFrom(filter);
647
648 // Filter stayed the same, return input.
649 if (filter == new_filter) {
650 return javaCompilerFilter;
651 }
652
653 // Create a new string object and return.
654 std::string new_filter_str = CompilerFilter::NameOfFilter(new_filter);
655 return env->NewStringUTF(new_filter_str.c_str());
656}
657
Jeff Hao90671be2016-04-22 14:00:06 -0700658static jboolean DexFile_isBackedByOatFile(JNIEnv* env, jclass, jobject cookie) {
659 const OatFile* oat_file = nullptr;
660 std::vector<const DexFile*> dex_files;
661 if (!ConvertJavaArrayToDexFiles(env, cookie, /*out */ dex_files, /* out */ oat_file)) {
662 DCHECK(env->ExceptionCheck());
663 return false;
664 }
665 return oat_file != nullptr;
666}
667
Calin Juravle367b9d82017-05-15 18:18:39 -0700668static jobjectArray DexFile_getDexFileOutputPaths(JNIEnv* env,
Philip Cuadrab55ad7c2016-07-12 16:37:40 -0700669 jclass,
670 jstring javaFilename,
671 jstring javaInstructionSet) {
672 ScopedUtfChars filename(env, javaFilename);
673 if (env->ExceptionCheck()) {
674 return nullptr;
675 }
676
677 ScopedUtfChars instruction_set(env, javaInstructionSet);
678 if (env->ExceptionCheck()) {
679 return nullptr;
680 }
681
682 const InstructionSet target_instruction_set = GetInstructionSetFromString(
683 instruction_set.c_str());
684 if (target_instruction_set == kNone) {
685 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
686 std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set.c_str()));
687 env->ThrowNew(iae.get(), message.c_str());
688 return nullptr;
689 }
690
691 OatFileAssistant oat_file_assistant(filename.c_str(),
692 target_instruction_set,
Philip Cuadrab55ad7c2016-07-12 16:37:40 -0700693 false /* load_executable */);
694
695 std::unique_ptr<OatFile> best_oat_file = oat_file_assistant.GetBestOatFile();
696 if (best_oat_file == nullptr) {
697 return nullptr;
698 }
699
Calin Juravle367b9d82017-05-15 18:18:39 -0700700 std::string oat_filename = best_oat_file->GetLocation();
701 std::string vdex_filename = GetVdexFilename(best_oat_file->GetLocation());
702
703 ScopedLocalRef<jstring> jvdexFilename(env, env->NewStringUTF(vdex_filename.c_str()));
704 if (jvdexFilename.get() == nullptr) {
705 return nullptr;
706 }
707 ScopedLocalRef<jstring> joatFilename(env, env->NewStringUTF(oat_filename.c_str()));
708 if (joatFilename.get() == nullptr) {
709 return nullptr;
710 }
711
712 // Now create output array and copy the set into it.
713 jobjectArray result = env->NewObjectArray(2,
714 WellKnownClasses::java_lang_String,
715 nullptr);
716 env->SetObjectArrayElement(result, 0, jvdexFilename.get());
717 env->SetObjectArrayElement(result, 1, joatFilename.get());
718
719 return result;
Philip Cuadrab55ad7c2016-07-12 16:37:40 -0700720}
721
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700722static JNINativeMethod gMethods[] = {
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700723 NATIVE_METHOD(DexFile, closeDexFile, "(Ljava/lang/Object;)Z"),
Mathieu Chartier00310e02015-10-17 12:46:42 -0700724 NATIVE_METHOD(DexFile,
725 defineClassNative,
726 "(Ljava/lang/String;"
727 "Ljava/lang/ClassLoader;"
728 "Ljava/lang/Object;"
729 "Ldalvik/system/DexFile;"
730 ")Ljava/lang/Class;"),
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800731 NATIVE_METHOD(DexFile, getClassNameList, "(Ljava/lang/Object;)[Ljava/lang/String;"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700732 NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700733 NATIVE_METHOD(DexFile, getDexOptNeeded,
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700734 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ZZ)I"),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700735 NATIVE_METHOD(DexFile, openDexFileNative,
Mathieu Chartier689a7002015-11-20 10:29:42 -0800736 "(Ljava/lang/String;"
737 "Ljava/lang/String;"
738 "I"
739 "Ljava/lang/ClassLoader;"
740 "[Ldalvik/system/DexPathList$Element;"
741 ")Ljava/lang/Object;"),
Alex Lightea9465e2017-02-16 15:38:35 -0800742 NATIVE_METHOD(DexFile, createCookieWithDirectBuffer,
743 "(Ljava/nio/ByteBuffer;II)Ljava/lang/Object;"),
744 NATIVE_METHOD(DexFile, createCookieWithArray, "([BII)Ljava/lang/Object;"),
Andreas Gampec38be812016-03-23 15:03:46 -0700745 NATIVE_METHOD(DexFile, isValidCompilerFilter, "(Ljava/lang/String;)Z"),
746 NATIVE_METHOD(DexFile, isProfileGuidedCompilerFilter, "(Ljava/lang/String;)Z"),
Andreas Gampe86a785d2016-03-30 17:19:48 -0700747 NATIVE_METHOD(DexFile,
748 getNonProfileGuidedCompilerFilter,
749 "(Ljava/lang/String;)Ljava/lang/String;"),
Nicolas Geoffray741d4262017-05-03 13:08:36 +0100750 NATIVE_METHOD(DexFile,
751 getSafeModeCompilerFilter,
752 "(Ljava/lang/String;)Ljava/lang/String;"),
Jeff Hao90671be2016-04-22 14:00:06 -0700753 NATIVE_METHOD(DexFile, isBackedByOatFile, "(Ljava/lang/Object;)Z"),
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100754 NATIVE_METHOD(DexFile, getDexFileStatus,
Philip Cuadrab55ad7c2016-07-12 16:37:40 -0700755 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
Calin Juravle367b9d82017-05-15 18:18:39 -0700756 NATIVE_METHOD(DexFile, getDexFileOutputPaths,
757 "(Ljava/lang/String;Ljava/lang/String;)[Ljava/lang/String;")
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700758};
759
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700760void register_dalvik_system_DexFile(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700761 REGISTER_NATIVE_METHODS("dalvik/system/DexFile");
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700762}
763
764} // namespace art