Debian Codegraph @

c/tests/fuzz/fuzz-proactor-receive.c

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21
22 #define _POSIX_C_SOURCE 200809L
23
24 #include <proton/connection.h>
25 #include <proton/connection_driver.h>
26 #include <proton/delivery.h>
27 #include <proton/link.h>
28 #include <proton/message.h>
29 #include <proton/proactor.h>
30 #include <proton/session.h>
31 #include <proton/transport.h>
32 #include <proton/url.h>
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <time.h>
38 #include <unistd.h>
39
40 #include <arpa/inet.h>
41 #include <errno.h>
42 #include <errno.h>
43 #include <strings.h>
44 #include <sys/socket.h>
45 #include <sys/wait.h>
46
47 #include "libFuzzingEngine.h"
48
49 // bool VERBOSE = true;
50 bool VERBOSE = false;
51 // bool ERRORS = true;
52 bool ERRORS = false;
53
54 #define MAX_SIZE 1024
55
56 typedef char str[MAX_SIZE];
57
58 typedef struct app_data_t {
59 str address;
60 str container_id;
61 pn_rwbytes_t message_buffer;
62 int message_count;
63 int received;
64 pn_proactor_t *proactor;
65 bool finished;
66 } app_data_t;
67
68 static const int BATCH = 1000; /* Batch size for unlimited receive */
69
70 static int exit_code = 0;
71
72 static void check_condition(pn_event_t *e, pn_condition_t *cond) {
73 if (VERBOSE)
74 printf("beginning check_condition\n");
75 if (pn_condition_is_set(cond)) {
76 exit_code = 1;
77 if (VERBOSE || ERRORS)
78 fprintf(stderr, "%s: %s: %s\n", pn_event_type_name(pn_event_type(e)),
79 pn_condition_get_name(cond), pn_condition_get_description(cond));
80 }
81 }
82
83 static void decode_message(pn_delivery_t *dlv) {
84 static char buffer[MAX_SIZE];
85 ssize_t len;
86 // try to decode the message body
87 if (pn_delivery_pending(dlv) < MAX_SIZE) {
88 // read in the raw data
89 len = pn_link_recv(pn_delivery_link(dlv), buffer, MAX_SIZE);
90 if (len > 0) {
91 // decode it into a proton message
92 pn_message_t *m = pn_message();
93 if (PN_OK == pn_message_decode(m, buffer, len)) {
94 pn_string_t *s = pn_string(NULL);
95 pn_inspect(pn_message_body(m), s);
96 printf("%s\n", pn_string_get(s));
97 pn_free(s);
98 }
99 pn_message_free(m);
100 }
101 }
102 }
103
104 static void handle(app_data_t *app, pn_event_t *event) {
105 switch (pn_event_type(event)) {
106
107 case PN_CONNECTION_INIT: {
108 pn_connection_t *c = pn_event_connection(event);
109 pn_connection_set_container(c, app->container_id);
110 pn_connection_open(c);
111 pn_session_t *s = pn_session(c);
112 pn_session_open(s);
113 pn_link_t *l = pn_receiver(s, "my_receiver");
114 pn_terminus_set_address(pn_link_source(l), app->address);
115 pn_link_open(l);
116 /* cannot receive without granting credit: */
117 pn_link_flow(l, app->message_count ? app->message_count : BATCH);
118 } break;
119
120 case PN_DELIVERY: {
121 /* A message has been received */
122 pn_link_t *link = NULL;
123 pn_delivery_t *dlv = pn_event_delivery(event);
124 if (pn_delivery_readable(dlv) && !pn_delivery_partial(dlv)) {
125 link = pn_delivery_link(dlv);
126 decode_message(dlv);
127 /* Accept the delivery */
128 pn_delivery_update(dlv, PN_ACCEPTED);
129 /* done with the delivery, move to the next and free it */
130 pn_link_advance(link);
131 pn_delivery_settle(dlv); /* dlv is now freed */
132
133 if (app->message_count == 0) {
134 /* receive forever - see if more credit is needed */
135 if (pn_link_credit(link) < BATCH / 2) {
136 /* Grant enough credit to bring it up to BATCH: */
137 pn_link_flow(link, BATCH - pn_link_credit(link));
138 }
139 } else if (++app->received >= app->message_count) {
140 /* done receiving, close the endpoints */
141 printf("%d messages received\n", app->received);
142 pn_session_t *ssn = pn_link_session(link);
143 pn_link_close(link);
144 pn_session_close(ssn);
145 pn_connection_close(pn_session_connection(ssn));
146 }
147 }
148 } break;
149
150 case PN_TRANSPORT_ERROR:
151 check_condition(event, pn_transport_condition(pn_event_transport(event)));
152 pn_connection_close(pn_event_connection(event));
153 break;
154
155 case PN_CONNECTION_REMOTE_CLOSE:
156 check_condition(event,
157 pn_connection_remote_condition(pn_event_connection(event)));
158 pn_connection_close(pn_event_connection(event));
159 break;
160
161 case PN_SESSION_REMOTE_CLOSE:
162 check_condition(event,
163 pn_session_remote_condition(pn_event_session(event)));
164 pn_connection_close(pn_event_connection(event));
165 break;
166
167 case PN_LINK_REMOTE_CLOSE:
168 case PN_LINK_REMOTE_DETACH:
169 check_condition(event, pn_link_remote_condition(pn_event_link(event)));
170 pn_connection_close(pn_event_connection(event));
171 break;
172
173 case PN_PROACTOR_INACTIVE:
174 app->finished = true;
175 break;
176
177 default:
178 break;
179 }
180 }
181
182 double now(void) {
183 struct timespec spec;
184 if (clock_gettime(CLOCK_MONOTONIC, &spec) != 0) {
185 perror("clock_gettime");
186 exit(errno);
187 }
188 return (double)spec.tv_sec + (double)spec.tv_nsec / 1000000.0;
189 }
190
191 int sut(void) {
192 /* Default values for application and connection. */
193 app_data_t app = {{0}};
194 app.message_count = 2;
195
196 snprintf(app.container_id, sizeof(app.container_id), "%s:%d",
197 "fuzz_proactor_recv", getpid());
198
199 const char *address = "127.0.0.1:amqp";
200 strncpy(app.address, "jms.queue.example", sizeof(app.address));
201
202 if (VERBOSE)
203 printf("before proactor\n");
204 /* Create the proactor and connect */
205 app.proactor = pn_proactor();
206 pn_proactor_connect(app.proactor, pn_connection(), address);
207
208 if (VERBOSE)
209 printf("before loop\n");
210 double thence = now();
211 do {
212 if (VERBOSE)
213 printf("before set proactor timeout\n");
214 pn_proactor_set_timeout(app.proactor, 100);
215 if (VERBOSE)
216 printf("before proactor wait\n");
217 pn_event_batch_t *events = pn_proactor_wait(app.proactor);
218 pn_event_t *e;
219 if (VERBOSE)
220 printf("before proactor next batch\n");
221 while ((e = pn_event_batch_next(events))) {
222 handle(&app, e);
223 }
224 pn_proactor_done(app.proactor, events);
225
226 if (VERBOSE)
227 printf("before reloop\n");
228 double deltat = now() - thence;
229 if (VERBOSE)
230 printf("deltat %f", deltat);
231 if (deltat > 1) {
232 app.finished = true;
233 }
234 } while (!app.finished);
235
236 if (VERBOSE)
237 printf("after loop\n");
238 pn_proactor_free(app.proactor);
239 free(app.message_buffer.start);
240 return exit_code;
241 }
242
243 void serve_data(const uint8_t *Data, size_t Size) {
244 int sockfd;
245 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
246 perror("socket");
247 _Exit(errno);
248 }
249 int reuseaddr = 1;
250 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr,
251 sizeof(reuseaddr)) == -1) {
252 perror("setsockopt");
253 _Exit(errno);
254 }
255 struct sockaddr_in self;
256 memset(&self, 0, sizeof(self));
257 self.sin_family = AF_INET;
258 self.sin_port = htons(5672);
259 self.sin_addr.s_addr = INADDR_ANY;
260 if (bind(sockfd, (struct sockaddr *)&self, sizeof(self)) != 0) {
261 perror("bind");
262
263 // Lets unblock the old child that listens by starting new client to read
264 // from it. It breaks the fuzzing somewhat, but it is better to mess up one
265 // than many inputs.
266 if (VERBOSE)
267 printf("unblocking old bound child\n");
268 kill(getppid(), SIGUSR1);
269
270 _Exit(errno);
271 }
272 if (VERBOSE)
273 printf("bound\n");
274 if (listen(sockfd, 1) != 0) {
275 perror("listen");
276 _Exit(errno);
277 }
278
279 if (VERBOSE)
280 printf("listened, lets run sut\n");
281 kill(getppid(), SIGUSR1);
282
283 struct sockaddr_in client_addr;
284 socklen_t addrlen = sizeof(client_addr);
285 int clientfd = accept(sockfd, (struct sockaddr *)&client_addr, &addrlen);
286 if (VERBOSE)
287 printf("%s:%d connected\n", inet_ntoa(client_addr.sin_addr),
288 ntohs(client_addr.sin_port));
289 if (VERBOSE)
290 printf("will send\n");
291 send(clientfd, Data, Size, 0);
292 // sleep(1);
293 close(clientfd);
294 close(sockfd);
295 if (VERBOSE)
296 printf("done serving\n");
297 }
298
299 void run_sut(int s) {
300 if (VERBOSE)
301 printf("running sut\n");
302 sut();
303 if (VERBOSE)
304 printf("finished running sut\n");
305 }
306
307 void signal_callback_handler(int signum) {
308 if (VERBOSE)
309 printf("Caught signal SIGPIPE %d\n", signum);
310 }
311
312 bool DoInitialization(void) {
313 struct sigaction sa;
314 sa.sa_handler = run_sut;
315 sigemptyset(&sa.sa_mask);
316 sa.sa_flags = SA_RESTART; /* Restart functions if interrupted by handler */
317 if (sigaction(SIGUSR1, &sa, NULL) == -1) {
318 perror("sigaction");
319 exit(2);
320 }
321 sa.sa_handler = signal_callback_handler;
322 if (sigaction(SIGPIPE, &sa, NULL) == -1) {
323 perror("sigaction");
324 exit(2);
325 }
326 return true;
327 }
328
329 int LLVMFuzzerInitialize(int *argc, char ***argv) {
330 DoInitialization();
331 return 0;
332 }
333
334 int prev_pid = 0;
335
336 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
337 // sometimes, esp. with AFL, but libFuzz too,
338 // the old socket is still bound for new run and
339 // it skips all new runs...
340 if (prev_pid != 0) {
341 kill(SIGKILL, prev_pid);
342 }
343
344 pid_t pid = fork();
345 if (pid < 0) {
346 perror("fork");
347 exit(errno);
348 }
349 if (pid == 0) { // child
350 serve_data(Data, Size);
351 _Exit(0);
352 } else { // parent
353 prev_pid = pid;
354 if (VERBOSE)
355 printf("waiting for child\n");
356 siginfo_t status;
357 waitid(P_PID, pid, &status, WEXITED);
358 if (VERBOSE)
359 printf("finished waiting for child\n");
360 }
361 return 0;
362 }