/src/curl_fuzzer/proto_fuzzer/multi_socket_driver.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) Max Dymond, <cmeister2@gmail.com>, et al. |
3 | | * |
4 | | * SPDX-License-Identifier: curl |
5 | | */ |
6 | | |
7 | | /// @file |
8 | | /// @brief Bounded event-loop state for driving curl_multi_socket_action. |
9 | | |
10 | | #ifndef PROTO_FUZZER_MULTI_SOCKET_DRIVER_H_ |
11 | | #define PROTO_FUZZER_MULTI_SOCKET_DRIVER_H_ |
12 | | |
13 | | #include <curl/curl.h> |
14 | | |
15 | | #include <array> |
16 | | #include <cstddef> |
17 | | #include <cstdint> |
18 | | |
19 | | namespace proto_fuzzer { |
20 | | |
21 | | /// @class proto_fuzzer::MultiSocketDriver |
22 | | /// @brief Owns the socket and timer callback state required by libcurl's |
23 | | /// event-driven multi API. |
24 | | /// |
25 | | /// The fuzzer must retain callback storage until after the easy handle leaves |
26 | | /// the multi and the multi is cleaned up: either operation can synchronously |
27 | | /// issue CURL_POLL_REMOVE. A fixed watch table provides stable addresses for |
28 | | /// curl_multi_assign while preventing a mutated transfer from allocating an |
29 | | /// unbounded application-side poll set. |
30 | | class MultiSocketDriver { |
31 | | public: |
32 | | /// Result from one non-blocking event-loop turn. |
33 | | struct DriveResult { |
34 | | /// libcurl's result for the last socket action in the turn. |
35 | | CURLMcode code = CURLM_OK; |
36 | | /// True when an action ran or callback/handle state changed. |
37 | | bool made_progress = false; |
38 | | }; |
39 | | |
40 | | MultiSocketDriver(); |
41 | | ~MultiSocketDriver(); |
42 | | |
43 | | MultiSocketDriver(const MultiSocketDriver&) = delete; |
44 | | MultiSocketDriver& operator=(const MultiSocketDriver&) = delete; |
45 | | |
46 | | bool Install(CURLM* multi); |
47 | | |
48 | | CURLMcode Start(int* running_handles); |
49 | | |
50 | | DriveResult DriveReady(int* running_handles); |
51 | | |
52 | | void ProbeControlApis(); |
53 | | |
54 | | /// @return true if any socket callback exhausted the fixed watch table in this drive. |
55 | 321k | bool watch_exhausted() const { return watch_exhausted_; } |
56 | | |
57 | | private: |
58 | | /// One stable curl_multi_assign association. |
59 | | struct Watch { |
60 | | curl_socket_t socket = CURL_SOCKET_BAD; |
61 | | int interest = CURL_POLL_NONE; |
62 | | bool active = false; |
63 | | }; |
64 | | |
65 | | static int SocketCallback(CURL* easy, curl_socket_t socket, int what, void* user_data, void* socket_data); |
66 | | |
67 | | static int TimerCallback(CURLM* multi, long timeout_ms, void* user_data); |
68 | | |
69 | | int UpdateSocket(curl_socket_t socket, int what, void* socket_data); |
70 | | |
71 | | int UpdateTimer(long timeout_ms); |
72 | | |
73 | | Watch* FindWatch(curl_socket_t socket); |
74 | | |
75 | | Watch* FindFreeWatch(); |
76 | | |
77 | | /// Fixed application capacity, not a proven sufficient descriptor bound. |
78 | | /// A sufficient bound must also account for resolver, Happy Eyeballs, and |
79 | | /// internal filter descriptors; mock-connection and easy-handle limits alone |
80 | | /// do not establish one. Exhaustion reports a socket-callback error. |
81 | | static constexpr std::size_t kMaxWatches = 16; |
82 | | |
83 | | CURLM* multi_; |
84 | | std::array<Watch, kMaxWatches> watches_; |
85 | | long timeout_ms_; |
86 | | bool timer_pending_; |
87 | | bool watch_exhausted_; |
88 | | std::uint64_t generation_; |
89 | | }; |
90 | | |
91 | | } // namespace proto_fuzzer |
92 | | |
93 | | #endif // PROTO_FUZZER_MULTI_SOCKET_DRIVER_H_ |