Skip to content

Commit ad99c41

Browse files
Fix Win32 compilation failure (#524)
* Fix libsocket typedef name * Add Win32-specialized code for killing tasks
1 parent 6f97546 commit ad99c41

3 files changed

Lines changed: 27 additions & 13 deletions

File tree

Sources/Fuzzilli/Util/JavaScriptExecutor.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

1515
import Foundation
1616

17+
#if os(Windows)
18+
import WinSDK
19+
#endif /* os(Windows) */
20+
1721
public class JavaScriptExecutor {
1822
/// Path to the js shell binary.
1923
let executablePath: String
@@ -103,11 +107,21 @@ public class JavaScriptExecutor {
103107
break
104108
}
105109
}
106-
if task.isRunning {
110+
runningCheck: if task.isRunning {
111+
timedOut = true
112+
#if os(Windows)
113+
guard let processHandle = OpenProcess(DWORD(PROCESS_TERMINATE), false, DWORD(task.processIdentifier)) else {
114+
// Fall back to built-in termination
115+
task.terminate()
116+
break runningCheck
117+
}
118+
defer { CloseHandle(processHandle) }
119+
TerminateProcess(processHandle, 1)
120+
#else
107121
// Properly kill the task now with SIGKILL as it might be stuck
108122
// in Wasm, where SIGTERM is not enough.
109123
kill(task.processIdentifier, SIGKILL)
110-
timedOut = true
124+
#endif /* os(Windows) */
111125
}
112126
}
113127

Sources/libsocket/include/libsocket.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#endif
2323

2424
#if defined(_WIN32)
25-
typedef SOCKET socket_t;
25+
typedef SOCKET libsocket_t;
2626

2727
// We need C11 or newer due to the use of `_Generic`. Windows does not have a
2828
// signed size type, so we construct the equivalent type by inspecting the type

Sources/libsocket/socket-win32.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ static void __attribute__((__destructor__, __used__)) libsocket_fini(void) {
4444
(void)WSACleanup();
4545
}
4646

47-
socket_t socket_listen(const char *address, uint16_t port) {
48-
socket_t sock = socket(AF_INET, SOCK_STREAM, 0);
47+
libsocket_t socket_listen(const char *address, uint16_t port) {
48+
libsocket_t sock = socket(AF_INET, SOCK_STREAM, 0);
4949
if (sock == INVALID_SOCKET)
5050
return INVALID_SOCKET;
5151

@@ -68,8 +68,8 @@ socket_t socket_listen(const char *address, uint16_t port) {
6868
return sock;
6969
}
7070

71-
socket_t socket_accept(socket_t sock) {
72-
socket_t client = accept(sock, NULL, NULL);
71+
libsocket_t socket_accept(libsocket_t sock) {
72+
libsocket_t client = accept(sock, NULL, NULL);
7373
if (client == INVALID_SOCKET)
7474
return INVALID_SOCKET;
7575

@@ -82,7 +82,7 @@ socket_t socket_accept(socket_t sock) {
8282
return client;
8383
}
8484

85-
socket_t socket_connect(const char *address, uint16_t port) {
85+
libsocket_t socket_connect(const char *address, uint16_t port) {
8686
struct addrinfo ai;
8787
ZeroMemory(&ai, sizeof(ai));
8888
ai.ai_family = AF_UNSPEC;
@@ -96,7 +96,7 @@ socket_t socket_connect(const char *address, uint16_t port) {
9696
if (getaddrinfo(address, port_str, &ai, &ai_list))
9797
return INVALID_SOCKET;
9898

99-
socket_t sock;
99+
libsocket_t sock;
100100
struct addrinfo *ai_cur;
101101
for (ai_cur = ai_list; ai_cur; ai_cur = ai_cur->ai_next) {
102102
sock = socket(ai_cur->ai_family, ai_cur->ai_socktype, ai_cur->ai_protocol);
@@ -124,7 +124,7 @@ socket_t socket_connect(const char *address, uint16_t port) {
124124
return sock;
125125
}
126126

127-
ssize_t socket_send(socket_t sock, const uint8_t *data, size_t length) {
127+
ssize_t socket_send(libsocket_t sock, const uint8_t *data, size_t length) {
128128
assert(length <= INT_MAX && "unable to send > INT_MAX bytes");
129129
ssize_t remaining = length;
130130
while (remaining) {
@@ -137,16 +137,16 @@ ssize_t socket_send(socket_t sock, const uint8_t *data, size_t length) {
137137
return length;
138138
}
139139

140-
ssize_t socket_recv(socket_t sock, uint8_t *buffer, size_t length) {
140+
ssize_t socket_recv(libsocket_t sock, uint8_t *buffer, size_t length) {
141141
assert(length <= INT_MAX && "unable to receive >INT_MAX bytes");
142142
return recv(sock, (char *)buffer, length, 0);
143143
}
144144

145-
int socket_shutdown(socket_t socket) {
145+
int socket_shutdown(libsocket_t socket) {
146146
return shutdown(socket, SD_BOTH);
147147
}
148148

149-
int socket_close(socket_t socket) {
149+
int socket_close(libsocket_t socket) {
150150
return closesocket(socket);
151151
}
152152

0 commit comments

Comments
 (0)