dalvik_system_DexFile.cc revision 1d54e73444e017d3a65234e0f193846f3e27472b
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 1907ed66b5ae659c452cbe1ab20c3dbf1d6f546461Elliott Hughes#include "base/logging.h" 20aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom#include "class_linker.h" 2162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers#include "common_throws.h" 224f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers#include "dex_file-inl.h" 231d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/space/image_space.h" 241d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/space/space-inl.h" 2581f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom#include "image.h" 26eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes#include "jni_internal.h" 272dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/class_loader.h" 2805f3057d6a4d23d712092ccd36a531590bff323bIan Rogers#include "mirror/object-inl.h" 292dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/string.h" 30700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom#include "oat.h" 311d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom#include "os.h" 32aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom#include "runtime.h" 3300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "scoped_thread_state_change.h" 34c981848a3425662034c5429b61c035e7533b896dIan Rogers#include "ScopedLocalRef.h" 35f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom#include "ScopedUtfChars.h" 36eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes#include "toStringArray.h" 37eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes#include "zip_archive.h" 38f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom 39f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstromnamespace art { 40f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom 41f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom// A smart pointer that provides read-only access to a Java string's UTF chars. 42f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom// Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if 43f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom// passed a null jstring. The correct idiom is: 44f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom// 45f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom// NullableScopedUtfChars name(env, javaName); 46c252c3eacd83a0c110dd065690a7f652be35b0e7Brian Carlstrom// if (env->ExceptionCheck()) { 47f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom// return NULL; 48f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom// } 49f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom// // ... use name.c_str() 50f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom// 51f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom// TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option. 52f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstromclass NullableScopedUtfChars { 53ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes public: 54ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes NullableScopedUtfChars(JNIEnv* env, jstring s) : mEnv(env), mString(s) { 55ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes mUtfChars = (s != NULL) ? env->GetStringUTFChars(s, NULL) : NULL; 56ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes } 57f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom 58ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes ~NullableScopedUtfChars() { 59ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes if (mUtfChars) { 60ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes mEnv->ReleaseStringUTFChars(mString, mUtfChars); 61f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom } 62ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes } 63f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom 64ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes const char* c_str() const { 65ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes return mUtfChars; 66ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes } 67f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom 68ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes size_t size() const { 69ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes return strlen(mUtfChars); 70ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes } 71f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom 72ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes // Element access. 73ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes const char& operator[](size_t n) const { 74ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes return mUtfChars[n]; 75ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes } 76f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom 77ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes private: 78ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes JNIEnv* mEnv; 79ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes jstring mString; 80ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes const char* mUtfChars; 81f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom 82ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes // Disallow copy and assignment. 83ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes NullableScopedUtfChars(const NullableScopedUtfChars&); 84ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes void operator=(const NullableScopedUtfChars&); 85f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom}; 86f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom 87c393a4f7c91534ac41a81d66b12c22e68df28fc7jeffhaostatic jint DexFile_openDexFile(JNIEnv* env, jclass, jstring javaSourceName, jstring javaOutputName, jint) { 88f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom ScopedUtfChars sourceName(env, javaSourceName); 89f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom if (sourceName.c_str() == NULL) { 90f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom return 0; 91f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom } 925b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom std::string source(sourceName.c_str()); 93f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom NullableScopedUtfChars outputName(env, javaOutputName); 94c252c3eacd83a0c110dd065690a7f652be35b0e7Brian Carlstrom if (env->ExceptionCheck()) { 95f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom return 0; 96f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom } 9700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ScopedObjectAccess soa(env); 98262bf46ddc91e5b4fbd367127ff21a1877d939f2jeffhao const DexFile* dex_file; 99262bf46ddc91e5b4fbd367127ff21a1877d939f2jeffhao if (outputName.c_str() == NULL) { 1005b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom dex_file = Runtime::Current()->GetClassLinker()->FindDexFileInOatFileFromDexLocation(source); 101262bf46ddc91e5b4fbd367127ff21a1877d939f2jeffhao } else { 1025b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom std::string output(outputName.c_str()); 10300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers dex_file = 10400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Runtime::Current()->GetClassLinker()->FindOrCreateOatFileForDexLocation(source, output); 105c393a4f7c91534ac41a81d66b12c22e68df28fc7jeffhao } 106aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom if (dex_file == NULL) { 1075b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom LOG(WARNING) << "Failed to open dex file: " << source; 10862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow(); 10962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/io/IOException;", 11062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers "Unable to open dex file: %s", source.c_str()); 111c393a4f7c91534ac41a81d66b12c22e68df28fc7jeffhao return 0; 112aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom } 113aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom return static_cast<jint>(reinterpret_cast<uintptr_t>(dex_file)); 114aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom} 115aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom 11662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogersstatic const DexFile* toDexFile(int dex_file_address) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 117aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom const DexFile* dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(dex_file_address)); 118b3e66dfcd069db8818cd902d787ff1d7bbca45f2Elliott Hughes if (dex_file == NULL) { 11962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers ThrowNullPointerException(NULL, "dex_file == null"); 120aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom } 121aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom return dex_file; 122f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom} 123f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom 12400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic void DexFile_closeDexFile(JNIEnv* env, jclass, jint cookie) { 12500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers const DexFile* dex_file; 12600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers { 12700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ScopedObjectAccess soa(env); 12800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers dex_file = toDexFile(cookie); 12900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 130aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom if (dex_file == NULL) { 131aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom return; 132aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom } 133aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom if (Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) { 134aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom return; 135aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom } 136aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom delete dex_file; 137f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom} 138f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom 1390512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic jclass DexFile_defineClassNative(JNIEnv* env, jclass, jstring javaName, jobject javaLoader, 1400512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughes jint cookie) { 14100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ScopedObjectAccess soa(env); 142eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes const DexFile* dex_file = toDexFile(cookie); 143aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom if (dex_file == NULL) { 144aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom return NULL; 145aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom } 146df143242f4beaad4cc9fbabebfc033b68c40964eBrian Carlstrom ScopedUtfChars class_name(env, javaName); 147df143242f4beaad4cc9fbabebfc033b68c40964eBrian Carlstrom if (class_name.c_str() == NULL) { 148df143242f4beaad4cc9fbabebfc033b68c40964eBrian Carlstrom return NULL; 149df143242f4beaad4cc9fbabebfc033b68c40964eBrian Carlstrom } 150955724179c6c739524f610023287f56b24dc31deElliott Hughes const std::string descriptor(DotToDescriptor(class_name.c_str())); 151aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor); 152aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom if (dex_class_def == NULL) { 153aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom return NULL; 154aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom } 155aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 156aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom class_linker->RegisterDexFile(*dex_file); 1572dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::ClassLoader* class_loader = soa.Decode<mirror::ClassLoader*>(javaLoader); 1582dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::Class* result = class_linker->DefineClass(descriptor, class_loader, *dex_file, *dex_class_def); 15900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers return soa.AddLocalReference<jclass>(result); 160f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom} 161f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom 1620512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jint cookie) { 16300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers const DexFile* dex_file; 16400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers { 16500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ScopedObjectAccess soa(env); 16600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers dex_file = toDexFile(cookie); 16700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 168aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom if (dex_file == NULL) { 169aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom return NULL; 170aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom } 17103a20ba67cfdc46f5ad8d77242a666a4cb0512f2Brian Carlstrom 17203a20ba67cfdc46f5ad8d77242a666a4cb0512f2Brian Carlstrom std::vector<std::string> class_names; 17303a20ba67cfdc46f5ad8d77242a666a4cb0512f2Brian Carlstrom for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) { 17403a20ba67cfdc46f5ad8d77242a666a4cb0512f2Brian Carlstrom const DexFile::ClassDef& class_def = dex_file->GetClassDef(i); 17503a20ba67cfdc46f5ad8d77242a666a4cb0512f2Brian Carlstrom const char* descriptor = dex_file->GetClassDescriptor(class_def); 17603a20ba67cfdc46f5ad8d77242a666a4cb0512f2Brian Carlstrom class_names.push_back(DescriptorToDot(descriptor)); 17703a20ba67cfdc46f5ad8d77242a666a4cb0512f2Brian Carlstrom } 17803a20ba67cfdc46f5ad8d77242a666a4cb0512f2Brian Carlstrom return toStringArray(env, class_names); 179f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom} 180f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom 1810512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) { 182bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom bool debug_logging = false; 183bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom 18403a20ba67cfdc46f5ad8d77242a666a4cb0512f2Brian Carlstrom ScopedUtfChars filename(env, javaFilename); 18503a20ba67cfdc46f5ad8d77242a666a4cb0512f2Brian Carlstrom if (filename.c_str() == NULL) { 186bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom LOG(ERROR) << "DexFile_isDexOptNeeded null filename"; 1871d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom return JNI_TRUE; 1881d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom } 1891d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom 1901d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom if (!OS::FileExists(filename.c_str())) { 191bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom LOG(ERROR) << "DexFile_isDexOptNeeded file '" << filename.c_str() << "' does not exist"; 19200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ScopedObjectAccess soa(env); 19362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow(); 19462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/io/FileNotFoundException;", 19562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers "%s", filename.c_str()); 1961d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom return JNI_TRUE; 1971d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom } 1981d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom 1991d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom // Always treat elements of the bootclasspath as up-to-date. The 2001d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom // fact that code is running at all means that this should be true. 20181f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom Runtime* runtime = Runtime::Current(); 20281f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom ClassLinker* class_linker = runtime->GetClassLinker(); 2031d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom const std::vector<const DexFile*>& boot_class_path = class_linker->GetBootClassPath(); 2041d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom for (size_t i = 0; i < boot_class_path.size(); i++) { 2051d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom if (boot_class_path[i]->GetLocation() == filename.c_str()) { 206bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom if (debug_logging) { 207bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom LOG(INFO) << "DexFile_isDexOptNeeded ignoring boot class path file: " << filename.c_str(); 208bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom } 2091d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom return JNI_FALSE; 2101d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom } 2111d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom } 2121d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom 21330e2ea4a701d53f28431041af68dc0669e60c569Brian Carlstrom // Check if we have an odex file next to the dex file. 21430e2ea4a701d53f28431041af68dc0669e60c569Brian Carlstrom std::string odex_filename(OatFile::DexFilenameToOdexFilename(filename.c_str())); 21530e2ea4a701d53f28431041af68dc0669e60c569Brian Carlstrom UniquePtr<const OatFile> oat_file(OatFile::Open(odex_filename, odex_filename, NULL)); 21633e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers if (oat_file.get() != NULL) { 21700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ScopedObjectAccess soa(env); 21833e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers const art::OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(filename.c_str()); 21933e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers if (oat_dex_file == NULL) { 220afe25515c358617321d69ab4f25003e3d905d613Brian Carlstrom if (debug_logging) { 22133e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers LOG(INFO) << "DexFile_isDexOptNeeded GetOatDexFile failed"; 22233e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers } 22333e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers } else { 22433e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers uint32_t location_checksum; 22533e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers // If we have no classes.dex checksum such as in a user build, assume up-to-date. 22633e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers if (!DexFile::GetChecksum(filename.c_str(), location_checksum)) { 22733e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers if (debug_logging) { 22833e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers LOG(INFO) << "DexFile_isDexOptNeeded ignoring precompiled stripped file: " 22933e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers << filename.c_str(); 23033e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers } 23133e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers return JNI_FALSE; 23233e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers } 23333e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers if (ClassLinker::VerifyOatFileChecksums(oat_file.get(), filename.c_str(), location_checksum)) { 23433e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers if (debug_logging) { 23530e2ea4a701d53f28431041af68dc0669e60c569Brian Carlstrom LOG(INFO) << "DexFile_isDexOptNeeded precompiled file " << odex_filename 23633e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers << " is up-to-date checksum compared to " << filename.c_str(); 23733e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers } 23833e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers return JNI_FALSE; 239afe25515c358617321d69ab4f25003e3d905d613Brian Carlstrom } 240bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom } 2415b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom } 2425b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom 243a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom // Check if we have an oat file in the cache 24430e2ea4a701d53f28431041af68dc0669e60c569Brian Carlstrom std::string cache_location(GetDalvikCacheFilenameOrDie(filename.c_str())); 24530e2ea4a701d53f28431041af68dc0669e60c569Brian Carlstrom oat_file.reset(OatFile::Open(cache_location, filename.c_str(), NULL)); 24658cbbc25c91b96f4766c66cefa0a0cf6ba7b1d45Brian Carlstrom if (oat_file.get() == NULL) { 247bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location 248bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom << " does not exist for " << filename.c_str(); 2495b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom return JNI_TRUE; 2505b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom } 2515b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom 2521d54e73444e017d3a65234e0f193846f3e27472bIan Rogers gc::Heap* heap = runtime->GetHeap(); 2531d54e73444e017d3a65234e0f193846f3e27472bIan Rogers const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces(); 254b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier // TODO: C++0x auto 2551d54e73444e017d3a65234e0f193846f3e27472bIan Rogers typedef std::vector<gc::space::ContinuousSpace*>::const_iterator It; 2561d54e73444e017d3a65234e0f193846f3e27472bIan Rogers for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) { 2571d54e73444e017d3a65234e0f193846f3e27472bIan Rogers if ((*it)->IsImageSpace()) { 258b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier // TODO: Ensure this works with multiple image spaces. 2591d54e73444e017d3a65234e0f193846f3e27472bIan Rogers const ImageHeader& image_header = (*it)->AsImageSpace()->GetImageHeader(); 26028db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom if (oat_file->GetOatHeader().GetImageFileLocationOatChecksum() != image_header.GetOatChecksum()) { 26100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ScopedObjectAccess soa(env); 262b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location 26328db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom << " has out-of-date oat checksum compared to " 26428db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom << image_header.GetImageRoot(ImageHeader::kOatLocation)->AsString()->ToModifiedUtf8(); 26528db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom return JNI_TRUE; 26628db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom } 267700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom if (oat_file->GetOatHeader().GetImageFileLocationOatDataBegin() 268700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom != reinterpret_cast<uint32_t>(image_header.GetOatDataBegin())) { 26928db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom ScopedObjectAccess soa(env); 27028db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location 27128db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom << " has out-of-date oat begin compared to " 272b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier << image_header.GetImageRoot(ImageHeader::kOatLocation)->AsString()->ToModifiedUtf8(); 273b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier return JNI_TRUE; 274b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier } 275b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier } 27681f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom } 27781f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom 27833e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers ScopedObjectAccess soa(env); 279a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom uint32_t location_checksum; 280a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom if (!DexFile::GetChecksum(filename.c_str(), location_checksum)) { 281bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom LOG(ERROR) << "DexFile_isDexOptNeeded failed to compute checksum of " << filename.c_str(); 282a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom return JNI_TRUE; 283a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom } 284a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom 285afe25515c358617321d69ab4f25003e3d905d613Brian Carlstrom if (!ClassLinker::VerifyOatFileChecksums(oat_file.get(), filename.c_str(), location_checksum)) { 286bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location 28700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers << " has out-of-date checksum compared to " << filename.c_str(); 2885b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom return JNI_TRUE; 2895b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom } 2905b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom 291bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom if (debug_logging) { 292bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location 293bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom << " is up-to-date for " << filename.c_str(); 294bf2cb16f442fc48acd296d3d321590fb58173f36Brian Carlstrom } 295f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom return JNI_FALSE; 296f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom} 297f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom 298f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstromstatic JNINativeMethod gMethods[] = { 299f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom NATIVE_METHOD(DexFile, closeDexFile, "(I)V"), 30066a556f94e5dc9ba55bec9a11bee5671faa03e23Ian Rogers NATIVE_METHOD(DexFile, defineClassNative, "(Ljava/lang/String;Ljava/lang/ClassLoader;I)Ljava/lang/Class;"), 301f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom NATIVE_METHOD(DexFile, getClassNameList, "(I)[Ljava/lang/String;"), 302f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"), 303f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom NATIVE_METHOD(DexFile, openDexFile, "(Ljava/lang/String;Ljava/lang/String;I)I"), 304f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom}; 305f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom 306f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstromvoid register_dalvik_system_DexFile(JNIEnv* env) { 307eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes REGISTER_NATIVE_METHODS("dalvik/system/DexFile"); 308f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom} 309f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom 310f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom} // namespace art 311