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