• 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 "art_api/dex_file_external.h"
18 
19 #include <inttypes.h>
20 #include <stdint.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 
25 #include <cerrno>
26 #include <cstring>
27 #include <deque>
28 #include <map>
29 #include <memory>
30 #include <string>
31 #include <utility>
32 #include <vector>
33 
34 #include <android-base/logging.h>
35 #include <android-base/macros.h>
36 #include <android-base/mapped_file.h>
37 #include <android-base/stringprintf.h>
38 
39 #include <dex/class_accessor-inl.h>
40 #include <dex/code_item_accessors-inl.h>
41 #include <dex/dex_file-inl.h>
42 #include <dex/dex_file_loader.h>
43 
44 extern "C" {
45 
46 struct ADexFile_Method {
47   ADexFile* adex;
48   uint32_t index;
49   size_t offset;
50   size_t size;
51 };
52 
53 // Opaque implementation of ADexFile for the C interface.
54 struct ADexFile {
ADexFileADexFile55   explicit ADexFile(std::unique_ptr<const art::DexFile> dex_file)
56       : dex_file_(std::move(dex_file)) {}
57 
FindMethodADexFile58   inline bool FindMethod(uint32_t dex_offset, /*out*/ ADexFile_Method* result) {
59     uint32_t class_def_index;
60     if (GetClassDefIndex(dex_offset, &class_def_index)) {
61       art::ClassAccessor accessor(*dex_file_, class_def_index);
62       for (const art::ClassAccessor::Method& method : accessor.GetMethods()) {
63         art::CodeItemInstructionAccessor code = method.GetInstructions();
64         if (!code.HasCodeItem()) {
65           continue;
66         }
67         size_t offset = reinterpret_cast<const uint8_t*>(code.Insns()) - dex_file_->Begin();
68         size_t size = code.InsnsSizeInBytes();
69         if (offset <= dex_offset && dex_offset < offset + size) {
70           *result = ADexFile_Method {
71             .adex = this,
72             .index = method.GetIndex(),
73             .offset = offset,
74             .size = size,
75           };
76           return true;
77         }
78       }
79     }
80     return false;
81   }
82 
CreateClassCacheADexFile83   void CreateClassCache() {
84     // Create binary search table with (end_dex_offset, class_def_index) entries.
85     // That is, we don't assume that dex code of given class is consecutive.
86     std::deque<std::pair<uint32_t, uint32_t>> cache;
87     for (art::ClassAccessor accessor : dex_file_->GetClasses()) {
88       for (const art::ClassAccessor::Method& method : accessor.GetMethods()) {
89         art::CodeItemInstructionAccessor code = method.GetInstructions();
90         if (code.HasCodeItem()) {
91           int32_t offset = reinterpret_cast<const uint8_t*>(code.Insns()) - dex_file_->Begin();
92           DCHECK_NE(offset, 0);
93           cache.emplace_back(offset + code.InsnsSizeInBytes(), accessor.GetClassDefIndex());
94         }
95       }
96     }
97     std::sort(cache.begin(), cache.end());
98 
99     // If two consecutive methods belong to same class, we can merge them.
100     // This tends to reduce the number of entries (used memory) by 10x.
101     size_t num_entries = cache.size();
102     if (cache.size() > 1) {
103       for (auto it = std::next(cache.begin()); it != cache.end(); it++) {
104         if (std::prev(it)->second == it->second) {
105           std::prev(it)->first = 0;  // Clear entry with lower end_dex_offset (mark to remove).
106           num_entries--;
107         }
108       }
109     }
110 
111     // The cache is immutable now. Store it as continuous vector to save space.
112     class_cache_.reserve(num_entries);
113     auto pred = [](auto it) { return it.first != 0; };  // Entries to copy (not cleared above).
114     std::copy_if(cache.begin(), cache.end(), std::back_inserter(class_cache_), pred);
115   }
116 
GetClassDefIndexADexFile117   inline bool GetClassDefIndex(uint32_t dex_offset, uint32_t* class_def_index) {
118     if (class_cache_.empty()) {
119       CreateClassCache();
120     }
121 
122     // Binary search in the class cache. First element of the pair is the key.
123     auto comp = [](uint32_t value, const auto& it) { return value < it.first; };
124     auto it = std::upper_bound(class_cache_.begin(), class_cache_.end(), dex_offset, comp);
125     if (it != class_cache_.end()) {
126       *class_def_index = it->second;
127       return true;
128     }
129     return false;
130   }
131 
132   // The underlying ART object.
133   std::unique_ptr<const art::DexFile> dex_file_;
134 
135   // Binary search table with (end_dex_offset, class_def_index) entries.
136   std::vector<std::pair<uint32_t, uint32_t>> class_cache_;
137 
138   // Used as short lived temporary when needed. Avoids alloc/free.
139   std::string temporary_qualified_name_;
140 };
141 
ADexFile_create(const void * _Nonnull address,size_t size,size_t * _Nullable new_size,const char * _Nonnull location,ADexFile * _Nullable * _Nonnull out_dex_file)142 ADexFile_Error ADexFile_create(const void* _Nonnull address,
143                                size_t size,
144                                size_t* _Nullable new_size,
145                                const char* _Nonnull location,
146                                /*out*/ ADexFile* _Nullable * _Nonnull out_dex_file) {
147   *out_dex_file = nullptr;
148 
149   if (size < sizeof(art::DexFile::Header)) {
150     if (new_size != nullptr) {
151       *new_size = sizeof(art::DexFile::Header);
152     }
153     return ADEXFILE_ERROR_NOT_ENOUGH_DATA;
154   }
155 
156   const art::DexFile::Header* header = reinterpret_cast<const art::DexFile::Header*>(address);
157   uint32_t file_size = header->file_size_;
158   if (art::CompactDexFile::IsMagicValid(header->magic_)) {
159     // Compact dex files store the data section separately so that it can be shared.
160     // Therefore we need to extend the read memory range to include it.
161     // TODO: This might be wasteful as we might read data in between as well.
162     //       In practice, this should be fine, as such sharing only happens on disk.
163     uint32_t computed_file_size;
164     if (__builtin_add_overflow(header->data_off_, header->data_size_, &computed_file_size)) {
165       return ADEXFILE_ERROR_INVALID_HEADER;
166     }
167     if (computed_file_size > file_size) {
168       file_size = computed_file_size;
169     }
170   } else if (!art::StandardDexFile::IsMagicValid(header->magic_)) {
171     return ADEXFILE_ERROR_INVALID_HEADER;
172   }
173 
174   if (size < file_size) {
175     if (new_size != nullptr) {
176       *new_size = file_size;
177     }
178     return ADEXFILE_ERROR_NOT_ENOUGH_DATA;
179   }
180 
181   std::string loc_str(location);
182   art::DexFileLoader loader;
183   std::string error_msg;
184   std::unique_ptr<const art::DexFile> dex_file = loader.Open(static_cast<const uint8_t*>(address),
185                                                              size,
186                                                              loc_str,
187                                                              header->checksum_,
188                                                              /*oat_dex_file=*/nullptr,
189                                                              /*verify=*/false,
190                                                              /*verify_checksum=*/false,
191                                                              &error_msg);
192   if (dex_file == nullptr) {
193     LOG(ERROR) << "Can not open dex file " << loc_str << ": " << error_msg;
194     return ADEXFILE_ERROR_INVALID_DEX;
195   }
196 
197   *out_dex_file = new ADexFile(std::move(dex_file));
198   return ADEXFILE_ERROR_OK;
199 }
200 
ADexFile_destroy(ADexFile * self)201 void ADexFile_destroy(ADexFile* self) {
202   delete self;
203 }
204 
ADexFile_findMethodAtOffset(ADexFile * self,size_t dex_offset,ADexFile_MethodCallback * callback,void * callback_data)205 size_t ADexFile_findMethodAtOffset(ADexFile* self,
206                                    size_t dex_offset,
207                                    ADexFile_MethodCallback* callback,
208                                    void* callback_data) {
209   const art::DexFile* dex_file = self->dex_file_.get();
210   if (!dex_file->IsInDataSection(dex_file->Begin() + dex_offset)) {
211     return 0;  // The DEX offset is not within the bytecode of this dex file.
212   }
213 
214   if (dex_file->IsCompactDexFile()) {
215     // The data section of compact dex files might be shared.
216     // Check the subrange unique to this compact dex.
217     const art::CompactDexFile::Header& cdex_header =
218         dex_file->AsCompactDexFile()->GetHeader();
219     uint32_t begin = cdex_header.data_off_ + cdex_header.OwnedDataBegin();
220     uint32_t end = cdex_header.data_off_ + cdex_header.OwnedDataEnd();
221     if (dex_offset < begin || dex_offset >= end) {
222       return 0;  // The DEX offset is not within the bytecode of this dex file.
223     }
224   }
225 
226   ADexFile_Method info;
227   if (!self->FindMethod(dex_offset, &info)) {
228     return 0;
229   }
230 
231   callback(callback_data, &info);
232   return 1;
233 }
234 
ADexFile_forEachMethod(ADexFile * self,ADexFile_MethodCallback * callback,void * callback_data)235 size_t ADexFile_forEachMethod(ADexFile* self,
236                               ADexFile_MethodCallback* callback,
237                               void* callback_data) {
238   size_t count = 0;
239   for (art::ClassAccessor accessor : self->dex_file_->GetClasses()) {
240     for (const art::ClassAccessor::Method& method : accessor.GetMethods()) {
241       art::CodeItemInstructionAccessor code = method.GetInstructions();
242       if (code.HasCodeItem()) {
243         size_t offset = reinterpret_cast<const uint8_t*>(code.Insns()) - self->dex_file_->Begin();
244         ADexFile_Method info {
245           .adex = self,
246           .index = method.GetIndex(),
247           .offset = offset,
248           .size = code.InsnsSizeInBytes(),
249         };
250         callback(callback_data, &info);
251         count++;
252       }
253     }
254   }
255   return count;
256 }
257 
ADexFile_Method_getCodeOffset(const ADexFile_Method * self,size_t * out_size)258 size_t ADexFile_Method_getCodeOffset(const ADexFile_Method* self,
259                                      size_t* out_size) {
260   if (out_size != nullptr) {
261     *out_size = self->size;
262   }
263   return self->offset;
264 }
265 
ADexFile_Method_getName(const ADexFile_Method * self,size_t * out_size)266 const char* ADexFile_Method_getName(const ADexFile_Method* self,
267                                     size_t* out_size) {
268   const char* name = self->adex->dex_file_->GetMethodName(self->index);
269   if (out_size != nullptr) {
270     *out_size = strlen(name);
271   }
272   return name;
273 }
274 
ADexFile_Method_getQualifiedName(const ADexFile_Method * self,int with_params,size_t * out_size)275 const char* ADexFile_Method_getQualifiedName(const ADexFile_Method* self,
276                                              int with_params,
277                                              size_t* out_size) {
278   std::string& temp = self->adex->temporary_qualified_name_;
279   temp.clear();
280   self->adex->dex_file_->AppendPrettyMethod(self->index, with_params, &temp);
281   if (out_size != nullptr) {
282     *out_size = temp.size();
283   }
284   return temp.data();
285 }
286 
ADexFile_Method_getClassDescriptor(const ADexFile_Method * self,size_t * out_size)287 const char* ADexFile_Method_getClassDescriptor(const ADexFile_Method* self,
288                                                size_t* out_size) {
289   const art::dex::MethodId& method_id = self->adex->dex_file_->GetMethodId(self->index);
290   const char* name = self->adex->dex_file_->GetMethodDeclaringClassDescriptor(method_id);
291   if (out_size != nullptr) {
292     *out_size = strlen(name);
293   }
294   return name;
295 }
296 
ADexFile_Error_toString(ADexFile_Error self)297 const char* ADexFile_Error_toString(ADexFile_Error self) {
298   switch (self) {
299     case ADEXFILE_ERROR_OK: return "Ok";
300     case ADEXFILE_ERROR_INVALID_DEX: return "Dex file is invalid.";
301     case ADEXFILE_ERROR_NOT_ENOUGH_DATA: return "Not enough data. Incomplete dex file.";
302     case ADEXFILE_ERROR_INVALID_HEADER: return "Invalid dex file header.";
303   }
304   return nullptr;
305 }
306 
307 }  // extern "C"
308