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 "read_dex_file.h"
18 
19 #include <fcntl.h>
20 
21 #include <algorithm>
22 #include <iterator>
23 #include <string>
24 #include <utility>
25 #include <vector>
26 
27 #include <android-base/logging.h>
28 #include <android-base/unique_fd.h>
29 #include <art_api/dex_file_support.h>
30 
ReadSymbols(const std::vector<uint64_t> & dex_file_offsets,std::vector<DexFileSymbol> * symbols,const std::function<std::unique_ptr<art_api::dex::DexFile> (uint64_t offset)> & open_file_cb)31 static bool ReadSymbols(
32     const std::vector<uint64_t>& dex_file_offsets, std::vector<DexFileSymbol>* symbols,
33     const std::function<std::unique_ptr<art_api::dex::DexFile>(uint64_t offset)>& open_file_cb) {
34   for (uint64_t offset : dex_file_offsets) {
35     std::unique_ptr<art_api::dex::DexFile> dex_file = open_file_cb(offset);
36     if (dex_file == nullptr) {
37       return false;
38     }
39 
40     std::vector<art_api::dex::MethodInfo> file_syms = dex_file->GetAllMethodInfos(false);
41 
42     // Adjust offsets to be from the start of the combined file.
43     for (art_api::dex::MethodInfo& sym : file_syms) {
44       sym.offset += offset;
45     }
46 
47     if (symbols->empty()) {
48       *symbols = std::move(file_syms);
49     } else {
50       symbols->reserve(symbols->size() + file_syms.size());
51       std::move(std::begin(file_syms), std::end(file_syms), std::back_inserter(*symbols));
52     }
53   }
54 
55   return true;
56 }
57 
ReadSymbolsFromDexFileInMemory(void * addr,uint64_t size,const std::vector<uint64_t> & dex_file_offsets,std::vector<DexFileSymbol> * symbols)58 bool ReadSymbolsFromDexFileInMemory(void* addr, uint64_t size,
59                                     const std::vector<uint64_t>& dex_file_offsets,
60                                     std::vector<DexFileSymbol>* symbols) {
61   return ReadSymbols(
62       dex_file_offsets, symbols, [&](uint64_t offset) -> std::unique_ptr<art_api::dex::DexFile> {
63         size_t max_file_size;
64         if (__builtin_sub_overflow(size, offset, &max_file_size)) {
65           return nullptr;
66         }
67         uint8_t* file_addr = static_cast<uint8_t*>(addr) + offset;
68         std::string error_msg;
69         std::unique_ptr<art_api::dex::DexFile> dex_file =
70             art_api::dex::DexFile::OpenFromMemory(file_addr, &max_file_size, "", &error_msg);
71         if (dex_file == nullptr) {
72           LOG(WARNING) << "Failed to read dex file symbols: " << error_msg;
73           return nullptr;
74         }
75         return dex_file;
76       });
77 }
78 
ReadSymbolsFromDexFile(const std::string & file_path,const std::vector<uint64_t> & dex_file_offsets,std::vector<DexFileSymbol> * symbols)79 bool ReadSymbolsFromDexFile(const std::string& file_path,
80                             const std::vector<uint64_t>& dex_file_offsets,
81                             std::vector<DexFileSymbol>* symbols) {
82   android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(file_path.c_str(), O_RDONLY | O_CLOEXEC)));
83   if (fd == -1) {
84     return false;
85   }
86   return ReadSymbols(
87       dex_file_offsets, symbols, [&](uint64_t offset) -> std::unique_ptr<art_api::dex::DexFile> {
88         std::string error_msg;
89         std::unique_ptr<art_api::dex::DexFile> dex_file =
90             art_api::dex::DexFile::OpenFromFd(fd, offset, file_path, &error_msg);
91         if (dex_file == nullptr) {
92           LOG(WARNING) << "Failed to read dex file symbols from '" << file_path
93                        << "': " << error_msg;
94           return nullptr;
95         }
96         return dex_file;
97       });
98 }
99