dalvik_system_DexFile.cc revision c5f17732d8144491c642776b6b48c85dfadf4b52
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 <algorithm>
18#include <set>
19#include <fcntl.h>
20#ifdef __linux__
21#include <sys/sendfile.h>
22#else
23#include <sys/socket.h>
24#endif
25#include <sys/stat.h>
26#include <unistd.h>
27
28#include "base/logging.h"
29#include "class_linker.h"
30#include "common_throws.h"
31#include "dex_file-inl.h"
32#include "gc/space/image_space.h"
33#include "gc/space/space-inl.h"
34#include "image.h"
35#include "jni_internal.h"
36#include "mirror/class_loader.h"
37#include "mirror/object-inl.h"
38#include "mirror/string.h"
39#include "oat.h"
40#include "os.h"
41#include "profiler.h"
42#include "runtime.h"
43#include "scoped_thread_state_change.h"
44#include "ScopedFd.h"
45#include "ScopedLocalRef.h"
46#include "ScopedUtfChars.h"
47#include "utils.h"
48#include "well_known_classes.h"
49#include "zip_archive.h"
50
51namespace art {
52
53// A smart pointer that provides read-only access to a Java string's UTF chars.
54// Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if
55// passed a null jstring. The correct idiom is:
56//
57//   NullableScopedUtfChars name(env, javaName);
58//   if (env->ExceptionCheck()) {
59//       return NULL;
60//   }
61//   // ... use name.c_str()
62//
63// TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option.
64class NullableScopedUtfChars {
65 public:
66  NullableScopedUtfChars(JNIEnv* env, jstring s) : mEnv(env), mString(s) {
67    mUtfChars = (s != NULL) ? env->GetStringUTFChars(s, NULL) : NULL;
68  }
69
70  ~NullableScopedUtfChars() {
71    if (mUtfChars) {
72      mEnv->ReleaseStringUTFChars(mString, mUtfChars);
73    }
74  }
75
76  const char* c_str() const {
77    return mUtfChars;
78  }
79
80  size_t size() const {
81    return strlen(mUtfChars);
82  }
83
84  // Element access.
85  const char& operator[](size_t n) const {
86    return mUtfChars[n];
87  }
88
89 private:
90  JNIEnv* mEnv;
91  jstring mString;
92  const char* mUtfChars;
93
94  // Disallow copy and assignment.
95  NullableScopedUtfChars(const NullableScopedUtfChars&);
96  void operator=(const NullableScopedUtfChars&);
97};
98
99static jlong DexFile_openDexFileNative(JNIEnv* env, jclass, jstring javaSourceName, jstring javaOutputName, jint) {
100  ScopedUtfChars sourceName(env, javaSourceName);
101  if (sourceName.c_str() == NULL) {
102    return 0;
103  }
104  NullableScopedUtfChars outputName(env, javaOutputName);
105  if (env->ExceptionCheck()) {
106    return 0;
107  }
108
109  uint32_t dex_location_checksum;
110  uint32_t* dex_location_checksum_pointer = &dex_location_checksum;
111  std::vector<std::string> error_msgs;
112  std::string error_msg;
113  if (!DexFile::GetChecksum(sourceName.c_str(), dex_location_checksum_pointer, &error_msg)) {
114    dex_location_checksum_pointer = NULL;
115  }
116
117  ClassLinker* linker = Runtime::Current()->GetClassLinker();
118  const DexFile* dex_file;
119  if (outputName.c_str() == nullptr) {
120    // FindOrCreateOatFileForDexLocation can tolerate a missing dex_location_checksum
121    dex_file = linker->FindDexFileInOatFileFromDexLocation(sourceName.c_str(),
122                                                           dex_location_checksum_pointer,
123                                                           kRuntimeISA,
124                                                           &error_msgs);
125  } else {
126    // FindOrCreateOatFileForDexLocation requires the dex_location_checksum
127    if (dex_location_checksum_pointer == NULL) {
128      ScopedObjectAccess soa(env);
129      DCHECK(!error_msg.empty());
130      ThrowIOException("%s", error_msg.c_str());
131      return 0;
132    }
133    dex_file = linker->FindOrCreateOatFileForDexLocation(sourceName.c_str(), dex_location_checksum,
134                                                         outputName.c_str(), &error_msgs);
135  }
136  if (dex_file == nullptr) {
137    ScopedObjectAccess soa(env);
138    CHECK(!error_msgs.empty());
139    // The most important message is at the end. So set up nesting by going forward, which will
140    // wrap the existing exception as a cause for the following one.
141    auto it = error_msgs.begin();
142    auto itEnd = error_msgs.end();
143    for ( ; it != itEnd; ++it) {
144      ThrowWrappedIOException("%s", it->c_str());
145    }
146
147    return 0;
148  }
149  return static_cast<jlong>(reinterpret_cast<uintptr_t>(dex_file));
150}
151
152static const DexFile* toDexFile(jlong dex_file_address, JNIEnv* env) {
153  const DexFile* dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(dex_file_address));
154  if (UNLIKELY(dex_file == nullptr)) {
155    ScopedObjectAccess soa(env);
156    ThrowNullPointerException(NULL, "dex_file == null");
157  }
158  return dex_file;
159}
160
161static void DexFile_closeDexFile(JNIEnv* env, jclass, jlong cookie) {
162  const DexFile* dex_file;
163  dex_file = toDexFile(cookie, env);
164  if (dex_file == nullptr) {
165    return;
166  }
167  ScopedObjectAccess soa(env);
168  if (Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) {
169    return;
170  }
171  delete dex_file;
172}
173
174static jclass DexFile_defineClassNative(JNIEnv* env, jclass, jstring javaName, jobject javaLoader,
175                                        jlong cookie) {
176  const DexFile* dex_file = toDexFile(cookie, env);
177  if (dex_file == NULL) {
178    VLOG(class_linker) << "Failed to find dex_file";
179    return NULL;
180  }
181  ScopedUtfChars class_name(env, javaName);
182  if (class_name.c_str() == NULL) {
183    VLOG(class_linker) << "Failed to find class_name";
184    return NULL;
185  }
186  const std::string descriptor(DotToDescriptor(class_name.c_str()));
187  const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor.c_str());
188  if (dex_class_def == NULL) {
189    VLOG(class_linker) << "Failed to find dex_class_def";
190    return NULL;
191  }
192  ScopedObjectAccess soa(env);
193  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
194  class_linker->RegisterDexFile(*dex_file);
195  StackHandleScope<1> hs(soa.Self());
196  Handle<mirror::ClassLoader> class_loader(
197      hs.NewHandle(soa.Decode<mirror::ClassLoader*>(javaLoader)));
198  mirror::Class* result = class_linker->DefineClass(descriptor.c_str(), class_loader, *dex_file,
199                                                    *dex_class_def);
200  VLOG(class_linker) << "DexFile_defineClassNative returning " << result;
201  return soa.AddLocalReference<jclass>(result);
202}
203
204static jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jlong cookie) {
205  jobjectArray result = nullptr;
206  const DexFile* dex_file = toDexFile(cookie, env);
207  if (dex_file != nullptr) {
208    result = env->NewObjectArray(dex_file->NumClassDefs(), WellKnownClasses::java_lang_String,
209                                 nullptr);
210    if (result != nullptr) {
211      for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
212        const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
213        std::string descriptor(DescriptorToDot(dex_file->GetClassDescriptor(class_def)));
214        ScopedLocalRef<jstring> jdescriptor(env, env->NewStringUTF(descriptor.c_str()));
215        if (jdescriptor.get() == nullptr) {
216          return nullptr;
217        }
218        env->SetObjectArrayElement(result, i, jdescriptor.get());
219      }
220    }
221  }
222  return result;
223}
224
225static void CopyProfileFile(const char* oldfile, const char* newfile) {
226  ScopedFd src(open(oldfile, O_RDONLY));
227  if (src.get() == -1) {
228    PLOG(ERROR) << "Failed to open profile file " << oldfile
229      << ". My uid:gid is " << getuid() << ":" << getgid();
230    return;
231  }
232
233  struct stat stat_src;
234  if (fstat(src.get(), &stat_src) == -1) {
235    PLOG(ERROR) << "Failed to get stats for profile file  " << oldfile
236      << ". My uid:gid is " << getuid() << ":" << getgid();
237    return;
238  }
239
240  // Create the copy with rw------- (only accessible by system)
241  ScopedFd dst(open(newfile, O_WRONLY|O_CREAT|O_TRUNC, 0600));
242  if (dst.get()  == -1) {
243    PLOG(ERROR) << "Failed to create/write prev profile file " << newfile
244      << ".  My uid:gid is " << getuid() << ":" << getgid();
245    return;
246  }
247
248#ifdef __linux__
249  if (sendfile(dst.get(), src.get(), nullptr, stat_src.st_size) == -1) {
250#else
251  off_t len;
252  if (sendfile(dst.get(), src.get(), 0, &len, nullptr, 0) == -1) {
253#endif
254    PLOG(ERROR) << "Failed to copy profile file " << oldfile << " to " << newfile
255      << ". My uid:gid is " << getuid() << ":" << getgid();
256  }
257}
258
259static jboolean IsDexOptNeededInternal(JNIEnv* env, const char* filename,
260    const char* pkgname, const char* instruction_set, const jboolean defer) {
261  const bool kVerboseLogging = false;  // Spammy logging.
262  const bool kReasonLogging = true;  // Logging of reason for returning JNI_TRUE.
263
264  if ((filename == nullptr) || !OS::FileExists(filename)) {
265    LOG(ERROR) << "DexFile_isDexOptNeeded file '" << filename << "' does not exist";
266    ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException"));
267    const char* message = (filename == nullptr) ? "<empty file name>" : filename;
268    env->ThrowNew(fnfe.get(), message);
269    return JNI_FALSE;
270  }
271
272  // Always treat elements of the bootclasspath as up-to-date.  The
273  // fact that code is running at all means that this should be true.
274  Runtime* runtime = Runtime::Current();
275  ClassLinker* class_linker = runtime->GetClassLinker();
276  // TODO: We're assuming that the 64 and 32 bit runtimes have identical
277  // class paths. isDexOptNeeded will not necessarily be called on a runtime
278  // that has the same instruction set as the file being dexopted.
279  const std::vector<const DexFile*>& boot_class_path = class_linker->GetBootClassPath();
280  for (size_t i = 0; i < boot_class_path.size(); i++) {
281    if (boot_class_path[i]->GetLocation() == filename) {
282      if (kVerboseLogging) {
283        LOG(INFO) << "DexFile_isDexOptNeeded ignoring boot class path file: " << filename;
284      }
285      return JNI_FALSE;
286    }
287  }
288
289  const InstructionSet target_instruction_set = GetInstructionSetFromString(instruction_set);
290
291  // Check if we have an odex file next to the dex file.
292  std::string odex_filename(DexFilenameToOdexFilename(filename, kRuntimeISA));
293  std::string error_msg;
294  std::unique_ptr<const OatFile> oat_file(OatFile::Open(odex_filename, odex_filename, NULL, false,
295                                                        &error_msg));
296  if (oat_file.get() == nullptr) {
297    if (kVerboseLogging) {
298      LOG(INFO) << "DexFile_isDexOptNeeded failed to open oat file '" << filename
299          << "': " << error_msg;
300    }
301    error_msg.clear();
302  } else {
303    const art::OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(filename, NULL,
304                                                                           kReasonLogging);
305    if (oat_dex_file != nullptr) {
306      uint32_t location_checksum;
307      // If its not possible to read the classes.dex assume up-to-date as we won't be able to
308      // compile it anyway.
309      if (!DexFile::GetChecksum(filename, &location_checksum, &error_msg)) {
310        if (kVerboseLogging) {
311          LOG(INFO) << "DexFile_isDexOptNeeded ignoring precompiled stripped file: "
312              << filename << ": " << error_msg;
313        }
314        return JNI_FALSE;
315      }
316      if (ClassLinker::VerifyOatFileChecksums(oat_file.get(), filename, location_checksum,
317                                              target_instruction_set,
318                                              &error_msg)) {
319        if (kVerboseLogging) {
320          LOG(INFO) << "DexFile_isDexOptNeeded precompiled file " << odex_filename
321              << " has an up-to-date checksum compared to " << filename;
322        }
323        return JNI_FALSE;
324      } else {
325        if (kVerboseLogging) {
326          LOG(INFO) << "DexFile_isDexOptNeeded found precompiled file " << odex_filename
327              << " with an out-of-date checksum compared to " << filename
328              << ": " << error_msg;
329        }
330        error_msg.clear();
331      }
332    }
333  }
334
335  // Check the profile file.  We need to rerun dex2oat if the profile has changed significantly
336  // since the last time, or it's new.
337  // If the 'defer' argument is true then this will be retried later.  In this case we
338  // need to make sure that the profile file copy is not made so that we will get the
339  // same result second time.
340  if (Runtime::Current()->GetProfilerOptions().IsEnabled() && (pkgname != nullptr)) {
341    const std::string profile_file = GetDalvikCacheOrDie("profiles", false /* create_if_absent */)
342        + std::string("/") + pkgname;
343    const std::string profile_cache_dir = GetDalvikCacheOrDie("profile-cache",
344                                                              false /* create_if_absent */);
345
346    // Make the profile cache if it doesn't exist.
347    mkdir(profile_cache_dir.c_str(), 0700);
348
349    // The previous profile file (a copy of the profile the last time this was run) is
350    // in the dalvik-cache directory because this is owned by system.  The profiles
351    // directory is owned by install so system cannot write files in there.
352    std::string prev_profile_file = profile_cache_dir + std::string("/") + pkgname;
353
354    struct stat profstat, prevstat;
355    int e1 = stat(profile_file.c_str(), &profstat);
356    int e2 = stat(prev_profile_file.c_str(), &prevstat);
357    if (e1 < 0) {
358      // No profile file, need to run dex2oat
359      if (kReasonLogging) {
360        LOG(INFO) << "DexFile_isDexOptNeeded profile file " << profile_file << " doesn't exist";
361      }
362      return JNI_TRUE;
363    }
364
365    if (e2 == 0) {
366      // There is a previous profile file.  Check if the profile has changed significantly.
367      // A change in profile is considered significant if X% (change_thr property) of the top K%
368      // (compile_thr property) samples has changed.
369      double top_k_threshold = Runtime::Current()->GetProfilerOptions().GetTopKThreshold();
370      double change_threshold = Runtime::Current()->GetProfilerOptions().GetTopKChangeThreshold();
371      double change_percent = 0.0;
372      ProfileFile new_profile, old_profile;
373      bool new_ok = new_profile.LoadFile(profile_file);
374      bool old_ok = old_profile.LoadFile(prev_profile_file);
375      if (!new_ok || !old_ok) {
376        if (kVerboseLogging) {
377          LOG(INFO) << "DexFile_isDexOptNeeded Ignoring invalid profiles: "
378                    << (new_ok ?  "" : profile_file) << " " << (old_ok ? "" : prev_profile_file);
379        }
380      } else {
381        std::set<std::string> new_top_k, old_top_k;
382        new_profile.GetTopKSamples(new_top_k, top_k_threshold);
383        old_profile.GetTopKSamples(old_top_k, top_k_threshold);
384        if (new_top_k.empty()) {
385          if (kVerboseLogging) {
386            LOG(INFO) << "DexFile_isDexOptNeeded empty profile: " << profile_file;
387          }
388          // If the new topK is empty we shouldn't optimize so we leave the change_percent at 0.0.
389        } else {
390          std::set<std::string> diff;
391          std::set_difference(new_top_k.begin(), new_top_k.end(), old_top_k.begin(), old_top_k.end(),
392            std::inserter(diff, diff.end()));
393          // TODO: consider using the usedPercentage instead of the plain diff count.
394          change_percent = 100.0 * static_cast<double>(diff.size()) / static_cast<double>(new_top_k.size());
395          if (kVerboseLogging) {
396            std::set<std::string>::iterator end = diff.end();
397            for (std::set<std::string>::iterator it = diff.begin(); it != end; it++) {
398              LOG(INFO) << "DexFile_isDexOptNeeded new in topK: " << *it;
399            }
400          }
401        }
402      }
403
404      if (change_percent > change_threshold) {
405        if (kReasonLogging) {
406          LOG(INFO) << "DexFile_isDexOptNeeded size of new profile file " << profile_file <<
407          " is significantly different from old profile file " << prev_profile_file << " (top "
408          << top_k_threshold << "% samples changed in proportion of " << change_percent << "%)";
409        }
410        if (!defer) {
411          CopyProfileFile(profile_file.c_str(), prev_profile_file.c_str());
412        }
413        return JNI_TRUE;
414      }
415    } else {
416      // Previous profile does not exist.  Make a copy of the current one.
417      if (kVerboseLogging) {
418        LOG(INFO) << "DexFile_isDexOptNeeded previous profile doesn't exist: " << prev_profile_file;
419      }
420      if (!defer) {
421        CopyProfileFile(profile_file.c_str(), prev_profile_file.c_str());
422      }
423    }
424  }
425
426  // Check if we have an oat file in the cache
427  const std::string cache_dir(GetDalvikCacheOrDie(instruction_set));
428  const std::string cache_location(
429      GetDalvikCacheFilenameOrDie(filename, cache_dir.c_str()));
430  oat_file.reset(OatFile::Open(cache_location, filename, NULL, false, &error_msg));
431  if (oat_file.get() == nullptr) {
432    if (kReasonLogging) {
433      LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
434          << " does not exist for " << filename << ": " << error_msg;
435    }
436    return JNI_TRUE;
437  }
438
439  uint32_t location_checksum;
440  if (!DexFile::GetChecksum(filename, &location_checksum, &error_msg)) {
441    if (kReasonLogging) {
442      LOG(ERROR) << "DexFile_isDexOptNeeded failed to compute checksum of " << filename
443            << " (error " << error_msg << ")";
444    }
445    return JNI_TRUE;
446  }
447
448  if (!ClassLinker::VerifyOatFileChecksums(oat_file.get(), filename, location_checksum,
449                                           target_instruction_set, &error_msg)) {
450    if (kReasonLogging) {
451      LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
452          << " has out-of-date checksum compared to " << filename
453          << " (error " << error_msg << ")";
454    }
455    return JNI_TRUE;
456  }
457
458  if (kVerboseLogging) {
459    LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
460              << " is up-to-date for " << filename;
461  }
462  CHECK(error_msg.empty()) << error_msg;
463  return JNI_FALSE;
464}
465
466static jboolean DexFile_isDexOptNeededInternal(JNIEnv* env, jclass, jstring javaFilename,
467    jstring javaPkgname, jstring javaInstructionSet, jboolean defer) {
468  ScopedUtfChars filename(env, javaFilename);
469  NullableScopedUtfChars pkgname(env, javaPkgname);
470  ScopedUtfChars instruction_set(env, javaInstructionSet);
471
472  return IsDexOptNeededInternal(env, filename.c_str(), pkgname.c_str(),
473                                instruction_set.c_str(), defer);
474}
475
476// public API, NULL pkgname
477static jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
478  const char* instruction_set = GetInstructionSetString(kRuntimeISA);
479  ScopedUtfChars filename(env, javaFilename);
480  return IsDexOptNeededInternal(env, filename.c_str(), nullptr /* pkgname */,
481                                instruction_set, false /* defer */);
482}
483
484
485static JNINativeMethod gMethods[] = {
486  NATIVE_METHOD(DexFile, closeDexFile, "(J)V"),
487  NATIVE_METHOD(DexFile, defineClassNative, "(Ljava/lang/String;Ljava/lang/ClassLoader;J)Ljava/lang/Class;"),
488  NATIVE_METHOD(DexFile, getClassNameList, "(J)[Ljava/lang/String;"),
489  NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
490  NATIVE_METHOD(DexFile, isDexOptNeededInternal, "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Z)Z"),
491  NATIVE_METHOD(DexFile, openDexFileNative, "(Ljava/lang/String;Ljava/lang/String;I)J"),
492};
493
494void register_dalvik_system_DexFile(JNIEnv* env) {
495  REGISTER_NATIVE_METHODS("dalvik/system/DexFile");
496}
497
498}  // namespace art
499