dalvik_system_DexFile.cc revision 0512f02dd6623c0870c11fbf3274d7462f732136
1f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom/*
2f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom * Copyright (C) 2008 The Android Open Source Project
3f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom *
4f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom * Licensed under the Apache License, Version 2.0 (the "License");
5f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom * you may not use this file except in compliance with the License.
6f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom * You may obtain a copy of the License at
7f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom *
8f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom *      http://www.apache.org/licenses/LICENSE-2.0
9f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom *
10f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom * Unless required by applicable law or agreed to in writing, software
11f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom * distributed under the License is distributed on an "AS IS" BASIS,
12f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom * See the License for the specific language governing permissions and
14f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom * limitations under the License.
15f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom */
16f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom
171d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom#include <unistd.h>
181d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom
19aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom#include "class_loader.h"
20aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom#include "class_linker.h"
21aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom#include "dex_file.h"
22f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom#include "logging.h"
231d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom#include "os.h"
24aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom#include "runtime.h"
25c393a4f7c91534ac41a81d66b12c22e68df28fc7jeffhao#include "zip_archive.h"
2603a20ba67cfdc46f5ad8d77242a666a4cb0512f2Brian Carlstrom#include "toStringArray.h"
27c981848a3425662034c5429b61c035e7533b896dIan Rogers#include "ScopedLocalRef.h"
28f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom#include "ScopedUtfChars.h"
29f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom
30f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
31f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom
32f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstromnamespace art {
33f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom
34f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom// A smart pointer that provides read-only access to a Java string's UTF chars.
35f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom// Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if
36f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom// passed a null jstring. The correct idiom is:
37f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom//
38f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom//   NullableScopedUtfChars name(env, javaName);
39c252c3eacd83a0c110dd065690a7f652be35b0e7Brian Carlstrom//   if (env->ExceptionCheck()) {
40f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom//       return NULL;
41f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom//   }
42f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom//   // ... use name.c_str()
43f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom//
44f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom// TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option.
45f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstromclass NullableScopedUtfChars {
46ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes public:
47ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes  NullableScopedUtfChars(JNIEnv* env, jstring s) : mEnv(env), mString(s) {
48ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes    mUtfChars = (s != NULL) ? env->GetStringUTFChars(s, NULL) : NULL;
49ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes  }
50f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom
51ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes  ~NullableScopedUtfChars() {
52ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes    if (mUtfChars) {
53ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes      mEnv->ReleaseStringUTFChars(mString, mUtfChars);
54f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom    }
55ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes  }
56f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom
57ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes  const char* c_str() const {
58ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes    return mUtfChars;
59ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes  }
60f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom
61ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes  size_t size() const {
62ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes    return strlen(mUtfChars);
63ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes  }
64f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom
65ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes  // Element access.
66ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes  const char& operator[](size_t n) const {
67ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes    return mUtfChars[n];
68ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes  }
69f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom
70ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes private:
71ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes  JNIEnv* mEnv;
72ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes  jstring mString;
73ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes  const char* mUtfChars;
74f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom
75ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes  // Disallow copy and assignment.
76ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes  NullableScopedUtfChars(const NullableScopedUtfChars&);
77ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes  void operator=(const NullableScopedUtfChars&);
78f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom};
79f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom
80c393a4f7c91534ac41a81d66b12c22e68df28fc7jeffhaostatic jint DexFile_openDexFile(JNIEnv* env, jclass, jstring javaSourceName, jstring javaOutputName, jint) {
81f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom  ScopedUtfChars sourceName(env, javaSourceName);
82f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom  if (sourceName.c_str() == NULL) {
83f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom    return 0;
84f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom  }
855b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom  std::string source(sourceName.c_str());
86f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom  NullableScopedUtfChars outputName(env, javaOutputName);
87c252c3eacd83a0c110dd065690a7f652be35b0e7Brian Carlstrom  if (env->ExceptionCheck()) {
88f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom    return 0;
89f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom  }
90262bf46ddc91e5b4fbd367127ff21a1877d939f2jeffhao  const DexFile* dex_file;
91262bf46ddc91e5b4fbd367127ff21a1877d939f2jeffhao  if (outputName.c_str() == NULL) {
925b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom    dex_file = Runtime::Current()->GetClassLinker()->FindDexFileInOatFileFromDexLocation(source);
93262bf46ddc91e5b4fbd367127ff21a1877d939f2jeffhao  } else {
945b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom    std::string output(outputName.c_str());
955b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom    dex_file = Runtime::Current()->GetClassLinker()->FindOrCreateOatFileForDexLocation(source, output);
96c393a4f7c91534ac41a81d66b12c22e68df28fc7jeffhao  }
97aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  if (dex_file == NULL) {
985b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom    LOG(WARNING) << "Failed to open dex file: " << source;
99d601af8d9549ca95a911afb2fc67c4e562da661fBrian Carlstrom    jniThrowExceptionFmt(env, "java/io/IOException", "unable to open dex file: %s",
1005b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom                         source.c_str());
101c393a4f7c91534ac41a81d66b12c22e68df28fc7jeffhao    return 0;
102aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
103aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  return static_cast<jint>(reinterpret_cast<uintptr_t>(dex_file));
104aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom}
105aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
106aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstromstatic const DexFile* toDexFile(JNIEnv* env, int dex_file_address) {
107aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  const DexFile* dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(dex_file_address));
108b3e66dfcd069db8818cd902d787ff1d7bbca45f2Elliott Hughes  if (dex_file == NULL) {
109aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    jniThrowNullPointerException(env, "dex_file == null");
110aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
111aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  return dex_file;
112f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom}
113f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom
1140512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic void DexFile_closeDexFile(JNIEnv* env, jclass, jint cookie) {
115aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  const DexFile* dex_file = toDexFile(env, cookie);
116aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  if (dex_file == NULL) {
117aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    return;
118aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
119aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  if (Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) {
120aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    return;
121aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
122aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  delete dex_file;
123f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom}
124f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom
1250512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic jclass DexFile_defineClassNative(JNIEnv* env, jclass, jstring javaName, jobject javaLoader,
1260512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughes                                        jint cookie) {
127b82b6878fb000d4731063b1bf15c39ff7c50b61fBrian Carlstrom  ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
128aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  const DexFile* dex_file = toDexFile(env, cookie);
129aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  if (dex_file == NULL) {
130aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    return NULL;
131aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
132df143242f4beaad4cc9fbabebfc033b68c40964eBrian Carlstrom  ScopedUtfChars class_name(env, javaName);
133df143242f4beaad4cc9fbabebfc033b68c40964eBrian Carlstrom  if (class_name.c_str() == NULL) {
134df143242f4beaad4cc9fbabebfc033b68c40964eBrian Carlstrom    return NULL;
135df143242f4beaad4cc9fbabebfc033b68c40964eBrian Carlstrom  }
136955724179c6c739524f610023287f56b24dc31deElliott Hughes  const std::string descriptor(DotToDescriptor(class_name.c_str()));
137aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
138aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  if (dex_class_def == NULL) {
139aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    return NULL;
140aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
141aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
142aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  Object* class_loader_object = Decode<Object*>(env, javaLoader);
143aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  ClassLoader* class_loader = down_cast<ClassLoader*>(class_loader_object);
144aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
145aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  class_linker->RegisterDexFile(*dex_file);
146aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  Class* result = class_linker->DefineClass(descriptor, class_loader, *dex_file, *dex_class_def);
147aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  return AddLocalReference<jclass>(env, result);
148f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom}
149f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom
1500512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jint cookie) {
151aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  const DexFile* dex_file = toDexFile(env, cookie);
152aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  if (dex_file == NULL) {
153aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    return NULL;
154aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
15503a20ba67cfdc46f5ad8d77242a666a4cb0512f2Brian Carlstrom
15603a20ba67cfdc46f5ad8d77242a666a4cb0512f2Brian Carlstrom  std::vector<std::string> class_names;
15703a20ba67cfdc46f5ad8d77242a666a4cb0512f2Brian Carlstrom  for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
15803a20ba67cfdc46f5ad8d77242a666a4cb0512f2Brian Carlstrom    const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
15903a20ba67cfdc46f5ad8d77242a666a4cb0512f2Brian Carlstrom    const char* descriptor = dex_file->GetClassDescriptor(class_def);
16003a20ba67cfdc46f5ad8d77242a666a4cb0512f2Brian Carlstrom    class_names.push_back(DescriptorToDot(descriptor));
16103a20ba67cfdc46f5ad8d77242a666a4cb0512f2Brian Carlstrom  }
16203a20ba67cfdc46f5ad8d77242a666a4cb0512f2Brian Carlstrom  return toStringArray(env, class_names);
163f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom}
164f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom
1650512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
166bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom  bool debug_logging = false;
167bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom
16803a20ba67cfdc46f5ad8d77242a666a4cb0512f2Brian Carlstrom  ScopedUtfChars filename(env, javaFilename);
16903a20ba67cfdc46f5ad8d77242a666a4cb0512f2Brian Carlstrom  if (filename.c_str() == NULL) {
170bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom    LOG(ERROR) << "DexFile_isDexOptNeeded null filename";
1711d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom    return JNI_TRUE;
1721d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom  }
1731d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom
1741d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom  if (!OS::FileExists(filename.c_str())) {
175bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom    LOG(ERROR) << "DexFile_isDexOptNeeded file '" << filename.c_str() << "' does not exist";
1761d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom    jniThrowExceptionFmt(env, "java/io/FileNotFoundException", "%s", filename.c_str());
1771d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom    return JNI_TRUE;
1781d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom  }
1791d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom
1801d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom  // Always treat elements of the bootclasspath as up-to-date.  The
1811d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom  // fact that code is running at all means that this should be true.
1821d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1831d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom  const std::vector<const DexFile*>& boot_class_path = class_linker->GetBootClassPath();
1841d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom  for (size_t i = 0; i < boot_class_path.size(); i++) {
1851d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom    if (boot_class_path[i]->GetLocation() == filename.c_str()) {
186bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom      if (debug_logging) {
187bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom        LOG(INFO) << "DexFile_isDexOptNeeded ignoring boot class path file: " << filename.c_str();
188bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom      }
1891d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom      return JNI_FALSE;
1901d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom    }
1911d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom  }
1921d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom
193a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  // If we have an oat file next to the dex file, assume up-to-date.
194a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  // A user build looks like this, and it will have no classes.dex in
195a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  // the input for checksum validation.
196a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  std::string oat_filename(OatFile::DexFilenameToOatFilename(filename.c_str()));
197a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  const OatFile* oat_file = class_linker->FindOatFileFromOatLocation(oat_filename);
198a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  if (oat_file != NULL && oat_file->GetOatDexFile(filename.c_str()) != NULL) {
199bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom    if (debug_logging) {
200bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom      LOG(INFO) << "DexFile_isDexOptNeeded ignoring precompiled file: " << filename.c_str();
201bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom    }
202a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    return JNI_FALSE;
2035b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom  }
2045b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom
205a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  // Check if we have an oat file in the cache
206a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  std::string cache_location(GetArtCacheFilenameOrDie(oat_filename));
207a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  oat_file = class_linker->FindOatFileFromOatLocation(cache_location);
2085b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom  if (oat_file == NULL) {
209bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom    LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
210bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom              << " does not exist for " << filename.c_str();
2115b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom    return JNI_TRUE;
2125b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom  }
2135b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom
2145b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom  const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(filename.c_str());
2155b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom  if (oat_dex_file == NULL) {
216bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom    LOG(ERROR) << "DexFile_isDexOptNeeded cache file " << cache_location
217bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom               << " does not contain contents for " << filename.c_str();
218bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom    std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file->GetOatDexFiles();
219bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom    for (size_t i = 0; i < oat_dex_files.size(); i++) {
220bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom      const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
221bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom      LOG(ERROR) << "DexFile_isDexOptNeeded cache file " << cache_location
222bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom                 << " contains contents for " << oat_dex_file->GetDexFileLocation();
223bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom    }
2241d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom    return JNI_TRUE;
2251d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom  }
2265b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom
227a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  uint32_t location_checksum;
228a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  if (!DexFile::GetChecksum(filename.c_str(), location_checksum)) {
229bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom    LOG(ERROR) << "DexFile_isDexOptNeeded failed to compute checksum of " << filename.c_str();
230a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    return JNI_TRUE;
231a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  }
232a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom
2335b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom  if (location_checksum != oat_dex_file->GetDexFileLocationChecksum()) {
234bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom    LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
235bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom              << " has out-of-date checksum compared to " << filename.c_str();
2365b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom    return JNI_TRUE;
2375b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom  }
2385b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom
239bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom  if (debug_logging) {
240bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom    LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
241bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom              << " is up-to-date for " << filename.c_str();
242bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom  }
243f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom  return JNI_FALSE;
244f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom}
245f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom
246f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstromstatic JNINativeMethod gMethods[] = {
247f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom  NATIVE_METHOD(DexFile, closeDexFile, "(I)V"),
24866a556f94e5dc9ba55bec9a11bee5671faa03e23Ian Rogers  NATIVE_METHOD(DexFile, defineClassNative, "(Ljava/lang/String;Ljava/lang/ClassLoader;I)Ljava/lang/Class;"),
249f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom  NATIVE_METHOD(DexFile, getClassNameList, "(I)[Ljava/lang/String;"),
250f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom  NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
251f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom  NATIVE_METHOD(DexFile, openDexFile, "(Ljava/lang/String;Ljava/lang/String;I)I"),
252f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom};
253f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom
254f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstromvoid register_dalvik_system_DexFile(JNIEnv* env) {
255f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom  jniRegisterNativeMethods(env, "dalvik/system/DexFile", gMethods, NELEM(gMethods));
256f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom}
257f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom
258f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom}  // namespace art
259