Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 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 | |
Brian Carlstrom | 1d9f52b | 2011-10-13 10:50:45 -0700 | [diff] [blame] | 17 | #include <unistd.h> |
| 18 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 19 | #include "class_loader.h" |
| 20 | #include "class_linker.h" |
| 21 | #include "dex_file.h" |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 22 | #include "logging.h" |
Brian Carlstrom | 1d9f52b | 2011-10-13 10:50:45 -0700 | [diff] [blame] | 23 | #include "os.h" |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 24 | #include "runtime.h" |
jeffhao | c393a4f | 2011-10-19 13:46:09 -0700 | [diff] [blame] | 25 | #include "zip_archive.h" |
Brian Carlstrom | 03a20ba | 2011-10-13 10:24:13 -0700 | [diff] [blame] | 26 | #include "toStringArray.h" |
Ian Rogers | c981848 | 2012-01-11 08:52:51 -0800 | [diff] [blame] | 27 | #include "ScopedLocalRef.h" |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 28 | #include "ScopedUtfChars.h" |
| 29 | |
| 30 | #include "JniConstants.h" // Last to avoid problems with LOG redefinition. |
| 31 | |
| 32 | namespace art { |
| 33 | |
| 34 | namespace { |
| 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); |
Brian Carlstrom | c252c3e | 2011-10-16 23:21:02 -0700 | [diff] [blame] | 41 | // if (env->ExceptionCheck()) { |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 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. |
| 47 | class NullableScopedUtfChars { |
| 48 | public: |
| 49 | NullableScopedUtfChars(JNIEnv* env, jstring s) |
| 50 | : mEnv(env), mString(s) |
| 51 | { |
| 52 | mUtfChars = (s != NULL) ? env->GetStringUTFChars(s, NULL) : NULL; |
| 53 | } |
| 54 | |
| 55 | ~NullableScopedUtfChars() { |
| 56 | if (mUtfChars) { |
| 57 | mEnv->ReleaseStringUTFChars(mString, mUtfChars); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | const char* c_str() const { |
| 62 | return mUtfChars; |
| 63 | } |
| 64 | |
| 65 | size_t size() const { |
| 66 | return strlen(mUtfChars); |
| 67 | } |
| 68 | |
| 69 | // Element access. |
| 70 | const char& operator[](size_t n) const { |
| 71 | return mUtfChars[n]; |
| 72 | } |
| 73 | |
| 74 | private: |
| 75 | JNIEnv* mEnv; |
| 76 | jstring mString; |
| 77 | const char* mUtfChars; |
| 78 | |
| 79 | // Disallow copy and assignment. |
| 80 | NullableScopedUtfChars(const NullableScopedUtfChars&); |
| 81 | void operator=(const NullableScopedUtfChars&); |
| 82 | }; |
| 83 | |
jeffhao | c393a4f | 2011-10-19 13:46:09 -0700 | [diff] [blame] | 84 | static jint DexFile_openDexFile(JNIEnv* env, jclass, jstring javaSourceName, jstring javaOutputName, jint) { |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 85 | ScopedUtfChars sourceName(env, javaSourceName); |
| 86 | if (sourceName.c_str() == NULL) { |
| 87 | return 0; |
| 88 | } |
| 89 | NullableScopedUtfChars outputName(env, javaOutputName); |
Brian Carlstrom | c252c3e | 2011-10-16 23:21:02 -0700 | [diff] [blame] | 90 | if (env->ExceptionCheck()) { |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 91 | return 0; |
| 92 | } |
jeffhao | c393a4f | 2011-10-19 13:46:09 -0700 | [diff] [blame] | 93 | const DexFile* dex_file; |
| 94 | if (outputName.c_str() == NULL) { |
| 95 | dex_file = DexFile::Open(sourceName.c_str(), ""); |
| 96 | } else { |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 97 | // Sanity check the arguments. |
| 98 | if (!IsValidZipFilename(sourceName.c_str()) || !IsValidDexFilename(outputName.c_str())) { |
| 99 | LOG(ERROR) << "Bad filenames extracting dex '" << outputName.c_str() |
| 100 | << "' from zip '" << sourceName.c_str() << "'"; |
Brian Carlstrom | 2e3d1b2 | 2012-01-09 18:01:56 -0800 | [diff] [blame] | 101 | return 0; |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 102 | } |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 103 | // Generate the output oat file for the source dex file |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 104 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 105 | UniquePtr<File> file(OS::OpenFile(outputName.c_str(), true)); |
| 106 | if (file.get() == NULL) { |
Ian Rogers | 725aee5 | 2012-01-11 11:56:56 -0800 | [diff] [blame] | 107 | LOG(WARNING) << "unable to create oat file: " << outputName.c_str(); |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 108 | jniThrowExceptionFmt(env, "java/io/IOException", "unable to create oat file: %s", |
| 109 | outputName.c_str()); |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 110 | return 0; |
| 111 | } |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 112 | if (!class_linker->GenerateOatFile(sourceName.c_str(), file->Fd(), outputName.c_str())) { |
Ian Rogers | 725aee5 | 2012-01-11 11:56:56 -0800 | [diff] [blame] | 113 | LOG(WARNING) << "unable to generate oat file: " << outputName.c_str(); |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 114 | jniThrowExceptionFmt(env, "java/io/IOException", "unable to generate oat file: %s", |
| 115 | outputName.c_str()); |
| 116 | return 0; |
| 117 | } |
| 118 | UniquePtr<OatFile> oat_file(OatFile::Open(outputName.c_str(), "", NULL)); |
| 119 | if (oat_file.get() == NULL) { |
Ian Rogers | 725aee5 | 2012-01-11 11:56:56 -0800 | [diff] [blame] | 120 | LOG(WARNING) << "unable to open oat file: " << outputName.c_str(); |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 121 | jniThrowExceptionFmt(env, "java/io/IOException", "unable to open oat file: %s", |
| 122 | outputName.c_str()); |
| 123 | return 0; |
| 124 | } |
| 125 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(sourceName.c_str()); |
| 126 | if (oat_dex_file == NULL) { |
Ian Rogers | 725aee5 | 2012-01-11 11:56:56 -0800 | [diff] [blame] | 127 | LOG(WARNING) << "unable to find dex file in oat file: " << outputName.c_str(); |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 128 | jniThrowExceptionFmt(env, "java/io/IOException", "unable to find dex file in oat file: %s", |
| 129 | outputName.c_str()); |
| 130 | return 0; |
| 131 | } |
Ian Rogers | 725aee5 | 2012-01-11 11:56:56 -0800 | [diff] [blame] | 132 | Runtime::Current()->GetClassLinker()->RegisterOatFile(*oat_file.release()); |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 133 | dex_file = oat_dex_file->OpenDexFile(); |
jeffhao | c393a4f | 2011-10-19 13:46:09 -0700 | [diff] [blame] | 134 | } |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 135 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 136 | if (dex_file == NULL) { |
Ian Rogers | 725aee5 | 2012-01-11 11:56:56 -0800 | [diff] [blame] | 137 | LOG(WARNING) << "unable to open dex file: " << sourceName.c_str(); |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 138 | jniThrowExceptionFmt(env, "java/io/IOException", "unable to open dex file: %s", |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 139 | sourceName.c_str()); |
jeffhao | c393a4f | 2011-10-19 13:46:09 -0700 | [diff] [blame] | 140 | return 0; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 141 | } |
| 142 | return static_cast<jint>(reinterpret_cast<uintptr_t>(dex_file)); |
| 143 | } |
| 144 | |
| 145 | static const DexFile* toDexFile(JNIEnv* env, int dex_file_address) { |
| 146 | const DexFile* dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(dex_file_address)); |
Elliott Hughes | b3e66df | 2012-01-12 14:49:18 -0800 | [diff] [blame] | 147 | if (dex_file == NULL) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 148 | jniThrowNullPointerException(env, "dex_file == null"); |
| 149 | } |
| 150 | return dex_file; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void DexFile_closeDexFile(JNIEnv* env, jclass, jint cookie) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 154 | const DexFile* dex_file = toDexFile(env, cookie); |
| 155 | if (dex_file == NULL) { |
| 156 | return; |
| 157 | } |
| 158 | if (Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) { |
| 159 | return; |
| 160 | } |
| 161 | delete dex_file; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | jclass DexFile_defineClass(JNIEnv* env, jclass, jstring javaName, jobject javaLoader, jint cookie) { |
Brian Carlstrom | b82b687 | 2011-10-26 17:18:07 -0700 | [diff] [blame] | 165 | ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 166 | const DexFile* dex_file = toDexFile(env, cookie); |
| 167 | if (dex_file == NULL) { |
| 168 | return NULL; |
| 169 | } |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 170 | ScopedUtfChars class_name(env, javaName); |
| 171 | if (class_name.c_str() == NULL) { |
| 172 | return NULL; |
| 173 | } |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 174 | const std::string descriptor(DotToDescriptor(class_name.c_str())); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 175 | const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor); |
| 176 | if (dex_class_def == NULL) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 177 | return NULL; |
| 178 | } |
| 179 | |
| 180 | Object* class_loader_object = Decode<Object*>(env, javaLoader); |
| 181 | ClassLoader* class_loader = down_cast<ClassLoader*>(class_loader_object); |
| 182 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 183 | class_linker->RegisterDexFile(*dex_file); |
| 184 | Class* result = class_linker->DefineClass(descriptor, class_loader, *dex_file, *dex_class_def); |
Brian Carlstrom | 41ec477 | 2011-10-11 01:35:58 -0700 | [diff] [blame] | 185 | if (env->ExceptionCheck()) { |
Elliott Hughes | e8f3212 | 2012-01-24 18:23:30 -0800 | [diff] [blame] | 186 | // If we threw a ClassNotFoundException, stifle it, since the contract in the caller |
| 187 | // says we simply return null if the class is not found. |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 188 | jthrowable exception = env->ExceptionOccurred(); |
Brian Carlstrom | 41ec477 | 2011-10-11 01:35:58 -0700 | [diff] [blame] | 189 | env->ExceptionClear(); |
Elliott Hughes | e8f3212 | 2012-01-24 18:23:30 -0800 | [diff] [blame] | 190 | ScopedLocalRef<jclass> exception_class(env, env->FindClass("java/lang/ClassNotFoundException")); |
| 191 | if (!env->IsInstanceOf(exception, exception_class.get())) { |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 192 | env->Throw(exception); |
| 193 | } |
Brian Carlstrom | 41ec477 | 2011-10-11 01:35:58 -0700 | [diff] [blame] | 194 | return NULL; |
| 195 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 196 | return AddLocalReference<jclass>(env, result); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jint cookie) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 200 | const DexFile* dex_file = toDexFile(env, cookie); |
| 201 | if (dex_file == NULL) { |
| 202 | return NULL; |
| 203 | } |
Brian Carlstrom | 03a20ba | 2011-10-13 10:24:13 -0700 | [diff] [blame] | 204 | |
| 205 | std::vector<std::string> class_names; |
| 206 | for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) { |
| 207 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(i); |
| 208 | const char* descriptor = dex_file->GetClassDescriptor(class_def); |
| 209 | class_names.push_back(DescriptorToDot(descriptor)); |
| 210 | } |
| 211 | return toStringArray(env, class_names); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) { |
Brian Carlstrom | 03a20ba | 2011-10-13 10:24:13 -0700 | [diff] [blame] | 215 | ScopedUtfChars filename(env, javaFilename); |
| 216 | if (filename.c_str() == NULL) { |
Brian Carlstrom | 1d9f52b | 2011-10-13 10:50:45 -0700 | [diff] [blame] | 217 | return JNI_TRUE; |
Brian Carlstrom | 03a20ba | 2011-10-13 10:24:13 -0700 | [diff] [blame] | 218 | } |
Brian Carlstrom | 1d9f52b | 2011-10-13 10:50:45 -0700 | [diff] [blame] | 219 | |
| 220 | if (!OS::FileExists(filename.c_str())) { |
| 221 | jniThrowExceptionFmt(env, "java/io/FileNotFoundException", "%s", filename.c_str()); |
| 222 | return JNI_TRUE; |
| 223 | } |
| 224 | |
| 225 | // Always treat elements of the bootclasspath as up-to-date. The |
| 226 | // fact that code is running at all means that this should be true. |
| 227 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 228 | const std::vector<const DexFile*>& boot_class_path = class_linker->GetBootClassPath(); |
| 229 | for (size_t i = 0; i < boot_class_path.size(); i++) { |
| 230 | if (boot_class_path[i]->GetLocation() == filename.c_str()) { |
| 231 | return JNI_FALSE; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | UniquePtr<const DexFile> dex_file(DexFile::Open(filename.c_str(), "")); |
| 236 | if (dex_file.get() == NULL) { |
| 237 | return JNI_TRUE; |
| 238 | } |
| 239 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 240 | const OatFile* oat_file = class_linker->FindOatFileForDexFile(*dex_file.get()); |
Brian Carlstrom | fad7143 | 2011-10-16 20:25:10 -0700 | [diff] [blame] | 241 | if (oat_file == NULL) { |
Brian Carlstrom | 1d9f52b | 2011-10-13 10:50:45 -0700 | [diff] [blame] | 242 | return JNI_TRUE; |
| 243 | } |
Brian Carlstrom | fad7143 | 2011-10-16 20:25:10 -0700 | [diff] [blame] | 244 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file->GetLocation()); |
| 245 | if (oat_dex_file == NULL) { |
| 246 | return JNI_TRUE; |
| 247 | } |
| 248 | if (oat_dex_file->GetDexFileChecksum() != dex_file->GetHeader().checksum_) { |
| 249 | return JNI_TRUE; |
| 250 | } |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 251 | return JNI_FALSE; |
| 252 | } |
| 253 | |
| 254 | static JNINativeMethod gMethods[] = { |
| 255 | NATIVE_METHOD(DexFile, closeDexFile, "(I)V"), |
| 256 | NATIVE_METHOD(DexFile, defineClass, "(Ljava/lang/String;Ljava/lang/ClassLoader;I)Ljava/lang/Class;"), |
| 257 | NATIVE_METHOD(DexFile, getClassNameList, "(I)[Ljava/lang/String;"), |
| 258 | NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"), |
| 259 | NATIVE_METHOD(DexFile, openDexFile, "(Ljava/lang/String;Ljava/lang/String;I)I"), |
| 260 | }; |
| 261 | |
| 262 | } // namespace |
| 263 | |
| 264 | void register_dalvik_system_DexFile(JNIEnv* env) { |
| 265 | jniRegisterNativeMethods(env, "dalvik/system/DexFile", gMethods, NELEM(gMethods)); |
| 266 | } |
| 267 | |
| 268 | } // namespace art |