Debian Codegraph @

fuzz/fuzz_codec.c

raw symbols SCIP
1 // Copyright 2007 - 2026, Arthur Chan and the rabbitmq-c contributors.
2 // SPDX-License-Identifier: mit
3
4 #include <stdint.h>
5 #include <stdlib.h>
6 #include <string.h>
7
8 #include <rabbitmq-c/amqp.h>
9 #include <rabbitmq-c/framing.h>
10
11 // Round-trips the AMQP codecs. The existing fuzzers only decode; the generated
12 // serializers (amqp_encode_method/properties/table) and amqp_table_clone are
13 // unreachable from hand-written input. Each mode decodes attacker bytes into the
14 // in-memory struct, then re-encodes/clones it -- the decoder supplies the
15 // adversarial-but-valid structs the encoders would otherwise never see.
16
17 #define ENC_BUF_SIZE (1u << 20)
18
19 static void roundtrip_method(const uint8_t *p, size_t n) {
20 amqp_pool_t pool;
21 void *decoded = NULL;
22 amqp_method_number_t method_id;
23 amqp_bytes_t encoded;
24
25 if (n < 4) {
26 return;
27 }
28 method_id = ((amqp_method_number_t)p[0] << 24) |
29 ((amqp_method_number_t)p[1] << 16) |
30 ((amqp_method_number_t)p[2] << 8) | (amqp_method_number_t)p[3];
31 encoded.len = n - 4;
32 encoded.bytes = (void *)(p + 4);
33
34 init_amqp_pool(&pool, 4096);
35 if (amqp_decode_method(method_id, &pool, encoded, &decoded) ==
36 AMQP_STATUS_OK &&
37 decoded != NULL) {
38 void *buf = malloc(ENC_BUF_SIZE);
39 if (buf != NULL) {
40 amqp_bytes_t out;
41 out.len = ENC_BUF_SIZE;
42 out.bytes = buf;
43 amqp_encode_method(method_id, decoded, out);
44 free(buf);
45 }
46 }
47 empty_amqp_pool(&pool);
48 }
49
50 static void roundtrip_properties(const uint8_t *p, size_t n) {
51 amqp_pool_t pool;
52 void *decoded = NULL;
53 uint16_t class_id;
54 amqp_bytes_t encoded;
55
56 if (n < 2) {
57 return;
58 }
59 class_id = ((uint16_t)p[0] << 8) | (uint16_t)p[1];
60 encoded.len = n - 2;
61 encoded.bytes = (void *)(p + 2);
62
63 init_amqp_pool(&pool, 4096);
64 if (amqp_decode_properties(class_id, &pool, encoded, &decoded) ==
65 AMQP_STATUS_OK &&
66 decoded != NULL) {
67 void *buf = malloc(ENC_BUF_SIZE);
68 if (buf != NULL) {
69 amqp_bytes_t out;
70 out.len = ENC_BUF_SIZE;
71 out.bytes = buf;
72 amqp_encode_properties(class_id, decoded, out);
73 free(buf);
74 }
75 }
76 empty_amqp_pool(&pool);
77 }
78
79 static void roundtrip_table(const uint8_t *p, size_t n) {
80 amqp_pool_t pool;
81 amqp_table_t decoded;
82 amqp_bytes_t encoded;
83 size_t offset = 0;
84
85 encoded.len = n;
86 encoded.bytes = (void *)p;
87
88 init_amqp_pool(&pool, 4096);
89 memset(&decoded, 0, sizeof(decoded));
90 if (amqp_decode_table(encoded, &pool, &decoded, &offset) == AMQP_STATUS_OK) {
91 void *buf = malloc(ENC_BUF_SIZE);
92 if (buf != NULL) {
93 amqp_bytes_t out;
94 size_t out_off = 0;
95 out.len = ENC_BUF_SIZE;
96 out.bytes = buf;
97 amqp_encode_table(out, &decoded, &out_off);
98 free(buf);
99 }
100 {
101 amqp_pool_t clone_pool;
102 amqp_table_t clone;
103 init_amqp_pool(&clone_pool, 4096);
104 memset(&clone, 0, sizeof(clone));
105 amqp_table_clone(&decoded, &clone, &clone_pool);
106 empty_amqp_pool(&clone_pool);
107 }
108 }
109 empty_amqp_pool(&pool);
110 }
111
112 // First byte selects the codec; the remainder is the payload, framed like the
113 // matching decode fuzzer (method-id / class-id prefix where applicable).
114 extern int LLVMFuzzerTestOneInput(const char *data, size_t size) {
115 const uint8_t *bytes = (const uint8_t *)data;
116
117 if (size < 1) {
118 return 0;
119 }
120 switch (bytes[0] % 3) {
121 case 0:
122 roundtrip_method(bytes + 1, size - 1);
123 break;
124 case 1:
125 roundtrip_properties(bytes + 1, size - 1);
126 break;
127 default:
128 roundtrip_table(bytes + 1, size - 1);
129 break;
130 }
131 return 0;
132 }