Coverage Report

Created: 2026-12-01 07:02

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