Debian Codegraph @

test/core/gprpp/chunked_vector_fuzzer.cc

raw
1 // Copyright 2021 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <stddef.h>
16
17 #include <algorithm>
18 #include <map>
19 #include <memory>
20 #include <utility>
21 #include <vector>
22
23 #include <grpc/event_engine/memory_allocator.h>
24 #include <grpc/support/log.h>
25
26 #include "src/core/lib/gprpp/chunked_vector.h"
27 #include "src/core/lib/gprpp/ref_counted_ptr.h"
28 #include "src/core/lib/resource_quota/arena.h"
29 #include "src/core/lib/resource_quota/memory_quota.h"
30 #include "src/core/lib/resource_quota/resource_quota.h"
31 #include "src/libfuzzer/libfuzzer_macro.h"
32 #include "test/core/gprpp/chunked_vector_fuzzer.pb.h"
33
34 bool squelch = true;
35 bool leak_check = true;
36
37 static auto* g_memory_allocator = new grpc_core::MemoryAllocator(
38 grpc_core::ResourceQuota::Default()->memory_quota()->CreateMemoryAllocator(
39 "test"));
40
41 static constexpr size_t kChunkSize = 17;
42 using IntHdl = std::shared_ptr<int>;
43
44 namespace grpc_core {
45 struct Comparison {
46 explicit Comparison(Arena* arena) : chunked(arena) {}
47
48 ChunkedVector<IntHdl, kChunkSize> chunked;
49 std::vector<IntHdl> std;
50
51 // Check that both chunked and std are equivalent.
52 void AssertOk() const {
53 GPR_ASSERT(std.size() == chunked.size());
54 auto it_chunked = chunked.cbegin();
55 auto it_std = std.cbegin();
56 while (it_std != std.cend()) {
57 GPR_ASSERT(**it_std == **it_chunked);
58 ++it_chunked;
59 ++it_std;
60 }
61 GPR_ASSERT(it_chunked == chunked.cend());
62 }
63 };
64
65 class Fuzzer {
66 public:
67 Fuzzer() = default;
68 ~Fuzzer() = default;
69
70 void Act(const chunked_vector_fuzzer::Action& action) {
71 switch (action.action_type_case()) {
72 case chunked_vector_fuzzer::Action::kEmplaceBack: {
73 // Add some value to the back of a comparison, assert that both vectors
74 // are equivalent.
75 auto* c = Mutate(action.emplace_back().vector());
76 c->chunked.EmplaceBack(
77 std::make_shared<int>(action.emplace_back().value()));
78 c->std.emplace_back(
79 std::make_shared<int>(action.emplace_back().value()));
80 c->AssertOk();
81 } break;
82 case chunked_vector_fuzzer::Action::kPopBack: {
83 // Remove some value to the back of a comparison, assert that both
84 // vectors are equivalent.
85 auto* c = Mutate(action.pop_back().vector());
86 if (!c->chunked.empty()) {
87 c->chunked.PopBack();
88 c->std.pop_back();
89 c->AssertOk();
90 }
91 } break;
92 case chunked_vector_fuzzer::Action::kCopy: {
93 // Copy one vector into another, assert both everything stays
94 // equivalent.
95 auto it_from = vectors_.find(action.copy().from());
96 if (it_from == vectors_.end()) {
97 it_from =
98 vectors_.emplace(action.copy().from(), Comparison(arena_.get()))
99 .first;
100 }
101 auto it_to = vectors_.find(action.copy().to());
102 if (it_to == vectors_.end()) {
103 it_to = vectors_.emplace(action.copy().to(), it_from->second).first;
104 } else {
105 it_to->second = it_from->second;
106 }
107 it_from->second.AssertOk();
108 it_to->second.AssertOk();
109 } break;
110 case chunked_vector_fuzzer::Action::kMove: {
111 // Move one vector into another, assert both everything stays
112 // equivalent.
113 auto it_from = vectors_.find(action.move().from());
114 if (it_from == vectors_.end()) {
115 it_from =
116 vectors_.emplace(action.move().from(), Comparison(arena_.get()))
117 .first;
118 }
119 auto it_to = vectors_.find(action.move().to());
120 if (it_to == vectors_.end()) {
121 it_to =
122 vectors_.emplace(action.move().to(), std::move(it_from->second))
123 .first;
124 } else {
125 it_to->second = it_from->second;
126 }
127 it_from->second.AssertOk();
128 it_to->second.AssertOk();
129 } break;
130 case chunked_vector_fuzzer::Action::kClear: {
131 // Clear a vector, assert that both underlying vectors are equivalent.
132 auto* c = Mutate(action.clear().vector());
133 c->chunked.Clear();
134 c->std.clear();
135 c->AssertOk();
136 } break;
137 case chunked_vector_fuzzer::Action::kSwap: {
138 // Swap two vectors, assert that both underlying vectors are equivalent.
139 auto* from = Mutate(action.swap().from());
140 auto* to = Mutate(action.swap().to());
141 from->chunked.Swap(&to->chunked);
142 from->std.swap(to->std);
143 from->AssertOk();
144 } break;
145 case chunked_vector_fuzzer::Action::kRemoveIf: {
146 // Apply std::remove_if to a vector, assert that underlying vectors
147 // remain equivalent.
148 auto cond = [&](const IntHdl& hdl) {
149 return *hdl == action.remove_if().value();
150 };
151 auto* c = Mutate(action.remove_if().vector());
152 c->chunked.SetEnd(
153 std::remove_if(c->chunked.begin(), c->chunked.end(), cond));
154 c->std.erase(std::remove_if(c->std.begin(), c->std.end(), cond),
155 c->std.end());
156 c->AssertOk();
157 } break;
158 case chunked_vector_fuzzer::Action::ACTION_TYPE_NOT_SET:
159 break;
160 }
161 }
162
163 private:
164 Comparison* Mutate(int index) {
165 auto it = vectors_.find(index);
166 if (it != vectors_.end()) {
167 return &it->second;
168 }
169 return &vectors_.emplace(index, Comparison(arena_.get())).first->second;
170 }
171
172 ScopedArenaPtr arena_ = MakeScopedArena(128, g_memory_allocator);
173 std::map<int, Comparison> vectors_;
174 };
175 } // namespace grpc_core
176
177 DEFINE_PROTO_FUZZER(const chunked_vector_fuzzer::Msg& msg) {
178 grpc_core::Fuzzer fuzzer;
179 for (int i = 0; i < msg.actions_size(); i++) {
180 fuzzer.Act(msg.actions(i));
181 }
182 }