xref: /aosp_15_r20/art/libdexfile/dex/dex_file_tracking_registrar.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2017 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include "dex_file_tracking_registrar.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include <deque>
20*795d594fSAndroid Build Coastguard Worker #include <tuple>
21*795d594fSAndroid Build Coastguard Worker 
22*795d594fSAndroid Build Coastguard Worker #include <android-base/logging.h>
23*795d594fSAndroid Build Coastguard Worker 
24*795d594fSAndroid Build Coastguard Worker // For dex tracking through poisoning. Note: Requires forcing sanitization. This is the reason for
25*795d594fSAndroid Build Coastguard Worker // the ifdefs and early include.
26*795d594fSAndroid Build Coastguard Worker #ifdef ART_DEX_FILE_ACCESS_TRACKING
27*795d594fSAndroid Build Coastguard Worker #ifndef ART_ENABLE_ADDRESS_SANITIZER
28*795d594fSAndroid Build Coastguard Worker #define ART_ENABLE_ADDRESS_SANITIZER
29*795d594fSAndroid Build Coastguard Worker #endif
30*795d594fSAndroid Build Coastguard Worker #endif
31*795d594fSAndroid Build Coastguard Worker #include "base/memory_tool.h"
32*795d594fSAndroid Build Coastguard Worker 
33*795d594fSAndroid Build Coastguard Worker #include "class_accessor-inl.h"
34*795d594fSAndroid Build Coastguard Worker #include "code_item_accessors-inl.h"
35*795d594fSAndroid Build Coastguard Worker #include "dex_file-inl.h"
36*795d594fSAndroid Build Coastguard Worker 
37*795d594fSAndroid Build Coastguard Worker namespace art {
38*795d594fSAndroid Build Coastguard Worker namespace dex {
39*795d594fSAndroid Build Coastguard Worker namespace tracking {
40*795d594fSAndroid Build Coastguard Worker 
41*795d594fSAndroid Build Coastguard Worker // If true, poison dex files to track accesses.
42*795d594fSAndroid Build Coastguard Worker static constexpr bool kDexFileAccessTracking =
43*795d594fSAndroid Build Coastguard Worker #ifdef ART_DEX_FILE_ACCESS_TRACKING
44*795d594fSAndroid Build Coastguard Worker     true;
45*795d594fSAndroid Build Coastguard Worker #else
46*795d594fSAndroid Build Coastguard Worker     false;
47*795d594fSAndroid Build Coastguard Worker #endif
48*795d594fSAndroid Build Coastguard Worker 
49*795d594fSAndroid Build Coastguard Worker // The following are configurations of poisoning certain sections of a Dex File.
50*795d594fSAndroid Build Coastguard Worker // More will be added
51*795d594fSAndroid Build Coastguard Worker enum DexTrackingType {
52*795d594fSAndroid Build Coastguard Worker   // Poisons all of a Dex File when set.
53*795d594fSAndroid Build Coastguard Worker   kWholeDexTracking,
54*795d594fSAndroid Build Coastguard Worker   // Poisons all Code Items of a Dex File when set.
55*795d594fSAndroid Build Coastguard Worker   kCodeItemTracking,
56*795d594fSAndroid Build Coastguard Worker   // Poisons all subsections of a Code Item, except the Insns bytecode array
57*795d594fSAndroid Build Coastguard Worker   // section, when set for all Code Items in a Dex File.
58*795d594fSAndroid Build Coastguard Worker   kCodeItemNonInsnsTracking,
59*795d594fSAndroid Build Coastguard Worker   // Poisons all subsections of a Code Item, except the Insns bytecode array
60*795d594fSAndroid Build Coastguard Worker   // section, when set for all Code Items in a Dex File.
61*795d594fSAndroid Build Coastguard Worker   // Additionally unpoisons the entire Code Item when method is a class
62*795d594fSAndroid Build Coastguard Worker   // initializer.
63*795d594fSAndroid Build Coastguard Worker   kCodeItemNonInsnsNoClinitTracking,
64*795d594fSAndroid Build Coastguard Worker   // Poisons the size and offset information along with the first instruction.
65*795d594fSAndroid Build Coastguard Worker   // This is so that accessing multiple instructions while accessing a code item
66*795d594fSAndroid Build Coastguard Worker   // once will not trigger unnecessary accesses.
67*795d594fSAndroid Build Coastguard Worker   kCodeItemStartTracking,
68*795d594fSAndroid Build Coastguard Worker   // Poisons all String Data Items of a Dex Files when set.
69*795d594fSAndroid Build Coastguard Worker   kStringDataItemTracking,
70*795d594fSAndroid Build Coastguard Worker   // Poisons the first byte of the utf16_size value and the first byte of the
71*795d594fSAndroid Build Coastguard Worker   // data section for all String Data Items of a Dex File.
72*795d594fSAndroid Build Coastguard Worker   kStringDataItemStartTracking,
73*795d594fSAndroid Build Coastguard Worker   // Poisons based on a custom tracking system which can be specified in
74*795d594fSAndroid Build Coastguard Worker   // SetDexSections
75*795d594fSAndroid Build Coastguard Worker   kCustomTracking,
76*795d594fSAndroid Build Coastguard Worker };
77*795d594fSAndroid Build Coastguard Worker 
78*795d594fSAndroid Build Coastguard Worker // Intended for local changes only.
79*795d594fSAndroid Build Coastguard Worker // Represents the current configuration being run.
80*795d594fSAndroid Build Coastguard Worker static constexpr DexTrackingType kCurrentTrackingSystem = kWholeDexTracking;
81*795d594fSAndroid Build Coastguard Worker 
82*795d594fSAndroid Build Coastguard Worker // Intended for local changes only.
SetDexSections()83*795d594fSAndroid Build Coastguard Worker void DexFileTrackingRegistrar::SetDexSections() {
84*795d594fSAndroid Build Coastguard Worker   if (kDexFileAccessTracking && dex_file_ != nullptr) {
85*795d594fSAndroid Build Coastguard Worker     // Logs the Dex File's location and starting address if tracking is enabled
86*795d594fSAndroid Build Coastguard Worker     LOG(ERROR) << "RegisterDexFile: " << dex_file_->GetLocation() + " @ " << std::hex
87*795d594fSAndroid Build Coastguard Worker                << reinterpret_cast<uintptr_t>(dex_file_->Begin());
88*795d594fSAndroid Build Coastguard Worker     switch (kCurrentTrackingSystem) {
89*795d594fSAndroid Build Coastguard Worker       case kWholeDexTracking:
90*795d594fSAndroid Build Coastguard Worker         SetDexFileRegistration(true);
91*795d594fSAndroid Build Coastguard Worker         break;
92*795d594fSAndroid Build Coastguard Worker       case kCodeItemTracking:
93*795d594fSAndroid Build Coastguard Worker         SetAllCodeItemRegistration(true);
94*795d594fSAndroid Build Coastguard Worker         break;
95*795d594fSAndroid Build Coastguard Worker       case kCodeItemNonInsnsTracking:
96*795d594fSAndroid Build Coastguard Worker         SetAllCodeItemRegistration(true);
97*795d594fSAndroid Build Coastguard Worker         SetAllInsnsRegistration(false);
98*795d594fSAndroid Build Coastguard Worker         break;
99*795d594fSAndroid Build Coastguard Worker       case kCodeItemNonInsnsNoClinitTracking:
100*795d594fSAndroid Build Coastguard Worker         SetAllCodeItemRegistration(true);
101*795d594fSAndroid Build Coastguard Worker         SetAllInsnsRegistration(false);
102*795d594fSAndroid Build Coastguard Worker         SetCodeItemRegistration("<clinit>", false);
103*795d594fSAndroid Build Coastguard Worker         break;
104*795d594fSAndroid Build Coastguard Worker       case kCodeItemStartTracking:
105*795d594fSAndroid Build Coastguard Worker         SetAllCodeItemStartRegistration(true);
106*795d594fSAndroid Build Coastguard Worker         break;
107*795d594fSAndroid Build Coastguard Worker       case kStringDataItemTracking:
108*795d594fSAndroid Build Coastguard Worker         SetAllStringDataRegistration(true);
109*795d594fSAndroid Build Coastguard Worker         break;
110*795d594fSAndroid Build Coastguard Worker       case kStringDataItemStartTracking:
111*795d594fSAndroid Build Coastguard Worker         SetAllStringDataStartRegistration(true);
112*795d594fSAndroid Build Coastguard Worker         break;
113*795d594fSAndroid Build Coastguard Worker       case kCustomTracking:
114*795d594fSAndroid Build Coastguard Worker         // TODO: Add/remove additional calls here to (un)poison sections of
115*795d594fSAndroid Build Coastguard Worker         // dex_file_
116*795d594fSAndroid Build Coastguard Worker         break;
117*795d594fSAndroid Build Coastguard Worker       default:
118*795d594fSAndroid Build Coastguard Worker         break;
119*795d594fSAndroid Build Coastguard Worker     }
120*795d594fSAndroid Build Coastguard Worker   }
121*795d594fSAndroid Build Coastguard Worker }
122*795d594fSAndroid Build Coastguard Worker 
RegisterDexFile(const DexFile * dex_file)123*795d594fSAndroid Build Coastguard Worker void RegisterDexFile(const DexFile* dex_file) {
124*795d594fSAndroid Build Coastguard Worker   DexFileTrackingRegistrar dex_tracking_registrar(dex_file);
125*795d594fSAndroid Build Coastguard Worker   dex_tracking_registrar.SetDexSections();
126*795d594fSAndroid Build Coastguard Worker   dex_tracking_registrar.SetCurrentRanges();
127*795d594fSAndroid Build Coastguard Worker }
128*795d594fSAndroid Build Coastguard Worker 
SetRegistrationRange(const void * begin,size_t size,bool should_poison)129*795d594fSAndroid Build Coastguard Worker inline void SetRegistrationRange(const void* begin, size_t size, bool should_poison) {
130*795d594fSAndroid Build Coastguard Worker   if (should_poison) {
131*795d594fSAndroid Build Coastguard Worker     MEMORY_TOOL_MAKE_NOACCESS(begin, size);
132*795d594fSAndroid Build Coastguard Worker   } else {
133*795d594fSAndroid Build Coastguard Worker     // Note: MEMORY_TOOL_MAKE_UNDEFINED has the same functionality with Address
134*795d594fSAndroid Build Coastguard Worker     // Sanitizer.
135*795d594fSAndroid Build Coastguard Worker     // Historical note: The difference has not been tested with Valgrind.
136*795d594fSAndroid Build Coastguard Worker     MEMORY_TOOL_MAKE_DEFINED(begin, size);
137*795d594fSAndroid Build Coastguard Worker   }
138*795d594fSAndroid Build Coastguard Worker }
139*795d594fSAndroid Build Coastguard Worker 
SetCurrentRanges()140*795d594fSAndroid Build Coastguard Worker void DexFileTrackingRegistrar::SetCurrentRanges() {
141*795d594fSAndroid Build Coastguard Worker   // This also empties range_values_ to avoid redundant (un)poisoning upon
142*795d594fSAndroid Build Coastguard Worker   // subsequent calls.
143*795d594fSAndroid Build Coastguard Worker   while (!range_values_.empty()) {
144*795d594fSAndroid Build Coastguard Worker     const std::tuple<const void*, size_t, bool>& current_range = range_values_.front();
145*795d594fSAndroid Build Coastguard Worker     SetRegistrationRange(std::get<0>(current_range),
146*795d594fSAndroid Build Coastguard Worker                          std::get<1>(current_range),
147*795d594fSAndroid Build Coastguard Worker                          std::get<2>(current_range));
148*795d594fSAndroid Build Coastguard Worker     range_values_.pop_front();
149*795d594fSAndroid Build Coastguard Worker   }
150*795d594fSAndroid Build Coastguard Worker }
151*795d594fSAndroid Build Coastguard Worker 
SetDexFileRegistration(bool should_poison)152*795d594fSAndroid Build Coastguard Worker void DexFileTrackingRegistrar::SetDexFileRegistration(bool should_poison) {
153*795d594fSAndroid Build Coastguard Worker   const void* dex_file_begin = reinterpret_cast<const void*>(dex_file_->Begin());
154*795d594fSAndroid Build Coastguard Worker   size_t dex_file_size = dex_file_->Size();
155*795d594fSAndroid Build Coastguard Worker   range_values_.push_back(std::make_tuple(dex_file_begin, dex_file_size, should_poison));
156*795d594fSAndroid Build Coastguard Worker }
157*795d594fSAndroid Build Coastguard Worker 
SetAllCodeItemRegistration(bool should_poison)158*795d594fSAndroid Build Coastguard Worker void DexFileTrackingRegistrar::SetAllCodeItemRegistration(bool should_poison) {
159*795d594fSAndroid Build Coastguard Worker   for (ClassAccessor accessor : dex_file_->GetClasses()) {
160*795d594fSAndroid Build Coastguard Worker     for (const ClassAccessor::Method& method : accessor.GetMethods()) {
161*795d594fSAndroid Build Coastguard Worker       const dex::CodeItem* code_item = method.GetCodeItem();
162*795d594fSAndroid Build Coastguard Worker       if (code_item != nullptr) {
163*795d594fSAndroid Build Coastguard Worker         const void* code_item_begin = reinterpret_cast<const void*>(code_item);
164*795d594fSAndroid Build Coastguard Worker         size_t code_item_size = dex_file_->GetCodeItemSize(*code_item);
165*795d594fSAndroid Build Coastguard Worker         range_values_.push_back(std::make_tuple(code_item_begin, code_item_size, should_poison));
166*795d594fSAndroid Build Coastguard Worker       }
167*795d594fSAndroid Build Coastguard Worker     }
168*795d594fSAndroid Build Coastguard Worker   }
169*795d594fSAndroid Build Coastguard Worker }
170*795d594fSAndroid Build Coastguard Worker 
SetAllCodeItemStartRegistration(bool should_poison)171*795d594fSAndroid Build Coastguard Worker void DexFileTrackingRegistrar::SetAllCodeItemStartRegistration(bool should_poison) {
172*795d594fSAndroid Build Coastguard Worker   for (ClassAccessor class_accessor : dex_file_->GetClasses()) {
173*795d594fSAndroid Build Coastguard Worker     for (const ClassAccessor::Method& method : class_accessor.GetMethods()) {
174*795d594fSAndroid Build Coastguard Worker       const dex::CodeItem* code_item = method.GetCodeItem();
175*795d594fSAndroid Build Coastguard Worker       if (code_item != nullptr) {
176*795d594fSAndroid Build Coastguard Worker         const void* code_item_begin = reinterpret_cast<const void*>(code_item);
177*795d594fSAndroid Build Coastguard Worker         size_t code_item_start = reinterpret_cast<size_t>(code_item);
178*795d594fSAndroid Build Coastguard Worker         CodeItemInstructionAccessor accessor(*dex_file_, code_item);
179*795d594fSAndroid Build Coastguard Worker         size_t code_item_start_end = reinterpret_cast<size_t>(accessor.Insns());
180*795d594fSAndroid Build Coastguard Worker         size_t code_item_start_size = code_item_start_end - code_item_start;
181*795d594fSAndroid Build Coastguard Worker         range_values_.push_back(std::make_tuple(code_item_begin,
182*795d594fSAndroid Build Coastguard Worker                                                 code_item_start_size,
183*795d594fSAndroid Build Coastguard Worker                                                 should_poison));
184*795d594fSAndroid Build Coastguard Worker       }
185*795d594fSAndroid Build Coastguard Worker     }
186*795d594fSAndroid Build Coastguard Worker   }
187*795d594fSAndroid Build Coastguard Worker }
188*795d594fSAndroid Build Coastguard Worker 
SetAllInsnsRegistration(bool should_poison)189*795d594fSAndroid Build Coastguard Worker void DexFileTrackingRegistrar::SetAllInsnsRegistration(bool should_poison) {
190*795d594fSAndroid Build Coastguard Worker   for (ClassAccessor class_accessor : dex_file_->GetClasses()) {
191*795d594fSAndroid Build Coastguard Worker     for (const ClassAccessor::Method& method : class_accessor.GetMethods()) {
192*795d594fSAndroid Build Coastguard Worker       const dex::CodeItem* code_item = method.GetCodeItem();
193*795d594fSAndroid Build Coastguard Worker       if (code_item != nullptr) {
194*795d594fSAndroid Build Coastguard Worker         CodeItemInstructionAccessor accessor(*dex_file_, code_item);
195*795d594fSAndroid Build Coastguard Worker         const void* insns_begin = reinterpret_cast<const void*>(accessor.Insns());
196*795d594fSAndroid Build Coastguard Worker         // Member insns_size_in_code_units_ is in 2-byte units
197*795d594fSAndroid Build Coastguard Worker         size_t insns_size = accessor.InsnsSizeInCodeUnits() * 2;
198*795d594fSAndroid Build Coastguard Worker         range_values_.push_back(std::make_tuple(insns_begin, insns_size, should_poison));
199*795d594fSAndroid Build Coastguard Worker       }
200*795d594fSAndroid Build Coastguard Worker     }
201*795d594fSAndroid Build Coastguard Worker   }
202*795d594fSAndroid Build Coastguard Worker }
203*795d594fSAndroid Build Coastguard Worker 
SetCodeItemRegistration(const char * class_name,bool should_poison)204*795d594fSAndroid Build Coastguard Worker void DexFileTrackingRegistrar::SetCodeItemRegistration(const char* class_name, bool should_poison) {
205*795d594fSAndroid Build Coastguard Worker   for (ClassAccessor accessor : dex_file_->GetClasses()) {
206*795d594fSAndroid Build Coastguard Worker     for (const ClassAccessor::Method& method : accessor.GetMethods()) {
207*795d594fSAndroid Build Coastguard Worker       const dex::MethodId& methodid_item = dex_file_->GetMethodId(method.GetIndex());
208*795d594fSAndroid Build Coastguard Worker       const char * methodid_name = dex_file_->GetMethodName(methodid_item);
209*795d594fSAndroid Build Coastguard Worker       const dex::CodeItem* code_item = method.GetCodeItem();
210*795d594fSAndroid Build Coastguard Worker       if (code_item != nullptr && strcmp(methodid_name, class_name) == 0) {
211*795d594fSAndroid Build Coastguard Worker         const void* code_item_begin = reinterpret_cast<const void*>(code_item);
212*795d594fSAndroid Build Coastguard Worker         size_t code_item_size = dex_file_->GetCodeItemSize(*code_item);
213*795d594fSAndroid Build Coastguard Worker         range_values_.push_back(std::make_tuple(code_item_begin, code_item_size, should_poison));
214*795d594fSAndroid Build Coastguard Worker       }
215*795d594fSAndroid Build Coastguard Worker     }
216*795d594fSAndroid Build Coastguard Worker   }
217*795d594fSAndroid Build Coastguard Worker }
218*795d594fSAndroid Build Coastguard Worker 
SetAllStringDataStartRegistration(bool should_poison)219*795d594fSAndroid Build Coastguard Worker void DexFileTrackingRegistrar::SetAllStringDataStartRegistration(bool should_poison) {
220*795d594fSAndroid Build Coastguard Worker   for (size_t stringid_ctr = 0; stringid_ctr < dex_file_->NumStringIds(); ++stringid_ctr) {
221*795d594fSAndroid Build Coastguard Worker     const dex::StringId & string_id = dex_file_->GetStringId(StringIndex(stringid_ctr));
222*795d594fSAndroid Build Coastguard Worker     const void* string_data_begin = reinterpret_cast<const void*>(dex_file_->Begin() + string_id.string_data_off_);
223*795d594fSAndroid Build Coastguard Worker     // Data Section of String Data Item
224*795d594fSAndroid Build Coastguard Worker     const void* string_data_data_begin = reinterpret_cast<const void*>(dex_file_->GetStringData(string_id));
225*795d594fSAndroid Build Coastguard Worker     range_values_.push_back(std::make_tuple(string_data_begin, 1, should_poison));
226*795d594fSAndroid Build Coastguard Worker     range_values_.push_back(std::make_tuple(string_data_data_begin, 1, should_poison));
227*795d594fSAndroid Build Coastguard Worker   }
228*795d594fSAndroid Build Coastguard Worker }
229*795d594fSAndroid Build Coastguard Worker 
SetAllStringDataRegistration(bool should_poison)230*795d594fSAndroid Build Coastguard Worker void DexFileTrackingRegistrar::SetAllStringDataRegistration(bool should_poison) {
231*795d594fSAndroid Build Coastguard Worker   size_t map_offset = dex_file_->GetHeader().map_off_;
232*795d594fSAndroid Build Coastguard Worker   auto map_list = reinterpret_cast<const dex::MapList*>(dex_file_->Begin() + map_offset);
233*795d594fSAndroid Build Coastguard Worker   for (size_t map_ctr = 0; map_ctr < map_list->size_; ++map_ctr) {
234*795d594fSAndroid Build Coastguard Worker     const dex::MapItem& map_item = map_list->list_[map_ctr];
235*795d594fSAndroid Build Coastguard Worker     if (map_item.type_ == DexFile::kDexTypeStringDataItem) {
236*795d594fSAndroid Build Coastguard Worker       const dex::MapItem& next_map_item = map_list->list_[map_ctr + 1];
237*795d594fSAndroid Build Coastguard Worker       const void* string_data_begin = reinterpret_cast<const void*>(dex_file_->Begin() + map_item.offset_);
238*795d594fSAndroid Build Coastguard Worker       size_t string_data_size = next_map_item.offset_ - map_item.offset_;
239*795d594fSAndroid Build Coastguard Worker       range_values_.push_back(std::make_tuple(string_data_begin, string_data_size, should_poison));
240*795d594fSAndroid Build Coastguard Worker     }
241*795d594fSAndroid Build Coastguard Worker   }
242*795d594fSAndroid Build Coastguard Worker }
243*795d594fSAndroid Build Coastguard Worker 
244*795d594fSAndroid Build Coastguard Worker }  // namespace tracking
245*795d594fSAndroid Build Coastguard Worker }  // namespace dex
246*795d594fSAndroid Build Coastguard Worker }  // namespace art
247