• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdint.h>
18 #include <sys/mman.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22 
23 #include <memory>
24 
25 #include <android-base/unique_fd.h>
26 #include <art_api/dex_file_support.h>
27 
28 #include <unwindstack/MapInfo.h>
29 #include <unwindstack/Memory.h>
30 
31 #include "DexFile.h"
32 
33 namespace unwindstack {
34 
Create(uint64_t dex_file_offset_in_memory,Memory * memory,MapInfo * info)35 std::unique_ptr<DexFile> DexFile::Create(uint64_t dex_file_offset_in_memory, Memory* memory,
36                                          MapInfo* info) {
37   if (!info->name.empty()) {
38     std::unique_ptr<DexFile> dex_file =
39         DexFileFromFile::Create(dex_file_offset_in_memory - info->start + info->offset, info->name);
40     if (dex_file) {
41       return dex_file;
42     }
43   }
44   return DexFileFromMemory::Create(dex_file_offset_in_memory, memory, info->name);
45 }
46 
GetMethodInformation(uint64_t dex_offset,std::string * method_name,uint64_t * method_offset)47 bool DexFile::GetMethodInformation(uint64_t dex_offset, std::string* method_name,
48                                    uint64_t* method_offset) {
49   art_api::dex::MethodInfo method_info = GetMethodInfoForOffset(dex_offset, false);
50   if (method_info.offset == 0) {
51     return false;
52   }
53   *method_name = method_info.name;
54   *method_offset = dex_offset - method_info.offset;
55   return true;
56 }
57 
Create(uint64_t dex_file_offset_in_file,const std::string & file)58 std::unique_ptr<DexFileFromFile> DexFileFromFile::Create(uint64_t dex_file_offset_in_file,
59                                                          const std::string& file) {
60   android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(file.c_str(), O_RDONLY | O_CLOEXEC)));
61   if (fd == -1) {
62     return nullptr;
63   }
64 
65   std::string error_msg;
66   std::unique_ptr<art_api::dex::DexFile> art_dex_file =
67       OpenFromFd(fd, dex_file_offset_in_file, file, &error_msg);
68   if (art_dex_file == nullptr) {
69     return nullptr;
70   }
71 
72   return std::unique_ptr<DexFileFromFile>(new DexFileFromFile(std::move(*art_dex_file.release())));
73 }
74 
Create(uint64_t dex_file_offset_in_memory,Memory * memory,const std::string & name)75 std::unique_ptr<DexFileFromMemory> DexFileFromMemory::Create(uint64_t dex_file_offset_in_memory,
76                                                              Memory* memory,
77                                                              const std::string& name) {
78   std::vector<uint8_t> backing_memory;
79 
80   for (size_t size = 0;;) {
81     std::string error_msg;
82     std::unique_ptr<art_api::dex::DexFile> art_dex_file =
83         OpenFromMemory(backing_memory.data(), &size, name, &error_msg);
84 
85     if (art_dex_file != nullptr) {
86       return std::unique_ptr<DexFileFromMemory>(
87           new DexFileFromMemory(std::move(*art_dex_file.release()), std::move(backing_memory)));
88     }
89 
90     if (!error_msg.empty()) {
91       return nullptr;
92     }
93 
94     backing_memory.resize(size);
95     if (!memory->ReadFully(dex_file_offset_in_memory, backing_memory.data(),
96                            backing_memory.size())) {
97       return nullptr;
98     }
99   }
100 }
101 
102 }  // namespace unwindstack
103