Coverage Report

Created: 2026-09-04 06:30

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
39.2k
#define CHECK_EQ(expr, res) if ( (expr) != (res) ) { goto end; }
21
#define CHECK_NE(expr, res) if ( (expr) == (res) ) { goto end; }
22
23
9.33k
#define BADPTR ((void*)0x12)
24
2.34k
#define MAX_TOPICS 50
25
26
52.3k
#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
13.9k
    ds(ds)
39
13.9k
{ }
40
41
13.9k
Base::~Base() { }
42
43
7.78k
MqttQoS Base::GetQoS(void) const {
44
7.78k
    switch ( ds.Get<uint8_t>() % 3 ) {
45
4.55k
        case    0:
46
4.55k
            return MQTT_QOS_0;
47
2.04k
        case    1:
48
2.04k
            return MQTT_QOS_1;
49
1.12k
        case    2:
50
1.12k
            return MQTT_QOS_2;
51
0
        default:
52
            /* Silence compiler warning */
53
0
            abort();
54
7.78k
    }
55
7.78k
}
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
6.94k
    Base(ds)
70
6.94k
{ }
71
72
6.94k
Topic::~Topic() { }
73
74
6.94k
bool Topic::Generate(void) {
75
6.94k
    bool ret;
76
77
6.94k
    memset(&topic, 0, sizeof(topic));
78
79
6.94k
    strings.push_back( ds.Get<std::string>() );
80
6.94k
    topic.topic_filter = strings.back().c_str();
81
82
6.94k
    topic.qos = GetQoS();
83
84
6.94k
    ret = true;
85
6.94k
end:
86
6.32k
    return ret;
87
6.94k
}
88
89
5.93k
MqttTopic Topic::Get(void) {
90
5.93k
    return topic;
91
5.93k
}
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.34k
    Base(ds)
106
2.34k
{ }
107
108
2.34k
Topics::~Topics() {
109
6.94k
    for (auto& t : topics) {
110
6.94k
        delete t;
111
6.94k
    }
112
2.34k
}
113
        
114
2.34k
bool Topics::Generate(void) {
115
2.34k
    bool ret;
116
117
2.34k
    try {
118
2.34k
        const auto numTopics = ds.Get<uint16_t>() % (MAX_TOPICS+1);
119
120
9.29k
        for (size_t i = 0; i < numTopics; i++) {
121
6.94k
            topics.push_back(new Topic(ds));
122
6.94k
            CHECK_EQ(topics.back()->Generate(), true);
123
6.94k
        }
124
125
2.34k
        ret = true;
126
2.34k
    } catch ( ... ) { }
127
128
2.34k
end:
129
2.34k
    return ret;
130
2.34k
}
131
132
2.17k
MqttTopic* Topics::ToArray(void) {
133
2.17k
    auto ret = new MqttTopic[topics.size()];
134
135
8.10k
    for (size_t i = 0; i < Size(); i++) {
136
5.93k
        ret[i] = topics[i]->Get();
137
5.93k
    }
138
2.17k
    return ret;
139
2.17k
}
140
        
141
10.2k
size_t Topics::Size(void) const {
142
10.2k
    return topics.size();
143
10.2k
}
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.50k
{
178
4.50k
    (void)context;
179
4.50k
    (void)host;
180
4.50k
    (void)port;
181
4.50k
    (void)timeout_ms;
182
183
4.50k
    return MQTT_CODE_SUCCESS;
184
4.50k
}
185
186
static int mqtt_recv(void *context, byte* buf, int buf_len, int timeout_ms)
187
35.5k
{
188
35.5k
    (void)context;
189
35.5k
    (void)timeout_ms;
190
191
35.5k
    auto fuzzer = static_cast<wolfMQTTFuzzer*>(context);
192
35.5k
    return fuzzer->recv(buf, buf_len);
193
35.5k
}
194
195
static int mqtt_write(void *context, const byte* buf, int buf_len, int timeout_ms)
196
13.3k
{
197
13.3k
    (void)context;
198
13.3k
    (void)timeout_ms;
199
13.3k
    (void)buf;
200
201
13.3k
    auto fuzzer = static_cast<wolfMQTTFuzzer*>(context);
202
13.3k
    return fuzzer->write(buf_len);
203
13.3k
}
204
205
static int mqtt_disconnect(void *context)
206
137
{
207
137
    (void)context;
208
209
137
    return MQTT_CODE_SUCCESS;
210
137
}
211
212
static int mqtt_message_cb(MqttClient *client, MqttMessage *msg, byte msg_new, byte msg_done)
213
4.15k
{
214
4.15k
    return MQTT_CODE_SUCCESS;
215
4.15k
}
216
217
9.24k
void* wolfMQTTFuzzer::malloc(const size_t n) {
218
9.24k
    return n == 0 ? BADPTR : ::malloc(n);
219
9.24k
}
220
221
9.33k
void wolfMQTTFuzzer::free(void* ptr) {
222
9.33k
    if ( ptr == BADPTR ) {
223
0
        return;
224
0
    }
225
226
9.33k
    ::free(ptr);
227
9.33k
}
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.48k
word16 wolfMQTTFuzzer::GetPacketId(void) const {
240
3.48k
    return ds.Get<word16>();
241
3.48k
}
242
243
1.51k
bool wolfMQTTFuzzer::subscribe(void) {
244
1.51k
    MqttTopic* topicsArray = nullptr;
245
246
1.51k
    bool ret = false;
247
248
1.51k
    try {
249
1.51k
        Topics topics(ds);
250
1.51k
        CHECK_EQ(topics.Generate(), true);
251
252
1.51k
        MqttSubscribe subscribe;
253
254
1.51k
        memset(&subscribe, 0, sizeof(subscribe));
255
256
1.51k
        subscribe.packet_id = GetPacketId();
257
1.51k
        topicsArray = topics.ToArray();
258
1.51k
        subscribe.topic_count = topics.Size();
259
1.51k
        subscribe.topics = topicsArray;
260
261
1.51k
        CHECK_EQ(MqttClient_Subscribe(&client, &subscribe), MQTT_CODE_SUCCESS);
262
263
214
        ret = true;
264
214
    } catch ( ... ) { }
265
266
1.51k
end:
267
1.51k
    if ( topicsArray ) {
268
1.37k
        delete[] topicsArray;
269
1.37k
    }
270
1.51k
    return ret;
271
1.51k
}
272
273
829
bool wolfMQTTFuzzer::unsubscribe(void) {
274
829
    MqttTopic* topicsArray = nullptr;
275
276
829
    bool ret = false;
277
278
829
    try {
279
829
        Topics topics(ds);
280
829
        CHECK_EQ(topics.Generate(), true);
281
282
829
        MqttUnsubscribe unsubscribe;
283
284
829
        memset(&unsubscribe, 0, sizeof(unsubscribe));
285
286
829
        unsubscribe.packet_id = GetPacketId();
287
829
        topicsArray = topics.ToArray();
288
829
        unsubscribe.topic_count = topics.Size();
289
829
        unsubscribe.topics = topicsArray;
290
291
829
        CHECK_EQ(MqttClient_Unsubscribe(&client, &unsubscribe), MQTT_CODE_SUCCESS);
292
293
73
        ret = true;
294
73
    } catch ( ... ) { }
295
296
829
end:
297
829
    if ( topicsArray ) {
298
796
        delete[] topicsArray;
299
796
    }
300
829
    return ret;
301
829
}
302
303
1.32k
bool wolfMQTTFuzzer::publish(void) {
304
1.32k
    bool ret = false;
305
306
1.32k
    try {
307
1.32k
        MqttPublish publish;
308
309
1.32k
        memset(&publish, 0, sizeof(publish));
310
311
1.32k
        publish.retain = ds.Get<bool>() ? 1 : 0;
312
1.32k
        publish.qos = GetQoS();
313
1.32k
        publish.duplicate = ds.Get<bool>() ? 1 : 0;
314
315
1.32k
        const auto topic_str = ds.Get<std::string>();
316
1.32k
        publish.topic_name = topic_str.c_str();
317
318
1.32k
        publish.packet_id = GetPacketId();
319
320
1.32k
        auto buffer = ds.GetData(0);
321
1.32k
        publish.buffer = buffer.data();
322
1.32k
        publish.total_len = buffer.size();
323
324
1.32k
        if ( DEBUG ) {
325
0
            printf("publish: topic name size: %zu\n", strlen(topic_str.c_str()));
326
0
        }
327
328
1.32k
        CHECK_EQ(MqttClient_Publish(&client, &publish), MQTT_CODE_SUCCESS);
329
330
433
        ret = true;
331
433
    } catch ( ... ) { }
332
333
1.32k
end:
334
1.32k
    return ret;
335
1.32k
}
336
337
1.34k
bool wolfMQTTFuzzer::ping(void) {
338
1.34k
    bool ret = false;
339
340
1.34k
    MqttPing ping;
341
342
1.34k
    memset(&ping, 0, sizeof(ping));
343
344
1.34k
    CHECK_EQ(MqttClient_Ping_ex(&client, &ping), true);
345
346
0
    ret = true;
347
348
1.34k
end:
349
1.34k
    return ret;
350
0
}
351
352
6.26k
bool wolfMQTTFuzzer::wait(void) {
353
6.26k
    bool ret = false;
354
355
6.26k
    CHECK_EQ(MqttClient_WaitMessage(&client, 1000), MQTT_CODE_SUCCESS);
356
357
1.20k
    ret = true;
358
359
6.26k
end:
360
6.26k
    return ret;
361
1.20k
}
362
363
wolfMQTTFuzzer::wolfMQTTFuzzer(fuzzing::datasource::Datasource& ds) :
364
4.66k
    Base(ds)
365
4.66k
{ }
366
367
4.66k
wolfMQTTFuzzer::~wolfMQTTFuzzer() {
368
4.66k
    this->free(tx_buf);
369
4.66k
    this->free(rx_buf);
370
4.66k
}
371
372
4.66k
bool wolfMQTTFuzzer::Initialize(void) {
373
4.66k
    bool ret = false;
374
375
4.66k
    try {
376
        /* net */
377
4.66k
        {
378
4.66k
            memset(&net, 0, sizeof(net));
379
380
4.66k
            net.connect = mqtt_connect;
381
4.66k
            net.read = mqtt_recv;
382
4.66k
            net.write = mqtt_write;
383
4.66k
            net.disconnect = mqtt_disconnect;
384
4.66k
            net.context = this;
385
4.66k
        }
386
387
        /* client */
388
4.66k
        {
389
4.66k
            memset(&client, 0, sizeof(client));
390
391
4.66k
            tx_size = ds.Get<uint16_t>();
392
4.66k
            tx_size = 4096;
393
4.66k
            tx_buf = (uint8_t*)this->malloc(tx_size);
394
4.66k
            rx_size = ds.Get<uint16_t>();
395
4.66k
            rx_size = 4096;
396
4.66k
            rx_buf = (uint8_t*)this->malloc(rx_size);
397
4.66k
            memset(tx_buf, 0, tx_size);
398
4.66k
            memset(rx_buf, 0, rx_size);
399
400
4.66k
            client.msg_cb = mqtt_message_cb;
401
4.66k
            client.tx_buf = tx_buf;
402
4.66k
            client.tx_buf_len = tx_size;
403
4.66k
            client.rx_buf = rx_buf;
404
4.66k
            client.rx_buf_len = rx_size;
405
4.66k
            client.cmd_timeout_ms = 1000;
406
4.66k
        }
407
408
        /* connect */
409
        /* Zero-initialize: only a subset of lwt_msg's fields are set below when
410
         * LWT is enabled, and the MQTT v5 encoder in MqttEncode_Connect reads
411
         * other members (e.g. props). Leaving them uninitialized is a harness
412
         * bug that MSAN reports as a use-of-uninitialized-value inside
413
         * MqttEncode_Connect. */
414
4.66k
        MqttMessage lwt_msg;
415
4.66k
        memset(&lwt_msg, 0, sizeof(lwt_msg));
416
4.66k
        {
417
4.66k
            memset(&connect, 0, sizeof(connect));
418
419
4.66k
            connect.keep_alive_sec = 1;
420
4.66k
            connect.clean_session = ds.Get<bool>() ? 1 : 0;
421
4.66k
            client_id = ds.Get<std::string>();
422
4.66k
            connect.client_id = client_id.c_str();
423
4.66k
            connect.enable_lwt = ds.Get<bool>() ? 1 : 0;
424
4.66k
        }
425
            
426
4.66k
        std::string lwt_topic_name;
427
4.66k
        std::vector<uint8_t> lwt_buffer;
428
429
4.66k
        if ( connect.enable_lwt ) {
430
105
            lwt_topic_name = ds.Get<std::string>();
431
105
            lwt_buffer = ds.GetData(0);
432
433
105
            connect.lwt_msg = &lwt_msg;
434
105
            lwt_msg.qos = GetQoS();
435
105
            lwt_msg.retain = ds.Get<bool>() ? 1 : 0;
436
105
            lwt_msg.topic_name = lwt_topic_name.c_str();
437
105
            lwt_msg.buffer = lwt_buffer.data();
438
105
            lwt_msg.total_len = lwt_buffer.size();
439
105
        }
440
441
4.66k
        CHECK_EQ(MqttSocket_Init(&client, &net), MQTT_CODE_SUCCESS);
442
443
#if 0
444
        if ( ds.Get<bool>() ) {
445
            //CHECK_EQ(MqttClient_SetPropertyCallback(&client, mqtt_property_cb, NULL);
446
        }
447
#endif
448
449
4.66k
        CHECK_EQ(MqttClient_NetConnect(&client, "dummy", 12345, 1000, 0, NULL), MQTT_CODE_SUCCESS);
450
4.66k
        CHECK_EQ(MqttClient_Connect(&client, &connect), MQTT_CODE_SUCCESS);
451
452
2.90k
        ret = true;
453
454
2.90k
    } catch ( ... ) {
455
163
        return false;
456
163
    }
457
458
4.50k
end:
459
4.50k
    return ret;
460
4.66k
}
461
462
2.73k
void wolfMQTTFuzzer::Run(void) {
463
2.73k
    try {
464
2.73k
        const auto numActions = ds.Get<uint8_t>() % 20;
465
466
15.0k
        for (size_t i = 0; i < numActions; i++) {
467
14.9k
            switch ( ds.Get<uint8_t>() ) {
468
1.51k
                case    0:
469
1.51k
                    subscribe();
470
1.51k
                    break;
471
829
                case    1:
472
829
                    unsubscribe();
473
829
                    break;
474
1.32k
                case    2:
475
1.32k
                    publish();
476
1.32k
                    break;
477
1.34k
                case    3:
478
1.34k
                    ping();
479
1.34k
                    break;
480
6.26k
                case    4:
481
6.26k
                    wait();
482
6.26k
                    break;
483
14.9k
            }
484
14.9k
        }
485
486
154
        MqttClient_NetDisconnect(&client);
487
2.60k
    } catch ( ... ) { }
488
2.73k
}
489
490
35.5k
int wolfMQTTFuzzer::recv(byte* buf, const int buf_len) {
491
35.5k
    try {
492
35.5k
        const auto data = ds.GetData(0);
493
35.5k
        const size_t copySize = buf_len > data.size() ? data.size() : buf_len;
494
35.5k
        if ( copySize ) {
495
30.2k
            memcpy(buf, data.data(), copySize);
496
30.2k
        }
497
35.5k
        if ( DEBUG )
498
0
        {
499
0
            printf("Recv: %zu bytes (%d requested)\n", copySize, buf_len);
500
0
            for (size_t i = 0; i < copySize; i++) {
501
0
                printf("%02X ", data[i]);
502
0
            }
503
0
            printf("\n");
504
0
        }
505
35.5k
        return copySize;
506
35.5k
    } catch ( ... ) {
507
2.10k
        if ( DEBUG ) printf("Recv: -1\n");
508
2.10k
        return -1;
509
2.10k
    }
510
35.5k
}
511
512
13.3k
int wolfMQTTFuzzer::write(const int buf_len) {
513
13.3k
    try {
514
13.3k
        if ( ds.Get<bool>() == true ) {
515
717
            if ( DEBUG ) printf("write: -1\n");
516
717
            return -1;
517
717
        }
518
519
12.6k
        const auto ret = (int)(ds.Get<uint32_t>() % (buf_len+1));
520
12.6k
        if ( DEBUG ) printf("write: %d bytes (%d requested)\n", ret, buf_len);
521
12.6k
        return ret;
522
13.3k
    } catch ( ... ) {
523
793
        return -1;
524
793
    }
525
13.3k
}
526
527
4.66k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
528
4.66k
    fuzzing::datasource::Datasource ds(data, size);
529
4.66k
    wolfMQTTFuzzer fuzzer(ds);
530
531
4.66k
    CHECK_EQ(fuzzer.Initialize(), true);
532
533
2.73k
    fuzzer.Run();
534
535
4.66k
end:
536
4.66k
    return 0;
537
2.73k
}