1 /*
2  * Copyright (C) 2017 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 #ifndef ART_LIBDEXFILE_DEX_DEX_FILE_TRACKING_REGISTRAR_H_
18 #define ART_LIBDEXFILE_DEX_DEX_FILE_TRACKING_REGISTRAR_H_
19 
20 #include <deque>
21 #include <tuple>
22 
23 #include "dex_file.h"
24 
25 namespace art {
26 namespace dex {
27 namespace tracking {
28 
29 // Class for (un)poisoning various sections of Dex Files
30 //
31 // This class provides the means to log accesses only of sections whose
32 // accesses are needed. All accesses are displayed as stack traces in
33 // logcat.
34 class DexFileTrackingRegistrar {
35  public:
DexFileTrackingRegistrar(const DexFile * const dex_file)36   explicit DexFileTrackingRegistrar(const DexFile* const dex_file)
37       : dex_file_(dex_file) {
38   }
39 
40   // This function is where the functions below it are called to actually
41   // poison sections.
42   void SetDexSections();
43 
44   // Uses data contained inside range_values_ to poison memory through the
45   // memory tool.
46   void SetCurrentRanges();
47 
48  private:
49   void SetDexFileRegistration(bool should_poison);
50 
51   // Set of functions concerning Code Items of dex_file_
52   void SetAllCodeItemRegistration(bool should_poison);
53   // Sets the insns_ section of all code items.
54   void SetAllInsnsRegistration(bool should_poison);
55   // This function finds the code item of a class based on class name.
56   void SetCodeItemRegistration(const char* class_name, bool should_poison);
57   // Sets the size and offset information along with first instruction in insns_
58   // section of all code items.
59   void SetAllCodeItemStartRegistration(bool should_poison);
60 
61   // Set of functions concerning String Data Items of dex_file_
62   void SetAllStringDataRegistration(bool should_poison);
63   // Sets the first byte of size value and data section of all string data
64   // items.
65   void SetAllStringDataStartRegistration(bool should_poison);
66 
67   // Contains tuples of all ranges of memory that need to be explicitly
68   // (un)poisoned by the memory tool.
69   std::deque<std::tuple<const void *, size_t, bool>> range_values_;
70 
71   const DexFile* const dex_file_;
72 };
73 
74 // This function is meant to called externally to use DexfileTrackingRegistrar
75 void RegisterDexFile(const DexFile* dex_file);
76 
77 }  // namespace tracking
78 }  // namespace dex
79 }  // namespace art
80 
81 #endif  // ART_LIBDEXFILE_DEX_DEX_FILE_TRACKING_REGISTRAR_H_
82