Debian Codegraph @

test/core/transport/binder/end2end/fuzzers/fuzzer_utils.h

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 #ifndef GRPC_TEST_CORE_TRANSPORT_BINDER_END2END_FUZZERS_FUZZER_UTILS_H
16 #define GRPC_TEST_CORE_TRANSPORT_BINDER_END2END_FUZZERS_FUZZER_UTILS_H
17
18 #include <memory>
19 #include <queue>
20 #include <string>
21 #include <thread>
22 #include <vector>
23
24 #include "absl/memory/memory.h"
25 #include "absl/status/status.h"
26
27 #include <grpc/support/log.h>
28
29 #include "src/core/ext/transport/binder/wire_format/binder.h"
30 #include "src/core/ext/transport/binder/wire_format/wire_reader.h"
31 #include "test/core/transport/binder/end2end/fuzzers/binder_transport_fuzzer.pb.h"
32
33 namespace grpc_binder {
34 namespace fuzzing {
35
36 // A WritableParcel implementation that simply does nothing. Don't use
37 // MockWritableParcel here since capturing calls is expensive.
38 class NoOpWritableParcel : public WritableParcel {
39 public:
40 int32_t GetDataSize() const override { return 0; }
41 absl::Status WriteInt32(int32_t /*data*/) override {
42 return absl::OkStatus();
43 }
44 absl::Status WriteInt64(int64_t /*data*/) override {
45 return absl::OkStatus();
46 }
47 absl::Status WriteBinder(HasRawBinder* /*binder*/) override {
48 return absl::OkStatus();
49 }
50 absl::Status WriteString(absl::string_view /*s*/) override {
51 return absl::OkStatus();
52 }
53 absl::Status WriteByteArray(const int8_t* /*buffer*/,
54 int32_t /*length*/) override {
55 return absl::OkStatus();
56 }
57 };
58
59 // Binder implementation used in fuzzing.
60 //
61 // Most of its the functionalities are no-op, except ConstructTxReceiver now
62 // returns a TransactionReceiverForFuzzing.
63 class BinderForFuzzing : public Binder {
64 public:
65 BinderForFuzzing() : input_(std::make_unique<NoOpWritableParcel>()) {}
66
67 explicit BinderForFuzzing(const binder_transport_fuzzer::IncomingParcels& p)
68 : incoming_parcels_(p), input_(std::make_unique<NoOpWritableParcel>()) {}
69
70 void Initialize() override {}
71 absl::Status PrepareTransaction() override { return absl::OkStatus(); }
72
73 absl::Status Transact(BinderTransportTxCode /*tx_code*/) override {
74 return absl::OkStatus();
75 }
76
77 std::unique_ptr<TransactionReceiver> ConstructTxReceiver(
78 grpc_core::RefCountedPtr<WireReader> wire_reader_ref,
79 TransactionReceiver::OnTransactCb cb) const override;
80
81 WritableParcel* GetWritableParcel() const override { return input_.get(); }
82 void* GetRawBinder() override { return nullptr; }
83
84 private:
85 binder_transport_fuzzer::IncomingParcels incoming_parcels_;
86 std::unique_ptr<WritableParcel> input_;
87 };
88
89 // ReadableParcel implementation used in fuzzing.
90 //
91 // It consumes a Parcel generated by mutator, and returns the data in the Parcel
92 // upon user's requests.
93 class ReadableParcelForFuzzing : public ReadableParcel {
94 public:
95 explicit ReadableParcelForFuzzing(const binder_transport_fuzzer::Parcel& p)
96 : parcel_data_size_(p.data_size()), consumed_data_size_(0) {
97 for (const auto& v : p.values()) {
98 values_.push(v);
99 }
100 }
101
102 // Construct from SetupTransportParcel, which have fixed types of data in it.
103 explicit ReadableParcelForFuzzing(
104 const binder_transport_fuzzer::SetupTransportParcel& p)
105 : parcel_data_size_(p.data_size()), consumed_data_size_(0) {
106 // Creates value for protocol version and put it into the queue
107 binder_transport_fuzzer::Value version_value;
108 version_value.set_i32(p.version());
109 values_.push(version_value);
110
111 // Creates a binder value and put it into the queue
112 binder_transport_fuzzer::Value binder_value;
113 binder_value.mutable_binder(); // sets one-of field
114 values_.push(binder_value);
115 }
116
117 int32_t GetDataSize() const override;
118 absl::Status ReadInt32(int32_t* data) override;
119 absl::Status ReadInt64(int64_t* data) override;
120 absl::Status ReadBinder(std::unique_ptr<Binder>* binder) override;
121 absl::Status ReadByteArray(std::string* data) override;
122 absl::Status ReadString(std::string* data) override;
123
124 private:
125 // Stores data/objects in binder in their order. Since we don't support random
126 // access using a std::queue is enough here.
127 std::queue<binder_transport_fuzzer::Value> values_;
128
129 const int32_t parcel_data_size_;
130
131 static constexpr size_t kParcelDataSizeLimit = 1024 * 1024;
132 size_t consumed_data_size_;
133 };
134
135 void JoinFuzzingThread();
136
137 // TransactionReceiver implementation used in fuzzing.
138 //
139 // When constructed, start sending fuzzed requests to the client. When all the
140 // bytes are consumed, the reference to WireReader will be released.
141 class TransactionReceiverForFuzzing : public TransactionReceiver {
142 public:
143 TransactionReceiverForFuzzing(
144 binder_transport_fuzzer::IncomingParcels incoming_parcels,
145 grpc_core::RefCountedPtr<WireReader> wire_reader_ref,
146 TransactionReceiver::OnTransactCb cb);
147
148 void* GetRawBinder() override { return nullptr; }
149 };
150
151 } // namespace fuzzing
152 } // namespace grpc_binder
153
154 #endif // GRPC_TEST_CORE_TRANSPORT_BINDER_END2END_FUZZERS_FUZZER_UTILS_H