Coverage Report

Created: 2025-08-28 06:18

/src/uWebSockets/fuzzing/EpollEchoServer.cpp
Line
Count
Source (jump to first uncovered line)
1
/* We rely on wrapped syscalls */
2
#include "libEpollFuzzer/epoll_fuzzer.h"
3
4
#include "App.h"
5
6
/* We keep this one for teardown later on */
7
struct us_listen_socket_t *listen_socket;
8
9
/* This test is run by libEpollFuzzer */
10
6.99k
void test() {
11
12
6.99k
    struct PerSocketData {
13
6.99k
        int nothing;
14
6.99k
        std::shared_ptr<bool> valid;
15
6.99k
    };
16
17
    /* First byte determines what compressor to use */
18
6.99k
    unsigned char compressorByte;
19
6.99k
    if (consume_byte(&compressorByte)) {
20
        //uWS::Loop::get()->free();
21
0
        return;
22
0
    }
23
24
6.99k
    uWS::CompressOptions compressors[] = {
25
6.99k
        uWS::DISABLED,
26
6.99k
        uWS::SHARED_COMPRESSOR,
27
6.99k
        uWS::DEDICATED_COMPRESSOR_3KB,
28
6.99k
        uWS::DEDICATED_COMPRESSOR_4KB,
29
6.99k
        uWS::DEDICATED_COMPRESSOR_8KB,
30
6.99k
        uWS::DEDICATED_COMPRESSOR_16KB,
31
6.99k
        uWS::DEDICATED_COMPRESSOR_32KB,
32
6.99k
        uWS::DEDICATED_COMPRESSOR_64KB,
33
6.99k
        uWS::DEDICATED_COMPRESSOR_128KB,
34
6.99k
        uWS::DEDICATED_COMPRESSOR_256KB
35
6.99k
    };
36
37
6.99k
    uWS::CompressOptions compressor = compressors[compressorByte % 10];
38
39
6.99k
    {
40
6.99k
        auto app = uWS::App().ws<PerSocketData>("/broadcast", {
41
            /* Settings */
42
6.99k
            .compression = compressor,
43
            /* We want this to be low so that we can hit it, yet bigger than 256 */
44
6.99k
            .maxPayloadLength = 300,
45
6.99k
            .idleTimeout = 12,
46
            /* Handlers */
47
143k
            .open = [](auto *ws) {
48
                /* Subscribe to anything */
49
143k
                ws->subscribe(/*req->getHeader(*/"topic"/*)*/);
50
143k
            },
51
51.9k
            .message = [](auto *ws, std::string_view message, uWS::OpCode opCode) {
52
51.9k
                if (message.length() && message[0] == 'C') {
53
195
                    ws->close();
54
51.7k
                } else if (message.length() && message[0] == 'E') {
55
538
                    ws->end(1006);
56
51.2k
                } else {
57
                    /* Publish to topic sent by message */
58
51.2k
                    ws->publish(message, message, opCode, true);
59
60
51.2k
                    if (message.length() && message[0] == 'U') {
61
1.00k
                        ws->unsubscribe(message);
62
1.00k
                    }
63
51.2k
                }
64
51.9k
            },
65
37.1k
            .drain = [](auto *ws) {
66
                /* Check getBufferedAmount here */
67
37.1k
            },
68
6.99k
            .ping = [](auto *ws, std::string_view) {
69
70
1.07k
            },
71
6.99k
            .pong = [](auto *ws, std::string_view) {
72
73
3.69k
            },
74
143k
            .close = [](auto *ws, int code, std::string_view message) {
75
                /* Cause reported crash */
76
143k
                ws->close();
77
143k
            }
78
6.99k
        }).ws<PerSocketData>("/*", {
79
            /* Settings */
80
6.99k
            .compression = compressor,
81
            /* We want this to be low so that we can hit it, yet bigger than 256 */
82
6.99k
            .maxPayloadLength = 300,
83
6.99k
            .idleTimeout = 12,
84
            /* Handlers */
85
37.7k
            .open = [](auto *ws) {
86
87
37.7k
                ws->getUserData()->valid.reset(new bool{true});
88
89
                //if (req->getHeader("close_me").length()) {
90
                //    ws->close();
91
                //} else if (req->getHeader("end_me").length()) {
92
                //    ws->end(1006);
93
                //}
94
37.7k
            },
95
14.0k
            .message = [](auto *ws, std::string_view message, uWS::OpCode opCode) {
96
14.0k
                if (message.length() > 300) {
97
                    /* Inform the sanitizer of the fault */
98
0
                    fprintf(stderr, "Too long message passed\n");
99
0
                    free((void *) -1);
100
0
                }
101
102
14.0k
                if (message.length() && message[0] == 'C') {
103
373
                    ws->close();
104
13.7k
                } else if (message.length() && message[0] == 'E') {
105
207
                    ws->end(1006);
106
13.5k
                } else {
107
13.5k
                    ws->send(message, opCode, true);
108
13.5k
                }
109
14.0k
            },
110
6.99k
            .drain = [](auto *ws) {
111
                /* Check getBufferedAmount here */
112
4.96k
            },
113
9.78k
            .ping = [](auto *ws, std::string_view) {
114
                /* Here we test send and end while uncorked, by having them send from deferred */
115
9.78k
                PerSocketData *psd = (PerSocketData *) ws->getUserData();
116
117
9.78k
                uWS::Loop::get()->defer([ws, valid = psd->valid]() {
118
6.03k
                    if (*valid.get()) {
119
                        /* We haven't been closed */
120
2.48k
                        ws->send("Hello!", uWS::TEXT, false);
121
2.48k
                        ws->end(1000);
122
2.48k
                    }
123
6.03k
                });
124
9.78k
            },
125
6.99k
            .pong = [](auto *ws, std::string_view) {
126
127
1.52k
            },
128
37.7k
            .close = [](auto *ws, int code, std::string_view message) {
129
37.7k
                (*ws->getUserData()->valid.get()) = false;
130
37.7k
            }
131
6.99k
        }).listen(9001, [](us_listen_socket_t *listenSocket) {
132
6.99k
            listen_socket = listenSocket;
133
6.99k
        });
134
135
6.99k
        app.run();
136
6.99k
    }
137
138
6.99k
    uWS::Loop::get()->free();
139
6.99k
}
140
141
/* Thus function should shutdown the event-loop and let the test fall through */
142
16.1k
void teardown() {
143
  /* If we are called twice there's a bug (it potentially could if
144
   * all open sockets cannot be error-closed in one epoll_wait call).
145
   * But we only allow 1k FDs and we have a buffer of 1024 from epoll_wait */
146
16.1k
  if (!listen_socket) {
147
0
    exit(-1);
148
0
  }
149
150
  /* We might have open sockets still, and these will be error-closed by epoll_wait */
151
  // us_socket_context_close - close all open sockets created with this socket context
152
16.1k
    if (listen_socket) {
153
16.1k
        us_listen_socket_close(0, listen_socket);
154
16.1k
        listen_socket = NULL;
155
16.1k
    }
156
16.1k
}