dalvik_system_DexFile.cc revision 66a556f94e5dc9ba55bec9a11bee5671faa03e23
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 <unistd.h> 18 19#include "class_loader.h" 20#include "class_linker.h" 21#include "dex_file.h" 22#include "logging.h" 23#include "os.h" 24#include "runtime.h" 25#include "zip_archive.h" 26#include "toStringArray.h" 27#include "ScopedLocalRef.h" 28#include "ScopedUtfChars.h" 29 30#include "JniConstants.h" // Last to avoid problems with LOG redefinition. 31 32namespace art { 33 34namespace { 35 36// A smart pointer that provides read-only access to a Java string's UTF chars. 37// Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if 38// passed a null jstring. The correct idiom is: 39// 40// NullableScopedUtfChars name(env, javaName); 41// if (env->ExceptionCheck()) { 42// return NULL; 43// } 44// // ... use name.c_str() 45// 46// TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option. 47class NullableScopedUtfChars { 48 public: 49 NullableScopedUtfChars(JNIEnv* env, jstring s) : mEnv(env), mString(s) { 50 mUtfChars = (s != NULL) ? env->GetStringUTFChars(s, NULL) : NULL; 51 } 52 53 ~NullableScopedUtfChars() { 54 if (mUtfChars) { 55 mEnv->ReleaseStringUTFChars(mString, mUtfChars); 56 } 57 } 58 59 const char* c_str() const { 60 return mUtfChars; 61 } 62 63 size_t size() const { 64 return strlen(mUtfChars); 65 } 66 67 // Element access. 68 const char& operator[](size_t n) const { 69 return mUtfChars[n]; 70 } 71 72 private: 73 JNIEnv* mEnv; 74 jstring mString; 75 const char* mUtfChars; 76 77 // Disallow copy and assignment. 78 NullableScopedUtfChars(const NullableScopedUtfChars&); 79 void operator=(const NullableScopedUtfChars&); 80}; 81 82static jint DexFile_openDexFile(JNIEnv* env, jclass, jstring javaSourceName, jstring javaOutputName, jint) { 83 ScopedUtfChars sourceName(env, javaSourceName); 84 if (sourceName.c_str() == NULL) { 85 return 0; 86 } 87 std::string source(sourceName.c_str()); 88 NullableScopedUtfChars outputName(env, javaOutputName); 89 if (env->ExceptionCheck()) { 90 return 0; 91 } 92 const DexFile* dex_file; 93 if (outputName.c_str() == NULL) { 94 dex_file = Runtime::Current()->GetClassLinker()->FindDexFileInOatFileFromDexLocation(source); 95 } else { 96 std::string output(outputName.c_str()); 97 dex_file = Runtime::Current()->GetClassLinker()->FindOrCreateOatFileForDexLocation(source, output); 98 } 99 if (dex_file == NULL) { 100 LOG(WARNING) << "Failed to open dex file: " << source; 101 jniThrowExceptionFmt(env, "java/io/IOException", "unable to open dex file: %s", 102 source.c_str()); 103 return 0; 104 } 105 return static_cast<jint>(reinterpret_cast<uintptr_t>(dex_file)); 106} 107 108static const DexFile* toDexFile(JNIEnv* env, int dex_file_address) { 109 const DexFile* dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(dex_file_address)); 110 if (dex_file == NULL) { 111 jniThrowNullPointerException(env, "dex_file == null"); 112 } 113 return dex_file; 114} 115 116void DexFile_closeDexFile(JNIEnv* env, jclass, jint cookie) { 117 const DexFile* dex_file = toDexFile(env, cookie); 118 if (dex_file == NULL) { 119 return; 120 } 121 if (Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) { 122 return; 123 } 124 delete dex_file; 125} 126 127jclass DexFile_defineClassNative(JNIEnv* env, jclass, jstring javaName, jobject javaLoader, 128 jint cookie) { 129 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable); 130 const DexFile* dex_file = toDexFile(env, cookie); 131 if (dex_file == NULL) { 132 return NULL; 133 } 134 ScopedUtfChars class_name(env, javaName); 135 if (class_name.c_str() == NULL) { 136 return NULL; 137 } 138 const std::string descriptor(DotToDescriptor(class_name.c_str())); 139 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor); 140 if (dex_class_def == NULL) { 141 return NULL; 142 } 143 144 Object* class_loader_object = Decode<Object*>(env, javaLoader); 145 ClassLoader* class_loader = down_cast<ClassLoader*>(class_loader_object); 146 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 147 class_linker->RegisterDexFile(*dex_file); 148 Class* result = class_linker->DefineClass(descriptor, class_loader, *dex_file, *dex_class_def); 149 return AddLocalReference<jclass>(env, result); 150} 151 152jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jint cookie) { 153 const DexFile* dex_file = toDexFile(env, cookie); 154 if (dex_file == NULL) { 155 return NULL; 156 } 157 158 std::vector<std::string> class_names; 159 for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) { 160 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i); 161 const char* descriptor = dex_file->GetClassDescriptor(class_def); 162 class_names.push_back(DescriptorToDot(descriptor)); 163 } 164 return toStringArray(env, class_names); 165} 166 167jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) { 168 ScopedUtfChars filename(env, javaFilename); 169 if (filename.c_str() == NULL) { 170 return JNI_TRUE; 171 } 172 173 if (!OS::FileExists(filename.c_str())) { 174 jniThrowExceptionFmt(env, "java/io/FileNotFoundException", "%s", filename.c_str()); 175 return JNI_TRUE; 176 } 177 178 // Always treat elements of the bootclasspath as up-to-date. The 179 // fact that code is running at all means that this should be true. 180 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 181 const std::vector<const DexFile*>& boot_class_path = class_linker->GetBootClassPath(); 182 for (size_t i = 0; i < boot_class_path.size(); i++) { 183 if (boot_class_path[i]->GetLocation() == filename.c_str()) { 184 return JNI_FALSE; 185 } 186 } 187 188 uint32_t location_checksum; 189 if (!DexFile::GetChecksum(filename.c_str(), location_checksum)) { 190 return JNI_TRUE; 191 } 192 193 std::string oat_filename(OatFile::DexFilenameToOatFilename(filename.c_str())); 194 const OatFile* oat_file(class_linker->FindOatFileFromOatLocation(oat_filename)); 195 if (oat_file == NULL) { 196 return JNI_TRUE; 197 } 198 199 const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(filename.c_str()); 200 if (oat_dex_file == NULL) { 201 return JNI_TRUE; 202 } 203 204 if (location_checksum != oat_dex_file->GetDexFileLocationChecksum()) { 205 return JNI_TRUE; 206 } 207 208 return JNI_FALSE; 209} 210 211static JNINativeMethod gMethods[] = { 212 NATIVE_METHOD(DexFile, closeDexFile, "(I)V"), 213 NATIVE_METHOD(DexFile, defineClassNative, "(Ljava/lang/String;Ljava/lang/ClassLoader;I)Ljava/lang/Class;"), 214 NATIVE_METHOD(DexFile, getClassNameList, "(I)[Ljava/lang/String;"), 215 NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"), 216 NATIVE_METHOD(DexFile, openDexFile, "(Ljava/lang/String;Ljava/lang/String;I)I"), 217}; 218 219} // namespace 220 221void register_dalvik_system_DexFile(JNIEnv* env) { 222 jniRegisterNativeMethods(env, "dalvik/system/DexFile", gMethods, NELEM(gMethods)); 223} 224 225} // namespace art 226