Coverage Report

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