Coverage Report

Created: 2023-09-25 07:17

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