aorecomp

Anarchy Online classic in the browser

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


commit 0b4d79b6b4c1caabd5ff0d7865aa3456b66cc167
parent e786b3802e7cccd397e7bc02076c020d6be1634a
Author: anon <anon@example.com>
Date:   Thu, 15 Oct 2026 11:57:10 +0000

Decode native music streams and support legacy floating-point parsing

Diffstat:
Mdecompiled/Makefile.linux | 12+++++++++---
Mdecompiled/runtime/guest_crt.c | 30++++++++++++++++++++++++++++++
Mdecompiled/runtime/linux_audio.c | 187++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
Adecompiled/tests/atof.c | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mdecompiled/tests/audio.c | 110+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atools/reconstruction/test_portable_atof.py | 22++++++++++++++++++++++
6 files changed, 414 insertions(+), 13 deletions(-)

diff --git a/decompiled/Makefile.linux b/decompiled/Makefile.linux @@ -12,7 +12,7 @@ OBJECTS := $(patsubst %.c,build/linux/%.o,$(GAME_SOURCES) $(DATA_SOURCES) $(RUNT all: build/linux/game build/linux/game: $(OBJECTS) - $(CC) $(OBJECTS) -lm -pthread -lSDL2 -lGL $(shell pkg-config --libs freetype2 fontconfig) -o $@ + $(CC) $(OBJECTS) -lm -pthread -lSDL2 -lsndfile -lGL $(shell pkg-config --libs freetype2 fontconfig) -o $@ objects: $(OBJECTS) @@ -140,7 +140,7 @@ build/linux/guest-stat: tests/guest_stat.c runtime/guest_stdio.c runtime/guest_c build/linux/dynamic-imports: tests/dynamic_imports.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_sync.c runtime/linux_file.c runtime/linux_tls.c runtime/linux_thread.c runtime/linux_snapshot.c runtime/linux_network.c runtime/guest_crt.c runtime/guest_stdio.c runtime/guest_format.c runtime/guest_exception.c runtime/guest_rtti.c runtime/guest_msvcp.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c $(wildcard runtime/*.h) mkdir -p $(@D) - $(CC) $(CPPFLAGS) $(CFLAGS) $(shell pkg-config --cflags freetype2 fontconfig) $(filter %.c,$^) -pthread -lm -lSDL2 -lGL $(shell pkg-config --libs freetype2 fontconfig) -o $@ + $(CC) $(CPPFLAGS) $(CFLAGS) $(shell pkg-config --cflags freetype2 fontconfig) $(filter %.c,$^) -pthread -lm -lSDL2 -lsndfile -lGL $(shell pkg-config --libs freetype2 fontconfig) -o $@ build/linux/create-directory: tests/create_directory.c runtime/linux_platform.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/linux_platform.h runtime/guest_memory.h mkdir -p $(@D) @@ -200,6 +200,7 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric ./build/linux/locale ./build/linux/splitpath ./build/linux/atoi + ./build/linux/atof ./build/linux/graphics ./build/linux/window ./build/linux/surfaces @@ -274,7 +275,7 @@ build/linux/fonts: tests/fonts.c runtime/linux_gdi.c runtime/guest_memory.c runt test: build/linux/fonts build/linux/audio: tests/audio.c runtime/linux_audio.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c runtime/linux_audio.h runtime/guest_heap.h runtime/guest_memory.h runtime/recomp_types.h - $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -pthread -lSDL2 -o $@ + $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -pthread -lSDL2 -lsndfile -lm -Wl,--wrap=SDL_OpenAudioDevice -o $@ test: build/linux/audio @@ -297,3 +298,8 @@ build/linux/splitpath: tests/splitpath.c runtime/guest_crt.c runtime/guest_heap. $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@ test: build/linux/splitpath + +build/linux/atof: tests/atof.c runtime/guest_crt.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_crt.h runtime/guest_heap.h runtime/guest_memory.h + $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@ + +test: build/linux/atof diff --git a/decompiled/runtime/guest_crt.c b/decompiled/runtime/guest_crt.c @@ -425,6 +425,35 @@ static void crt_floor(void) g_esp += 4; } +static void crt_atof(void) +{ + const char *input = (const char *)ADDR(MEM32(g_esp + 4)); + while (*input == ' ' || (*input >= '\t' && *input <= '\r')) ++input; + const char *end = input, *exponent = NULL; + if (*end == '+' || *end == '-') ++end; + while (*end >= '0' && *end <= '9') ++end; + if (*end == '.') { + ++end; + while (*end >= '0' && *end <= '9') ++end; + } + if (*end == 'e' || *end == 'E' || *end == 'd' || *end == 'D') { + exponent = end++; + if (*end == '+' || *end == '-') ++end; + while (*end >= '0' && *end <= '9') ++end; + } + /* MSVCRT accepts decimal/D exponents, rejects hex/inf/nan, and preserves errno. */ + size_t length = end - input; + char *decimal = malloc(length + 1); + if (!decimal) abort(); + memcpy(decimal, input, length); + decimal[length] = 0; + if (exponent) decimal[exponent - input] = 'e'; + double value = strtod(decimal, NULL); + free(decimal); + fp_push_impl(g_st, &g_fp_top, value); + g_esp += 4; +} + static void crt_eh_prolog(void) { /* Match runtime/native32.c:native32_shim_EH_prolog's guest stack layout. */ @@ -837,6 +866,7 @@ recomp_func_t guest_crt_lookup(const char *name) {"??2@YAPAXI@Z", crt_new}, {"??3@YAXPAX@Z", crt_free}, {"_ftol", crt_ftol}, {"floor", crt_floor}, + {"atof", crt_atof}, {"_EH_prolog", crt_eh_prolog}, {"strlen", crt_strlen}, {"strcmp", crt_strcmp}, {"strncmp", crt_strncmp}, diff --git a/decompiled/runtime/linux_audio.c b/decompiled/runtime/linux_audio.c @@ -3,19 +3,74 @@ #include <SDL2/SDL.h> #include <pthread.h> #include <strings.h> +#include <sndfile.h> +#include <limits.h> +#include <math.h> static pthread_once_t audio_once = PTHREAD_ONCE_INIT; static int audio_available; static pthread_mutex_t audio_mutex = PTHREAD_MUTEX_INITIALIZER; static int miles_started, sample_capacity = 64; static uint32_t redist_directory, next_driver = 0xf6000000; +static uint32_t next_stream = 0xf6800000; +struct audio_stream { + uint32_t handle, frames, position; + int status, volume; + float *samples; + struct audio_stream *next; +}; static struct audio_driver { struct audio_driver *next; uint32_t handle; SDL_AudioDeviceID device; int sample_capacity, volume, latency; + SDL_AudioSpec format; + struct audio_stream *streams; } *drivers; +/* SDL holds its device lock during this callback; it never enters guest code. */ +static void render_audio(void *userdata, Uint8 *output, int length) +{ + struct audio_driver *driver = userdata; + int bytes = SDL_AUDIO_BITSIZE(driver->format.format) / 8; + int frames = length / (bytes * driver->format.channels); + for (int frame = 0; frame < frames; ++frame) { + 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) + sample += stream->samples[stream->position * driver->format.channels + channel] * + (stream->volume / 127.0f) * (driver->volume / 127.0f); + if (sample < -1) sample = -1; + if (sample > 1) sample = 1; + int index = frame * driver->format.channels + channel; + if (bytes == 2) { + int value = lrintf(sample * 32768); + if (value > 32767) value = 32767; + output[index * 2] = value; + output[index * 2 + 1] = (unsigned)value >> 8; + } else { + int value = lrintf(sample * 128) + 128; + output[index] = value > 255 ? 255 : value; + } + } + for (struct audio_stream *stream = driver->streams; stream; stream = stream->next) + if (stream->status == 4 && ++stream->position == stream->frames) stream->status = 2; + } +} + +static void destroy_driver_locked(struct audio_driver *driver) +{ + SDL_CloseAudioDevice(driver->device); + while (driver->streams) { + struct audio_stream *stream = driver->streams; + driver->streams = stream->next; + free(stream->samples); + free(stream); + } + free(driver); +} + static void initialize_audio(void) { audio_available = SDL_InitSubSystem(SDL_INIT_AUDIO) == 0; @@ -98,29 +153,33 @@ static void wave_out_open(void) if (!audio_available) g_eax = 6; /* MMSYSERR_NODRIVER */ else if (index != UINT32_MAX && index >= (unsigned)SDL_GetNumAudioDevices(0)) g_eax = 2; else { + struct audio_driver *driver = calloc(1, sizeof(*driver)); + if (!driver) abort(); SDL_AudioSpec desired = {0}; desired.freq = MEM32(format + 4); desired.channels = channels; desired.format = bits == 16 ? AUDIO_S16LSB : AUDIO_U8; desired.samples = 512; - SDL_AudioSpec obtained; + desired.callback = render_audio; + desired.userdata = driver; SDL_AudioDeviceID device = SDL_OpenAudioDevice(index == UINT32_MAX ? NULL : - SDL_GetAudioDeviceName(index, 0), 0, &desired, &obtained, 0); + SDL_GetAudioDeviceName(index, 0), 0, &desired, &driver->format, 0); if (device) { - struct audio_driver *driver = calloc(1, sizeof(*driver)); - if (!driver) abort(); driver->handle = next_driver++; driver->device = device; driver->sample_capacity = sample_capacity; driver->volume = 127; - driver->latency = (unsigned)obtained.samples * 1000 / obtained.freq; + driver->latency = (unsigned)driver->format.samples * 1000 / driver->format.freq; driver->next = drivers; drivers = driver; MEM32(output) = driver->handle; g_eax = 0; fprintf(stderr, "[linux-audio] %s: %d Hz, %u channels, %u bits\n", SDL_GetCurrentAudioDriver(), desired.freq, channels, bits); - } else fprintf(stderr, "[linux-audio] open: %s\n", SDL_GetError()); + } else { + fprintf(stderr, "[linux-audio] open: %s\n", SDL_GetError()); + free(driver); + } } if (pthread_mutex_unlock(&audio_mutex)) abort(); g_esp += 20; @@ -134,9 +193,8 @@ static void wave_out_close(void) while (*driver && (*driver)->handle != handle) driver = &(*driver)->next; if (!*driver) abort(); struct audio_driver *closed = *driver; - SDL_CloseAudioDevice(closed->device); *driver = closed->next; - free(closed); + destroy_driver_locked(closed); if (pthread_mutex_unlock(&audio_mutex)) abort(); g_esp += 8; } @@ -148,7 +206,9 @@ static void set_digital_master_volume(void) struct audio_driver *driver = drivers; while (driver && driver->handle != handle) driver = driver->next; if (!driver) abort(); + SDL_LockAudioDevice(driver->device); driver->volume = (int32_t)MEM32(g_esp + 8); + SDL_UnlockAudioDevice(driver->device); if (pthread_mutex_unlock(&audio_mutex)) abort(); g_esp += 12; } @@ -158,9 +218,8 @@ static void miles_shutdown(void) if (pthread_mutex_lock(&audio_mutex)) abort(); while (drivers) { struct audio_driver *closed = drivers; - SDL_CloseAudioDevice(closed->device); drivers = closed->next; - free(closed); + destroy_driver_locked(closed); } miles_started = 0; if (pthread_mutex_unlock(&audio_mutex)) abort(); @@ -183,6 +242,107 @@ static void digital_latency(void) g_esp += 8; } +static void open_stream(void) +{ + uint32_t handle = MEM32(g_esp + 4), filename = MEM32(g_esp + 8), memory = MEM32(g_esp + 12); + g_esp += 16; + g_eax = 0; + if (!handle || !filename) return; + if (memory) { fprintf(stderr, "Explicit Miles stream memory limits are not implemented\n"); abort(); } + if (pthread_mutex_lock(&audio_mutex)) abort(); + struct audio_driver *driver = drivers; + while (driver && driver->handle != handle) driver = driver->next; + if (!driver) abort(); + + /* Decode the file once; SDL converts its channels/rate into driver-format floats. */ + char *path = strdup((const char *)ADDR(filename)); + if (!path) abort(); + for (char *p = path; *p; ++p) if (*p == '\\') *p = '/'; + SF_INFO info = {0}; + SNDFILE *file = sf_open(path, SFM_READ, &info); + if (file) { + if (info.frames <= 0 || info.channels < 1 || info.channels > 2 || + info.frames > INT_MAX / (int)sizeof(float) / info.channels) abort(); + float *decoded = malloc(info.frames * info.channels * sizeof(float)); + if (!decoded) abort(); + sf_count_t frames = sf_readf_float(file, decoded, info.frames); + if (frames <= 0 || sf_error(file)) abort(); + if (sf_close(file)) abort(); + SDL_AudioStream *conversion = SDL_NewAudioStream(AUDIO_F32SYS, info.channels, info.samplerate, + AUDIO_F32SYS, driver->format.channels, driver->format.freq); + if (!conversion || SDL_AudioStreamPut(conversion, decoded, frames * info.channels * sizeof(float)) || + SDL_AudioStreamFlush(conversion)) abort(); + free(decoded); + int size = SDL_AudioStreamAvailable(conversion); + if (size <= 0 || size % (sizeof(float) * driver->format.channels)) abort(); + struct audio_stream *stream = calloc(1, sizeof(*stream)); + if (!stream || next_stream >= 0xf7000000u) abort(); + stream->samples = malloc(size); + if (!stream->samples || SDL_AudioStreamGet(conversion, stream->samples, size) != size) abort(); + SDL_FreeAudioStream(conversion); + stream->handle = next_stream++; + stream->frames = size / sizeof(float) / driver->format.channels; + stream->status = 2; + stream->volume = 127; + SDL_LockAudioDevice(driver->device); + stream->next = driver->streams; + driver->streams = stream; + SDL_UnlockAudioDevice(driver->device); + g_eax = stream->handle; + fprintf(stderr, "[linux-audio] decoded %s: %u frames\n", path, stream->frames); + } else fprintf(stderr, "[linux-audio] stream %s: %s\n", path, sf_strerror(NULL)); + free(path); + if (pthread_mutex_unlock(&audio_mutex)) abort(); +} + +enum stream_operation { STREAM_STATUS, STREAM_VOLUME, STREAM_START, STREAM_PAUSE, STREAM_CLOSE, STREAM_POSITION }; + +static void stream_control(enum stream_operation operation, unsigned arguments) +{ + uint32_t handle = MEM32(g_esp + 4), value = arguments > 1 ? MEM32(g_esp + 8) : 0; + uint32_t position = arguments == 3 ? MEM32(g_esp + 12) : 0; + g_esp += 4 + arguments * 4; + g_eax = UINT32_MAX; + if (!handle) return; + if (pthread_mutex_lock(&audio_mutex)) abort(); + struct audio_driver *driver = drivers; + struct audio_stream **link = NULL; + for (; driver; driver = driver->next) { + link = &driver->streams; + while (*link && (*link)->handle != handle) link = &(*link)->next; + if (*link) break; + } + if (!driver) abort(); + SDL_LockAudioDevice(driver->device); + struct audio_stream *stream = *link; + if (operation == STREAM_STATUS) g_eax = stream->status; + else if (operation == STREAM_POSITION) { + if (value) MEM32(value) = llrint((double)stream->frames * 1000 / driver->format.freq); + if (position) MEM32(position) = llrint((double)stream->position * 1000 / driver->format.freq); + } + else if (operation == STREAM_VOLUME) stream->volume = (int32_t)value; + else if (operation == STREAM_START) { stream->position = 0; stream->status = 4; } + else if (operation == STREAM_PAUSE) { + if (value) { if (stream->status == 4) stream->status = 8; } + else if (stream->position < stream->frames) stream->status = 4; + } else if (operation == STREAM_CLOSE) { + *link = stream->next; + free(stream->samples); + free(stream); + } else abort(); + SDL_UnlockAudioDevice(driver->device); + if (operation == STREAM_START || (operation == STREAM_PAUSE && !value)) + SDL_PauseAudioDevice(driver->device, 0); + if (pthread_mutex_unlock(&audio_mutex)) abort(); +} + +static void start_stream(void) { stream_control(STREAM_START, 1); } +static void pause_stream(void) { stream_control(STREAM_PAUSE, 2); } +static void close_stream(void) { stream_control(STREAM_CLOSE, 1); } +static void stream_status(void) { stream_control(STREAM_STATUS, 1); } +static void set_stream_volume(void) { stream_control(STREAM_VOLUME, 2); } +static void stream_ms_position(void) { stream_control(STREAM_POSITION, 3); } + recomp_func_t linux_audio_lookup(const char *dll, const char *name) { if (!strcasecmp(dll, "winmm.dll") && !strcmp(name, "waveOutGetNumDevs")) @@ -197,6 +357,13 @@ recomp_func_t linux_audio_lookup(const char *dll, const char *name) {"_AIL_set_digital_master_volume@8", set_digital_master_volume}, {"_AIL_shutdown@0", miles_shutdown}, {"_AIL_digital_latency@4", digital_latency}, + {"_AIL_open_stream@12", open_stream}, + {"_AIL_start_stream@4", start_stream}, + {"_AIL_pause_stream@8", pause_stream}, + {"_AIL_close_stream@4", close_stream}, + {"_AIL_stream_status@4", stream_status}, + {"_AIL_set_stream_volume@8", set_stream_volume}, + {"_AIL_stream_ms_position@12", stream_ms_position}, }; for (unsigned i = 0; i < sizeof(functions) / sizeof(functions[0]); ++i) if (!strcmp(name, functions[i].name)) return functions[i].function; diff --git a/decompiled/tests/atof.c b/decompiled/tests/atof.c @@ -0,0 +1,66 @@ +#include <assert.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#ifdef _WIN32 +#include <windows.h> +#else +#include "guest_crt.h" +#include "guest_memory.h" +recomp_func_t recomp_lookup(uint32_t address) { abort(); } +recomp_func_t recomp_lookup_import(uint32_t address) { abort(); } +#endif + +int main(void) +{ + const char *cases[] = {"", "0", "-0", "1", "-2.5", " 3.75tail", ".5", "+.5", + "1e3", "1e", "1e+", "0x1.8p+1", "nan", "inf", "infinity", "1.#INF", "1.#QNAN", + "1e309", "-1e309", "1e-400", "1e-308", "2.2250738585072014e-308", + "4.9406564584124654e-324", "1d2", "-", "\v\f\r\n\t42", "0.1", "1.2345678901234567"}; + const uint64_t expected[] = {0, 0, 0x8000000000000000ull, 0x3ff0000000000000ull, + 0xc004000000000000ull, 0x400e000000000000ull, 0x3fe0000000000000ull, 0x3fe0000000000000ull, + 0x408f400000000000ull, 0x3ff0000000000000ull, 0x3ff0000000000000ull, 0, 0, 0, 0, + 0x3ff0000000000000ull, 0x3ff0000000000000ull, 0x7ff0000000000000ull, 0xfff0000000000000ull, + 0, 0x000730d67819e8d2ull, 0x0010000000000000ull, 1, 0x4059000000000000ull, 0, + 0x4045000000000000ull, 0x3fb999999999999aull, 0x3ff3c0ca428c59fbull}; +#ifdef _WIN32 + HMODULE library = LoadLibraryA("msvcrt.dll"); + double (__cdecl *convert)(const char *) = (void *)GetProcAddress(library, "atof"); + int *(__cdecl *get_errno)(void) = (void *)GetProcAddress(library, "_errno"); + assert(convert && get_errno); +#else + assert(guest_map("atof test", 0x50000000, 0x10000)); + g_fs_base = 0x50000000; +#endif + for (unsigned i = 0; i < sizeof(cases) / sizeof(cases[0]); ++i) { +#ifdef _WIN32 + *get_errno() = 777; +#else + strcpy((char *)ADDR(0x50000100), cases[i]); + MEM32(guest_crt_errno_address()) = 777; + g_esp = 0x5000ff00; + MEM32(g_esp + 4) = 0x50000100; + g_ebx = 123; g_esi = 234; g_edi = 345; g_ebp = 456; + g_fp_top = 1; + g_st[0] = 13.5; +#endif + +#ifdef _WIN32 + double value = convert(cases[i]); + int error = *get_errno(); +#else + guest_crt_lookup("atof")(); + double value = g_st[0]; + int error = MEM32(guest_crt_errno_address()); +#endif + +#ifndef _WIN32 + assert(g_esp == 0x5000ff04 && g_fp_top == 2 && g_st[1] == 13.5); + assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456); +#endif + uint64_t bits; + memcpy(&bits, &value, sizeof(bits)); + assert(bits == expected[i] && error == 777); + } + puts("atof: 28 decimal, exponent, overflow, subnormal, signed zero and rejected-format cases; errno and x87 ABI passed"); +} diff --git a/decompiled/tests/audio.c b/decompiled/tests/audio.c @@ -12,7 +12,38 @@ static HMODULE miles; #include "linux_audio.h" #include "guest_memory.h" #include <SDL2/SDL.h> +#include <sndfile.h> +#include <stdatomic.h> +#include <unistd.h> #define POINTER(p) ((void *)ADDR(p)) +static SDL_AudioCallback device_callback; +static void *device_userdata; +static _Atomic int capture, nonzero_samples, bad_samples; + +/* Capture actual output from the public playback path, including SDL scheduling. */ +static void capture_audio(void *userdata, Uint8 *output, int length) +{ + device_callback(device_userdata, output, length); + if (!capture) return; + for (int i = 0; i < length; i += 2) { + int16_t sample; + memcpy(&sample, output + i, 2); + if (sample) ++nonzero_samples; + if (sample && sample != 8192) ++bad_samples; + } +} + +SDL_AudioDeviceID __real_SDL_OpenAudioDevice(const char *, int, const SDL_AudioSpec *, SDL_AudioSpec *, int); +SDL_AudioDeviceID __wrap_SDL_OpenAudioDevice(const char *name, int input, const SDL_AudioSpec *desired, + SDL_AudioSpec *obtained, int changes) +{ + SDL_AudioSpec spec = *desired; + device_callback = spec.callback; + device_userdata = spec.userdata; + spec.callback = capture_audio; + spec.userdata = NULL; + return __real_SDL_OpenAudioDevice(name, input, &spec, obtained, changes); +} #endif static uint32_t call(const char *name, unsigned count, const uint32_t *args) @@ -24,6 +55,7 @@ static uint32_t call(const char *name, unsigned count, const uint32_t *args) case 0: return ((uint32_t (__stdcall *)(void))function)(); case 1: return ((uint32_t (__stdcall *)(uint32_t))function)(args[0]); case 2: return ((uint32_t (__stdcall *)(uint32_t, uint32_t))function)(args[0], args[1]); + case 3: return ((uint32_t (__stdcall *)(uint32_t, uint32_t, uint32_t))function)(args[0], args[1], args[2]); case 4: return ((uint32_t (__stdcall *)(uint32_t, uint32_t, uint32_t, uint32_t))function)(args[0], args[1], args[2], args[3]); default: abort(); } @@ -115,6 +147,76 @@ static void rejected_formats(uint32_t scratch) } } +#ifndef _WIN32 +static void stream_playback(uint32_t scratch) +{ + char path[] = "/tmp/ao-audio-XXXXXX"; + int descriptor = mkstemp(path); + assert(descriptor >= 0); + SF_INFO info = {.samplerate = 44100, .channels = 1, .format = SF_FORMAT_WAV | SF_FORMAT_PCM_16}; + SNDFILE *file = sf_open_fd(descriptor, SFM_WRITE, &info, 1); + assert(file); + float samples[4410]; + for (unsigned i = 0; i < 4410; ++i) samples[i] = 0.25f; + assert(sf_writef_float(file, samples, 4410) == 4410 && !sf_close(file)); + uint16_t format[] = {1, 2, 44100, 0, 45328, 2, 4, 16}; + memcpy(POINTER(scratch + 16), format, sizeof(format)); + uint32_t args[] = {scratch, 0, UINT32_MAX, scratch + 16}; + assert(!call("_AIL_waveOutOpen@16", 4, args)); + uint32_t driver = MEM32(scratch); + strcpy(POINTER(scratch + 64), path); + uint32_t open[] = {driver, scratch + 64, 0}; + + uint32_t stream = call("_AIL_open_stream@12", 3, open); + assert(stream && call("_AIL_stream_status@4", 1, &stream) == 2); + memset(POINTER(scratch + 128), 0xa5, 16); + uint32_t position[] = {stream, scratch + 132, scratch + 136}; + call("_AIL_stream_ms_position@12", 3, position); + assert(MEM32(scratch + 132) == 100 && MEM32(scratch + 136) == 0); + capture = 1; + call("_AIL_start_stream@4", 1, &stream); + assert(call("_AIL_stream_status@4", 1, &stream) == 4); + for (unsigned i = 0; call("_AIL_stream_status@4", 1, &stream) == 4 && i < 1000; ++i) SDL_Delay(1); + + assert(call("_AIL_stream_status@4", 1, &stream) == 2); + assert(nonzero_samples == 8820 && !bad_samples); + call("_AIL_stream_ms_position@12", 3, position); + assert(MEM32(scratch + 132) == 100 && MEM32(scratch + 136) == 100); + assert(MEM32(scratch + 128) == 0xa5a5a5a5 && MEM32(scratch + 140) == 0xa5a5a5a5); + capture = 0; + + /* Pause retains the current position; unpausing completes the remaining audio. */ + call("_AIL_start_stream@4", 1, &stream); + uint32_t pause[] = {stream, 1}; + call("_AIL_pause_stream@8", 2, pause); + call("_AIL_stream_ms_position@12", 3, position); + uint32_t paused_position = MEM32(scratch + 136); + SDL_Delay(30); + assert(call("_AIL_stream_status@4", 1, &stream) == 8); + call("_AIL_stream_ms_position@12", 3, position); + assert(MEM32(scratch + 136) == paused_position); + pause[1] = 0; + call("_AIL_pause_stream@8", 2, pause); + for (unsigned i = 0; call("_AIL_stream_status@4", 1, &stream) == 4 && i < 1000; ++i) SDL_Delay(1); + assert(call("_AIL_stream_status@4", 1, &stream) == 2); + + /* A muted stream still advances to completion while producing only silence. */ + uint32_t volume[] = {stream, 0}; + call("_AIL_set_stream_volume@8", 2, volume); + nonzero_samples = bad_samples = 0; + capture = 1; + call("_AIL_start_stream@4", 1, &stream); + for (unsigned i = 0; call("_AIL_stream_status@4", 1, &stream) == 4 && i < 1000; ++i) SDL_Delay(1); + assert(call("_AIL_stream_status@4", 1, &stream) == 2); + assert(!nonzero_samples && !bad_samples); + capture = 0; + call("_AIL_close_stream@4", 1, &stream); + call("_AIL_waveOutClose@4", 1, &driver); + assert(!unlink(path)); + fprintf(stderr, "Linux SDL: 8820 PCM samples verified, pause/resume, restart, mute and stream cleanup passed.\n"); +} +#endif + int main(int argc, char **argv) { #ifdef _WIN32 @@ -137,6 +239,13 @@ int main(int argc, char **argv) assert(call("_AIL_startup@0", 0, NULL) == 0); uint32_t null_driver = 0; assert(call("_AIL_digital_latency@4", 1, &null_driver) == 0); + uint32_t null_stream[] = {0, 0, 0}; + assert(!call("_AIL_open_stream@12", 3, null_stream)); + assert(call("_AIL_stream_status@4", 1, null_stream) == UINT32_MAX); + memset(POINTER(address + 128), 0xa5, 16); + uint32_t null_position[] = {0, address + 132, address + 136}; + call("_AIL_stream_ms_position@12", 3, null_position); + for (unsigned i = 128; i < 144; ++i) assert(((unsigned char *)POINTER(address))[i] == 0xa5); uint32_t preference[] = {1, 16}; assert(call("_AIL_set_preference@8", 2, preference) == 64); preference[1] = 32; @@ -150,6 +259,7 @@ int main(int argc, char **argv) } else driver_lifetime(address); #else driver_lifetime(address); + stream_playback(address); fprintf(stderr, "Linux SDL: device enumeration and 4 PCM format open/close/shutdown cases passed.\n"); #endif assert(call("_AIL_set_preference@8", 2, preference) == 64); diff --git a/tools/reconstruction/test_portable_atof.py b/tools/reconstruction/test_portable_atof.py @@ -0,0 +1,22 @@ +"""Compare guest floating-point conversion with Windows MSVCRT.""" +import pathlib +import subprocess + + +root = pathlib.Path(__file__).resolve().parents[2] +decompiled = root / "decompiled" +work = root / "tools/reconstruction/work/portable-atof" +work.mkdir(parents=True, exist_ok=True) +reference = work / "reference.exe" +subprocess.run([ + str(root / "tools/rust/llvm-mingw-20261006-ucrt-x86_64/bin/i686-w64-mingw32-clang.exe"), + "-O2", "tests/atof.c", "-o", str(reference), +], cwd=decompiled, check=True) +expected = subprocess.check_output([str(reference)], text=True, timeout=30) +linux_path = "/mnt/" + decompiled.drive[0].lower() + decompiled.as_posix()[2:] +wsl = ["wsl", "-d", "Ubuntu-26.04", "--cd", linux_path, "--exec"] +subprocess.run(wsl + ["make", "-s", "-f", "Makefile.linux", "build/linux/atof"], check=True) +actual = subprocess.check_output(wsl + ["./build/linux/atof"], text=True, timeout=30) +assert actual == expected, (expected, actual) +print("Windows == Linux contract: " + actual.strip()) +