commit 12dffe54681d185c95b77d0d86c6aad1a1bfdd08
parent 1cb0e6eb0203803b6105bc8de43018d9f6f3fc70
Author: anon <anon@example.com>
Date: Thu, 15 Oct 2026 11:25:14 +0000
Implement connected TCP event notifications on Linux
Diffstat:
6 files changed, 399 insertions(+), 18 deletions(-)
diff --git a/decompiled/Makefile.linux b/decompiled/Makefile.linux
@@ -74,7 +74,7 @@ build/linux/guest-scanf: tests/guest_scanf.c runtime/guest_crt.c runtime/guest_h
mkdir -p $(@D)
$(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@
-build/linux/network-startup: tests/network_startup.c runtime/linux_network.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/linux_network.h runtime/guest_memory.h
+build/linux/network-startup: tests/network_startup.c runtime/linux_network.c runtime/linux_sync.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/linux_network.h runtime/linux_sync.h runtime/guest_memory.h
mkdir -p $(@D)
$(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -pthread -lm -o $@
diff --git a/decompiled/runtime/linux_network.c b/decompiled/runtime/linux_network.c
@@ -1,11 +1,13 @@
#define _GNU_SOURCE
#include "linux_network.h"
+#include "linux_sync.h"
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <poll.h>
#include <sys/ioctl.h>
+#include <sys/epoll.h>
#include <time.h>
#include <unistd.h>
@@ -14,11 +16,15 @@ static pthread_mutex_t socket_mutex = PTHREAD_MUTEX_INITIALIZER;
struct guest_socket {
uint32_t handle;
int descriptor, connecting;
- unsigned shutdown; /* Local read/write bits 1/2; observed peer TCP EOF bit 4. */
+ unsigned shutdown; /* Local read/write bits 1/2; observed peer TCP FIN bit 4. */
+ struct semaphore_handle *event;
+ uint32_t selected, armed, recorded, close_error, receive_error;
+ int watched;
struct guest_socket *next;
};
static struct guest_socket *sockets;
static uint32_t next_handle = 0x09000000u;
+static int socket_events = -1;
static uint32_t socket_error(int error)
{
@@ -64,6 +70,150 @@ static struct guest_socket *find_socket_locked(uint32_t handle)
return NULL;
}
+static void arm_socket_events_locked(struct guest_socket *socket)
+{
+ uint32_t mask = socket->selected & socket->armed;
+ if (!mask) {
+ if (socket->watched && epoll_ctl(socket_events, EPOLL_CTL_DEL, socket->descriptor, NULL)) abort();
+ socket->watched = 0;
+ return;
+ }
+ struct epoll_event event = {.events = EPOLLONESHOT, .data.u32 = socket->handle};
+ if (mask & 1) event.events |= EPOLLIN;
+ if (mask & 32) event.events |= EPOLLRDHUP;
+ if (epoll_ctl(socket_events, socket->watched ? EPOLL_CTL_MOD : EPOLL_CTL_ADD, socket->descriptor, &event)) abort();
+ socket->watched = 1;
+}
+
+static void stop_socket_events_locked(struct guest_socket *socket)
+{
+ socket->selected = socket->armed = socket->recorded = 0;
+ arm_socket_events_locked(socket);
+ if (socket->event) linux_sync_event_release(socket->event);
+ socket->event = NULL;
+}
+
+static void record_socket_event_locked(struct guest_socket *socket, uint32_t mask, uint32_t error)
+{
+ socket->recorded |= mask;
+ socket->armed &= ~mask;
+ if (mask == 32) socket->close_error = error;
+ if (socket->event) linux_sync_event_change(socket->event, 1);
+}
+
+static void *watch_socket_events(void *argument)
+{
+ for (;;) {
+ struct epoll_event events[16];
+ int count = epoll_wait(socket_events, events, 16, -1);
+ if (count < 0) { if (errno == EINTR) continue; abort(); }
+ if (pthread_mutex_lock(&socket_mutex)) abort();
+ for (int i = 0; i < count; ++i) {
+ struct guest_socket *socket;
+ for (socket = sockets; socket; socket = socket->next)
+ if (socket->handle == events[i].data.u32) break;
+ if (!socket || !socket->selected) continue;
+
+ /* Preserve errors consumed by the monitor for the guest's next recv. */
+ if (events[i].events & EPOLLERR) {
+ int error;
+ socklen_t size = sizeof(error);
+ if (getsockopt(socket->descriptor, SOL_SOCKET, SO_ERROR, &error, &size)) abort();
+ if (error) socket->receive_error = socket_error(error);
+ }
+
+ /* Data and FIN may be reported together; a FIN alone is not FD_READ. */
+ unsigned char byte;
+ int result = recv(socket->descriptor, &byte, 1, MSG_PEEK | MSG_DONTWAIT);
+ int would_block = result < 0 && errno == EAGAIN;
+ if (result < 0 && !would_block) socket->receive_error = socket_error(errno);
+ if (!result || socket->receive_error || (events[i].events & (EPOLLRDHUP | EPOLLHUP))) {
+ if (!socket->receive_error) socket->shutdown |= 4;
+ if (socket->armed & 32) record_socket_event_locked(socket, 32, socket->receive_error);
+ }
+ if (result > 0 && (socket->armed & 1)) record_socket_event_locked(socket, 1, 0);
+ if (result > 0 || would_block) arm_socket_events_locked(socket);
+ }
+ if (pthread_mutex_unlock(&socket_mutex)) abort();
+ }
+ return NULL;
+}
+
+static void event_select_socket(void)
+{
+ uint32_t handle = MEM32(g_esp + 4), event_handle = MEM32(g_esp + 8), mask = MEM32(g_esp + 12);
+ g_esp += 16;
+ g_eax = UINT32_MAX;
+ if (pthread_mutex_lock(&socket_mutex)) abort();
+ struct guest_socket *socket = find_socket_locked(handle);
+ if (socket) {
+ if (mask & ~0x21u) {
+ fprintf(stderr, "Unsupported WSAEventSelect mask: %08x\n", mask);
+ abort();
+ }
+ struct semaphore_handle *event = mask && event_handle ? linux_sync_event_acquire(event_handle) : NULL;
+ if (mask && event_handle && !event) MEM32(g_fs_base + 0x34) = 10022;
+ else {
+ /* The reached AO path registers READ/CLOSE after its TCP connect succeeds. */
+ if (mask) {
+ struct sockaddr_in peer;
+ socklen_t size = sizeof(peer);
+ int type;
+ if (getpeername(socket->descriptor, (struct sockaddr *)&peer, &size)) {
+ fprintf(stderr, "Socket events before connection are not implemented\n");
+ abort();
+ }
+ size = sizeof(type);
+ if (getsockopt(socket->descriptor, SOL_SOCKET, SO_TYPE, &type, &size) || type != SOCK_STREAM) abort();
+ }
+ int nonblocking = 1;
+ if (ioctl(socket->descriptor, FIONBIO, &nonblocking)) abort();
+ if (mask && socket_events < 0) {
+ socket_events = epoll_create1(EPOLL_CLOEXEC);
+ pthread_t thread;
+ if (socket_events < 0 || pthread_create(&thread, NULL, watch_socket_events, NULL) || pthread_detach(thread)) abort();
+ }
+ stop_socket_events_locked(socket);
+ socket->event = event;
+ socket->selected = socket->armed = mask;
+ socket->close_error = 0;
+ arm_socket_events_locked(socket);
+ MEM32(g_fs_base + 0x34) = 0;
+ g_eax = 0;
+ }
+ }
+ if (pthread_mutex_unlock(&socket_mutex)) abort();
+}
+
+static void enum_socket_events(void)
+{
+ uint32_t handle = MEM32(g_esp + 4), event_handle = MEM32(g_esp + 8), output = MEM32(g_esp + 12);
+ g_esp += 16;
+ g_eax = UINT32_MAX;
+ if (pthread_mutex_lock(&socket_mutex)) abort();
+ struct guest_socket *socket = find_socket_locked(handle);
+ if (socket) {
+ if (!output) MEM32(g_fs_base + 0x34) = 10014;
+ else {
+ struct semaphore_handle *event = event_handle ? linux_sync_event_acquire(event_handle) : NULL;
+ if (event_handle && !event) MEM32(g_fs_base + 0x34) = 10022;
+ else {
+ MEM32(output) = socket->recorded;
+ if (socket->recorded & 1) MEM32(output + 4) = 0;
+ if (socket->recorded & 32) MEM32(output + 24) = socket->close_error;
+ socket->recorded = 0;
+ if (event) {
+ linux_sync_event_change(event, 0);
+ linux_sync_event_release(event);
+ }
+ MEM32(g_fs_base + 0x34) = 0;
+ g_eax = 0;
+ }
+ }
+ }
+ if (pthread_mutex_unlock(&socket_mutex)) abort();
+}
+
static void create_socket(void)
{
int family = MEM32(g_esp + 4), type = MEM32(g_esp + 8), protocol = MEM32(g_esp + 12);
@@ -81,7 +231,7 @@ static void create_socket(void)
else {
struct guest_socket *socket = malloc(sizeof(*socket));
if (!socket || next_handle >= 0x0a000000u) abort();
- *socket = (struct guest_socket){next_handle, descriptor, 0, 0, sockets};
+ *socket = (struct guest_socket){.handle = next_handle, .descriptor = descriptor, .next = sockets};
sockets = socket;
next_handle += 4;
g_eax = socket->handle;
@@ -99,6 +249,7 @@ static void close_socket(void)
if (pthread_mutex_lock(&socket_mutex)) abort();
struct guest_socket *socket = find_socket_locked(handle);
if (socket) {
+ stop_socket_events_locked(socket);
if (close(socket->descriptor)) MEM32(g_fs_base + 0x34) = socket_error(errno);
else {
struct guest_socket **link = &sockets;
@@ -121,6 +272,8 @@ static void control_socket(void)
struct guest_socket *socket = find_socket_locked(handle);
if (socket) {
if (!argument) MEM32(g_fs_base + 0x34) = 10014;
+ else if (command == 0x8004667eu && !MEM32(argument) && socket->selected)
+ MEM32(g_fs_base + 0x34) = 10022;
else {
unsigned long request;
if (command == 0x8004667eu) request = FIONBIO;
@@ -156,6 +309,14 @@ static int socket_descriptor(uint32_t handle, unsigned direction)
MEM32(g_fs_base + 0x34) = 10058;
socket = NULL;
}
+ if (socket && direction == 1 && socket->receive_error) {
+ int unread;
+ if (ioctl(socket->descriptor, FIONREAD, &unread)) abort();
+ if (!unread) {
+ MEM32(g_fs_base + 0x34) = socket->receive_error;
+ socket = NULL;
+ }
+ }
int descriptor = socket ? fcntl(socket->descriptor, F_DUPFD_CLOEXEC, 0) : -1;
if (socket && descriptor < 0) MEM32(g_fs_base + 0x34) = socket_error(errno);
if (pthread_mutex_unlock(&socket_mutex)) abort();
@@ -223,6 +384,25 @@ static void socket_address(int peer)
static void local_socket_address(void) { socket_address(0); }
static void peer_socket_address(void) { socket_address(1); }
+static uint32_t finish_socket_read(uint32_t handle, int32_t length, uint32_t error)
+{
+ if (pthread_mutex_lock(&socket_mutex)) abort();
+ for (struct guest_socket *socket = sockets; socket; socket = socket->next) {
+ if (socket->handle != handle) continue;
+ /* The monitor and recv may observe a reset in either order. */
+ if (error == 10053 || error == 10054 || error == 10060) socket->receive_error = error;
+ if (length && (g_eax == 0 || g_eax == UINT32_MAX) && socket->receive_error) {
+ error = socket->receive_error;
+ g_eax = UINT32_MAX;
+ }
+ socket->armed |= socket->selected & 1;
+ arm_socket_events_locked(socket);
+ break;
+ }
+ if (pthread_mutex_unlock(&socket_mutex)) abort();
+ return error;
+}
+
static void transfer_socket(int sending)
{
uint32_t handle = MEM32(g_esp + 4), buffer = MEM32(g_esp + 8), flags = MEM32(g_esp + 16);
@@ -257,6 +437,7 @@ static void transfer_socket(int sending)
}
}
if (close(descriptor)) abort();
+ if (!sending && buffer && length >= 0) error = finish_socket_read(handle, length, error);
MEM32(g_fs_base + 0x34) = error;
}
@@ -456,6 +637,7 @@ static void wsa_cleanup(void)
while (sockets) {
struct guest_socket *socket = sockets;
sockets = socket->next;
+ stop_socket_events_locked(socket);
if (close(socket->descriptor)) abort();
free(socket);
}
@@ -515,9 +697,10 @@ recomp_func_t linux_network_lookup(const char *name, uint32_t ordinal)
{"send", 19, send_socket}, {"recv", 16, receive_socket},
{"shutdown", 22, shutdown_socket},
{"getsockname", 6, local_socket_address}, {"getpeername", 5, peer_socket_address},
+ {"WSAEventSelect", 0, event_select_socket}, {"WSAEnumNetworkEvents", 0, enum_socket_events},
};
for (unsigned i = 0; i < sizeof(functions) / sizeof(functions[0]); ++i)
- if (name ? !strcmp(name, functions[i].name) : ordinal == functions[i].ordinal)
+ if (name ? !strcmp(name, functions[i].name) : ordinal && ordinal == functions[i].ordinal)
return functions[i].function;
return NULL;
}
diff --git a/decompiled/runtime/linux_sync.c b/decompiled/runtime/linux_sync.c
@@ -181,18 +181,23 @@ static void create_event_locked(void)
g_esp += 20;
}
+static void change_event_object_locked(struct semaphore_handle *handle, int signaled)
+{
+ /* Auto-reset wakes one waiter; manual-reset stays signaled for all waiters. */
+ atomic_store(&handle->state->count, signaled);
+ if (signaled && pthread_cond_broadcast(&changed)) abort();
+ if (signaled && syscall(SYS_futex, &handle->state->count, FUTEX_WAKE,
+ handle->kind == 3 ? INT_MAX : 1, NULL, NULL, 0) < 0)
+ system_failure("event wake");
+}
+
static void change_event_locked(int signaled)
{
struct semaphore_handle *handle = find_handle_locked(MEM32(g_esp + 4));
g_eax = 0;
if (handle && handle->kind != 1 && handle->kind != 3) MEM32(g_fs_base + 0x34) = 6;
else if (handle) {
- /* Auto-reset wakes one waiter; manual-reset stays signaled for all waiters. */
- atomic_store(&handle->state->count, signaled);
- if (signaled && pthread_cond_broadcast(&changed)) abort();
- if (signaled && syscall(SYS_futex, &handle->state->count, FUTEX_WAKE,
- handle->kind == 3 ? INT_MAX : 1, NULL, NULL, 0) < 0)
- system_failure("event wake");
+ change_event_object_locked(handle, signaled);
g_eax = 1;
}
g_esp += 8;
@@ -248,6 +253,31 @@ static void release_handle_locked(struct semaphore_handle *handle)
free(handle);
}
+/* Native I/O workers retain event objects without entering a guest CPU context. */
+struct semaphore_handle *linux_sync_event_acquire(uint32_t id)
+{
+ if (pthread_mutex_lock(&mutex)) abort();
+ struct semaphore_handle *event = find_handle_locked(id);
+ if (event && event->kind != 1 && event->kind != 3) event = NULL;
+ if (event) ++event->references;
+ if (pthread_mutex_unlock(&mutex)) abort();
+ return event;
+}
+
+void linux_sync_event_release(struct semaphore_handle *event)
+{
+ if (pthread_mutex_lock(&mutex)) abort();
+ release_handle_locked(event);
+ if (pthread_mutex_unlock(&mutex)) abort();
+}
+
+void linux_sync_event_change(struct semaphore_handle *event, int signaled)
+{
+ if (pthread_mutex_lock(&mutex)) abort();
+ change_event_object_locked(event, signaled);
+ if (pthread_mutex_unlock(&mutex)) abort();
+}
+
static void close_handle_locked(void)
{
struct semaphore_handle *handle = find_handle_locked(MEM32(g_esp + 4));
diff --git a/decompiled/runtime/linux_sync.h b/decompiled/runtime/linux_sync.h
@@ -2,4 +2,8 @@
#define LINUX_SYNC_H
#include "recomp_types.h"
recomp_func_t linux_sync_lookup(const char *name);
+struct semaphore_handle;
+struct semaphore_handle *linux_sync_event_acquire(uint32_t handle);
+void linux_sync_event_release(struct semaphore_handle *event);
+void linux_sync_event_change(struct semaphore_handle *event, int signaled);
#endif
diff --git a/decompiled/tests/network_startup.c b/decompiled/tests/network_startup.c
@@ -6,6 +6,7 @@
#include <winsock2.h>
#else
#include "linux_network.h"
+#include "linux_sync.h"
#include "guest_memory.h"
#include <sys/utsname.h>
#include <pthread.h>
@@ -13,7 +14,7 @@
#include <unistd.h>
#include <time.h>
-static uint32_t call(uint32_t ordinal, unsigned count, const uint32_t *arguments)
+static uint32_t call_function(recomp_func_t function, unsigned count, const uint32_t *arguments)
{
g_esp = g_fs_base + 0xef00;
for (unsigned i = 0; i < count; ++i) MEM32(g_esp + 4 + i * 4) = arguments[i];
@@ -21,12 +22,17 @@ static uint32_t call(uint32_t ordinal, unsigned count, const uint32_t *arguments
g_esi = 0x23456789;
g_edi = 0x3456789a;
g_ebp = 0x456789ab;
- linux_network_lookup(NULL, ordinal)();
+ function();
assert(g_esp == g_fs_base + 0xef00 + (count + 1) * 4);
assert(g_ebx == 0x12345678 && g_esi == 0x23456789);
assert(g_edi == 0x3456789a && g_ebp == 0x456789ab);
return g_eax;
}
+
+static uint32_t call(uint32_t ordinal, unsigned count, const uint32_t *arguments)
+{
+ return call_function(linux_network_lookup(NULL, ordinal), count, arguments);
+}
#endif
static uint32_t digest = 2166136261u;
@@ -244,6 +250,70 @@ static void socket_addresses(uint32_t handle, const struct sockaddr_in *local,
}
}
+static uint32_t create_network_event(void)
+{
+#ifdef _WIN32
+ return (uintptr_t)CreateEventA(NULL, TRUE, FALSE, NULL);
+#else
+ uint32_t arguments[] = {0, 1, 0, 0};
+ return call_function(linux_sync_lookup("CreateEventA"), 4, arguments);
+#endif
+}
+
+static uint32_t wait_network_event(uint32_t event, uint32_t timeout)
+{
+#ifdef _WIN32
+ return WaitForSingleObject((HANDLE)(uintptr_t)event, timeout);
+#else
+ uint32_t arguments[] = {event, timeout};
+ return call_function(linux_sync_lookup("WaitForSingleObject"), 2, arguments);
+#endif
+}
+
+static void close_network_event(uint32_t event)
+{
+#ifdef _WIN32
+ assert(CloseHandle((HANDLE)(uintptr_t)event));
+#else
+ assert(call_function(linux_sync_lookup("CloseHandle"), 1, &event));
+#endif
+}
+
+static int select_network_events(uint32_t handle, uint32_t event, uint32_t mask)
+{
+#ifdef _WIN32
+ return WSAEventSelect(handle, (HANDLE)(uintptr_t)event, mask);
+#else
+ uint32_t arguments[] = {handle, event, mask};
+ return call_function(linux_network_lookup("WSAEventSelect", 0), 3, arguments);
+#endif
+}
+
+static uint32_t network_events(uint32_t handle, uint32_t event)
+{
+ uint32_t output[11];
+ memset(output, 0xa5, sizeof(output));
+#ifndef _WIN32
+ MEM32(0x50000c00) = 0x12345678;
+ memcpy((void *)ADDR(0x50000c04), output, sizeof(output));
+ MEM32(0x50000c30) = 0x87654321;
+ uint32_t arguments[] = {handle, event, 0x50000c04};
+#endif
+ set_error(777);
+
+#ifdef _WIN32
+ int result = WSAEnumNetworkEvents(handle, (HANDLE)(uintptr_t)event, (WSANETWORKEVENTS *)output);
+#else
+ int result = call_function(linux_network_lookup("WSAEnumNetworkEvents", 0), 3, arguments);
+ memcpy(output, (void *)ADDR(0x50000c04), sizeof(output));
+ assert(MEM32(0x50000c00) == 0x12345678 && MEM32(0x50000c30) == 0x87654321);
+#endif
+
+ assert(result == 0 && get_error() == 0);
+ for (unsigned i = 0; i < 10; ++i) assert(output[i + 1] == (output[0] & (1u << i) ? 0 : 0xa5a5a5a5));
+ return output[0];
+}
+
static int socket_control(uint32_t handle, uint32_t command, uint32_t *value)
{
#ifdef _WIN32
@@ -385,6 +455,12 @@ static void loopback_connect(int nonblocking, int listening)
uint32_t accepted = accept(listener, (struct sockaddr *)&peer, &length);
assert(accepted != UINT32_MAX && peer.sin_addr.s_addr == htonl(0x7f000001));
socket_addresses(handle, &peer, &address);
+ uint32_t event = create_network_event();
+ assert(event && !select_network_events(handle, event, 0x21));
+ assert(network_events(handle, event) == 0);
+ assert(wait_network_event(event, 0) == 258);
+ mode = 0;
+ assert(socket_control(handle, 0x8004667e, &mode) == -1 && get_error() == 10022);
struct socket_set read = {1, {handle}}, write = {1, {handle}}, except = {1, {handle}};
int32_t timeout[] = {0, 10000};
double start = monotonic_seconds();
@@ -392,6 +468,15 @@ static void loopback_connect(int nonblocking, int listening)
assert(monotonic_seconds() - start >= 0.005);
assert(timeout[0] == 0 && timeout[1] == 10000 && get_error() == 0);
assert(send(accepted, "abc", 3, 0) == 3);
+ if (nonblocking) assert(!shutdown(accepted, 1));
+ assert(wait_network_event(event, 2000) == 0);
+ uint32_t events = network_events(handle, event);
+ if (nonblocking && events != 33) {
+ assert(wait_network_event(event, 2000) == 0);
+ events |= network_events(handle, event);
+ }
+ assert(events == (nonblocking ? 33 : 1));
+ assert(wait_network_event(event, 0) == 258);
read.count = 1;
assert(socket_select(&read, NULL, NULL, NULL) == 1 && read.count == 1);
timeout[1] = 0;
@@ -403,22 +488,30 @@ static void loopback_connect(int nonblocking, int listening)
unsigned char bytes[16];
memset(bytes, 0xa5, sizeof(bytes));
- for (int sending = 0; sending < 2; ++sending) {
- assert(socket_transfer(sending, handle, bytes, -1, 0) == -1 && get_error() == 10014);
- }
+ assert(socket_transfer(1, handle, bytes, -1, 0) == -1 && get_error() == 10014);
assert(socket_transfer(1, handle, NULL, 1, 0) == -1 && get_error() == 10014);
+ assert(socket_transfer(0, handle, bytes, -1, 0) == -1 && get_error() == 10014);
+ assert(wait_network_event(event, 10) == 258);
assert(socket_transfer(0, handle, bytes, 2, 2) == 2);
assert(!memcmp(bytes, "ab", 2) && get_error() == 0);
+ assert(wait_network_event(event, 2000) == 0);
+ assert(network_events(handle, event) == 1);
+ assert(wait_network_event(event, 0) == 258);
for (unsigned i = 2; i < sizeof(bytes); ++i) assert(bytes[i] == 0xa5);
assert(!socket_control(handle, 0x4004667f, &unread) && unread == 3);
assert(socket_transfer(0, handle, bytes, 1, 0) == 1 && bytes[0] == 'a');
+ assert(wait_network_event(event, 2000) == 0);
+ assert(network_events(handle, event) == 1);
assert(socket_transfer(0, handle, bytes, 16, 0) == 2 && !memcmp(bytes, "bc", 2));
mode = 1;
assert(!socket_control(handle, 0x8004667e, &mode));
- assert(socket_transfer(0, handle, bytes, 1, 0) == -1 && get_error() == 10035);
+ assert(socket_transfer(0, handle, bytes, 1, 0) == (nonblocking ? 0 : -1));
+ assert(get_error() == (nonblocking ? 0 : 10035));
assert(!memcmp(bytes, "bc", 2));
+ assert(wait_network_event(event, 10) == 258);
+ assert(socket_transfer(0, handle, bytes, -1, 0) == -1 && get_error() == 10014);
const unsigned char response[] = {0, 1, 0xfe, 0xff};
memcpy(bytes, response, sizeof(response));
assert(socket_transfer(1, handle, bytes, 4, 0) == 4 && get_error() == 0);
@@ -429,12 +522,21 @@ static void loopback_connect(int nonblocking, int listening)
assert(socket_shutdown(handle, 1) == 0 && get_error() == 0);
assert(recv(accepted, (char *)bytes, 1, 0) == 0);
assert(socket_transfer(1, handle, bytes, 1, 0) == -1 && get_error() == 10058);
- assert(!shutdown(accepted, 1)); /* SD_SEND and SHUT_WR are both 1. */
+ if (!nonblocking) {
+ assert(!shutdown(accepted, 1)); /* SD_SEND and SHUT_WR are both 1. */
+ assert(wait_network_event(event, 2000) == 0);
+ assert(network_events(handle, event) == 32);
+ }
+ assert(wait_network_event(event, 0) == 258);
read.count = 1;
assert(socket_select(&read, NULL, NULL, NULL) == 1);
assert(socket_transfer(0, handle, bytes, 16, 0) == 0 && get_error() == 0);
assert(socket_shutdown(handle, nonblocking ? 2 : 0) == 0 && get_error() == 0);
assert(socket_transfer(0, handle, bytes, 1, 0) == -1 && get_error() == 10058);
+ assert(!select_network_events(handle, 0, 0));
+ mode = 0;
+ assert(!socket_control(handle, 0x8004667e, &mode));
+ close_network_event(event);
#ifdef _WIN32
assert(!closesocket(accepted));
#else
@@ -449,6 +551,66 @@ static void loopback_connect(int nonblocking, int listening)
#endif
}
+static void reset_notification(void)
+{
+ uint32_t listener = socket(AF_INET, SOCK_STREAM, 0);
+ assert(listener != UINT32_MAX);
+ struct sockaddr_in address = {0};
+ address.sin_family = AF_INET;
+ address.sin_addr.s_addr = htonl(0x7f000001);
+ assert(!bind(listener, (const struct sockaddr *)&address, sizeof(address)));
+#ifdef _WIN32
+ int length = sizeof(address);
+#else
+ socklen_t length = sizeof(address);
+#endif
+ assert(!getsockname(listener, (struct sockaddr *)&address, &length));
+ assert(!listen(listener, 1));
+ uint32_t handle = socket_create(1);
+ assert(!socket_connect(handle, &address, sizeof(address)));
+ uint32_t accepted = accept(listener, NULL, NULL);
+ assert(accepted != UINT32_MAX);
+ uint32_t event = create_network_event();
+ assert(!select_network_events(handle, event, 0x21));
+ struct linger option = {1, 0};
+ assert(!setsockopt(accepted, SOL_SOCKET, SO_LINGER, (const char *)&option, sizeof(option)));
+
+#ifdef _WIN32
+ assert(!closesocket(accepted));
+#else
+ assert(!close(accepted));
+#endif
+ assert(wait_network_event(event, 2000) == 0);
+ uint32_t output[11];
+ memset(output, 0xa5, sizeof(output));
+#ifdef _WIN32
+ assert(!WSAEnumNetworkEvents(handle, (HANDLE)(uintptr_t)event, (WSANETWORKEVENTS *)output));
+#else
+ memcpy((void *)ADDR(0x50000c04), output, sizeof(output));
+ uint32_t arguments[] = {handle, event, 0x50000c04};
+ assert(!call_function(linux_network_lookup("WSAEnumNetworkEvents", 0), 3, arguments));
+ memcpy(output, (void *)ADDR(0x50000c04), sizeof(output));
+#endif
+
+ /* Local abortive close is reported differently by the two TCP stacks. */
+#ifdef _WIN32
+ uint32_t reset_error = 10053;
+#else
+ uint32_t reset_error = 10054;
+#endif
+ assert(output[0] == 32 && output[6] == reset_error);
+ unsigned char bytes[16] = {0};
+ assert(socket_transfer(0, handle, bytes, 1, 0) == -1 && get_error() == 10054);
+ assert(socket_transfer(0, handle, bytes, 1, 0) == -1 && get_error() == 10054);
+ close_network_event(event);
+ assert(!socket_close(handle));
+#ifdef _WIN32
+ assert(!closesocket(listener));
+#else
+ assert(!close(listener));
+#endif
+}
+
static void socket_lifetime(int initialized)
{
uint32_t value = 1;
@@ -636,6 +798,7 @@ int main(void)
loopback_connect(1, 1);
loopback_connect(0, 0);
loopback_connect(1, 0);
+ reset_notification();
uint32_t orphan = socket_create(1);
assert(orphan != UINT32_MAX);
for (unsigned i = 0; i < initialized; ++i) cleanup(0);
@@ -657,5 +820,6 @@ int main(void)
puts("transport: binary send/recv, peek, partial reads, would-block, graceful close, invalid lengths and canaries passed");
puts("shutdown: send/receive half-close, peer EOF, disabled directions, invalid handles/modes and guest ABI passed");
puts("socket addresses: local/peer endpoints, 16-byte IPv4 layout, buffer lengths, nulls, unbound sockets and canaries passed");
+ puts("network events: asynchronous read/close signals, enum/reset, peek/partial-read rearming and blocking-mode restriction passed");
return 0;
}
diff --git a/tools/reconstruction/test_portable_network_startup.py b/tools/reconstruction/test_portable_network_startup.py
@@ -18,7 +18,7 @@ linux_output = "/mnt/" + work.drive[0].lower() + work.as_posix()[2:] + "/transla
wsl = ["wsl", "-d", "Ubuntu-26.04", "--cd", linux_path, "--exec"]
subprocess.run(wsl + [
"gcc", "-O2", "-DRECOMP_PORTABLE", "-Iruntime", "-fno-strict-aliasing",
- "tests/network_startup.c", "runtime/linux_network.c", "runtime/guest_memory.c",
+ "tests/network_startup.c", "runtime/linux_network.c", "runtime/linux_sync.c", "runtime/guest_memory.c",
"runtime/recomp_state.c", "-pthread", "-lm", "-o", linux_output,
], check=True)
actual = subprocess.check_output(wsl + [linux_output], text=True, timeout=30)