aorecomp

Anarchy Online classic in the browser

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


commit c20a9b68fa244d7a6bb82cfa7ab1e8e7c705cec3
parent be53db09c6385b87cca1f04b691565a00a779c75
Author: anon <anon@example.com>
Date:   Thu, 15 Oct 2026 09:04:38 +0000

Support portable decoder longjmp and native audio initialization

Diffstat:
Mdecompiled/Makefile.linux | 20++++++++++++++++++--
Mdecompiled/runtime/eh32.c | 89+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Adecompiled/runtime/guest_jump.h | 30++++++++++++++++++++++++++++++
Adecompiled/runtime/linux_audio.c | 185+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Adecompiled/runtime/linux_audio.h | 5+++++
Mdecompiled/runtime/linux_imports.c | 5+++++
Mdecompiled/src/bbr.dll/lifted-0000.c | 4+++-
Mdecompiled/src/bbr.dll/lifted-0044.c | 4+++-
Mdecompiled/src/kr.dll/lifted-0000.c | 16+++++++++++-----
Mdecompiled/src/kr.dll/lifted-0001.c | 4+++-
Adecompiled/tests/audio.c | 153+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mdecompiled/tests/dynamic_imports.c | 1+
Adecompiled/tests/guest_jump.c | 285+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atools/reconstruction/test_portable_audio.py | 21+++++++++++++++++++++
Atools/reconstruction/test_portable_jump.py | 22++++++++++++++++++++++
15 files changed, 834 insertions(+), 10 deletions(-)

diff --git a/decompiled/Makefile.linux b/decompiled/Makefile.linux @@ -3,7 +3,7 @@ CPPFLAGS := -Iruntime -Isrc -Idata -DRECOMP_PORTABLE CFLAGS := -O2 -fno-strict-aliasing GAME_SOURCES := $(wildcard src/*/*.c) src/dispatch.c DATA_SOURCES := $(wildcard data/*.c) -RUNTIME_SOURCES := runtime/linux_host.c runtime/linux_imports.c runtime/linux_gdi.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_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 VENDOR_SOURCES := $(wildcard vendor/msvcp60/source/*.c) $(wildcard vendor/ace/source/*.c) OBJECTS := $(patsubst %.c,build/linux/%.o,$(GAME_SOURCES) $(DATA_SOURCES) $(RUNTIME_SOURCES) $(VENDOR_SOURCES)) @@ -54,6 +54,14 @@ build/linux/guest-eh: tests/guest_eh.c tests/guest_eh_lifted.c runtime/guest_mem mkdir -p $(@D) $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -o $@ +build/linux/guest-jump: tests/guest_jump.c runtime/guest_memory.c runtime/recomp_state.c runtime/eh32.c runtime/guest_jump.h runtime/recomp_types.h runtime/guest_memory.h data/game-data.h + $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -pthread -o $@ + +build/linux/guest-jump-local: tests/guest_jump.c runtime/guest_memory.c runtime/recomp_state.c runtime/eh32.c runtime/guest_jump.h runtime/recomp_types.h runtime/guest_memory.h data/game-data.h + $(CC) $(CPPFLAGS) $(CFLAGS) -DRECOMP_LOCAL_REGS $(filter %.c,$^) -pthread -o $@ + +test: build/linux/guest-jump build/linux/guest-jump-local + build/linux/guest-rand: tests/guest_rand.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_memory.h runtime/guest_heap.h mkdir -p $(@D) $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -pthread -lm -o $@ @@ -130,7 +138,7 @@ build/linux/guest-stat: tests/guest_stat.c runtime/guest_stdio.c runtime/guest_c mkdir -p $(@D) $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@ -build/linux/dynamic-imports: tests/dynamic_imports.c runtime/linux_imports.c runtime/linux_gdi.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) +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 $@ @@ -156,6 +164,9 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric ./build/linux/ace-token ./build/linux/msvcp-stdio-data ./build/linux/guest-eh + ./build/linux/guest-jump + ./build/linux/guest-jump-local + ./build/linux/audio ./build/linux/guest-rand ./build/linux/semaphore ./build/linux/guest-scanf @@ -257,3 +268,8 @@ build/linux/fonts: tests/fonts.c runtime/linux_gdi.c runtime/guest_memory.c runt $(CC) $(CPPFLAGS) $(CFLAGS) $(shell pkg-config --cflags freetype2 fontconfig) $(filter %.c,$^) -lm $(shell pkg-config --libs freetype2 fontconfig) -o $@ 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 $@ + +test: build/linux/audio diff --git a/decompiled/runtime/eh32.c b/decompiled/runtime/eh32.c @@ -46,6 +46,7 @@ #include "recomp_types.h" #include "game-data.h" +#include "guest_jump.h" #define EH_MAGIC_VC6 0x19930520u #define EH_MAGIC_VC7 0x19930521u @@ -61,10 +62,54 @@ static EH_THREAD_LOCAL recomp_ehframe_t* t_frames; /* newest first */ static EH_THREAD_LOCAL uint32_t t_cur_obj, t_cur_info; #ifdef RECOMP_PORTABLE +static _Thread_local guest_jump_frame *t_jumps; + +void guest_jump_leave(guest_jump_frame *frame) +{ + for (guest_jump_frame *p = t_jumps; p; p = p->prev) + if (p == frame) { t_jumps = frame->prev; return; } +} + +/* A caught C++ exception also invalidates abandoned setjmp landing pads. */ +static void discard_jumps(uint32_t stack) +{ + while (t_jumps && t_jumps->entry_esp < stack) t_jumps = t_jumps->prev; +} + +void guest_jump_save(guest_jump_frame *frame) +{ + if (MEM32(g_esp + 8)) { + fprintf(stderr, "Unsupported _setjmp3 unwind arguments from %08x\n", g_cur_func); + abort(); + } + guest_jump_leave(frame); + frame->prev = t_jumps; + frame->buffer = MEM32(g_esp + 4); + frame->function = g_cur_func; + t_jumps = frame; + + /* MSVCRT x86 _JUMP_BUFFER. Unused unwind-data words remain untouched. */ + uint32_t b = frame->buffer, registration = MEM32(g_fs_base); + MEM32(b) = g_ebp; + MEM32(b + 4) = g_ebx; + MEM32(b + 8) = g_edi; + MEM32(b + 12) = g_esi; + MEM32(b + 16) = g_esp; + MEM32(b + 20) = MEM32(g_esp); + MEM32(b + 24) = registration; + MEM32(b + 28) = registration == UINT32_MAX ? UINT32_MAX : MEM32(registration + 12); + MEM32(b + 32) = 0x56433230; + MEM32(b + 36) = 0; + g_eax = 0; + g_esp += 4; + g_flag_k = FK_NONE; +} + /* _endthreadex abandons guest frames without running C++ stack destructors. */ void recomp_eh_thread_exit(void) { t_frames = NULL; + t_jumps = NULL; t_cur_obj = t_cur_info = 0; } #endif @@ -125,6 +170,47 @@ static void unwind_to(uint32_t rec, uint32_t fi, int to) { } } +#ifdef RECOMP_PORTABLE +void guest_longjmp(void) +{ + uint32_t b = MEM32(g_esp + 4), value = MEM32(g_esp + 8); + guest_jump_frame *frame = t_jumps; + while (frame && frame->buffer != b) frame = frame->prev; + if (!frame) { + fprintf(stderr, "longjmp has no live landing pad for %08x\n", b); + abort(); + } + + /* Unwind intervening C++ frames using the same guest destructor metadata. */ + uint32_t registration = MEM32(b + 24); + for (uint32_t rec = MEM32(g_fs_base); rec != registration; rec = MEM32(rec)) { + uint32_t fi = rec && rec != UINT32_MAX ? funcinfo(MEM32(rec + 4)) : 0; + if (!fi) { + fprintf(stderr, "Unsupported longjmp SEH frame %08x\n", rec); + abort(); + } + unwind_to(rec, fi, -1); + } + if (registration != UINT32_MAX && MEM32(registration + 12) != MEM32(b + 28)) { + fprintf(stderr, "Unsupported longjmp local SEH unwind at %08x\n", registration); + abort(); + } + MEM32(g_fs_base) = registration; + while (t_frames && t_frames->entry_esp < frame->entry_esp) t_frames = t_frames->prev; + t_jumps = frame; + + g_ebp = MEM32(b); + g_ebx = MEM32(b + 4); + g_edi = MEM32(b + 8); + g_esi = MEM32(b + 12); + g_esp = MEM32(b + 16) + 4; + g_eax = value ? value : 1; + g_cur_func = frame->function; + g_flag_k = FK_NONE; + longjmp(frame->host, 1); +} +#endif + /* The ThrowInfo's CatchableType that a catch of `type` takes, or 0. */ static uint32_t catchable(uint32_t info, uint32_t type) { uint32_t arr = MEM32(info + 12); @@ -197,6 +283,9 @@ static int dispatch(uint32_t obj, uint32_t info) { g_esp = MEM32(ebp - 0x10); g_eax = resume; f->resume = resume; +#ifdef RECOMP_PORTABLE + discard_jumps(f->entry_esp); +#endif longjmp(f->jb, 1); } fprintf(stderr, "[eh32] frame 0x%08X caught, but no lifted body owns it (resume 0x%08X)\n", diff --git a/decompiled/runtime/guest_jump.h b/decompiled/runtime/guest_jump.h @@ -0,0 +1,30 @@ +#ifndef GUEST_JUMP_H +#define GUEST_JUMP_H +#include "recomp_types.h" + +#ifdef RECOMP_PORTABLE +/* The landing pad belongs to the function containing the guest setjmp call. */ +typedef struct guest_jump_frame { + struct guest_jump_frame *prev; + jmp_buf host; + uint32_t buffer, function, entry_esp; +} guest_jump_frame; + +void guest_jump_leave(guest_jump_frame *frame); +void guest_jump_save(guest_jump_frame *frame); +void guest_longjmp(void); + +#define RECOMP_JUMP_SCOPE() \ + guest_jump_frame _jump __attribute__((cleanup(guest_jump_leave))) = {.entry_esp = g_esp} +#define RECOMP_SETJMP(target, resume) do { \ + PUSH32(esp, resume); \ + RECOMP_REGS_OUT(); \ + guest_jump_save(&_jump); \ + if (setjmp(_jump.host)) { _fpu_cmp = 0; _df = 1; _itail_tgt = 0; } \ + RECOMP_REGS_IN(); \ +} while (0) +#else +#define RECOMP_JUMP_SCOPE() +#define RECOMP_SETJMP(target, resume) RECOMP_ICALL(target) +#endif +#endif diff --git a/decompiled/runtime/linux_audio.c b/decompiled/runtime/linux_audio.c @@ -0,0 +1,185 @@ +#include "linux_audio.h" +#include "guest_heap.h" +#include <SDL2/SDL.h> +#include <pthread.h> +#include <strings.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 struct audio_driver { + struct audio_driver *next; + uint32_t handle; + SDL_AudioDeviceID device; + int sample_capacity, volume; +} *drivers; + +static void initialize_audio(void) +{ + audio_available = SDL_InitSubSystem(SDL_INIT_AUDIO) == 0; + if (!audio_available) fprintf(stderr, "[linux-audio] %s\n", SDL_GetError()); +} + +static void wave_out_get_num_devs(void) +{ + if (pthread_once(&audio_once, initialize_audio)) abort(); + int count = audio_available ? SDL_GetNumAudioDevices(0) : 0; + if (count < 0) { + fprintf(stderr, "[linux-audio] enumerate: %s\n", SDL_GetError()); + count = 0; + } + g_eax = count; + g_esp += 4; +} + +static void set_redist_directory(void) +{ + uint32_t path = MEM32(g_esp + 4); + const char *text = path ? (const char *)ADDR(path) : ""; + size_t length = strlen(text); + if (length > 258) abort(); + if (pthread_mutex_lock(&audio_mutex)) abort(); + if (!redist_directory) redist_directory = guest_malloc(260); + if (!redist_directory) abort(); + char *destination = (char *)ADDR(redist_directory); + memmove(destination, text, length + 1); + if (length && destination[length - 1] != '\\' && destination[length - 1] != ':') { + destination[length] = '\\'; + destination[length + 1] = 0; + } + g_eax = redist_directory; + if (pthread_mutex_unlock(&audio_mutex)) abort(); + g_esp += 8; +} + +static void miles_startup(void) +{ + if (pthread_once(&audio_once, initialize_audio)) abort(); + if (pthread_mutex_lock(&audio_mutex)) abort(); + g_eax = !miles_started; + if (!miles_started) sample_capacity = 64; + miles_started = 1; + if (pthread_mutex_unlock(&audio_mutex)) abort(); + g_esp += 4; +} + +static void set_preference(void) +{ + uint32_t preference = MEM32(g_esp + 4); + if (preference != 1) { + fprintf(stderr, "Unsupported Miles preference %u\n", preference); + abort(); + } + if (pthread_mutex_lock(&audio_mutex)) abort(); + g_eax = sample_capacity; + sample_capacity = (int32_t)MEM32(g_esp + 8); + if (pthread_mutex_unlock(&audio_mutex)) abort(); + g_esp += 12; +} + +static void wave_out_open(void) +{ + uint32_t output = MEM32(g_esp + 4), wave = MEM32(g_esp + 8); + uint32_t index = MEM32(g_esp + 12), format = MEM32(g_esp + 16); + if (wave || !format) { + fprintf(stderr, "Unsupported Miles waveOut handle/default-format request\n"); + abort(); + } + unsigned channels = MEM16(format + 2), bits = MEM16(format + 14); + g_eax = 1; /* MMSYSERR_ERROR: Miles rejects non-PCM/unsupported PCM formats. */ + if (MEM16(format) != 1 || channels < 1 || channels > 2 || (bits != 8 && bits != 16)) { + g_esp += 20; + return; + } + if (pthread_once(&audio_once, initialize_audio)) abort(); + if (pthread_mutex_lock(&audio_mutex)) abort(); + if (!audio_available) g_eax = 6; /* MMSYSERR_NODRIVER */ + else if (index != UINT32_MAX && index >= (unsigned)SDL_GetNumAudioDevices(0)) g_eax = 2; + else { + SDL_AudioSpec desired = {0}; + desired.freq = MEM32(format + 4); + desired.channels = channels; + desired.format = bits == 16 ? AUDIO_S16LSB : AUDIO_U8; + desired.samples = 512; + SDL_AudioDeviceID device = SDL_OpenAudioDevice(index == UINT32_MAX ? NULL : + SDL_GetAudioDeviceName(index, 0), 0, &desired, NULL, 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->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()); + } + if (pthread_mutex_unlock(&audio_mutex)) abort(); + g_esp += 20; +} + +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; + SDL_CloseAudioDevice(closed->device); + *driver = closed->next; + free(closed); + if (pthread_mutex_unlock(&audio_mutex)) abort(); + g_esp += 8; +} + +static void set_digital_master_volume(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(); + driver->volume = (int32_t)MEM32(g_esp + 8); + if (pthread_mutex_unlock(&audio_mutex)) abort(); + g_esp += 12; +} + +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); + } + miles_started = 0; + if (pthread_mutex_unlock(&audio_mutex)) abort(); + g_esp += 4; +} + +recomp_func_t linux_audio_lookup(const char *dll, const char *name) +{ + if (!strcasecmp(dll, "winmm.dll") && !strcmp(name, "waveOutGetNumDevs")) + return wave_out_get_num_devs; + if (strcasecmp(dll, "mss32.dll")) return NULL; + static const struct { const char *name; recomp_func_t function; } functions[] = { + {"_AIL_set_redist_directory@4", set_redist_directory}, + {"_AIL_startup@0", miles_startup}, + {"_AIL_set_preference@8", set_preference}, + {"_AIL_waveOutOpen@16", wave_out_open}, + {"_AIL_waveOutClose@4", wave_out_close}, + {"_AIL_set_digital_master_volume@8", set_digital_master_volume}, + {"_AIL_shutdown@0", miles_shutdown}, + }; + for (unsigned i = 0; i < sizeof(functions) / sizeof(functions[0]); ++i) + if (!strcmp(name, functions[i].name)) return functions[i].function; + return NULL; +} diff --git a/decompiled/runtime/linux_audio.h b/decompiled/runtime/linux_audio.h @@ -0,0 +1,5 @@ +#ifndef LINUX_AUDIO_H +#define LINUX_AUDIO_H +#include "recomp_types.h" +recomp_func_t linux_audio_lookup(const char *dll, const char *name); +#endif diff --git a/decompiled/runtime/linux_imports.c b/decompiled/runtime/linux_imports.c @@ -4,6 +4,7 @@ #include "guest_format.h" #include "guest_exception.h" #include "guest_rtti.h" +#include "guest_jump.h" #include "guest_msvcp.h" #include "linux_platform.h" #include "linux_sync.h" @@ -17,6 +18,7 @@ #include "linux_graphics.h" #include "linux_window.h" #include "linux_gdi.h" +#include "linux_audio.h" #include <strings.h> /* External calls receive stable guest identifiers rather than host code addresses. */ @@ -157,6 +159,8 @@ recomp_func_t recomp_lookup_import(uint32_t va) if (function) return function; } if (imports[current_import].name) { + recomp_func_t audio = linux_audio_lookup(imports[current_import].dll, imports[current_import].name); + if (audio) return audio; if (!strcasecmp(imports[current_import].dll, "gdi32.dll")) { recomp_func_t function = linux_gdi_lookup(imports[current_import].name); if (function) return function; @@ -177,6 +181,7 @@ recomp_func_t recomp_lookup_import(uint32_t va) } } if (!strcasecmp(imports[current_import].dll, "msvcrt.dll") && imports[current_import].name) { + if (!strcmp(imports[current_import].name, "longjmp")) return guest_longjmp; if (!strcmp(imports[current_import].name, "_CxxThrowException")) { extern void native32_shim_CxxThrowException(void); return native32_shim_CxxThrowException; diff --git a/decompiled/src/bbr.dll/lifted-0000.c b/decompiled/src/bbr.dll/lifted-0000.c @@ -1,5 +1,6 @@ #define RECOMP_GENERATED_CODE #include "recomp_types.h" +#include "guest_jump.h" #include <math.h> #include <stdio.h> #include <stdlib.h> @@ -1375,6 +1376,7 @@ void sub_25001830(void) { uint32_t _flag_a = 0, _flag_b = 0; uint32_t _itail_tgt = 0; RECOMP_REGS_LOCALS + RECOMP_JUMP_SCOPE(); RECOMP_ENTER(0x25001830u); L_25001830: @@ -1466,7 +1468,7 @@ L_250018B6: edx = MEM32(ebp + (-0x4)); /* 0x250018B6: mov edx, dword ptr [ebp - 4] */ PUSH32(esp, edi); /* 0x250018B9: push edi */ PUSH32(esp, edx); /* 0x250018BA: push edx */ - RECOMP_ICALL(0x251380D6u); /* 0x250018BB: call 0x251380d6 */ /* not lifted */ + RECOMP_SETJMP(0x251380D6u, 0x250018C0u); /* 0x250018BB: call 0x251380d6 */ /* not lifted */ RECOMP_FLAGS_IN(); _flag_a = (uint32_t)(esp); _flag_b = (uint32_t)(8); _cf = (uint32_t)((uint32_t)(_flag_a + _flag_b) < (uint32_t)_flag_a); diff --git a/decompiled/src/bbr.dll/lifted-0044.c b/decompiled/src/bbr.dll/lifted-0044.c @@ -1,5 +1,6 @@ #define RECOMP_GENERATED_CODE #include "recomp_types.h" +#include "guest_jump.h" #include <math.h> #include <stdio.h> #include <stdlib.h> @@ -125903,6 +125904,7 @@ void sub_250C22E0(void) { uint32_t _flag_a = 0, _flag_b = 0; uint32_t _itail_tgt = 0; RECOMP_REGS_LOCALS + RECOMP_JUMP_SCOPE(); RECOMP_ENTER(0x250C22E0u); L_250C22E0: @@ -125945,7 +125947,7 @@ L_250C22FB: L_250C22FD: PUSH32(esp, eax); /* 0x250C22FD: push eax */ L_250C22FE: - RECOMP_ICALL(0x251380D6u); /* 0x250C22FE: call 0x251380d6 */ /* not lifted */ + RECOMP_SETJMP(0x251380D6u, 0x250C2303u); /* 0x250C22FE: call 0x251380d6 */ /* not lifted */ RECOMP_FLAGS_IN(); L_250C2303: _flag_a = (uint32_t)(esp); _flag_b = (uint32_t)(8); diff --git a/decompiled/src/kr.dll/lifted-0000.c b/decompiled/src/kr.dll/lifted-0000.c @@ -1,5 +1,6 @@ #define RECOMP_GENERATED_CODE #include "recomp_types.h" +#include "guest_jump.h" #include <math.h> #include <stdio.h> #include <stdlib.h> @@ -13,6 +14,7 @@ void sub_37001000(void) { uint32_t _flag_a = 0, _flag_b = 0; uint32_t _itail_tgt = 0; RECOMP_REGS_LOCALS + RECOMP_JUMP_SCOPE(); RECOMP_ENTER(0x37001000u); L_37001000: @@ -67,7 +69,7 @@ L_37001040: MEM32(eax + 0x48) = 0x37014850u; /* 0x37001044: mov dword ptr [eax + 0x48], 0x37014850 */ ecx = MEM32(ebp + (-0x4)); /* 0x3700104B: mov ecx, dword ptr [ebp - 4] */ PUSH32(esp, ecx); /* 0x3700104E: push ecx */ - RECOMP_ICALL(0x3701930Cu); /* 0x3700104F: call 0x3701930c */ /* not lifted */ + RECOMP_SETJMP(0x3701930Cu, 0x37001054u); /* 0x3700104F: call 0x3701930c */ /* not lifted */ RECOMP_FLAGS_IN(); _flag_a = (uint32_t)(esp); _flag_b = (uint32_t)(8); _cf = (uint32_t)((uint32_t)(_flag_a + _flag_b) < (uint32_t)_flag_a); @@ -338,6 +340,7 @@ void sub_370011E0(void) { uint32_t _flag_a = 0, _flag_b = 0; uint32_t _itail_tgt = 0; RECOMP_REGS_LOCALS + RECOMP_JUMP_SCOPE(); RECOMP_ENTER(0x370011E0u); L_370011E0: @@ -426,7 +429,7 @@ L_37001263: eax = MEM32(ebp + (-0x4)); /* 0x37001263: mov eax, dword ptr [ebp - 4] */ PUSH32(esp, ebx); /* 0x37001266: push ebx */ PUSH32(esp, eax); /* 0x37001267: push eax */ - RECOMP_ICALL(0x3701930Cu); /* 0x37001268: call 0x3701930c */ /* not lifted */ + RECOMP_SETJMP(0x3701930Cu, 0x3700126Du); /* 0x37001268: call 0x3701930c */ /* not lifted */ RECOMP_FLAGS_IN(); _flag_a = (uint32_t)(esp); _flag_b = (uint32_t)(8); _cf = (uint32_t)((uint32_t)(_flag_a + _flag_b) < (uint32_t)_flag_a); @@ -4176,6 +4179,7 @@ void sub_370026D0(void) { uint32_t _flag_a = 0, _flag_b = 0; uint32_t _itail_tgt = 0; RECOMP_REGS_LOCALS + RECOMP_JUMP_SCOPE(); RECOMP_ENTER(0x370026D0u); L_370026D0: @@ -4271,7 +4275,7 @@ L_37002759: edx = MEM32(ebp + (-0x4)); /* 0x37002759: mov edx, dword ptr [ebp - 4] */ PUSH32(esp, edi); /* 0x3700275C: push edi */ PUSH32(esp, edx); /* 0x3700275D: push edx */ - RECOMP_ICALL(0x3701930Cu); /* 0x3700275E: call 0x3701930c */ /* not lifted */ + RECOMP_SETJMP(0x3701930Cu, 0x37002763u); /* 0x3700275E: call 0x3701930c */ /* not lifted */ RECOMP_FLAGS_IN(); _flag_a = (uint32_t)(esp); _flag_b = (uint32_t)(8); _cf = (uint32_t)((uint32_t)(_flag_a + _flag_b) < (uint32_t)_flag_a); @@ -4724,6 +4728,7 @@ void sub_370029D0(void) { uint32_t _flag_a = 0, _flag_b = 0; uint32_t _itail_tgt = 0; RECOMP_REGS_LOCALS + RECOMP_JUMP_SCOPE(); RECOMP_ENTER(0x370029D0u); L_370029D0: @@ -4781,7 +4786,7 @@ L_37002A15: eax = MEM32(ebp + (-0x8)); /* 0x37002A15: mov eax, dword ptr [ebp - 8] */ PUSH32(esp, edi); /* 0x37002A18: push edi */ PUSH32(esp, eax); /* 0x37002A19: push eax */ - RECOMP_ICALL(0x3701930Cu); /* 0x37002A1A: call 0x3701930c */ /* not lifted */ + RECOMP_SETJMP(0x3701930Cu, 0x37002A1Fu); /* 0x37002A1A: call 0x3701930c */ /* not lifted */ RECOMP_FLAGS_IN(); _flag_a = (uint32_t)(esp); _flag_b = (uint32_t)(8); _cf = (uint32_t)((uint32_t)(_flag_a + _flag_b) < (uint32_t)_flag_a); @@ -8907,6 +8912,7 @@ void sub_37003E50(void) { uint32_t _flag_a = 0, _flag_b = 0; uint32_t _itail_tgt = 0; RECOMP_REGS_LOCALS + RECOMP_JUMP_SCOPE(); RECOMP_ENTER(0x37003E50u); L_37003E50: @@ -8935,7 +8941,7 @@ L_37003E66: L_37003E6B: PUSH32(esp, 0); /* 0x37003E6B: push 0 */ PUSH32(esp, eax); /* 0x37003E6D: push eax */ - RECOMP_ICALL(0x3701930Cu); /* 0x37003E6E: call 0x3701930c */ /* not lifted */ + RECOMP_SETJMP(0x3701930Cu, 0x37003E73u); /* 0x37003E6E: call 0x3701930c */ /* not lifted */ RECOMP_FLAGS_IN(); _flag_a = (uint32_t)(esp); _flag_b = (uint32_t)(8); _cf = (uint32_t)((uint32_t)(_flag_a + _flag_b) < (uint32_t)_flag_a); diff --git a/decompiled/src/kr.dll/lifted-0001.c b/decompiled/src/kr.dll/lifted-0001.c @@ -1,5 +1,6 @@ #define RECOMP_GENERATED_CODE #include "recomp_types.h" +#include "guest_jump.h" #include <math.h> #include <stdio.h> #include <stdlib.h> @@ -2694,6 +2695,7 @@ void sub_37005870(void) { uint32_t _flag_a = 0, _flag_b = 0; uint32_t _itail_tgt = 0; RECOMP_REGS_LOCALS + RECOMP_JUMP_SCOPE(); RECOMP_ENTER(0x37005870u); L_37005870: @@ -2736,7 +2738,7 @@ L_3700588B: L_3700588D: PUSH32(esp, eax); /* 0x3700588D: push eax */ L_3700588E: - RECOMP_ICALL(0x3701930Cu); /* 0x3700588E: call 0x3701930c */ /* not lifted */ + RECOMP_SETJMP(0x3701930Cu, 0x37005893u); /* 0x3700588E: call 0x3701930c */ /* not lifted */ RECOMP_FLAGS_IN(); L_37005893: _flag_a = (uint32_t)(esp); _flag_b = (uint32_t)(8); diff --git a/decompiled/tests/audio.c b/decompiled/tests/audio.c @@ -0,0 +1,153 @@ +#include <assert.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#ifdef _WIN32 +#define WIN32_LEAN_AND_MEAN +#include <windows.h> +#include <mmsystem.h> +static HMODULE miles; +#define POINTER(p) ((void *)(uintptr_t)(p)) +#else +#include "linux_audio.h" +#include "guest_memory.h" +#include <SDL2/SDL.h> +#define POINTER(p) ((void *)ADDR(p)) +#endif + +static uint32_t call(const char *name, unsigned count, const uint32_t *args) +{ +#ifdef _WIN32 + FARPROC function = GetProcAddress(miles, name); + assert(function); + switch (count) { + 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 4: return ((uint32_t (__stdcall *)(uint32_t, uint32_t, uint32_t, uint32_t))function)(args[0], args[1], args[2], args[3]); + default: abort(); + } +#else + g_esp = 0x5000ff00; + g_ebp = 0x11223344; g_ebx = 0x22334455; + g_esi = 0x33445566; g_edi = 0x44556677; + MEM32(g_esp) = RECOMP_RETADDR; + for (unsigned i = 0; i < count; ++i) MEM32(g_esp + 4 + i * 4) = args[i]; + recomp_func_t function = linux_audio_lookup("mss32.dll", name); + assert(function); + function(); + assert(g_esp == 0x5000ff04 + count * 4); + assert(g_ebp == 0x11223344 && g_ebx == 0x22334455); + assert(g_esi == 0x33445566 && g_edi == 0x44556677); + return g_eax; +#endif +} + +static void redist_paths(uint32_t input) +{ + const char *paths[] = {NULL, "", ".", "C:", "C:\\ao", "C:\\ao\\", "audio/"}; + const char *expected[] = {"", "", ".\\", "C:", "C:\\ao\\", "C:\\ao\\", "audio/\\"}; + uint32_t address = 0; + for (unsigned i = 0; i < 7; ++i) { + if (paths[i]) strcpy(POINTER(input), paths[i]); + uint32_t argument = paths[i] ? input : 0; + + uint32_t result = call("_AIL_set_redist_directory@4", 1, &argument); + + assert(result && !strcmp(POINTER(result), expected[i])); + if (i) assert(result == address); + if (paths[i]) assert(!strcmp(POINTER(input), paths[i])); + address = result; + } +} + +static void driver_lifetime(uint32_t scratch) +{ + for (unsigned i = 0; i < 4; ++i) { + uint8_t *bytes = POINTER(scratch); + uint16_t format[] = {1, i & 1 ? 2 : 1, 0, 0, 0, 0, 0, i & 2 ? 16 : 8}; + uint32_t rate = i & 1 ? 44100 : 22050; + uint32_t bandwidth = rate * format[1] * format[7] / 8; + format[6] = format[1] * format[7] / 8; + memcpy(bytes + 16, format, sizeof(format)); + memcpy(bytes + 20, &rate, 4); + memcpy(bytes + 24, &bandwidth, 4); + memset(bytes, 0xa5, 12); + uint32_t args[] = {scratch + 4, 0, UINT32_MAX, scratch + 16}; + + uint32_t result = call("_AIL_waveOutOpen@16", 4, args); + uint32_t driver; + memcpy(&driver, bytes + 4, 4); + + assert(result == 0 && driver && driver != 0xa5a5a5a5); + assert(bytes[0] == 0xa5 && bytes[3] == 0xa5 && bytes[8] == 0xa5 && bytes[11] == 0xa5); + uint32_t volume[] = {driver, i & 1 ? 127 : 0}; + call("_AIL_set_digital_master_volume@8", 2, volume); + if (i == 3) { + call("_AIL_shutdown@0", 0, NULL); + assert(call("_AIL_startup@0", 0, NULL) == 1); + } else call("_AIL_waveOutClose@4", 1, &driver); + } +} + +static void rejected_formats(uint32_t scratch) +{ + for (unsigned i = 0; i < 3; ++i) { + uint8_t *bytes = POINTER(scratch); + uint16_t format[] = {1, 2, 44100, 0, 45328, 2, 4, 16}; + if (i == 0) format[0] = 2; + if (i == 1) format[1] = 3; + if (i == 2) format[7] = 24; + memcpy(bytes + 16, format, sizeof(format)); + memset(bytes, 0xa5, 12); + uint32_t args[] = {scratch + 4, 0, UINT32_MAX, scratch + 16}; + + uint32_t result = call("_AIL_waveOutOpen@16", 4, args); + + assert(result == 1); + for (unsigned j = 0; j < 12; ++j) assert(bytes[j] == 0xa5); + } +} + +int main(int argc, char **argv) +{ +#ifdef _WIN32 + assert(argc == 2); + miles = LoadLibraryA(argv[1]); + assert(miles); + uint8_t scratch[1024]; + uint32_t address = (uint32_t)(uintptr_t)scratch; + unsigned devices = waveOutGetNumDevs(); +#else + assert(guest_map("audio test", 0x50000000, 0x10000)); + uint32_t address = 0x50000100; + g_esp = 0x5000ff00; + linux_audio_lookup("winmm.dll", "waveOutGetNumDevs")(); + assert(g_eax > 0 && g_eax == (unsigned)SDL_GetNumAudioDevices(0)); + assert(g_esp == 0x5000ff04); +#endif + redist_paths(address); + assert(call("_AIL_startup@0", 0, NULL) == 1); + assert(call("_AIL_startup@0", 0, NULL) == 0); + uint32_t preference[] = {1, 16}; + assert(call("_AIL_set_preference@8", 2, preference) == 64); + preference[1] = 32; + assert(call("_AIL_set_preference@8", 2, preference) == 16); + rejected_formats(address); +#ifdef _WIN32 + if (!devices) { + fprintf(stderr, "Windows reports no wave output device; device opening is not compared.\n"); + call("_AIL_shutdown@0", 0, NULL); + assert(call("_AIL_startup@0", 0, NULL) == 1); + } else driver_lifetime(address); +#else + driver_lifetime(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); + call("_AIL_shutdown@0", 0, NULL); +#ifdef _WIN32 + FreeLibrary(miles); +#endif + puts("audio: 7 directory cases, startup/reset, preferences and invalid PCM formats passed"); +} diff --git a/decompiled/tests/dynamic_imports.c b/decompiled/tests/dynamic_imports.c @@ -23,6 +23,7 @@ uint32_t linux_module_export(uint32_t handle, const char *name, uint32_t ordinal void linux_thread_notify(uint32_t reason) { (void)reason; } void recomp_eh_thread_exit(void) {} void native32_shim_CxxThrowException(void) { abort(); } +void guest_longjmp(void) { abort(); } static uint32_t call(const char *name, uint32_t first, uint32_t second) { diff --git a/decompiled/tests/guest_jump.c b/decompiled/tests/guest_jump.c @@ -0,0 +1,285 @@ +#include <assert.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#ifdef _WIN32 +#define WIN32_LEAN_AND_MEAN +#include <windows.h> +void *jump_set_target, *jump_long_target; +int jump_set(uint32_t *, int) __attribute__((returns_twice)); +__asm__(".text\n.globl _jump_set\n_jump_set:\njmp *_jump_set_target\n"); +static void jump_to(uint32_t *buffer, int value) +{ + ((void (__cdecl *)(uint32_t *, int))jump_long_target)(buffer, value); + abort(); +} +#else +#define RECOMP_GENERATED_CODE +#include "guest_jump.h" +#include "guest_memory.h" +#include <pthread.h> +static _Thread_local unsigned destructed; +static void destroy_local(void) +{ + assert(MEM32(g_ebp - 4) == UINT32_MAX); + ++destructed; + g_esp += 4; +} +uint32_t recomp_funcinfo(uint32_t handler) { return handler == 0x43002000 ? 0x43003000 : 0; } +recomp_func_t recomp_lookup(uint32_t address) { return address == 0x43001100 ? destroy_local : NULL; } +recomp_func_t recomp_lookup_manual(uint32_t address) { return NULL; } +recomp_func_t recomp_lookup_import(uint32_t address) { abort(); } +#endif + +/* The x86 reference deliberately clobbers the saved nonvolatile registers. */ +uint32_t jump_buffer[18], jump_result[6], jump_value; +#ifdef _WIN32 +void register_jump(void); +__asm__( + ".text\n.globl _register_jump\n_register_jump:\n" + "push %ebp\npush %ebx\npush %esi\npush %edi\n" + "mov %esp, _jump_result+20\n" + "mov $0x11223344,%ebp\nmov $0x22334455,%ebx\n" + "mov $0x33445566,%esi\nmov $0x44556677,%edi\n" + "push $0\npush $_jump_buffer+4\ncall *_jump_set_target\nadd $8,%esp\n" + "test %eax,%eax\njnz 1f\n" + "mov $7,%ebp\nmov $8,%ebx\nmov $9,%esi\nmov $10,%edi\n" + "push _jump_value\npush $_jump_buffer+4\ncall *_jump_long_target\nud2\n" + "1:\nmov %eax,_jump_result\nmov %ebp,_jump_result+4\n" + "mov %ebx,_jump_result+8\nmov %esi,_jump_result+12\nmov %edi,_jump_result+16\n" + "sub %esp,_jump_result+20\npop %edi\npop %esi\npop %ebx\npop %ebp\nret\n"); +#else +static void register_jump(void) +{ + int _fpu_cmp = 0, _df = 1; + uint32_t _itail_tgt = 0; + RECOMP_REGS_LOCALS + RECOMP_JUMP_SCOPE(); + ebp = 0x11223344; + ebx = 0x22334455; + esi = 0x33445566; + edi = 0x44556677; + PUSH32(esp, 0); + PUSH32(esp, 0x50000104); + RECOMP_SETJMP(0, 0x42001005); + esp += 8; + if (!eax) { + ebp = 7; ebx = 8; esi = 9; edi = 10; + PUSH32(esp, jump_value); + PUSH32(esp, 0x50000104); + PUSH32(esp, RECOMP_RETADDR); + RECOMP_REGS_OUT(); + guest_longjmp(); + abort(); + } + jump_result[0] = eax; + jump_result[1] = ebp; + jump_result[2] = ebx; + jump_result[3] = esi; + jump_result[4] = edi; + jump_result[5] = 0x5000ff00 - esp; + RECOMP_REGS_OUT(); +} +#endif + +static void registers_and_buffer(void) +{ + uint32_t values[] = {0, 1, 42, UINT32_MAX, 0x80000000}; + for (unsigned i = 0; i < 5; ++i) { + jump_value = values[i]; + memset(jump_buffer, 0xa5, sizeof(jump_buffer)); +#ifndef _WIN32 + memcpy((void *)ADDR(0x50000100), jump_buffer, sizeof(jump_buffer)); + g_esp = 0x5000ff00; +#endif + + register_jump(); +#ifndef _WIN32 + memcpy(jump_buffer, (void *)ADDR(0x50000100), sizeof(jump_buffer)); +#endif + + assert(jump_result[0] == (values[i] ? values[i] : 1)); + assert(jump_result[1] == 0x11223344 && jump_result[2] == 0x22334455); + assert(jump_result[3] == 0x33445566 && jump_result[4] == 0x44556677); + assert(jump_result[5] == 0); + assert(jump_buffer[0] == 0xa5a5a5a5 && jump_buffer[17] == 0xa5a5a5a5); + assert(jump_buffer[1] == 0x11223344 && jump_buffer[2] == 0x22334455); + assert(jump_buffer[3] == 0x44556677 && jump_buffer[4] == 0x33445566); + assert(jump_buffer[9] == 0x56433230 && jump_buffer[10] == 0); + for (unsigned word = 11; word < 17; ++word) assert(jump_buffer[word] == 0xa5a5a5a5); + } +} + +#ifdef _WIN32 +static unsigned nested(unsigned depth, uint32_t *outer, volatile unsigned *written) +{ + uint32_t buffer[16]; + if (!jump_set(buffer, 0)) { + ++*written; + if (depth) nested(depth - 1, outer ? outer : buffer, written); + jump_to(outer ? outer : buffer, 37); + } + return 37; +} +#else +static unsigned nested(unsigned depth, uint32_t outer, volatile unsigned *written) +{ + int _fpu_cmp = 0, _df = 1; + uint32_t _itail_tgt = 0; + RECOMP_REGS_LOCALS + RECOMP_JUMP_SCOPE(); + esp -= 64; + PUSH32(esp, 0); + PUSH32(esp, esp + 4); + RECOMP_SETJMP(0, 0x42002005); + esp += 8; + if (!eax) { + ++*written; + if (depth) { + uint32_t buffer = esp; + RECOMP_REGS_OUT(); + nested(depth - 1, outer ? outer : buffer, written); + RECOMP_REGS_IN(); + } + PUSH32(esp, 37); + PUSH32(esp, outer ? outer : esp + 4); + PUSH32(esp, RECOMP_RETADDR); + RECOMP_REGS_OUT(); + guest_longjmp(); + abort(); + } + unsigned result = eax; + esp += 64; + RECOMP_REGS_OUT(); + return result; +} +#endif + +static void nested_jumps(void) +{ + for (unsigned depth = 0; depth < 16; ++depth) { + volatile unsigned written = 0; +#ifndef _WIN32 + uint32_t stack = g_esp; +#endif + + unsigned result = nested(depth, 0, &written); + + assert(result == 37 && written == depth + 1); +#ifndef _WIN32 + assert(g_esp == stack); +#endif + } +} + +#ifndef _WIN32 +static void jump_across_cpp_frame(uint32_t buffer) +{ + recomp_ehframe_t frame; + frame.entry_esp = g_esp; + recomp_eh_push(&frame); + g_esp -= 16; + MEM32(g_esp) = MEM32(g_fs_base); + MEM32(g_esp + 4) = 0x43002000; + MEM32(g_esp + 8) = 0; + MEM32(g_esp + 12) = g_ebp; + MEM32(g_fs_base) = g_esp; + PUSH32(g_esp, 99); + PUSH32(g_esp, buffer); + PUSH32(g_esp, RECOMP_RETADDR); + guest_longjmp(); + abort(); +} + +static void cpp_unwind(void) +{ + int _fpu_cmp = 0, _df = 1; + uint32_t _itail_tgt = 0; + RECOMP_REGS_LOCALS + RECOMP_JUMP_SCOPE(); + assert(guest_map("jump unwind metadata", 0x43000000, 0x10000)); + MEM32(0x43003000) = 0x19930520; + MEM32(0x43003004) = 1; + MEM32(0x43003008) = 0x43003100; + MEM32(0x43003100) = UINT32_MAX; + MEM32(0x43003104) = 0x43001100; + esp = 0x5000ff00; + + PUSH32(esp, 0); + PUSH32(esp, 0x50000104); + RECOMP_SETJMP(0, 0x42003005); + esp += 8; + if (!eax) { + PUSH32(esp, RECOMP_RETADDR); + RECOMP_REGS_OUT(); + jump_across_cpp_frame(0x50000104); + abort(); + } + + assert(eax == 99 && esp == 0x5000ff00); + assert(destructed == 1 && MEM32(g_fs_base) == UINT32_MAX); + RECOMP_REGS_OUT(); +} +#endif + +#ifdef _WIN32 +static DWORD WINAPI thread_test(void *arg) +{ + for (unsigned i = 0; i < 64; ++i) nested_jumps(); + return 0; +} +#else +static void *thread_test(void *arg) +{ + uint32_t base = (uint32_t)(uintptr_t)arg; + g_esp = base + 0xff00; + g_fs_base = base; + MEM32(base) = UINT32_MAX; + for (unsigned i = 0; i < 64; ++i) nested_jumps(); + return NULL; +} +#endif + +int main(void) +{ +#ifdef _WIN32 + HMODULE crt = LoadLibraryA("msvcrt.dll"); + jump_set_target = (void *)GetProcAddress(crt, "_setjmp3"); + jump_long_target = (void *)GetProcAddress(crt, "longjmp"); +#else + assert(guest_map("jump test", 0x50000000, 0x50000)); + g_fs_base = 0x50000000; + MEM32(g_fs_base) = UINT32_MAX; +#endif + registers_and_buffer(); + nested_jumps(); +#ifndef _WIN32 + cpp_unwind(); + nested_jumps(); +#endif + +#ifdef _WIN32 + HANDLE threads[4]; +#else + pthread_t threads[4]; +#endif + + for (unsigned i = 0; i < 4; ++i) { +#ifdef _WIN32 + threads[i] = CreateThread(NULL, 0, thread_test, NULL, 0, NULL); + assert(threads[i]); +#else + assert(!pthread_create(&threads[i], NULL, thread_test, (void *)(uintptr_t)(0x50010000 + i * 0x10000))); +#endif + } + + for (unsigned i = 0; i < 4; ++i) { +#ifdef _WIN32 + assert(WaitForSingleObject(threads[i], INFINITE) == WAIT_OBJECT_0); + assert(CloseHandle(threads[i])); +#else + assert(!pthread_join(threads[i], NULL)); +#endif + } + puts("setjmp: five return values, x86 buffer/registers, 16 nested scopes, 4 concurrent threads passed"); +} diff --git a/tools/reconstruction/test_portable_audio.py b/tools/reconstruction/test_portable_audio.py @@ -0,0 +1,21 @@ +"""Compare the reached Miles startup contract with the bundled Windows runtime.""" +import pathlib +import subprocess + +root = pathlib.Path(__file__).resolve().parents[2] +decompiled = root / 'decompiled' +work = root / 'tools/reconstruction/work/portable-audio' +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/audio.c', '-lwinmm', '-o', str(reference), +], cwd=decompiled, check=True) +expected = subprocess.check_output([str(reference), + str(root / 'client-standalone-14.9.1/Mss32.dll')], 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/audio'], check=True) +actual = subprocess.check_output(wsl + ['./build/linux/audio'], text=True, timeout=30) +assert actual == expected, (expected, actual) +print('Windows Miles == Linux contract: ' + actual.strip()) diff --git a/tools/reconstruction/test_portable_jump.py b/tools/reconstruction/test_portable_jump.py @@ -0,0 +1,22 @@ +"""Compare MSVCRT x86 nonlocal jumps with the portable generated-C landing pads.""" +import pathlib +import subprocess + +root = pathlib.Path(__file__).resolve().parents[2] +decompiled = root / 'decompiled' +work = root / 'tools/reconstruction/work/portable-jump' +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/guest_jump.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/guest-jump', 'build/linux/guest-jump-local'], check=True) +for executable in ['guest-jump', 'guest-jump-local']: + actual = subprocess.check_output(wsl + ['./build/linux/' + executable], text=True, timeout=30) + assert actual == expected, (expected, actual) +print('Windows MSVCRT == Linux (global and local registers): ' + expected.strip())