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" |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 17 | #include "class_linker.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 18 | #include "globals.h" |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 19 | #include "leb128.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 20 | #include "logging.h" |
| 21 | #include "object.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 22 | #include "os.h" |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 23 | #include "stringprintf.h" |
| 24 | #include "thread.h" |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 25 | #include "utf.h" |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 26 | #include "utils.h" |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 27 | #include "zip_archive.h" |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 31 | const byte DexFile::kDexMagic[] = { 'd', 'e', 'x', '\n' }; |
| 32 | const byte DexFile::kDexMagicVersion[] = { '0', '3', '5', '\0' }; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 33 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 34 | DexFile::ClassPathEntry DexFile::FindInClassPath(const StringPiece& descriptor, |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 35 | const ClassPath& class_path) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 36 | for (size_t i = 0; i != class_path.size(); ++i) { |
| 37 | const DexFile* dex_file = class_path[i]; |
| 38 | const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor); |
| 39 | if (dex_class_def != NULL) { |
| 40 | return ClassPathEntry(dex_file, dex_class_def); |
| 41 | } |
| 42 | } |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 43 | // 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] | 44 | return ClassPathEntry(reinterpret_cast<const DexFile*>(NULL), |
| 45 | reinterpret_cast<const DexFile::ClassDef*>(NULL)); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 48 | void DexFile::OpenDexFiles(const std::vector<const char*>& dex_filenames, |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 49 | std::vector<const DexFile*>& dex_files, |
| 50 | const std::string& strip_location_prefix) { |
| 51 | for (size_t i = 0; i < dex_filenames.size(); i++) { |
| 52 | const char* dex_filename = dex_filenames[i]; |
| 53 | const DexFile* dex_file = Open(dex_filename, strip_location_prefix); |
| 54 | if (dex_file == NULL) { |
| 55 | fprintf(stderr, "could not open .dex from file %s\n", dex_filename); |
| 56 | exit(EXIT_FAILURE); |
| 57 | } |
| 58 | dex_files.push_back(dex_file); |
| 59 | } |
| 60 | } |
| 61 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 62 | const DexFile* DexFile::Open(const std::string& filename, |
| 63 | const std::string& strip_location_prefix) { |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 64 | if (IsValidZipFilename(filename)) { |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 65 | return DexFile::OpenZip(filename, strip_location_prefix); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 66 | } |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 67 | if (!IsValidDexFilename(filename)) { |
| 68 | LOG(WARNING) << "Attempting to open dex file with unknown extension '" << filename << "'"; |
| 69 | } |
| 70 | return DexFile::OpenFile(filename, filename, strip_location_prefix); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 71 | } |
| 72 | |
jeffhao | b4df514 | 2011-09-19 20:25:32 -0700 | [diff] [blame] | 73 | void DexFile::ChangePermissions(int prot) const { |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 74 | if (mprotect(mem_map_->GetAddress(), mem_map_->GetLength(), prot) != 0) { |
jeffhao | b4df514 | 2011-09-19 20:25:32 -0700 | [diff] [blame] | 75 | PLOG(FATAL) << "Failed to change dex file permissions to " << prot; |
| 76 | } |
| 77 | } |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 78 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 79 | const DexFile* DexFile::OpenFile(const std::string& filename, |
| 80 | const std::string& original_location, |
| 81 | const std::string& strip_location_prefix) { |
| 82 | StringPiece location = original_location; |
| 83 | if (!location.starts_with(strip_location_prefix)) { |
| 84 | LOG(ERROR) << filename << " does not start with " << strip_location_prefix; |
| 85 | return NULL; |
| 86 | } |
| 87 | location.remove_prefix(strip_location_prefix.size()); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 88 | int fd = open(filename.c_str(), O_RDONLY); // TODO: scoped_fd |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 89 | if (fd == -1) { |
| 90 | PLOG(ERROR) << "open(\"" << filename << "\", O_RDONLY) failed"; |
| 91 | return NULL; |
| 92 | } |
| 93 | struct stat sbuf; |
| 94 | memset(&sbuf, 0, sizeof(sbuf)); |
| 95 | if (fstat(fd, &sbuf) == -1) { |
| 96 | PLOG(ERROR) << "fstat \"" << filename << "\" failed"; |
| 97 | close(fd); |
| 98 | return NULL; |
| 99 | } |
| 100 | size_t length = sbuf.st_size; |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 101 | UniquePtr<MemMap> map(MemMap::Map(length, PROT_READ, MAP_PRIVATE, fd, 0)); |
| 102 | if (map.get() == NULL) { |
| 103 | LOG(ERROR) << "mmap \"" << filename << "\" failed"; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 104 | close(fd); |
| 105 | return NULL; |
| 106 | } |
| 107 | close(fd); |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 108 | byte* dex_file = map->GetAddress(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 109 | return OpenMemory(dex_file, length, location.ToString(), map.release()); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 112 | const char* DexFile::kClassesDex = "classes.dex"; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 113 | |
| 114 | class LockedFd { |
| 115 | public: |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 116 | static LockedFd* CreateAndLock(std::string& name, mode_t mode) { |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 117 | int fd = open(name.c_str(), O_CREAT | O_RDWR, mode); |
| 118 | if (fd == -1) { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 119 | PLOG(ERROR) << "Failed to open file '" << name << "'"; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 120 | return NULL; |
| 121 | } |
| 122 | fchmod(fd, mode); |
| 123 | |
| 124 | LOG(INFO) << "locking file " << name << " (fd=" << fd << ")"; |
| 125 | int result = flock(fd, LOCK_EX | LOCK_NB); |
| 126 | if (result == -1) { |
| 127 | LOG(WARNING) << "sleeping while locking file " << name; |
| 128 | result = flock(fd, LOCK_EX); |
| 129 | } |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 130 | if (result == -1) { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 131 | PLOG(ERROR) << "Failed to lock file '" << name << "'"; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 132 | close(fd); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 133 | return NULL; |
| 134 | } |
| 135 | return new LockedFd(fd); |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 136 | } |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 137 | |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 138 | int GetFd() const { |
| 139 | return fd_; |
| 140 | } |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 141 | |
| 142 | ~LockedFd() { |
| 143 | if (fd_ != -1) { |
| 144 | int result = flock(fd_, LOCK_UN); |
| 145 | if (result == -1) { |
| 146 | PLOG(WARNING) << "flock(" << fd_ << ", LOCK_UN) failed"; |
| 147 | } |
| 148 | close(fd_); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | private: |
Elliott Hughes | a51a3dd | 2011-10-17 15:19:26 -0700 | [diff] [blame] | 153 | explicit LockedFd(int fd) : fd_(fd) {} |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 154 | |
| 155 | int fd_; |
| 156 | }; |
| 157 | |
| 158 | class TmpFile { |
| 159 | public: |
Elliott Hughes | a51a3dd | 2011-10-17 15:19:26 -0700 | [diff] [blame] | 160 | explicit TmpFile(const std::string& name) : name_(name) {} |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 161 | ~TmpFile() { |
| 162 | unlink(name_.c_str()); |
| 163 | } |
| 164 | private: |
| 165 | const std::string name_; |
| 166 | }; |
| 167 | |
| 168 | // Open classes.dex from within a .zip, .jar, .apk, ... |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 169 | const DexFile* DexFile::OpenZip(const std::string& filename, |
| 170 | const std::string& strip_location_prefix) { |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 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 | |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 198 | UniquePtr<char[]> resolved(new char[PATH_MAX]); |
| 199 | char* absolute_path = realpath(filename.c_str(), resolved.get()); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 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 | } |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 205 | std::string cache_path_tmp(GetArtCacheFilenameOrDie(absolute_path)); |
| 206 | cache_path_tmp.push_back('@'); |
| 207 | cache_path_tmp.append(kClassesDex); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 208 | // Example cache_path_tmp = /data/art-cache/parent@dir@foo.jar@classes.dex |
| 209 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 210 | UniquePtr<ZipArchive> zip_archive(ZipArchive::Open(filename)); |
| 211 | if (zip_archive.get() == NULL) { |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 212 | LOG(ERROR) << "Failed to open " << filename << " when looking for classes.dex"; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 213 | return NULL; |
| 214 | } |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 215 | UniquePtr<ZipEntry> zip_entry(zip_archive->Find(kClassesDex)); |
| 216 | if (zip_entry.get() == NULL) { |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 217 | LOG(ERROR) << "Failed to find classes.dex within " << filename; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 218 | return NULL; |
| 219 | } |
| 220 | |
| 221 | std::string cache_path = StringPrintf("%s.%08x", cache_path_tmp.c_str(), zip_entry->GetCrc32()); |
| 222 | // Example cache_path = /data/art-cache/parent@dir@foo.jar@classes.dex.1a2b3c4d |
| 223 | |
| 224 | while (true) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 225 | if (OS::FileExists(cache_path.c_str())) { |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 226 | const DexFile* cached_dex_file = DexFile::OpenFile(cache_path, |
| 227 | filename, |
| 228 | strip_location_prefix); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 229 | if (cached_dex_file != NULL) { |
| 230 | return cached_dex_file; |
| 231 | } |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | // Try to open the temporary cache file, grabbing an exclusive |
| 235 | // lock. If somebody else is working on it, we'll block here until |
| 236 | // they complete. Because we're waiting on an external resource, |
| 237 | // we go into native mode. |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 238 | // Note that self can be NULL if we're parsing the bootclasspath |
Elliott Hughes | 40ef99e | 2011-08-11 17:44:34 -0700 | [diff] [blame] | 239 | // during JNI_CreateJavaVM. |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 240 | Thread* self = Thread::Current(); |
| 241 | UniquePtr<ScopedThreadStateChange> state_changer; |
| 242 | if (self != NULL) { |
| 243 | state_changer.reset(new ScopedThreadStateChange(self, Thread::kNative)); |
Elliott Hughes | 40ef99e | 2011-08-11 17:44:34 -0700 | [diff] [blame] | 244 | } |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 245 | UniquePtr<LockedFd> fd(LockedFd::CreateAndLock(cache_path_tmp, 0644)); |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 246 | state_changer.reset(NULL); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 247 | if (fd.get() == NULL) { |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 248 | PLOG(ERROR) << "Failed to lock file '" << cache_path_tmp << "'"; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 249 | return NULL; |
| 250 | } |
| 251 | |
| 252 | // Check to see if the fd we opened and locked matches the file in |
| 253 | // the filesystem. If they don't, then somebody else unlinked |
| 254 | // ours and created a new file, and we need to use that one |
| 255 | // instead. (If we caught them between the unlink and the create, |
| 256 | // we'll get an ENOENT from the file stat.) |
| 257 | struct stat fd_stat; |
| 258 | int fd_stat_result = fstat(fd->GetFd(), &fd_stat); |
| 259 | if (fd_stat_result == -1) { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 260 | PLOG(ERROR) << "Failed to stat open file '" << cache_path_tmp << "'"; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 261 | return NULL; |
| 262 | } |
| 263 | struct stat file_stat; |
| 264 | int file_stat_result = stat(cache_path_tmp.c_str(), &file_stat); |
| 265 | if (file_stat_result == -1 || |
| 266 | fd_stat.st_dev != file_stat.st_dev || fd_stat.st_ino != file_stat.st_ino) { |
| 267 | LOG(WARNING) << "our open cache file is stale; sleeping and retrying"; |
| 268 | usleep(250 * 1000); // if something is hosed, don't peg machine |
| 269 | continue; |
| 270 | } |
| 271 | |
| 272 | // We have the correct file open and locked. Extract classes.dex |
| 273 | TmpFile tmp_file(cache_path_tmp); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 274 | UniquePtr<File> file(OS::FileFromFd(cache_path_tmp.c_str(), fd->GetFd())); |
| 275 | if (file.get() == NULL) { |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 276 | LOG(ERROR) << "Failed to create file for '" << cache_path_tmp << "'"; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 277 | return NULL; |
| 278 | } |
| 279 | bool success = zip_entry->Extract(*file); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 280 | if (!success) { |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 281 | LOG(ERROR) << "Failed to extract classes.dex to '" << cache_path_tmp << "'"; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 282 | return NULL; |
| 283 | } |
| 284 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 285 | // TODO: restat and check length against zip_entry->GetUncompressedLength()? |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 286 | |
| 287 | // Compute checksum and compare to zip. If things look okay, rename from tmp. |
| 288 | off_t lseek_result = lseek(fd->GetFd(), 0, SEEK_SET); |
| 289 | if (lseek_result == -1) { |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 290 | PLOG(ERROR) << "Failed to seek to start of '" << cache_path_tmp << "'"; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 291 | return NULL; |
| 292 | } |
| 293 | const size_t kBufSize = 32768; |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 294 | UniquePtr<uint8_t[]> buf(new uint8_t[kBufSize]); |
| 295 | if (buf.get() == NULL) { |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 296 | LOG(ERROR) << "Failed to allocate buffer to checksum '" << cache_path_tmp << "'"; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 297 | return NULL; |
| 298 | } |
| 299 | uint32_t computed_crc = crc32(0L, Z_NULL, 0); |
| 300 | while (true) { |
| 301 | 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] | 302 | if (bytes_read == -1) { |
| 303 | PLOG(ERROR) << "Problem computing CRC of '" << cache_path_tmp << "'"; |
| 304 | return NULL; |
| 305 | } |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 306 | if (bytes_read == 0) { |
| 307 | break; |
| 308 | } |
| 309 | computed_crc = crc32(computed_crc, buf.get(), bytes_read); |
| 310 | } |
| 311 | if (computed_crc != zip_entry->GetCrc32()) { |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 312 | LOG(ERROR) << "Failed to validate checksum for '" << cache_path_tmp << "'"; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 313 | return NULL; |
| 314 | } |
| 315 | int rename_result = rename(cache_path_tmp.c_str(), cache_path.c_str()); |
| 316 | if (rename_result == -1) { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 317 | PLOG(ERROR) << "Failed to install dex cache file '" << cache_path << "'" |
Brian Carlstrom | 0024d6c | 2011-08-09 08:26:12 -0700 | [diff] [blame] | 318 | << " from '" << cache_path_tmp << "'"; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 319 | unlink(cache_path.c_str()); |
| 320 | } |
| 321 | } |
| 322 | // NOTREACHED |
| 323 | } |
| 324 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 325 | const DexFile* DexFile::OpenMemory(const byte* dex_bytes, size_t length, |
| 326 | const std::string& location, MemMap* mem_map) { |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 327 | UniquePtr<DexFile> dex_file(new DexFile(dex_bytes, length, location, mem_map)); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 328 | if (!dex_file->Init()) { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 329 | return NULL; |
| 330 | } else { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 331 | return dex_file.release(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
Jesse Wilson | 6bf1915 | 2011-09-29 13:12:33 -0400 | [diff] [blame] | 335 | DexFile::~DexFile() { |
Elliott Hughes | 8cef0b8 | 2011-10-11 19:24:00 -0700 | [diff] [blame] | 336 | // We don't call DeleteGlobalRef on dex_object_ because we're only called by DestroyJavaVM, and |
| 337 | // that's only called after DetachCurrentThread, which means there's no JNIEnv. We could |
| 338 | // re-attach, but cleaning up these global references is not obviously useful. It's not as if |
| 339 | // the global reference table is otherwise empty! |
Jesse Wilson | 6bf1915 | 2011-09-29 13:12:33 -0400 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | jobject DexFile::GetDexObject(JNIEnv* env) const { |
| 343 | MutexLock mu(dex_object_lock_); |
| 344 | if (dex_object_ != NULL) { |
| 345 | return dex_object_; |
| 346 | } |
| 347 | |
| 348 | void* address = const_cast<void*>(reinterpret_cast<const void*>(base_)); |
| 349 | jobject byte_buffer = env->NewDirectByteBuffer(address, length_); |
| 350 | if (byte_buffer == NULL) { |
| 351 | return NULL; |
| 352 | } |
| 353 | |
| 354 | jclass c = env->FindClass("com/android/dex/Dex"); |
| 355 | if (c == NULL) { |
| 356 | return NULL; |
| 357 | } |
| 358 | |
| 359 | jmethodID mid = env->GetStaticMethodID(c, "create", "(Ljava/nio/ByteBuffer;)Lcom/android/dex/Dex;"); |
| 360 | if (mid == NULL) { |
| 361 | return NULL; |
| 362 | } |
| 363 | |
| 364 | jvalue args[1]; |
| 365 | args[0].l = byte_buffer; |
| 366 | jobject local = env->CallStaticObjectMethodA(c, mid, args); |
| 367 | if (local == NULL) { |
| 368 | return NULL; |
| 369 | } |
| 370 | |
| 371 | dex_object_ = env->NewGlobalRef(local); |
| 372 | return dex_object_; |
| 373 | } |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 374 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 375 | bool DexFile::Init() { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 376 | InitMembers(); |
| 377 | if (!IsMagicValid()) { |
| 378 | return false; |
| 379 | } |
| 380 | InitIndex(); |
| 381 | return true; |
| 382 | } |
| 383 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 384 | void DexFile::InitMembers() { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 385 | const byte* b = base_; |
| 386 | header_ = reinterpret_cast<const Header*>(b); |
| 387 | const Header* h = header_; |
| 388 | string_ids_ = reinterpret_cast<const StringId*>(b + h->string_ids_off_); |
| 389 | type_ids_ = reinterpret_cast<const TypeId*>(b + h->type_ids_off_); |
| 390 | field_ids_ = reinterpret_cast<const FieldId*>(b + h->field_ids_off_); |
| 391 | method_ids_ = reinterpret_cast<const MethodId*>(b + h->method_ids_off_); |
| 392 | proto_ids_ = reinterpret_cast<const ProtoId*>(b + h->proto_ids_off_); |
| 393 | class_defs_ = reinterpret_cast<const ClassDef*>(b + h->class_defs_off_); |
| 394 | } |
| 395 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 396 | bool DexFile::IsMagicValid() { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 397 | return CheckMagic(header_->magic_); |
| 398 | } |
| 399 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 400 | bool DexFile::CheckMagic(const byte* magic) { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 401 | CHECK(magic != NULL); |
| 402 | if (memcmp(magic, kDexMagic, sizeof(kDexMagic)) != 0) { |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 403 | LOG(ERROR) << "Unrecognized magic number:" |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 404 | << " " << magic[0] |
| 405 | << " " << magic[1] |
| 406 | << " " << magic[2] |
| 407 | << " " << magic[3]; |
| 408 | return false; |
| 409 | } |
| 410 | const byte* version = &magic[sizeof(kDexMagic)]; |
| 411 | if (memcmp(version, kDexMagicVersion, sizeof(kDexMagicVersion)) != 0) { |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 412 | LOG(ERROR) << "Unrecognized version number:" |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 413 | << " " << version[0] |
| 414 | << " " << version[1] |
| 415 | << " " << version[2] |
| 416 | << " " << version[3]; |
| 417 | return false; |
| 418 | } |
| 419 | return true; |
| 420 | } |
| 421 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 422 | uint32_t DexFile::GetVersion() const { |
| 423 | const char* version = reinterpret_cast<const char*>(&GetHeader().magic_[sizeof(kDexMagic)]); |
| 424 | return atoi(version); |
| 425 | } |
| 426 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 427 | int32_t DexFile::GetStringLength(const StringId& string_id) const { |
| 428 | const byte* ptr = base_ + string_id.string_data_off_; |
| 429 | return DecodeUnsignedLeb128(&ptr); |
| 430 | } |
| 431 | |
| 432 | // Returns a pointer to the UTF-8 string data referred to by the given string_id. |
| 433 | const char* DexFile::GetStringDataAndLength(const StringId& string_id, int32_t* length) const { |
| 434 | CHECK(length != NULL); |
| 435 | const byte* ptr = base_ + string_id.string_data_off_; |
| 436 | *length = DecodeUnsignedLeb128(&ptr); |
| 437 | return reinterpret_cast<const char*>(ptr); |
| 438 | } |
| 439 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 440 | void DexFile::InitIndex() { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 441 | CHECK_EQ(index_.size(), 0U); |
| 442 | for (size_t i = 0; i < NumClassDefs(); ++i) { |
| 443 | const ClassDef& class_def = GetClassDef(i); |
| 444 | const char* descriptor = GetClassDescriptor(class_def); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 445 | index_[descriptor] = i; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 449 | bool DexFile::FindClassDefIndex(const StringPiece& descriptor, uint32_t& idx) const { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 450 | Index::const_iterator it = index_.find(descriptor); |
| 451 | if (it == index_.end()) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 452 | return false; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 453 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 454 | idx = it->second; |
| 455 | return true; |
| 456 | } |
| 457 | |
| 458 | const DexFile::ClassDef* DexFile::FindClassDef(const StringPiece& descriptor) const { |
| 459 | uint32_t idx; |
| 460 | if (FindClassDefIndex(descriptor, idx)) { |
| 461 | return &GetClassDef(idx); |
| 462 | } |
| 463 | return NULL; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 466 | const DexFile::FieldId* DexFile::FindFieldId(const DexFile::TypeId& declaring_klass, |
| 467 | const DexFile::StringId& name, |
| 468 | const DexFile::TypeId& type) const { |
| 469 | // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx |
| 470 | const uint16_t class_idx = GetIndexForTypeId(declaring_klass); |
| 471 | const uint32_t name_idx = GetIndexForStringId(name); |
| 472 | const uint16_t type_idx = GetIndexForTypeId(type); |
| 473 | uint32_t lo = 0; |
| 474 | uint32_t hi = NumFieldIds() - 1; |
| 475 | while (hi >= lo) { |
| 476 | uint32_t mid = (hi + lo) / 2; |
| 477 | const DexFile::FieldId& field = GetFieldId(mid); |
| 478 | if (class_idx > field.class_idx_) { |
| 479 | lo = mid + 1; |
| 480 | } else if (class_idx < field.class_idx_) { |
| 481 | hi = mid - 1; |
| 482 | } else { |
| 483 | if (name_idx > field.name_idx_) { |
| 484 | lo = mid + 1; |
| 485 | } else if (name_idx < field.name_idx_) { |
| 486 | hi = mid - 1; |
| 487 | } else { |
| 488 | if (type_idx > field.type_idx_) { |
| 489 | lo = mid + 1; |
| 490 | } else if (type_idx < field.type_idx_) { |
| 491 | hi = mid - 1; |
| 492 | } else { |
| 493 | return &field; |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | return NULL; |
| 499 | } |
| 500 | |
| 501 | const DexFile::MethodId* DexFile::FindMethodId(const DexFile::TypeId& declaring_klass, |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 502 | const DexFile::StringId& name, |
| 503 | const DexFile::ProtoId& signature) const { |
| 504 | // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 505 | const uint16_t class_idx = GetIndexForTypeId(declaring_klass); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 506 | const uint32_t name_idx = GetIndexForStringId(name); |
| 507 | const uint16_t proto_idx = GetIndexForProtoId(signature); |
| 508 | uint32_t lo = 0; |
| 509 | uint32_t hi = NumMethodIds() - 1; |
| 510 | while (hi >= lo) { |
| 511 | uint32_t mid = (hi + lo) / 2; |
| 512 | const DexFile::MethodId& method = GetMethodId(mid); |
| 513 | if (class_idx > method.class_idx_) { |
| 514 | lo = mid + 1; |
| 515 | } else if (class_idx < method.class_idx_) { |
| 516 | hi = mid - 1; |
| 517 | } else { |
| 518 | if (name_idx > method.name_idx_) { |
| 519 | lo = mid + 1; |
| 520 | } else if (name_idx < method.name_idx_) { |
| 521 | hi = mid - 1; |
| 522 | } else { |
| 523 | if (proto_idx > method.proto_idx_) { |
| 524 | lo = mid + 1; |
| 525 | } else if (proto_idx < method.proto_idx_) { |
| 526 | hi = mid - 1; |
| 527 | } else { |
| 528 | return &method; |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | return NULL; |
| 534 | } |
| 535 | |
| 536 | const DexFile::StringId* DexFile::FindStringId(const std::string& string) const { |
| 537 | uint32_t lo = 0; |
| 538 | uint32_t hi = NumStringIds() - 1; |
| 539 | while (hi >= lo) { |
| 540 | uint32_t mid = (hi + lo) / 2; |
| 541 | int32_t length; |
| 542 | const DexFile::StringId& str_id = GetStringId(mid); |
| 543 | const char* str = GetStringDataAndLength(str_id, &length); |
| 544 | int compare = CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(string.c_str(), str); |
| 545 | if (compare > 0) { |
| 546 | lo = mid + 1; |
| 547 | } else if (compare < 0) { |
| 548 | hi = mid - 1; |
| 549 | } else { |
| 550 | return &str_id; |
| 551 | } |
| 552 | } |
| 553 | return NULL; |
| 554 | } |
| 555 | |
| 556 | const DexFile::TypeId* DexFile::FindTypeId(uint32_t string_idx) const { |
| 557 | uint32_t lo = 0; |
| 558 | uint32_t hi = NumTypeIds() - 1; |
| 559 | while (hi >= lo) { |
| 560 | uint32_t mid = (hi + lo) / 2; |
| 561 | const TypeId& type_id = GetTypeId(mid); |
| 562 | if (string_idx > type_id.descriptor_idx_) { |
| 563 | lo = mid + 1; |
| 564 | } else if (string_idx < type_id.descriptor_idx_) { |
| 565 | hi = mid - 1; |
| 566 | } else { |
| 567 | return &type_id; |
| 568 | } |
| 569 | } |
| 570 | return NULL; |
| 571 | } |
| 572 | |
| 573 | const DexFile::ProtoId* DexFile::FindProtoId(uint16_t return_type_idx, |
| 574 | const std::vector<uint16_t>& signature_type_ids) const { |
| 575 | uint32_t lo = 0; |
| 576 | uint32_t hi = NumProtoIds() - 1; |
| 577 | while (hi >= lo) { |
| 578 | uint32_t mid = (hi + lo) / 2; |
| 579 | const DexFile::ProtoId& proto = GetProtoId(mid); |
| 580 | int compare = return_type_idx - proto.return_type_idx_; |
| 581 | if (compare == 0) { |
| 582 | DexFileParameterIterator it(*this, proto); |
| 583 | size_t i = 0; |
| 584 | while (it.HasNext() && i < signature_type_ids.size() && compare == 0) { |
| 585 | compare = signature_type_ids[i] - it.GetTypeId(); |
| 586 | it.Next(); |
| 587 | i++; |
| 588 | } |
| 589 | if (compare == 0) { |
| 590 | if (it.HasNext()) { |
| 591 | compare = -1; |
| 592 | } else if (i < signature_type_ids.size()) { |
| 593 | compare = 1; |
| 594 | } |
| 595 | } |
| 596 | } |
| 597 | if (compare > 0) { |
| 598 | lo = mid + 1; |
| 599 | } else if (compare < 0) { |
| 600 | hi = mid - 1; |
| 601 | } else { |
| 602 | return &proto; |
| 603 | } |
| 604 | } |
| 605 | return NULL; |
| 606 | } |
| 607 | |
| 608 | // Given a signature place the type ids into the given vector |
| 609 | bool DexFile::CreateTypeList(uint16_t* return_type_idx, std::vector<uint16_t>* param_type_idxs, |
| 610 | const std::string& signature) const { |
| 611 | if (signature[0] != '(') { |
| 612 | return false; |
| 613 | } |
| 614 | size_t offset = 1; |
| 615 | size_t end = signature.size(); |
| 616 | bool process_return = false; |
| 617 | while (offset < end) { |
| 618 | char c = signature[offset]; |
| 619 | offset++; |
| 620 | if (c == ')') { |
| 621 | process_return = true; |
| 622 | continue; |
| 623 | } |
| 624 | std::string descriptor; |
| 625 | descriptor += c; |
| 626 | while (c == '[') { // process array prefix |
| 627 | if (offset >= end) { // expect some descriptor following [ |
| 628 | return false; |
| 629 | } |
| 630 | c = signature[offset]; |
| 631 | offset++; |
| 632 | descriptor += c; |
| 633 | } |
| 634 | if (c == 'L') { // process type descriptors |
| 635 | do { |
| 636 | if (offset >= end) { // unexpected early termination of descriptor |
| 637 | return false; |
| 638 | } |
| 639 | c = signature[offset]; |
| 640 | offset++; |
| 641 | descriptor += c; |
| 642 | } while (c != ';'); |
| 643 | } |
| 644 | const DexFile::StringId* string_id = FindStringId(descriptor); |
| 645 | if (string_id == NULL) { |
| 646 | return false; |
| 647 | } |
| 648 | const DexFile::TypeId* type_id = FindTypeId(GetIndexForStringId(*string_id)); |
| 649 | if (type_id == NULL) { |
| 650 | return false; |
| 651 | } |
| 652 | uint16_t type_idx = GetIndexForTypeId(*type_id); |
| 653 | if (!process_return) { |
| 654 | param_type_idxs->push_back(type_idx); |
| 655 | } else { |
| 656 | *return_type_idx = type_idx; |
| 657 | return offset == end; // return true if the signature had reached a sensible end |
| 658 | } |
| 659 | } |
| 660 | return false; // failed to correctly parse return type |
| 661 | } |
| 662 | |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 663 | // Materializes the method descriptor for a method prototype. Method |
| 664 | // descriptors are not stored directly in the dex file. Instead, one |
| 665 | // must assemble the descriptor from references in the prototype. |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 666 | std::string DexFile::CreateMethodSignature(uint32_t proto_idx, int32_t* unicode_length) const { |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 667 | const ProtoId& proto_id = GetProtoId(proto_idx); |
| 668 | std::string descriptor; |
| 669 | descriptor.push_back('('); |
| 670 | const TypeList* type_list = GetProtoParameters(proto_id); |
| 671 | size_t parameter_length = 0; |
| 672 | if (type_list != NULL) { |
| 673 | // A non-zero number of arguments. Append the type names. |
| 674 | for (size_t i = 0; i < type_list->Size(); ++i) { |
| 675 | const TypeItem& type_item = type_list->GetTypeItem(i); |
| 676 | uint32_t type_idx = type_item.type_idx_; |
| 677 | int32_t type_length; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 678 | const char* name = StringByTypeIdx(type_idx, &type_length); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 679 | parameter_length += type_length; |
| 680 | descriptor.append(name); |
| 681 | } |
| 682 | } |
| 683 | descriptor.push_back(')'); |
| 684 | uint32_t return_type_idx = proto_id.return_type_idx_; |
| 685 | int32_t return_type_length; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 686 | const char* name = StringByTypeIdx(return_type_idx, &return_type_length); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 687 | descriptor.append(name); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 688 | if (unicode_length != NULL) { |
| 689 | *unicode_length = parameter_length + return_type_length + 2; // 2 for ( and ) |
| 690 | } |
Elliott Hughes | 0c424cb | 2011-08-26 10:16:25 -0700 | [diff] [blame] | 691 | return descriptor; |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 692 | } |
| 693 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 694 | |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 695 | 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] | 696 | // For native method, lineno should be -2 to indicate it is native. Note that |
| 697 | // "line number == -2" is how libcore tells from StackTraceElement. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 698 | if (method->GetCodeItemOffset() == 0) { |
Shih-wei Liao | ff0f9be | 2011-08-29 15:43:53 -0700 | [diff] [blame] | 699 | return -2; |
| 700 | } |
| 701 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 702 | const CodeItem* code_item = GetCodeItem(method->GetCodeItemOffset()); |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 703 | DCHECK(code_item != NULL); |
| 704 | |
| 705 | // A method with no line number info should return -1 |
| 706 | LineNumFromPcContext context(rel_pc, -1); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 707 | DecodeDebugInfo(code_item, method, LineNumForPcCb, NULL, &context); |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 708 | return context.line_num_; |
| 709 | } |
| 710 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 711 | int32_t DexFile::FindCatchHandlerOffset(const CodeItem &code_item, int32_t tries_size, |
| 712 | uint32_t address){ |
| 713 | // Note: Signed type is important for max and min. |
| 714 | int32_t min = 0; |
| 715 | int32_t max = tries_size - 1; |
| 716 | |
| 717 | while (max >= min) { |
| 718 | int32_t mid = (min + max) / 2; |
| 719 | const TryItem* pTry = DexFile::GetTryItems(code_item, mid); |
| 720 | uint32_t start = pTry->start_addr_; |
| 721 | if (address < start) { |
| 722 | max = mid - 1; |
| 723 | } else { |
| 724 | uint32_t end = start + pTry->insn_count_; |
| 725 | if (address >= end) { |
| 726 | min = mid + 1; |
| 727 | } else { // We have a winner! |
| 728 | return (int32_t) pTry->handler_off_; |
| 729 | } |
| 730 | } |
| 731 | } |
| 732 | // No match. |
| 733 | return -1; |
| 734 | } |
| 735 | |
| 736 | void DexFile::DecodeDebugInfo0(const CodeItem* code_item, const Method* method, |
| 737 | DexDebugNewPositionCb posCb, DexDebugNewLocalCb local_cb, |
| 738 | void* cnxt, const byte* stream, LocalInfo* local_in_reg) const { |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 739 | uint32_t line = DecodeUnsignedLeb128(&stream); |
| 740 | uint32_t parameters_size = DecodeUnsignedLeb128(&stream); |
| 741 | uint16_t arg_reg = code_item->registers_size_ - code_item->ins_size_; |
| 742 | uint32_t address = 0; |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 743 | bool need_locals = (local_cb != NULL); |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 744 | |
| 745 | if (!method->IsStatic()) { |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 746 | if (need_locals) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 747 | std::string descriptor = method->GetDeclaringClass()->GetDescriptor()->ToModifiedUtf8(); |
| 748 | const ClassDef* class_def = FindClassDef(descriptor); |
| 749 | CHECK(class_def != NULL) << descriptor; |
| 750 | local_in_reg[arg_reg].name_ = "this"; |
| 751 | local_in_reg[arg_reg].descriptor_ = GetClassDescriptor(*class_def); |
Elliott Hughes | 392b124 | 2011-11-30 13:55:50 -0800 | [diff] [blame^] | 752 | local_in_reg[arg_reg].signature_ = NULL; |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 753 | local_in_reg[arg_reg].start_address_ = 0; |
| 754 | local_in_reg[arg_reg].is_live_ = true; |
| 755 | } |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 756 | arg_reg++; |
| 757 | } |
| 758 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 759 | DexFileParameterIterator it(*this, GetProtoId(method->GetProtoIdx())); |
| 760 | for (uint32_t i = 0; i < parameters_size && it.HasNext(); ++i, it.Next()) { |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 761 | if (arg_reg >= code_item->registers_size_) { |
jeffhao | f872887 | 2011-10-28 19:11:13 -0700 | [diff] [blame] | 762 | LOG(ERROR) << "invalid stream - arg reg >= reg size (" << arg_reg |
| 763 | << " >= " << code_item->registers_size_ << ")"; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 764 | return; |
| 765 | } |
Elliott Hughes | 392b124 | 2011-11-30 13:55:50 -0800 | [diff] [blame^] | 766 | uint32_t id = DecodeUnsignedLeb128P1(&stream); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 767 | const char* descriptor = it.GetDescriptor(); |
Elliott Hughes | 392b124 | 2011-11-30 13:55:50 -0800 | [diff] [blame^] | 768 | if (need_locals && id != kDexNoIndex) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 769 | const char* name = StringDataByIdx(id); |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 770 | local_in_reg[arg_reg].name_ = name; |
| 771 | local_in_reg[arg_reg].descriptor_ = descriptor; |
Elliott Hughes | 392b124 | 2011-11-30 13:55:50 -0800 | [diff] [blame^] | 772 | local_in_reg[arg_reg].signature_ = NULL; |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 773 | local_in_reg[arg_reg].start_address_ = address; |
| 774 | local_in_reg[arg_reg].is_live_ = true; |
| 775 | } |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 776 | switch (*descriptor) { |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 777 | case 'D': |
| 778 | case 'J': |
| 779 | arg_reg += 2; |
| 780 | break; |
| 781 | default: |
| 782 | arg_reg += 1; |
| 783 | break; |
| 784 | } |
| 785 | } |
| 786 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 787 | if (it.HasNext()) { |
jeffhao | f872887 | 2011-10-28 19:11:13 -0700 | [diff] [blame] | 788 | LOG(ERROR) << "invalid stream - problem with parameter iterator"; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 789 | return; |
| 790 | } |
| 791 | |
| 792 | for (;;) { |
| 793 | uint8_t opcode = *stream++; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 794 | uint16_t reg; |
jeffhao | f872887 | 2011-10-28 19:11:13 -0700 | [diff] [blame] | 795 | uint16_t name_idx; |
| 796 | uint16_t descriptor_idx; |
| 797 | uint16_t signature_idx = 0; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 798 | |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 799 | switch (opcode) { |
| 800 | case DBG_END_SEQUENCE: |
| 801 | return; |
| 802 | |
| 803 | case DBG_ADVANCE_PC: |
| 804 | address += DecodeUnsignedLeb128(&stream); |
| 805 | break; |
| 806 | |
| 807 | case DBG_ADVANCE_LINE: |
Shih-wei Liao | 8a05d27 | 2011-10-15 18:45:43 -0700 | [diff] [blame] | 808 | line += DecodeSignedLeb128(&stream); |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 809 | break; |
| 810 | |
| 811 | case DBG_START_LOCAL: |
| 812 | case DBG_START_LOCAL_EXTENDED: |
| 813 | reg = DecodeUnsignedLeb128(&stream); |
| 814 | if (reg > code_item->registers_size_) { |
jeffhao | f872887 | 2011-10-28 19:11:13 -0700 | [diff] [blame] | 815 | LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > " |
| 816 | << code_item->registers_size_ << ")"; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 817 | return; |
| 818 | } |
| 819 | |
jeffhao | f872887 | 2011-10-28 19:11:13 -0700 | [diff] [blame] | 820 | name_idx = DecodeUnsignedLeb128P1(&stream); |
| 821 | descriptor_idx = DecodeUnsignedLeb128P1(&stream); |
| 822 | if (opcode == DBG_START_LOCAL_EXTENDED) { |
| 823 | signature_idx = DecodeUnsignedLeb128P1(&stream); |
| 824 | } |
| 825 | |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 826 | // Emit what was previously there, if anything |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 827 | if (need_locals) { |
| 828 | InvokeLocalCbIfLive(cnxt, reg, address, local_in_reg, local_cb); |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 829 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 830 | local_in_reg[reg].name_ = StringDataByIdx(name_idx); |
| 831 | local_in_reg[reg].descriptor_ = StringByTypeIdx(descriptor_idx); |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 832 | if (opcode == DBG_START_LOCAL_EXTENDED) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 833 | local_in_reg[reg].signature_ = StringDataByIdx(signature_idx); |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 834 | } |
| 835 | local_in_reg[reg].start_address_ = address; |
| 836 | local_in_reg[reg].is_live_ = true; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 837 | } |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 838 | break; |
| 839 | |
| 840 | case DBG_END_LOCAL: |
| 841 | reg = DecodeUnsignedLeb128(&stream); |
| 842 | if (reg > code_item->registers_size_) { |
jeffhao | f872887 | 2011-10-28 19:11:13 -0700 | [diff] [blame] | 843 | LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > " |
| 844 | << code_item->registers_size_ << ")"; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 845 | return; |
| 846 | } |
| 847 | |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 848 | if (need_locals) { |
| 849 | InvokeLocalCbIfLive(cnxt, reg, address, local_in_reg, local_cb); |
| 850 | local_in_reg[reg].is_live_ = false; |
| 851 | } |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 852 | break; |
| 853 | |
| 854 | case DBG_RESTART_LOCAL: |
| 855 | reg = DecodeUnsignedLeb128(&stream); |
| 856 | if (reg > code_item->registers_size_) { |
jeffhao | f872887 | 2011-10-28 19:11:13 -0700 | [diff] [blame] | 857 | LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > " |
| 858 | << code_item->registers_size_ << ")"; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 859 | return; |
| 860 | } |
| 861 | |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 862 | if (need_locals) { |
| 863 | if (local_in_reg[reg].name_ == NULL || local_in_reg[reg].descriptor_ == NULL) { |
jeffhao | f872887 | 2011-10-28 19:11:13 -0700 | [diff] [blame] | 864 | LOG(ERROR) << "invalid stream - no name or descriptor"; |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 865 | return; |
| 866 | } |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 867 | |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 868 | // If the register is live, the "restart" is superfluous, |
| 869 | // and we don't want to mess with the existing start address. |
| 870 | if (!local_in_reg[reg].is_live_) { |
| 871 | local_in_reg[reg].start_address_ = address; |
| 872 | local_in_reg[reg].is_live_ = true; |
| 873 | } |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 874 | } |
| 875 | break; |
| 876 | |
| 877 | case DBG_SET_PROLOGUE_END: |
| 878 | case DBG_SET_EPILOGUE_BEGIN: |
| 879 | case DBG_SET_FILE: |
| 880 | break; |
| 881 | |
Shih-wei Liao | 8e1b4ff | 2011-10-15 15:43:51 -0700 | [diff] [blame] | 882 | default: { |
| 883 | int adjopcode = opcode - DBG_FIRST_SPECIAL; |
| 884 | |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 885 | address += adjopcode / DBG_LINE_RANGE; |
| 886 | line += DBG_LINE_BASE + (adjopcode % DBG_LINE_RANGE); |
| 887 | |
| 888 | if (posCb != NULL) { |
| 889 | if (posCb(cnxt, address, line)) { |
| 890 | // early exit |
| 891 | return; |
| 892 | } |
| 893 | } |
| 894 | break; |
Shih-wei Liao | 8e1b4ff | 2011-10-15 15:43:51 -0700 | [diff] [blame] | 895 | } |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 896 | } |
| 897 | } |
| 898 | } |
| 899 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 900 | void DexFile::DecodeDebugInfo(const CodeItem* code_item, const art::Method* method, |
| 901 | DexDebugNewPositionCb posCb, DexDebugNewLocalCb local_cb, |
| 902 | void* cnxt) const { |
| 903 | const byte* stream = GetDebugInfoStream(code_item); |
| 904 | LocalInfo local_in_reg[code_item->registers_size_]; |
| 905 | |
| 906 | if (stream != NULL) { |
| 907 | DecodeDebugInfo0(code_item, method, posCb, local_cb, cnxt, stream, local_in_reg); |
| 908 | } |
| 909 | for (int reg = 0; reg < code_item->registers_size_; reg++) { |
| 910 | InvokeLocalCbIfLive(cnxt, reg, code_item->insns_size_in_code_units_, local_in_reg, local_cb); |
| 911 | } |
| 912 | } |
| 913 | |
| 914 | bool DexFile::LineNumForPcCb(void* cnxt, uint32_t address, uint32_t line_num) { |
| 915 | LineNumFromPcContext* context = (LineNumFromPcContext*) cnxt; |
| 916 | |
| 917 | // We know that this callback will be called in |
| 918 | // ascending address order, so keep going until we find |
| 919 | // a match or we've just gone past it. |
| 920 | if (address > context->address_) { |
| 921 | // The line number from the previous positions callback |
| 922 | // wil be the final result. |
| 923 | return true; |
| 924 | } else { |
| 925 | context->line_num_ = line_num; |
| 926 | return address == context->address_; |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | // Decodes the header section from the class data bytes. |
| 931 | void ClassDataItemIterator::ReadClassDataHeader() { |
| 932 | CHECK(ptr_pos_ != NULL); |
| 933 | header_.static_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_); |
| 934 | header_.instance_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_); |
| 935 | header_.direct_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_); |
| 936 | header_.virtual_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_); |
| 937 | } |
| 938 | |
| 939 | void ClassDataItemIterator::ReadClassDataField() { |
| 940 | field_.field_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_); |
| 941 | field_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_); |
| 942 | } |
| 943 | |
| 944 | void ClassDataItemIterator::ReadClassDataMethod() { |
| 945 | method_.method_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_); |
| 946 | method_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_); |
| 947 | method_.code_off_ = DecodeUnsignedLeb128(&ptr_pos_); |
| 948 | } |
| 949 | |
| 950 | // Read a signed integer. "zwidth" is the zero-based byte count. |
| 951 | static int32_t ReadSignedInt(const byte* ptr, int zwidth) { |
| 952 | int32_t val = 0; |
| 953 | for (int i = zwidth; i >= 0; --i) { |
| 954 | val = ((uint32_t)val >> 8) | (((int32_t)*ptr++) << 24); |
| 955 | } |
| 956 | val >>= (3 - zwidth) * 8; |
| 957 | return val; |
| 958 | } |
| 959 | |
| 960 | // Read an unsigned integer. "zwidth" is the zero-based byte count, |
| 961 | // "fill_on_right" indicates which side we want to zero-fill from. |
| 962 | static uint32_t ReadUnsignedInt(const byte* ptr, int zwidth, bool fill_on_right) { |
| 963 | uint32_t val = 0; |
| 964 | if (!fill_on_right) { |
| 965 | for (int i = zwidth; i >= 0; --i) { |
| 966 | val = (val >> 8) | (((uint32_t)*ptr++) << 24); |
| 967 | } |
| 968 | val >>= (3 - zwidth) * 8; |
| 969 | } else { |
| 970 | for (int i = zwidth; i >= 0; --i) { |
| 971 | val = (val >> 8) | (((uint32_t)*ptr++) << 24); |
| 972 | } |
| 973 | } |
| 974 | return val; |
| 975 | } |
| 976 | |
| 977 | // Read a signed long. "zwidth" is the zero-based byte count. |
| 978 | static int64_t ReadSignedLong(const byte* ptr, int zwidth) { |
| 979 | int64_t val = 0; |
| 980 | for (int i = zwidth; i >= 0; --i) { |
| 981 | val = ((uint64_t)val >> 8) | (((int64_t)*ptr++) << 56); |
| 982 | } |
| 983 | val >>= (7 - zwidth) * 8; |
| 984 | return val; |
| 985 | } |
| 986 | |
| 987 | // Read an unsigned long. "zwidth" is the zero-based byte count, |
| 988 | // "fill_on_right" indicates which side we want to zero-fill from. |
| 989 | static uint64_t ReadUnsignedLong(const byte* ptr, int zwidth, bool fill_on_right) { |
| 990 | uint64_t val = 0; |
| 991 | if (!fill_on_right) { |
| 992 | for (int i = zwidth; i >= 0; --i) { |
| 993 | val = (val >> 8) | (((uint64_t)*ptr++) << 56); |
| 994 | } |
| 995 | val >>= (7 - zwidth) * 8; |
| 996 | } else { |
| 997 | for (int i = zwidth; i >= 0; --i) { |
| 998 | val = (val >> 8) | (((uint64_t)*ptr++) << 56); |
| 999 | } |
| 1000 | } |
| 1001 | return val; |
| 1002 | } |
| 1003 | |
| 1004 | EncodedStaticFieldValueIterator::EncodedStaticFieldValueIterator(const DexFile& dex_file, |
| 1005 | DexCache* dex_cache, ClassLinker* linker, const DexFile::ClassDef& class_def) : |
| 1006 | dex_file_(dex_file), dex_cache_(dex_cache), linker_(linker), array_size_(), pos_(-1), type_(0) { |
| 1007 | ptr_ = dex_file.GetEncodedStaticFieldValuesArray(class_def); |
| 1008 | if (ptr_ == NULL) { |
| 1009 | array_size_ = 0; |
| 1010 | } else { |
| 1011 | array_size_ = DecodeUnsignedLeb128(&ptr_); |
| 1012 | } |
| 1013 | if (array_size_ > 0) { |
| 1014 | Next(); |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | void EncodedStaticFieldValueIterator::Next() { |
| 1019 | pos_++; |
| 1020 | if (pos_ >= array_size_) { |
| 1021 | return; |
| 1022 | } |
| 1023 | byte value_type = *ptr_++; |
| 1024 | byte value_arg = value_type >> kEncodedValueArgShift; |
| 1025 | size_t width = value_arg + 1; // assume and correct later |
| 1026 | type_ = value_type & kEncodedValueTypeMask; |
| 1027 | switch (type_) { |
| 1028 | case kBoolean: |
| 1029 | jval_.i = (value_arg != 0) ? 1 : 0; |
| 1030 | width = 0; |
| 1031 | break; |
| 1032 | case kByte: |
| 1033 | jval_.i = ReadSignedInt(ptr_, value_arg); |
| 1034 | CHECK(IsInt(8, jval_.i)); |
| 1035 | break; |
| 1036 | case kShort: |
| 1037 | jval_.i = ReadSignedInt(ptr_, value_arg); |
| 1038 | CHECK(IsInt(16, jval_.i)); |
| 1039 | break; |
| 1040 | case kChar: |
| 1041 | jval_.i = ReadUnsignedInt(ptr_, value_arg, false); |
| 1042 | CHECK(IsUint(16, jval_.i)); |
| 1043 | break; |
| 1044 | case kInt: |
| 1045 | jval_.i = ReadSignedInt(ptr_, value_arg); |
| 1046 | break; |
| 1047 | case kLong: |
| 1048 | jval_.j = ReadSignedLong(ptr_, value_arg); |
| 1049 | break; |
| 1050 | case kFloat: |
| 1051 | jval_.i = ReadUnsignedInt(ptr_, value_arg, true); |
| 1052 | break; |
| 1053 | case kDouble: |
| 1054 | jval_.j = ReadUnsignedLong(ptr_, value_arg, true); |
| 1055 | break; |
| 1056 | case kString: |
| 1057 | case kType: |
| 1058 | case kMethod: |
| 1059 | case kEnum: |
| 1060 | jval_.i = ReadUnsignedInt(ptr_, value_arg, false); |
| 1061 | break; |
| 1062 | case kField: |
| 1063 | case kArray: |
| 1064 | case kAnnotation: |
| 1065 | UNIMPLEMENTED(FATAL) << ": type " << type_; |
| 1066 | break; |
| 1067 | case kNull: |
| 1068 | jval_.l = NULL; |
| 1069 | width = 0; |
| 1070 | break; |
| 1071 | default: |
| 1072 | LOG(FATAL) << "Unreached"; |
| 1073 | } |
| 1074 | ptr_ += width; |
| 1075 | } |
| 1076 | |
| 1077 | void EncodedStaticFieldValueIterator::ReadValueToField(Field* field) const { |
| 1078 | switch (type_) { |
| 1079 | case kBoolean: field->SetBoolean(NULL, jval_.z); break; |
| 1080 | case kByte: field->SetByte(NULL, jval_.b); break; |
| 1081 | case kShort: field->SetShort(NULL, jval_.s); break; |
| 1082 | case kChar: field->SetChar(NULL, jval_.c); break; |
| 1083 | case kInt: field->SetInt(NULL, jval_.i); break; |
| 1084 | case kLong: field->SetLong(NULL, jval_.j); break; |
| 1085 | case kFloat: field->SetFloat(NULL, jval_.f); break; |
| 1086 | case kDouble: field->SetDouble(NULL, jval_.d); break; |
| 1087 | case kNull: field->SetObject(NULL, NULL); break; |
| 1088 | case kString: { |
| 1089 | String* resolved = linker_->ResolveString(dex_file_, jval_.i, dex_cache_); |
| 1090 | field->SetObject(NULL, resolved); |
| 1091 | break; |
| 1092 | } |
| 1093 | default: UNIMPLEMENTED(FATAL) << ": type " << type_; |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | CatchHandlerIterator::CatchHandlerIterator(const DexFile::CodeItem& code_item, uint32_t address) { |
| 1098 | handler_.address_ = -1; |
| 1099 | int32_t offset = -1; |
| 1100 | |
| 1101 | // Short-circuit the overwhelmingly common cases. |
| 1102 | switch (code_item.tries_size_) { |
| 1103 | case 0: |
| 1104 | break; |
| 1105 | case 1: { |
| 1106 | const DexFile::TryItem* tries = DexFile::GetTryItems(code_item, 0); |
| 1107 | uint32_t start = tries->start_addr_; |
| 1108 | if (address >= start) { |
| 1109 | uint32_t end = start + tries->insn_count_; |
| 1110 | if (address < end) { |
| 1111 | offset = tries->handler_off_; |
| 1112 | } |
| 1113 | } |
| 1114 | break; |
| 1115 | } |
| 1116 | default: |
| 1117 | offset = DexFile::FindCatchHandlerOffset(code_item, code_item.tries_size_, address); |
| 1118 | } |
| 1119 | if (offset >= 0) { |
| 1120 | const byte* handler_data = DexFile::GetCatchHandlerData(code_item, offset); |
| 1121 | Init(handler_data); |
| 1122 | } else { |
| 1123 | // Not found, initialize as empty |
| 1124 | current_data_ = NULL; |
| 1125 | remaining_count_ = -1; |
| 1126 | catch_all_ = false; |
| 1127 | DCHECK(!HasNext()); |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | void CatchHandlerIterator::Init(const byte* handler_data) { |
| 1132 | current_data_ = handler_data; |
| 1133 | remaining_count_ = DecodeSignedLeb128(¤t_data_); |
| 1134 | |
| 1135 | // If remaining_count_ is non-positive, then it is the negative of |
| 1136 | // the number of catch types, and the catches are followed by a |
| 1137 | // catch-all handler. |
| 1138 | if (remaining_count_ <= 0) { |
| 1139 | catch_all_ = true; |
| 1140 | remaining_count_ = -remaining_count_; |
| 1141 | } else { |
| 1142 | catch_all_ = false; |
| 1143 | } |
| 1144 | Next(); |
| 1145 | } |
| 1146 | |
| 1147 | void CatchHandlerIterator::Next() { |
| 1148 | if (remaining_count_ > 0) { |
| 1149 | handler_.type_idx_ = DecodeUnsignedLeb128(¤t_data_); |
| 1150 | handler_.address_ = DecodeUnsignedLeb128(¤t_data_); |
| 1151 | remaining_count_--; |
| 1152 | return; |
| 1153 | } |
| 1154 | |
| 1155 | if (catch_all_) { |
| 1156 | handler_.type_idx_ = DexFile::kDexNoIndex16; |
| 1157 | handler_.address_ = DecodeUnsignedLeb128(¤t_data_); |
| 1158 | catch_all_ = false; |
| 1159 | return; |
| 1160 | } |
| 1161 | |
| 1162 | // no more handler |
| 1163 | remaining_count_ = -1; |
| 1164 | } |
| 1165 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1166 | } // namespace art |