Debian Codegraph @

c/tests/fuzz/fuzz-connection-driver.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 #include <stdint.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "proton/connection_driver.h"
28 #include "proton/engine.h"
29 #include "proton/logger.h"
30 #include "proton/message.h"
31
32 #include "libFuzzingEngine.h"
33
34 // This fuzzer is a variant of the receive.c proactor example
35
36 #define MAX_SIZE 1024
37
38 typedef char str[MAX_SIZE];
39
40 typedef struct app_data_t {
41 str container_id;
42 pn_rwbytes_t message_buffer;
43 int message_count;
44 int received;
45 } app_data_t;
46
47 static void fdc_write(pn_connection_driver_t *driver);
48 size_t fcd_read(pn_connection_driver_t *driver, uint8_t **data, size_t *size);
49 static void decode_message(pn_delivery_t *dlv);
50 static void handle(app_data_t *app, pn_event_t *event);
51 static void check_condition(pn_event_t *e, pn_condition_t *cond);
52
53 // const bool VERBOSE = true;
54 const bool VERBOSE = false;
55 // const bool ERRORS = true;
56 const bool ERRORS = false;
57
58 // I could not get rid of the error messages on stderr in any other way
59 void devnull(intptr_t context, pn_log_subsystem_t sub, pn_log_level_t sev, const char *message) {}
60
61 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
62 if (VERBOSE)
63 printf("BEGIN LLVMFuzzerTestOneInput\n");
64 app_data_t app = {{0}};
65 sprintf(app.container_id, "%s:%06x",
66 "fuzz_connection_driver", rand() & 0xffffff);
67
68 pn_connection_driver_t driver;
69 if (pn_connection_driver_init(&driver, NULL, NULL) != 0) {
70 printf("pn_connection_driver_init\n");
71 exit(1);
72 }
73
74 pn_logger_set_log_sink(pn_default_logger(), devnull, 0);
75
76 uint8_t *data = (uint8_t *)Data;
77 size_t size = Size;
78
79 fdc_write(&driver);
80
81 pn_event_t *event;
82 while ((event = pn_connection_driver_next_event(&driver)) != NULL) {
83 handle(&app, event);
84 }
85
86 fdc_write(&driver);
87
88 do {
89 fdc_write(&driver);
90 fcd_read(&driver, &data, &size);
91 if (VERBOSE)
92 printf("size is %d, data is %p\n", (int)size, (void *)data);
93 while ((event = pn_connection_driver_next_event(&driver)) != NULL) {
94 handle(&app, event);
95 }
96 } while (size > 0);
97
98 pn_connection_driver_close(&driver);
99 pn_connection_driver_destroy(&driver);
100 if (VERBOSE)
101 printf("END LLVMFuzzerTestOneInput\n");
102 return 0;
103 }
104
105 static void handle(app_data_t *app, pn_event_t *event) {
106 switch (pn_event_type(event)) {
107
108 case PN_CONNECTION_INIT: {
109 pn_connection_t *c = pn_event_connection(event);
110 pn_connection_set_container(c, app->container_id);
111 pn_connection_open(c);
112 pn_session_t *s = pn_session(c);
113 pn_session_open(s);
114 pn_link_t *l = pn_receiver(s, "my_receiver");
115 pn_terminus_set_address(pn_link_source(l), NULL);
116 pn_link_open(l);
117 pn_link_flow(l, 20);
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 } break;
134
135 case PN_TRANSPORT_ERROR:
136 check_condition(event, pn_transport_condition(pn_event_transport(event)));
137 pn_connection_close(pn_event_connection(event));
138 break;
139
140 case PN_CONNECTION_REMOTE_CLOSE:
141 check_condition(event,
142 pn_connection_remote_condition(pn_event_connection(event)));
143 pn_connection_close(pn_event_connection(event));
144 break;
145
146 case PN_SESSION_REMOTE_CLOSE:
147 check_condition(event,
148 pn_session_remote_condition(pn_event_session(event)));
149 pn_connection_close(pn_event_connection(event));
150 break;
151
152 case PN_LINK_REMOTE_CLOSE:
153 case PN_LINK_REMOTE_DETACH:
154 check_condition(event, pn_link_remote_condition(pn_event_link(event)));
155 pn_connection_close(pn_event_connection(event));
156 break;
157
158 default:
159 break;
160 }
161 }
162
163 static void check_condition(pn_event_t *e, pn_condition_t *cond) {
164 if (VERBOSE)
165 printf("beginning check_condition\n");
166 if (pn_condition_is_set(cond)) {
167 if (VERBOSE || ERRORS)
168 fprintf(stderr, "%s: %s: %s\n", pn_event_type_name(pn_event_type(e)),
169 pn_condition_get_name(cond), pn_condition_get_description(cond));
170 }
171 }
172
173 static void decode_message(pn_delivery_t *dlv) {
174 static char buffer[MAX_SIZE];
175 ssize_t len;
176 // try to decode the message body
177 if (pn_delivery_pending(dlv) < MAX_SIZE) {
178 // read in the raw data
179 len = pn_link_recv(pn_delivery_link(dlv), buffer, MAX_SIZE);
180 if (len > 0) {
181 // decode it into a proton message
182 pn_message_t *m = pn_message();
183 if (PN_OK == pn_message_decode(m, buffer, len)) {
184 pn_string_t *s = pn_string(NULL);
185 pn_inspect(pn_message_body(m), s);
186 if (ERRORS)
187 printf("%s\n", pn_string_get(s));
188 pn_free(s);
189 }
190 pn_message_free(m);
191 }
192 }
193 }
194
195 // reads up to `size` bytes from `data`,
196 // updates `data` pointer and `size` to the unread portion of original `data`,
197 // returns new value of `size`
198 size_t fcd_read(pn_connection_driver_t *driver, uint8_t **data, size_t *size) {
199 pn_rwbytes_t buf = pn_connection_driver_read_buffer(driver);
200 size_t s = (*size < buf.size) ? *size : buf.size;
201 if (buf.start == NULL) {
202 // The engine offered a null buffer for further input.
203 // This is legit, because it is just that the "socket" was closed
204 // for further input, after reading the invalid header.
205 *size = 0;
206 return *size;
207 }
208 memcpy(buf.start, *data, s);
209
210 pn_connection_driver_read_done(driver, s);
211 *data += s;
212 *size -= s;
213
214 return *size;
215 }
216
217 // drops the data in the buffer and reports them as written
218 static void fdc_write(pn_connection_driver_t *driver) {
219 pn_bytes_t buffer = pn_connection_driver_write_buffer(driver);
220 pn_connection_driver_write_done(driver, buffer.size);
221 }