Skip to content

Commit 4d4d3fa

Browse files
authored
Fuzzilli: adapt NetworkSync for libsocket changes (googleprojectzero#277)
Update the `NetworkSync` implementation to use the new `socket_t` type. Additionally, adjust the dispatch usage to use the Windows extensions to properly handle the socket source registration.
1 parent 13e6e63 commit 4d4d3fa

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

Sources/Fuzzilli/Modules/NetworkSync.swift

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ protocol MessageHandler {
7777

7878
/// A connection to a network peer that speaks the above protocol.
7979
class Connection {
80-
/// File descriptor of the socket.
81-
let socket: Int32
80+
/// The file descriptor on POSIX or SOCKET handle on Windows of the socket.
81+
let socket: libsocket.socket_t
8282

8383
// Whether this connection has been closed.
8484
private(set) var closed = false
@@ -104,12 +104,16 @@ class Connection {
104104
/// Pending outgoing data. Must only be accessed on this connection's dispatch queue.
105105
private var sendQueue: [Data] = []
106106

107-
init(socket: Int32, handler: MessageHandler) {
107+
init(socket: libsocket.socket_t, handler: MessageHandler) {
108108
self.socket = socket
109109
self.handler = handler
110110
self.queue = DispatchQueue(label: "Socket \(socket)")
111-
111+
112+
#if os(Windows)
113+
self.readSource = DispatchSource.makeReadSource(handle: HANDLE(bitPattern: UInt(socket))!, queue: self.queue)
114+
#else
112115
self.readSource = DispatchSource.makeReadSource(fileDescriptor: socket, queue: self.queue)
116+
#endif
113117
self.readSource?.setEventHandler { [weak self] in
114118
self?.handleDataAvailable()
115119
}
@@ -216,7 +220,11 @@ class Connection {
216220
writeSource = nil
217221
} else if writeSource == nil {
218222
// Otherwise ensure we have an active write source to notify us when the next chunk can be sent
223+
#if os(Windows)
224+
writeSource = DispatchSource.makeWriteSource(handle: HANDLE(bitPattern: UInt(socket))!, queue: self.queue)
225+
#else
219226
writeSource = DispatchSource.makeWriteSource(fileDescriptor: socket, queue: self.queue)
227+
#endif
220228
writeSource?.setEventHandler { [weak self] in
221229
self?.sendPendingData()
222230
}
@@ -292,8 +300,8 @@ class Connection {
292300
}
293301

294302
public class NetworkMaster: Module, MessageHandler {
295-
/// File descriptor of the server socket.
296-
private var serverFd: Int32 = -1
303+
/// File descriptor or SOCKET handle of the server socket.
304+
private var serverFd: libsocket.socket_t = INVALID_SOCKET
297305

298306
/// Associated fuzzer.
299307
unowned let fuzzer: Fuzzer
@@ -311,7 +319,7 @@ public class NetworkMaster: Module, MessageHandler {
311319
private var serverQueue: DispatchQueue? = nil
312320

313321
/// Active workers. The key is the socket filedescriptor number.
314-
private var workers = [Int32: Worker]()
322+
private var workers = [libsocket.socket_t: Worker]()
315323

316324
/// Since fuzzer state can grow quite large (> 100MB) and takes long to serialize,
317325
/// we cache the serialized state for a short time.
@@ -334,7 +342,11 @@ public class NetworkMaster: Module, MessageHandler {
334342
}
335343

336344
self.serverQueue = DispatchQueue(label: "Server Queue \(serverFd)")
345+
#if os(Windows)
346+
self.connectionSource = DispatchSource.makeReadSource(handle: HANDLE(bitPattern: UInt(serverFd))!, queue: serverQueue)
347+
#else
337348
self.connectionSource = DispatchSource.makeReadSource(fileDescriptor: serverFd, queue: serverQueue)
349+
#endif
338350
self.connectionSource?.setEventHandler {
339351
let socket = libsocket.socket_accept(self.serverFd)
340352
fuzzer.async {
@@ -381,7 +393,7 @@ public class NetworkMaster: Module, MessageHandler {
381393
}
382394
}
383395

384-
private func handleNewConnection(_ socket: Int32) {
396+
private func handleNewConnection(_ socket: libsocket.socket_t) {
385397
guard socket > 0 else {
386398
return logger.error("Failed to accept client connection")
387399
}
@@ -647,17 +659,16 @@ public class NetworkWorker: Module, MessageHandler {
647659
}
648660

649661
private func connect() {
650-
var fd: Int32 = -1
662+
var fd: libsocket.socket_t = INVALID_SOCKET
651663
for _ in 0..<10 {
652664
fd = libsocket.socket_connect(masterHostname, masterPort)
653-
if fd >= 0 {
665+
if fd != INVALID_SOCKET {
654666
break
655-
} else {
656-
logger.warning("Failed to connect to master. Retrying in 30 seconds")
657-
Thread.sleep(forTimeInterval: 30)
658667
}
668+
logger.warning("Failed to connect to master. Retrying in 30 seconds")
669+
Thread.sleep(forTimeInterval: 30)
659670
}
660-
if fd < 0 {
671+
if fd == INVALID_SOCKET {
661672
logger.fatal("Failed to connect to master")
662673
}
663674

Sources/libsocket/include/libsocket.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ typedef __typeof__(_Generic((size_t)0, \
3737
#endif
3838
#else
3939
typedef int socket_t;
40+
#define INVALID_SOCKET (-1)
4041
#endif
4142

4243
socket_t socket_listen(const char* address, uint16_t port);

Sources/libsocket/socket-posix.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
socket_t socket_listen(const char* address, uint16_t port) {
2828
socket_t fd = socket(AF_INET, SOCK_STREAM, 0);
2929
if (fd < 0) {
30-
return -1;
30+
return INVALID_SOCKET;
3131
}
3232

3333
int arg = 1;
@@ -36,7 +36,7 @@ socket_t socket_listen(const char* address, uint16_t port) {
3636
int flags = fcntl(fd, F_GETFL, 0);
3737
if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
3838
close(fd);
39-
return -2;
39+
return INVALID_SOCKET;
4040
}
4141

4242
struct sockaddr_in serv_addr;
@@ -47,7 +47,7 @@ socket_t socket_listen(const char* address, uint16_t port) {
4747

4848
if (bind(fd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
4949
close(fd);
50-
return -3;
50+
return INVALID_SOCKET;
5151
}
5252

5353
listen(fd, 256);
@@ -57,7 +57,7 @@ socket_t socket_listen(const char* address, uint16_t port) {
5757
socket_t socket_accept(socket_t fd) {
5858
socket_t client_fd = accept(fd, NULL, 0);
5959
if (client_fd < 0) {
60-
return -1;
60+
return INVALID_SOCKET;
6161
}
6262

6363
#ifdef __APPLE__
@@ -68,11 +68,11 @@ socket_t socket_accept(socket_t fd) {
6868
int flags = fcntl(client_fd, F_GETFL, 0);
6969
if (fcntl(client_fd, F_SETFL, flags | O_NONBLOCK) == -1) {
7070
close(client_fd);
71-
return -2;
71+
return INVALID_SOCKET;
7272
}
7373
if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
7474
close(fd);
75-
return -3;
75+
return INVALID_SOCKET;
7676
}
7777

7878
return client_fd;
@@ -90,7 +90,7 @@ socket_t socket_connect(const char* address, uint16_t port) {
9090

9191
struct addrinfo* result;
9292
if (getaddrinfo (address, portbuf, &hint, &result) != 0) {
93-
return -1;
93+
return INVALID_SOCKET;
9494
}
9595

9696
socket_t fd;
@@ -112,7 +112,7 @@ socket_t socket_connect(const char* address, uint16_t port) {
112112
freeaddrinfo(result);
113113

114114
if (addr == NULL) {
115-
return -2;
115+
return INVALID_SOCKET;
116116
}
117117

118118
#ifdef __APPLE__
@@ -123,11 +123,11 @@ socket_t socket_connect(const char* address, uint16_t port) {
123123
int flags = fcntl(fd, F_GETFL, 0);
124124
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
125125
close(fd);
126-
return -3;
126+
return INVALID_SOCKET;
127127
}
128128
if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
129129
close(fd);
130-
return -4;
130+
return INVALID_SOCKET;
131131
}
132132

133133
return fd;

0 commit comments

Comments
 (0)