summaryrefslogtreecommitdiff
path: root/libunwindstack/DexFile.cpp
blob: 8fc3d23ecb60d057a9820faaa7775058aa01841d (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
/*
 * Copyright (C) 2018 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.
 */

#include <stdint.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <memory>

#define LOG_TAG "unwind"
#include <log/log.h>

#include <android-base/unique_fd.h>
#include <art_api/dex_file_support.h>

#include <unwindstack/MapInfo.h>
#include <unwindstack/Memory.h>

#include "DexFile.h"

namespace unwindstack {

static bool CheckDexSupport() {
  if (std::string err_msg; !art_api::dex::TryLoadLibdexfileExternal(&err_msg)) {
    ALOGW("Failed to initialize DEX file support: %s", err_msg.c_str());
    return false;
  }
  return true;
}

static bool HasDexSupport() {
  static bool has_dex_support = CheckDexSupport();
  return has_dex_support;
}

std::unique_ptr<DexFile> DexFile::Create(uint64_t dex_file_offset_in_memory, Memory* memory,
                                         MapInfo* info) {
  if (UNLIKELY(!HasDexSupport())) {
    return nullptr;
  }

  size_t max_size = info->end - dex_file_offset_in_memory;
  if (memory->IsLocal()) {
    size_t size = max_size;

    std::string err_msg;
    std::unique_ptr<art_api::dex::DexFile> art_dex_file = DexFile::OpenFromMemory(
        reinterpret_cast<void const*>(dex_file_offset_in_memory), &size, info->name, &err_msg);
    if (art_dex_file != nullptr && size <= max_size) {
      return std::unique_ptr<DexFile>(new DexFile(art_dex_file));
    }
  }

  if (!info->name.empty()) {
    std::unique_ptr<DexFile> dex_file =
        DexFileFromFile::Create(dex_file_offset_in_memory - info->start + info->offset, info->name);
    if (dex_file) {
      return dex_file;
    }
  }
  return DexFileFromMemory::Create(dex_file_offset_in_memory, memory, info->name, max_size);
}

bool DexFile::GetMethodInformation(uint64_t dex_offset, std::string* method_name,
                                   uint64_t* method_offset) {
  art_api::dex::MethodInfo method_info = GetMethodInfoForOffset(dex_offset, false);
  if (method_info.offset == 0) {
    return false;
  }
  *method_name = method_info.name;
  *method_offset = dex_offset - method_info.offset;
  return true;
}

std::unique_ptr<DexFileFromFile> DexFileFromFile::Create(uint64_t dex_file_offset_in_file,
                                                         const std::string& file) {
  if (UNLIKELY(!HasDexSupport())) {
    return nullptr;
  }

  android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(file.c_str(), O_RDONLY | O_CLOEXEC)));
  if (fd == -1) {
    return nullptr;
  }

  std::string error_msg;
  std::unique_ptr<art_api::dex::DexFile> art_dex_file =
      OpenFromFd(fd, dex_file_offset_in_file, file, &error_msg);
  if (art_dex_file == nullptr) {
    return nullptr;
  }

  return std::unique_ptr<DexFileFromFile>(new DexFileFromFile(art_dex_file));
}

std::unique_ptr<DexFileFromMemory> DexFileFromMemory::Create(uint64_t dex_file_offset_in_memory,
                                                             Memory* memory,
                                                             const std::string& name,
                                                             size_t max_size) {
  if (UNLIKELY(!HasDexSupport())) {
    return nullptr;
  }

  std::vector<uint8_t> backing_memory;

  for (size_t size = 0;;) {
    std::string error_msg;
    std::unique_ptr<art_api::dex::DexFile> art_dex_file =
        OpenFromMemory(backing_memory.data(), &size, name, &error_msg);
    if (size > max_size) {
      return nullptr;
    }

    if (art_dex_file != nullptr) {
      return std::unique_ptr<DexFileFromMemory>(
          new DexFileFromMemory(art_dex_file, std::move(backing_memory)));
    }

    if (!error_msg.empty()) {
      return nullptr;
    }

    backing_memory.resize(size);
    if (!memory->ReadFully(dex_file_offset_in_memory, backing_memory.data(),
                           backing_memory.size())) {
      return nullptr;
    }
  }
}

}  // namespace unwindstack