File: Parser.tcc

package info (click to toggle)
lief 0.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid
  • size: 16,036 kB
  • sloc: cpp: 76,013; python: 6,167; ansic: 3,355; pascal: 404; sh: 98; makefile: 32
file content (479 lines) | stat: -rw-r--r-- 16,634 bytes parent folder | download | duplicates (2)
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/* Copyright 2017 R. Thomas
 * Copyright 2017 Quarkslab
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <type_traits>

#include "LIEF/logging++.hpp"

#include "LIEF/utils.hpp"

#include "LIEF/DEX.hpp"

#include "Header.tcc"
#include "Object.tcc"

#include "oat_64.tcc"
#include "oat_79.tcc"
#include "oat_124.tcc"
#include "oat_131.tcc"

namespace LIEF {
namespace OAT {


template<>
void Parser::parse_dex_files<OAT88_t>(void) {
  return this->parse_dex_files<OAT79_t>();
}

template<>
void Parser::parse_oat_classes<OAT88_t>(void) {
  return this->parse_oat_classes<OAT79_t>();
}


// Parse Binary
// ============
template<>
void Parser::parse_binary<OAT64_t>(void) {

  std::vector<uint8_t> raw_oat;

  if (this->oat_binary_->has_symbol("oatdata")) {
    const LIEF::ELF::Symbol* oat_data = this->oat_binary_->get_symbol("oatdata").as<LIEF::ELF::Symbol>();

    raw_oat.reserve(oat_data->size());

    const std::vector<uint8_t>& raw_data = this->oat_binary_->get_content_from_virtual_address(oat_data->value(), oat_data->size());
    std::move(std::begin(raw_data), std::end(raw_data), std::back_inserter(raw_oat));

    this->data_address_ = oat_data->value();
    this->data_size_    = oat_data->size();
  }

  if (this->oat_binary_->has_symbol("oatexec")) {
    const LIEF::ELF::Symbol* oat_exec = this->oat_binary_->get_symbol("oatexec").as<LIEF::ELF::Symbol>();

    this->exec_start_ = oat_exec->value();
    this->exec_size_  = oat_exec->size();

    const std::vector<uint8_t>& raw_oatexec = this->oat_binary_->get_content_from_virtual_address(oat_exec->value(), oat_exec->size());

    uint32_t padding = this->exec_start_ - (this->data_address_ + this->data_size_);

    raw_oat.reserve(raw_oat.size() + oat_exec->size() + padding);
    raw_oat.insert(std::end(raw_oat), padding, 0);

    std::move(std::begin(raw_oatexec), std::end(raw_oatexec), std::back_inserter(raw_oat));
  }

  uint32_t padding = align(raw_oat.size(), sizeof(uint32_t) * 8) - raw_oat.size();
  raw_oat.insert(std::end(raw_oat), padding, 0);

  this->stream_ = std::unique_ptr<VectorStream>(new VectorStream{std::move(raw_oat)});

  this->parse_header<OAT64_t>();
  this->parse_dex_files<OAT64_t>();
  this->parse_oat_classes<OAT64_t>();
}

template<>
void Parser::parse_binary<OAT79_t>(void) {

  std::vector<uint8_t> raw_oat;

  if (this->oat_binary_->has_symbol("oatdata")) {
    const LIEF::ELF::Symbol* oat_data = this->oat_binary_->get_symbol("oatdata").as<LIEF::ELF::Symbol>();

    raw_oat.reserve(oat_data->size());

    const std::vector<uint8_t>& raw_data = this->oat_binary_->get_content_from_virtual_address(oat_data->value(), oat_data->size());
    std::move(std::begin(raw_data), std::end(raw_data), std::back_inserter(raw_oat));

    this->data_address_ = oat_data->value();
    this->data_size_    = oat_data->size();
  }

  if (this->oat_binary_->has_symbol("oatexec")) {
    const LIEF::ELF::Symbol* oat_exec = this->oat_binary_->get_symbol("oatexec").as<LIEF::ELF::Symbol>();

    this->exec_start_ = oat_exec->value();
    this->exec_size_  = oat_exec->size();

    const std::vector<uint8_t>& raw_oatexec = this->oat_binary_->get_content_from_virtual_address(oat_exec->value(), oat_exec->size());

    uint32_t padding = this->exec_start_ - (this->data_address_ + this->data_size_);

    raw_oat.reserve(raw_oat.size() + oat_exec->size() + padding);
    raw_oat.insert(std::end(raw_oat), padding, 0);

    std::move(std::begin(raw_oatexec), std::end(raw_oatexec), std::back_inserter(raw_oat));
  }

  uint32_t padding = align(raw_oat.size(), sizeof(uint32_t) * 8) - raw_oat.size();
  raw_oat.insert(std::end(raw_oat), padding, 0);

  this->stream_ = std::unique_ptr<VectorStream>(new VectorStream{std::move(raw_oat)});


  this->parse_header<OAT79_t>();
  this->parse_dex_files<OAT79_t>();

  this->parse_type_lookup_table<OAT79_t>();
  this->parse_oat_classes<OAT79_t>();
}

template<>
void Parser::parse_binary<OAT88_t>(void) {
  std::vector<uint8_t> raw_oat;

  if (this->oat_binary_->has_symbol("oatdata")) {
    const LIEF::ELF::Symbol* oat_data = this->oat_binary_->get_symbol("oatdata").as<LIEF::ELF::Symbol>();

    raw_oat.reserve(oat_data->size());

    const std::vector<uint8_t>& raw_data = this->oat_binary_->get_content_from_virtual_address(oat_data->value(), oat_data->size());
    std::move(std::begin(raw_data), std::end(raw_data), std::back_inserter(raw_oat));

    this->data_address_ = oat_data->value();
    this->data_size_    = oat_data->size();
  }

  if (this->oat_binary_->has_symbol("oatexec")) {
    const LIEF::ELF::Symbol* oat_exec = this->oat_binary_->get_symbol("oatexec").as<LIEF::ELF::Symbol>();

    this->exec_start_ = oat_exec->value();
    this->exec_size_  = oat_exec->size();

    const std::vector<uint8_t>& raw_oatexec = this->oat_binary_->get_content_from_virtual_address(oat_exec->value(), oat_exec->size());

    uint32_t padding = this->exec_start_ - (this->data_address_ + this->data_size_);

    raw_oat.reserve(raw_oat.size() + oat_exec->size() + padding);
    raw_oat.insert(std::end(raw_oat), padding, 0);

    std::move(std::begin(raw_oatexec), std::end(raw_oatexec), std::back_inserter(raw_oat));
  }

  uint32_t padding = align(raw_oat.size(), sizeof(uint32_t) * 8) - raw_oat.size();
  raw_oat.insert(std::end(raw_oat), padding, 0);

  this->stream_ = std::unique_ptr<VectorStream>(new VectorStream{std::move(raw_oat)});


  this->parse_header<OAT88_t>();
  this->parse_dex_files<OAT88_t>();

  this->parse_type_lookup_table<OAT88_t>();
  this->parse_oat_classes<OAT88_t>();
}

template<>
void Parser::parse_binary<OAT124_t>(void) {
  std::vector<uint8_t> raw_oat;

  if (this->oat_binary_->has_symbol("oatdata")) {
    const LIEF::ELF::Symbol* oat_data = this->oat_binary_->get_symbol("oatdata").as<LIEF::ELF::Symbol>();

    raw_oat.reserve(oat_data->size());

    const std::vector<uint8_t>& raw_data = this->oat_binary_->get_content_from_virtual_address(oat_data->value(), oat_data->size());
    std::move(std::begin(raw_data), std::end(raw_data), std::back_inserter(raw_oat));

    this->data_address_ = oat_data->value();
    this->data_size_    = oat_data->size();
  }

  if (this->oat_binary_->has_symbol("oatexec")) {
    const LIEF::ELF::Symbol* oat_exec = this->oat_binary_->get_symbol("oatexec").as<LIEF::ELF::Symbol>();

    this->exec_start_ = oat_exec->value();
    this->exec_size_  = oat_exec->size();

    const std::vector<uint8_t>& raw_oatexec = this->oat_binary_->get_content_from_virtual_address(oat_exec->value(), oat_exec->size());

    uint32_t padding = this->exec_start_ - (this->data_address_ + this->data_size_);

    raw_oat.reserve(raw_oat.size() + oat_exec->size() + padding);
    raw_oat.insert(std::end(raw_oat), padding, 0);

    std::move(std::begin(raw_oatexec), std::end(raw_oatexec), std::back_inserter(raw_oat));
  }

  uint32_t padding = align(raw_oat.size(), sizeof(uint32_t) * 8) - raw_oat.size();
  raw_oat.insert(std::end(raw_oat), padding, 0);

  this->stream_ = std::unique_ptr<VectorStream>(new VectorStream{std::move(raw_oat)});


  this->parse_header<OAT124_t>();
  this->parse_dex_files<OAT124_t>();
  if (this->has_vdex()) {
    this->parse_type_lookup_table<OAT124_t>();
    this->parse_oat_classes<OAT124_t>();
  }
}

template<>
void Parser::parse_binary<OAT131_t>(void) {
  std::vector<uint8_t> raw_oat;

  if (this->oat_binary_->has_symbol("oatdata")) {
    const LIEF::ELF::Symbol* oat_data = this->oat_binary_->get_symbol("oatdata").as<LIEF::ELF::Symbol>();

    raw_oat.reserve(oat_data->size());

    const std::vector<uint8_t>& raw_data = this->oat_binary_->get_content_from_virtual_address(oat_data->value(), oat_data->size());
    std::move(std::begin(raw_data), std::end(raw_data), std::back_inserter(raw_oat));

    this->data_address_ = oat_data->value();
    this->data_size_    = oat_data->size();
  }

  if (this->oat_binary_->has_symbol("oatexec")) {
    const LIEF::ELF::Symbol* oat_exec = this->oat_binary_->get_symbol("oatexec").as<LIEF::ELF::Symbol>();

    this->exec_start_ = oat_exec->value();
    this->exec_size_  = oat_exec->size();

    const std::vector<uint8_t>& raw_oatexec = this->oat_binary_->get_content_from_virtual_address(oat_exec->value(), oat_exec->size());

    uint32_t padding = this->exec_start_ - (this->data_address_ + this->data_size_);

    raw_oat.reserve(raw_oat.size() + oat_exec->size() + padding);
    raw_oat.insert(std::end(raw_oat), padding, 0);

    std::move(std::begin(raw_oatexec), std::end(raw_oatexec), std::back_inserter(raw_oat));
  }

  uint32_t padding = align(raw_oat.size(), sizeof(uint32_t) * 8) - raw_oat.size();
  raw_oat.insert(std::end(raw_oat), padding, 0);

  this->stream_ = std::unique_ptr<VectorStream>(new VectorStream{std::move(raw_oat)});

  this->parse_header<OAT131_t>();
  this->parse_dex_files<OAT131_t>();

  if (this->has_vdex()) {
    this->parse_type_lookup_table<OAT131_t>();
    this->parse_oat_classes<OAT131_t>();
  }
}



template<typename OAT_T>
void Parser::parse_header(void) {
  VLOG(VDEBUG) << "Parsing OAT header";
  using oat_header = typename OAT_T::oat_header;

  const oat_header& oat_hdr = this->stream_->peek<oat_header>(0);
  this->oat_binary_->header_ = &oat_hdr;
  VLOG(VDEBUG) << "Nb dex files: " << std::dec << this->oat_binary_->header_.nb_dex_files();
  VLOG(VDEBUG) << "OAT version: " << std::dec << oat_hdr.oat_version;

  this->parse_header_keys<OAT_T>();
}


template<typename OAT_T>
void Parser::parse_header_keys(void) {
  using oat_header = typename OAT_T::oat_header;

  const uint64_t keys_offset = sizeof(oat_header);
  const size_t keys_size = this->oat_binary_->header_.key_value_size();

  std::string key_values;

  const char* keys_start = this->stream_->peek_array<char>(keys_offset, keys_size, /* check */false);
  if (keys_start != nullptr) {
    key_values = {keys_start, keys_size};
  }

  for (HEADER_KEYS key : header_keys_list) {
    std::string key_str = std::string{'\0'} + Header::key_to_string(key);

    size_t pos = key_values.find(key_str);

    if (pos != std::string::npos) {
      std::string value = std::string{key_values.data() + pos + key_str.size() + 1};
      this->oat_binary_->header_.dex2oat_context_.emplace(key, value);
    }
  }
}



template<typename OAT_T>
void Parser::parse_type_lookup_table(void) {
  //using oat_header           = typename OAT_T::oat_header;
  //using dex_file             = typename OAT_T::dex_file;
  //using lookup_table_entry_t = typename OAT_T::lookup_table_entry_t;


  //VLOG(VDEBUG) << "Parsing TypeLookupTable";
  //for (size_t i = 0; i < this->oat_binary_->dex_files_.size(); ++i) {

  //  const DexFile* oat_dex_file = this->oat_binary_->oat_dex_files_[i];
  //  uint64_t tlt_offset = oat_dex_file->lookup_table_offset();

  //  VLOG(VDEBUG) << "Getting TypeLookupTable for DexFile "
  //                << oat_dex_file->location()
  //                << " (#" << std::dec << oat_dex_file->dex_file().header().nb_classes() << ")";
  //  for (size_t j = 0; j < oat_dex_file->dex_file().header().nb_classes();) {
  //    const lookup_table_entry_t* entry = reinterpret_cast<const lookup_table_entry_t*>(this->stream_->read(tlt_offset, sizeof(lookup_table_entry_t)));

  //    if (entry->str_offset) {
  //      uint64_t string_offset = oat_dex_file->dex_offset() + entry->str_offset;
  //      std::pair<uint64_t, uint64_t> len_size = this->stream_->read_uleb128(string_offset);
  //      string_offset += len_size.second;
  //      std::string class_name = this->stream_->get_string(string_offset);
  //      //VLOG(VDEBUG) << "    " << "#" << std::dec << j << " " << class_name;
  //      ++j;
  //    }
  //    tlt_offset += sizeof(lookup_table_entry_t);
  //  }
  //}
}


template<typename OAT_T>
void Parser::parse_oat_classes(void) {
  VLOG(VDEBUG) << "Parsing OAT Classes";
  for (size_t dex_idx = 0; dex_idx < this->oat_binary_->oat_dex_files_.size(); ++dex_idx) {
    DexFile* oat_dex_file = this->oat_binary_->oat_dex_files_[dex_idx];
    const DEX::File& dex_file = oat_dex_file->dex_file();

    const std::vector<uint32_t>& classes_offsets = oat_dex_file->classes_offsets();
    uint32_t nb_classes = dex_file.header().nb_classes();

    VLOG(VDEBUG) << "Dealing with DexFile #" << std::dec << dex_idx
                 << " (" << nb_classes << ")";

    for (size_t class_idx = 0; class_idx < nb_classes; ++class_idx) {
      const DEX::Class& cls = dex_file.get_class(class_idx);

      CHECK_LE(cls.index(), classes_offsets.size());
      uint32_t oat_class_offset = classes_offsets[cls.index()];
      this->stream_->setpos(oat_class_offset);

      // OAT Status
      OAT_CLASS_STATUS status = static_cast<OAT_CLASS_STATUS>(this->stream_->read<int16_t>());

      // OAT Type
      OAT_CLASS_TYPES type = static_cast<OAT_CLASS_TYPES>(this->stream_->read<uint16_t>());

      // Bitmap (if type is "some compiled")
      uint32_t method_bitmap_size = 0;
      std::vector<uint32_t> bitmap;

      if (type == OAT_CLASS_TYPES::OAT_CLASS_SOME_COMPILED) {
        method_bitmap_size = this->stream_->read<uint32_t>();
        const uint32_t nb_entries = method_bitmap_size / sizeof(uint32_t);

        const uint32_t* raw = this->stream_->read_array<uint32_t>(nb_entries, /* check */false);
        if (raw != nullptr) {
          bitmap = {raw, raw + nb_entries};
        }
      }

      Class* oat_class = new Class{status, type, const_cast<DEX::Class*>(&cls), bitmap};
      this->oat_binary_->classes_.emplace(cls.fullname(), oat_class);


      // Methods Offsets
      const uint64_t method_offsets = this->stream_->pos();
      this->parse_oat_methods<OAT_T>(method_offsets, oat_class, cls);
    }
  }
}

template<typename OAT_T>
void Parser::parse_oat_methods(uint64_t methods_offsets, Class* clazz, const DEX::Class& dex_class) {
  using oat_quick_method_header = typename OAT_T::oat_quick_method_header;
  DEX::it_const_methods methods = dex_class.methods();

  for (size_t method_idx = 0; method_idx < methods.size(); ++method_idx) {

    const DEX::Method& method = methods[method_idx];
    if (not clazz->is_quickened(method)) {
      continue;
    }

    uint32_t computed_index = clazz->method_offsets_index(method);
    uint32_t code_off = this->stream_->peek<uint32_t>(methods_offsets + computed_index * sizeof(uint32_t));

    // Offset of the Quick method header relative to the beginning of oatexec
    uint32_t quick_method_header_off = code_off - sizeof(oat_quick_method_header);
    quick_method_header_off &= ~1u;

    if (not this->stream_->can_read<oat_quick_method_header>(quick_method_header_off)) {
      break;
    }

    const oat_quick_method_header& quick_header = this->stream_->peek<oat_quick_method_header>(quick_method_header_off);

    uint32_t vmap_table_offset = code_off - quick_header.vmap_table_offset;

    std::unique_ptr<Method> oat_method{new Method{const_cast<DEX::Method*>(&method), clazz}};

    if (quick_header.code_size > 0) {

      const uint8_t* code = this->stream_->peek_array<uint8_t>(code_off, quick_header.code_size, /* check */false);
      if (code != nullptr) {
        oat_method->quick_code_ = {code, code + quick_header.code_size};
      }
    }

    // Quickened with "Optimizing compiler"
    if (quick_header.code_size > 0 and vmap_table_offset > 0) {
    }

    // Quickened with "dex2dex"
    if (quick_header.code_size == 0 and vmap_table_offset > 0) {
      this->stream_->setpos(vmap_table_offset);

      for (size_t pc = 0, round = 0; pc < method.bytecode().size(); ++round) {
        if (this->stream_->pos() >= this->stream_->size()) {
          break;
        }

        uint32_t new_pc = static_cast<uint32_t>(this->stream_->read_uleb128());

        if (new_pc <= pc and round > 0) {
          break;
        }

        pc = new_pc;


        if (this->stream_->pos() >= this->stream_->size()) {
          break;
        }

        uint32_t index = static_cast<uint32_t>(this->stream_->read_uleb128());
        oat_method->dex_method().insert_dex2dex_info(pc, index);
      }

    }
    clazz->methods_.push_back(oat_method.get());
    this->oat_binary_->methods_.push_back(oat_method.release());
  }

}

}
}