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" |
Brian Carlstrom | 03a20ba | 2011-10-13 10:24:13 -0700 | [diff] [blame] | 25 | #include "toStringArray.h" |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 26 | #include "ScopedUtfChars.h" |
| 27 | |
| 28 | #include "JniConstants.h" // Last to avoid problems with LOG redefinition. |
| 29 | |
| 30 | namespace art { |
| 31 | |
| 32 | namespace { |
| 33 | |
| 34 | // A smart pointer that provides read-only access to a Java string's UTF chars. |
| 35 | // Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if |
| 36 | // passed a null jstring. The correct idiom is: |
| 37 | // |
| 38 | // NullableScopedUtfChars name(env, javaName); |
Brian Carlstrom | c252c3e | 2011-10-16 23:21:02 -0700 | [diff] [blame] | 39 | // if (env->ExceptionCheck()) { |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 40 | // return NULL; |
| 41 | // } |
| 42 | // // ... use name.c_str() |
| 43 | // |
| 44 | // TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option. |
| 45 | class NullableScopedUtfChars { |
| 46 | public: |
| 47 | NullableScopedUtfChars(JNIEnv* env, jstring s) |
| 48 | : mEnv(env), mString(s) |
| 49 | { |
| 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 | |
| 82 | static jint DexFile_openDexFile(JNIEnv* env, jclass, jstring javaSourceName, jstring javaOutputName) { |
| 83 | ScopedUtfChars sourceName(env, javaSourceName); |
| 84 | if (sourceName.c_str() == NULL) { |
| 85 | return 0; |
| 86 | } |
| 87 | NullableScopedUtfChars outputName(env, javaOutputName); |
Brian Carlstrom | c252c3e | 2011-10-16 23:21:02 -0700 | [diff] [blame] | 88 | if (env->ExceptionCheck()) { |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 89 | return 0; |
| 90 | } |
Brian Carlstrom | c252c3e | 2011-10-16 23:21:02 -0700 | [diff] [blame] | 91 | if (outputName.c_str() != NULL) { |
| 92 | // TODO: extract dex and run dex2oat like oatopt |
| 93 | UNIMPLEMENTED(FATAL) << sourceName.c_str() << " " << outputName.c_str(); |
| 94 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 95 | const DexFile* dex_file = DexFile::Open(sourceName.c_str(), ""); |
| 96 | if (dex_file == NULL) { |
| 97 | jniThrowExceptionFmt(env, "java/io/IOException", "unable to open DEX file: %s", |
| 98 | sourceName.c_str()); |
| 99 | return NULL; |
| 100 | } |
| 101 | return static_cast<jint>(reinterpret_cast<uintptr_t>(dex_file)); |
| 102 | } |
| 103 | |
| 104 | static const DexFile* toDexFile(JNIEnv* env, int dex_file_address) { |
| 105 | const DexFile* dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(dex_file_address)); |
| 106 | if ((dex_file == NULL)) { |
| 107 | jniThrowNullPointerException(env, "dex_file == null"); |
| 108 | } |
| 109 | return dex_file; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void DexFile_closeDexFile(JNIEnv* env, jclass, jint cookie) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 113 | const DexFile* dex_file = toDexFile(env, cookie); |
| 114 | if (dex_file == NULL) { |
| 115 | return; |
| 116 | } |
| 117 | if (Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) { |
| 118 | return; |
| 119 | } |
| 120 | delete dex_file; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | jclass DexFile_defineClass(JNIEnv* env, jclass, jstring javaName, jobject javaLoader, jint cookie) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 124 | const DexFile* dex_file = toDexFile(env, cookie); |
| 125 | if (dex_file == NULL) { |
| 126 | return NULL; |
| 127 | } |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 128 | ScopedUtfChars class_name(env, javaName); |
| 129 | if (class_name.c_str() == NULL) { |
| 130 | return NULL; |
| 131 | } |
| 132 | const std::string descriptor = DotToDescriptor(class_name.c_str()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 133 | const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor); |
| 134 | if (dex_class_def == NULL) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 135 | return NULL; |
| 136 | } |
| 137 | |
| 138 | Object* class_loader_object = Decode<Object*>(env, javaLoader); |
| 139 | ClassLoader* class_loader = down_cast<ClassLoader*>(class_loader_object); |
| 140 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 141 | class_linker->RegisterDexFile(*dex_file); |
| 142 | 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] | 143 | if (env->ExceptionCheck()) { |
| 144 | env->ExceptionClear(); |
| 145 | return NULL; |
| 146 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 147 | return AddLocalReference<jclass>(env, result); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jint cookie) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 151 | const DexFile* dex_file = toDexFile(env, cookie); |
| 152 | if (dex_file == NULL) { |
| 153 | return NULL; |
| 154 | } |
Brian Carlstrom | 03a20ba | 2011-10-13 10:24:13 -0700 | [diff] [blame] | 155 | |
| 156 | std::vector<std::string> class_names; |
| 157 | for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) { |
| 158 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(i); |
| 159 | const char* descriptor = dex_file->GetClassDescriptor(class_def); |
| 160 | class_names.push_back(DescriptorToDot(descriptor)); |
| 161 | } |
| 162 | return toStringArray(env, class_names); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) { |
Brian Carlstrom | 03a20ba | 2011-10-13 10:24:13 -0700 | [diff] [blame] | 166 | ScopedUtfChars filename(env, javaFilename); |
| 167 | if (filename.c_str() == NULL) { |
Brian Carlstrom | 1d9f52b | 2011-10-13 10:50:45 -0700 | [diff] [blame] | 168 | return JNI_TRUE; |
Brian Carlstrom | 03a20ba | 2011-10-13 10:24:13 -0700 | [diff] [blame] | 169 | } |
Brian Carlstrom | 1d9f52b | 2011-10-13 10:50:45 -0700 | [diff] [blame] | 170 | |
| 171 | if (!OS::FileExists(filename.c_str())) { |
| 172 | jniThrowExceptionFmt(env, "java/io/FileNotFoundException", "%s", filename.c_str()); |
| 173 | return JNI_TRUE; |
| 174 | } |
| 175 | |
| 176 | // Always treat elements of the bootclasspath as up-to-date. The |
| 177 | // fact that code is running at all means that this should be true. |
| 178 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 179 | const std::vector<const DexFile*>& boot_class_path = class_linker->GetBootClassPath(); |
| 180 | for (size_t i = 0; i < boot_class_path.size(); i++) { |
| 181 | if (boot_class_path[i]->GetLocation() == filename.c_str()) { |
| 182 | return JNI_FALSE; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | UniquePtr<const DexFile> dex_file(DexFile::Open(filename.c_str(), "")); |
| 187 | if (dex_file.get() == NULL) { |
| 188 | return JNI_TRUE; |
| 189 | } |
| 190 | |
Brian Carlstrom | fad7143 | 2011-10-16 20:25:10 -0700 | [diff] [blame] | 191 | const OatFile* oat_file = class_linker->FindOatFile(*dex_file.get()); |
| 192 | if (oat_file == NULL) { |
Brian Carlstrom | 1d9f52b | 2011-10-13 10:50:45 -0700 | [diff] [blame] | 193 | return JNI_TRUE; |
| 194 | } |
Brian Carlstrom | fad7143 | 2011-10-16 20:25:10 -0700 | [diff] [blame] | 195 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file->GetLocation()); |
| 196 | if (oat_dex_file == NULL) { |
| 197 | return JNI_TRUE; |
| 198 | } |
| 199 | if (oat_dex_file->GetDexFileChecksum() != dex_file->GetHeader().checksum_) { |
| 200 | return JNI_TRUE; |
| 201 | } |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 202 | return JNI_FALSE; |
| 203 | } |
| 204 | |
| 205 | static JNINativeMethod gMethods[] = { |
| 206 | NATIVE_METHOD(DexFile, closeDexFile, "(I)V"), |
| 207 | NATIVE_METHOD(DexFile, defineClass, "(Ljava/lang/String;Ljava/lang/ClassLoader;I)Ljava/lang/Class;"), |
| 208 | NATIVE_METHOD(DexFile, getClassNameList, "(I)[Ljava/lang/String;"), |
| 209 | NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"), |
| 210 | NATIVE_METHOD(DexFile, openDexFile, "(Ljava/lang/String;Ljava/lang/String;I)I"), |
| 211 | }; |
| 212 | |
| 213 | } // namespace |
| 214 | |
| 215 | void register_dalvik_system_DexFile(JNIEnv* env) { |
| 216 | jniRegisterNativeMethods(env, "dalvik/system/DexFile", gMethods, NELEM(gMethods)); |
| 217 | } |
| 218 | |
| 219 | } // namespace art |