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" |
jeffhao | c393a4f | 2011-10-19 13:46:09 -0700 | [diff] [blame^] | 22 | #include "file.h" |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 23 | #include "logging.h" |
Brian Carlstrom | 1d9f52b | 2011-10-13 10:50:45 -0700 | [diff] [blame] | 24 | #include "os.h" |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 25 | #include "runtime.h" |
jeffhao | c393a4f | 2011-10-19 13:46:09 -0700 | [diff] [blame^] | 26 | #include "space.h" |
| 27 | #include "zip_archive.h" |
Brian Carlstrom | 03a20ba | 2011-10-13 10:24:13 -0700 | [diff] [blame] | 28 | #include "toStringArray.h" |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 29 | #include "ScopedUtfChars.h" |
| 30 | |
| 31 | #include "JniConstants.h" // Last to avoid problems with LOG redefinition. |
| 32 | |
| 33 | namespace art { |
| 34 | |
| 35 | namespace { |
| 36 | |
| 37 | // A smart pointer that provides read-only access to a Java string's UTF chars. |
| 38 | // Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if |
| 39 | // passed a null jstring. The correct idiom is: |
| 40 | // |
| 41 | // NullableScopedUtfChars name(env, javaName); |
Brian Carlstrom | c252c3e | 2011-10-16 23:21:02 -0700 | [diff] [blame] | 42 | // if (env->ExceptionCheck()) { |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 43 | // return NULL; |
| 44 | // } |
| 45 | // // ... use name.c_str() |
| 46 | // |
| 47 | // TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option. |
| 48 | class NullableScopedUtfChars { |
| 49 | public: |
| 50 | NullableScopedUtfChars(JNIEnv* env, jstring s) |
| 51 | : mEnv(env), mString(s) |
| 52 | { |
| 53 | mUtfChars = (s != NULL) ? env->GetStringUTFChars(s, NULL) : NULL; |
| 54 | } |
| 55 | |
| 56 | ~NullableScopedUtfChars() { |
| 57 | if (mUtfChars) { |
| 58 | mEnv->ReleaseStringUTFChars(mString, mUtfChars); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | const char* c_str() const { |
| 63 | return mUtfChars; |
| 64 | } |
| 65 | |
| 66 | size_t size() const { |
| 67 | return strlen(mUtfChars); |
| 68 | } |
| 69 | |
| 70 | // Element access. |
| 71 | const char& operator[](size_t n) const { |
| 72 | return mUtfChars[n]; |
| 73 | } |
| 74 | |
| 75 | private: |
| 76 | JNIEnv* mEnv; |
| 77 | jstring mString; |
| 78 | const char* mUtfChars; |
| 79 | |
| 80 | // Disallow copy and assignment. |
| 81 | NullableScopedUtfChars(const NullableScopedUtfChars&); |
| 82 | void operator=(const NullableScopedUtfChars&); |
| 83 | }; |
| 84 | |
jeffhao | c393a4f | 2011-10-19 13:46:09 -0700 | [diff] [blame^] | 85 | static jint DexFile_openDexFile(JNIEnv* env, jclass, jstring javaSourceName, jstring javaOutputName, jint) { |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 86 | ScopedUtfChars sourceName(env, javaSourceName); |
| 87 | if (sourceName.c_str() == NULL) { |
| 88 | return 0; |
| 89 | } |
| 90 | NullableScopedUtfChars outputName(env, javaOutputName); |
Brian Carlstrom | c252c3e | 2011-10-16 23:21:02 -0700 | [diff] [blame] | 91 | if (env->ExceptionCheck()) { |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 92 | return 0; |
| 93 | } |
Brian Carlstrom | c252c3e | 2011-10-16 23:21:02 -0700 | [diff] [blame] | 94 | if (outputName.c_str() != NULL) { |
jeffhao | c393a4f | 2011-10-19 13:46:09 -0700 | [diff] [blame^] | 95 | // Check that output name ends in .dex. |
| 96 | if (!StringPiece(outputName.c_str()).ends_with(".dex")) { |
| 97 | LOG(ERROR) << "Output filename '" << outputName.c_str() << "' does not end with .dex"; |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | // Extract the dex file. |
| 102 | UniquePtr<ZipArchive> zip_archive(ZipArchive::Open(sourceName.c_str())); |
| 103 | if (zip_archive.get() == NULL) { |
| 104 | LOG(ERROR) << "Failed to open " << sourceName.c_str() << " when looking for classes.dex"; |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | UniquePtr<ZipEntry> zip_entry(zip_archive->Find(DexFile::kClassesDex)); |
| 109 | if (zip_entry.get() == NULL) { |
| 110 | LOG(ERROR) << "Failed to find classes.dex within " << sourceName.c_str(); |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | UniquePtr<File> file(OS::OpenFile(outputName.c_str(), true)); |
| 115 | bool success = zip_entry->Extract(*file); |
| 116 | if (!success) { |
| 117 | LOG(ERROR) << "Failed to extract classes.dex from " << sourceName.c_str(); |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | // Fork and exec dex2oat. |
| 122 | pid_t pid = fork(); |
| 123 | if (pid == 0) { |
| 124 | std::string boot_image_option("--boot-image="); |
| 125 | boot_image_option += Heap::GetSpaces()[0]->GetImageFilename(); |
| 126 | |
| 127 | std::string dex_file_option("--dex-file="); |
| 128 | dex_file_option += outputName.c_str(); |
| 129 | |
| 130 | std::string oat_file_option("--oat="); |
| 131 | oat_file_option += outputName.c_str(); |
| 132 | oat_file_option.erase(oat_file_option.size() - 3); |
| 133 | oat_file_option += "oat"; |
| 134 | |
| 135 | execl("/system/bin/dex2oatd", |
| 136 | "/system/bin/dex2oatd", |
| 137 | "-Xms64m", |
| 138 | "-Xmx64m", |
| 139 | boot_image_option.c_str(), |
| 140 | dex_file_option.c_str(), |
| 141 | oat_file_option.c_str(), |
| 142 | NULL); |
| 143 | |
| 144 | PLOG(FATAL) << "execl(dex2oatd) failed"; |
| 145 | return 0; |
| 146 | } else { |
| 147 | // Wait for dex2oat to finish. |
| 148 | int status; |
| 149 | pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0)); |
| 150 | if (got_pid != pid) { |
| 151 | PLOG(ERROR) << "waitpid failed: wanted " << pid << ", got " << got_pid; |
| 152 | return 0; |
| 153 | } |
| 154 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 155 | LOG(ERROR) << "dex2oatd failed with dex-file=" << outputName.c_str(); |
| 156 | return 0; |
| 157 | } |
| 158 | } |
Brian Carlstrom | c252c3e | 2011-10-16 23:21:02 -0700 | [diff] [blame] | 159 | } |
jeffhao | c393a4f | 2011-10-19 13:46:09 -0700 | [diff] [blame^] | 160 | |
| 161 | const DexFile* dex_file; |
| 162 | if (outputName.c_str() == NULL) { |
| 163 | dex_file = DexFile::Open(sourceName.c_str(), ""); |
| 164 | } else { |
| 165 | dex_file = DexFile::Open(outputName.c_str(), ""); |
| 166 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 167 | if (dex_file == NULL) { |
| 168 | jniThrowExceptionFmt(env, "java/io/IOException", "unable to open DEX file: %s", |
| 169 | sourceName.c_str()); |
jeffhao | c393a4f | 2011-10-19 13:46:09 -0700 | [diff] [blame^] | 170 | return 0; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 171 | } |
| 172 | return static_cast<jint>(reinterpret_cast<uintptr_t>(dex_file)); |
| 173 | } |
| 174 | |
| 175 | static const DexFile* toDexFile(JNIEnv* env, int dex_file_address) { |
| 176 | const DexFile* dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(dex_file_address)); |
| 177 | if ((dex_file == NULL)) { |
| 178 | jniThrowNullPointerException(env, "dex_file == null"); |
| 179 | } |
| 180 | return dex_file; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void DexFile_closeDexFile(JNIEnv* env, jclass, jint cookie) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 184 | const DexFile* dex_file = toDexFile(env, cookie); |
| 185 | if (dex_file == NULL) { |
| 186 | return; |
| 187 | } |
| 188 | if (Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) { |
| 189 | return; |
| 190 | } |
| 191 | delete dex_file; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | jclass DexFile_defineClass(JNIEnv* env, jclass, jstring javaName, jobject javaLoader, jint cookie) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 195 | const DexFile* dex_file = toDexFile(env, cookie); |
| 196 | if (dex_file == NULL) { |
| 197 | return NULL; |
| 198 | } |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 199 | ScopedUtfChars class_name(env, javaName); |
| 200 | if (class_name.c_str() == NULL) { |
| 201 | return NULL; |
| 202 | } |
| 203 | const std::string descriptor = DotToDescriptor(class_name.c_str()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 204 | const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor); |
| 205 | if (dex_class_def == NULL) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 206 | return NULL; |
| 207 | } |
| 208 | |
| 209 | Object* class_loader_object = Decode<Object*>(env, javaLoader); |
| 210 | ClassLoader* class_loader = down_cast<ClassLoader*>(class_loader_object); |
| 211 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 212 | class_linker->RegisterDexFile(*dex_file); |
| 213 | 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] | 214 | if (env->ExceptionCheck()) { |
| 215 | env->ExceptionClear(); |
| 216 | return NULL; |
| 217 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 218 | return AddLocalReference<jclass>(env, result); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jint cookie) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 222 | const DexFile* dex_file = toDexFile(env, cookie); |
| 223 | if (dex_file == NULL) { |
| 224 | return NULL; |
| 225 | } |
Brian Carlstrom | 03a20ba | 2011-10-13 10:24:13 -0700 | [diff] [blame] | 226 | |
| 227 | std::vector<std::string> class_names; |
| 228 | for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) { |
| 229 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(i); |
| 230 | const char* descriptor = dex_file->GetClassDescriptor(class_def); |
| 231 | class_names.push_back(DescriptorToDot(descriptor)); |
| 232 | } |
| 233 | return toStringArray(env, class_names); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) { |
Brian Carlstrom | 03a20ba | 2011-10-13 10:24:13 -0700 | [diff] [blame] | 237 | ScopedUtfChars filename(env, javaFilename); |
| 238 | if (filename.c_str() == NULL) { |
Brian Carlstrom | 1d9f52b | 2011-10-13 10:50:45 -0700 | [diff] [blame] | 239 | return JNI_TRUE; |
Brian Carlstrom | 03a20ba | 2011-10-13 10:24:13 -0700 | [diff] [blame] | 240 | } |
Brian Carlstrom | 1d9f52b | 2011-10-13 10:50:45 -0700 | [diff] [blame] | 241 | |
| 242 | if (!OS::FileExists(filename.c_str())) { |
| 243 | jniThrowExceptionFmt(env, "java/io/FileNotFoundException", "%s", filename.c_str()); |
| 244 | return JNI_TRUE; |
| 245 | } |
| 246 | |
| 247 | // Always treat elements of the bootclasspath as up-to-date. The |
| 248 | // fact that code is running at all means that this should be true. |
| 249 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 250 | const std::vector<const DexFile*>& boot_class_path = class_linker->GetBootClassPath(); |
| 251 | for (size_t i = 0; i < boot_class_path.size(); i++) { |
| 252 | if (boot_class_path[i]->GetLocation() == filename.c_str()) { |
| 253 | return JNI_FALSE; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | UniquePtr<const DexFile> dex_file(DexFile::Open(filename.c_str(), "")); |
| 258 | if (dex_file.get() == NULL) { |
| 259 | return JNI_TRUE; |
| 260 | } |
| 261 | |
Brian Carlstrom | fad7143 | 2011-10-16 20:25:10 -0700 | [diff] [blame] | 262 | const OatFile* oat_file = class_linker->FindOatFile(*dex_file.get()); |
| 263 | if (oat_file == NULL) { |
Brian Carlstrom | 1d9f52b | 2011-10-13 10:50:45 -0700 | [diff] [blame] | 264 | return JNI_TRUE; |
| 265 | } |
Brian Carlstrom | fad7143 | 2011-10-16 20:25:10 -0700 | [diff] [blame] | 266 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file->GetLocation()); |
| 267 | if (oat_dex_file == NULL) { |
| 268 | return JNI_TRUE; |
| 269 | } |
| 270 | if (oat_dex_file->GetDexFileChecksum() != dex_file->GetHeader().checksum_) { |
| 271 | return JNI_TRUE; |
| 272 | } |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 273 | return JNI_FALSE; |
| 274 | } |
| 275 | |
| 276 | static JNINativeMethod gMethods[] = { |
| 277 | NATIVE_METHOD(DexFile, closeDexFile, "(I)V"), |
| 278 | NATIVE_METHOD(DexFile, defineClass, "(Ljava/lang/String;Ljava/lang/ClassLoader;I)Ljava/lang/Class;"), |
| 279 | NATIVE_METHOD(DexFile, getClassNameList, "(I)[Ljava/lang/String;"), |
| 280 | NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"), |
| 281 | NATIVE_METHOD(DexFile, openDexFile, "(Ljava/lang/String;Ljava/lang/String;I)I"), |
| 282 | }; |
| 283 | |
| 284 | } // namespace |
| 285 | |
| 286 | void register_dalvik_system_DexFile(JNIEnv* env) { |
| 287 | jniRegisterNativeMethods(env, "dalvik/system/DexFile", gMethods, NELEM(gMethods)); |
| 288 | } |
| 289 | |
| 290 | } // namespace art |