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