Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3 | #include "dex_file.h" |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 4 | |
| 5 | #include <fcntl.h> |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 6 | #include <limits.h> |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 7 | #include <stdio.h> |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 8 | #include <string.h> |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 9 | #include <sys/file.h> |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 10 | #include <sys/mman.h> |
| 11 | #include <sys/stat.h> |
| 12 | #include <sys/types.h> |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 13 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 14 | #include <map> |
| 15 | |
| 16 | #include "UniquePtr.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "globals.h" |
| 18 | #include "logging.h" |
| 19 | #include "object.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 20 | #include "os.h" |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 21 | #include "stringprintf.h" |
| 22 | #include "thread.h" |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 23 | #include "utils.h" |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 24 | #include "zip_archive.h" |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 28 | const byte DexFile::kDexMagic[] = { 'd', 'e', 'x', '\n' }; |
| 29 | const byte DexFile::kDexMagicVersion[] = { '0', '3', '5', '\0' }; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 30 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 31 | DexFile::ClassPathEntry DexFile::FindInClassPath(const StringPiece& descriptor, |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 32 | const ClassPath& class_path) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 33 | for (size_t i = 0; i != class_path.size(); ++i) { |
| 34 | const DexFile* dex_file = class_path[i]; |
| 35 | const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor); |
| 36 | if (dex_class_def != NULL) { |
| 37 | return ClassPathEntry(dex_file, dex_class_def); |
| 38 | } |
| 39 | } |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 40 | // TODO: remove reinterpret_cast when issue with -std=gnu++0x host issue resolved |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 41 | return ClassPathEntry(reinterpret_cast<const DexFile*>(NULL), |
| 42 | reinterpret_cast<const DexFile::ClassDef*>(NULL)); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 45 | void DexFile::OpenDexFiles(std::vector<const char*>& dex_filenames, |
| 46 | std::vector<const DexFile*>& dex_files, |
| 47 | const std::string& strip_location_prefix) { |
| 48 | for (size_t i = 0; i < dex_filenames.size(); i++) { |
| 49 | const char* dex_filename = dex_filenames[i]; |
| 50 | const DexFile* dex_file = Open(dex_filename, strip_location_prefix); |
| 51 | if (dex_file == NULL) { |
| 52 | fprintf(stderr, "could not open .dex from file %s\n", dex_filename); |
| 53 | exit(EXIT_FAILURE); |
| 54 | } |
| 55 | dex_files.push_back(dex_file); |
| 56 | } |
| 57 | } |
| 58 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 59 | const DexFile* DexFile::Open(const std::string& filename, |
| 60 | const std::string& strip_location_prefix) { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 61 | if (filename.size() < 4) { |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 62 | LOG(WARNING) << "Attempting to open dex file with unknown extension '" << filename << "'"; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 63 | } |
| 64 | std::string suffix(filename.substr(filename.size() - 4)); |
| 65 | if (suffix == ".zip" || suffix == ".jar" || suffix == ".apk") { |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 66 | return DexFile::OpenZip(filename, strip_location_prefix); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 67 | } else { |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 68 | return DexFile::OpenFile(filename, filename, strip_location_prefix); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
jeffhao | b4df514 | 2011-09-19 20:25:32 -0700 | [diff] [blame] | 72 | void DexFile::ChangePermissions(int prot) const { |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 73 | if (mprotect(mem_map_->GetAddress(), mem_map_->GetLength(), prot) != 0) { |
jeffhao | b4df514 | 2011-09-19 20:25:32 -0700 | [diff] [blame] | 74 | PLOG(FATAL) << "Failed to change dex file permissions to " << prot; |
| 75 | } |
| 76 | } |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 77 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 78 | const DexFile* DexFile::OpenFile(const std::string& filename, |
| 79 | const std::string& original_location, |
| 80 | const std::string& strip_location_prefix) { |
| 81 | StringPiece location = original_location; |
| 82 | if (!location.starts_with(strip_location_prefix)) { |
| 83 | LOG(ERROR) << filename << " does not start with " << strip_location_prefix; |
| 84 | return NULL; |
| 85 | } |
| 86 | location.remove_prefix(strip_location_prefix.size()); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 87 | int fd = open(filename.c_str(), O_RDONLY); // TODO: scoped_fd |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 88 | if (fd == -1) { |
| 89 | PLOG(ERROR) << "open(\"" << filename << "\", O_RDONLY) failed"; |
| 90 | return NULL; |
| 91 | } |
| 92 | struct stat sbuf; |
| 93 | memset(&sbuf, 0, sizeof(sbuf)); |
| 94 | if (fstat(fd, &sbuf) == -1) { |
| 95 | PLOG(ERROR) << "fstat \"" << filename << "\" failed"; |
| 96 | close(fd); |
| 97 | return NULL; |
| 98 | } |
| 99 | size_t length = sbuf.st_size; |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 100 | UniquePtr<MemMap> map(MemMap::Map(length, PROT_READ, MAP_PRIVATE, fd, 0)); |
| 101 | if (map.get() == NULL) { |
| 102 | LOG(ERROR) << "mmap \"" << filename << "\" failed"; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 103 | close(fd); |
| 104 | return NULL; |
| 105 | } |
| 106 | close(fd); |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 107 | byte* dex_file = map->GetAddress(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 108 | return OpenMemory(dex_file, length, location.ToString(), map.release()); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 111 | static const char* kClassesDex = "classes.dex"; |
| 112 | |
| 113 | class LockedFd { |
| 114 | public: |
| 115 | static LockedFd* CreateAndLock(std::string& name, mode_t mode) { |
| 116 | int fd = open(name.c_str(), O_CREAT | O_RDWR, mode); |
| 117 | if (fd == -1) { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 118 | PLOG(ERROR) << "Failed to open file '" << name << "'"; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 119 | return NULL; |
| 120 | } |
| 121 | fchmod(fd, mode); |
| 122 | |
| 123 | LOG(INFO) << "locking file " << name << " (fd=" << fd << ")"; |
| 124 | int result = flock(fd, LOCK_EX | LOCK_NB); |
| 125 | if (result == -1) { |
| 126 | LOG(WARNING) << "sleeping while locking file " << name; |
| 127 | result = flock(fd, LOCK_EX); |
| 128 | } |
| 129 | if (result == -1 ) { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 130 | PLOG(ERROR) << "Failed to lock file '" << name << "'"; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 131 | close(fd); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 132 | return NULL; |
| 133 | } |
| 134 | return new LockedFd(fd); |
| 135 | } |
| 136 | |
| 137 | int GetFd() const { |
| 138 | return fd_; |
| 139 | } |
| 140 | |
| 141 | ~LockedFd() { |
| 142 | if (fd_ != -1) { |
| 143 | int result = flock(fd_, LOCK_UN); |
| 144 | if (result == -1) { |
| 145 | PLOG(WARNING) << "flock(" << fd_ << ", LOCK_UN) failed"; |
| 146 | } |
| 147 | close(fd_); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | private: |
| 152 | LockedFd(int fd) : fd_(fd) {} |
| 153 | |
| 154 | int fd_; |
| 155 | }; |
| 156 | |
| 157 | class TmpFile { |
| 158 | public: |
| 159 | TmpFile(const std::string name) : name_(name) {} |
| 160 | ~TmpFile() { |
| 161 | unlink(name_.c_str()); |
| 162 | } |
| 163 | private: |
| 164 | const std::string name_; |
| 165 | }; |
| 166 | |
| 167 | // Open classes.dex from within a .zip, .jar, .apk, ... |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 168 | const DexFile* DexFile::OpenZip(const std::string& filename, |
| 169 | const std::string& strip_location_prefix) { |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 170 | |
| 171 | // First, look for a ".dex" alongside the jar file. It will have |
| 172 | // the same name/path except for the extension. |
| 173 | |
| 174 | // Example filename = dir/foo.jar |
| 175 | std::string adjacent_dex_filename(filename); |
| 176 | size_t found = adjacent_dex_filename.find_last_of("."); |
| 177 | if (found == std::string::npos) { |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 178 | LOG(ERROR) << "No . in filename" << filename; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 179 | return NULL; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 180 | } |
| 181 | adjacent_dex_filename.replace(adjacent_dex_filename.begin() + found, |
| 182 | adjacent_dex_filename.end(), |
| 183 | ".dex"); |
| 184 | // Example adjacent_dex_filename = dir/foo.dex |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 185 | if (OS::FileExists(adjacent_dex_filename.c_str())) { |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 186 | const DexFile* adjacent_dex_file = DexFile::OpenFile(adjacent_dex_filename, |
| 187 | filename, |
| 188 | strip_location_prefix); |
Elliott Hughes | e0fc0ef | 2011-08-12 17:39:17 -0700 | [diff] [blame] | 189 | if (adjacent_dex_file != NULL) { |
Brian Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame] | 190 | // We don't verify anything in this case, because we aren't in |
| 191 | // the cache and typically the file is in the readonly /system |
| 192 | // area, so if something is wrong, there is nothing we can do. |
| 193 | return adjacent_dex_file; |
Elliott Hughes | e0fc0ef | 2011-08-12 17:39:17 -0700 | [diff] [blame] | 194 | } |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 195 | return NULL; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | char resolved[PATH_MAX]; |
| 199 | char* absolute_path = realpath(filename.c_str(), resolved); |
| 200 | if (absolute_path == NULL) { |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 201 | LOG(ERROR) << "Failed to create absolute path for " << filename |
| 202 | << " when looking for classes.dex"; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 203 | return NULL; |
| 204 | } |
| 205 | std::string cache_file(absolute_path+1); // skip leading slash |
| 206 | std::replace(cache_file.begin(), cache_file.end(), '/', '@'); |
| 207 | cache_file.push_back('@'); |
| 208 | cache_file.append(kClassesDex); |
| 209 | // Example cache_file = parent@dir@foo.jar@classes.dex |
| 210 | |
| 211 | const char* data_root = getenv("ANDROID_DATA"); |
| 212 | if (data_root == NULL) { |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 213 | if (OS::DirectoryExists("/data")) { |
| 214 | data_root = "/data"; |
| 215 | } else { |
| 216 | data_root = "/tmp"; |
| 217 | } |
| 218 | } |
| 219 | if (!OS::DirectoryExists(data_root)) { |
| 220 | LOG(ERROR) << "Failed to find ANDROID_DATA directory " << data_root; |
| 221 | return NULL; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 224 | std::string art_cache = StringPrintf("%s/art-cache", data_root); |
| 225 | |
| 226 | if (!OS::DirectoryExists(art_cache.c_str())) { |
| 227 | if (StringPiece(art_cache).starts_with("/tmp/")) { |
| 228 | int result = mkdir(art_cache.c_str(), 0700); |
| 229 | if (result != 0) { |
Elliott Hughes | 380fac0 | 2011-09-16 16:01:07 -0700 | [diff] [blame] | 230 | LOG(FATAL) << "Failed to create art-cache directory " << art_cache; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 231 | return NULL; |
| 232 | } |
| 233 | } else { |
Elliott Hughes | 380fac0 | 2011-09-16 16:01:07 -0700 | [diff] [blame] | 234 | LOG(FATAL) << "Failed to find art-cache directory " << art_cache; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 235 | return NULL; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | std::string cache_path_tmp = StringPrintf("%s/%s", art_cache.c_str(), cache_file.c_str()); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 240 | // Example cache_path_tmp = /data/art-cache/parent@dir@foo.jar@classes.dex |
| 241 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 242 | UniquePtr<ZipArchive> zip_archive(ZipArchive::Open(filename)); |
| 243 | if (zip_archive.get() == NULL) { |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 244 | LOG(ERROR) << "Failed to open " << filename << " when looking for classes.dex"; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 245 | return NULL; |
| 246 | } |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 247 | UniquePtr<ZipEntry> zip_entry(zip_archive->Find(kClassesDex)); |
| 248 | if (zip_entry.get() == NULL) { |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 249 | LOG(ERROR) << "Failed to find classes.dex within " << filename; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 250 | return NULL; |
| 251 | } |
| 252 | |
| 253 | std::string cache_path = StringPrintf("%s.%08x", cache_path_tmp.c_str(), zip_entry->GetCrc32()); |
| 254 | // Example cache_path = /data/art-cache/parent@dir@foo.jar@classes.dex.1a2b3c4d |
| 255 | |
| 256 | while (true) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 257 | if (OS::FileExists(cache_path.c_str())) { |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 258 | const DexFile* cached_dex_file = DexFile::OpenFile(cache_path, |
| 259 | filename, |
| 260 | strip_location_prefix); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 261 | if (cached_dex_file != NULL) { |
| 262 | return cached_dex_file; |
| 263 | } |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | // Try to open the temporary cache file, grabbing an exclusive |
| 267 | // lock. If somebody else is working on it, we'll block here until |
| 268 | // they complete. Because we're waiting on an external resource, |
| 269 | // we go into native mode. |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 270 | // Note that self can be NULL if we're parsing the bootclasspath |
Elliott Hughes | 40ef99e | 2011-08-11 17:44:34 -0700 | [diff] [blame] | 271 | // during JNI_CreateJavaVM. |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 272 | Thread* self = Thread::Current(); |
| 273 | UniquePtr<ScopedThreadStateChange> state_changer; |
| 274 | if (self != NULL) { |
| 275 | state_changer.reset(new ScopedThreadStateChange(self, Thread::kNative)); |
Elliott Hughes | 40ef99e | 2011-08-11 17:44:34 -0700 | [diff] [blame] | 276 | } |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 277 | UniquePtr<LockedFd> fd(LockedFd::CreateAndLock(cache_path_tmp, 0644)); |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 278 | state_changer.reset(NULL); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 279 | if (fd.get() == NULL) { |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 280 | return NULL; |
| 281 | } |
| 282 | |
| 283 | // Check to see if the fd we opened and locked matches the file in |
| 284 | // the filesystem. If they don't, then somebody else unlinked |
| 285 | // ours and created a new file, and we need to use that one |
| 286 | // instead. (If we caught them between the unlink and the create, |
| 287 | // we'll get an ENOENT from the file stat.) |
| 288 | struct stat fd_stat; |
| 289 | int fd_stat_result = fstat(fd->GetFd(), &fd_stat); |
| 290 | if (fd_stat_result == -1) { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 291 | PLOG(ERROR) << "Failed to stat open file '" << cache_path_tmp << "'"; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 292 | return NULL; |
| 293 | } |
| 294 | struct stat file_stat; |
| 295 | int file_stat_result = stat(cache_path_tmp.c_str(), &file_stat); |
| 296 | if (file_stat_result == -1 || |
| 297 | fd_stat.st_dev != file_stat.st_dev || fd_stat.st_ino != file_stat.st_ino) { |
| 298 | LOG(WARNING) << "our open cache file is stale; sleeping and retrying"; |
| 299 | usleep(250 * 1000); // if something is hosed, don't peg machine |
| 300 | continue; |
| 301 | } |
| 302 | |
| 303 | // We have the correct file open and locked. Extract classes.dex |
| 304 | TmpFile tmp_file(cache_path_tmp); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 305 | UniquePtr<File> file(OS::FileFromFd(cache_path_tmp.c_str(), fd->GetFd())); |
| 306 | if (file.get() == NULL) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 307 | return NULL; |
| 308 | } |
| 309 | bool success = zip_entry->Extract(*file); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 310 | if (!success) { |
| 311 | return NULL; |
| 312 | } |
| 313 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 314 | // TODO: restat and check length against zip_entry->GetUncompressedLength()? |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 315 | |
| 316 | // Compute checksum and compare to zip. If things look okay, rename from tmp. |
| 317 | off_t lseek_result = lseek(fd->GetFd(), 0, SEEK_SET); |
| 318 | if (lseek_result == -1) { |
| 319 | return NULL; |
| 320 | } |
| 321 | const size_t kBufSize = 32768; |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 322 | UniquePtr<uint8_t[]> buf(new uint8_t[kBufSize]); |
| 323 | if (buf.get() == NULL) { |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 324 | return NULL; |
| 325 | } |
| 326 | uint32_t computed_crc = crc32(0L, Z_NULL, 0); |
| 327 | while (true) { |
| 328 | ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd->GetFd(), buf.get(), kBufSize)); |
Brian Carlstrom | 0024d6c | 2011-08-09 08:26:12 -0700 | [diff] [blame] | 329 | if (bytes_read == -1) { |
| 330 | PLOG(ERROR) << "Problem computing CRC of '" << cache_path_tmp << "'"; |
| 331 | return NULL; |
| 332 | } |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 333 | if (bytes_read == 0) { |
| 334 | break; |
| 335 | } |
| 336 | computed_crc = crc32(computed_crc, buf.get(), bytes_read); |
| 337 | } |
| 338 | if (computed_crc != zip_entry->GetCrc32()) { |
| 339 | return NULL; |
| 340 | } |
| 341 | int rename_result = rename(cache_path_tmp.c_str(), cache_path.c_str()); |
| 342 | if (rename_result == -1) { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 343 | PLOG(ERROR) << "Failed to install dex cache file '" << cache_path << "'" |
Brian Carlstrom | 0024d6c | 2011-08-09 08:26:12 -0700 | [diff] [blame] | 344 | << " from '" << cache_path_tmp << "'"; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 345 | unlink(cache_path.c_str()); |
| 346 | } |
| 347 | } |
| 348 | // NOTREACHED |
| 349 | } |
| 350 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 351 | const DexFile* DexFile::OpenMemory(const byte* dex_bytes, size_t length, |
| 352 | const std::string& location, MemMap* mem_map) { |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 353 | UniquePtr<DexFile> dex_file(new DexFile(dex_bytes, length, location, mem_map)); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 354 | if (!dex_file->Init()) { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 355 | return NULL; |
| 356 | } else { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 357 | return dex_file.release(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
Jesse Wilson | 6bf1915 | 2011-09-29 13:12:33 -0400 | [diff] [blame] | 361 | DexFile::~DexFile() { |
Elliott Hughes | 8cef0b8 | 2011-10-11 19:24:00 -0700 | [diff] [blame] | 362 | // We don't call DeleteGlobalRef on dex_object_ because we're only called by DestroyJavaVM, and |
| 363 | // that's only called after DetachCurrentThread, which means there's no JNIEnv. We could |
| 364 | // re-attach, but cleaning up these global references is not obviously useful. It's not as if |
| 365 | // the global reference table is otherwise empty! |
Jesse Wilson | 6bf1915 | 2011-09-29 13:12:33 -0400 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | jobject DexFile::GetDexObject(JNIEnv* env) const { |
| 369 | MutexLock mu(dex_object_lock_); |
| 370 | if (dex_object_ != NULL) { |
| 371 | return dex_object_; |
| 372 | } |
| 373 | |
| 374 | void* address = const_cast<void*>(reinterpret_cast<const void*>(base_)); |
| 375 | jobject byte_buffer = env->NewDirectByteBuffer(address, length_); |
| 376 | if (byte_buffer == NULL) { |
| 377 | return NULL; |
| 378 | } |
| 379 | |
| 380 | jclass c = env->FindClass("com/android/dex/Dex"); |
| 381 | if (c == NULL) { |
| 382 | return NULL; |
| 383 | } |
| 384 | |
| 385 | jmethodID mid = env->GetStaticMethodID(c, "create", "(Ljava/nio/ByteBuffer;)Lcom/android/dex/Dex;"); |
| 386 | if (mid == NULL) { |
| 387 | return NULL; |
| 388 | } |
| 389 | |
| 390 | jvalue args[1]; |
| 391 | args[0].l = byte_buffer; |
| 392 | jobject local = env->CallStaticObjectMethodA(c, mid, args); |
| 393 | if (local == NULL) { |
| 394 | return NULL; |
| 395 | } |
| 396 | |
| 397 | dex_object_ = env->NewGlobalRef(local); |
| 398 | return dex_object_; |
| 399 | } |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 400 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 401 | bool DexFile::Init() { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 402 | InitMembers(); |
| 403 | if (!IsMagicValid()) { |
| 404 | return false; |
| 405 | } |
| 406 | InitIndex(); |
| 407 | return true; |
| 408 | } |
| 409 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 410 | void DexFile::InitMembers() { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 411 | const byte* b = base_; |
| 412 | header_ = reinterpret_cast<const Header*>(b); |
| 413 | const Header* h = header_; |
| 414 | string_ids_ = reinterpret_cast<const StringId*>(b + h->string_ids_off_); |
| 415 | type_ids_ = reinterpret_cast<const TypeId*>(b + h->type_ids_off_); |
| 416 | field_ids_ = reinterpret_cast<const FieldId*>(b + h->field_ids_off_); |
| 417 | method_ids_ = reinterpret_cast<const MethodId*>(b + h->method_ids_off_); |
| 418 | proto_ids_ = reinterpret_cast<const ProtoId*>(b + h->proto_ids_off_); |
| 419 | class_defs_ = reinterpret_cast<const ClassDef*>(b + h->class_defs_off_); |
| 420 | } |
| 421 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 422 | bool DexFile::IsMagicValid() { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 423 | return CheckMagic(header_->magic_); |
| 424 | } |
| 425 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 426 | bool DexFile::CheckMagic(const byte* magic) { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 427 | CHECK(magic != NULL); |
| 428 | if (memcmp(magic, kDexMagic, sizeof(kDexMagic)) != 0) { |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 429 | LOG(ERROR) << "Unrecognized magic number:" |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 430 | << " " << magic[0] |
| 431 | << " " << magic[1] |
| 432 | << " " << magic[2] |
| 433 | << " " << magic[3]; |
| 434 | return false; |
| 435 | } |
| 436 | const byte* version = &magic[sizeof(kDexMagic)]; |
| 437 | if (memcmp(version, kDexMagicVersion, sizeof(kDexMagicVersion)) != 0) { |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 438 | LOG(ERROR) << "Unrecognized version number:" |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 439 | << " " << version[0] |
| 440 | << " " << version[1] |
| 441 | << " " << version[2] |
| 442 | << " " << version[3]; |
| 443 | return false; |
| 444 | } |
| 445 | return true; |
| 446 | } |
| 447 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 448 | void DexFile::InitIndex() { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 449 | CHECK_EQ(index_.size(), 0U); |
| 450 | for (size_t i = 0; i < NumClassDefs(); ++i) { |
| 451 | const ClassDef& class_def = GetClassDef(i); |
| 452 | const char* descriptor = GetClassDescriptor(class_def); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 453 | index_[descriptor] = i; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 457 | bool DexFile::FindClassDefIndex(const StringPiece& descriptor, uint32_t& idx) const { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 458 | Index::const_iterator it = index_.find(descriptor); |
| 459 | if (it == index_.end()) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 460 | return false; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 461 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 462 | idx = it->second; |
| 463 | return true; |
| 464 | } |
| 465 | |
| 466 | const DexFile::ClassDef* DexFile::FindClassDef(const StringPiece& descriptor) const { |
| 467 | uint32_t idx; |
| 468 | if (FindClassDefIndex(descriptor, idx)) { |
| 469 | return &GetClassDef(idx); |
| 470 | } |
| 471 | return NULL; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 474 | // Materializes the method descriptor for a method prototype. Method |
| 475 | // descriptors are not stored directly in the dex file. Instead, one |
| 476 | // must assemble the descriptor from references in the prototype. |
Elliott Hughes | 0c424cb | 2011-08-26 10:16:25 -0700 | [diff] [blame] | 477 | std::string DexFile::CreateMethodDescriptor(uint32_t proto_idx, |
| 478 | int32_t* unicode_length) const { |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 479 | const ProtoId& proto_id = GetProtoId(proto_idx); |
| 480 | std::string descriptor; |
| 481 | descriptor.push_back('('); |
| 482 | const TypeList* type_list = GetProtoParameters(proto_id); |
| 483 | size_t parameter_length = 0; |
| 484 | if (type_list != NULL) { |
| 485 | // A non-zero number of arguments. Append the type names. |
| 486 | for (size_t i = 0; i < type_list->Size(); ++i) { |
| 487 | const TypeItem& type_item = type_list->GetTypeItem(i); |
| 488 | uint32_t type_idx = type_item.type_idx_; |
| 489 | int32_t type_length; |
| 490 | const char* name = dexStringByTypeIdx(type_idx, &type_length); |
| 491 | parameter_length += type_length; |
| 492 | descriptor.append(name); |
| 493 | } |
| 494 | } |
| 495 | descriptor.push_back(')'); |
| 496 | uint32_t return_type_idx = proto_id.return_type_idx_; |
| 497 | int32_t return_type_length; |
| 498 | const char* name = dexStringByTypeIdx(return_type_idx, &return_type_length); |
| 499 | descriptor.append(name); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 500 | if (unicode_length != NULL) { |
| 501 | *unicode_length = parameter_length + return_type_length + 2; // 2 for ( and ) |
| 502 | } |
Elliott Hughes | 0c424cb | 2011-08-26 10:16:25 -0700 | [diff] [blame] | 503 | return descriptor; |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 506 | // Read a signed integer. "zwidth" is the zero-based byte count. |
| 507 | static int32_t ReadSignedInt(const byte* ptr, int zwidth) |
| 508 | { |
| 509 | int32_t val = 0; |
| 510 | for (int i = zwidth; i >= 0; --i) { |
| 511 | val = ((uint32_t)val >> 8) | (((int32_t)*ptr++) << 24); |
| 512 | } |
| 513 | val >>= (3 - zwidth) * 8; |
| 514 | return val; |
| 515 | } |
| 516 | |
| 517 | // Read an unsigned integer. "zwidth" is the zero-based byte count, |
| 518 | // "fill_on_right" indicates which side we want to zero-fill from. |
| 519 | static uint32_t ReadUnsignedInt(const byte* ptr, int zwidth, |
| 520 | bool fill_on_right) { |
| 521 | uint32_t val = 0; |
| 522 | if (!fill_on_right) { |
| 523 | for (int i = zwidth; i >= 0; --i) { |
| 524 | val = (val >> 8) | (((uint32_t)*ptr++) << 24); |
| 525 | } |
| 526 | val >>= (3 - zwidth) * 8; |
| 527 | } else { |
| 528 | for (int i = zwidth; i >= 0; --i) { |
| 529 | val = (val >> 8) | (((uint32_t)*ptr++) << 24); |
| 530 | } |
| 531 | } |
| 532 | return val; |
| 533 | } |
| 534 | |
| 535 | // Read a signed long. "zwidth" is the zero-based byte count. |
| 536 | static int64_t ReadSignedLong(const byte* ptr, int zwidth) { |
| 537 | int64_t val = 0; |
| 538 | for (int i = zwidth; i >= 0; --i) { |
| 539 | val = ((uint64_t)val >> 8) | (((int64_t)*ptr++) << 56); |
| 540 | } |
| 541 | val >>= (7 - zwidth) * 8; |
| 542 | return val; |
| 543 | } |
| 544 | |
| 545 | // Read an unsigned long. "zwidth" is the zero-based byte count, |
| 546 | // "fill_on_right" indicates which side we want to zero-fill from. |
| 547 | static uint64_t ReadUnsignedLong(const byte* ptr, int zwidth, |
| 548 | bool fill_on_right) { |
| 549 | uint64_t val = 0; |
| 550 | if (!fill_on_right) { |
| 551 | for (int i = zwidth; i >= 0; --i) { |
| 552 | val = (val >> 8) | (((uint64_t)*ptr++) << 56); |
| 553 | } |
| 554 | val >>= (7 - zwidth) * 8; |
| 555 | } else { |
| 556 | for (int i = zwidth; i >= 0; --i) { |
| 557 | val = (val >> 8) | (((uint64_t)*ptr++) << 56); |
| 558 | } |
| 559 | } |
| 560 | return val; |
| 561 | } |
| 562 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 563 | DexFile::ValueType DexFile::ReadEncodedValue(const byte** stream, |
| 564 | JValue* value) const { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 565 | const byte* ptr = *stream; |
| 566 | byte value_type = *ptr++; |
| 567 | byte value_arg = value_type >> kEncodedValueArgShift; |
| 568 | size_t width = value_arg + 1; // assume and correct later |
| 569 | int type = value_type & kEncodedValueTypeMask; |
| 570 | switch (type) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 571 | case DexFile::kByte: { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 572 | int32_t b = ReadSignedInt(ptr, value_arg); |
| 573 | CHECK(IsInt(8, b)); |
| 574 | value->i = b; |
| 575 | break; |
| 576 | } |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 577 | case DexFile::kShort: { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 578 | int32_t s = ReadSignedInt(ptr, value_arg); |
| 579 | CHECK(IsInt(16, s)); |
| 580 | value->i = s; |
| 581 | break; |
| 582 | } |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 583 | case DexFile::kChar: { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 584 | uint32_t c = ReadUnsignedInt(ptr, value_arg, false); |
| 585 | CHECK(IsUint(16, c)); |
| 586 | value->i = c; |
| 587 | break; |
| 588 | } |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 589 | case DexFile::kInt: |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 590 | value->i = ReadSignedInt(ptr, value_arg); |
| 591 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 592 | case DexFile::kLong: |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 593 | value->j = ReadSignedLong(ptr, value_arg); |
| 594 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 595 | case DexFile::kFloat: |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 596 | value->i = ReadUnsignedInt(ptr, value_arg, true); |
| 597 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 598 | case DexFile::kDouble: |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 599 | value->j = ReadUnsignedLong(ptr, value_arg, true); |
| 600 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 601 | case DexFile::kBoolean: |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 602 | value->i = (value_arg != 0); |
| 603 | width = 0; |
| 604 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 605 | case DexFile::kString: |
| 606 | case DexFile::kType: |
| 607 | case DexFile::kMethod: |
| 608 | case DexFile::kEnum: |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 609 | value->i = ReadUnsignedInt(ptr, value_arg, false); |
| 610 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 611 | case DexFile::kField: |
| 612 | case DexFile::kArray: |
| 613 | case DexFile::kAnnotation: |
Elliott Hughes | 53b6131 | 2011-08-12 18:28:20 -0700 | [diff] [blame] | 614 | UNIMPLEMENTED(FATAL) << ": type " << type; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 615 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 616 | case DexFile::kNull: |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 617 | value->i = 0; |
| 618 | width = 0; |
| 619 | break; |
| 620 | default: |
| 621 | LOG(FATAL) << "Unreached"; |
| 622 | } |
| 623 | ptr += width; |
| 624 | *stream = ptr; |
| 625 | return static_cast<ValueType>(type); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 626 | } |
| 627 | |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 628 | String* DexFile::dexArtStringById(int32_t idx) const { |
| 629 | if (idx == -1) { |
| 630 | return NULL; |
| 631 | } |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 632 | return String::AllocFromModifiedUtf8(dexStringById(idx)); |
| 633 | } |
| 634 | |
| 635 | int32_t DexFile::GetLineNumFromPC(const art::Method* method, uint32_t rel_pc) const { |
Shih-wei Liao | ff0f9be | 2011-08-29 15:43:53 -0700 | [diff] [blame] | 636 | // For native method, lineno should be -2 to indicate it is native. Note that |
| 637 | // "line number == -2" is how libcore tells from StackTraceElement. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 638 | if (method->GetCodeItemOffset() == 0) { |
Shih-wei Liao | ff0f9be | 2011-08-29 15:43:53 -0700 | [diff] [blame] | 639 | return -2; |
| 640 | } |
| 641 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 642 | const CodeItem* code_item = GetCodeItem(method->GetCodeItemOffset()); |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 643 | DCHECK(code_item != NULL); |
| 644 | |
| 645 | // A method with no line number info should return -1 |
| 646 | LineNumFromPcContext context(rel_pc, -1); |
| 647 | dexDecodeDebugInfo(code_item, method, LineNumForPcCb, NULL, &context); |
| 648 | return context.line_num_; |
| 649 | } |
| 650 | |
| 651 | void DexFile::dexDecodeDebugInfo0(const CodeItem* code_item, const art::Method* method, |
| 652 | DexDebugNewPositionCb posCb, DexDebugNewLocalCb local_cb, |
| 653 | void* cnxt, const byte* stream, LocalInfo* local_in_reg) const { |
| 654 | uint32_t line = DecodeUnsignedLeb128(&stream); |
| 655 | uint32_t parameters_size = DecodeUnsignedLeb128(&stream); |
| 656 | uint16_t arg_reg = code_item->registers_size_ - code_item->ins_size_; |
| 657 | uint32_t address = 0; |
| 658 | |
| 659 | if (!method->IsStatic()) { |
| 660 | local_in_reg[arg_reg].name_ = String::AllocFromModifiedUtf8("this"); |
| 661 | local_in_reg[arg_reg].descriptor_ = method->GetDeclaringClass()->GetDescriptor(); |
| 662 | local_in_reg[arg_reg].signature_ = NULL; |
| 663 | local_in_reg[arg_reg].start_address_ = 0; |
| 664 | local_in_reg[arg_reg].is_live_ = true; |
| 665 | arg_reg++; |
| 666 | } |
| 667 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 668 | ParameterIterator *it = GetParameterIterator(GetProtoId(method->GetProtoIdx())); |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 669 | for (uint32_t i = 0; i < parameters_size && it->HasNext(); ++i, it->Next()) { |
| 670 | if (arg_reg >= code_item->registers_size_) { |
| 671 | LOG(FATAL) << "invalid stream"; |
| 672 | return; |
| 673 | } |
| 674 | |
| 675 | String* descriptor = String::AllocFromModifiedUtf8(it->GetDescriptor()); |
| 676 | String* name = dexArtStringById(DecodeUnsignedLeb128P1(&stream)); |
| 677 | |
| 678 | local_in_reg[arg_reg].name_ = name; |
| 679 | local_in_reg[arg_reg].descriptor_ = descriptor; |
| 680 | local_in_reg[arg_reg].signature_ = NULL; |
| 681 | local_in_reg[arg_reg].start_address_ = address; |
| 682 | local_in_reg[arg_reg].is_live_ = true; |
| 683 | switch (descriptor->CharAt(0)) { |
| 684 | case 'D': |
| 685 | case 'J': |
| 686 | arg_reg += 2; |
| 687 | break; |
| 688 | default: |
| 689 | arg_reg += 1; |
| 690 | break; |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | if (it->HasNext()) { |
| 695 | LOG(FATAL) << "invalid stream"; |
| 696 | return; |
| 697 | } |
| 698 | |
| 699 | for (;;) { |
| 700 | uint8_t opcode = *stream++; |
| 701 | uint8_t adjopcode = opcode - DBG_FIRST_SPECIAL; |
| 702 | uint16_t reg; |
| 703 | |
| 704 | |
| 705 | switch (opcode) { |
| 706 | case DBG_END_SEQUENCE: |
| 707 | return; |
| 708 | |
| 709 | case DBG_ADVANCE_PC: |
| 710 | address += DecodeUnsignedLeb128(&stream); |
| 711 | break; |
| 712 | |
| 713 | case DBG_ADVANCE_LINE: |
| 714 | line += DecodeUnsignedLeb128(&stream); |
| 715 | break; |
| 716 | |
| 717 | case DBG_START_LOCAL: |
| 718 | case DBG_START_LOCAL_EXTENDED: |
| 719 | reg = DecodeUnsignedLeb128(&stream); |
| 720 | if (reg > code_item->registers_size_) { |
| 721 | LOG(FATAL) << "invalid stream"; |
| 722 | return; |
| 723 | } |
| 724 | |
| 725 | // Emit what was previously there, if anything |
| 726 | InvokeLocalCbIfLive(cnxt, reg, address, local_in_reg, local_cb); |
| 727 | |
| 728 | local_in_reg[reg].name_ = dexArtStringById(DecodeUnsignedLeb128P1(&stream)); |
| 729 | local_in_reg[reg].descriptor_ = dexArtStringByTypeIdx(DecodeUnsignedLeb128P1(&stream)); |
| 730 | if (opcode == DBG_START_LOCAL_EXTENDED) { |
| 731 | local_in_reg[reg].signature_ = dexArtStringById(DecodeUnsignedLeb128P1(&stream)); |
| 732 | } else { |
| 733 | local_in_reg[reg].signature_ = NULL; |
| 734 | } |
| 735 | local_in_reg[reg].start_address_ = address; |
| 736 | local_in_reg[reg].is_live_ = true; |
| 737 | break; |
| 738 | |
| 739 | case DBG_END_LOCAL: |
| 740 | reg = DecodeUnsignedLeb128(&stream); |
| 741 | if (reg > code_item->registers_size_) { |
| 742 | LOG(FATAL) << "invalid stream"; |
| 743 | return; |
| 744 | } |
| 745 | |
| 746 | InvokeLocalCbIfLive(cnxt, reg, address, local_in_reg, local_cb); |
| 747 | local_in_reg[reg].is_live_ = false; |
| 748 | break; |
| 749 | |
| 750 | case DBG_RESTART_LOCAL: |
| 751 | reg = DecodeUnsignedLeb128(&stream); |
| 752 | if (reg > code_item->registers_size_) { |
| 753 | LOG(FATAL) << "invalid stream"; |
| 754 | return; |
| 755 | } |
| 756 | |
| 757 | if (local_in_reg[reg].name_ == NULL |
| 758 | || local_in_reg[reg].descriptor_ == NULL) { |
| 759 | LOG(FATAL) << "invalid stream"; |
| 760 | return; |
| 761 | } |
| 762 | |
| 763 | // If the register is live, the "restart" is superfluous, |
| 764 | // and we don't want to mess with the existing start address. |
| 765 | if (!local_in_reg[reg].is_live_) { |
| 766 | local_in_reg[reg].start_address_ = address; |
| 767 | local_in_reg[reg].is_live_ = true; |
| 768 | } |
| 769 | break; |
| 770 | |
| 771 | case DBG_SET_PROLOGUE_END: |
| 772 | case DBG_SET_EPILOGUE_BEGIN: |
| 773 | case DBG_SET_FILE: |
| 774 | break; |
| 775 | |
| 776 | default: |
| 777 | address += adjopcode / DBG_LINE_RANGE; |
| 778 | line += DBG_LINE_BASE + (adjopcode % DBG_LINE_RANGE); |
| 779 | |
| 780 | if (posCb != NULL) { |
| 781 | if (posCb(cnxt, address, line)) { |
| 782 | // early exit |
| 783 | return; |
| 784 | } |
| 785 | } |
| 786 | break; |
| 787 | } |
| 788 | } |
| 789 | } |
| 790 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 791 | } // namespace art |