blob: e07339cbb62801068c2f3fbfd5544b64f7a7a1a6 [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
Elliott Hughes07ed66b2012-12-12 18:34:25 -080019#include "base/logging.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070020#include "class_linker.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080021#include "common_throws.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070022#include "dex_file-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080023#include "gc/space.h"
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070024#include "image.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070025#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "mirror/class_loader.h"
Ian Rogers05f30572013-02-20 12:13:11 -080027#include "mirror/object-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "mirror/string.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080029#include "oat.h"
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -070030#include "os.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070031#include "runtime.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070032#include "scoped_thread_state_change.h"
Ian Rogersc9818482012-01-11 08:52:51 -080033#include "ScopedLocalRef.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070034#include "ScopedUtfChars.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070035#include "toStringArray.h"
36#include "zip_archive.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070037
38namespace art {
39
Brian Carlstromf91c8c32011-09-21 17:30:34 -070040// A smart pointer that provides read-only access to a Java string's UTF chars.
41// Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if
42// passed a null jstring. The correct idiom is:
43//
44// NullableScopedUtfChars name(env, javaName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -070045// if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070046// return NULL;
47// }
48// // ... use name.c_str()
49//
50// TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option.
51class NullableScopedUtfChars {
Elliott Hughesba8eee12012-01-24 20:25:24 -080052 public:
53 NullableScopedUtfChars(JNIEnv* env, jstring s) : mEnv(env), mString(s) {
54 mUtfChars = (s != NULL) ? env->GetStringUTFChars(s, NULL) : NULL;
55 }
56
57 ~NullableScopedUtfChars() {
58 if (mUtfChars) {
59 mEnv->ReleaseStringUTFChars(mString, mUtfChars);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070060 }
Elliott Hughesba8eee12012-01-24 20:25:24 -080061 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070062
Elliott Hughesba8eee12012-01-24 20:25:24 -080063 const char* c_str() const {
64 return mUtfChars;
65 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070066
Elliott Hughesba8eee12012-01-24 20:25:24 -080067 size_t size() const {
68 return strlen(mUtfChars);
69 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070070
Elliott Hughesba8eee12012-01-24 20:25:24 -080071 // Element access.
72 const char& operator[](size_t n) const {
73 return mUtfChars[n];
74 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070075
Elliott Hughesba8eee12012-01-24 20:25:24 -080076 private:
77 JNIEnv* mEnv;
78 jstring mString;
79 const char* mUtfChars;
Brian Carlstromf91c8c32011-09-21 17:30:34 -070080
Elliott Hughesba8eee12012-01-24 20:25:24 -080081 // Disallow copy and assignment.
82 NullableScopedUtfChars(const NullableScopedUtfChars&);
83 void operator=(const NullableScopedUtfChars&);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070084};
85
jeffhaoc393a4f2011-10-19 13:46:09 -070086static jint DexFile_openDexFile(JNIEnv* env, jclass, jstring javaSourceName, jstring javaOutputName, jint) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070087 ScopedUtfChars sourceName(env, javaSourceName);
88 if (sourceName.c_str() == NULL) {
89 return 0;
90 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -080091 std::string source(sourceName.c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -070092 NullableScopedUtfChars outputName(env, javaOutputName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -070093 if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070094 return 0;
95 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070096 ScopedObjectAccess soa(env);
jeffhaoc393a4f2011-10-19 13:46:09 -070097 const DexFile* dex_file;
98 if (outputName.c_str() == NULL) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -080099 dex_file = Runtime::Current()->GetClassLinker()->FindDexFileInOatFileFromDexLocation(source);
jeffhaoc393a4f2011-10-19 13:46:09 -0700100 } else {
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800101 std::string output(outputName.c_str());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700102 dex_file =
103 Runtime::Current()->GetClassLinker()->FindOrCreateOatFileForDexLocation(source, output);
jeffhaoc393a4f2011-10-19 13:46:09 -0700104 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700105 if (dex_file == NULL) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800106 LOG(WARNING) << "Failed to open dex file: " << source;
Ian Rogers62d6c772013-02-27 08:32:07 -0800107 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
108 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/io/IOException;",
109 "Unable to open dex file: %s", source.c_str());
jeffhaoc393a4f2011-10-19 13:46:09 -0700110 return 0;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700111 }
112 return static_cast<jint>(reinterpret_cast<uintptr_t>(dex_file));
113}
114
Ian Rogers62d6c772013-02-27 08:32:07 -0800115static const DexFile* toDexFile(int dex_file_address) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700116 const DexFile* dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(dex_file_address));
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800117 if (dex_file == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800118 ThrowNullPointerException(NULL, "dex_file == null");
Brian Carlstromaded5f72011-10-07 17:15:04 -0700119 }
120 return dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700121}
122
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700123static void DexFile_closeDexFile(JNIEnv* env, jclass, jint cookie) {
124 const DexFile* dex_file;
125 {
126 ScopedObjectAccess soa(env);
127 dex_file = toDexFile(cookie);
128 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700129 if (dex_file == NULL) {
130 return;
131 }
132 if (Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) {
133 return;
134 }
135 delete dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700136}
137
Elliott Hughes0512f022012-03-15 22:10:52 -0700138static jclass DexFile_defineClassNative(JNIEnv* env, jclass, jstring javaName, jobject javaLoader,
139 jint cookie) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700140 ScopedObjectAccess soa(env);
Elliott Hugheseac76672012-05-24 21:56:51 -0700141 const DexFile* dex_file = toDexFile(cookie);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700142 if (dex_file == NULL) {
143 return NULL;
144 }
Brian Carlstromdf143242011-10-10 18:05:34 -0700145 ScopedUtfChars class_name(env, javaName);
146 if (class_name.c_str() == NULL) {
147 return NULL;
148 }
Elliott Hughes95572412011-12-13 18:14:20 -0800149 const std::string descriptor(DotToDescriptor(class_name.c_str()));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700150 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
151 if (dex_class_def == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700152 return NULL;
153 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700154 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
155 class_linker->RegisterDexFile(*dex_file);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800156 mirror::ClassLoader* class_loader = soa.Decode<mirror::ClassLoader*>(javaLoader);
157 mirror::Class* result = class_linker->DefineClass(descriptor, class_loader, *dex_file, *dex_class_def);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700158 return soa.AddLocalReference<jclass>(result);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700159}
160
Elliott Hughes0512f022012-03-15 22:10:52 -0700161static jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jint cookie) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700162 const DexFile* dex_file;
163 {
164 ScopedObjectAccess soa(env);
165 dex_file = toDexFile(cookie);
166 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700167 if (dex_file == NULL) {
168 return NULL;
169 }
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700170
171 std::vector<std::string> class_names;
172 for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
173 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
174 const char* descriptor = dex_file->GetClassDescriptor(class_def);
175 class_names.push_back(DescriptorToDot(descriptor));
176 }
177 return toStringArray(env, class_names);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700178}
179
Elliott Hughes0512f022012-03-15 22:10:52 -0700180static jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800181 bool debug_logging = false;
182
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700183 ScopedUtfChars filename(env, javaFilename);
184 if (filename.c_str() == NULL) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800185 LOG(ERROR) << "DexFile_isDexOptNeeded null filename";
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700186 return JNI_TRUE;
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700187 }
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700188
189 if (!OS::FileExists(filename.c_str())) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800190 LOG(ERROR) << "DexFile_isDexOptNeeded file '" << filename.c_str() << "' does not exist";
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700191 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800192 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
193 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/io/FileNotFoundException;",
194 "%s", filename.c_str());
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700195 return JNI_TRUE;
196 }
197
198 // Always treat elements of the bootclasspath as up-to-date. The
199 // fact that code is running at all means that this should be true.
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700200 Runtime* runtime = Runtime::Current();
201 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700202 const std::vector<const DexFile*>& boot_class_path = class_linker->GetBootClassPath();
203 for (size_t i = 0; i < boot_class_path.size(); i++) {
204 if (boot_class_path[i]->GetLocation() == filename.c_str()) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800205 if (debug_logging) {
206 LOG(INFO) << "DexFile_isDexOptNeeded ignoring boot class path file: " << filename.c_str();
207 }
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700208 return JNI_FALSE;
209 }
210 }
211
Brian Carlstrom30e2ea42013-06-19 23:25:37 -0700212 // Check if we have an odex file next to the dex file.
213 std::string odex_filename(OatFile::DexFilenameToOdexFilename(filename.c_str()));
214 UniquePtr<const OatFile> oat_file(OatFile::Open(odex_filename, odex_filename, NULL));
Ian Rogers33e95662013-05-20 20:29:14 -0700215 if (oat_file.get() != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700216 ScopedObjectAccess soa(env);
Ian Rogers33e95662013-05-20 20:29:14 -0700217 const art::OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(filename.c_str());
218 if (oat_dex_file == NULL) {
Brian Carlstromafe25512012-06-27 17:02:58 -0700219 if (debug_logging) {
Ian Rogers33e95662013-05-20 20:29:14 -0700220 LOG(INFO) << "DexFile_isDexOptNeeded GetOatDexFile failed";
Brian Carlstromafe25512012-06-27 17:02:58 -0700221 }
Ian Rogers33e95662013-05-20 20:29:14 -0700222 } else {
223 uint32_t location_checksum;
224 // If we have no classes.dex checksum such as in a user build, assume up-to-date.
225 if (!DexFile::GetChecksum(filename.c_str(), location_checksum)) {
226 if (debug_logging) {
227 LOG(INFO) << "DexFile_isDexOptNeeded ignoring precompiled stripped file: "
228 << filename.c_str();
229 }
230 return JNI_FALSE;
231 }
232 if (ClassLinker::VerifyOatFileChecksums(oat_file.get(), filename.c_str(), location_checksum)) {
233 if (debug_logging) {
Brian Carlstrom30e2ea42013-06-19 23:25:37 -0700234 LOG(INFO) << "DexFile_isDexOptNeeded precompiled file " << odex_filename
Ian Rogers33e95662013-05-20 20:29:14 -0700235 << " is up-to-date checksum compared to " << filename.c_str();
236 }
237 return JNI_FALSE;
238 }
Brian Carlstromafe25512012-06-27 17:02:58 -0700239 }
Brian Carlstromfad71432011-10-16 20:25:10 -0700240 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800241
Brian Carlstroma004aa92012-02-08 18:05:09 -0800242 // Check if we have an oat file in the cache
Brian Carlstrom30e2ea42013-06-19 23:25:37 -0700243 std::string cache_location(GetDalvikCacheFilenameOrDie(filename.c_str()));
244 oat_file.reset(OatFile::Open(cache_location, filename.c_str(), NULL));
Brian Carlstrom58cbbc22012-03-29 17:52:00 -0700245 if (oat_file.get() == NULL) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800246 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
247 << " does not exist for " << filename.c_str();
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800248 return JNI_TRUE;
249 }
250
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700251 Heap* heap = runtime->GetHeap();
252 const Spaces& spaces = heap->GetSpaces();
253 // TODO: C++0x auto
254 for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) {
255 if ((*cur)->IsImageSpace()) {
256 // TODO: Ensure this works with multiple image spaces.
257 const ImageHeader& image_header = (*cur)->AsImageSpace()->GetImageHeader();
Brian Carlstrom28db0122012-10-18 16:20:41 -0700258 if (oat_file->GetOatHeader().GetImageFileLocationOatChecksum() != image_header.GetOatChecksum()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700259 ScopedObjectAccess soa(env);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700260 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
Brian Carlstrom28db0122012-10-18 16:20:41 -0700261 << " has out-of-date oat checksum compared to "
262 << image_header.GetImageRoot(ImageHeader::kOatLocation)->AsString()->ToModifiedUtf8();
263 return JNI_TRUE;
264 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800265 if (oat_file->GetOatHeader().GetImageFileLocationOatDataBegin()
266 != reinterpret_cast<uint32_t>(image_header.GetOatDataBegin())) {
Brian Carlstrom28db0122012-10-18 16:20:41 -0700267 ScopedObjectAccess soa(env);
268 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
269 << " has out-of-date oat begin compared to "
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700270 << image_header.GetImageRoot(ImageHeader::kOatLocation)->AsString()->ToModifiedUtf8();
271 return JNI_TRUE;
272 }
273 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700274 }
275
Ian Rogers33e95662013-05-20 20:29:14 -0700276 ScopedObjectAccess soa(env);
Brian Carlstroma004aa92012-02-08 18:05:09 -0800277 uint32_t location_checksum;
278 if (!DexFile::GetChecksum(filename.c_str(), location_checksum)) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800279 LOG(ERROR) << "DexFile_isDexOptNeeded failed to compute checksum of " << filename.c_str();
Brian Carlstroma004aa92012-02-08 18:05:09 -0800280 return JNI_TRUE;
281 }
282
Brian Carlstromafe25512012-06-27 17:02:58 -0700283 if (!ClassLinker::VerifyOatFileChecksums(oat_file.get(), filename.c_str(), location_checksum)) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800284 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700285 << " has out-of-date checksum compared to " << filename.c_str();
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800286 return JNI_TRUE;
287 }
288
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800289 if (debug_logging) {
290 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
291 << " is up-to-date for " << filename.c_str();
292 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700293 return JNI_FALSE;
294}
295
296static JNINativeMethod gMethods[] = {
297 NATIVE_METHOD(DexFile, closeDexFile, "(I)V"),
Ian Rogers66a556f2012-02-14 00:05:38 -0800298 NATIVE_METHOD(DexFile, defineClassNative, "(Ljava/lang/String;Ljava/lang/ClassLoader;I)Ljava/lang/Class;"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700299 NATIVE_METHOD(DexFile, getClassNameList, "(I)[Ljava/lang/String;"),
300 NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
301 NATIVE_METHOD(DexFile, openDexFile, "(Ljava/lang/String;Ljava/lang/String;I)I"),
302};
303
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700304void register_dalvik_system_DexFile(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700305 REGISTER_NATIVE_METHODS("dalvik/system/DexFile");
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700306}
307
308} // namespace art