Coverage Report

Created: 2025-12-05 06:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/uWebSockets/fuzzing/EpollEchoServer.cpp
Line
Count
Source
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
7.06k
void test() {
11
12
7.06k
    struct PerSocketData {
13
7.06k
        int nothing;
14
7.06k
        std::shared_ptr<bool> valid;
15
7.06k
    };
16
17
    /* First byte determines what compressor to use */
18
7.06k
    unsigned char compressorByte;
19
7.06k
    if (consume_byte(&compressorByte)) {
20
        //uWS::Loop::get()->free();
21
0
        return;
22
0
    }
23
24
7.06k
    uWS::CompressOptions compressors[] = {
25
7.06k
        uWS::DISABLED,
26
7.06k
        uWS::SHARED_COMPRESSOR,
27
7.06k
        uWS::DEDICATED_COMPRESSOR_3KB,
28
7.06k
        uWS::DEDICATED_COMPRESSOR_4KB,
29
7.06k
        uWS::DEDICATED_COMPRESSOR_8KB,
30
7.06k
        uWS::DEDICATED_COMPRESSOR_16KB,
31
7.06k
        uWS::DEDICATED_COMPRESSOR_32KB,
32
7.06k
        uWS::DEDICATED_COMPRESSOR_64KB,
33
7.06k
        uWS::DEDICATED_COMPRESSOR_128KB,
34
7.06k
        uWS::DEDICATED_COMPRESSOR_256KB
35
7.06k
    };
36
37
7.06k
    uWS::CompressOptions compressor = compressors[compressorByte % 10];
38
39
7.06k
    {
40
7.06k
        auto app = uWS::App().ws<PerSocketData>("/broadcast", {
41
            /* Settings */
42
7.06k
            .compression = compressor,
43
            /* We want this to be low so that we can hit it, yet bigger than 256 */
44
7.06k
            .maxPayloadLength = 300,
45
7.06k
            .idleTimeout = 12,
46
            /* Handlers */
47
195k
            .open = [](auto *ws) {
48
                /* Subscribe to anything */
49
195k
                ws->subscribe(/*req->getHeader(*/"topic"/*)*/);
50
195k
            },
51
71.9k
            .message = [](auto *ws, std::string_view message, uWS::OpCode opCode) {
52
71.9k
                if (message.length() && message[0] == 'C') {
53
194
                    ws->close();
54
71.8k
                } else if (message.length() && message[0] == 'E') {
55
564
                    ws->end(1006);
56
71.2k
                } else {
57
                    /* Publish to topic sent by message */
58
71.2k
                    ws->publish(message, message, opCode, true);
59
60
71.2k
                    if (message.length() && message[0] == 'U') {
61
564
                        ws->unsubscribe(message);
62
564
                    }
63
71.2k
                }
64
71.9k
            },
65
41.3k
            .drain = [](auto *ws) {
66
                /* Check getBufferedAmount here */
67
41.3k
            },
68
7.06k
            .ping = [](auto *ws, std::string_view) {
69
70
845
            },
71
7.06k
            .pong = [](auto *ws, std::string_view) {
72
73
863
            },
74
195k
            .close = [](auto *ws, int code, std::string_view message) {
75
                /* Cause reported crash */
76
195k
                ws->close();
77
195k
            }
78
7.06k
        }).ws<PerSocketData>("/*", {
79
            /* Settings */
80
7.06k
            .compression = compressor,
81
            /* We want this to be low so that we can hit it, yet bigger than 256 */
82
7.06k
            .maxPayloadLength = 300,
83
7.06k
            .idleTimeout = 12,
84
            /* Handlers */
85
32.8k
            .open = [](auto *ws) {
86
87
32.8k
                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
32.8k
            },
95
14.1k
            .message = [](auto *ws, std::string_view message, uWS::OpCode opCode) {
96
14.1k
                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.1k
                if (message.length() && message[0] == 'C') {
103
386
                    ws->close();
104
13.8k
                } else if (message.length() && message[0] == 'E') {
105
208
                    ws->end(1006);
106
13.6k
                } else {
107
13.6k
                    ws->send(message, opCode, true);
108
13.6k
                }
109
14.1k
            },
110
7.06k
            .drain = [](auto *ws) {
111
                /* Check getBufferedAmount here */
112
3.56k
            },
113
8.41k
            .ping = [](auto *ws, std::string_view) {
114
                /* Here we test send and end while uncorked, by having them send from deferred */
115
8.41k
                PerSocketData *psd = (PerSocketData *) ws->getUserData();
116
117
8.41k
                uWS::Loop::get()->defer([ws, valid = psd->valid]() {
118
5.47k
                    if (*valid.get()) {
119
                        /* We haven't been closed */
120
1.49k
                        ws->send("Hello!", uWS::TEXT, false);
121
1.49k
                        ws->end(1000);
122
1.49k
                    }
123
5.47k
                });
124
8.41k
            },
125
7.06k
            .pong = [](auto *ws, std::string_view) {
126
127
1.51k
            },
128
32.8k
            .close = [](auto *ws, int code, std::string_view message) {
129
32.8k
                (*ws->getUserData()->valid.get()) = false;
130
32.8k
            }
131
7.06k
        }).listen(9001, [](us_listen_socket_t *listenSocket) {
132
7.06k
            listen_socket = listenSocket;
133
7.06k
        });
134
135
7.06k
        app.run();
136
7.06k
    }
137
138
7.06k
    uWS::Loop::get()->free();
139
7.06k
}
140
141
/* Thus function should shutdown the event-loop and let the test fall through */
142
15.9k
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
15.9k
  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
15.9k
    if (listen_socket) {
153
15.9k
        us_listen_socket_close(0, listen_socket);
154
        listen_socket = NULL;
155
15.9k
    }
156
15.9k
}