dalvik_system_DexFile.cc revision fd97429f258acde6ee24a6f74c9050b2343e40cd
1/*
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
17#include "dalvik_system_DexFile.h"
18
19#include "base/logging.h"
20#include "base/stl_util.h"
21#include "base/stringprintf.h"
22#include "class_linker.h"
23#include "common_throws.h"
24#include "compiler_filter.h"
25#include "dex_file-inl.h"
26#include "jni_internal.h"
27#include "mirror/class_loader.h"
28#include "mirror/object-inl.h"
29#include "mirror/string.h"
30#include "oat_file_assistant.h"
31#include "oat_file_manager.h"
32#include "os.h"
33#include "profiler.h"
34#include "runtime.h"
35#include "scoped_thread_state_change.h"
36#include "ScopedLocalRef.h"
37#include "ScopedUtfChars.h"
38#include "utils.h"
39#include "well_known_classes.h"
40#include "zip_archive.h"
41
42namespace art {
43
44static bool ConvertJavaArrayToDexFiles(
45    JNIEnv* env,
46    jobject arrayObject,
47    /*out*/ std::vector<const DexFile*>& dex_files,
48    /*out*/ const OatFile*& oat_file) {
49  jarray array = reinterpret_cast<jarray>(arrayObject);
50
51  jsize array_size = env->GetArrayLength(array);
52  if (env->ExceptionCheck() == JNI_TRUE) {
53    return false;
54  }
55
56  // TODO: Optimize. On 32bit we can use an int array.
57  jboolean is_long_data_copied;
58  jlong* long_data = env->GetLongArrayElements(reinterpret_cast<jlongArray>(array),
59                                               &is_long_data_copied);
60  if (env->ExceptionCheck() == JNI_TRUE) {
61    return false;
62  }
63
64  oat_file = reinterpret_cast<const OatFile*>(static_cast<uintptr_t>(long_data[kOatFileIndex]));
65  dex_files.reserve(array_size - 1);
66  for (jsize i = kDexFileIndexStart; i < array_size; ++i) {
67    dex_files.push_back(reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(long_data[i])));
68  }
69
70  env->ReleaseLongArrayElements(reinterpret_cast<jlongArray>(array), long_data, JNI_ABORT);
71  return env->ExceptionCheck() != JNI_TRUE;
72}
73
74static jlongArray ConvertDexFilesToJavaArray(JNIEnv* env,
75                                             const OatFile* oat_file,
76                                             std::vector<std::unique_ptr<const DexFile>>& vec) {
77  // Add one for the oat file.
78  jlongArray long_array = env->NewLongArray(static_cast<jsize>(kDexFileIndexStart + vec.size()));
79  if (env->ExceptionCheck() == JNI_TRUE) {
80    return nullptr;
81  }
82
83  jboolean is_long_data_copied;
84  jlong* long_data = env->GetLongArrayElements(long_array, &is_long_data_copied);
85  if (env->ExceptionCheck() == JNI_TRUE) {
86    return nullptr;
87  }
88
89  long_data[kOatFileIndex] = reinterpret_cast<uintptr_t>(oat_file);
90  for (size_t i = 0; i < vec.size(); ++i) {
91    long_data[kDexFileIndexStart + i] = reinterpret_cast<uintptr_t>(vec[i].get());
92  }
93
94  env->ReleaseLongArrayElements(long_array, long_data, 0);
95  if (env->ExceptionCheck() == JNI_TRUE) {
96    return nullptr;
97  }
98
99  // Now release all the unique_ptrs.
100  for (auto& dex_file : vec) {
101    dex_file.release();
102  }
103
104  return long_array;
105}
106
107// A smart pointer that provides read-only access to a Java string's UTF chars.
108// Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if
109// passed a null jstring. The correct idiom is:
110//
111//   NullableScopedUtfChars name(env, javaName);
112//   if (env->ExceptionCheck()) {
113//       return null;
114//   }
115//   // ... use name.c_str()
116//
117// TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option.
118class NullableScopedUtfChars {
119 public:
120  NullableScopedUtfChars(JNIEnv* env, jstring s) : mEnv(env), mString(s) {
121    mUtfChars = (s != nullptr) ? env->GetStringUTFChars(s, nullptr) : nullptr;
122  }
123
124  ~NullableScopedUtfChars() {
125    if (mUtfChars) {
126      mEnv->ReleaseStringUTFChars(mString, mUtfChars);
127    }
128  }
129
130  const char* c_str() const {
131    return mUtfChars;
132  }
133
134  size_t size() const {
135    return strlen(mUtfChars);
136  }
137
138  // Element access.
139  const char& operator[](size_t n) const {
140    return mUtfChars[n];
141  }
142
143 private:
144  JNIEnv* mEnv;
145  jstring mString;
146  const char* mUtfChars;
147
148  // Disallow copy and assignment.
149  NullableScopedUtfChars(const NullableScopedUtfChars&);
150  void operator=(const NullableScopedUtfChars&);
151};
152
153static jobject DexFile_openDexFileNative(JNIEnv* env,
154                                         jclass,
155                                         jstring javaSourceName,
156                                         jstring javaOutputName,
157                                         jint flags ATTRIBUTE_UNUSED,
158                                         jobject class_loader,
159                                         jobjectArray dex_elements) {
160  ScopedUtfChars sourceName(env, javaSourceName);
161  if (sourceName.c_str() == nullptr) {
162    return 0;
163  }
164  NullableScopedUtfChars outputName(env, javaOutputName);
165  if (env->ExceptionCheck()) {
166    return 0;
167  }
168  Runtime* const runtime = Runtime::Current();
169  ClassLinker* linker = runtime->GetClassLinker();
170  std::vector<std::unique_ptr<const DexFile>> dex_files;
171  std::vector<std::string> error_msgs;
172  const OatFile* oat_file = nullptr;
173
174  dex_files = runtime->GetOatFileManager().OpenDexFilesFromOat(sourceName.c_str(),
175                                                               outputName.c_str(),
176                                                               class_loader,
177                                                               dex_elements,
178                                                               /*out*/ &oat_file,
179                                                               /*out*/ &error_msgs);
180
181  if (!dex_files.empty()) {
182    jlongArray array = ConvertDexFilesToJavaArray(env, oat_file, dex_files);
183    if (array == nullptr) {
184      ScopedObjectAccess soa(env);
185      for (auto& dex_file : dex_files) {
186        if (linker->FindDexCache(soa.Self(), *dex_file, true) != nullptr) {
187          dex_file.release();
188        }
189      }
190    }
191    return array;
192  } else {
193    ScopedObjectAccess soa(env);
194    CHECK(!error_msgs.empty());
195    // The most important message is at the end. So set up nesting by going forward, which will
196    // wrap the existing exception as a cause for the following one.
197    auto it = error_msgs.begin();
198    auto itEnd = error_msgs.end();
199    for ( ; it != itEnd; ++it) {
200      ThrowWrappedIOException("%s", it->c_str());
201    }
202
203    return nullptr;
204  }
205}
206
207static jboolean DexFile_closeDexFile(JNIEnv* env, jclass, jobject cookie) {
208  std::vector<const DexFile*> dex_files;
209  const OatFile* oat_file;
210  if (!ConvertJavaArrayToDexFiles(env, cookie, dex_files, oat_file)) {
211    Thread::Current()->AssertPendingException();
212    return JNI_FALSE;
213  }
214  Runtime* const runtime = Runtime::Current();
215  bool all_deleted = true;
216  {
217    ScopedObjectAccess soa(env);
218    mirror::Object* dex_files_object = soa.Decode<mirror::Object*>(cookie);
219    mirror::LongArray* long_dex_files = dex_files_object->AsLongArray();
220    // Delete dex files associated with this dalvik.system.DexFile since there should not be running
221    // code using it. dex_files is a vector due to multidex.
222    ClassLinker* const class_linker = runtime->GetClassLinker();
223    int32_t i = kDexFileIndexStart;  // Oat file is at index 0.
224    for (const DexFile* dex_file : dex_files) {
225      if (dex_file != nullptr) {
226        // Only delete the dex file if the dex cache is not found to prevent runtime crashes if there
227        // are calls to DexFile.close while the ART DexFile is still in use.
228        if (class_linker->FindDexCache(soa.Self(), *dex_file, true) == nullptr) {
229          // Clear the element in the array so that we can call close again.
230          long_dex_files->Set(i, 0);
231          delete dex_file;
232        } else {
233          all_deleted = false;
234        }
235      }
236      ++i;
237    }
238  }
239
240  // oat_file can be null if we are running without dex2oat.
241  if (all_deleted && oat_file != nullptr) {
242    // If all of the dex files are no longer in use we can unmap the corresponding oat file.
243    VLOG(class_linker) << "Unregistering " << oat_file;
244    runtime->GetOatFileManager().UnRegisterAndDeleteOatFile(oat_file);
245  }
246  return all_deleted ? JNI_TRUE : JNI_FALSE;
247}
248
249static jclass DexFile_defineClassNative(JNIEnv* env,
250                                        jclass,
251                                        jstring javaName,
252                                        jobject javaLoader,
253                                        jobject cookie,
254                                        jobject dexFile) {
255  std::vector<const DexFile*> dex_files;
256  const OatFile* oat_file;
257  if (!ConvertJavaArrayToDexFiles(env, cookie, /*out*/ dex_files, /*out*/ oat_file)) {
258    VLOG(class_linker) << "Failed to find dex_file";
259    DCHECK(env->ExceptionCheck());
260    return nullptr;
261  }
262
263  ScopedUtfChars class_name(env, javaName);
264  if (class_name.c_str() == nullptr) {
265    VLOG(class_linker) << "Failed to find class_name";
266    return nullptr;
267  }
268  const std::string descriptor(DotToDescriptor(class_name.c_str()));
269  const size_t hash(ComputeModifiedUtf8Hash(descriptor.c_str()));
270  for (auto& dex_file : dex_files) {
271    const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor.c_str(), hash);
272    if (dex_class_def != nullptr) {
273      ScopedObjectAccess soa(env);
274      ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
275      StackHandleScope<1> hs(soa.Self());
276      Handle<mirror::ClassLoader> class_loader(
277          hs.NewHandle(soa.Decode<mirror::ClassLoader*>(javaLoader)));
278      class_linker->RegisterDexFile(
279          *dex_file,
280          class_linker->GetOrCreateAllocatorForClassLoader(class_loader.Get()));
281      mirror::Class* result = class_linker->DefineClass(soa.Self(),
282                                                        descriptor.c_str(),
283                                                        hash,
284                                                        class_loader,
285                                                        *dex_file,
286                                                        *dex_class_def);
287      // Add the used dex file. This only required for the DexFile.loadClass API since normal
288      // class loaders already keep their dex files live.
289      class_linker->InsertDexFileInToClassLoader(soa.Decode<mirror::Object*>(dexFile),
290                                                 class_loader.Get());
291      if (result != nullptr) {
292        VLOG(class_linker) << "DexFile_defineClassNative returning " << result
293                           << " for " << class_name.c_str();
294        return soa.AddLocalReference<jclass>(result);
295      }
296    }
297  }
298  VLOG(class_linker) << "Failed to find dex_class_def " << class_name.c_str();
299  return nullptr;
300}
301
302// Needed as a compare functor for sets of const char
303struct CharPointerComparator {
304  bool operator()(const char *str1, const char *str2) const {
305    return strcmp(str1, str2) < 0;
306  }
307};
308
309// Note: this can be an expensive call, as we sort out duplicates in MultiDex files.
310static jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jobject cookie) {
311  const OatFile* oat_file = nullptr;
312  std::vector<const DexFile*> dex_files;
313  if (!ConvertJavaArrayToDexFiles(env, cookie, /*out */ dex_files, /* out */ oat_file)) {
314    DCHECK(env->ExceptionCheck());
315    return nullptr;
316  }
317
318  // Push all class descriptors into a set. Use set instead of unordered_set as we want to
319  // retrieve all in the end.
320  std::set<const char*, CharPointerComparator> descriptors;
321  for (auto& dex_file : dex_files) {
322    for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
323      const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
324      const char* descriptor = dex_file->GetClassDescriptor(class_def);
325      descriptors.insert(descriptor);
326    }
327  }
328
329  // Now create output array and copy the set into it.
330  jobjectArray result = env->NewObjectArray(descriptors.size(),
331                                            WellKnownClasses::java_lang_String,
332                                            nullptr);
333  if (result != nullptr) {
334    auto it = descriptors.begin();
335    auto it_end = descriptors.end();
336    jsize i = 0;
337    for (; it != it_end; it++, ++i) {
338      std::string descriptor(DescriptorToDot(*it));
339      ScopedLocalRef<jstring> jdescriptor(env, env->NewStringUTF(descriptor.c_str()));
340      if (jdescriptor.get() == nullptr) {
341        return nullptr;
342      }
343      env->SetObjectArrayElement(result, i, jdescriptor.get());
344    }
345  }
346  return result;
347}
348
349static jint GetDexOptNeeded(JNIEnv* env,
350                            const char* filename,
351                            const char* instruction_set,
352                            const char* compiler_filter_name,
353                            bool profile_changed) {
354  if ((filename == nullptr) || !OS::FileExists(filename)) {
355    LOG(ERROR) << "DexFile_getDexOptNeeded file '" << filename << "' does not exist";
356    ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException"));
357    const char* message = (filename == nullptr) ? "<empty file name>" : filename;
358    env->ThrowNew(fnfe.get(), message);
359    return -1;
360  }
361
362  const InstructionSet target_instruction_set = GetInstructionSetFromString(instruction_set);
363  if (target_instruction_set == kNone) {
364    ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
365    std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set));
366    env->ThrowNew(iae.get(), message.c_str());
367    return -1;
368  }
369
370  CompilerFilter::Filter filter;
371  if (!CompilerFilter::ParseCompilerFilter(compiler_filter_name, &filter)) {
372    ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
373    std::string message(StringPrintf("Compiler filter %s is invalid.", compiler_filter_name));
374    env->ThrowNew(iae.get(), message.c_str());
375    return -1;
376  }
377
378  // TODO: Verify the dex location is well formed, and throw an IOException if
379  // not?
380
381  OatFileAssistant oat_file_assistant(filename, target_instruction_set, profile_changed, false);
382
383  // Always treat elements of the bootclasspath as up-to-date.
384  if (oat_file_assistant.IsInBootClassPath()) {
385    return OatFileAssistant::kNoDexOptNeeded;
386  }
387  return oat_file_assistant.GetDexOptNeeded(filter);
388}
389
390static jint DexFile_getDexOptNeeded(JNIEnv* env,
391                                    jclass,
392                                    jstring javaFilename,
393                                    jstring javaInstructionSet,
394                                    jstring javaTargetCompilerFilter,
395                                    jboolean newProfile) {
396  ScopedUtfChars filename(env, javaFilename);
397  if (env->ExceptionCheck()) {
398    return -1;
399  }
400
401  ScopedUtfChars instruction_set(env, javaInstructionSet);
402  if (env->ExceptionCheck()) {
403    return -1;
404  }
405
406  ScopedUtfChars target_compiler_filter(env, javaTargetCompilerFilter);
407  if (env->ExceptionCheck()) {
408    return -1;
409  }
410
411  return GetDexOptNeeded(env,
412                         filename.c_str(),
413                         instruction_set.c_str(),
414                         target_compiler_filter.c_str(),
415                         newProfile == JNI_TRUE);
416}
417
418// public API
419static jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
420  const char* instruction_set = GetInstructionSetString(kRuntimeISA);
421  ScopedUtfChars filename(env, javaFilename);
422  jint status = GetDexOptNeeded(
423      env,
424      filename.c_str(),
425      instruction_set,
426      "speed-profile",
427      /*profile_changed*/false);
428  return (status != OatFileAssistant::kNoDexOptNeeded) ? JNI_TRUE : JNI_FALSE;
429}
430
431static jboolean DexFile_isValidCompilerFilter(JNIEnv* env,
432                                            jclass javeDexFileClass ATTRIBUTE_UNUSED,
433                                            jstring javaCompilerFilter) {
434  ScopedUtfChars compiler_filter(env, javaCompilerFilter);
435  if (env->ExceptionCheck()) {
436    return -1;
437  }
438
439  CompilerFilter::Filter filter;
440  return CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)
441      ? JNI_TRUE : JNI_FALSE;
442}
443
444static jboolean DexFile_isProfileGuidedCompilerFilter(JNIEnv* env,
445                                                      jclass javeDexFileClass ATTRIBUTE_UNUSED,
446                                                      jstring javaCompilerFilter) {
447  ScopedUtfChars compiler_filter(env, javaCompilerFilter);
448  if (env->ExceptionCheck()) {
449    return -1;
450  }
451
452  CompilerFilter::Filter filter;
453  if (!CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)) {
454    return JNI_FALSE;
455  }
456  return CompilerFilter::DependsOnProfile(filter) ? JNI_TRUE : JNI_FALSE;
457}
458
459static jstring DexFile_getNonProfileGuidedCompilerFilter(JNIEnv* env,
460                                                         jclass javeDexFileClass ATTRIBUTE_UNUSED,
461                                                         jstring javaCompilerFilter) {
462  ScopedUtfChars compiler_filter(env, javaCompilerFilter);
463  if (env->ExceptionCheck()) {
464    return nullptr;
465  }
466
467  CompilerFilter::Filter filter;
468  if (!CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)) {
469    return javaCompilerFilter;
470  }
471
472  CompilerFilter::Filter new_filter = CompilerFilter::GetNonProfileDependentFilterFrom(filter);
473
474  // Filter stayed the same, return input.
475  if (filter == new_filter) {
476    return javaCompilerFilter;
477  }
478
479  // Create a new string object and return.
480  std::string new_filter_str = CompilerFilter::NameOfFilter(new_filter);
481  return env->NewStringUTF(new_filter_str.c_str());
482}
483
484static JNINativeMethod gMethods[] = {
485  NATIVE_METHOD(DexFile, closeDexFile, "(Ljava/lang/Object;)Z"),
486  NATIVE_METHOD(DexFile,
487                defineClassNative,
488                "(Ljava/lang/String;"
489                "Ljava/lang/ClassLoader;"
490                "Ljava/lang/Object;"
491                "Ldalvik/system/DexFile;"
492                ")Ljava/lang/Class;"),
493  NATIVE_METHOD(DexFile, getClassNameList, "(Ljava/lang/Object;)[Ljava/lang/String;"),
494  NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
495  NATIVE_METHOD(DexFile, getDexOptNeeded,
496                "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Z)I"),
497  NATIVE_METHOD(DexFile, openDexFileNative,
498                "(Ljava/lang/String;"
499                "Ljava/lang/String;"
500                "I"
501                "Ljava/lang/ClassLoader;"
502                "[Ldalvik/system/DexPathList$Element;"
503                ")Ljava/lang/Object;"),
504  NATIVE_METHOD(DexFile, isValidCompilerFilter, "(Ljava/lang/String;)Z"),
505  NATIVE_METHOD(DexFile, isProfileGuidedCompilerFilter, "(Ljava/lang/String;)Z"),
506  NATIVE_METHOD(DexFile,
507                getNonProfileGuidedCompilerFilter,
508                "(Ljava/lang/String;)Ljava/lang/String;"),
509};
510
511void register_dalvik_system_DexFile(JNIEnv* env) {
512  REGISTER_NATIVE_METHODS("dalvik/system/DexFile");
513}
514
515}  // namespace art
516