summaryrefslogtreecommitdiff
path: root/simpleperf/read_apk.h
blob: baee28747b309934732c48878d3626ad170a40b7 (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
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * 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.
 */

#ifndef SIMPLE_PERF_READ_APK_H_
#define SIMPLE_PERF_READ_APK_H_

#include <stdint.h>

#include <memory>
#include <string>
#include <tuple>
#include <unordered_map>

#include "read_elf.h"

namespace simpleperf {

// Container for info an on ELF file embedded into an APK file
class EmbeddedElf {
 public:
  EmbeddedElf() : entry_offset_(0), entry_size_(0) {}

  EmbeddedElf(const std::string& filepath, const std::string& entry_name, uint64_t entry_offset,
              size_t entry_size)
      : filepath_(filepath),
        entry_name_(entry_name),
        entry_offset_(entry_offset),
        entry_size_(entry_size) {}

  // Path to APK file
  const std::string& filepath() const { return filepath_; }

  // Entry name within zip archive
  const std::string& entry_name() const { return entry_name_; }

  // Offset of zip entry from start of containing APK file
  uint64_t entry_offset() const { return entry_offset_; }

  // Size of zip entry (length of embedded ELF)
  uint32_t entry_size() const { return entry_size_; }

 private:
  std::string filepath_;    // containing APK path
  std::string entry_name_;  // name of entry in zip index of embedded elf file
  uint64_t entry_offset_;   // offset of ELF from start of containing APK file
  uint32_t entry_size_;     // size of ELF file in zip
};

// APK inspector helper class
class ApkInspector {
 public:
  static EmbeddedElf* FindElfInApkByOffset(const std::string& apk_path, uint64_t file_offset);
  static EmbeddedElf* FindElfInApkByName(const std::string& apk_path,
                                         const std::string& entry_name);

 private:
  static std::unique_ptr<EmbeddedElf> FindElfInApkByOffsetWithoutCache(const std::string& apk_path,
                                                                       uint64_t file_offset);
  static std::unique_ptr<EmbeddedElf> FindElfInApkByNameWithoutCache(const std::string& apk_path,
                                                                     const std::string& entry_name);

  struct ApkNode {
    // Map from entry_offset to EmbeddedElf.
    std::unordered_map<uint64_t, std::unique_ptr<EmbeddedElf>> offset_map;
    // Map from entry_name to EmbeddedElf.
    std::unordered_map<std::string, EmbeddedElf*> name_map;
  };
  static std::unordered_map<std::string, ApkNode> embedded_elf_cache_;
};

std::string GetUrlInApk(const std::string& apk_path, const std::string& elf_filename);
std::tuple<bool, std::string, std::string> SplitUrlInApk(const std::string& path);

// Parse path like "[anon:dalvik-classes.dex extracted in memory from /..base.apk] (deleted)",
// or "/dev/ashmem/dalvik-classes.dex extracted in memory from /..base.apk (deleted)" on Android P.
bool ParseExtractedInMemoryPath(const std::string& path, std::string* zip_path,
                                std::string* entry_name);

}  // namespace simpleperf

#endif  // SIMPLE_PERF_READ_APK_H_