Coverage Report

Created: 2026-07-16 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wolfmqtt-fuzzers/fuzzer.cpp
Line
Count
Source
1
// Copyright 2026 Google LLC.
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 <fuzzing/datasource/datasource.hpp>
16
#include <wolfmqtt/mqtt_client.h>
17
#include <wolfmqtt/mqtt_packet.h>
18
#include <optional>
19
20
38.6k
#define CHECK_EQ(expr, res) if ( (expr) != (res) ) { goto end; }
21
#define CHECK_NE(expr, res) if ( (expr) == (res) ) { goto end; }
22
23
8.55k
#define BADPTR ((void*)0x12)
24
2.66k
#define MAX_TOPICS 50
25
26
40.8k
#define DEBUG 0
27
28
class Base {
29
    protected:
30
        fuzzing::datasource::Datasource& ds;
31
        MqttQoS GetQoS(void) const;
32
    public:
33
        Base(fuzzing::datasource::Datasource& ds);
34
        ~Base();
35
};
36
37
Base::Base(fuzzing::datasource::Datasource& ds) :
38
14.1k
    ds(ds)
39
14.1k
{ }
40
41
14.1k
Base::~Base() { }
42
43
7.80k
MqttQoS Base::GetQoS(void) const {
44
7.80k
    switch ( ds.Get<uint8_t>() % 3 ) {
45
4.85k
        case    0:
46
4.85k
            return MQTT_QOS_0;
47
1.68k
        case    1:
48
1.68k
            return MQTT_QOS_1;
49
1.21k
        case    2:
50
1.21k
            return MQTT_QOS_2;
51
0
        default:
52
            /* Silence compiler warning */
53
0
            abort();
54
7.80k
    }
55
7.80k
}
56
57
class Topic : public Base {
58
    private:
59
        MqttTopic topic;
60
        std::vector<std::string> strings;
61
    public:
62
        Topic(fuzzing::datasource::Datasource& ds);
63
        ~Topic();
64
        bool Generate(void);
65
        MqttTopic Get(void);
66
};
67
68
Topic::Topic(fuzzing::datasource::Datasource& ds) :
69
7.17k
    Base(ds)
70
7.17k
{ }
71
72
7.17k
Topic::~Topic() { }
73
74
7.17k
bool Topic::Generate(void) {
75
7.17k
    bool ret;
76
77
7.17k
    memset(&topic, 0, sizeof(topic));
78
79
7.17k
    strings.push_back( ds.Get<std::string>() );
80
7.17k
    topic.topic_filter = strings.back().c_str();
81
82
7.17k
    topic.qos = GetQoS();
83
84
7.17k
    ret = true;
85
7.17k
end:
86
6.37k
    return ret;
87
7.17k
}
88
89
6.37k
MqttTopic Topic::Get(void) {
90
6.37k
    return topic;
91
6.37k
}
92
93
class Topics : public Base {
94
    private:
95
        std::vector<Topic*> topics;
96
    public:
97
        Topics(fuzzing::datasource::Datasource& ds);
98
        ~Topics();
99
        bool Generate(void);
100
        MqttTopic* ToArray(void);
101
        size_t Size(void) const;
102
};
103
104
Topics::Topics(fuzzing::datasource::Datasource& ds) :
105
2.66k
    Base(ds)
106
2.66k
{ }
107
108
2.66k
Topics::~Topics() {
109
7.17k
    for (auto& t : topics) {
110
7.17k
        delete t;
111
7.17k
    }
112
2.66k
}
113
        
114
2.66k
bool Topics::Generate(void) {
115
2.66k
    bool ret;
116
117
2.66k
    try {
118
2.66k
        const auto numTopics = ds.Get<uint16_t>() % (MAX_TOPICS+1);
119
120
9.83k
        for (size_t i = 0; i < numTopics; i++) {
121
7.17k
            topics.push_back(new Topic(ds));
122
7.17k
            CHECK_EQ(topics.back()->Generate(), true);
123
7.17k
        }
124
125
2.66k
        ret = true;
126
2.66k
    } catch ( ... ) { }
127
128
2.66k
end:
129
2.66k
    return ret;
130
2.66k
}
131
132
2.48k
MqttTopic* Topics::ToArray(void) {
133
2.48k
    auto ret = new MqttTopic[topics.size()];
134
135
8.85k
    for (size_t i = 0; i < Size(); i++) {
136
6.37k
        ret[i] = topics[i]->Get();
137
6.37k
    }
138
2.48k
    return ret;
139
2.48k
}
140
        
141
11.3k
size_t Topics::Size(void) const {
142
11.3k
    return topics.size();
143
11.3k
}
144
145
class wolfMQTTFuzzer : public Base {
146
        MqttClient client;
147
        MqttNet net;
148
        MqttConnect connect;
149
150
        uint8_t* tx_buf = nullptr, *rx_buf = nullptr;
151
        size_t tx_size = 0, rx_size = 0;
152
153
        std::string client_id;
154
155
        void* malloc(const size_t n);
156
        void free(void* ptr);
157
158
        word16 GetPacketId(void) const;
159
        std::optional<Topic> GetTopic(void) const;
160
161
        bool subscribe(void);
162
        bool unsubscribe(void);
163
        bool publish(void);
164
        bool ping(void);
165
        bool wait(void);
166
    public:
167
        wolfMQTTFuzzer(fuzzing::datasource::Datasource& ds);
168
        ~wolfMQTTFuzzer();
169
        bool Initialize(void);
170
        void Run(void);
171
        int recv(byte* buf, const int buf_len);
172
        int write(const int buf_len);
173
174
};
175
176
static int mqtt_connect(void *context, const char* host, word16 port, int timeout_ms)
177
4.11k
{
178
4.11k
    (void)context;
179
4.11k
    (void)host;
180
4.11k
    (void)port;
181
4.11k
    (void)timeout_ms;
182
183
4.11k
    return MQTT_CODE_SUCCESS;
184
4.11k
}
185
186
static int mqtt_recv(void *context, byte* buf, int buf_len, int timeout_ms)
187
27.0k
{
188
27.0k
    (void)context;
189
27.0k
    (void)timeout_ms;
190
191
27.0k
    auto fuzzer = static_cast<wolfMQTTFuzzer*>(context);
192
27.0k
    return fuzzer->recv(buf, buf_len);
193
27.0k
}
194
195
static int mqtt_write(void *context, const byte* buf, int buf_len, int timeout_ms)
196
10.6k
{
197
10.6k
    (void)context;
198
10.6k
    (void)timeout_ms;
199
10.6k
    (void)buf;
200
201
10.6k
    auto fuzzer = static_cast<wolfMQTTFuzzer*>(context);
202
10.6k
    return fuzzer->write(buf_len);
203
10.6k
}
204
205
static int mqtt_disconnect(void *context)
206
131
{
207
131
    (void)context;
208
209
131
    return MQTT_CODE_SUCCESS;
210
131
}
211
212
static int mqtt_message_cb(MqttClient *client, MqttMessage *msg, byte msg_new, byte msg_done)
213
3.06k
{
214
3.06k
    return MQTT_CODE_SUCCESS;
215
3.06k
}
216
217
8.47k
void* wolfMQTTFuzzer::malloc(const size_t n) {
218
8.47k
    return n == 0 ? BADPTR : ::malloc(n);
219
8.47k
}
220
221
8.55k
void wolfMQTTFuzzer::free(void* ptr) {
222
8.55k
    if ( ptr == BADPTR ) {
223
0
        return;
224
0
    }
225
226
8.55k
    ::free(ptr);
227
8.55k
}
228
229
0
std::optional<Topic> wolfMQTTFuzzer::GetTopic(void) const {
230
0
    Topic topic(ds);
231
232
0
    if ( topic.Generate() == false ) {
233
0
        return std::nullopt;
234
0
    }
235
236
0
    return topic;
237
0
}
238
239
3.75k
word16 wolfMQTTFuzzer::GetPacketId(void) const {
240
3.75k
    return ds.Get<word16>();
241
3.75k
}
242
243
1.77k
bool wolfMQTTFuzzer::subscribe(void) {
244
1.77k
    MqttTopic* topicsArray = nullptr;
245
246
1.77k
    bool ret = false;
247
248
1.77k
    try {
249
1.77k
        Topics topics(ds);
250
1.77k
        CHECK_EQ(topics.Generate(), true);
251
252
1.77k
        MqttSubscribe subscribe;
253
254
1.77k
        memset(&subscribe, 0, sizeof(subscribe));
255
256
1.77k
        subscribe.packet_id = GetPacketId();
257
1.77k
        topicsArray = topics.ToArray();
258
1.77k
        subscribe.topic_count = topics.Size();
259
1.77k
        subscribe.topics = topicsArray;
260
261
1.77k
        CHECK_EQ(MqttClient_Subscribe(&client, &subscribe), MQTT_CODE_SUCCESS);
262
263
206
        ret = true;
264
206
    } catch ( ... ) { }
265
266
1.77k
end:
267
1.77k
    if ( topicsArray ) {
268
1.63k
        delete[] topicsArray;
269
1.63k
    }
270
1.77k
    return ret;
271
1.77k
}
272
273
887
bool wolfMQTTFuzzer::unsubscribe(void) {
274
887
    MqttTopic* topicsArray = nullptr;
275
276
887
    bool ret = false;
277
278
887
    try {
279
887
        Topics topics(ds);
280
887
        CHECK_EQ(topics.Generate(), true);
281
282
887
        MqttUnsubscribe unsubscribe;
283
284
887
        memset(&unsubscribe, 0, sizeof(unsubscribe));
285
286
887
        unsubscribe.packet_id = GetPacketId();
287
887
        topicsArray = topics.ToArray();
288
887
        unsubscribe.topic_count = topics.Size();
289
887
        unsubscribe.topics = topicsArray;
290
291
887
        CHECK_EQ(MqttClient_Unsubscribe(&client, &unsubscribe), MQTT_CODE_SUCCESS);
292
293
86
        ret = true;
294
86
    } catch ( ... ) { }
295
296
887
end:
297
887
    if ( topicsArray ) {
298
851
        delete[] topicsArray;
299
851
    }
300
887
    return ret;
301
887
}
302
303
1.29k
bool wolfMQTTFuzzer::publish(void) {
304
1.29k
    bool ret = false;
305
306
1.29k
    try {
307
1.29k
        MqttPublish publish;
308
309
1.29k
        memset(&publish, 0, sizeof(publish));
310
311
1.29k
        publish.retain = ds.Get<bool>() ? 1 : 0;
312
1.29k
        publish.qos = GetQoS();
313
1.29k
        publish.duplicate = ds.Get<bool>() ? 1 : 0;
314
315
1.29k
        const auto topic_str = ds.Get<std::string>();
316
1.29k
        publish.topic_name = topic_str.c_str();
317
318
1.29k
        publish.packet_id = GetPacketId();
319
320
1.29k
        auto buffer = ds.GetData(0);
321
1.29k
        publish.buffer = buffer.data();
322
1.29k
        publish.total_len = buffer.size();
323
324
1.29k
        if ( DEBUG ) {
325
0
            printf("publish: topic name size: %zu\n", strlen(topic_str.c_str()));
326
0
        }
327
328
1.29k
        CHECK_EQ(MqttClient_Publish(&client, &publish), MQTT_CODE_SUCCESS);
329
330
436
        ret = true;
331
436
    } catch ( ... ) { }
332
333
1.29k
end:
334
1.29k
    return ret;
335
1.29k
}
336
337
1.49k
bool wolfMQTTFuzzer::ping(void) {
338
1.49k
    bool ret = false;
339
340
1.49k
    MqttPing ping;
341
342
1.49k
    memset(&ping, 0, sizeof(ping));
343
344
1.49k
    CHECK_EQ(MqttClient_Ping_ex(&client, &ping), true);
345
346
0
    ret = true;
347
348
1.49k
end:
349
1.49k
    return ret;
350
0
}
351
352
6.29k
bool wolfMQTTFuzzer::wait(void) {
353
6.29k
    bool ret = false;
354
355
6.29k
    CHECK_EQ(MqttClient_WaitMessage(&client, 1000), MQTT_CODE_SUCCESS);
356
357
1.46k
    ret = true;
358
359
6.29k
end:
360
6.29k
    return ret;
361
1.46k
}
362
363
wolfMQTTFuzzer::wolfMQTTFuzzer(fuzzing::datasource::Datasource& ds) :
364
4.27k
    Base(ds)
365
4.27k
{ }
366
367
4.27k
wolfMQTTFuzzer::~wolfMQTTFuzzer() {
368
4.27k
    this->free(tx_buf);
369
4.27k
    this->free(rx_buf);
370
4.27k
}
371
372
4.27k
bool wolfMQTTFuzzer::Initialize(void) {
373
4.27k
    bool ret = false;
374
375
4.27k
    try {
376
        /* net */
377
4.27k
        {
378
4.27k
            memset(&net, 0, sizeof(net));
379
380
4.27k
            net.connect = mqtt_connect;
381
4.27k
            net.read = mqtt_recv;
382
4.27k
            net.write = mqtt_write;
383
4.27k
            net.disconnect = mqtt_disconnect;
384
4.27k
            net.context = this;
385
4.27k
        }
386
387
        /* client */
388
4.27k
        {
389
4.27k
            memset(&client, 0, sizeof(client));
390
391
4.27k
            tx_size = ds.Get<uint16_t>();
392
4.27k
            tx_size = 4096;
393
4.27k
            tx_buf = (uint8_t*)this->malloc(tx_size);
394
4.27k
            rx_size = ds.Get<uint16_t>();
395
4.27k
            rx_size = 4096;
396
4.27k
            rx_buf = (uint8_t*)this->malloc(rx_size);
397
4.27k
            memset(tx_buf, 0, tx_size);
398
4.27k
            memset(rx_buf, 0, rx_size);
399
400
4.27k
            client.msg_cb = mqtt_message_cb;
401
4.27k
            client.tx_buf = tx_buf;
402
4.27k
            client.tx_buf_len = tx_size;
403
4.27k
            client.rx_buf = rx_buf;
404
4.27k
            client.rx_buf_len = rx_size;
405
4.27k
            client.cmd_timeout_ms = 1000;
406
4.27k
        }
407
408
        /* connect */
409
4.27k
        MqttMessage lwt_msg;
410
4.27k
        {
411
4.27k
            memset(&connect, 0, sizeof(connect));
412
413
4.27k
            connect.keep_alive_sec = 1;
414
4.27k
            connect.clean_session = ds.Get<bool>() ? 1 : 0;
415
4.27k
            client_id = ds.Get<std::string>();
416
4.27k
            connect.client_id = client_id.c_str();
417
4.27k
            connect.enable_lwt = ds.Get<bool>() ? 1 : 0;
418
4.27k
        }
419
            
420
4.27k
        std::string lwt_topic_name;
421
4.27k
        std::vector<uint8_t> lwt_buffer;
422
423
4.27k
        if ( connect.enable_lwt ) {
424
117
            lwt_topic_name = ds.Get<std::string>();
425
117
            lwt_buffer = ds.GetData(0);
426
427
117
            connect.lwt_msg = &lwt_msg;
428
117
            lwt_msg.qos = GetQoS();
429
117
            lwt_msg.retain = ds.Get<bool>() ? 1 : 0;
430
117
            lwt_msg.topic_name = lwt_topic_name.c_str();
431
117
            lwt_msg.buffer = lwt_buffer.data();
432
117
            lwt_msg.total_len = lwt_buffer.size();
433
117
        }
434
435
4.27k
        CHECK_EQ(MqttSocket_Init(&client, &net), MQTT_CODE_SUCCESS);
436
437
#if 0
438
        if ( ds.Get<bool>() ) {
439
            //CHECK_EQ(MqttClient_SetPropertyCallback(&client, mqtt_property_cb, NULL);
440
        }
441
#endif
442
443
4.27k
        CHECK_EQ(MqttClient_NetConnect(&client, "dummy", 12345, 1000, 0, NULL), MQTT_CODE_SUCCESS);
444
4.27k
        CHECK_EQ(MqttClient_Connect(&client, &connect), MQTT_CODE_SUCCESS);
445
446
2.97k
        ret = true;
447
448
2.97k
    } catch ( ... ) {
449
163
        return false;
450
163
    }
451
452
4.11k
end:
453
4.11k
    return ret;
454
4.27k
}
455
456
2.80k
void wolfMQTTFuzzer::Run(void) {
457
2.80k
    try {
458
2.80k
        const auto numActions = ds.Get<uint8_t>() % 20;
459
460
16.0k
        for (size_t i = 0; i < numActions; i++) {
461
15.8k
            switch ( ds.Get<uint8_t>() ) {
462
1.77k
                case    0:
463
1.77k
                    subscribe();
464
1.77k
                    break;
465
887
                case    1:
466
887
                    unsubscribe();
467
887
                    break;
468
1.29k
                case    2:
469
1.29k
                    publish();
470
1.29k
                    break;
471
1.49k
                case    3:
472
1.49k
                    ping();
473
1.49k
                    break;
474
6.29k
                case    4:
475
6.29k
                    wait();
476
6.29k
                    break;
477
15.8k
            }
478
15.8k
        }
479
480
142
        MqttClient_NetDisconnect(&client);
481
2.67k
    } catch ( ... ) { }
482
2.80k
}
483
484
27.0k
int wolfMQTTFuzzer::recv(byte* buf, const int buf_len) {
485
27.0k
    try {
486
27.0k
        const auto data = ds.GetData(0);
487
27.0k
        const size_t copySize = buf_len > data.size() ? data.size() : buf_len;
488
27.0k
        if ( copySize ) {
489
21.7k
            memcpy(buf, data.data(), copySize);
490
21.7k
        }
491
27.0k
        if ( DEBUG )
492
0
        {
493
0
            printf("Recv: %zu bytes (%d requested)\n", copySize, buf_len);
494
0
            for (size_t i = 0; i < copySize; i++) {
495
0
                printf("%02X ", data[i]);
496
0
            }
497
0
            printf("\n");
498
0
        }
499
27.0k
        return copySize;
500
27.0k
    } catch ( ... ) {
501
1.83k
        if ( DEBUG ) printf("Recv: -1\n");
502
1.83k
        return -1;
503
1.83k
    }
504
27.0k
}
505
506
10.6k
int wolfMQTTFuzzer::write(const int buf_len) {
507
10.6k
    try {
508
10.6k
        if ( ds.Get<bool>() == true ) {
509
681
            if ( DEBUG ) printf("write: -1\n");
510
681
            return -1;
511
681
        }
512
513
9.98k
        const auto ret = (int)(ds.Get<uint32_t>() % (buf_len+1));
514
9.98k
        if ( DEBUG ) printf("write: %d bytes (%d requested)\n", ret, buf_len);
515
9.98k
        return ret;
516
10.6k
    } catch ( ... ) {
517
668
        return -1;
518
668
    }
519
10.6k
}
520
521
4.27k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
522
4.27k
    fuzzing::datasource::Datasource ds(data, size);
523
4.27k
    wolfMQTTFuzzer fuzzer(ds);
524
525
4.27k
    CHECK_EQ(fuzzer.Initialize(), true);
526
527
2.80k
    fuzzer.Run();
528
529
4.27k
end:
530
4.27k
    return 0;
531
2.80k
}