summaryrefslogtreecommitdiffstats
path: root/src/raw_dex_file.cc
blob: 2d97a9ab47ca192f924fab45af597fc05bf58ae4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Copyright 2011 Google Inc. All Rights Reserved.

#include "src/raw_dex_file.h"

#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <map>

#include "src/base64.h"
#include "src/globals.h"
#include "src/logging.h"
#include "src/object.h"
#include "src/scoped_ptr.h"
#include "src/utils.h"

namespace art {

const byte RawDexFile::kDexMagic[] = { 'd', 'e', 'x', '\n' };
const byte RawDexFile::kDexMagicVersion[] = { '0', '3', '5', '\0' };

// Helper class to deallocate mmap-backed .dex files.
class MmapCloser : public RawDexFile::Closer {
 public:
  MmapCloser(void* addr, size_t length) : addr_(addr), length_(length) {
    CHECK(addr != NULL);
  };
  virtual ~MmapCloser() {
    if (munmap(addr_, length_) == -1) {
      LG << "munmap: " << strerror(errno);  // TODO: PLOG
    }
  }
 private:
  void* addr_;
  size_t length_;
};

// Helper class for deallocating new/delete-backed .dex files.
class PtrCloser : public RawDexFile::Closer {
 public:
  PtrCloser(byte* addr) : addr_(addr) {};
  virtual ~PtrCloser() { delete[] addr_; }
 private:
  byte* addr_;
};

RawDexFile::Closer::~Closer() {}

RawDexFile* RawDexFile::OpenFile(const char* filename) {
  CHECK(filename != NULL);
  int fd = open(filename, O_RDONLY);  // TODO: scoped_fd
  if (fd == -1) {
    LG << "open: " << strerror(errno);  // TODO: PLOG
    return NULL;
  }
  struct stat sbuf;
  memset(&sbuf, 0, sizeof(sbuf));
  if (fstat(fd, &sbuf) == -1) {
    LG << "fstat: " << strerror(errno);  // TODO: PLOG
    close(fd);
    return NULL;
  }
  size_t length = sbuf.st_size;
  void* addr = mmap(NULL, length, PROT_READ, MAP_SHARED, fd, 0);
  if (addr == MAP_FAILED) {
    LG << "mmap: " << strerror(errno);  // TODO: PLOG
    close(fd);
    return NULL;
  }
  close(fd);
  byte* dex_file = reinterpret_cast<byte*>(addr);
  Closer* closer = new MmapCloser(addr, length);
  return Open(dex_file, length, closer);
}

RawDexFile* RawDexFile::OpenBase64(const char* base64) {
  CHECK(base64 != NULL);
  size_t length;
  byte* dex_file = DecodeBase64(base64, &length);
  if (dex_file == NULL) {
    return NULL;
  }
  RawDexFile::Closer* closer = new PtrCloser(dex_file);
  return Open(dex_file, length, closer);
}

RawDexFile* RawDexFile::Open(const byte* dex_file, size_t length,
                             Closer* closer) {
  scoped_ptr<RawDexFile> raw(new RawDexFile(dex_file, length, closer));
  if (!raw->Init()) {
    return NULL;
  } else {
    return raw.release();
  }
}

RawDexFile::~RawDexFile() {}

bool RawDexFile::Init() {
  InitMembers();
  if (!IsMagicValid()) {
    return false;
  }
  InitIndex();
  return true;
}

void RawDexFile::InitMembers() {
  const byte* b = base_;
  header_ = reinterpret_cast<const Header*>(b);
  const Header* h = header_;
  string_ids_ = reinterpret_cast<const StringId*>(b + h->string_ids_off_);
  type_ids_ = reinterpret_cast<const TypeId*>(b + h->type_ids_off_);
  field_ids_ = reinterpret_cast<const FieldId*>(b + h->field_ids_off_);
  method_ids_ = reinterpret_cast<const MethodId*>(b + h->method_ids_off_);
  proto_ids_ = reinterpret_cast<const ProtoId*>(b + h->proto_ids_off_);
  class_defs_ = reinterpret_cast<const ClassDef*>(b + h->class_defs_off_);
}

bool RawDexFile::IsMagicValid() {
  return CheckMagic(header_->magic_);
}

bool RawDexFile::CheckMagic(const byte* magic) {
  CHECK(magic != NULL);
  if (memcmp(magic, kDexMagic, sizeof(kDexMagic)) != 0) {
    LOG(WARN) << "Unrecognized magic number:"
              << " " << magic[0]
              << " " << magic[1]
              << " " << magic[2]
              << " " << magic[3];
    return false;
  }
  const byte* version = &magic[sizeof(kDexMagic)];
  if (memcmp(version, kDexMagicVersion, sizeof(kDexMagicVersion)) != 0) {
    LOG(WARN) << "Unrecognized version number:"
              << " " << version[0]
              << " " << version[1]
              << " " << version[2]
              << " " << version[3];
    return false;
  }
  return true;
}

void RawDexFile::InitIndex() {
  CHECK_EQ(index_.size(), 0);
  for (size_t i = 0; i < NumClassDefs(); ++i) {
    const ClassDef& class_def = GetClassDef(i);
    const char* descriptor = GetClassDescriptor(class_def);
    index_[descriptor] = &class_def;
  }
}

const RawDexFile::ClassDef* RawDexFile::FindClassDef(const char* descriptor) const {
  CHECK(descriptor != NULL);
  Index::const_iterator it = index_.find(descriptor);
  if (it == index_.end()) {
    return NULL;
  } else {
    return it->second;
  }
}

// Read a signed integer.  "zwidth" is the zero-based byte count.
static int32_t ReadSignedInt(const byte* ptr, int zwidth)
{
  int32_t val = 0;
  for (int i = zwidth; i >= 0; --i) {
    val = ((uint32_t)val >> 8) | (((int32_t)*ptr++) << 24);
  }
  val >>= (3 - zwidth) * 8;
  return val;
}

// Read an unsigned integer.  "zwidth" is the zero-based byte count,
// "fill_on_right" indicates which side we want to zero-fill from.
static uint32_t ReadUnsignedInt(const byte* ptr, int zwidth,
                                bool fill_on_right) {
  uint32_t val = 0;
  if (!fill_on_right) {
    for (int i = zwidth; i >= 0; --i) {
      val = (val >> 8) | (((uint32_t)*ptr++) << 24);
    }
    val >>= (3 - zwidth) * 8;
  } else {
    for (int i = zwidth; i >= 0; --i) {
      val = (val >> 8) | (((uint32_t)*ptr++) << 24);
    }
  }
  return val;
}

// Read a signed long.  "zwidth" is the zero-based byte count.
static int64_t ReadSignedLong(const byte* ptr, int zwidth) {
  int64_t val = 0;
  for (int i = zwidth; i >= 0; --i) {
    val = ((uint64_t)val >> 8) | (((int64_t)*ptr++) << 56);
  }
  val >>= (7 - zwidth) * 8;
  return val;
}

// Read an unsigned long.  "zwidth" is the zero-based byte count,
// "fill_on_right" indicates which side we want to zero-fill from.
static uint64_t ReadUnsignedLong(const byte* ptr, int zwidth,
                                 bool fill_on_right) {
  uint64_t val = 0;
  if (!fill_on_right) {
    for (int i = zwidth; i >= 0; --i) {
      val = (val >> 8) | (((uint64_t)*ptr++) << 56);
    }
    val >>= (7 - zwidth) * 8;
  } else {
    for (int i = zwidth; i >= 0; --i) {
      val = (val >> 8) | (((uint64_t)*ptr++) << 56);
    }
  }
  return val;
}

RawDexFile::ValueType RawDexFile::ReadEncodedValue(const byte** stream,
                                                   JValue* value) const {
  const byte* ptr = *stream;
  byte value_type = *ptr++;
  byte value_arg = value_type >> kEncodedValueArgShift;
  size_t width = value_arg + 1;  // assume and correct later
  int type = value_type & kEncodedValueTypeMask;
  switch (type) {
    case RawDexFile::kByte: {
      int32_t b = ReadSignedInt(ptr, value_arg);
      CHECK(IsInt(8, b));
      value->i = b;
      break;
    }
    case RawDexFile::kShort: {
      int32_t s = ReadSignedInt(ptr, value_arg);
      CHECK(IsInt(16, s));
      value->i = s;
      break;
    }
    case RawDexFile::kChar: {
      uint32_t c = ReadUnsignedInt(ptr, value_arg, false);
      CHECK(IsUint(16, c));
      value->i = c;
      break;
    }
    case RawDexFile::kInt:
      value->i = ReadSignedInt(ptr, value_arg);
      break;
    case RawDexFile::kLong:
      value->j = ReadSignedLong(ptr, value_arg);
      break;
    case RawDexFile::kFloat:
      value->i = ReadUnsignedInt(ptr, value_arg, true);
      break;
    case RawDexFile::kDouble:
      value->j = ReadUnsignedLong(ptr, value_arg, true);
      break;
    case RawDexFile::kBoolean:
      value->i = (value_arg != 0);
      width = 0;
      break;
    case RawDexFile::kString:
    case RawDexFile::kType:
    case RawDexFile::kMethod:
    case RawDexFile::kEnum:
      value->i = ReadUnsignedInt(ptr, value_arg, false);
      break;
    case RawDexFile::kField:
    case RawDexFile::kArray:
    case RawDexFile::kAnnotation:
      LOG(FATAL) << "Unimplemented";
      break;
    case RawDexFile::kNull:
      value->i = 0;
      width = 0;
      break;
    default:
      LOG(FATAL) << "Unreached";
  }
  ptr += width;
  *stream = ptr;
  return static_cast<ValueType>(type);
}

}  // namespace art