blob: bb486e2705aaccf9dccf8ec3c83da845a7c7d265 [file] [log] [blame]
Brian Carlstromf91c8c32011-09-21 17:30:34 -07001/*
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 Carlstrom1d9f52b2011-10-13 10:50:45 -070017#include <unistd.h>
18
Brian Carlstromaded5f72011-10-07 17:15:04 -070019#include "class_loader.h"
20#include "class_linker.h"
21#include "dex_file.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070022#include "logging.h"
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -070023#include "os.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070024#include "runtime.h"
Brian Carlstrom03a20ba2011-10-13 10:24:13 -070025#include "toStringArray.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070026#include "ScopedUtfChars.h"
27
28#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
29
30namespace art {
31
32namespace {
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 Carlstromc252c3e2011-10-16 23:21:02 -070039// if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070040// return NULL;
41// }
42// // ... use name.c_str()
43//
44// TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option.
45class NullableScopedUtfChars {
46public:
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
72private:
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) {
83 ScopedUtfChars sourceName(env, javaSourceName);
84 if (sourceName.c_str() == NULL) {
85 return 0;
86 }
87 NullableScopedUtfChars outputName(env, javaOutputName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -070088 if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070089 return 0;
90 }
Brian Carlstromc252c3e2011-10-16 23:21:02 -070091 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 Carlstromaded5f72011-10-07 17:15:04 -070095 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
104static 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 Carlstromf91c8c32011-09-21 17:30:34 -0700110}
111
112void DexFile_closeDexFile(JNIEnv* env, jclass, jint cookie) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700113 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 Carlstromf91c8c32011-09-21 17:30:34 -0700121}
122
123jclass DexFile_defineClass(JNIEnv* env, jclass, jstring javaName, jobject javaLoader, jint cookie) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700124 const DexFile* dex_file = toDexFile(env, cookie);
125 if (dex_file == NULL) {
126 return NULL;
127 }
Brian Carlstromdf143242011-10-10 18:05:34 -0700128 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 Carlstromaded5f72011-10-07 17:15:04 -0700133 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
134 if (dex_class_def == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700135 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 Carlstrom41ec4772011-10-11 01:35:58 -0700143 if (env->ExceptionCheck()) {
144 env->ExceptionClear();
145 return NULL;
146 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700147 return AddLocalReference<jclass>(env, result);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700148}
149
150jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jint cookie) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700151 const DexFile* dex_file = toDexFile(env, cookie);
152 if (dex_file == NULL) {
153 return NULL;
154 }
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700155
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 Carlstromf91c8c32011-09-21 17:30:34 -0700163}
164
165jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700166 ScopedUtfChars filename(env, javaFilename);
167 if (filename.c_str() == NULL) {
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700168 return JNI_TRUE;
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700169 }
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700170
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 Carlstromfad71432011-10-16 20:25:10 -0700191 const OatFile* oat_file = class_linker->FindOatFile(*dex_file.get());
192 if (oat_file == NULL) {
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700193 return JNI_TRUE;
194 }
Brian Carlstromfad71432011-10-16 20:25:10 -0700195 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 Carlstromf91c8c32011-09-21 17:30:34 -0700202 return JNI_FALSE;
203}
204
205static 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
215void register_dalvik_system_DexFile(JNIEnv* env) {
216 jniRegisterNativeMethods(env, "dalvik/system/DexFile", gMethods, NELEM(gMethods));
217}
218
219} // namespace art