aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


commit 25fb492525f1abaa7c34d0a05a29281053c475ff
parent 191fa4bdeac666a84d012c1d9c126b45a16fe3bb
Author: anon <anon@example.com>
Date:   Sat, 17 Oct 2026 18:11:02 +0000

Move music control and loading off the game loop

Diffstat:
Msrc/Makefile.linux | 16+++++++++++++++-
Msrc/Makefile.wasm | 11++++++++++-
Msrc/game/mmr.dll/lifted-0000.c | 6++++++
Msrc/game/mmr.dll/lifted-0001.c | 24++++++++++++++++++++++++
Msrc/game/mmr.dll/lifted-0002.c | 15+++++++++++++++
Msrc/runtime/guest_stdio.c | 192+++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------
Msrc/runtime/linux_audio.c | 116++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------
Msrc/runtime/linux_audio.h | 4++++
Asrc/runtime/linux_music.c | 169+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/runtime/linux_music.h | 13+++++++++++++
Msrc/runtime/linux_thread.c | 26+++++++++++++++++++++-----
Msrc/runtime/linux_thread.h | 1+
Msrc/tests/audio.c | 38++++++++++++++++++++++++++++++++++++++
Asrc/tests/music.c | 185+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/tests/stdio_concurrency.c | 101+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
15 files changed, 835 insertions(+), 82 deletions(-)

diff --git a/src/Makefile.linux b/src/Makefile.linux @@ -3,7 +3,7 @@ CPPFLAGS := -Iruntime -Igame -Idata -DRECOMP_PORTABLE CFLAGS := -O2 -fno-strict-aliasing GAME_SOURCES := $(wildcard game/*/*.c) game/dispatch.c DATA_SOURCES := $(wildcard data/*.c) -RUNTIME_SOURCES := runtime/linux_host.c runtime/linux_imports.c runtime/linux_gdi.c runtime/linux_audio.c runtime/linux_window.c runtime/linux_graphics.c runtime/gl_renderer.c runtime/linux_platform.c runtime/linux_diagnostics.c runtime/linux_sync.c runtime/linux_file.c runtime/linux_tls.c runtime/linux_thread.c runtime/linux_snapshot.c runtime/linux_network.c runtime/guest_memory.c runtime/guest_heap.c runtime/guest_crt.c runtime/guest_stdio.c runtime/guest_exception.c runtime/guest_rtti.c runtime/guest_format.c runtime/guest_msvcp.c runtime/recomp_state.c runtime/eh32.c runtime/linux_path.c +RUNTIME_SOURCES := runtime/linux_host.c runtime/linux_imports.c runtime/linux_gdi.c runtime/linux_audio.c runtime/linux_music.c runtime/linux_window.c runtime/linux_graphics.c runtime/gl_renderer.c runtime/linux_platform.c runtime/linux_diagnostics.c runtime/linux_sync.c runtime/linux_file.c runtime/linux_tls.c runtime/linux_thread.c runtime/linux_snapshot.c runtime/linux_network.c runtime/guest_memory.c runtime/guest_heap.c runtime/guest_crt.c runtime/guest_stdio.c runtime/guest_exception.c runtime/guest_rtti.c runtime/guest_format.c runtime/guest_msvcp.c runtime/recomp_state.c runtime/eh32.c runtime/linux_path.c VENDOR_SOURCES := $(wildcard vendor/msvcp60/source/*.c) $(wildcard vendor/ace/source/*.c) OBJECTS := $(patsubst %.c,build/linux/objects/%.o,$(GAME_SOURCES) $(DATA_SOURCES) $(RUNTIME_SOURCES) $(VENDOR_SOURCES)) @@ -169,6 +169,8 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric ./build/linux/guest-jump ./build/linux/guest-jump-local ./build/linux/audio + ./build/linux/music + ./build/linux/stdio-concurrency ./build/linux/guest-rand ./build/linux/semaphore ./build/linux/mutex @@ -527,3 +529,15 @@ build/linux/loading-fade: tests/loading_fade.c game/ddr.dll/lifted-0014.c runtim $(CC) $(CPPFLAGS) $(CFLAGS) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -pthread -lm -o $@ test: build/linux/loading-fade + +build/linux/music: tests/music.c runtime/linux_music.c runtime/linux_thread.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c game/mmr.dll/lifted-0001.c runtime/linux_music.h runtime/linux_thread.h + mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -pthread -lm -o $@ + +test: build/linux/music + +build/linux/stdio-concurrency: tests/stdio_concurrency.c runtime/guest_stdio.c runtime/guest_crt.c runtime/guest_format.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c runtime/linux_path.c runtime/guest_stdio.h + mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -pthread -lm -Wl,--wrap=read -o $@ + +test: build/linux/stdio-concurrency diff --git a/src/Makefile.wasm b/src/Makefile.wasm @@ -3,7 +3,7 @@ CPPFLAGS := -Iruntime -Igame -Idata -DRECOMP_PORTABLE # Keep exception landing pads in Wasm so JSPI can yield through guest calls. CFLAGS := -O2 -fno-strict-aliasing -pthread -sSUPPORT_LONGJMP=wasm PORT_FLAGS := -sUSE_SDL=2 -sUSE_FREETYPE=1 -RUNTIME_SOURCES := runtime/linux_host.c runtime/linux_imports.c runtime/linux_gdi.c runtime/linux_audio.c runtime/browser_audio.c runtime/linux_window.c runtime/linux_graphics.c runtime/gl_renderer.c runtime/linux_platform.c runtime/linux_diagnostics.c runtime/linux_sync.c runtime/linux_file.c runtime/linux_tls.c runtime/linux_thread.c runtime/linux_snapshot.c runtime/browser_network.c runtime/guest_memory.c runtime/guest_heap.c runtime/guest_crt.c runtime/guest_stdio.c runtime/guest_exception.c runtime/guest_rtti.c runtime/guest_format.c runtime/guest_msvcp.c runtime/recomp_state.c runtime/eh32.c runtime/linux_path.c runtime/browser_host.c +RUNTIME_SOURCES := runtime/linux_host.c runtime/linux_imports.c runtime/linux_gdi.c runtime/linux_audio.c runtime/linux_music.c runtime/browser_audio.c runtime/linux_window.c runtime/linux_graphics.c runtime/gl_renderer.c runtime/linux_platform.c runtime/linux_diagnostics.c runtime/linux_sync.c runtime/linux_file.c runtime/linux_tls.c runtime/linux_thread.c runtime/linux_snapshot.c runtime/browser_network.c runtime/guest_memory.c runtime/guest_heap.c runtime/guest_crt.c runtime/guest_stdio.c runtime/guest_exception.c runtime/guest_rtti.c runtime/guest_format.c runtime/guest_msvcp.c runtime/recomp_state.c runtime/eh32.c runtime/linux_path.c runtime/browser_host.c GAME_SOURCES := $(wildcard game/*/*.c) game/dispatch.c DATA_SOURCES := $(wildcard data/*.c) VENDOR_SOURCES := $(wildcard vendor/msvcp60/source/*.c) $(wildcard vendor/ace/source/*.c) @@ -117,3 +117,12 @@ build/wasm/texture-resampling.js: tests/texture_resampling.c game/bbr.dll/lifted build/wasm/loading-fade.js: tests/loading_fade.c game/ddr.dll/lifted-0014.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_memory.h mkdir -p $(@D) $(CC) $(CPPFLAGS) $(CFLAGS) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -lm -o $@ + + +build/wasm/web/music-test.html: tests/music.c runtime/linux_music.c runtime/linux_thread.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c game/mmr.dll/lifted-0001.c runtime/linux_music.h runtime/linux_thread.h + @mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -sPROXY_TO_PTHREAD -sPTHREAD_POOL_SIZE=2 -sALLOW_BLOCKING_ON_MAIN_THREAD=0 -sALLOW_MEMORY_GROWTH -o $@ + +build/wasm/web/stdio-concurrency-test.html: tests/stdio_concurrency.c runtime/guest_stdio.c runtime/guest_crt.c runtime/guest_format.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c runtime/linux_path.c runtime/guest_stdio.h + @mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -sWASMFS -sPROXY_TO_PTHREAD -sPTHREAD_POOL_SIZE=2 -sALLOW_BLOCKING_ON_MAIN_THREAD=0 -sALLOW_MEMORY_GROWTH -Wl,--wrap=read -o $@ diff --git a/src/game/mmr.dll/lifted-0000.c b/src/game/mmr.dll/lifted-0000.c @@ -1,5 +1,8 @@ #define RECOMP_GENERATED_CODE #include "recomp_types.h" +#if defined(RECOMP_PORTABLE) && !defined(_WIN32) +#include "linux_music.h" +#endif #include <math.h> #include <stdio.h> #include <stdlib.h> @@ -1950,6 +1953,9 @@ L_3A001A6A: L_3A001A71: RECOMP_ICALL(0x3A008160u); /* 0x3A001A71: call 0x3a008160 */ /* not lifted */ RECOMP_FLAGS_IN(); +#if defined(RECOMP_PORTABLE) && !defined(_WIN32) + linux_music_shutdown(edi); goto L_3A001ABB; +#endif ecx = MEM32(0x3A00D2DC); /* 0x3A001A76: mov ecx, dword ptr [0x3a00d2dc] */ /* test ecx, ecx */ /* 0x3A001A7C: test ecx, ecx */ _flag_a = (uint32_t)(ecx); _flag_b = (uint32_t)(ecx); diff --git a/src/game/mmr.dll/lifted-0001.c b/src/game/mmr.dll/lifted-0001.c @@ -1,5 +1,8 @@ #define RECOMP_GENERATED_CODE #include "recomp_types.h" +#if defined(RECOMP_PORTABLE) && !defined(_WIN32) +#include "linux_music.h" +#endif #include <math.h> #include <stdio.h> #include <stdlib.h> @@ -773,6 +776,9 @@ void sub_3A004700(void) { RECOMP_ENTER(0x3A004700u); L_3A004700: +#if defined(RECOMP_PORTABLE) && !defined(_WIN32) + if (!linux_music_worker) { linux_music_mute(ecx, MEM8(esp + 4)); esp += 8; RECOMP_REGS_OUT(); return; } +#endif PUSH32(esp, ebp); /* 0x3A004700: push ebp */ ebp = esp; /* 0x3A004701: mov ebp, esp */ SET_LO8(eax, MEM8(ebp + 0x8)); /* 0x3A004703: mov al, byte ptr [ebp + 8] */ @@ -1118,6 +1124,9 @@ void sub_3A004A10(void) { RECOMP_ENTER(0x3A004A10u); L_3A004A10: +#if defined(RECOMP_PORTABLE) && !defined(_WIN32) + if (!linux_music_worker) { linux_music_event(ecx, MEM32(esp + 4)); esp += 8; RECOMP_REGS_OUT(); return; } +#endif PUSH32(esp, ebp); /* 0x3A004A10: push ebp */ ebp = esp; /* 0x3A004A11: mov ebp, esp */ PUSH32(esp, esi); /* 0x3A004A13: push esi */ @@ -1248,6 +1257,9 @@ void sub_3A004AD0(void) { RECOMP_ENTER(0x3A004AD0u); L_3A004AD0: +#if defined(RECOMP_PORTABLE) && !defined(_WIN32) + if (!linux_music_worker) { linux_music_start(ecx); esp += 4; RECOMP_REGS_OUT(); return; } +#endif PUSH32(esp, esi); /* 0x3A004AD0: push esi */ esi = ecx; /* 0x3A004AD1: mov esi, ecx */ RECOMP_ICALL(0x3A004770u); /* 0x3A004AD3: call 0x3a004770 */ /* not lifted */ @@ -1480,6 +1492,9 @@ void sub_3A004C00(void) { RECOMP_ENTER(0x3A004C00u); L_3A004C00: +#if defined(RECOMP_PORTABLE) && !defined(_WIN32) + if (!linux_music_worker) { linux_music_zone(ecx, MEM32(esp + 4), MEM32(esp + 8)); esp += 12; RECOMP_REGS_OUT(); return; } +#endif PUSH32(esp, ebp); /* 0x3A004C00: push ebp */ ebp = esp; /* 0x3A004C01: mov ebp, esp */ PUSH32(esp, esi); /* 0x3A004C03: push esi */ @@ -1712,6 +1727,9 @@ L_3A004D30: _flag_k = FK_NARROW(FK_CMP, 24); if (CMP_EQ(_flag_a, _flag_b)) goto L_3A004D89; /* 0x3A004D3B: je 0x3a004d89 */ L_3A004D3D: +#if defined(RECOMP_PORTABLE) && !defined(_WIN32) + linux_music_shutdown(esi); goto L_3A004D82; +#endif ecx = MEM32(0x3A00D2DC); /* 0x3A004D3D: mov ecx, dword ptr [0x3a00d2dc] */ /* cmp ecx, ebx */ /* 0x3A004D43: cmp ecx, ebx */ _flag_a = (uint32_t)(ecx); _flag_b = (uint32_t)(ebx); @@ -2168,6 +2186,9 @@ L_3A004F9B: L_3A004FB9: { _st[0] = _st[0]; g_st_i64[0] = g_st_i64[0]; fp_pop(); } /* 0x3A004FB9: fstp st(0) */ L_3A004FBB: +#if defined(RECOMP_PORTABLE) && !defined(_WIN32) + linux_music_volume(edi); goto L_3A004FE1; +#endif ecx = MEM32(0x3A00D2DC); /* 0x3A004FBB: mov ecx, dword ptr [0x3a00d2dc] */ /* test ecx, ecx */ /* 0x3A004FC1: test ecx, ecx */ _flag_a = (uint32_t)(ecx); _flag_b = (uint32_t)(ecx); @@ -2518,6 +2539,9 @@ void sub_3A004FBB(void) { RECOMP_ENTER(0x3A004FBBu); L_3A004FBB: +#if defined(RECOMP_PORTABLE) && !defined(_WIN32) + linux_music_volume(edi); goto L_3A004FE1; +#endif ecx = MEM32(0x3A00D2DC); /* 0x3A004FBB: mov ecx, dword ptr [0x3a00d2dc] */ /* test ecx, ecx */ /* 0x3A004FC1: test ecx, ecx */ _flag_a = (uint32_t)(ecx); _flag_b = (uint32_t)(ecx); diff --git a/src/game/mmr.dll/lifted-0002.c b/src/game/mmr.dll/lifted-0002.c @@ -1,5 +1,8 @@ #define RECOMP_GENERATED_CODE #include "recomp_types.h" +#if defined(RECOMP_PORTABLE) && !defined(_WIN32) +#include "linux_music.h" +#endif #include <math.h> #include <stdio.h> #include <stdlib.h> @@ -7078,6 +7081,9 @@ L_3A001A6A: L_3A001A71: RECOMP_ICALL(0x3A008160u); /* 0x3A001A71: call 0x3a008160 */ /* not lifted */ RECOMP_FLAGS_IN(); +#if defined(RECOMP_PORTABLE) && !defined(_WIN32) + linux_music_shutdown(edi); goto L_3A001ABB; +#endif ecx = MEM32(0x3A00D2DC); /* 0x3A001A76: mov ecx, dword ptr [0x3a00d2dc] */ /* test ecx, ecx */ /* 0x3A001A7C: test ecx, ecx */ _flag_a = (uint32_t)(ecx); _flag_b = (uint32_t)(ecx); @@ -7377,6 +7383,9 @@ L_3A004F9B: L_3A004FB9: { _st[0] = _st[0]; g_st_i64[0] = g_st_i64[0]; fp_pop(); } /* 0x3A004FB9: fstp st(0) */ L_3A004FBB: +#if defined(RECOMP_PORTABLE) && !defined(_WIN32) + linux_music_volume(edi); goto L_3A004FE1; +#endif ecx = MEM32(0x3A00D2DC); /* 0x3A004FBB: mov ecx, dword ptr [0x3a00d2dc] */ /* test ecx, ecx */ /* 0x3A004FC1: test ecx, ecx */ _flag_a = (uint32_t)(ecx); _flag_b = (uint32_t)(ecx); @@ -8520,6 +8529,9 @@ void sub_3A007C90(void) { goto L_3A007C90; L_3A004AD0: +#if defined(RECOMP_PORTABLE) && !defined(_WIN32) + if (!linux_music_worker) { linux_music_start(ecx); esp += 4; RECOMP_REGS_OUT(); return; } +#endif PUSH32(esp, esi); /* 0x3A004AD0: push esi */ esi = ecx; /* 0x3A004AD1: mov esi, ecx */ RECOMP_ICALL(0x3A004770u); /* 0x3A004AD3: call 0x3a004770 */ /* not lifted */ @@ -8685,6 +8697,9 @@ L_3A004D30: _flag_k = FK_NARROW(FK_CMP, 24); if (CMP_EQ(_flag_a, _flag_b)) goto L_3A004D89; /* 0x3A004D3B: je 0x3a004d89 */ L_3A004D3D: +#if defined(RECOMP_PORTABLE) && !defined(_WIN32) + linux_music_shutdown(esi); goto L_3A004D82; +#endif ecx = MEM32(0x3A00D2DC); /* 0x3A004D3D: mov ecx, dword ptr [0x3a00d2dc] */ /* cmp ecx, ebx */ /* 0x3A004D43: cmp ecx, ebx */ _flag_a = (uint32_t)(ecx); _flag_b = (uint32_t)(ebx); diff --git a/src/runtime/guest_stdio.c b/src/runtime/guest_stdio.c @@ -15,11 +15,16 @@ /* FILE pointers and buffers are guest addresses. Native descriptors stay here. */ struct stream { + pthread_mutex_t mutex; + int in_use, writable; uint32_t file, input_bytes; int descriptor, text, text_eof, lookahead, input_lf; }; static struct stream streams[512]; static unsigned stream_count; +static unsigned char descriptor_numbers[512]; +static pthread_mutex_t registry_mutex = PTHREAD_MUTEX_INITIALIZER; +static pthread_once_t streams_once = PTHREAD_ONCE_INIT; static int file_error(int error) { @@ -32,11 +37,15 @@ static int file_error(int error) static void initialize_streams(void) { - if (stream_count) return; + for (unsigned i = 0; i < 512; ++i) + if (pthread_mutex_init(&streams[i].mutex, NULL)) abort(); uint32_t iob = guest_crt_data("_iob"); for (unsigned i = 0; i < 20; ++i) { streams[i].file = iob + i * 32; streams[i].descriptor = i < 3 ? fcntl(i, F_DUPFD_CLOEXEC, 3) : -1; + streams[i].in_use = i < 3; + streams[i].writable = i == 1 || i == 2; + descriptor_numbers[i] = i < 3; streams[i].text = 1; streams[i].lookahead = -1; if (i < 3 && streams[i].descriptor < 0) { perror("standard stream duplication"); abort(); } @@ -46,9 +55,13 @@ static void initialize_streams(void) static struct stream *find_stream(uint32_t file) { - initialize_streams(); + if (pthread_once(&streams_once, initialize_streams)) abort(); + if (pthread_mutex_lock(&registry_mutex)) abort(); + struct stream *stream = NULL; for (unsigned i = 0; i < stream_count; ++i) - if (streams[i].file == file) return &streams[i]; + if (streams[i].file == file) { stream = &streams[i]; break; } + if (pthread_mutex_unlock(&registry_mutex)) abort(); + if (stream) return stream; fprintf(stderr, "Unknown guest FILE address %08x\n", file); abort(); } @@ -106,10 +119,21 @@ static int flush_stream(struct stream *stream) static int flush_all(void) { - int result = 0; + /* Snapshot writable streams without waiting for unrelated input reads. */ + if (pthread_once(&streams_once, initialize_streams)) abort(); + struct stream *pending[512]; + unsigned count = 0; + if (pthread_mutex_lock(&registry_mutex)) abort(); for (unsigned i = 0; i < stream_count; ++i) - if (streams[i].descriptor >= 0 && (MEM32(streams[i].file + 12) & 2)) - if (flush_stream(&streams[i])) result = -1; + if (streams[i].in_use && streams[i].writable) pending[count++] = &streams[i]; + if (pthread_mutex_unlock(&registry_mutex)) abort(); + int result = 0; + for (unsigned i = 0; i < count; ++i) { + if (pthread_mutex_lock(&pending[i]->mutex)) abort(); + if (pending[i]->descriptor >= 0 && (MEM32(pending[i]->file + 12) & 2)) + if (flush_stream(pending[i])) result = -1; + if (pthread_mutex_unlock(&pending[i]->mutex)) abort(); + } return result; } @@ -122,6 +146,10 @@ static int close_stream(struct stream *stream) if (MEM32(file + 12) & 8) guest_free(MEM32(file + 8)); MEM32(file) = MEM32(file + 4) = MEM32(file + 8) = MEM32(file + 12) = 0; stream->descriptor = -1; + if (pthread_mutex_lock(&registry_mutex)) abort(); + descriptor_numbers[MEM32(file + 16)] = 0; + stream->writable = 0; + if (pthread_mutex_unlock(&registry_mutex)) abort(); return result; } @@ -155,14 +183,14 @@ static uint32_t open_stream(struct stream *stream, const char *name, const char free(path); if (descriptor < 0) { file_error(error); return 0; } - /* Guest descriptor numbers are independent of the host descriptor table. */ + /* Reserve the guest number under the registry lock, after file I/O. */ + if (pthread_mutex_lock(&registry_mutex)) abort(); unsigned number; - for (number = 0; number < 512; ++number) { - unsigned i; - for (i = 0; i < stream_count; ++i) - if (streams[i].descriptor >= 0 && MEM32(streams[i].file + 16) == number) break; - if (i == stream_count) break; - } + for (number = 0; number < 512 && descriptor_numbers[number]; ++number) {} + if (number == 512) abort(); + descriptor_numbers[number] = 1; + stream->writable = (flags & (2 | 0x80)) != 0; + if (pthread_mutex_unlock(&registry_mutex)) abort(); stream->descriptor = descriptor; stream->text = text; stream->text_eof = 0; @@ -174,32 +202,57 @@ static uint32_t open_stream(struct stream *stream, const char *name, const char return stream->file; } -static void crt_fopen_locked(void) +static void crt_fopen(void) { - initialize_streams(); + /* Reserve a FILE before opening it; neither open nor read holds the registry. */ + if (pthread_once(&streams_once, initialize_streams)) abort(); + if (pthread_mutex_lock(&registry_mutex)) abort(); unsigned i; - for (i = 0; i < stream_count; ++i) if (streams[i].descriptor < 0) break; - if (i == 512) { file_error(EMFILE); g_eax = 0; } - else { - if (i == stream_count) { - streams[i].file = guest_malloc(32); - if (!streams[i].file) { file_error(ENOMEM); g_eax = 0; g_esp += 4; return; } - memset((void *)ADDR(streams[i].file), 0, 32); - streams[i].descriptor = -1; - ++stream_count; + for (i = 0; i < stream_count; ++i) if (!streams[i].in_use) break; + if (i == 512) { + if (pthread_mutex_unlock(&registry_mutex)) abort(); + file_error(EMFILE); g_eax = 0; g_esp += 4; + return; + } + if (i == stream_count) { + streams[i].file = guest_malloc(32); + if (!streams[i].file) { + if (pthread_mutex_unlock(&registry_mutex)) abort(); + file_error(ENOMEM); g_eax = 0; g_esp += 4; + return; } - g_eax = open_stream(&streams[i], (const char *)ADDR(MEM32(g_esp + 4)), - (const char *)ADDR(MEM32(g_esp + 8))); + memset((void *)ADDR(streams[i].file), 0, 32); + streams[i].descriptor = -1; + ++stream_count; + } + streams[i].in_use = 1; + if (pthread_mutex_unlock(&registry_mutex)) abort(); + if (pthread_mutex_lock(&streams[i].mutex)) abort(); + g_eax = open_stream(&streams[i], (const char *)ADDR(MEM32(g_esp + 4)), + (const char *)ADDR(MEM32(g_esp + 8))); + if (!g_eax) { + if (pthread_mutex_lock(&registry_mutex)) abort(); + streams[i].in_use = 0; + if (pthread_mutex_unlock(&registry_mutex)) abort(); } + if (pthread_mutex_unlock(&streams[i].mutex)) abort(); g_esp += 4; } static void crt_freopen_locked(void) { struct stream *stream = find_stream(MEM32(g_esp + 12)); + if (pthread_mutex_lock(&registry_mutex)) abort(); + stream->in_use = 1; + if (pthread_mutex_unlock(&registry_mutex)) abort(); if (stream->descriptor >= 0) close_stream(stream); g_eax = open_stream(stream, (const char *)ADDR(MEM32(g_esp + 4)), (const char *)ADDR(MEM32(g_esp + 8))); + if (!g_eax) { + if (pthread_mutex_lock(&registry_mutex)) abort(); + stream->in_use = 0; + if (pthread_mutex_unlock(&registry_mutex)) abort(); + } g_esp += 4; } @@ -270,7 +323,11 @@ static void crt_fflush_locked(void) static void crt_fclose_locked(void) { - g_eax = close_stream(find_stream(MEM32(g_esp + 4))); + struct stream *stream = find_stream(MEM32(g_esp + 4)); + g_eax = close_stream(stream); + if (pthread_mutex_lock(&registry_mutex)) abort(); + stream->in_use = 0; + if (pthread_mutex_unlock(&registry_mutex)) abort(); g_esp += 4; } @@ -628,7 +685,7 @@ static void crt_fread_locked(void) g_esp += 4; } -static void crt_chmod_locked(void) +static void crt_chmod(void) { const char *name = (const char *)ADDR(MEM32(g_esp + 4)); uint32_t mode = MEM32(g_esp + 8); @@ -653,7 +710,7 @@ static void crt_chmod_locked(void) g_esp += 4; } -static void crt_stat_locked(void) +static void crt_stat(void) { const char *name = (const char *)ADDR(MEM32(g_esp + 4)); uint32_t output = MEM32(g_esp + 8); @@ -714,38 +771,55 @@ static void crt_fprintf_locked(void) { print_file(MEM32(g_esp + 4), MEM32(g_esp static void crt_vfprintf_locked(void) { print_file(MEM32(g_esp + 4), MEM32(g_esp + 8), MEM32(g_esp + 12)); } static void crt_printf_locked(void) { print_file(guest_crt_data("_iob") + 32, MEM32(g_esp + 4), g_esp + 8); } -/* API calls serialize shared stream/handle state and synchronous file positions. */ -static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; -static void call_serialized(recomp_func_t function) +/* Only operations on the same FILE wait for its I/O and buffered position. */ +static void call_stream(recomp_func_t function, uint32_t file) { - if (pthread_mutex_lock(&mutex)) abort(); + struct stream *stream = find_stream(file); + if (pthread_mutex_lock(&stream->mutex)) abort(); function(); - if (pthread_mutex_unlock(&mutex)) abort(); -} - -static void crt_fopen(void) { call_serialized(crt_fopen_locked); } -static void crt_freopen(void) { call_serialized(crt_freopen_locked); } -static void crt_fclose(void) { call_serialized(crt_fclose_locked); } -static void crt_fflush(void) { call_serialized(crt_fflush_locked); } -static void crt_fputc(void) { call_serialized(crt_fputc_locked); } -static void crt_fwrite(void) { call_serialized(crt_fwrite_locked); } -static void crt_fputs(void) { call_serialized(crt_fputs_locked); } -static void crt_setvbuf(void) { call_serialized(crt_setvbuf_locked); } -static void crt_fileno(void) { call_serialized(crt_fileno_locked); } -static void crt_fprintf(void) { call_serialized(crt_fprintf_locked); } -static void crt_vfprintf(void) { call_serialized(crt_vfprintf_locked); } -static void crt_printf(void) { call_serialized(crt_printf_locked); } -static void crt_chmod(void) { call_serialized(crt_chmod_locked); } -static void crt_stat(void) { call_serialized(crt_stat_locked); } -static void crt_ftell(void) { call_serialized(crt_ftell_locked); } -static void crt_fseek(void) { call_serialized(crt_fseek_locked); } -static void crt_fgetpos(void) { call_serialized(crt_fgetpos_locked); } -static void crt_fsetpos(void) { call_serialized(crt_fsetpos_locked); } -static void crt_fread(void) { call_serialized(crt_fread_locked); } -static void crt_fgetc(void) { call_serialized(crt_fgetc_locked); } -static void crt_fgets(void) { call_serialized(crt_fgets_locked); } -static void crt_fscanf(void) { call_serialized(crt_fscanf_locked); } -static void crt_filbuf(void) { call_serialized(crt_filbuf_locked); } + if (pthread_mutex_unlock(&stream->mutex)) abort(); +} + +static void crt_freopen(void) { call_stream(crt_freopen_locked, MEM32(g_esp + 12)); } +static void crt_fclose(void) { call_stream(crt_fclose_locked, MEM32(g_esp + 4)); } +static void crt_fflush(void) +{ + if (MEM32(g_esp + 4)) call_stream(crt_fflush_locked, MEM32(g_esp + 4)); + else { g_eax = flush_all(); g_esp += 4; } +} +static void crt_fputc(void) { call_stream(crt_fputc_locked, MEM32(g_esp + 8)); } +static void crt_fputs(void) { call_stream(crt_fputs_locked, MEM32(g_esp + 8)); } +static void crt_setvbuf(void) { call_stream(crt_setvbuf_locked, MEM32(g_esp + 4)); } +static void crt_fileno(void) { call_stream(crt_fileno_locked, MEM32(g_esp + 4)); } +static void crt_fprintf(void) { call_stream(crt_fprintf_locked, MEM32(g_esp + 4)); } +static void crt_vfprintf(void) { call_stream(crt_vfprintf_locked, MEM32(g_esp + 4)); } +static void crt_printf(void) { call_stream(crt_printf_locked, guest_crt_data("_iob") + 32); } +static void crt_ftell(void) { call_stream(crt_ftell_locked, MEM32(g_esp + 4)); } +static void crt_fseek(void) { call_stream(crt_fseek_locked, MEM32(g_esp + 4)); } +static void crt_fgetpos(void) { call_stream(crt_fgetpos_locked, MEM32(g_esp + 4)); } +static void crt_fsetpos(void) { call_stream(crt_fsetpos_locked, MEM32(g_esp + 4)); } +static void crt_fgetc(void) { call_stream(crt_fgetc_locked, MEM32(g_esp + 4)); } +static void crt_fscanf(void) { call_stream(crt_fscanf_locked, MEM32(g_esp + 4)); } +static void crt_filbuf(void) { call_stream(crt_filbuf_locked, MEM32(g_esp + 4)); } + +/* Empty/invalid-size operations return without looking up a FILE, as in MSVCRT. */ +static void crt_fread(void) +{ + uint64_t size = MEM32(g_esp + 8), count = MEM32(g_esp + 12); + if (!size || !count || size * count > UINT32_MAX) crt_fread_locked(); + else call_stream(crt_fread_locked, MEM32(g_esp + 16)); +} +static void crt_fwrite(void) +{ + uint64_t size = MEM32(g_esp + 8), count = MEM32(g_esp + 12); + if (!size || !count || size * count > UINT32_MAX) crt_fwrite_locked(); + else call_stream(crt_fwrite_locked, MEM32(g_esp + 16)); +} +static void crt_fgets(void) +{ + if ((int32_t)MEM32(g_esp + 8) <= 0) crt_fgets_locked(); + else call_stream(crt_fgets_locked, MEM32(g_esp + 12)); +} recomp_func_t guest_stdio_lookup(const char *name) { diff --git a/src/runtime/linux_audio.c b/src/runtime/linux_audio.c @@ -60,7 +60,9 @@ static struct audio_driver { struct audio_driver *next; uint32_t handle; SDL_AudioDeviceID device; - int sample_capacity, volume, latency; + int sample_capacity, volume, latency, closing, music_muted; + uint32_t music_generation; + unsigned references; SDL_AudioSpec format; struct audio_stream *streams; struct audio_sample *samples; @@ -198,11 +200,11 @@ static void render_audio(void *userdata, Uint8 *output, int length) for (int channel = 0; channel < driver->format.channels; ++channel) { float sample = 0; for (struct audio_stream *stream = driver->streams; stream; stream = stream->next) - if (stream->status == 4) + if (stream->status == 4 && !driver->music_muted && !driver->closing) sample += stream->samples[stream->position * driver->format.channels + channel] * (stream->volume / 127.0f) * (driver->volume / 127.0f); for (struct audio_sample *voice = driver->samples; voice; voice = voice->next) { - if (voice->status != 4 || !voice->frames) continue; + if (voice->status != 4 || !voice->frames || driver->closing) continue; unsigned index = voice->position; unsigned next = index + 1 < voice->frames ? index + 1 : index; float fraction = voice->position - index; @@ -231,7 +233,7 @@ static void render_audio(void *userdata, Uint8 *output, int length) for (struct audio_stream *stream = driver->streams; stream; stream = stream->next) if (stream->status == 4 && ++stream->position == stream->frames) stream->status = 2; for (struct audio_sample *voice = driver->samples; voice; voice = voice->next) { - if (voice->status != 4 || !voice->frames) continue; + if (voice->status != 4 || !voice->frames || driver->closing) continue; voice->position += (double)voice->rate / driver->format.freq; while (voice->position >= voice->frames) { if (voice->loops == 1) { voice->status = 2; voice->position = voice->frames; break; } @@ -242,9 +244,11 @@ static void render_audio(void *userdata, Uint8 *output, int length) } } -static void destroy_driver_locked(struct audio_driver *driver) +static void destroy_driver(struct audio_driver *driver) { + /* Joining the output worker must not hold a lock used by the game loop. */ audio_output_close(driver->device); + if (pthread_mutex_lock(&audio_mutex)) abort(); while (driver->streams) { struct audio_stream *stream = driver->streams; driver->streams = stream->next; @@ -259,6 +263,7 @@ static void destroy_driver_locked(struct audio_driver *driver) free(sample); } free(driver); + if (pthread_mutex_unlock(&audio_mutex)) abort(); } static void initialize_audio(void) @@ -360,6 +365,7 @@ static void wave_out_open(void) #endif if (device) { driver->handle = next_driver++; + driver->references = 1; driver->device = device; driver->sample_capacity = sample_capacity; driver->volume = 127; @@ -384,17 +390,79 @@ static void wave_out_open(void) g_esp += 20; } +/* A queued music command and the music owner each retain their driver. */ +void linux_audio_retain_driver(uint32_t handle) +{ + if (pthread_mutex_lock(&audio_mutex)) abort(); + struct audio_driver *driver = drivers; + while (driver && driver->handle != handle) driver = driver->next; + if (!driver) abort(); + ++driver->references; + if (pthread_mutex_unlock(&audio_mutex)) abort(); +} + +static struct audio_driver *release_driver_locked(struct audio_driver **link) +{ + struct audio_driver *driver = *link; + if (--driver->references) return NULL; + *link = driver->next; + return driver; +} + +void linux_audio_release_driver(uint32_t handle) +{ + if (pthread_mutex_lock(&audio_mutex)) abort(); + struct audio_driver **link = &drivers; + while (*link && (*link)->handle != handle) link = &(*link)->next; + if (!*link) abort(); + struct audio_driver *closed = release_driver_locked(link); + if (pthread_mutex_unlock(&audio_mutex)) abort(); + if (closed) destroy_driver(closed); +} + +uint32_t linux_audio_mute_music(uint32_t handle, int muted) +{ + if (pthread_mutex_lock(&audio_mutex)) abort(); + struct audio_driver *driver = drivers; + while (driver && driver->handle != handle) driver = driver->next; + if (!driver) abort(); + audio_output_lock(driver->device); + /* Unmute alone must not revive a play submitted before the last mute. */ + if (muted) { + driver->music_muted = 1; + ++driver->music_generation; + } + uint32_t generation = driver->music_generation; + audio_output_unlock(driver->device); + if (pthread_mutex_unlock(&audio_mutex)) abort(); + return generation; +} + +void linux_audio_enable_music(uint32_t handle, uint32_t generation) +{ + if (pthread_mutex_lock(&audio_mutex)) abort(); + struct audio_driver *driver = drivers; + while (driver && driver->handle != handle) driver = driver->next; + if (!driver) abort(); + audio_output_lock(driver->device); + if (generation == driver->music_generation) driver->music_muted = 0; + audio_output_unlock(driver->device); + if (pthread_mutex_unlock(&audio_mutex)) abort(); +} + static void wave_out_close(void) { uint32_t handle = MEM32(g_esp + 4); if (pthread_mutex_lock(&audio_mutex)) abort(); - struct audio_driver **driver = &drivers; - while (*driver && (*driver)->handle != handle) driver = &(*driver)->next; - if (!*driver) abort(); - struct audio_driver *closed = *driver; - *driver = closed->next; - destroy_driver_locked(closed); + struct audio_driver **link = &drivers; + while (*link && (*link)->handle != handle) link = &(*link)->next; + if (!*link || (*link)->closing) abort(); + audio_output_lock((*link)->device); + (*link)->closing = 1; + audio_output_unlock((*link)->device); + struct audio_driver *closed = release_driver_locked(link); if (pthread_mutex_unlock(&audio_mutex)) abort(); + if (closed) destroy_driver(closed); g_esp += 8; } @@ -415,13 +483,29 @@ static void set_digital_master_volume(void) static void miles_shutdown(void) { if (pthread_mutex_lock(&audio_mutex)) abort(); - while (drivers) { - struct audio_driver *closed = drivers; - drivers = closed->next; - destroy_driver_locked(closed); + struct audio_driver *closed = NULL, **link = &drivers; + while (*link) { + struct audio_driver *driver = *link; + if (!driver->closing) { + audio_output_lock(driver->device); + driver->closing = 1; + audio_output_unlock(driver->device); + struct audio_driver *released = release_driver_locked(link); + if (released) { + released->next = closed; + closed = released; + continue; + } + } + link = &driver->next; } miles_started = 0; if (pthread_mutex_unlock(&audio_mutex)) abort(); + while (closed) { + struct audio_driver *next = closed->next; + destroy_driver(closed); + closed = next; + } g_esp += 4; } @@ -489,6 +573,7 @@ static void open_stream(void) stream->frames = size / sizeof(float) / channels; stream->status = 2; stream->volume = 127; + fprintf(stderr, "[linux-audio] decoded %s: %u frames\n", path, stream->frames); /* The driver may have closed while file I/O ran independently. */ if (pthread_mutex_lock(&audio_mutex)) abort(); driver = drivers; @@ -501,7 +586,6 @@ static void open_stream(void) driver->streams = stream; audio_output_unlock(driver->device); g_eax = stream->handle; - fprintf(stderr, "[linux-audio] decoded %s: %u frames\n", path, stream->frames); } else { free(stream->samples); free(stream); diff --git a/src/runtime/linux_audio.h b/src/runtime/linux_audio.h @@ -2,5 +2,9 @@ #define LINUX_AUDIO_H #include "recomp_types.h" void linux_audio_load_sound(void); +void linux_audio_retain_driver(uint32_t handle); +void linux_audio_release_driver(uint32_t handle); +uint32_t linux_audio_mute_music(uint32_t handle, int muted); +void linux_audio_enable_music(uint32_t handle, uint32_t generation); recomp_func_t linux_audio_lookup(const char *dll, const char *name); #endif diff --git a/src/runtime/linux_music.c b/src/runtime/linux_music.c @@ -0,0 +1,169 @@ +#include "linux_music.h" +#include "linux_audio.h" +#include "linux_thread.h" +#include "guest_heap.h" +#include <pthread.h> + +/* Only this worker enters mmr's music controller and its pr player. */ +_Thread_local int linux_music_worker; +enum music_operation { MUSIC_START, MUSIC_EVENT, MUSIC_ZONE, MUSIC_MUTE, MUSIC_VOLUME, MUSIC_SHUTDOWN }; +struct music_command { + enum music_operation operation; + uint32_t driver, combat, zone, event, volume, generation; + uint8_t enabled, muted, force; + struct music_command *next; +}; +static struct music_command *head, *tail; +static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; +static pthread_cond_t changed = PTHREAD_COND_INITIALIZER; +static pthread_once_t once = PTHREAD_ONCE_INIT; + +static void call_music(uint32_t address, uint32_t object, unsigned count, const uint32_t *arguments) +{ + uint32_t stack = g_esp; + for (unsigned i = count; i; --i) PUSH32(g_esp, arguments[i - 1]); + PUSH32(g_esp, RECOMP_RETADDR); + g_ecx = object; + recomp_func_t function = recomp_lookup(address); + if (!function) abort(); + function(); + if (g_esp != stack) abort(); +} + +static void run_music(void) +{ + linux_music_worker = 1; + uint32_t manager = guest_malloc(0x10c), audio = guest_malloc(0x24), driver = 0; + uint32_t volume = 0; + int have_volume = 0; + if (!manager || !audio) abort(); + for (;;) { + if (pthread_mutex_lock(&mutex)) abort(); + while (!head) + if (pthread_cond_wait(&changed, &mutex)) abort(); + struct music_command *command = head; + head = command->next; + if (!head) tail = NULL; + if (pthread_mutex_unlock(&mutex)) abort(); + + /* The private manager contains only music state; no queued game pointers. */ + int release_command = 1; + if (driver != command->driver) { + if (driver) { + uint32_t muted = 1; + call_music(0x3A004700, manager, 1, &muted); + linux_audio_release_driver(driver); + } + driver = command->driver; + release_command = 0; /* Transfer the command's reference to the owner. */ + memset((void *)ADDR(manager), 0, 0x10c); + memset((void *)ADDR(audio), 0, 0x24); + MEM32(manager + 0x60) = audio; + MEM32(manager + 0x74) = UINT32_MAX; + MEM32(audio + 0xc) = driver; + MEM8(audio + 0x10) = 1; + } + MEM8(manager + 4) = command->enabled; + MEM32(manager + 0x94) = command->combat; + MEM8(manager + 0x108) = command->muted; + MEM8(manager + 0x109) = command->force; + + /* Preserve mmr's project, layer selection and pr's duration-based scheduler. */ + switch (command->operation) { + case MUSIC_START: + call_music(0x3A004AD0, manager, 0, NULL); + break; + case MUSIC_EVENT: + call_music(0x3A004A10, manager, 1, &command->event); + break; + case MUSIC_ZONE: { + uint32_t arguments[] = {command->zone, command->event}; + call_music(0x3A004C00, manager, 2, arguments); + break; + } + case MUSIC_MUTE: { + uint32_t muted = command->muted; + call_music(0x3A004700, manager, 1, &muted); + break; + } + case MUSIC_VOLUME: + volume = command->volume; + have_volume = 1; + break; + case MUSIC_SHUTDOWN: { + uint32_t muted = 1; + call_music(0x3A004700, manager, 1, &muted); + linux_audio_release_driver(driver); + driver = 0; + break; + } + } + if (have_volume && MEM32(0x3A00D2DC)) + call_music(MEM32(0x3A00A160), MEM32(0x3A00D2DC), 1, &volume); + /* A mute invalidates earlier plays even when an unmute is already queued. */ + if (!command->muted && (command->operation == MUSIC_START || + command->operation == MUSIC_EVENT || command->operation == MUSIC_ZONE)) + linux_audio_enable_music(command->driver, command->generation); + if (release_command) linux_audio_release_driver(command->driver); + free(command); + } +} + +static void start_worker(void) { linux_thread_start_worker(run_music); } + +static int enqueue(uint32_t manager, enum music_operation operation, uint32_t zone, uint32_t event) +{ + uint32_t audio = MEM32(manager + 0x60); + if (!audio || !MEM8(audio + 0x10)) return 0; + struct music_command *command = calloc(1, sizeof(*command)); + if (!command) abort(); + command->operation = operation; + command->driver = MEM32(audio + 0xc); + command->enabled = MEM8(manager + 4); + command->combat = MEM32(manager + 0x94); + command->muted = MEM8(manager + 0x108); + command->force = MEM8(manager + 0x109); + command->zone = zone; + command->event = event; + command->volume = MEM32(0x3A00D03C); + linux_audio_retain_driver(command->driver); + /* Mute immediately, including streams that an outstanding open will publish. */ + command->generation = linux_audio_mute_music(command->driver, + operation == MUSIC_SHUTDOWN || command->muted); + if (pthread_once(&once, start_worker)) abort(); + if (pthread_mutex_lock(&mutex)) abort(); + if (tail) tail->next = command; + else head = command; + tail = command; + if (pthread_cond_signal(&changed)) abort(); + if (pthread_mutex_unlock(&mutex)) abort(); + return 1; +} + +void linux_music_start(uint32_t manager) { enqueue(manager, MUSIC_START, 0, 0); } +void linux_music_event(uint32_t manager, uint32_t event) { enqueue(manager, MUSIC_EVENT, 0, event); } + +void linux_music_zone(uint32_t manager, uint32_t zone, uint32_t event) +{ + if (!MEM8(manager + 4)) { + MEM32(manager + 0x64) = MEM32(manager + 0x74) = UINT32_MAX; + return; + } + MEM32(manager + 0x64) = zone; + enqueue(manager, MUSIC_ZONE, zone, event); +} + +void linux_music_mute(uint32_t manager, uint8_t muted) +{ + MEM8(manager + 0x108) = muted; + if (muted) MEM32(manager + 0x74) = UINT32_MAX; + enqueue(manager, MUSIC_MUTE, 0, 0); +} + +void linux_music_volume(uint32_t manager) +{ + if (!MEM8(0x3A00D040)) return; + if (enqueue(manager, MUSIC_VOLUME, 0, 0)) MEM8(0x3A00D040) = 0; +} + +void linux_music_shutdown(uint32_t manager) { enqueue(manager, MUSIC_SHUTDOWN, 0, 0); } diff --git a/src/runtime/linux_music.h b/src/runtime/linux_music.h @@ -0,0 +1,13 @@ +#ifndef LINUX_MUSIC_H +#define LINUX_MUSIC_H +#include <stdint.h> + +extern _Thread_local int linux_music_worker; +void linux_music_start(uint32_t manager); +void linux_music_event(uint32_t manager, uint32_t event); +void linux_music_zone(uint32_t manager, uint32_t zone, uint32_t event); +void linux_music_mute(uint32_t manager, uint8_t muted); +void linux_music_volume(uint32_t manager); +void linux_music_shutdown(uint32_t manager); + +#endif diff --git a/src/runtime/linux_thread.c b/src/runtime/linux_thread.c @@ -12,6 +12,7 @@ void recomp_eh_thread_exit(void); /* A live thread and its open handle each own a reference. Closing a handle * never terminates the thread; waits keep their own reference while blocked. */ struct guest_thread { + void (*host_entry)(void); uint32_t handle, id, entry, argument, allocation, stack_size, exit_code; uint32_t timer_delay; unsigned references; @@ -93,6 +94,10 @@ static void *run_thread(void *argument) pthread_cleanup_push(finish_thread, thread); linux_thread_notify(2); /* DLL_THREAD_ATTACH */ + if (thread->host_entry) { + thread->host_entry(); + goto finished; + } if (thread->timer_delay) { /* A cancelled one-shot timer wakes without entering its guest callback. */ struct timespec deadline; @@ -139,10 +144,11 @@ finished: } static void create_thread(uint32_t stack_size, uint32_t entry, uint32_t argument, - uint32_t flags, uint32_t id, int automatic, uint32_t timer_delay) + uint32_t flags, uint32_t id, int automatic, uint32_t timer_delay, + void (*host_entry)(void)) { g_eax = automatic ? UINT32_MAX : 0; - if (!entry) { MEM32(guest_crt_errno_address()) = 22; return; } + if (!entry && !host_entry) { MEM32(guest_crt_errno_address()) = 22; return; } if (!stack_size) stack_size = 0x400000; uint64_t rounded = ((uint64_t)stack_size + 0xffffu) & ~0xffffull; if (rounded > UINT32_MAX - 0x10000u) { MEM32(guest_crt_errno_address()) = 12; return; } @@ -152,6 +158,7 @@ static void create_thread(uint32_t stack_size, uint32_t entry, uint32_t argument thread->allocation = guest_malloc(rounded + 0x10000); if (!thread->allocation) { free(thread); MEM32(guest_crt_errno_address()) = 12; return; } thread->entry = entry; + thread->host_entry = host_entry; thread->argument = argument; thread->timer_delay = timer_delay; thread->stack_size = rounded; @@ -191,6 +198,15 @@ static void create_thread(uint32_t stack_size, uint32_t entry, uint32_t argument check_pthread(pthread_mutex_unlock(&mutex)); } +/* Runtime workers calling guest code need the same stack, TLS and DLL setup. */ +void linux_thread_start_worker(void (*entry)(void)) +{ + uint32_t saved = g_eax; + create_thread(0, 0, 0, 0, 0, 1, 0, entry); + if (g_eax == UINT32_MAX) abort(); + g_eax = saved; +} + static void begin_thread(void) { uint32_t stack_size = MEM32(g_esp + 8), entry = MEM32(g_esp + 12); @@ -201,14 +217,14 @@ static void begin_thread(void) abort(); } g_esp += 4; /* _beginthreadex is cdecl; its entry function is stdcall. */ - create_thread(stack_size, entry, argument, flags, id, 0, 0); + create_thread(stack_size, entry, argument, flags, id, 0, 0, NULL); } static void begin_automatic_thread(void) { uint32_t entry = MEM32(g_esp + 4), stack_size = MEM32(g_esp + 8), argument = MEM32(g_esp + 12); g_esp += 4; - create_thread(stack_size, entry, argument, 0, 0, 1, 0); + create_thread(stack_size, entry, argument, 0, 0, 1, 0, NULL); } static void set_timer(void) @@ -222,7 +238,7 @@ static void set_timer(void) exit(3); } if (!delay || !entry) return; - create_thread(0, entry, argument, 0, 0, 0, delay); + create_thread(0, entry, argument, 0, 0, 0, delay, NULL); } static void kill_timer(void) diff --git a/src/runtime/linux_thread.h b/src/runtime/linux_thread.h @@ -4,5 +4,6 @@ recomp_func_t linux_thread_lookup(const char *name); void linux_thread_notify(uint32_t reason); +void linux_thread_start_worker(void (*entry)(void)); #endif diff --git a/src/tests/audio.c b/src/tests/audio.c @@ -486,6 +486,43 @@ static void *open_blocked_stream(void *argument) return (void *)(uintptr_t)g_eax; } +static void retained_music_driver(uint32_t scratch) +{ + uint32_t open[] = {scratch, 0, UINT32_MAX, scratch + 16}; + assert(!call("_AIL_waveOutOpen@16", 4, open)); + uint32_t driver = MEM32(scratch); + linux_audio_retain_driver(driver); + uint32_t stream_open[] = {driver, scratch + 64, 0}; + uint32_t stream = call("_AIL_open_stream@12", 3, stream_open); + assert(stream); + uint32_t stale_generation = linux_audio_mute_music(driver, 0); + linux_audio_mute_music(driver, 1); + uint32_t generation = linux_audio_mute_music(driver, 0); + linux_audio_enable_music(driver, stale_generation); + nonzero_samples = bad_samples = 0; + capture = 1; + + call("_AIL_start_stream@4", 1, &stream); + SDL_Delay(30); + + assert(!nonzero_samples); + linux_audio_enable_music(driver, generation); + for (unsigned i = 0; call("_AIL_stream_status@4", 1, &stream) == 4 && i < 1000; ++i) SDL_Delay(1); + assert(nonzero_samples > 0 && !bad_samples); + capture = 0; + + call("_AIL_waveOutClose@4", 1, &driver); + call("_AIL_shutdown@0", 0, NULL); + + assert(call("_AIL_stream_status@4", 1, &stream) == 2); + call("_AIL_close_stream@4", 1, &stream); + linux_audio_release_driver(driver); + assert(!call("_AIL_waveOutOpen@16", 4, open)); + driver = MEM32(scratch); + call("_AIL_waveOutClose@4", 1, &driver); + puts("Music driver: immediate mute, retained shutdown, worker release and reopening passed"); +} + static void asynchronous_sound(uint32_t scratch) { char path[] = "/tmp/ao-async-audio-XXXXXX"; @@ -581,6 +618,7 @@ static void asynchronous_sound(uint32_t scratch) block_audio_open = 0; void *result; assert(!pthread_join(music, &result) && !result); + retained_music_driver(scratch); assert(!unlink(path)); fprintf(stderr, "Async sound: blocked I/O, immediate stop, voice reuse, playback and cache ownership passed.\n"); } diff --git a/src/tests/music.c b/src/tests/music.c @@ -0,0 +1,185 @@ +#include "linux_music.h" +#include "linux_thread.h" +#include "guest_heap.h" +#include <assert.h> +#include <stdatomic.h> +#include <time.h> + +void sub_3A004700(void); +void sub_3A004A10(void); +void sub_3A004AD0(void); +void sub_3A004C00(void); +static _Atomic unsigned references = 1, attached, entered, release_load; +static _Atomic int muted; +static _Atomic unsigned generation; +static unsigned signals, plays, stops, destroys, loads, volumes; +static uint32_t events[8], forces[8], last_volume; + +/* The real mmr controller runs on a real guest-capable worker. Only pr and + the driver are substitutes, so the test can hold project loading open. */ +void linux_audio_retain_driver(uint32_t driver) { assert(driver == 11); ++references; } +void linux_audio_release_driver(uint32_t driver) { assert(driver == 11); assert(references); --references; } +uint32_t linux_audio_mute_music(uint32_t driver, int value) +{ + assert(driver == 11); + if (value) { muted = 1; ++generation; } + return generation; +} +void linux_audio_enable_music(uint32_t driver, uint32_t value) +{ + assert(driver == 11); + if (generation == value) muted = 0; +} +uint32_t guest_crt_errno_address(void) { return g_fs_base + 0x40; } +void recomp_eh_thread_exit(void) {} +recomp_func_t recomp_lookup_manual(uint32_t address) { (void)address; return NULL; } +recomp_func_t recomp_lookup_import(uint32_t address) { (void)address; abort(); } +void linux_thread_notify(uint32_t reason) +{ + assert(reason == 2); + assert(MEM32(g_fs_base + 0x18) == g_fs_base); + assert(g_esp > MEM32(g_fs_base + 8) && g_esp < MEM32(g_fs_base + 4)); + ++attached; +} + +static void wait_for(_Atomic unsigned *value, unsigned expected) +{ + for (unsigned i = 0; atomic_load(value) != expected && i < 5000; ++i) { + struct timespec delay = {0, 1000000}; + nanosleep(&delay, NULL); + } + assert(atomic_load(value) == expected); +} + +static void ensure_project(void) +{ + assert(linux_music_worker && g_ecx != 0x50001000); + assert(MEM32(MEM32(g_ecx + 0x60) + 0xc) == 11); + if (!MEM32(0x3A00D2DC)) { + if (!loads) { + entered = 1; + wait_for(&release_load, 1); + } + ++loads; + MEM32(0x3A00D2DC) = guest_malloc(32); + MEM32(MEM32(0x3A00D2DC) + 0x18) = 0x50002000; + for (unsigned i = 0; i < 17; ++i) MEM32(g_ecx + 0xc4 + 4 * i) = 100 + i; + } + g_esp += 4; +} + +static void find_layer(void) +{ + assert(linux_music_worker); + uint32_t name = MEM32(g_esp + 4); + assert(name == 0x3A00D1B8 || name == 0x3A00D21C); + g_eax = name == 0x3A00D1B8 ? 17 : 23; + g_esp += 8; +} + +static void signal_event(void) +{ + assert(linux_music_worker && signals < 8); + events[signals] = MEM32(g_esp + 4); + forces[signals++] = MEM32(g_esp + 8); + g_esp += 12; +} +static void play(void) { assert(linux_music_worker); ++plays; g_esp += 12; } +static void stop(void) { assert(linux_music_worker); ++stops; g_esp += 12; } +static void destroy(void) { assert(linux_music_worker); ++destroys; g_esp += 4; } +static void volume(void) +{ + assert(linux_music_worker); + ++volumes; + last_volume = MEM32(g_esp + 4); + g_esp += 8; +} +static void link_player(void) { MEM32(g_ecx + 0x1c) = MEM32(g_esp + 4); g_esp += 8; } +static void delete_player(void) { guest_free(MEM32(g_esp + 4)); g_esp += 4; } + +recomp_func_t recomp_lookup(uint32_t address) +{ + switch (address) { + case 0x3A004700: return sub_3A004700; + case 0x3A004770: return ensure_project; + case 0x3A004A10: return sub_3A004A10; + case 0x3A004AD0: return sub_3A004AD0; + case 0x3A004C00: return sub_3A004C00; + case 0x3A008D40: return link_player; + case 0x3A008F76: return delete_player; + case 1: return find_layer; + case 2: return signal_event; + case 3: return play; + case 4: return volume; + case 5: return stop; + case 6: return destroy; + } + fprintf(stderr, "Unexpected music test call %08x\n", address); + abort(); +} + +int main(void) +{ + assert(guest_map("music globals", 0x3A000000, 0x10000)); + assert(guest_map("music main thread", 0x50000000, 0x20000)); + g_fs_base = 0x50010000; + g_esp = 0x5000ff00; + MEM32(0x3A00A150) = 1; MEM32(0x3A00A154) = 2; MEM32(0x3A00A158) = 3; + MEM32(0x3A00A160) = 4; MEM32(0x3A00A164) = 5; MEM32(0x3A00A168) = 6; + uint32_t manager = 0x50001000, audio = 0x50001200; + MEM32(manager + 0x60) = audio; + MEM8(manager + 4) = MEM8(manager + 0x109) = MEM8(audio + 0x10) = 1; + MEM32(audio + 0xc) = 11; + MEM32(0x3A00D03C) = 0x3e800000; /* 0.25f */ + MEM8(0x3A00D040) = 1; + linux_music_volume(manager); + linux_music_start(manager); + wait_for(&entered, 1); + + MEM32(manager + 0x94) = 5; + MEM8(manager + 0x109) = 0; + linux_music_zone(manager, 695, 42); + MEM32(manager + 0x94) = 0; + linux_music_zone(manager, 695, (uint32_t)-911); + linux_music_zone(manager, 695, (uint32_t)-911); + linux_music_event(manager, 65535); + linux_music_mute(manager, 1); + assert(muted); + linux_music_mute(manager, 0); + assert(muted); + linux_music_event(manager, 44); + MEM32(0x3A00D03C) = 0x3f400000; /* 0.75f */ + MEM8(0x3A00D040) = 1; + linux_music_volume(manager); + linux_music_shutdown(manager); + --references; /* The game closes its driver and frees its manager immediately. */ + memset((void *)ADDR(manager), 0xa5, 0x224); + + assert(!release_load && muted && references > 0 && attached == 1); + release_load = 1; + wait_for(&references, 0); + assert(loads == 2 && signals == 5 && plays == 1 && stops == 2 && destroys == 2); + assert(events[0] == 17 && forces[0] == 1); + assert(events[1] == 105 && forces[1] == 0); + assert(events[2] == 23 && forces[2] == 0); + assert(events[3] == 65535 && forces[3] == 1); + assert(events[4] == 44 && forces[4] == 0); + assert(volumes && last_volume == 0x3f400000 && !MEM32(0x3A00D2DC)); + + references = 1; + memset((void *)ADDR(manager), 0, 0x224); + MEM32(manager + 0x60) = audio; + MEM8(manager + 4) = MEM8(manager + 0x109) = MEM8(audio + 0x10) = 1; + MEM32(audio + 0xc) = 11; + + linux_music_zone(manager, 695, (uint32_t)-911); + linux_music_shutdown(manager); + --references; + wait_for(&references, 0); + + assert(loads == 3 && signals == 6 && plays == 2 && stops == 3 && destroys == 3); + assert(events[5] == 23 && forces[5] == 1 && last_volume == 0x3f400000); + puts("Music: blocked project load does not block zone, volume, mute or shutdown; snapshots and driver lifetime passed"); + puts("Music: startup, combat override, neutral zone, duplicate event, forced transition and mute/unmute preserved"); + return 0; +} diff --git a/src/tests/stdio_concurrency.c b/src/tests/stdio_concurrency.c @@ -0,0 +1,101 @@ +#include "guest_stdio.h" +#include "guest_crt.h" +#include "guest_memory.h" +#include <assert.h> +#include <pthread.h> +#include <stdatomic.h> +#include <time.h> +#include <unistd.h> + +static _Thread_local int blocked_reader; +static _Atomic int entered, release_read; +ssize_t __real_read(int descriptor, void *buffer, size_t size); +ssize_t __wrap_read(int descriptor, void *buffer, size_t size) +{ + if (blocked_reader) { + entered = 1; + while (!release_read) { + struct timespec delay = {0, 1000000}; + nanosleep(&delay, NULL); + } + } + return __real_read(descriptor, buffer, size); +} +recomp_func_t recomp_lookup(uint32_t address) { (void)address; abort(); } +recomp_func_t recomp_lookup_import(uint32_t address) { return recomp_lookup(address); } + +static uint32_t call(const char *name, unsigned count, const uint32_t *arguments) +{ + uint32_t stack = g_esp; + if (count) memcpy((void *)ADDR(stack + 4), arguments, count * 4); + recomp_func_t function = guest_stdio_lookup(name); + assert(function); + function(); + assert(g_esp == stack + 4); + g_esp = stack; + return g_eax; +} + +static void *read_blocked_file(void *argument) +{ + g_fs_base = 0x50020000; + g_esp = 0x5003ff00; + blocked_reader = 1; + uint32_t arguments[] = {0x50028000, 1, 1, (uintptr_t)argument}; + assert(call("fread", 4, arguments) == 1); + assert(MEM8(0x50028000) == 'A'); + return NULL; +} + +int main(void) +{ + assert(guest_map("concurrent stdio threads", 0x50000000, 0x40000)); + g_fs_base = 0x50001000; + g_esp = 0x5000ff00; + char directory[] = "/tmp/ao-stdio-concurrency-XXXXXX"; + assert(mkdtemp(directory) && !chdir(directory)); + FILE *input = fopen("first.bin", "wb"); + assert(input && fwrite("AB", 1, 2, input) == 2 && !fclose(input)); + input = fopen("second.bin", "wb"); + assert(input && fwrite("CD", 1, 2, input) == 2 && !fclose(input)); + strcpy((void *)ADDR(0x50004000), "first.bin"); + strcpy((void *)ADDR(0x50004100), "rb"); + strcpy((void *)ADDR(0x50004200), "output.txt"); + strcpy((void *)ADDR(0x50004300), "w"); + strcpy((void *)ADDR(0x50004400), "second.bin"); + uint32_t first = call("fopen", 2, (uint32_t[]){0x50004000, 0x50004100}); + assert(first); + pthread_t reader; + assert(!pthread_create(&reader, NULL, read_blocked_file, (void *)(uintptr_t)first)); + for (unsigned i = 0; !entered && i < 5000; ++i) { + struct timespec delay = {0, 1000000}; + nanosleep(&delay, NULL); + } + assert(entered); + + uint32_t second = call("fopen", 2, (uint32_t[]){0x50004400, 0x50004100}); + assert(second && second != first); + uint32_t read_count = call("fread", 4, (uint32_t[]){0x50005000, 1, 2, second}); + uint32_t position = call("ftell", 1, &second); + uint32_t stat_result = call("_stat", 2, (uint32_t[]){0x50004400, 0x50005200}); + uint32_t reopened = call("freopen", 3, (uint32_t[]){0x50004400, 0x50004100, second}); + uint32_t output = call("fopen", 2, (uint32_t[]){0x50004200, 0x50004300}); + assert(output); + assert(call("fputc", 2, (uint32_t[]){'X', output}) == 'X'); + uint32_t flush_result = call("fflush", 1, (uint32_t[]){0}); + assert(!call("fclose", 1, &output)); + + assert(entered && !release_read); + assert(read_count == 2 && position == 2 && !stat_result && MEM32(0x50005214) == 2); + assert(MEM8(0x50005000) == 'C' && MEM8(0x50005001) == 'D'); + assert(reopened == second && !call("ftell", 1, &second) && !flush_result); + input = fopen("output.txt", "rb"); + assert(input && fgetc(input) == 'X' && fgetc(input) == EOF && !fclose(input)); + release_read = 1; + assert(!pthread_join(reader, NULL)); + assert(!call("fclose", 1, &first) && !call("fclose", 1, &second)); + assert(!remove("first.bin") && !remove("second.bin") && !remove("output.txt")); + assert(!chdir("/tmp") && !rmdir(directory)); + puts("stdio concurrency: blocked input leaves other opens, reads, stat, reopen, output and flush-all responsive"); + return 0; +}