blob: e6d45e3513364df451ee24f2649b17933f9eb53a [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);
39// if (env->ExceptionOccurred()) {
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.
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);
88 if (env->ExceptionOccurred()) {
89 return 0;
90 }
Brian Carlstromaded5f72011-10-07 17:15:04 -070091 const DexFile* dex_file = DexFile::Open(sourceName.c_str(), "");
92 if (dex_file == NULL) {
93 jniThrowExceptionFmt(env, "java/io/IOException", "unable to open DEX file: %s",
94 sourceName.c_str());
95 return NULL;
96 }
97 return static_cast<jint>(reinterpret_cast<uintptr_t>(dex_file));
98}
99
100static const DexFile* toDexFile(JNIEnv* env, int dex_file_address) {
101 const DexFile* dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(dex_file_address));
102 if ((dex_file == NULL)) {
103 jniThrowNullPointerException(env, "dex_file == null");
104 }
105 return dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700106}
107
108void DexFile_closeDexFile(JNIEnv* env, jclass, jint cookie) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700109 const DexFile* dex_file = toDexFile(env, cookie);
110 if (dex_file == NULL) {
111 return;
112 }
113 if (Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) {
114 return;
115 }
116 delete dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700117}
118
119jclass DexFile_defineClass(JNIEnv* env, jclass, jstring javaName, jobject javaLoader, jint cookie) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700120 const DexFile* dex_file = toDexFile(env, cookie);
121 if (dex_file == NULL) {
122 return NULL;
123 }
Brian Carlstromdf143242011-10-10 18:05:34 -0700124 ScopedUtfChars class_name(env, javaName);
125 if (class_name.c_str() == NULL) {
126 return NULL;
127 }
128 const std::string descriptor = DotToDescriptor(class_name.c_str());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700129 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
130 if (dex_class_def == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700131 return NULL;
132 }
133
134 Object* class_loader_object = Decode<Object*>(env, javaLoader);
135 ClassLoader* class_loader = down_cast<ClassLoader*>(class_loader_object);
136 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
137 class_linker->RegisterDexFile(*dex_file);
138 Class* result = class_linker->DefineClass(descriptor, class_loader, *dex_file, *dex_class_def);
Brian Carlstrom41ec4772011-10-11 01:35:58 -0700139 if (env->ExceptionCheck()) {
140 env->ExceptionClear();
141 return NULL;
142 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700143 return AddLocalReference<jclass>(env, result);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700144}
145
146jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jint cookie) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700147 const DexFile* dex_file = toDexFile(env, cookie);
148 if (dex_file == NULL) {
149 return NULL;
150 }
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700151
152 std::vector<std::string> class_names;
153 for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
154 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
155 const char* descriptor = dex_file->GetClassDescriptor(class_def);
156 class_names.push_back(DescriptorToDot(descriptor));
157 }
158 return toStringArray(env, class_names);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700159}
160
161jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700162 ScopedUtfChars filename(env, javaFilename);
163 if (filename.c_str() == NULL) {
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700164 return JNI_TRUE;
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700165 }
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700166
167 if (!OS::FileExists(filename.c_str())) {
168 jniThrowExceptionFmt(env, "java/io/FileNotFoundException", "%s", filename.c_str());
169 return JNI_TRUE;
170 }
171
172 // Always treat elements of the bootclasspath as up-to-date. The
173 // fact that code is running at all means that this should be true.
174 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
175 const std::vector<const DexFile*>& boot_class_path = class_linker->GetBootClassPath();
176 for (size_t i = 0; i < boot_class_path.size(); i++) {
177 if (boot_class_path[i]->GetLocation() == filename.c_str()) {
178 return JNI_FALSE;
179 }
180 }
181
182 UniquePtr<const DexFile> dex_file(DexFile::Open(filename.c_str(), ""));
183 if (dex_file.get() == NULL) {
184 return JNI_TRUE;
185 }
186
187 UniquePtr<const OatFile> oat_file(class_linker->FindOatFile(*dex_file.get()));
188 if (oat_file.get() == NULL) {
189 return JNI_TRUE;
190 }
191
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700192 return JNI_FALSE;
193}
194
195static JNINativeMethod gMethods[] = {
196 NATIVE_METHOD(DexFile, closeDexFile, "(I)V"),
197 NATIVE_METHOD(DexFile, defineClass, "(Ljava/lang/String;Ljava/lang/ClassLoader;I)Ljava/lang/Class;"),
198 NATIVE_METHOD(DexFile, getClassNameList, "(I)[Ljava/lang/String;"),
199 NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
200 NATIVE_METHOD(DexFile, openDexFile, "(Ljava/lang/String;Ljava/lang/String;I)I"),
201};
202
203} // namespace
204
205void register_dalvik_system_DexFile(JNIEnv* env) {
206 jniRegisterNativeMethods(env, "dalvik/system/DexFile", gMethods, NELEM(gMethods));
207}
208
209} // namespace art