Coverage Report

Created: 2025-07-04 06:41

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