zhangmeng
2023-03-01 777333ff834744ac5665fa9abe5ec6373d25cda8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc.  All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
#ifndef GOOGLE_PROTOBUF_FIELD_ACCESS_LISTENER_H__
#define GOOGLE_PROTOBUF_FIELD_ACCESS_LISTENER_H__
 
#include <cstddef>
#include <functional>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
 
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/arenastring.h>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/map.h>
#include <google/protobuf/stubs/once.h>
#include <google/protobuf/repeated_field.h>
 
 
namespace google {
namespace protobuf {
namespace internal {
template <typename T>
struct ResolvedType {
  using type = T;
};
}  // namespace internal
// Tracks the events of field accesses for all protos
// that are built with --inject_field_listener_events. This is a global
// interface which you must implement yourself and register with
// RegisterListener() function. All events consist of Descriptors,
// FieldAccessTypes and the underlying storage for tracking the memory which is
// accessed where possible and makes sense. Users are responsible for the
// implementations to be thread safe.
class FieldAccessListener {
 public:
  FieldAccessListener() = default;
  virtual ~FieldAccessListener() = default;
 
  // The memory annotations of the proto fields that are touched by the
  // accessors. They are returned as if the operation completes.
  struct DataAnnotation {
    DataAnnotation() = default;
    DataAnnotation(const void* other_address, size_t other_size)
        : address(other_address), size(other_size) {}
    const void* address = nullptr;
    size_t size = 0;
  };
  using AddressInfo = std::vector<DataAnnotation>;
  using AddressInfoExtractor = std::function<AddressInfo()>;
 
  enum class FieldAccessType {
    kAdd,          // add_<field>(f)
    kAddMutable,   // add_<field>()
    kGet,          // <field>() and <repeated_field>(i)
    kClear,        // clear_<field>()
    kHas,          // has_<field>()
    kList,         // <repeated_field>()
    kMutable,      // mutable_<field>()
    kMutableList,  // mutable_<repeated_field>()
    kRelease,      // release_<field>()
    kSet,          // set_<field>() and set_<repeated_field>(i)
    kSize,         // <repeated_field>_size()
  };
 
  static FieldAccessListener* GetListener();
 
  // Registers the field listener, can be called only once, |listener| must
  // outlive all proto accesses (in most cases, the lifetime of the program).
  static void RegisterListener(FieldAccessListener* listener);
 
  // All field accessors noted in FieldAccessType have this call.
  // |extractor| extracts the address info from the field
  virtual void OnFieldAccess(const AddressInfoExtractor& extractor,
                             const FieldDescriptor* descriptor,
                             FieldAccessType access_type) = 0;
 
  // Side effect calls.
  virtual void OnDeserializationAccess(const Message* message) = 0;
  virtual void OnSerializationAccess(const Message* message) = 0;
  virtual void OnReflectionAccess(const Descriptor* descriptor) = 0;
  virtual void OnByteSizeAccess(const Message* message) = 0;
  // We can probably add more if we need to, like {Merge,Copy}{From}Access.
 
  // Extracts all the addresses from the underlying fields.
  template <typename T>
  AddressInfo ExtractFieldInfo(const T* field_value);
 
 
 private:
  template <typename T>
  AddressInfo ExtractFieldInfoSpecific(const T* field_value,
                                       internal::ResolvedType<T>);
 
  AddressInfo ExtractFieldInfoSpecific(const Message* field_value,
                                       internal::ResolvedType<Message>);
 
  AddressInfo ExtractFieldInfoSpecific(const std::string* field_value,
                                       internal::ResolvedType<std::string>);
 
  AddressInfo ExtractFieldInfoSpecific(
      const internal::ArenaStringPtr* field_value,
      internal::ResolvedType<internal::ArenaStringPtr>);
 
  template <typename T>
  AddressInfo ExtractFieldInfoSpecific(
      const RepeatedField<T>* field_value,
      internal::ResolvedType<RepeatedField<T>>);
 
  template <typename T>
  AddressInfo ExtractFieldInfoSpecific(
      const RepeatedPtrField<T>* field_value,
      internal::ResolvedType<RepeatedPtrField<T>>);
 
  template <typename K, typename V>
  AddressInfo ExtractFieldInfoSpecific(const Map<K, V>* field_value,
                                       internal::ResolvedType<Map<K, V>>);
 
  static internal::once_flag register_once_;
  static FieldAccessListener* field_listener_;
  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldAccessListener);
};
 
template <typename T>
inline FieldAccessListener::AddressInfo FieldAccessListener::ExtractFieldInfo(
    const T* field_value) {
  return ExtractFieldInfoSpecific(field_value, internal::ResolvedType<T>());
}
 
 
template <typename T>
inline FieldAccessListener::AddressInfo
FieldAccessListener::ExtractFieldInfoSpecific(const T* field_value,
                                              internal::ResolvedType<T>) {
  static_assert(std::is_trivial<T>::value,
                "This overload should be chosen only for trivial types");
  return FieldAccessListener::AddressInfo{FieldAccessListener::DataAnnotation(
      static_cast<const void*>(field_value), sizeof(*field_value))};
}
 
inline FieldAccessListener::AddressInfo
FieldAccessListener::ExtractFieldInfoSpecific(
    const std::string* field_value, internal::ResolvedType<std::string>) {
  return FieldAccessListener::AddressInfo{FieldAccessListener::DataAnnotation(
      static_cast<const void*>(field_value->c_str()), field_value->length())};
}
 
inline FieldAccessListener::AddressInfo
FieldAccessListener::ExtractFieldInfoSpecific(
    const internal::ArenaStringPtr* field_value,
    internal::ResolvedType<internal::ArenaStringPtr>) {
  return FieldAccessListener::ExtractFieldInfoSpecific(
      field_value->GetPointer(), internal::ResolvedType<std::string>());
}
 
template <typename T>
inline FieldAccessListener::AddressInfo
FieldAccessListener::ExtractFieldInfoSpecific(
    const RepeatedField<T>* field_value,
    internal::ResolvedType<RepeatedField<T>>) {
  // TODO(jianzhouzh): This can cause data races. Synchronize this if needed.
  FieldAccessListener::AddressInfo address_info;
  address_info.reserve(field_value->size());
  for (int i = 0, ie = field_value->size(); i < ie; ++i) {
    auto sub = ExtractFieldInfoSpecific(&field_value->Get(i),
                                        internal::ResolvedType<T>());
    address_info.insert(address_info.end(), sub.begin(), sub.end());
  }
  return address_info;
}
 
template <typename T>
inline FieldAccessListener::AddressInfo
FieldAccessListener::ExtractFieldInfoSpecific(
    const RepeatedPtrField<T>* field_value,
    internal::ResolvedType<RepeatedPtrField<T>>) {
  FieldAccessListener::AddressInfo address_info;
  // TODO(jianzhouzh): This can cause data races. Synchronize this if needed.
  address_info.reserve(field_value->size());
  for (int i = 0, ie = field_value->size(); i < ie; ++i) {
    auto sub = ExtractFieldInfoSpecific(&field_value->Get(i),
                                        internal::ResolvedType<T>());
    address_info.insert(address_info.end(), sub.begin(), sub.end());
  }
  return address_info;
}
 
template <typename K, typename V>
inline FieldAccessListener::AddressInfo
FieldAccessListener::ExtractFieldInfoSpecific(
    const Map<K, V>* field_value, internal::ResolvedType<Map<K, V>>) {
  // TODO(jianzhouzh): This can cause data races. Synchronize this if needed.
  FieldAccessListener::AddressInfo address_info;
  address_info.reserve(field_value->size());
  for (auto it = field_value->begin(); it != field_value->end(); ++it) {
    auto sub_first =
        ExtractFieldInfoSpecific(&it->first, internal::ResolvedType<K>());
    auto sub_second =
        ExtractFieldInfoSpecific(&it->second, internal::ResolvedType<V>());
    address_info.insert(address_info.end(), sub_first.begin(), sub_first.end());
    address_info.insert(address_info.end(), sub_second.begin(),
                        sub_second.end());
  }
  return address_info;
}
 
inline FieldAccessListener::AddressInfo
FieldAccessListener::ExtractFieldInfoSpecific(const Message* field_value,
                                              internal::ResolvedType<Message>) {
  // TODO(jianzhouzh): implement and adjust all annotations in the compiler.
  return {};
}
 
}  // namespace protobuf
}  // namespace google
 
#endif  // GOOGLE_PROTOBUF_FIELD_ACCESS_LISTENER_H__