blob: 0617dae1ae20f77752020a2a3fcfe96ccb54d7dd [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"
Narayan Kamath8943c1d2016-05-02 13:14:48 +010033#include "oat_file.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080034#include "oat_file_assistant.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070035#include "oat_file_manager.h"
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -070036#include "os.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070037#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070038#include "scoped_thread_state_change-inl.h"
Ian Rogersc9818482012-01-11 08:52:51 -080039#include "ScopedLocalRef.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070040#include "ScopedUtfChars.h"
Calin Juravlebb0b53f2014-05-23 17:33:29 +010041#include "utils.h"
Ian Rogersdd157d72014-05-15 14:47:50 -070042#include "well_known_classes.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070043#include "zip_archive.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070044
45namespace art {
46
Andreas Gampe46ee31b2016-12-14 10:11:49 -080047using android::base::StringPrintf;
48
Mathieu Chartiere58991b2015-10-13 07:59:34 -070049static bool ConvertJavaArrayToDexFiles(
50 JNIEnv* env,
51 jobject arrayObject,
52 /*out*/ std::vector<const DexFile*>& dex_files,
53 /*out*/ const OatFile*& oat_file) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -080054 jarray array = reinterpret_cast<jarray>(arrayObject);
55
56 jsize array_size = env->GetArrayLength(array);
57 if (env->ExceptionCheck() == JNI_TRUE) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -070058 return false;
Andreas Gampe324b9bb2015-02-23 16:33:22 -080059 }
60
61 // TODO: Optimize. On 32bit we can use an int array.
62 jboolean is_long_data_copied;
63 jlong* long_data = env->GetLongArrayElements(reinterpret_cast<jlongArray>(array),
64 &is_long_data_copied);
65 if (env->ExceptionCheck() == JNI_TRUE) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -070066 return false;
Andreas Gampe324b9bb2015-02-23 16:33:22 -080067 }
68
Mathieu Chartiere58991b2015-10-13 07:59:34 -070069 oat_file = reinterpret_cast<const OatFile*>(static_cast<uintptr_t>(long_data[kOatFileIndex]));
70 dex_files.reserve(array_size - 1);
71 for (jsize i = kDexFileIndexStart; i < array_size; ++i) {
72 dex_files.push_back(reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(long_data[i])));
Andreas Gampe324b9bb2015-02-23 16:33:22 -080073 }
74
75 env->ReleaseLongArrayElements(reinterpret_cast<jlongArray>(array), long_data, JNI_ABORT);
Mathieu Chartiere58991b2015-10-13 07:59:34 -070076 return env->ExceptionCheck() != JNI_TRUE;
Andreas Gampe324b9bb2015-02-23 16:33:22 -080077}
78
Mathieu Chartiere58991b2015-10-13 07:59:34 -070079static jlongArray ConvertDexFilesToJavaArray(JNIEnv* env,
80 const OatFile* oat_file,
81 std::vector<std::unique_ptr<const DexFile>>& vec) {
82 // Add one for the oat file.
Mathieu Chartier80b37b72015-10-12 18:13:39 -070083 jlongArray long_array = env->NewLongArray(static_cast<jsize>(kDexFileIndexStart + vec.size()));
Andreas Gampe324b9bb2015-02-23 16:33:22 -080084 if (env->ExceptionCheck() == JNI_TRUE) {
85 return nullptr;
86 }
87
88 jboolean is_long_data_copied;
89 jlong* long_data = env->GetLongArrayElements(long_array, &is_long_data_copied);
90 if (env->ExceptionCheck() == JNI_TRUE) {
91 return nullptr;
92 }
93
Mathieu Chartiere58991b2015-10-13 07:59:34 -070094 long_data[kOatFileIndex] = reinterpret_cast<uintptr_t>(oat_file);
95 for (size_t i = 0; i < vec.size(); ++i) {
96 long_data[kDexFileIndexStart + i] = reinterpret_cast<uintptr_t>(vec[i].get());
Andreas Gampe324b9bb2015-02-23 16:33:22 -080097 }
98
99 env->ReleaseLongArrayElements(long_array, long_data, 0);
100 if (env->ExceptionCheck() == JNI_TRUE) {
101 return nullptr;
102 }
103
104 // Now release all the unique_ptrs.
105 for (auto& dex_file : vec) {
106 dex_file.release();
107 }
108
109 return long_array;
110}
111
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700112// A smart pointer that provides read-only access to a Java string's UTF chars.
113// Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if
114// passed a null jstring. The correct idiom is:
115//
116// NullableScopedUtfChars name(env, javaName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -0700117// if (env->ExceptionCheck()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700118// return null;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700119// }
120// // ... use name.c_str()
121//
122// TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option.
123class NullableScopedUtfChars {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800124 public:
125 NullableScopedUtfChars(JNIEnv* env, jstring s) : mEnv(env), mString(s) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700126 mUtfChars = (s != nullptr) ? env->GetStringUTFChars(s, nullptr) : nullptr;
Elliott Hughesba8eee12012-01-24 20:25:24 -0800127 }
128
129 ~NullableScopedUtfChars() {
130 if (mUtfChars) {
131 mEnv->ReleaseStringUTFChars(mString, mUtfChars);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700132 }
Elliott Hughesba8eee12012-01-24 20:25:24 -0800133 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700134
Elliott Hughesba8eee12012-01-24 20:25:24 -0800135 const char* c_str() const {
136 return mUtfChars;
137 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700138
Elliott Hughesba8eee12012-01-24 20:25:24 -0800139 size_t size() const {
140 return strlen(mUtfChars);
141 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700142
Elliott Hughesba8eee12012-01-24 20:25:24 -0800143 // Element access.
144 const char& operator[](size_t n) const {
145 return mUtfChars[n];
146 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700147
Elliott Hughesba8eee12012-01-24 20:25:24 -0800148 private:
149 JNIEnv* mEnv;
150 jstring mString;
151 const char* mUtfChars;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700152
Elliott Hughesba8eee12012-01-24 20:25:24 -0800153 // Disallow copy and assignment.
154 NullableScopedUtfChars(const NullableScopedUtfChars&);
155 void operator=(const NullableScopedUtfChars&);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700156};
157
Alex Lightea9465e2017-02-16 15:38:35 -0800158static std::unique_ptr<MemMap> AllocateDexMemoryMap(JNIEnv* env, jint start, jint end) {
159 if (end <= start) {
160 ScopedObjectAccess soa(env);
161 ThrowWrappedIOException("Bad range");
162 return nullptr;
163 }
164
165 std::string error_message;
166 size_t length = static_cast<size_t>(end - start);
167 std::unique_ptr<MemMap> dex_mem_map(MemMap::MapAnonymous("DEX data",
168 nullptr,
169 length,
170 PROT_READ | PROT_WRITE,
171 /* low_4gb */ false,
172 /* reuse */ false,
173 &error_message));
174 if (dex_mem_map == nullptr) {
175 ScopedObjectAccess soa(env);
176 ThrowWrappedIOException("%s", error_message.c_str());
177 }
178 return dex_mem_map;
179}
180
181static const DexFile* CreateDexFile(JNIEnv* env, std::unique_ptr<MemMap> dex_mem_map) {
182 std::string location = StringPrintf("Anonymous-DexFile@%p-%p",
183 dex_mem_map->Begin(),
184 dex_mem_map->End());
185 std::string error_message;
186 std::unique_ptr<const DexFile> dex_file(DexFile::Open(location,
187 0,
188 std::move(dex_mem_map),
189 /* verify */ true,
190 /* verify_location */ true,
191 &error_message));
192 if (dex_file == nullptr) {
193 ScopedObjectAccess soa(env);
194 ThrowWrappedIOException("%s", error_message.c_str());
195 return nullptr;
196 }
197
198 if (!dex_file->DisableWrite()) {
199 ScopedObjectAccess soa(env);
200 ThrowWrappedIOException("Failed to make dex file read-only");
201 return nullptr;
202 }
203
204 return dex_file.release();
205}
206
207static jobject CreateSingleDexFileCookie(JNIEnv* env, std::unique_ptr<MemMap> data) {
208 std::unique_ptr<const DexFile> dex_file(CreateDexFile(env, std::move(data)));
209 if (dex_file.get() == nullptr) {
210 DCHECK(env->ExceptionCheck());
211 return nullptr;
212 }
213 std::vector<std::unique_ptr<const DexFile>> dex_files;
214 dex_files.push_back(std::move(dex_file));
215 return ConvertDexFilesToJavaArray(env, nullptr, dex_files);
216}
217
218static jobject DexFile_createCookieWithDirectBuffer(JNIEnv* env,
219 jclass,
220 jobject buffer,
221 jint start,
222 jint end) {
223 uint8_t* base_address = reinterpret_cast<uint8_t*>(env->GetDirectBufferAddress(buffer));
224 if (base_address == nullptr) {
225 ScopedObjectAccess soa(env);
226 ThrowWrappedIOException("dexFileBuffer not direct");
227 return 0;
228 }
229
230 std::unique_ptr<MemMap> dex_mem_map(AllocateDexMemoryMap(env, start, end));
231 if (dex_mem_map == nullptr) {
232 DCHECK(Thread::Current()->IsExceptionPending());
233 return 0;
234 }
235
236 size_t length = static_cast<size_t>(end - start);
237 memcpy(dex_mem_map->Begin(), base_address, length);
238 return CreateSingleDexFileCookie(env, std::move(dex_mem_map));
239}
240
241static jobject DexFile_createCookieWithArray(JNIEnv* env,
242 jclass,
243 jbyteArray buffer,
244 jint start,
245 jint end) {
246 std::unique_ptr<MemMap> dex_mem_map(AllocateDexMemoryMap(env, start, end));
247 if (dex_mem_map == nullptr) {
248 DCHECK(Thread::Current()->IsExceptionPending());
249 return 0;
250 }
251
252 auto destination = reinterpret_cast<jbyte*>(dex_mem_map.get()->Begin());
253 env->GetByteArrayRegion(buffer, start, end - start, destination);
254 return CreateSingleDexFileCookie(env, std::move(dex_mem_map));
255}
256
Calin Juravle1f7079b2017-04-18 21:25:37 -0700257// TODO(calin): clean up the unused parameters (here and in libcore).
Mathieu Chartierb190d942015-11-12 10:00:58 -0800258static jobject DexFile_openDexFileNative(JNIEnv* env,
259 jclass,
260 jstring javaSourceName,
Calin Juravle1f7079b2017-04-18 21:25:37 -0700261 jstring javaOutputName ATTRIBUTE_UNUSED,
Mathieu Chartierb190d942015-11-12 10:00:58 -0800262 jint flags ATTRIBUTE_UNUSED,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800263 jobject class_loader,
264 jobjectArray dex_elements) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700265 ScopedUtfChars sourceName(env, javaSourceName);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700266 if (sourceName.c_str() == nullptr) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700267 return 0;
268 }
Calin Juravle1f7079b2017-04-18 21:25:37 -0700269
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700270 Runtime* const runtime = Runtime::Current();
271 ClassLinker* linker = runtime->GetClassLinker();
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800272 std::vector<std::unique_ptr<const DexFile>> dex_files;
Andreas Gampe833a4852014-05-21 18:46:59 -0700273 std::vector<std::string> error_msgs;
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700274 const OatFile* oat_file = nullptr;
Andreas Gampe833a4852014-05-21 18:46:59 -0700275
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700276 dex_files = runtime->GetOatFileManager().OpenDexFilesFromOat(sourceName.c_str(),
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800277 class_loader,
278 dex_elements,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700279 /*out*/ &oat_file,
280 /*out*/ &error_msgs);
Andreas Gampe833a4852014-05-21 18:46:59 -0700281
Richard Uhler66d874d2015-01-15 09:37:19 -0800282 if (!dex_files.empty()) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700283 jlongArray array = ConvertDexFilesToJavaArray(env, oat_file, dex_files);
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800284 if (array == nullptr) {
285 ScopedObjectAccess soa(env);
286 for (auto& dex_file : dex_files) {
Vladimir Markocd556b02017-02-03 11:47:34 +0000287 if (linker->IsDexFileRegistered(soa.Self(), *dex_file)) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800288 dex_file.release();
289 }
290 }
291 }
292 return array;
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700293 } else {
Vladimir Marko60836d52014-01-16 15:53:38 +0000294 ScopedObjectAccess soa(env);
Andreas Gampe329d1882014-04-08 10:32:19 -0700295 CHECK(!error_msgs.empty());
296 // The most important message is at the end. So set up nesting by going forward, which will
297 // wrap the existing exception as a cause for the following one.
298 auto it = error_msgs.begin();
299 auto itEnd = error_msgs.end();
300 for ( ; it != itEnd; ++it) {
301 ThrowWrappedIOException("%s", it->c_str());
302 }
303
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800304 return nullptr;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700305 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700306}
307
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700308static jboolean DexFile_closeDexFile(JNIEnv* env, jclass, jobject cookie) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700309 std::vector<const DexFile*> dex_files;
310 const OatFile* oat_file;
311 if (!ConvertJavaArrayToDexFiles(env, cookie, dex_files, oat_file)) {
312 Thread::Current()->AssertPendingException();
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700313 return JNI_FALSE;
314 }
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700315 Runtime* const runtime = Runtime::Current();
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700316 bool all_deleted = true;
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700317 {
318 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700319 ObjPtr<mirror::Object> dex_files_object = soa.Decode<mirror::Object>(cookie);
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700320 ObjPtr<mirror::LongArray> long_dex_files = dex_files_object->AsLongArray();
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700321 // Delete dex files associated with this dalvik.system.DexFile since there should not be running
322 // code using it. dex_files is a vector due to multidex.
323 ClassLinker* const class_linker = runtime->GetClassLinker();
324 int32_t i = kDexFileIndexStart; // Oat file is at index 0.
325 for (const DexFile* dex_file : dex_files) {
326 if (dex_file != nullptr) {
327 // Only delete the dex file if the dex cache is not found to prevent runtime crashes if there
328 // are calls to DexFile.close while the ART DexFile is still in use.
Vladimir Markocd556b02017-02-03 11:47:34 +0000329 if (!class_linker->IsDexFileRegistered(soa.Self(), *dex_file)) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700330 // Clear the element in the array so that we can call close again.
331 long_dex_files->Set(i, 0);
332 delete dex_file;
333 } else {
334 all_deleted = false;
335 }
336 }
337 ++i;
Andreas Gampe833a4852014-05-21 18:46:59 -0700338 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700339 }
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700340
Mathieu Chartierfdccbd42015-10-14 10:58:41 -0700341 // oat_file can be null if we are running without dex2oat.
342 if (all_deleted && oat_file != nullptr) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700343 // If all of the dex files are no longer in use we can unmap the corresponding oat file.
344 VLOG(class_linker) << "Unregistering " << oat_file;
345 runtime->GetOatFileManager().UnRegisterAndDeleteOatFile(oat_file);
346 }
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700347 return all_deleted ? JNI_TRUE : JNI_FALSE;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700348}
349
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700350static jclass DexFile_defineClassNative(JNIEnv* env,
351 jclass,
352 jstring javaName,
353 jobject javaLoader,
Mathieu Chartier00310e02015-10-17 12:46:42 -0700354 jobject cookie,
355 jobject dexFile) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700356 std::vector<const DexFile*> dex_files;
357 const OatFile* oat_file;
358 if (!ConvertJavaArrayToDexFiles(env, cookie, /*out*/ dex_files, /*out*/ oat_file)) {
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700359 VLOG(class_linker) << "Failed to find dex_file";
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800360 DCHECK(env->ExceptionCheck());
361 return nullptr;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700362 }
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800363
Brian Carlstromdf143242011-10-10 18:05:34 -0700364 ScopedUtfChars class_name(env, javaName);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700365 if (class_name.c_str() == nullptr) {
Brian Carlstrom2e450bf2013-09-06 15:39:46 -0700366 VLOG(class_linker) << "Failed to find class_name";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700367 return nullptr;
Brian Carlstromdf143242011-10-10 18:05:34 -0700368 }
Elliott Hughes95572412011-12-13 18:14:20 -0800369 const std::string descriptor(DotToDescriptor(class_name.c_str()));
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800370 const size_t hash(ComputeModifiedUtf8Hash(descriptor.c_str()));
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700371 for (auto& dex_file : dex_files) {
David Sehr9aa352e2016-09-15 18:13:52 -0700372 const DexFile::ClassDef* dex_class_def =
373 OatDexFile::FindClassDef(*dex_file, descriptor.c_str(), hash);
Andreas Gampe833a4852014-05-21 18:46:59 -0700374 if (dex_class_def != nullptr) {
375 ScopedObjectAccess soa(env);
376 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Andreas Gampe833a4852014-05-21 18:46:59 -0700377 StackHandleScope<1> hs(soa.Self());
378 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700379 hs.NewHandle(soa.Decode<mirror::ClassLoader>(javaLoader)));
Vladimir Markocd556b02017-02-03 11:47:34 +0000380 ObjPtr<mirror::DexCache> dex_cache =
381 class_linker->RegisterDexFile(*dex_file, class_loader.Get());
382 if (dex_cache == nullptr) {
383 // OOME or InternalError (dexFile already registered with a different class loader).
384 soa.Self()->AssertPendingException();
385 return nullptr;
386 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700387 ObjPtr<mirror::Class> result = class_linker->DefineClass(soa.Self(),
388 descriptor.c_str(),
389 hash,
390 class_loader,
391 *dex_file,
392 *dex_class_def);
Mathieu Chartier00310e02015-10-17 12:46:42 -0700393 // Add the used dex file. This only required for the DexFile.loadClass API since normal
394 // class loaders already keep their dex files live.
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700395 class_linker->InsertDexFileInToClassLoader(soa.Decode<mirror::Object>(dexFile),
Mathieu Chartier00310e02015-10-17 12:46:42 -0700396 class_loader.Get());
Andreas Gampe833a4852014-05-21 18:46:59 -0700397 if (result != nullptr) {
Brian Carlstrom667ab7c2014-10-16 19:12:28 -0700398 VLOG(class_linker) << "DexFile_defineClassNative returning " << result
399 << " for " << class_name.c_str();
Andreas Gampe833a4852014-05-21 18:46:59 -0700400 return soa.AddLocalReference<jclass>(result);
401 }
402 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700403 }
Brian Carlstrom667ab7c2014-10-16 19:12:28 -0700404 VLOG(class_linker) << "Failed to find dex_class_def " << class_name.c_str();
Andreas Gampe833a4852014-05-21 18:46:59 -0700405 return nullptr;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700406}
407
Andreas Gampe833a4852014-05-21 18:46:59 -0700408// Needed as a compare functor for sets of const char
409struct CharPointerComparator {
410 bool operator()(const char *str1, const char *str2) const {
411 return strcmp(str1, str2) < 0;
412 }
413};
414
415// Note: this can be an expensive call, as we sort out duplicates in MultiDex files.
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800416static jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jobject cookie) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700417 const OatFile* oat_file = nullptr;
418 std::vector<const DexFile*> dex_files;
419 if (!ConvertJavaArrayToDexFiles(env, cookie, /*out */ dex_files, /* out */ oat_file)) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800420 DCHECK(env->ExceptionCheck());
421 return nullptr;
422 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700423
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800424 // Push all class descriptors into a set. Use set instead of unordered_set as we want to
425 // retrieve all in the end.
426 std::set<const char*, CharPointerComparator> descriptors;
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700427 for (auto& dex_file : dex_files) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800428 for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
429 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
430 const char* descriptor = dex_file->GetClassDescriptor(class_def);
431 descriptors.insert(descriptor);
Andreas Gampe833a4852014-05-21 18:46:59 -0700432 }
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800433 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700434
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800435 // Now create output array and copy the set into it.
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700436 jobjectArray result = env->NewObjectArray(descriptors.size(),
437 WellKnownClasses::java_lang_String,
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800438 nullptr);
439 if (result != nullptr) {
440 auto it = descriptors.begin();
441 auto it_end = descriptors.end();
442 jsize i = 0;
443 for (; it != it_end; it++, ++i) {
444 std::string descriptor(DescriptorToDot(*it));
445 ScopedLocalRef<jstring> jdescriptor(env, env->NewStringUTF(descriptor.c_str()));
446 if (jdescriptor.get() == nullptr) {
447 return nullptr;
Ian Rogersdd157d72014-05-15 14:47:50 -0700448 }
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800449 env->SetObjectArrayElement(result, i, jdescriptor.get());
Ian Rogersdd157d72014-05-15 14:47:50 -0700450 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700451 }
Ian Rogersdd157d72014-05-15 14:47:50 -0700452 return result;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700453}
454
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700455static jint GetDexOptNeeded(JNIEnv* env,
456 const char* filename,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700457 const char* instruction_set,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000458 const char* compiler_filter_name,
459 bool profile_changed) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100460 if ((filename == nullptr) || !OS::FileExists(filename)) {
Richard Uhler95abd042015-03-24 09:51:28 -0700461 LOG(ERROR) << "DexFile_getDexOptNeeded file '" << filename << "' does not exist";
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700462 ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException"));
Narayan Kamath11d9f062014-04-23 20:24:57 +0100463 const char* message = (filename == nullptr) ? "<empty file name>" : filename;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700464 env->ThrowNew(fnfe.get(), message);
Calin Juravleb077e152016-02-18 18:47:37 +0000465 return -1;
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700466 }
467
Alex Light6e183f22014-07-18 14:57:04 -0700468 const InstructionSet target_instruction_set = GetInstructionSetFromString(instruction_set);
Andreas Gampe20c89302014-08-19 17:28:06 -0700469 if (target_instruction_set == kNone) {
470 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
471 std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set));
472 env->ThrowNew(iae.get(), message.c_str());
Calin Juravleb077e152016-02-18 18:47:37 +0000473 return -1;
Andreas Gampe20c89302014-08-19 17:28:06 -0700474 }
Alex Light6e183f22014-07-18 14:57:04 -0700475
Andreas Gampe29d38e72016-03-23 15:31:51 +0000476 CompilerFilter::Filter filter;
477 if (!CompilerFilter::ParseCompilerFilter(compiler_filter_name, &filter)) {
478 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
479 std::string message(StringPrintf("Compiler filter %s is invalid.", compiler_filter_name));
480 env->ThrowNew(iae.get(), message.c_str());
481 return -1;
482 }
483
Richard Uhler66d874d2015-01-15 09:37:19 -0800484 // TODO: Verify the dex location is well formed, and throw an IOException if
485 // not?
Andreas Gampe29d38e72016-03-23 15:31:51 +0000486
Richard Uhlerd1472a22016-04-15 15:18:56 -0700487 OatFileAssistant oat_file_assistant(filename, target_instruction_set, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800488
489 // Always treat elements of the bootclasspath as up-to-date.
490 if (oat_file_assistant.IsInBootClassPath()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700491 return OatFileAssistant::kNoDexOptNeeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800492 }
Richard Uhlerd1472a22016-04-15 15:18:56 -0700493 return oat_file_assistant.GetDexOptNeeded(filter, profile_changed);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700494}
495
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100496static jstring DexFile_getDexFileStatus(JNIEnv* env,
497 jclass,
498 jstring javaFilename,
499 jstring javaInstructionSet) {
500 ScopedUtfChars filename(env, javaFilename);
501 if (env->ExceptionCheck()) {
502 return nullptr;
503 }
504
505 ScopedUtfChars instruction_set(env, javaInstructionSet);
506 if (env->ExceptionCheck()) {
507 return nullptr;
508 }
509
510 const InstructionSet target_instruction_set = GetInstructionSetFromString(
511 instruction_set.c_str());
512 if (target_instruction_set == kNone) {
513 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
514 std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set.c_str()));
515 env->ThrowNew(iae.get(), message.c_str());
516 return nullptr;
517 }
518
519 OatFileAssistant oat_file_assistant(filename.c_str(), target_instruction_set,
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100520 false /* load_executable */);
Richard Uhler46cc64f2016-11-14 14:53:55 +0000521 return env->NewStringUTF(oat_file_assistant.GetStatusDump().c_str());
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100522}
523
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700524static jint DexFile_getDexOptNeeded(JNIEnv* env,
525 jclass,
526 jstring javaFilename,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700527 jstring javaInstructionSet,
Andreas Gampec38be812016-03-23 15:03:46 -0700528 jstring javaTargetCompilerFilter,
529 jboolean newProfile) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100530 ScopedUtfChars filename(env, javaFilename);
Andreas Gampe20c89302014-08-19 17:28:06 -0700531 if (env->ExceptionCheck()) {
Calin Juravleb077e152016-02-18 18:47:37 +0000532 return -1;
Andreas Gampe20c89302014-08-19 17:28:06 -0700533 }
534
Narayan Kamath11d9f062014-04-23 20:24:57 +0100535 ScopedUtfChars instruction_set(env, javaInstructionSet);
Andreas Gampe20c89302014-08-19 17:28:06 -0700536 if (env->ExceptionCheck()) {
Calin Juravleb077e152016-02-18 18:47:37 +0000537 return -1;
Andreas Gampe20c89302014-08-19 17:28:06 -0700538 }
Narayan Kamath11d9f062014-04-23 20:24:57 +0100539
Andreas Gampec38be812016-03-23 15:03:46 -0700540 ScopedUtfChars target_compiler_filter(env, javaTargetCompilerFilter);
541 if (env->ExceptionCheck()) {
542 return -1;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000543 }
Andreas Gampec38be812016-03-23 15:03:46 -0700544
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700545 return GetDexOptNeeded(env,
546 filename.c_str(),
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700547 instruction_set.c_str(),
Andreas Gampec38be812016-03-23 15:03:46 -0700548 target_compiler_filter.c_str(),
549 newProfile == JNI_TRUE);
Narayan Kamath11d9f062014-04-23 20:24:57 +0100550}
551
Calin Juravleb077e152016-02-18 18:47:37 +0000552// public API
Narayan Kamath11d9f062014-04-23 20:24:57 +0100553static jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
Richard Uhler06e3f4f2016-05-24 15:42:37 -0700554 ScopedUtfChars filename_utf(env, javaFilename);
555 if (env->ExceptionCheck()) {
556 return JNI_FALSE;
557 }
558
559 const char* filename = filename_utf.c_str();
560 if ((filename == nullptr) || !OS::FileExists(filename)) {
561 LOG(ERROR) << "DexFile_isDexOptNeeded file '" << filename << "' does not exist";
562 ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException"));
563 const char* message = (filename == nullptr) ? "<empty file name>" : filename;
564 env->ThrowNew(fnfe.get(), message);
565 return JNI_FALSE;
566 }
567
Richard Uhlerd1472a22016-04-15 15:18:56 -0700568 OatFileAssistant oat_file_assistant(filename, kRuntimeISA, false);
Richard Uhler06e3f4f2016-05-24 15:42:37 -0700569 return oat_file_assistant.IsUpToDate() ? JNI_FALSE : JNI_TRUE;
Dave Allison39c3bfb2014-01-28 18:33:52 -0800570}
571
Andreas Gampec38be812016-03-23 15:03:46 -0700572static jboolean DexFile_isValidCompilerFilter(JNIEnv* env,
573 jclass javeDexFileClass ATTRIBUTE_UNUSED,
574 jstring javaCompilerFilter) {
575 ScopedUtfChars compiler_filter(env, javaCompilerFilter);
576 if (env->ExceptionCheck()) {
577 return -1;
578 }
579
580 CompilerFilter::Filter filter;
581 return CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)
582 ? JNI_TRUE : JNI_FALSE;
583}
584
585static jboolean DexFile_isProfileGuidedCompilerFilter(JNIEnv* env,
586 jclass javeDexFileClass ATTRIBUTE_UNUSED,
587 jstring javaCompilerFilter) {
588 ScopedUtfChars compiler_filter(env, javaCompilerFilter);
589 if (env->ExceptionCheck()) {
590 return -1;
591 }
592
593 CompilerFilter::Filter filter;
594 if (!CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)) {
595 return JNI_FALSE;
596 }
597 return CompilerFilter::DependsOnProfile(filter) ? JNI_TRUE : JNI_FALSE;
598}
599
Andreas Gampe86a785d2016-03-30 17:19:48 -0700600static jstring DexFile_getNonProfileGuidedCompilerFilter(JNIEnv* env,
601 jclass javeDexFileClass ATTRIBUTE_UNUSED,
602 jstring javaCompilerFilter) {
603 ScopedUtfChars compiler_filter(env, javaCompilerFilter);
604 if (env->ExceptionCheck()) {
605 return nullptr;
606 }
607
608 CompilerFilter::Filter filter;
609 if (!CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)) {
610 return javaCompilerFilter;
611 }
612
613 CompilerFilter::Filter new_filter = CompilerFilter::GetNonProfileDependentFilterFrom(filter);
614
615 // Filter stayed the same, return input.
616 if (filter == new_filter) {
617 return javaCompilerFilter;
618 }
619
620 // Create a new string object and return.
621 std::string new_filter_str = CompilerFilter::NameOfFilter(new_filter);
622 return env->NewStringUTF(new_filter_str.c_str());
623}
624
Jeff Hao90671be2016-04-22 14:00:06 -0700625static jboolean DexFile_isBackedByOatFile(JNIEnv* env, jclass, jobject cookie) {
626 const OatFile* oat_file = nullptr;
627 std::vector<const DexFile*> dex_files;
628 if (!ConvertJavaArrayToDexFiles(env, cookie, /*out */ dex_files, /* out */ oat_file)) {
629 DCHECK(env->ExceptionCheck());
630 return false;
631 }
632 return oat_file != nullptr;
633}
634
Philip Cuadrab55ad7c2016-07-12 16:37:40 -0700635static jstring DexFile_getDexFileOutputPath(JNIEnv* env,
636 jclass,
637 jstring javaFilename,
638 jstring javaInstructionSet) {
639 ScopedUtfChars filename(env, javaFilename);
640 if (env->ExceptionCheck()) {
641 return nullptr;
642 }
643
644 ScopedUtfChars instruction_set(env, javaInstructionSet);
645 if (env->ExceptionCheck()) {
646 return nullptr;
647 }
648
649 const InstructionSet target_instruction_set = GetInstructionSetFromString(
650 instruction_set.c_str());
651 if (target_instruction_set == kNone) {
652 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
653 std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set.c_str()));
654 env->ThrowNew(iae.get(), message.c_str());
655 return nullptr;
656 }
657
658 OatFileAssistant oat_file_assistant(filename.c_str(),
659 target_instruction_set,
Philip Cuadrab55ad7c2016-07-12 16:37:40 -0700660 false /* load_executable */);
661
662 std::unique_ptr<OatFile> best_oat_file = oat_file_assistant.GetBestOatFile();
663 if (best_oat_file == nullptr) {
664 return nullptr;
665 }
666
667 return env->NewStringUTF(best_oat_file->GetLocation().c_str());
668}
669
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700670static JNINativeMethod gMethods[] = {
Mathieu Chartier1d7d7f12015-09-25 16:48:57 -0700671 NATIVE_METHOD(DexFile, closeDexFile, "(Ljava/lang/Object;)Z"),
Mathieu Chartier00310e02015-10-17 12:46:42 -0700672 NATIVE_METHOD(DexFile,
673 defineClassNative,
674 "(Ljava/lang/String;"
675 "Ljava/lang/ClassLoader;"
676 "Ljava/lang/Object;"
677 "Ldalvik/system/DexFile;"
678 ")Ljava/lang/Class;"),
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800679 NATIVE_METHOD(DexFile, getClassNameList, "(Ljava/lang/Object;)[Ljava/lang/String;"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700680 NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700681 NATIVE_METHOD(DexFile, getDexOptNeeded,
Andreas Gampec38be812016-03-23 15:03:46 -0700682 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Z)I"),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700683 NATIVE_METHOD(DexFile, openDexFileNative,
Mathieu Chartier689a7002015-11-20 10:29:42 -0800684 "(Ljava/lang/String;"
685 "Ljava/lang/String;"
686 "I"
687 "Ljava/lang/ClassLoader;"
688 "[Ldalvik/system/DexPathList$Element;"
689 ")Ljava/lang/Object;"),
Alex Lightea9465e2017-02-16 15:38:35 -0800690 NATIVE_METHOD(DexFile, createCookieWithDirectBuffer,
691 "(Ljava/nio/ByteBuffer;II)Ljava/lang/Object;"),
692 NATIVE_METHOD(DexFile, createCookieWithArray, "([BII)Ljava/lang/Object;"),
Andreas Gampec38be812016-03-23 15:03:46 -0700693 NATIVE_METHOD(DexFile, isValidCompilerFilter, "(Ljava/lang/String;)Z"),
694 NATIVE_METHOD(DexFile, isProfileGuidedCompilerFilter, "(Ljava/lang/String;)Z"),
Andreas Gampe86a785d2016-03-30 17:19:48 -0700695 NATIVE_METHOD(DexFile,
696 getNonProfileGuidedCompilerFilter,
697 "(Ljava/lang/String;)Ljava/lang/String;"),
Jeff Hao90671be2016-04-22 14:00:06 -0700698 NATIVE_METHOD(DexFile, isBackedByOatFile, "(Ljava/lang/Object;)Z"),
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100699 NATIVE_METHOD(DexFile, getDexFileStatus,
Philip Cuadrab55ad7c2016-07-12 16:37:40 -0700700 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
701 NATIVE_METHOD(DexFile, getDexFileOutputPath,
Narayan Kamath8943c1d2016-05-02 13:14:48 +0100702 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;")
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700703};
704
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700705void register_dalvik_system_DexFile(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700706 REGISTER_NATIVE_METHODS("dalvik/system/DexFile");
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700707}
708
709} // namespace art