diff --git a/.github/workflows/apple-silicon.yml b/.github/workflows/apple-silicon.yml new file mode 100644 index 0000000000..c04b0cbc30 --- /dev/null +++ b/.github/workflows/apple-silicon.yml @@ -0,0 +1,35 @@ +name: Fresh Apple Silicon installer build +on: + workflow_dispatch: + push: + paths: ['port/macos/**', 'port/linux/**', 'source/**', 'tools/**', 'validation/**', '.github/workflows/apple-silicon.yml'] + pull_request: + paths: ['port/macos/**', 'port/linux/**', 'source/**', 'tools/**', 'validation/**', '.github/workflows/apple-silicon.yml'] +permissions: + contents: read +jobs: + build: + runs-on: macos-15 + timeout-minutes: 60 + steps: + - uses: actions/checkout@v4 + - name: Confirm ARM64 and create synthetic disc fixture + env: + FIXTURE_ISO: ${{ runner.temp }}/synthetic-halo.iso + run: | + test "$(uname -m)" = arm64 + python3 - <<'PY' + import importlib.util, os + from pathlib import Path + spec = importlib.util.spec_from_file_location('fixture', 'port/macos/tooling/tests/test_xbox_assets.py') + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + module.fixture(Path(os.environ['FIXTURE_ISO'])) + PY + - name: Run installer without game downloads or game launches + env: + HALO_WORK_DIR: ${{ runner.temp }}/fresh-build + HALO_APP_OUTPUT: ${{ runner.temp }}/Halo Fixture Build.app + run: ./port/macos/setup.sh "$RUNNER_TEMP/synthetic-halo.iso" --jobs 2 + - name: Static native bundle check + run: '"$RUNNER_TEMP/Halo Fixture Build.app/Contents/MacOS/Halo Combat Evolved" --check' diff --git a/README.md b/README.md index f253552bb0..7da65be1a8 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,7 @@ Each platform has its own instructions: | Linux (32-bit x86 executable, OpenGL 4.5, SDL3) | [port/linux/README.md](port/linux/README.md) | | Windows (32-bit x86 executable, OpenGL 4.5, SDL3) | [port/windows/README.md](port/windows/README.md) | | Android (arm64 app, OpenGL ES 3, SDL3) | [port/android/README.md](port/android/README.md) | +| macOS (Apple Silicon, experimental source build) | [port/macos/BUILD.md](port/macos/BUILD.md) | The Linux README also gives the controls, the settings and the multiplayer functions. These are almost the same on all platforms. diff --git a/port/android/guest/runtime/guest_host.h b/port/android/guest/runtime/guest_host.h index 2c18715aa7..a4fe1863e9 100644 --- a/port/android/guest/runtime/guest_host.h +++ b/port/android/guest/runtime/guest_host.h @@ -44,6 +44,18 @@ void host_memory_watch_initialize(void); void host_memory_watch_protect(unsigned int address, unsigned int size); unsigned int host_memory_watch_generation(unsigned int address, unsigned int size); unsigned int host_memory_watch_serial(void); +#ifdef HALO_MACOS +int host_memory_fingerprint_page(unsigned int address, unsigned long long *hashes); +int host_memory_fingerprint_range(unsigned int address, unsigned int pages, + void *entries, volatile unsigned long *counter, unsigned long *newest); +unsigned int host_bink_open(const char *path, void *info, int audio_enabled); +int host_bink_decode(unsigned int token); +int host_bink_next(unsigned int token, void *info); +int host_bink_wait(unsigned int token); +int host_bink_copy(unsigned int token, void *destination, int pitch, + unsigned int height, unsigned int x, unsigned int y, unsigned int flags); +void host_bink_close(unsigned int token); +#endif void host_memory_watch_prepare_write(unsigned int address, unsigned int size); void host_memory_watch_forget(unsigned int address, unsigned int size); @@ -59,6 +71,13 @@ long long host_sdl_ticks(void); long long host_sdl_thread_id(void); unsigned int host_sdl_create_window(const char *title, int width, int height, long long flags); void host_sdl_window_size_in_pixels(unsigned int window, int *width, int *height); +#ifdef HALO_MACOS +int host_sdl_window_size(unsigned int window, int *width, int *height); +long long host_sdl_window_flags(unsigned int window); +int host_sdl_set_window_fullscreen(unsigned int window, int enabled); +int host_sdl_set_window_size(unsigned int window, int width, int height); +int host_sdl_warp_mouse(unsigned int window, float x, float y); +#endif int host_sdl_set_relative_mouse(unsigned int window, int enabled); int host_sdl_gl_set_attribute(int attribute, int value); unsigned int host_sdl_gl_create_context(unsigned int window); diff --git a/port/android/guest/runtime/guest_sdl.c b/port/android/guest/runtime/guest_sdl.c index ac775a6ddf..73f62ee112 100644 --- a/port/android/guest/runtime/guest_sdl.c +++ b/port/android/guest/runtime/guest_sdl.c @@ -115,6 +115,37 @@ bool SDL_SetWindowRelativeMouseMode(SDL_Window *window, bool enabled) return host_sdl_set_relative_mouse((unsigned int)window, enabled) != 0; } +#ifdef HALO_MACOS +bool SDL_GetWindowSize(SDL_Window *window, int *width, int *height) +{ + int w = 0, h = 0; + int result = host_sdl_window_size((unsigned int)window, &w, &h); + if (width) *width = w; + if (height) *height = h; + return result != 0; +} + +SDL_WindowFlags SDL_GetWindowFlags(SDL_Window *window) +{ + return (SDL_WindowFlags)host_sdl_window_flags((unsigned int)window); +} + +bool SDL_SetWindowSize(SDL_Window *window, int width, int height) +{ + return host_sdl_set_window_size((unsigned int)window, width, height) != 0; +} + +bool SDL_SetWindowFullscreen(SDL_Window *window, bool fullscreen) +{ + return host_sdl_set_window_fullscreen((unsigned int)window, fullscreen) != 0; +} + +void SDL_WarpMouseInWindow(SDL_Window *window, float x, float y) +{ + (void)host_sdl_warp_mouse((unsigned int)window, x, y); +} +#endif + bool SDL_GL_SetAttribute(SDL_GLAttr attribute, int value) { return host_sdl_gl_set_attribute((int)attribute, value) != 0; diff --git a/port/linux/game/network_distributed.c b/port/linux/game/network_distributed.c index ed87574fb1..a4994e10da 100644 --- a/port/linux/game/network_distributed.c +++ b/port/linux/game/network_distributed.c @@ -799,6 +799,19 @@ static void distributed_client_send_predictions( /* (the host) a client's own players: the latest of each, taken at the next tick */ +#ifdef HALO_MACOS +/* Fixed-size counters and opt-in output; disabled in ordinary human play. */ +static void distributed_trace(short stage, long machine, long type, long tick, long count) +{ + static long enabled = -1, counters[10]; + if (enabled < 0) enabled = getenv("HALO_NETWORK_TRACE") && strcmp(getenv("HALO_NETWORK_TRACE"), "1") == 0; + if (enabled && stage >= 0 && stage < 10 && (++counters[stage] <= 16 || counters[stage] % 300 == 0)) + fprintf(stderr, "[mp-decoder] stage=%d n=%ld machine=%ld type=%ld tick=%ld count=%ld\n", (int)stage, counters[stage], machine, type, tick, count); +} +#else +#define distributed_trace(stage,machine,type,tick,count) ((void)0) +#endif + static void distributed_handle_predictions( long machine_index, struct distributed_unit_state const *states, @@ -813,8 +826,10 @@ static void distributed_handle_predictions( if (state->player_index >= MAXIMUM_TRACKED_PLAYERS || !distributed_machine_has_player(machine_index, state->player_index)) { + distributed_trace(5, machine_index, state->player_index, state->unit_index, count); continue; } + distributed_trace(6, machine_index, state->player_index, state->unit_index, count); distributed_predictions[state->player_index].valid = TRUE; distributed_predictions[state->player_index].state = *state; } @@ -843,6 +858,7 @@ static void distributed_apply_predictions( real dy = state->position.y - object->object.position.y; real dz = state->position.z - object->object.position.z; + distributed_trace(7, player_index, unit_index, (long)((dx * dx + dy * dy + dz * dz) * 1000.0f), game_time_get()); if (dx * dx + dy * dy + dz * dz <= HOST_ACCEPT_TOLERANCE * HOST_ACCEPT_TOLERANCE) distributed_apply_state(unit_index, state, 0.0f, HOST_BLEND_DISTANCE); } @@ -1016,11 +1032,13 @@ static void distributed_handle_inputs( if (input->player_index >= MAXIMUM_TRACKED_PLAYERS || !distributed_machine_has_player(machine_index, input->player_index)) { + distributed_trace(8, machine_index, input->player_index, input->tick, count); continue; } player = distributed_player(input->player_index); if (!player) continue; + distributed_trace(9, machine_index, input->player_index, input->tick, input->control_flags); update_server_handle_distributed_input(DATUM_INDEX_NEW(input->player_index, player->identifier), input->tick, &input->action, input->control_flags, DISTRIBUTED_INPUT_HISTORY); if (input->host_time != NONE && machine_index >= 0 && machine_index < HALO_PORT_MAXIMUM_NETWORK_MACHINES) @@ -1681,6 +1699,7 @@ void network_distributed_handle_message( if (size < sizeof(header) || !network_game_distributed() || !game_in_progress()) return; csmemcpy(&header, message, sizeof(header)); + distributed_trace(0, machine_index, header.type, header.game_time, header.count); /* a tick's messages in one: each as if it came alone */ if (header.type == _distributed_message_batch) { @@ -1738,7 +1757,10 @@ void network_distributed_handle_message( case _distributed_message_vehicle_prediction: case _distributed_message_player_inputs: if (machine_index == NONE || game_connection() != _game_connection_network_server) + { + distributed_trace(1, machine_index, header.type, header.game_time, header.count); return; + } break; default: if (game_connection() != _game_connection_network_client) @@ -1750,7 +1772,11 @@ void network_distributed_handle_message( break; } if (distributed_message_stale(machine_index, &header)) + { + distributed_trace(2, machine_index, header.type, header.game_time, header.count); return; + } + distributed_trace(3, machine_index, header.type, header.game_time, header.count); switch (header.type) { diff --git a/port/linux/game/network_test.c b/port/linux/game/network_test.c index 2abe2c044a..99e1b4a782 100644 --- a/port/linux/game/network_test.c +++ b/port/linux/game/network_test.c @@ -28,6 +28,7 @@ Called from the main loop every frame (main.c). */ #include "cseries.h" +#include "bink/bink_playback.h" #include "main/main.h" #include "interface/player_ui.h" #include "interface/ui_widget.h" @@ -437,6 +438,14 @@ static void network_test_pickup( } } +/* main_menu_scenario_loaded can already be true while the intro owns the + * texture-cache scratch pool. Host setup precaches its map using that same + * pool, so wait for normal movie disposal before starting the menu timer. */ +static boolean network_test_frontend_ready(boolean main_menu_loaded) +{ + return main_menu_loaded && !bink_playback_active(); +} + void network_test_update( boolean main_menu_loaded, real seconds) @@ -545,7 +554,7 @@ void network_test_update( } } - if (!main_menu_loaded) + if (!network_test_frontend_ready(main_menu_loaded)) return; network_test.menu_seconds += seconds; /* (the main menu settling first) */ diff --git a/port/linux/include/StdDef.h b/port/linux/include/StdDef.h index 3465d16218..b50a16dc57 100644 --- a/port/linux/include/StdDef.h +++ b/port/linux/include/StdDef.h @@ -1,2 +1,8 @@ -/* the game includes ; Linux file names are case sensitive */ +/* The game includes ; Linux file names are case sensitive. On + * case-insensitive macOS filesystems this lowercase shim aliases StdDef.h + * itself, so skip this include directory to reach the target C library. */ +#if defined(HALO_MACOS) +#include_next +#else #include +#endif diff --git a/port/linux/include/halo_linux_prefix.h b/port/linux/include/halo_linux_prefix.h index 57d3359dcc..f1be0a8f79 100644 --- a/port/linux/include/halo_linux_prefix.h +++ b/port/linux/include/halo_linux_prefix.h @@ -47,7 +47,7 @@ emitting them so the redefinition above cannot reach them. */ /* ---------- __declspec(selectany) data (XDK D3DCONST tables) */ -#define DECLSPEC_SELECTANY __attribute__((weak)) +#define DECLSPEC_SELECTANY __attribute__((__weak__)) /* ---------- MSVC intrinsics diff --git a/port/linux/src/bink_null.c b/port/linux/src/bink_null.c index 37f03ca4bd..a3c63317f6 100644 --- a/port/linux/src/bink_null.c +++ b/port/linux/src/bink_null.c @@ -11,11 +11,24 @@ RADEXPLINK is __stdcall. */ #include "platform.h" +#ifdef HALO_MACOS +#include "guest_host.h" +#include "port_config.h" +#include +#endif typedef void *(__stdcall *rad_memory_allocate_proc)(unsigned long size); typedef void (__stdcall *rad_memory_free_proc)(void *memory); typedef void *(__stdcall *bink_sound_system_open_proc)(unsigned long param); typedef struct BINK *HBINK; +#ifdef HALO_MACOS +struct BINK +{ + unsigned long Width, Height, Frames, FrameNum, LastFrameNum; + unsigned int native_token; +}; +typedef char bink_wire_size_must_be_24[sizeof(struct BINK) == 24 ? 1 : -1]; +#endif void __stdcall RADSetMemory(rad_memory_allocate_proc allocate, rad_memory_free_proc release) { @@ -25,15 +38,24 @@ void __stdcall RADSetMemory(rad_memory_allocate_proc allocate, rad_memory_free_p void *__stdcall BinkOpenDirectSound(unsigned long param) { +#ifdef HALO_MACOS + return (void *)param; +#else (void)param; return NULL; +#endif } long __stdcall BinkSetSoundSystem(bink_sound_system_open_proc open, unsigned long param) { (void)open; (void)param; - return 0; + return +#ifdef HALO_MACOS + 1; +#else + 0; +#endif } void __stdcall BinkSetIOSize(unsigned long io_size) @@ -44,10 +66,55 @@ void __stdcall BinkSetIOSize(unsigned long io_size) HBINK __stdcall BinkOpen(const char *name, unsigned long flags) { (void)flags; +#ifdef HALO_MACOS + char path[1024]; + HBINK movie = calloc(1, sizeof(*movie)); + if (!movie) return NULL; + platform_translate_path(name, path, sizeof(path)); + movie->native_token = host_bink_open(path, movie, config_boolean("audio.enabled")); + if (!movie->native_token) { free(movie); platform_log("native Bink cannot open %s", path); return NULL; } + return movie; +#else platform_log("Bink video is not supported; skipping \"%s\"", name ? name : ""); return NULL; +#endif } +/* The native adapter owns codec/audio objects; the guest owns only this + * wire record. Closing/skipping releases both sides immediately. */ +#ifdef HALO_MACOS +void __stdcall BinkClose(HBINK movie) { if (movie) { host_bink_close(movie->native_token); free(movie); } } +long __stdcall BinkDoFrame(HBINK movie) +{ + if (!movie || !host_bink_decode(movie->native_token)) host_abort("native Bink video decode failed"); + return 0; +} +void __stdcall BinkNextFrame(HBINK movie) +{ + if (!movie || !host_bink_next(movie->native_token, movie)) host_abort("native Bink frame state failed"); +} +long __stdcall BinkWait(HBINK movie) { return movie ? host_bink_wait(movie->native_token) : 0; } +long __stdcall BinkCopyToBuffer(HBINK movie, void *destination, long pitch, + unsigned long height, unsigned long x, unsigned long y, unsigned long flags) +{ + if (!movie || !host_bink_copy(movie->native_token, destination, pitch, height, x, y, flags)) + host_abort("native Bink destination span or format invalid"); + return 0; +} +void __stdcall BinkGetSummary(HBINK movie, void *summary) +{ + unsigned long *words = summary; + memset(summary, 0, 31 * sizeof(unsigned long)); + if (movie) { words[0] = movie->Width; words[1] = movie->Height; words[8] = movie->Frames; words[9] = movie->FrameNum; } +} +void __stdcall BinkGetRealtime(HBINK movie, void *realtime, unsigned long frame_count) +{ + unsigned long *words = realtime; + (void)frame_count; + memset(realtime, 0, 14 * sizeof(unsigned long)); + if (movie) { words[0] = movie->FrameNum; words[3] = movie->FrameNum ? movie->FrameNum : 1; } +} +#else /* never reached without an open movie */ void __stdcall BinkClose(HBINK bink) { (void)bink; } @@ -66,3 +133,4 @@ long __stdcall BinkCopyToBuffer(HBINK bink, void *destination, long destination_ void __stdcall BinkGetSummary(HBINK bink, void *summary) { (void)bink; (void)summary; } void __stdcall BinkGetRealtime(HBINK bink, void *realtime, unsigned long frame_count) { (void)bink; (void)realtime; (void)frame_count; } +#endif diff --git a/port/linux/src/d3d8_gl.c b/port/linux/src/d3d8_gl.c index 36c5f23826..cf8ed99807 100644 --- a/port/linux/src/d3d8_gl.c +++ b/port/linux/src/d3d8_gl.c @@ -25,6 +25,9 @@ Conventions carried over from the Xbox: #include "sdl_platform.h" #include "halo_ui_pointer.h" #include "port_config.h" +#ifdef HALO_MACOS +#include "../../macos/gl41_compat.h" +#endif #include #include @@ -35,7 +38,7 @@ Conventions carried over from the Xbox: void d3d8_surface_initialize(D3DSurface *surface, D3DFORMAT format, unsigned long width, unsigned long height); void d3d8_surface_resize(D3DSurface *surface, D3DFORMAT format, unsigned long width, unsigned long height); -#ifdef HALO_ANDROID +#if defined(HALO_ANDROID) && !defined(HALO_MACOS) /* OpenGL ES 3 (port/android/README.md): the desktop formats, enumerants and entry points used below that ES lacks */ #define GL_BGRA GL_RGBA @@ -51,7 +54,10 @@ and entry points used below that ES lacks */ #define GL_CLAMP_TO_BORDER 0x812d #endif -/* what the context supports (gl_initialize) */ +#endif + +#if defined(HALO_ANDROID) || defined(HALO_MACOS) +/* Capabilities are initialized from the actual current GL context. */ struct xgpu_capabilities xgpu_capabilities; #endif @@ -84,7 +90,19 @@ static long ui_offset; static void screen_mode_choose(long *width, float scale[2]) { -#ifdef HALO_ANDROID +#ifdef HALO_MACOS + long render_width, render_height; + if (platform_render_resolution(&render_width, &render_height)) + { + /* Widen the camera's logical viewport; keep the original vertical + FOV and centered 640-column UI. Targets use explicit pixel sizes. */ + *width = ((SCREEN_HEIGHT * render_width + render_height / 2) / render_height + 1) & ~1L; + scale[0] = (float)render_width / (float)*width; + scale[1] = (float)render_height / (float)SCREEN_HEIGHT; + return; + } +#endif + #if defined(HALO_ANDROID) && !defined(HALO_MACOS) /* display.screen_width, or 0 for the display's shape, which the app passes (port/android/host/host_main.c) */ const char *display = getenv("HALO_DISPLAY_WIDTH"); @@ -108,6 +126,14 @@ static void screen_mode_choose(long *width, float scale[2]) long wanted = (SCREEN_HEIGHT * display_width + display_height / 2) / display_height; *width = wanted < 640 ? 640 : wanted > SCREEN_MAXIMUM_WIDTH ? SCREEN_MAXIMUM_WIDTH : wanted & ~1L; + /* Keep logical coordinates and aspect fixed while choosing a smaller + fullscreen render target. The existing display blit fills the desktop. */ + long render_width = config_integer("display.render_width"); + if (render_width >= 640 && render_width < display_width) + { + display_height = (display_height * render_width + display_width / 2) / display_width; + display_width = render_width; + } scale[0] = (float)display_width / (float)*width; scale[1] = (float)display_height / (float)SCREEN_HEIGHT; /* a display narrower or wider than the game can be: the picture @@ -720,7 +746,7 @@ static GLuint compile_shader(GLenum type, const char *source, const char *what) return shader; } -#ifndef HALO_ANDROID +#if !defined(HALO_ANDROID) && !defined(HALO_MACOS) static void GLAPIENTRY gl_debug_callback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const void *user) { @@ -750,6 +776,16 @@ static struct render_target_entry *render_target_get(const D3DSurface *surface) struct render_target_entry *entry; unsigned long width, height; BOOL depth; + static unsigned trace_allocations; + unsigned trace_index = 0; + if (getenv("HALO_TRACE_RT_ALLOC") && trace_allocations < 32) + { + trace_index = ++trace_allocations; + fprintf(stderr, "[rt-alloc] #%u input surface=%p common=0x%08x data=0x%08x format=0x%08x size=0x%08x\n", + trace_index, (const void *)surface, surface ? (unsigned)surface->Common : 0, + surface ? (unsigned)surface->Data : 0, surface ? (unsigned)surface->Format : 0, + surface ? (unsigned)surface->Size : 0); + } if (!surface || !surface->Data) return NULL; @@ -780,6 +816,14 @@ static struct render_target_entry *render_target_get(const D3DSurface *surface) entry->target.scale[1] = scale[1]; entry->target.gl_width = (unsigned long)(width * scale[0] + 0.5f); entry->target.gl_height = (unsigned long)(height * scale[1] + 0.5f); + if (trace_index) + { + union { float f; unsigned bits; } scale_x = { .f = scale[0] }, scale_y = { .f = scale[1] }; + fprintf(stderr, "[rt-alloc] #%u decoded=%lux%lu depth=%d scale={0x%08x/%.9g,0x%08x/%.9g} gl=%lux%lu data=0x%08lx format=0x%08x size=0x%08x\n", + trace_index, width, height, (int)depth, scale_x.bits, scale_x.f, scale_y.bits, scale_y.f, + entry->target.gl_width, entry->target.gl_height, (unsigned long)surface->Data, + (unsigned)surface->Format, (unsigned)surface->Size); + } glGenTextures(1, &entry->target.texture); glBindTexture(GL_TEXTURE_2D, entry->target.texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0); @@ -789,6 +833,10 @@ static struct render_target_entry *render_target_get(const D3DSurface *surface) else glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, (GLsizei)entry->target.gl_width, (GLsizei)entry->target.gl_height, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL); + if (trace_index) + fprintf(stderr, "[rt-alloc] #%u uploaded texture=%u size=%lux%lu depth=%d gl-error=0x%04x\n", + trace_index, entry->target.texture, entry->target.gl_width, entry->target.gl_height, + (int)depth, (unsigned)glGetError()); xgpu_gl_state_invalidate(); entry->next = render_targets; render_targets = entry; @@ -876,7 +924,7 @@ static void gl_initialize(void) glGetIntegerv(GL_MAJOR_VERSION, &major); glGetIntegerv(GL_MINOR_VERSION, &minor); -#ifdef HALO_ANDROID + #if defined(HALO_ANDROID) && !defined(HALO_MACOS) { BOOL es32 = major > 3 || (major == 3 && minor >= 2); @@ -903,6 +951,21 @@ static void gl_initialize(void) (int)major, (int)minor, xgpu_capabilities.copy_image, xgpu_capabilities.border_clamp, xgpu_capabilities.anisotropy, xgpu_capabilities.s3tc, xgpu_capabilities.atomic_counters); } +#elif defined(HALO_MACOS) + /* OpenGL 4.1 has neither clip control nor core KHR_debug. The vertex + shader performs the clip-origin/depth conversion for this context. */ + static char glsl_version[4]; + xgpu_capabilities.copy_image = FALSE; + /* GL_CLAMP_TO_BORDER is core long before the required 4.1 context. */ + xgpu_capabilities.border_clamp = TRUE; + xgpu_capabilities.anisotropy = host_gl_has_extension("GL_EXT_texture_filter_anisotropic"); + xgpu_capabilities.s3tc = host_gl_has_extension("GL_EXT_texture_compression_s3tc"); + xgpu_capabilities.base_vertex = major > 3 || (major == 3 && minor >= 2); + /* Fragment atomic counters entered desktop GL in 4.2, after Apple's 4.1 cap. */ + xgpu_capabilities.atomic_counters = FALSE; + snprintf(glsl_version, sizeof(glsl_version), "%d", (int)major * 100 + (int)minor * 10); + xgpu_capabilities.shading_language = glsl_version; + glEnable(GL_PROGRAM_POINT_SIZE); #else if (config_boolean("debug.gl_debug")) { @@ -942,7 +1005,7 @@ static void gl_initialize(void) #endif glGenSamplers(D3DTSS_MAXSTAGES, device.samplers); glGenQueries(VISIBILITY_TEST_SLOTS, device.queries); -#ifndef HALO_ANDROID +#if !defined(HALO_ANDROID) && !defined(HALO_MACOS) glGenBuffers(1, &device.visibility_results_buffer); glBindBuffer(GL_QUERY_BUFFER, device.visibility_results_buffer); glBufferStorage(GL_QUERY_BUFFER, VISIBILITY_TEST_SLOTS * sizeof(GLuint), NULL, @@ -1129,7 +1192,7 @@ HRESULT WINAPI Direct3D_CreateDevice(UINT adapter, D3DDEVTYPE device_type, void /* ---------- the menus' pointer */ -#ifdef HALO_ANDROID +#if defined(HALO_ANDROID) && !defined(HALO_MACOS) int halo_ui_pointer_update(int menus_active, struct halo_ui_pointer *pointer) { (void)menus_active; @@ -1204,7 +1267,7 @@ long halo_screen_commit(void) screen_width = width; screen_scale[0] = scale[0]; screen_scale[1] = scale[1]; -#ifndef HALO_ANDROID +#if !defined(HALO_ANDROID) || defined(HALO_MACOS) if (device.created) { device.presentation.BackBufferWidth = (UINT)width; @@ -1394,7 +1457,7 @@ HRESULT WINAPI D3DDevice_EndVisibilityTest(DWORD index) device.queries[0] = device.queries[index]; device.queries[index] = scratch; device.query_pending[index] = TRUE; -#ifndef HALO_ANDROID +#if !defined(HALO_ANDROID) && !defined(HALO_MACOS) if (device.visibility_results) { /* the GPU writes the count into the slot once it is known */ @@ -1442,7 +1505,7 @@ HRESULT WINAPI D3DDevice_GetVisibilityTestResult(DWORD index, UINT *result, ULON return S_OK; } #endif -#ifndef HALO_ANDROID +#if !defined(HALO_ANDROID) && !defined(HALO_MACOS) if (device.visibility_results) { /* the latest count the GPU has written: from this test, or while @@ -2066,7 +2129,7 @@ struct mip_composite static struct mip_composite *mip_composites; -#ifdef HALO_ANDROID +#if defined(HALO_ANDROID) && !defined(HALO_MACOS) static GLuint framebuffer_get(GLuint color, GLuint depth); /* glCopyImageSubData for ES 3.0/3.1 contexts without the extension */ @@ -2085,6 +2148,35 @@ static void copy_level_by_blit(GLuint source, GLuint destination, GLint level, G /* the blit bypasses the cached state, so the next draw must re-apply it */ xgpu_gl_state_invalidate(); } +#elif defined(HALO_MACOS) +/* OpenGL 4.1 has no core glCopyImageSubData. Copy through private FBOs and +restore independent READ/DRAW bindings plus scissor after each mip copy. */ +static struct halo_gl41_copy_state macos_copy_state; + +static void macos_draw_buffer(GLenum buffer) +{ + glDrawBuffers(1, &buffer); +} + +static int copy_level_by_blit(GLuint source, GLuint destination, GLint level, GLsizei width, GLsizei height) +{ + const struct halo_gl41_copy_api api = { + halo_glGetIntegerv, + halo_glIsEnabled, + halo_glEnable, + halo_glDisable, + halo_glGenFramebuffers, + halo_glBindFramebuffer, + halo_glFramebufferTexture2D, + halo_glReadBuffer, + macos_draw_buffer, + halo_glCheckFramebufferStatus, + halo_glBlitFramebuffer, + }; + + return halo_gl41_copy_image_2d(&api, &macos_copy_state, source, 0, + destination, level, width, height); +} #endif static GLuint mip_composite_get(const struct xgpu_texture_description *description, unsigned long data) @@ -2131,15 +2223,27 @@ static GLuint mip_composite_get(const struct xgpu_texture_description *descripti if (!target || target->width != width || target->height != height || target->gl_width != width || target->gl_height != height) break; -#ifdef HALO_ANDROID +#if defined(HALO_ANDROID) && !defined(HALO_MACOS) if (!xgpu_capabilities.copy_image) { copy_level_by_blit(target->texture, composite->texture, (GLint)level, (GLsizei)width, (GLsizei)height); } else -#endif + { + glCopyImageSubData(target->texture, GL_TEXTURE_2D, 0, 0, 0, 0, + composite->texture, GL_TEXTURE_2D, (GLint)level, 0, 0, 0, (GLsizei)width, (GLsizei)height, 1); + } +#elif defined(HALO_MACOS) + if (!copy_level_by_blit(target->texture, composite->texture, (GLint)level, + (GLsizei)width, (GLsizei)height)) + { + platform_log("cannot copy mip %lu of render target texture %u through OpenGL 4.1", level, target->texture); + break; + } +#else glCopyImageSubData(target->texture, GL_TEXTURE_2D, 0, 0, 0, 0, composite->texture, GL_TEXTURE_2D, (GLint)level, 0, 0, 0, (GLsizei)width, (GLsizei)height, 1); +#endif rendered_levels++; } glBindTexture(GL_TEXTURE_2D, composite->texture); @@ -2359,7 +2463,7 @@ static void apply_raster_state(BOOL has_depth) state_enable(&gl_state.cull_face, GL_CULL_FACE, rs[D3DRS_CULLMODE] != D3DCULL_NONE); if (rs[D3DRS_CULLMODE] != D3DCULL_NONE) { -#ifdef HALO_ANDROID +#if defined(HALO_ANDROID) || defined(HALO_MACOS) /* the vertex shader flips y in clip space, which (unlike desktop GL's upper-left clip origin) also flips the winding */ GLenum front_face = rs[D3DRS_FRONTFACE] == D3DFRONT_CCW ? GL_CW : GL_CCW; @@ -2509,7 +2613,7 @@ static struct program_entry *prepare_draw(BOOL immediate) key.alpha_test_function = D3D__RenderState[D3DRS_ALPHATESTENABLE] ? D3D__RenderState[D3DRS_ALPHAFUNC] : 0; key.fog_enable = D3D__RenderState[D3DRS_FOGENABLE] != 0; key.fog_table_mode = (unsigned char)D3D__RenderState[D3DRS_FOGTABLEMODE]; -#ifdef HALO_ANDROID +#if defined(HALO_ANDROID) && !defined(HALO_MACOS) key.count_samples = device.visibility_test_active && xgpu_capabilities.atomic_counters; #endif @@ -2526,7 +2630,7 @@ static struct program_entry *prepare_draw(BOOL immediate) else stats.draws++; state_program(entry->program); -#ifdef HALO_ANDROID +#if defined(HALO_ANDROID) && !defined(HALO_MACOS) if (key.count_samples) glBindBufferRange(GL_ATOMIC_COUNTER_BUFFER, 0, device.visibility_counters, (GLintptr)(device.counter_active * sizeof(GLuint)), sizeof(GLuint)); diff --git a/port/linux/src/gl.h b/port/linux/src/gl.h index 578e056705..f65537f4c1 100644 --- a/port/linux/src/gl.h +++ b/port/linux/src/gl.h @@ -16,7 +16,7 @@ Windows it is __stdcall too, and SDL would include windows.h without it) */ #ifndef _WIN32 #undef APIENTRY #endif -#ifdef HALO_ANDROID +#if defined(HALO_ANDROID) && !defined(HALO_MACOS) #include #include #define GLAPIENTRY GL_APIENTRY @@ -25,7 +25,7 @@ Windows it is __stdcall too, and SDL would include windows.h without it) */ #endif #pragma pop_macro("APIENTRY") -#ifdef HALO_ANDROID +#if defined(HALO_ANDROID) && !defined(HALO_MACOS) /* OpenGL ES 3.2 (port/android/README.md); tools/android_gl_stubs.py reads this list to generate the guest's entry points */ /* ANDROID_GL_FUNCTIONS_BEGIN */ @@ -131,13 +131,25 @@ this list to generate the guest's entry points */ X(glGetQueryObjectuiv) /* ANDROID_GL_FUNCTIONS_END */ #else +#ifdef HALO_MACOS +#define HALO_GL_COPY_IMAGE(X) +#define HALO_GL_CLIP_CONTROL(X) +#define HALO_GL_BUFFER_STORAGE(X) +#define HALO_GL_DEBUG_CALLBACK(X) +#else +#define HALO_GL_COPY_IMAGE(X) X(glCopyImageSubData) +#define HALO_GL_CLIP_CONTROL(X) X(glClipControl) +#define HALO_GL_BUFFER_STORAGE(X) X(glBufferStorage) +#define HALO_GL_DEBUG_CALLBACK(X) X(glDebugMessageCallback) +#endif #define GL_FUNCTIONS(X) \ X(glGetString) \ X(glGetIntegerv) \ X(glGetTexImage) \ - X(glCopyImageSubData) \ + HALO_GL_COPY_IMAGE(X) \ X(glGenerateMipmap) \ X(glGetError) \ + X(glIsEnabled) \ X(glEnable) \ X(glDisable) \ X(glViewport) \ @@ -165,7 +177,7 @@ this list to generate the guest's entry points */ X(glReadPixels) \ X(glFinish) \ X(glFlush) \ - X(glClipControl) \ + HALO_GL_CLIP_CONTROL(X) \ X(glGenTextures) \ X(glDeleteTextures) \ X(glBindTexture) \ @@ -191,12 +203,14 @@ this list to generate the guest's entry points */ X(glCheckFramebufferStatus) \ X(glBlitFramebuffer) \ X(glDrawBuffers) \ + X(glReadBuffer) \ X(glGenBuffers) \ X(glDeleteBuffers) \ X(glBindBuffer) \ X(glBufferData) \ X(glBufferSubData) \ - X(glBufferStorage) \ + X(glBindBufferRange) \ + HALO_GL_BUFFER_STORAGE(X) \ X(glMapBufferRange) \ X(glBindBufferBase) \ X(glGenVertexArrays) \ @@ -234,7 +248,7 @@ this list to generate the guest's entry points */ X(glBeginQuery) \ X(glEndQuery) \ X(glGetQueryObjectuiv) \ - X(glDebugMessageCallback) + HALO_GL_DEBUG_CALLBACK(X) #endif #define GL_DECLARE_FUNCTION(name) extern __typeof__(&name) halo_##name; @@ -244,10 +258,12 @@ GL_FUNCTIONS(GL_DECLARE_FUNCTION) /* call sites use the ordinary names; gl_functions.c, which defines the pointers, sees the declarations without these aliases */ #ifndef GL_FUNCTIONS_DEFINE -#ifdef HALO_ANDROID +#if defined(HALO_ANDROID) && !defined(HALO_MACOS) #define glGetString halo_glGetString #define glGetIntegerv halo_glGetIntegerv +#ifndef HALO_MACOS #define glCopyImageSubData halo_glCopyImageSubData +#endif #define glGenerateMipmap halo_glGenerateMipmap #define glGetError halo_glGetError #define glEnable halo_glEnable @@ -348,7 +364,11 @@ pointers, sees the declarations without these aliases */ #define glGetString halo_glGetString #define glGetIntegerv halo_glGetIntegerv #define glGetTexImage halo_glGetTexImage +#define glIsEnabled halo_glIsEnabled +#define glReadBuffer halo_glReadBuffer +#ifndef HALO_MACOS #define glCopyImageSubData halo_glCopyImageSubData +#endif #define glGenerateMipmap halo_glGenerateMipmap #define glGetError halo_glGetError #define glEnable halo_glEnable diff --git a/port/linux/src/halo_linker_common.c b/port/linux/src/halo_linker_common.c index 8d80c3802c..dd3094cd83 100644 --- a/port/linux/src/halo_linker_common.c +++ b/port/linux/src/halo_linker_common.c @@ -25,7 +25,7 @@ missing from the reconstruction (docs/xbox_link_probe_20260924.md). /* ---------- pooled COMMON globals */ #define HALO_COMMON(name, size) \ - __attribute__((weak, aligned(16))) unsigned char name[size] + __attribute__((__weak__, aligned(16))) unsigned char name[size] HALO_COMMON(ai_globals, 12); /* struct ai_globals_data *ai_globals */ HALO_COMMON(antenna_data, 12); /* struct data_array *antenna_data */ @@ -168,14 +168,14 @@ HALO_COMMON(window_globals, 160); /* struct window_globals_prefix window_globals /* Round-to-integer as the x87 does in the current rounding mode, like the inline fast_ftol in cseries.h (fld/fistp). */ -__attribute__((weak)) long fast_ftol_C(float value) +__attribute__((__weak__)) long fast_ftol_C(float value) { return lrintf(value); } /* The debug console's "crash" command, which deliberately brings the game down to exercise crash handling. */ -__attribute__((weak)) void main_crash(const char *reason) +__attribute__((__weak__)) void main_crash(const char *reason) { platform_log("main_crash: %s", reason ? reason : ""); abort(); diff --git a/port/linux/src/input_bindings.c b/port/linux/src/input_bindings.c new file mode 100644 index 0000000000..ab3d1f4b23 --- /dev/null +++ b/port/linux/src/input_bindings.c @@ -0,0 +1,47 @@ +#include "input_bindings.h" +#include +#include +static SDL_Scancode key_name(const char *name) +{ + static const struct { const char *name; SDL_Scancode key; } names[] = { + {"SPACE",SDL_SCANCODE_SPACE},{"TAB",SDL_SCANCODE_TAB},{"ENTER",SDL_SCANCODE_RETURN}, + {"ESCAPE",SDL_SCANCODE_ESCAPE},{"BACKSPACE",SDL_SCANCODE_BACKSPACE}, + {"LEFTCTRL",SDL_SCANCODE_LCTRL},{"CTRL",SDL_SCANCODE_LCTRL}, + {"LEFTSHIFT",SDL_SCANCODE_LSHIFT},{"SHIFT",SDL_SCANCODE_LSHIFT}, + {"LEFTALT",SDL_SCANCODE_LALT},{"ALT",SDL_SCANCODE_LALT}, + {"UP",SDL_SCANCODE_UP},{"DOWN",SDL_SCANCODE_DOWN},{"LEFT",SDL_SCANCODE_LEFT},{"RIGHT",SDL_SCANCODE_RIGHT}}; + unsigned int i; + if (name[0] && !name[1]) { + if (name[0]>='A' && name[0]<='Z') return (SDL_Scancode)(SDL_SCANCODE_A+name[0]-'A'); + if (name[0]>='1' && name[0]<='9') return (SDL_Scancode)(SDL_SCANCODE_1+name[0]-'1'); + if (name[0]=='0') return SDL_SCANCODE_0; + } + for(i=0;i=sizeof(token)) return 0; token[n++]=(char)toupper((unsigned char)*text++); } + while(n && (token[n-1]==' ' || token[n-1]=='\t')) n--; + token[n]=0; + if (!n) return 0; + if(!strcmp(token,"WHEEL")) result.wheel=1; + else if(!strncmp(token,"MOUSE",5) && token[5]>='1' && token[5]<='5' && !token[6]) result.mouse_mask|=(unsigned char)(1u<<(token[5]-'0')); + else { key=key_name(token); if(key==SDL_SCANCODE_UNKNOWN || result.key_count==8) return 0; result.keys[result.key_count++]=key; } + if (*text==',') { text++; if(!*text) return 0; } + } + *binding=result; return 1; +} +int input_binding_down(const struct input_binding *binding, const unsigned char *keys, + const unsigned char *buttons, int captured) +{ + unsigned int i; + for(i=0;ikey_count;i++) if(keys[binding->keys[i]]) return 1; + if(captured) for(i=1;i<=5;i++) if((binding->mouse_mask&(1u< +struct input_binding { SDL_Scancode keys[8]; unsigned char key_count, mouse_mask, wheel; }; +/* Comma separated physical key names; empty disables an action. */ +int input_binding_parse(const char *text, struct input_binding *binding); +int input_binding_down(const struct input_binding *binding, const unsigned char *keys, + const unsigned char *buttons, int captured); +#endif diff --git a/port/linux/src/memory_watch.c b/port/linux/src/memory_watch.c index 67bbabefde..4b52370e1e 100644 --- a/port/linux/src/memory_watch.c +++ b/port/linux/src/memory_watch.c @@ -17,7 +17,193 @@ such a write (memory_watch_prepare_write) is not enough on its own: the renderer can protect the pages again before the kernel writes them. */ +#if defined(HALO_MACOS) && defined(HALO_MEMORY_WATCH_TEST) +#include "memory_watch_test_platform.h" +#else #include "platform.h" +#endif + +#ifdef HALO_MACOS + +#ifdef HALO_MEMORY_WATCH_TEST_ARENA +extern unsigned char halo_watch_test_arena[]; +#undef PLATFORM_CONTIGUOUS_BASE +#define PLATFORM_CONTIGUOUS_BASE ((unsigned long)halo_watch_test_arena) +#undef PLATFORM_CONTIGUOUS_SIZE +#define PLATFORM_CONTIGUOUS_SIZE 0x10000UL +#endif + +/* +Darwin's AOT host does not currently install the source port's signal-based +write watch. Do not report a stable generation in its place: both texture +lookup and the vertex/index mirror use generations to skip reading guest +bytes. Instead, fingerprint the requested 4 KiB pages on each query. This +is deliberately a correctness fallback; it trades CPU time for not reusing +GPU copies based on an unwatched page. + +There is no mprotect here, and the serial changes on every lookup so the +renderer cannot take its recent-texture shortcut without checking the +resource pages. `prepare_write` and `forget` invalidate fingerprints so a +write/free/reuse is also observed even if the bytes happen to compare equal +at the first query after the event. +*/ + +#include +#include +#include +#ifndef HALO_MEMORY_WATCH_TEST +#include "guest_host.h" +#endif + +#define WATCH_PAGE_SIZE 0x1000UL +#define WATCH_PAGE_COUNT (PLATFORM_CONTIGUOUS_SIZE / WATCH_PAGE_SIZE) + +struct page_fingerprint +{ + uint64_t first; + uint64_t second; + unsigned long generation; + unsigned char valid; +}; + +static struct page_fingerprint page_fingerprints[WATCH_PAGE_COUNT]; +static volatile unsigned long current_generation = 1; +static pthread_mutex_t fingerprint_lock = PTHREAD_MUTEX_INITIALIZER; + +static BOOL watched_span(unsigned long address, unsigned long size, + unsigned long *first, unsigned long *last) +{ + unsigned long offset; + + if (!size || address < PLATFORM_CONTIGUOUS_BASE) + return FALSE; + offset = address - PLATFORM_CONTIGUOUS_BASE; + if (offset >= PLATFORM_CONTIGUOUS_SIZE || size > PLATFORM_CONTIGUOUS_SIZE - offset) + return FALSE; + if (!platform_is_contiguous((void *)address) || + !platform_is_contiguous((void *)(address + size - 1))) + return FALSE; + *first = offset / WATCH_PAGE_SIZE; + *last = (offset + size - 1) / WATCH_PAGE_SIZE; + return *last < WATCH_PAGE_COUNT; +} + +#ifdef HALO_MEMORY_WATCH_TEST +static void fingerprint_page(unsigned long page, uint64_t *first, uint64_t *second) +{ +#ifndef HALO_MEMORY_WATCH_TEST + /* Preserve both fingerprints and every lookup scan. Only the expensive + * byte loop leaves translated i386 code; no write is assumed absent. */ + unsigned long long hashes[2]; + if (!host_memory_fingerprint_page(PLATFORM_CONTIGUOUS_BASE + page * WATCH_PAGE_SIZE, hashes)) + host_abort("invalid guest span in memory page fingerprint"); + *first = hashes[0]; + *second = hashes[1]; +#else + const volatile unsigned char *bytes = (const volatile unsigned char *)( + PLATFORM_CONTIGUOUS_BASE + page * WATCH_PAGE_SIZE); + uint64_t a = UINT64_C(14695981039346656037); + uint64_t b = UINT64_C(0x9e3779b97f4a7c15); + unsigned long index; + + for (index = 0; index < WATCH_PAGE_SIZE; index++) + { + uint64_t value = bytes[index]; + a = (a ^ value) * UINT64_C(1099511628211); + b ^= value + UINT64_C(0x9e3779b97f4a7c15) + (b << 6) + (b >> 2); + b *= UINT64_C(0xbf58476d1ce4e5b9); + } + *first = a; + *second = b; +#endif +} +#endif + +void memory_watch_initialize(void) +{ + /* Zero-initialized fingerprints are ready; this backend never protects pages. */ +} + +void memory_watch_protect(unsigned long address, unsigned long size) +{ + /* No page protection is installed. Every generation query fingerprints bytes. */ + (void)address; + (void)size; +} + +unsigned long memory_watch_generation(unsigned long address, unsigned long size) +{ + unsigned long first_page, last_page, newest = 0; +#ifdef HALO_MEMORY_WATCH_TEST + unsigned long page; +#else + typedef char fingerprint_wire_size_must_be_24[sizeof(struct page_fingerprint) == 24 ? 1 : -1]; +#endif + + if (!watched_span(address, size, &first_page, &last_page)) + return 0; + pthread_mutex_lock(&fingerprint_lock); +#ifndef HALO_MEMORY_WATCH_TEST + /* The native range loop implements the same per-page comparison and + * generation updates, including invalidated entries, in one ABI call. */ + if (!host_memory_fingerprint_range(PLATFORM_CONTIGUOUS_BASE + first_page * WATCH_PAGE_SIZE, + last_page - first_page + 1, &page_fingerprints[first_page], ¤t_generation, &newest)) + host_abort("invalid guest span in memory range fingerprint"); +#else + for (page = first_page; page <= last_page; page++) + { + struct page_fingerprint *entry = &page_fingerprints[page]; + uint64_t first, second; + + fingerprint_page(page, &first, &second); + if (!entry->valid || entry->first != first || entry->second != second) + { + entry->first = first; + entry->second = second; + entry->valid = 1; + entry->generation = __sync_add_and_fetch(¤t_generation, 1); + } + if (entry->generation > newest) + newest = entry->generation; + } +#endif + pthread_mutex_unlock(&fingerprint_lock); + return newest; +} + +unsigned long memory_watch_serial(void) +{ + /* Force callers to validate their own ranges instead of trusting a global + serial that cannot identify which guest pages have changed. */ + return __sync_add_and_fetch(¤t_generation, 1); +} + +static void invalidate_span(unsigned long address, unsigned long size) +{ + unsigned long first_page, last_page, page; + + if (!watched_span(address, size, &first_page, &last_page)) + return; + pthread_mutex_lock(&fingerprint_lock); + for (page = first_page; page <= last_page; page++) + { + page_fingerprints[page].valid = 0; + page_fingerprints[page].generation = __sync_add_and_fetch(¤t_generation, 1); + } + pthread_mutex_unlock(&fingerprint_lock); +} + +void memory_watch_prepare_write(void *address, unsigned long size) +{ + invalidate_span((unsigned long)address, size); +} + +void memory_watch_forget(void *address, unsigned long size) +{ + invalidate_span((unsigned long)address, size); +} + +#else #include #include @@ -194,3 +380,5 @@ void memory_watch_forget(void *address, unsigned long size) page_generation[page] = __sync_add_and_fetch(¤t_generation, 1); } } + +#endif /* HALO_MACOS */ diff --git a/port/linux/src/msvc_crt.c b/port/linux/src/msvc_crt.c index afeaddac64..b3ae9b9e02 100644 --- a/port/linux/src/msvc_crt.c +++ b/port/linux/src/msvc_crt.c @@ -295,7 +295,7 @@ static unsigned short msvc_to_control_word(unsigned int value, unsigned short wo return word; } -#ifdef HALO_ANDROID +#if defined(HALO_ANDROID) && !defined(HALO_MACOS) /* AArch64: the rounding mode lives in FPCR.RMode, the sticky exception flags in FPSR. Precision control and exception unmasking have no equivalent; the rest of the MSVC control word is only remembered. */ diff --git a/port/linux/src/nv2a_psh.c b/port/linux/src/nv2a_psh.c index 5223c9ab87..68f6c8e133 100644 --- a/port/linux/src/nv2a_psh.c +++ b/port/linux/src/nv2a_psh.c @@ -323,6 +323,9 @@ static void dot_input(struct xgpu_text *text, const DWORD *state, int stage) "precision highp sampler2D;\n" \ "precision highp sampler3D;\n" \ "precision highp samplerCube;\n" +#elif defined(HALO_MACOS) +#define SAMPLE_BIAS "" +#define SHADER_VERSION "#version 410 core\n" #else #define SAMPLE_BIAS "" #define SHADER_VERSION "#version 450 core\n" diff --git a/port/linux/src/nv2a_vsh.c b/port/linux/src/nv2a_vsh.c index bfb1a7b16f..15190b7686 100644 --- a/port/linux/src/nv2a_vsh.c +++ b/port/linux/src/nv2a_vsh.c @@ -144,6 +144,8 @@ static const char shader_prologue[] = /* the #version line comes first, from the context's capabilities */ "precision highp float;\n" "precision highp int;\n" +#elif defined(HALO_MACOS) + "#version 410 core\n" #else "#version 450 core\n" #endif @@ -232,7 +234,7 @@ char *nv2a_vertex_shader_to_glsl(const DWORD *instructions, unsigned long instru "\tvec4 oFog = vec4(1.0), oPts = vec4(point_size), oUnused = vec4(0.0);\n" "\tint a0 = 0;\n" "\tvec4 A, B, C, mac, ilu;\n"); -#ifdef HALO_ANDROID +#if defined(HALO_ANDROID) || defined(HALO_MACOS) xgpu_text_append(&text, "\tvec4 clip_position = vec4(0.0);\n\tbool clip_captured = false;\n"); #endif @@ -286,7 +288,7 @@ char *nv2a_vertex_shader_to_glsl(const DWORD *instructions, unsigned long instru case _ilu_lit: xgpu_text_append(&text, "\tilu = nv2a_lit(C);\n"); break; default: xgpu_text_append(&text, "\tilu = vec4(0.0);\n"); break; } -#ifdef HALO_ANDROID +#if defined(HALO_ANDROID) || defined(HALO_MACOS) /* the screen-space conversion takes the reciprocal of the clip-space position's w (rcc of r12.w); keep the position it converts */ if (ilu == _ilu_rcc && field(instruction, 3, 28, 2) == _mux_temporary && @@ -340,7 +342,7 @@ char *nv2a_vertex_shader_to_glsl(const DWORD *instructions, unsigned long instru /* Direct3D 8 puts pixel centres on integer screen coordinates (the game offsets its screen-space quads by -0.5 to match), OpenGL on half-integers */ -#ifdef HALO_ANDROID +#if defined(HALO_ANDROID) || defined(HALO_MACOS) /* The conversion is screen = clip * c[-38] * rcc(w) + c[-37]; undoing it by multiplying by w again is lossy near the camera plane, where rcc clamps and 1/w rounds differently on each GPU (Mali put vertices @@ -355,7 +357,7 @@ char *nv2a_vertex_shader_to_glsl(const DWORD *instructions, unsigned long instru "\tvec3 ndc = (vec3(oPos.xy + vec2(0.5 + screen_offset, 0.5), oPos.z) - viewport_offset.xyz) / scale;\n" "\tgl_Position = vec4(ndc * oPos.w, oPos.w);\n" #endif -#ifdef HALO_ANDROID +#if defined(HALO_ANDROID) || defined(HALO_MACOS) /* what glClipControl(GL_UPPER_LEFT, GL_ZERO_TO_ONE) does on desktop GL: rows from the top, depth 0..1 */ "\tgl_Position.y = -gl_Position.y;\n" @@ -372,7 +374,7 @@ char *nv2a_vertex_shader_to_glsl(const DWORD *instructions, unsigned long instru "\txT3 = oT3;\n" "\txFog = oFog.x;\n" "}\n" -#ifdef HALO_ANDROID +#if defined(HALO_ANDROID) || defined(HALO_MACOS) , XGPU_VERTEX_CONSTANT_BIAS - 38, XGPU_VERTEX_CONSTANT_BIAS - 37 #endif ); diff --git a/port/linux/src/port_config.c b/port/linux/src/port_config.c index 004871e3b6..b02d547ee7 100644 --- a/port/linux/src/port_config.c +++ b/port/linux/src/port_config.c @@ -63,13 +63,30 @@ struct config_setting const char *comment; }; +#ifdef HALO_MACOS +#define DEFAULT_FULLSCREEN "false" +#define DEFAULT_WINDOW_SCALE "1" +#else +#define DEFAULT_FULLSCREEN "true" +#define DEFAULT_WINDOW_SCALE "2" +#endif + static const struct config_setting config_settings[] = { - { "display.fullscreen", _config_boolean, "true", "HALO_FULLSCREEN", _environment_value, _platform_desktop, +#ifdef HALO_MACOS + { "display.resolution", _config_string, "\"1280x720\"", "HALO_RESOLUTION", _environment_value, _platform_desktop, + "Render pixels: 1280x720 or 1920x1080 for widescreen, 640x480 for\n" + "the original picture, native for the display's shape. Retina window\n" + "density does not multiply the internal rendering resolution." }, +#endif + { "display.fullscreen", _config_boolean, DEFAULT_FULLSCREEN, "HALO_FULLSCREEN", _environment_value, _platform_desktop, "Start fullscreen, drawing at the display's resolution and shape; false\n" "starts in a window, which draws the Xbox's 640x480. F11 switches." }, - { "display.window_scale", _config_integer, "2", "HALO_WINDOW_SCALE", _environment_value, _platform_desktop, + { "display.window_scale", _config_integer, DEFAULT_WINDOW_SCALE, "HALO_WINDOW_SCALE", _environment_value, _platform_desktop, "The window's size as a multiple of 640x480 (it can be resized)." }, + { "display.render_width", _config_integer, "0", "HALO_RENDER_WIDTH", _environment_value, _platform_desktop, + "Fullscreen rendering width in pixels: 0 uses the display; positive\n" + "values from 640 up to the display width keep the display's shape." }, { "display.screen_width", _config_integer, "0", "HALO_SCREEN_WIDTH", _environment_value, _platform_android, "Columns of the 480-line picture: 0 for the display's shape, 640 for the\n" "Xbox's 4:3." }, @@ -89,6 +106,27 @@ static const struct config_setting config_settings[] = { "input.invert_mouse", _config_boolean, "false", "HALO_MOUSE_INVERT", _environment_set_is_true, _platform_desktop, "Moving the mouse forward looks down." }, + /* Physical key bindings: comma separated keys, Mouse1..5, Wheel; empty disables. */ + { "input.forward", _config_string, "\"W\"", "HALO_KEY_FORWARD", _environment_value, _platform_desktop, "Gameplay binding; comma separated physical key names." }, + { "input.backward", _config_string, "\"S\"", "HALO_KEY_BACKWARD", _environment_value, _platform_desktop, "Gameplay binding; comma separated physical key names." }, + { "input.left", _config_string, "\"A\"", "HALO_KEY_LEFT", _environment_value, _platform_desktop, "Gameplay binding; comma separated physical key names." }, + { "input.right", _config_string, "\"D\"", "HALO_KEY_RIGHT", _environment_value, _platform_desktop, "Gameplay binding; comma separated physical key names." }, + { "input.jump", _config_string, "\"Space\"", "HALO_KEY_JUMP", _environment_value, _platform_desktop, "Gameplay binding; comma separated physical key names." }, + { "input.melee", _config_string, "\"F,Mouse4\"", "HALO_KEY_MELEE", _environment_value, _platform_desktop, "Gameplay binding; comma separated physical key names." }, + { "input.action", _config_string, "\"E,R\"", "HALO_KEY_ACTION", _environment_value, _platform_desktop, "Gameplay binding; comma separated physical key names." }, + { "input.change_weapon", _config_string, "\"Tab,Wheel\"", "HALO_KEY_CHANGE_WEAPON", _environment_value, _platform_desktop, "Gameplay binding; comma separated physical key names." }, + { "input.flashlight", _config_string, "\"Q\"", "HALO_KEY_FLASHLIGHT", _environment_value, _platform_desktop, "Gameplay binding; comma separated physical key names." }, + { "input.change_grenade", _config_string, "\"X\"", "HALO_KEY_CHANGE_GRENADE", _environment_value, _platform_desktop, "Gameplay binding; comma separated physical key names." }, + { "input.fire", _config_string, "\"Mouse1\"", "HALO_KEY_FIRE", _environment_value, _platform_desktop, "Gameplay binding; comma separated physical key names." }, + { "input.crouch", _config_string, "\"LeftCtrl,C\"", "HALO_KEY_CROUCH", _environment_value, _platform_desktop, "Gameplay binding; comma separated physical key names." }, +#ifdef HALO_MACOS + { "input.grenade", _config_string, "\"G\"", "HALO_KEY_GRENADE", _environment_value, _platform_desktop, "Throw grenade." }, + { "input.zoom", _config_string, "\"Z,Mouse2,Mouse3\"", "HALO_KEY_ZOOM", _environment_value, _platform_desktop, "Toggle weapon zoom." }, +#else + { "input.grenade", _config_string, "\"G,Mouse2\"", "HALO_KEY_GRENADE", _environment_value, _platform_desktop, "Throw grenade." }, + { "input.zoom", _config_string, "\"Z,Mouse3\"", "HALO_KEY_ZOOM", _environment_value, _platform_desktop, "Toggle weapon zoom." }, +#endif + { "game.language", _config_string, "\"\"", "HALO_LANGUAGE", _environment_value, _platform_all, "The language the game asks the Xbox for: \"ja\", \"de\", \"fr\", \"es\" or \"it\";\n" "empty for English. The game data decides what is translated." }, @@ -217,7 +255,7 @@ static const struct config_setting config_settings[] = #define NUMBER_OF_CONFIG_SETTINGS (sizeof(config_settings) / sizeof(config_settings[0])) -#ifdef HALO_ANDROID +#if defined(HALO_ANDROID) && !defined(HALO_MACOS) #define CONFIG_PLATFORM _platform_android #else #define CONFIG_PLATFORM _platform_desktop @@ -239,7 +277,14 @@ static pthread_mutex_t config_lock = PTHREAD_MUTEX_INITIALIZER; static void config_path(char *path, size_t size) { -#ifdef HALO_ANDROID +#ifdef HALO_MACOS + /* Native AOT keeps user settings beside its isolated saves. The data + root may contain read-only links to the original extracted maps. */ + const char *root = getenv("HALO_CONFIG_ROOT"); + if (!root || !*root) + root = getenv("HALO_SAVE_ROOT"); + snprintf(path, size, "%s/config.toml", root && *root ? root : "."); +#elif defined(HALO_ANDROID) /* the data folder, which the app names (port/android/host/host_main.c) */ const char *root = getenv("HALO_DATA_ROOT"); @@ -367,7 +412,7 @@ static void config_append_setting(struct config_text *text, const struct config_ if (*line) line++; } -#ifndef HALO_ANDROID +#if !defined(HALO_ANDROID) || defined(HALO_MACOS) /* (Android apps have no environment to set) */ switch (setting->environment_style) { @@ -394,7 +439,7 @@ static char *config_default_text(void) char section[32] = ""; size_t index; -#ifdef HALO_ANDROID +#if defined(HALO_ANDROID) && !defined(HALO_MACOS) config_append(&text, "# Halo settings\n" "#\n" @@ -769,9 +814,9 @@ static int config_line_section(const char *line, const char *end, char *section, return 1; } -/* sets a boolean setting, for now and in config.toml: its line there is -changed (or added), the rest of the file kept as it is */ -int config_write_boolean(const char *name, int value) +/* Set a typed value now and in config.toml; replace only its line and +preserve the rest of the player's file. String callers supply checked TOML. */ +static int config_write_serialized(const char *name, const char *value, enum config_type type) { const char *dot = strchr(name, '.'); long index = config_setting_index(name); @@ -782,15 +827,21 @@ int config_write_boolean(const char *name, int value) const char *line; int written = 0, in_section = 0, succeeded; - if (index < 0 || config_settings[index].type != _config_boolean || !dot || (size_t)(dot - name) >= sizeof(section)) + if (index < 0 || config_settings[index].type != type || !dot || (size_t)(dot - name) >= sizeof(section)) return 0; /* (the file read first, as the other settings are) */ - config_boolean(name); + config_value(name, type); pthread_mutex_lock(&config_lock); - config_values[index].boolean = value != 0; + if (type == _config_boolean) config_values[index].boolean = !strcmp(value, "true"); + else { + char *copy = config_copy(value + 1, strlen(value) - 2); + if (!copy) { pthread_mutex_unlock(&config_lock); return 0; } + free(config_values[index].string); + config_values[index].string = copy; + } snprintf(section, sizeof(section), "%.*s", (int)(dot - name), name); snprintf(key, sizeof(key), "%s", dot + 1); - snprintf(line_text, sizeof(line_text), "%s = %s\n", key, value ? "true" : "false"); + snprintf(line_text, sizeof(line_text), "%s = %s\n", key, value); snprintf(wanted, sizeof(wanted), "%s", section); config_path(path, sizeof(path)); text = config_read_file(path, &size); @@ -847,6 +898,22 @@ int config_write_boolean(const char *name, int value) return succeeded; } +int config_write_boolean(const char *name, int value) +{ + return config_write_serialized(name, value ? "true" : "false", _config_boolean); +} + +int config_write_string(const char *name, const char *value) +{ + char quoted[80]; + size_t length; + if (!value || (length = strlen(value)) > sizeof(quoted) - 3) return 0; + for (size_t i = 0; i < length; ++i) + if ((unsigned char)value[i] < 32 || value[i] == '"' || value[i] == '\\') return 0; + snprintf(quoted, sizeof(quoted), "\"%s\"", value); + return config_write_serialized(name, quoted, _config_string); +} + /* ---------- public code */ int config_boolean(const char *name) diff --git a/port/linux/src/port_config.h b/port/linux/src/port_config.h index 9b66ec547e..68d38cc27e 100644 --- a/port/linux/src/port_config.h +++ b/port/linux/src/port_config.h @@ -21,5 +21,7 @@ const char *config_string(const char *name); /* sets a boolean setting, and writes it into config.toml (only its line changes); 1 on success */ int config_write_boolean(const char *name, int value); +/* Plain strings without control characters, quotes or backslashes; preserves other settings. */ +int config_write_string(const char *name, const char *value); #endif diff --git a/port/linux/src/posix_files.c b/port/linux/src/posix_files.c index 92f2f48695..a348b2e690 100644 --- a/port/linux/src/posix_files.c +++ b/port/linux/src/posix_files.c @@ -30,6 +30,14 @@ static void fill_information(const struct stat *st, struct posix_file_informatio if (!(st->st_mode & S_IWUSR)) information->flags |= _posix_file_is_read_only; split64((unsigned long long)st->st_size, &information->size_low, &information->size_high); +#ifdef __APPLE__ + information->modification_seconds = (posix_ulong)st->st_mtimespec.tv_sec; + information->modification_nanoseconds = (posix_ulong)st->st_mtimespec.tv_nsec; + information->access_seconds = (posix_ulong)st->st_atimespec.tv_sec; + information->access_nanoseconds = (posix_ulong)st->st_atimespec.tv_nsec; + information->creation_seconds = (posix_ulong)st->st_ctimespec.tv_sec; + information->creation_nanoseconds = (posix_ulong)st->st_ctimespec.tv_nsec; +#else information->modification_seconds = (posix_ulong)st->st_mtim.tv_sec; information->modification_nanoseconds = (posix_ulong)st->st_mtim.tv_nsec; information->access_seconds = (posix_ulong)st->st_atim.tv_sec; @@ -37,6 +45,7 @@ static void fill_information(const struct stat *st, struct posix_file_informatio /* Linux has no portable creation time; the change time is the closest */ information->creation_seconds = (posix_ulong)st->st_ctim.tv_sec; information->creation_nanoseconds = (posix_ulong)st->st_ctim.tv_nsec; +#endif } int posix_stat(const char *path, struct posix_file_information *information) diff --git a/port/linux/src/posix_net.c b/port/linux/src/posix_net.c index d3d6486860..c9106d94b5 100644 --- a/port/linux/src/posix_net.c +++ b/port/linux/src/posix_net.c @@ -18,7 +18,9 @@ with the host ABI. #include #include #include +#ifndef __APPLE__ #include +#endif #include #include #include @@ -28,6 +30,16 @@ with the host ABI. #include "posix.h" +#ifdef __APPLE__ +/* Darwin sockets are close-on-exec only after fcntl; SOCK_CLOEXEC is absent. */ +#ifndef SOCK_CLOEXEC +#define SOCK_CLOEXEC 0 +#endif +#ifndef MSG_NOSIGNAL +#define MSG_NOSIGNAL 0 +#endif +#endif + /* Winsock error codes (winsockx.h) */ #define WSAEINTR 10004 #define WSAEBADF 10009 @@ -124,7 +136,16 @@ int posix_socket_last_error(void) int posix_socket(int family, int type, int protocol) { - return succeed(socket(family, type | SOCK_CLOEXEC, protocol)); + int descriptor = socket(family, type | SOCK_CLOEXEC, protocol); +#ifdef __APPLE__ + if (descriptor >= 0) + { + int enabled = 1; + (void)fcntl(descriptor, F_SETFD, FD_CLOEXEC); + (void)setsockopt(descriptor, SOL_SOCKET, SO_NOSIGPIPE, &enabled, sizeof(enabled)); + } +#endif + return succeed(descriptor); } int posix_socket_close(int socket) @@ -163,7 +184,18 @@ int posix_socket_listen(int socket, int backlog) int posix_socket_accept(int socket, void *address, int *address_length) { socklen_t length = address_length ? (socklen_t)*address_length : 0; - int result = accept4(socket, address, address_length ? &length : NULL, SOCK_CLOEXEC); + int result; +#ifdef __APPLE__ + result = accept(socket, address, address_length ? &length : NULL); + if (result >= 0) + { + int enabled = 1; + (void)fcntl(result, F_SETFD, FD_CLOEXEC); + (void)setsockopt(result, SOL_SOCKET, SO_NOSIGPIPE, &enabled, sizeof(enabled)); + } +#else + result = accept4(socket, address, address_length ? &length : NULL, SOCK_CLOEXEC); +#endif if (address_length) *address_length = (int)length; @@ -412,6 +444,9 @@ posix_ulong posix_local_ipv4_address(void) void posix_random_bytes(void *buffer, posix_ulong size) { +#ifdef __APPLE__ + arc4random_buf(buffer, size); +#else unsigned char *cursor = buffer; while (size) @@ -427,6 +462,7 @@ void posix_random_bytes(void *buffer, posix_ulong size) cursor += count; size -= (posix_ulong)count; } +#endif } posix_ulong posix_resolve_ipv4(const char *host) @@ -449,7 +485,7 @@ posix_ulong posix_resolve_ipv4(const char *host) int posix_command_line_argument(int index, char *buffer, posix_ulong size) { -#ifdef __ANDROID__ +#if defined(__ANDROID__) || defined(HALO_MACOS) (void)index; (void)buffer; (void)size; @@ -484,7 +520,7 @@ posix_ulong posix_process_id(void) return (posix_ulong)getpid(); } -#ifndef __ANDROID__ +#if !defined(__ANDROID__) && !defined(HALO_MACOS) /* runs a program with its arguments and waits for it; its exit status, or -1 */ static int run_program(char *const arguments[]) { @@ -502,7 +538,7 @@ static int run_program(char *const arguments[]) int posix_register_url_scheme(const char *scheme, const char *description) { -#ifdef __ANDROID__ +#if defined(__ANDROID__) || defined(HALO_MACOS) (void)scheme; (void)description; return 0; @@ -566,7 +602,7 @@ int posix_register_url_scheme(const char *scheme, const char *description) int posix_discord_connect(void) { -#ifdef __ANDROID__ +#if defined(__ANDROID__) || defined(HALO_MACOS) return -1; #else /* where Discord (and its Flatpak and Snap packages) put discord-ipc-N */ diff --git a/port/linux/src/sdl_platform.c b/port/linux/src/sdl_platform.c index fda1bfd3f1..bf74ff2be0 100644 --- a/port/linux/src/sdl_platform.c +++ b/port/linux/src/sdl_platform.c @@ -11,6 +11,10 @@ and the debug keyboard that the game's console reads. #include "platform.h" #include "sdl_platform.h" +extern void halo_input_focus_lost(void); +#ifdef HALO_MACOS +extern void halo_input_gamepads_changed(void); +#endif #include "gl.h" #include "port_config.h" #include "p2p.h" @@ -30,7 +34,7 @@ static struct platform_input_state input_state; /* keys pressed since the last read, so a press and release between two reads still counts as a press (input injected on Android, or a slow frame) */ static unsigned char keys_pressed[SDL_SCANCODE_COUNT]; -#ifndef HALO_ANDROID +#if !defined(HALO_ANDROID) || defined(HALO_MACOS) /* the menus' pointer (platform_ui_pointer_set_active), under input_lock */ static struct platform_ui_pointer ui_pointer; static float ui_pointer_wheel; @@ -57,7 +61,7 @@ BOOL platform_sdl_initialize(void) if (p2p_hand_off_invite()) exit(EXIT_SUCCESS); SDL_SetHint(SDL_HINT_APP_NAME, "Halo"); -#ifdef HALO_ANDROID +#if defined(HALO_ANDROID) && !defined(HALO_MACOS) /* landscape only; the back key arrives as a key event (xinput_sdl.c) instead of closing the activity */ SDL_SetHint(SDL_HINT_ORIENTATIONS, "LandscapeLeft LandscapeRight"); @@ -298,14 +302,16 @@ int halo_interpolation_enabled(void) return enabled; } -#ifndef HALO_ANDROID +#if !defined(HALO_ANDROID) || defined(HALO_MACOS) /* whether the window opens fullscreen (display.fullscreen), never when it is hidden */ static BOOL platform_fullscreen_setting(void) { return !config_boolean("debug.hidden_window") && config_boolean("display.fullscreen"); } +#endif +#ifndef HALO_ANDROID /* whether the game is, or is to be, fullscreen, and if so the size in pixels of the display it fills (d3d8_gl.c draws at that resolution) */ BOOL platform_screen_mode(long *width, long *height) @@ -326,7 +332,37 @@ BOOL platform_screen_mode(long *width, long *height) *height = (long)(mode->h * mode->pixel_density + 0.5f); return TRUE; } +#endif +#ifdef HALO_MACOS +BOOL platform_render_resolution(long *width, long *height) +{ + const char *resolution = config_string("display.resolution"); + if (!strcmp(resolution, "native")) return FALSE; + if (!strcmp(resolution, "1920x1080")) { *width = 1920; *height = 1080; } + else if (!strcmp(resolution, "640x480")) { *width = 640; *height = 480; } + else { *width = 1280; *height = 720; } + return TRUE; +} +static void platform_cycle_render_resolution(void) +{ + const char *current = config_string("display.resolution"); + const char *next = !strcmp(current, "640x480") ? "1280x720" : + !strcmp(current, "1280x720") ? "1920x1080" : "640x480"; + int points_w = 0, points_h = 0, pixels_w = 0, pixels_h = 0; + long width = !strcmp(next, "640x480") ? 640 : !strcmp(next, "1280x720") ? 1280 : 1920; + long height = !strcmp(next, "640x480") ? 480 : !strcmp(next, "1280x720") ? 720 : 1080; + SDL_GetWindowSize(platform_window, &points_w, &points_h); + SDL_GetWindowSizeInPixels(platform_window, &pixels_w, &pixels_h); + double density = points_w > 0 && pixels_w > 0 ? (double)pixels_w / points_w : 1.0; + if (density < 1.0) density = 1.0; + if (!SDL_SetWindowSize(platform_window, (int)(width / density + 0.5), (int)(height / density + 0.5))) + platform_log("display mode: cannot resize window: %s", SDL_GetError()); + if (!config_write_string("display.resolution", next)) + platform_log("display mode: cannot persist %s", next); + /* halo_screen_commit reuses the normal back/depth-buffer resize path next frame. */ + platform_log("display mode: %s (F10)", next); +} #endif BOOL platform_video_initialize(unsigned long width, unsigned long height) { @@ -339,11 +375,26 @@ BOOL platform_video_initialize(unsigned long width, unsigned long height) return FALSE; if (scale < 1) scale = 1; +#ifdef HALO_MACOS + { + long render_width, render_height; + if (platform_render_resolution(&render_width, &render_height)) + { + width = (unsigned long)render_width; + height = (unsigned long)render_height; + scale = 1; + } + } +#endif -#ifdef HALO_ANDROID +#if defined(HALO_ANDROID) && !defined(HALO_MACOS) SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); +#elif defined(HALO_MACOS) + SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); #else SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4); @@ -352,8 +403,14 @@ BOOL platform_video_initialize(unsigned long width, unsigned long height) SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0); SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0); +#ifdef HALO_MACOS + SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, + SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG | + (config_boolean("debug.gl_debug") ? SDL_GL_CONTEXT_DEBUG_FLAG : 0)); +#else if (config_boolean("debug.gl_debug")) SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG); +#endif #if !defined(HALO_ANDROID) && !defined(_WIN32) /* Mesa's GL thread: the renderer makes thousands of GL calls a frame and never waits for their results, so handing them to a thread of @@ -362,7 +419,7 @@ BOOL platform_video_initialize(unsigned long width, unsigned long height) setenv("mesa_glthread", "true", 0); #endif -#ifdef HALO_ANDROID +#if defined(HALO_ANDROID) && !defined(HALO_MACOS) platform_window = SDL_CreateWindow("Halo", (int)(width * scale), (int)(height * scale), SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN); #else @@ -381,7 +438,7 @@ BOOL platform_video_initialize(unsigned long width, unsigned long height) return FALSE; } platform_gl_context = SDL_GL_CreateContext(platform_window); -#ifdef HALO_ANDROID +#if defined(HALO_ANDROID) && !defined(HALO_MACOS) /* ES 3.2 where the driver has it, otherwise the renderer makes do with 3.0 plus extensions */ if (!platform_gl_context) @@ -402,7 +459,7 @@ BOOL platform_video_initialize(unsigned long width, unsigned long height) (void)version; platform_event_thread = SDL_GetCurrentThreadID(); platform_log("OpenGL %s on %s", (const char *)glGetString(GL_VERSION), (const char *)glGetString(GL_RENDERER)); -#ifndef HALO_ANDROID +#if !defined(HALO_ANDROID) || defined(HALO_MACOS) platform_mouse_capture(TRUE); #endif return TRUE; @@ -589,7 +646,7 @@ static void platform_invite_clipboard(BOOL look) SDL_SetClipboardText(invite); snprintf(seen, sizeof(seen), "%s", invite); platform_log("Internet play: the invite link is on the clipboard"); -#ifdef HALO_ANDROID +#if defined(HALO_ANDROID) && !defined(HALO_MACOS) SDL_ShowAndroidToast("Hosting: the invite link is on the clipboard", 1, -1, 0, 0); #endif } @@ -602,7 +659,7 @@ static void platform_invite_clipboard(BOOL look) snprintf(seen, sizeof(seen), "%s", text); if (p2p_join_invite(text)) { -#ifdef HALO_ANDROID +#if defined(HALO_ANDROID) && !defined(HALO_MACOS) SDL_ShowAndroidToast("Joining the invite on the clipboard", 1, -1, 0, 0); #endif } @@ -683,6 +740,13 @@ void platform_pump_events(void) double seconds = config_real("debug.exit_after"); exit_ticks = seconds > 0.0 ? SDL_GetTicks() + (Uint64)(seconds * 1000.0) : 0; + if (getenv("HALO_TRACE_SCREEN_MODE")) + { + union { double value; uint64_t bits; } raw_seconds = { .value = seconds }; + fprintf(stderr, "[exit-after-trace] seconds=%.17g bits=0x%016llx now=%llu deadline=%llu\n", + raw_seconds.value, (unsigned long long)raw_seconds.bits, + (unsigned long long)SDL_GetTicks(), (unsigned long long)exit_ticks); + } } if (exit_ticks && SDL_GetTicks() >= exit_ticks) { @@ -704,7 +768,39 @@ void platform_pump_events(void) exit(EXIT_SUCCESS); case SDL_EVENT_KEY_DOWN: case SDL_EVENT_KEY_UP: - if (event.key.scancode < SDL_SCANCODE_COUNT) +#ifdef HALO_MACOS + if (event.key.down && event.key.scancode == SDL_SCANCODE_Q && (event.key.mod & SDL_KMOD_GUI)) { + pthread_mutex_unlock(&input_lock); platform_log("Command-Q"); exit(EXIT_SUCCESS); + } + /* Leave Command-Tab and macOS screenshot chords to the system. + Screenshot overlays need a free pointer even if focus stays here. */ + if (event.key.mod & SDL_KMOD_GUI) + { + input_state.mouse_released = TRUE; + platform_mouse_capture(FALSE); + memset(input_state.keys, 0, sizeof(input_state.keys)); + memset(input_state.mouse_buttons, 0, sizeof(input_state.mouse_buttons)); + memset(keys_pressed, 0, sizeof(keys_pressed)); + keystroke_head = keystroke_count = 0; + input_state.mouse_dx = input_state.mouse_dy = input_state.mouse_wheel = 0; + halo_input_focus_lost(); + break; + } + /* Consume both edges/repeats so F10 never enters gameplay or text input. */ + if (event.key.scancode == SDL_SCANCODE_F10) + { + if (event.key.down && !event.key.repeat && + !(event.key.mod & (SDL_KMOD_CTRL | SDL_KMOD_ALT | SDL_KMOD_SHIFT | SDL_KMOD_GUI))) + platform_cycle_render_resolution(); + break; + } + if (event.key.down && event.key.scancode == SDL_SCANCODE_ESCAPE) + { + input_state.mouse_released = TRUE; + platform_mouse_capture(FALSE); + } +#endif + if (event.key.scancode >= 0 && event.key.scancode < SDL_SCANCODE_COUNT) { input_state.keys[event.key.scancode] = event.key.down; if (event.key.down) @@ -717,7 +813,7 @@ void platform_pump_events(void) input_state.mouse_released = !input_state.mouse_released; platform_mouse_capture(!input_state.mouse_released && !input_state.ui_pointer); } -#ifndef HALO_ANDROID +#if !defined(HALO_ANDROID) || defined(HALO_MACOS) /* F11 switches between fullscreen and the window (SDL keeps the window's size and place while fullscreen) */ if (event.key.down && !event.key.repeat && event.key.scancode == SDL_SCANCODE_F11) @@ -728,7 +824,7 @@ void platform_pump_events(void) #endif break; case SDL_EVENT_MOUSE_MOTION: -#ifndef HALO_ANDROID +#if !defined(HALO_ANDROID) || defined(HALO_MACOS) /* in the menus the mouse moves the pointer, not the view */ if (input_state.ui_pointer) { @@ -743,7 +839,17 @@ void platform_pump_events(void) break; case SDL_EVENT_MOUSE_BUTTON_DOWN: case SDL_EVENT_MOUSE_BUTTON_UP: -#ifndef HALO_ANDROID +#ifdef HALO_MACOS + if (input_state.focused && input_state.mouse_released && !input_state.ui_pointer && + event.button.down && event.button.button == SDL_BUTTON_LEFT) + { + input_state.mouse_released = FALSE; + input_state.mouse_dx = input_state.mouse_dy = 0; + platform_mouse_capture(TRUE); + break; /* the recapture click must not fire the weapon */ + } +#endif +#if !defined(HALO_ANDROID) || defined(HALO_MACOS) /* clicks in the menus go to the pointer; a button held down when the menu closes stays up until pressed again, so the click that resumes the game does not also fire */ @@ -766,7 +872,7 @@ void platform_pump_events(void) input_state.mouse_buttons[event.button.button] = event.button.down; break; case SDL_EVENT_MOUSE_WHEEL: -#ifndef HALO_ANDROID +#if !defined(HALO_ANDROID) || defined(HALO_MACOS) if (input_state.ui_pointer) { /* whole notches: smooth-scrolling wheels send fractions */ @@ -790,18 +896,35 @@ void platform_pump_events(void) memset(input_state.keys, 0, sizeof(input_state.keys)); memset(input_state.mouse_buttons, 0, sizeof(input_state.mouse_buttons)); input_state.focused = FALSE; + memset(keys_pressed, 0, sizeof(keys_pressed)); + keystroke_head = keystroke_count = 0; + input_state.mouse_dx = input_state.mouse_dy = input_state.mouse_wheel = 0; + halo_input_focus_lost(); +#if !defined(HALO_ANDROID) || defined(HALO_MACOS) + platform_mouse_capture(FALSE); + ui_pointer.left_clicks = ui_pointer.right_clicks = ui_pointer.wheel_steps = 0; + ui_pointer_wheel = 0.0f; +#endif break; case SDL_EVENT_WINDOW_FOCUS_GAINED: input_state.focused = TRUE; look_at_clipboard = TRUE; -#ifndef HALO_ANDROID +#if !defined(HALO_ANDROID) || defined(HALO_MACOS) if (!input_state.mouse_released && !input_state.ui_pointer) platform_mouse_capture(TRUE); #endif break; case SDL_EVENT_GAMEPAD_ADDED: SDL_OpenGamepad(event.gdevice.which); +#ifdef HALO_MACOS + halo_input_gamepads_changed(); +#endif break; +#ifdef HALO_MACOS + case SDL_EVENT_GAMEPAD_REMOVED: + halo_input_gamepads_changed(); + break; +#endif default: break; } @@ -811,7 +934,7 @@ void platform_pump_events(void) platform_invite_clipboard(look_at_clipboard); } -#ifndef HALO_ANDROID +#if !defined(HALO_ANDROID) || defined(HALO_MACOS) /* ---------- the menus' pointer */ /* While a menu is up the mouse is released, its pointer shows (centered when diff --git a/port/linux/src/sdl_platform.h b/port/linux/src/sdl_platform.h index fe7e84ab67..d2f8adf7bb 100644 --- a/port/linux/src/sdl_platform.h +++ b/port/linux/src/sdl_platform.h @@ -35,6 +35,9 @@ struct platform_keystroke BOOL platform_sdl_initialize(void); /* creates the window and makes its OpenGL context current on this thread */ BOOL platform_video_initialize(unsigned long width, unsigned long height); +#ifdef HALO_MACOS +BOOL platform_render_resolution(long *width, long *height); +#endif #ifndef HALO_ANDROID BOOL platform_screen_mode(long *width, long *height); #endif @@ -49,7 +52,7 @@ void platform_mouse_capture(BOOL capture); void platform_pump_events(void); /* a snapshot of the input state; consume_motion resets the mouse deltas */ void platform_input_read(struct platform_input_state *state, BOOL consume_motion); -#ifndef HALO_ANDROID +#if !defined(HALO_ANDROID) || defined(HALO_MACOS) /* the pointer in the menus (d3d8_gl.c, halo_ui_pointer_update) */ struct platform_ui_pointer { diff --git a/port/linux/src/xgpu.h b/port/linux/src/xgpu.h index 7d276da87a..9cd4ac61bd 100644 --- a/port/linux/src/xgpu.h +++ b/port/linux/src/xgpu.h @@ -13,15 +13,16 @@ device itself (d3d8_gl.c). #include "platform.h" #include "gl.h" -#ifdef HALO_ANDROID -/* OpenGL ES features that are optional (d3d8_gl.c gl_initialize) */ +#if defined(HALO_ANDROID) || defined(HALO_MACOS) +/* GL features that are optional on the Android and native macOS renderers + * (d3d8_gl.c gl_initialize). */ struct xgpu_capabilities { BOOL copy_image; BOOL border_clamp; BOOL anisotropy; BOOL s3tc; - /* ES 3.2: glDrawElementsBaseVertex */ + /* GL ES 3.2 / desktop GL 3.2: glDrawElementsBaseVertex */ BOOL base_vertex; /* ES 3.1 with fragment atomic counters: exact visibility test counts */ BOOL atomic_counters; diff --git a/port/linux/src/xinput_sdl.c b/port/linux/src/xinput_sdl.c index 287c113729..6b8af865df 100644 --- a/port/linux/src/xinput_sdl.c +++ b/port/linux/src/xinput_sdl.c @@ -35,9 +35,11 @@ drive the controller. #include "platform.h" #include "sdl_platform.h" #include "port_config.h" +#include "input_bindings.h" #include #include +#include #include #include @@ -86,7 +88,7 @@ static float mouse_sensitivity(void) if (sensitivity < 0.0f) { sensitivity = (float)config_real("input.mouse_sensitivity"); - if (sensitivity <= 0.0f) + if (!isfinite(sensitivity) || sensitivity <= 0.0f) sensitivity = 1.0f; } return sensitivity; @@ -132,7 +134,7 @@ static void mouse_poll(const struct platform_input_state *input) mouse_pending_x = 0.0f; mouse_pending_y = 0.0f; } - if (!input->mouse_released) + if (input->focused && !input->mouse_released) { mouse_pending_x += input->mouse_dx; mouse_pending_y += input->mouse_dy; @@ -150,17 +152,92 @@ static BYTE analog(BOOL down) return down ? 0xff : 0x00; } +enum { B_FORWARD, B_BACKWARD, B_LEFT, B_RIGHT, B_JUMP, B_MELEE, B_ACTION, + B_WEAPON, B_FLASHLIGHT, B_GRENADE_TYPE, B_GRENADE, B_FIRE, B_CROUCH, B_ZOOM, B_COUNT }; +static struct input_binding bindings[B_COUNT]; +static void load_bindings(void) +{ + static int loaded; + static const char *names[B_COUNT] = {"forward","backward","left","right","jump","melee","action", + "change_weapon","flashlight","change_grenade","grenade","fire","crouch","zoom"}; + static const char *defaults[B_COUNT] = {"W","S","A","D","Space","F,Mouse4","E,R", + "Tab,Wheel","Q","X", +#ifdef HALO_MACOS + "G","Mouse1","LeftCtrl,C","Z,Mouse2,Mouse3" +#else + "G,Mouse2","Mouse1","LeftCtrl,C","Z,Mouse3" +#endif + }; + int i; + if(loaded) return; + for(i=0;i=18) return NULL; + if(physical_gamepad_present) { + static const wchar_t *sony[18]={L"Cross",L"Circle",L"Square",L"Triangle",L"R1",L"L1",L"L2",L"R2", + L"D-pad Up",L"D-pad Down",L"D-pad Left",L"D-pad Right",L"Options",L"Share",L"L3",L"R3",L"Left Stick",L"Right Stick"}; + if(physical_gamepad_type==SDL_GAMEPAD_TYPE_PS3 || physical_gamepad_type==SDL_GAMEPAD_TYPE_PS4 || physical_gamepad_type==SDL_GAMEPAD_TYPE_PS5) return sony[icon]; + return NULL; + } + if(names[icon]) { snprintf(name,sizeof(name),"input.%s",names[icon]); text=config_string(name); } + else text=fixed[icon]; + if(names[icon] && !input_binding_parse(text,&check)) text=fallback[icon]; + if(icon==16) { + const char *f=config_string("input.forward"), *b=config_string("input.backward"); + const char *l=config_string("input.left"), *r=config_string("input.right"); + if(strcmp(f,"W") || strcmp(b,"S") || strcmp(l,"A") || strcmp(r,"D")) { + snprintf(movement,sizeof(movement),"%s/%s/%s/%s",f,b,l,r); text=movement; + } + } + if(!text || !*text) text="Unbound"; + for(i=0;i<39 && text[i] && text[i]!=',';i++) labels[icon][i]=(wchar_t)(unsigned char)text[i]; + labels[icon][i]=0; + return labels[icon]; +} +#endif +void halo_input_focus_lost(void) +{ + pthread_mutex_lock(&mouse_lock); + mouse_pending_x=mouse_pending_y=mouse_wheel_accumulated=0.0f; + mouse_polls_unconsumed=0; wheel_moved_ms=wheel_press_until_ms=0; wheel_scrolling=FALSE; + pthread_mutex_unlock(&mouse_lock); +} + static void keyboard_gamepad(const struct platform_input_state *input, XINPUT_GAMEPAD *pad) { const unsigned char *k = input->keys; BOOL mouse = !input->mouse_released; const unsigned char *m = input->mouse_buttons; int x = 0, y = 0; + load_bindings(); +#define DOWN(b) input_binding_down(&bindings[b], k, m, mouse) - if (k[SDL_SCANCODE_D]) x++; - if (k[SDL_SCANCODE_A]) x--; - if (k[SDL_SCANCODE_W]) y++; - if (k[SDL_SCANCODE_S]) y--; + if (DOWN(B_RIGHT)) x++; + if (DOWN(B_LEFT)) x--; + if (DOWN(B_FORWARD)) y++; + if (DOWN(B_BACKWARD)) y--; if (x || y) { /* full deflection, diagonals on the unit circle */ @@ -176,23 +253,23 @@ static void keyboard_gamepad(const struct platform_input_state *input, XINPUT_GA if (k[SDL_SCANCODE_RIGHT]) pad->wButtons |= XINPUT_GAMEPAD_DPAD_RIGHT; if (k[SDL_SCANCODE_ESCAPE]) pad->wButtons |= XINPUT_GAMEPAD_START; if (k[SDL_SCANCODE_F1]) pad->wButtons |= XINPUT_GAMEPAD_BACK; - if (k[SDL_SCANCODE_LCTRL] || k[SDL_SCANCODE_C]) pad->wButtons |= XINPUT_GAMEPAD_LEFT_THUMB; - if (k[SDL_SCANCODE_Z] || (mouse && m[SDL_BUTTON_MIDDLE])) pad->wButtons |= XINPUT_GAMEPAD_RIGHT_THUMB; + if (DOWN(B_CROUCH)) pad->wButtons |= XINPUT_GAMEPAD_LEFT_THUMB; + if (DOWN(B_ZOOM)) pad->wButtons |= XINPUT_GAMEPAD_RIGHT_THUMB; - pad->bAnalogButtons[XINPUT_GAMEPAD_A] |= analog(k[SDL_SCANCODE_SPACE] || k[SDL_SCANCODE_RETURN] || + pad->bAnalogButtons[XINPUT_GAMEPAD_A] |= analog(DOWN(B_JUMP) || k[SDL_SCANCODE_RETURN] || k[SDL_SCANCODE_KP_ENTER]); - pad->bAnalogButtons[XINPUT_GAMEPAD_B] |= analog(k[SDL_SCANCODE_F] || k[SDL_SCANCODE_BACKSPACE] || - (mouse && m[SDL_BUTTON_X1])); + pad->bAnalogButtons[XINPUT_GAMEPAD_B] |= analog(DOWN(B_MELEE) || k[SDL_SCANCODE_BACKSPACE]); #ifdef HALO_ANDROID /* the system back key (gesture or button) backs out of menus */ pad->bAnalogButtons[XINPUT_GAMEPAD_B] |= analog(k[SDL_SCANCODE_AC_BACK]); #endif - pad->bAnalogButtons[XINPUT_GAMEPAD_X] |= analog(k[SDL_SCANCODE_E] || k[SDL_SCANCODE_R]); - pad->bAnalogButtons[XINPUT_GAMEPAD_Y] |= analog(k[SDL_SCANCODE_TAB] || SDL_GetTicks() < wheel_press_until_ms); - pad->bAnalogButtons[XINPUT_GAMEPAD_WHITE] |= analog(k[SDL_SCANCODE_Q]); - pad->bAnalogButtons[XINPUT_GAMEPAD_BLACK] |= analog(k[SDL_SCANCODE_X]); - pad->bAnalogButtons[XINPUT_GAMEPAD_LEFT_TRIGGER] |= analog(k[SDL_SCANCODE_G] || (mouse && m[SDL_BUTTON_RIGHT])); - pad->bAnalogButtons[XINPUT_GAMEPAD_RIGHT_TRIGGER] |= analog(mouse && m[SDL_BUTTON_LEFT]); + pad->bAnalogButtons[XINPUT_GAMEPAD_X] |= analog(DOWN(B_ACTION)); + pad->bAnalogButtons[XINPUT_GAMEPAD_Y] |= analog(DOWN(B_WEAPON) || (bindings[B_WEAPON].wheel && SDL_GetTicks() < wheel_press_until_ms)); + pad->bAnalogButtons[XINPUT_GAMEPAD_WHITE] |= analog(DOWN(B_FLASHLIGHT)); + pad->bAnalogButtons[XINPUT_GAMEPAD_BLACK] |= analog(DOWN(B_GRENADE_TYPE)); + pad->bAnalogButtons[XINPUT_GAMEPAD_LEFT_TRIGGER] |= analog(DOWN(B_GRENADE)); + pad->bAnalogButtons[XINPUT_GAMEPAD_RIGHT_TRIGGER] |= analog(DOWN(B_FIRE)); +#undef DOWN } /* A scroll of the wheel switches weapons once: it holds Y for WHEEL_PRESS_MS @@ -226,6 +303,7 @@ static void test_input_gamepad(XINPUT_GAMEPAD *pad) { static int checked; static int seed = -1; + static int movement_only; double t; if (!checked) @@ -233,14 +311,21 @@ static void test_input_gamepad(XINPUT_GAMEPAD *pad) const char *setting = config_string("debug.test_input"); checked = 1; - if (!strncmp(setting, "bot:", 4)) + if (!strncmp(setting, "move:", 5)) + { + seed = atoi(setting + 5); + movement_only = 1; + } + else if (!strncmp(setting, "bot:", 4)) seed = atoi(setting + 4); else if (!strcmp(setting, "bot")) seed = 0; } if (seed < 0) return; - if (test_input_holding_action) + if (movement_only) + memset(pad, 0, sizeof(*pad)); + if (test_input_holding_action && !movement_only) { /* (standing still, the button held from a second on) */ if (SDL_GetTicks() - test_input_holding_action_since >= 1000) @@ -251,6 +336,8 @@ static void test_input_gamepad(XINPUT_GAMEPAD *pad) pad->sThumbLY = (SHORT)(sin(t * 0.9) * 32000.0); pad->sThumbLX = (SHORT)(cos(t * 0.6 + seed) * 20000.0); pad->sThumbRX = (SHORT)(sin(t * 0.4) * 14000.0); + if (movement_only) + return; if (fmod(t, 3.0) < 0.3) pad->bAnalogButtons[XINPUT_GAMEPAD_RIGHT_TRIGGER] = 255; if (fmod(t, 5.0) < 0.1) @@ -283,18 +370,70 @@ static void wheel_update(void) /* ---------- SDL gamepads */ /* the SDL gamepads in connection order, at most one per port */ +#ifdef HALO_MACOS +/* Four fixed player slots. Enumerate/allocate an SDL list only initially or + * after hot-plug. A token lookup also recovers a missed removal notification. */ +static pthread_mutex_t gamepad_cache_lock = PTHREAD_MUTEX_INITIALIZER; +static SDL_JoystickID assigned_gamepads[PORT_COUNT]; +static SDL_Gamepad *cached_gamepads[PORT_COUNT]; +static int cached_gamepad_count; +static BOOL gamepad_cache_dirty = TRUE; +void halo_input_gamepads_changed(void) +{ + pthread_mutex_lock(&gamepad_cache_lock); + gamepad_cache_dirty = TRUE; + pthread_mutex_unlock(&gamepad_cache_lock); +} +#endif + static int sdl_gamepads(SDL_Gamepad *gamepads[PORT_COUNT]) { SDL_JoystickID *ids; int count = 0, index, found = 0; +#ifdef HALO_MACOS + pthread_mutex_lock(&gamepad_cache_lock); + for(index=0;index 32767) result = 32767; @@ -380,6 +524,8 @@ static void sdl_gamepad_state(SDL_Gamepad *gamepad, XINPUT_GAMEPAD *pad) left_trigger = SDL_GetGamepadAxis(gamepad, SDL_GAMEPAD_AXIS_LEFT_TRIGGER) * 255 / 32767; right_trigger = SDL_GetGamepadAxis(gamepad, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER) * 255 / 32767; + left_trigger = left_trigger < 0 ? 0 : left_trigger > 255 ? 255 : left_trigger; + right_trigger = right_trigger < 0 ? 0 : right_trigger > 255 ? 255 : right_trigger; if (left_trigger > pad->bAnalogButtons[XINPUT_GAMEPAD_LEFT_TRIGGER]) pad->bAnalogButtons[XINPUT_GAMEPAD_LEFT_TRIGGER] = (BYTE)left_trigger; if (right_trigger > pad->bAnalogButtons[XINPUT_GAMEPAD_RIGHT_TRIGGER]) @@ -414,7 +560,7 @@ static DWORD connected_gamepads(void) /* the first pad shares port 0 with the keyboard */ for (port = 1; port < count; port++) - mask |= 1UL << port; + if (gamepads[port]) mask |= 1UL << port; return mask; } @@ -496,19 +642,22 @@ DWORD WINAPI XInputGetState(HANDLE device, PXINPUT_STATE state) { struct platform_input_state input; + physical_gamepad_present = gamepads[0] != NULL; + physical_gamepad_type = physical_gamepad_present ? SDL_GetGamepadType(gamepads[0]) : SDL_GAMEPAD_TYPE_UNKNOWN; platform_input_read(&input, TRUE); mouse_poll(&input); wheel_update(); - if (!console_is_active()) + if (input.focused && !console_is_active()) keyboard_gamepad(&input, &state->Gamepad); - if (count > 0) + if (gamepads[0]) sdl_gamepad_state(gamepads[0], &state->Gamepad); test_input_gamepad(&state->Gamepad); } - else if (port < count) + else if (port < count && gamepads[port]) { sdl_gamepad_state(gamepads[port], &state->Gamepad); } + else return ERROR_DEVICE_NOT_CONNECTED; if (memcmp(&state->Gamepad, &controllers[port].previous, sizeof(state->Gamepad))) { @@ -521,24 +670,18 @@ DWORD WINAPI XInputGetState(HANDLE device, PXINPUT_STATE state) DWORD WINAPI XInputSetState(HANDLE device, PXINPUT_FEEDBACK feedback) { - int port = controller_port(device); - SDL_Gamepad *gamepads[PORT_COUNT]; - int count; - - if (!feedback) - return ERROR_INVALID_PARAMETER; - feedback->Header.dwStatus = ERROR_SUCCESS; - if (port < 0) - return ERROR_DEVICE_NOT_CONNECTED; - count = sdl_gamepads(gamepads); - if (port < count) - { - /* the game refreshes the motors every frame; rumble a little longer - than that so they do not stutter */ - SDL_RumbleGamepad(gamepads[port], feedback->Rumble.wLeftMotorSpeed, - feedback->Rumble.wRightMotorSpeed, 100); - } - return ERROR_SUCCESS; + int port=controller_port(device); SDL_Gamepad *gamepads[PORT_COUNT]; int count; + DWORD status=ERROR_DEVICE_NOT_CONNECTED; + if(!feedback) return ERROR_INVALID_PARAMETER; + if(port>=0) { + count=sdl_gamepads(gamepads); + if(portRumble.wLeftMotorSpeed,feedback->Rumble.wRightMotorSpeed,100)?ERROR_SUCCESS:50; /* ERROR_NOT_SUPPORTED */ + else if(port==0) status=50; /* Keyboard/mouse has no motors. */ + } + feedback->Header.dwStatus=status; + if(feedback->Header.hEvent) SetEvent(feedback->Header.hEvent); + return status; } DWORD WINAPI XInputDebugInitKeyboardQueue(PXINPUT_DEBUG_KEYQUEUE_PARAMETERS parameters) diff --git a/port/macos/AUDIO.md b/port/macos/AUDIO.md new file mode 100644 index 0000000000..81ed10a051 --- /dev/null +++ b/port/macos/AUDIO.md @@ -0,0 +1,55 @@ +# macOS guest audio status + +The host audio adapter uses SDL3 `SDL_OpenAudioDeviceStream` and translates the +guest's 12-byte ILP32 `SDL_AudioSpec` explicitly. A dedicated guest producer +worker attaches musl TLS and fills at most 40 ms of input PCM in 10 ms chunks. +The DirectSound guest callback retains its userdata/stream handle and advances +its buffer cursors by the requested frame count; production runs up to 40 ms +ahead of physical playback. The SDL device callback only notifies the worker +with a nonblocking trylock, so it never waits for guest mixing or scheduling. +PCM submission occurs on the worker outside SDL's device callback and outside +the binding mutex. Resume fills the initial lead before starting playback. +Shutdown pauses its logical stream, disables callbacks, joins the producer, +then destroys the stream and releases its guest stack. + +Audio is opt-in while game startup is being validated: + +```sh +HALO_AUDIO_ENABLE=1 +``` + +`halo_host_sdl_audio_test` validates S16LE stereo 22.05 kHz, guest stack +arguments/TLS, bounded production, and shutdown during a deliberately slow +100 ms guest callback. A second logical movie stream continues receiving +callbacks during the delay and after the guest stream is destroyed; its own +pause/resume is also tested. The old synchronous bridge fails that shared +device responsiveness check. The updated bridge and test pass with SDL's dummy +driver and AddressSanitizer. The fixture submits silent PCM and does not prove +subjective game or movie sound quality. + +`HALO_AUDIO_EVIDENCE=1` now measures F32 sample validity, peak/RMS level, +callback duration, callbacks exceeding their requested audio duration, and +short fills. The ordinary menu run `ordinary-menu-audio14` submitted 2,793,472 +finite samples at 48 kHz stereo with no samples above full scale; peak was +0.867825 and RMS was 0.118513. Its longest callback took 0.464 ms against a +4096-byte request duration of about 10.7 ms. It recorded no late callbacks, +short fills, or worker errors. This validates the menu's decoded float output +and bridge timing; it does not establish subjective music quality or smooth +audio during campaign loading. + +During actual intro run `bink-complete23`, the old synchronous bridge stalled +47.740 ms: producer wake delay accounted for 47.728 ms, whereas the largest +guest mixer invocation was 2.666 ms. This motivated the asynchronous producer. +Its updated evidence reports physical callback duration, producer wake and +invoke time, input queue maximum/bound, and device requests that lacked queued +data. Actual intro audio continuity and final movie drain require a fresh run. + +Actual `intro-timer24` validated the async bridge: 4,238,016 finite game-mixer +samples, no values above full scale, no short fills or device requests lacking +queued PCM, and at most 15,360 bytes of queued 48 kHz stereo F32 input (40 ms). +The longest physical SDL callback took 0.012 ms; producer wake delay reached +26.114 ms and guest mixing reached 1.289 ms. The independent movie decoder +submitted all 1,542,270 source PCM samples and closed with queued/available +bytes both zero, no drain timeout, and all 481 video frames presented. The +video still started slowly and caught up later; this audio timing evidence +does not establish smooth intro video or subjective listening quality. diff --git a/port/macos/BUILD.md b/port/macos/BUILD.md new file mode 100644 index 0000000000..c97f4b2ac3 --- /dev/null +++ b/port/macos/BUILD.md @@ -0,0 +1,15 @@ +# Apple Silicon source build + +This experimental target builds from the current checkout, including indexed working changes; it never fetches an older copy of this repository or overwrites its source with a Mac overlay. Stage newly added source inputs first. The standalone public tooling has completed its pinned-source native build and static bundle checks. Runtime acceptance exercised bundled-data startup, audio, keyboard controls and exact F10 preset cycling on macOS 26. + +Install Apple Command Line Tools and Homebrew, then run from the repository root with your own supported retail Xbox XDVDFS disc image: + +```bash +./port/macos/setup.sh "/path/to/your/Halo Xbox.iso" --jobs 4 +``` + +The helper installs build dependencies, fetches pinned XboxRecomp/musl dependencies, compiles and translates the game, runs source/native fixtures, and packages a native signed app. All maps, movies and non-system libraries are bundled. It does not download Halo or launch the game. Existing images, preferences and saves are preserved. `HALO_WORK_DIR` and `HALO_APP_OUTPUT` select fresh locations outside this checkout; default output is `~/Applications/Halo Combat Evolved.app`. + +Only indexed source bytes enter the isolated build; working edits and deletions remain intact, generated outputs/assets are excluded, and source symlinks are rejected. Provenance records the current commit and relative source hashes. Mac deployment defaults to 14; the package minimum is the maximum of its engine and library deployment requirements. F10 cycles and saves 4:3 (640×480), 720p and 1080p; F11 toggles fullscreen and F12 releases the pointer. Two same-host native instances synchronized movement on Blood Gulch and Battle Creek. Older OS support, physical wireless controllers, full mission completion, cross-platform networking and HD texture packs remain unverified. + +Original Mac tooling is MIT; the root source license and third-party notices remain unchanged. See NOTICE.md and tooling/licenses. Build CI uses synthetic headers, never downloads game data and never uploads generated translations or compiled apps. diff --git a/port/macos/CMakeLists.txt b/port/macos/CMakeLists.txt new file mode 100644 index 0000000000..345bb91a44 --- /dev/null +++ b/port/macos/CMakeLists.txt @@ -0,0 +1,297 @@ +cmake_minimum_required(VERSION 3.25) +if(NOT DEFINED CMAKE_OSX_DEPLOYMENT_TARGET) + set(CMAKE_OSX_DEPLOYMENT_TARGET "14.0" CACHE STRING "Minimum native macOS deployment version") +endif() +project(HaloMacOSSourceAOT LANGUAGES C OBJC) + +option(BUILD_TESTING "Build host unit tests" OFF) +include(CTest) +find_package(PkgConfig REQUIRED) +find_package(Python3 REQUIRED COMPONENTS Interpreter) +find_package(Threads REQUIRED) +pkg_check_modules(SDL3 REQUIRED IMPORTED_TARGET sdl3) +pkg_check_modules(FFMPEG REQUIRED IMPORTED_TARGET libavformat libavcodec libavutil libswscale libswresample) +pkg_get_variable(SDL3_INCLUDEDIR sdl3 includedir) + +set(HALO_ROOT "${CMAKE_CURRENT_LIST_DIR}/../.." CACHE PATH "Halo source root") +set(HALO_AOT_DIR "${HALO_ROOT}/build/macos-aot" CACHE PATH "i386 guest ELF build directory") +set(HALO_LIFT_DIR "${HALO_AOT_DIR}/lifted-final" CACHE PATH "Generated ARM64 AOT C directory") +set(HALO_AOT_HOST_OPTIMIZATION "1" CACHE STRING "Native generated-C optimization level (0, 1, 2, 3, s); independent of guest i386 code generation") +set_property(CACHE HALO_AOT_HOST_OPTIMIZATION PROPERTY STRINGS 0 1 2 3 s) +if(NOT HALO_AOT_HOST_OPTIMIZATION MATCHES "^[0123s]$") + message(FATAL_ERROR "HALO_AOT_HOST_OPTIMIZATION must be 0, 1, 2, 3, or s") +endif() +set(HALO_AOT_HOST_CPU "" CACHE STRING "Optional native generated-C CPU tuning, such as apple-m3; empty uses the toolchain default") +if(HALO_AOT_HOST_CPU) + include(CheckCCompilerFlag) + unset(HALO_AOT_HOST_CPU_SUPPORTED CACHE) + check_c_compiler_flag("-mcpu=${HALO_AOT_HOST_CPU}" HALO_AOT_HOST_CPU_SUPPORTED) + if(NOT HALO_AOT_HOST_CPU_SUPPORTED) + message(FATAL_ERROR "Native compiler does not support -mcpu=${HALO_AOT_HOST_CPU}") + endif() +endif() + +# Compile upstream router discovery with the native ABI; no BSD structs cross into the guest. +file(GLOB HALO_MINIUPNPC_SOURCES CONFIGURE_DEPENDS "${HALO_ROOT}/port/third_party/miniupnpc/src/*.c") +add_library(halo_miniupnpc STATIC ${HALO_MINIUPNPC_SOURCES}) +target_include_directories(halo_miniupnpc PUBLIC "${HALO_ROOT}/port/third_party/miniupnpc/include" + PRIVATE "${HALO_ROOT}/port/third_party/miniupnpc/src") +target_compile_definitions(halo_miniupnpc PRIVATE MINIUPNP_STATICLIB MINIUPNPC_SET_SOCKET_TIMEOUT MINIUPNPC_GET_SRC_ADDR) + +set(HALO_MACOS_HOST_SOURCES + guest_address.c + guest_allocator.c + guest_call.c + guest_image.c + guest_heap.c + guest_errno.c + guest_x87_80.c + recomp_state.c + recomp_runtime.c + guest_import_registry.c + guest_callback.c + guest_thread.c + gl41_compat.c + gl_bridge_support.c + gl_host_imports.c + gl_native_proc.c + host_import_bindings.c + host_imports.c + host_imports_recomp.c + host_memory_fingerprint.c + host_game_evidence.c + host_bink.c + host_sdl.c + host_app_icon.m + host_sdl_audio.c + host_services.c + host_syscall.c + posix_host_imports.c + posix_backend_files.c + posix_backend_net.c + posix_backend_upnp.c +) + +add_library(halo_macos_host STATIC ${HALO_MACOS_HOST_SOURCES}) +target_compile_features(halo_macos_host PUBLIC c_std_11) +target_compile_definitions(halo_macos_host PUBLIC HALO_MACOS=1 RECOMP_GENERATED_CODE=1) +target_include_directories(halo_macos_host PUBLIC + ${CMAKE_CURRENT_LIST_DIR} + ${HALO_ROOT}/port/linux/src + ${HALO_AOT_DIR} + ${HALO_LIFT_DIR}) +target_compile_options(halo_macos_host PRIVATE -Wall -Wextra -Werror) +target_link_libraries(halo_macos_host PUBLIC halo_miniupnpc PkgConfig::SDL3 PkgConfig::FFMPEG "-framework AppKit") + +set(HALO_GENERATED_GL_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated-gl") +set(HALO_GENERATED_GL_C "${HALO_GENERATED_GL_DIR}/gl_wrappers.c") +set(HALO_GENERATED_GL_MANIFEST "${HALO_GENERATED_GL_DIR}/gl_tokens.json") +add_custom_command( + OUTPUT "${HALO_GENERATED_GL_C}" "${HALO_GENERATED_GL_MANIFEST}" + COMMAND "${Python3_EXECUTABLE}" "${HALO_ROOT}/tools/macos_gl_bridge.py" + --gl-header "${HALO_ROOT}/port/linux/src/gl.h" + --sdl-header "${SDL3_INCLUDEDIR}/SDL3/SDL_opengl.h" + --sdl-header "${SDL3_INCLUDEDIR}/SDL3/SDL_opengl_glext.h" + --output "${HALO_GENERATED_GL_MANIFEST}" + --c-output "${HALO_GENERATED_GL_C}" + DEPENDS "${HALO_ROOT}/tools/macos_gl_bridge.py" "${HALO_ROOT}/port/linux/src/gl.h" + "${SDL3_INCLUDEDIR}/SDL3/SDL_opengl.h" "${SDL3_INCLUDEDIR}/SDL3/SDL_opengl_glext.h" + VERBATIM) +add_library(halo_macos_gl_wrappers OBJECT "${HALO_GENERATED_GL_C}") +target_compile_features(halo_macos_gl_wrappers PRIVATE c_std_11) +target_compile_definitions(halo_macos_gl_wrappers PRIVATE HALO_MACOS=1 RECOMP_GENERATED_CODE=1) +target_include_directories(halo_macos_gl_wrappers PRIVATE + ${HALO_LIFT_DIR} ${HALO_AOT_DIR} ${CMAKE_CURRENT_LIST_DIR}) +target_link_libraries(halo_macos_gl_wrappers PRIVATE PkgConfig::SDL3) + +file(GLOB HALO_LIFT_SHARDS CONFIGURE_DEPENDS "${HALO_LIFT_DIR}/recomp_[0-9][0-9][0-9][0-9].c") +if(HALO_LIFT_SHARDS) + add_library(halo_macos_guest OBJECT + ${HALO_LIFT_SHARDS} + ${HALO_LIFT_DIR}/recomp_dispatch.c + ${HALO_LIFT_DIR}/recomp_manual_dispatch.c + ${HALO_LIFT_DIR}/recomp_import_aliases.c + ${HALO_LIFT_DIR}/recomp_stubs_unresolved.c) + target_compile_features(halo_macos_guest PRIVATE c_std_11) + target_include_directories(halo_macos_guest PRIVATE ${HALO_LIFT_DIR} ${CMAKE_CURRENT_LIST_DIR}) + target_compile_options(halo_macos_guest PRIVATE "-O${HALO_AOT_HOST_OPTIMIZATION}" -Wno-parentheses-equality) + if(HALO_AOT_HOST_CPU) + target_compile_options(halo_macos_guest PRIVATE "-mcpu=${HALO_AOT_HOST_CPU}") + endif() + add_executable(halo_macos_aot main.c + $ + $) + target_include_directories(halo_macos_aot PRIVATE ${HALO_LIFT_DIR} ${HALO_AOT_DIR}) + target_compile_features(halo_macos_aot PRIVATE c_std_11) + target_compile_options(halo_macos_aot PRIVATE -Wall -Wextra -Werror) + target_link_libraries(halo_macos_aot PRIVATE halo_macos_host PkgConfig::SDL3 m) + # Archive members can be silently omitted if a guest alias satisfies the + # same native symbol. Check even omitted members before the final link. + add_custom_command(TARGET halo_macos_aot PRE_LINK + COMMAND "${Python3_EXECUTABLE}" "${HALO_ROOT}/tools/check_macos_symbol_namespaces.py" + --lift-dir "${HALO_LIFT_DIR}" + --archive "$" + --archive "$" + VERBATIM) +endif() + +if(BUILD_TESTING) + add_test(NAME halo_multiplayer_harness_guards + COMMAND "${Python3_EXECUTABLE}" "${HALO_ROOT}/tools/test_macos_multiplayer_harness.py") + add_test(NAME halo_symbol_namespaces + COMMAND "${Python3_EXECUTABLE}" "${HALO_ROOT}/tools/test_macos_symbol_namespaces.py") + add_executable(halo_gl41_copy_namespace_test gl41_compat.c tests/gl41_compat_test.c) + target_compile_features(halo_gl41_copy_namespace_test PRIVATE c_std_11) + target_compile_options(halo_gl41_copy_namespace_test PRIVATE -Wall -Wextra -Werror -UNDEBUG) + add_test(NAME halo_gl41_copy_namespace_test COMMAND halo_gl41_copy_namespace_test) + add_executable(halo_gl_copy_bridge_test tests/gl_copy_bridge_test.c + $) + target_link_libraries(halo_gl_copy_bridge_test PRIVATE halo_macos_host "-framework OpenGL") + target_compile_options(halo_gl_copy_bridge_test PRIVATE -Wall -Wextra -Werror -UNDEBUG) + add_test(NAME halo_gl_copy_bridge_test COMMAND halo_gl_copy_bridge_test) + add_executable(halo_input_bindings_test tests/input_bindings_test.c "${HALO_ROOT}/port/linux/src/input_bindings.c") + target_link_libraries(halo_input_bindings_test PRIVATE PkgConfig::SDL3) + target_compile_options(halo_input_bindings_test PRIVATE -Wall -Wextra -Werror -UNDEBUG) + add_test(NAME halo_input_bindings_test COMMAND halo_input_bindings_test) + + add_executable(halo_host_bink_codec_test + guest_address.c guest_call.c host_bink.c tests/host_bink_codec_test.c) + target_include_directories(halo_host_bink_codec_test PRIVATE ${CMAKE_CURRENT_LIST_DIR}) + target_compile_features(halo_host_bink_codec_test PRIVATE c_std_11) + target_compile_options(halo_host_bink_codec_test PRIVATE -Wall -Wextra -Werror -UNDEBUG) + target_link_libraries(halo_host_bink_codec_test PRIVATE PkgConfig::SDL3 PkgConfig::FFMPEG) + # The fixture consumes user-owned movie data only when its path is supplied. + set(HALO_BINK_TEST_MOVIE "" CACHE FILEPATH "Optional original intro.bik for native decoder verification") + if(HALO_BINK_TEST_MOVIE) + add_test(NAME halo_host_bink_codec_test COMMAND halo_host_bink_codec_test "${HALO_BINK_TEST_MOVIE}") + endif() + + add_executable(halo_host_memory_fingerprint_test + guest_address.c guest_call.c host_memory_fingerprint.c tests/host_memory_fingerprint_test.c) + target_include_directories(halo_host_memory_fingerprint_test PRIVATE ${CMAKE_CURRENT_LIST_DIR}) + target_compile_features(halo_host_memory_fingerprint_test PRIVATE c_std_11) + target_compile_options(halo_host_memory_fingerprint_test PRIVATE -Wall -Wextra -Werror -UNDEBUG) + add_test(NAME halo_host_memory_fingerprint_test COMMAND halo_host_memory_fingerprint_test) + add_executable(halo_gl_native_proc_cache_test gl_native_proc.c tests/gl_native_proc_cache_test.c) + target_include_directories(halo_gl_native_proc_cache_test PRIVATE ${CMAKE_CURRENT_LIST_DIR}) + target_compile_options(halo_gl_native_proc_cache_test PRIVATE -Wall -Wextra -Werror -UNDEBUG) + target_link_libraries(halo_gl_native_proc_cache_test PRIVATE PkgConfig::SDL3) + add_test(NAME halo_gl_native_proc_cache_test COMMAND halo_gl_native_proc_cache_test) + + add_executable(halo_host_frame_metrics_test tests/host_frame_metrics_test.c) + target_include_directories(halo_host_frame_metrics_test PRIVATE ${CMAKE_CURRENT_LIST_DIR}) + target_compile_features(halo_host_frame_metrics_test PRIVATE c_std_11) + target_compile_options(halo_host_frame_metrics_test PRIVATE -Wall -Wextra -Werror -UNDEBUG) + add_test(NAME halo_host_frame_metrics_test COMMAND halo_host_frame_metrics_test) + + add_executable(halo_gl_shader_source_test + guest_address.c guest_allocator.c guest_call.c gl_bridge_support.c + tests/gl_shader_source_test.c) + target_include_directories(halo_gl_shader_source_test PRIVATE ${CMAKE_CURRENT_LIST_DIR}) + target_compile_features(halo_gl_shader_source_test PRIVATE c_std_11) + target_compile_options(halo_gl_shader_source_test PRIVATE -Wall -Wextra -Werror -UNDEBUG) + target_link_libraries(halo_gl_shader_source_test PRIVATE PkgConfig::SDL3) + add_test(NAME halo_gl_shader_source_test COMMAND halo_gl_shader_source_test) + + add_executable(halo_gl_bridge_wrapper_test + guest_address.c guest_call.c tests/gl_bridge_wrapper_test.c + "${HALO_GENERATED_GL_C}") + target_include_directories(halo_gl_bridge_wrapper_test PRIVATE + ${HALO_LIFT_DIR} ${HALO_AOT_DIR} ${CMAKE_CURRENT_LIST_DIR}) + target_compile_features(halo_gl_bridge_wrapper_test PRIVATE c_std_11) + target_compile_definitions(halo_gl_bridge_wrapper_test PRIVATE + HALO_MACOS=1 RECOMP_GENERATED_CODE=1) + target_compile_options(halo_gl_bridge_wrapper_test PRIVATE + -Wall -Wextra -Werror -UNDEBUG) + target_link_libraries(halo_gl_bridge_wrapper_test PRIVATE PkgConfig::SDL3) + add_test(NAME halo_gl_bridge_wrapper_test COMMAND halo_gl_bridge_wrapper_test) + + add_executable(halo_posix_host_imports_test + guest_address.c guest_call.c posix_host_imports.c + posix_backend_files.c posix_backend_net.c posix_backend_upnp.c + tests/posix_host_imports_test.c) + target_include_directories(halo_posix_host_imports_test PRIVATE + ${CMAKE_CURRENT_LIST_DIR} ${HALO_ROOT}/port/linux/src) + target_compile_features(halo_posix_host_imports_test PRIVATE c_std_11) + target_compile_definitions(halo_posix_host_imports_test PRIVATE + _POSIX_C_SOURCE=200809L _DARWIN_C_SOURCE=1) + target_compile_options(halo_posix_host_imports_test PRIVATE + -Wall -Wextra -Werror -UNDEBUG) + target_link_libraries(halo_posix_host_imports_test PRIVATE halo_miniupnpc) + add_test(NAME halo_posix_host_imports_test COMMAND halo_posix_host_imports_test) + add_test(NAME halo_posix_local_alias_test COMMAND halo_posix_host_imports_test --alias) + add_test(NAME halo_posix_alias_online_guard_test COMMAND halo_posix_host_imports_test --online) + add_test(NAME halo_posix_alias_invalid_ip_test COMMAND halo_posix_host_imports_test --invalid-ip) + add_test(NAME halo_posix_alias_collision_test COMMAND halo_posix_host_imports_test --collision) + + add_executable(halo_guest_image_test guest_address.c guest_image.c tests/guest_image_test.c) + target_include_directories(halo_guest_image_test PRIVATE ${CMAKE_CURRENT_LIST_DIR}) + target_compile_features(halo_guest_image_test PRIVATE c_std_11) + target_compile_options(halo_guest_image_test PRIVATE -Wall -Wextra -Werror -pedantic -UNDEBUG) + add_test(NAME halo_guest_image_test COMMAND halo_guest_image_test) + + add_executable(halo_memory_watch_fallback_test + ${HALO_ROOT}/port/linux/src/memory_watch.c + tests/memory_watch_fallback_test.c) + target_compile_definitions(halo_memory_watch_fallback_test PRIVATE HALO_MACOS=1 HALO_MEMORY_WATCH_TEST=1) + target_include_directories(halo_memory_watch_fallback_test PRIVATE + ${CMAKE_CURRENT_LIST_DIR}/tests ${HALO_ROOT}/port/linux/src) + target_compile_features(halo_memory_watch_fallback_test PRIVATE c_std_11) + target_compile_options(halo_memory_watch_fallback_test PRIVATE -Wall -Wextra -Werror -UNDEBUG) + add_test(NAME halo_memory_watch_fallback_test COMMAND halo_memory_watch_fallback_test) + + add_executable(halo_guest_heap_test + guest_address.c guest_allocator.c guest_call.c guest_heap.c + host_syscall.c host_services.c tests/guest_heap_test.c) + target_include_directories(halo_guest_heap_test PRIVATE ${CMAKE_CURRENT_LIST_DIR}) + target_compile_features(halo_guest_heap_test PRIVATE c_std_11) + target_compile_options(halo_guest_heap_test PRIVATE -Wall -Wextra -Werror -UNDEBUG) + add_test(NAME halo_guest_heap_test COMMAND halo_guest_heap_test) + + add_executable(halo_host_services_test + guest_address.c guest_call.c host_services.c tests/host_services_test.c) + add_executable(halo_host_syscall_test + guest_address.c guest_allocator.c guest_call.c host_services.c + host_syscall.c tests/host_syscall_test.c) + foreach(clock_test halo_host_services_test halo_host_syscall_test) + target_include_directories(${clock_test} PRIVATE ${CMAKE_CURRENT_LIST_DIR}) + target_compile_features(${clock_test} PRIVATE c_std_11) + target_compile_options(${clock_test} PRIVATE -Wall -Wextra -Werror -UNDEBUG) + add_test(NAME ${clock_test} COMMAND ${clock_test}) + endforeach() + + add_executable(halo_guest_x87_80_test + guest_x87_80.c tests/guest_x87_80_test.c) + target_include_directories(halo_guest_x87_80_test PRIVATE ${CMAKE_CURRENT_LIST_DIR}) + target_compile_features(halo_guest_x87_80_test PRIVATE c_std_11) + target_compile_options(halo_guest_x87_80_test PRIVATE -Wall -Wextra -Werror -UNDEBUG) + add_test(NAME halo_guest_x87_80_test COMMAND halo_guest_x87_80_test) + + add_executable(halo_guest_callback_test + guest_address.c guest_allocator.c guest_call.c host_services.c + guest_callback.c tests/guest_callback_test.c) + target_include_directories(halo_guest_callback_test PRIVATE ${CMAKE_CURRENT_LIST_DIR}) + target_compile_features(halo_guest_callback_test PRIVATE c_std_11) + target_compile_options(halo_guest_callback_test PRIVATE -Wall -Wextra -Werror -UNDEBUG) + target_link_libraries(halo_guest_callback_test PRIVATE PkgConfig::SDL3) + add_test(NAME halo_guest_callback_test COMMAND halo_guest_callback_test) + + add_executable(halo_recomp_state_test recomp_state.c tests/recomp_state_test.c) + target_include_directories(halo_recomp_state_test PRIVATE ${HALO_LIFT_DIR} ${CMAKE_CURRENT_LIST_DIR}) + target_compile_features(halo_recomp_state_test PRIVATE c_std_11) + target_compile_definitions(halo_recomp_state_test PRIVATE RECOMP_GENERATED_CODE=1) + target_compile_options(halo_recomp_state_test PRIVATE -Wall -Wextra -Werror -UNDEBUG) + add_test(NAME halo_recomp_state_test COMMAND halo_recomp_state_test) + + add_executable(halo_host_sdl_audio_test + guest_address.c guest_allocator.c guest_call.c host_services.c + guest_callback.c host_sdl_audio.c tests/host_sdl_audio_test.c) + target_include_directories(halo_host_sdl_audio_test PRIVATE ${CMAKE_CURRENT_LIST_DIR}) + target_compile_features(halo_host_sdl_audio_test PRIVATE c_std_11) + target_compile_definitions(halo_host_sdl_audio_test PRIVATE + _POSIX_C_SOURCE=200809L _DARWIN_C_SOURCE=1) + target_compile_options(halo_host_sdl_audio_test PRIVATE -Wall -Wextra -Werror -UNDEBUG) + target_link_libraries(halo_host_sdl_audio_test PRIVATE PkgConfig::SDL3 Threads::Threads) + add_test(NAME halo_host_sdl_audio_test COMMAND halo_host_sdl_audio_test) + set_tests_properties(halo_host_sdl_audio_test PROPERTIES TIMEOUT 10) +endif() diff --git a/port/macos/COMPATIBILITY.md b/port/macos/COMPATIBILITY.md new file mode 100644 index 0000000000..7a993259f4 --- /dev/null +++ b/port/macos/COMPATIBILITY.md @@ -0,0 +1,105 @@ +# Native macOS compatibility + +The selected game, network and renderer updates are taken from +[cybersecurity/halo-ce-universal](https://github.com/cybersecurity/halo-ce-universal) +main commit `0ef2ed7dc7a96299ea7d8d02a1bc3f9fb1c231af` (checked 2026-09-29). +The previous source snapshot was `dddaffd4`. The three-way integration record +and preserved local files are in `build/upstream-merge-0ef2ed7d/`. + +## Multiplayer + +This source uses upstream network protocol **4**, including distributed +simulation, joining a game in progress, matching host player datum slots, +and PAL map normalization. It retains the original guest packet encoding, +32-bit game records, 30 Hz simulation, map checks and network version checks. +It does not substitute host C structs for game records. + +Upstream defines a maximum of **128 players on 128 machines**. That is a +source limit, not a test result for 128 active clients. Native tests have +passed TCP and UDP localhost payloads through the actual guest import ABI, +including Linux/Xbox IPv4 addresses converted to Darwin, accepted peer and +received source addresses, select read/write lists, and checked output bounds. +These tests do not establish a successful game join or cross-platform session. +A matching protocol-4 counterpart and game-level host/join test are still +required before claiming verified crossplay. Older protocol-1 builds are +incompatible; the normal upstream version mismatch message is preserved. + +For local system link testing, use `network.online = false`. Upstream's +`debug.network_test = "host:bloodgulch"` and `"join"` automate real sessions. +The native bridge includes upstream miniupnpc router discovery/UDP mapping. +`network.allow_upnp = false` prevents requests to a router; enable it only +when desired. No router forwarding or internet/NAT traversal has been verified +on this machine. + +Five musl process-spawn imports remain explicitly unbound: `posix_spawn` and +its four file-action helpers. The current Mac platform excludes the Linux +URL installer/updater command paths that would use them. They are not dummy +success implementations. Native URL scheme registration and command-line +invite discovery also remain unavailable through this bridge; do not promise +invite-link installation or Linux updater compatibility on macOS. + +## Maps, mods and visuals + +The port keeps upstream map/tag loading and game data layouts. It includes +upstream PAL tag normalization and its gameplay, HUD, grenade and renderer +fixes. This is compatibility with the upstream game's data model, not support +for arbitrary Halo PC/Custom Edition DLL plugins, SAPP, Windows executables, +or every modded map. Compatible map/tag content still has to pass the same +upstream format/version checks and be tested with the matching session data. + +Existing settings control display refresh interpolation (enabled by default), +fullscreen, window scale and the Mac internal `display.render_width` option. +Interpolation draws between 30 Hz game ticks; it does not change the network +tick rate. Renderer updates preserve the Mac OpenGL presentation path. +There is no newly claimed shader-mod or PC plugin API. + +Measured native performance has reached 59.5–60 FPS outside the A10 cryo +chamber, but the chamber itself can render near 30 FPS. This is a scene +performance limit, not proof of steady 60 FPS everywhere. With interpolation +and vsync enabled, the source bypasses the Xbox throttle, pending-flip wait, +and 30 Hz render selection. There is one SDL swap per presented frame. +CPU work that misses a display deadline can move presentation to the next +refresh; a 15–16 ms swap alone does not prove a second pacing wait. No +speculative timer or simulation-rate change was made to hide this limit. + + +## Mac controls + +The keyboard and mouse drive player one, alongside an optional SDL gamepad. +Default controls: WASD movement, mouse look, Space jump, left mouse fire, +right mouse/Z/middle mouse zoom, G grenade, E or R context action/reload, +F or Mouse4 melee, Tab/wheel weapon switch, X grenade type, Q flashlight, +and Left Ctrl/C crouch. Arrow keys and Return navigate/confirm menus; +Backspace goes back; Escape pauses; F11 toggles fullscreen; F12 releases or +recaptures the mouse; Command-Q uses the game's normal exit path. + +`config.toml` has `[input]` string entries `forward`, `backward`, `left`, +`right`, `jump`, `fire`, `zoom`, `grenade`, `action`, `melee`, `change_weapon`, +`change_grenade`, `flashlight`, and `crouch`. For example: + +```toml +[input] +action = "E,R" +zoom = "Mouse2,Z" +grenade = "G" +mouse_sensitivity = 1.0 +invert_mouse = false +``` + +Bindings accept comma-separated physical letters, digits, Space, Tab, +Enter, Escape, Backspace, LeftCtrl/Ctrl, LeftShift/Shift, LeftAlt/Alt, +Up/Down/Left/Right, Mouse1 through Mouse5, and Wheel for weapon switching. +An empty string disables an action. Invalid values log a warning and use +the default. Settings are read at startup; restart after editing. Gamepad +button preferences remain game preferences; keyboard actions retain their +original controller action semantics (interaction and reload share X). + +On focus loss, key presses, mouse buttons, accumulated motion and wheel input +are cleared and cursor capture is released. Capture resumes on focus regain +when desired by the player. HUD control icons display the configured first +key/mouse binding, Mouse for look and WASD for movement; a connected physical +gamepad retains the original controller icons. Original map assets are not edited. + +The x87 runtime models registers as host doubles. Exact zero and ordinary +parser examples are tested, but the original binary80 extra precision is not +fully preserved. This existing translation limit is not a networking format change. diff --git a/port/macos/GL_BRIDGE.md b/port/macos/GL_BRIDGE.md new file mode 100644 index 0000000000..f9b72ba395 --- /dev/null +++ b/port/macos/GL_BRIDGE.md @@ -0,0 +1,37 @@ +# Native GL procedure bridge + +`tools/macos_gl_bridge.py` reads the desktop `GL_FUNCTIONS` list and the SDL3 +OpenGL prototypes. It emits deterministic guest procedure tokens starting at +`0xFE100000` with a `0x10` stride, typed wrappers named `sub_`, and the +name/token/wrapper lookup functions. Host pointers never enter guest memory. + +Each generated wrapper decodes 32-bit i686 cdecl stack slots using +`mac_guest_stack_arg32`, preserves float bit patterns, reconstructs guest +`GLdouble`, widens guest 32-bit `GLsizeiptr`/`GLintptr`, writes scalar returns +to `g_eax`, and advances `g_esp` by the cdecl return-address pop. The host must +implement `mac_macos_gl_native_proc()` with the current SDL GL context's proc +resolver and register the generated wrapper lookup with `recomp_lookup_manual`. + +`gl_bridge_support.c` resolves guest pointer spans explicitly. Image transfers +account for GL pixel format/type and pack/unpack alignment, row/image length, +and skip state. Buffer and index pointers are treated as offsets only when the +corresponding GL buffer is bound. Shader source vectors are translated from +guest 32-bit pointers into host pointer vectors. Unknown pointer signatures +fail closed. `glGetString` copies each requested string into a persistent guest +allocation provided by `mac_guest_allocate`; it never narrows a host pointer or +reserves a guessed guest VA. `glMapBufferRange` pointer results fail unless a +guest mapping exists. + +Example generation and checks on a host with SDL3 headers: + +```sh +python3 tools/macos_gl_bridge.py \ + --gl-header port/linux/src/gl.h \ + --sdl-header "$SDL3_INCLUDE_DIR/SDL3/SDL_opengl.h" \ + --sdl-header "$SDL3_INCLUDE_DIR/SDL3/SDL_opengl_glext.h" \ + --output /tmp/macos-gl-tokens.json \ + --c-output /tmp/macos_gl_token_wrappers.c +``` + +The generated C belongs only in the native AOT target, which supplies its +generated `recomp_types.h`; it is not part of the ordinary 32-bit source build. diff --git a/port/macos/LICENSE b/port/macos/LICENSE new file mode 100644 index 0000000000..32fb6be8d3 --- /dev/null +++ b/port/macos/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 halo-ce-apple-silicon contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/port/macos/NOTICE.md b/port/macos/NOTICE.md new file mode 100644 index 0000000000..d0f696b1d6 --- /dev/null +++ b/port/macos/NOTICE.md @@ -0,0 +1,5 @@ +The MIT license in port/macos/LICENSE applies to independently authored macOS host/build glue, original host fixtures and Mac documentation, plus the original added macos_* and test/check/run_macos_* tools and portable guest compilation helper. It does not relicense existing source, upstream patch context, Halo data, Xbox SDK material, artwork or screenshots. Existing source files keep the root CC0 license and retained third-party notices. + +XboxRecomp is fetched at the public pin in tooling/pins.json. The runtime patch modifies its MIT-licensed translator; tooling/licenses/XboxRecomp-MIT.txt preserves the upstream copyright. Bundled compiler-rt keeps its Apache 2.0 WITH LLVM-exception license. SHA-verified musl retains tooling/licenses/musl.txt. Upstream vendors and Homebrew libraries keep their own licenses; they are not covered by this MIT file. + +The generated helmet icon and prompt are fan artwork, excluded from the original-code MIT grant. This unofficial project is not endorsed by Microsoft, Bungie or Xbox. Game images/data and generated translation are not committed or uploaded. Build from your own supported retail Xbox image; setup never downloads Halo. diff --git a/port/macos/README.md b/port/macos/README.md new file mode 100644 index 0000000000..a85af98917 --- /dev/null +++ b/port/macos/README.md @@ -0,0 +1,44 @@ +# Halo on macOS + +Open **Halo Combat Evolved.app** from the build directory. Keep the original +game data in the configured data folder. The game uses the original Halo maps +and audio; this port does not distribute them. + +Settings are saved at +`~/Library/Application Support/Halo Native AOT/save/config.toml`. +Quit the game, edit the file, then reopen it. Existing display settings control +fullscreen, window scale, interpolation and internal render width. F11 switches +fullscreen while playing. + +| Action | Default keys | +|---|---| +| Move / look | WASD / mouse | +| Jump / fire | Space / left mouse | +| Zoom / grenade | Right mouse, Z or middle mouse / G | +| Use or reload | E or R (context sensitive) | +| Melee | F or Mouse4 | +| Change weapon / grenade type | Tab or wheel / X | +| Crouch / flashlight | Left Ctrl or C / Q | +| Pause | Escape | +| Menu navigation / confirm / back | Arrows / Return / Backspace | +| Release mouse / fullscreen | F12 / F11 | +| Quit normally | Command-Q | + +Focus loss releases the mouse and clears held keys/buttons. Click or focus the +game again to resume. The pointer works in menus; mouse look applies in game. + +To rebind, edit the `[input]` action strings, such as `jump = "Space"`, +`action = "E,R"`, `zoom = "Mouse2,Z"` or `grenade = "G"`. A comma separates +alternatives. `Mouse1` is left, `Mouse2` right, `Mouse3` middle; `Wheel` switches +weapons. `mouse_sensitivity` defaults to `1.0`; `invert_mouse` defaults to +`false`. An empty action string disables it. Tutorial control labels follow +keyboard bindings; connecting a gamepad retains controller icons. + +Multiplayer source follows upstream protocol 4. Local system link uses +`network.online = false`. Router forwarding is controlled by +`network.allow_upnp`; set it to `false` to prevent UPnP requests. The upstream +128-player limit has not been tested with 128 clients on this Mac. Read +[compatibility notes](COMPATIBILITY.md) for the verified network +bridge tests and current crossplay, map/mod and invite-link limitations. + +For rebuilding and diagnostics, see [Apple Silicon build instructions](https://github.com/pkyanam/halo-ce-apple-silicon#readme). diff --git a/port/macos/compiler-rt/LICENSE.TXT b/port/macos/compiler-rt/LICENSE.TXT new file mode 100644 index 0000000000..5a79a1b9d5 --- /dev/null +++ b/port/macos/compiler-rt/LICENSE.TXT @@ -0,0 +1,311 @@ +============================================================================== +The LLVM Project is under the Apache License v2.0 with LLVM Exceptions: +============================================================================== + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + +---- LLVM Exceptions to the Apache 2.0 License ---- + +As an exception, if, as a result of your compiling your source code, portions +of this Software are embedded into an Object form of such source code, you +may redistribute such embedded portions in such Object form without complying +with the conditions of Sections 4(a), 4(b) and 4(d) of the License. + +In addition, if you combine or link compiled forms of this Software with +software that is licensed under the GPLv2 ("Combined Software") and if a +court of competent jurisdiction determines that the patent provision (Section +3), the indemnity provision (Section 9) or other Section of the License +conflicts with the conditions of the GPLv2, you may retroactively and +prospectively choose to deem waived or otherwise exclude such Section(s) of +the License, but only in their entirety and only with respect to the Combined +Software. + +============================================================================== +Software from third parties included in the LLVM Project: +============================================================================== +The LLVM Project contains third party software which is under different license +terms. All such code will be identified clearly using at least one of two +mechanisms: +1) It will be in a separate directory tree with its own `LICENSE.txt` or + `LICENSE` file at the top containing the specific license and restrictions + which apply to that software, or +2) It will contain specific license and restriction terms at the top of every + file. + +============================================================================== +Legacy LLVM License (https://llvm.org/docs/DeveloperPolicy.html#legacy): +============================================================================== + +The compiler_rt library is dual licensed under both the University of Illinois +"BSD-Like" license and the MIT license. As a user of this code you may choose +to use it under either license. As a contributor, you agree to allow your code +to be used under both. + +Full text of the relevant licenses is included below. + +============================================================================== + +University of Illinois/NCSA +Open Source License + +Copyright (c) 2009-2019 by the contributors listed in CREDITS.TXT + +All rights reserved. + +Developed by: + + LLVM Team + + University of Illinois at Urbana-Champaign + + http://llvm.org + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal with +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimers. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimers in the + documentation and/or other materials provided with the distribution. + + * Neither the names of the LLVM Team, University of Illinois at + Urbana-Champaign, nor the names of its contributors may be used to + endorse or promote products derived from this Software without specific + prior written permission. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE +SOFTWARE. + +============================================================================== + +Copyright (c) 2009-2015 by the contributors listed in CREDITS.TXT + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/port/macos/compiler-rt/divdi3.c b/port/macos/compiler-rt/divdi3.c new file mode 100644 index 0000000000..d71e138d99 --- /dev/null +++ b/port/macos/compiler-rt/divdi3.c @@ -0,0 +1,22 @@ +//===-- divdi3.c - Implement __divdi3 -------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file implements __divdi3 for the compiler_rt library. +// +//===----------------------------------------------------------------------===// + +#include "int_lib.h" + +// Returns: a / b + +#define fixint_t di_int +#define fixuint_t du_int +#define COMPUTE_UDIV(a, b) __udivmoddi4((a), (b), (du_int *)0) +#include "int_div_impl.inc" + +COMPILER_RT_ABI di_int __divdi3(di_int a, di_int b) { return __divXi3(a, b); } diff --git a/port/macos/compiler-rt/int_div_impl.inc b/port/macos/compiler-rt/int_div_impl.inc new file mode 100644 index 0000000000..dc1f97cbea --- /dev/null +++ b/port/macos/compiler-rt/int_div_impl.inc @@ -0,0 +1,95 @@ +//===-- int_div_impl.inc - Integer division ---------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// Helpers used by __udivsi3, __umodsi3, __udivdi3, and __umodsi3. +// +//===----------------------------------------------------------------------===// + +#define clz(a) (sizeof(a) == sizeof(unsigned long long) ? __builtin_clzll(a) : clzsi(a)) + +// Adapted from Figure 3-40 of The PowerPC Compiler Writer's Guide +static __inline fixuint_t __udivXi3(fixuint_t n, fixuint_t d) { + const unsigned N = sizeof(fixuint_t) * CHAR_BIT; + // d == 0 cases are unspecified. + unsigned sr = (d ? clz(d) : N) - (n ? clz(n) : N); + // 0 <= sr <= N - 1 or sr is very large. + if (sr > N - 1) // n < d + return 0; + if (sr == N - 1) // d == 1 + return n; + ++sr; + // 1 <= sr <= N - 1. Shifts do not trigger UB. + fixuint_t r = n >> sr; + n <<= N - sr; + fixuint_t carry = 0; + for (; sr > 0; --sr) { + r = (r << 1) | (n >> (N - 1)); + n = (n << 1) | carry; + // Branch-less version of: + // carry = 0; + // if (r >= d) r -= d, carry = 1; + const fixint_t s = (fixint_t)(d - r - 1) >> (N - 1); + carry = s & 1; + r -= d & s; + } + n = (n << 1) | carry; + return n; +} + +// Mostly identical to __udivXi3 but the return values are different. +static __inline fixuint_t __umodXi3(fixuint_t n, fixuint_t d) { + const unsigned N = sizeof(fixuint_t) * CHAR_BIT; + // d == 0 cases are unspecified. + unsigned sr = (d ? clz(d) : N) - (n ? clz(n) : N); + // 0 <= sr <= N - 1 or sr is very large. + if (sr > N - 1) // n < d + return n; + if (sr == N - 1) // d == 1 + return 0; + ++sr; + // 1 <= sr <= N - 1. Shifts do not trigger UB. + fixuint_t r = n >> sr; + n <<= N - sr; + fixuint_t carry = 0; + for (; sr > 0; --sr) { + r = (r << 1) | (n >> (N - 1)); + n = (n << 1) | carry; + // Branch-less version of: + // carry = 0; + // if (r >= d) r -= d, carry = 1; + const fixint_t s = (fixint_t)(d - r - 1) >> (N - 1); + carry = s & 1; + r -= d & s; + } + return r; +} + +#ifdef COMPUTE_UDIV +static __inline fixint_t __divXi3(fixint_t a, fixint_t b) { + const int N = (int)(sizeof(fixint_t) * CHAR_BIT) - 1; + fixint_t s_a = a >> N; // s_a = a < 0 ? -1 : 0 + fixint_t s_b = b >> N; // s_b = b < 0 ? -1 : 0 + fixuint_t a_u = (fixuint_t)(a ^ s_a) + (-s_a); // negate if s_a == -1 + fixuint_t b_u = (fixuint_t)(b ^ s_b) + (-s_b); // negate if s_b == -1 + s_a ^= s_b; // sign of quotient + return (COMPUTE_UDIV(a_u, b_u) ^ s_a) + (-s_a); // negate if s_a == -1 +} +#endif // COMPUTE_UDIV + +#ifdef ASSIGN_UMOD +static __inline fixint_t __modXi3(fixint_t a, fixint_t b) { + const int N = (int)(sizeof(fixint_t) * CHAR_BIT) - 1; + fixint_t s = b >> N; // s = b < 0 ? -1 : 0 + fixuint_t b_u = (fixuint_t)(b ^ s) + (-s); // negate if s == -1 + s = a >> N; // s = a < 0 ? -1 : 0 + fixuint_t a_u = (fixuint_t)(a ^ s) + (-s); // negate if s == -1 + fixuint_t res; + ASSIGN_UMOD(res, a_u, b_u); + return (res ^ s) + (-s); // negate if s == -1 +} +#endif // ASSIGN_UMOD diff --git a/port/macos/compiler-rt/int_endianness.h b/port/macos/compiler-rt/int_endianness.h new file mode 100644 index 0000000000..291c6b58c8 --- /dev/null +++ b/port/macos/compiler-rt/int_endianness.h @@ -0,0 +1,114 @@ +//===-- int_endianness.h - configuration header for compiler-rt -----------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file is a configuration header for compiler-rt. +// This file is not part of the interface of this library. +// +//===----------------------------------------------------------------------===// + +#ifndef INT_ENDIANNESS_H +#define INT_ENDIANNESS_H + +#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \ + defined(__ORDER_LITTLE_ENDIAN__) + +// Clang and GCC provide built-in endianness definitions. +#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ +#define _YUGA_LITTLE_ENDIAN 0 +#define _YUGA_BIG_ENDIAN 1 +#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ +#define _YUGA_LITTLE_ENDIAN 1 +#define _YUGA_BIG_ENDIAN 0 +#endif // __BYTE_ORDER__ + +#else // Compilers other than Clang or GCC. + +#if defined(__SVR4) && defined(__sun) +#include + +#if defined(_BIG_ENDIAN) +#define _YUGA_LITTLE_ENDIAN 0 +#define _YUGA_BIG_ENDIAN 1 +#elif defined(_LITTLE_ENDIAN) +#define _YUGA_LITTLE_ENDIAN 1 +#define _YUGA_BIG_ENDIAN 0 +#else // !_LITTLE_ENDIAN +#error "unknown endianness" +#endif // !_LITTLE_ENDIAN + +#endif // Solaris + +// .. + +#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || \ + defined(__minix) +#include + +#if _BYTE_ORDER == _BIG_ENDIAN +#define _YUGA_LITTLE_ENDIAN 0 +#define _YUGA_BIG_ENDIAN 1 +#elif _BYTE_ORDER == _LITTLE_ENDIAN +#define _YUGA_LITTLE_ENDIAN 1 +#define _YUGA_BIG_ENDIAN 0 +#endif // _BYTE_ORDER + +#endif // *BSD + +#if defined(__OpenBSD__) +#include + +#if _BYTE_ORDER == _BIG_ENDIAN +#define _YUGA_LITTLE_ENDIAN 0 +#define _YUGA_BIG_ENDIAN 1 +#elif _BYTE_ORDER == _LITTLE_ENDIAN +#define _YUGA_LITTLE_ENDIAN 1 +#define _YUGA_BIG_ENDIAN 0 +#endif // _BYTE_ORDER + +#endif // OpenBSD + +// .. + +// Mac OSX has __BIG_ENDIAN__ or __LITTLE_ENDIAN__ automatically set by the +// compiler (at least with GCC) +#if defined(__APPLE__) || defined(__ellcc__) + +#ifdef __BIG_ENDIAN__ +#if __BIG_ENDIAN__ +#define _YUGA_LITTLE_ENDIAN 0 +#define _YUGA_BIG_ENDIAN 1 +#endif +#endif // __BIG_ENDIAN__ + +#ifdef __LITTLE_ENDIAN__ +#if __LITTLE_ENDIAN__ +#define _YUGA_LITTLE_ENDIAN 1 +#define _YUGA_BIG_ENDIAN 0 +#endif +#endif // __LITTLE_ENDIAN__ + +#endif // Mac OSX + +// .. + +#if defined(_WIN32) + +#define _YUGA_LITTLE_ENDIAN 1 +#define _YUGA_BIG_ENDIAN 0 + +#endif // Windows + +#endif // Clang or GCC. + +// . + +#if !defined(_YUGA_LITTLE_ENDIAN) || !defined(_YUGA_BIG_ENDIAN) +#error Unable to determine endian +#endif // Check we found an endianness correctly. + +#endif // INT_ENDIANNESS_H diff --git a/port/macos/compiler-rt/int_lib.h b/port/macos/compiler-rt/int_lib.h new file mode 100644 index 0000000000..943430de25 --- /dev/null +++ b/port/macos/compiler-rt/int_lib.h @@ -0,0 +1,171 @@ +//===-- int_lib.h - configuration header for compiler-rt -----------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file is a configuration header for compiler-rt. +// This file is not part of the interface of this library. +// +//===----------------------------------------------------------------------===// + +#ifndef INT_LIB_H +#define INT_LIB_H + +// Assumption: Signed integral is 2's complement. +// Assumption: Right shift of signed negative is arithmetic shift. +// Assumption: Endianness is little or big (not mixed). + +// ABI macro definitions + +#if __ARM_EABI__ +#ifdef COMPILER_RT_ARMHF_TARGET +#define COMPILER_RT_ABI +#else +#define COMPILER_RT_ABI __attribute__((__pcs__("aapcs"))) +#endif +#else +#define COMPILER_RT_ABI +#endif + +#define AEABI_RTABI __attribute__((__pcs__("aapcs"))) + +#if defined(_MSC_VER) && !defined(__clang__) +#define ALWAYS_INLINE __forceinline +#define NOINLINE __declspec(noinline) +#define NORETURN __declspec(noreturn) +#define UNUSED +#else +#define ALWAYS_INLINE __attribute__((always_inline)) +#define NOINLINE __attribute__((noinline)) +#define NORETURN __attribute__((noreturn)) +#define UNUSED __attribute__((unused)) +#endif + +#define STR(a) #a +#define XSTR(a) STR(a) +#define SYMBOL_NAME(name) XSTR(__USER_LABEL_PREFIX__) #name + +#if defined(__ELF__) || defined(__MINGW32__) || defined(__wasm__) || \ + defined(_AIX) || defined(__CYGWIN__) +#define COMPILER_RT_ALIAS(name, aliasname) \ + COMPILER_RT_ABI __typeof(name) aliasname __attribute__((__alias__(#name))); +#elif defined(__APPLE__) +#if defined(VISIBILITY_HIDDEN) +#define COMPILER_RT_ALIAS_VISIBILITY(name) \ + __asm__(".private_extern " SYMBOL_NAME(name)); +#else +#define COMPILER_RT_ALIAS_VISIBILITY(name) +#endif +#define COMPILER_RT_ALIAS(name, aliasname) \ + __asm__(".globl " SYMBOL_NAME(aliasname)); \ + COMPILER_RT_ALIAS_VISIBILITY(aliasname) \ + __asm__(SYMBOL_NAME(aliasname) " = " SYMBOL_NAME(name)); \ + COMPILER_RT_ABI __typeof(name) aliasname; +#elif defined(_WIN32) || defined(__UEFI__) +#define COMPILER_RT_ALIAS(name, aliasname) +#else +#error Unsupported target +#endif + +#if (defined(__FreeBSD__) || defined(__NetBSD__)) && \ + (defined(_KERNEL) || defined(_STANDALONE)) +// +// Kernel and boot environment can't use normal headers, +// so use the equivalent system headers. +// NB: FreeBSD (and OpenBSD) deprecate machine/limits.h in +// favour of sys/limits.h, so prefer the former, but fall +// back on the latter if not available since NetBSD only has +// the latter. +// +#if defined(__has_include) && __has_include() +#include +#else +#include +#endif +#include +#include +#else +// Include the standard compiler builtin headers we use functionality from. +#include +#include +#include +#include +#endif + +// Include the commonly used internal type definitions. +#include "int_types.h" + +// Include internal utility function declarations. +#include "int_util.h" + +COMPILER_RT_ABI int __paritysi2(si_int a); +COMPILER_RT_ABI int __paritydi2(di_int a); + +COMPILER_RT_ABI di_int __divdi3(di_int a, di_int b); +COMPILER_RT_ABI si_int __divsi3(si_int a, si_int b); +COMPILER_RT_ABI su_int __udivsi3(su_int n, su_int d); + +COMPILER_RT_ABI su_int __udivmodsi4(su_int a, su_int b, su_int *rem); +COMPILER_RT_ABI du_int __udivmoddi4(du_int a, du_int b, du_int *rem); +#ifdef CRT_HAS_128BIT +COMPILER_RT_ABI int __clzti2(ti_int a); +COMPILER_RT_ABI tu_int __udivmodti4(tu_int a, tu_int b, tu_int *rem); +#endif + +// Definitions for builtins unavailable on MSVC +#if defined(_MSC_VER) && !defined(__clang__) +#include + +static int __inline __builtin_ctz(uint32_t value) { + unsigned long trailing_zero = 0; + if (_BitScanForward(&trailing_zero, value)) + return trailing_zero; + return 32; +} + +static int __inline __builtin_clz(uint32_t value) { + unsigned long leading_zero = 0; + if (_BitScanReverse(&leading_zero, value)) + return 31 - leading_zero; + return 32; +} + +#if defined(_M_ARM) || defined(_M_X64) +static int __inline __builtin_clzll(uint64_t value) { + unsigned long leading_zero = 0; + if (_BitScanReverse64(&leading_zero, value)) + return 63 - leading_zero; + return 64; +} +#else +static int __inline __builtin_clzll(uint64_t value) { + if (value == 0) + return 64; + uint32_t msh = (uint32_t)(value >> 32); + uint32_t lsh = (uint32_t)(value & 0xFFFFFFFF); + if (msh != 0) + return __builtin_clz(msh); + return 32 + __builtin_clz(lsh); +} +#endif + +#define __builtin_clzl __builtin_clzll + +static bool __inline __builtin_sadd_overflow(int x, int y, int *result) { + if ((x < 0) != (y < 0)) { + *result = x + y; + return false; + } + int tmp = (unsigned int)x + (unsigned int)y; + if ((tmp < 0) != (x < 0)) + return true; + *result = tmp; + return false; +} + +#endif // defined(_MSC_VER) && !defined(__clang__) + +#endif // INT_LIB_H diff --git a/port/macos/compiler-rt/int_math.h b/port/macos/compiler-rt/int_math.h new file mode 100644 index 0000000000..08bfe922ff --- /dev/null +++ b/port/macos/compiler-rt/int_math.h @@ -0,0 +1,113 @@ +//===-- int_math.h - internal math inlines --------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file is not part of the interface of this library. +// +// This file defines substitutes for the libm functions used in some of the +// compiler-rt implementations, defined in such a way that there is not a direct +// dependency on libm or math.h. Instead, we use the compiler builtin versions +// where available. This reduces our dependencies on the system SDK by foisting +// the responsibility onto the compiler. +// +//===----------------------------------------------------------------------===// + +#ifndef INT_MATH_H +#define INT_MATH_H + +#ifndef __has_builtin +#define __has_builtin(x) 0 +#endif + +#if defined(_MSC_VER) && !defined(__clang__) +#include +#include +#endif + +#if defined(_MSC_VER) && !defined(__clang__) +#define CRT_INFINITY INFINITY +#else +#define CRT_INFINITY __builtin_huge_valf() +#endif + +#if defined(_MSC_VER) && !defined(__clang__) +#define crt_isfinite(x) _finite((x)) +#define crt_isinf(x) !_finite((x)) +#define crt_isnan(x) _isnan((x)) +#else +// Define crt_isfinite in terms of the builtin if available, otherwise provide +// an alternate version in terms of our other functions. This supports some +// versions of GCC which didn't have __builtin_isfinite. +#if __has_builtin(__builtin_isfinite) +#define crt_isfinite(x) __builtin_isfinite((x)) +#elif defined(__GNUC__) +#define crt_isfinite(x) \ + __extension__(({ \ + __typeof((x)) x_ = (x); \ + !crt_isinf(x_) && !crt_isnan(x_); \ + })) +#else +#error "Do not know how to check for infinity" +#endif // __has_builtin(__builtin_isfinite) +#define crt_isinf(x) __builtin_isinf((x)) +#define crt_isnan(x) __builtin_isnan((x)) +#endif // _MSC_VER + +#if defined(_MSC_VER) && !defined(__clang__) +#define crt_copysign(x, y) copysign((x), (y)) +#define crt_copysignf(x, y) copysignf((x), (y)) +#define crt_copysignl(x, y) copysignl((x), (y)) +#else +#define crt_copysign(x, y) __builtin_copysign((x), (y)) +#define crt_copysignf(x, y) __builtin_copysignf((x), (y)) +#define crt_copysignl(x, y) __builtin_copysignl((x), (y)) +// We define __has_builtin to always return 0 for GCC versions below 10, +// but __builtin_copysignf128 is available since version 7. +#if __has_builtin(__builtin_copysignf128) || \ + (defined(__GNUC__) && __GNUC__ >= 7) +#define crt_copysignf128(x, y) __builtin_copysignf128((x), (y)) +#elif __has_builtin(__builtin_copysignq) +#define crt_copysignf128(x, y) __builtin_copysignq((x), (y)) +#endif +#endif + +#if defined(_MSC_VER) && !defined(__clang__) +#define crt_fabs(x) fabs((x)) +#define crt_fabsf(x) fabsf((x)) +#define crt_fabsl(x) fabs((x)) +#else +#define crt_fabs(x) __builtin_fabs((x)) +#define crt_fabsf(x) __builtin_fabsf((x)) +#define crt_fabsl(x) __builtin_fabsl((x)) +// We define __has_builtin to always return 0 for GCC versions below 10, +// but __builtin_fabsf128 is available since version 7. +#if __has_builtin(__builtin_fabsf128) || (defined(__GNUC__) && __GNUC__ >= 7) +#define crt_fabsf128(x) __builtin_fabsf128((x)) +#elif __has_builtin(__builtin_fabsq) +#define crt_fabsf128(x) __builtin_fabsq((x)) +#endif +#endif + +#if defined(_MSC_VER) && !defined(__clang__) +#define crt_fmaxl(x, y) __max((x), (y)) +#else +#define crt_fmaxl(x, y) __builtin_fmaxl((x), (y)) +#endif + +#if defined(_MSC_VER) && !defined(__clang__) +#define crt_logbl(x) logbl((x)) +#else +#define crt_logbl(x) __builtin_logbl((x)) +#endif + +#if defined(_MSC_VER) && !defined(__clang__) +#define crt_scalbnl(x, y) scalbnl((x), (y)) +#else +#define crt_scalbnl(x, y) __builtin_scalbnl((x), (y)) +#endif + +#endif // INT_MATH_H diff --git a/port/macos/compiler-rt/int_types.h b/port/macos/compiler-rt/int_types.h new file mode 100644 index 0000000000..7c7f8cb64a --- /dev/null +++ b/port/macos/compiler-rt/int_types.h @@ -0,0 +1,276 @@ +//===-- int_lib.h - configuration header for compiler-rt -----------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file is not part of the interface of this library. +// +// This file defines various standard types, most importantly a number of unions +// used to access parts of larger types. +// +//===----------------------------------------------------------------------===// + +#ifndef INT_TYPES_H +#define INT_TYPES_H + +#include "int_endianness.h" + +// si_int is defined in Linux sysroot's asm-generic/siginfo.h +#ifdef si_int +#undef si_int +#endif +typedef int32_t si_int; +typedef uint32_t su_int; +#if UINT_MAX == 0xFFFFFFFF +#define clzsi __builtin_clz +#define ctzsi __builtin_ctz +#elif ULONG_MAX == 0xFFFFFFFF +#define clzsi __builtin_clzl +#define ctzsi __builtin_ctzl +#else +#error could not determine appropriate clzsi macro for this system +#endif + +typedef int64_t di_int; +typedef uint64_t du_int; + +typedef union { + di_int all; + struct { +#if _YUGA_LITTLE_ENDIAN + su_int low; + si_int high; +#else + si_int high; + su_int low; +#endif // _YUGA_LITTLE_ENDIAN + } s; +} dwords; + +typedef union { + du_int all; + struct { +#if _YUGA_LITTLE_ENDIAN + su_int low; + su_int high; +#else + su_int high; + su_int low; +#endif // _YUGA_LITTLE_ENDIAN + } s; +} udwords; + +#if defined(__LP64__) || defined(__wasm__) || defined(__mips64) || \ + defined(__SIZEOF_INT128__) || defined(_WIN64) +#define CRT_HAS_128BIT +#endif + +// MSVC doesn't have a working 128bit integer type. Users should really compile +// compiler-rt with clang, but if they happen to be doing a standalone build for +// asan or something else, disable the 128 bit parts so things sort of work. +#if defined(_MSC_VER) && !defined(__clang__) +#undef CRT_HAS_128BIT +#endif + +#ifdef CRT_HAS_128BIT +typedef int ti_int __attribute__((mode(TI))); +typedef unsigned tu_int __attribute__((mode(TI))); + +typedef union { + ti_int all; + struct { +#if _YUGA_LITTLE_ENDIAN + du_int low; + di_int high; +#else + di_int high; + du_int low; +#endif // _YUGA_LITTLE_ENDIAN + } s; +} twords; + +typedef union { + tu_int all; + struct { +#if _YUGA_LITTLE_ENDIAN + du_int low; + du_int high; +#else + du_int high; + du_int low; +#endif // _YUGA_LITTLE_ENDIAN + } s; +} utwords; + +static __inline ti_int make_ti(di_int h, di_int l) { + twords r; + r.s.high = (du_int)h; + r.s.low = (du_int)l; + return r.all; +} + +static __inline tu_int make_tu(du_int h, du_int l) { + utwords r; + r.s.high = h; + r.s.low = l; + return r.all; +} + +#endif // CRT_HAS_128BIT + +// FreeBSD's boot environment does not support using floating-point and poisons +// the float and double keywords. +#if defined(__FreeBSD__) && defined(_STANDALONE) +#define CRT_HAS_FLOATING_POINT 0 +#else +#define CRT_HAS_FLOATING_POINT 1 +#endif + +#if CRT_HAS_FLOATING_POINT +typedef union { + su_int u; + float f; +} float_bits; + +typedef union { + udwords u; + double f; +} double_bits; + +typedef struct { +#if _YUGA_LITTLE_ENDIAN + udwords low; + udwords high; +#else + udwords high; + udwords low; +#endif // _YUGA_LITTLE_ENDIAN +} uqwords; + +// Check if the target supports 80 bit extended precision long doubles. +// Notably, on x86 Windows, MSVC only provides a 64-bit long double, but GCC +// still makes it 80 bits. Clang will match whatever compiler it is trying to +// be compatible with. On 32-bit x86 Android, long double is 64 bits, while on +// x86_64 Android, long double is 128 bits. +#if (defined(__i386__) || defined(__x86_64__)) && \ + !(defined(_MSC_VER) || defined(__ANDROID__)) +#define HAS_80_BIT_LONG_DOUBLE 1 +#elif defined(__m68k__) || defined(__ia64__) +#define HAS_80_BIT_LONG_DOUBLE 1 +#else +#define HAS_80_BIT_LONG_DOUBLE 0 +#endif + +#if HAS_80_BIT_LONG_DOUBLE +typedef long double xf_float; +typedef union { + uqwords u; + xf_float f; +} xf_bits; +#endif + +#ifdef __powerpc64__ +// From https://gcc.gnu.org/wiki/Ieee128PowerPC: +// PowerPC64 uses the following suffixes: +// IFmode: IBM extended double +// KFmode: IEEE 128-bit floating point +// TFmode: Matches the default for long double. With -mabi=ieeelongdouble, +// it is IEEE 128-bit, with -mabi=ibmlongdouble IBM extended double +// Since compiler-rt only implements the tf set of libcalls, we use long double +// for the tf_float typedef. +typedef long double tf_float; +#define CRT_LDBL_128BIT +#define CRT_HAS_F128 +#if __LDBL_MANT_DIG__ == 113 && !defined(__LONG_DOUBLE_IBM128__) +#define CRT_HAS_IEEE_TF +#define CRT_LDBL_IEEE_F128 +#endif +#define TF_C(x) x##L +#elif __LDBL_MANT_DIG__ == 113 || \ + (__FLT_RADIX__ == 16 && __LDBL_MANT_DIG__ == 28) +// Use long double instead of __float128 if it matches the IEEE 128-bit format +// or the IBM hexadecimal format. +#define CRT_LDBL_128BIT +#define CRT_HAS_F128 +#if __LDBL_MANT_DIG__ == 113 +#define CRT_HAS_IEEE_TF +#define CRT_LDBL_IEEE_F128 +#endif +typedef long double tf_float; +#define TF_C(x) x##L +#elif defined(__FLOAT128__) || defined(__SIZEOF_FLOAT128__) +#define CRT_HAS___FLOAT128_KEYWORD +#define CRT_HAS_F128 +// NB: we assume the __float128 type uses IEEE representation. +#define CRT_HAS_IEEE_TF +typedef __float128 tf_float; +#define TF_C(x) x##Q +#endif + +#ifdef CRT_HAS_F128 +typedef union { + uqwords u; + tf_float f; +} tf_bits; +#endif + +// __(u)int128_t is currently needed to compile the *tf builtins as we would +// otherwise need to manually expand the bit manipulation on two 64-bit value. +#if defined(CRT_HAS_128BIT) && defined(CRT_HAS_F128) +#define CRT_HAS_TF_MODE +#endif + +#if __STDC_VERSION__ >= 199901L && !defined(_MSC_VER) +typedef float _Complex Fcomplex; +typedef double _Complex Dcomplex; +typedef long double _Complex Lcomplex; +#if defined(CRT_LDBL_128BIT) +typedef Lcomplex Qcomplex; +#define CRT_HAS_NATIVE_COMPLEX_F128 +#elif defined(CRT_HAS___FLOAT128_KEYWORD) +#if defined(__clang_major__) && __clang_major__ > 10 +// Clang prior to 11 did not support __float128 _Complex. +typedef __float128 _Complex Qcomplex; +#define CRT_HAS_NATIVE_COMPLEX_F128 +#elif defined(__GNUC__) && __GNUC__ >= 7 +// GCC does not allow __float128 _Complex, but accepts _Float128 _Complex. +typedef _Float128 _Complex Qcomplex; +#define CRT_HAS_NATIVE_COMPLEX_F128 +#endif +#endif + +#define COMPLEX_REAL(x) __real__(x) +#define COMPLEX_IMAGINARY(x) __imag__(x) +#else +typedef struct { + float real, imaginary; +} Fcomplex; + +typedef struct { + double real, imaginary; +} Dcomplex; + +typedef struct { + long double real, imaginary; +} Lcomplex; + +#define COMPLEX_REAL(x) (x).real +#define COMPLEX_IMAGINARY(x) (x).imaginary +#endif + +#ifdef CRT_HAS_NATIVE_COMPLEX_F128 +#define COMPLEXTF_REAL(x) __real__(x) +#define COMPLEXTF_IMAGINARY(x) __imag__(x) +#elif defined(CRT_HAS_F128) +typedef struct { + tf_float real, imaginary; +} Qcomplex; +#define COMPLEXTF_REAL(x) (x).real +#define COMPLEXTF_IMAGINARY(x) (x).imaginary +#endif + +#endif // CRT_HAS_FLOATING_POINT +#endif // INT_TYPES_H diff --git a/port/macos/compiler-rt/int_util.h b/port/macos/compiler-rt/int_util.h new file mode 100644 index 0000000000..c372c2edc6 --- /dev/null +++ b/port/macos/compiler-rt/int_util.h @@ -0,0 +1,47 @@ +//===-- int_util.h - internal utility functions ---------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file is not part of the interface of this library. +// +// This file defines non-inline utilities which are available for use in the +// library. The function definitions themselves are all contained in int_util.c +// which will always be compiled into any compiler-rt library. +// +//===----------------------------------------------------------------------===// + +#ifndef INT_UTIL_H +#define INT_UTIL_H + +/// \brief Trigger a program abort (or panic for kernel code). +#define compilerrt_abort() __compilerrt_abort_impl(__FILE__, __LINE__, __func__) + +NORETURN void __compilerrt_abort_impl(const char *file, int line, + const char *function); + +#define COMPILE_TIME_ASSERT(expr) COMPILE_TIME_ASSERT1(expr, __COUNTER__) +#define COMPILE_TIME_ASSERT1(expr, cnt) COMPILE_TIME_ASSERT2(expr, cnt) +#define COMPILE_TIME_ASSERT2(expr, cnt) \ + typedef char ct_assert_##cnt[(expr) ? 1 : -1] UNUSED + +// Force unrolling the code specified to be repeated N times. +#define REPEAT_0_TIMES(code_to_repeat) /* do nothing */ +#define REPEAT_1_TIMES(code_to_repeat) code_to_repeat +#define REPEAT_2_TIMES(code_to_repeat) \ + REPEAT_1_TIMES(code_to_repeat) \ + code_to_repeat +#define REPEAT_3_TIMES(code_to_repeat) \ + REPEAT_2_TIMES(code_to_repeat) \ + code_to_repeat +#define REPEAT_4_TIMES(code_to_repeat) \ + REPEAT_3_TIMES(code_to_repeat) \ + code_to_repeat + +#define REPEAT_N_TIMES_(N, code_to_repeat) REPEAT_##N##_TIMES(code_to_repeat) +#define REPEAT_N_TIMES(N, code_to_repeat) REPEAT_N_TIMES_(N, code_to_repeat) + +#endif // INT_UTIL_H diff --git a/port/macos/compiler-rt/moddi3.c b/port/macos/compiler-rt/moddi3.c new file mode 100644 index 0000000000..15cf80b995 --- /dev/null +++ b/port/macos/compiler-rt/moddi3.c @@ -0,0 +1,22 @@ +//===-- moddi3.c - Implement __moddi3 -------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file implements __moddi3 for the compiler_rt library. +// +//===----------------------------------------------------------------------===// + +#include "int_lib.h" + +// Returns: a % b + +#define fixint_t di_int +#define fixuint_t du_int +#define ASSIGN_UMOD(res, a, b) __udivmoddi4((a), (b), &(res)) +#include "int_div_impl.inc" + +COMPILER_RT_ABI di_int __moddi3(di_int a, di_int b) { return __modXi3(a, b); } diff --git a/port/macos/compiler-rt/udivdi3.c b/port/macos/compiler-rt/udivdi3.c new file mode 100644 index 0000000000..74319cbe71 --- /dev/null +++ b/port/macos/compiler-rt/udivdi3.c @@ -0,0 +1,23 @@ +//===-- udivdi3.c - Implement __udivdi3 -----------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file implements __udivdi3 for the compiler_rt library. +// +//===----------------------------------------------------------------------===// + +#include "int_lib.h" + +typedef du_int fixuint_t; +typedef di_int fixint_t; +#include "int_div_impl.inc" + +// Returns: a / b + +COMPILER_RT_ABI du_int __udivdi3(du_int a, du_int b) { + return __udivXi3(a, b); +} diff --git a/port/macos/compiler-rt/udivmoddi4.c b/port/macos/compiler-rt/udivmoddi4.c new file mode 100644 index 0000000000..123e5fb05f --- /dev/null +++ b/port/macos/compiler-rt/udivmoddi4.c @@ -0,0 +1,200 @@ +//===-- udivmoddi4.c - Implement __udivmoddi4 -----------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file implements __udivmoddi4 for the compiler_rt library. +// +//===----------------------------------------------------------------------===// + +#include "int_lib.h" + +// Effects: if rem != 0, *rem = a % b +// Returns: a / b + +// Translated from Figure 3-40 of The PowerPC Compiler Writer's Guide + +#if defined(_MSC_VER) && !defined(__clang__) +// MSVC throws a warning about mod 0 here, disable it for builds that +// warn-as-error +#pragma warning(push) +#pragma warning(disable : 4723 4724) +#endif + +COMPILER_RT_ABI du_int __udivmoddi4(du_int a, du_int b, du_int *rem) { + const unsigned n_uword_bits = sizeof(su_int) * CHAR_BIT; + const unsigned n_udword_bits = sizeof(du_int) * CHAR_BIT; + udwords n; + n.all = a; + udwords d; + d.all = b; + udwords q; + udwords r; + unsigned sr; + // special cases, X is unknown, K != 0 + if (n.s.high == 0) { + if (d.s.high == 0) { + // 0 X + // --- + // 0 X + if (rem) + *rem = n.s.low % d.s.low; + return n.s.low / d.s.low; + } + // 0 X + // --- + // K X + if (rem) + *rem = n.s.low; + return 0; + } + // n.s.high != 0 + if (d.s.low == 0) { + if (d.s.high == 0) { + // K X + // --- + // 0 0 + if (rem) + *rem = n.s.high % d.s.low; + return n.s.high / d.s.low; + } + // d.s.high != 0 + if (n.s.low == 0) { + // K 0 + // --- + // K 0 + if (rem) { + r.s.high = n.s.high % d.s.high; + r.s.low = 0; + *rem = r.all; + } + return n.s.high / d.s.high; + } + // K K + // --- + // K 0 + if ((d.s.high & (d.s.high - 1)) == 0) /* if d is a power of 2 */ { + if (rem) { + r.s.low = n.s.low; + r.s.high = n.s.high & (d.s.high - 1); + *rem = r.all; + } + return n.s.high >> ctzsi(d.s.high); + } + // K K + // --- + // K 0 + sr = clzsi(d.s.high) - clzsi(n.s.high); + // 0 <= sr <= n_uword_bits - 2 or sr large + if (sr > n_uword_bits - 2) { + if (rem) + *rem = n.all; + return 0; + } + ++sr; + // 1 <= sr <= n_uword_bits - 1 + // q.all = n.all << (n_udword_bits - sr); + q.s.low = 0; + q.s.high = n.s.low << (n_uword_bits - sr); + // r.all = n.all >> sr; + r.s.high = n.s.high >> sr; + r.s.low = (n.s.high << (n_uword_bits - sr)) | (n.s.low >> sr); + } else /* d.s.low != 0 */ { + if (d.s.high == 0) { + // K X + // --- + // 0 K + if ((d.s.low & (d.s.low - 1)) == 0) /* if d is a power of 2 */ { + if (rem) + *rem = n.s.low & (d.s.low - 1); + if (d.s.low == 1) + return n.all; + sr = ctzsi(d.s.low); + q.s.high = n.s.high >> sr; + q.s.low = (n.s.high << (n_uword_bits - sr)) | (n.s.low >> sr); + return q.all; + } + // K X + // --- + // 0 K + sr = 1 + n_uword_bits + clzsi(d.s.low) - clzsi(n.s.high); + // 2 <= sr <= n_udword_bits - 1 + // q.all = n.all << (n_udword_bits - sr); + // r.all = n.all >> sr; + if (sr == n_uword_bits) { + q.s.low = 0; + q.s.high = n.s.low; + r.s.high = 0; + r.s.low = n.s.high; + } else if (sr < n_uword_bits) /* 2 <= sr <= n_uword_bits - 1 */ { + q.s.low = 0; + q.s.high = n.s.low << (n_uword_bits - sr); + r.s.high = n.s.high >> sr; + r.s.low = (n.s.high << (n_uword_bits - sr)) | (n.s.low >> sr); + } else /* n_uword_bits + 1 <= sr <= n_udword_bits - 1 */ { + q.s.low = n.s.low << (n_udword_bits - sr); + q.s.high = (n.s.high << (n_udword_bits - sr)) | + (n.s.low >> (sr - n_uword_bits)); + r.s.high = 0; + r.s.low = n.s.high >> (sr - n_uword_bits); + } + } else { + // K X + // --- + // K K + sr = clzsi(d.s.high) - clzsi(n.s.high); + // 0 <= sr <= n_uword_bits - 1 or sr large + if (sr > n_uword_bits - 1) { + if (rem) + *rem = n.all; + return 0; + } + ++sr; + // 1 <= sr <= n_uword_bits + // q.all = n.all << (n_udword_bits - sr); + q.s.low = 0; + if (sr == n_uword_bits) { + q.s.high = n.s.low; + r.s.high = 0; + r.s.low = n.s.high; + } else { + q.s.high = n.s.low << (n_uword_bits - sr); + r.s.high = n.s.high >> sr; + r.s.low = (n.s.high << (n_uword_bits - sr)) | (n.s.low >> sr); + } + } + } + // Not a special case + // q and r are initialized with: + // q.all = n.all << (n_udword_bits - sr); + // r.all = n.all >> sr; + // 1 <= sr <= n_udword_bits - 1 + su_int carry = 0; + for (; sr > 0; --sr) { + // r:q = ((r:q) << 1) | carry + r.s.high = (r.s.high << 1) | (r.s.low >> (n_uword_bits - 1)); + r.s.low = (r.s.low << 1) | (q.s.high >> (n_uword_bits - 1)); + q.s.high = (q.s.high << 1) | (q.s.low >> (n_uword_bits - 1)); + q.s.low = (q.s.low << 1) | carry; + // carry = 0; + // if (r.all >= d.all) + // { + // r.all -= d.all; + // carry = 1; + // } + const di_int s = (di_int)(d.all - r.all - 1) >> (n_udword_bits - 1); + carry = s & 1; + r.all -= d.all & s; + } + q.all = (q.all << 1) | carry; + if (rem) + *rem = r.all; + return q.all; +} + +#if defined(_MSC_VER) && !defined(__clang__) +#pragma warning(pop) +#endif diff --git a/port/macos/compiler-rt/umoddi3.c b/port/macos/compiler-rt/umoddi3.c new file mode 100644 index 0000000000..e672da96ef --- /dev/null +++ b/port/macos/compiler-rt/umoddi3.c @@ -0,0 +1,23 @@ +//===-- umoddi3.c - Implement __umoddi3 -----------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file implements __umoddi3 for the compiler_rt library. +// +//===----------------------------------------------------------------------===// + +#include "int_lib.h" + +typedef du_int fixuint_t; +typedef di_int fixint_t; +#include "int_div_impl.inc" + +// Returns: a % b + +COMPILER_RT_ABI du_int __umoddi3(du_int a, du_int b) { + return __umodXi3(a, b); +} diff --git a/port/macos/elf32_format.h b/port/macos/elf32_format.h new file mode 100644 index 0000000000..252259de80 --- /dev/null +++ b/port/macos/elf32_format.h @@ -0,0 +1,76 @@ +#ifndef HALO_MACOS_ELF32_FORMAT_H +#define HALO_MACOS_ELF32_FORMAT_H + +#include + +#define HALO_ELFCLASS32 1 +#define HALO_ELFDATA2LSB 1 +#define HALO_EVCURRENT 1 +#define HALO_ET_EXEC 2 +#define HALO_EM_386 3 +#define HALO_PT_LOAD 1 +#define HALO_PT_TLS 7 +#define HALO_SHT_SYMTAB 2 +#define HALO_SHT_STRTAB 3 +#define HALO_STT_FUNC 2 +#define HALO_STT_OBJECT 1 +#define HALO_SHN_UNDEF 0 +#define HALO_PF_X 1 +#define HALO_PF_W 2 +#define HALO_PF_R 4 + +struct halo_elf32_ehdr +{ + unsigned char ident[16]; + uint16_t type; + uint16_t machine; + uint32_t version; + uint32_t entry; + uint32_t phoff; + uint32_t shoff; + uint32_t flags; + uint16_t ehsize; + uint16_t phentsize; + uint16_t phnum; + uint16_t shentsize; + uint16_t shnum; + uint16_t shstrndx; +}; + +struct halo_elf32_phdr +{ + uint32_t type; + uint32_t offset; + uint32_t vaddr; + uint32_t paddr; + uint32_t filesz; + uint32_t memsz; + uint32_t flags; + uint32_t align; +}; + +struct halo_elf32_shdr +{ + uint32_t name; + uint32_t type; + uint32_t flags; + uint32_t addr; + uint32_t offset; + uint32_t size; + uint32_t link; + uint32_t info; + uint32_t addralign; + uint32_t entsize; +}; + +struct halo_elf32_sym +{ + uint32_t name; + uint32_t value; + uint32_t size; + unsigned char info; + unsigned char other; + uint16_t shndx; +}; + +#endif diff --git a/port/macos/gl41_compat.c b/port/macos/gl41_compat.c new file mode 100644 index 0000000000..a6a9ccecf5 --- /dev/null +++ b/port/macos/gl41_compat.c @@ -0,0 +1,81 @@ +/* OpenGL 4.1 fallbacks used by the native macOS source port. */ + +#include "gl41_compat.h" + +enum +{ + GL_READ_FRAMEBUFFER_VALUE = 0x8CA8, + GL_DRAW_FRAMEBUFFER_VALUE = 0x8CA9, + GL_READ_FRAMEBUFFER_BINDING_VALUE = 0x8CAA, + GL_DRAW_FRAMEBUFFER_BINDING_VALUE = 0x8CA6, + GL_SCISSOR_TEST_VALUE = 0x0C11, + GL_COLOR_ATTACHMENT0_VALUE = 0x8CE0, + GL_TEXTURE_2D_VALUE = 0x0DE1, + GL_COLOR_BUFFER_BIT_VALUE = 0x00004000, + GL_NEAREST_VALUE = 0x2600, + GL_FRAMEBUFFER_COMPLETE_VALUE = 0x8CD5, +}; + +static int api_valid(const struct halo_gl41_copy_api *api) +{ + return api && api->get_integer && api->is_enabled && api->enable && api->disable && + api->gen_framebuffers && api->bind_framebuffer && api->framebuffer_texture_2d && + api->read_buffer && api->draw_buffer && api->check_framebuffer_status && api->blit_framebuffer; +} + +int mac_gl41_copy_image_2d(const struct halo_gl41_copy_api *api, + struct halo_gl41_copy_state *state, + uint32_t source, int32_t source_level, + uint32_t destination, int32_t destination_level, + int32_t width, int32_t height) +{ + int32_t previous_read, previous_draw; + int scissor_enabled; + int result = 0; + + if (!api_valid(api) || !state || !source || !destination || + source_level < 0 || destination_level < 0 || width <= 0 || height <= 0) + return 0; + if (!state->read_framebuffer || !state->draw_framebuffer) + { + uint32_t framebuffers[2] = { 0, 0 }; + + api->gen_framebuffers(2, framebuffers); + if (!framebuffers[0] || !framebuffers[1] || framebuffers[0] == framebuffers[1]) + return 0; + state->read_framebuffer = framebuffers[0]; + state->draw_framebuffer = framebuffers[1]; + } + + api->get_integer(GL_READ_FRAMEBUFFER_BINDING_VALUE, &previous_read); + api->get_integer(GL_DRAW_FRAMEBUFFER_BINDING_VALUE, &previous_draw); + scissor_enabled = api->is_enabled(GL_SCISSOR_TEST_VALUE) != 0; + + api->bind_framebuffer(GL_READ_FRAMEBUFFER_VALUE, state->read_framebuffer); + api->framebuffer_texture_2d(GL_READ_FRAMEBUFFER_VALUE, GL_COLOR_ATTACHMENT0_VALUE, + GL_TEXTURE_2D_VALUE, source, source_level); + api->read_buffer(GL_COLOR_ATTACHMENT0_VALUE); + if (api->check_framebuffer_status(GL_READ_FRAMEBUFFER_VALUE) != GL_FRAMEBUFFER_COMPLETE_VALUE) + goto restore; + + api->bind_framebuffer(GL_DRAW_FRAMEBUFFER_VALUE, state->draw_framebuffer); + api->framebuffer_texture_2d(GL_DRAW_FRAMEBUFFER_VALUE, GL_COLOR_ATTACHMENT0_VALUE, + GL_TEXTURE_2D_VALUE, destination, destination_level); + api->draw_buffer(GL_COLOR_ATTACHMENT0_VALUE); + if (api->check_framebuffer_status(GL_DRAW_FRAMEBUFFER_VALUE) != GL_FRAMEBUFFER_COMPLETE_VALUE) + goto restore; + + api->disable(GL_SCISSOR_TEST_VALUE); + api->blit_framebuffer(0, 0, width, height, 0, 0, width, height, + GL_COLOR_BUFFER_BIT_VALUE, GL_NEAREST_VALUE); + result = 1; + +restore: + api->bind_framebuffer(GL_READ_FRAMEBUFFER_VALUE, (uint32_t)previous_read); + api->bind_framebuffer(GL_DRAW_FRAMEBUFFER_VALUE, (uint32_t)previous_draw); + if (scissor_enabled) + api->enable(GL_SCISSOR_TEST_VALUE); + else + api->disable(GL_SCISSOR_TEST_VALUE); + return result; +} diff --git a/port/macos/gl41_compat.h b/port/macos/gl41_compat.h new file mode 100644 index 0000000000..8563f43ff9 --- /dev/null +++ b/port/macos/gl41_compat.h @@ -0,0 +1,47 @@ +/* +GL41_COMPAT.H + +Small compatibility helpers for the macOS OpenGL 4.1 renderer. The function +table is supplied by the renderer so this file does not depend on a GL loader +or on a particular SDL context owner. +*/ + +#ifndef HALO_MACOS_GL41_COMPAT_H +#define HALO_MACOS_GL41_COMPAT_H + +#include + +struct halo_gl41_copy_api +{ + void (*get_integer)(uint32_t name, int32_t *value); + uint8_t (*is_enabled)(uint32_t capability); + void (*enable)(uint32_t capability); + void (*disable)(uint32_t capability); + void (*gen_framebuffers)(int32_t count, uint32_t *framebuffers); + void (*bind_framebuffer)(uint32_t target, uint32_t framebuffer); + void (*framebuffer_texture_2d)(uint32_t target, uint32_t attachment, + uint32_t texture_target, uint32_t texture, int32_t level); + void (*read_buffer)(uint32_t source); + void (*draw_buffer)(uint32_t destination); + uint32_t (*check_framebuffer_status)(uint32_t target); + void (*blit_framebuffer)(int32_t src_x0, int32_t src_y0, int32_t src_x1, int32_t src_y1, + int32_t dst_x0, int32_t dst_y0, int32_t dst_x1, int32_t dst_y1, + uint32_t mask, uint32_t filter); +}; + +struct halo_gl41_copy_state +{ + uint32_t read_framebuffer; + uint32_t draw_framebuffer; +}; + +/* Copies one 2D mip using a framebuffer blit. It saves and restores the +caller’s independent READ/DRAW framebuffer bindings and scissor enable state. +The two private FBOs are created lazily and belong to this state object. */ +int mac_gl41_copy_image_2d(const struct halo_gl41_copy_api *api, + struct halo_gl41_copy_state *state, + uint32_t source, int32_t source_level, + uint32_t destination, int32_t destination_level, + int32_t width, int32_t height); + +#endif diff --git a/port/macos/gl_bridge_support.c b/port/macos/gl_bridge_support.c new file mode 100644 index 0000000000..d30f19da94 --- /dev/null +++ b/port/macos/gl_bridge_support.c @@ -0,0 +1,527 @@ +#include "gl_bridge_support.h" + +#include "guest_address.h" +#include "guest_allocator.h" +#include "guest_call.h" +#include "gl_token_registry.h" + +#include +#include + +#include +#include +#include +#include +#include + +extern void *mac_macos_gl_native_proc(const char *name); +extern int mac_guest_allocate(size_t size, size_t alignment, uint32_t *guest_va_out); + +static void gl_bridge_fatal(const char *name, const char *reason) +{ + fprintf(stderr, "[mac-gl-bridge] %s: %s\n", name ? name : "?", + reason ? reason : "bridge failure"); + abort(); +} + +void mac_macos_gl_bridge_fault(const char *name, const char *reason) +{ + gl_bridge_fatal(name, reason); +} + +static size_t component_count(uint32_t format) +{ + switch (format) + { + case GL_RED: + case GL_GREEN: + case GL_BLUE: + case GL_ALPHA: + case GL_LUMINANCE: + case GL_DEPTH_COMPONENT: + case GL_STENCIL_INDEX: + return 1; + case GL_RG: + return 2; + case GL_RGB: + case GL_BGR: + return 3; + case GL_RGBA: + case GL_BGRA: + return 4; + default: + return 0; + } +} + +static size_t scalar_bytes(uint32_t type) +{ + switch (type) + { + case GL_BYTE: + case GL_UNSIGNED_BYTE: + return 1; + case GL_SHORT: + case GL_UNSIGNED_SHORT: + case GL_HALF_FLOAT: + return 2; + case GL_INT: + case GL_UNSIGNED_INT: + case GL_FLOAT: + return 4; + case GL_DOUBLE: + return 8; + case GL_UNSIGNED_SHORT_5_6_5: + case GL_UNSIGNED_SHORT_4_4_4_4: + case GL_UNSIGNED_SHORT_5_5_5_1: + case GL_UNSIGNED_INT_8_8_8_8: + case GL_UNSIGNED_INT_10_10_10_2: + case GL_INT_2_10_10_10_REV: + case GL_UNSIGNED_INT_2_10_10_10_REV: + case GL_UNSIGNED_INT_10F_11F_11F_REV: + case GL_UNSIGNED_INT_5_9_9_9_REV: + case GL_UNSIGNED_INT_24_8: + return 0; + case GL_FLOAT_32_UNSIGNED_INT_24_8_REV: + return 8; + default: + return 0; + } +} + +size_t mac_macos_gl_pixel_span(uint32_t width, uint32_t height, + uint32_t format, uint32_t type, uint32_t alignment) +{ + uint64_t row, stride, total; + size_t components = component_count(format); + size_t scalar = scalar_bytes(type); + + if (width == 0 || height == 0 || alignment == 0 || + (alignment & (alignment - 1)) != 0 || alignment > 8) + return 0; + if (!components || !scalar) + { + switch (type) + { + case GL_UNSIGNED_SHORT_5_6_5: + case GL_UNSIGNED_SHORT_4_4_4_4: + case GL_UNSIGNED_SHORT_5_5_5_1: + if (components != 3 && components != 4) return 0; + scalar = 2; + components = 1; + break; + case GL_UNSIGNED_INT_8_8_8_8: + case GL_UNSIGNED_INT_10_10_10_2: + case GL_INT_2_10_10_10_REV: + case GL_UNSIGNED_INT_2_10_10_10_REV: + if (components != 4) return 0; + scalar = 4; + components = 1; + break; + case GL_UNSIGNED_INT_10F_11F_11F_REV: + case GL_UNSIGNED_INT_5_9_9_9_REV: + if (components != 3) return 0; + scalar = 4; + components = 1; + break; + case GL_UNSIGNED_INT_24_8: + if (format != GL_DEPTH_STENCIL) return 0; + scalar = 4; + components = 1; + break; + case GL_FLOAT_32_UNSIGNED_INT_24_8_REV: + if (format != GL_DEPTH_STENCIL) return 0; + scalar = 8; + components = 1; + break; + default: + return 0; + } + } + row = (uint64_t)width * components * scalar; + stride = (row + alignment - 1) & ~((uint64_t)alignment - 1); + if (height > 1 && stride > (UINT64_MAX - row) / (height - 1)) + return 0; + total = stride * (height - 1) + row; + if (total == 0 || total > SIZE_MAX) + return 0; + return (size_t)total; +} + +static int get_integer(uint32_t pname, GLint *value); + +static size_t pixel_transfer_span(uint32_t width, uint32_t height, + uint32_t depth, uint32_t format, uint32_t type, int pack) +{ + GLint alignment = 4, row_length = 0, image_height = 0; + GLint skip_pixels = 0, skip_rows = 0, skip_images = 0; + uint64_t pixel_bytes, row_pixels, row_bytes, row_stride, image_rows; + uint64_t image_stride, total; + const GLenum alignment_name = pack ? GL_PACK_ALIGNMENT : GL_UNPACK_ALIGNMENT; + const GLenum row_length_name = pack ? GL_PACK_ROW_LENGTH : GL_UNPACK_ROW_LENGTH; + const GLenum image_height_name = pack ? GL_PACK_IMAGE_HEIGHT : GL_UNPACK_IMAGE_HEIGHT; + const GLenum skip_pixels_name = pack ? GL_PACK_SKIP_PIXELS : GL_UNPACK_SKIP_PIXELS; + const GLenum skip_rows_name = pack ? GL_PACK_SKIP_ROWS : GL_UNPACK_SKIP_ROWS; + const GLenum skip_images_name = pack ? GL_PACK_SKIP_IMAGES : GL_UNPACK_SKIP_IMAGES; + + if (!width || !height || !depth || !get_integer(alignment_name, &alignment) || + !get_integer(row_length_name, &row_length) || + !get_integer(image_height_name, &image_height) || + !get_integer(skip_pixels_name, &skip_pixels) || + !get_integer(skip_rows_name, &skip_rows) || + !get_integer(skip_images_name, &skip_images) || alignment <= 0 || + row_length < 0 || image_height < 0 || skip_pixels < 0 || skip_rows < 0 || skip_images < 0) + return 0; + pixel_bytes = mac_macos_gl_pixel_span(1, 1, format, type, 1); + if (!pixel_bytes) + return 0; + row_pixels = row_length > 0 ? (uint32_t)row_length : width; + image_rows = image_height > 0 ? (uint32_t)image_height : + (uint64_t)(uint32_t)skip_rows + height; + if (row_pixels < (uint64_t)(uint32_t)skip_pixels + width || + (image_height > 0 && image_rows < (uint64_t)(uint32_t)skip_rows + height)) + return 0; + row_bytes = row_pixels * pixel_bytes; + if (row_bytes > UINT64_MAX - ((uint32_t)alignment - 1)) + return 0; + row_stride = (row_bytes + (uint32_t)alignment - 1) & ~((uint64_t)alignment - 1); + if (row_stride && image_rows > UINT64_MAX / row_stride) + return 0; + image_stride = row_stride * image_rows; + uint64_t last_image = (uint64_t)depth - 1 + (uint32_t)skip_images; + if (image_stride && last_image > (UINT64_MAX / image_stride)) + return 0; + total = last_image * image_stride; + if (row_stride && (uint64_t)(skip_rows + height - 1) > UINT64_MAX / row_stride) + return 0; + if (total > UINT64_MAX - (uint64_t)(skip_rows + height - 1) * row_stride) + return 0; + total += (uint64_t)(skip_rows + height - 1) * row_stride; + if ((uint64_t)skip_pixels > UINT64_MAX / pixel_bytes) + return 0; + if (total > UINT64_MAX - (uint64_t)skip_pixels * pixel_bytes) + return 0; + total += (uint64_t)skip_pixels * pixel_bytes; + if (total > UINT64_MAX - (uint64_t)width * pixel_bytes) + return 0; + total += (uint64_t)width * pixel_bytes; + return total && total <= SIZE_MAX ? (size_t)total : 0; +} + +static int get_integer(uint32_t pname, GLint *value) +{ + typedef void (APIENTRYP get_integer_fn)(GLenum, GLint *); + get_integer_fn fn = (get_integer_fn)mac_macos_gl_native_proc("glGetIntegerv"); + if (!fn) + return 0; + fn((GLenum)pname, value); + return 1; +} + +static int pixel_buffer_is_bound(int pack) +{ + GLint binding = 0; + return get_integer(pack ? GL_PIXEL_PACK_BUFFER_BINDING : + GL_PIXEL_UNPACK_BUFFER_BINDING, &binding) && binding != 0; +} + +static size_t texture_image_span(uint32_t target, uint32_t level, + uint32_t format, uint32_t type, int pack) +{ + typedef void (APIENTRYP get_level_fn)(GLenum, GLint, GLenum, GLint *); + get_level_fn get_level = (get_level_fn)mac_macos_gl_native_proc( + "glGetTexLevelParameteriv"); + GLint width = 0, height = 1, depth = 1, pbo = 0; + uint32_t binding = pack ? GL_PIXEL_PACK_BUFFER_BINDING : GL_PIXEL_UNPACK_BUFFER_BINDING; + + if (!get_level || !get_integer(binding, &pbo)) + return 0; + get_level((GLenum)target, (GLint)level, GL_TEXTURE_WIDTH, &width); + get_level((GLenum)target, (GLint)level, GL_TEXTURE_HEIGHT, &height); + if ((GLenum)target == GL_TEXTURE_3D || (GLenum)target == GL_TEXTURE_2D_ARRAY) + get_level((GLenum)target, (GLint)level, GL_TEXTURE_DEPTH, &depth); + if (width <= 0 || height <= 0 || depth <= 0) + return 0; + return pixel_transfer_span((uint32_t)width, (uint32_t)height, + (uint32_t)depth, format, type, pack); +} + +static size_t gl_pointer_span(const char *name, unsigned index, + const uint32_t *a, unsigned count) +{ + uint64_t n; + if (!name || !a) + return 0; +#define NAME_IS(value) (strcmp(name, value) == 0) + if (NAME_IS("glGetIntegerv") && index == 1) + { + switch (a[0]) + { + case GL_VIEWPORT: + case GL_SCISSOR_BOX: + case GL_COLOR_WRITEMASK: return 4 * sizeof(GLint); + case GL_MAX_VIEWPORT_DIMS: return 2 * sizeof(GLint); + default: return sizeof(GLint); + } + } + if (NAME_IS("glGetTexImage") && index == 4 && count >= 5) + return texture_image_span(a[0], a[1], a[2], a[3], 1); + if (NAME_IS("glReadPixels") && index == 6 && count >= 7) + { + return pixel_transfer_span(a[2], a[3], 1, a[4], a[5], 1); + } + if ((NAME_IS("glGenTextures") || NAME_IS("glDeleteTextures") || + NAME_IS("glGenSamplers") || NAME_IS("glGenFramebuffers") || + NAME_IS("glDeleteFramebuffers") || NAME_IS("glGenBuffers") || + NAME_IS("glDeleteBuffers") || NAME_IS("glGenVertexArrays") || + NAME_IS("glGenQueries")) && count >= 2 && index == 1) + { + n = (uint64_t)a[0] * sizeof(GLuint); + return n <= SIZE_MAX ? (size_t)n : 0; + } + if ((NAME_IS("glTexImage2D") && index == 8 && count >= 9) || + (NAME_IS("glTexSubImage2D") && index == 8 && count >= 9)) + { + return pixel_transfer_span(a[3], a[4], 1, a[6], a[7], 0); + } + if (NAME_IS("glTexImage3D") && index == 9 && count >= 10) + { + return pixel_transfer_span(a[3], a[4], a[5], a[7], a[8], 0); + } + if (NAME_IS("glCompressedTexImage2D") && index == 7 && count >= 8) return a[6]; + if (NAME_IS("glCompressedTexImage3D") && index == 8 && count >= 9) return a[7]; + if ((NAME_IS("glTexParameteriv") || NAME_IS("glTexParameterfv") || + NAME_IS("glSamplerParameterfv")) && index == (NAME_IS("glSamplerParameterfv") ? 2u : 2u)) + return (a[1] == 0x1004u /* GL_TEXTURE_BORDER_COLOR */ || + a[1] == 0x8E46u /* GL_TEXTURE_SWIZZLE_RGBA */) ? 4 * sizeof(GLint) : sizeof(GLint); + if (NAME_IS("glDrawBuffers") && index == 1 && count >= 2) + { + n = (uint64_t)a[0] * sizeof(GLenum); + return n <= SIZE_MAX ? (size_t)n : 0; + } + if (NAME_IS("glBufferData") && index == 2 && count >= 4) return (size_t)a[1]; + if (NAME_IS("glBufferSubData") && index == 3 && count >= 4) return (size_t)a[2]; + if ((NAME_IS("glVertexAttribPointer") && index == 5) || + (NAME_IS("glVertexAttribIPointer") && index == 4)) + { + GLint bound = 0; + if (!get_integer(GL_ARRAY_BUFFER_BINDING, &bound)) return 0; + return bound ? SIZE_MAX : 0; /* VBO byte offset, not a guest pointer. */ + } + if (NAME_IS("glVertexAttrib4fv") && index == 1) return 4 * sizeof(GLfloat); + if ((NAME_IS("glDrawElements") && index == 3) || + (NAME_IS("glDrawElementsBaseVertex") && index == 3)) + { + GLint bound = 0; + if (!get_integer(GL_ELEMENT_ARRAY_BUFFER_BINDING, &bound)) return 0; + if (bound) return SIZE_MAX; + size_t element = a[2] == GL_UNSIGNED_BYTE ? 1 : + a[2] == GL_UNSIGNED_SHORT ? 2 : a[2] == GL_UNSIGNED_INT ? 4 : 0; + n = (uint64_t)a[1] * element; + return element && n <= SIZE_MAX ? (size_t)n : 0; + } + if (NAME_IS("glGetShaderiv") && index == 2) return sizeof(GLint); + if (NAME_IS("glGetShaderInfoLog") && count >= 4) + { + if (index == 2) return sizeof(GLsizei); + if (index == 3) return a[1]; + } + if (NAME_IS("glGetProgramiv") && index == 2) return sizeof(GLint); + if (NAME_IS("glGetProgramInfoLog") && count >= 4) + { + if (index == 2) return sizeof(GLsizei); + if (index == 3) return a[1]; + } + if (NAME_IS("glUniform1iv") && index == 2 && count >= 3) + { + n = (uint64_t)a[1] * sizeof(GLint); + return n <= SIZE_MAX ? (size_t)n : 0; + } + if (NAME_IS("glUniform4fv") && index == 2 && count >= 3) + { + n = (uint64_t)a[1] * 4 * sizeof(GLfloat); + return n <= SIZE_MAX ? (size_t)n : 0; + } + if (NAME_IS("glGetQueryObjectuiv") && index == 2) return sizeof(GLuint); + if (NAME_IS("glShaderSource") && index == 3 && count >= 4) + { + n = (uint64_t)a[1] * sizeof(GLint); + return a[3] && n <= SIZE_MAX ? (size_t)n : 0; + } +#undef NAME_IS + return 0; +} + +static int is_string_arg(const char *name, unsigned index) +{ + return ((strcmp(name, "glBindAttribLocation") == 0 || + strcmp(name, "glBindFragDataLocation") == 0 || + strcmp(name, "glGetUniformLocation") == 0) && + ((strcmp(name, "glGetUniformLocation") == 0 && index == 1) || index == 2)); +} + +struct guest_gl_string_slot { GLenum name; uint32_t guest_va; }; +static struct guest_gl_string_slot gl_string_slots[8]; +static pthread_mutex_t gl_string_lock = PTHREAD_MUTEX_INITIALIZER; + +static const char *guest_string_bounded(uint32_t guest_va, size_t limit) +{ + for (size_t length = 0; length < limit; length++) + { + if (length > UINT32_MAX - guest_va) + return NULL; + const char *character = (const char *)mac_guest_address_resolve(guest_va + (uint32_t)length, 1); + if (!character) + return NULL; + if (*character == '\0') + return (const char *)mac_guest_address_resolve(guest_va, length + 1); + } + return NULL; +} + +static const char *guest_string(uint32_t guest_va) +{ + return guest_string_bounded(guest_va, 4096); +} + +uint32_t mac_macos_gl_token_for_guest_name(uint32_t guest_name_va) +{ + const char *name = guest_string(guest_name_va); + if (!name) + return 0; + return mac_macos_gl_token_for_name(name); +} + +static const GLchar **shader_source_strings(uint32_t guest_array_va, + uint32_t count, uint32_t lengths_va) +{ + static _Thread_local const GLchar *strings[256]; + if (count > 256 || (count && !guest_array_va)) + return NULL; + for (uint32_t index = 0; index < count; index++) + { + uint32_t guest_string_va = 0; + uint64_t pointer_address = (uint64_t)guest_array_va + (uint64_t)index * sizeof(uint32_t); + if (pointer_address > UINT32_MAX || mac_guest_read((uint32_t)pointer_address, + &guest_string_va, sizeof(guest_string_va))) + return NULL; + if (lengths_va) + { + int32_t length = -1; + uint64_t length_address = (uint64_t)lengths_va + (uint64_t)index * sizeof(uint32_t); + if (length_address > UINT32_MAX || mac_guest_read((uint32_t)length_address, &length, sizeof(length)) || + length < 0) + strings[index] = guest_string_bounded(guest_string_va, 1024 * 1024); + else + strings[index] = (const GLchar *)mac_guest_address_resolve(guest_string_va, + (size_t)length); + } + else + strings[index] = guest_string_bounded(guest_string_va, 1024 * 1024); + if (!strings[index]) return NULL; + } + return strings; +} + +void *mac_macos_gl_guest_pointer(const char *name, unsigned arg_index, + uint32_t guest_va, const uint32_t *raw_args, unsigned arg_count) +{ + size_t bytes; + if (!name || (!raw_args && arg_count)) + return NULL; + if (strcmp(name, "glShaderSource") == 0 && arg_index == 2 && arg_count >= 4) + { + const GLchar **strings = shader_source_strings(guest_va, raw_args[1], raw_args[3]); + if (!strings && raw_args[1]) + { + fprintf(stderr, "[shader-source-failure] shader=%u count=%u vector=%08x lengths=%08x\n", + raw_args[0], raw_args[1], guest_va, raw_args[3]); + gl_bridge_fatal(name, "shader string vector is invalid or exceeds 256 entries"); + } + return (void *)strings; + } + if (is_string_arg(name, arg_index)) + return (void *)guest_string(guest_va); + if (((strcmp(name, "glReadPixels") == 0 && arg_index == 6) || + (strcmp(name, "glGetTexImage") == 0 && arg_index == 4)) && + pixel_buffer_is_bound(1)) + return (void *)(uintptr_t)guest_va; + if (((strcmp(name, "glTexImage2D") == 0 && arg_index == 8) || + (strcmp(name, "glTexImage3D") == 0 && arg_index == 9) || + (strcmp(name, "glTexSubImage2D") == 0 && arg_index == 8) || + (strcmp(name, "glCompressedTexImage2D") == 0 && arg_index == 7) || + (strcmp(name, "glCompressedTexImage3D") == 0 && arg_index == 8)) && + pixel_buffer_is_bound(0)) + return (void *)(uintptr_t)guest_va; + if ((strcmp(name, "glVertexAttribPointer") == 0 && arg_index == 5) || + (strcmp(name, "glVertexAttribIPointer") == 0 && arg_index == 4) || + ((strcmp(name, "glDrawElements") == 0 || + strcmp(name, "glDrawElementsBaseVertex") == 0) && arg_index == 3)) + { + bytes = gl_pointer_span(name, arg_index, raw_args, arg_count); + if (bytes == SIZE_MAX) + return (void *)(uintptr_t)guest_va; + } + else + bytes = gl_pointer_span(name, arg_index, raw_args, arg_count); + if (!bytes) + return NULL; + return mac_guest_address_resolve(guest_va, bytes); +} + +uint32_t mac_macos_gl_return_guest_pointer(const char *name, + const void *host_pointer, const uint32_t *raw_args, unsigned arg_count) +{ + uint32_t guest_va = 0; + if (!host_pointer) + return 0; + if (strcmp(name, "glGetString") == 0) + { + struct guest_gl_string_slot *slot = NULL; + uint32_t pname; + if (!raw_args || arg_count < 1) + gl_bridge_fatal(name, "missing GL string name argument"); + pname = raw_args[0]; + pthread_mutex_lock(&gl_string_lock); + for (size_t index = 0; index < sizeof(gl_string_slots) / sizeof(gl_string_slots[0]); index++) + { + if (gl_string_slots[index].guest_va && gl_string_slots[index].name == pname) + { + slot = &gl_string_slots[index]; + break; + } + if (!gl_string_slots[index].guest_va && !slot) + slot = &gl_string_slots[index]; + } + if (!slot) + { + pthread_mutex_unlock(&gl_string_lock); + gl_bridge_fatal(name, "guest GL string slots exhausted"); + } + if (!slot->guest_va) + { + if (mac_guest_allocate(4096, 16, &guest_va) != 0 || !guest_va) + { + pthread_mutex_unlock(&gl_string_lock); + gl_bridge_fatal(name, "guest scratch allocator could not allocate GL string storage"); + } + slot->name = pname; + slot->guest_va = guest_va; + } + guest_va = slot->guest_va; + pthread_mutex_unlock(&gl_string_lock); + const char *text = (const char *)host_pointer; + const char *end = memchr(text, '\0', 4096); + if (!end) + gl_bridge_fatal(name, "native GL string exceeds 4095 bytes"); + size_t bytes = (size_t)(end - text) + 1; + char *destination = (char *)mac_guest_address_resolve(guest_va, bytes); + if (!destination) + gl_bridge_fatal(name, "guest scratch string allocation is not mapped"); + memcpy(destination, text, bytes); + return guest_va; + } + if (mac_guest_address_from_host(host_pointer, 1, &guest_va) != 0) + gl_bridge_fatal(name, "native pointer result has no guest address mapping"); + return guest_va; +} diff --git a/port/macos/gl_bridge_support.h b/port/macos/gl_bridge_support.h new file mode 100644 index 0000000000..f215091486 --- /dev/null +++ b/port/macos/gl_bridge_support.h @@ -0,0 +1,20 @@ +#ifndef HALO_MACOS_GL_BRIDGE_SUPPORT_H +#define HALO_MACOS_GL_BRIDGE_SUPPORT_H + +#include +#include + +/* Used by generated i686 cdecl wrappers in macos_gl_token_wrappers.c. */ +void *mac_macos_gl_guest_pointer(const char *name, unsigned arg_index, + uint32_t guest_va, const uint32_t *raw_args, unsigned arg_count); +uint32_t mac_macos_gl_return_guest_pointer(const char *name, + const void *host_pointer, const uint32_t *raw_args, unsigned arg_count); +void mac_macos_gl_bridge_fault(const char *name, const char *reason); +int mac_guest_allocate(size_t size, size_t alignment, uint32_t *guest_va_out); +uint32_t mac_macos_gl_token_for_guest_name(uint32_t guest_name_va); + +/* Exposed for the focused fixture and shared byte-size checks. */ +size_t mac_macos_gl_pixel_span(uint32_t width, uint32_t height, + uint32_t format, uint32_t type, uint32_t alignment); + +#endif diff --git a/port/macos/gl_host_imports.c b/port/macos/gl_host_imports.c new file mode 100644 index 0000000000..7844b9115d --- /dev/null +++ b/port/macos/gl_host_imports.c @@ -0,0 +1,361 @@ +#include "gl_host_imports.h" + +#include "guest_address.h" +#include "guest_call.h" +#include "host_imports.h" +#include "gl41_compat.h" +#include "gl_bridge_support.h" +#include "gl_native_proc.h" +#include "host_services.h" + +#include +#include + +#include +#include +#include +#include + +#define FRAME_FENCE_SLOTS 8u +#define GUEST_STRING_LIMIT 4096u + +typedef const GLubyte *(APIENTRYP gl_get_string_fn)(GLenum); +typedef const GLubyte *(APIENTRYP gl_get_string_i_fn)(GLenum, GLuint); +typedef void (APIENTRYP gl_get_integer_fn)(GLenum, GLint *); +typedef void (APIENTRYP gl_get_buffer_sub_data_fn)(GLenum, GLintptr, GLsizeiptr, void *); +typedef void *(APIENTRYP gl_map_buffer_range_fn)(GLenum, GLintptr, GLsizeiptr, GLbitfield); +typedef GLboolean (APIENTRYP gl_unmap_buffer_fn)(GLenum); +typedef void (APIENTRYP gl_buffer_sub_data_fn)(GLenum, GLintptr, GLsizeiptr, const void *); +typedef GLsync (APIENTRYP gl_fence_sync_fn)(GLenum, GLbitfield); +typedef GLenum (APIENTRYP gl_client_wait_sync_fn)(GLsync, GLbitfield, GLuint64); +typedef void (APIENTRYP gl_delete_sync_fn)(GLsync); + +static GLsync frame_fences[FRAME_FENCE_SLOTS]; + +static void import_fail(const char *name, const char *reason) +{ + char message[256]; + snprintf(message, sizeof(message), "OpenGL host import %s: %s", name, reason); + mac_host_abort(message); +} + +static uint32_t argument(uint32_t index, const char *name) +{ + (void)name; + return mac_guest_host_import_arg32(index); +} + +static void cdecl_return(const char *name) +{ + (void)name; + mac_guest_host_import_return_void(); +} + +static void cdecl_return32(uint32_t value) +{ + mac_guest_host_import_return32(value); +} + +static const char *guest_c_string(uint32_t guest_va, const char *name) +{ + for (uint32_t length = 0; length < GUEST_STRING_LIMIT; length++) + { + uint64_t address = (uint64_t)guest_va + length; + const char *character; + if (address > UINT32_MAX || !(character = mac_guest_address_resolve((uint32_t)address, 1))) + return NULL; + if (!*character) + return mac_guest_address_resolve(guest_va, (size_t)length + 1); + } + (void)name; + return NULL; +} + +static void import_get_string(void) +{ + const char *name = "host_gl_get_string"; + GLenum pname = argument(0, name); + int32_t index = (int32_t)argument(1, name); + uint32_t buffer_va = argument(2, name); + uint32_t capacity = argument(3, name); + char *buffer; + const GLubyte *text = NULL; + if (!capacity) + goto done; + buffer = mac_guest_address_resolve(buffer_va, capacity); + if (!buffer) + import_fail(name, "output string buffer is unmapped"); + buffer[0] = '\0'; + if (index >= 0) + { + gl_get_string_i_fn get_string_i = (gl_get_string_i_fn)mac_macos_gl_native_proc("glGetStringi"); + if (!get_string_i) + import_fail(name, "glGetStringi is unavailable"); + text = get_string_i(pname, (GLuint)index); + } + else + { + gl_get_string_fn get_string = (gl_get_string_fn)mac_macos_gl_native_proc("glGetString"); + if (!get_string) + import_fail(name, "glGetString is unavailable"); + text = get_string(pname); + } + if (text) + { + uint32_t offset = 0; + while (offset + 1 < capacity && text[offset]) + { + buffer[offset] = (char)text[offset]; + offset++; + } + buffer[offset] = '\0'; + } +done: + cdecl_return(name); +} + +static void import_has_extension(void) +{ + const char *name = "host_gl_has_extension"; + const char *wanted = guest_c_string(argument(0, name), name); + GLint count = 0; + int found = 0; + gl_get_integer_fn get_integer; + gl_get_string_i_fn get_string_i; + if (!wanted) + import_fail(name, "extension name is unmapped or unterminated"); + get_integer = (gl_get_integer_fn)mac_macos_gl_native_proc("glGetIntegerv"); + get_string_i = (gl_get_string_i_fn)mac_macos_gl_native_proc("glGetStringi"); + if (!get_integer || !get_string_i) + import_fail(name, "extension enumeration requires glGetIntegerv and glGetStringi"); + get_integer(GL_NUM_EXTENSIONS, &count); + if (count < 0 || count > 65536) + import_fail(name, "driver returned an invalid extension count"); + for (GLint index = 0; index < count; index++) + { + const GLubyte *extension = get_string_i(GL_EXTENSIONS, (GLuint)index); + if (extension && strcmp((const char *)extension, wanted) == 0) + { + found = 1; + break; + } + } + cdecl_return32((uint32_t)found); +} + +static void import_read_buffer_word(void) +{ + const char *name = "host_gl_read_buffer_word"; + GLuint buffer = argument(0, name); + uint32_t offset = argument(1, name); + GLint previous = 0; + uint32_t value = 0; + gl_get_integer_fn get_integer = (gl_get_integer_fn)mac_macos_gl_native_proc("glGetIntegerv"); + gl_get_buffer_sub_data_fn get_sub_data = + (gl_get_buffer_sub_data_fn)mac_macos_gl_native_proc("glGetBufferSubData"); + typedef void (APIENTRYP bind_buffer_fn)(GLenum, GLuint); + bind_buffer_fn bind_buffer = (bind_buffer_fn)mac_macos_gl_native_proc("glBindBuffer"); + if (!get_integer || !get_sub_data || !bind_buffer) + import_fail(name, "buffer readback functions are unavailable"); + get_integer(GL_ARRAY_BUFFER_BINDING, &previous); + bind_buffer(GL_ARRAY_BUFFER, buffer); + get_sub_data(GL_ARRAY_BUFFER, (GLintptr)offset, sizeof(value), &value); + bind_buffer(GL_ARRAY_BUFFER, (GLuint)previous); + cdecl_return32(value); +} + +static void import_buffer_write(void) +{ + const char *name = "host_gl_buffer_write"; + GLenum target = argument(0, name); + uint32_t offset = argument(1, name); + uint32_t size = argument(2, name); + uint32_t data_va = argument(3, name); + const void *data; + gl_map_buffer_range_fn map_range; + gl_unmap_buffer_fn unmap; + gl_buffer_sub_data_fn sub_data; + void *mapping; + if (!size) + { + cdecl_return(name); + return; + } + data = mac_guest_address_resolve(data_va, size); + if (!data) + import_fail(name, "source data span is unmapped"); + map_range = (gl_map_buffer_range_fn)mac_macos_gl_native_proc("glMapBufferRange"); + unmap = (gl_unmap_buffer_fn)mac_macos_gl_native_proc("glUnmapBuffer"); + sub_data = (gl_buffer_sub_data_fn)mac_macos_gl_native_proc("glBufferSubData"); + if (!map_range || !unmap || !sub_data) + import_fail(name, "buffer update functions are unavailable"); + mapping = map_range(target, (GLintptr)offset, (GLsizeiptr)size, + GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT | GL_MAP_INVALIDATE_RANGE_BIT); + if (!mapping) + sub_data(target, (GLintptr)offset, (GLsizeiptr)size, data); + else + { + memcpy(mapping, data, size); + if (!unmap(target)) + import_fail(name, "mapped buffer became corrupt during update"); + } + cdecl_return(name); +} + +static void import_fence_frame(void) +{ + static unsigned trace_calls; + int trace = getenv("HALO_GL_FENCE_TRACE") != NULL; + uint64_t begun_ns = trace ? SDL_GetTicksNS() : 0; + const char *name = "host_gl_fence_frame"; + uint32_t slot = argument(0, name); + gl_delete_sync_fn delete_sync; + gl_fence_sync_fn fence_sync; + if (slot >= FRAME_FENCE_SLOTS) + { + cdecl_return(name); + return; + } + delete_sync = (gl_delete_sync_fn)mac_macos_gl_native_proc("glDeleteSync"); + fence_sync = (gl_fence_sync_fn)mac_macos_gl_native_proc("glFenceSync"); + if (!delete_sync || !fence_sync) + import_fail(name, "GL sync functions are unavailable"); + if (frame_fences[slot]) + delete_sync(frame_fences[slot]); + frame_fences[slot] = fence_sync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); + if (trace && ++trace_calls <= 500) + fprintf(stderr, "[gl-fence] call=%u slot=%u elapsed_ms=%.3f context=%p thread=%llu valid=%d\n", + trace_calls, slot, (SDL_GetTicksNS() - begun_ns) / 1e6, + (void *)SDL_GL_GetCurrentContext(), (unsigned long long)SDL_GetCurrentThreadID(), + frame_fences[slot] != NULL); + cdecl_return(name); +} + +static void import_wait_frame(void) +{ + static unsigned trace_calls; + int trace = getenv("HALO_GL_FENCE_TRACE") != NULL; + uint64_t begun_ns = trace ? SDL_GetTicksNS() : 0; + const char *name = "host_gl_wait_frame"; + uint32_t slot = argument(0, name); + gl_client_wait_sync_fn wait_sync; + gl_delete_sync_fn delete_sync; + if (slot >= FRAME_FENCE_SLOTS || !frame_fences[slot]) + { + cdecl_return(name); + return; + } + wait_sync = (gl_client_wait_sync_fn)mac_macos_gl_native_proc("glClientWaitSync"); + delete_sync = (gl_delete_sync_fn)mac_macos_gl_native_proc("glDeleteSync"); + if (!wait_sync || !delete_sync) + import_fail(name, "GL sync functions are unavailable"); + GLenum result = wait_sync(frame_fences[slot], GL_SYNC_FLUSH_COMMANDS_BIT, UINT64_C(1000000000)); + delete_sync(frame_fences[slot]); + frame_fences[slot] = NULL; + if (trace && ++trace_calls <= 500) + fprintf(stderr, "[gl-fence-wait] call=%u slot=%u elapsed_ms=%.3f polls=1 status=0x%x failed=%d context=%p thread=%llu\n", + trace_calls, slot, (SDL_GetTicksNS() - begun_ns) / 1e6, result, + result == GL_WAIT_FAILED, (void *)SDL_GL_GetCurrentContext(), + (unsigned long long)SDL_GetCurrentThreadID()); + if (result == GL_WAIT_FAILED) + import_fail(name, "GPU frame fence wait failed"); + cdecl_return(name); +} + +static void import_guest_gl_get_proc_address(void) +{ + const uint32_t name_va = argument(0, "guest_gl_get_proc_address"); + cdecl_return32(mac_macos_gl_token_for_guest_name(name_va)); +} + +typedef void (APIENTRYP gl_enable_fn)(GLenum); +typedef GLboolean (APIENTRYP gl_is_enabled_fn)(GLenum); +typedef void (APIENTRYP gl_gen_framebuffers_fn)(GLsizei, GLuint *); +typedef void (APIENTRYP gl_bind_framebuffer_fn)(GLenum, GLuint); +typedef void (APIENTRYP gl_framebuffer_texture_2d_fn)(GLenum, GLenum, GLenum, GLuint, GLint); +typedef void (APIENTRYP gl_read_buffer_fn)(GLenum); +typedef void (APIENTRYP gl_draw_buffers_fn)(GLsizei, const GLenum *); +typedef GLenum (APIENTRYP gl_check_framebuffer_status_fn)(GLenum); +typedef void (APIENTRYP gl_blit_framebuffer_fn)(GLint, GLint, GLint, GLint, + GLint, GLint, GLint, GLint, GLbitfield, GLenum); + +static void native_draw_buffer(uint32_t buffer) +{ + gl_draw_buffers_fn draw_buffers = (gl_draw_buffers_fn)mac_macos_gl_native_proc("glDrawBuffers"); + GLenum target = (GLenum)buffer; + if (!draw_buffers) + import_fail("halo_gl41_copy_image_2d", "glDrawBuffers is unavailable"); + draw_buffers(1, &target); +} + +static int make_native_copy_api(struct halo_gl41_copy_api *api) +{ + api->get_integer = (void (*)(uint32_t, int32_t *))mac_macos_gl_native_proc("glGetIntegerv"); + api->is_enabled = (uint8_t (*)(uint32_t))mac_macos_gl_native_proc("glIsEnabled"); + api->enable = (void (*)(uint32_t))mac_macos_gl_native_proc("glEnable"); + api->disable = (void (*)(uint32_t))mac_macos_gl_native_proc("glDisable"); + api->gen_framebuffers = (void (*)(int32_t, uint32_t *))mac_macos_gl_native_proc("glGenFramebuffers"); + api->bind_framebuffer = (void (*)(uint32_t, uint32_t))mac_macos_gl_native_proc("glBindFramebuffer"); + api->framebuffer_texture_2d = (void (*)(uint32_t, uint32_t, uint32_t, uint32_t, int32_t)) + mac_macos_gl_native_proc("glFramebufferTexture2D"); + api->read_buffer = (void (*)(uint32_t))mac_macos_gl_native_proc("glReadBuffer"); + api->draw_buffer = native_draw_buffer; + api->check_framebuffer_status = (uint32_t (*)(uint32_t)) + mac_macos_gl_native_proc("glCheckFramebufferStatus"); + api->blit_framebuffer = (void (*)(int32_t, int32_t, int32_t, int32_t, + int32_t, int32_t, int32_t, int32_t, uint32_t, uint32_t)) + mac_macos_gl_native_proc("glBlitFramebuffer"); + return api->get_integer && api->is_enabled && api->enable && api->disable && + api->gen_framebuffers && api->bind_framebuffer && api->framebuffer_texture_2d && + api->read_buffer && api->draw_buffer && api->check_framebuffer_status && + api->blit_framebuffer; +} + +static void import_gl41_copy_image_2d(void) +{ + const char *name = "halo_gl41_copy_image_2d"; + uint32_t api_va = argument(0, name); + uint32_t state_va = argument(1, name); + struct halo_gl41_copy_api native_api; + struct halo_gl41_copy_state state; + int result; + /* The guest API table contains synthetic 32-bit GL callable tokens. Check + * that the expected object exists, but never cast those tokens to pointers. */ + if (!api_va || !mac_guest_address_resolve(api_va, 11u * sizeof(uint32_t))) + import_fail(name, "guest GL API descriptor is unmapped"); + if (!state_va || mac_guest_read(state_va, &state, sizeof(state)) != 0) + import_fail(name, "guest copy-state object is unmapped"); + if (!make_native_copy_api(&native_api)) + import_fail(name, "required native OpenGL 4.1 entry point is unavailable"); + result = mac_gl41_copy_image_2d(&native_api, &state, + argument(2, name), (int32_t)argument(3, name), + argument(4, name), (int32_t)argument(5, name), + (int32_t)argument(6, name), (int32_t)argument(7, name)); + if (mac_guest_write(state_va, &state, sizeof(state)) != 0) + import_fail(name, "cannot write updated private FBO state to guest memory"); + cdecl_return32((uint32_t)result); +} + +mac_guest_import_bridge mac_macos_gl_host_import_bridge(const char *name) +{ + static const struct + { + const char *name; + mac_guest_import_bridge bridge; + } imports[] = { + { "guest_gl_get_proc_address", import_guest_gl_get_proc_address }, + { "halo_gl41_copy_image_2d", import_gl41_copy_image_2d }, + { "host_gl_buffer_write", import_buffer_write }, + { "host_gl_fence_frame", import_fence_frame }, + { "host_gl_get_string", import_get_string }, + { "host_gl_has_extension", import_has_extension }, + { "host_gl_read_buffer_word", import_read_buffer_word }, + { "host_gl_wait_frame", import_wait_frame }, + }; + for (size_t index = 0; index < sizeof(imports) / sizeof(imports[0]); index++) + { + if (name && strcmp(name, imports[index].name) == 0) + return imports[index].bridge; + } + return NULL; +} diff --git a/port/macos/gl_host_imports.h b/port/macos/gl_host_imports.h new file mode 100644 index 0000000000..fb4416b546 --- /dev/null +++ b/port/macos/gl_host_imports.h @@ -0,0 +1,10 @@ +#ifndef HALO_MACOS_GL_HOST_IMPORTS_H +#define HALO_MACOS_GL_HOST_IMPORTS_H + +#include "guest_import_registry.h" + +/* Return the cdecl host bridge for one Android renderer host_gl_* import, or + * NULL. The global host-import registry owns deterministic token ordering. */ +mac_guest_import_bridge mac_macos_gl_host_import_bridge(const char *name); + +#endif diff --git a/port/macos/gl_native_proc.c b/port/macos/gl_native_proc.c new file mode 100644 index 0000000000..3fa3268826 --- /dev/null +++ b/port/macos/gl_native_proc.c @@ -0,0 +1,105 @@ +#include "gl_native_proc.h" + +#include + +#include +#include +#include + +#define GL_PROC_CACHE_CAPACITY 256 +#define GL_PROC_NAME_CAPACITY 128 + +struct gl_proc_cache_entry +{ + SDL_GLContext context; + char name[GL_PROC_NAME_CAPACITY]; + void *address; + unsigned hash; + int occupied; +}; + +static struct gl_proc_cache_entry gl_proc_cache[GL_PROC_CACHE_CAPACITY]; +static pthread_mutex_t gl_proc_cache_mutex = PTHREAD_MUTEX_INITIALIZER; +static int no_context_reported; + +void mac_macos_gl_native_proc_clear(void) +{ + pthread_mutex_lock(&gl_proc_cache_mutex); + memset(gl_proc_cache, 0, sizeof(gl_proc_cache)); + no_context_reported = 0; + pthread_mutex_unlock(&gl_proc_cache_mutex); +} + +void *mac_macos_gl_native_proc(const char *name) +{ + SDL_GLContext context; + size_t name_length; + size_t free_slot = GL_PROC_CACHE_CAPACITY; + void *address; + unsigned hash = 2166136261u; + + if (!name || !name[0]) + return NULL; + name_length = strlen(name); + if (name_length >= GL_PROC_NAME_CAPACITY) + { + fprintf(stderr, "[mac-gl] refusing overlong GL entry-point name\n"); + return NULL; + } + for (size_t index = 0; index < name_length; ++index) + hash = (hash ^ (unsigned char)name[index]) * 16777619u; + context = SDL_GL_GetCurrentContext(); + if (!context) + { + pthread_mutex_lock(&gl_proc_cache_mutex); + if (!no_context_reported) + { + fprintf(stderr, "[mac-gl] GL proc lookup requested without a current SDL context\n"); + no_context_reported = 1; + } + pthread_mutex_unlock(&gl_proc_cache_mutex); + return NULL; + } + + pthread_mutex_lock(&gl_proc_cache_mutex); + /* Look up by name hash instead of comparing every cached GL name on + * each draw. Context remains part of the key; clear still invalidates all. */ + for (size_t probe = 0; probe < GL_PROC_CACHE_CAPACITY; probe++) + { + size_t index = (hash + probe) % GL_PROC_CACHE_CAPACITY; + struct gl_proc_cache_entry *entry = &gl_proc_cache[index]; + if (!entry->occupied) + { + if (free_slot == GL_PROC_CACHE_CAPACITY) + free_slot = index; + break; + } + if (entry->hash == hash && entry->context == context && strcmp(entry->name, name) == 0) + { + address = entry->address; + pthread_mutex_unlock(&gl_proc_cache_mutex); + return address; + } + } + + /* SDL requires a current GL context for portable proc-address lookup. */ + SDL_FunctionPointer function = SDL_GL_GetProcAddress(name); + address = (void *)function; + if (!address) + fprintf(stderr, "[mac-gl] SDL has no current-context entry point for %s: %s\n", + name, SDL_GetError()); + if (free_slot == GL_PROC_CACHE_CAPACITY) + { + pthread_mutex_unlock(&gl_proc_cache_mutex); + fprintf(stderr, "[mac-gl] GL proc cache is full; cannot cache %s\n", name); + return address; + } + struct gl_proc_cache_entry *entry = &gl_proc_cache[free_slot]; + entry->context = context; + memcpy(entry->name, name, name_length + 1); + entry->address = address; + entry->hash = hash; + entry->occupied = 1; + pthread_mutex_unlock(&gl_proc_cache_mutex); + return address; +} diff --git a/port/macos/gl_native_proc.h b/port/macos/gl_native_proc.h new file mode 100644 index 0000000000..d309c04967 --- /dev/null +++ b/port/macos/gl_native_proc.h @@ -0,0 +1,19 @@ +#ifndef HALO_MACOS_GL_NATIVE_PROC_H +#define HALO_MACOS_GL_NATIVE_PROC_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* Resolve a native GL entry point for the SDL GL context current on this + * thread. Returns NULL if there is no current context or SDL has no entry. */ +void *mac_macos_gl_native_proc(const char *name); + +/* Drop cached addresses when the owning SDL GL context is destroyed. */ +void mac_macos_gl_native_proc_clear(void); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/port/macos/gl_token_registry.h b/port/macos/gl_token_registry.h new file mode 100644 index 0000000000..c1fc4a8c9f --- /dev/null +++ b/port/macos/gl_token_registry.h @@ -0,0 +1,12 @@ +#ifndef HALO_MACOS_GL_TOKEN_REGISTRY_H +#define HALO_MACOS_GL_TOKEN_REGISTRY_H + +#include + +typedef void (*mac_macos_gl_guest_function)(void); + +/* Generated procedure-name/token registry for i686 GL function pointers. */ +uint32_t mac_macos_gl_token_for_name(const char *name); +mac_macos_gl_guest_function mac_macos_gl_wrapper_for_token(uint32_t token); + +#endif diff --git a/port/macos/guest_address.c b/port/macos/guest_address.c new file mode 100644 index 0000000000..5129eb24f0 --- /dev/null +++ b/port/macos/guest_address.c @@ -0,0 +1,316 @@ +#include "guest_address.h" + +#include +#include +#include +#include + +#define GUEST_ADDRESS_RANGE_LIMIT 64 +#define GUEST_ADDRESS_SPACE_SIZE (UINT64_C(1) << 32) + +struct guest_address_range +{ + uint32_t guest_base; + uint64_t size; + uintptr_t host_base; + int owns_mapping; + int occupied; +}; + +static struct guest_address_range ranges[GUEST_ADDRESS_RANGE_LIMIT]; +static pthread_mutex_t ranges_lock = PTHREAD_MUTEX_INITIALIZER; +static void *resolve_guest_address(uint32_t guest_address, size_t size, + int allow_null_address); + +size_t mac_guest_address_host_page_size(void) +{ + long page_size = sysconf(_SC_PAGESIZE); + return page_size > 0 ? (size_t)page_size : 0; +} + +static int span_end(uint64_t base, size_t size, uint64_t *end_out) +{ + uint64_t end = base + (uint64_t)size; + + if (end < base) + return -1; + *end_out = end; + return 0; +} + +static int host_page_span(uint32_t guest_address, size_t size, + uint32_t *aligned_address_out, size_t *aligned_size_out) +{ + size_t page_size = mac_guest_address_host_page_size(); + uint64_t end, aligned_end; + uint64_t mask; + + if (!page_size || (page_size & (page_size - 1)) != 0 || !size || + span_end(guest_address, size, &end) != 0 || end > GUEST_ADDRESS_SPACE_SIZE) + return -1; + mask = (uint64_t)page_size - 1; + aligned_end = (end + mask) & ~mask; + if (aligned_end > GUEST_ADDRESS_SPACE_SIZE || aligned_end <= (guest_address & ~mask)) + return -1; + *aligned_address_out = (uint32_t)((uint64_t)guest_address & ~mask); + *aligned_size_out = (size_t)(aligned_end - *aligned_address_out); + return 0; +} + +int mac_guest_address_register(uint32_t guest_base, void *host_base, size_t size) +{ + uint64_t guest_end; + uintptr_t host_start = (uintptr_t)host_base; + size_t index, free_index = GUEST_ADDRESS_RANGE_LIMIT; + int result = -1; + + if (!host_base || !size || span_end(guest_base, size, &guest_end) != 0 || + guest_end > GUEST_ADDRESS_SPACE_SIZE || size > UINTPTR_MAX - host_start) + return -1; + + pthread_mutex_lock(&ranges_lock); + for (index = 0; index < GUEST_ADDRESS_RANGE_LIMIT; index++) + { + const struct guest_address_range *range = &ranges[index]; + uint64_t range_guest_end; + + if (!range->occupied) + { + if (free_index == GUEST_ADDRESS_RANGE_LIMIT) + free_index = index; + continue; + } + range_guest_end = (uint64_t)range->guest_base + range->size; + if ((uint64_t)guest_base < range_guest_end && range->guest_base < guest_end) + goto done; + } + if (free_index != GUEST_ADDRESS_RANGE_LIMIT) + { + ranges[free_index].guest_base = guest_base; + ranges[free_index].size = size; + ranges[free_index].host_base = host_start; + ranges[free_index].occupied = 1; + result = 0; + } + +done: + pthread_mutex_unlock(&ranges_lock); + return result; +} + +void *mac_guest_address_reserve(uint32_t guest_base, size_t size) +{ + void *host_base; + size_t index; + + if (!size) + return NULL; + host_base = mmap(NULL, size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (host_base == MAP_FAILED) + return NULL; + if (mac_guest_address_register(guest_base, host_base, size) != 0) + { + munmap(host_base, size); + return NULL; + } + pthread_mutex_lock(&ranges_lock); + for (index = 0; index < GUEST_ADDRESS_RANGE_LIMIT; index++) + { + if (ranges[index].occupied && ranges[index].guest_base == guest_base) + { + ranges[index].owns_mapping = 1; + break; + } + } + pthread_mutex_unlock(&ranges_lock); + return host_base; +} + +int mac_guest_address_commit(uint32_t guest_address, size_t size, int protection) +{ + void *host_address; + size_t page_size = mac_guest_address_host_page_size(); + size_t aligned_size; + uint32_t aligned_address; + + if (!page_size || host_page_span(guest_address, size, &aligned_address, &aligned_size) != 0 || + !(host_address = resolve_guest_address(aligned_address, aligned_size, 1)) || + (uintptr_t)host_address % page_size) + return -1; + return mprotect(host_address, aligned_size, protection); +} + +int mac_guest_address_protect(uint32_t guest_address, size_t size, int protection) +{ + void *host_address; + size_t page_size = mac_guest_address_host_page_size(); + size_t aligned_size; + uint32_t aligned_address; + + if (!page_size || host_page_span(guest_address, size, &aligned_address, &aligned_size) != 0 || + !(host_address = resolve_guest_address(aligned_address, aligned_size, 1)) || + (uintptr_t)host_address % page_size) + return -1; + return mprotect(host_address, aligned_size, protection); +} + +int mac_guest_address_unregister(uint32_t guest_base) +{ + size_t index; + int result = -1; + + pthread_mutex_lock(&ranges_lock); + for (index = 0; index < GUEST_ADDRESS_RANGE_LIMIT; index++) + { + if (ranges[index].occupied && ranges[index].guest_base == guest_base) + { + if (ranges[index].owns_mapping) + munmap((void *)ranges[index].host_base, (size_t)ranges[index].size); + ranges[index] = (struct guest_address_range){ 0 }; + result = 0; + break; + } + } + pthread_mutex_unlock(&ranges_lock); + return result; +} + +static void *resolve_guest_address(uint32_t guest_address, size_t size, + int allow_null_address) +{ + uint64_t guest_end; + void *result = NULL; + size_t index; + + if ((!allow_null_address && !guest_address) || span_end(guest_address, size, &guest_end) != 0 || + guest_end > GUEST_ADDRESS_SPACE_SIZE) + return NULL; + pthread_mutex_lock(&ranges_lock); + for (index = 0; index < GUEST_ADDRESS_RANGE_LIMIT; index++) + { + const struct guest_address_range *range = &ranges[index]; + uint64_t range_end; + + if (!range->occupied) + continue; + range_end = (uint64_t)range->guest_base + range->size; + if (guest_address >= range->guest_base && guest_end <= range_end) + { + result = (void *)(range->host_base + (guest_address - range->guest_base)); + break; + } + } + pthread_mutex_unlock(&ranges_lock); + return result; +} + +void *mac_guest_address_resolve(uint32_t guest_address, size_t size) +{ + return resolve_guest_address(guest_address, size, 0); +} + +size_t mac_guest_address_available(uint32_t guest_address) +{ + size_t available = 0; + size_t index; + + if (!guest_address) + return 0; + pthread_mutex_lock(&ranges_lock); + for (index = 0; index < GUEST_ADDRESS_RANGE_LIMIT; index++) + { + const struct guest_address_range *range = &ranges[index]; + uint64_t range_end; + + if (!range->occupied || guest_address < range->guest_base) + continue; + range_end = (uint64_t)range->guest_base + range->size; + if (guest_address < range_end) + { + uint64_t remaining = range_end - guest_address; + available = remaining > SIZE_MAX ? SIZE_MAX : (size_t)remaining; + break; + } + } + pthread_mutex_unlock(&ranges_lock); + return available; +} + +int mac_guest_address_from_host(const void *host_address, size_t size, + uint32_t *guest_address_out) +{ + uintptr_t host_start = (uintptr_t)host_address; + uintptr_t host_end; + size_t index; + int found = 0; + int result = -1; + + if (!host_address || !guest_address_out || size > UINTPTR_MAX - host_start) + return -1; + host_end = host_start + size; + pthread_mutex_lock(&ranges_lock); + for (index = 0; index < GUEST_ADDRESS_RANGE_LIMIT; index++) + { + const struct guest_address_range *range = &ranges[index]; + uintptr_t range_end; + uint64_t guest_address; + + if (!range->occupied) + continue; + range_end = range->host_base + (uintptr_t)range->size; + if (host_start < range->host_base || host_end > range_end) + continue; + guest_address = (uint64_t)range->guest_base + (host_start - range->host_base); + if (guest_address == 0 || guest_address > UINT32_MAX) + break; + if (found) + { + result = -1; + found = 0; + break; + } + *guest_address_out = (uint32_t)guest_address; + found = 1; + } + if (found) + result = 0; + pthread_mutex_unlock(&ranges_lock); + return result; +} + +int mac_guest_address_linear_offset(uint32_t guest_base, const void *host_base, + ptrdiff_t *offset_out) +{ + size_t index; + int result = -1; + uintptr_t host = (uintptr_t)host_base; + + if (!host_base || !offset_out || host > (uintptr_t)PTRDIFF_MAX || + (uintptr_t)guest_base > (uintptr_t)PTRDIFF_MAX) + return -1; + pthread_mutex_lock(&ranges_lock); + for (index = 0; index < GUEST_ADDRESS_RANGE_LIMIT; index++) + { + if (ranges[index].occupied && ranges[index].guest_base == guest_base && + ranges[index].host_base == host) + { + *offset_out = (ptrdiff_t)host - (ptrdiff_t)guest_base; + result = 0; + break; + } + } + pthread_mutex_unlock(&ranges_lock); + return result; +} + +void mac_guest_address_reset(void) +{ + pthread_mutex_lock(&ranges_lock); + for (size_t index = 0; index < GUEST_ADDRESS_RANGE_LIMIT; index++) + { + if (ranges[index].occupied && ranges[index].owns_mapping) + munmap((void *)ranges[index].host_base, (size_t)ranges[index].size); + ranges[index] = (struct guest_address_range){ 0 }; + } + pthread_mutex_unlock(&ranges_lock); +} diff --git a/port/macos/guest_address.h b/port/macos/guest_address.h new file mode 100644 index 0000000000..dd290786a0 --- /dev/null +++ b/port/macos/guest_address.h @@ -0,0 +1,67 @@ +/* + * Guest virtual-address mapping helpers for the native macOS port. + * + * Xbox cache structures and guest ABIs contain 32-bit virtual addresses. + * Native arm64 host pointers are 64-bit and must never be truncated to fit + * those fields. Register each mapped guest range against its host backing + * and resolve guest addresses explicitly at the host boundary. + */ + +#ifndef HALO_MACOS_GUEST_ADDRESS_H +#define HALO_MACOS_GUEST_ADDRESS_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* Register one non-overlapping guest VA range and its host backing. Multiple + * guest ranges may alias the same host bytes. The entire range must fit in + * the 32-bit Xbox address space. Returns 0 on success, -1 for invalid, + * guest-overlapping, or full-table registrations. */ +int mac_guest_address_register(uint32_t guest_base, void *host_base, size_t size); + +/* Reserve a guest VA range in a high, OS-chosen host address range. The + * reservation is PROT_NONE and does not commit backing pages. This allows + * translated code to keep Xbox VAs while host code resolves through the + * registered offset map. Returns the host base or NULL. */ +void *mac_guest_address_reserve(uint32_t guest_base, size_t size); + +/* Commit anonymous pages in an owned reservation by changing their + * protection. File-backed mappings need a separate Darwin VM path. */ +int mac_guest_address_commit(uint32_t guest_address, size_t size, int protection); +int mac_guest_address_protect(uint32_t guest_address, size_t size, int protection); +size_t mac_guest_address_host_page_size(void); + +/* Remove a range previously registered at guest_base. Returns 0 on success. */ +int mac_guest_address_unregister(uint32_t guest_base); + +/* Resolve a guest VA span. The full span must lie in one registered range; + * overflow, unmapped spans and null guest VA return NULL. */ +void *mac_guest_address_resolve(uint32_t guest_address, size_t size); +/* Number of contiguous bytes available from guest_address to the end of its + * registered range, or zero if the address is not mapped/null. */ +size_t mac_guest_address_available(uint32_t guest_address); + +/* Convert a host span back to a guest VA. The full span must be contained in + * exactly one registered host backing; aliases are ambiguous and fail. + * Returns 0 on success and -1 otherwise. */ +int mac_guest_address_from_host(const void *host_address, size_t size, + uint32_t *guest_address_out); + +/* Offset suitable for the translated runtime's XBOX_PTR/ MEM macros. This is + * the host-base minus guest-base for a registered range and is valid only + * when all guest memory shares that one linear backing arena. */ +int mac_guest_address_linear_offset(uint32_t guest_base, const void *host_base, + ptrdiff_t *offset_out); + +/* Clear all registrations. Call only after guest threads have stopped. */ +void mac_guest_address_reset(void); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/port/macos/guest_allocator.c b/port/macos/guest_allocator.c new file mode 100644 index 0000000000..e3b60315e7 --- /dev/null +++ b/port/macos/guest_allocator.c @@ -0,0 +1,79 @@ +#include "guest_allocator.h" + +#include "guest_address.h" + +#include +#include + +static struct mac_guest_allocator guest_allocator; +static int guest_allocator_installed; +static pthread_mutex_t allocator_mutex = PTHREAD_MUTEX_INITIALIZER; + +static int valid_alignment(size_t alignment) +{ + return alignment && (alignment & (alignment - 1)) == 0; +} + +int mac_guest_allocator_install(const struct mac_guest_allocator *allocator) +{ + int result = -1; + + if (!allocator || !allocator->allocate || !allocator->release) + return -1; + pthread_mutex_lock(&allocator_mutex); + if (!guest_allocator_installed) + { + guest_allocator = *allocator; + guest_allocator_installed = 1; + result = 0; + } + pthread_mutex_unlock(&allocator_mutex); + return result; +} + +int mac_guest_allocate(size_t size, size_t alignment, uint32_t *guest_va_out) +{ + struct mac_guest_allocator allocator; + uint32_t guest_va = 0; + int result; + + if (!size || !guest_va_out || !valid_alignment(alignment)) + return -1; + pthread_mutex_lock(&allocator_mutex); + if (!guest_allocator_installed) + { + pthread_mutex_unlock(&allocator_mutex); + return -1; + } + allocator = guest_allocator; + pthread_mutex_unlock(&allocator_mutex); + result = allocator.allocate(allocator.context, size, alignment, &guest_va); + if (result != 0) + return result; + if (!guest_va || (guest_va & (alignment - 1)) != 0 || + !mac_guest_address_resolve(guest_va, size)) + { + allocator.release(allocator.context, guest_va, size); + return -1; + } + *guest_va_out = guest_va; + return 0; +} + +int mac_guest_release(uint32_t guest_va, size_t size) +{ + struct mac_guest_allocator allocator; + + if (!guest_va || !size || !mac_guest_address_resolve(guest_va, size)) + return -1; + pthread_mutex_lock(&allocator_mutex); + if (!guest_allocator_installed) + { + pthread_mutex_unlock(&allocator_mutex); + return -1; + } + allocator = guest_allocator; + pthread_mutex_unlock(&allocator_mutex); + allocator.release(allocator.context, guest_va, size); + return 0; +} diff --git a/port/macos/guest_allocator.h b/port/macos/guest_allocator.h new file mode 100644 index 0000000000..7f23b8ec43 --- /dev/null +++ b/port/macos/guest_allocator.h @@ -0,0 +1,22 @@ +#ifndef HALO_MACOS_GUEST_ALLOCATOR_H +#define HALO_MACOS_GUEST_ALLOCATOR_H + +#include +#include + +/* The platform layer must not pick guest VAs independently of the guest + * heap/stack map. The executable installs its real allocator here; returned + * spans must already be committed and registered with guest_address. */ +struct mac_guest_allocator +{ + void *context; + int (*allocate)(void *context, size_t size, size_t alignment, + uint32_t *guest_va_out); + void (*release)(void *context, uint32_t guest_va, size_t size); +}; + +int mac_guest_allocator_install(const struct mac_guest_allocator *allocator); +int mac_guest_allocate(size_t size, size_t alignment, uint32_t *guest_va_out); +int mac_guest_release(uint32_t guest_va, size_t size); + +#endif diff --git a/port/macos/guest_aot.ld b/port/macos/guest_aot.ld new file mode 100644 index 0000000000..5be899db3c --- /dev/null +++ b/port/macos/guest_aot.ld @@ -0,0 +1,43 @@ +OUTPUT_FORMAT(elf32-i386) +OUTPUT_ARCH(i386) +ENTRY(__guest_start) +PROVIDE(time = __time64); + +PHDRS +{ + text PT_LOAD FLAGS(5); + ro PT_LOAD FLAGS(4); + data PT_LOAD FLAGS(6); + tls PT_TLS FLAGS(4); + imports PT_LOAD FLAGS(5); +} + +SECTIONS +{ + . = 0x00010000; + .text : { *(.text .text.*) } :text + .rodata : { *(.rodata .rodata.*) } :ro + .preinit_array : { KEEP(*(.preinit_array*)) } :ro + .init_array : { + __guest_init_start = .; + KEEP(*(SORT_BY_INIT_PRIORITY(.init_array.*))) + KEEP(*(.init_array)) + __guest_init_end = .; + } :ro + .fini_array : { + __fini_array_start = .; + KEEP(*(SORT_BY_INIT_PRIORITY(.fini_array.*))) + KEEP(*(.fini_array)) + __fini_array_end = .; + } :ro + .data : { *(.data .data.*) } :data + .tdata : { __tls_start = .; *(.tdata .tdata.*) } :data :tls + .tbss (NOLOAD) : { *(.tbss .tbss.*) } :data :tls + __tls_end = .; + .bss (NOLOAD) : { *(.bss .bss.* COMMON) } :data + . = ALIGN(0x1000); + __guest_image_end = .; + + . = 0x70000000; + .host_imports : { KEEP(*(.host_imports)) } :imports +} diff --git a/port/macos/guest_call.c b/port/macos/guest_call.c new file mode 100644 index 0000000000..31daa8515a --- /dev/null +++ b/port/macos/guest_call.c @@ -0,0 +1,72 @@ +#include "guest_call.h" + +#include "guest_address.h" +#include + +int mac_guest_read(uint32_t guest_address, void *destination, size_t size) +{ + const void *source; + + if (!destination || !(source = mac_guest_address_resolve(guest_address, size))) + return -1; + memcpy(destination, source, size); + return 0; +} + +int mac_guest_write(uint32_t guest_address, const void *source, size_t size) +{ + void *destination; + + if (!source || !(destination = mac_guest_address_resolve(guest_address, size))) + return -1; + memcpy(destination, source, size); + return 0; +} + +int mac_guest_stack_return(uint32_t guest_esp, uint32_t *return_va_out) +{ + return mac_guest_read(guest_esp, return_va_out, sizeof(*return_va_out)); +} + +int mac_guest_stack_arg_span(uint32_t guest_esp, uint32_t argument_index, + size_t size, void **span_out) +{ + uint64_t address = (uint64_t)guest_esp + 4u + (uint64_t)argument_index * 4u; + + if (!span_out || address > UINT32_MAX) + return -1; + *span_out = mac_guest_address_resolve((uint32_t)address, size); + return *span_out ? 0 : -1; +} + +int mac_guest_stack_arg32(uint32_t guest_esp, uint32_t argument_index, + uint32_t *value_out) +{ + void *span; + + if (!value_out || mac_guest_stack_arg_span(guest_esp, argument_index, + sizeof(*value_out), &span) != 0) + return -1; + memcpy(value_out, span, sizeof(*value_out)); + return 0; +} + +int mac_guest_stack_arg64(uint32_t guest_esp, uint32_t argument_index, + uint64_t *value_out) +{ + uint64_t address = (uint64_t)guest_esp + 4 + (uint64_t)argument_index * 8; + if (!value_out || address > UINT32_MAX) + return -1; + return mac_guest_read((uint32_t)address, value_out, sizeof(*value_out)); +} + +int mac_guest_stack_after_return(uint32_t guest_esp, uint32_t callee_bytes, + uint32_t *new_esp_out) +{ + uint64_t result = (uint64_t)guest_esp + 4u + callee_bytes; + + if (!new_esp_out || result > UINT32_MAX) + return -1; + *new_esp_out = (uint32_t)result; + return 0; +} diff --git a/port/macos/guest_call.h b/port/macos/guest_call.h new file mode 100644 index 0000000000..5f83d3a402 --- /dev/null +++ b/port/macos/guest_call.h @@ -0,0 +1,37 @@ +/* + * Helpers for host callbacks entered from translated 32-bit x86 functions. + * At callback entry the translated guest ESP points at the 32-bit return VA; + * cdecl/stdcall arguments follow it in 4-byte slots. Host functions must use + * these helpers instead of treating guest VAs as native pointers. + */ + +#ifndef HALO_MACOS_GUEST_CALL_H +#define HALO_MACOS_GUEST_CALL_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +int mac_guest_read(uint32_t guest_address, void *destination, size_t size); +int mac_guest_write(uint32_t guest_address, const void *source, size_t size); +int mac_guest_stack_return(uint32_t guest_esp, uint32_t *return_va_out); +int mac_guest_stack_arg32(uint32_t guest_esp, uint32_t argument_index, + uint32_t *value_out); +int mac_guest_stack_arg64(uint32_t guest_esp, uint32_t argument_index, + uint64_t *value_out); +int mac_guest_stack_arg_span(uint32_t guest_esp, uint32_t argument_index, + size_t size, void **span_out); + +/* Compute guest ESP after a translated host callback returns. `callee_bytes` + * is zero for cdecl, or the byte count consumed by stdcall. */ +int mac_guest_stack_after_return(uint32_t guest_esp, uint32_t callee_bytes, + uint32_t *new_esp_out); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/port/macos/guest_callback.c b/port/macos/guest_callback.c new file mode 100644 index 0000000000..f62978f269 --- /dev/null +++ b/port/macos/guest_callback.c @@ -0,0 +1,185 @@ +#include "guest_callback.h" + +#include "guest_allocator.h" +#include "guest_call.h" +#include "host_services.h" + +#include +#include +#include + +#define GUEST_CALLBACK_RETURN UINT32_C(0xffffffff) +#define GUEST_CALLBACK_MAX_ARGS 8u +#define GUEST_CALLBACK_STACK_ALIGNMENT 16u + +struct mac_guest_callback_context +{ + struct mac_guest_thread_runtime runtime; + pthread_t owner; + uint32_t guest_thread_va; + uint32_t stack_base; + uint32_t stack_top; + size_t stack_size; + int attached; + int invoking; +}; + +static struct mac_guest_thread_runtime installed_runtime; +static uint32_t installed_attach_va; +static int runtime_installed; +static pthread_mutex_t runtime_mutex = PTHREAD_MUTEX_INITIALIZER; + +int mac_guest_callback_runtime_install(const struct mac_guest_thread_runtime *runtime, + uint32_t guest_thread_attach_va) +{ + int result = -1; + if (!runtime || !runtime->initialize || !runtime->dispatch || !guest_thread_attach_va) + return -1; + pthread_mutex_lock(&runtime_mutex); + if (!runtime_installed) + { + installed_runtime = *runtime; + installed_attach_va = guest_thread_attach_va; + runtime_installed = 1; + result = 0; + } + pthread_mutex_unlock(&runtime_mutex); + return result; +} + +static int current_runtime(struct mac_guest_thread_runtime *runtime, + uint32_t *attach_va) +{ + pthread_mutex_lock(&runtime_mutex); + if (runtime_installed) + { + *runtime = installed_runtime; + *attach_va = installed_attach_va; + } + pthread_mutex_unlock(&runtime_mutex); + return runtime_installed ? 0 : -1; +} + +static int make_frame(struct mac_guest_callback_context *context, + const uint32_t *arguments, size_t argument_count, uint32_t *guest_esp_out) +{ + uint32_t frame[GUEST_CALLBACK_MAX_ARGS + 1]; + uint64_t bytes = (argument_count + 1) * sizeof(uint32_t); + uint32_t guest_esp; + + if (argument_count > GUEST_CALLBACK_MAX_ARGS || + (argument_count && !arguments) || bytes > context->stack_size) + return EINVAL; + { + uint32_t candidate = context->stack_top - (uint32_t)bytes; + guest_esp = (candidate & ~UINT32_C(15)) + 12u; + if (guest_esp > candidate) + guest_esp -= 16u; + } + /* An i386 SysV call enters with ESP == 12 (mod 16): the caller's aligned + * stack held args, then CALL pushed the four-byte return address. */ + if (guest_esp < context->stack_base || + (uint64_t)guest_esp + bytes > context->stack_top || + (guest_esp & 15u) != 12u) + return EINVAL; + frame[0] = GUEST_CALLBACK_RETURN; + for (size_t index = 0; index < argument_count; ++index) + frame[index + 1] = arguments[index]; + if (mac_guest_write(guest_esp, frame, (size_t)bytes) != 0) + return EFAULT; + *guest_esp_out = guest_esp; + return 0; +} + +int mac_guest_callback_context_create_current(size_t guest_stack_size, + struct mac_guest_callback_context **context_out) +{ + struct mac_guest_callback_context *context; + struct mac_guest_thread_runtime runtime; + uint32_t attach_va, esp; + uint32_t frame = GUEST_CALLBACK_RETURN; + uint64_t end; + int error; + + if (!context_out || guest_stack_size < 64u * 1024u || + current_runtime(&runtime, &attach_va) != 0 || mac_host_get_guest_tp() != 0) + return EINVAL; + context = calloc(1, sizeof(*context)); + if (!context) + return ENOMEM; + error = mac_guest_allocate(guest_stack_size, 16u * 1024u, &context->stack_base); + if (error != 0) + { + free(context); + return error == ENOMEM ? ENOMEM : EAGAIN; + } + end = (uint64_t)context->stack_base + guest_stack_size; + if (end > UINT32_MAX) + { + (void)mac_guest_release(context->stack_base, guest_stack_size); + free(context); + return EOVERFLOW; + } + context->stack_size = guest_stack_size; + context->stack_top = (uint32_t)end & ~(GUEST_CALLBACK_STACK_ALIGNMENT - 1u); + context->runtime = runtime; + context->owner = pthread_self(); + if (context->stack_top < context->stack_base + sizeof(frame)) + { + (void)mac_guest_release(context->stack_base, guest_stack_size); + free(context); + return EOVERFLOW; + } + esp = context->stack_top - sizeof(frame); + if ((esp & 15u) != 12u || mac_guest_write(esp, &frame, sizeof(frame)) != 0) + { + (void)mac_guest_release(context->stack_base, guest_stack_size); + free(context); + return EFAULT; + } + context->runtime.initialize(context->runtime.context, 0, esp); + context->runtime.dispatch(context->runtime.context, attach_va); + context->guest_thread_va = mac_host_get_guest_tp(); + if (!context->guest_thread_va) + { + (void)mac_guest_release(context->stack_base, guest_stack_size); + free(context); + return EFAULT; + } + context->attached = 1; + *context_out = context; + return 0; +} + +int mac_guest_callback_invoke(struct mac_guest_callback_context *context, + uint32_t guest_function_va, const uint32_t *arguments, size_t argument_count) +{ + uint32_t esp; + int error; + + if (!context || !context->attached || !guest_function_va || + !pthread_equal(context->owner, pthread_self()) || context->invoking) + return EINVAL; + error = make_frame(context, arguments, argument_count, &esp); + if (error != 0) + return error; + context->invoking = 1; + context->runtime.initialize(context->runtime.context, context->guest_thread_va, esp); + context->runtime.dispatch(context->runtime.context, guest_function_va); + context->invoking = 0; + return 0; +} + +void mac_guest_callback_context_destroy_current(struct mac_guest_callback_context *context) +{ + if (!context) + return; + if (!pthread_equal(context->owner, pthread_self())) + mac_host_abort("guest callback context destroyed from a different thread"); + if (context->invoking) + mac_host_abort("guest callback context destroyed during a callback"); + if (context->attached) + mac_host_set_guest_tp(0); + (void)mac_guest_release(context->stack_base, context->stack_size); + free(context); +} diff --git a/port/macos/guest_callback.h b/port/macos/guest_callback.h new file mode 100644 index 0000000000..77ccd18c9c --- /dev/null +++ b/port/macos/guest_callback.h @@ -0,0 +1,26 @@ +#ifndef HALO_MACOS_GUEST_CALLBACK_H +#define HALO_MACOS_GUEST_CALLBACK_H + +#include "guest_thread.h" + +#include +#include + +struct mac_guest_callback_context; + +/* Install the same translated dispatcher used by native guest threads plus + * the ELF VA of __guest_thread_attach. Callback workers run attach on their + * own native thread before dispatching any guest callback. */ +int mac_guest_callback_runtime_install(const struct mac_guest_thread_runtime *runtime, + uint32_t guest_thread_attach_va); + +/* These functions are called only from the native worker thread that will run + * callbacks. It gets a guest stack and guest pthread/TLS through the guest's + * own __guest_thread_attach routine. */ +int mac_guest_callback_context_create_current(size_t guest_stack_size, + struct mac_guest_callback_context **context_out); +int mac_guest_callback_invoke(struct mac_guest_callback_context *context, + uint32_t guest_function_va, const uint32_t *arguments, size_t argument_count); +void mac_guest_callback_context_destroy_current(struct mac_guest_callback_context *context); + +#endif diff --git a/port/macos/guest_errno.c b/port/macos/guest_errno.c new file mode 100644 index 0000000000..494b195ca7 --- /dev/null +++ b/port/macos/guest_errno.c @@ -0,0 +1,65 @@ +#include "guest_errno.h" +#include "guest_address.h" +#include "guest_call.h" +#include "host_services.h" +#ifndef RECOMP_GENERATED_CODE +#define RECOMP_GENERATED_CODE 1 +#endif +#include "recomp_types.h" +#include +#include + +static void (*errno_accessor)(void); +static _Thread_local uint32_t cached_tp, cached_address; +static _Thread_local int discovering; + +int mac_guest_errno_install(void (*accessor)(void)) +{ + if (!accessor || errno_accessor) return -1; + errno_accessor = accessor; + return 0; +} + +int mac_guest_errno_set_native(int native_error) +{ + uint32_t tp = mac_host_get_guest_tp(); + int native_saved = errno, result = -1; + if (!tp || !errno_accessor || discovering) {errno=native_saved;return -1;} + if (cached_tp != tp || !mac_guest_address_resolve(cached_address, sizeof(int32_t))) + { + /* The accessor is a real translated cdecl function. Preserve its bounded + * scratch stack and every live guest register, including x87/MMX/SSE. */ + uint32_t general[] = {g_eax,g_ecx,g_edx,g_esp,g_ebx,g_esi,g_edi,g_ebp}; + uint32_t saved_seh=g_seh_ebp, saved_fs=g_fs_base, saved_mxcsr=g_mxcsr; + double saved_fp[8]; memcpy(saved_fp,g_fp_stack,sizeof(saved_fp)); + RecompXmm saved_xmm[]={g_xmm0,g_xmm1,g_xmm2,g_xmm3,g_xmm4,g_xmm5,g_xmm6,g_xmm7}; + RecompMmx saved_mm[]={g_mm0,g_mm1,g_mm2,g_mm3,g_mm4,g_mm5,g_mm6,g_mm7}; + int saved_top=g_fp_top,saved_df=g_df,saved_cmp=g_fp_cmp; + uint16_t saved_control=g_fp_control_word,saved_status=g_fp_status_word; + uint8_t saved_empty=g_fp_empty_mask; + unsigned char scratch[256]; + uint32_t original_stack=general[3], frame, address=0, sentinel=UINT32_MAX; + if (original_stack < sizeof(scratch) || mac_guest_read(original_stack-sizeof(scratch),scratch,sizeof(scratch))) {errno=native_saved;return -1;} + frame=((original_stack-32u)&~UINT32_C(15))+12u; + if (mac_guest_write(frame,&sentinel,sizeof(sentinel))) {errno=native_saved;return -1;} + discovering=1; g_esp=frame; errno_accessor(); + if (g_esp==frame+4 && mac_host_get_guest_tp()==tp) address=g_eax; + discovering=0; + (void)mac_guest_write(original_stack-sizeof(scratch),scratch,sizeof(scratch)); + g_eax=general[0];g_ecx=general[1];g_edx=general[2];g_esp=general[3]; + g_ebx=general[4];g_esi=general[5];g_edi=general[6];g_ebp=general[7]; + g_seh_ebp=saved_seh;g_fs_base=saved_fs;g_mxcsr=saved_mxcsr; + memcpy(g_fp_stack,saved_fp,sizeof(saved_fp));g_fp_top=saved_top;g_df=saved_df;g_fp_cmp=saved_cmp; + g_fp_control_word=saved_control;g_fp_status_word=saved_status;g_fp_empty_mask=saved_empty; + g_xmm0=saved_xmm[0];g_xmm1=saved_xmm[1];g_xmm2=saved_xmm[2];g_xmm3=saved_xmm[3]; + g_xmm4=saved_xmm[4];g_xmm5=saved_xmm[5];g_xmm6=saved_xmm[6];g_xmm7=saved_xmm[7]; + g_mm0=saved_mm[0];g_mm1=saved_mm[1];g_mm2=saved_mm[2];g_mm3=saved_mm[3]; + g_mm4=saved_mm[4];g_mm5=saved_mm[5];g_mm6=saved_mm[6];g_mm7=saved_mm[7]; + if (!address || !mac_guest_address_resolve(address,sizeof(int32_t))) {errno=native_saved;return -1;} + cached_tp=tp;cached_address=address; + } + int32_t linux_error=mac_host_linux_errno(native_error); + result=mac_guest_write(cached_address,&linux_error,sizeof(linux_error)); + errno=native_saved; + return result; +} diff --git a/port/macos/guest_errno.h b/port/macos/guest_errno.h new file mode 100644 index 0000000000..a88608b0ff --- /dev/null +++ b/port/macos/guest_errno.h @@ -0,0 +1,8 @@ +#ifndef HALO_MACOS_GUEST_ERRNO_H +#define HALO_MACOS_GUEST_ERRNO_H + +/* Install the exact hash-matched translated musl __errno_location accessor. + * The writer discovers guest TLS through that accessor, never host TLS offsets. */ +int mac_guest_errno_install(void (*location_accessor)(void)); +int mac_guest_errno_set_native(int native_error); +#endif diff --git a/port/macos/guest_heap.c b/port/macos/guest_heap.c new file mode 100644 index 0000000000..2f8fdb220a --- /dev/null +++ b/port/macos/guest_heap.c @@ -0,0 +1,501 @@ +#include "guest_heap.h" + +#include "guest_address.h" +#include "guest_allocator.h" +#include "host_syscall.h" + +#include +#include +#include +#include +#include +#include + +#define IMPORT_BASE UINT32_C(0x70000000) +#define GUEST_SPACE_SIZE (UINT64_C(1) << 32) +#define IMPORT_RESERVED_SIZE UINT32_C(0x10000) +#define GUEST_PAGE_SIZE UINT32_C(4096) +#define LINUX_MAP_FIXED UINT32_C(0x10) +#define LINUX_MAP_ANONYMOUS UINT32_C(0x20) +#define LINUX_MAP_NORESERVE UINT32_C(0x4000) +#define LINUX_MAP_FIXED_NOREPLACE UINT32_C(0x100000) + +struct guest_heap_state +{ + pthread_mutex_t mutex; + uint32_t base; + uint32_t page_size; + uint32_t page_count; + uint32_t total_page_count; + uint8_t *allocated; + uint8_t *reserved; + uint8_t *replaceable; + uint8_t *protection; + /* Native protection granularity can be 16KB while Xbox mappings are 4KB. */ + uint8_t *guest_protection; + int installed; +}; + +static struct guest_heap_state heap = { .mutex = PTHREAD_MUTEX_INITIALIZER }; + +static int round_guest_span(size_t size, uint32_t *pages_out, uint32_t *rounded_out) +{ + uint64_t pages; + if (!size || !heap.page_size) + return -EINVAL; + pages = ((uint64_t)size + heap.page_size - 1) / heap.page_size; + if (!pages || pages > heap.total_page_count || pages * heap.page_size > UINT32_MAX) + return -ENOMEM; + *pages_out = (uint32_t)pages; + *rounded_out = (uint32_t)(pages * heap.page_size); + return 0; +} + +static int find_free_pages(uint32_t requested_va, uint32_t page_count, + uint32_t first_limit, uint32_t page_alignment, int fixed, + uint32_t *first_page_out) +{ + uint32_t first = 0; + if (fixed) + { + first = requested_va / heap.page_size; + if (first > heap.total_page_count || page_count > heap.total_page_count - first) + return -ENOMEM; + for (uint32_t i = 0; i < page_count; ++i) + if (heap.allocated[first + i]) + return -ENOMEM; + *first_page_out = first; + return 0; + } + uint32_t first_available = heap.base / heap.page_size; + if (!page_alignment || (page_alignment & (page_alignment - 1)) || + page_alignment % heap.page_size) + return -EINVAL; + if (first_limit < first_available || page_count > first_limit - first_available) + return -ENOMEM; + for (first = first_available; + first <= first_limit - page_count; ++first) + { + uint32_t i; + uint64_t address = (uint64_t)first * heap.page_size; + if (address & (page_alignment - 1)) + continue; + for (i = 0; i < page_count && !heap.allocated[first + i]; ++i) {} + if (i == page_count) + { + *first_page_out = first; + return 0; + } + first += i; + } + return -ENOMEM; +} + +static int commit_pages(uint32_t first, uint32_t count, int protection) +{ + uint32_t address = first * heap.page_size; + size_t length = (size_t)count * heap.page_size; + if (mac_guest_address_commit(address, length, protection) != 0) + return -ENOMEM; + return 0; +} + +static int range_is_replaceable_reservation(uint32_t first, uint32_t count) +{ + if (first > heap.total_page_count || count > heap.total_page_count - first) + return 0; + for (uint32_t i = 0; i < count; ++i) + if (!heap.replaceable[first + i]) + return 0; + return 1; +} + +static uint32_t guest_round_4k(uint32_t size) +{ + return (uint32_t)(((uint64_t)size + GUEST_PAGE_SIZE - 1) & + ~(uint64_t)(GUEST_PAGE_SIZE - 1)); +} + +static int is_replaceable_guest_span(uint32_t address, uint32_t length) +{ + uint64_t end = (uint64_t)address + length; + uint32_t first, last; + if (!length || address % GUEST_PAGE_SIZE || end > GUEST_SPACE_SIZE || + end % GUEST_PAGE_SIZE) + return 0; + first = address / heap.page_size; + last = (uint32_t)((end + heap.page_size - 1) / heap.page_size); + return range_is_replaceable_reservation(first, last - first); +} + +static int set_guest_span_protection(uint32_t address, uint32_t length, + int protection) +{ + uint32_t guest_first = address / GUEST_PAGE_SIZE; + uint32_t guest_count = length / GUEST_PAGE_SIZE; + uint32_t host_first = address / heap.page_size; + uint32_t host_last = (uint32_t)(((uint64_t)address + length + heap.page_size - 1) / + heap.page_size); + for (uint32_t i = 0; i < guest_count; ++i) + heap.guest_protection[guest_first + i] = (uint8_t)protection; + /* A 16KB host page receives the union of its four guest-page permissions. + * macOS cannot enforce independent protection within those subpages, but + * this preserves neighboring data and avoids making a live block inaccessible. */ + for (uint32_t hp = host_first; hp < host_last; ++hp) + { + uint32_t gf = hp * heap.page_size / GUEST_PAGE_SIZE; + int effective = PROT_NONE; + for (uint32_t sub = 0; sub < heap.page_size / GUEST_PAGE_SIZE; ++sub) + effective |= heap.guest_protection[gf + sub]; + if (mac_guest_address_protect(hp * heap.page_size, heap.page_size, + effective) != 0) + return -1; + heap.protection[hp] = (uint8_t)effective; + } + return 0; +} + +static int release_pages(uint32_t first, uint32_t count) +{ + uint32_t address = first * heap.page_size; + size_t length = (size_t)count * heap.page_size; + if (mac_guest_address_protect(address, length, PROT_NONE) != 0) + return -EINVAL; + /* The next mmap/stack allocation must see zero-filled pages, as it would + * when the kernel first maps anonymous memory. */ + void *host = mac_guest_address_resolve(address, length); + if (!host || madvise(host, length, MADV_DONTNEED) != 0) + return -ENOMEM; + for (uint32_t i = 0; i < count; ++i) + { + heap.allocated[first + i] = 0; + heap.protection[first + i] = 0; + } + return 0; +} + +static int heap_allocate(void *context, size_t size, size_t alignment, + uint32_t *guest_va_out) +{ + uint32_t count, rounded, first; + int result; + (void)context; + if (!guest_va_out || !alignment || (alignment & (alignment - 1))) + return EINVAL; + if ((result = round_guest_span(size, &count, &rounded)) != 0) + return -result; + if (alignment < heap.page_size) + alignment = heap.page_size; + if (alignment > UINT32_MAX || alignment % heap.page_size) + return EINVAL; + pthread_mutex_lock(&heap.mutex); + result = find_free_pages(0, count, heap.base / heap.page_size + heap.page_count, + (uint32_t)alignment, 0, &first); + if (!result) + { + result = commit_pages(first, count, PROT_READ | PROT_WRITE); + if (!result) + { + for (uint32_t i = 0; i < count; ++i) + { + heap.allocated[first + i] = 1; + heap.protection[first + i] = PROT_READ | PROT_WRITE; + } + memset(mac_guest_address_resolve(first * heap.page_size, rounded), + 0, rounded); + *guest_va_out = first * heap.page_size; + } + } + pthread_mutex_unlock(&heap.mutex); + return result > 0 ? result : -result; +} + +static void heap_release(void *context, uint32_t guest_va, size_t size) +{ + uint32_t count, rounded; + uint32_t offset; + int result; + (void)context; + if (!heap.installed || guest_va < heap.base) + return; + offset = guest_va - heap.base; + if (offset % heap.page_size || round_guest_span(size, &count, &rounded) != 0) + return; + pthread_mutex_lock(&heap.mutex); + uint32_t first = guest_va / heap.page_size; + if (first <= heap.total_page_count && count <= heap.total_page_count - first) + { + result = 0; + for (uint32_t i = 0; i < count; ++i) + if (!heap.allocated[first + i] || heap.reserved[first + i]) + { result = -EINVAL; break; } + if (!result) + result = release_pages(first, count); + (void)result; + } + pthread_mutex_unlock(&heap.mutex); +} + +static int64_t vm_map(void *context, uint32_t requested, uint32_t size, + int protection, uint32_t flags, int fd, uint64_t offset, + uint32_t *mapped_out) +{ + uint32_t count, rounded, first, map_page_offset = 0; + int no_replace = (flags & LINUX_MAP_FIXED_NOREPLACE) != 0; + int fixed = (flags & LINUX_MAP_FIXED) != 0 || no_replace; + int result; + (void)context; + if (!mapped_out || !size || fd != -1 || offset != 0 || + !(flags & LINUX_MAP_ANONYMOUS) || !(flags & UINT32_C(0x02))) + return -38; /* ENOSYS: file mappings are not installed in the arena. */ + if (protection & PROT_EXEC) + return -EACCES; /* guest code is translated; never expose executable arena pages. */ + if (fixed) + { + uint64_t end = (uint64_t)requested + size; + if (requested % GUEST_PAGE_SIZE || end > GUEST_SPACE_SIZE) + return -EINVAL; + map_page_offset = requested % heap.page_size; + count = (uint32_t)(((uint64_t)map_page_offset + size + heap.page_size - 1) / + heap.page_size); + if (count > heap.total_page_count) + return -ENOMEM; + rounded = count * heap.page_size; + } + else if ((result = round_guest_span(size, &count, &rounded)) != 0) + return result; + pthread_mutex_lock(&heap.mutex); + if (fixed && !no_replace) + { + uint64_t first64 = (uint64_t)requested / heap.page_size; + if (first64 > heap.total_page_count || count > heap.total_page_count - first64) + result = -ENOMEM; + else + { + first = (uint32_t)first64; + /* MAP_FIXED may replace pages inside the reserved Xbox contiguous + * window. Outside that explicitly-owned range, refuse to clobber + * existing image/import/guest mappings. */ + if (range_is_replaceable_reservation(first, count)) + result = 0; + else + result = find_free_pages(requested, count, heap.total_page_count, + heap.page_size, 1, &first); + } + } + else + result = find_free_pages(requested, count, + fixed ? heap.total_page_count : heap.base / heap.page_size + heap.page_count, + heap.page_size, fixed, &first); + if (!result && no_replace && protection == PROT_NONE) + { + /* The source Xbox backend first reserves its 128 MiB physical window + * with PROT_NONE|MAP_FIXED_NOREPLACE|MAP_NORESERVE. The whole guest + * address space already has a Darwin PROT_NONE reservation, so this + * operation records ownership without touching/committing 128 MiB. */ + *mapped_out = requested; + for (uint32_t i = 0; i < count; ++i) + { + heap.allocated[first + i] = 1; + heap.reserved[first + i] = 1; + heap.replaceable[first + i] = 1; + heap.protection[first + i] = PROT_NONE; + } + if (is_replaceable_guest_span(requested, rounded)) + memset(heap.guest_protection + requested / GUEST_PAGE_SIZE, PROT_NONE, + rounded / GUEST_PAGE_SIZE); + pthread_mutex_unlock(&heap.mutex); + return 0; + } + if (!result && fixed && !no_replace && protection == PROT_NONE && + is_replaceable_guest_span(requested, guest_round_4k(size))) + { + uint32_t guest_length = guest_round_4k(size); + void *host = mac_guest_address_resolve(requested, guest_length); + /* MAP_FIXED of anonymous PROT_NONE pages is the source backend's + * XPhysicalFree operation: discard the old backing while retaining the + * window reservation. This also guarantees future recommit is zeroed. */ + if (!host || commit_pages(first, count, PROT_READ | PROT_WRITE) != 0) + result = -ENOMEM; + else + { + /* Darwin MADV_DONTNEED may retain anonymous bytes until memory + * pressure, unlike replacing an anonymous Linux MAP_FIXED mapping. + * Clear explicitly before restoring the guard protection. */ + memset(host, 0, guest_length); + if (set_guest_span_protection(requested, guest_length, PROT_NONE) != 0) + { + result = -ENOMEM; + pthread_mutex_unlock(&heap.mutex); + return result; + } + *mapped_out = requested; + pthread_mutex_unlock(&heap.mutex); + return 0; + } + } + if (!result) + { + /* Anonymous mmap pages are cleared before the requested final protection + * takes effect. A read-only or PROT_NONE mapping must still be writable + * during this host-side initialization. */ + result = commit_pages(first, count, PROT_READ | PROT_WRITE); + if (!result) + { + for (uint32_t i = 0; i < count; ++i) + { + heap.allocated[first + i] = 1; + if (no_replace) + { + heap.reserved[first + i] = 1; + heap.replaceable[first + i] = 1; + } + heap.protection[first + i] = (uint8_t)(PROT_READ | PROT_WRITE); + } + *mapped_out = fixed ? requested : first * heap.page_size; + /* Linux zeroes the entire guest page span, including the tail of + * an unaligned mmap length. Darwin may retain released backing; + * musl mallocng relies on that tail for its zero end marker. Fixed + * 4KB subpage mappings must preserve neighboring Xbox blocks. */ + uint32_t clear_length = fixed ? guest_round_4k(size) : rounded; + memset(mac_guest_address_resolve(*mapped_out, clear_length), 0, clear_length); + if (fixed && is_replaceable_guest_span(*mapped_out, guest_round_4k(size))) + { + if (set_guest_span_protection(*mapped_out, guest_round_4k(size), protection) != 0) + result = -ENOMEM; + } + else if (protection != (PROT_READ | PROT_WRITE) && + mac_guest_address_protect(first * heap.page_size, + (size_t)count * heap.page_size, protection) != 0) + { + (void)release_pages(first, count); + result = -ENOMEM; + } + else if (!result) + { + for (uint32_t i = 0; i < count; ++i) + heap.protection[first + i] = (uint8_t)protection; + } + } + } + pthread_mutex_unlock(&heap.mutex); + return result; +} + +static int64_t vm_unmap(void *context, uint32_t address, uint32_t size) +{ + uint32_t count, first, offset; + uint64_t end = (uint64_t)address + size; + int result; + (void)context; + if (!size) + return -EINVAL; + if (!size || address % GUEST_PAGE_SIZE || end > GUEST_SPACE_SIZE) + return -EINVAL; + first = address / heap.page_size; + offset = address % heap.page_size; + count = (uint32_t)(((uint64_t)offset + size + heap.page_size - 1) / + heap.page_size); + pthread_mutex_lock(&heap.mutex); + if (first > heap.total_page_count || count > heap.total_page_count - first) + result = -EINVAL; + else + { + for (uint32_t i = 0; i < count; ++i) + if (!heap.allocated[first + i] || heap.reserved[first + i]) + { pthread_mutex_unlock(&heap.mutex); return -EINVAL; } + result = release_pages(first, count); + } + pthread_mutex_unlock(&heap.mutex); + return result; +} + +static int64_t vm_protect(void *context, uint32_t address, uint32_t size, + int protection) +{ + uint32_t count, first, offset; + uint64_t end = (uint64_t)address + size; + int result = 0; + (void)context; + if (!size || protection & PROT_EXEC) + return -EINVAL; + if (!size || address % GUEST_PAGE_SIZE || end > GUEST_SPACE_SIZE) + return -EINVAL; + first = address / heap.page_size; + offset = address % heap.page_size; + count = (uint32_t)(((uint64_t)offset + size + heap.page_size - 1) / + heap.page_size); + pthread_mutex_lock(&heap.mutex); + if (first > heap.total_page_count || count > heap.total_page_count - first) + result = -EINVAL; + else + { + for (uint32_t i = 0; i < count; ++i) + if (!heap.allocated[first + i]) { result = -EINVAL; break; } + if (!result && is_replaceable_guest_span(address, guest_round_4k(size))) + result = set_guest_span_protection(address, guest_round_4k(size), protection); + else if (!result && mac_guest_address_protect(first * heap.page_size, + (size_t)count * heap.page_size, protection) != 0) + result = -ENOMEM; + if (!result) + for (uint32_t i = 0; i < count; ++i) heap.protection[first + i] = (uint8_t)protection; + } + pthread_mutex_unlock(&heap.mutex); + return result; +} + +int mac_guest_heap_install(uint32_t image_end_va) +{ + struct mac_guest_allocator allocator = { NULL, heap_allocate, heap_release }; + struct mac_guest_vm_ops operations = { NULL, vm_map, vm_unmap, vm_protect }; + size_t host_page = mac_guest_address_host_page_size(); + uint64_t aligned_base; + if (heap.installed || !host_page || (host_page & (host_page - 1)) || + image_end_va >= IMPORT_BASE || (IMPORT_BASE % host_page) || + (IMPORT_RESERVED_SIZE % host_page)) + return -1; + aligned_base = ((uint64_t)image_end_va + host_page - 1) & ~((uint64_t)host_page - 1); + if (aligned_base >= IMPORT_BASE || ((IMPORT_BASE - aligned_base) % host_page)) + return -1; + heap.base = (uint32_t)aligned_base; + heap.page_size = (uint32_t)host_page; + heap.page_count = (IMPORT_BASE - heap.base) / heap.page_size; + heap.total_page_count = (uint32_t)(GUEST_SPACE_SIZE / heap.page_size); + heap.allocated = calloc(heap.total_page_count, 1); + heap.reserved = calloc(heap.total_page_count, 1); + heap.replaceable = calloc(heap.total_page_count, 1); + heap.protection = calloc(heap.total_page_count, 1); + heap.guest_protection = calloc(GUEST_SPACE_SIZE / GUEST_PAGE_SIZE, 1); + if (!heap.allocated || !heap.reserved || !heap.replaceable || !heap.protection || + !heap.guest_protection) + { + free(heap.allocated); free(heap.reserved); free(heap.replaceable); free(heap.protection); + free(heap.guest_protection); + heap.allocated = heap.reserved = heap.replaceable = heap.protection = NULL; + heap.guest_protection = NULL; + return -1; + } + /* Keep null page, loaded image, and synthetic import-stub range unavailable + * to brk/mmap allocations. MAP_FIXED at 0x80000000 (the Xbox RAM window) + * remains legal when its pages are otherwise free. */ + for (uint32_t page = 0; page < (uint32_t)(((uint64_t)image_end_va + host_page - 1) / host_page); ++page) + heap.allocated[page] = heap.reserved[page] = 1; + { + uint32_t first = IMPORT_BASE / heap.page_size; + uint32_t count = IMPORT_RESERVED_SIZE / heap.page_size; + for (uint32_t page = 0; page < count; ++page) + heap.allocated[first + page] = heap.reserved[first + page] = 1; + memset(heap.guest_protection + ((first * heap.page_size) / GUEST_PAGE_SIZE), + PROT_NONE, (size_t)count * heap.page_size / GUEST_PAGE_SIZE); + } + if (mac_guest_allocator_install(&allocator) != 0 || mac_guest_vm_ops_install(&operations) != 0) + { + free(heap.allocated); free(heap.reserved); free(heap.replaceable); free(heap.protection); + free(heap.guest_protection); + heap.allocated = heap.reserved = heap.replaceable = heap.protection = NULL; + heap.guest_protection = NULL; + return -1; + } + heap.installed = 1; + return 0; +} diff --git a/port/macos/guest_heap.h b/port/macos/guest_heap.h new file mode 100644 index 0000000000..df6d0cf5d8 --- /dev/null +++ b/port/macos/guest_heap.h @@ -0,0 +1,20 @@ +#ifndef HALO_MACOS_GUEST_HEAP_H +#define HALO_MACOS_GUEST_HEAP_H + +#include + +/* Installs the shared stack/mmap allocator into the linker-free guest VA + * interval [align_up(image_end, host_page), 0x70000000). The image loader must + * already own one linear 4 GiB address reservation. Anonymous + * MAP_FIXED_NOREPLACE records ranges inside that arena, and later MAP_FIXED + * may overlay only those explicitly replaceable reservations (used by the + * Xbox contiguous-memory window at 0x80000000). + * Darwin commits/protects at host-page granularity; the guest ABI still uses + * 4KB pages. In the replaceable Xbox window, subpage data is tracked and a + * host page receives the union of its subpage permissions, so adjacent live + * blocks survive a 4KB free. macOS cannot enforce independent permissions + * within one 16KB host page. File-backed mappings and executable guest + * mappings fail explicitly. */ +int mac_guest_heap_install(uint32_t image_end_va); + +#endif diff --git a/port/macos/guest_image.c b/port/macos/guest_image.c new file mode 100644 index 0000000000..5ee868a0d2 --- /dev/null +++ b/port/macos/guest_image.c @@ -0,0 +1,249 @@ +#include "guest_image.h" + +#include "guest_address.h" +#include "elf32_format.h" + +#include +#include +#include + +#define GUEST_SPACE_SIZE UINT64_C(0x100000000) +#define IMPORT_BASE UINT32_C(0x70000000) + +static int fail(char *out, size_t cap, const char *message) +{ + if (out && cap) + snprintf(out, cap, "%s", message); + return -1; +} + +static int bytes_fit(size_t total, uint64_t offset, uint64_t length) +{ + return offset <= total && length <= (uint64_t)total - offset; +} + +static int guest_span(uint32_t address, uint32_t length) +{ + return (uint64_t)address + length <= GUEST_SPACE_SIZE; +} + +int mac_guest_image_load(const void *elf_bytes, size_t elf_size, + struct mac_guest_image *image_out, char *error_out, size_t error_capacity) +{ + const unsigned char *bytes = elf_bytes; + const struct halo_elf32_ehdr *header; + const struct halo_elf32_phdr *programs; + void *arena; + uint32_t low = UINT32_MAX, high = 0; + uint32_t code_low = UINT32_MAX, code_high = 0; + uint32_t guest_cursor; + uint32_t tls_va = 0; + size_t tls_file = 0, tls_memory = 0, tls_alignment = 1; + size_t page_size; + int map_protection; + + if (!bytes || !image_out || elf_size < sizeof(struct halo_elf32_ehdr)) + return fail(error_out, error_capacity, "truncated ELF header"); + header = (const struct halo_elf32_ehdr *)bytes; + if (memcmp(header->ident, "\177ELF", 4) != 0 || + header->ident[4] != HALO_ELFCLASS32 || + header->ident[5] != HALO_ELFDATA2LSB || + header->machine != HALO_EM_386 || header->type != HALO_ET_EXEC || + header->phentsize != sizeof(struct halo_elf32_phdr) || header->phnum == 0) + return fail(error_out, error_capacity, "expected little-endian ELF32/i386 ET_EXEC"); + if (!bytes_fit(elf_size, header->phoff, + (uint64_t)header->phnum * sizeof(struct halo_elf32_phdr))) + return fail(error_out, error_capacity, "program header table outside ELF"); + programs = (const struct halo_elf32_phdr *)(bytes + header->phoff); + for (unsigned i = 0; i < header->phnum; ++i) + { + const struct halo_elf32_phdr *ph = &programs[i]; + uint64_t end; + if (ph->type == HALO_PT_TLS) + { + if (tls_memory || ph->filesz > ph->memsz || + !bytes_fit(elf_size, ph->offset, ph->filesz)) + return fail(error_out, error_capacity, "invalid PT_TLS segment"); + tls_va = ph->vaddr; + tls_file = ph->filesz; + tls_memory = ph->memsz; + tls_alignment = ph->align ? ph->align : 1; + continue; + } + if (ph->type != HALO_PT_LOAD || !ph->memsz) + continue; + if (ph->filesz > ph->memsz || + !bytes_fit(elf_size, ph->offset, ph->filesz) || + !guest_span(ph->vaddr, ph->memsz)) + return fail(error_out, error_capacity, "invalid PT_LOAD span"); + end = (uint64_t)ph->vaddr + ph->memsz; + /* Import trampolines are intercepted by address before guest memory. */ + if (ph->vaddr >= IMPORT_BASE) + continue; + if (ph->vaddr < low) low = ph->vaddr; + if (end > high) high = (uint32_t)end; + if (ph->flags & HALO_PF_X) + { + if (ph->vaddr < code_low) code_low = ph->vaddr; + if (end > code_high) code_high = (uint32_t)end; + } + } + if (low == UINT32_MAX || high <= low || code_low == UINT32_MAX || + code_high <= code_low || header->entry < code_low || header->entry >= code_high) + return fail(error_out, error_capacity, "ELF has no loadable entry image"); + + /* All ELF segment pages start writable for relocation-free copy/zero fill. + * Guest code is mechanically translated, so native execution never branches + * into this data arena. */ + arena = mac_guest_address_reserve(0, (size_t)GUEST_SPACE_SIZE); + if (!arena) + return fail(error_out, error_capacity, "unable to reserve 4 GiB guest VA arena"); + if (mac_guest_address_commit(low, (size_t)(high - low), + PROT_READ | PROT_WRITE) != 0) + { + mac_guest_address_reset(); + return fail(error_out, error_capacity, "unable to commit guest ELF image pages"); + } + for (unsigned i = 0; i < header->phnum; ++i) + { + const struct halo_elf32_phdr *ph = &programs[i]; + void *destination; + if (ph->type != HALO_PT_LOAD || !ph->memsz || ph->vaddr >= IMPORT_BASE) + continue; + destination = mac_guest_address_resolve(ph->vaddr, ph->memsz); + if (!destination) + { + mac_guest_address_reset(); + return fail(error_out, error_capacity, "PT_LOAD resolves outside guest arena"); + } + memcpy(destination, bytes + ph->offset, ph->filesz); + if (ph->memsz > ph->filesz) + memset((unsigned char *)destination + ph->filesz, 0, + ph->memsz - ph->filesz); + } + /* Restore a conservative image-wide protection derived from segment flags. + * The source image contains code and mutable globals in nearby segments, and + * Darwin rounds mprotect to 16 KiB pages; therefore use the union. */ + map_protection = 0; + for (unsigned i = 0; i < header->phnum; ++i) + { + const struct halo_elf32_phdr *ph = &programs[i]; + if (ph->type != HALO_PT_LOAD || !ph->memsz || ph->vaddr >= IMPORT_BASE) + continue; + if (ph->flags & (HALO_PF_R | HALO_PF_X)) map_protection |= PROT_READ; + if (ph->flags & HALO_PF_W) map_protection |= PROT_WRITE; + /* Guest code bytes are data to the AOT host. Do not request native + * executable arena pages; this also avoids macOS W^X/JIT entitlements. */ + } + page_size = mac_guest_address_host_page_size(); + guest_cursor = low & ~((uint32_t)page_size - 1u); + if (!page_size || mac_guest_address_protect(guest_cursor, + (size_t)(((uint64_t)high + page_size - 1) / page_size * page_size - guest_cursor), + map_protection) != 0) + { + mac_guest_address_reset(); + return fail(error_out, error_capacity, "unable to finalize guest image protection"); + } + if (mac_guest_address_linear_offset(0, arena, &image_out->memory_offset) != 0) + { + mac_guest_address_reset(); + return fail(error_out, error_capacity, "guest arena has no linear memory offset"); + } + image_out->arena_host_base = arena; + image_out->entry_va = header->entry; + image_out->code_lo_va = code_low; + image_out->code_hi_va = code_high; + image_out->image_end_va = high; + image_out->tls_image_va = tls_va; + image_out->tls_file_size = tls_file; + image_out->tls_memory_size = tls_memory; + image_out->tls_alignment = tls_alignment; + if (error_out && error_capacity) error_out[0] = '\0'; + return 0; +} + +static int find_symbol_of_type(const void *elf_bytes, size_t elf_size, + const char *name, uint32_t *guest_va_out, unsigned type, uint32_t object_size) +{ + const unsigned char *bytes = elf_bytes; + const struct halo_elf32_ehdr *header = elf_bytes; + const struct halo_elf32_shdr *sections; + uint32_t found_va = 0; + int found = 0; + if (!bytes || !name || !guest_va_out || elf_size < sizeof(*header) || + memcmp(header->ident, "\177ELF", 4) != 0 || + header->ident[4] != HALO_ELFCLASS32 || + header->shentsize != sizeof(struct halo_elf32_shdr) || !header->shnum || + !bytes_fit(elf_size, header->shoff, + (uint64_t)header->shnum * sizeof(struct halo_elf32_shdr))) + return -1; + sections = (const struct halo_elf32_shdr *)(bytes + header->shoff); + for (unsigned section_index = 0; section_index < header->shnum; ++section_index) + { + const struct halo_elf32_shdr *symbols = §ions[section_index]; + const struct halo_elf32_shdr *strings; + const struct halo_elf32_sym *entries; + if (symbols->type != HALO_SHT_SYMTAB || + symbols->entsize != sizeof(struct halo_elf32_sym) || + symbols->link >= header->shnum || + !bytes_fit(elf_size, symbols->offset, symbols->size)) + continue; + strings = §ions[symbols->link]; + if (strings->type != HALO_SHT_STRTAB || + !bytes_fit(elf_size, strings->offset, strings->size)) + continue; + entries = (const struct halo_elf32_sym *)(bytes + symbols->offset); + for (size_t index = 0; index < symbols->size / sizeof(*entries); ++index) + { + const struct halo_elf32_sym *entry = &entries[index]; + const char *symbol_name; + size_t remaining; + if (entry->shndx == HALO_SHN_UNDEF || + (entry->info & 0x0f) != type || entry->name >= strings->size) + continue; + symbol_name = (const char *)(bytes + strings->offset + entry->name); + remaining = strings->size - entry->name; + if (memchr(symbol_name, '\0', remaining) && strcmp(symbol_name, name) == 0) + { + if (type == HALO_STT_OBJECT) + { + int in_load_segment = 0; + if (header->phentsize == sizeof(struct halo_elf32_phdr) && + bytes_fit(elf_size, header->phoff, (uint64_t)header->phnum * sizeof(struct halo_elf32_phdr))) + { + const struct halo_elf32_phdr *segments = (const void *)(bytes + header->phoff); + for (unsigned p = 0; p < header->phnum; ++p) + if (segments[p].type == HALO_PT_LOAD && (segments[p].flags & HALO_PF_R) && + entry->value >= segments[p].vaddr && + (uint64_t)entry->value + object_size <= (uint64_t)segments[p].vaddr + segments[p].memsz) + in_load_segment = 1; + } + if (found || !object_size || entry->size != object_size || + entry->shndx >= header->shnum || !entry->value || + entry->value >= IMPORT_BASE || !in_load_segment || + !mac_guest_address_resolve(entry->value, object_size)) + return -1; + found = 1; + found_va = entry->value; + continue; + } + *guest_va_out = entry->value; + return 0; + } + } + } + if (found) { *guest_va_out = found_va; return 0; } + return -1; +} + +int mac_guest_image_find_symbol(const void *elf_bytes, size_t elf_size, + const char *name, uint32_t *guest_va_out) +{ + return find_symbol_of_type(elf_bytes, elf_size, name, guest_va_out, HALO_STT_FUNC, 0); +} + +int mac_guest_image_find_object(const void *elf_bytes, size_t elf_size, + const char *name, uint32_t object_size, uint32_t *guest_va_out) +{ + return find_symbol_of_type(elf_bytes, elf_size, name, guest_va_out, HALO_STT_OBJECT, object_size); +} diff --git a/port/macos/guest_image.h b/port/macos/guest_image.h new file mode 100644 index 0000000000..556f16c77e --- /dev/null +++ b/port/macos/guest_image.h @@ -0,0 +1,37 @@ +#ifndef HALO_MACOS_GUEST_IMAGE_H +#define HALO_MACOS_GUEST_IMAGE_H + +#include +#include + +struct mac_guest_image +{ + void *arena_host_base; + uint32_t entry_va; + uint32_t code_lo_va; + uint32_t code_hi_va; + uint32_t image_end_va; + uint32_t tls_image_va; + size_t tls_file_size; + size_t tls_memory_size; + size_t tls_alignment; + ptrdiff_t memory_offset; +}; + +/* Loads an ELF32/i386 ET_EXEC image into a registered high host arena. The + * reserved host mapping backs the full 32-bit guest address space; PT_LOAD + * segments below the synthetic-import window are copied and BSS is zeroed. + * The 0x70000000 import segment is intentionally left unmapped for runtime + * token dispatch. Returns 0 or -1 with a bounded diagnostic in error_out. */ +int mac_guest_image_load(const void *elf_bytes, size_t elf_size, + struct mac_guest_image *image_out, char *error_out, size_t error_capacity); + +/* Look up a defined ELF32 symbol while the original ELF bytes are available. + * Used for runtime roots such as __guest_start and __guest_thread_start. */ +int mac_guest_image_find_symbol(const void *elf_bytes, size_t elf_size, + const char *name, uint32_t *guest_va_out); +/* Data-symbol lookup stays separate from the function-root lookup. */ +int mac_guest_image_find_object(const void *elf_bytes, size_t elf_size, + const char *name, uint32_t object_size, uint32_t *guest_va_out); + +#endif diff --git a/port/macos/guest_import_registry.c b/port/macos/guest_import_registry.c new file mode 100644 index 0000000000..a67a4898af --- /dev/null +++ b/port/macos/guest_import_registry.c @@ -0,0 +1,463 @@ +#include "guest_import_registry.h" + +#include "host_services.h" + +#include +#include +#include + +struct import_entry +{ + char *name; + mac_guest_import_bridge bridge; +}; + +static struct import_entry entries[MAC_GUEST_IMPORT_CAPACITY]; +static pthread_mutex_t entries_mutex = PTHREAD_MUTEX_INITIALIZER; +static size_t entry_count; + +static void unsupported_import_bridge(void) +{ + mac_host_abort("unsupported generated host import invoked"); +} + +static void dispatch_slot(size_t slot) +{ + mac_guest_import_bridge bridge = NULL; + const char *name = NULL; + pthread_mutex_lock(&entries_mutex); + if (slot < entry_count) + { + bridge = entries[slot].bridge; + name = entries[slot].name; + } + pthread_mutex_unlock(&entries_mutex); + if (bridge == unsupported_import_bridge) + { + mac_host_logf(3, "unsupported generated host import: %s", name ? name : ""); + mac_host_abort("unsupported generated host import invoked"); + } + if (!bridge) + { + mac_host_logf(3, "unregistered AOT import token slot %zu", slot); + mac_host_abort("unregistered AOT import token invoked"); + } + bridge(); +} + +void mac_guest_import_dispatch_token(uint32_t token) +{ + uint32_t delta; + if (token < MAC_GUEST_IMPORT_TOKEN_BASE) + { + mac_host_abort("invalid host-import token below reserved range"); + } + delta = token - MAC_GUEST_IMPORT_TOKEN_BASE; + if (delta % MAC_GUEST_IMPORT_TOKEN_STRIDE != 0) + { + mac_host_abort("unaligned host-import token"); + } + dispatch_slot(delta / MAC_GUEST_IMPORT_TOKEN_STRIDE); +} + +static void import_wrapper_0(void) { dispatch_slot(0); } +static void import_wrapper_1(void) { dispatch_slot(1); } +static void import_wrapper_2(void) { dispatch_slot(2); } +static void import_wrapper_3(void) { dispatch_slot(3); } +static void import_wrapper_4(void) { dispatch_slot(4); } +static void import_wrapper_5(void) { dispatch_slot(5); } +static void import_wrapper_6(void) { dispatch_slot(6); } +static void import_wrapper_7(void) { dispatch_slot(7); } +static void import_wrapper_8(void) { dispatch_slot(8); } +static void import_wrapper_9(void) { dispatch_slot(9); } +static void import_wrapper_10(void) { dispatch_slot(10); } +static void import_wrapper_11(void) { dispatch_slot(11); } +static void import_wrapper_12(void) { dispatch_slot(12); } +static void import_wrapper_13(void) { dispatch_slot(13); } +static void import_wrapper_14(void) { dispatch_slot(14); } +static void import_wrapper_15(void) { dispatch_slot(15); } +static void import_wrapper_16(void) { dispatch_slot(16); } +static void import_wrapper_17(void) { dispatch_slot(17); } +static void import_wrapper_18(void) { dispatch_slot(18); } +static void import_wrapper_19(void) { dispatch_slot(19); } +static void import_wrapper_20(void) { dispatch_slot(20); } +static void import_wrapper_21(void) { dispatch_slot(21); } +static void import_wrapper_22(void) { dispatch_slot(22); } +static void import_wrapper_23(void) { dispatch_slot(23); } +static void import_wrapper_24(void) { dispatch_slot(24); } +static void import_wrapper_25(void) { dispatch_slot(25); } +static void import_wrapper_26(void) { dispatch_slot(26); } +static void import_wrapper_27(void) { dispatch_slot(27); } +static void import_wrapper_28(void) { dispatch_slot(28); } +static void import_wrapper_29(void) { dispatch_slot(29); } +static void import_wrapper_30(void) { dispatch_slot(30); } +static void import_wrapper_31(void) { dispatch_slot(31); } +static void import_wrapper_32(void) { dispatch_slot(32); } +static void import_wrapper_33(void) { dispatch_slot(33); } +static void import_wrapper_34(void) { dispatch_slot(34); } +static void import_wrapper_35(void) { dispatch_slot(35); } +static void import_wrapper_36(void) { dispatch_slot(36); } +static void import_wrapper_37(void) { dispatch_slot(37); } +static void import_wrapper_38(void) { dispatch_slot(38); } +static void import_wrapper_39(void) { dispatch_slot(39); } +static void import_wrapper_40(void) { dispatch_slot(40); } +static void import_wrapper_41(void) { dispatch_slot(41); } +static void import_wrapper_42(void) { dispatch_slot(42); } +static void import_wrapper_43(void) { dispatch_slot(43); } +static void import_wrapper_44(void) { dispatch_slot(44); } +static void import_wrapper_45(void) { dispatch_slot(45); } +static void import_wrapper_46(void) { dispatch_slot(46); } +static void import_wrapper_47(void) { dispatch_slot(47); } +static void import_wrapper_48(void) { dispatch_slot(48); } +static void import_wrapper_49(void) { dispatch_slot(49); } +static void import_wrapper_50(void) { dispatch_slot(50); } +static void import_wrapper_51(void) { dispatch_slot(51); } +static void import_wrapper_52(void) { dispatch_slot(52); } +static void import_wrapper_53(void) { dispatch_slot(53); } +static void import_wrapper_54(void) { dispatch_slot(54); } +static void import_wrapper_55(void) { dispatch_slot(55); } +static void import_wrapper_56(void) { dispatch_slot(56); } +static void import_wrapper_57(void) { dispatch_slot(57); } +static void import_wrapper_58(void) { dispatch_slot(58); } +static void import_wrapper_59(void) { dispatch_slot(59); } +static void import_wrapper_60(void) { dispatch_slot(60); } +static void import_wrapper_61(void) { dispatch_slot(61); } +static void import_wrapper_62(void) { dispatch_slot(62); } +static void import_wrapper_63(void) { dispatch_slot(63); } +static void import_wrapper_64(void) { dispatch_slot(64); } +static void import_wrapper_65(void) { dispatch_slot(65); } +static void import_wrapper_66(void) { dispatch_slot(66); } +static void import_wrapper_67(void) { dispatch_slot(67); } +static void import_wrapper_68(void) { dispatch_slot(68); } +static void import_wrapper_69(void) { dispatch_slot(69); } +static void import_wrapper_70(void) { dispatch_slot(70); } +static void import_wrapper_71(void) { dispatch_slot(71); } +static void import_wrapper_72(void) { dispatch_slot(72); } +static void import_wrapper_73(void) { dispatch_slot(73); } +static void import_wrapper_74(void) { dispatch_slot(74); } +static void import_wrapper_75(void) { dispatch_slot(75); } +static void import_wrapper_76(void) { dispatch_slot(76); } +static void import_wrapper_77(void) { dispatch_slot(77); } +static void import_wrapper_78(void) { dispatch_slot(78); } +static void import_wrapper_79(void) { dispatch_slot(79); } +static void import_wrapper_80(void) { dispatch_slot(80); } +static void import_wrapper_81(void) { dispatch_slot(81); } +static void import_wrapper_82(void) { dispatch_slot(82); } +static void import_wrapper_83(void) { dispatch_slot(83); } +static void import_wrapper_84(void) { dispatch_slot(84); } +static void import_wrapper_85(void) { dispatch_slot(85); } +static void import_wrapper_86(void) { dispatch_slot(86); } +static void import_wrapper_87(void) { dispatch_slot(87); } +static void import_wrapper_88(void) { dispatch_slot(88); } +static void import_wrapper_89(void) { dispatch_slot(89); } +static void import_wrapper_90(void) { dispatch_slot(90); } +static void import_wrapper_91(void) { dispatch_slot(91); } +static void import_wrapper_92(void) { dispatch_slot(92); } +static void import_wrapper_93(void) { dispatch_slot(93); } +static void import_wrapper_94(void) { dispatch_slot(94); } +static void import_wrapper_95(void) { dispatch_slot(95); } +static void import_wrapper_96(void) { dispatch_slot(96); } +static void import_wrapper_97(void) { dispatch_slot(97); } +static void import_wrapper_98(void) { dispatch_slot(98); } +static void import_wrapper_99(void) { dispatch_slot(99); } +static void import_wrapper_100(void) { dispatch_slot(100); } +static void import_wrapper_101(void) { dispatch_slot(101); } +static void import_wrapper_102(void) { dispatch_slot(102); } +static void import_wrapper_103(void) { dispatch_slot(103); } +static void import_wrapper_104(void) { dispatch_slot(104); } +static void import_wrapper_105(void) { dispatch_slot(105); } +static void import_wrapper_106(void) { dispatch_slot(106); } +static void import_wrapper_107(void) { dispatch_slot(107); } +static void import_wrapper_108(void) { dispatch_slot(108); } +static void import_wrapper_109(void) { dispatch_slot(109); } +static void import_wrapper_110(void) { dispatch_slot(110); } +static void import_wrapper_111(void) { dispatch_slot(111); } +static void import_wrapper_112(void) { dispatch_slot(112); } +static void import_wrapper_113(void) { dispatch_slot(113); } +static void import_wrapper_114(void) { dispatch_slot(114); } +static void import_wrapper_115(void) { dispatch_slot(115); } +static void import_wrapper_116(void) { dispatch_slot(116); } +static void import_wrapper_117(void) { dispatch_slot(117); } +static void import_wrapper_118(void) { dispatch_slot(118); } +static void import_wrapper_119(void) { dispatch_slot(119); } +static void import_wrapper_120(void) { dispatch_slot(120); } +static void import_wrapper_121(void) { dispatch_slot(121); } +static void import_wrapper_122(void) { dispatch_slot(122); } +static void import_wrapper_123(void) { dispatch_slot(123); } +static void import_wrapper_124(void) { dispatch_slot(124); } +static void import_wrapper_125(void) { dispatch_slot(125); } +static void import_wrapper_126(void) { dispatch_slot(126); } +static void import_wrapper_127(void) { dispatch_slot(127); } + +static mac_guest_function const wrappers[MAC_GUEST_IMPORT_CAPACITY] = +{ + import_wrapper_0, + import_wrapper_1, + import_wrapper_2, + import_wrapper_3, + import_wrapper_4, + import_wrapper_5, + import_wrapper_6, + import_wrapper_7, + import_wrapper_8, + import_wrapper_9, + import_wrapper_10, + import_wrapper_11, + import_wrapper_12, + import_wrapper_13, + import_wrapper_14, + import_wrapper_15, + import_wrapper_16, + import_wrapper_17, + import_wrapper_18, + import_wrapper_19, + import_wrapper_20, + import_wrapper_21, + import_wrapper_22, + import_wrapper_23, + import_wrapper_24, + import_wrapper_25, + import_wrapper_26, + import_wrapper_27, + import_wrapper_28, + import_wrapper_29, + import_wrapper_30, + import_wrapper_31, + import_wrapper_32, + import_wrapper_33, + import_wrapper_34, + import_wrapper_35, + import_wrapper_36, + import_wrapper_37, + import_wrapper_38, + import_wrapper_39, + import_wrapper_40, + import_wrapper_41, + import_wrapper_42, + import_wrapper_43, + import_wrapper_44, + import_wrapper_45, + import_wrapper_46, + import_wrapper_47, + import_wrapper_48, + import_wrapper_49, + import_wrapper_50, + import_wrapper_51, + import_wrapper_52, + import_wrapper_53, + import_wrapper_54, + import_wrapper_55, + import_wrapper_56, + import_wrapper_57, + import_wrapper_58, + import_wrapper_59, + import_wrapper_60, + import_wrapper_61, + import_wrapper_62, + import_wrapper_63, + import_wrapper_64, + import_wrapper_65, + import_wrapper_66, + import_wrapper_67, + import_wrapper_68, + import_wrapper_69, + import_wrapper_70, + import_wrapper_71, + import_wrapper_72, + import_wrapper_73, + import_wrapper_74, + import_wrapper_75, + import_wrapper_76, + import_wrapper_77, + import_wrapper_78, + import_wrapper_79, + import_wrapper_80, + import_wrapper_81, + import_wrapper_82, + import_wrapper_83, + import_wrapper_84, + import_wrapper_85, + import_wrapper_86, + import_wrapper_87, + import_wrapper_88, + import_wrapper_89, + import_wrapper_90, + import_wrapper_91, + import_wrapper_92, + import_wrapper_93, + import_wrapper_94, + import_wrapper_95, + import_wrapper_96, + import_wrapper_97, + import_wrapper_98, + import_wrapper_99, + import_wrapper_100, + import_wrapper_101, + import_wrapper_102, + import_wrapper_103, + import_wrapper_104, + import_wrapper_105, + import_wrapper_106, + import_wrapper_107, + import_wrapper_108, + import_wrapper_109, + import_wrapper_110, + import_wrapper_111, + import_wrapper_112, + import_wrapper_113, + import_wrapper_114, + import_wrapper_115, + import_wrapper_116, + import_wrapper_117, + import_wrapper_118, + import_wrapper_119, + import_wrapper_120, + import_wrapper_121, + import_wrapper_122, + import_wrapper_123, + import_wrapper_124, + import_wrapper_125, + import_wrapper_126, + import_wrapper_127 +}; + +int mac_guest_import_register(const char *name, mac_guest_import_bridge bridge, + uint32_t *token_out) +{ + size_t index; + char *copy; + if (!name || !*name || !bridge) + return -1; + pthread_mutex_lock(&entries_mutex); + for (index = 0; index < entry_count; ++index) + { + if (strcmp(entries[index].name, name) == 0) + { + if (entries[index].bridge != bridge) + { + pthread_mutex_unlock(&entries_mutex); + return -1; + } + if (token_out) + *token_out = MAC_GUEST_IMPORT_TOKEN_BASE + (uint32_t)(index * MAC_GUEST_IMPORT_TOKEN_STRIDE); + pthread_mutex_unlock(&entries_mutex); + return 0; + } + } + if (entry_count == MAC_GUEST_IMPORT_CAPACITY) + { + pthread_mutex_unlock(&entries_mutex); + return -1; + } + copy = malloc(strlen(name) + 1); + if (!copy) + { + pthread_mutex_unlock(&entries_mutex); + return -1; + } + strcpy(copy, name); + entries[entry_count].name = copy; + entries[entry_count].bridge = bridge; + if (token_out) + *token_out = MAC_GUEST_IMPORT_TOKEN_BASE + (uint32_t)(entry_count * MAC_GUEST_IMPORT_TOKEN_STRIDE); + ++entry_count; + pthread_mutex_unlock(&entries_mutex); + return 0; +} + +int mac_guest_import_register_manifest(const char *const *names, size_t count, + const mac_guest_import_provider *providers, size_t provider_count, + mac_guest_import_bridge unsupported_fallback) +{ + mac_guest_import_bridge resolved[MAC_GUEST_IMPORT_CAPACITY]; + size_t index; + + if ((!names && count) || (!providers && provider_count) || + count > MAC_GUEST_IMPORT_CAPACITY) + return -1; + pthread_mutex_lock(&entries_mutex); + if (entry_count != 0) + { + pthread_mutex_unlock(&entries_mutex); + return -1; + } + pthread_mutex_unlock(&entries_mutex); + + for (index = 0; index < count; ++index) + { + if (!names[index] || !*names[index] || + (index && strcmp(names[index - 1], names[index]) >= 0)) + return -1; + resolved[index] = NULL; + for (size_t provider = 0; provider < provider_count; ++provider) + { + if (providers[provider] && (resolved[index] = providers[provider](names[index]))) + break; + } + if (!resolved[index]) + resolved[index] = unsupported_fallback ? unsupported_fallback : unsupported_import_bridge; + } + for (index = 0; index < count; ++index) + if (mac_guest_import_register(names[index], resolved[index], NULL) != 0) + return -1; + return 0; +} + +uint32_t mac_guest_import_token(const char *name) +{ + size_t index; + uint32_t token = 0; + if (!name) + return 0; + pthread_mutex_lock(&entries_mutex); + for (index = 0; index < entry_count; ++index) + { + if (strcmp(entries[index].name, name) == 0) + { + token = MAC_GUEST_IMPORT_TOKEN_BASE + (uint32_t)(index * MAC_GUEST_IMPORT_TOKEN_STRIDE); + break; + } + } + pthread_mutex_unlock(&entries_mutex); + return token; +} + +mac_guest_function mac_guest_import_resolve(uint32_t token) +{ + uint32_t offset; + size_t slot; + mac_guest_function function = NULL; + if (token < MAC_GUEST_IMPORT_TOKEN_BASE) + return NULL; + offset = token - MAC_GUEST_IMPORT_TOKEN_BASE; + if ((offset % MAC_GUEST_IMPORT_TOKEN_STRIDE) != 0) + return NULL; + slot = offset / MAC_GUEST_IMPORT_TOKEN_STRIDE; + pthread_mutex_lock(&entries_mutex); + if (slot < entry_count) + function = wrappers[slot]; + pthread_mutex_unlock(&entries_mutex); + return function; +} + +size_t mac_guest_import_count(void) +{ + size_t count; + pthread_mutex_lock(&entries_mutex); + count = entry_count; + pthread_mutex_unlock(&entries_mutex); + return count; +} + +void mac_guest_import_reset(void) +{ + size_t index; + pthread_mutex_lock(&entries_mutex); + for (index = 0; index < entry_count; ++index) + { + free(entries[index].name); + entries[index] = (struct import_entry){ 0 }; + } + entry_count = 0; + pthread_mutex_unlock(&entries_mutex); +} diff --git a/port/macos/guest_import_registry.h b/port/macos/guest_import_registry.h new file mode 100644 index 0000000000..9be80ba8bc --- /dev/null +++ b/port/macos/guest_import_registry.h @@ -0,0 +1,34 @@ +#ifndef HALO_MACOS_GUEST_IMPORT_REGISTRY_H +#define HALO_MACOS_GUEST_IMPORT_REGISTRY_H + +#include +#include + +typedef void (*mac_guest_function)(void); +typedef void (*mac_guest_import_bridge)(void); +typedef mac_guest_import_bridge (*mac_guest_import_provider)(const char *name); + +/* Synthetic guest VA subrange reserved for native host-import tokens. The + * AOT manual resolver must check this registry before the Xbox kernel thunk + * resolver. Each token is four-byte spaced and maps to a unique host wrapper. */ +#define MAC_GUEST_IMPORT_TOKEN_BASE UINT32_C(0x70000000) +#define MAC_GUEST_IMPORT_TOKEN_STRIDE 16u +#define MAC_GUEST_IMPORT_CAPACITY 128u + +int mac_guest_import_register(const char *name, mac_guest_import_bridge bridge, + uint32_t *token_out); +/* Registers one already sorted final ELF import manifest by asking each + * provider for a bridge by name. A supplied fallback is used for unknown + * imports and must fail loudly when invoked. */ +int mac_guest_import_register_manifest(const char *const *names, size_t count, + const mac_guest_import_provider *providers, size_t provider_count, + mac_guest_import_bridge unsupported_fallback); +uint32_t mac_guest_import_token(const char *name); +mac_guest_function mac_guest_import_resolve(uint32_t token); +/* Dispatch the bridge for one exact token; used by manifest-generated + * strong `sub_` aliases for direct AOT calls. */ +void mac_guest_import_dispatch_token(uint32_t token); +size_t mac_guest_import_count(void); +void mac_guest_import_reset(void); + +#endif diff --git a/port/macos/guest_thread.c b/port/macos/guest_thread.c new file mode 100644 index 0000000000..550719f1e2 --- /dev/null +++ b/port/macos/guest_thread.c @@ -0,0 +1,150 @@ +#include "guest_thread.h" + +#include "guest_allocator.h" +#include "guest_call.h" +#include "host_services.h" + +#include +#include +#include + +#define GUEST_THREAD_RETURN_SENTINEL UINT32_C(0xffffffff) +#define GUEST_STACK_ALIGNMENT 16u + +struct thread_start +{ + struct mac_guest_thread_runtime runtime; + uint32_t guest_thread_va; + uint32_t guest_start_va; + uint32_t guest_stack_base; + size_t guest_stack_size; + uint32_t initial_guest_esp; +}; + +static struct mac_guest_thread_runtime installed_runtime; +static uint32_t installed_guest_start_va; +static int runtime_installed; +static pthread_mutex_t runtime_mutex = PTHREAD_MUTEX_INITIALIZER; + +int mac_guest_thread_runtime_install(const struct mac_guest_thread_runtime *runtime, + uint32_t guest_start_va) +{ + int result = -1; + + if (!runtime || !runtime->initialize || !runtime->dispatch || !guest_start_va) + return -1; + pthread_mutex_lock(&runtime_mutex); + if (!runtime_installed) + { + installed_runtime = *runtime; + installed_guest_start_va = guest_start_va; + runtime_installed = 1; + result = 0; + } + pthread_mutex_unlock(&runtime_mutex); + return result; +} + +int mac_host_thread_create(uint32_t guest_thread_va, uint32_t stack_size) +{ + struct mac_guest_thread_runtime runtime; + uint32_t guest_start_va; + + pthread_mutex_lock(&runtime_mutex); + if (!runtime_installed) + { + pthread_mutex_unlock(&runtime_mutex); + return EINVAL; + } + runtime = installed_runtime; + guest_start_va = installed_guest_start_va; + pthread_mutex_unlock(&runtime_mutex); + return mac_guest_thread_create(&runtime, guest_thread_va, guest_start_va, stack_size); +} + +static void *guest_thread_main(void *opaque) +{ + struct thread_start *start = opaque; + struct mac_guest_thread_runtime runtime = start->runtime; + uint32_t guest_thread_va = start->guest_thread_va; + uint32_t guest_start_va = start->guest_start_va; + uint32_t guest_stack_base = start->guest_stack_base; + size_t guest_stack_size = start->guest_stack_size; + uint32_t guest_esp = start->initial_guest_esp; + + free(start); + mac_host_set_guest_tp(guest_thread_va); + runtime.initialize(runtime.context, guest_thread_va, guest_esp); + runtime.dispatch(runtime.context, guest_start_va); + mac_host_set_guest_tp(0); + (void)mac_guest_release(guest_stack_base, guest_stack_size); + return NULL; +} + +int mac_guest_thread_create(const struct mac_guest_thread_runtime *runtime, + uint32_t guest_thread_va, uint32_t guest_start_va, size_t stack_size) +{ + struct thread_start *start; + pthread_attr_t attributes; + pthread_t thread; + uint32_t guest_stack_base; + size_t guest_stack_size; + uint64_t guest_stack_end; + uint32_t guest_stack_top, guest_esp; + uint32_t frame[2]; + int error; + + if (!runtime || !runtime->initialize || !runtime->dispatch || !guest_thread_va || + !guest_start_va || stack_size < 16 * 1024) + return EINVAL; + guest_stack_size = stack_size; + if (mac_guest_allocate(guest_stack_size, GUEST_STACK_ALIGNMENT, &guest_stack_base) != 0) + return EAGAIN; + guest_stack_end = (uint64_t)guest_stack_base + guest_stack_size; + if (guest_stack_end > UINT32_MAX || + guest_stack_size < sizeof(frame) + GUEST_STACK_ALIGNMENT) + { + (void)mac_guest_release(guest_stack_base, guest_stack_size); + return EINVAL; + } + guest_stack_top = (uint32_t)guest_stack_end & ~(GUEST_STACK_ALIGNMENT - 1); + guest_esp = guest_stack_top - (uint32_t)sizeof(frame); + frame[0] = GUEST_THREAD_RETURN_SENTINEL; + frame[1] = guest_thread_va; + if (mac_guest_write(guest_esp, frame, sizeof(frame)) != 0) + { + (void)mac_guest_release(guest_stack_base, guest_stack_size); + return EFAULT; + } + + start = malloc(sizeof(*start)); + if (!start) + { + (void)mac_guest_release(guest_stack_base, guest_stack_size); + return ENOMEM; + } + start->runtime = *runtime; + start->guest_thread_va = guest_thread_va; + start->guest_start_va = guest_start_va; + start->guest_stack_base = guest_stack_base; + start->guest_stack_size = guest_stack_size; + start->initial_guest_esp = guest_esp; + + error = pthread_attr_init(&attributes); + if (error != 0) + { + free(start); + (void)mac_guest_release(guest_stack_base, guest_stack_size); + return error; + } + error = pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_DETACHED); + if (error == 0) + error = pthread_create(&thread, &attributes, guest_thread_main, start); + pthread_attr_destroy(&attributes); + if (error != 0) + { + free(start); + (void)mac_guest_release(guest_stack_base, guest_stack_size); + } + return error; +} diff --git a/port/macos/guest_thread.h b/port/macos/guest_thread.h new file mode 100644 index 0000000000..77d8ccf0be --- /dev/null +++ b/port/macos/guest_thread.h @@ -0,0 +1,30 @@ +#ifndef HALO_MACOS_GUEST_THREAD_H +#define HALO_MACOS_GUEST_THREAD_H + +#include +#include + +/* Runtime operations supplied by the AOT executable. Stack allocation uses + * the installed guest allocator; `initialize` runs on the new pthread and + * initializes that thread's recomp TLS/register/TIB state; `dispatch` resolves + * and invokes translated guest code (never casts a guest VA to a native + * function pointer). */ +struct mac_guest_thread_runtime +{ + void *context; + void (*initialize)(void *context, uint32_t guest_thread_va, uint32_t guest_esp); + void (*dispatch)(void *context, uint32_t guest_function_va); +}; + +/* Starts the guest's translated thread-start routine on a native pthread. + * Returns 0 or a POSIX error number, matching the Android host contract. */ +int mac_guest_thread_create(const struct mac_guest_thread_runtime *runtime, + uint32_t guest_thread_va, uint32_t guest_start_va, size_t stack_size); + +/* Import-facing form. The host image installs the runtime callbacks and the + * translated guest thread-start VA once before guest pthreads can be created. */ +int mac_guest_thread_runtime_install(const struct mac_guest_thread_runtime *runtime, + uint32_t guest_start_va); +int mac_host_thread_create(uint32_t guest_thread_va, uint32_t stack_size); + +#endif diff --git a/port/macos/guest_x87_80.c b/port/macos/guest_x87_80.c new file mode 100644 index 0000000000..537ea7bda4 --- /dev/null +++ b/port/macos/guest_x87_80.c @@ -0,0 +1,230 @@ +#include "guest_x87_80.h" + +#include + +static uint64_t read_le64(const uint8_t *bytes) +{ + uint64_t value = 0; + for (unsigned i = 0; i < 8; ++i) + value |= (uint64_t)bytes[i] << (i * 8); + return value; +} + +static uint16_t read_le16(const uint8_t *bytes) +{ + return (uint16_t)((uint16_t)bytes[0] | ((uint16_t)bytes[1] << 8)); +} + +static void write_le64(uint8_t *bytes, uint64_t value) +{ + for (unsigned i = 0; i < 8; ++i) + bytes[i] = (uint8_t)(value >> (i * 8)); +} + +static void write_le16(uint8_t *bytes, uint16_t value) +{ + bytes[0] = (uint8_t)value; + bytes[1] = (uint8_t)(value >> 8); +} + +static int double_from_bits(uint64_t bits, double *out) +{ + memcpy(out, &bits, sizeof(bits)); + return 0; +} + +static uint64_t round_right(uint64_t value, unsigned shift, unsigned sign, + unsigned rounding_mode, int *inexact) +{ + uint64_t quotient, remainder; + int increment = 0; + + if (!shift) + { + *inexact = 0; + return value; + } + if (shift < 64) + { + quotient = value >> shift; + remainder = value & ((UINT64_C(1) << shift) - 1); + *inexact = remainder != 0; + if (rounding_mode == 0) + { + uint64_t halfway = UINT64_C(1) << (shift - 1); + increment = remainder > halfway || (remainder == halfway && (quotient & 1)); + } + } + else if (shift == 64) + { + quotient = 0; + remainder = value; + *inexact = remainder != 0; + if (rounding_mode == 0) + increment = remainder > (UINT64_C(1) << 63); + } + else + { + quotient = 0; + remainder = value; + *inexact = remainder != 0; + } + + if (*inexact && ((rounding_mode == 1 && sign) || (rounding_mode == 2 && !sign))) + increment = 1; + return quotient + (unsigned)increment; +} + +static uint64_t overflow_bits(unsigned sign, unsigned rounding_mode) +{ + int to_infinity = rounding_mode == 0 || + (rounding_mode == 1 && sign) || (rounding_mode == 2 && !sign); + uint64_t magnitude = to_infinity ? UINT64_C(0x7FF0000000000000) : + UINT64_C(0x7FEFFFFFFFFFFFFF); + return ((uint64_t)sign << 63) | magnitude; +} + +int mac_guest_x87_80_load(const void *bytes10, unsigned rounding_mode, double *out) +{ + const uint8_t *bytes = bytes10; + uint64_t significand, bits, fraction; + uint16_t sign_exponent, exponent; + unsigned sign; + int unbiased; + + if (!bytes || !out || rounding_mode > 3) + return -1; + significand = read_le64(bytes); + sign_exponent = read_le16(bytes + 8); + sign = sign_exponent >> 15; + exponent = sign_exponent & 0x7FFFu; + + if (exponent == 0x7FFFu) + { + if (!(significand & UINT64_C(0x8000000000000000))) + return -1; + fraction = (significand & UINT64_C(0x7FFFFFFFFFFFFFFF)) >> 11; + if (!fraction && (significand & UINT64_C(0x7FFFFFFFFFFFFFFF))) + fraction = 1; + if (fraction && !(fraction & UINT64_C(0x0008000000000000))) + fraction |= UINT64_C(0x0008000000000000); /* quiet signaling NaNs */ + bits = ((uint64_t)sign << 63) | UINT64_C(0x7FF0000000000000) | fraction; + return double_from_bits(bits, out); + } + + if (exponent == 0) + { + if (significand == 0) + { + bits = (uint64_t)sign << 63; + return double_from_bits(bits, out); + } + /* Binary80 subnormals are below binary64's range; honor directed RC. */ + bits = ((uint64_t)sign << 63) | + ((rounding_mode == 1 && sign) || (rounding_mode == 2 && !sign) ? 1 : 0); + return double_from_bits(bits, out); + } + + /* Reject x87 unnormal encodings; accept pseudo-denormals as exponent 1. */ + if (!(significand & UINT64_C(0x8000000000000000))) + return -1; + unbiased = (int)(exponent ? exponent : 1) - 16383; + fraction = significand & UINT64_C(0x7FFFFFFFFFFFFFFF); + + if (unbiased >= -1022) + { + uint64_t rounded; + int inexact; + rounded = round_right(significand, 11, sign, rounding_mode, &inexact); + (void)inexact; + if (rounded == (UINT64_C(1) << 53)) + { + rounded >>= 1; + ++unbiased; + } + if (unbiased > 1023) + return double_from_bits(overflow_bits(sign, rounding_mode), out); + bits = ((uint64_t)sign << 63) | + ((uint64_t)(unbiased + 1023) << 52) | + (rounded & UINT64_C(0x000FFFFFFFFFFFFF)); + return double_from_bits(bits, out); + } + + /* Subnormal binary64 values are integer multiples of 2^-1074. */ + { + int right_shift = -(unbiased + 1011); + uint64_t units; + int inexact; + if (right_shift <= 0) + return -1; + units = round_right(significand, (unsigned)right_shift, sign, rounding_mode, &inexact); + if (units >= (UINT64_C(1) << 52)) + { + if (units == (UINT64_C(1) << 52)) + return double_from_bits(((uint64_t)sign << 63) | UINT64_C(0x0010000000000000), out); + return double_from_bits(overflow_bits(sign, rounding_mode), out); + } + (void)inexact; + bits = ((uint64_t)sign << 63) | units; + return double_from_bits(bits, out); + } +} + +int mac_guest_x87_80_store(void *bytes10, double value) +{ + uint8_t *bytes = bytes10; + uint64_t bits, significand, fraction; + uint16_t sign_exponent; + unsigned sign; + int exponent; + + if (!bytes) + return -1; + memcpy(&bits, &value, sizeof(bits)); + sign = (unsigned)(bits >> 63); + exponent = (int)((bits >> 52) & 0x7FFu); + fraction = bits & UINT64_C(0x000FFFFFFFFFFFFF); + + if (exponent == 0x7FF) + { + if (!fraction) + significand = UINT64_C(0x8000000000000000); + else + { + /* Preserve payload and quiet bit in the wider significand. */ + significand = UINT64_C(0x8000000000000000) | (fraction << 11); + if (!(significand & UINT64_C(0x4000000000000000))) + significand |= UINT64_C(0x4000000000000000); + } + sign_exponent = (uint16_t)((sign << 15) | 0x7FFFu); + } + else if (!exponent && !fraction) + { + significand = 0; + sign_exponent = (uint16_t)(sign << 15); + } + else if (exponent) + { + int unbiased = exponent - 1023; + significand = (UINT64_C(1) << 63) | (fraction << 11); + sign_exponent = (uint16_t)((sign << 15) | (unsigned)(unbiased + 16383)); + } + else + { + /* Normalize a binary64 subnormal exactly into binary80. */ + unsigned top = 0; + uint64_t mantissa = fraction; + while (mantissa >>= 1) + ++top; + mantissa = fraction; + { + int unbiased = (int)top - 1074; + significand = mantissa << (63 - top); + sign_exponent = (uint16_t)((sign << 15) | (unsigned)(unbiased + 16383)); + } + } + + write_le64(bytes, significand); + write_le16(bytes + 8, sign_exponent); + return 0; +} diff --git a/port/macos/guest_x87_80.h b/port/macos/guest_x87_80.h new file mode 100644 index 0000000000..2e469c08f7 --- /dev/null +++ b/port/macos/guest_x87_80.h @@ -0,0 +1,19 @@ +#ifndef HALO_MACOS_GUEST_X87_80_H +#define HALO_MACOS_GUEST_X87_80_H + +#include + +/* + * Convert the little-endian 10-byte x87 binary80 memory representation used + * by the guest. The helpers do not cast through host long double (which is + * binary64 on Apple Silicon). Loading rounds finite values to binary64 using + * the supplied x87 RC bits (nearest-even, down, up, toward-zero). This models + * the runtime's double-backed x87 registers and therefore loses precision + * for values that require more than 53 significand bits. Invalid x87 + * encodings fail closed. Return 0 on success and -1 on invalid arguments or + * unsupported encodings. + */ +int mac_guest_x87_80_load(const void *bytes10, unsigned rounding_mode, double *out); +int mac_guest_x87_80_store(void *bytes10, double value); + +#endif diff --git a/port/macos/host_app_icon.h b/port/macos/host_app_icon.h new file mode 100644 index 0000000000..809a3999a5 --- /dev/null +++ b/port/macos/host_app_icon.h @@ -0,0 +1,8 @@ +#ifndef HALO_HOST_APP_ICON_H +#define HALO_HOST_APP_ICON_H + +/* Apply the bundle's icon to the native SDL process on the AppKit thread. */ +void mac_host_apply_app_icon(void); +void mac_host_activate_app(void); + +#endif diff --git a/port/macos/host_app_icon.m b/port/macos/host_app_icon.m new file mode 100644 index 0000000000..e613ce6106 --- /dev/null +++ b/port/macos/host_app_icon.m @@ -0,0 +1,36 @@ +#import + +#include "host_app_icon.h" +#include +#include + +void mac_host_activate_app(void) +{ + if (![NSThread isMainThread]) + return; + @autoreleasepool { + [[NSApplication sharedApplication] activate]; + } +} + +void mac_host_apply_app_icon(void) +{ + if (![NSThread isMainThread]) + return; + @autoreleasepool { + const char *configured_path = getenv("HALO_APP_ICON"); + NSString *path = configured_path && configured_path[0] + ? [NSString stringWithUTF8String:configured_path] + : [[NSBundle mainBundle] pathForResource:@"HaloCombatEvolved" ofType:@"icns"]; + if (!path) + return; + NSImage *icon = [[NSImage alloc] initWithContentsOfFile:path]; + if (icon) { + [[NSApplication sharedApplication] setApplicationIconImage:icon]; + fprintf(stderr, "[macos] native application icon loaded: %s\n", [path fileSystemRepresentation]); + [icon release]; + } else { + fprintf(stderr, "[macos] cannot load application icon: %s\n", [path fileSystemRepresentation]); + } + } +} diff --git a/port/macos/host_bink.c b/port/macos/host_bink.c new file mode 100644 index 0000000000..1949cfcd0c --- /dev/null +++ b/port/macos/host_bink.c @@ -0,0 +1,459 @@ +#include "host_bink.h" +#include "guest_address.h" +#include "guest_call.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct halo_bink +{ + AVFormatContext *format; + AVCodecContext *video, *audio; + AVFrame *video_frame, *audio_frame; + AVPacket *packet; + struct SwsContext *scale; + struct SwrContext *resample; + SDL_AudioStream *output; + int video_index, audio_index; + unsigned char *pixels; + struct halo_bink_info info; + struct halo_bink_evidence evidence; + uint64_t start_ns; + uint64_t copied_decode_count, presented_decode_count; + uint64_t first_present_ns, last_present_ns; + uint64_t presentation_gaps[1024], gap_count, long_gaps; + uint64_t gap_min_ns, gap_max_ns; + int pending_close, tail_error, audio_preloaded; + AVRational rate; +}; +_Static_assert(sizeof(struct halo_bink_info) == 20, "BINK leading five guest DWORDs"); + +static int preload_audio(struct halo_bink *movie, const char *path); +static void audio_snapshot(struct halo_bink *movie, const char *phase); + +static AVCodecContext *open_codec(AVStream *stream) +{ + const AVCodec *codec = avcodec_find_decoder(stream->codecpar->codec_id); + AVCodecContext *context = codec ? avcodec_alloc_context3(codec) : NULL; + if (!context) return NULL; + if (avcodec_parameters_to_context(context, stream->codecpar) < 0 || + avcodec_open2(context, codec, NULL) < 0) { avcodec_free_context(&context); return NULL; } + return context; +} + +struct halo_bink *mac_bink_open_path(const char *path, int audio_output) +{ + struct halo_bink *movie = calloc(1, sizeof(*movie)); + if (!movie) return NULL; + movie->video_index = movie->audio_index = -1; + if (avformat_open_input(&movie->format, path, NULL, NULL) < 0 || + avformat_find_stream_info(movie->format, NULL) < 0) goto fail; + movie->video_index = av_find_best_stream(movie->format, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0); + if (movie->video_index < 0) goto fail; + AVStream *stream = movie->format->streams[movie->video_index]; + if (stream->codecpar->codec_id != AV_CODEC_ID_BINKVIDEO) goto fail; + movie->video = open_codec(stream); + if (!movie->video || movie->video->width <= 0 || movie->video->height <= 0 || + movie->video->width > 4096 || movie->video->height > 4096) goto fail; + movie->rate = av_guess_frame_rate(movie->format, stream, NULL); + if (movie->rate.num <= 0 || movie->rate.den <= 0) goto fail; + int64_t frame_count = stream->nb_frames > 0 ? stream->nb_frames : + av_rescale_q(stream->duration, stream->time_base, av_inv_q(movie->rate)); + if (frame_count <= 0 || frame_count > UINT32_MAX) goto fail; + movie->info = (struct halo_bink_info){(uint32_t)movie->video->width, + (uint32_t)movie->video->height, (uint32_t)frame_count, 0, 0}; + movie->pixels = malloc((size_t)movie->info.width * movie->info.height * 4); + movie->video_frame = av_frame_alloc(); movie->audio_frame = av_frame_alloc(); + movie->packet = av_packet_alloc(); + if (!movie->pixels || !movie->video_frame || !movie->audio_frame || !movie->packet) goto fail; + movie->audio_index = av_find_best_stream(movie->format, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0); + if (movie->audio_index >= 0) + { + movie->audio = open_codec(movie->format->streams[movie->audio_index]); + if (!movie->audio) goto fail; + AVChannelLayout stereo = AV_CHANNEL_LAYOUT_STEREO; + if (swr_alloc_set_opts2(&movie->resample, &stereo, AV_SAMPLE_FMT_FLT, 48000, + &movie->audio->ch_layout, movie->audio->sample_fmt, movie->audio->sample_rate, 0, NULL) < 0 || + swr_init(movie->resample) < 0) goto fail; + if (audio_output) + { + SDL_AudioSpec spec = {SDL_AUDIO_F32, 2, 48000}; + movie->output = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec, NULL, NULL); + if (!movie->output) { fprintf(stderr,"[bink] audio device failed: %s\n", SDL_GetError()); goto fail; } + } + } + /* Decode the soundtrack independently of rendering. A slow first window + * frame must never starve audio or leave delayed PCM behind the movie. */ + if (movie->audio && !preload_audio(movie, path)) goto fail; + return movie; +fail: + mac_bink_close(movie); + return NULL; +} + +static int submit_pcm(struct halo_bink *movie, float *pcm, int frames) +{ + for (int i = 0; i < frames * 2; ++i) + { + if (!isfinite(pcm[i])) return 0; + ++movie->evidence.pcm_samples; + movie->evidence.nonzero_pcm_samples += pcm[i] != 0; + double amplitude = fabs((double)pcm[i]); + if (amplitude > movie->evidence.pcm_peak) movie->evidence.pcm_peak = amplitude; + } + return !movie->output || SDL_PutAudioStreamData(movie->output, pcm, frames * 2 * (int)sizeof(float)); +} + +static int decode_audio(struct halo_bink *movie) +{ + while (avcodec_receive_frame(movie->audio, movie->audio_frame) == 0) + { + int capacity = swr_get_out_samples(movie->resample, movie->audio_frame->nb_samples); + if (capacity <= 0 || capacity > 1048576) return 0; + float *pcm = malloc((size_t)capacity * 2 * sizeof(float)); + if (!pcm) return 0; + unsigned char *output = (unsigned char *)pcm; + int frames = swr_convert(movie->resample, &output, capacity, + (const unsigned char **)movie->audio_frame->extended_data, movie->audio_frame->nb_samples); + if (frames < 0) { free(pcm); return 0; } + int success = submit_pcm(movie, pcm, frames); + free(pcm); av_frame_unref(movie->audio_frame); + if (!success) return 0; + } + return 1; +} + +static int preload_audio(struct halo_bink *movie, const char *path) +{ + AVFormatContext *audio_file = NULL; + if (avformat_open_input(&audio_file, path, NULL, NULL) < 0) return 0; + int ok = 1; + while (av_read_frame(audio_file, movie->packet) >= 0) + { + if (movie->packet->stream_index == movie->audio_index) + ok = avcodec_send_packet(movie->audio, movie->packet) >= 0 && decode_audio(movie); + av_packet_unref(movie->packet); + if (!ok) break; + } + avformat_close_input(&audio_file); + if (!ok) return 0; + int result = avcodec_send_packet(movie->audio, NULL); + if ((result < 0 && result != AVERROR_EOF) || !decode_audio(movie)) return 0; + for (;;) + { + float pcm[2048]; unsigned char *output = (unsigned char *)pcm; + int frames = swr_convert(movie->resample, &output, 1024, NULL, 0); + if (frames < 0 || !submit_pcm(movie, pcm, frames)) return 0; + if (!frames) break; + } + if (movie->output && !SDL_FlushAudioStream(movie->output)) return 0; + movie->audio_preloaded = movie->evidence.audio_drained = 1; + audio_snapshot(movie, "preloaded"); + return 1; +} + +static void audio_snapshot(struct halo_bink *movie, const char *phase) +{ + if (!movie->output) return; + int queued = SDL_GetAudioStreamQueued(movie->output); + int available = SDL_GetAudioStreamAvailable(movie->output); + SDL_AudioDeviceID device = SDL_GetAudioStreamDevice(movie->output); + SDL_AudioSpec src = {0}, dst = {0}; + SDL_GetAudioStreamFormat(movie->output, &src, &dst); + uint64_t submitted = movie->evidence.pcm_samples * sizeof(float); + /* This stream never changes input format. Consumption means delivery to + * the device mixer, with final hardware buffers accounted for on close. */ + uint64_t consumed = queued >= 0 && submitted >= (uint64_t)queued ? submitted - (uint64_t)queued : 0; + fprintf(stderr,"[bink-audio] phase=%s frame=%llu elapsed_ms=%.3f queued=%d available=%d consumed_samples=%llu submitted_samples=%llu device=%u paused=%d input=%x/%d/%d output=%x/%d/%d\n", + phase,(unsigned long long)movie->evidence.video_frames, + movie->start_ns ? (double)(SDL_GetTicksNS()-movie->start_ns)/1e6 : 0, + queued,available,(unsigned long long)(consumed/sizeof(float)), + (unsigned long long)movie->evidence.pcm_samples,device,SDL_AudioDevicePaused(device), + src.format,src.channels,src.freq,dst.format,dst.channels,dst.freq); +} + +static int finish_audio(struct halo_bink *movie) +{ + if (movie->evidence.audio_drained) return 1; + while (av_read_frame(movie->format, movie->packet) >= 0) + { + int ok = 1; + if (movie->packet->stream_index == movie->audio_index) + ok = avcodec_send_packet(movie->audio, movie->packet) >= 0 && decode_audio(movie); + av_packet_unref(movie->packet); + if (!ok) return 0; + } + if (movie->audio) + { + int result = avcodec_send_packet(movie->audio, NULL); + if (result < 0 && result != AVERROR_EOF) return 0; + if (!decode_audio(movie)) return 0; + for (;;) + { + float pcm[2048]; + unsigned char *output = (unsigned char *)pcm; + int frames = swr_convert(movie->resample, &output, 1024, NULL, 0); + if (frames < 0 || !submit_pcm(movie, pcm, frames)) return 0; + if (!frames) break; + } + } + if (movie->output && !SDL_FlushAudioStream(movie->output)) return 0; + movie->evidence.audio_drained = 1; + return 1; +} + +int mac_bink_decode(struct halo_bink *movie) +{ + if (!movie) return 0; + for (;;) + { + int result = avcodec_receive_frame(movie->video, movie->video_frame); + if (result == 0) + { + movie->scale = sws_getCachedContext(movie->scale, movie->video->width, movie->video->height, + movie->video_frame->format, movie->video->width, movie->video->height, + AV_PIX_FMT_BGRA, SWS_BILINEAR, NULL, NULL, NULL); + if (!movie->scale) return 0; + unsigned char *destination[4] = {movie->pixels, NULL, NULL, NULL}; + int pitches[4] = {(int)movie->info.width * 4, 0, 0, 0}; + if (sws_scale(movie->scale, (const unsigned char *const *)movie->video_frame->data, + movie->video_frame->linesize, 0, movie->video->height, destination, pitches) <= 0) return 0; + av_frame_unref(movie->video_frame); ++movie->evidence.video_frames; + if (!movie->start_ns) movie->start_ns = SDL_GetTicksNS(); + if (movie->evidence.video_frames % 60 == 0) audio_snapshot(movie, "playing"); + return 1; + } + if (result != AVERROR(EAGAIN) || av_read_frame(movie->format, movie->packet) < 0) return 0; + int ok = 1; + if (movie->packet->stream_index == movie->video_index) + ok = avcodec_send_packet(movie->video, movie->packet) >= 0; + else if (!movie->audio_preloaded && movie->packet->stream_index == movie->audio_index) + ok = avcodec_send_packet(movie->audio, movie->packet) >= 0 && decode_audio(movie); + av_packet_unref(movie->packet); + if (!ok) return 0; + } +} + +void mac_bink_next(struct halo_bink *movie) +{ + if (!movie) return; + movie->info.last_frame_num = movie->info.frame_num; + /* The game advances before it copies/draws and stops at Frames-1. + * FrameNum names the decoded zero-based frame, not the next frame. */ + if (movie->evidence.video_frames) + movie->info.frame_num = (uint32_t)(movie->evidence.video_frames - 1); + if (movie->evidence.video_frames == movie->info.frames && !finish_audio(movie)) + movie->tail_error = 1; +} +int mac_bink_wait(struct halo_bink *movie) +{ + if (!movie || !movie->start_ns) return 0; + /* Draw frame zero immediately; subsequent swaps land at file PTS. The + * final frame receives its own full duration in natural close below. */ + uint64_t target = movie->start_ns + (uint64_t)av_rescale((int64_t)movie->evidence.video_frames - 1, + INT64_C(1000000000) * movie->rate.den, movie->rate.num); + uint64_t now = SDL_GetTicksNS(); + if (now >= target) return 0; + /* The guest's unthrottled BinkWait loop polls; yield instead of burning CPU. */ + if (target - now > 1000000) SDL_Delay(1); + return 1; +} +const struct halo_bink_info *mac_bink_info(const struct halo_bink *movie) { return &movie->info; } +const unsigned char *mac_bink_pixels(const struct halo_bink *movie) { return movie->pixels; } +const struct halo_bink_evidence *mac_bink_evidence(const struct halo_bink *movie) { return &movie->evidence; } +int mac_bink_start_playback(struct halo_bink *movie) +{ + if (!movie) return 0; + /* The first displayed frame defines PTS zero for both soundtrack and video. */ + movie->start_ns = SDL_GetTicksNS(); + if (movie->output && !SDL_ResumeAudioStreamDevice(movie->output)) + { fprintf(stderr,"[bink] audio resume failed: %s\n",SDL_GetError()); movie->tail_error = 1; return 0; } + audio_snapshot(movie, "started"); + return 1; +} + +int mac_bink_audio_pending(const struct halo_bink *movie) +{ + if (!movie || !movie->output) return 0; + int queued = SDL_GetAudioStreamQueued(movie->output); + int available = SDL_GetAudioStreamAvailable(movie->output); + return queued < 0 || available < 0 ? -1 : queued > available ? queued : available; +} +void mac_bink_close(struct halo_bink *movie) +{ + if (!movie) return; + if (movie->output) SDL_DestroyAudioStream(movie->output); + swr_free(&movie->resample); sws_freeContext(movie->scale); + avcodec_free_context(&movie->video); avcodec_free_context(&movie->audio); + av_frame_free(&movie->video_frame); av_frame_free(&movie->audio_frame); + av_packet_free(&movie->packet); avformat_close_input(&movie->format); + free(movie->pixels); free(movie); +} + +static struct halo_bink *movies[8]; /* GL-owning guest thread only. */ +uint32_t mac_host_bink_open(uint32_t path_va, uint32_t info_va, int audio_enabled) +{ + const char *path = mac_guest_address_resolve(path_va, 1); + size_t available = mac_guest_address_available(path_va); + size_t limit = available < 4096 ? available : 4096; + if (!path || !limit || strnlen(path, limit) == limit || + !mac_guest_address_resolve(info_va, sizeof(struct halo_bink_info))) return 0; + for (uint32_t i = 1; i < 8; ++i) if (!movies[i]) + { + const char *enabled = getenv("HALO_AUDIO_ENABLE"); + movies[i] = mac_bink_open_path(path, audio_enabled && enabled && strcmp(enabled, "1") == 0); + if (!movies[i]) return 0; + if (mac_guest_write(info_va, &movies[i]->info, sizeof(movies[i]->info)) != 0) { mac_host_bink_close(i); return 0; } + fprintf(stderr,"[bink] opened native video %ux%u frames=%u audio=%d\n",movies[i]->info.width,movies[i]->info.height,movies[i]->info.frames,movies[i]->output != NULL); + return i; + } + return 0; +} +int mac_host_bink_decode(uint32_t token) { return token < 8 && mac_bink_decode(movies[token]); } +int mac_host_bink_next(uint32_t token, uint32_t info_va) +{ + if (token >= 8 || !movies[token] || !mac_guest_address_resolve(info_va, sizeof(struct halo_bink_info))) return 0; + mac_bink_next(movies[token]); return mac_guest_write(info_va, &movies[token]->info, sizeof(movies[token]->info)) == 0; +} +int mac_host_bink_wait(uint32_t token) { return token < 8 ? mac_bink_wait(movies[token]) : 0; } +int mac_host_bink_copy(uint32_t token, uint32_t destination_va, int32_t pitch, + uint32_t height, uint32_t x, uint32_t y, uint32_t flags) +{ + struct halo_bink *movie = token < 8 ? movies[token] : NULL; + if (!movie || !movie->evidence.video_frames || (flags & 0xff) != 3 || pitch <= 0 || + x > UINT32_MAX / 4 || (uint64_t)(x + (uint64_t)movie->info.width) * 4 > (uint32_t)pitch || + y > height || movie->info.height > height - y) return 0; + uint64_t offset = (uint64_t)y * (uint32_t)pitch + (uint64_t)x * 4; + uint64_t size = (uint64_t)(movie->info.height - 1) * (uint32_t)pitch + (uint64_t)movie->info.width * 4; + if ((uint64_t)destination_va + offset > UINT32_MAX || size > SIZE_MAX) return 0; + unsigned char *destination = mac_guest_address_resolve((uint32_t)(destination_va + offset), (size_t)size); + if (!destination) return 0; + for (uint32_t row = 0; row < movie->info.height; ++row) + memcpy(destination + (size_t)row * (uint32_t)pitch, movie->pixels + (size_t)row * movie->info.width * 4, (size_t)movie->info.width * 4); + if (movie->copied_decode_count != movie->evidence.video_frames) + { + movie->copied_decode_count = movie->evidence.video_frames; + ++movie->evidence.copied_frames; + } + return 1; +} +static int compare_gap(const void *a, const void *b) +{ + uint64_t first = *(const uint64_t *)a, second = *(const uint64_t *)b; + return (first > second) - (first < second); +} + +static void report_cadence(struct halo_bink *movie) +{ + size_t count = movie->gap_count < 1024 ? (size_t)movie->gap_count : 1024; + qsort(movie->presentation_gaps, count, sizeof(movie->presentation_gaps[0]), compare_gap); + double median_ms = count ? (double)movie->presentation_gaps[count/2]/1e6 : 0; + if (count && !(count & 1)) median_ms = ((double)movie->presentation_gaps[count/2-1] + movie->presentation_gaps[count/2])/2e6; + double first_error_ms = movie->first_present_ns && movie->start_ns ? + (double)((int64_t)movie->first_present_ns - (int64_t)movie->start_ns)/1e6 : 0; + double final_error_ms = movie->last_present_ns && movie->start_ns ? + (double)((int64_t)movie->last_present_ns - (int64_t)movie->start_ns)/1e6 - + (double)(movie->evidence.video_frames - 1) * movie->rate.den * 1000.0 / movie->rate.num : 0; + fprintf(stderr,"[bink-cadence] presented=%llu gaps=%llu sampled_gaps=%zu min_ms=%.3f median_ms=%.3f max_ms=%.3f gaps_over_50ms=%llu first_pts_error_ms=%.3f final_pts_error_ms=%.3f\n", + (unsigned long long)movie->evidence.presented_frames,(unsigned long long)movie->gap_count,count, + (double)movie->gap_min_ns/1e6,median_ms,(double)movie->gap_max_ns/1e6, + (unsigned long long)movie->long_gaps,first_error_ms,final_error_ms); +} + +static void close_native_movie(uint32_t token, int natural) +{ + if (token >= 8 || !movies[token]) return; + struct halo_bink *movie = movies[token]; + int queued = 0, available = 0, timeout = 0; + if (natural && movie->output) + { + audio_snapshot(movie, "final-presented"); + uint64_t deadline = SDL_GetTicks() + 2000; + for (;;) + { + queued = SDL_GetAudioStreamQueued(movie->output); + available = SDL_GetAudioStreamAvailable(movie->output); + if (queued == 0 && available == 0) break; + if (queued < 0 || available < 0) { movie->tail_error = 1; break; } + if (SDL_GetTicks() >= deadline) { timeout = 1; break; } + SDL_Delay(1); + } + /* The queue reaches zero after delivery to the audio device. Allow + * its final buffer to play before destroying the stream. */ + SDL_AudioSpec spec; int frames = 0; + if (!timeout && SDL_GetAudioDeviceFormat(SDL_GetAudioStreamDevice(movie->output), &spec, &frames) && spec.freq > 0) + SDL_Delay((uint32_t)((uint64_t)frames * 2000 / (uint32_t)spec.freq + 1)); + } + if (natural && movie->last_present_ns) + { + uint64_t until = movie->last_present_ns + (uint64_t)av_rescale(1, + INT64_C(1000000000) * movie->rate.den, movie->rate.num); + while (SDL_GetTicksNS() < until) SDL_Delay(1); + } + if (natural) audio_snapshot(movie, "closed"); + report_cadence(movie); + fprintf(stderr,"[bink] closed frames=%llu copied=%llu presented=%llu expected=%u duration_ms=%.3f elapsed_ms=%.3f presentation_ms=%.3f audio_drained=%d queued=%d available=%d timeout=%d tail_error=%d natural=%d pcm_samples=%llu nonzero_samples=%llu peak=%.6f\n", + (unsigned long long)movies[token]->evidence.video_frames,(unsigned long long)movies[token]->evidence.copied_frames, + (unsigned long long)movie->evidence.presented_frames, movie->info.frames, + (double)movie->info.frames * movie->rate.den * 1000.0 / movie->rate.num, + movie->start_ns ? (double)(SDL_GetTicksNS()-movie->start_ns)/1e6 : 0, + movie->first_present_ns ? (double)(movie->last_present_ns-movie->first_present_ns)/1e6 : 0, + movie->evidence.audio_drained, queued, available, timeout, movie->tail_error, natural, + (unsigned long long)movie->evidence.pcm_samples, (unsigned long long)movie->evidence.nonzero_pcm_samples, movie->evidence.pcm_peak); + mac_bink_close(movies[token]); movies[token] = NULL; +} + +void mac_host_bink_close(uint32_t token) +{ + if (token >= 8 || !movies[token]) return; + if (movies[token]->evidence.video_frames == movies[token]->info.frames && + movies[token]->copied_decode_count > movies[token]->presented_decode_count) + { + /* Guest BinkClose runs inside its draw, before the final host swap. */ + movies[token]->pending_close = 1; + return; + } + close_native_movie(token, 0); +} + +void mac_host_bink_presented(void) +{ + for (uint32_t token = 1; token < 8; ++token) + { + struct halo_bink *movie = movies[token]; + if (!movie) continue; + if (movie->copied_decode_count > movie->presented_decode_count) + { + movie->presented_decode_count = movie->copied_decode_count; + ++movie->evidence.presented_frames; + uint64_t presented_ns = SDL_GetTicksNS(); + if (movie->last_present_ns) + { + uint64_t gap = presented_ns - movie->last_present_ns; + if (movie->gap_count < 1024) movie->presentation_gaps[movie->gap_count] = gap; + if (!movie->gap_count || gap < movie->gap_min_ns) movie->gap_min_ns = gap; + if (gap > movie->gap_max_ns) movie->gap_max_ns = gap; + movie->long_gaps += gap > UINT64_C(50000000); + ++movie->gap_count; + } + movie->last_present_ns = presented_ns; + if (!movie->first_present_ns) + { + movie->first_present_ns = movie->last_present_ns; + mac_bink_start_playback(movie); + } + if (getenv("HALO_BINK_FRAME_TRACE")) + fprintf(stderr,"[bink-frame] token=%u frame=%llu expected=%u pts_ms=%.3f elapsed_ms=%.3f\n", token, + (unsigned long long)movie->presented_decode_count, movie->info.frames, + (double)(movie->presented_decode_count - 1) * movie->rate.den * 1000.0 / movie->rate.num, + (double)(SDL_GetTicksNS()-movie->start_ns)/1e6); + } + if (movie->pending_close) close_native_movie(token, 1); + } +} diff --git a/port/macos/host_bink.h b/port/macos/host_bink.h new file mode 100644 index 0000000000..f1cea9dd50 --- /dev/null +++ b/port/macos/host_bink.h @@ -0,0 +1,26 @@ +#ifndef HALO_HOST_BINK_H +#define HALO_HOST_BINK_H +#include +#include +struct halo_bink; +struct halo_bink_info { uint32_t width, height, frames, frame_num, last_frame_num; }; +struct halo_bink_evidence { uint64_t video_frames, pcm_samples, nonzero_pcm_samples; double pcm_peak; uint64_t copied_frames, presented_frames; int audio_drained; }; +struct halo_bink *mac_bink_open_path(const char *path, int audio_output); +int mac_bink_decode(struct halo_bink *movie); +void mac_bink_next(struct halo_bink *movie); +int mac_bink_wait(struct halo_bink *movie); +const struct halo_bink_info *mac_bink_info(const struct halo_bink *movie); +const unsigned char *mac_bink_pixels(const struct halo_bink *movie); +const struct halo_bink_evidence *mac_bink_evidence(const struct halo_bink *movie); +int mac_bink_start_playback(struct halo_bink *movie); +int mac_bink_audio_pending(const struct halo_bink *movie); +void mac_bink_close(struct halo_bink *movie); +uint32_t mac_host_bink_open(uint32_t path_va, uint32_t info_va, int audio_enabled); +int mac_host_bink_decode(uint32_t token); +int mac_host_bink_next(uint32_t token, uint32_t info_va); +int mac_host_bink_wait(uint32_t token); +int mac_host_bink_copy(uint32_t token, uint32_t destination_va, int32_t pitch, + uint32_t height, uint32_t x, uint32_t y, uint32_t flags); +void mac_host_bink_close(uint32_t token); +void mac_host_bink_presented(void); +#endif diff --git a/port/macos/host_frame_metrics.h b/port/macos/host_frame_metrics.h new file mode 100644 index 0000000000..0a6ca1df3f --- /dev/null +++ b/port/macos/host_frame_metrics.h @@ -0,0 +1,56 @@ +#ifndef HALO_HOST_FRAME_METRICS_H +#define HALO_HOST_FRAME_METRICS_H + +#include +#include + +#define HALO_FRAME_SAMPLE_COUNT 120 +struct halo_frame_metrics +{ + uint64_t previous_ns, elapsed_ns, swap_ns; + uint64_t intervals[HALO_FRAME_SAMPLE_COUNT]; + unsigned count; +}; + +/* Called only on the GL thread, after a successful swap. Timestamp gaps + * measure delivered frames; swap time separately includes vsync waits. */ +static inline int halo_frame_metrics_add(struct halo_frame_metrics *metrics, + uint64_t completed_ns, uint64_t swap_ns) +{ + if (!metrics->previous_ns || completed_ns <= metrics->previous_ns) + { + metrics->previous_ns = completed_ns; + return 0; + } + uint64_t interval = completed_ns - metrics->previous_ns; + metrics->previous_ns = completed_ns; + metrics->intervals[metrics->count++] = interval; + metrics->elapsed_ns += interval; + metrics->swap_ns += swap_ns; + return metrics->count == HALO_FRAME_SAMPLE_COUNT; +} + +static inline uint64_t halo_frame_metrics_percentile(const struct halo_frame_metrics *metrics, + unsigned percentile) +{ + uint64_t sorted[HALO_FRAME_SAMPLE_COUNT]; + if (!metrics->count || percentile > 100) return 0; + for (unsigned i = 0; i < metrics->count; ++i) + { + uint64_t value = metrics->intervals[i]; + unsigned j = i; + while (j && sorted[j - 1] > value) { sorted[j] = sorted[j - 1]; --j; } + sorted[j] = value; + } + unsigned rank = (metrics->count * percentile + 99) / 100; + return sorted[rank ? rank - 1 : 0]; +} + +static inline void halo_frame_metrics_reset_window(struct halo_frame_metrics *metrics) +{ + metrics->count = 0; + metrics->elapsed_ns = 0; + metrics->swap_ns = 0; +} + +#endif diff --git a/port/macos/host_game_evidence.c b/port/macos/host_game_evidence.c new file mode 100644 index 0000000000..8df3efe8fe --- /dev/null +++ b/port/macos/host_game_evidence.c @@ -0,0 +1,51 @@ +/* Diagnostic-only load evidence, resolved from the exact loaded ELF symbols. + * Source game.c verifies map_loaded/active prefix and options.map_name offsets. + * Observes a successful native swap; it does not modify guest state. */ +#include "host_game_evidence.h" +#include "guest_address.h" +#include "guest_call.h" +#include +#include +#include +static uint32_t globals_symbol, scenario_symbol; +static uint64_t next_report, frames; +void mac_game_evidence_configure(uint32_t globals_va,uint32_t scenario_va) +{ globals_symbol=globals_va; scenario_symbol=scenario_va; next_report=frames=0; } +static uint32_t ui_count_va,ui_targets_va,ui_x_va,ui_y_va; +static int32_t previous_count=-1; +static int16_t previous_x=-32768,previous_y=-32768; +void mac_ui_evidence_configure(uint32_t count,uint32_t targets,uint32_t x,uint32_t y) +{ ui_count_va=count;ui_targets_va=targets;ui_x_va=x;ui_y_va=y; } +static void ui_evidence_presented(void) +{ + if(!ui_count_va) return; + int32_t count;int16_t x,y;unsigned char targets[96*16]; + if(mac_guest_read(ui_count_va,&count,4)||mac_guest_read(ui_x_va,&x,2)||mac_guest_read(ui_y_va,&y,2)|| + count<0||count>96||mac_guest_read(ui_targets_va,targets,(size_t)count*16)) return; + if(count==previous_count&&x==previous_x&&y==previous_y) return; + previous_count=count;previous_x=x;previous_y=y; + fprintf(stderr,"[ui-pointer] click=%d,%d targets=%d\n",x,y,count); + for(int32_t i=0;i126) name[i]='?'; + fprintf(stderr,"[game-state] frame=%llu ticks_ms=%llu map_loaded=%u active=%u loading=%u scenario=%08x map=%s\n", + (unsigned long long)frames,(unsigned long long)ticks_ms,prefix[0],prefix[1],prefix[3],scenario,name); +} diff --git a/port/macos/host_game_evidence.h b/port/macos/host_game_evidence.h new file mode 100644 index 0000000000..08a5e28ad1 --- /dev/null +++ b/port/macos/host_game_evidence.h @@ -0,0 +1,7 @@ +#ifndef HALO_HOST_GAME_EVIDENCE_H +#define HALO_HOST_GAME_EVIDENCE_H +#include +void mac_game_evidence_configure(uint32_t globals_va, uint32_t scenario_va); +void mac_ui_evidence_configure(uint32_t count, uint32_t targets, uint32_t click_x, uint32_t click_y); +void mac_game_evidence_presented(void); +#endif diff --git a/port/macos/host_import_bindings.c b/port/macos/host_import_bindings.c new file mode 100644 index 0000000000..1a7ec54b46 --- /dev/null +++ b/port/macos/host_import_bindings.c @@ -0,0 +1,47 @@ +#include "host_import_bindings.h" + +#include "guest_import_registry.h" +#include "host_import_names.h" +#include "host_imports.h" +#include "host_services.h" +#include "gl_host_imports.h" +#include "posix_host_imports.h" + +#include + +static size_t bound_count; +static size_t missing_count; + +static const mac_guest_import_provider providers[] = { + mac_macos_host_import_bridge, + mac_macos_gl_host_import_bridge, + mac_macos_posix_host_import_bridge, +}; + +int mac_macos_host_imports_register(void) +{ + if (mac_guest_host_imports_install_recomp_tls() != 0) + return -1; + bound_count = 0; + missing_count = 0; + for (size_t i = 0; i < MAC_GUEST_HOST_IMPORT_COUNT; ++i) + { + int found = 0; + for (size_t p = 0; p < sizeof(providers) / sizeof(providers[0]); ++p) + if (providers[p](mac_guest_host_import_names[i])) { found = 1; break; } + if (found) ++bound_count; + else + { + ++missing_count; + mac_host_logf(2, "host import has no Darwin provider: %s", mac_guest_host_import_names[i]); + } + } + mac_host_logf(missing_count ? 2 : 1, "bound %zu/%u generated guest imports (%zu fail-closed)", + bound_count, MAC_GUEST_HOST_IMPORT_COUNT, missing_count); + return mac_guest_import_register_manifest(mac_guest_host_import_names, + MAC_GUEST_HOST_IMPORT_COUNT, providers, + sizeof(providers) / sizeof(providers[0]), NULL); +} + +size_t mac_macos_host_imports_bound_count(void) { return bound_count; } +size_t mac_macos_host_imports_missing_count(void) { return missing_count; } diff --git a/port/macos/host_import_bindings.h b/port/macos/host_import_bindings.h new file mode 100644 index 0000000000..2bd6b573d1 --- /dev/null +++ b/port/macos/host_import_bindings.h @@ -0,0 +1,11 @@ +#ifndef HALO_MACOS_HOST_IMPORT_BINDINGS_H +#define HALO_MACOS_HOST_IMPORT_BINDINGS_H + +#include + +/* Install the generated final-ELF import manifest in exact sorted order. */ +int mac_macos_host_imports_register(void); +size_t mac_macos_host_imports_bound_count(void); +size_t mac_macos_host_imports_missing_count(void); + +#endif diff --git a/port/macos/host_imports.c b/port/macos/host_imports.c new file mode 100644 index 0000000000..c63c4afce4 --- /dev/null +++ b/port/macos/host_imports.c @@ -0,0 +1,492 @@ +#include "host_imports.h" + +#include "guest_address.h" +#include "guest_call.h" +#include "guest_thread.h" +#include "host_sdl.h" +#include "host_memory_fingerprint.h" +#include "host_bink.h" +#include "host_sdl_audio.h" +#include "host_services.h" +#include "host_syscall.h" + +#include +#include +#include +#include + +#define GUEST_STRING_LIMIT 4096u + +static struct mac_guest_import_registers registers; +static pthread_mutex_t registers_mutex = PTHREAD_MUTEX_INITIALIZER; +static int registers_installed; +static _Atomic int audio_disabled_reported; + +int mac_guest_host_import_registers_install(const struct mac_guest_import_registers *value) +{ + int result = -1; + if (!value || !value->get_esp || !value->set_esp || !value->set_eax || !value->set_edx) + return -1; + pthread_mutex_lock(®isters_mutex); + if (!registers_installed) + { + registers = *value; + registers_installed = 1; + result = 0; + } + pthread_mutex_unlock(®isters_mutex); + return result; +} + +static struct mac_guest_import_registers active_registers(void) +{ + struct mac_guest_import_registers value; + pthread_mutex_lock(®isters_mutex); + value = registers; + pthread_mutex_unlock(®isters_mutex); + if (!registers_installed) + mac_host_abort("host import entered before translator register accessors were installed"); + return value; +} + +uint32_t mac_guest_host_import_esp(void) +{ + struct mac_guest_import_registers state = active_registers(); + return state.get_esp(state.context); +} + +uint32_t mac_guest_host_import_arg32(uint32_t index) +{ + struct mac_guest_import_registers state = active_registers(); + uint32_t result; + if (mac_guest_stack_arg32(state.get_esp(state.context), index, &result) != 0) + mac_host_abort("invalid guest stack argument in host import"); + return result; +} + +uint64_t mac_guest_host_import_arg64_words(uint32_t low_index) +{ + uint32_t low = mac_guest_host_import_arg32(low_index); + uint32_t high = mac_guest_host_import_arg32(low_index + 1); + return (uint64_t)low | ((uint64_t)high << 32); +} + +void mac_guest_host_import_return32(uint32_t value) +{ + struct mac_guest_import_registers state = active_registers(); + uint32_t entry_esp = state.get_esp(state.context), after; + if (mac_guest_stack_after_return(entry_esp, 0, &after) != 0) + mac_host_abort("guest stack overflow returning from host import"); + state.set_eax(state.context, value); + state.set_esp(state.context, after); +} + +void mac_guest_host_import_return64(int64_t value) +{ + struct mac_guest_import_registers state = active_registers(); + uint64_t bits = (uint64_t)value; + uint32_t entry_esp = state.get_esp(state.context), after; + if (mac_guest_stack_after_return(entry_esp, 0, &after) != 0) + mac_host_abort("guest stack overflow returning from host import"); + state.set_eax(state.context, (uint32_t)bits); + state.set_edx(state.context, (uint32_t)(bits >> 32)); + state.set_esp(state.context, after); +} + +void mac_guest_host_import_return_void(void) +{ + struct mac_guest_import_registers state = active_registers(); + uint32_t after; + if (mac_guest_stack_after_return(state.get_esp(state.context), 0, &after) != 0) + mac_host_abort("guest stack overflow returning from host import"); + state.set_esp(state.context, after); +} + +static int copy_guest_string(uint32_t address, char output[GUEST_STRING_LIMIT]) +{ + for (size_t index = 0; index < GUEST_STRING_LIMIT; ++index) + { + uint64_t guest_byte_address = (uint64_t)address + index; + const char *byte; + if (guest_byte_address > UINT32_MAX || !(byte = mac_guest_address_resolve( + (uint32_t)guest_byte_address, 1))) + return -1; + output[index] = *byte; + if (!*byte) + return 0; + } + return -1; +} + +static void bridge_host_log(void) +{ + char message[GUEST_STRING_LIMIT]; + uint32_t priority = mac_guest_host_import_arg32(0), text_va = mac_guest_host_import_arg32(1); + if (copy_guest_string(text_va, message) != 0) + mac_host_abort("host_log received an invalid guest string"); + /* Android priorities: verbose/debug/info/warn/error/fatal. */ + if (priority <= 3) priority = 0; + else if (priority == 4) priority = 1; + else if (priority == 5) priority = 2; + else priority = 3; + mac_host_log((int)priority, message); + mac_guest_host_import_return_void(); +} + +static void bridge_host_abort(void) +{ + char message[GUEST_STRING_LIMIT]; + if (copy_guest_string(mac_guest_host_import_arg32(0), message) != 0) + mac_host_abort("host_abort received an invalid guest string"); + mac_host_abort(message); +} + +static void bridge_host_exit(void) +{ + mac_host_exit((int32_t)mac_guest_host_import_arg32(0)); +} + +static void bridge_host_errno(void) +{ + mac_guest_host_import_return32((uint32_t)mac_host_errno()); +} + +static void bridge_host_get_tp(void) +{ + mac_guest_host_import_return32(mac_host_get_guest_tp()); +} + +static void bridge_host_set_tp(void) +{ + mac_host_set_guest_tp(mac_guest_host_import_arg32(0)); + mac_guest_host_import_return_void(); +} + +static void bridge_host_thread_create(void) +{ + int result = mac_host_thread_create(mac_guest_host_import_arg32(0), mac_guest_host_import_arg32(1)); + mac_guest_host_import_return32((uint32_t)result); +} + +static void bridge_host_syscall(void) +{ + int64_t result = mac_host_arm64_32_syscall_from_stack(mac_guest_host_import_esp()); + mac_guest_host_import_return64(result); +} + +static void bridge_sdl_init(void) +{ + mac_guest_host_import_return32((uint32_t)mac_host_sdl_init(mac_guest_host_import_arg32(0))); +} + +static void bridge_sdl_set_hint(void) +{ + char name[GUEST_STRING_LIMIT], value[GUEST_STRING_LIMIT]; + if (copy_guest_string(mac_guest_host_import_arg32(0), name) != 0 || + copy_guest_string(mac_guest_host_import_arg32(1), value) != 0) + mac_host_abort("host_sdl_set_hint received an invalid guest string"); + mac_guest_host_import_return32((uint32_t)mac_host_sdl_set_hint(mac_guest_host_import_arg32(0), mac_guest_host_import_arg32(1))); +} + +static void bridge_sdl_get_error(void) +{ + (void)mac_host_sdl_get_error(mac_guest_host_import_arg32(0), mac_guest_host_import_arg32(1)); + mac_guest_host_import_return_void(); +} + +static void bridge_sdl_ticks(void) +{ + mac_guest_host_import_return64(mac_host_sdl_ticks()); +} + +static void bridge_sdl_thread_id(void) +{ + mac_guest_host_import_return64(mac_host_sdl_thread_id()); +} + +static void bridge_sdl_create_window(void) +{ + uint32_t title_va = mac_guest_host_import_arg32(0); + uint64_t flags = mac_guest_host_import_arg64_words(3); + mac_guest_host_import_return32(mac_host_sdl_create_window(title_va, (int32_t)mac_guest_host_import_arg32(1), + (int32_t)mac_guest_host_import_arg32(2), (int64_t)flags)); +} + +static void bridge_sdl_window_size(void) +{ + mac_host_sdl_window_size_in_pixels(mac_guest_host_import_arg32(0), mac_guest_host_import_arg32(1), mac_guest_host_import_arg32(2)); + mac_guest_host_import_return_void(); +} + +static void bridge_platform_screen_mode(void) +{ + mac_guest_host_import_return32((uint32_t)mac_host_sdl_platform_screen_mode( + mac_guest_host_import_arg32(0), mac_guest_host_import_arg32(1))); +} + +static void bridge_sdl_window_logical_size(void) +{ + mac_guest_host_import_return32((uint32_t)mac_host_sdl_window_size( + mac_guest_host_import_arg32(0), mac_guest_host_import_arg32(1), mac_guest_host_import_arg32(2))); +} + +static void bridge_sdl_window_flags(void) +{ + mac_guest_host_import_return64(mac_host_sdl_window_flags(mac_guest_host_import_arg32(0))); +} + +static void bridge_sdl_window_set_size(void) +{ + mac_guest_host_import_return32((uint32_t)mac_host_sdl_set_window_size( + mac_guest_host_import_arg32(0), (int32_t)mac_guest_host_import_arg32(1), (int32_t)mac_guest_host_import_arg32(2))); +} + +static void bridge_sdl_window_fullscreen(void) +{ + mac_guest_host_import_return32((uint32_t)mac_host_sdl_set_window_fullscreen( + mac_guest_host_import_arg32(0), (int32_t)mac_guest_host_import_arg32(1))); +} + +static void bridge_sdl_warp_mouse(void) +{ + uint32_t x_bits = mac_guest_host_import_arg32(1), y_bits = mac_guest_host_import_arg32(2); + float x, y; + memcpy(&x, &x_bits, sizeof(x)); + memcpy(&y, &y_bits, sizeof(y)); + mac_guest_host_import_return32((uint32_t)mac_host_sdl_warp_mouse(mac_guest_host_import_arg32(0), x, y)); +} + +static void bridge_sdl_relative_mouse(void) +{ + mac_guest_host_import_return32((uint32_t)mac_host_sdl_set_relative_mouse(mac_guest_host_import_arg32(0), + (int32_t)mac_guest_host_import_arg32(1))); +} + +static void bridge_sdl_gl_attribute(void) +{ + mac_guest_host_import_return32((uint32_t)mac_host_sdl_gl_set_attribute((int32_t)mac_guest_host_import_arg32(0), + (int32_t)mac_guest_host_import_arg32(1))); +} + +static void bridge_sdl_gl_create_context(void) +{ + mac_guest_host_import_return32(mac_host_sdl_gl_create_context(mac_guest_host_import_arg32(0))); +} + +static void bridge_sdl_gl_make_current(void) +{ + mac_guest_host_import_return32((uint32_t)mac_host_sdl_gl_make_current(mac_guest_host_import_arg32(0), mac_guest_host_import_arg32(1))); +} + +static void bridge_sdl_gl_swap_interval(void) +{ + mac_guest_host_import_return32((uint32_t)mac_host_sdl_gl_set_swap_interval((int32_t)mac_guest_host_import_arg32(0))); +} + +static void bridge_sdl_gl_swap_window(void) +{ + mac_guest_host_import_return32((uint32_t)mac_host_sdl_gl_swap_window(mac_guest_host_import_arg32(0))); +} + +static void bridge_sdl_poll_event(void) +{ + mac_guest_host_import_return32((uint32_t)mac_host_sdl_poll_event(mac_guest_host_import_arg32(0))); +} + +static void bridge_sdl_set_clipboard(void) +{ + mac_guest_host_import_return32((uint32_t)mac_host_sdl_set_clipboard_text(mac_guest_host_import_arg32(0))); +} + +static void bridge_sdl_get_clipboard(void) +{ + (void)mac_host_sdl_get_clipboard_text(mac_guest_host_import_arg32(0), mac_guest_host_import_arg32(1)); + mac_guest_host_import_return_void(); +} + +static void bridge_sdl_toast(void) +{ + char message[GUEST_STRING_LIMIT]; + if (copy_guest_string(mac_guest_host_import_arg32(0), message) != 0) + mac_host_abort("host_sdl_show_toast received an invalid guest string"); + mac_guest_host_import_return32((uint32_t)mac_host_sdl_show_toast(mac_guest_host_import_arg32(0), + (int32_t)mac_guest_host_import_arg32(1), (int32_t)mac_guest_host_import_arg32(2), + (int32_t)mac_guest_host_import_arg32(3), (int32_t)mac_guest_host_import_arg32(4))); +} + +static void bridge_sdl_message_box(void) +{ + mac_guest_host_import_return32((uint32_t)mac_host_sdl_show_simple_message_box(mac_guest_host_import_arg32(0), + mac_guest_host_import_arg32(1), mac_guest_host_import_arg32(2))); +} + +static void bridge_sdl_get_gamepads(void) +{ + mac_guest_host_import_return32((uint32_t)mac_host_sdl_get_gamepads(mac_guest_host_import_arg32(0), + (int32_t)mac_guest_host_import_arg32(1))); +} + +static void bridge_sdl_open_gamepad(void) +{ + mac_guest_host_import_return32(mac_host_sdl_open_gamepad(mac_guest_host_import_arg32(0))); +} + +static void bridge_sdl_gamepad_from_id(void) +{ + mac_guest_host_import_return32(mac_host_sdl_gamepad_from_id(mac_guest_host_import_arg32(0))); +} + +static void bridge_sdl_gamepad_axis(void) +{ + mac_guest_host_import_return32((uint32_t)mac_host_sdl_gamepad_axis(mac_guest_host_import_arg32(0), (int32_t)mac_guest_host_import_arg32(1))); +} + +static void bridge_sdl_gamepad_button(void) +{ + mac_guest_host_import_return32((uint32_t)mac_host_sdl_gamepad_button(mac_guest_host_import_arg32(0), (int32_t)mac_guest_host_import_arg32(1))); +} + +static void bridge_sdl_gamepad_type(void) +{ + mac_guest_host_import_return32((uint32_t)mac_host_sdl_gamepad_type(mac_guest_host_import_arg32(0))); +} + +static void bridge_sdl_rumble(void) +{ + mac_guest_host_import_return32((uint32_t)mac_host_sdl_rumble_gamepad(mac_guest_host_import_arg32(0), + mac_guest_host_import_arg32(1), mac_guest_host_import_arg32(2), mac_guest_host_import_arg32(3))); +} + +static void report_audio_disabled(void) +{ + if (!atomic_exchange_explicit(&audio_disabled_reported, 1, memory_order_relaxed)) + mac_host_log(1, "SDL guest audio is opt-in; set HALO_AUDIO_ENABLE=1 to enable translated callbacks"); +} + +static void bridge_sdl_audio_open(void) +{ + uint32_t device = mac_guest_host_import_arg32(0); + uint32_t spec_va = mac_guest_host_import_arg32(1); + uint32_t callback_va = mac_guest_host_import_arg32(2); + uint32_t userdata_va = mac_guest_host_import_arg32(3); + if (!mac_host_sdl_audio_enabled()) report_audio_disabled(); + mac_guest_host_import_return32(mac_host_sdl_open_audio_stream(device, spec_va, + callback_va, userdata_va)); +} + +static void bridge_sdl_audio_put(void) +{ + uint32_t stream = mac_guest_host_import_arg32(0); + uint32_t data_va = mac_guest_host_import_arg32(1); + int length = (int32_t)mac_guest_host_import_arg32(2); + if (!mac_host_sdl_audio_enabled()) report_audio_disabled(); + mac_guest_host_import_return32((uint32_t)mac_host_sdl_put_audio_stream_data(stream, + data_va, length)); +} + +static void bridge_sdl_audio_resume(void) +{ + uint32_t stream = mac_guest_host_import_arg32(0); + if (!mac_host_sdl_audio_enabled()) report_audio_disabled(); + mac_guest_host_import_return32((uint32_t)mac_host_sdl_resume_audio_stream_device(stream)); +} + +#define IMPORT(name, fn) { name, fn } +static void bridge_memory_fingerprint_page(void) +{ + mac_guest_host_import_return32((uint32_t)mac_host_memory_fingerprint_page( + mac_guest_host_import_arg32(0), mac_guest_host_import_arg32(1))); +} +static void bridge_memory_fingerprint_range(void) +{ + mac_guest_host_import_return32((uint32_t)mac_host_memory_fingerprint_range( + mac_guest_host_import_arg32(0), mac_guest_host_import_arg32(1), + mac_guest_host_import_arg32(2), mac_guest_host_import_arg32(3), mac_guest_host_import_arg32(4))); +} +static void bridge_bink_open(void) +{ + mac_guest_host_import_return32(mac_host_bink_open(mac_guest_host_import_arg32(0), + mac_guest_host_import_arg32(1), (int32_t)mac_guest_host_import_arg32(2))); +} +static void bridge_bink_decode(void) { mac_guest_host_import_return32((uint32_t)mac_host_bink_decode(mac_guest_host_import_arg32(0))); } +static void bridge_bink_next(void) +{ + mac_guest_host_import_return32((uint32_t)mac_host_bink_next(mac_guest_host_import_arg32(0), mac_guest_host_import_arg32(1))); +} +static void bridge_bink_wait(void) { mac_guest_host_import_return32((uint32_t)mac_host_bink_wait(mac_guest_host_import_arg32(0))); } +static void bridge_bink_close(void) { mac_host_bink_close(mac_guest_host_import_arg32(0)); mac_guest_host_import_return_void(); } +static void bridge_bink_copy(void) +{ + mac_guest_host_import_return32((uint32_t)mac_host_bink_copy(mac_guest_host_import_arg32(0), + mac_guest_host_import_arg32(1), (int32_t)mac_guest_host_import_arg32(2), + mac_guest_host_import_arg32(3), mac_guest_host_import_arg32(4), + mac_guest_host_import_arg32(5), mac_guest_host_import_arg32(6))); +} + +static const struct +{ + const char *name; + mac_guest_import_bridge bridge; +} host_imports[] = +{ + IMPORT("host_abort", bridge_host_abort), + IMPORT("host_bink_open", bridge_bink_open), + IMPORT("host_bink_decode", bridge_bink_decode), + IMPORT("host_bink_next", bridge_bink_next), + IMPORT("host_bink_wait", bridge_bink_wait), + IMPORT("host_bink_close", bridge_bink_close), + IMPORT("host_bink_copy", bridge_bink_copy), + IMPORT("host_errno", bridge_host_errno), + IMPORT("host_exit", bridge_host_exit), + IMPORT("host_get_tp", bridge_host_get_tp), + IMPORT("host_log", bridge_host_log), + IMPORT("host_memory_fingerprint_page", bridge_memory_fingerprint_page), + IMPORT("host_memory_fingerprint_range", bridge_memory_fingerprint_range), + IMPORT("host_sdl_create_window", bridge_sdl_create_window), + IMPORT("host_sdl_gamepad_axis", bridge_sdl_gamepad_axis), + IMPORT("host_sdl_gamepad_button", bridge_sdl_gamepad_button), + IMPORT("host_sdl_gamepad_from_id", bridge_sdl_gamepad_from_id), + IMPORT("host_sdl_gamepad_type", bridge_sdl_gamepad_type), + IMPORT("host_sdl_get_clipboard_text", bridge_sdl_get_clipboard), + IMPORT("host_sdl_get_error", bridge_sdl_get_error), + IMPORT("host_sdl_get_gamepads", bridge_sdl_get_gamepads), + IMPORT("host_sdl_gl_create_context", bridge_sdl_gl_create_context), + IMPORT("host_sdl_gl_make_current", bridge_sdl_gl_make_current), + IMPORT("host_sdl_gl_set_attribute", bridge_sdl_gl_attribute), + IMPORT("host_sdl_gl_set_swap_interval", bridge_sdl_gl_swap_interval), + IMPORT("host_sdl_gl_swap_window", bridge_sdl_gl_swap_window), + IMPORT("host_sdl_init", bridge_sdl_init), + IMPORT("host_sdl_open_audio_stream", bridge_sdl_audio_open), + IMPORT("host_sdl_open_gamepad", bridge_sdl_open_gamepad), + IMPORT("host_sdl_poll_event", bridge_sdl_poll_event), + IMPORT("host_sdl_put_audio_stream_data", bridge_sdl_audio_put), + IMPORT("host_sdl_resume_audio_stream_device", bridge_sdl_audio_resume), + IMPORT("host_sdl_rumble_gamepad", bridge_sdl_rumble), + IMPORT("host_sdl_set_clipboard_text", bridge_sdl_set_clipboard), + IMPORT("host_sdl_set_hint", bridge_sdl_set_hint), + IMPORT("host_sdl_set_relative_mouse", bridge_sdl_relative_mouse), + IMPORT("host_sdl_show_simple_message_box", bridge_sdl_message_box), + IMPORT("host_sdl_show_toast", bridge_sdl_toast), + IMPORT("host_sdl_thread_id", bridge_sdl_thread_id), + IMPORT("host_sdl_ticks", bridge_sdl_ticks), + IMPORT("host_sdl_window_size_in_pixels", bridge_sdl_window_size), + IMPORT("host_sdl_window_size", bridge_sdl_window_logical_size), + IMPORT("host_sdl_window_flags", bridge_sdl_window_flags), + IMPORT("host_sdl_set_window_fullscreen", bridge_sdl_window_fullscreen), + IMPORT("host_sdl_set_window_size", bridge_sdl_window_set_size), + IMPORT("host_sdl_warp_mouse", bridge_sdl_warp_mouse), + IMPORT("platform_screen_mode", bridge_platform_screen_mode), + IMPORT("host_set_tp", bridge_host_set_tp), + IMPORT("host_syscall", bridge_host_syscall), + IMPORT("host_thread_create", bridge_host_thread_create) +}; + +mac_guest_import_bridge mac_macos_host_import_bridge(const char *name) +{ + if (!name) + return NULL; + for (size_t index = 0; index < sizeof(host_imports) / sizeof(host_imports[0]); ++index) + if (strcmp(name, host_imports[index].name) == 0) + return host_imports[index].bridge; + return NULL; +} diff --git a/port/macos/host_imports.h b/port/macos/host_imports.h new file mode 100644 index 0000000000..b4a7bbb5d9 --- /dev/null +++ b/port/macos/host_imports.h @@ -0,0 +1,32 @@ +#ifndef HALO_MACOS_HOST_IMPORTS_H +#define HALO_MACOS_HOST_IMPORTS_H + +#include "guest_import_registry.h" + +#include + +/* Accessors for the active translator TLS state. Calls are made on the guest + * thread, so these must read/write that thread's current guest register set. */ +struct mac_guest_import_registers +{ + void *context; + uint32_t (*get_esp)(void *context); + void (*set_esp)(void *context, uint32_t value); + void (*set_eax)(void *context, uint32_t value); + void (*set_edx)(void *context, uint32_t value); +}; + +int mac_guest_host_import_registers_install(const struct mac_guest_import_registers *registers); +/* Production adapter compiled with the generated recomp_types.h globals. */ +int mac_guest_host_imports_install_recomp_tls(void); +uint32_t mac_guest_host_import_esp(void); +uint32_t mac_guest_host_import_arg32(uint32_t index); +uint64_t mac_guest_host_import_arg64_words(uint32_t low_index); +void mac_guest_host_import_return32(uint32_t value); +void mac_guest_host_import_return64(int64_t value); +void mac_guest_host_import_return_void(void); +/* Returns a cdecl guest bridge for implemented host_* imports, or NULL. The + * sorted manifest owner binds these in deterministic token order. */ +mac_guest_import_bridge mac_macos_host_import_bridge(const char *name); + +#endif diff --git a/port/macos/host_imports_recomp.c b/port/macos/host_imports_recomp.c new file mode 100644 index 0000000000..ea01d8473a --- /dev/null +++ b/port/macos/host_imports_recomp.c @@ -0,0 +1,35 @@ +#include "host_imports.h" + +#include "recomp_types.h" + +static uint32_t active_esp(void *context) +{ + (void)context; + return g_esp; +} + +static void set_esp(void *context, uint32_t value) +{ + (void)context; + g_esp = value; +} + +static void set_eax(void *context, uint32_t value) +{ + (void)context; + g_eax = value; +} + +static void set_edx(void *context, uint32_t value) +{ + (void)context; + g_edx = value; +} + +int mac_guest_host_imports_install_recomp_tls(void) +{ + const struct mac_guest_import_registers access = { + NULL, active_esp, set_esp, set_eax, set_edx + }; + return mac_guest_host_import_registers_install(&access); +} diff --git a/port/macos/host_memory_fingerprint.c b/port/macos/host_memory_fingerprint.c new file mode 100644 index 0000000000..a5e9d45d2c --- /dev/null +++ b/port/macos/host_memory_fingerprint.c @@ -0,0 +1,142 @@ +#include "host_memory_fingerprint.h" +#include "guest_address.h" +#include "guest_call.h" +#include +#include +#include +#include +#include + +/* Four-way mapping bounds storage to 32 MiB. Collisions only evict a snapshot; + * every successful call still reads/validates all 4096 current guest bytes. */ +#define SNAPSHOT_SETS 2048 +#define SNAPSHOT_WAYS 4 +struct page_snapshot { unsigned char bytes[4096]; uint64_t hashes[2]; uint64_t touched; uint32_t address; int valid; }; +static struct page_snapshot snapshots[SNAPSHOT_SETS][SNAPSHOT_WAYS]; +static uint64_t snapshot_clock; +static pthread_mutex_t snapshot_lock = PTHREAD_MUTEX_INITIALIZER; +static struct halo_fingerprint_stats stats; +static int metrics_enabled = -1; +static uint64_t report_ns, report_frames; +static struct halo_fingerprint_stats reported; + +static uint64_t monotonic_ns(void) +{ + struct timespec value; + clock_gettime(CLOCK_MONOTONIC, &value); + return (uint64_t)value.tv_sec * UINT64_C(1000000000) + (uint64_t)value.tv_nsec; +} + +/* Caller owns snapshot_lock; range calls avoid repeating the ABI boundary, + * address lookup and lock acquisition for every page of a resource. */ +static const uint64_t *fingerprint_locked(uint32_t address_va, const unsigned char *bytes) +{ + if (metrics_enabled < 0) metrics_enabled = getenv("HALO_FINGERPRINT_METRICS") != NULL; + uint64_t started = metrics_enabled ? monotonic_ns() : 0; + struct page_snapshot *set = snapshots[(address_va >> 12) % SNAPSHOT_SETS]; + struct page_snapshot *entry = NULL; + for (unsigned way = 0; way < SNAPSHOT_WAYS; ++way) + if (set[way].valid && set[way].address == address_va) { entry = &set[way]; break; } + if (!entry) + { + entry = &set[0]; + for (unsigned way = 0; way < SNAPSHOT_WAYS; ++way) + { + if (!set[way].valid) { entry = &set[way]; break; } + if (set[way].touched < entry->touched) entry = &set[way]; + } + } + entry->touched = ++snapshot_clock; + ++stats.scans; + if (entry->valid && entry->address == address_va && !memcmp(bytes, entry->bytes, sizeof(entry->bytes))) + ++stats.identical; + else + { + /* Hash the same snapshot we retain. Copying AFTER hashing live bytes + * could associate an old hash with newer concurrent guest writes. */ + if (entry->valid && entry->address != address_va) ++stats.collisions; + memcpy(entry->bytes, bytes, sizeof(entry->bytes)); + entry->address = address_va; + entry->hashes[0] = UINT64_C(14695981039346656037); + entry->hashes[1] = UINT64_C(0x9e3779b97f4a7c15); + for (unsigned i = 0; i < 4096; ++i) + { + uint64_t value = entry->bytes[i]; + entry->hashes[0] = (entry->hashes[0] ^ value) * UINT64_C(1099511628211); + entry->hashes[1] ^= value + UINT64_C(0x9e3779b97f4a7c15) + (entry->hashes[1] << 6) + (entry->hashes[1] >> 2); + entry->hashes[1] *= UINT64_C(0xbf58476d1ce4e5b9); + } + entry->valid = 1; + ++stats.rehashed; + } + if (metrics_enabled) stats.scan_ns += monotonic_ns() - started; + return entry->hashes; +} + +int mac_host_memory_fingerprint_page(uint32_t address_va, uint32_t hashes_va) +{ + const unsigned char *bytes = mac_guest_address_resolve(address_va, 4096); + uint64_t hashes[2]; + if (!bytes || !mac_guest_address_resolve(hashes_va, sizeof(hashes))) return 0; + pthread_mutex_lock(&snapshot_lock); + memcpy(hashes, fingerprint_locked(address_va, bytes), sizeof(hashes)); + pthread_mutex_unlock(&snapshot_lock); + return mac_guest_write(hashes_va, hashes, sizeof(hashes)) == 0; +} + +int mac_host_memory_fingerprint_range(uint32_t address_va, uint32_t pages, + uint32_t entries_va, uint32_t counter_va, uint32_t newest_va) +{ + /* Guest page_fingerprint is 24 bytes: hashes at0/8, generation at16, + * valid at20. Validate all spans before changing any generation. */ + if (!pages || pages > 65536 || (counter_va & 3)) return 0; + const unsigned char *bytes = mac_guest_address_resolve(address_va, (size_t)pages * 4096); + unsigned char *entries = mac_guest_address_resolve(entries_va, (size_t)pages * 24); + uint32_t *counter = mac_guest_address_resolve(counter_va, sizeof(*counter)); + if (!bytes || !entries || !counter || !mac_guest_address_resolve(newest_va, 4)) return 0; + uint32_t newest = 0; + pthread_mutex_lock(&snapshot_lock); + for (uint32_t page = 0; page < pages; ++page) + { + unsigned char *entry = entries + (size_t)page * 24; + const uint64_t *hashes = fingerprint_locked(address_va + page * 4096, bytes + (size_t)page * 4096); + uint32_t generation; + if (!entry[20] || memcmp(entry, hashes, 16)) + { + memcpy(entry, hashes, 16); + entry[20] = 1; + generation = __atomic_add_fetch(counter, 1, __ATOMIC_SEQ_CST); + memcpy(entry + 16, &generation, sizeof(generation)); + } + else memcpy(&generation, entry + 16, sizeof(generation)); + if (generation > newest) newest = generation; + } + pthread_mutex_unlock(&snapshot_lock); + return mac_guest_write(newest_va, &newest, sizeof(newest)) == 0; +} + +void mac_host_memory_fingerprint_stats(struct halo_fingerprint_stats *output) +{ + pthread_mutex_lock(&snapshot_lock); *output = stats; pthread_mutex_unlock(&snapshot_lock); +} + +void mac_host_memory_fingerprint_presented(void) +{ + pthread_mutex_lock(&snapshot_lock); + if (metrics_enabled > 0) + { + uint64_t now = monotonic_ns(); + ++report_frames; + if (!report_ns) report_ns = now; + if (now - report_ns >= UINT64_C(1000000000)) + { + uint64_t scans = stats.scans - reported.scans; + fprintf(stderr,"[fingerprint] frames=%llu scans=%llu scans_per_frame=%.1f read_MiB=%.3f identical=%llu rehashed=%llu collisions=%llu scan_ms=%.3f\n", + (unsigned long long)report_frames,(unsigned long long)scans,(double)scans/report_frames, + (double)scans*4096/(1024*1024),(unsigned long long)(stats.identical-reported.identical), + (unsigned long long)(stats.rehashed-reported.rehashed),(unsigned long long)(stats.collisions-reported.collisions),(double)(stats.scan_ns-reported.scan_ns)/1e6); + reported = stats; report_frames = 0; report_ns = now; + } + } + pthread_mutex_unlock(&snapshot_lock); +} diff --git a/port/macos/host_memory_fingerprint.h b/port/macos/host_memory_fingerprint.h new file mode 100644 index 0000000000..965d52cc96 --- /dev/null +++ b/port/macos/host_memory_fingerprint.h @@ -0,0 +1,13 @@ +#ifndef HALO_HOST_MEMORY_FINGERPRINT_H +#define HALO_HOST_MEMORY_FINGERPRINT_H +#include +/* Read exactly one guest 4 KiB page and write both unchanged source-port + * fingerprints to one checked 16-byte guest span. Reuse is permitted only + * after comparing every current byte against a retained snapshot. */ +int mac_host_memory_fingerprint_page(uint32_t address_va, uint32_t hashes_va); +int mac_host_memory_fingerprint_range(uint32_t address_va, uint32_t pages, + uint32_t entries_va, uint32_t counter_va, uint32_t newest_va); +struct halo_fingerprint_stats { uint64_t scans, identical, rehashed, collisions, scan_ns; }; +void mac_host_memory_fingerprint_stats(struct halo_fingerprint_stats *output); +void mac_host_memory_fingerprint_presented(void); +#endif diff --git a/port/macos/host_sdl.c b/port/macos/host_sdl.c new file mode 100644 index 0000000000..61cf05b99b --- /dev/null +++ b/port/macos/host_sdl.c @@ -0,0 +1,874 @@ +#include "host_sdl.h" +#include "host_app_icon.h" +#include "host_frame_metrics.h" +#include "host_memory_fingerprint.h" +#include "host_bink.h" +#include "host_game_evidence.h" + +#include "guest_address.h" +#include "guest_call.h" +#include "host_services.h" + +#include +#include +#include +#include +#include +#include +#include + +#define MAC_SDL_HANDLE_COUNT 256 +#define MAC_SDL_STRING_LIMIT 4096 + +enum mac_sdl_handle_type +{ + _mac_sdl_handle_free, + _mac_sdl_handle_window, + _mac_sdl_handle_context, + _mac_sdl_handle_gamepad +}; + +struct mac_sdl_handle +{ + enum mac_sdl_handle_type type; + void *object; + uint32_t generation; /* gamepad tokens reject references from disconnected devices */ +}; + +static struct mac_sdl_handle handles[MAC_SDL_HANDLE_COUNT]; +static pthread_mutex_t handles_mutex = PTHREAD_MUTEX_INITIALIZER; + +static int guest_string(uint32_t address, char *output, size_t capacity) +{ + const char *input; + size_t available, scan, length; + + if (!address || !capacity || !(input = mac_guest_address_resolve(address, 1))) + return -1; + available = mac_guest_address_available(address); + scan = available < capacity ? available : capacity; + if (!scan) + return -1; + length = strnlen(input, scan); + if (length == scan) + return -1; + memcpy(output, input, length + 1); + return 0; +} + +static uint32_t handle_new(enum mac_sdl_handle_type type, void *object) +{ + uint32_t index; + + if (!object) + return 0; + pthread_mutex_lock(&handles_mutex); + for (index = 1; index < MAC_SDL_HANDLE_COUNT; ++index) + { + if (handles[index].type == type && handles[index].object == object) + { + uint32_t token = type == _mac_sdl_handle_gamepad ? index | (handles[index].generation << 8) : index; + pthread_mutex_unlock(&handles_mutex); + return token; + } + } + for (index = 1; index < MAC_SDL_HANDLE_COUNT; ++index) + { + if (handles[index].type == _mac_sdl_handle_free) + { + handles[index].type = type; + handles[index].object = object; + if (type == _mac_sdl_handle_gamepad && !handles[index].generation) + handles[index].generation = 1; + uint32_t token = type == _mac_sdl_handle_gamepad ? index | (handles[index].generation << 8) : index; + pthread_mutex_unlock(&handles_mutex); + return token; + } + } + pthread_mutex_unlock(&handles_mutex); + mac_host_log(3, "SDL guest handle table exhausted"); + return 0; +} + +static void *handle_get(uint32_t handle, enum mac_sdl_handle_type type) +{ + void *object = NULL; + uint32_t index = handle & 255; + if (!index || (type != _mac_sdl_handle_gamepad && handle >= MAC_SDL_HANDLE_COUNT)) + return NULL; + pthread_mutex_lock(&handles_mutex); + if (handles[index].type == type && (type != _mac_sdl_handle_gamepad || + (handle >> 8) == handles[index].generation)) + object = handles[index].object; + pthread_mutex_unlock(&handles_mutex); + return object; +} + +static void gamepad_removed(SDL_JoystickID id) +{ + /* Close our one owning reference, not every guest lookup of that device. + * Keep window/context tokens unchanged; only gamepads have generations. */ + pthread_mutex_lock(&handles_mutex); + for (unsigned i = 1; i < MAC_SDL_HANDLE_COUNT; ++i) + { + if (handles[i].type != _mac_sdl_handle_gamepad || + SDL_GetGamepadID(handles[i].object) != id) + continue; + SDL_CloseGamepad(handles[i].object); + handles[i].object = NULL; + handles[i].type = _mac_sdl_handle_free; + handles[i].generation = (handles[i].generation + 1) & 0x00ffffff; + if (!handles[i].generation) handles[i].generation = 1; + } + pthread_mutex_unlock(&handles_mutex); +} + +int mac_host_sdl_init(uint32_t flags) +{ + int initialized = SDL_Init((SDL_InitFlags)flags) ? 1 : 0; + if (initialized && (flags & SDL_INIT_VIDEO)) + mac_host_apply_app_icon(); + return initialized; +} + +int mac_host_sdl_set_hint(uint32_t name_va, uint32_t value_va) +{ + char name[MAC_SDL_STRING_LIMIT]; + char value[MAC_SDL_STRING_LIMIT]; + + if (guest_string(name_va, name, sizeof(name)) != 0 || + guest_string(value_va, value, sizeof(value)) != 0) + { + SDL_SetError("invalid guest string passed to SDL_SetHint"); + return 0; + } + return SDL_SetHint(name, value) ? 1 : 0; +} + +int mac_host_sdl_get_error(uint32_t buffer_va, uint32_t size) +{ + const char *error = SDL_GetError(); + char *buffer; + size_t length; + + if (!size) + return 1; + buffer = mac_guest_address_resolve(buffer_va, size); + if (!buffer) + return 0; + length = strnlen(error ? error : "", size - 1); + memcpy(buffer, error ? error : "", length); + buffer[length] = '\0'; + return 1; +} + +int64_t mac_host_sdl_ticks(void) +{ + Uint64 ticks = SDL_GetTicks(); + const char *trace = getenv("HALO_TRACE_HOST_TICKS"); + if (trace && trace[0] && strcmp(trace, "0") != 0) + { + static atomic_uint trace_count; + unsigned index = atomic_fetch_add_explicit(&trace_count, 1, memory_order_relaxed); + if (index < 12) + fprintf(stderr, "[host-sdl-ticks] call=%u ticks_ms=%llu ticks_ns=%llu thread=%llu\n", + index + 1, (unsigned long long)ticks, + (unsigned long long)SDL_GetTicksNS(), + (unsigned long long)SDL_GetCurrentThreadID()); + } + return (int64_t)ticks; +} + +int64_t mac_host_sdl_thread_id(void) +{ + return (int64_t)SDL_GetCurrentThreadID(); +} + +uint32_t mac_host_sdl_create_window(uint32_t title_va, int width, int height, int64_t flags) +{ + char title[MAC_SDL_STRING_LIMIT]; + SDL_Window *window; + + if (guest_string(title_va, title, sizeof(title)) != 0) + { + SDL_SetError("invalid guest window title"); + return 0; + } + if ((width == 1280 && height == 720) || (width == 1920 && height == 1080)) + { + /* HD presets describe render pixels, not Retina points. Keep a + 1080p request from becoming a 3840x2160 window drawable. */ + const SDL_DisplayMode *mode = SDL_GetDesktopDisplayMode(SDL_GetPrimaryDisplay()); + float density = mode && mode->pixel_density > 0 ? mode->pixel_density : 1.0f; + int pixel_width = width, pixel_height = height; + width = (int)(width / density + 0.5f); + height = (int)(height / density + 0.5f); + if (mode && (width > mode->w * 0.9f || height > mode->h * 0.9f)) + { + float fit = mode->w * 0.9f / width; + float fit_y = mode->h * 0.9f / height; + if (fit_y < fit) fit = fit_y; + width = (int)(width * fit); + height = (int)(height * fit); + } + mac_host_logf(1, "HD window render=%dx%d points=%dx%d density=%.3g", pixel_width, pixel_height, width, height, (double)density); + } + const char *test_role = getenv("HALO_SDL_TEST_ROLE"); + if (test_role && (!strcmp(test_role, "host") || !strcmp(test_role, "client"))) + { + const char *script = getenv("HALO_TEST_INPUT"); + const char *control = script && !strncmp(script,"move:",5) ? "movement-only automation" : + script && !strncmp(script,"bot:",4) ? "scripted bot control" : "observer; manual input"; + snprintf(title, sizeof(title), "Halo Multiplayer Test — %s — %s", test_role, control); + } + window = SDL_CreateWindow(title, width, height, (SDL_WindowFlags)flags); + /* Test-only positioning keeps two owned network engines visible independently. */ + const char *test_position = getenv("HALO_SDL_TEST_WINDOW_POSITION"); + if (window && test_position) + { + int x, y; char extra; + if (sscanf(test_position, "%d,%d%c", &x, &y, &extra) == 2 && + x >= -10000 && x <= 10000 && y >= -10000 && y <= 10000) + { + bool positioned = SDL_SetWindowPosition(window, x, y); + mac_host_logf(1, "test window position=%d,%d result=%d", x, y, positioned); + } + } + if (window && !(flags & SDL_WINDOW_HIDDEN)) + { + SDL_RaiseWindow(window); + mac_host_activate_app(); + } + return handle_new(_mac_sdl_handle_window, window); +} + +int mac_host_sdl_window_size_in_pixels(uint32_t window, uint32_t width_va, uint32_t height_va) +{ + SDL_Window *object = handle_get(window, _mac_sdl_handle_window); + int width = 0, height = 0; + + if (!object) + return 0; + if (!mac_guest_address_resolve(width_va, sizeof(uint32_t)) || + !mac_guest_address_resolve(height_va, sizeof(uint32_t))) + return 0; + SDL_GetWindowSizeInPixels(object, &width, &height); + return mac_guest_write(width_va, &width, sizeof(width)) == 0 && + mac_guest_write(height_va, &height, sizeof(height)) == 0; +} + +int mac_host_sdl_window_size(uint32_t window, uint32_t width_va, uint32_t height_va) +{ + SDL_Window *object = handle_get(window, _mac_sdl_handle_window); + int width = 0, height = 0; + if (!object || !mac_guest_address_resolve(width_va, sizeof(uint32_t)) || + !mac_guest_address_resolve(height_va, sizeof(uint32_t))) + return 0; + if (!SDL_GetWindowSize(object, &width, &height)) + return 0; + return mac_guest_write(width_va, &width, sizeof(width)) == 0 && + mac_guest_write(height_va, &height, sizeof(height)) == 0; +} + +int64_t mac_host_sdl_window_flags(uint32_t window) +{ + SDL_Window *object = handle_get(window, _mac_sdl_handle_window); + return object ? (int64_t)SDL_GetWindowFlags(object) : 0; +} + +int mac_host_sdl_set_window_size(uint32_t window, int width, int height) +{ + SDL_Window *object = handle_get(window, _mac_sdl_handle_window); + return object && width > 0 && height > 0 && SDL_SetWindowSize(object, width, height); +} + +int mac_host_sdl_set_window_fullscreen(uint32_t window, int enabled) +{ + SDL_Window *object = handle_get(window, _mac_sdl_handle_window); + return object && SDL_SetWindowFullscreen(object, enabled != 0); +} + +int mac_host_sdl_warp_mouse(uint32_t window, float x, float y) +{ + SDL_Window *object = handle_get(window, _mac_sdl_handle_window); + if (!object) + return 0; + SDL_WarpMouseInWindow(object, x, y); + return 1; +} + +int mac_host_sdl_platform_screen_mode(uint32_t width_va, uint32_t height_va) +{ + SDL_DisplayID display = 0; + const SDL_DisplayMode *mode; + int width = 0, height = 0, fullscreen = 0; + const int trace = getenv("HALO_TRACE_SCREEN_MODE") != NULL; + if (!mac_guest_address_resolve(width_va, sizeof(uint32_t)) || + !mac_guest_address_resolve(height_va, sizeof(uint32_t))) + { + if (trace) + mac_host_logf(1, "[screen-mode-host] invalid outputs width_va=0x%08x height_va=0x%08x", + width_va, height_va); + return 0; + } + pthread_mutex_lock(&handles_mutex); + for (size_t index = 1; index < MAC_SDL_HANDLE_COUNT; ++index) + if (handles[index].type == _mac_sdl_handle_window && handles[index].object) + { + SDL_Window *window = handles[index].object; + SDL_WindowFlags flags = SDL_GetWindowFlags(window); + if (flags & SDL_WINDOW_FULLSCREEN) + { + fullscreen = 1; + display = SDL_GetDisplayForWindow(window); + break; + } + } + pthread_mutex_unlock(&handles_mutex); + if (!fullscreen) + { + if (trace) + mac_host_logf(1, "[screen-mode-host] fullscreen=0 output VAs 0x%08x/0x%08x left unchanged", + width_va, height_va); + return 0; + } + if (!display) + display = SDL_GetPrimaryDisplay(); + mode = display ? SDL_GetDesktopDisplayMode(display) : NULL; + if (!mode) + { + if (trace) + mac_host_logf(1, "[screen-mode-host] fullscreen=1 display=%u mode unavailable", + (unsigned)display); + return 0; + } + width = (int)(mode->w * mode->pixel_density + 0.5f); + height = (int)(mode->h * mode->pixel_density + 0.5f); + int written = mac_guest_write(width_va, &width, sizeof(width)) == 0 && + mac_guest_write(height_va, &height, sizeof(height)) == 0; + if (trace) + mac_host_logf(1, "[screen-mode-host] fullscreen=1 display=%u mode=%dx%d density=%.6g -> %dx%d guest=0x%08x/0x%08x written=%d", + (unsigned)display, mode->w, mode->h, (double)mode->pixel_density, + width, height, width_va, height_va, written); + return written; +} + +static int input_trace_enabled(void) +{ + static int enabled = -1; + if (enabled < 0) enabled = getenv("HALO_INPUT_TRACE") != NULL; + return enabled; +} + +int mac_host_sdl_set_relative_mouse(uint32_t window, int enabled) +{ + SDL_Window *object = handle_get(window, _mac_sdl_handle_window); + if (object) SDL_SetWindowKeyboardGrab(object, false); + int result = object ? (SDL_SetWindowRelativeMouseMode(object, enabled != 0) ? 1 : 0) : 0; + if (object && input_trace_enabled()) + fprintf(stderr, "[input-capture] requested=%d relative=%d keyboard_grab=%d focus=%d result=%d\n", + enabled != 0, SDL_GetWindowRelativeMouseMode(object), SDL_GetWindowKeyboardGrab(object), + (SDL_GetWindowFlags(object) & SDL_WINDOW_INPUT_FOCUS) != 0, result); + return result; +} + +int mac_host_sdl_gl_set_attribute(int attribute, int value) +{ + return SDL_GL_SetAttribute((SDL_GLAttr)attribute, value) ? 1 : 0; +} + +uint32_t mac_host_sdl_gl_create_context(uint32_t window) +{ + SDL_Window *object = handle_get(window, _mac_sdl_handle_window); + SDL_GLContext context = object ? SDL_GL_CreateContext(object) : NULL; + return handle_new(_mac_sdl_handle_context, context); +} + +int mac_host_sdl_gl_make_current(uint32_t window, uint32_t context) +{ + return SDL_GL_MakeCurrent(handle_get(window, _mac_sdl_handle_window), + handle_get(context, _mac_sdl_handle_context)) ? 1 : 0; +} + +int mac_host_sdl_gl_set_swap_interval(int interval) +{ + return SDL_GL_SetSwapInterval(interval) ? 1 : 0; +} + +/* Opt-in evidence capture, before presentation. No guest framebuffer is changed. */ +static void capture_presented_frame(SDL_Window *window) +{ + const char *directory = getenv("HALO_CAPTURE_FRAME_DIR"); + const char *interval_text = getenv("HALO_CAPTURE_FRAME_INTERVAL_MS"); + const char *only_at_text = getenv("HALO_CAPTURE_FRAME_AT_MS"); + static unsigned frame; + static Uint64 last_capture; + static unsigned milestone_count; + if (!directory || !*directory) + return; + ++frame; + Uint64 now = SDL_GetTicks(); + Uint64 interval = interval_text ? strtoull(interval_text, NULL, 10) : 0; + if (only_at_text && *only_at_text) + { + const char *cursor = only_at_text; + for (unsigned index = 0; index < milestone_count; ++index) + { + cursor = strchr(cursor, ','); + if (!cursor) return; + ++cursor; + } + char *end; + Uint64 milestone = strtoull(cursor, &end, 10); + if (!milestone || (*end && *end != ',') || now < milestone) + return; + ++milestone_count; + } + else if (frame != 1 && frame != 30 && frame != 120 && + (!interval || now - last_capture < interval)) + return; + last_capture = now; + typedef void (APIENTRYP get_integer_fn)(GLenum, GLint *); + typedef void (APIENTRYP bind_framebuffer_fn)(GLenum, GLuint); + typedef void (APIENTRYP bind_buffer_fn)(GLenum, GLuint); + typedef void (APIENTRYP read_buffer_fn)(GLenum); + typedef void (APIENTRYP pixel_store_fn)(GLenum, GLint); + typedef void (APIENTRYP read_pixels_fn)(GLint, GLint, GLsizei, GLsizei, GLenum, GLenum, void *); + get_integer_fn get_integer = (get_integer_fn)SDL_GL_GetProcAddress("glGetIntegerv"); + bind_framebuffer_fn bind_framebuffer = (bind_framebuffer_fn)SDL_GL_GetProcAddress("glBindFramebuffer"); + bind_buffer_fn bind_buffer = (bind_buffer_fn)SDL_GL_GetProcAddress("glBindBuffer"); + read_buffer_fn read_buffer = (read_buffer_fn)SDL_GL_GetProcAddress("glReadBuffer"); + pixel_store_fn pixel_store = (pixel_store_fn)SDL_GL_GetProcAddress("glPixelStorei"); + read_pixels_fn read_pixels = (read_pixels_fn)SDL_GL_GetProcAddress("glReadPixels"); + int width = 0, height = 0; + GLint previous_fbo = 0, previous_buffer = 0, default_buffer = 0, previous_pack = 4; + GLint previous_pbo = 0, previous_row_length = 0, previous_skip_rows = 0, previous_skip_pixels = 0; + if (!get_integer || !bind_framebuffer || !bind_buffer || !read_buffer || !pixel_store || !read_pixels || + !SDL_GetWindowSizeInPixels(window, &width, &height) || width <= 0 || height <= 0 || + width > 16384 || height > 16384) + return; + size_t row_bytes = (size_t)width * 4; + unsigned char *pixels = malloc(row_bytes * (size_t)height); + unsigned char *row = malloc(row_bytes); + if (!pixels || !row) + { + free(pixels); free(row); + return; + } + get_integer(GL_READ_FRAMEBUFFER_BINDING, &previous_fbo); + get_integer(GL_READ_BUFFER, &previous_buffer); + get_integer(GL_PACK_ALIGNMENT, &previous_pack); + get_integer(GL_PIXEL_PACK_BUFFER_BINDING, &previous_pbo); + get_integer(GL_PACK_ROW_LENGTH, &previous_row_length); + get_integer(GL_PACK_SKIP_ROWS, &previous_skip_rows); + get_integer(GL_PACK_SKIP_PIXELS, &previous_skip_pixels); + bind_framebuffer(GL_READ_FRAMEBUFFER, 0); + get_integer(GL_READ_BUFFER, &default_buffer); + bind_buffer(GL_PIXEL_PACK_BUFFER, 0); + read_buffer(GL_BACK); + pixel_store(GL_PACK_ALIGNMENT, 1); + pixel_store(GL_PACK_ROW_LENGTH, 0); + pixel_store(GL_PACK_SKIP_ROWS, 0); + pixel_store(GL_PACK_SKIP_PIXELS, 0); + read_pixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels); + pixel_store(GL_PACK_ALIGNMENT, previous_pack); + pixel_store(GL_PACK_ROW_LENGTH, previous_row_length); + pixel_store(GL_PACK_SKIP_ROWS, previous_skip_rows); + pixel_store(GL_PACK_SKIP_PIXELS, previous_skip_pixels); + bind_buffer(GL_PIXEL_PACK_BUFFER, (GLuint)previous_pbo); + read_buffer((GLenum)default_buffer); + bind_framebuffer(GL_READ_FRAMEBUFFER, (GLuint)previous_fbo); + read_buffer((GLenum)previous_buffer); + size_t nonblack = 0; + for (size_t index = 0; index < (size_t)width * (size_t)height; ++index) + if (pixels[index * 4] || pixels[index * 4 + 1] || pixels[index * 4 + 2]) + ++nonblack; + for (int y = 0; y < height / 2; ++y) + { + unsigned char *top = pixels + (size_t)y * row_bytes; + unsigned char *bottom = pixels + (size_t)(height - 1 - y) * row_bytes; + memcpy(row, top, row_bytes); memcpy(top, bottom, row_bytes); memcpy(bottom, row, row_bytes); + } + char path[4096]; + int length = snprintf(path, sizeof(path), "%s/frame-%04u.bmp", directory, frame); + SDL_Surface *surface = SDL_CreateSurfaceFrom(width, height, SDL_PIXELFORMAT_RGBA32, pixels, (int)row_bytes); + int saved = surface && length > 0 && (size_t)length < sizeof(path) && SDL_SaveBMP(surface, path); + fprintf(stderr, "[frame-capture] frame=%u ticks_ms=%llu size=%dx%d nonblack=%zu saved=%d path=%s\n", + frame, (unsigned long long)now, width, height, nonblack, saved, path); + SDL_DestroySurface(surface); + free(pixels); free(row); +} + +int mac_host_sdl_gl_swap_window(uint32_t window) +{ + SDL_Window *object = handle_get(window, _mac_sdl_handle_window); + static struct halo_frame_metrics metrics; + static unsigned window_index; + const char *metric_setting = getenv("HALO_FRAME_METRICS"); + int measure = metric_setting && strcmp(metric_setting, "1") == 0; + if (!object) + return 0; + capture_presented_frame(object); + const bool trace_swap = getenv("HALO_SDL_SWAP_TRACE") != NULL; + static unsigned traced_swap; + uint64_t begin_ns = measure || trace_swap ? SDL_GetTicksNS() : 0; + if (!SDL_GL_SwapWindow(object)) return 0; + uint64_t swapped_ns = measure || trace_swap ? SDL_GetTicksNS() : 0; + if (trace_swap && (++traced_swap <= 500 || traced_swap % 300 == 0)) + { + SDL_WindowFlags flags = SDL_GetWindowFlags(object); + fprintf(stderr, "[swap-trace] frame=%u ticks_ms=%llu swap_ms=%.3f focus=%d occluded=%d flags=%llx\n", + traced_swap, (unsigned long long)SDL_GetTicks(), (double)(swapped_ns - begin_ns) / 1e6, + (flags & SDL_WINDOW_INPUT_FOCUS) != 0, (flags & SDL_WINDOW_OCCLUDED) != 0, (unsigned long long)flags); + } + mac_host_bink_presented(); + mac_game_evidence_presented(); + mac_host_memory_fingerprint_presented(); + if (measure) + { + uint64_t end_ns = SDL_GetTicksNS(); + if (halo_frame_metrics_add(&metrics, end_ns, swapped_ns - begin_ns)) + { + fprintf(stderr, "[frame-metrics] window=%u frames=%u elapsed_ms=%.3f fps=%.3f mean_ms=%.3f p50_ms=%.3f p95_ms=%.3f p99_ms=%.3f swap_mean_ms=%.3f capture=%d\n", + ++window_index, metrics.count, (double)metrics.elapsed_ns / 1e6, + (double)metrics.count * 1e9 / (double)metrics.elapsed_ns, + (double)metrics.elapsed_ns / (double)metrics.count / 1e6, + (double)halo_frame_metrics_percentile(&metrics, 50) / 1e6, + (double)halo_frame_metrics_percentile(&metrics, 95) / 1e6, + (double)halo_frame_metrics_percentile(&metrics, 99) / 1e6, + (double)metrics.swap_ns / (double)metrics.count / 1e6, + getenv("HALO_CAPTURE_FRAME_DIR") != NULL); + halo_frame_metrics_reset_window(&metrics); + } + } + return 1; +} + +static int event_has_guest_safe_layout(Uint32 type) +{ + switch (type) + { + case SDL_EVENT_QUIT: + case SDL_EVENT_TERMINATING: + case SDL_EVENT_WILL_ENTER_BACKGROUND: + case SDL_EVENT_DID_ENTER_BACKGROUND: + case SDL_EVENT_WILL_ENTER_FOREGROUND: + case SDL_EVENT_DID_ENTER_FOREGROUND: + case SDL_EVENT_WINDOW_SHOWN: + case SDL_EVENT_WINDOW_HIDDEN: + case SDL_EVENT_WINDOW_EXPOSED: + case SDL_EVENT_WINDOW_MOVED: + case SDL_EVENT_WINDOW_RESIZED: + case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED: + case SDL_EVENT_WINDOW_MINIMIZED: + case SDL_EVENT_WINDOW_MAXIMIZED: + case SDL_EVENT_WINDOW_RESTORED: + case SDL_EVENT_WINDOW_MOUSE_ENTER: + case SDL_EVENT_WINDOW_MOUSE_LEAVE: + case SDL_EVENT_WINDOW_FOCUS_GAINED: + case SDL_EVENT_WINDOW_FOCUS_LOST: + case SDL_EVENT_WINDOW_CLOSE_REQUESTED: + case SDL_EVENT_KEY_DOWN: + case SDL_EVENT_KEY_UP: + case SDL_EVENT_TEXT_INPUT: + case SDL_EVENT_MOUSE_MOTION: + case SDL_EVENT_MOUSE_BUTTON_DOWN: + case SDL_EVENT_MOUSE_BUTTON_UP: + case SDL_EVENT_MOUSE_WHEEL: + case SDL_EVENT_GAMEPAD_AXIS_MOTION: + case SDL_EVENT_GAMEPAD_BUTTON_DOWN: + case SDL_EVENT_GAMEPAD_BUTTON_UP: + case SDL_EVENT_GAMEPAD_ADDED: + case SDL_EVENT_GAMEPAD_REMOVED: + case SDL_EVENT_GAMEPAD_REMAPPED: + return 1; + default: + return 0; + } +} + +/* Bounded opt-in input smoke through the same SDL events as real input. */ +static void inject_test_input(void) +{ + static unsigned states[5], look_state, close_state, turn_state; + const char *names[5] = {"HALO_SDL_TEST_SKIP_AT_MS", "HALO_SDL_TEST_MOVE_AT_MS", + "HALO_SDL_TEST_USE_AT_MS", "HALO_SDL_TEST_FULLSCREEN_AT_MS", "HALO_SDL_TEST_QUIT_AT_MS"}; + const SDL_Scancode codes[5] = {SDL_SCANCODE_SPACE, SDL_SCANCODE_W, + SDL_SCANCODE_E, SDL_SCANCODE_F11, SDL_SCANCODE_Q}; + Uint64 now = SDL_GetTicks(); + /* Explicit bounded verification only; normal app startup strips this variable. */ + static unsigned cycle_sent; + const char *cycle_times = getenv("HALO_SDL_TEST_DISPLAY_CYCLE_AT_MS"); + if (cycle_times) for (unsigned pulse = 0; pulse < 8 && *cycle_times; ++pulse) + { + char *end; + Uint64 at = strtoull(cycle_times, &end, 10); + if (end == cycle_times || (*end && *end != ',') || !at || at > 600000) break; + if (now >= at && !(cycle_sent & (1u << pulse))) + { + SDL_Event key; SDL_zero(key); + key.type = SDL_EVENT_KEY_DOWN; key.key.down = true; + key.key.scancode = SDL_SCANCODE_F10; key.key.key = SDLK_F10; + key.key.timestamp = SDL_GetTicksNS(); + if (SDL_PushEvent(&key)) + { + key.key.repeat = true; SDL_PushEvent(&key); /* Must not double-cycle. */ + key.type = SDL_EVENT_KEY_UP; key.key.down = false; key.key.repeat = false; SDL_PushEvent(&key); + cycle_sent |= 1u << pulse; + fprintf(stderr, "[test-input] F10 pulse=%u ticks_ms=%llu\n", pulse, (unsigned long long)now); + } + } + cycle_times = *end ? end + 1 : end; + } + for (unsigned index = 0; index < 5; ++index) + { + const char *setting = getenv(names[index]); + if (!setting || !*setting || states[index] >= 2) + continue; + char *end; + Uint64 start = strtoull(setting, &end, 10); + if (*end || !start || start > 600000) + continue; + bool down = states[index] == 0; + if (now < start + (down ? 0 : 1000)) + continue; + SDL_Event event; + SDL_zero(event); + event.type = down ? SDL_EVENT_KEY_DOWN : SDL_EVENT_KEY_UP; + event.key.timestamp = SDL_GetTicksNS(); + event.key.scancode = codes[index]; + event.key.key = SDL_GetKeyFromScancode(codes[index], SDL_KMOD_NONE, false); + event.key.down = down; + if (index == 4) event.key.mod = SDL_KMOD_GUI; + if (SDL_PushEvent(&event)) + { + ++states[index]; + fprintf(stderr, "[test-input] scancode=%u down=%d ticks_ms=%llu\n", + (unsigned)codes[index], down, (unsigned long long)now); + } + } + const char *look = getenv("HALO_SDL_TEST_LOOK_AT_MS"); + if (look && *look && look_state < 4) + { + char *end; + Uint64 start = strtoull(look, &end, 10); + if (!*end && start && start <= 600000 && now >= start + look_state * 1000) + { + SDL_Event event; + SDL_zero(event); + event.type = SDL_EVENT_MOUSE_MOTION; + event.motion.timestamp = SDL_GetTicksNS(); + event.motion.x = 320; event.motion.y = 240; + event.motion.xrel = look_state == 0 ? 300 : look_state == 1 ? -300 : 0; + event.motion.yrel = look_state == 2 ? -150 : look_state == 3 ? 300 : 0; + if (SDL_PushEvent(&event)) + { + ++look_state; + fprintf(stderr, "[test-input] mouse_dx=%.0f mouse_dy=%.0f ticks_ms=%llu\n", + event.motion.xrel, event.motion.yrel, (unsigned long long)now); + } + } + } + const char *turn = getenv("HALO_SDL_TEST_TURN_AT_MS"); + if (turn && *turn && !turn_state) + { + char *end; + Uint64 start = strtoull(turn, &end, 10); + if (!*end && start && start <= 600000 && now >= start) + { + SDL_Event event; + SDL_zero(event); + event.type = SDL_EVENT_MOUSE_MOTION; + event.motion.timestamp = SDL_GetTicksNS(); + event.motion.x = 320; event.motion.y = 240; + event.motion.xrel = 600; + if (SDL_PushEvent(&event)) + { + turn_state = 1; + fprintf(stderr, "[test-input] turn_dx=600 ticks_ms=%llu\n", (unsigned long long)now); + } + } + } + const char *close_at = getenv("HALO_SDL_TEST_CLOSE_AT_MS"); + if (close_at && *close_at && !close_state) + { + char *end; + Uint64 start = strtoull(close_at, &end, 10); + if (!*end && start && start <= 600000 && now >= start) + { + SDL_Event event; + SDL_zero(event); + event.type = SDL_EVENT_WINDOW_CLOSE_REQUESTED; + pthread_mutex_lock(&handles_mutex); + for (unsigned index = 1; index < MAC_SDL_HANDLE_COUNT; ++index) + if (handles[index].type == _mac_sdl_handle_window) + { event.window.windowID = SDL_GetWindowID(handles[index].object); break; } + pthread_mutex_unlock(&handles_mutex); + if (event.window.windowID && SDL_PushEvent(&event)) + { close_state = 1; fprintf(stderr, "[test-input] window-close ticks_ms=%llu\n", (unsigned long long)now); } + } + } +} + +int mac_host_sdl_poll_event(uint32_t event_va) +{ + SDL_Event event; + + if (!event_va || !mac_guest_address_resolve(event_va, sizeof(event))) + return 0; + inject_test_input(); + while (SDL_PollEvent(&event)) + { + if (input_trace_enabled() && + (event.type == SDL_EVENT_WINDOW_FOCUS_GAINED || event.type == SDL_EVENT_WINDOW_FOCUS_LOST || + event.type == SDL_EVENT_KEY_DOWN || event.type == SDL_EVENT_KEY_UP || + event.type == SDL_EVENT_MOUSE_BUTTON_DOWN || event.type == SDL_EVENT_MOUSE_BUTTON_UP)) + fprintf(stderr, "[input-event] type=%u scan=%u mod=%x button=%u\n", (unsigned)event.type, + (event.type == SDL_EVENT_KEY_DOWN || event.type == SDL_EVENT_KEY_UP) ? event.key.scancode : 0, + (event.type == SDL_EVENT_KEY_DOWN || event.type == SDL_EVENT_KEY_UP) ? event.key.mod : 0, + (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN || event.type == SDL_EVENT_MOUSE_BUTTON_UP) ? event.button.button : 0); + if (input_trace_enabled() && (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN || event.type == SDL_EVENT_MOUSE_BUTTON_UP)) + fprintf(stderr,"[input-point] window=%u x=%.3f y=%.3f down=%d\n",event.button.windowID,event.button.x,event.button.y,event.button.down); + if (event.type == SDL_EVENT_GAMEPAD_REMOVED) + gamepad_removed(event.gdevice.which); + if (event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED) + { + /* The native app owns one SDL game window. Deliver its close button + * through the gameplay's existing normal quit path, without relying + * on SDL's optional last-window-quit policy. */ + SDL_Window *closing = SDL_GetWindowFromID(event.window.windowID); + pthread_mutex_lock(&handles_mutex); + for (unsigned index = 1; closing && index < MAC_SDL_HANDLE_COUNT; ++index) + if (handles[index].type == _mac_sdl_handle_window && handles[index].object == closing) + { event.type = SDL_EVENT_QUIT; break; } + pthread_mutex_unlock(&handles_mutex); + } + /* SDL drop/user events can contain host pointers; do not leak them into + * the 32-bit guest event buffer. Unsupported event types are discarded. */ + if (!event_has_guest_safe_layout(event.type)) + continue; + return mac_guest_write(event_va, &event, sizeof(event)) == 0; + } + return 0; +} + +int mac_host_sdl_set_clipboard_text(uint32_t text_va) +{ + char text[MAC_SDL_STRING_LIMIT]; + if (guest_string(text_va, text, sizeof(text)) != 0) + return 0; + return SDL_SetClipboardText(text) ? 1 : 0; +} + +int mac_host_sdl_get_clipboard_text(uint32_t buffer_va, uint32_t size) +{ + char *guest_buffer; + char *text; + size_t length; + + if (!size) + return 1; + guest_buffer = mac_guest_address_resolve(buffer_va, size); + if (!guest_buffer) + return 0; + text = SDL_GetClipboardText(); + length = strnlen(text ? text : "", size - 1); + if (length) + memcpy(guest_buffer, text, length); + guest_buffer[length] = '\0'; + SDL_free(text); + return 1; +} + +int mac_host_sdl_show_toast(uint32_t message_va, int duration, int gravity, int x, int y) +{ + (void)message_va; + (void)duration; + (void)gravity; + (void)x; + (void)y; + SDL_SetError("Android toast notifications are not available on macOS"); + return 0; +} + +int mac_host_sdl_show_simple_message_box(uint32_t flags, uint32_t title_va, uint32_t message_va) +{ + char title[MAC_SDL_STRING_LIMIT]; + char message[MAC_SDL_STRING_LIMIT]; + if (guest_string(title_va, title, sizeof(title)) != 0 || + guest_string(message_va, message, sizeof(message)) != 0) + return 0; + return SDL_ShowSimpleMessageBox((SDL_MessageBoxFlags)flags, title, message, NULL) ? 1 : 0; +} + +int mac_host_sdl_get_gamepads(uint32_t ids_va, int capacity) +{ + int count = 0, index; + SDL_JoystickID *ids; + uint32_t *guest_ids; + + if (capacity < 0) + return -1; + if (capacity && !mac_guest_address_resolve(ids_va, (size_t)capacity * sizeof(uint32_t))) + return -1; + ids = SDL_GetGamepads(&count); + if (!ids) + return 0; + if (count > capacity) + count = capacity; + guest_ids = malloc((size_t)count * sizeof(*guest_ids)); + if (count && !guest_ids) + { + SDL_free(ids); + return 0; + } + for (index = 0; index < count; ++index) + guest_ids[index] = (uint32_t)ids[index]; + SDL_free(ids); + if (count && mac_guest_write(ids_va, guest_ids, (size_t)count * sizeof(*guest_ids)) != 0) + count = 0; + free(guest_ids); + return count; +} + +uint32_t mac_host_sdl_open_gamepad(uint32_t id) +{ + SDL_Gamepad *object = SDL_GetGamepadFromID((SDL_JoystickID)id); + return handle_new(_mac_sdl_handle_gamepad, object ? object : SDL_OpenGamepad((SDL_JoystickID)id)); +} + +uint32_t mac_host_sdl_gamepad_from_id(uint32_t id) +{ + return handle_new(_mac_sdl_handle_gamepad, SDL_GetGamepadFromID((SDL_JoystickID)id)); +} + +static SDL_Gamepad *gamepad_locked(uint32_t token) +{ + uint32_t index = token & 255; + return index && handles[index].type == _mac_sdl_handle_gamepad && + handles[index].generation == (token >> 8) ? handles[index].object : NULL; +} +int mac_host_sdl_gamepad_axis(uint32_t gamepad, int axis) +{ + pthread_mutex_lock(&handles_mutex); SDL_Gamepad *object=gamepad_locked(gamepad); + int result=object ? SDL_GetGamepadAxis(object,(SDL_GamepadAxis)axis) : 0; + pthread_mutex_unlock(&handles_mutex); return result; +} +int mac_host_sdl_gamepad_button(uint32_t gamepad, int button) +{ + pthread_mutex_lock(&handles_mutex); SDL_Gamepad *object=gamepad_locked(gamepad); + int result=object ? SDL_GetGamepadButton(object,(SDL_GamepadButton)button) : 0; + pthread_mutex_unlock(&handles_mutex); return result; +} +int mac_host_sdl_gamepad_type(uint32_t gamepad) +{ + pthread_mutex_lock(&handles_mutex); SDL_Gamepad *object=gamepad_locked(gamepad); + int result=object ? SDL_GetGamepadType(object) : SDL_GAMEPAD_TYPE_UNKNOWN; + pthread_mutex_unlock(&handles_mutex); return result; +} +int mac_host_sdl_rumble_gamepad(uint32_t gamepad, uint32_t low, uint32_t high, uint32_t milliseconds) +{ + pthread_mutex_lock(&handles_mutex); SDL_Gamepad *object=gamepad_locked(gamepad); + int result=object ? SDL_RumbleGamepad(object,(Uint16)low,(Uint16)high,milliseconds) : 0; + pthread_mutex_unlock(&handles_mutex); return result; +} diff --git a/port/macos/host_sdl.h b/port/macos/host_sdl.h new file mode 100644 index 0000000000..b4f4ee2f8c --- /dev/null +++ b/port/macos/host_sdl.h @@ -0,0 +1,40 @@ +#ifndef HALO_MACOS_HOST_SDL_H +#define HALO_MACOS_HOST_SDL_H + +#include + +/* Darwin host implementations of the upstream Android host_sdl_* imports. + * All string/buffer/event pointers are 32-bit guest virtual addresses. */ +int mac_host_sdl_init(uint32_t flags); +int mac_host_sdl_set_hint(uint32_t name_va, uint32_t value_va); +int mac_host_sdl_get_error(uint32_t buffer_va, uint32_t size); +int64_t mac_host_sdl_ticks(void); +int64_t mac_host_sdl_thread_id(void); +uint32_t mac_host_sdl_create_window(uint32_t title_va, int width, int height, int64_t flags); +int mac_host_sdl_window_size_in_pixels(uint32_t window, uint32_t width_va, uint32_t height_va); +int mac_host_sdl_window_size(uint32_t window, uint32_t width_va, uint32_t height_va); +int64_t mac_host_sdl_window_flags(uint32_t window); +int mac_host_sdl_set_window_fullscreen(uint32_t window, int enabled); +int mac_host_sdl_set_window_size(uint32_t window, int width, int height); +int mac_host_sdl_warp_mouse(uint32_t window, float x, float y); +int mac_host_sdl_platform_screen_mode(uint32_t width_va, uint32_t height_va); +int mac_host_sdl_set_relative_mouse(uint32_t window, int enabled); +int mac_host_sdl_gl_set_attribute(int attribute, int value); +uint32_t mac_host_sdl_gl_create_context(uint32_t window); +int mac_host_sdl_gl_make_current(uint32_t window, uint32_t context); +int mac_host_sdl_gl_set_swap_interval(int interval); +int mac_host_sdl_gl_swap_window(uint32_t window); +int mac_host_sdl_poll_event(uint32_t event_va); +int mac_host_sdl_set_clipboard_text(uint32_t text_va); +int mac_host_sdl_get_clipboard_text(uint32_t buffer_va, uint32_t size); +int mac_host_sdl_show_toast(uint32_t message_va, int duration, int gravity, int x, int y); +int mac_host_sdl_show_simple_message_box(uint32_t flags, uint32_t title_va, uint32_t message_va); +int mac_host_sdl_get_gamepads(uint32_t ids_va, int capacity); +uint32_t mac_host_sdl_open_gamepad(uint32_t id); +uint32_t mac_host_sdl_gamepad_from_id(uint32_t id); +int mac_host_sdl_gamepad_axis(uint32_t gamepad, int axis); +int mac_host_sdl_gamepad_button(uint32_t gamepad, int button); +int mac_host_sdl_gamepad_type(uint32_t gamepad); +int mac_host_sdl_rumble_gamepad(uint32_t gamepad, uint32_t low, uint32_t high, uint32_t milliseconds); + +#endif diff --git a/port/macos/host_sdl_audio.c b/port/macos/host_sdl_audio.c new file mode 100644 index 0000000000..ce6b3782b4 --- /dev/null +++ b/port/macos/host_sdl_audio.c @@ -0,0 +1,509 @@ +#include "host_sdl_audio.h" + +#include "guest_address.h" +#include "guest_callback.h" +#include "guest_call.h" +#include "host_services.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define AUDIO_HANDLE_COUNT 256u +#define AUDIO_GUEST_STACK_SIZE (256u * 1024u) +#define AUDIO_LEAD_MS 40u +#define AUDIO_CHUNK_MS 10u +#define AUDIO_BUFFER_LIMIT (16u * 1024u * 1024u) + +/* SDL_AudioSpec is 12 bytes in the 32-bit guest headers: 32-bit enum format, + * channels, and sample rate. Read fields explicitly; never pass guest layout + * memory as the arm64 host's SDL struct. */ +struct guest_sdl_audio_spec +{ + uint32_t format; + int32_t channels; + int32_t frequency; +}; +_Static_assert(sizeof(struct guest_sdl_audio_spec) == 12, "guest SDL_AudioSpec layout"); + +struct audio_binding +{ + uint32_t handle; + uint32_t callback_va; + uint32_t userdata_va; + SDL_AudioStream *stream; + pthread_t worker; + pthread_mutex_t mutex; + pthread_cond_t requested; + pthread_cond_t completed; + int sync_initialized; + int worker_started; + int worker_ready; + int worker_error; + int stopping; + int pending; + int producing; + int prefilled; + uint32_t lead_bytes; + uint32_t chunk_bytes; + _Atomic uint64_t underrun_requests; + uint64_t max_queued_bytes; + int additional; + int total; + unsigned char *buffer; + size_t buffer_length; + size_t buffer_capacity; + SDL_AudioFormat format; + int channels; + int frequency; + uint64_t callback_ns; + uint64_t max_callback_ns; + uint64_t request_ns; + uint64_t worker_wake_ns; + uint64_t max_worker_wake_ns; + uint64_t guest_invoke_ns; + uint64_t max_guest_invoke_ns; + uint64_t late_callbacks; + uint64_t short_callbacks; + uint64_t sample_count; + uint64_t nonfinite_samples; + uint64_t clipped_samples; + double sample_sum_squares; + double sample_peak; + uint64_t evidence_bytes; + uint64_t evidence_nonzero_bytes; + uint64_t evidence_callbacks; + uint64_t evidence_report_ms; +}; + +static struct audio_binding *audio_handles[AUDIO_HANDLE_COUNT]; +static pthread_mutex_t audio_handles_mutex = PTHREAD_MUTEX_INITIALIZER; +static pthread_once_t audio_shutdown_once = PTHREAD_ONCE_INIT; +static int audio_shutdown_registered; +static _Thread_local struct audio_binding *callback_binding; + +static void register_audio_shutdown(void) +{ + audio_shutdown_registered = atexit(mac_host_sdl_audio_shutdown) == 0; +} + +int mac_host_sdl_audio_enabled(void) +{ + const char *value = getenv("HALO_AUDIO_ENABLE"); + return value && strcmp(value, "1") == 0; +} + +static uint32_t handle_add(struct audio_binding *binding) +{ + uint32_t index; + pthread_mutex_lock(&audio_handles_mutex); + for (index = 1; index < AUDIO_HANDLE_COUNT; ++index) + if (!audio_handles[index]) + { + audio_handles[index] = binding; + binding->handle = index; + pthread_mutex_unlock(&audio_handles_mutex); + return index; + } + pthread_mutex_unlock(&audio_handles_mutex); + return 0; +} + +static struct audio_binding *handle_get(uint32_t handle) +{ + struct audio_binding *binding = NULL; + if (handle == 0 || handle >= AUDIO_HANDLE_COUNT) + return NULL; + pthread_mutex_lock(&audio_handles_mutex); + binding = audio_handles[handle]; + pthread_mutex_unlock(&audio_handles_mutex); + return binding; +} + +static void handle_remove(uint32_t handle) +{ + if (handle == 0 || handle >= AUDIO_HANDLE_COUNT) + return; + pthread_mutex_lock(&audio_handles_mutex); + audio_handles[handle] = NULL; + pthread_mutex_unlock(&audio_handles_mutex); +} + +static int guest_callback_put(struct audio_binding *binding, + uint32_t data_va, int length) +{ + const void *data; + size_t needed, capacity; + unsigned char *replacement; + + if (length < 0) + return 0; + if (length == 0) + return 1; + if (!(data = mac_guest_address_resolve(data_va, (size_t)length)) || + (size_t)length > AUDIO_BUFFER_LIMIT - binding->buffer_length) + return 0; + needed = binding->buffer_length + (size_t)length; + if (needed > binding->buffer_capacity) + { + capacity = binding->buffer_capacity ? binding->buffer_capacity : 4096; + while (capacity < needed && capacity < AUDIO_BUFFER_LIMIT / 2) + capacity *= 2; + if (capacity < needed) + capacity = needed; + replacement = SDL_realloc(binding->buffer, capacity); + if (!replacement) + return 0; + binding->buffer = replacement; + binding->buffer_capacity = capacity; + } + memcpy(binding->buffer + binding->buffer_length, data, (size_t)length); + binding->buffer_length = needed; + return 1; +} + +static int audio_submit(struct audio_binding *binding) +{ + pthread_mutex_lock(&binding->mutex); + uint64_t callback_ns = binding->callback_ns, max_callback_ns = binding->max_callback_ns; + uint64_t underrun_requests = atomic_load_explicit(&binding->underrun_requests, memory_order_relaxed); + pthread_mutex_unlock(&binding->mutex); + if (binding->buffer_length) + { + if (!SDL_PutAudioStreamData(binding->stream, binding->buffer, + (int)binding->buffer_length)) + return 0; + else if (getenv("HALO_AUDIO_EVIDENCE")) + { + uint64_t now = SDL_GetTicks(); + binding->evidence_bytes += binding->buffer_length; + ++binding->evidence_callbacks; + for (size_t i = 0; i < binding->buffer_length; ++i) + binding->evidence_nonzero_bytes += binding->buffer[i] != 0; + if (binding->format == SDL_AUDIO_F32LE) + for (size_t i = 0; i + sizeof(float) <= binding->buffer_length; i += sizeof(float)) + { + float sample; + memcpy(&sample, binding->buffer + i, sizeof(sample)); + if (!isfinite(sample)) { ++binding->nonfinite_samples; continue; } + double magnitude = fabs((double)sample); + ++binding->sample_count; + binding->clipped_samples += magnitude > 1.0; + binding->sample_sum_squares += (double)sample * sample; + if (magnitude > binding->sample_peak) binding->sample_peak = magnitude; + } + if (!binding->evidence_report_ms || now - binding->evidence_report_ms >= 1000) + { + mac_host_logf(1, "[audio-evidence] stream=%u format=0x%04x callbacks=%llu submitted_bytes=%llu nonzero_bytes=%llu worker_error=%d", + binding->handle, (unsigned)binding->format, + (unsigned long long)binding->evidence_callbacks, + (unsigned long long)binding->evidence_bytes, + (unsigned long long)binding->evidence_nonzero_bytes, binding->worker_error); + binding->evidence_report_ms = now; + mac_host_logf(1, "[audio-quality] stream=%u rate=%d channels=%d samples=%llu nonfinite=%llu above_fullscale=%llu peak=%.6g rms=%.6g callback_ms=%.3f max_ms=%.3f late_callbacks=%llu short_callbacks=%llu requested=%d submitted=%zu worker_wake_ms=%.3f max_worker_wake_ms=%.3f guest_invoke_ms=%.3f max_guest_invoke_ms=%.3f underrun_requests=%llu max_queued_bytes=%llu lead_bytes=%u", + binding->handle, binding->frequency, binding->channels, + (unsigned long long)binding->sample_count, (unsigned long long)binding->nonfinite_samples, + (unsigned long long)binding->clipped_samples, binding->sample_peak, + binding->sample_count ? sqrt(binding->sample_sum_squares / binding->sample_count) : 0.0, + callback_ns / 1e6, max_callback_ns / 1e6, + (unsigned long long)binding->late_callbacks, (unsigned long long)binding->short_callbacks, + binding->additional, binding->buffer_length, + binding->worker_wake_ns / 1e6, binding->max_worker_wake_ns / 1e6, + binding->guest_invoke_ns / 1e6, binding->max_guest_invoke_ns / 1e6, + (unsigned long long)underrun_requests, (unsigned long long)binding->max_queued_bytes, binding->lead_bytes); + } + } + binding->buffer_length = 0; + } + return 1; +} + +static void *audio_guest_worker(void *opaque) +{ + struct audio_binding *binding = opaque; + struct mac_guest_callback_context *guest_context = NULL; + int error = mac_guest_callback_context_create_current(AUDIO_GUEST_STACK_SIZE, + &guest_context); + pthread_mutex_lock(&binding->mutex); + binding->worker_error = error; + binding->worker_ready = 1; + pthread_cond_broadcast(&binding->completed); + pthread_mutex_unlock(&binding->mutex); + if (error != 0) return NULL; + for (;;) + { + pthread_mutex_lock(&binding->mutex); + while (!binding->producing && !binding->stopping) + pthread_cond_wait(&binding->requested, &binding->mutex); + if (binding->stopping) + { + pthread_mutex_unlock(&binding->mutex); + break; + } + binding->pending = 0; + pthread_mutex_unlock(&binding->mutex); + /* Query/submit outside the binding mutex: SDL takes its stream lock, + * while the device callback briefly takes this mutex in reverse. */ + int queued = SDL_GetAudioStreamQueued(binding->stream); + if (queued < 0) { error = EIO; break; } + if ((uint32_t)queued > binding->lead_bytes) { error = EOVERFLOW; break; } + if ((uint64_t)queued > binding->max_queued_bytes) binding->max_queued_bytes = queued; + if ((uint32_t)queued < binding->lead_bytes) + { + uint32_t additional = binding->lead_bytes - (uint32_t)queued; + if (additional > binding->chunk_bytes) additional = binding->chunk_bytes; + /* The DirectSound callback ignores total and consumes precisely the + * requested input frames. Produce those frames early in 10ms chunks, + * retaining ordered cursor advancement with at most 40ms lead. */ + uint32_t arguments[4] = {binding->userdata_va, binding->handle, + additional, binding->lead_bytes}; + uint64_t invoked_ns = SDL_GetTicksNS(); + pthread_mutex_lock(&binding->mutex); + binding->additional = additional; + binding->worker_wake_ns = binding->request_ns ? invoked_ns - binding->request_ns : 0; + if (binding->worker_wake_ns > binding->max_worker_wake_ns) + binding->max_worker_wake_ns = binding->worker_wake_ns; + binding->request_ns = 0; + pthread_mutex_unlock(&binding->mutex); + callback_binding = binding; + error = mac_guest_callback_invoke(guest_context, binding->callback_va, arguments, 4); + callback_binding = NULL; + binding->guest_invoke_ns = SDL_GetTicksNS() - invoked_ns; + if (binding->guest_invoke_ns > binding->max_guest_invoke_ns) + binding->max_guest_invoke_ns = binding->guest_invoke_ns; + if (error) break; + if (binding->buffer_length < additional) ++binding->short_callbacks; + if (binding->guest_invoke_ns * (uint64_t)binding->frequency * + SDL_AUDIO_BYTESIZE(binding->format) * binding->channels > (uint64_t)additional * 1000000000) + ++binding->late_callbacks; + /* A callback must not turn the small lead into an unbounded queue. */ + if (!binding->buffer_length) { error = EIO; break; } + if ((uint64_t)queued + binding->buffer_length > binding->lead_bytes) { error = EOVERFLOW; break; } + if (!audio_submit(binding)) { error = EIO; break; } + if (additional) continue; + } + pthread_mutex_lock(&binding->mutex); + binding->prefilled = 1; + pthread_cond_broadcast(&binding->completed); + struct timespec deadline; + clock_gettime(CLOCK_REALTIME, &deadline); + deadline.tv_nsec += 5000000; + if (deadline.tv_nsec >= 1000000000) { ++deadline.tv_sec; deadline.tv_nsec -= 1000000000; } + if (!binding->pending && !binding->stopping) + pthread_cond_timedwait(&binding->requested, &binding->mutex, &deadline); + pthread_mutex_unlock(&binding->mutex); + } + pthread_mutex_lock(&binding->mutex); + binding->worker_error = error; + binding->stopping = 1; + pthread_cond_broadcast(&binding->completed); + pthread_mutex_unlock(&binding->mutex); + mac_guest_callback_context_destroy_current(guest_context); + return NULL; +} + +static void SDLCALL audio_stream_callback(void *userdata, SDL_AudioStream *stream, + int additional, int total) +{ + struct audio_binding *binding = userdata; + uint64_t started_ns = SDL_GetTicksNS(); + (void)stream; + (void)total; + if (additional > 0) + atomic_fetch_add_explicit(&binding->underrun_requests, 1, memory_order_relaxed); + /* Never wait for guest code, the mixer lock, or the producer. SDL holds + * the stream lock on the shared physical output thread here. A skipped + * notification is harmless: the bounded producer also wakes every 5ms. */ + if (pthread_mutex_trylock(&binding->mutex) != 0) return; + if (!binding->stopping && !binding->worker_error) + { + if (!binding->request_ns) binding->request_ns = started_ns; + binding->pending = 1; + pthread_cond_signal(&binding->requested); + } + binding->callback_ns = SDL_GetTicksNS() - started_ns; + if (binding->callback_ns > binding->max_callback_ns) + binding->max_callback_ns = binding->callback_ns; + pthread_mutex_unlock(&binding->mutex); +} + +uint32_t mac_host_sdl_open_audio_stream(uint32_t device, uint32_t spec_va, + uint32_t callback_va, uint32_t userdata_va) +{ + struct guest_sdl_audio_spec guest_spec; + SDL_AudioSpec host_spec; + struct audio_binding *binding; + SDL_AudioStream *stream; + int error; + + if (!mac_host_sdl_audio_enabled()) + return 0; + if (pthread_once(&audio_shutdown_once, register_audio_shutdown) != 0 || + !audio_shutdown_registered) + { + SDL_SetError("could not register SDL audio shutdown cleanup"); + return 0; + } + if (mac_guest_read(spec_va, &guest_spec, sizeof(guest_spec)) != 0 || + (guest_spec.channels <= 0 || guest_spec.channels > 32) || guest_spec.frequency <= 0) + { + SDL_SetError("invalid guest SDL_AudioSpec"); + return 0; + } + host_spec.format = (SDL_AudioFormat)guest_spec.format; + host_spec.channels = guest_spec.channels; + host_spec.freq = guest_spec.frequency; + binding = SDL_calloc(1, sizeof(*binding)); + if (!binding) + return 0; + atomic_init(&binding->underrun_requests, 0); + binding->callback_va = callback_va; + binding->userdata_va = userdata_va; + binding->format = host_spec.format; + binding->channels = host_spec.channels; + binding->frequency = host_spec.freq; + uint64_t frame_bytes = SDL_AUDIO_BYTESIZE(host_spec.format) * (uint64_t)host_spec.channels; + uint64_t lead = ((uint64_t)host_spec.freq * AUDIO_LEAD_MS + 999) / 1000 * frame_bytes; + uint64_t chunk = ((uint64_t)host_spec.freq * AUDIO_CHUNK_MS + 999) / 1000 * frame_bytes; + if (!frame_bytes || !lead || lead > AUDIO_BUFFER_LIMIT || chunk > INT32_MAX) goto fail_alloc; + binding->lead_bytes = (uint32_t)lead; + binding->chunk_bytes = (uint32_t)chunk; + if (pthread_mutex_init(&binding->mutex, NULL) != 0) + goto fail_alloc; + if (pthread_cond_init(&binding->requested, NULL) != 0) + goto fail_mutex; + if (pthread_cond_init(&binding->completed, NULL) != 0) + goto fail_requested; + binding->sync_initialized = 1; + stream = SDL_OpenAudioDeviceStream((SDL_AudioDeviceID)device, &host_spec, + callback_va ? audio_stream_callback : NULL, binding); + if (!stream) + goto fail_sync; + binding->stream = stream; + if (!(binding->handle = handle_add(binding))) + goto fail_stream; + if (callback_va) + { + error = pthread_create(&binding->worker, NULL, audio_guest_worker, binding); + if (error != 0) + { + SDL_SetError("could not start translated audio callback worker (%d)", error); + goto fail_handle; + } + binding->worker_started = 1; + pthread_mutex_lock(&binding->mutex); + while (!binding->worker_ready) + pthread_cond_wait(&binding->completed, &binding->mutex); + error = binding->worker_error; + pthread_mutex_unlock(&binding->mutex); + if (error != 0) + { + SDL_SetError("could not attach translated audio callback thread (%d)", error); + goto fail_handle; + } + } + return binding->handle; + +fail_handle: + if (binding->worker_started) + { + pthread_mutex_lock(&binding->mutex); + binding->stopping = 1; + pthread_cond_broadcast(&binding->requested); + pthread_mutex_unlock(&binding->mutex); + (void)SDL_PauseAudioStreamDevice(binding->stream); + } + SDL_SetAudioStreamGetCallback(binding->stream, NULL, NULL); + if (binding->worker_started) + pthread_join(binding->worker, NULL); + SDL_DestroyAudioStream(binding->stream); + handle_remove(binding->handle); + pthread_cond_destroy(&binding->completed); + pthread_cond_destroy(&binding->requested); + pthread_mutex_destroy(&binding->mutex); + SDL_free(binding->buffer); + SDL_free(binding); + return 0; +fail_stream: + SDL_DestroyAudioStream(stream); +fail_sync: + pthread_cond_destroy(&binding->completed); +fail_requested: + pthread_cond_destroy(&binding->requested); +fail_mutex: + pthread_mutex_destroy(&binding->mutex); +fail_alloc: + SDL_free(binding); + return 0; +} + +int mac_host_sdl_put_audio_stream_data(uint32_t stream_handle, + uint32_t data_va, int length) +{ + struct audio_binding *binding = handle_get(stream_handle); + const void *data; + if (!binding || length < 0) + return 0; + if (callback_binding == binding) + return guest_callback_put(binding, data_va, length); + if (!length) + return 1; + if (!(data = mac_guest_address_resolve(data_va, (size_t)length))) + return 0; + return SDL_PutAudioStreamData(binding->stream, data, length) ? 1 : 0; +} + +int mac_host_sdl_resume_audio_stream_device(uint32_t stream_handle) +{ + struct audio_binding *binding = handle_get(stream_handle); + if (!binding) return 0; + if (binding->worker_started) + { + pthread_mutex_lock(&binding->mutex); + binding->producing = 1; + pthread_cond_signal(&binding->requested); + while (!binding->prefilled && !binding->worker_error && !binding->stopping) + pthread_cond_wait(&binding->completed, &binding->mutex); + int ready = binding->prefilled && !binding->worker_error; + pthread_mutex_unlock(&binding->mutex); + if (!ready) return 0; + } + return SDL_ResumeAudioStreamDevice(binding->stream) ? 1 : 0; +} + +void mac_host_sdl_audio_shutdown(void) +{ + for (uint32_t handle = 1; handle < AUDIO_HANDLE_COUNT; ++handle) + { + struct audio_binding *binding = handle_get(handle); + if (!binding) + continue; + if (binding->worker_started) + { + pthread_mutex_lock(&binding->mutex); + binding->stopping = 1; + pthread_cond_broadcast(&binding->requested); + pthread_mutex_unlock(&binding->mutex); + (void)SDL_PauseAudioStreamDevice(binding->stream); + } + SDL_SetAudioStreamGetCallback(binding->stream, NULL, NULL); + if (binding->worker_started) + pthread_join(binding->worker, NULL); + SDL_DestroyAudioStream(binding->stream); + handle_remove(handle); + if (binding->sync_initialized) + { + pthread_cond_destroy(&binding->completed); + pthread_cond_destroy(&binding->requested); + pthread_mutex_destroy(&binding->mutex); + } + SDL_free(binding->buffer); + SDL_free(binding); + } +} diff --git a/port/macos/host_sdl_audio.h b/port/macos/host_sdl_audio.h new file mode 100644 index 0000000000..20268cef32 --- /dev/null +++ b/port/macos/host_sdl_audio.h @@ -0,0 +1,16 @@ +#ifndef HALO_MACOS_HOST_SDL_AUDIO_H +#define HALO_MACOS_HOST_SDL_AUDIO_H + +#include + +/* Audio is opt-in until the translated callback path has been exercised on + * the target game/runtime: set HALO_AUDIO_ENABLE=1 to enable it. */ +int mac_host_sdl_audio_enabled(void); +uint32_t mac_host_sdl_open_audio_stream(uint32_t device, uint32_t spec_va, + uint32_t callback_va, uint32_t userdata_va); +int mac_host_sdl_put_audio_stream_data(uint32_t stream_handle, + uint32_t data_va, int length); +int mac_host_sdl_resume_audio_stream_device(uint32_t stream_handle); +void mac_host_sdl_audio_shutdown(void); + +#endif diff --git a/port/macos/host_services.c b/port/macos/host_services.c new file mode 100644 index 0000000000..cf854bb58a --- /dev/null +++ b/port/macos/host_services.c @@ -0,0 +1,494 @@ +#include "host_services.h" + +#include "guest_address.h" +#include "guest_call.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef PATH_MAX +#define PATH_MAX 4096 +#endif + +/* Linux i386 open(2) flags from the guest ABI. */ +#define GUEST_O_ACCMODE 00000003u +#define GUEST_O_CREAT 00000100u +#define GUEST_O_EXCL 00000200u +#define GUEST_O_NOCTTY 00000400u +#define GUEST_O_TRUNC 00001000u +#define GUEST_O_APPEND 00002000u +#define GUEST_O_NONBLOCK 00004000u +#define GUEST_O_ASYNC 00020000u +#define GUEST_O_DIRECT 00040000u +#define GUEST_O_LARGEFILE 00100000u +#define GUEST_O_DIRECTORY 00200000u +#define GUEST_O_NOFOLLOW 00400000u +#define GUEST_O_NOATIME 01000000u +#define GUEST_O_CLOEXEC 02000000u +#define GUEST_O_DSYNC 00010000u +#define GUEST_O_SYNC 04010000u +#define GUEST_AT_FDCWD (-100) + +enum guest_linux_errno +{ + _guest_eperm = 1, _guest_enoent = 2, _guest_esrch = 3, _guest_eintr = 4, + _guest_eio = 5, _guest_enxio = 6, _guest_e2big = 7, _guest_enoexec = 8, + _guest_ebadf = 9, _guest_echild = 10, _guest_eagain = 11, _guest_enomem = 12, + _guest_eacces = 13, _guest_efault = 14, _guest_ebusy = 16, _guest_eexist = 17, + _guest_exdev = 18, _guest_enodev = 19, _guest_enotdir = 20, _guest_eisdir = 21, + _guest_einval = 22, _guest_enfile = 23, _guest_emfile = 24, _guest_enotty = 25, + _guest_etxtbsy = 26, _guest_efbig = 27, _guest_enospc = 28, _guest_espipe = 29, + _guest_erofs = 30, _guest_emlink = 31, _guest_epipe = 32, _guest_edom = 33, + _guest_erange = 34, _guest_edeadlk = 35, _guest_enametoolong = 36, + _guest_enolck = 37, _guest_enosys = 38, _guest_enotempty = 39, _guest_eloop = 40, + _guest_eoverflow = 75, + _guest_enomsg = 42, _guest_eidrm = 43, _guest_enostr = 60, _guest_enodata = 61, + _guest_etime = 62, _guest_enosr = 63, _guest_enotsock = 88, _guest_edestaddrreq = 89, + _guest_emsgsize = 90, _guest_eprototype = 91, _guest_enoprotoopt = 92, + _guest_eprotonosupport = 93, _guest_esocktnosupport = 94, _guest_eopnotsupp = 95, + _guest_epfnosupport = 96, _guest_eafnosupport = 97, _guest_eaddrinuse = 98, + _guest_eaddrnotavail = 99, _guest_enetdown = 100, _guest_enetunreach = 101, + _guest_enetreset = 102, _guest_econnaborted = 103, _guest_econnreset = 104, + _guest_enobufs = 105, _guest_eisconn = 106, _guest_enotconn = 107, + _guest_eshutdown = 108, _guest_etoomanyrefs = 109, _guest_etimedout = 110, + _guest_econnrefused = 111, _guest_ehostdown = 112, _guest_ehostunreach = 113, + _guest_ealready = 114, _guest_einprogress = 115, _guest_estale = 116, + _guest_ecanceled = 125, _guest_eownerdead = 130, _guest_enotrecoverable = 131 +}; + +struct guest_timespec32 +{ + int32_t seconds; + int32_t nanoseconds; +}; + +/* Linux clock_gettime64's kernel record, independent of host long/time_t. */ +struct guest_kernel_timespec64 +{ + int64_t seconds; + int64_t nanoseconds; +}; +_Static_assert(sizeof(struct guest_kernel_timespec64) == 16, + "guest time64 clock record must contain two 64-bit fields"); + +struct guest_timeval32 +{ + int32_t seconds; + int32_t microseconds; +}; + +static pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER; +static _Thread_local uint32_t guest_tp; +static _Thread_local int config_trace_fd = -1; + +void mac_host_log(int priority, const char *message) +{ + static const char *const levels[] = { "debug", "info", "warn", "error", "fatal" }; + const char *level = priority < 0 || priority >= (int)(sizeof(levels) / sizeof(levels[0])) ? + "info" : levels[priority]; + + pthread_mutex_lock(&log_mutex); + fprintf(stderr, "halo-macos[%s]: %s\n", level, message ? message : "(null)"); + fflush(stderr); + pthread_mutex_unlock(&log_mutex); +} + +void mac_host_logf(int priority, const char *format, ...) +{ + char message[2048]; + va_list arguments; + + va_start(arguments, format); + vsnprintf(message, sizeof(message), format, arguments); + va_end(arguments); + mac_host_log(priority, message); +} + +void mac_host_abort(const char *message) +{ + mac_host_log(4, message ? message : "guest abort"); + abort(); +} + +void mac_host_exit(int status) +{ + mac_host_logf(1, "guest process exit (%d)", status); + exit(status); +} + +int mac_host_errno(void) +{ + return mac_host_linux_errno(errno); +} + +int mac_host_linux_errno(int darwin_errno) +{ + switch (darwin_errno) + { + case EPERM: return _guest_eperm; + case ENOENT: return _guest_enoent; + case ESRCH: return _guest_esrch; + case EINTR: return _guest_eintr; + case EIO: return _guest_eio; + case ENXIO: return _guest_enxio; + case E2BIG: return _guest_e2big; + case ENOEXEC: return _guest_enoexec; + case EBADF: return _guest_ebadf; + case ECHILD: return _guest_echild; + case EAGAIN: return _guest_eagain; + case ENOMEM: return _guest_enomem; + case EACCES: return _guest_eacces; + case EFAULT: return _guest_efault; + case EBUSY: return _guest_ebusy; + case EEXIST: return _guest_eexist; + case EXDEV: return _guest_exdev; + case ENODEV: return _guest_enodev; + case ENOTDIR: return _guest_enotdir; + case EISDIR: return _guest_eisdir; + case EINVAL: return _guest_einval; + case ENFILE: return _guest_enfile; + case EMFILE: return _guest_emfile; + case ENOTTY: return _guest_enotty; + case ETXTBSY: return _guest_etxtbsy; + case EFBIG: return _guest_efbig; + case ENOSPC: return _guest_enospc; + case ESPIPE: return _guest_espipe; + case EROFS: return _guest_erofs; + case EMLINK: return _guest_emlink; + case EPIPE: return _guest_epipe; + case EDOM: return _guest_edom; + case ERANGE: return _guest_erange; + case EDEADLK: return _guest_edeadlk; + case ENAMETOOLONG: return _guest_enametoolong; + case ENOLCK: return _guest_enolck; + case ENOSYS: return _guest_enosys; + case ENOTEMPTY: return _guest_enotempty; + case ELOOP: return _guest_eloop; +#ifdef ENOMSG + case ENOMSG: return _guest_enomsg; +#endif +#ifdef EIDRM + case EIDRM: return _guest_eidrm; +#endif +#ifdef ENOSTR + case ENOSTR: return _guest_enostr; +#endif +#ifdef ENODATA + case ENODATA: return _guest_enodata; +#endif +#ifdef ETIME + case ETIME: return _guest_etime; +#endif +#ifdef ENOSR + case ENOSR: return _guest_enosr; +#endif + case ENOTSOCK: return _guest_enotsock; + case EDESTADDRREQ: return _guest_edestaddrreq; + case EMSGSIZE: return _guest_emsgsize; + case EPROTOTYPE: return _guest_eprototype; + case ENOPROTOOPT: return _guest_enoprotoopt; + case EPROTONOSUPPORT: return _guest_eprotonosupport; +#ifdef ESOCKTNOSUPPORT + case ESOCKTNOSUPPORT: return _guest_esocktnosupport; +#endif + case EOPNOTSUPP: return _guest_eopnotsupp; +#ifdef EPFNOSUPPORT + case EPFNOSUPPORT: return _guest_epfnosupport; +#endif + case EAFNOSUPPORT: return _guest_eafnosupport; + case EADDRINUSE: return _guest_eaddrinuse; + case EADDRNOTAVAIL: return _guest_eaddrnotavail; + case ENETDOWN: return _guest_enetdown; + case ENETUNREACH: return _guest_enetunreach; + case ENETRESET: return _guest_enetreset; + case ECONNABORTED: return _guest_econnaborted; + case ECONNRESET: return _guest_econnreset; + case ENOBUFS: return _guest_enobufs; + case EISCONN: return _guest_eisconn; + case ENOTCONN: return _guest_enotconn; + case ESHUTDOWN: return _guest_eshutdown; +#ifdef ETOOMANYREFS + case ETOOMANYREFS: return _guest_etoomanyrefs; +#endif + case ETIMEDOUT: return _guest_etimedout; + case ECONNREFUSED: return _guest_econnrefused; +#ifdef EHOSTDOWN + case EHOSTDOWN: return _guest_ehostdown; +#endif + case EHOSTUNREACH: return _guest_ehostunreach; + case EALREADY: return _guest_ealready; + case EINPROGRESS: return _guest_einprogress; +#ifdef ESTALE + case ESTALE: return _guest_estale; +#endif +#ifdef ECANCELED + case ECANCELED: return _guest_ecanceled; +#endif +#ifdef EOWNERDEAD + case EOWNERDEAD: return _guest_eownerdead; +#endif +#ifdef ENOTRECOVERABLE + case ENOTRECOVERABLE: return _guest_enotrecoverable; +#endif + default: + mac_host_logf(3, "unmapped Darwin errno %d; reporting Linux EIO", darwin_errno); + return _guest_eio; + } +} + +uint64_t mac_host_thread_id(void) +{ + uint64_t thread_id = 0; + (void)pthread_threadid_np(NULL, &thread_id); + return thread_id; +} + +uint32_t mac_host_get_guest_tp(void) +{ + return guest_tp; +} + +void mac_host_set_guest_tp(uint32_t value) +{ + guest_tp = value; +} + +static int host_clock_id(int guest_clock_id, clockid_t *host_clock_id_out) +{ + switch (guest_clock_id) + { + case 0: *host_clock_id_out = CLOCK_REALTIME; return 0; + case 1: *host_clock_id_out = CLOCK_MONOTONIC; return 0; +#ifdef CLOCK_PROCESS_CPUTIME_ID + case 2: *host_clock_id_out = CLOCK_PROCESS_CPUTIME_ID; return 0; +#endif +#ifdef CLOCK_THREAD_CPUTIME_ID + case 3: *host_clock_id_out = CLOCK_THREAD_CPUTIME_ID; return 0; +#endif + default: return -_guest_einval; + } +} + +static int64_t copy_timespec_to_guest(uint32_t address, const struct timespec *value) +{ + struct guest_timespec32 guest_value; + + if (!address) + return 0; + if (value->tv_sec < INT32_MIN || value->tv_sec > INT32_MAX || + value->tv_nsec < INT32_MIN || value->tv_nsec > INT32_MAX) + return -_guest_eoverflow; + guest_value.seconds = (int32_t)value->tv_sec; + guest_value.nanoseconds = (int32_t)value->tv_nsec; + return mac_guest_write(address, &guest_value, sizeof(guest_value)) == 0 ? 0 : -_guest_efault; +} + +int64_t mac_host_clock_gettime(int guest_clock_id, uint32_t guest_timespec_va) +{ + clockid_t clock_id; + struct timespec value; + + if (host_clock_id(guest_clock_id, &clock_id) != 0) + return -_guest_einval; + if (clock_gettime(clock_id, &value) != 0) + return -mac_host_linux_errno(errno); + return copy_timespec_to_guest(guest_timespec_va, &value); +} + +int64_t mac_host_clock_getres(int guest_clock_id, uint32_t guest_timespec_va) +{ + clockid_t clock_id; + struct timespec value; + + if (host_clock_id(guest_clock_id, &clock_id) != 0) + return -_guest_einval; + if (clock_getres(clock_id, &value) != 0) + return -mac_host_linux_errno(errno); + return copy_timespec_to_guest(guest_timespec_va, &value); +} + +int64_t mac_host_clock_gettime64(int guest_clock_id, uint32_t guest_timespec_va) +{ + clockid_t clock_id; + struct timespec value; + struct guest_kernel_timespec64 guest_value; + + if (host_clock_id(guest_clock_id, &clock_id) != 0) + return -_guest_einval; + if (clock_gettime(clock_id, &value) != 0) + return -mac_host_linux_errno(errno); + guest_value.seconds = (int64_t)value.tv_sec; + guest_value.nanoseconds = (int64_t)value.tv_nsec; + return mac_guest_write(guest_timespec_va, &guest_value, sizeof(guest_value)) == 0 ? + 0 : -_guest_efault; +} + +int64_t mac_host_gettimeofday(uint32_t guest_timeval_va) +{ + struct timeval value; + struct guest_timeval32 guest_value; + + if (!guest_timeval_va) + return 0; + if (gettimeofday(&value, NULL) != 0) + return -mac_host_linux_errno(errno); + if (value.tv_sec < INT32_MIN || value.tv_sec > INT32_MAX) + return -_guest_eoverflow; + guest_value.seconds = (int32_t)value.tv_sec; + guest_value.microseconds = (int32_t)value.tv_usec; + return mac_guest_write(guest_timeval_va, &guest_value, sizeof(guest_value)) == 0 ? 0 : -_guest_efault; +} + +static int guest_open_flags(uint32_t guest_flags, int *host_flags_out) +{ + const uint32_t known_flags = GUEST_O_ACCMODE | GUEST_O_CREAT | GUEST_O_EXCL | + GUEST_O_NOCTTY | GUEST_O_TRUNC | GUEST_O_APPEND | GUEST_O_NONBLOCK | + GUEST_O_ASYNC | GUEST_O_DIRECT | GUEST_O_LARGEFILE | GUEST_O_DIRECTORY | + GUEST_O_NOFOLLOW | GUEST_O_NOATIME | GUEST_O_CLOEXEC | GUEST_O_DSYNC | + GUEST_O_SYNC; + int flags; + + if ((guest_flags & ~known_flags) || (guest_flags & GUEST_O_ACCMODE) == 3 || + (guest_flags & (GUEST_O_DIRECT | GUEST_O_NOATIME))) + return -_guest_einval; + switch (guest_flags & GUEST_O_ACCMODE) + { + case 0: flags = O_RDONLY; break; + case 1: flags = O_WRONLY; break; + default: flags = O_RDWR; break; + } +#define MAP_OPEN_FLAG(guest_flag, host_flag) do { if (guest_flags & (guest_flag)) flags |= (host_flag); } while (0) + MAP_OPEN_FLAG(GUEST_O_CREAT, O_CREAT); + MAP_OPEN_FLAG(GUEST_O_EXCL, O_EXCL); + MAP_OPEN_FLAG(GUEST_O_NOCTTY, O_NOCTTY); + MAP_OPEN_FLAG(GUEST_O_TRUNC, O_TRUNC); + MAP_OPEN_FLAG(GUEST_O_APPEND, O_APPEND); + MAP_OPEN_FLAG(GUEST_O_NONBLOCK, O_NONBLOCK); + MAP_OPEN_FLAG(GUEST_O_DIRECTORY, O_DIRECTORY); + MAP_OPEN_FLAG(GUEST_O_NOFOLLOW, O_NOFOLLOW); + MAP_OPEN_FLAG(GUEST_O_CLOEXEC, O_CLOEXEC); +#ifdef O_ASYNC + MAP_OPEN_FLAG(GUEST_O_ASYNC, O_ASYNC); +#else + if (guest_flags & GUEST_O_ASYNC) return -_guest_einval; +#endif +#ifdef O_DSYNC + if (guest_flags & GUEST_O_DSYNC) flags |= O_DSYNC; +#else + if (guest_flags & GUEST_O_DSYNC) return -_guest_einval; +#endif + if (guest_flags & GUEST_O_SYNC) flags |= O_SYNC; +#undef MAP_OPEN_FLAG + *host_flags_out = flags; + return 0; +} + +static int guest_path_copy(uint32_t path_va, char path[PATH_MAX]) +{ + const char *source; + size_t available, scan_size, length; + + if (!path_va || !(source = mac_guest_address_resolve(path_va, 1))) + return -_guest_efault; + available = mac_guest_address_available(path_va); + scan_size = available < PATH_MAX ? available : PATH_MAX; + if (!scan_size) + return -_guest_efault; + length = strnlen(source, scan_size); + if (length == scan_size) + return -_guest_enametoolong; + memcpy(path, source, length + 1); + return 0; +} + +int64_t mac_host_openat(int directory_fd, uint32_t guest_path_va, + uint32_t linux_flags, uint32_t mode) +{ + char path[PATH_MAX]; + int host_flags; + int result; + int error = guest_open_flags(linux_flags, &host_flags); + + if (error) + return error; + error = guest_path_copy(guest_path_va, path); + if (error) + return error; + result = openat(directory_fd == GUEST_AT_FDCWD ? AT_FDCWD : directory_fd, + path, host_flags, (mode_t)mode); + int64_t returned = result < 0 ? -mac_host_linux_errno(errno) : result; + if (getenv("HALO_TRACE_CONFIG_IO") && strstr(path, "config.toml")) + { + config_trace_fd = result; + mac_host_logf(1, "[config-io] open path=%s flags=0x%x result=%lld", + path, linux_flags, (long long)returned); + } + return returned; +} + +static int64_t guest_io_buffer(int fd, uint32_t buffer_va, uint32_t size, + int64_t offset, int positional, int writing) +{ + void *buffer = NULL; + ssize_t result; + + if (size) + { + buffer = mac_guest_address_resolve(buffer_va, size); + if (!buffer) + return -_guest_efault; + } + if (size > (uint32_t)SSIZE_MAX) + return -_guest_einval; + if (writing) + result = positional ? pwrite(fd, buffer, size, (off_t)offset) : write(fd, buffer, size); + else + result = positional ? pread(fd, buffer, size, (off_t)offset) : read(fd, buffer, size); + return result < 0 ? -mac_host_linux_errno(errno) : result; +} + +int64_t mac_host_read(int fd, uint32_t guest_buffer_va, uint32_t size) +{ + return guest_io_buffer(fd, guest_buffer_va, size, 0, 0, 0); +} + +int64_t mac_host_write(int fd, uint32_t guest_buffer_va, uint32_t size) +{ + return guest_io_buffer(fd, guest_buffer_va, size, 0, 0, 1); +} + +int64_t mac_host_pread(int fd, uint32_t guest_buffer_va, uint32_t size, int64_t offset) +{ + return guest_io_buffer(fd, guest_buffer_va, size, offset, 1, 0); +} + +int64_t mac_host_pwrite(int fd, uint32_t guest_buffer_va, uint32_t size, int64_t offset) +{ + return guest_io_buffer(fd, guest_buffer_va, size, offset, 1, 1); +} + +int64_t mac_host_lseek(int fd, int64_t offset, int whence) +{ + off_t result = lseek(fd, (off_t)offset, whence); + int64_t returned = result == (off_t)-1 ? -mac_host_linux_errno(errno) : (int64_t)result; + if (getenv("HALO_TRACE_CONFIG_IO") && fd == config_trace_fd) + mac_host_logf(1, "[config-io] seek fd=%d offset=%lld whence=%d result=%lld", + fd, (long long)offset, whence, (long long)returned); + return returned; +} + +int64_t mac_host_close(int fd) +{ + return close(fd) == 0 ? 0 : -mac_host_linux_errno(errno); +} diff --git a/port/macos/host_services.h b/port/macos/host_services.h new file mode 100644 index 0000000000..3fc026d4e8 --- /dev/null +++ b/port/macos/host_services.h @@ -0,0 +1,49 @@ +/* Native Darwin side of the source-built guest's non-graphics host boundary. + * Guest addresses are 32-bit Xbox VAs; buffer parameters below are always + * guest VAs, never truncated host pointers. */ + +#ifndef HALO_MACOS_HOST_SERVICES_H +#define HALO_MACOS_HOST_SERVICES_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +void mac_host_log(int priority, const char *message); +void mac_host_logf(int priority, const char *format, ...); +void mac_host_abort(const char *message) __attribute__((noreturn)); +void mac_host_exit(int status) __attribute__((noreturn)); +int mac_host_errno(void); +int mac_host_linux_errno(int darwin_errno); +uint64_t mac_host_thread_id(void); +uint32_t mac_host_get_guest_tp(void); +void mac_host_set_guest_tp(uint32_t value); + +/* Guest Linux clock IDs and 32-bit time layouts. Results use Linux syscall + * convention: 0 on success, otherwise a negative errno value. */ +int64_t mac_host_clock_gettime(int guest_clock_id, uint32_t guest_timespec_va); +/* Hybrid guest syscall 403 uses the Linux 16-byte kernel time64 record; + * legacy syscall 113 retains its eight-byte time32 layout. */ +int64_t mac_host_clock_gettime64(int guest_clock_id, uint32_t guest_timespec_va); +int64_t mac_host_clock_getres(int guest_clock_id, uint32_t guest_timespec_va); +int64_t mac_host_gettimeofday(uint32_t guest_timeval_va); + +/* File calls accept Linux open flag values and checked guest pointers. */ +int64_t mac_host_openat(int directory_fd, uint32_t guest_path_va, + uint32_t linux_flags, uint32_t mode); +int64_t mac_host_read(int fd, uint32_t guest_buffer_va, uint32_t size); +int64_t mac_host_write(int fd, uint32_t guest_buffer_va, uint32_t size); +int64_t mac_host_pread(int fd, uint32_t guest_buffer_va, uint32_t size, + int64_t offset); +int64_t mac_host_pwrite(int fd, uint32_t guest_buffer_va, uint32_t size, + int64_t offset); +int64_t mac_host_lseek(int fd, int64_t offset, int whence); +int64_t mac_host_close(int fd); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/port/macos/host_syscall.c b/port/macos/host_syscall.c new file mode 100644 index 0000000000..e4bbfaf8a2 --- /dev/null +++ b/port/macos/host_syscall.c @@ -0,0 +1,507 @@ +#include "host_syscall.h" + +#include "guest_address.h" +#include "guest_call.h" +#include "host_services.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* The ELF guest is i386/ILP32, but its syscall number macros are generated + * from Android's arm64_32 Linux table. Its seven cdecl long-long slots carry + * those numbers unchanged; pointers remain 32-bit. The shared Linux time64 + * extension uses a separate 16-byte record, preserving legacy time32 calls. */ +enum arm64_32_syscall_number +{ + _guest_exit = 93, _guest_exit_group = 94, _guest_futex = 98, + _guest_nanosleep = 101, _guest_clock_gettime = 113, _guest_clock_nanosleep = 115, + _guest_sched_yield = 124, _guest_gettimeofday = 169, + _guest_getpid = 172, _guest_getuid = 174, _guest_geteuid = 175, + _guest_getgid = 176, _guest_getegid = 177, _guest_gettid = 178, + _guest_munmap = 215, _guest_mmap = 222, _guest_mprotect = 226, + _guest_openat = 56, _guest_close = 57, _guest_lseek = 62, + _guest_read = 63, _guest_write = 64, _guest_readv = 65, _guest_writev = 66, + _guest_pread64 = 67, _guest_pwrite64 = 68, _guest_mkdirat = 34, + _guest_unlinkat = 35, _guest_chdir = 49, _guest_getrandom = 278, + /* Linux's time64 extension is shared by i386 and arm64_32. */ + _guest_clock_gettime64 = 403, _guest_clock_nanosleep_time64 = 407 +}; + +enum +{ + _guest_eperm = 1, _guest_enoent = 2, _guest_eintr = 4, _guest_eio = 5, + _guest_ebadf = 9, _guest_eagain = 11, _guest_enomem = 12, + _guest_efault = 14, _guest_eexist = 17, _guest_einval = 22, + _guest_enosys = 38, _guest_etimedout = 110 +}; + +enum +{ + _guest_prot_none = 0, _guest_prot_read = 1, _guest_prot_write = 2, + _guest_prot_exec = 4, + _guest_map_shared = 1, _guest_map_private = 2, _guest_map_fixed = 0x10, + _guest_map_anonymous = 0x20, + _guest_futex_wait = 0, _guest_futex_wake = 1, + _guest_futex_private = 128, _guest_futex_realtime = 256 +}; + +struct guest_timespec32 +{ + int32_t seconds; + int32_t nanoseconds; +}; + +/* The guest is i386 even though this syscall table uses arm64_32 numbers. + * Linux i386 struct iovec is two 32-bit words; never reinterpret this as the + * native arm64 `struct iovec`. */ +struct guest_iovec32 +{ + uint32_t base; + uint32_t length; +}; + +_Static_assert(sizeof(struct guest_iovec32) == 8, "i386 iovec layout"); + +struct futex_waiter +{ + struct futex_waiter *next; + uint32_t guest_address; + pthread_cond_t condition; + int woken; +}; + +#define FUTEX_BUCKET_COUNT 64 +struct futex_bucket +{ + pthread_mutex_t mutex; + struct futex_waiter *waiters; +}; + +static struct futex_bucket futex_buckets[FUTEX_BUCKET_COUNT]; +static pthread_once_t futex_once = PTHREAD_ONCE_INIT; +static pthread_mutex_t vm_ops_mutex = PTHREAD_MUTEX_INITIALIZER; +static struct mac_guest_vm_ops vm_ops; +static int vm_ops_installed; +static _Atomic uint32_t next_guest_tid = 1; +static _Thread_local uint32_t current_guest_tid; + +static void initialize_futex_buckets(void) +{ + for (size_t index = 0; index < FUTEX_BUCKET_COUNT; ++index) + pthread_mutex_init(&futex_buckets[index].mutex, NULL); +} + +int mac_guest_vm_ops_install(const struct mac_guest_vm_ops *operations) +{ + int result = -1; + + if (!operations || !operations->map || !operations->unmap || !operations->protect) + return -1; + pthread_mutex_lock(&vm_ops_mutex); + if (!vm_ops_installed) + { + vm_ops = *operations; + vm_ops_installed = 1; + result = 0; + } + pthread_mutex_unlock(&vm_ops_mutex); + return result; +} + +static struct mac_guest_vm_ops get_vm_ops(int *installed) +{ + struct mac_guest_vm_ops result; + pthread_mutex_lock(&vm_ops_mutex); + *installed = vm_ops_installed; + result = vm_ops; + pthread_mutex_unlock(&vm_ops_mutex); + return result; +} + +static uint32_t guest_tid(void) +{ + if (!current_guest_tid) + { + current_guest_tid = atomic_fetch_add_explicit(&next_guest_tid, 1, + memory_order_relaxed); + if (!current_guest_tid) + current_guest_tid = atomic_fetch_add_explicit(&next_guest_tid, 1, + memory_order_relaxed); + } + return current_guest_tid; +} + +static int64_t syscall_mmap(const uint64_t a[6]) +{ + struct mac_guest_vm_ops operations; + int installed; + uint32_t mapped = 0; + /* Although the syscall number table is arm64_32, this project's guest + * objects use ILP32 pointers and pass mmap's sixth argument as byte offset. */ + uint64_t offset = a[5]; + int64_t result; + + operations = get_vm_ops(&installed); + if (!installed) + return -_guest_enosys; + result = operations.map(operations.context, (uint32_t)a[0], (uint32_t)a[1], + (int)a[2], (uint32_t)a[3], (int32_t)a[4], offset, &mapped); + if (result != 0) + return result; + if (!mapped || !mac_guest_address_resolve(mapped, (uint32_t)a[1])) + return -_guest_efault; + return mapped; +} + +static int64_t syscall_munmap(uint32_t address, uint32_t size) +{ + struct mac_guest_vm_ops operations; + int installed; + operations = get_vm_ops(&installed); + return installed ? operations.unmap(operations.context, address, size) : -_guest_enosys; +} + +static int64_t syscall_mprotect(uint32_t address, uint32_t size, int protection) +{ + struct mac_guest_vm_ops operations; + int installed; + if (protection & ~(_guest_prot_read | _guest_prot_write | _guest_prot_exec)) + return -_guest_einval; + operations = get_vm_ops(&installed); + return installed ? operations.protect(operations.context, address, size, protection) : -_guest_enosys; +} + +static int64_t syscall_nanosleep(uint32_t request_va, uint32_t remaining_va) +{ + struct guest_timespec32 request; + struct timespec native_request, native_remaining; + struct guest_timespec32 guest_remaining; + if (mac_guest_read(request_va, &request, sizeof(request)) != 0) + return -_guest_efault; + if (request.seconds < 0 || request.nanoseconds < 0 || request.nanoseconds >= 1000000000) + return -_guest_einval; + native_request.tv_sec = request.seconds; + native_request.tv_nsec = request.nanoseconds; + if (nanosleep(&native_request, &native_remaining) == 0) + return 0; + if (errno == EINTR && remaining_va) + { + guest_remaining.seconds = (int32_t)native_remaining.tv_sec; + guest_remaining.nanoseconds = (int32_t)native_remaining.tv_nsec; + if (mac_guest_write(remaining_va, &guest_remaining, sizeof(guest_remaining)) != 0) + return -_guest_efault; + } + return -mac_host_linux_errno(errno); +} + +/* Darwin does not provide clock_nanosleep. Resolve an absolute wall/monotonic + * deadline against that same clock, then use nanosleep for the remaining span. + * Preserve EINTR and Linux's rule that absolute requests do not write rem. */ +static int64_t syscall_clock_nanosleep(int clock, int flags, uint32_t request_va, + uint32_t remaining_va, int time64) +{ + int64_t seconds, nanoseconds; + clockid_t native_clock; + if (clock == 0) native_clock = CLOCK_REALTIME; + else if (clock == 1) native_clock = CLOCK_MONOTONIC; + else return -_guest_einval; + if (flags != 0 && flags != 1) return -_guest_einval; + if (time64) + { + int64_t request[2]; + if (mac_guest_read(request_va, request, sizeof(request))) return -_guest_efault; + seconds = request[0]; nanoseconds = request[1]; + } + else + { + struct guest_timespec32 request; + if (mac_guest_read(request_va, &request, sizeof(request))) return -_guest_efault; + seconds = request.seconds; nanoseconds = request.nanoseconds; + } + if (seconds < 0 || nanoseconds < 0 || nanoseconds >= 1000000000) return -_guest_einval; + if (!flags && remaining_va && !mac_guest_address_resolve(remaining_va, time64 ? 16 : 8)) + return -_guest_efault; + struct timespec span = {(time_t)seconds, (long)nanoseconds}, remaining; + if (flags) + { + struct timespec now; + if (clock_gettime(native_clock, &now)) return -mac_host_linux_errno(errno); + if (seconds < now.tv_sec || (seconds == now.tv_sec && nanoseconds <= now.tv_nsec)) return 0; + span.tv_sec -= now.tv_sec; + span.tv_nsec -= now.tv_nsec; + if (span.tv_nsec < 0) { --span.tv_sec; span.tv_nsec += 1000000000; } + } + if (nanosleep(&span, &remaining) == 0) return 0; + int error = errno; + if (error == EINTR && !flags && remaining_va) + { + if (time64) + { + int64_t value[2] = {remaining.tv_sec, remaining.tv_nsec}; + if (mac_guest_write(remaining_va, value, sizeof(value))) return -_guest_efault; + } + else + { + struct guest_timespec32 value = {(int32_t)remaining.tv_sec, (int32_t)remaining.tv_nsec}; + if (mac_guest_write(remaining_va, &value, sizeof(value))) return -_guest_efault; + } + } + return -mac_host_linux_errno(error); +} + +static int64_t syscall_vector_io(int fd, uint32_t guest_iov_va, int32_t count, int writing) +{ + struct guest_iovec32 guest_iov[1024]; + struct iovec host_iov[1024]; + uint64_t total = 0; + ssize_t written; + + if (count < 0 || count > (int32_t)(sizeof(guest_iov) / sizeof(guest_iov[0]))) + return -mac_host_linux_errno(EINVAL); + if (count == 0) + { + int flags = fcntl(fd, F_GETFL); + if (flags < 0) + return -mac_host_linux_errno(errno); + if ((flags & O_ACCMODE) == (writing ? O_RDONLY : O_WRONLY)) + return -_guest_ebadf; + return 0; + } + if (mac_guest_read(guest_iov_va, guest_iov, + (size_t)count * sizeof(guest_iov[0])) != 0) + return -_guest_efault; + for (int32_t index = 0; index < count; ++index) + { + /* ssize_t is signed 32-bit in the guest, even on the 64-bit host. */ + if (guest_iov[index].length > (uint64_t)INT32_MAX - total) + return -mac_host_linux_errno(EINVAL); + total += guest_iov[index].length; + host_iov[index].iov_len = guest_iov[index].length; + host_iov[index].iov_base = guest_iov[index].length ? + mac_guest_address_resolve(guest_iov[index].base, guest_iov[index].length) : NULL; + if (guest_iov[index].length && !host_iov[index].iov_base) + return -_guest_efault; + } + written = writing ? writev(fd, host_iov, count) : readv(fd, host_iov, count); + return written < 0 ? -mac_host_linux_errno(errno) : (int64_t)written; +} + +static struct futex_bucket *futex_bucket_for(uint32_t address) +{ + return &futex_buckets[(address >> 2) % FUTEX_BUCKET_COUNT]; +} + +static int64_t syscall_futex_wait(uint32_t address, uint32_t expected, uint32_t timeout_va) +{ + struct futex_bucket *bucket; + struct futex_waiter waiter; + struct guest_timespec32 timeout; + struct timespec absolute_timeout; + uint32_t *word; + int has_timeout = timeout_va != 0; + int wait_error = 0; + + if ((address & 3u) || !(word = mac_guest_address_resolve(address, sizeof(*word)))) + return -_guest_efault; + if (has_timeout && mac_guest_read(timeout_va, &timeout, sizeof(timeout)) != 0) + return -_guest_efault; + if (has_timeout && (timeout.seconds < 0 || timeout.nanoseconds < 0 || + timeout.nanoseconds >= 1000000000)) + return -_guest_einval; + if (has_timeout) + { + if (clock_gettime(CLOCK_REALTIME, &absolute_timeout) != 0) + return -mac_host_linux_errno(errno); + absolute_timeout.tv_sec += timeout.seconds; + absolute_timeout.tv_nsec += timeout.nanoseconds; + if (absolute_timeout.tv_nsec >= 1000000000) + { + absolute_timeout.tv_sec++; + absolute_timeout.tv_nsec -= 1000000000; + } + } + pthread_once(&futex_once, initialize_futex_buckets); + bucket = futex_bucket_for(address); + waiter = (struct futex_waiter){ 0 }; + waiter.guest_address = address; + pthread_cond_init(&waiter.condition, NULL); + pthread_mutex_lock(&bucket->mutex); + if (__atomic_load_n(word, __ATOMIC_ACQUIRE) != expected) + { + pthread_mutex_unlock(&bucket->mutex); + pthread_cond_destroy(&waiter.condition); + return -_guest_eagain; + } + waiter.next = bucket->waiters; + bucket->waiters = &waiter; + while (!waiter.woken) + { + wait_error = has_timeout ? pthread_cond_timedwait(&waiter.condition, + &bucket->mutex, &absolute_timeout) : pthread_cond_wait(&waiter.condition, + &bucket->mutex); + if (wait_error == ETIMEDOUT) + break; + if (wait_error != 0) + break; + } + { + struct futex_waiter **link = &bucket->waiters; + while (*link && *link != &waiter) + link = &(*link)->next; + if (*link) + *link = waiter.next; + } + pthread_mutex_unlock(&bucket->mutex); + pthread_cond_destroy(&waiter.condition); + if (wait_error == ETIMEDOUT) + return -_guest_etimedout; + if (wait_error != 0) + return -mac_host_linux_errno(wait_error); + return 0; +} + +static int64_t syscall_futex_wake(uint32_t address, int32_t count) +{ + struct futex_bucket *bucket; + struct futex_waiter *waiter; + int32_t woken = 0; + if ((address & 3u) || !mac_guest_address_resolve(address, sizeof(uint32_t))) + return -_guest_efault; + if (count < 0) + return -_guest_einval; + pthread_once(&futex_once, initialize_futex_buckets); + bucket = futex_bucket_for(address); + pthread_mutex_lock(&bucket->mutex); + for (waiter = bucket->waiters; waiter && woken < count; waiter = waiter->next) + { + if (waiter->guest_address == address && !waiter->woken) + { + waiter->woken = 1; + pthread_cond_signal(&waiter->condition); + woken++; + } + } + pthread_mutex_unlock(&bucket->mutex); + return woken; +} + +static int64_t syscall_futex(uint32_t address, int operation, uint32_t value, + uint32_t timeout_va) +{ + int command = operation & ~(int)(_guest_futex_private | _guest_futex_realtime); + if ((operation & ~(_guest_futex_private | _guest_futex_realtime | 0x7f)) != 0) + return -_guest_einval; + switch (command) + { + case _guest_futex_wait: + if (operation & _guest_futex_realtime) + return -_guest_einval; + return syscall_futex_wait(address, value, timeout_va); + case _guest_futex_wake: + return syscall_futex_wake(address, (int32_t)value); + default: + return -_guest_enosys; + } +} + +int64_t mac_host_arm64_32_syscall(uint64_t number, const uint64_t a[6]) +{ + uint32_t guest_address, guest_size; + if (!a || number > UINT32_MAX) + return -_guest_enosys; + switch ((uint32_t)number) + { + case _guest_exit: + pthread_exit(NULL); + case _guest_exit_group: + mac_host_exit((int32_t)a[0]); + case _guest_read: + return mac_host_read((int32_t)a[0], (uint32_t)a[1], (uint32_t)a[2]); + case _guest_write: + return mac_host_write((int32_t)a[0], (uint32_t)a[1], (uint32_t)a[2]); + case _guest_writev: + return syscall_vector_io((int32_t)a[0], (uint32_t)a[1], (int32_t)a[2], 1); + case _guest_readv: + return syscall_vector_io((int32_t)a[0], (uint32_t)a[1], (int32_t)a[2], 0); + case _guest_pread64: + return mac_host_pread((int32_t)a[0], (uint32_t)a[1], (uint32_t)a[2], + (int64_t)a[3]); + case _guest_pwrite64: + return mac_host_pwrite((int32_t)a[0], (uint32_t)a[1], (uint32_t)a[2], + (int64_t)a[3]); + case _guest_openat: + return mac_host_openat((int32_t)a[0], (uint32_t)a[1], (uint32_t)a[2], (uint32_t)a[3]); + case _guest_close: + return mac_host_close((int32_t)a[0]); + case _guest_lseek: + return (int32_t)mac_host_lseek((int32_t)a[0], (int32_t)a[1], (int32_t)a[2]); + case _guest_getpid: + return (int64_t)getpid(); + case _guest_getuid: + return (int64_t)getuid(); + case _guest_getgid: + return (int64_t)getgid(); + case _guest_geteuid: + return (int64_t)geteuid(); + case _guest_getegid: + return (int64_t)getegid(); + case _guest_gettimeofday: + return mac_host_gettimeofday((uint32_t)a[0]); + case _guest_munmap: + return syscall_munmap((uint32_t)a[0], (uint32_t)a[1]); + case _guest_mprotect: + return syscall_mprotect((uint32_t)a[0], (uint32_t)a[1], (int)a[2]); + case _guest_sched_yield: + return sched_yield() == 0 ? 0 : -mac_host_linux_errno(errno); + case _guest_clock_nanosleep: + return syscall_clock_nanosleep((int32_t)a[0], (int32_t)a[1], (uint32_t)a[2], (uint32_t)a[3], 0); + case _guest_clock_nanosleep_time64: + return syscall_clock_nanosleep((int32_t)a[0], (int32_t)a[1], (uint32_t)a[2], (uint32_t)a[3], 1); + case _guest_nanosleep: + return syscall_nanosleep((uint32_t)a[0], (uint32_t)a[1]); + case _guest_gettid: + return guest_tid(); + case _guest_futex: + return syscall_futex((uint32_t)a[0], (int)a[1], (uint32_t)a[2], (uint32_t)a[3]); + case _guest_clock_gettime: + return mac_host_clock_gettime((int)a[0], (uint32_t)a[1]); + case _guest_clock_gettime64: + return mac_host_clock_gettime64((int)a[0], (uint32_t)a[1]); + case _guest_mmap: + return syscall_mmap(a); + case _guest_getrandom: + guest_address = (uint32_t)a[0]; + guest_size = (uint32_t)a[1]; + if (guest_size && !mac_guest_address_resolve(guest_address, guest_size)) + return -_guest_efault; + if ((uint32_t)a[2] & ~3u) + return -_guest_einval; + if (guest_size) + arc4random_buf(mac_guest_address_resolve(guest_address, guest_size), guest_size); + return guest_size; + default: + return -_guest_enosys; + } +} + +int64_t mac_host_arm64_32_syscall_from_stack(uint32_t guest_esp) +{ + uint64_t values[7]; + for (uint32_t index = 0; index < 7; ++index) + { + if (mac_guest_stack_arg64(guest_esp, index, &values[index]) != 0) + return -_guest_efault; + } + return mac_host_arm64_32_syscall(values[0], &values[1]); +} diff --git a/port/macos/host_syscall.h b/port/macos/host_syscall.h new file mode 100644 index 0000000000..6ceb730d5d --- /dev/null +++ b/port/macos/host_syscall.h @@ -0,0 +1,30 @@ +#ifndef HALO_MACOS_HOST_SYSCALL_H +#define HALO_MACOS_HOST_SYSCALL_H + +#include + +/* Optional VM operations are supplied by the executable's guest allocator. + * The callbacks must preserve one linear guest VA backing and account for + * Darwin's host page size. Unsupported file-backed mappings should fail. */ +struct mac_guest_vm_ops +{ + void *context; + int64_t (*map)(void *context, uint32_t address, uint32_t size, + int protection, uint32_t flags, int fd, uint64_t offset, + uint32_t *mapped_guest_va_out); + int64_t (*unmap)(void *context, uint32_t address, uint32_t size); + int64_t (*protect)(void *context, uint32_t address, uint32_t size, + int protection); +}; + +int mac_guest_vm_ops_install(const struct mac_guest_vm_ops *operations); +/* The source-built ILP32 guest forwards syscall numbers from the arm64_32 + * Linux table plus its shared Linux time64 extension (clock_gettime64 403). + * Pointers remain 32-bit; 403 writes two 64-bit fields, while legacy clock + * call 113 writes two 32-bit fields. Returns + * Linux convention: result or negative guest errno; unknown calls are ENOSYS. */ +int64_t mac_host_arm64_32_syscall(uint64_t number, const uint64_t arguments[6]); +/* Reads number+six 64-bit argument slots at the checked i386 guest stack. */ +int64_t mac_host_arm64_32_syscall_from_stack(uint32_t guest_esp); + +#endif diff --git a/port/macos/main.c b/port/macos/main.c new file mode 100644 index 0000000000..5605b1d141 --- /dev/null +++ b/port/macos/main.c @@ -0,0 +1,370 @@ +#include "guest_address.h" +#include "guest_allocator.h" +#include "guest_call.h" +#include "guest_callback.h" +#include "guest_heap.h" +#include "guest_errno.h" +#include "guest_image.h" +#include "guest_import_registry.h" +#include "guest_thread.h" +#include "host_import_bindings.h" +#include "host_services.h" +#include "host_game_evidence.h" +#include "gl_token_registry.h" +#include "recomp_state.h" + +#include "recomp_funcs.h" +#include "recomp_image_symbols.h" + +#include +#include +#include +#include +#include +#include +#include + +#define GUEST_PAGE_SIZE 4096u +#define MAIN_GUEST_STACK_SIZE (8u * 1024u * 1024u) +#define GUEST_RETURN_SENTINEL UINT32_C(0xffffffff) + +struct guest_boot +{ + uint32_t argc; + uint32_t argv; + uint32_t environment; + uint32_t page_size; +}; + +static void *read_file(const char *path, size_t *size_out) +{ + FILE *file = fopen(path, "rb"); + long size; + void *bytes; + if (!file) return NULL; + if (fseek(file, 0, SEEK_END) != 0 || (size = ftell(file)) <= 0 || + fseek(file, 0, SEEK_SET) != 0) + { + fclose(file); + return NULL; + } + bytes = malloc((size_t)size); + if (!bytes || fread(bytes, 1, (size_t)size, file) != (size_t)size) + { + free(bytes); + fclose(file); + return NULL; + } + fclose(file); + *size_out = (size_t)size; + return bytes; +} + +static recomp_func_t resolve_guest_function(uint32_t guest_va) +{ + recomp_func_t function = mac_guest_import_resolve(guest_va); + if (!function) function = recomp_lookup_manual(guest_va); + if (!function) function = recomp_lookup(guest_va); + return function; +} + +static void initialize_worker(void *context, uint32_t guest_thread_va, + uint32_t guest_esp) +{ + (void)context; + (void)guest_thread_va; + mac_recomp_state_initialize_thread(guest_esp, 0); +} + +static void dispatch_worker(void *context, uint32_t guest_function_va) +{ + recomp_func_t function; + (void)context; + function = resolve_guest_function(guest_function_va); + if (!function) + { + mac_host_logf(3, "no translated worker start at 0x%08x", guest_function_va); + mac_host_abort("unresolved guest worker entry"); + } + function(); +} + +static int verify_indirect_host_import(void) +{ + uint32_t token = mac_guest_import_token("host_get_tp"); + uint32_t gl_token = mac_macos_gl_token_for_name("glGetString"); + uint32_t stack_va, sentinel = GUEST_RETURN_SENTINEL; + recomp_func_t callback; + if (!token || !RECOMP_ICALL_IS_CODE(token) || !gl_token || + !RECOMP_ICALL_IS_CODE(gl_token) || !recomp_lookup_kernel(gl_token) || + mac_guest_allocate(16, 16, &stack_va) != 0 || + mac_guest_write(stack_va, &sentinel, sizeof(sentinel)) != 0) + return -1; + callback = recomp_lookup_manual(token); + if (!callback) + return -1; + g_esp = stack_va; + g_eax = UINT32_MAX; + callback(); + return g_eax == 0 && g_esp == stack_va + 4 ? 0 : -1; +} + +static int make_directory(const char *path) +{ + if (mkdir(path, 0700) == 0 || errno == EEXIST) + return 0; + return -1; +} + +static int write_guest_string(const char *value, uint32_t *guest_va_out) +{ + size_t length = strlen(value) + 1; + if (mac_guest_allocate(length, 1, guest_va_out) != 0) + return -1; + return mac_guest_write(*guest_va_out, value, length); +} + +static int make_guest_boot(const char *data_root, const char *save_root, + uint32_t *boot_va_out) +{ + struct guest_boot boot = { 1, 0, 0, GUEST_PAGE_SIZE }; + uint32_t argv_va, env_va, argv[2], env[128]; + uint32_t boot_va; + const char *home = save_root; + const char *env_names[] = { "HALO_DATA_ROOT", "HALO_SAVE_ROOT", "HOME", "XDG_DATA_HOME" }; + const char *env_values[] = { data_root, save_root, home, save_root }; + const char *trace_rt_alloc = getenv("HALO_TRACE_RT_ALLOC"); + const char *exit_after = getenv("HALO_EXIT_AFTER"); + size_t env_count = 0; + + if (write_guest_string("halo", &argv[0]) != 0 || + mac_guest_allocate(sizeof(argv), 4, &argv_va) != 0 || + mac_guest_allocate(sizeof(env), 4, &env_va) != 0 || + mac_guest_allocate(sizeof(boot), 4, &boot_va) != 0) + return -1; + argv[1] = 0; + for (size_t i = 0; i < 4; ++i) + { + char entry[4096]; + int count = snprintf(entry, sizeof(entry), "%s=%s", env_names[i], env_values[i]); + if (count < 0 || (size_t)count >= sizeof(entry) || + write_guest_string(entry, &env[i]) != 0) + return -1; + } + env_count = 4; + if (trace_rt_alloc && *trace_rt_alloc) + { + char entry[128]; + int count = snprintf(entry, sizeof(entry), "HALO_TRACE_RT_ALLOC=%s", trace_rt_alloc); + if (count < 0 || (size_t)count >= sizeof(entry) || + write_guest_string(entry, &env[env_count++]) != 0) + return -1; + } + if (exit_after && *exit_after) + { + char entry[128]; + int count = snprintf(entry, sizeof(entry), "HALO_EXIT_AFTER=%s", exit_after); + if (count < 0 || (size_t)count >= sizeof(entry) || + write_guest_string(entry, &env[env_count++]) != 0) + return -1; + } + /* Guest configuration owns the HALO_* options, including user-rebound + * keys. Forward them with a hard capacity bound rather than maintaining + * a second incomplete list of configuration names in the native host. */ + extern char **environ; + for (char **entry = environ; entry && *entry; ++entry) + { + if (strncmp(*entry, "HALO_", 5) != 0) + continue; + const char *owned_names[] = { "HALO_DATA_ROOT", "HALO_SAVE_ROOT", "HALO_TRACE_RT_ALLOC", "HALO_EXIT_AFTER" }; + int owned = 0; + for (size_t index = 0; index < sizeof(owned_names) / sizeof(owned_names[0]); ++index) + { + size_t length = strlen(owned_names[index]); + if (strncmp(*entry, owned_names[index], length) == 0 && (*entry)[length] == '=') owned = 1; + } + if (owned) continue; + if (env_count + 1 >= sizeof(env) / sizeof(env[0]) || strlen(*entry) >= 4096 || + write_guest_string(*entry, &env[env_count++]) != 0) + return -1; + } + env[env_count] = 0; + boot.argv = argv_va; + boot.environment = env_va; + if (mac_guest_write(argv_va, argv, sizeof(argv)) != 0 || + mac_guest_write(env_va, env, (env_count + 1) * sizeof(env[0])) != 0 || + mac_guest_write(boot_va, &boot, sizeof(boot)) != 0) + return -1; + *boot_va_out = boot_va; + return 0; +} + +static int launch_guest(const char *elf_path) +{ + struct mac_guest_image image; + struct mac_guest_thread_runtime thread_runtime = { + NULL, initialize_worker, dispatch_worker + }; + char error[256]; + void *elf_bytes; + size_t elf_size; + uint32_t guest_boot_va, guest_stack_va, thread_start_va, thread_attach_va, errno_location_va; + uint32_t guest_esp; + uint32_t stack_frame[2] = { GUEST_RETURN_SENTINEL, 0 }; + recomp_func_t entry; + const char *data_root = getenv("HALO_DATA_ROOT"); + const char *save_root = getenv("HALO_SAVE_ROOT"); + char default_data_root[] = "."; + char default_save_root[4096]; + + if (!data_root || !*data_root) data_root = default_data_root; + if (!save_root || !*save_root) + { + int n = snprintf(default_save_root, sizeof(default_save_root), + "%s/build/macos-aot/run-data/save", getenv("HALO_PROJECT_ROOT") ? + getenv("HALO_PROJECT_ROOT") : "."); + if (n < 0 || (size_t)n >= sizeof(default_save_root)) + return 2; + save_root = default_save_root; + } + if (make_directory("build/macos-aot/run-data") != 0 && errno != EEXIST) + { + fprintf(stderr, "cannot create isolated run-data directory: %s\n", strerror(errno)); + return 2; + } + if (make_directory(save_root) != 0 && errno != EEXIST) + { + fprintf(stderr, "cannot create isolated save root %s: %s\n", save_root, strerror(errno)); + return 2; + } + if (setenv("HALO_DATA_ROOT", data_root, 1) != 0 || + setenv("HALO_SAVE_ROOT", save_root, 1) != 0 || setenv("HOME", save_root, 1) != 0) + { + fprintf(stderr, "cannot configure guest data/save roots: %s\n", strerror(errno)); + return 2; + } + + elf_bytes = read_file(elf_path, &elf_size); + if (!elf_bytes) + { + fprintf(stderr, "cannot read AOT guest ELF: %s (%s)\n", elf_path, strerror(errno)); + return 2; + } + if (mac_guest_image_load(elf_bytes, elf_size, &image, error, sizeof(error)) != 0) + { + fprintf(stderr, "guest ELF load failed: %s\n", error); + free(elf_bytes); + return 2; + } + if (image.entry_va != HALO_RECOMP_ENTRY_VA || + mac_guest_image_find_symbol(elf_bytes, elf_size, "__guest_thread_start", + &thread_start_va) != 0 || thread_start_va != HALO_RECOMP_THREAD_START_VA || + mac_guest_image_find_symbol(elf_bytes, elf_size, "__guest_thread_attach", + &thread_attach_va) != 0 || thread_attach_va != HALO_RECOMP_THREAD_ATTACH_VA || + mac_guest_image_find_symbol(elf_bytes, elf_size, "__errno_location", + &errno_location_va) != 0 || errno_location_va != HALO_RECOMP_ERRNO_LOCATION_VA) + { + fprintf(stderr, "AOT ELF does not match generated lift symbols (%s)\n", + HALO_RECOMP_IMAGE_SHA256); + free(elf_bytes); + return 2; + } + if (getenv("HALO_MAP_EVIDENCE")) + { + uint32_t globals_va = 0, scenario_va = 0; + if (mac_guest_image_find_object(elf_bytes, elf_size, "game_globals", 4, &globals_va) != 0 || + mac_guest_image_find_object(elf_bytes, elf_size, "global_scenario_index", 4, &scenario_va) != 0) + { + fprintf(stderr, "map evidence symbols unavailable in loaded ELF\n"); + free(elf_bytes); + return 2; + } + mac_game_evidence_configure(globals_va, scenario_va); + } + if (getenv("HALO_UI_POINTER_TRACE")) + { + uint32_t count=0,targets=0,x=0,y=0; + if(mac_guest_image_find_object(elf_bytes,elf_size,"ui_mouse_target_count",4,&count)|| + mac_guest_image_find_object(elf_bytes,elf_size,"ui_mouse_targets",1536,&targets)|| + mac_guest_image_find_object(elf_bytes,elf_size,"ui_mouse_click_x",2,&x)|| + mac_guest_image_find_object(elf_bytes,elf_size,"ui_mouse_click_y",2,&y)) + { fprintf(stderr,"UI pointer evidence symbols unavailable\n");free(elf_bytes);return 2; } + mac_ui_evidence_configure(count,targets,x,y); + } + free(elf_bytes); + + if (mac_recomp_state_set_image(image.memory_offset, image.code_lo_va, + image.code_hi_va) != 0 || mac_guest_heap_install(image.image_end_va) != 0) + { + fprintf(stderr, "failed to install guest image memory/runtime state\n"); + return 2; + } + if (mac_macos_host_imports_register() != 0) + { + fprintf(stderr, "failed to register host import manifest\n"); + return 2; + } + fprintf(stderr, "AOT imports: %zu bound, %zu fail-fast\n", + mac_macos_host_imports_bound_count(), mac_macos_host_imports_missing_count()); + (void)recomp_dispatch_init(); + if (mac_guest_errno_install(recomp_lookup(errno_location_va)) != 0) + { + fprintf(stderr, "failed to install hash-matched guest errno accessor\n"); + return 2; + } + if (verify_indirect_host_import() != 0) + { + fprintf(stderr, "translated function-pointer host import check failed\n"); + return 2; + } + if (mac_guest_thread_runtime_install(&thread_runtime, thread_start_va) != 0) + { + fprintf(stderr, "failed to install guest thread runtime\n"); + return 2; + } + if (mac_guest_callback_runtime_install(&thread_runtime, thread_attach_va) != 0) + { + fprintf(stderr, "failed to install guest callback runtime\n"); + return 2; + } + if (make_guest_boot(data_root, save_root, &guest_boot_va) != 0 || + mac_guest_allocate(MAIN_GUEST_STACK_SIZE, 16 * 1024, &guest_stack_va) != 0) + { + fprintf(stderr, "cannot allocate guest boot block or main stack\n"); + return 2; + } + guest_esp = (guest_stack_va + MAIN_GUEST_STACK_SIZE) & ~UINT32_C(15); + guest_esp -= sizeof(stack_frame); + if (getenv("HALO_TRACE_GUEST_STACK")) + fprintf(stderr, "[GUEST-MAIN-STACK] thread=%llu base=0x%08x size=%u top=0x%08x entry_esp=0x%08x host=%p\n", + (unsigned long long)(uintptr_t)pthread_self(), guest_stack_va, + MAIN_GUEST_STACK_SIZE, guest_stack_va + MAIN_GUEST_STACK_SIZE, + guest_esp, mac_guest_address_resolve(guest_stack_va, MAIN_GUEST_STACK_SIZE)); + stack_frame[1] = guest_boot_va; + if (mac_guest_write(guest_esp, stack_frame, sizeof(stack_frame)) != 0) + { + fprintf(stderr, "cannot initialize guest entry stack\n"); + return 2; + } + mac_recomp_state_initialize_thread(guest_esp, 0); + entry = resolve_guest_function(image.entry_va); + if (!entry) + { + fprintf(stderr, "guest entry VA 0x%08x is not translated\n", image.entry_va); + return 2; + } + fprintf(stderr, "AOT image %s (%s), image end 0x%08x; data %s; saves %s\n", + elf_path, HALO_RECOMP_IMAGE_SHA256, image.image_end_va, data_root, save_root); + fprintf(stderr, "starting translated source guest at 0x%08x\n", image.entry_va); + entry(); + fprintf(stderr, "guest entry returned unexpectedly (EAX=0x%08x)\n", g_eax); + return 1; +} + +int main(int argc, char **argv) +{ + const char *elf_path = argc > 1 ? argv[1] : getenv("HALO_GUEST_ELF"); + if (!elf_path || !*elf_path) + elf_path = "build/macos-aot/halo_guest.elf"; + return launch_guest(elf_path); +} diff --git a/port/macos/musl-i386/pthread_arch.h b/port/macos/musl-i386/pthread_arch.h new file mode 100644 index 0000000000..9d7437459c --- /dev/null +++ b/port/macos/musl-i386/pthread_arch.h @@ -0,0 +1,13 @@ +/* Darwin's native thread has its own FS/GS base. The translated i386 guest + * stores musl's thread pointer through guest_thread.c/host_get_tp instead. + * Keep the i386 signal-context EIP mapping while replacing only __get_tp. */ +#include + +uintptr_t __guest_get_tp(void); + +static inline uintptr_t __get_tp(void) +{ + return __guest_get_tp(); +} + +#define MC_PC gregs[REG_EIP] diff --git a/port/macos/posix_backend_files.c b/port/macos/posix_backend_files.c new file mode 100644 index 0000000000..63e7d5dd36 --- /dev/null +++ b/port/macos/posix_backend_files.c @@ -0,0 +1,4 @@ +/* Compile the POSIX file backend under a private host namespace. The original + * names are occupied by AOT guest-import trampolines in the final executable. */ +#include "posix_backend_names.h" +#include "../linux/src/posix_files.c" diff --git a/port/macos/posix_backend_names.h b/port/macos/posix_backend_names.h new file mode 100644 index 0000000000..cb3007c13c --- /dev/null +++ b/port/macos/posix_backend_names.h @@ -0,0 +1,52 @@ +#ifndef HALO_MACOS_POSIX_BACKEND_NAMES_H +#define HALO_MACOS_POSIX_BACKEND_NAMES_H + +/* Native host backend names must not collide with recomp_import_aliases.c, + * which exports the original posix_* spellings as guest import trampolines. */ +#define posix_stat mac_native_posix_stat +#define posix_fstat mac_native_posix_fstat +#define posix_set_file_times mac_native_posix_set_file_times +#define posix_seek mac_native_posix_seek +#define posix_truncate mac_native_posix_truncate +#define posix_disk_space mac_native_posix_disk_space +#define posix_set_read_only mac_native_posix_set_read_only +#define posix_make_directory mac_native_posix_make_directory +#define posix_directory_open mac_native_posix_directory_open +#define posix_directory_next mac_native_posix_directory_next +#define posix_directory_close mac_native_posix_directory_close +#define posix_find_entry_case_insensitive mac_native_posix_find_entry_case_insensitive +#define posix_socket_last_error mac_native_posix_socket_last_error +#define posix_socket mac_native_posix_socket +#define posix_socket_close mac_native_posix_socket_close +#define posix_socket_bind mac_native_posix_socket_bind +#define posix_socket_connect mac_native_posix_socket_connect +#define posix_socket_listen mac_native_posix_socket_listen +#define posix_socket_accept mac_native_posix_socket_accept +#define posix_socket_send mac_native_posix_socket_send +#define posix_socket_sendto mac_native_posix_socket_sendto +#define posix_socket_recv mac_native_posix_socket_recv +#define posix_socket_recvfrom mac_native_posix_socket_recvfrom +#define posix_socket_shutdown mac_native_posix_socket_shutdown +#define posix_socket_set_nonblocking mac_native_posix_socket_set_nonblocking +#define posix_socket_bytes_available mac_native_posix_socket_bytes_available +#define posix_socket_set_nodelay mac_native_posix_socket_set_nodelay +#define posix_socket_setsockopt mac_native_posix_socket_setsockopt +#define posix_socket_getsockopt mac_native_posix_socket_getsockopt +#define posix_socket_getsockname mac_native_posix_socket_getsockname +#define posix_socket_getpeername mac_native_posix_socket_getpeername +#define posix_socket_select mac_native_posix_socket_select +#define posix_local_ipv4_address mac_native_posix_local_ipv4_address +#define posix_random_bytes mac_native_posix_random_bytes +#define posix_resolve_ipv4 mac_native_posix_resolve_ipv4 +#define posix_command_line_argument mac_native_posix_command_line_argument +#define posix_process_id mac_native_posix_process_id +#define posix_register_url_scheme mac_native_posix_register_url_scheme +#define posix_discord_connect mac_native_posix_discord_connect +#define posix_discord_write mac_native_posix_discord_write +#define posix_discord_read mac_native_posix_discord_read +#define posix_discord_close mac_native_posix_discord_close + +#define posix_upnp_forward_udp mac_native_posix_upnp_forward_udp +#define posix_upnp_stop_forwarding_udp mac_native_posix_upnp_stop_forwarding_udp + +#endif diff --git a/port/macos/posix_backend_net.c b/port/macos/posix_backend_net.c new file mode 100644 index 0000000000..8c119e4e57 --- /dev/null +++ b/port/macos/posix_backend_net.c @@ -0,0 +1,36 @@ +/* Compile the POSIX network backend under a private host namespace. */ +#include "posix_backend_names.h" +#undef posix_socket_sendto +#define posix_socket_sendto mac_native_posix_socket_sendto_original +#include "../linux/src/posix_net.c" +#undef posix_socket_sendto + +/* Preserve the backend's Winsock error namespace for bridge validation too. */ +int mac_native_posix_bridge_error(int error) +{ + errno = error; + return fail(); +} + +/* Darwin rejects a destination on a connected socket even when it is its + * existing peer. Linux/Winsock callers use sendto in this situation. Retry + * with send only after proving exact IPv4 endpoint equality; alternate peers + * retain the original failure rather than silently reaching the wrong peer. */ +int mac_native_posix_socket_sendto(int socket, const void *buffer, int length, + int flags, const void *address, int address_length) +{ + int result = mac_native_posix_socket_sendto_original(socket, buffer, length, flags, address, address_length); + if (result < 0 && errno == EISCONN && address && address_length == sizeof(struct sockaddr_in)) + { + int saved_error = errno; + struct sockaddr_in peer; + socklen_t peer_length = sizeof(peer); + struct sockaddr_in const *destination = address; + if (destination->sin_family == AF_INET && !getpeername(socket, (struct sockaddr *)&peer, &peer_length) && + peer_length == sizeof(peer) && peer.sin_family == AF_INET && + peer.sin_addr.s_addr == destination->sin_addr.s_addr && peer.sin_port == destination->sin_port) + return succeed((int)send(socket, buffer, (size_t)length, flags | MSG_NOSIGNAL)); + errno = saved_error; + } + return result; +} diff --git a/port/macos/posix_backend_upnp.c b/port/macos/posix_backend_upnp.c new file mode 100644 index 0000000000..7a3577838f --- /dev/null +++ b/port/macos/posix_backend_upnp.c @@ -0,0 +1,2 @@ +#include "posix_backend_names.h" +#include "../linux/src/posix_upnp.c" diff --git a/port/macos/posix_host_imports.c b/port/macos/posix_host_imports.c new file mode 100644 index 0000000000..170753dd98 --- /dev/null +++ b/port/macos/posix_host_imports.c @@ -0,0 +1,539 @@ +#include "posix_host_imports.h" + +#include "guest_address.h" +#include "guest_call.h" +#include "guest_errno.h" +#include "host_imports.h" +#include "host_services.h" +#include "posix_backend_names.h" +#include "../linux/src/posix.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +extern int mac_native_posix_bridge_error(int error); + +#define POSIX_STRING_CAP 4096u +#define POSIX_IO_CAP (16u * 1024u * 1024u) + +static int guest_string(uint32_t address, char output[POSIX_STRING_CAP]) +{ + const char *input; + size_t available, limit, length; + if (!address || !(input = mac_guest_address_resolve(address, 1))) + { errno = EFAULT; return -1; } + available = mac_guest_address_available(address); + limit = available < POSIX_STRING_CAP ? available : POSIX_STRING_CAP; + length = strnlen(input, limit); + if (length == limit) + { errno = EFAULT; return -1; } + memcpy(output, input, length + 1); + return 0; +} + +static void *guest_span(uint32_t address, size_t size) +{ + if (size > POSIX_IO_CAP) + { errno = EFAULT; return NULL; } + void *span = mac_guest_address_resolve(address, size); + if (!span) errno = EFAULT; + return span; +} + +#define ARG(n) mac_guest_host_import_arg32(n) +#define RET(v) mac_guest_host_import_return32((uint32_t)(v)) +#define VOIDRET() mac_guest_host_import_return_void() +/* Filesystem -1 results follow guest musl errno, unlike Winsock errors. */ +#define FILERET(v) do { int file_result_ = (v); if (file_result_ < 0) { \ + int file_error_ = errno; (void)mac_guest_errno_set_native(file_error_); } \ + RET(file_result_); } while (0) + + +/* Diagnostic transport alias for two real local game instances on Darwin, + * where unassigned 127/8 addresses cannot bind. This changes endpoint address + * records only; network payloads and the guest's self-address logic are intact. + * It is disabled unless explicitly requested with internet play disabled. */ +static pthread_once_t test_alias_once = PTHREAD_ONCE_INIT; +static struct { int enabled, valid; uint32_t logical, physical; } test_alias; +static void initialize_test_alias(void) +{ + const char *setting = getenv("HALO_LOCAL_TEST_IPV4_ALIAS"); + const char *online = getenv("HALO_NET_ONLINE"); + const char *local = getenv("HALO_NET_ADDRESS"); + struct in_addr logical, physical, configured; + uint32_t host; + test_alias.valid = 1; + if (!setting) return; + test_alias.enabled = 1; test_alias.valid = 0; + if (!online || (strcasecmp(online,"false") && strcmp(online,"0") && + strcasecmp(online,"off") && strcasecmp(online,"no"))) goto invalid; + if (inet_pton(AF_INET,setting,&logical)!=1 || !local || inet_pton(AF_INET,local,&configured)!=1) goto invalid; + host=ntohl(logical.s_addr); + if ((host & 0xff000000u)!=0x7f000000u || (host & 0xffffffu)<=1 || + (host & 0xffffffu)==0xffffffu) goto invalid; + if (inet_pton(AF_INET,"127.0.0.1",&physical)!=1 || configured.s_addr==physical.s_addr) goto invalid; + test_alias.logical=logical.s_addr; test_alias.physical=physical.s_addr; test_alias.valid=1; + fprintf(stderr,"[local-test-ipv4] logical=%s native=127.0.0.1 offline=1\n",setting); + return; +invalid: + fprintf(stderr,"[local-test-ipv4] rejected: require distinct 127/8 alias, explicit network address, and HALO_NET_ONLINE=false\n"); +} +static int test_alias_map(uint32_t address, int to_native, uint32_t *result) +{ + pthread_once(&test_alias_once,initialize_test_alias); + if (!test_alias.valid) return mac_native_posix_bridge_error(EINVAL); + *result=address; + if (!test_alias.enabled) return 0; + /* The physical loopback is reserved for this mapping. Supplying it from + * the guest would collide with its own special self-address semantics. */ + if (to_native && address==test_alias.physical) return mac_native_posix_bridge_error(EINVAL); + if (address==(to_native?test_alias.logical:test_alias.physical)) + *result=to_native?test_alias.physical:test_alias.logical; + return 0; +} + +/* Xbox/Linux sockaddr_in begins with a 16-bit AF_INET. Darwin stores an + * eight-bit length followed by an eight-bit family; port/address keep their + * network byte order. Marshal this record rather than passing it through. */ +static int ipv4_input(uint32_t va, uint32_t length, struct sockaddr_in *native) +{ + uint8_t wire[16]; + uint16_t family; + if (length != sizeof(wire)) return mac_native_posix_bridge_error(EINVAL); + if (mac_guest_read(va, wire, sizeof(wire))) return mac_native_posix_bridge_error(EFAULT); + memcpy(&family, wire, 2); + if (family != 2) return mac_native_posix_bridge_error(EAFNOSUPPORT); + memset(native, 0, sizeof(*native)); + native->sin_len = sizeof(*native); + native->sin_family = AF_INET; + memcpy((uint8_t *)native + 2, wire + 2, 14); + return test_alias_map(native->sin_addr.s_addr, 1, &native->sin_addr.s_addr); +} + +static int ipv4_output(uint32_t va, uint32_t capacity, const struct sockaddr_in *native) +{ + uint8_t wire[16]; + uint16_t family = 2; + if (native->sin_family != AF_INET) return -1; + memcpy(wire, &family, 2); + memcpy(wire + 2, (const uint8_t *)native + 2, 14); + uint32_t address; + if (test_alias_map(native->sin_addr.s_addr, 0, &address)) return -1; + memcpy(wire + 4, &address, 4); + return mac_guest_write(va, wire, capacity < sizeof(wire) ? capacity : sizeof(wire)); +} + +static void b_posix_stat(void) +{ + char path[POSIX_STRING_CAP]; + struct posix_file_information information; + uint32_t out = ARG(1); + int result; + if (guest_string(ARG(0), path) || !guest_span(out, sizeof(information))) { FILERET(-1); return; } + result = posix_stat(path, &information); + if (!result && mac_guest_write(out, &information, sizeof(information))) result = -1; + FILERET(result); +} + +static void b_posix_fstat(void) +{ + struct posix_file_information information; + uint32_t out = ARG(1); + int result; + if (!guest_span(out, sizeof(information))) { FILERET(-1); return; } + result = posix_fstat((int32_t)ARG(0), &information); + if (!result && mac_guest_write(out, &information, sizeof(information))) result = -1; + FILERET(result); +} + +static void b_posix_set_file_times(void) +{ + char path[POSIX_STRING_CAP]; + if (guest_string(ARG(0), path)) { FILERET(-1); return; } + FILERET(posix_set_file_times(path, ARG(1), ARG(2), ARG(3), ARG(4))); +} + +static void b_posix_seek(void) +{ + posix_ulong low, high; + uint32_t low_va = ARG(4), high_va = ARG(5); + int result; + if (!guest_span(low_va, 4) || !guest_span(high_va, 4)) { FILERET(-1); return; } + result = posix_seek((int32_t)ARG(0), (int32_t)ARG(1), (int32_t)ARG(2), + (int32_t)ARG(3), &low, &high); + if (!result && (mac_guest_write(low_va, &low, 4) || mac_guest_write(high_va, &high, 4))) result = -1; + FILERET(result); +} + +static void b_posix_truncate(void) +{ + FILERET(posix_truncate((int32_t)ARG(0), ARG(1), ARG(2))); +} + +static void b_posix_disk_space(void) +{ + char path[POSIX_STRING_CAP]; + posix_ulong free_low, free_high, total_low, total_high; + uint32_t va[4] = { ARG(1), ARG(2), ARG(3), ARG(4) }; + int result; + if (guest_string(ARG(0), path)) { FILERET(-1); return; } + for (unsigned i = 0; i < 4; ++i) if (!guest_span(va[i], 4)) { FILERET(-1); return; } + result = posix_disk_space(path, &free_low, &free_high, &total_low, &total_high); + if (!result && (mac_guest_write(va[0], &free_low, 4) || mac_guest_write(va[1], &free_high, 4) || + mac_guest_write(va[2], &total_low, 4) || mac_guest_write(va[3], &total_high, 4))) result = -1; + FILERET(result); +} + +static void b_posix_set_read_only(void) +{ + char path[POSIX_STRING_CAP]; + if (guest_string(ARG(0), path)) { FILERET(-1); return; } + FILERET(posix_set_read_only(path, (int32_t)ARG(1))); +} + +static void b_posix_make_directory(void) +{ + char path[POSIX_STRING_CAP]; + if (guest_string(ARG(0), path)) { FILERET(-1); return; } + FILERET(posix_make_directory(path)); +} + +static void b_posix_directory_open(void) +{ + char path[POSIX_STRING_CAP]; + void *handle; + if (guest_string(ARG(0), path)) { (void)mac_guest_errno_set_native(errno); RET(0); return; } + handle = posix_directory_open(path); + if (!handle) (void)mac_guest_errno_set_native(errno); + RET((uintptr_t)handle); +} + +static void b_posix_directory_next(void) +{ + uint32_t name_va = ARG(1), capacity = ARG(2); + void *name = guest_span(name_va, capacity); + if (!name || !capacity) { RET(0); return; } + RET(posix_directory_next((void *)(uintptr_t)ARG(0), name, capacity)); +} + +static void b_posix_directory_close(void) +{ + posix_directory_close((void *)(uintptr_t)ARG(0)); + VOIDRET(); +} + +static void b_posix_find_entry(void) +{ + char directory[POSIX_STRING_CAP], name[POSIX_STRING_CAP]; + uint32_t out_va = ARG(2), capacity = ARG(3); + void *out = guest_span(out_va, capacity); + if (guest_string(ARG(0), directory) || guest_string(ARG(1), name) || !out || !capacity) { RET(0); return; } + RET(posix_find_entry_case_insensitive(directory, name, out, capacity)); +} + +static void b_posix_local_ipv4(void) { RET(posix_local_ipv4_address()); } + +static void b_posix_random_bytes(void) +{ + uint32_t va = ARG(0), size = ARG(1); + void *buffer = guest_span(va, size); + if (!buffer && size) { mac_host_abort("posix_random_bytes received invalid guest span"); } + posix_random_bytes(buffer, size); + VOIDRET(); +} + +static void b_posix_resolve_ipv4(void) +{ + char host[POSIX_STRING_CAP]; + if (guest_string(ARG(0), host)) { RET(0); return; } + RET(posix_resolve_ipv4(host)); +} + +static void b_posix_command_line(void) +{ + uint32_t buffer_va = ARG(1), capacity = ARG(2); + void *buffer = guest_span(buffer_va, capacity); + if (!buffer && capacity) { RET(0); return; } + RET(posix_command_line_argument((int32_t)ARG(0), buffer, capacity)); +} + +static void b_posix_process_id(void) { RET(posix_process_id()); } + +static void b_posix_register_url(void) +{ + char scheme[POSIX_STRING_CAP], description[POSIX_STRING_CAP]; + if (guest_string(ARG(0), scheme) || guest_string(ARG(1), description)) { RET(0); return; } + RET(posix_register_url_scheme(scheme, description)); +} + +static void b_posix_discord_connect(void) { RET(posix_discord_connect()); } +static void b_posix_discord_close(void) { posix_discord_close((int32_t)ARG(0)); VOIDRET(); } + +static void b_posix_discord_read(void) +{ + uint32_t va = ARG(1), size = ARG(2); + void *buffer = guest_span(va, size); + if (!buffer && size) { RET(-1); return; } + RET(posix_discord_read((int32_t)ARG(0), buffer, (int32_t)size)); +} + +static void b_posix_discord_write(void) +{ + uint32_t va = ARG(1), size = ARG(2); + const void *buffer = guest_span(va, size); + if (!buffer && size) { RET(-1); return; } + RET(posix_discord_write((int32_t)ARG(0), buffer, (int32_t)size)); +} + +static void b_posix_socket(void) { RET(posix_socket((int32_t)ARG(0), (int32_t)ARG(1), (int32_t)ARG(2))); } +static void b_posix_socket_close(void) { RET(posix_socket_close((int32_t)ARG(0))); } +static void socket_failure_trace(const char *operation, int descriptor, + const struct sockaddr_in *target, int result) +{ + int saved_errno = errno, error = posix_socket_last_error(); + const char *enabled = getenv("HALO_NETWORK_TRACE"); + struct sockaddr_in local = {0}; + socklen_t length = sizeof(local); + char endpoint[INET_ADDRSTRLEN] = "unknown"; + if (result >= 0 || !enabled || strcmp(enabled, "1") != 0) + return; + if (!target && getsockname(descriptor, (struct sockaddr *)&local, &length) == 0) + target = &local; + if (target && target->sin_family == AF_INET) + (void)inet_ntop(AF_INET, &target->sin_addr, endpoint, sizeof(endpoint)); + fprintf(stderr, "[network-socket-failure] op=%s fd=%d native_ipv4=%s port=%u errno=%d winsock=%d\n", + operation, descriptor, endpoint, target ? ntohs(target->sin_port) : 0, + saved_errno, error); + /* Logging must not change native errno or the backend's Winsock error. */ + errno = saved_errno; +} +static void datagram_trace(const char *operation, int descriptor, + const struct sockaddr_in *peer, const void *buffer, uint32_t requested, int result) +{ + static _Thread_local unsigned long sent_count, received_count; + unsigned long *count = strcmp(operation, "sendto") == 0 ? &sent_count : &received_count; + const char *enabled = getenv("HALO_NETWORK_TRACE"); + if (!enabled || strcmp(enabled, "1") != 0) return; + ++*count; + if (result < 0) { socket_failure_trace(operation, descriptor, peer, result); return; } + if (*count > 16 && *count % 300 != 0) return; + int saved_errno = errno; + char ip[INET_ADDRSTRLEN] = "unknown", local_ip[INET_ADDRSTRLEN] = "unknown", header[25] = {0}; + struct sockaddr_in local={0}; socklen_t local_length=sizeof(local); + if (getsockname(descriptor,(struct sockaddr *)&local,&local_length)==0) + (void)inet_ntop(AF_INET,&local.sin_addr,local_ip,sizeof(local_ip)); + const unsigned char *bytes = buffer; + unsigned length = (unsigned)result < 12 ? (unsigned)result : 12; + if (peer) (void)inet_ntop(AF_INET, &peer->sin_addr, ip, sizeof(ip)); + for (unsigned i=0;isin_port):0,requested,result,header); + errno = saved_errno; +} +static void b_posix_socket_listen(void) +{ + int descriptor = (int32_t)ARG(0); + int result = posix_socket_listen(descriptor, (int32_t)ARG(1)); + socket_failure_trace("listen", descriptor, NULL, result); + RET(result); +} +static void b_posix_socket_connect(void) +{ + struct sockaddr_in address; + if (ipv4_input(ARG(1), ARG(2), &address)) { RET(-1); return; } + int descriptor = (int32_t)ARG(0); + int result = posix_socket_connect(descriptor, &address, sizeof(address)); + socket_failure_trace("connect", descriptor, &address, result); + RET(result); +} +static void b_posix_socket_bind(void) +{ + struct sockaddr_in address; + if (ipv4_input(ARG(1), ARG(2), &address)) { RET(-1); return; } + int descriptor = (int32_t)ARG(0); + int result = posix_socket_bind(descriptor, &address, sizeof(address)); + socket_failure_trace("bind", descriptor, &address, result); + RET(result); +} +static void b_posix_socket_accept(void) +{ + uint32_t va = ARG(1), len_va = ARG(2), capacity = 0; + struct sockaddr_in address; + int length = sizeof(address), result; + if (va && (!len_va || mac_guest_read(len_va, &capacity, 4) || + !guest_span(va, capacity))) { RET(mac_native_posix_bridge_error(EFAULT)); return; } + result = posix_socket_accept((int32_t)ARG(0), va ? &address : NULL, va ? &length : NULL); + if (result >= 0 && va && (ipv4_output(va, capacity, &address) || mac_guest_write(len_va, &length, 4))) + { posix_socket_close(result); result = -1; } + RET(result); +} +static void b_posix_socket_send(void) +{ + uint32_t va = ARG(1), n = ARG(2); const void *p = guest_span(va, n); + if (!p && n) { RET(mac_native_posix_bridge_error(EFAULT)); return; } + RET(posix_socket_send((int32_t)ARG(0), p, (int32_t)n, (int32_t)ARG(3))); +} +static void b_posix_socket_sendto(void) +{ + uint32_t n = ARG(2); const void *buffer = guest_span(ARG(1), n); + struct sockaddr_in address; + if (!buffer && n) { RET(mac_native_posix_bridge_error(EFAULT)); return; } + if (ipv4_input(ARG(4), ARG(5), &address)) { RET(-1); return; } + int descriptor=(int32_t)ARG(0); + int result=posix_socket_sendto(descriptor,buffer,(int32_t)n,(int32_t)ARG(3),&address,sizeof(address)); + datagram_trace("sendto",descriptor,&address,buffer,n,result); + RET(result); +} +static void b_posix_socket_recv(void) +{ + uint32_t va = ARG(1), n = ARG(2); void *p = guest_span(va, n); + if (!p && n) { RET(mac_native_posix_bridge_error(EFAULT)); return; } + RET(posix_socket_recv((int32_t)ARG(0), p, (int32_t)n, (int32_t)ARG(3))); +} +static void b_posix_socket_recvfrom(void) +{ + uint32_t n = ARG(2), va = ARG(4), len_va = ARG(5), capacity = 0; + void *buffer = guest_span(ARG(1), n); + struct sockaddr_in address; + int length = sizeof(address), result; + if ((!buffer && n) || (va && (!len_va || mac_guest_read(len_va, &capacity, 4) || !guest_span(va, capacity)))) + { RET(mac_native_posix_bridge_error(EFAULT)); return; } + result = posix_socket_recvfrom((int32_t)ARG(0), buffer, (int32_t)n, (int32_t)ARG(3), va ? &address : NULL, va ? &length : NULL); + if (result >= 0) datagram_trace("recvfrom",(int32_t)ARG(0),va?&address:NULL,buffer,n,result); + if (result >= 0 && va && (ipv4_output(va, capacity, &address) || mac_guest_write(len_va, &length, 4))) result = -1; + RET(result); +} +static void b_posix_socket_shutdown(void) { RET(posix_socket_shutdown((int32_t)ARG(0), (int32_t)ARG(1))); } +static void b_posix_socket_nonblocking(void) { RET(posix_socket_set_nonblocking((int32_t)ARG(0), (int32_t)ARG(1))); } +static void b_posix_socket_nodelay(void) { RET(posix_socket_set_nodelay((int32_t)ARG(0))); } +static void b_posix_socket_last_error(void) { RET(posix_socket_last_error()); } +static void b_posix_socket_bytes_available(void) +{ + uint32_t out_va = ARG(1); posix_ulong value; int result; + if (!guest_span(out_va, 4)) { RET(mac_native_posix_bridge_error(EFAULT)); return; } + result = posix_socket_bytes_available((int32_t)ARG(0), &value); + if (!result && mac_guest_write(out_va, &value, 4)) result = -1; + RET(result); +} +static void b_posix_socket_setsockopt(void) +{ + uint32_t va = ARG(3), n = ARG(4); const void *p = guest_span(va, n); + if (!p && n) { RET(mac_native_posix_bridge_error(EFAULT)); return; } + RET(posix_socket_setsockopt((int32_t)ARG(0), (int32_t)ARG(1), (int32_t)ARG(2), p, (int32_t)n)); +} +static void b_posix_socket_getsockopt(void) +{ + uint32_t va = ARG(3), len_va = ARG(4), n = 0; int *length = guest_span(len_va, 4), result; void *p; + if (!length || mac_guest_read(len_va, &n, 4)) { RET(mac_native_posix_bridge_error(EFAULT)); return; } + p = guest_span(va, n); if (!p && n) { RET(mac_native_posix_bridge_error(EFAULT)); return; } + result = posix_socket_getsockopt((int32_t)ARG(0), (int32_t)ARG(1), (int32_t)ARG(2), p, (int *)&n); + if (mac_guest_write(len_va, &n, 4)) result = -1; + RET(result); +} +static void b_posix_socket_getsockname(void) +{ + uint32_t va = ARG(1), len_va = ARG(2), capacity; + struct sockaddr_in address; + int length = sizeof(address), result; + if (mac_guest_read(len_va, &capacity, 4) || !guest_span(va, capacity)) { RET(mac_native_posix_bridge_error(EFAULT)); return; } + result = posix_socket_getsockname((int32_t)ARG(0), &address, &length); + if (!result && (ipv4_output(va, capacity, &address) || mac_guest_write(len_va, &length, 4))) result = -1; + RET(result); +} +static void b_posix_socket_getpeername(void) +{ + uint32_t va = ARG(1), len_va = ARG(2), capacity; + struct sockaddr_in address; + int length = sizeof(address), result; + if (mac_guest_read(len_va, &capacity, 4) || !guest_span(va, capacity)) { RET(mac_native_posix_bridge_error(EFAULT)); return; } + result = posix_socket_getpeername((int32_t)ARG(0), &address, &length); + if (!result && (ipv4_output(va, capacity, &address) || mac_guest_write(len_va, &length, 4))) result = -1; + RET(result); +} + +/* Select lists are guest arrays of signed 32-bit descriptors. Convert them to + * bounded host arrays; Darwin fd_set is never exposed to guest storage. */ +static void b_posix_socket_select(void) +{ + int32_t counts[3]; uint32_t list_va[3] = {ARG(0),ARG(2),ARG(4)}; + int32_t count_va[3] = {(int32_t)ARG(1),(int32_t)ARG(3),(int32_t)ARG(5)}; + int *lists[3] = {NULL,NULL,NULL}; int result; + for (int i=0;i<3;i++) { + uint32_t cap = 0; + if (count_va[i] && mac_guest_read((uint32_t)count_va[i], &cap, 4)) { RET(mac_native_posix_bridge_error(EFAULT)); return; } + counts[i]=(int32_t)cap; + if (counts[i] < 0 || counts[i] > 1024) { RET(mac_native_posix_bridge_error(EFAULT)); return; } + if (counts[i]) { + lists[i]=calloc((size_t)counts[i], sizeof(int)); + if (!lists[i] || mac_guest_read(list_va[i], lists[i], (size_t)counts[i]*4)) { for (int j=0;j<3;j++) free(lists[j]); RET(mac_native_posix_bridge_error(EFAULT)); return; } + } + } + result=posix_socket_select(lists[0],(int*)&counts[0],lists[1],(int*)&counts[1],lists[2],(int*)&counts[2], + (int32_t)ARG(6),(int32_t)ARG(7),(int32_t)ARG(8)); + for (int i=0;i<3;i++) { + if (counts[i] > 0 && mac_guest_write(list_va[i],lists[i],(size_t)counts[i]*4)) result=-1; + if (count_va[i] && mac_guest_write((uint32_t)count_va[i],&counts[i],4)) result=-1; + free(lists[i]); + } + RET(result); +} + +static void b_posix_upnp_forward_udp(void) +{ + uint32_t address_va = ARG(1), port_va = ARG(2), error_va = ARG(3), capacity = ARG(4); + posix_ulong address = 0; unsigned short port = 0; int result; + char *error; + if (!guest_span(address_va, 4) || !guest_span(port_va, 2) || !capacity || + capacity > POSIX_STRING_CAP || !guest_span(error_va, capacity)) { RET(0); return; } + error = calloc(capacity, 1); + if (!error) { RET(0); return; } + result = posix_upnp_forward_udp((unsigned short)ARG(0), &address, &port, error, (int)capacity); + error[capacity - 1] = 0; + if (mac_guest_write(error_va, error, capacity)) result = 0; + if (result && (mac_guest_write(address_va, &address, 4) || mac_guest_write(port_va, &port, 2))) result = 0; + free(error); RET(result); +} +static void b_posix_upnp_stop_forwarding_udp(void) +{ + posix_upnp_stop_forwarding_udp((unsigned short)ARG(0)); + mac_guest_host_import_return_void(); +} + +#define BIND(n,f) { #n, b_##f } +static const struct { const char *name; mac_guest_import_bridge bridge; } entries[] = { + BIND(posix_stat,posix_stat), BIND(posix_fstat,posix_fstat), BIND(posix_set_file_times,posix_set_file_times), + BIND(posix_seek,posix_seek), BIND(posix_truncate,posix_truncate), BIND(posix_disk_space,posix_disk_space), + BIND(posix_set_read_only,posix_set_read_only), BIND(posix_make_directory,posix_make_directory), + BIND(posix_directory_open,posix_directory_open), BIND(posix_directory_next,posix_directory_next), + BIND(posix_directory_close,posix_directory_close), BIND(posix_find_entry_case_insensitive,posix_find_entry), + BIND(posix_local_ipv4_address,posix_local_ipv4), BIND(posix_random_bytes,posix_random_bytes), + BIND(posix_resolve_ipv4,posix_resolve_ipv4), BIND(posix_command_line_argument,posix_command_line), + BIND(posix_process_id,posix_process_id), BIND(posix_register_url_scheme,posix_register_url), + BIND(posix_discord_connect,posix_discord_connect), BIND(posix_discord_close,posix_discord_close), + BIND(posix_discord_read,posix_discord_read), BIND(posix_discord_write,posix_discord_write), + BIND(posix_socket,posix_socket), BIND(posix_socket_close,posix_socket_close), BIND(posix_socket_listen,posix_socket_listen), + BIND(posix_socket_connect,posix_socket_connect), BIND(posix_socket_bind,posix_socket_bind), + BIND(posix_socket_accept,posix_socket_accept), BIND(posix_socket_send,posix_socket_send), + BIND(posix_socket_sendto,posix_socket_sendto), BIND(posix_socket_recv,posix_socket_recv), + BIND(posix_socket_recvfrom,posix_socket_recvfrom), BIND(posix_socket_shutdown,posix_socket_shutdown), + BIND(posix_socket_set_nonblocking,posix_socket_nonblocking), BIND(posix_socket_set_nodelay,posix_socket_nodelay), + BIND(posix_socket_last_error,posix_socket_last_error), BIND(posix_socket_bytes_available,posix_socket_bytes_available), + BIND(posix_socket_setsockopt,posix_socket_setsockopt), BIND(posix_socket_getsockopt,posix_socket_getsockopt), + BIND(posix_socket_getsockname,posix_socket_getsockname), BIND(posix_socket_getpeername,posix_socket_getpeername), + BIND(posix_socket_select,posix_socket_select), + BIND(posix_upnp_forward_udp,posix_upnp_forward_udp), BIND(posix_upnp_stop_forwarding_udp,posix_upnp_stop_forwarding_udp) +}; + +mac_guest_import_bridge mac_macos_posix_host_import_bridge(const char *name) +{ + if (!name) return NULL; + for (size_t i=0;i + +/* The source ELF contains the complete guest function image; this AOT target + * has no separate Xbox kernel-thunk range. Imported Android host functions + * are resolved by the exact manifest aliases before this fallback. */ +recomp_func_t recomp_lookup_kernel(uint32_t guest_va) +{ + /* GL procedure tokens live in the FE100000 namespace. The AOT guest holds + * these 32-bit tokens in its function tables; they resolve to typed guest + * cdecl wrappers, never to native 64-bit GL pointers. */ + if (guest_va >= UINT32_C(0xFE100000)) + return (recomp_func_t)mac_macos_gl_wrapper_for_token(guest_va); + return NULL; +} + +void recomp_icall_fail_log(uint32_t guest_va) +{ + static _Atomic unsigned reports; + unsigned report = atomic_fetch_add_explicit(&reports, 1, memory_order_relaxed); + if (report < 32) + mac_host_logf(3, "unresolved guest indirect call target 0x%08x (icalls=%llu)", + guest_va, (unsigned long long)g_icall_count); + mac_host_abort("unresolved in-range translated indirect call"); +} + +void recomp_icall_not_code_log(uint32_t guest_va) +{ + static _Atomic unsigned reports; + unsigned report = atomic_fetch_add_explicit(&reports, 1, memory_order_relaxed); + if (report < 32) + mac_host_logf(3, "guest indirect target outside code/import ranges: 0x%08x", + guest_va); + mac_host_abort("invalid guest indirect target"); +} + +void recomp_unsupported_instruction(uint32_t guest_va) +{ + mac_host_logf(3, "AOT guest reached unsupported instruction at 0x%08x", guest_va); + mac_host_logf(3, "guest registers esp=0x%08x ebp=0x%08x eax=0x%08x ecx=0x%08x edx=0x%08x", + g_esp, g_ebp, g_eax, g_ecx, g_edx); + uint32_t frame = g_ebp; + for (unsigned index = 0; index < 16 && frame; ++index) + { + uint32_t words[2]; + if (mac_guest_read(frame, words, sizeof(words)) != 0) + break; + mac_host_logf(3, "guest frame %u fp=0x%08x return=0x%08x parent=0x%08x", + index, frame, words[1], words[0]); + uint32_t arguments[6]; + if (mac_guest_read(frame + 8, arguments, sizeof(arguments)) == 0) + { + mac_host_logf(3, "guest frame %u words=%08x %08x %08x %08x %08x %08x", + index, arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5]); + if (guest_va == 0x003e2a23 && index == 1) + for (unsigned arg = 0; arg < 2; ++arg) + { + uint32_t bytes[4]; + if (arguments[arg] >= 8 && mac_guest_read(arguments[arg] - 8, bytes, sizeof(bytes)) == 0) + mac_host_logf(3, "malloc guard arg%u va=%08x surrounding=%08x %08x %08x %08x", + arg, arguments[arg], bytes[0], bytes[1], bytes[2], bytes[3]); + } + } + if (words[0] <= frame || words[0] - frame > 1024 * 1024) + break; + frame = words[0]; + } + mac_host_abort("translated guest instruction is unsupported"); +} diff --git a/port/macos/recomp_state.c b/port/macos/recomp_state.c new file mode 100644 index 0000000000..1d69a44983 --- /dev/null +++ b/port/macos/recomp_state.c @@ -0,0 +1,75 @@ +#include "recomp_state.h" + +#define RECOMP_GENERATED_CODE 1 +#include "recomp_types.h" + +#include + +ptrdiff_t g_xbox_mem_offset; +uint32_t g_xbox_code_lo; +uint32_t g_xbox_code_hi; + +RECOMP_TLS uint32_t g_eax, g_ecx, g_edx, g_esp; +RECOMP_TLS uint32_t g_ebx, g_esi, g_edi, g_ebp; +RECOMP_TLS double g_fp_stack[8]; +RECOMP_TLS int g_fp_top; +RECOMP_TLS uint32_t g_mxcsr; +RECOMP_TLS uint32_t g_fs_base; +RECOMP_TLS uint32_t g_seh_ebp; +RECOMP_TLS int g_df; +RECOMP_TLS uint16_t g_fp_control_word; +RECOMP_TLS int g_fp_cmp; +RECOMP_TLS uint16_t g_fp_status_word; +RECOMP_TLS uint8_t g_fp_empty_mask; +RECOMP_TLS RecompXmm g_xmm0, g_xmm1, g_xmm2, g_xmm3; +RECOMP_TLS RecompXmm g_xmm4, g_xmm5, g_xmm6, g_xmm7; +RECOMP_TLS RecompMmx g_mm0, g_mm1, g_mm2, g_mm3; +RECOMP_TLS RecompMmx g_mm4, g_mm5, g_mm6, g_mm7; + +volatile uint32_t g_icall_trace[ICALL_TRACE_SIZE]; +volatile uint32_t g_icall_trace_idx; +volatile uint64_t g_icall_count; + +int mac_recomp_state_set_image(ptrdiff_t memory_offset, uint32_t code_lo, + uint32_t code_hi) +{ + if (code_hi <= code_lo) + return -1; + g_xbox_mem_offset = memory_offset; + g_xbox_code_lo = code_lo; + g_xbox_code_hi = code_hi; + return 0; +} + +void mac_recomp_state_initialize_thread(uint32_t guest_esp, uint32_t fs_base) +{ + g_eax = g_ecx = g_edx = 0; + g_esp = guest_esp; + g_ebx = g_esi = g_edi = g_ebp = 0; + memset(g_fp_stack, 0, sizeof(g_fp_stack)); + g_fp_top = 0; + g_fs_base = fs_base; + g_seh_ebp = 0; + g_df = 0; + g_fp_control_word = UINT16_C(0x037f); + g_mxcsr = UINT32_C(0x1f80); + g_fp_cmp = 0; + g_fp_status_word = 0; + g_fp_empty_mask = UINT8_C(0xff); + memset(&g_xmm0, 0, sizeof(g_xmm0)); + memset(&g_xmm1, 0, sizeof(g_xmm1)); + memset(&g_xmm2, 0, sizeof(g_xmm2)); + memset(&g_xmm3, 0, sizeof(g_xmm3)); + memset(&g_xmm4, 0, sizeof(g_xmm4)); + memset(&g_xmm5, 0, sizeof(g_xmm5)); + memset(&g_xmm6, 0, sizeof(g_xmm6)); + memset(&g_xmm7, 0, sizeof(g_xmm7)); + memset(&g_mm0, 0, sizeof(g_mm0)); + memset(&g_mm1, 0, sizeof(g_mm1)); + memset(&g_mm2, 0, sizeof(g_mm2)); + memset(&g_mm3, 0, sizeof(g_mm3)); + memset(&g_mm4, 0, sizeof(g_mm4)); + memset(&g_mm5, 0, sizeof(g_mm5)); + memset(&g_mm6, 0, sizeof(g_mm6)); + memset(&g_mm7, 0, sizeof(g_mm7)); +} diff --git a/port/macos/recomp_state.h b/port/macos/recomp_state.h new file mode 100644 index 0000000000..81c3e18bd3 --- /dev/null +++ b/port/macos/recomp_state.h @@ -0,0 +1,25 @@ +#ifndef HALO_MACOS_RECOMP_STATE_H +#define HALO_MACOS_RECOMP_STATE_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* Install the one image-wide linear guest-memory offset and executable VA + * bounds returned by mac_guest_image_load/the ELF program headers. */ +int mac_recomp_state_set_image(ptrdiff_t memory_offset, uint32_t code_lo, + uint32_t code_hi); + +/* Reset the translator's per-native-thread x86/x87/SSE state before entering + * a translated guest thread. guest_esp is the guest stack pointer the runtime + * has prepared; fs_base is a guest VA, or zero when no guest FS/TIB is used. */ +void mac_recomp_state_initialize_thread(uint32_t guest_esp, uint32_t fs_base); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/port/macos/resources/HaloCombatEvolved.icns b/port/macos/resources/HaloCombatEvolved.icns new file mode 100644 index 0000000000..338564f7a2 Binary files /dev/null and b/port/macos/resources/HaloCombatEvolved.icns differ diff --git a/port/macos/resources/macos_app_launcher.m b/port/macos/resources/macos_app_launcher.m new file mode 100644 index 0000000000..3090d02145 --- /dev/null +++ b/port/macos/resources/macos_app_launcher.m @@ -0,0 +1,237 @@ +#import +#import +#include +#include +#include +#include +#include +#include +#include + +static BOOL checking; +static BOOL diagnosticMode; +static int instanceLock = -1; + +static int fail(NSString *message) +{ + if (instanceLock >= 0) { + flock(instanceLock, LOCK_UN); + close(instanceLock); + instanceLock = -1; + } + fprintf(stderr, "%s\n", [message UTF8String]); + if (!checking) { + [NSApplication sharedApplication]; + [NSApp setActivationPolicy:NSApplicationActivationPolicyAccessory]; + [NSApp activate]; + NSAlert *alert = [[[NSAlert alloc] init] autorelease]; + [alert setMessageText:@"Halo Combat Evolved could not start"]; + [alert setInformativeText:message]; + [alert runModal]; + } + return 2; +} + +static NSString *sha256(NSString *path) +{ + FILE *file = fopen([path fileSystemRepresentation], "rb"); + if (!file) return nil; +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + CC_SHA256_CTX hash; + CC_SHA256_Init(&hash); + unsigned char buffer[65536], digest[CC_SHA256_DIGEST_LENGTH]; + size_t count; + while ((count = fread(buffer, 1, sizeof(buffer), file)) != 0) + CC_SHA256_Update(&hash, buffer, (CC_LONG)count); + BOOL okay = !ferror(file); + fclose(file); + if (!okay) return nil; + CC_SHA256_Final(digest, &hash); +#pragma clang diagnostic pop + NSMutableString *result = [NSMutableString string]; + for (NSUInteger i = 0; i < sizeof(digest); ++i) + [result appendFormat:@"%02x", digest[i]]; + return result; +} + +static BOOL mkdirs(NSString *path) +{ + return [[NSFileManager defaultManager] createDirectoryAtPath:path + withIntermediateDirectories:YES attributes:nil error:nil]; +} + +static NSString *absolutePath(NSString *path) +{ + path = [path stringByExpandingTildeInPath]; + if (![path isAbsolutePath]) + path = [[[NSFileManager defaultManager] currentDirectoryPath] stringByAppendingPathComponent:path]; + return [path stringByStandardizingPath]; +} + +int main(int argc, char **argv) +{ + @autoreleasepool { + for (int i = 1; i < argc; ++i) + if (!strcmp(argv[i], "--check")) checking = YES; + NSString *state = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/Halo Native AOT"]; + NSString *assetOverride = nil; + for (int i = 1; i < argc; ++i) { + if (!strcmp(argv[i], "--check")) checking = YES; + else if (!strcmp(argv[i], "--diagnostic")) diagnosticMode = YES; + else if ((!strcmp(argv[i], "--state-root") || !strcmp(argv[i], "--assets")) && i + 1 < argc) { + BOOL isState = !strcmp(argv[i], "--state-root"); + NSString *value = absolutePath([NSString stringWithUTF8String:argv[++i]]); + if (isState) state = value; else assetOverride = value; + } else if (!strncmp(argv[i], "-psn_", 5)) continue; + else return fail(@"Unknown startup option. Supported options: --check, --assets PATH, --state-root PATH."); + } + NSBundle *bundle = [NSBundle mainBundle]; + NSString *resources = [bundle resourcePath]; + NSString *engine = [[bundle executablePath] stringByDeletingLastPathComponent]; + engine = [engine stringByAppendingPathComponent:@"halo_engine"]; + NSString *elf = [resources stringByAppendingPathComponent:@"halo_guest.elf"]; + NSString *manifestPath = [resources stringByAppendingPathComponent:@"launch-config.json"]; + NSData *manifestData = [NSData dataWithContentsOfFile:manifestPath]; + NSDictionary *manifest = manifestData ? [NSJSONSerialization JSONObjectWithData:manifestData options:0 error:nil] : nil; + if (![manifest isKindOfClass:[NSDictionary class]]) return fail(@"The application is missing its launch manifest. Rebuild or reinstall it."); + for (NSString *key in @[@"binary_sha256", @"elf_sha256"]) { + NSString *path = [key isEqualToString:@"binary_sha256"] ? engine : elf; + NSString *actual = sha256(path); + if (!actual || ![actual isEqual:[manifest objectForKey:key]]) + return fail([NSString stringWithFormat:@"The bundled %@ differs from its verified package. Rebuild or reinstall the application.", [path lastPathComponent]]); + } + if (assetOverride && !diagnosticMode) return fail(@"External --assets is available only with explicit --diagnostic. Normal startup uses bundled game data."); + NSString *bundledAssets = [resources stringByAppendingPathComponent:@"GameData"]; + NSString *assets = assetOverride ?: bundledAssets; + BOOL bundledData = assetOverride == nil; + if (bundledData) { + NSData *sealData = [NSData dataWithContentsOfFile:[assets stringByAppendingPathComponent:@"asset-manifest.json"]]; + NSDictionary *seal = sealData ? [NSJSONSerialization JSONObjectWithData:sealData options:0 error:nil] : nil; + NSArray *entries = [seal isKindOfClass:[NSDictionary class]] ? [seal objectForKey:@"assets"] : nil; + if (![seal isKindOfClass:[NSDictionary class]] || ![entries isKindOfClass:[NSArray class]] || ![entries count]) + return fail(@"Bundled game data is missing its asset manifest. Rebuild the app from your own disc image."); + if (![sha256([assets stringByAppendingPathComponent:@"asset-manifest.json"]) isEqual:[manifest objectForKey:@"asset_manifest_sha256"]]) + return fail(@"The bundled asset manifest differs from its package provenance."); + for (NSDictionary *entry in entries) { + if (![entry isKindOfClass:[NSDictionary class]]) return fail(@"Invalid bundled asset manifest entry."); + NSString *relative = [entry objectForKey:@"path"]; + if (![relative isKindOfClass:[NSString class]] || [relative isAbsolutePath] || [[relative pathComponents] containsObject:@".."]) + return fail(@"Invalid bundled asset manifest path."); + NSString *path = [assets stringByAppendingPathComponent:relative]; + NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil]; + if (![[attributes objectForKey:NSFileType] isEqual:NSFileTypeRegular] || ![[attributes objectForKey:NSFileSize] isEqual:[entry objectForKey:@"bytes"]]) + return fail([NSString stringWithFormat:@"Bundled game asset %@ is missing or differs in size. Rebuild the app.", relative]); + if (checking && ![[entry objectForKey:@"sha256"] isEqual:sha256(path)]) + return fail([NSString stringWithFormat:@"Bundled game asset %@ differs from its recorded hash. Rebuild the app.", relative]); + } + } + NSString *maps = [assets stringByAppendingPathComponent:@"maps"]; + NSString *movies = [assets stringByAppendingPathComponent:@"bink"]; + NSFileManager *files = [NSFileManager defaultManager]; + if (![files fileExistsAtPath:movies]) movies = [assets stringByAppendingPathComponent:@"bink-source"]; + if (![files fileExistsAtPath:maps] || ![files fileExistsAtPath:movies]) + return fail([NSString stringWithFormat:@"Game maps or movies are missing at %@. Rebuild the app from a supported Xbox image.", assets]); + NSString *data = [state stringByAppendingPathComponent:@"data"]; + NSString *save = [state stringByAppendingPathComponent:@"save"]; + NSString *icon = [resources stringByAppendingPathComponent:@"HaloCombatEvolved.icns"]; + if (checking) { + NSDictionary *report = @{ @"binary":engine, @"elf":elf, @"assets":assets, @"data_root":data, + @"save_root":save, @"config":[save stringByAppendingPathComponent:@"config.toml"], + @"icon":icon, @"assets_in_bundle":@(bundledData), @"asset_hashes_verified":@(bundledData && checking), @"verified":@YES, @"launches_game":@NO, @"diagnostic_mode":@(diagnosticMode) }; + NSData *json = [NSJSONSerialization dataWithJSONObject:report options:NSJSONWritingPrettyPrinted error:nil]; + fwrite([json bytes], 1, [json length], stdout); fputc('\n', stdout); + return 0; + } + if (!mkdirs(state)) return fail(@"The user Application Support directory cannot be created."); + NSString *lockPath = [state stringByAppendingPathComponent:@"instance.lock"]; + int lock = open([lockPath fileSystemRepresentation], O_RDWR | O_CREAT, 0600); + if (lock < 0) return fail(@"The application instance lock cannot be opened."); + if (lock < 3) { + int retained = fcntl(lock, F_DUPFD, 3); + close(lock); + if (retained < 0) return fail(@"The application instance lock cannot be retained."); + lock = retained; + } + if (flock(lock, LOCK_EX | LOCK_NB) != 0) { + int lockError = errno; + if (lockError != EWOULDBLOCK && lockError != EAGAIN) { + close(lock); + return fail(@"The application instance lock cannot be acquired."); + } + char pidText[32] = {0}; + pread(lock, pidText, sizeof(pidText) - 1, 0); + pid_t pid = (pid_t)strtol(pidText, NULL, 10); + NSRunningApplication *existing = pid > 0 ? [NSRunningApplication runningApplicationWithProcessIdentifier:pid] : nil; + if (existing) [existing activateWithOptions:0]; + close(lock); + if (!existing) return fail(@"Halo is already running. Close the current game before starting another instance."); + return 0; + } + instanceLock = lock; + /* This descriptor intentionally survives exec: the actual game owns the lock. */ + char pidText[32]; + int pidLength = snprintf(pidText, sizeof(pidText), "%ld\n", (long)getpid()); + if (ftruncate(lock, 0) || pwrite(lock, pidText, (size_t)pidLength, 0) != pidLength) + return fail(@"The application instance lock cannot be updated."); + if (!mkdirs(data) || !mkdirs(save) || !mkdirs([state stringByAppendingPathComponent:@"build/macos-aot/run-data"])) + return fail(@"The game data and save directories cannot be created."); + NSString *configPath = [save stringByAppendingPathComponent:@"config.toml"]; + int config = open([configPath fileSystemRepresentation], O_WRONLY | O_CREAT | O_EXCL, 0600); + if (config >= 0) { + NSData *starter = [NSData dataWithContentsOfFile:[resources stringByAppendingPathComponent:@"starter-config.toml"]]; + if (!starter || write(config, [starter bytes], [starter length]) != (ssize_t)[starter length]) { + close(config); + unlink([configPath fileSystemRepresentation]); + return fail(@"The initial game settings cannot be written."); + } + close(config); + } else if (errno != EEXIST) return fail(@"The game settings file cannot be opened."); + if ([files fileExistsAtPath:[data stringByAppendingPathComponent:@"init.txt"]]) + return fail(@"The normal game data folder contains a test init.txt. Remove that test override before normal startup."); + NSMutableArray *assetDirectories = [NSMutableArray arrayWithArray:@[@"maps", @"bink"]]; + for (NSString *name in [files contentsOfDirectoryAtPath:assets error:nil]) + if ([name hasPrefix:@"maps_"]) [assetDirectories addObject:name]; + for (NSString *oldName in [files contentsOfDirectoryAtPath:data error:nil]) { + if (![oldName hasPrefix:@"maps_"] || [assetDirectories containsObject:oldName]) continue; + NSString *oldPath = [data stringByAppendingPathComponent:oldName]; + if ([files destinationOfSymbolicLinkAtPath:oldPath error:nil]) { + if (![files removeItemAtPath:oldPath error:nil]) return fail(@"An obsolete language-data link cannot be removed."); + } else return fail(@"A language-data directory in writable state conflicts with the bundled assets. Preserve/move it before starting the app."); + } + for (NSString *name in assetDirectories) { + NSString *destination = [data stringByAppendingPathComponent:name]; + NSString *source = [name isEqualToString:@"bink"] ? movies : [assets stringByAppendingPathComponent:name]; + NSString *oldLink = [files destinationOfSymbolicLinkAtPath:destination error:nil]; + if (!oldLink && [files fileExistsAtPath:destination]) return fail(@"A writable-state asset directory conflicts with the bundle overlay. Preserve/move that directory before starting the app."); + if (oldLink && ![oldLink isEqual:source]) { + if (![files removeItemAtPath:destination error:nil]) return fail(@"A game data link cannot be updated."); + } + if (![files fileExistsAtPath:destination] && ![files createSymbolicLinkAtPath:destination withDestinationPath:source error:nil]) + return fail(@"A game data link cannot be created."); + } + NSDictionary *environment = [[NSProcessInfo processInfo] environment]; + NSSet *diagnostic = [NSSet setWithArray:@[@"HALO_EXIT_AFTER", @"HALO_TRACE_RT_ALLOC", @"HALO_FRAME_METRICS", + @"HALO_UI_POINTER_TRACE", @"HALO_INPUT_TRACE", @"HALO_FINGERPRINT_METRICS", @"HALO_AUDIO_EVIDENCE", @"HALO_BINK_FRAME_TRACE", @"HALO_SDL_SWAP_TRACE", @"HALO_GL_FENCE_TRACE"]]; + for (NSString *key in environment) { + BOOL permittedDiagnostic = diagnosticMode && ([key hasPrefix:@"HALO_SDL_TEST_"] || [key hasPrefix:@"HALO_CAPTURE_"] || [key isEqualToString:@"HALO_FRAME_METRICS"] || [key isEqualToString:@"HALO_INPUT_TRACE"] || [key isEqualToString:@"HALO_UI_POINTER_TRACE"]); + if (!permittedDiagnostic && ([key hasPrefix:@"HALO_TEST_"] || [key hasPrefix:@"HALO_SDL_TEST_"] || [key hasPrefix:@"HALO_CAPTURE_"] || [diagnostic containsObject:key])) + unsetenv([key UTF8String]); + } + NSDictionary *settings = @{ @"HALO_GUEST_ELF":elf, @"HALO_DATA_ROOT":data, @"HALO_SAVE_ROOT":save, + @"HALO_CONFIG_ROOT":save, @"HALO_PROJECT_ROOT":state, @"HALO_AUDIO_ENABLE":@"1", @"HALO_APP_ICON":icon }; + for (NSString *key in settings) + if (setenv([key UTF8String], [[settings objectForKey:key] fileSystemRepresentation], 1) != 0) + return fail(@"The game environment cannot be configured."); + NSString *logPath = [state stringByAppendingPathComponent:@"halo.log"]; + int log = open([logPath fileSystemRepresentation], O_WRONLY | O_CREAT | O_TRUNC, 0600); + if (log < 0 || dup2(log, STDOUT_FILENO) < 0 || dup2(log, STDERR_FILENO) < 0) + return fail(@"The game log cannot be opened."); + if (log > STDERR_FILENO) close(log); + if (chdir([state fileSystemRepresentation]) != 0) return fail(@"The writable game directory cannot be opened."); + fprintf(stderr, "[launcher] verified bundled engine; data=%s save=%s\n", [data fileSystemRepresentation], [save fileSystemRepresentation]); + char *arguments[] = {(char *)[engine fileSystemRepresentation], (char *)[elf fileSystemRepresentation], NULL}; + execv(arguments[0], arguments); + return fail([NSString stringWithFormat:@"The bundled game could not execute: %s", strerror(errno)]); + } +} diff --git a/port/macos/resources/master-chief-mark-v-icon.png b/port/macos/resources/master-chief-mark-v-icon.png new file mode 100644 index 0000000000..d99fe7d8fd Binary files /dev/null and b/port/macos/resources/master-chief-mark-v-icon.png differ diff --git a/port/macos/resources/master-chief-mark-v-icon.prompt.txt b/port/macos/resources/master-chief-mark-v-icon.prompt.txt new file mode 100644 index 0000000000..b926893cea --- /dev/null +++ b/port/macos/resources/master-chief-mark-v-icon.prompt.txt @@ -0,0 +1,12 @@ +Generated with built-in image_gen.imagegen; transparent_background=true. +Reference: https://www.halowaypoint.com/news/master-class +Reference image: https://content.halocdn.com/media/Default/community/CANON_FODDER_Franchise/cf_ce-chief-sc-7ee8f83847a6425699d6eda3e5d7eea8.jpg + +Use case: stylized-concept +Asset type: polished macOS Dock application icon, square transparent PNG. +Primary request: create an accurately recognizable classic Master Chief MJOLNIR Mark V helmet from Halo: Combat Evolved (2001), helmet only, on a genuinely transparent background. +Input image: official Halo Waypoint original Halo Combat Evolved screenshot is a reference image for helmet identity and geometry only. Preserve the classic broad angular green brow shelf with two pointed outer corners, flattened polygonal crown, wide uninterrupted wraparound gold visor, angular olive green lower face shield, two small paired circular side fittings and inset horizontal black vent slots beside the chin. This is the original CE Mark V silhouette. +Style/medium: exceptionally clean premium 3D app icon rendering with crisp faceted military armor and simplified details that stay legible in a small Dock; subtle realistic material texture, luminous amber gold visor, rich olive green armor. +Composition/framing: centered symmetrical front view helmet only; square canvas; helmet occupies about 84 percent of canvas height; complete silhouette visible with balanced transparent margin. No shoulders or neck. +Lighting/mood: controlled studio highlights define the green armor and gold visor; confident and iconic. +Constraints: actual transparent alpha around helmet; preserve accurate original CE Mark V design; all shading contained inside helmet silhouette; no rounded rectangle app tile; no floor; no external shadow; no background; no glow outside silhouette; no lettering, logo, badge, watermark, weapons, extra objects, or scene. Do not substitute a Halo 2/3/Infinite Mark VI helmet. diff --git a/port/macos/resources/starter-config.toml b/port/macos/resources/starter-config.toml new file mode 100644 index 0000000000..826b61588b --- /dev/null +++ b/port/macos/resources/starter-config.toml @@ -0,0 +1,22 @@ +# Halo Combat Evolved settings +# F10 cycles 4:3 (640x480), 720p and 1080p. F11 switches fullscreen. +# F12 releases or recaptures the mouse. +# Edit while the game is closed; existing settings are never replaced on install. + +[display] +resolution = "1280x720" +fullscreen = false +window_scale = 1 +# Cap fullscreen rendering at 1280 pixels wide while preserving display shape. +# Use 0 for native display resolution, or another width of at least 640. +render_width = 1280 +vsync = true +interpolation = true + +[audio] +enabled = true +volume = 1.0 + +[input] +mouse_sensitivity = 1.0 +invert_mouse = false diff --git a/port/macos/setup.sh b/port/macos/setup.sh new file mode 100755 index 0000000000..dd4ed2f0df --- /dev/null +++ b/port/macos/setup.sh @@ -0,0 +1,5 @@ +#!/bin/bash +set -euo pipefail +port_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +source_root=$(cd "$port_dir/../.." && pwd) +exec bash "$port_dir/tooling/setup.sh" "$@" --source-checkout "$source_root" diff --git a/port/macos/tests/gamepad_virtual_test.inc b/port/macos/tests/gamepad_virtual_test.inc new file mode 100644 index 0000000000..7ab4b63878 --- /dev/null +++ b/port/macos/tests/gamepad_virtual_test.inc @@ -0,0 +1,67 @@ +#undef SDL_GetGamepadFromID +#undef SDL_OpenGamepad +#undef SDL_GetGamepadAxis +#undef SDL_GetGamepadButton +static SDL_Event event_buffer; +static int rumble_calls; +static int notify_gamepad_events=1; +static bool rumble(void *data,Uint16 low,Uint16 high){(void)data;assert(low==1234&&high==5678);rumble_calls++;return true;} +static SDL_JoystickID attach(bool sony,bool motors) +{ + SDL_VirtualJoystickDesc desc; SDL_INIT_INTERFACE(&desc); + desc.type=SDL_JOYSTICK_TYPE_GAMEPAD;desc.naxes=6;desc.nbuttons=SDL_GAMEPAD_BUTTON_COUNT; + desc.vendor_id=sony?0x054c:0x045e;desc.product_id=sony?0x0ce6:0x028e; + desc.name=sony?"Fixture DualSense":"Fixture Xbox 360"; + desc.axis_mask=(1u<=0);return id; +} +static void pump(void){(void)notify_gamepad_events;SDL_UpdateJoysticks();while(mac_host_sdl_poll_event(0x1000)){ +#ifdef HALO_GAMEPAD_CACHE_TEST + if(notify_gamepad_events && (event_buffer.type==SDL_EVENT_GAMEPAD_ADDED || event_buffer.type==SDL_EVENT_GAMEPAD_REMOVED)) halo_input_gamepads_changed(); +#endif + } } +static void neutral(SDL_Joystick *j){for(int i=0;i<6;i++)assert(SDL_SetJoystickVirtualAxis(j,i,i>=4?-32768:0));for(int i=0;i0&&fix_dead_zone(-9010,9000)<0);assert(fix_dead_zone(32767,9000)==32767&&fix_dead_zone(-32768,9000)==-32768); + /* Remove while every button/stick/trigger is held. Slot one must not move. */ + assert(SDL_DetachVirtualJoystick(xbox));pump();assert(sdl_gamepads(pads)==2&&!pads[0]&&(uint32_t)(uintptr_t)pads[1]==dt);assert(connected_gamepads()==3); + assert(!mac_host_sdl_gamepad_button(xt,SDL_GAMEPAD_BUTTON_SOUTH));assert(!mac_host_sdl_gamepad_axis(xt,SDL_GAMEPAD_AXIS_LEFTX));assert(!mac_host_sdl_rumble_gamepad(xt,1234,5678,100));SDL_CloseJoystick(x); + SDL_JoystickID replacement=attach(false,true);SDL_Joystick *r=SDL_OpenJoystick(replacement);neutral(r);assert(sdl_gamepads(pads)==2&&pads[0]&&(uint32_t)(uintptr_t)pads[0]!=xt&&(uint32_t)(uintptr_t)pads[1]==dt); + assert(SDL_SetJoystickVirtualButton(d,SDL_GAMEPAD_BUTTON_START,true));assert(SDL_SetJoystickVirtualButton(d,SDL_GAMEPAD_BUTTON_DPAD_DOWN,true));pump();XINPUT_GAMEPAD menu=state(pads[1]);assert(menu.wButtons==(XINPUT_GAMEPAD_START|XINPUT_GAMEPAD_DPAD_DOWN)); + notify_gamepad_events=0; /* missing notification must still recover stale tokens */ + assert(SDL_DetachVirtualJoystick(sony));assert(SDL_DetachVirtualJoystick(replacement));pump();SDL_CloseJoystick(d);SDL_CloseJoystick(r);assert(sdl_gamepads(pads)==0&&connected_gamepads()==1);SDL_Quit();mac_guest_address_reset(); + puts("Virtual Xbox/DualSense native-token/source-XInput ABI mapping, neutral/full inputs, menu, held disconnect, stable reconnect slots, truthful rumble: PASS");return 0; +} diff --git a/port/macos/tests/gl41_compat_test.c b/port/macos/tests/gl41_compat_test.c new file mode 100644 index 0000000000..5c3c0d45f3 --- /dev/null +++ b/port/macos/tests/gl41_compat_test.c @@ -0,0 +1,125 @@ +#include "../gl41_compat.h" + +#include +/* The generated guest alias has this exact public name. Its coexistence must + * never redirect the native helper back into the guest import dispatcher. */ +void halo_gl41_copy_image_2d(void) +{ + assert(!"native copy called the guest import alias"); +} +#include + +struct mock_gl +{ + int32_t read_binding, draw_binding; + int scissor; + int complete; + int blits; + int32_t width, height, source_level, destination_level; + uint32_t source, destination; +}; + +static struct mock_gl mock; + +static void get_integer(uint32_t name, int32_t *value) +{ + if (name == 0x8CAA) *value = mock.read_binding; + else if (name == 0x8CA6) *value = mock.draw_binding; + else assert(0); +} + +static uint8_t is_enabled(uint32_t capability) +{ + assert(capability == 0x0C11); + return (uint8_t)mock.scissor; +} + +static void enable(uint32_t capability) +{ + assert(capability == 0x0C11); + mock.scissor = 1; +} + +static void disable(uint32_t capability) +{ + assert(capability == 0x0C11); + mock.scissor = 0; +} + +static void gen_framebuffers(int32_t count, uint32_t *framebuffers) +{ + assert(count == 2); + framebuffers[0] = 40; + framebuffers[1] = 41; +} + +static void bind_framebuffer(uint32_t target, uint32_t framebuffer) +{ + if (target == 0x8CA8) mock.read_binding = (int32_t)framebuffer; + else if (target == 0x8CA9) mock.draw_binding = (int32_t)framebuffer; + else assert(0); +} + +static void framebuffer_texture_2d(uint32_t target, uint32_t attachment, + uint32_t texture_target, uint32_t texture, int32_t level) +{ + assert(target == 0x8CA8 || target == 0x8CA9); + assert(attachment == 0x8CE0 && texture_target == 0x0DE1); + if (target == 0x8CA8) + { + mock.source = texture; + mock.source_level = level; + } + else + { + mock.destination = texture; + mock.destination_level = level; + } +} + +static void read_buffer(uint32_t source) { assert(source == 0x8CE0); } +static void draw_buffer(uint32_t destination) { assert(destination == 0x8CE0); } +static uint32_t check_framebuffer_status(uint32_t target) +{ + assert(target == 0x8CA8 || target == 0x8CA9); + return mock.complete ? 0x8CD5 : 0; +} + +static void blit_framebuffer(int32_t sx0, int32_t sy0, int32_t sx1, int32_t sy1, + int32_t dx0, int32_t dy0, int32_t dx1, int32_t dy1, uint32_t mask, uint32_t filter) +{ + assert(sx0 == 0 && sy0 == 0 && dx0 == 0 && dy0 == 0); + assert(sx1 == dx1 && sy1 == dy1); + assert(mask == 0x4000 && filter == 0x2600); + mock.width = sx1; + mock.height = sy1; + mock.blits++; +} + +int main(void) +{ + const struct halo_gl41_copy_api api = { + get_integer, is_enabled, enable, disable, gen_framebuffers, + bind_framebuffer, framebuffer_texture_2d, read_buffer, draw_buffer, + check_framebuffer_status, blit_framebuffer, + }; + struct halo_gl41_copy_state state = { 0, 0 }; + + mock = (struct mock_gl){ 3, 7, 1, 1, 0, 0, 0, 0, 0, 0, 0 }; + assert(mac_gl41_copy_image_2d(&api, &state, 12, 2, 19, 3, 128, 64)); + assert(state.read_framebuffer == 40 && state.draw_framebuffer == 41); + assert(mock.source == 12 && mock.source_level == 2); + assert(mock.destination == 19 && mock.destination_level == 3); + assert(mock.width == 128 && mock.height == 64 && mock.blits == 1); + assert(mock.read_binding == 3 && mock.draw_binding == 7 && mock.scissor == 1); + + mock.complete = 0; + mock.scissor = 0; + assert(!mac_gl41_copy_image_2d(&api, &state, 12, 0, 19, 0, 32, 32)); + assert(mock.blits == 1); + assert(mock.read_binding == 3 && mock.draw_binding == 7 && mock.scissor == 0); + assert(!mac_gl41_copy_image_2d(&api, &state, 0, 0, 19, 0, 32, 32)); + + puts("OpenGL 4.1 FBO-copy fallback: state-preservation cases passed"); + return 0; +} diff --git a/port/macos/tests/gl_bridge_link_test.c b/port/macos/tests/gl_bridge_link_test.c new file mode 100644 index 0000000000..65d0b48c74 --- /dev/null +++ b/port/macos/tests/gl_bridge_link_test.c @@ -0,0 +1,108 @@ +#include "../gl_bridge_support.h" +#include "../gl_host_imports.h" +#include "../gl_native_proc.h" +#include "../gl_token_registry.h" +#include "../guest_address.h" +#include "../guest_allocator.h" +#include "../guest_call.h" + +#include +#include +#include +#include + +_Thread_local uint32_t g_eax; +_Thread_local uint32_t g_esp; + +#define TEST_GUEST_BASE 0x10000u +#define TEST_GUEST_SIZE 0x10000u + +static uint8_t guest_backing[TEST_GUEST_SIZE]; +static uint32_t allocation_cursor = TEST_GUEST_BASE + 0x8000u; +static uint32_t import_esp = TEST_GUEST_BASE + 0x400u; + +static int test_allocate(void *context, size_t size, size_t alignment, + uint32_t *guest_va_out) +{ + (void)context; + if (!size || !alignment || !guest_va_out) + return -1; + uint64_t address = ((uint64_t)allocation_cursor + alignment - 1) & + ~((uint64_t)alignment - 1); + if (address + size > TEST_GUEST_BASE + TEST_GUEST_SIZE) + return -1; + *guest_va_out = (uint32_t)address; + allocation_cursor = (uint32_t)(address + size); + return 0; +} + +static void test_release(void *context, uint32_t guest_va, size_t size) +{ + (void)context; + (void)guest_va; + (void)size; +} + +uint32_t mac_guest_host_import_arg32(uint32_t index) +{ + uint32_t value = 0; + assert(mac_guest_read(import_esp + 4u + index * 4u, &value, sizeof(value)) == 0); + return value; +} + +void mac_guest_host_import_return32(uint32_t value) +{ + g_eax = value; + import_esp += 4; +} + +void mac_guest_host_import_return_void(void) +{ + import_esp += 4; +} + +void mac_host_abort(const char *message) +{ + (void)message; + abort(); +} + +int main(void) +{ + const struct mac_guest_allocator allocator = { + NULL, test_allocate, test_release + }; + const uint32_t known_name_va = TEST_GUEST_BASE + 0x100u; + const uint32_t unknown_name_va = TEST_GUEST_BASE + 0x120u; + const uint32_t proc_name_va = TEST_GUEST_BASE + 0x140u; + const uint32_t get_string_args[] = { 0x1F02u }; + uint32_t version_va; + + assert(mac_guest_address_register(TEST_GUEST_BASE, guest_backing, + sizeof(guest_backing)) == 0); + memcpy(guest_backing + (known_name_va - TEST_GUEST_BASE), "glViewport", sizeof("glViewport")); + memcpy(guest_backing + (unknown_name_va - TEST_GUEST_BASE), "glNoSuchCall", sizeof("glNoSuchCall")); + memcpy(guest_backing + (proc_name_va - TEST_GUEST_BASE), "glViewport", sizeof("glViewport")); + assert(mac_guest_allocator_install(&allocator) == 0); + assert(mac_macos_gl_token_for_guest_name(known_name_va) == 0xFE100080u); + assert(mac_macos_gl_token_for_guest_name(unknown_name_va) == 0); + assert(mac_macos_gl_wrapper_for_token(0xFE100080u) != NULL); + assert(mac_macos_gl_wrapper_for_token(0xFEFFFFFFu) == NULL); + assert(mac_macos_gl_native_proc("glGetString") == NULL); + mac_guest_import_bridge proc_bridge = + mac_macos_gl_host_import_bridge("guest_gl_get_proc_address"); + assert(proc_bridge != NULL); + assert(mac_macos_gl_host_import_bridge("halo_gl41_copy_image_2d") != NULL); + assert(mac_macos_gl_host_import_bridge("host_gl_get_string") != NULL); + assert(mac_macos_gl_host_import_bridge("unknown_gl_import") == NULL); + memcpy(guest_backing + (import_esp + 4u - TEST_GUEST_BASE), &proc_name_va, sizeof(proc_name_va)); + proc_bridge(); + assert(g_eax == 0xFE100080u && import_esp == TEST_GUEST_BASE + 0x404u); + version_va = mac_macos_gl_return_guest_pointer("glGetString", "4.1 test", + get_string_args, 1); + assert(version_va != 0); + assert(strcmp((const char *)mac_guest_address_resolve(version_va, sizeof("4.1 test")), + "4.1 test") == 0); + mac_macos_gl_native_proc_clear(); + return 0; +} diff --git a/port/macos/tests/gl_bridge_support_test.c b/port/macos/tests/gl_bridge_support_test.c new file mode 100644 index 0000000000..772b68d9cb --- /dev/null +++ b/port/macos/tests/gl_bridge_support_test.c @@ -0,0 +1,162 @@ +#include "../gl_bridge_support.h" +#include "../gl_token_registry.h" +#include "../guest_address.h" + +#include + +#include +#include +#include +#include + +#define GUEST_BASE 0x2000u +#define GUEST_SIZE 0x8000u + +static uint8_t guest_memory[GUEST_SIZE]; +static uint32_t next_guest_alloc = GUEST_BASE + 0x3000; +static GLint pack_row_length = 0; +static GLint pack_skip_pixels = 0; +static GLint pack_skip_rows = 0; +static GLint array_buffer_binding = 0; +static GLint element_buffer_binding = 0; + +static void APIENTRY fake_get_integer(GLenum name, GLint *value) +{ + switch (name) + { + case GL_PACK_ALIGNMENT: *value = 4; break; + case GL_UNPACK_ALIGNMENT: *value = 4; break; + case GL_PACK_ROW_LENGTH: *value = pack_row_length; break; + case GL_PACK_IMAGE_HEIGHT: *value = 0; break; + case GL_PACK_SKIP_PIXELS: *value = pack_skip_pixels; break; + case GL_PACK_SKIP_ROWS: *value = pack_skip_rows; break; + case GL_PACK_SKIP_IMAGES: *value = 0; break; + case GL_PIXEL_PACK_BUFFER_BINDING: *value = 0; break; + case GL_PIXEL_UNPACK_BUFFER_BINDING: *value = 0; break; + case GL_ARRAY_BUFFER_BINDING: *value = array_buffer_binding; break; + case GL_ELEMENT_ARRAY_BUFFER_BINDING: *value = element_buffer_binding; break; + default: *value = 0; break; + } +} + +void *mac_macos_gl_native_proc(const char *name) +{ + return strcmp(name, "glGetIntegerv") == 0 ? (void *)fake_get_integer : NULL; +} + +uint32_t mac_macos_gl_token_for_name(const char *name) +{ + return name && strcmp(name, "glViewport") == 0 ? 0xFE100080u : 0; +} + +void *mac_guest_address_resolve(uint32_t guest_address, size_t size) +{ + if (guest_address < GUEST_BASE || size > GUEST_SIZE || + (uint64_t)guest_address + size > (uint64_t)GUEST_BASE + GUEST_SIZE) + return NULL; + return guest_memory + (guest_address - GUEST_BASE); +} + +int mac_guest_address_from_host(const void *host_address, size_t size, + uint32_t *guest_address_out) +{ + uintptr_t base = (uintptr_t)guest_memory, pointer = (uintptr_t)host_address; + if (!guest_address_out || pointer < base || size > GUEST_SIZE || + pointer - base > GUEST_SIZE - size) + return -1; + *guest_address_out = GUEST_BASE + (uint32_t)(pointer - base); + return 0; +} + +int mac_guest_read(uint32_t guest_address, void *destination, size_t size) +{ + void *source = mac_guest_address_resolve(guest_address, size); + if (!source || !destination) return -1; + memcpy(destination, source, size); + return 0; +} + +int mac_guest_allocate(size_t size, size_t alignment, uint32_t *guest_va_out) +{ + if (!size || !guest_va_out || !alignment || (alignment & (alignment - 1))) return -1; + uint64_t aligned = ((uint64_t)next_guest_alloc + alignment - 1) & ~(uint64_t)(alignment - 1); + if (aligned + size > (uint64_t)GUEST_BASE + GUEST_SIZE) return -1; + *guest_va_out = (uint32_t)aligned; + next_guest_alloc = (uint32_t)(aligned + size); + return 0; +} + +static void put_guest(uint32_t va, const void *value, size_t size) +{ + void *destination = mac_guest_address_resolve(va, size); + assert(destination); + memcpy(destination, value, size); +} + +int main(void) +{ + assert(mac_macos_gl_pixel_span(10, 4, GL_RGBA, GL_UNSIGNED_BYTE, 4) == 160); + assert(mac_macos_gl_pixel_span(3, 2, GL_RGB, GL_UNSIGNED_BYTE, 4) == 21); + assert(mac_macos_gl_pixel_span(3, 2, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 4) == 14); + assert(mac_macos_gl_pixel_span(3, 2, GL_RGBA, GL_FLOAT, 8) == 96); + assert(mac_macos_gl_pixel_span(0, 1, GL_RGBA, GL_UNSIGNED_BYTE, 4) == 0); + assert(mac_macos_gl_pixel_span(1, 1, GL_BGRA, GL_UNSIGNED_BYTE, 3) == 0); + assert(mac_macos_gl_pixel_span(UINT32_MAX, UINT32_MAX, GL_RGBA, GL_FLOAT, 8) == 0); + + const uint32_t buffer_data_args[] = { GL_ARRAY_BUFFER, 16, GUEST_BASE + 0x100, GL_STATIC_DRAW }; + assert(mac_macos_gl_guest_pointer("glBufferData", 2, buffer_data_args[2], + buffer_data_args, 4) == guest_memory + 0x100); + + array_buffer_binding = 1; + const uint32_t vertex_args[] = { 0, 3, GL_FLOAT, GL_FALSE, 24, 0x28 }; + assert(mac_macos_gl_guest_pointer("glVertexAttribPointer", 5, 0x28, + vertex_args, 6) == (void *)(uintptr_t)0x28); + array_buffer_binding = 0; + element_buffer_binding = 1; + const uint32_t draw_args[] = { GL_TRIANGLES, 12, GL_UNSIGNED_SHORT, 0x40 }; + assert(mac_macos_gl_guest_pointer("glDrawElements", 3, 0x40, + draw_args, 4) == (void *)(uintptr_t)0x40); + element_buffer_binding = 0; + + pack_row_length = 4; + pack_skip_pixels = 1; + pack_skip_rows = 1; + const uint32_t read_args[] = { 0, 0, 3, 2, GL_RGB, GL_UNSIGNED_BYTE, GUEST_BASE }; + assert(mac_macos_gl_guest_pointer("glReadPixels", 6, GUEST_BASE, + read_args, 7) == guest_memory); + pack_row_length = pack_skip_pixels = pack_skip_rows = 0; + + const uint32_t first_string = GUEST_BASE + 0x300; + const uint32_t second_string = GUEST_BASE + 0x340; + const uint32_t strings_va = GUEST_BASE + 0x200; + const uint32_t lengths_va = GUEST_BASE + 0x240; + const uint32_t string_ptrs[] = { first_string, second_string }; + const int32_t string_lengths[] = { 3, -1 }; + put_guest(strings_va, string_ptrs, sizeof(string_ptrs)); + put_guest(lengths_va, string_lengths, sizeof(string_lengths)); + put_guest(first_string, "abcX", 4); + put_guest(second_string, "def\0", 4); + put_guest(GUEST_BASE + 0x380, "glViewport", sizeof("glViewport")); + put_guest(GUEST_BASE + 0x3A0, "glNotAFunction", sizeof("glNotAFunction")); + assert(mac_macos_gl_token_for_guest_name(GUEST_BASE + 0x380) == 0xFE100080u); + assert(mac_macos_gl_token_for_guest_name(GUEST_BASE + 0x3A0) == 0); + assert(mac_macos_gl_token_for_guest_name(GUEST_BASE + GUEST_SIZE) == 0); + const uint32_t shader_args[] = { 7, 2, strings_va, lengths_va }; + const GLchar **native_strings = (const GLchar **)mac_macos_gl_guest_pointer( + "glShaderSource", 2, strings_va, shader_args, 4); + assert(native_strings && strcmp(native_strings[0], "abcX") == 0 && + strcmp(native_strings[1], "def") == 0); + assert(mac_macos_gl_guest_pointer("glShaderSource", 3, lengths_va, + shader_args, 4) == guest_memory + 0x240); + + const uint32_t version_args[] = { GL_VERSION }; + const uint32_t version_va = mac_macos_gl_return_guest_pointer("glGetString", + "4.1 mock driver", version_args, 1); + const uint32_t renderer_args[] = { GL_RENDERER }; + const uint32_t renderer_va = mac_macos_gl_return_guest_pointer("glGetString", + "mock renderer", renderer_args, 1); + assert(version_va && renderer_va && version_va != renderer_va); + assert(strcmp((const char *)mac_guest_address_resolve(version_va, 15), "4.1 mock driver") == 0); + assert(strcmp((const char *)mac_guest_address_resolve(renderer_va, 14), "mock renderer") == 0); + return 0; +} diff --git a/port/macos/tests/gl_bridge_wrapper_test.c b/port/macos/tests/gl_bridge_wrapper_test.c new file mode 100644 index 0000000000..ad313217a9 --- /dev/null +++ b/port/macos/tests/gl_bridge_wrapper_test.c @@ -0,0 +1,110 @@ +#include + +#include +#include +#include +#include + +#include "guest_address.h" + +typedef void (*gl_guest_wrapper)(void); +extern uint32_t mac_macos_gl_token_for_name(const char *name); +extern gl_guest_wrapper mac_macos_gl_wrapper_for_token(uint32_t token); + +_Thread_local uint32_t g_eax; +_Thread_local uint32_t g_esp; +_Thread_local uint32_t g_ebp; +ptrdiff_t g_xbox_mem_offset; +static uint32_t guest_stack[16]; +static GLint observed_viewport[4]; +static uint32_t observed_gl_string_name; + +static void APIENTRY fake_glViewport(GLint x, GLint y, GLsizei width, GLsizei height) +{ + observed_viewport[0] = x; + observed_viewport[1] = y; + observed_viewport[2] = width; + observed_viewport[3] = height; +} + +static GLenum APIENTRY fake_glGetError(void) +{ + return GL_INVALID_ENUM; +} + +static const GLubyte *APIENTRY fake_glGetString(GLenum name) +{ + observed_gl_string_name = name; + return (const GLubyte *)"OpenGL 4.1 wrapper fixture"; +} + +void *mac_macos_gl_native_proc(const char *name) +{ + if (strcmp(name, "glViewport") == 0) return (void *)fake_glViewport; + if (strcmp(name, "glGetError") == 0) return (void *)fake_glGetError; + if (strcmp(name, "glGetString") == 0) return (void *)fake_glGetString; + return NULL; +} + +void *mac_macos_gl_guest_pointer(const char *name, unsigned arg_index, + uint32_t guest_va, const uint32_t *raw_args, unsigned arg_count) +{ + (void)name; (void)arg_index; (void)guest_va; (void)raw_args; (void)arg_count; + return NULL; +} + +uint32_t mac_macos_gl_return_guest_pointer(const char *name, const void *host_pointer, + const uint32_t *raw_args, unsigned arg_count) +{ + assert(strcmp(name, "glGetString") == 0); + assert(host_pointer != NULL && raw_args != NULL && arg_count == 1); + assert(raw_args[0] == GL_VERSION); + return 0x2000; +} + +int mac_guest_allocate(size_t size, size_t alignment, uint32_t *guest_va_out) +{ + (void)size; (void)alignment; (void)guest_va_out; + return 0; +} + +void mac_macos_gl_bridge_fault(const char *name, const char *reason) +{ + (void)name; (void)reason; + assert(!"unexpected bridge fault"); +} + +int main(void) +{ + gl_guest_wrapper wrapper; + uint32_t token = mac_macos_gl_token_for_name("glViewport"); + guest_stack[0] = UINT32_C(0xDEADBEEF); /* caller return VA */ + assert(mac_guest_address_register(0x1000, guest_stack, sizeof(guest_stack)) == 0); + assert(token == 0xFE100080u); + wrapper = mac_macos_gl_wrapper_for_token(token); + assert(wrapper != NULL); + g_esp = 0x1000; + guest_stack[1] = (uint32_t)-320; + guest_stack[2] = (uint32_t)-240; + guest_stack[3] = 1280; + guest_stack[4] = 720; + wrapper(); + assert(g_esp == 0x1004); + assert(observed_viewport[0] == -320 && observed_viewport[1] == -240 && + observed_viewport[2] == 1280 && observed_viewport[3] == 720); + wrapper = mac_macos_gl_wrapper_for_token(mac_macos_gl_token_for_name("glGetError")); + assert(wrapper != NULL); + g_esp = 0x1000; + wrapper(); + assert(g_eax == GL_INVALID_ENUM && g_esp == 0x1004); + wrapper = mac_macos_gl_wrapper_for_token(mac_macos_gl_token_for_name("glGetString")); + assert(wrapper != NULL); + g_esp = 0x1000; + guest_stack[1] = GL_VERSION; + observed_gl_string_name = 0; + wrapper(); + assert(observed_gl_string_name == GL_VERSION); + assert(g_eax == 0x2000 && g_esp == 0x1004); + mac_guest_address_reset(); + return 0; +} diff --git a/port/macos/tests/gl_copy_bridge_test.c b/port/macos/tests/gl_copy_bridge_test.c new file mode 100644 index 0000000000..81af13d895 --- /dev/null +++ b/port/macos/tests/gl_copy_bridge_test.c @@ -0,0 +1,184 @@ +#include "../gl_bridge_support.h" +#include "../gl_host_imports.h" +#include "../gl_native_proc.h" +#include "../gl_token_registry.h" +#include "../guest_address.h" +#include "../guest_allocator.h" +#include "../guest_call.h" + +#define GL_SILENCE_DEPRECATION 1 +#define GL_GLEXT_PROTOTYPES 1 +#include +#include +#include +#include +#include +#include +#include +#include + +extern _Thread_local uint32_t g_eax; +extern _Thread_local uint32_t g_esp; + +#define TEST_GUEST_BASE 0x10000u +#define TEST_GUEST_SIZE 0x10000u + +static uint8_t guest_backing[TEST_GUEST_SIZE]; +static uint32_t allocation_cursor = TEST_GUEST_BASE + 0x8000u; +static uint32_t import_esp = TEST_GUEST_BASE + 0x400u; + +static int test_allocate(void *context, size_t size, size_t alignment, + uint32_t *guest_va_out) +{ + (void)context; + if (!size || !alignment || !guest_va_out) + return -1; + uint64_t address = ((uint64_t)allocation_cursor + alignment - 1) & + ~((uint64_t)alignment - 1); + if (address + size > TEST_GUEST_BASE + TEST_GUEST_SIZE) + return -1; + *guest_va_out = (uint32_t)address; + allocation_cursor = (uint32_t)(address + size); + return 0; +} + +static void test_release(void *context, uint32_t guest_va, size_t size) +{ + (void)context; + (void)guest_va; + (void)size; +} + +uint32_t mac_guest_host_import_arg32(uint32_t index) +{ + uint32_t value = 0; + assert(mac_guest_read(import_esp + 4u + index * 4u, &value, sizeof(value)) == 0); + return value; +} + +void mac_guest_host_import_return32(uint32_t value) +{ + g_eax = value; + import_esp += 4; +} + +void mac_guest_host_import_return_void(void) +{ + import_esp += 4; +} + +void mac_host_abort(const char *message) +{ + (void)message; + abort(); +} + +/* Coexists with the generated guest alias; invoking this from the native bridge + * would reproduce the original recursive dispatch. */ +void halo_gl41_copy_image_2d(void) { assert(!"native bridge called guest alias"); } + +static void test_actual_copy_bridge(void) +{ + assert(SDL_Init(SDL_INIT_VIDEO)); + assert(SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4)); + assert(SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1)); + assert(SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE)); + SDL_Window *window = SDL_CreateWindow("Halo GL copy regression",64,64,SDL_WINDOW_OPENGL|SDL_WINDOW_HIDDEN); + assert(window); + SDL_GLContext context=SDL_GL_CreateContext(window); assert(context); + mac_macos_gl_native_proc_clear(); + GLuint textures[2], previous_fbos[2]; + glGenTextures(2,textures); glGenFramebuffers(2,previous_fbos); + unsigned char pixels[8*8*4], actual[sizeof pixels]; + for(unsigned i=0;i>mip, args[]={api_va,state_va,textures[0],mip,textures[1],mip,width,width}; + import_esp=TEST_GUEST_BASE+0x400; + assert(mac_guest_write(import_esp+4,args,sizeof args)==0); + mac_macos_gl_host_import_bridge("halo_gl41_copy_image_2d")(); + assert(g_eax==1 && import_esp==TEST_GUEST_BASE+0x404); + GLint read,draw; glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING,&read); glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING,&draw); + assert(read==(GLint)previous_fbos[0] && draw==(GLint)previous_fbos[1] && glIsEnabled(GL_SCISSOR_TEST)); + glBindFramebuffer(GL_READ_FRAMEBUFFER,previous_fbos[0]); + glFramebufferTexture2D(GL_READ_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D,textures[1],mip); + glReadBuffer(GL_COLOR_ATTACHMENT0); assert(glCheckFramebufferStatus(GL_READ_FRAMEBUFFER)==GL_FRAMEBUFFER_COMPLETE); + glReadPixels(0,0,width,width,GL_RGBA,GL_UNSIGNED_BYTE,actual); + assert(memcmp(actual,pixels,width*width*4)==0 && glGetError()==GL_NO_ERROR); + } + /* A smaller non-square region must preserve every destination pixel outside it. */ + unsigned char expected[sizeof pixels]; memset(expected,0xa5,sizeof expected); + glBindTexture(GL_TEXTURE_2D,textures[1]); + glTexSubImage2D(GL_TEXTURE_2D,0,0,0,8,8,GL_RGBA,GL_UNSIGNED_BYTE,expected); + glBindFramebuffer(GL_READ_FRAMEBUFFER,previous_fbos[0]); + glBindFramebuffer(GL_DRAW_FRAMEBUFFER,previous_fbos[1]); + glEnable(GL_SCISSOR_TEST); glScissor(7,7,1,1); + uint32_t subargs[]={TEST_GUEST_BASE+0x1000,TEST_GUEST_BASE+0x1100,textures[0],0,textures[1],0,3,2}; + import_esp=TEST_GUEST_BASE+0x400; + assert(mac_guest_write(import_esp+4,subargs,sizeof subargs)==0); + mac_macos_gl_host_import_bridge("halo_gl41_copy_image_2d")(); + assert(g_eax==1 && import_esp==TEST_GUEST_BASE+0x404); + GLint subread,subdraw; glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING,&subread); glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING,&subdraw); + assert(subread==(GLint)previous_fbos[0] && subdraw==(GLint)previous_fbos[1] && glIsEnabled(GL_SCISSOR_TEST)); + glBindFramebuffer(GL_READ_FRAMEBUFFER,previous_fbos[0]); + glFramebufferTexture2D(GL_READ_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D,textures[1],0); + glReadBuffer(GL_COLOR_ATTACHMENT0); + glReadPixels(0,0,8,8,GL_RGBA,GL_UNSIGNED_BYTE,actual); + for(unsigned row=0;row<2;row++) memcpy(expected+row*8*4,pixels+row*8*4,3*4); + assert(memcmp(actual,expected,sizeof expected)==0 && glGetError()==GL_NO_ERROR); + glDeleteTextures(2,textures);glDeleteFramebuffers(2,previous_fbos); + SDL_GL_DestroyContext(context);SDL_DestroyWindow(window);SDL_Quit(); + puts("Actual guest import/native GL4.1 pixels+mip+state+alias regression passed"); +} + +int main(void) +{ + const struct mac_guest_allocator allocator = { + NULL, test_allocate, test_release + }; + const uint32_t known_name_va = TEST_GUEST_BASE + 0x100u; + const uint32_t unknown_name_va = TEST_GUEST_BASE + 0x120u; + const uint32_t proc_name_va = TEST_GUEST_BASE + 0x140u; + const uint32_t get_string_args[] = { 0x1F02u }; + uint32_t version_va; + + assert(mac_guest_address_register(TEST_GUEST_BASE, guest_backing, + sizeof(guest_backing)) == 0); + memcpy(guest_backing + (known_name_va - TEST_GUEST_BASE), "glViewport", sizeof("glViewport")); + memcpy(guest_backing + (unknown_name_va - TEST_GUEST_BASE), "glNoSuchCall", sizeof("glNoSuchCall")); + memcpy(guest_backing + (proc_name_va - TEST_GUEST_BASE), "glViewport", sizeof("glViewport")); + assert(mac_guest_allocator_install(&allocator) == 0); + assert(mac_macos_gl_token_for_guest_name(known_name_va) == 0xFE100080u); + assert(mac_macos_gl_token_for_guest_name(unknown_name_va) == 0); + assert(mac_macos_gl_wrapper_for_token(0xFE100080u) != NULL); + assert(mac_macos_gl_wrapper_for_token(0xFEFFFFFFu) == NULL); + assert(mac_macos_gl_native_proc("glGetString") == NULL); + mac_guest_import_bridge proc_bridge = + mac_macos_gl_host_import_bridge("guest_gl_get_proc_address"); + assert(proc_bridge != NULL); + assert(mac_macos_gl_host_import_bridge("halo_gl41_copy_image_2d") != NULL); + assert(mac_macos_gl_host_import_bridge("host_gl_get_string") != NULL); + assert(mac_macos_gl_host_import_bridge("unknown_gl_import") == NULL); + memcpy(guest_backing + (import_esp + 4u - TEST_GUEST_BASE), &proc_name_va, sizeof(proc_name_va)); + proc_bridge(); + assert(g_eax == 0xFE100080u && import_esp == TEST_GUEST_BASE + 0x404u); + version_va = mac_macos_gl_return_guest_pointer("glGetString", "4.1 test", + get_string_args, 1); + assert(version_va != 0); + assert(strcmp((const char *)mac_guest_address_resolve(version_va, sizeof("4.1 test")), + "4.1 test") == 0); + mac_macos_gl_native_proc_clear(); + test_actual_copy_bridge(); + return 0; +} diff --git a/port/macos/tests/gl_native_proc_cache_test.c b/port/macos/tests/gl_native_proc_cache_test.c new file mode 100644 index 0000000000..1327ecf54c --- /dev/null +++ b/port/macos/tests/gl_native_proc_cache_test.c @@ -0,0 +1,55 @@ +#include "../gl_native_proc.h" +#include +#include +#include +#include +#include + +static SDL_GLContext current; +static unsigned lookups; +static void first(void) {} +static void second(void) {} +SDL_GLContext SDL_GL_GetCurrentContext(void) { return current; } +SDL_FunctionPointer SDL_GL_GetProcAddress(const char *name) +{ + ++lookups; + if (!strcmp(name, "glMissing")) return NULL; + return current == (SDL_GLContext)(uintptr_t)1 ? first : second; +} +const char *SDL_GetError(void) { return "fixture missing entry point"; } + +int main(void) +{ + assert(!mac_macos_gl_native_proc("glGetString")); + assert(!mac_macos_gl_native_proc("")); + assert(!mac_macos_gl_native_proc(NULL)); + current = (SDL_GLContext)(uintptr_t)1; + char name[64]; + for (unsigned i = 0; i < 100; ++i) + { + snprintf(name, sizeof(name), "glFixture%u", i); + assert(mac_macos_gl_native_proc(name) == (void *)first); + } + assert(lookups == 100); + /* Independently allocated names must hit the same cache key. */ + for (unsigned i = 0; i < 100; ++i) + { + snprintf(name, sizeof(name), "glFixture%u", i); + assert(mac_macos_gl_native_proc(name) == (void *)first); + } + assert(lookups == 100); + current = (SDL_GLContext)(uintptr_t)2; + assert(mac_macos_gl_native_proc("glFixture0") == (void *)second); + assert(lookups == 101); + current = (SDL_GLContext)(uintptr_t)1; + assert(mac_macos_gl_native_proc("glFixture0") == (void *)first); + assert(lookups == 101); + assert(!mac_macos_gl_native_proc("glMissing")); + assert(!mac_macos_gl_native_proc("glMissing")); + assert(lookups == 102); + mac_macos_gl_native_proc_clear(); + assert(mac_macos_gl_native_proc("glFixture0") == (void *)first); + assert(lookups == 103); + puts("gl_native_proc_cache_test: repeat names, context isolation, missing procedures and clear passed"); + return 0; +} diff --git a/port/macos/tests/gl_native_proc_test.c b/port/macos/tests/gl_native_proc_test.c new file mode 100644 index 0000000000..c2ff0c0e88 --- /dev/null +++ b/port/macos/tests/gl_native_proc_test.c @@ -0,0 +1,14 @@ +#include "../gl_native_proc.h" + +#include + +int main(void) +{ + /* No SDL GL context is created by this fixture. The resolver must fail + * closed instead of returning a process-global or truncated pointer. */ + assert(mac_macos_gl_native_proc("glGetString") == 0); + assert(mac_macos_gl_native_proc("") == 0); + assert(mac_macos_gl_native_proc(0) == 0); + mac_macos_gl_native_proc_clear(); + return 0; +} diff --git a/port/macos/tests/gl_shader_source_test.c b/port/macos/tests/gl_shader_source_test.c new file mode 100644 index 0000000000..47de034465 --- /dev/null +++ b/port/macos/tests/gl_shader_source_test.c @@ -0,0 +1,52 @@ +#include "gl_bridge_support.h" +#include "guest_address.h" + +#include +#include +#include + +static unsigned char memory[32768]; + +void *mac_macos_gl_native_proc(const char *name) +{ + (void)name; + return NULL; +} + +uint32_t mac_macos_gl_token_for_name(const char *name) +{ + (void)name; + return 0; +} + +int main(void) +{ + const uint32_t base = 0x10000; + const uint32_t vector_va = base + 64; + const uint32_t lengths_va = base + 96; + const uint32_t source_va = base + 256; + uint32_t raw[4] = {1, 1, vector_va, 0}; + const char *const *strings; + assert(mac_guest_address_register(base, memory, sizeof(memory)) == 0); + memcpy(memory + 64, &source_va, 4); + memset(memory + 256, 'x', 9164); + memory[256 + 9164] = 0; + strings = mac_macos_gl_guest_pointer("glShaderSource", 2, vector_va, raw, 4); + assert(strings && strings[0] == (const char *)memory + 256); + assert(strlen(strings[0]) == 9164); + /* Negative lengths retain GL's NUL-terminated source semantics. */ + int32_t length = -1; + memcpy(memory + 96, &length, 4); + raw[3] = lengths_va; + strings = mac_macos_gl_guest_pointer("glShaderSource", 2, vector_va, raw, 4); + assert(strings && strlen(strings[0]) == 9164); + /* Explicit lengths permit a source with no terminating NUL. */ + length = 10000; + memcpy(memory + 96, &length, 4); + memset(memory + 256, 'y', (size_t)length); + strings = mac_macos_gl_guest_pointer("glShaderSource", 2, vector_va, raw, 4); + assert(strings && strings[0] == (const char *)memory + 256); + assert(strings[0][9999] == 'y'); + assert(mac_guest_address_unregister(base) == 0); + return 0; +} diff --git a/port/macos/tests/guest_address_test.c b/port/macos/tests/guest_address_test.c new file mode 100644 index 0000000000..5c5e519e49 --- /dev/null +++ b/port/macos/tests/guest_address_test.c @@ -0,0 +1,63 @@ +#include "../guest_address.h" + +#include +#include +#include +#include + +int main(void) +{ + unsigned char backing[32] = { 0 }; + uint32_t guest_address = 0; + unsigned char *reserved; + size_t host_page_size = mac_guest_address_host_page_size(); + ptrdiff_t guest_offset; + + mac_guest_address_reset(); + assert(mac_guest_address_register(0x80300000u, backing, sizeof(backing)) == 0); + assert(mac_guest_address_resolve(0x80300004u, 4) == backing + 4); + assert(mac_guest_address_resolve(0x8030001fu, 1) == backing + 31); + assert(mac_guest_address_resolve(0x8030001fu, 2) == NULL); + assert(mac_guest_address_resolve(0x802fffffu, 1) == NULL); + assert(mac_guest_address_resolve(0, 1) == NULL); + assert(mac_guest_address_from_host(backing + 7, 8, &guest_address) == 0); + assert(guest_address == 0x80300007u); + assert(mac_guest_address_from_host(backing + 31, 2, &guest_address) == -1); + assert(mac_guest_address_register(0x80300010u, backing + 16, 8) == -1); + assert(mac_guest_address_register(0x80400000u, backing + 1, 4) == 0); + assert(mac_guest_address_resolve(0x80400001u, 2) == backing + 2); + assert(mac_guest_address_from_host(backing + 2, 1, &guest_address) == -1); + assert(mac_guest_address_register(0xfffffff0u, backing, 32) == -1); + assert(mac_guest_address_unregister(0x80400000u) == 0); + assert(mac_guest_address_unregister(0x80300000u) == 0); + assert(mac_guest_address_resolve(0x80300004u, 4) == NULL); + assert(mac_guest_address_unregister(0x80300000u) == -1); + mac_guest_address_reset(); + + assert(host_page_size >= 0x1000u && (host_page_size & (host_page_size - 1)) == 0); + reserved = mac_guest_address_reserve(0x70000000u, host_page_size * 2); + assert(reserved != NULL); + assert(mac_guest_address_resolve(0x70000000u, 1) == reserved); + assert(mac_guest_address_commit(0x70000000u, host_page_size, + PROT_READ | PROT_WRITE) == 0); + assert(mac_guest_address_resolve(0x70000000u, 1) == reserved); + reserved[0] = 0x5A; + assert(mac_guest_address_protect(0x70000000u, host_page_size, PROT_READ) == 0); + assert(mac_guest_address_resolve(0x70000000u, 1) == reserved); + assert(mac_guest_address_commit(0x70000000u + 0x1000u, 0x1000u, + PROT_READ | PROT_WRITE) == 0); + assert(mac_guest_address_unregister(0x70000000u) == 0); + assert(mac_guest_address_resolve(0x70000000u, 1) == NULL); + + reserved = mac_guest_address_reserve(0, (size_t)UINT64_C(0x100000000)); + assert(reserved != NULL); + assert(mac_guest_address_resolve(0, 1) == NULL); + assert(mac_guest_address_resolve(0x80000000u, 1) == reserved + 0x80000000u); + assert(mac_guest_address_from_host(reserved + 0x80000000u, 1, &guest_address) == 0); + assert(guest_address == 0x80000000u); + assert(mac_guest_address_linear_offset(0, reserved, &guest_offset) == 0); + assert((uintptr_t)(guest_offset + (ptrdiff_t)0x80000000u) == + (uintptr_t)(reserved + 0x80000000u)); + assert(mac_guest_address_unregister(0) == 0); + return 0; +} diff --git a/port/macos/tests/guest_allocator_test.c b/port/macos/tests/guest_allocator_test.c new file mode 100644 index 0000000000..e8b1c280db --- /dev/null +++ b/port/macos/tests/guest_allocator_test.c @@ -0,0 +1,60 @@ +#include "guest_address.h" +#include "guest_allocator.h" + +#include +#include +#include +#include + +struct test_allocator_state +{ + uint32_t next; + unsigned int releases; +}; + +static int test_allocate(void *opaque, size_t size, size_t alignment, + uint32_t *guest_va_out) +{ + struct test_allocator_state *state = opaque; + uint64_t aligned = ((uint64_t)state->next + alignment - 1) & ~(uint64_t)(alignment - 1); + if (aligned + size > UINT32_C(0x40004000)) + return -1; + *guest_va_out = (uint32_t)aligned; + state->next = (uint32_t)(aligned + size); + return 0; +} + +static void test_release(void *opaque, uint32_t guest_va, size_t size) +{ + struct test_allocator_state *state = opaque; + assert(guest_va >= UINT32_C(0x40000000)); + assert(size > 0); + state->releases++; +} + +int main(void) +{ + const uint32_t base = UINT32_C(0x40000000); + const size_t size = 0x4000; + void *memory = mmap(NULL, size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + struct test_allocator_state state = { base, 0 }; + struct mac_guest_allocator allocator = { &state, test_allocate, test_release }; + uint32_t guest_va = 0; + + assert(memory != MAP_FAILED); + assert(mac_guest_address_register(base, memory, size) == 0); + assert(mac_guest_allocator_install(&allocator) == 0); + assert(mac_guest_allocate(96, 64, &guest_va) == 0); + assert(guest_va == base); + assert(mac_guest_address_resolve(guest_va, 96) == memory); + assert(mac_guest_release(guest_va, 96) == 0); + assert(state.releases == 1); + assert(mac_guest_allocate(32, 3, &guest_va) == -1); + assert(mac_guest_allocate(size, 16, &guest_va) == -1); + assert(state.releases == 1); + + mac_guest_address_reset(); + puts("guest_allocator_test: ok"); + return 0; +} diff --git a/port/macos/tests/guest_call_test.c b/port/macos/tests/guest_call_test.c new file mode 100644 index 0000000000..b28e52d402 --- /dev/null +++ b/port/macos/tests/guest_call_test.c @@ -0,0 +1,34 @@ +#include "../guest_address.h" +#include "../guest_call.h" + +#include +#include +#include + +int main(void) +{ + uint32_t stack[8] = { 0x00123456u, 0x11223344u, 0xAABBCCDDu, 0 }; + uint32_t value, new_esp; + uint64_t value64; + void *span; + + mac_guest_address_reset(); + assert(mac_guest_address_register(0x1000u, stack, sizeof(stack)) == 0); + assert(mac_guest_stack_return(0x1000u, &value) == 0 && value == 0x00123456u); + assert(mac_guest_stack_arg32(0x1000u, 0, &value) == 0 && value == 0x11223344u); + assert(mac_guest_stack_arg32(0x1000u, 1, &value) == 0 && value == 0xAABBCCDDu); + assert(mac_guest_stack_arg64(0x1000u, 0, &value64) == 0 && + value64 == UINT64_C(0xAABBCCDD11223344)); + assert(mac_guest_stack_arg_span(0x1000u, 1, sizeof(value), &span) == 0); + assert(span == &stack[2]); + assert(mac_guest_stack_arg32(0xFFFFFFFCu, 0, &value) == -1); + assert(mac_guest_stack_after_return(0x1000u, 0, &new_esp) == 0 && new_esp == 0x1004u); + assert(mac_guest_stack_after_return(0x1000u, 8, &new_esp) == 0 && new_esp == 0x100Cu); + assert(mac_guest_stack_after_return(0xFFFFFFFCu, 0, &new_esp) == -1); + assert(mac_guest_write(0x100Cu, &value, sizeof(value)) == 0); + assert(stack[3] == 0xAABBCCDDu); + assert(mac_guest_read(0x100Cu, &value, sizeof(value)) == 0 && value == stack[3]); + assert(mac_guest_read(0x101Du, &value, sizeof(value)) == -1); + mac_guest_address_reset(); + return 0; +} diff --git a/port/macos/tests/guest_callback_test.c b/port/macos/tests/guest_callback_test.c new file mode 100644 index 0000000000..90dca03854 --- /dev/null +++ b/port/macos/tests/guest_callback_test.c @@ -0,0 +1,95 @@ +#include "guest_address.h" +#include "guest_allocator.h" +#include "guest_callback.h" +#include "guest_call.h" +#include "host_services.h" + +#include +#include +#include +#include + +#define TEST_GUEST_BASE UINT32_C(0x12000000) +#define TEST_GUEST_SIZE (1024u * 1024u) +#define TEST_RETURN UINT32_C(0xffffffff) +#define TEST_ATTACH_VA UINT32_C(0x00401000) +#define TEST_CALLBACK_VA UINT32_C(0x00402000) +#define TEST_TP UINT32_C(0x12001000) + +static unsigned char guest_memory[TEST_GUEST_SIZE]; +static uint32_t allocation_cursor = TEST_GUEST_BASE; +static uint32_t current_esp; +static uint32_t current_function; +static uint32_t observed_args[4]; +static uint32_t observed_return; +static uint32_t observed_thread; + +static int test_allocate(void *context, size_t size, size_t alignment, + uint32_t *guest_va_out) +{ + uint64_t aligned = ((uint64_t)allocation_cursor + alignment - 1) & ~(uint64_t)(alignment - 1); + (void)context; + if (!guest_va_out || aligned + size > TEST_GUEST_BASE + TEST_GUEST_SIZE) + return -1; + *guest_va_out = (uint32_t)aligned; + allocation_cursor = (uint32_t)(aligned + size); + return 0; +} + +static void test_release(void *context, uint32_t guest_va, size_t size) +{ + (void)context; + (void)guest_va; + (void)size; +} + +static void test_initialize(void *context, uint32_t guest_thread_va, uint32_t guest_esp) +{ + (void)context; + observed_thread = guest_thread_va; + current_esp = guest_esp; +} + +static void test_dispatch(void *context, uint32_t guest_function_va) +{ + uint32_t frame[5] = {0}; + (void)context; + current_function = guest_function_va; + if (guest_function_va == TEST_ATTACH_VA) + { + mac_host_set_guest_tp(TEST_TP); + return; + } + assert(guest_function_va == TEST_CALLBACK_VA); + assert(mac_guest_read(current_esp, frame, sizeof(frame)) == 0); + observed_return = frame[0]; + memcpy(observed_args, frame + 1, sizeof(observed_args)); +} + +int main(void) +{ + struct mac_guest_allocator allocator = { NULL, test_allocate, test_release }; + struct mac_guest_thread_runtime runtime = { NULL, test_initialize, test_dispatch }; + struct mac_guest_callback_context *callback = NULL; + const uint32_t args[] = { 0x11, 0x22, 0x33, 0x44 }; + + assert(mac_guest_address_register(TEST_GUEST_BASE, guest_memory, sizeof(guest_memory)) == 0); + assert(mac_guest_allocator_install(&allocator) == 0); + assert(mac_guest_callback_runtime_install(&runtime, TEST_ATTACH_VA) == 0); + assert(mac_guest_callback_context_create_current(64u * 1024u, &callback) == 0); + assert(callback != NULL); + assert(mac_host_get_guest_tp() == TEST_TP); + assert(current_function == TEST_ATTACH_VA); + assert(observed_thread == 0); + + assert(mac_guest_callback_invoke(callback, TEST_CALLBACK_VA, args, 4) == 0); + assert(current_function == TEST_CALLBACK_VA); + assert(observed_thread == TEST_TP); + assert(observed_return == TEST_RETURN); + assert(memcmp(observed_args, args, sizeof(args)) == 0); + + mac_guest_callback_context_destroy_current(callback); + assert(mac_host_get_guest_tp() == 0); + mac_guest_address_reset(); + return 0; +} diff --git a/port/macos/tests/guest_heap_test.c b/port/macos/tests/guest_heap_test.c new file mode 100644 index 0000000000..20b49e3e9e --- /dev/null +++ b/port/macos/tests/guest_heap_test.c @@ -0,0 +1,228 @@ +#include "guest_address.h" +#include "guest_allocator.h" +#include "guest_heap.h" +#include "host_syscall.h" + +#include +#include +#include +#include + +#define SYS_MMAP 222 +#define SYS_MUNMAP 215 +#define SYS_MPROTECT 226 +#define LINUX_MAP_PRIVATE 0x02 +#define LINUX_MAP_FIXED 0x10 +#define LINUX_MAP_ANONYMOUS 0x20 +#define LINUX_MAP_NORESERVE 0x4000 +#define LINUX_MAP_FIXED_NOREPLACE 0x100000 + +static int64_t call6(uint64_t number, uint64_t a0, uint64_t a1, + uint64_t a2, uint64_t a3, uint64_t a4, uint64_t a5) +{ + uint64_t args[6] = { a0, a1, a2, a3, a4, a5 }; + return mac_host_arm64_32_syscall(number, args); +} + +int main(void) +{ + void *arena = mac_guest_address_reserve(0, UINT64_C(1) << 32); + uint32_t first, again; + const uint32_t xbox_window = UINT32_C(0x80001000); + int64_t result; + assert(arena != NULL); + assert(mac_guest_heap_install(0x00100000) == 0); + assert(mac_guest_allocate(9000, 65536, &first) == 0); + assert((first & 0xffff) == 0); + uint8_t *guest = mac_guest_address_resolve(first, 9000); + assert(guest != NULL); + for (unsigned i = 0; i < 9000; ++i) assert(guest[i] == 0); + memset(guest, 0xa5, 9000); + assert(mac_guest_release(first, 9000) == 0); + assert(mac_guest_allocate(9000, 65536, &again) == 0 && again == first); + guest = mac_guest_address_resolve(again, 9000); + for (unsigned i = 0; i < 9000; ++i) assert(guest[i] == 0); + + /* mmap zero-fills before applying the requested final protection. */ + result = call6(SYS_MMAP, 0, 4096, PROT_READ, + LINUX_MAP_PRIVATE | LINUX_MAP_ANONYMOUS, + (uint64_t)(int64_t)-1, 0); + assert(result > 0); + guest = mac_guest_address_resolve((uint32_t)result, 4096); + assert(guest != NULL); + for (unsigned i = 0; i < 4096; ++i) assert(guest[i] == 0); + assert(call6(SYS_MUNMAP, (uint32_t)result, 4096, 0, 0, 0, 0) == 0); + /* Reuse formerly poisoned anonymous backing with a short request. The + * malloc end guard at the rounded guest-page boundary must be zero. */ + result = call6(SYS_MMAP, 0, 4096, PROT_READ | PROT_WRITE, + LINUX_MAP_PRIVATE | LINUX_MAP_ANONYMOUS, + (uint64_t)(int64_t)-1, 0); + assert(result > 0); + uint32_t poisoned_va = (uint32_t)result; + guest = mac_guest_address_resolve(poisoned_va, 4096); + memset(guest, 0xca, 4096); + assert(call6(SYS_MUNMAP, poisoned_va, 4096, 0, 0, 0, 0) == 0); + result = call6(SYS_MMAP, 0, 17, PROT_READ | PROT_WRITE, + LINUX_MAP_PRIVATE | LINUX_MAP_ANONYMOUS, + (uint64_t)(int64_t)-1, 0); + assert(result == poisoned_va); + guest = mac_guest_address_resolve(poisoned_va, 4096); + for (unsigned i = 0; i < 4096; ++i) assert(guest[i] == 0); + assert(guest[4092] == 0); + assert(call6(SYS_MUNMAP, poisoned_va, 17, 0, 0, 0, 0) == 0); + result = call6(SYS_MMAP, 0, 4096, PROT_NONE, + LINUX_MAP_PRIVATE | LINUX_MAP_ANONYMOUS, + (uint64_t)(int64_t)-1, 0); + assert(result > 0); + assert(call6(SYS_MPROTECT, (uint32_t)result, 4096, + PROT_READ | PROT_WRITE, 0, 0, 0) == 0); + guest = mac_guest_address_resolve((uint32_t)result, 4096); + assert(guest != NULL); + for (unsigned i = 0; i < 4096; ++i) assert(guest[i] == 0); + assert(call6(SYS_MUNMAP, (uint32_t)result, 4096, 0, 0, 0, 0) == 0); + + /* Linux mmap accepts a 4K subpage address/length while Darwin commits its + * enclosing 16K host page. */ + result = call6(SYS_MMAP, xbox_window, 4096, PROT_READ | PROT_WRITE, + LINUX_MAP_PRIVATE | LINUX_MAP_FIXED | LINUX_MAP_ANONYMOUS, + (uint64_t)(int64_t)-1, 0); + assert(result == xbox_window); + guest = mac_guest_address_resolve(xbox_window, 4096); + assert(guest != NULL); + memset(guest, 0x5a, 4096); + assert(call6(SYS_MPROTECT, xbox_window, 4096, PROT_READ, 0, 0, 0) == 0); + assert(call6(SYS_MPROTECT, xbox_window, 4096, PROT_READ | PROT_WRITE, 0, 0, 0) == 0); + assert(call6(SYS_MUNMAP, xbox_window, 4096, 0, 0, 0, 0) == 0); + + /* Fixed mappings cannot replace the loaded image or the import-stub window. */ + result = call6(SYS_MMAP, 0x10000, 4096, PROT_READ | PROT_WRITE, + LINUX_MAP_PRIVATE | LINUX_MAP_FIXED | LINUX_MAP_ANONYMOUS, + (uint64_t)(int64_t)-1, 0); + assert(result == -12); /* ENOMEM: collision with reserved image pages */ + result = call6(SYS_MMAP, 0x70000000, 4096, PROT_READ | PROT_WRITE, + LINUX_MAP_PRIVATE | LINUX_MAP_FIXED | LINUX_MAP_ANONYMOUS, + (uint64_t)(int64_t)-1, 0); + assert(result == -12); + result = call6(SYS_MMAP, 0x80010000, 4096, PROT_READ | PROT_EXEC, + LINUX_MAP_PRIVATE | LINUX_MAP_FIXED | LINUX_MAP_ANONYMOUS, + (uint64_t)(int64_t)-1, 0); + assert(result == -13); /* EACCES: native arena pages are never executable */ + + /* The Linux Xbox layer reserves the 128 MiB guest physical window with + * MAP_FIXED_NOREPLACE|MAP_NORESERVE, then overlays allocated blocks using + * MAP_FIXED. Both operations stay inside the already-reserved high arena. */ + result = call6(SYS_MMAP, 0x80000000, 0x08000000, PROT_NONE, + LINUX_MAP_PRIVATE | LINUX_MAP_FIXED_NOREPLACE | LINUX_MAP_ANONYMOUS | + LINUX_MAP_NORESERVE, (uint64_t)(int64_t)-1, 0); + assert(result == 0x80000000); + result = call6(SYS_MMAP, 0x80000000, 0x08000000, PROT_NONE, + LINUX_MAP_PRIVATE | LINUX_MAP_FIXED_NOREPLACE | LINUX_MAP_ANONYMOUS | + LINUX_MAP_NORESERVE, (uint64_t)(int64_t)-1, 0); + assert(result == -12); /* a second no-replace reservation collides */ + + /* Two independent Xbox 4KB blocks can share one 16KB Darwin host page. + * Freeing/recommitting either block must not erase or hide its neighbor. */ + { + const uint32_t left = 0x80001000; + const uint32_t right = 0x80002000; + uint8_t *left_bytes, *right_bytes; + assert(mac_guest_address_host_page_size() == 16384); + assert(call6(SYS_MMAP, left, 4096, PROT_READ | PROT_WRITE, + LINUX_MAP_PRIVATE | LINUX_MAP_FIXED | LINUX_MAP_ANONYMOUS | + LINUX_MAP_NORESERVE, (uint64_t)(int64_t)-1, 0) == left); + left_bytes = mac_guest_address_resolve(left, 4096); + assert(left_bytes != NULL); + memset(left_bytes, 0x4c, 4096); + assert(call6(SYS_MMAP, right, 4096, PROT_READ | PROT_WRITE, + LINUX_MAP_PRIVATE | LINUX_MAP_FIXED | LINUX_MAP_ANONYMOUS | + LINUX_MAP_NORESERVE, (uint64_t)(int64_t)-1, 0) == right); + right_bytes = mac_guest_address_resolve(right, 4096); + assert(right_bytes != NULL); + memset(right_bytes, 0xb2, 4096); + /* Linux rounds anonymous mmap length up to a guest page. Its tail + * must be zero too, even when Darwin retains the former backing. */ + assert(call6(SYS_MMAP, left, 17, PROT_READ | PROT_WRITE, + LINUX_MAP_PRIVATE | LINUX_MAP_FIXED | LINUX_MAP_ANONYMOUS, + (uint64_t)(int64_t)-1, 0) == left); + for (unsigned i = 0; i < 4096; ++i) + { + assert(left_bytes[i] == 0); + assert(right_bytes[i] == 0xb2); + } + assert(call6(SYS_MMAP, left, 4096, PROT_NONE, + LINUX_MAP_PRIVATE | LINUX_MAP_FIXED | LINUX_MAP_ANONYMOUS | + LINUX_MAP_NORESERVE, (uint64_t)(int64_t)-1, 0) == left); + for (unsigned i = 0; i < 4096; ++i) assert(right_bytes[i] == 0xb2); + assert(call6(SYS_MMAP, left, 4096, PROT_READ | PROT_WRITE, + LINUX_MAP_PRIVATE | LINUX_MAP_FIXED | LINUX_MAP_ANONYMOUS | + LINUX_MAP_NORESERVE, (uint64_t)(int64_t)-1, 0) == left); + left_bytes = mac_guest_address_resolve(left, 4096); + for (unsigned i = 0; i < 4096; ++i) + { + assert(left_bytes[i] == 0); + assert(right_bytes[i] == 0xb2); + } + assert(call6(SYS_MMAP, left, 4096, PROT_NONE, + LINUX_MAP_PRIVATE | LINUX_MAP_FIXED | LINUX_MAP_ANONYMOUS | + LINUX_MAP_NORESERVE, (uint64_t)(int64_t)-1, 0) == left); + for (unsigned i = 0; i < 4096; ++i) assert(right_bytes[i] == 0xb2); + assert(call6(SYS_MMAP, right, 4096, PROT_NONE, + LINUX_MAP_PRIVATE | LINUX_MAP_FIXED | LINUX_MAP_ANONYMOUS | + LINUX_MAP_NORESERVE, (uint64_t)(int64_t)-1, 0) == right); + } + + result = call6(SYS_MMAP, 0x81A00000, 16u * 1024u * 1024u, + PROT_READ | PROT_WRITE, + LINUX_MAP_PRIVATE | LINUX_MAP_FIXED | LINUX_MAP_ANONYMOUS | + LINUX_MAP_NORESERVE, (uint64_t)(int64_t)-1, 0); + assert(result == 0x81A00000); /* XPhysicalAlloc fixed physical address */ + guest = mac_guest_address_resolve((uint32_t)result, 16u * 1024u * 1024u); + assert(guest != NULL); + memset(guest, 0x39, 16u * 1024u * 1024u); + result = call6(SYS_MMAP, 0x81A00000, 16u * 1024u * 1024u, PROT_NONE, + LINUX_MAP_PRIVATE | LINUX_MAP_FIXED | LINUX_MAP_ANONYMOUS | + LINUX_MAP_NORESERVE, (uint64_t)(int64_t)-1, 0); + assert(result == 0x81A00000); /* XPhysicalFree restores the reservation */ + assert(call6(SYS_MUNMAP, 0x81A00000, 16u * 1024u * 1024u, 0, 0, 0, 0) == -22); + assert(call6(SYS_MPROTECT, 0x81A00000, 16u * 1024u * 1024u, + PROT_READ | PROT_WRITE, 0, 0, 0) == 0); + guest = mac_guest_address_resolve(0x81A00000, 16u * 1024u * 1024u); + assert(guest && guest[0] == 0 && guest[16u * 1024u * 1024u - 1] == 0); + assert(call6(SYS_MMAP, 0x81A00000, 16u * 1024u * 1024u, PROT_NONE, + LINUX_MAP_PRIVATE | LINUX_MAP_FIXED | LINUX_MAP_ANONYMOUS | + LINUX_MAP_NORESERVE, (uint64_t)(int64_t)-1, 0) == 0x81A00000); + result = call6(SYS_MMAP, 0x81A00000, 16u * 1024u * 1024u, + PROT_READ | PROT_WRITE, + LINUX_MAP_PRIVATE | LINUX_MAP_FIXED | LINUX_MAP_ANONYMOUS | + LINUX_MAP_NORESERVE, (uint64_t)(int64_t)-1, 0); + assert(result == 0x81A00000); + guest = mac_guest_address_resolve((uint32_t)result, 16u * 1024u * 1024u); + assert(guest && guest[0] == 0 && guest[16u * 1024u * 1024u - 1] == 0); + assert(call6(SYS_MMAP, 0x81A00000, 16u * 1024u * 1024u, PROT_NONE, + LINUX_MAP_PRIVATE | LINUX_MAP_FIXED | LINUX_MAP_ANONYMOUS | + LINUX_MAP_NORESERVE, (uint64_t)(int64_t)-1, 0) == 0x81A00000); + + /* mmap must clear anonymous bytes before applying read-only or no-access + * protection. Temporarily make each mapping writable to inspect the zero + * fill, then unmap it. This catches a host-side memset after final mprotect. */ + for (int protection_index = 0; protection_index < 2; ++protection_index) + { + int initial_protection = protection_index == 0 ? PROT_READ : PROT_NONE; + uint32_t mapped_va = 0; + result = call6(SYS_MMAP, 0, 8192, initial_protection, + LINUX_MAP_PRIVATE | LINUX_MAP_ANONYMOUS, + (uint64_t)(int64_t)-1, 0); + assert(result > 0 && (uint64_t)result <= UINT32_MAX); + mapped_va = (uint32_t)result; + assert(call6(SYS_MPROTECT, mapped_va, 8192, PROT_READ | PROT_WRITE, + 0, 0, 0) == 0); + guest = mac_guest_address_resolve(mapped_va, 8192); + assert(guest != NULL); + for (unsigned i = 0; i < 8192; ++i) assert(guest[i] == 0); + assert(call6(SYS_MUNMAP, mapped_va, 8192, 0, 0, 0, 0) == 0); + } + + assert(mac_guest_release(again, 9000) == 0); + assert(mac_guest_address_unregister(0) == 0); + return 0; +} diff --git a/port/macos/tests/guest_image_test.c b/port/macos/tests/guest_image_test.c new file mode 100644 index 0000000000..0ac4010a88 --- /dev/null +++ b/port/macos/tests/guest_image_test.c @@ -0,0 +1,117 @@ +#include "../guest_address.h" +#include "../guest_image.h" +#include "../elf32_format.h" + +#include +#include +#include +#include + +union fixture +{ + max_align_t align; + unsigned char bytes[512]; +}; + +int main(void) +{ + union fixture fixture = { 0 }; + struct halo_elf32_ehdr *eh = (struct halo_elf32_ehdr *)fixture.bytes; + struct halo_elf32_phdr *ph; + struct halo_elf32_shdr *sh; + struct halo_elf32_sym *sym; + struct mac_guest_image image; + char error[160]; + const unsigned char file_data[] = { 0x90, 0xC3, 0x12, 0x34 }; + unsigned char *loaded; + + memcpy(eh->ident, "\177ELF", 4); + eh->ident[4] = HALO_ELFCLASS32; + eh->ident[5] = HALO_ELFDATA2LSB; + eh->ident[6] = HALO_EVCURRENT; + eh->type = HALO_ET_EXEC; + eh->machine = HALO_EM_386; + eh->version = HALO_EVCURRENT; + eh->entry = 0x10000; + eh->phoff = sizeof(*eh); + eh->ehsize = sizeof(*eh); + eh->phentsize = sizeof(struct halo_elf32_phdr); + eh->phnum = 2; + eh->shoff = 280; + eh->shentsize = sizeof(struct halo_elf32_shdr); + eh->shnum = 3; + ph = (struct halo_elf32_phdr *)(fixture.bytes + eh->phoff); + ph[0].type = HALO_PT_LOAD; + ph[0].offset = 256; + ph[0].vaddr = 0x10000; + ph[0].filesz = sizeof(file_data); + ph[0].memsz = 64; + ph[0].flags = HALO_PF_R | HALO_PF_X; + ph[0].align = 4096; + ph[1].type = HALO_PT_TLS; + ph[1].offset = 260; + ph[1].vaddr = 0x10004; + ph[1].filesz = 2; + ph[1].memsz = 8; + ph[1].align = 4; + sh = (struct halo_elf32_shdr *)(fixture.bytes + eh->shoff); + sh[1].type = HALO_SHT_SYMTAB; + sh[1].offset = 400; + sh[1].size = 2 * sizeof(struct halo_elf32_sym); + sh[1].link = 2; + sh[1].entsize = sizeof(struct halo_elf32_sym); + sh[2].type = HALO_SHT_STRTAB; + sh[2].offset = 440; + sh[2].size = 16; + sym = (struct halo_elf32_sym *)(fixture.bytes + sh[1].offset); + sym[1].name = 1; + sym[1].value = 0x10000; + sym[1].size = 64; + sym[1].info = HALO_STT_FUNC; + sym[1].shndx = 1; + memcpy(fixture.bytes + sh[2].offset, "\0entry\0unused\0", 14); + memcpy(fixture.bytes + 256, file_data, sizeof(file_data)); + + if (mac_guest_image_load(fixture.bytes, sizeof(fixture.bytes), + &image, error, sizeof(error)) != 0) + { + fprintf(stderr, "load failed: %s\n", error); + return 1; + } + assert(image.entry_va == 0x10000); + assert(image.code_lo_va == 0x10000 && image.code_hi_va == 0x10040); + assert(image.image_end_va == 0x10040); + assert(image.tls_image_va == 0x10004); + assert(image.tls_file_size == 2 && image.tls_memory_size == 8); + uint32_t symbol_va = 0; + assert(mac_guest_image_find_symbol(fixture.bytes, sizeof(fixture.bytes), + "entry", &symbol_va) == 0 && symbol_va == 0x10000); + assert(mac_guest_image_find_symbol(fixture.bytes, sizeof(fixture.bytes), + "missing", &symbol_va) != 0); + assert(mac_guest_image_find_object(fixture.bytes, sizeof(fixture.bytes), "entry", 4, &symbol_va) != 0); + sym[1].info = HALO_STT_OBJECT; + sym[1].size = 4; + assert(mac_guest_image_find_object(fixture.bytes, sizeof(fixture.bytes), "entry", 4, &symbol_va) == 0 && symbol_va == 0x10000); + assert(mac_guest_image_find_symbol(fixture.bytes, sizeof(fixture.bytes), "entry", &symbol_va) != 0); + assert(mac_guest_image_find_object(fixture.bytes, sizeof(fixture.bytes), "entry", 8, &symbol_va) != 0); + sym[1].value = 0x9000; + assert(mac_guest_image_find_object(fixture.bytes, sizeof(fixture.bytes), "entry", 4, &symbol_va) != 0); + sym[1].value = 0x10000; + sym[0] = sym[1]; + assert(mac_guest_image_find_object(fixture.bytes, sizeof(fixture.bytes), "entry", 4, &symbol_va) != 0); + memset(&sym[0], 0, sizeof(sym[0])); + loaded = mac_guest_address_resolve(0x10000, 64); + assert(loaded); + assert(memcmp(loaded, file_data, sizeof(file_data)) == 0); + for (size_t i = sizeof(file_data); i < 64; ++i) + assert(loaded[i] == 0); + assert(image.memory_offset > 0); + mac_guest_address_reset(); + + eh->entry = 0x9000; + assert(mac_guest_image_load(fixture.bytes, sizeof(fixture.bytes), + &image, error, sizeof(error)) != 0); + assert(strstr(error, "entry image") != NULL); + puts("ELF32 guest image loader: PASS"); + return 0; +} diff --git a/port/macos/tests/guest_import_registry_test.c b/port/macos/tests/guest_import_registry_test.c new file mode 100644 index 0000000000..a76511bd9e --- /dev/null +++ b/port/macos/tests/guest_import_registry_test.c @@ -0,0 +1,49 @@ +#include "guest_import_registry.h" + +#include +#include +#include + +static int first_calls; +static int second_calls; + +static void first_bridge(void) +{ + first_calls++; +} + +static void second_bridge(void) +{ + second_calls++; +} + +int main(void) +{ + uint32_t first_token = 0, second_token = 0; + mac_guest_function first; + mac_guest_function second; + + assert(mac_guest_import_register("host_syscall", first_bridge, &first_token) == 0); + assert(mac_guest_import_register("host_thread_create", second_bridge, &second_token) == 0); + assert(first_token == MAC_GUEST_IMPORT_TOKEN_BASE); + assert(second_token == MAC_GUEST_IMPORT_TOKEN_BASE + MAC_GUEST_IMPORT_TOKEN_STRIDE); + assert(mac_guest_import_count() == 2); + assert(mac_guest_import_token("host_syscall") == first_token); + assert(mac_guest_import_register("host_syscall", first_bridge, NULL) == 0); + assert(mac_guest_import_register("host_syscall", second_bridge, NULL) == -1); + + first = mac_guest_import_resolve(first_token); + second = mac_guest_import_resolve(second_token); + assert(first && second && first != second); + first(); + second(); + assert(first_calls == 1 && second_calls == 1); + assert(mac_guest_import_resolve(first_token + 1) == NULL); + assert(mac_guest_import_resolve(MAC_GUEST_IMPORT_TOKEN_BASE + 0x1000) == NULL); + + mac_guest_import_reset(); + assert(mac_guest_import_count() == 0); + assert(mac_guest_import_resolve(first_token) == NULL); + puts("guest_import_registry_test: ok"); + return 0; +} diff --git a/port/macos/tests/guest_thread_test.c b/port/macos/tests/guest_thread_test.c new file mode 100644 index 0000000000..743317df43 --- /dev/null +++ b/port/macos/tests/guest_thread_test.c @@ -0,0 +1,119 @@ +#include "guest_address.h" +#include "guest_allocator.h" +#include "guest_call.h" +#include "guest_thread.h" +#include "host_services.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#define TEST_GUEST_BASE UINT32_C(0x50000000) +#define TEST_GUEST_SIZE (64u * 1024u) +#define TEST_THREAD_VA UINT32_C(0x50000100) +#define TEST_START_VA UINT32_C(0x12345678) +#define TEST_RETURN_VA UINT32_C(0xffffffff) + +struct test_state +{ + pthread_mutex_t mutex; + pthread_cond_t condition; + uint32_t next; + uint32_t initialized_esp; + uint32_t initialized_thread; + int dispatched; + int released; +}; + +static struct test_state state = { + PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, TEST_GUEST_BASE, 0, 0, 0, 0 +}; + +static int test_allocate(void *context, size_t size, size_t alignment, + uint32_t *guest_va_out) +{ + struct test_state *test = context; + uint64_t aligned = ((uint64_t)test->next + alignment - 1) & ~(uint64_t)(alignment - 1); + if (aligned + size > (uint64_t)TEST_GUEST_BASE + TEST_GUEST_SIZE) + return ENOMEM; + *guest_va_out = (uint32_t)aligned; + test->next = (uint32_t)(aligned + size); + return 0; +} + +static void test_release(void *context, uint32_t guest_va, size_t size) +{ + struct test_state *test = context; + assert(guest_va >= TEST_GUEST_BASE); + assert(size >= 16 * 1024); + pthread_mutex_lock(&test->mutex); + test->released++; + pthread_cond_broadcast(&test->condition); + pthread_mutex_unlock(&test->mutex); +} + +static void initialize_guest_thread(void *context, uint32_t thread_va, + uint32_t guest_esp) +{ + struct test_state *test = context; + assert(mac_host_get_guest_tp() == thread_va); + pthread_mutex_lock(&test->mutex); + test->initialized_thread = thread_va; + test->initialized_esp = guest_esp; + pthread_mutex_unlock(&test->mutex); +} + +static void dispatch_guest(void *context, uint32_t function_va) +{ + struct test_state *test = context; + uint32_t frame[2]; + uint32_t guest_esp; + + assert(function_va == TEST_START_VA); + pthread_mutex_lock(&test->mutex); + guest_esp = test->initialized_esp; + pthread_mutex_unlock(&test->mutex); + assert(mac_guest_read(guest_esp, frame, sizeof(frame)) == 0); + assert(frame[0] == TEST_RETURN_VA); + assert(frame[1] == TEST_THREAD_VA); + assert(mac_host_get_guest_tp() == TEST_THREAD_VA); + pthread_mutex_lock(&test->mutex); + test->dispatched++; + pthread_cond_broadcast(&test->condition); + pthread_mutex_unlock(&test->mutex); +} + +int main(void) +{ + void *memory = mmap(NULL, TEST_GUEST_SIZE, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + struct mac_guest_allocator allocator = { &state, test_allocate, test_release }; + struct mac_guest_thread_runtime runtime = { &state, initialize_guest_thread, dispatch_guest }; + struct timespec deadline; + + assert(memory != MAP_FAILED); + assert(mac_guest_address_register(TEST_GUEST_BASE, memory, TEST_GUEST_SIZE) == 0); + assert(mac_guest_allocator_install(&allocator) == 0); + assert(mac_guest_thread_runtime_install(&runtime, TEST_START_VA) == 0); + assert(mac_host_thread_create(TEST_THREAD_VA, 32 * 1024) == 0); + + assert(clock_gettime(CLOCK_REALTIME, &deadline) == 0); + deadline.tv_sec += 3; + pthread_mutex_lock(&state.mutex); + while (!state.released) + assert(pthread_cond_timedwait(&state.condition, &state.mutex, &deadline) == 0); + assert(state.dispatched == 1); + assert(state.initialized_thread == TEST_THREAD_VA); + assert(state.initialized_esp % 16 == 8); + pthread_mutex_unlock(&state.mutex); + assert(mac_host_get_guest_tp() == 0); + + mac_guest_address_reset(); + puts("guest_thread_test: ok"); + return 0; +} diff --git a/port/macos/tests/guest_x87_80_test.c b/port/macos/tests/guest_x87_80_test.c new file mode 100644 index 0000000000..86a99a2beb --- /dev/null +++ b/port/macos/tests/guest_x87_80_test.c @@ -0,0 +1,90 @@ +#include "../guest_x87_80.h" + +#include +#include +#include +#include + +static void roundtrip(double input) +{ + uint8_t encoded[10]; + double output = 1.0; + assert(mac_guest_x87_80_store(encoded, input) == 0); + assert(mac_guest_x87_80_load(encoded, 0, &output) == 0); + if (isnan(input)) + assert(isnan(output)); + else + assert(memcmp(&input, &output, sizeof(input)) == 0); +} + +int main(void) +{ + uint8_t raw[10] = {0}; + double output; + + roundtrip(0.0); + roundtrip(-0.0); + roundtrip(1.0); + roundtrip(-2.5); + roundtrip(0x1.fffffffffffffp+1023); + roundtrip(0x1p-1022); + roundtrip(0x0.0000000000001p-1022); + roundtrip(INFINITY); + roundtrip(-INFINITY); + roundtrip(NAN); + + /* Decode canonical values written independently of the encoder. */ + memset(raw, 0, sizeof(raw)); + raw[7] = 0x80; + raw[8] = 0xFF; + raw[9] = 0x3F; /* +1.0 */ + assert(mac_guest_x87_80_load(raw, 0, &output) == 0 && output == 1.0); + memset(raw, 0, sizeof(raw)); + raw[7] = 0x80; + raw[8] = 0xCD; + raw[9] = 0x3B; /* binary64's smallest subnormal, encoded as binary80 */ + assert(mac_guest_x87_80_load(raw, 0, &output) == 0); + assert(output == 0x0.0000000000001p-1022); + + /* A finite binary80 halfway value rounds to nearest-even. */ + memset(raw, 0, sizeof(raw)); + raw[7] = 0x80; + raw[8] = 0xFF; + raw[9] = 0x3F; + raw[1] = 0x04; /* 1 + 2^-53, tie rounds to even 1.0 */ + assert(mac_guest_x87_80_load(raw, 0, &output) == 0 && output == 1.0); + assert(mac_guest_x87_80_load(raw, 2, &output) == 0 && output == nextafter(1.0, INFINITY)); + assert(mac_guest_x87_80_load(raw, 1, &output) == 0 && output == 1.0); + assert(mac_guest_x87_80_load(raw, 3, &output) == 0 && output == 1.0); + + /* A halfway value between zero and the least subnormal follows guest RC. */ + memset(raw, 0, sizeof(raw)); + raw[7] = 0x80; + raw[8] = 0xCC; + raw[9] = 0x3B; /* 2^-1075 */ + assert(mac_guest_x87_80_load(raw, 0, &output) == 0 && output == 0.0); + assert(mac_guest_x87_80_load(raw, 2, &output) == 0 && output == 0x0.0000000000001p-1022); + + /* Directed conversion handles finite overflow and tiny extended inputs. */ + memset(raw, 0, sizeof(raw)); + raw[7] = 0x80; + raw[8] = 0xFF; + raw[9] = 0x43; /* +2^1024: exponent bias 16383 + 1024 */ + assert(mac_guest_x87_80_load(raw, 0, &output) == 0 && isinf(output) && output > 0); + assert(mac_guest_x87_80_load(raw, 3, &output) == 0 && output == 0x1.fffffffffffffp+1023); + raw[9] |= 0x80; + assert(mac_guest_x87_80_load(raw, 1, &output) == 0 && isinf(output) && output < 0); + + memset(raw, 0, sizeof(raw)); + raw[0] = 1; /* positive binary80 subnormal */ + assert(mac_guest_x87_80_load(raw, 0, &output) == 0 && output == 0.0); + assert(mac_guest_x87_80_load(raw, 2, &output) == 0 && output == 0x0.0000000000001p-1022); + + /* Noncanonical unnormal values fail closed. */ + memset(raw, 0, sizeof(raw)); + raw[7] = 0x40; + raw[8] = 1; + assert(mac_guest_x87_80_load(raw, 0, &output) == -1); + + return 0; +} diff --git a/port/macos/tests/host_bink_codec_test.c b/port/macos/tests/host_bink_codec_test.c new file mode 100644 index 0000000000..f81849c8fb --- /dev/null +++ b/port/macos/tests/host_bink_codec_test.c @@ -0,0 +1,110 @@ +#include "host_bink.h" +#include +#include +#include +#include "guest_address.h" +#include +#include + +int main(int argc, char **argv) +{ + if (argc != 2) { fprintf(stderr,"usage: host_bink_codec_test intro.bik\n"); return 2; } + assert(SDL_Init(SDL_INIT_EVENTS)); + struct halo_bink *movie = mac_bink_open_path(argv[1], 0); + assert(movie); + const struct halo_bink_info *info = mac_bink_info(movie); + assert(info->width == 640 && info->height == 480 && info->frames > 400); + unsigned frames = 0; + uint64_t colored_bytes = 0; + while (mac_bink_decode(movie)) + { + const unsigned char *pixels = mac_bink_pixels(movie); + for (size_t i = 0; i < (size_t)info->width * info->height * 4; i += 4) + colored_bytes += pixels[i] != 0 || pixels[i+1] != 0 || pixels[i+2] != 0; + ++frames; mac_bink_next(movie); + } + const struct halo_bink_evidence *evidence = mac_bink_evidence(movie); + assert(frames == info->frames && colored_bytes > 0); + assert(evidence->pcm_samples > 1000000 && evidence->nonzero_pcm_samples > 0); + /* Lossy Bink reconstruction can overshoot full scale; SDL handles output + * clipping. Require finite, nonzero, bounded decoder output here. */ + assert(evidence->pcm_peak > 0 && evidence->pcm_peak < 4); + printf("native intro decode: frames=%u colored_pixels=%llu PCM_samples=%llu nonzero=%llu peak=%.6f\n", + frames,(unsigned long long)colored_bytes,(unsigned long long)evidence->pcm_samples, + (unsigned long long)evidence->nonzero_pcm_samples,evidence->pcm_peak); + mac_bink_close(movie); + /* Exercise the exact five-DWORD guest wire and pitched texture transfer. */ + const uint32_t base = 0x30000000; + const size_t guest_size = 2 * 1024 * 1024; + unsigned char *guest = calloc(1, guest_size); + assert(guest && strlen(argv[1]) < 1024); + assert(mac_guest_address_register(base, guest, guest_size) == 0); + strcpy((char *)guest, argv[1]); + memset(guest + 1024, 0xa5, 32); + assert(!mac_host_bink_open(base, base + (uint32_t)guest_size - 10, 0)); + uint32_t token = mac_host_bink_open(base, base + 1024, 0); + assert(token > 0 && token < 8); + uint32_t wire[5]; memcpy(wire, guest + 1024, sizeof(wire)); + assert(wire[0] == 640 && wire[1] == 480 && wire[2] == 481 && wire[3] == 0 && wire[4] == 0); + for (unsigned i = 1044; i < 1056; ++i) assert(guest[i] == 0xa5); + assert(mac_host_bink_decode(token)); + assert(mac_host_bink_next(token, base + 1024)); + memcpy(wire, guest + 1024, sizeof(wire)); + assert(wire[3] == 0 && wire[4] == 0); + assert(mac_host_bink_wait(token) == 0); /* First frame has PTS zero. */ + const uint32_t output_va = base + 4096; + const uint32_t pitch = 644 * 4; + memset(guest + 4096, 0xa5, (size_t)pitch * 482); + assert(mac_host_bink_copy(token, output_va, (int32_t)pitch, 482, 2, 1, 0x80000003)); + for (unsigned i = 0; i < pitch; ++i) assert(guest[4096 + i] == 0xa5); + for (unsigned row = 1; row <= 480; ++row) + { + unsigned char *pixels = guest + 4096 + row * pitch; + for (unsigned i = 0; i < 8; ++i) assert(pixels[i] == 0xa5); + for (unsigned i = 2568; i < pitch; ++i) assert(pixels[i] == 0xa5); + } + assert(!mac_host_bink_copy(token, output_va, 2560, 480, 1, 0, 3)); + assert(!mac_host_bink_copy(token, output_va, -2560, 480, 0, 0, 3)); + assert(!mac_host_bink_copy(token, output_va, 2560, 479, 0, 0, 3)); + assert(!mac_host_bink_copy(token, base + (uint32_t)guest_size - 10, 2560, 480, 0, 0, 3)); + assert(!mac_host_bink_copy(token, output_va, 2560, 480, 0, 0, 2)); + mac_host_bink_presented(); + /* Mirror the guest's DoFrame/Next/Copy/Present/end predicate exactly. + * Every one of its 481 source frames must survive that predicate. */ + unsigned presented = 1; + while (wire[3] != wire[2] - 1) + { + assert(mac_host_bink_decode(token)); + assert(mac_host_bink_next(token, base + 1024)); + assert(mac_host_bink_copy(token, output_va, (int32_t)pitch, 482, 2, 1, 0x80000003)); + memcpy(wire, guest + 1024, sizeof(wire)); + ++presented; + if (wire[3] == wire[2] - 1) mac_host_bink_close(token); + mac_host_bink_presented(); + } + assert(presented == 481); + mac_host_bink_close(token); mac_host_bink_close(token); + assert(!mac_host_bink_decode(token)); + assert(!mac_host_bink_next(token, base + 1024)); + mac_guest_address_reset(); free(guest); + /* Native audio submission/close is checked against SDL's dummy device; + * the codec fixture never plays through the user's speakers. */ + assert(SDL_SetHint(SDL_HINT_AUDIO_DRIVER, "dummy")); + assert(SDL_InitSubSystem(SDL_INIT_AUDIO)); + movie = mac_bink_open_path(argv[1], 1); + assert(movie); + /* Full soundtrack is ready while the device remains paused before the + * first video frame. Slow video decode cannot starve this queue. */ + assert(mac_bink_evidence(movie)->pcm_samples == 1542270); + assert(mac_bink_audio_pending(movie) == 1542270 * (int)sizeof(float)); + for (unsigned i = 0; i < 240; ++i) { assert(mac_bink_decode(movie)); mac_bink_next(movie); } + assert(mac_bink_evidence(movie)->nonzero_pcm_samples > 0); + assert(mac_bink_start_playback(movie)); + uint64_t audio_deadline = SDL_GetTicks() + 18000; + while (mac_bink_audio_pending(movie) > 0 && SDL_GetTicks() < audio_deadline) SDL_Delay(10); + assert(mac_bink_audio_pending(movie) == 0); + assert(mac_bink_evidence(movie)->pcm_samples == 1542270); + puts("native BINK dummy device consumed all 1542270 PCM samples without video progression"); + mac_bink_close(movie); + SDL_Quit(); puts("native BINK five-DWORD wire, timing, pitched copy, bad bounds and close passed"); return 0; +} diff --git a/port/macos/tests/host_frame_metrics_test.c b/port/macos/tests/host_frame_metrics_test.c new file mode 100644 index 0000000000..9502b26c63 --- /dev/null +++ b/port/macos/tests/host_frame_metrics_test.c @@ -0,0 +1,31 @@ +#include "host_frame_metrics.h" +#include +#include + +int main(void) +{ + struct halo_frame_metrics metrics = {0}; + uint64_t timestamp = 1000000000; + assert(!halo_frame_metrics_add(&metrics, timestamp, 0)); + for (unsigned i = 0; i < HALO_FRAME_SAMPLE_COUNT; ++i) + { + timestamp += 16666667; + assert(halo_frame_metrics_add(&metrics, timestamp, 1000000) == (i == 119)); + } + assert(metrics.elapsed_ns == UINT64_C(2000000040)); + assert(metrics.swap_ns == UINT64_C(120000000)); + assert(halo_frame_metrics_percentile(&metrics, 99) == 16666667); + halo_frame_metrics_reset_window(&metrics); + assert(metrics.count == 0 && metrics.previous_ns == timestamp); + for (unsigned i = 1; i <= HALO_FRAME_SAMPLE_COUNT; ++i) + { + timestamp += i; + (void)halo_frame_metrics_add(&metrics, timestamp, 0); + } + assert(halo_frame_metrics_percentile(&metrics, 50) == 60); + assert(halo_frame_metrics_percentile(&metrics, 95) == 114); + assert(halo_frame_metrics_percentile(&metrics, 99) == 119); + assert(halo_frame_metrics_percentile(&metrics, 100) == 120); + puts("host_frame_metrics_test: ok"); + return 0; +} diff --git a/port/macos/tests/host_imports_test.c b/port/macos/tests/host_imports_test.c new file mode 100644 index 0000000000..40913f92f7 --- /dev/null +++ b/port/macos/tests/host_imports_test.c @@ -0,0 +1,67 @@ +#include "guest_address.h" +#include "guest_import_registry.h" +#include "host_imports.h" +#include "host_services.h" +#include "recomp_types.h" + +#include +#include +#include +#include +#include + +#define TEST_BASE UINT32_C(0x62000000) +#define TEST_SIZE (16u * 1024u) + +_Thread_local uint32_t g_esp; +_Thread_local uint32_t g_eax; +_Thread_local uint32_t g_edx; + +int main(void) +{ + unsigned char arena[TEST_SIZE] = { 0 }; + uint32_t return_va = UINT32_C(0x12345678); + uint64_t syscall_frame[7] = { 172, 0, 0, 0, 0, 0, 0 }; + const char *manifest[] = { "host_get_tp", "host_set_tp", "host_syscall" }; + const mac_guest_import_provider providers[] = { mac_macos_host_import_bridge }; + mac_guest_function bridge; + + assert(mac_guest_address_register(TEST_BASE, arena, sizeof(arena)) == 0); + assert(mac_guest_host_imports_install_recomp_tls() == 0); + assert(mac_guest_import_register_manifest(manifest, 3, providers, 1, NULL) == 0); + g_esp = TEST_BASE + 0x100; + memcpy(arena + 0x100, &return_va, sizeof(return_va)); + memcpy(arena + 0x104, syscall_frame, sizeof(syscall_frame)); + + bridge = mac_guest_import_resolve(mac_guest_import_token("host_syscall")); + assert(bridge != NULL); + bridge(); + assert(g_esp == TEST_BASE + 0x104); /* cdecl callee pops return only */ + assert(g_eax == (uint32_t)getpid()); + assert(g_edx == 0); + + g_esp = TEST_BASE + 0x200; + memcpy(arena + 0x200, &return_va, sizeof(return_va)); + *(uint32_t *)(void *)(arena + 0x204) = UINT32_C(0x76543210); + bridge = mac_guest_import_resolve(mac_guest_import_token("host_set_tp")); + bridge(); + assert(mac_host_get_guest_tp() == UINT32_C(0x76543210)); + assert(g_esp == TEST_BASE + 0x204); + + g_esp = TEST_BASE + 0x300; + memcpy(arena + 0x300, &return_va, sizeof(return_va)); + bridge = mac_guest_import_resolve(mac_guest_import_token("host_get_tp")); + bridge(); + assert(g_eax == UINT32_C(0x76543210)); + assert(g_esp == TEST_BASE + 0x304); + + assert(mac_macos_host_import_bridge("host_sdl_gl_make_current") != NULL); + assert(mac_macos_host_import_bridge("host_sdl_open_audio_stream") != NULL); + assert(mac_macos_host_import_bridge("host_memory_watch_generation") == NULL); + assert(mac_macos_host_import_bridge("host_import_that_does_not_exist") == NULL); + + mac_guest_import_reset(); + mac_guest_address_reset(); + puts("host_imports_test: ok"); + return 0; +} diff --git a/port/macos/tests/host_memory_fingerprint_test.c b/port/macos/tests/host_memory_fingerprint_test.c new file mode 100644 index 0000000000..5592959edc --- /dev/null +++ b/port/macos/tests/host_memory_fingerprint_test.c @@ -0,0 +1,163 @@ +#include "host_memory_fingerprint.h" +#include "guest_address.h" +#include +#include +#include +#include +#include +#include + +/* Original guest algorithm, retained independently for bit-exact checks. */ +static void original(const volatile unsigned char *bytes, uint64_t output[2]) +{ + uint64_t a = UINT64_C(14695981039346656037), b = UINT64_C(0x9e3779b97f4a7c15); + for (unsigned i = 0; i < 4096; ++i) + { + uint64_t value = bytes[i]; + a = (a ^ value) * UINT64_C(1099511628211); + b ^= value + UINT64_C(0x9e3779b97f4a7c15) + (b << 6) + (b >> 2); + b *= UINT64_C(0xbf58476d1ce4e5b9); + } + output[0] = a; output[1] = b; +} + +int main(void) +{ + unsigned char memory[8192] = {0}; + uint64_t expected[2], actual[2], before[2]; + const uint32_t base = 0x30000000; + assert(mac_guest_address_register(base, memory, sizeof(memory)) == 0); + for (unsigned pass = 0; pass < 4; ++pass) + { + unsigned offset = pass == 1 ? 1 : 0; + if (pass == 1) + { + uint32_t state = 0x12345678; + for (unsigned i = 0; i < 4097; ++i) + { state = state * 1664525u + 1013904223u; memory[i] = (unsigned char)(state >> 24); } + } + if (pass == 2) memory[0] ^= 0x80; + if (pass == 3) memory[4095] ^= 0x01; + original(memory + offset, expected); + assert(mac_host_memory_fingerprint_page(base + offset, base + 5001)); + memcpy(actual, memory + 5001, sizeof(actual)); + assert(!memcmp(actual, expected, sizeof(actual))); + if (pass == 3) assert(memcmp(actual, before, sizeof(actual)) != 0); + memcpy(before, actual, sizeof(before)); + } + /* Every byte may change without prepare_write. All such writes must be + * detected, and unchanged scans must remain exactly bit-identical. */ + struct halo_fingerprint_stats stats_before, stats_after; + mac_host_memory_fingerprint_stats(&stats_before); + for (unsigned i = 0; i < 32; ++i) assert(mac_host_memory_fingerprint_page(base, base + 5001)); + mac_host_memory_fingerprint_stats(&stats_after); + assert(stats_after.identical - stats_before.identical == 32); + for (unsigned i = 0; i < 4096; ++i) + { + memcpy(before, memory + 5001, sizeof(before)); + memory[i] ^= 1; + original(memory, expected); + assert(mac_host_memory_fingerprint_page(base, base + 5001)); + memcpy(actual, memory + 5001, sizeof(actual)); + assert(!memcmp(actual, expected, sizeof(actual))); + assert(memcmp(actual, before, sizeof(actual))); + } + /* Four colliding pages should coexist. A fifth evicts the oldest, whose + * next query must rehash rather than incorrectly reuse another page. */ + const unsigned collision_stride = 8 * 1024 * 1024; + const size_t collision_size = 4 * collision_stride + 4096; + unsigned char *collision = calloc(1, collision_size); + const uint32_t collision_base = 0x40001000; + assert(collision && !mac_guest_address_register(collision_base, collision, collision_size)); + for (unsigned i = 0; i < 5; ++i) collision[i * collision_stride] = (unsigned char)(i + 1); + mac_host_memory_fingerprint_stats(&stats_before); + for (unsigned i = 0; i < 4; ++i) + assert(mac_host_memory_fingerprint_page(collision_base + i * collision_stride, base + 5001)); + for (unsigned i = 0; i < 4; ++i) + assert(mac_host_memory_fingerprint_page(collision_base + i * collision_stride, base + 5001)); + mac_host_memory_fingerprint_stats(&stats_after); + assert(stats_after.identical - stats_before.identical == 4); + assert(stats_after.collisions == stats_before.collisions); + for (unsigned i = 0; i < 10; ++i) + { + unsigned offset = (i % 5) * collision_stride; + original(collision + offset, expected); + assert(mac_host_memory_fingerprint_page(collision_base + offset, base + 5001)); + memcpy(actual, memory + 5001, sizeof(actual)); + assert(!memcmp(actual, expected, sizeof(actual))); + } + mac_host_memory_fingerprint_stats(&stats_after); + assert(stats_after.collisions - stats_before.collisions >= 6); + /* Compare batched range generations with the original independent page + * hashes for overlapping subranges, dirty and explicitly invalidated pages. */ + unsigned char *range = calloc(1, 32768); + const uint32_t range_base = 0x50000000; + assert(range && !mac_guest_address_register(range_base, range, 32768)); + struct wire_entry { uint64_t hashes[2]; uint32_t generation; unsigned char valid, padding[3]; }; + _Static_assert(sizeof(struct wire_entry) == 24, "guest fingerprint wire"); + struct wire_entry reference[4] = {{0}}; + uint32_t reference_counter = 1, counter = 1; + memcpy(range + 24000, &counter, 4); + for (unsigned pass = 0; pass < 8; ++pass) + { + unsigned begin = pass % 3, pages = 4 - begin; + if (pass == 1) range[4096 + 9] ^= 0x80; + if (pass == 3) range[3 * 4096 + 4095] ^= 0x01; + if (pass == 5) + { + reference[2].valid = 0; + range[20000 + 2 * 24 + 20] = 0; + ++reference_counter; + memcpy(range + 24000, &reference_counter, 4); + } + uint32_t newest = 0, native_newest; + for (unsigned page = begin; page < begin + pages; ++page) + { + original(range + page * 4096, expected); + if (!reference[page].valid || memcmp(reference[page].hashes, expected, 16)) + { + memcpy(reference[page].hashes, expected, 16); + reference[page].valid = 1; + reference[page].generation = ++reference_counter; + } + if (reference[page].generation > newest) newest = reference[page].generation; + } + assert(mac_host_memory_fingerprint_range(range_base + begin * 4096, pages, + range_base + 20000 + begin * 24, range_base + 24000, range_base + 24004)); + memcpy(&native_newest, range + 24004, 4); + memcpy(&counter, range + 24000, 4); + assert(native_newest == newest && counter == reference_counter); + assert(!memcmp(range + 20000, reference, sizeof(reference))); + } + unsigned char unchanged[104]; memcpy(unchanged, range + 20000, 96); memcpy(unchanged + 96, range + 24000, 8); + assert(!mac_host_memory_fingerprint_range(range_base + 30000, 2, range_base + 20000, range_base + 24000, range_base + 24004)); + assert(!mac_host_memory_fingerprint_range(range_base, 2, range_base + 32760, range_base + 24000, range_base + 24004)); + assert(!mac_host_memory_fingerprint_range(range_base, 2, range_base + 20000, range_base + 24001, range_base + 24004)); + assert(!mac_host_memory_fingerprint_range(range_base, 0, range_base + 20000, range_base + 24000, range_base + 24004)); + assert(!memcmp(unchanged, range + 20000, 96) && !memcmp(unchanged + 96, range + 24000, 8)); + /* A focused repeated-static-page benchmark compares exactly the same + * checked hash output with the former dependent-multiply byte scan. */ + const unsigned iterations = 10000; + struct timespec start, end; + clock_gettime(CLOCK_MONOTONIC, &start); + for (unsigned i = 0; i < iterations; ++i) original(memory, expected); + clock_gettime(CLOCK_MONOTONIC, &end); + double original_ms = (end.tv_sec-start.tv_sec)*1000.0 + (end.tv_nsec-start.tv_nsec)/1e6; + clock_gettime(CLOCK_MONOTONIC, &start); + for (unsigned i = 0; i < iterations; ++i) assert(mac_host_memory_fingerprint_page(base, base + 5001)); + clock_gettime(CLOCK_MONOTONIC, &end); + double cached_ms = (end.tv_sec-start.tv_sec)*1000.0 + (end.tv_nsec-start.tv_nsec)/1e6; + memcpy(actual, memory + 5001, sizeof(actual)); + assert(!memcmp(actual, expected, sizeof(actual))); + printf("10000 full-page validations: original %.3f ms snapshot %.3f ms\n",original_ms,cached_ms); + memset(memory + 5001, 0xa5, 16); + assert(!mac_host_memory_fingerprint_page(base + 4097, base + 5001)); + for (unsigned i = 5001; i < 5017; ++i) assert(memory[i] == 0xa5); + assert(!mac_host_memory_fingerprint_page(base, base + 8180)); + assert(!mac_host_memory_fingerprint_page(0, base + 5001)); + assert(!mac_host_memory_fingerprint_page(base, 0)); + assert(!mac_host_memory_fingerprint_page(0xfffff001, base + 5001)); + mac_guest_address_reset(); free(collision); free(range); + puts("host_memory_fingerprint_test: exact dual hashes, all-byte mutations, repeated validation, collisions, alignment and bounds passed"); + return 0; +} diff --git a/port/macos/tests/host_sdl_audio_test.c b/port/macos/tests/host_sdl_audio_test.c new file mode 100644 index 0000000000..cb873b3e27 --- /dev/null +++ b/port/macos/tests/host_sdl_audio_test.c @@ -0,0 +1,205 @@ +#include "guest_address.h" +#include "guest_allocator.h" +#include "guest_callback.h" +#include "guest_call.h" +#include "host_services.h" +#include "host_sdl_audio.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#define TEST_GUEST_BASE UINT32_C(0x12000000) +#define TEST_GUEST_SIZE (1024u * 1024u) +#define TEST_ATTACH_VA UINT32_C(0x00401000) +#define TEST_CALLBACK_VA UINT32_C(0x00402000) +#define TEST_TP UINT32_C(0x12001000) +#define TEST_SPEC_VA UINT32_C(0x12002000) +#define TEST_PCM_VA UINT32_C(0x12003000) +#define TEST_PCM_BYTES 4096u + +struct guest_sdl_audio_spec +{ + uint32_t format; + int32_t channels; + int32_t frequency; +}; + +static unsigned char guest_memory[TEST_GUEST_SIZE]; +static uint32_t allocation_cursor = TEST_GUEST_BASE + 0x10000; +static pthread_mutex_t callback_mutex = PTHREAD_MUTEX_INITIALIZER; +static pthread_cond_t callback_cond = PTHREAD_COND_INITIALIZER; +static unsigned callback_count; +static unsigned successful_puts; +static uint32_t expected_userdata; +static uint32_t observed_stream; +static uint32_t observed_additional; +static uint32_t observed_total; +static unsigned movie_callbacks; +static void SDLCALL movie_callback(void *userdata, SDL_AudioStream *stream, int additional, int total) +{ + (void)userdata; (void)total; + unsigned char pcm[8192] = {0}; + assert(additional >= 0); + while (additional > 0) { + int bytes = additional < (int)sizeof(pcm) ? additional : (int)sizeof(pcm); + assert(SDL_PutAudioStreamData(stream, pcm, bytes)); + additional -= bytes; + } + pthread_mutex_lock(&callback_mutex); + ++movie_callbacks; + pthread_mutex_unlock(&callback_mutex); +} + +static int test_allocate(void *context, size_t size, size_t alignment, + uint32_t *guest_va_out) +{ + uint64_t aligned = ((uint64_t)allocation_cursor + alignment - 1) & ~(uint64_t)(alignment - 1); + (void)context; + if (!guest_va_out || aligned + size > TEST_GUEST_BASE + TEST_GUEST_SIZE) + return -1; + *guest_va_out = (uint32_t)aligned; + allocation_cursor = (uint32_t)(aligned + size); + return 0; +} + +static void test_release(void *context, uint32_t guest_va, size_t size) +{ + (void)context; + (void)guest_va; + (void)size; +} + +static void test_dispatch(void *context, uint32_t guest_function_va) +{ + uint32_t esp, frame[5]; + (void)context; + if (guest_function_va == TEST_ATTACH_VA) + { + mac_host_set_guest_tp(TEST_TP); + return; + } + assert(guest_function_va == TEST_CALLBACK_VA); + /* The callback dispatcher has installed the guest ESP into its runtime; + * read it from the test worker's scratch slot populated by initialize. */ + extern _Thread_local uint32_t test_guest_esp; + esp = test_guest_esp; + assert(mac_guest_read(esp, frame, sizeof(frame)) == 0); + assert(frame[1] == expected_userdata); + pthread_mutex_lock(&callback_mutex); + observed_stream = frame[2]; + observed_additional = frame[3]; + observed_total = frame[4]; + int delay_callback = callback_count == 4; + ++callback_count; + pthread_cond_broadcast(&callback_cond); + pthread_mutex_unlock(&callback_mutex); + if (delay_callback) + SDL_Delay(100); + assert(mac_host_sdl_put_audio_stream_data(observed_stream, TEST_PCM_VA, + (int)observed_additional) == 1); + pthread_mutex_lock(&callback_mutex); + ++successful_puts; + pthread_cond_broadcast(&callback_cond); + pthread_mutex_unlock(&callback_mutex); +} + +_Thread_local uint32_t test_guest_esp; + +static void test_initialize_with_esp(void *context, uint32_t guest_thread_va, uint32_t guest_esp) +{ + (void)context; + (void)guest_thread_va; + test_guest_esp = guest_esp; +} + +static int wait_for_callbacks(unsigned minimum) +{ + struct timespec deadline; + clock_gettime(CLOCK_REALTIME, &deadline); + deadline.tv_sec += 3; + pthread_mutex_lock(&callback_mutex); + while (callback_count < minimum) + { + int error = pthread_cond_timedwait(&callback_cond, &callback_mutex, &deadline); + if (error != 0) + break; + } + int ready = callback_count >= minimum; + pthread_mutex_unlock(&callback_mutex); + return ready; +} + +int main(void) +{ + struct mac_guest_allocator allocator = { NULL, test_allocate, test_release }; + struct mac_guest_thread_runtime runtime = { NULL, test_initialize_with_esp, test_dispatch }; + struct guest_sdl_audio_spec spec = { SDL_AUDIO_S16LE, 2, 22050 }; + const char *driver = getenv("HALO_AUDIO_TEST_DRIVER"); + uint32_t stream; + + assert(mac_guest_address_register(TEST_GUEST_BASE, guest_memory, sizeof(guest_memory)) == 0); + assert(mac_guest_allocator_install(&allocator) == 0); + assert(mac_guest_callback_runtime_install(&runtime, TEST_ATTACH_VA) == 0); + assert(mac_guest_write(TEST_SPEC_VA, &spec, sizeof(spec)) == 0); + memset(guest_memory + (TEST_PCM_VA - TEST_GUEST_BASE), 0, TEST_PCM_BYTES); + expected_userdata = 0x55667788; + assert(setenv("HALO_AUDIO_ENABLE", "1", 1) == 0); + if (!driver || !*driver) + driver = "dummy"; + assert(SDL_SetHint(SDL_HINT_AUDIO_DRIVER, driver)); + assert(SDL_Init(SDL_INIT_AUDIO)); + assert(SDL_GetCurrentAudioDriver() && strcmp(SDL_GetCurrentAudioDriver(), driver) == 0); + + stream = mac_host_sdl_open_audio_stream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, + TEST_SPEC_VA, TEST_CALLBACK_VA, expected_userdata); + assert(stream != 0); + SDL_AudioSpec movie_spec = {SDL_AUDIO_F32, 2, 48000}; + SDL_AudioStream *movie = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, + &movie_spec, movie_callback, NULL); + assert(movie && SDL_ResumeAudioStreamDevice(movie)); + assert(mac_host_sdl_resume_audio_stream_device(stream) == 1); + assert(wait_for_callbacks(5)); + pthread_mutex_lock(&callback_mutex); + unsigned before_movie = movie_callbacks; + pthread_mutex_unlock(&callback_mutex); + SDL_Delay(70); /* Guest mixing remains stalled for 100ms. */ + pthread_mutex_lock(&callback_mutex); + unsigned during_stall = movie_callbacks - before_movie; + pthread_mutex_unlock(&callback_mutex); + assert(during_stall >= 2); /* Shared physical output keeps servicing movie. */ + pthread_mutex_lock(&callback_mutex); + assert(observed_stream == stream); + assert(observed_additional > 0 && observed_total >= observed_additional); + pthread_mutex_unlock(&callback_mutex); + pthread_mutex_lock(&callback_mutex); + unsigned before_shutdown = movie_callbacks; + pthread_mutex_unlock(&callback_mutex); + mac_host_sdl_audio_shutdown(); + SDL_Delay(70); + pthread_mutex_lock(&callback_mutex); + assert(movie_callbacks >= before_shutdown + 2); + unsigned before_pause = movie_callbacks; + pthread_mutex_unlock(&callback_mutex); + assert(SDL_PauseAudioStreamDevice(movie)); + SDL_Delay(50); + pthread_mutex_lock(&callback_mutex); + assert(movie_callbacks <= before_pause + 1); + pthread_mutex_unlock(&callback_mutex); + assert(SDL_ResumeAudioStreamDevice(movie)); + SDL_Delay(70); + pthread_mutex_lock(&callback_mutex); + assert(movie_callbacks >= before_pause + 2); + pthread_mutex_unlock(&callback_mutex); + assert(successful_puts >= 1); + SDL_DestroyAudioStream(movie); + SDL_Quit(); + mac_guest_address_reset(); + puts("async audio: 100ms guest stall preserves movie callbacks; shutdown/pause/resume pass"); + return 0; +} diff --git a/port/macos/tests/host_sdl_test.c b/port/macos/tests/host_sdl_test.c new file mode 100644 index 0000000000..dfeab0a97e --- /dev/null +++ b/port/macos/tests/host_sdl_test.c @@ -0,0 +1,86 @@ +#include "guest_address.h" +#include "host_sdl.h" + +#include +#include +#include +#include +#include +#include +#include + +int main(void) +{ + const uint32_t guest_base = 0x30000000u; + const size_t guest_size = 0x4000; + void *guest_memory = mmap(NULL, guest_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + SDL_Event event; + SDL_Event *guest_event; + + assert(guest_memory != MAP_FAILED); + assert(mac_guest_address_register(guest_base, guest_memory, guest_size) == 0); + assert(mac_host_sdl_init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)); + + /* Window objects remain tokens; logical dimensions are separate from + * Retina pixel dimensions and bad guest outputs must fail unchanged. */ + strcpy(guest_memory, "Halo bridge test"); + uint32_t window = mac_host_sdl_create_window(guest_base, 640, 480, SDL_WINDOW_HIDDEN); + assert(window != 0); + uint32_t width_va = guest_base + 256, height_va = guest_base + 260; + int *width = mac_guest_address_resolve(width_va, sizeof(*width)); + int *height = mac_guest_address_resolve(height_va, sizeof(*height)); + assert(mac_host_sdl_window_size(window, width_va, height_va)); + assert(*width == 640 && *height == 480); + assert((mac_host_sdl_window_flags(window) & SDL_WINDOW_HIDDEN) != 0); + assert(!mac_host_sdl_set_window_size(0,1280,720)); + assert(!mac_host_sdl_set_window_size(window,0,720)); + assert(!mac_host_sdl_set_window_size(window,1280,-1)); + const int sizes[][2]={{1280,720},{1920,1080},{640,480}}; + for(unsigned i=0;i<3;i++) { + assert(mac_host_sdl_set_window_size(window,sizes[i][0],sizes[i][1])); + SDL_PumpEvents(); + assert(mac_host_sdl_window_size(window,width_va,height_va)); + assert(*width==sizes[i][0] && *height==sizes[i][1]); + assert(mac_host_sdl_window_size_in_pixels(window,width_va,height_va)); + assert(*width>0 && *height>0); + } + *width = 123; + assert(!mac_host_sdl_window_size(window, width_va, 0)); + assert(*width == 123); + assert(!mac_host_sdl_window_size(0, width_va, height_va)); + assert(mac_host_sdl_window_flags(0) == 0); + assert(!mac_host_sdl_set_window_fullscreen(0, 1)); + assert(!mac_host_sdl_warp_mouse(0, 320.0f, 240.0f)); + assert(mac_host_sdl_set_window_fullscreen(window, 1)); + assert((mac_host_sdl_window_flags(window) & SDL_WINDOW_FULLSCREEN) != 0); + assert(mac_host_sdl_set_window_fullscreen(window, 0)); + assert((mac_host_sdl_window_flags(window) & SDL_WINDOW_FULLSCREEN) == 0); + assert(mac_host_sdl_warp_mouse(window, 320.5f, 240.25f)); + /* Window creation queues unrelated events before the fixture below. */ + while (SDL_PollEvent(&event)) {} + + /* Pointer-bearing user events are discarded; the next supported event is + * copied into guest memory with native pointer fields never exposed. */ + memset(&event, 0, sizeof(event)); + event.type = SDL_EVENT_USER; + event.user.data1 = (void *)(uintptr_t)0x12345678; + assert(SDL_PushEvent(&event)); + memset(&event, 0, sizeof(event)); + event.type = SDL_EVENT_KEY_DOWN; + event.key.key = SDLK_RETURN; + event.key.down = true; + assert(SDL_PushEvent(&event)); + + assert(mac_host_sdl_poll_event(guest_base)); + guest_event = mac_guest_address_resolve(guest_base, sizeof(*guest_event)); + assert(guest_event); + assert(guest_event->type == SDL_EVENT_KEY_DOWN); + assert(guest_event->key.key == SDLK_RETURN); + assert(guest_event->key.down); + + SDL_Quit(); + mac_guest_address_reset(); + puts("host_sdl_test: ok"); + return 0; +} diff --git a/port/macos/tests/host_services_test.c b/port/macos/tests/host_services_test.c new file mode 100644 index 0000000000..80c789d637 --- /dev/null +++ b/port/macos/tests/host_services_test.c @@ -0,0 +1,64 @@ +#include "../guest_address.h" +#include "../host_services.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +int main(void) +{ + unsigned char guest_memory[512] = { 0 }; + char host_path[] = "/tmp/halo-host-services-XXXXXX"; + const char payload[] = "guest-buffer-io"; + uint32_t guest_timespec = 0x10000100u; + uint32_t guest_timeval = 0x10000108u; + int32_t seconds, subsecond; + int fd, guest_fd; + + fd = mkstemp(host_path); + assert(fd >= 0); + assert(close(fd) == 0); + assert(mac_guest_address_register(0x10000000u, guest_memory, + sizeof(guest_memory)) == 0); + strcpy((char *)guest_memory, host_path); + memcpy(guest_memory + 0x40, payload, sizeof(payload)); + + guest_fd = (int)mac_host_openat(AT_FDCWD, 0x10000000u, 2u | 0x00001000u, 0600u); + assert(guest_fd >= 0); + assert(mac_host_pwrite(guest_fd, 0x10000040u, sizeof(payload), 0) == (int64_t)sizeof(payload)); + assert(mac_host_pread(guest_fd, 0x10000080u, sizeof(payload), 0) == (int64_t)sizeof(payload)); + assert(memcmp(guest_memory + 0x80, payload, sizeof(payload)) == 0); + assert(mac_host_read(guest_fd, 0x10000FFFu, 8) == -EFAULT); + assert(mac_host_openat(AT_FDCWD, 0x10000000u, 2u | 0x00040000u, 0600u) == -EINVAL); + assert(mac_host_close(guest_fd) == 0); + + assert(mac_host_clock_gettime(1, guest_timespec) == 0); + memcpy(&seconds, guest_memory + 0x100, sizeof(seconds)); + memcpy(&subsecond, guest_memory + 0x104, sizeof(subsecond)); + assert(seconds > 0 && subsecond >= 0 && subsecond < 1000000000); + assert(mac_host_gettimeofday(guest_timeval) == 0); + memcpy(&seconds, guest_memory + 0x108, sizeof(seconds)); + assert(seconds > 0); + assert(mac_host_clock_gettime(99, guest_timespec) == -EINVAL); + /* The time64 record fully replaces stale seconds/nanoseconds high words. */ + memset(guest_memory + 0x140, 0xCA, 16); + assert(mac_host_clock_gettime64(1, 0x10000140u) == 0); + int64_t seconds64, nanoseconds64; + memcpy(&seconds64, guest_memory + 0x140, 8); + memcpy(&nanoseconds64, guest_memory + 0x148, 8); + assert(seconds64 > 0 && nanoseconds64 >= 0 && nanoseconds64 < 1000000000); + assert(mac_host_clock_gettime64(99, 0x10000140u) == -EINVAL); + assert(mac_host_clock_gettime64(1, 0x100001F8u) == -EFAULT); + assert(mac_host_clock_gettime64(1, 0) == -EFAULT); + assert(mac_host_thread_id() != 0); + mac_host_set_guest_tp(0x12345678u); + assert(mac_host_get_guest_tp() == 0x12345678u); + assert(unlink(host_path) == 0); + assert(mac_guest_address_unregister(0x10000000u) == 0); + return 0; +} diff --git a/port/macos/tests/host_syscall_test.c b/port/macos/tests/host_syscall_test.c new file mode 100644 index 0000000000..2e910fc6c5 --- /dev/null +++ b/port/macos/tests/host_syscall_test.c @@ -0,0 +1,292 @@ +#include "guest_address.h" +#include "guest_call.h" +#include "host_syscall.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define TEST_BASE UINT32_C(0x61000000) +#define TEST_SIZE (64u * 1024u) + +struct guest_timespec32 +{ + int32_t seconds; + int32_t nanoseconds; +}; + +struct guest_iovec32 +{ + uint32_t base; + uint32_t length; +}; + +static void interrupted_sleep(int signal_number) { (void)signal_number; } +static void *interrupt_sleeper(void *opaque) +{ + pthread_t *target = opaque; + struct timespec delay = {0, 2000000}; + nanosleep(&delay, NULL); + assert(pthread_kill(*target, SIGUSR1) == 0); + return NULL; +} + +static uint32_t futex_word_va = TEST_BASE + 0x1000; +static volatile int32_t *futex_word; +static int64_t waiter_result; + +struct vm_test_state +{ + uint64_t observed_offset; + uint32_t mapped_address; +}; + +static int64_t test_vm_map(void *context, uint32_t address, uint32_t size, + int protection, uint32_t flags, int fd, uint64_t offset, + uint32_t *mapped_guest_va_out) +{ + struct vm_test_state *state = context; + (void)address; + (void)size; + (void)protection; + (void)flags; + (void)fd; + state->observed_offset = offset; + *mapped_guest_va_out = state->mapped_address; + return 0; +} + +static int64_t test_vm_unmap(void *context, uint32_t address, uint32_t size) +{ + (void)context; + (void)address; + (void)size; + return 0; +} + +static int64_t test_vm_protect(void *context, uint32_t address, uint32_t size, + int protection) +{ + (void)context; + (void)address; + (void)size; + (void)protection; + return 0; +} + +static void *futex_waiter(void *unused) +{ + uint64_t arguments[6] = { futex_word_va, 0, 0, 0, 0, 0 }; + (void)unused; + waiter_result = mac_host_arm64_32_syscall(98, arguments); + return NULL; +} + +int main(void) +{ + void *memory = mmap(NULL, TEST_SIZE, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + char *guest_memory; + uint64_t arguments[6] = { 0 }; + uint64_t syscall_slots[7] = { 0 }; + uint8_t call_stack[4 + 7 * 8] = { 0 }; + uint32_t guest_esp = TEST_BASE + 0x200; + int fd; + char template[] = "/tmp/halo-host-syscall-XXXXXX"; + char payload[] = "native-guest-io"; + char readback[sizeof(payload)] = { 0 }; + struct guest_iovec32 guest_iov[2]; + struct guest_timespec32 *timespec; + pthread_t thread; + struct vm_test_state vm_state = { 0, TEST_BASE + 0x2000 }; + struct mac_guest_vm_ops vm_ops = { + &vm_state, test_vm_map, test_vm_unmap, test_vm_protect + }; + + assert(memory != MAP_FAILED); + assert(mac_guest_address_register(TEST_BASE, memory, TEST_SIZE) == 0); + guest_memory = memory; + futex_word = (int32_t *)(guest_memory + 0x1000); + + /* The checked cdecl reader consumes the i386 long-long syscall frame. */ + { + uint32_t return_va = UINT32_C(0x12345678); + memcpy(call_stack, &return_va, sizeof(return_va)); + } + syscall_slots[0] = 172; /* arm64_32 getpid */ + memcpy(call_stack + 4, syscall_slots, sizeof(syscall_slots)); + memcpy(guest_memory + 0x200, call_stack, sizeof(call_stack)); + assert(mac_host_arm64_32_syscall_from_stack(guest_esp) == (int64_t)getpid()); + assert(mac_guest_stack_arg64(guest_esp, 0, &syscall_slots[0]) == 0); + assert(syscall_slots[0] == 172); + + /* arm64_32 syscall IDs retain checked ILP32 guest path/data spans. */ + fd = mkstemp(template); + assert(fd >= 0); + close(fd); + unlink(template); + memcpy(guest_memory + 0x400, template, strlen(template) + 1); + arguments[0] = (uint32_t)-100; /* AT_FDCWD */ + arguments[1] = TEST_BASE + 0x400; + arguments[2] = 2u | 0100u; /* O_RDWR | O_CREAT */ + arguments[3] = 0600; + fd = (int)mac_host_arm64_32_syscall(56, arguments); + assert(fd >= 0); + memcpy(guest_memory + 0x800, payload, sizeof(payload)); + guest_iov[0] = (struct guest_iovec32){ TEST_BASE + 0x800, 6 }; + guest_iov[1] = (struct guest_iovec32){ TEST_BASE + 0x806, + (uint32_t)sizeof(payload) - 6 }; + memcpy(guest_memory + 0xB00, guest_iov, sizeof(guest_iov)); + arguments[0] = (uint32_t)fd; + arguments[1] = TEST_BASE + 0xB00; + arguments[2] = 2; + assert(mac_host_arm64_32_syscall(66, arguments) == (int64_t)sizeof(payload)); + assert(mac_host_arm64_32_syscall(62, (uint64_t[6]){ (uint32_t)fd, 0, 0 }) == 0); + arguments[1] = TEST_BASE + 0x900; + arguments[2] = sizeof(payload); + assert(mac_host_arm64_32_syscall(63, arguments) == (int64_t)sizeof(payload)); + /* readv validates every destination before consuming any file bytes. */ + assert(mac_host_arm64_32_syscall(62, (uint64_t[6]){ (uint32_t)fd, 0, 0 }) == 0); + memset(guest_memory + 0xC00, 0xAA, sizeof(payload)); + guest_iov[0] = (struct guest_iovec32){ TEST_BASE + 0xC00, 6 }; + guest_iov[1] = (struct guest_iovec32){ TEST_BASE + TEST_SIZE, 4 }; + memcpy(guest_memory + 0xB00, guest_iov, sizeof(guest_iov)); + assert(mac_host_arm64_32_syscall(65, (uint64_t[6]){ (uint32_t)fd, TEST_BASE + 0xB00, 2 }) == -14); + assert((unsigned char)guest_memory[0xC00] == 0xAA); + assert(mac_host_arm64_32_syscall(62, (uint64_t[6]){ (uint32_t)fd, 0, 1 }) == 0); + guest_iov[1] = (struct guest_iovec32){ TEST_BASE + 0xC06, (uint32_t)sizeof(payload) - 6 }; + memcpy(guest_memory + 0xB00, guest_iov, sizeof(guest_iov)); + assert(mac_host_arm64_32_syscall(65, (uint64_t[6]){ (uint32_t)fd, TEST_BASE + 0xB00, 2 }) == sizeof(payload)); + assert(memcmp(guest_memory + 0xC00, payload, sizeof(payload)) == 0); + assert(mac_host_arm64_32_syscall(65, (uint64_t[6]){ (uint32_t)fd, 0, UINT32_MAX }) == -22); + guest_iov[0].length = UINT32_C(0x80000000); + memcpy(guest_memory + 0xB00, guest_iov, sizeof(guest_iov)); + assert(mac_host_arm64_32_syscall(65, (uint64_t[6]){ (uint32_t)fd, TEST_BASE + 0xB00, 1 }) == -22); + guest_iov[0] = (struct guest_iovec32){ TEST_BASE + 0x800, 6 }; + memcpy(readback, guest_memory + 0x900, sizeof(readback)); + assert(memcmp(readback, payload, sizeof(payload)) == 0); + /* Positioned I/O uses arm64_32 syscall IDs with 64-bit byte offsets, + * while buffer VAs and the pointed-to data remain guest32. */ + memset(guest_memory + 0xA80, 0, sizeof(payload)); + arguments[0] = (uint32_t)fd; + arguments[1] = TEST_BASE + 0x800; + arguments[2] = sizeof(payload); + arguments[3] = UINT64_C(0x100000020); + assert(mac_host_arm64_32_syscall(68, arguments) == (int64_t)sizeof(payload)); + arguments[1] = TEST_BASE + 0xA80; + assert(mac_host_arm64_32_syscall(67, arguments) == (int64_t)sizeof(payload)); + assert(memcmp(guest_memory + 0xA80, payload, sizeof(payload)) == 0); + arguments[1] = TEST_BASE + TEST_SIZE - 1; + arguments[2] = 2; + assert(mac_host_arm64_32_syscall(68, arguments) == -14); /* bad guest span */ + arguments[1] = TEST_BASE + 0x800; + arguments[2] = sizeof(payload); + arguments[3] = UINT64_MAX; + assert(mac_host_arm64_32_syscall(67, arguments) == -22); /* invalid offset */ + guest_iov[1] = (struct guest_iovec32){ TEST_BASE + TEST_SIZE - 1, 8 }; + memcpy(guest_memory + 0xB00, guest_iov, sizeof(guest_iov)); + arguments[1] = TEST_BASE + 0xB00; + assert(mac_host_arm64_32_syscall(66, arguments) == -14); /* invalid late span: no partial write */ + assert(mac_host_arm64_32_syscall(66, (uint64_t[6]){ UINT32_MAX, + TEST_BASE + TEST_SIZE, 0 }) == -9); /* count zero still checks fd */ + assert(mac_host_arm64_32_syscall(57, (uint64_t[6]){ (uint32_t)fd }) == 0); + unlink(template); + + /* A clock result is marshalled into the guest's 32-bit timespec layout. */ + arguments[0] = 1; /* CLOCK_MONOTONIC */ + arguments[1] = TEST_BASE + 0xA00; + assert(mac_host_arm64_32_syscall(113, arguments) == 0); + timespec = (struct guest_timespec32 *)(guest_memory + 0xA00); + assert(timespec->seconds >= 0 && timespec->nanoseconds >= 0 && + timespec->nanoseconds < 1000000000); + /* Hybrid syscall 403 selects the complete 16-byte Linux time64 record. */ + memset(guest_memory + 0xA20, 0xCA, 16); + arguments[1] = TEST_BASE + 0xA20; + assert(mac_host_arm64_32_syscall(403, arguments) == 0); + int64_t seconds64, nanoseconds64; + memcpy(&seconds64, guest_memory + 0xA20, 8); + memcpy(&nanoseconds64, guest_memory + 0xA28, 8); + assert(seconds64 >= 0 && nanoseconds64 >= 0 && nanoseconds64 < 1000000000); + arguments[1] = TEST_BASE + TEST_SIZE - 8; + assert(mac_host_arm64_32_syscall(403, arguments) == -14); + /* Legacy115 reads8 bytes; time64 extension407 reads16. Both sleep + * until real absolute deadlines rather than reporting dummy success. */ + struct timespec before, after; + clock_gettime(CLOCK_MONOTONIC, &before); + int64_t deadline64[2] = {before.tv_sec, before.tv_nsec + 20000000}; + if (deadline64[1] >= 1000000000) { ++deadline64[0]; deadline64[1] -= 1000000000; } + memcpy(guest_memory + 0xA40, deadline64, sizeof(deadline64)); + memset(guest_memory + 0xA60, 0xCA, 16); + assert(mac_host_arm64_32_syscall(407, (uint64_t[6]){1, 1, TEST_BASE + 0xA40, TEST_BASE + 0xA60}) == 0); + clock_gettime(CLOCK_MONOTONIC, &after); + assert((after.tv_sec - before.tv_sec) * INT64_C(1000000000) + after.tv_nsec - before.tv_nsec >= 20000000); + assert((unsigned char)guest_memory[0xA60] == 0xCA); /* absolute rem untouched */ + struct guest_timespec32 span32 = {0, 2000000}; + memcpy(guest_memory + 0xA40, &span32, sizeof(span32)); + assert(mac_host_arm64_32_syscall(115, (uint64_t[6]){1, 0, TEST_BASE + 0xA40, 0}) == 0); + assert(mac_host_arm64_32_syscall(407, (uint64_t[6]){1, 0, TEST_BASE + TEST_SIZE - 8, 0}) == -14); + assert(mac_host_arm64_32_syscall(407, (uint64_t[6]){3, 0, TEST_BASE + 0xA40, 0}) == -22); + deadline64[0] = INT64_C(0x100000000); deadline64[1] = 1000000000; + memcpy(guest_memory + 0xA40, deadline64, sizeof(deadline64)); + assert(mac_host_arm64_32_syscall(407, (uint64_t[6]){1, 0, TEST_BASE + 0xA40, 0}) == -22); + deadline64[0] = -1; deadline64[1] = 0; + memcpy(guest_memory + 0xA40, deadline64, sizeof(deadline64)); + assert(mac_host_arm64_32_syscall(407, (uint64_t[6]){1, 0, TEST_BASE + 0xA40, 0}) == -22); + deadline64[0] = 0; deadline64[1] = -1; + memcpy(guest_memory + 0xA40, deadline64, sizeof(deadline64)); + assert(mac_host_arm64_32_syscall(407, (uint64_t[6]){1, 0, TEST_BASE + 0xA40, 0}) == -22); + assert(mac_host_arm64_32_syscall(115, (uint64_t[6]){1, 2, TEST_BASE + 0xA40, 0}) == -22); + struct sigaction action = {0}, previous; + action.sa_handler = interrupted_sleep; + sigemptyset(&action.sa_mask); + assert(sigaction(SIGUSR1, &action, &previous) == 0); + pthread_t target = pthread_self(), interrupter; + deadline64[0] = 0; deadline64[1] = 20000000; + memcpy(guest_memory + 0xA40, deadline64, sizeof(deadline64)); + assert(pthread_create(&interrupter, NULL, interrupt_sleeper, &target) == 0); + assert(mac_host_arm64_32_syscall(407, (uint64_t[6]){1, 0, TEST_BASE + 0xA40, TEST_BASE + 0xA60}) == -4); + assert(pthread_join(interrupter, NULL) == 0); + int64_t remaining64[2]; memcpy(remaining64, guest_memory + 0xA60, sizeof(remaining64)); + assert(remaining64[0] == 0 && remaining64[1] > 0 && remaining64[1] < 20000000); + assert(sigaction(SIGUSR1, &previous, NULL) == 0); + /* mmap is the arm64_32 number 222 and its offset is in bytes, not pages. */ + assert(mac_guest_vm_ops_install(&vm_ops) == 0); + arguments[0] = 0; + arguments[1] = 4096; + arguments[2] = 3; + arguments[3] = 2; + arguments[4] = (uint32_t)-1; + arguments[5] = 12345; + assert(mac_host_arm64_32_syscall(222, arguments) == vm_state.mapped_address); + assert(vm_state.observed_offset == 12345); + + /* Futex wake is address-specific and the wait uses the expected word. */ + *futex_word = 0; + assert(pthread_create(&thread, NULL, futex_waiter, NULL) == 0); + { + struct timespec pause = { 0, 10000000 }; + nanosleep(&pause, NULL); + } + *futex_word = 1; + arguments[0] = futex_word_va; + arguments[1] = 1; /* FUTEX_WAKE */ + arguments[2] = 1; + assert(mac_host_arm64_32_syscall(98, arguments) == 1); + pthread_join(thread, NULL); + assert(waiter_result == 0); + + assert(mac_host_arm64_32_syscall(9999, arguments) == -38); /* ENOSYS */ + arguments[0] = TEST_BASE + TEST_SIZE - 4; + arguments[1] = 0xFFFFFFFFu; + assert(mac_host_arm64_32_syscall(63, arguments) == -14); /* EFAULT */ + + mac_guest_address_reset(); + puts("host_syscall_test: ok"); + return 0; +} diff --git a/port/macos/tests/input_bindings_test.c b/port/macos/tests/input_bindings_test.c new file mode 100644 index 0000000000..c5365f3f67 --- /dev/null +++ b/port/macos/tests/input_bindings_test.c @@ -0,0 +1,21 @@ +#include "../../linux/src/input_bindings.h" +#include +#include +#include +int main(void) +{ + struct input_binding binding; + unsigned char keys[SDL_SCANCODE_COUNT]={0}, buttons[6]={0}; + assert(input_binding_parse(" e , R, Mouse2",&binding)); + keys[SDL_SCANCODE_E]=1; assert(input_binding_down(&binding,keys,buttons,0)); + keys[SDL_SCANCODE_E]=0; buttons[2]=1; + assert(!input_binding_down(&binding,keys,buttons,0)); + assert(input_binding_down(&binding,keys,buttons,1)); + assert(input_binding_parse("Tab,Wheel",&binding) && binding.wheel); + assert(input_binding_parse("",&binding) && !input_binding_down(&binding,keys,buttons,1)); + assert(!input_binding_parse("Mouse0",&binding)); + assert(!input_binding_parse("Space,",&binding)); + assert(!input_binding_parse("unknown",&binding)); + assert(!input_binding_parse("A,B,C,D,E,F,G,H,I",&binding)); + puts("input_bindings_test: ok"); +} diff --git a/port/macos/tests/memory_watch_fallback_test.c b/port/macos/tests/memory_watch_fallback_test.c new file mode 100644 index 0000000000..bccafa40c7 --- /dev/null +++ b/port/macos/tests/memory_watch_fallback_test.c @@ -0,0 +1,53 @@ +#include +#include +#include +#include + +#include "memory_watch_test_platform.h" + +unsigned char halo_watch_test_arena[0x10000]; + +void memory_watch_initialize(void); +void memory_watch_protect(unsigned long address, unsigned long size); +unsigned long memory_watch_generation(unsigned long address, unsigned long size); +unsigned long memory_watch_serial(void); +void memory_watch_prepare_write(void *address, unsigned long size); +void memory_watch_forget(void *address, unsigned long size); + +int platform_is_contiguous(const void *address) +{ + uintptr_t value = (uintptr_t)address; + uintptr_t base = (uintptr_t)halo_watch_test_arena; + return value >= base && value - base < sizeof(halo_watch_test_arena); +} + +int main(void) +{ + unsigned long address = (unsigned long)halo_watch_test_arena + 0x1ff0; + unsigned long before, unchanged, changed, prepared, forgotten; + + memset(halo_watch_test_arena, 0, sizeof(halo_watch_test_arena)); + memory_watch_initialize(); + before = memory_watch_generation(address, 32); + unchanged = memory_watch_generation(address, 32); + assert(before != 0); + assert(unchanged == before); + + halo_watch_test_arena[0x2005] = 0x5a; + changed = memory_watch_generation(address, 32); + assert(changed > unchanged); + assert(memory_watch_generation(address, 32) == changed); + + memory_watch_prepare_write((void *)address, 32); + prepared = memory_watch_generation(address, 32); + assert(prepared > changed); + memory_watch_forget((void *)address, 32); + forgotten = memory_watch_generation(address, 32); + assert(forgotten > prepared); + + assert(memory_watch_generation(address, PLATFORM_CONTIGUOUS_SIZE) == 0); + assert(memory_watch_generation(address + PLATFORM_CONTIGUOUS_SIZE, 1) == 0); + assert(memory_watch_serial() != memory_watch_serial()); + puts("macOS content-fingerprint memory-watch fallback: PASS"); + return 0; +} diff --git a/port/macos/tests/memory_watch_test_platform.h b/port/macos/tests/memory_watch_test_platform.h new file mode 100644 index 0000000000..7b06354bac --- /dev/null +++ b/port/macos/tests/memory_watch_test_platform.h @@ -0,0 +1,18 @@ +#ifndef HALO_MEMORY_WATCH_TEST_PLATFORM_H +#define HALO_MEMORY_WATCH_TEST_PLATFORM_H + +#include + +#ifndef TRUE +#define TRUE 1 +#define FALSE 0 +typedef int BOOL; +#endif + +extern unsigned char halo_watch_test_arena[]; +#define PLATFORM_CONTIGUOUS_BASE ((unsigned long)halo_watch_test_arena) +#define PLATFORM_CONTIGUOUS_SIZE 0x10000UL + +BOOL platform_is_contiguous(const void *address); + +#endif diff --git a/port/macos/tests/posix_host_imports_test.c b/port/macos/tests/posix_host_imports_test.c new file mode 100644 index 0000000000..d2257d4bce --- /dev/null +++ b/port/macos/tests/posix_host_imports_test.c @@ -0,0 +1,237 @@ +#include "../guest_address.h" +#include "../guest_call.h" +#include "../host_imports.h" +#include "../posix_host_imports.h" +#include "../../linux/src/posix.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* This isolated adapter test records native errno. Actual translated TLS + * behavior is exercised by translated_errno_test.c, not this leaf. */ +static int recorded_file_errno; +int mac_guest_errno_set_native(int error){recorded_file_errno=error;return 0;} + +static int alias_mode; +static uint32_t active_esp; +static uint32_t eax_value; + +uint32_t mac_guest_host_import_esp(void) { return active_esp; } +uint32_t mac_guest_host_import_arg32(uint32_t index) +{ + uint32_t value; + assert(mac_guest_stack_arg32(active_esp, index, &value) == 0); + return value; +} +uint64_t mac_guest_host_import_arg64_words(uint32_t index) +{ + return (uint64_t)mac_guest_host_import_arg32(index) | + ((uint64_t)mac_guest_host_import_arg32(index + 1) << 32); +} +void mac_guest_host_import_return32(uint32_t value) +{ + uint32_t next; + eax_value = value; + assert(mac_guest_stack_after_return(active_esp, 0, &next) == 0); + active_esp = next; +} +void mac_guest_host_import_return64(int64_t value) { mac_guest_host_import_return32((uint32_t)value); } +void mac_guest_host_import_return_void(void) +{ + uint32_t next; + assert(mac_guest_stack_after_return(active_esp, 0, &next) == 0); + active_esp = next; +} +void mac_host_abort(const char *message) { fprintf(stderr, "abort: %s\n", message); abort(); } + +static int32_t invoke(const char *name, const uint32_t *args, size_t count) +{ + uint32_t frame[10] = {0xdeadbeef}; + mac_guest_import_bridge bridge = mac_macos_posix_host_import_bridge(name); + assert(bridge && count <= 9); + memcpy(frame + 1, args, count * 4); + assert(mac_guest_write(0x1400, frame, (count + 1) * 4) == 0); + active_esp = 0x1400; bridge(); assert(active_esp == 0x1404); + return (int32_t)eax_value; +} +#define CALL(name, ...) invoke(#name, (uint32_t[]){__VA_ARGS__}, sizeof((uint32_t[]){__VA_ARGS__}) / 4) +static void put32(uint32_t va, uint32_t value) { assert(!mac_guest_write(va, &value, 4)); } +static uint32_t get32(uint32_t va) { uint32_t value; assert(!mac_guest_read(va, &value, 4)); return value; } +static void network_roundtrips(void) +{ + /* Linux/Xbox wire sockaddr, deliberately not Darwin sockaddr_in. */ + uint8_t address[16] = {2,0,0,0,127,0,0,1}; + uint8_t actual[16], peer[16]; + if(alias_mode) address[7]=2; + char text[8]; + int udp, sender, listener, client, accepted; + assert(!mac_guest_write(0x2000, address, 16)); + assert(!mac_guest_write(0x2300, "protocol", 8)); + udp = CALL(posix_socket, 2, 2, 17); sender = CALL(posix_socket, 2, 2, 17); + assert(udp >= 0 && sender >= 0); + assert(CALL(posix_socket_bind, udp, 0x2000, 15) == -1); + assert(CALL(posix_socket_last_error, 0) == 10022); + assert(CALL(posix_socket_bind, udp, 0x2000, 16) == 0); + put32(0x2200, 16); + assert(CALL(posix_socket_getsockname, udp, 0x2100, 0x2200) == 0 && get32(0x2200) == 16); + assert(!mac_guest_read(0x2100, actual, 16)); + assert(actual[0] == 2 && actual[1] == 0 && actual[7] == (alias_mode?2:1) && (actual[2] || actual[3])); + assert(CALL(posix_socket_sendto, sender, 0x2300, 8, 0, 0x2100, 16) == 8); + put32(0x2500, udp); put32(0x2510, 1); put32(0x2520, sender); put32(0x2530, 1); put32(0x2550, 0); + assert(CALL(posix_socket_select, 0x2500, 0x2510, 0, 0, 0, 0, 1, 0, 0) == 1); + put32(0x2510, 1); + assert(CALL(posix_socket_select, 0x2500, 0x2510, 0x2520, 0x2530, 0, 0x2550, 1, 0, 0) == 2); + assert(get32(0x2510) == 1 && get32(0x2500) == (uint32_t)udp); + assert(get32(0x2530) == 1 && get32(0x2520) == (uint32_t)sender); + assert(CALL(posix_socket_bytes_available, udp, 0x2560) == 0 && get32(0x2560) >= 8); + put32(0x2200, 16); + assert(CALL(posix_socket_recvfrom, udp, 0x2400, 8, 0, 0x10ff9, 0x2200) == -1); + assert(CALL(posix_socket_last_error, 0) == 10014); + assert(CALL(posix_socket_recvfrom, udp, 0x2400, 8, 0, 0x2000, 0x2200) == 8); + assert(!mac_guest_read(0x2400, text, 8) && !memcmp(text, "protocol", 8)); + assert(!mac_guest_read(0x2000, peer, 16) && peer[0] == 2 && peer[1] == 0); + /* Actual connected UDP reproduces Darwin's EISCONN at this boundary. */ + assert(CALL(posix_socket_connect, sender, 0x2100, 16) == 0); + put32(0x2200, 16); assert(CALL(posix_socket_getpeername, sender, 0x2800, 0x2200) == 0); + assert(!mac_guest_read(0x2800, actual, 16)); assert(actual[7] == (alias_mode?2:1)); + struct sockaddr_in native_peer; socklen_t native_length = sizeof(native_peer); + assert(getpeername(sender, (struct sockaddr *)&native_peer, &native_length) == 0); + assert(sendto(sender, "protocol", 8, 0, (struct sockaddr *)&native_peer, native_length) == -1 && errno == EISCONN); + assert(CALL(posix_socket_sendto, sender, 0x2300, 8, 0, 0x2100, 16) == 8); + put32(0x2200,16); assert(CALL(posix_socket_recvfrom, udp, 0x2400, 8, 0, 0x2000, 0x2200) == 8); + assert(!mac_guest_read(0x2400,text,8) && !memcmp(text,"protocol",8)); + uint8_t alternate[16]; assert(!mac_guest_read(0x2100,alternate,16)); alternate[3] ^= 1; + assert(!mac_guest_write(0x2900,alternate,16)); + assert(CALL(posix_socket_sendto, sender, 0x2300, 8, 0, 0x2900, 16) == -1); + assert(CALL(posix_socket_last_error, 0) == 10056); + assert(CALL(posix_socket_bytes_available, udp, 0x2560) == 0 && get32(0x2560) == 0); + put32(0x2200,16);assert(CALL(posix_socket_getpeername,sender,0x2800,0x2200)==0); + assert(!mac_guest_read(0x2800,actual,16) && actual[2]==native_peer.sin_port%256 && actual[3]==native_peer.sin_port/256); + put32(0x2200, 16); assert(CALL(posix_socket_getsockname, sender, 0x2100, 0x2200) == 0); + assert(!mac_guest_read(0x2100, actual, 16) && !memcmp(peer, actual, 4)); + assert(CALL(posix_socket_close, sender) == 0 && CALL(posix_socket_close, udp) == 0); + assert(!mac_guest_write(0x2000, address, 16)); + listener = CALL(posix_socket, 2, 1, 6); client = CALL(posix_socket, 2, 1, 6); + assert(listener >= 0 && client >= 0); + assert(CALL(posix_socket_bind, listener, 0x2000, 16) == 0); + assert(CALL(posix_socket_listen, listener, 1) == 0); + put32(0x2200, 16); assert(CALL(posix_socket_getsockname, listener, 0x2100, 0x2200) == 0); + /* Two servers cannot own the same fixed endpoint. Diagnostics must retain + the actual Darwin-to-Winsock error instead of obscuring EADDRINUSE. */ + assert(setenv("HALO_NETWORK_TRACE", "1", 1) == 0); + int contender = CALL(posix_socket, 2, 1, 6); + assert(contender >= 0 && CALL(posix_socket_bind, contender, 0x2100, 16) == -1); + assert(CALL(posix_socket_last_error, 0) == 10048); + assert(CALL(posix_socket_listen, -1, 1) == -1); + assert(CALL(posix_socket_last_error, 0) == 10009); + assert(CALL(posix_socket_connect, -1, 0x2100, 16) == -1); + assert(CALL(posix_socket_last_error, 0) == 10009); + assert(CALL(posix_socket_close, contender) == 0); + assert(unsetenv("HALO_NETWORK_TRACE") == 0); + assert(CALL(posix_socket_connect, client, 0x2100, 16) == 0); + put32(0x2200, 16); accepted = CALL(posix_socket_accept, listener, 0x2000, 0x2200); + assert(accepted >= 0 && get32(0x2200) == 16); + assert(!mac_guest_read(0x2000, peer, 16) && peer[0] == 2 && peer[1] == 0); + assert(CALL(posix_socket_set_nodelay, client) == 0); + put32(0x2540, accepted); put32(0x2550, 1); + assert(CALL(posix_socket_select, 0, 0, 0, 0, 0x2540, 0x2550, 0, 0, 0) == 0); + assert(get32(0x2550) == 0); + assert(CALL(posix_socket_send, client, 0x2300, 8, 0) == 8); + assert(CALL(posix_socket_recv, accepted, 0x2400, 8, 0) == 8); + assert(!mac_guest_read(0x2400, text, 8) && !memcmp(text, "protocol", 8)); + put32(0x2200, 16); assert(CALL(posix_socket_getpeername, accepted, 0x2100, 0x2200) == 0); + assert(!mac_guest_read(0x2100, actual, 16) && !memcmp(peer, actual, 8)); + memset(actual, 0xcc, 16); assert(!mac_guest_write(0x2100, actual, 16)); + put32(0x2200, 2); assert(CALL(posix_socket_getsockname, client, 0x2100, 0x2200) == 0 && get32(0x2200) == 16); + assert(!mac_guest_read(0x2100, actual, 16) && actual[0] == 2 && actual[1] == 0 && actual[2] == 0xcc); + assert(CALL(posix_socket_close, accepted) == 0 && CALL(posix_socket_close, client) == 0 && CALL(posix_socket_close, listener) == 0); +} + +int main(int argc, char **argv) +{ + if(argc==2 && !strcmp(argv[1],"--alias")) { + alias_mode=1; + assert(!setenv("HALO_LOCAL_TEST_IPV4_ALIAS","127.0.0.2",1)); + assert(!setenv("HALO_NET_ONLINE","false",1)); + assert(!setenv("HALO_NET_ADDRESS","127.0.0.2",1)); + } + else if(argc==2) { + uint8_t memory[256]={0}, wire[16]={2,0,0,0,127,0,0,2}; int socket; + assert(!setenv("HALO_LOCAL_TEST_IPV4_ALIAS", !strcmp(argv[1],"--invalid-ip")?"192.168.1.2":"127.0.0.2",1)); + assert(!setenv("HALO_NET_ONLINE", !strcmp(argv[1],"--online")?"true":"false",1)); + assert(!setenv("HALO_NET_ADDRESS", !strcmp(argv[1],"--collision")?"127.0.0.1":"127.0.0.2",1)); + assert(!mac_guest_address_register(0x1400,memory,sizeof(memory))); + assert(!mac_guest_write(0x1480,wire,sizeof(wire))); + socket=CALL(posix_socket,2,2,17); assert(socket>=0); + assert(CALL(posix_socket_bind,socket,0x1480,16)==-1); + assert(CALL(posix_socket_last_error,0)==10022); + assert(CALL(posix_socket_close,socket)==0); + mac_guest_address_reset(); puts("local test alias rejects invalid/online/collision setup"); return 0; + } + + uint8_t *memory = calloc(1, 0x10000); + char temp_path[] = "/tmp/halo-posix-bridge-XXXXXX"; + char lookup_directory[] = "/tmp/halo-posix-find-XXXXXX"; + char lookup_path[256]; + int fd = mkstemp(temp_path); + uint32_t frame[5] = { 0xdeadbeef, 0x1200, 0x1300, 0, 0 }; + struct posix_file_information info; + mac_guest_import_bridge stat_bridge; + assert(memory && fd >= 0); + assert(sizeof(struct posix_file_information) == 36); + assert(offsetof(struct posix_file_information, modification_seconds) == 12); + assert(mac_guest_address_register(0x1000, memory, 0x10000) == 0); + strcpy((char *)memory + 0x200, temp_path); + assert(write(fd, "bridge", 6) == 6); + close(fd); + memcpy(memory + 0x400, frame, sizeof(frame)); + active_esp = 0x1400; + stat_bridge = mac_macos_posix_host_import_bridge("posix_stat"); + assert(stat_bridge); + stat_bridge(); + assert(active_esp == 0x1404 && (int32_t)eax_value == 0); + memcpy(&info, memory + 0x300, sizeof(info)); + assert(info.size_low == 6 && info.size_high == 0); + assert(info.modification_seconds != 0); + strcpy((char *)memory + 0x200, "/nonexistent-halo-errno-fixture/file"); + recorded_file_errno=0;active_esp=0x1400;stat_bridge(); + assert((int32_t)eax_value == -1 && recorded_file_errno == ENOENT); + strcpy((char *)memory + 0x200, temp_path); + assert(mkdtemp(lookup_directory) != NULL); + assert(snprintf(lookup_path, sizeof(lookup_path), "%s/%s", lookup_directory, + "Map Cache.bin") > 0); + fd = open(lookup_path, O_WRONLY | O_CREAT | O_EXCL, 0600); + assert(fd >= 0 && write(fd, "x", 1) == 1); + close(fd); + strcpy((char *)memory + 0x200, lookup_directory); + strcpy((char *)memory + 0x300, "map cache.BIN"); + frame[0] = 0xdeadbeef; frame[1] = 0x1200; frame[2] = 0x1300; + frame[3] = 0x1500; frame[4] = 64; + memcpy(memory + 0x400, frame, sizeof(frame)); + active_esp = 0x1400; + stat_bridge = mac_macos_posix_host_import_bridge("posix_find_entry_case_insensitive"); + assert(stat_bridge); + stat_bridge(); + assert(active_esp == 0x1404 && eax_value == 1); + assert(strcmp((char *)memory + 0x500, "Map Cache.bin") == 0); + network_roundtrips(); + assert(mac_macos_posix_host_import_bridge("posix_upnp_stop_forwarding_udp")); + assert(CALL(posix_upnp_forward_udp, 0, 0x10fff, 0x2200, 0x2300, 64) == 0); + assert(mac_macos_posix_host_import_bridge("posix_spawn") == NULL); + assert(mac_guest_address_unregister(0x1000) == 0); + assert(unlink(temp_path) == 0); + assert(unlink(lookup_path) == 0); + assert(rmdir(lookup_directory) == 0); + free(memory); + puts("posix_host_imports_test: ok"); + return 0; +} diff --git a/port/macos/tests/recomp_state_test.c b/port/macos/tests/recomp_state_test.c new file mode 100644 index 0000000000..3c40168e7c --- /dev/null +++ b/port/macos/tests/recomp_state_test.c @@ -0,0 +1,26 @@ +#include "recomp_state.h" +#define RECOMP_GENERATED_CODE 1 +#include + +#include +#include + +int main(void) +{ + assert(mac_recomp_state_set_image((ptrdiff_t)0x12345000, 0x10000, 0x90000) == 0); + assert(g_xbox_mem_offset == (ptrdiff_t)0x12345000); + assert(g_xbox_code_lo == 0x10000 && g_xbox_code_hi == 0x90000); + assert(mac_recomp_state_set_image(0, 0x20000, 0x10000) != 0); + + g_eax = 1; g_esp = 2; g_fp_stack[3] = 4; g_xmm2.u[1] = 5; + g_mm7.q = 6; g_fs_base = 7; g_df = 1; + mac_recomp_state_initialize_thread(0x7fff0000, 0x700000); + assert(g_eax == 0 && g_esp == 0x7fff0000 && g_fs_base == 0x700000); + assert(g_fp_stack[3] == 0.0 && g_xmm2.u[1] == 0 && g_mm7.q == 0); + assert(g_df == 0 && g_fp_control_word == 0x037f && g_fp_empty_mask == 0xff); + assert(g_mxcsr == UINT32_C(0x1f80)); + g_mxcsr = UINT32_C(0x5f80); + mac_recomp_state_initialize_thread(0x7fff0000, 0x700000); + assert(g_mxcsr == UINT32_C(0x1f80)); + return 0; +} diff --git a/port/macos/tests/recomp_types.h b/port/macos/tests/recomp_types.h new file mode 100644 index 0000000000..29f86657a5 --- /dev/null +++ b/port/macos/tests/recomp_types.h @@ -0,0 +1,7 @@ +#ifndef HALO_MACOS_TEST_RECOMP_TYPES_H +#define HALO_MACOS_TEST_RECOMP_TYPES_H +#include +extern _Thread_local uint32_t g_eax; +extern _Thread_local uint32_t g_edx; +extern _Thread_local uint32_t g_esp; +#endif diff --git a/port/macos/tests/translated_clock_test.c b/port/macos/tests/translated_clock_test.c new file mode 100644 index 0000000000..1f96bd1b36 --- /dev/null +++ b/port/macos/tests/translated_clock_test.c @@ -0,0 +1,81 @@ +#define RECOMP_GENERATED_CODE +#include "recomp_funcs.h" +#include "guest_image.h" +#include "guest_address.h" +#include "recomp_state.h" +#include "host_syscall.h" +#include +#include +#include +#include +#include + +static void unexpected(uint32_t va) +{ + fprintf(stderr, "unexpected guest call %08x\n", va); + abort(); +} +void recomp_unsupported_instruction(uint32_t va) { unexpected(va); } +void recomp_icall_fail_log(uint32_t va) { unexpected(va); } +void recomp_icall_not_code_log(uint32_t va) { unexpected(va); } +recomp_func_t recomp_lookup_manual(uint32_t va) { (void)va; return NULL; } +recomp_func_t recomp_lookup(uint32_t va) { (void)va; return NULL; } +recomp_func_t recomp_lookup_kernel(uint32_t va) { (void)va; return NULL; } +void host_get_tp(void) { g_eax = 0x60002000; g_esp += 4; } +void host_syscall(void) +{ + int64_t result = mac_host_arm64_32_syscall_from_stack(g_esp); + g_eax = (uint32_t)result; + g_edx = (uint32_t)((uint64_t)result >> 32); + g_esp += 4; +} + +static int64_t monotonic_microseconds(void) +{ + struct timespec value; + assert(clock_gettime(CLOCK_MONOTONIC, &value) == 0); + return (int64_t)value.tv_sec * 1000000 + value.tv_nsec / 1000; +} + +int main(int argc, char **argv) +{ + assert(argc == 2); + FILE *file = fopen(argv[1], "rb"); + assert(file); + fseek(file, 0, SEEK_END); + long size = ftell(file); + rewind(file); + void *bytes = malloc((size_t)size); + assert(fread(bytes, 1, (size_t)size, file) == (size_t)size); + fclose(file); + struct mac_guest_image image; + char error[256]; + assert(mac_guest_image_load(bytes, (size_t)size, &image, error, sizeof(error)) == 0); + assert(mac_guest_address_commit(0x60000000, 0x10000, PROT_READ | PROT_WRITE) == 0); + assert(mac_recomp_state_set_image(image.memory_offset, image.code_lo_va, image.code_hi_va) == 0); + int64_t previous = 0; + for (unsigned index = 0; index < 8; ++index) + { + /* Stale stack bytes exposed the original short time32 write. */ + memset((void *)XBOX_PTR(0x60000000), 0xCA, 0x10000); + mac_recomp_state_initialize_thread(0x6000f000, 0x60002000); + MEM32(g_esp) = 0; + MEM32(g_esp + 4) = 0x60000100; + int64_t before = monotonic_microseconds(); + HALO_QPC_FUNCTION(); + int64_t after = monotonic_microseconds(); + int64_t counter = SMEM64(0x60000100); + assert(g_eax == 1 && g_esp == 0x6000f008); + assert(counter >= before && counter <= after && counter >= previous); + previous = counter; + } + mac_recomp_state_initialize_thread(0x6000f000, 0x60002000); + MEM32(g_esp) = 0; + MEM32(g_esp + 4) = 0x60000100; + HALO_QPF_FUNCTION(); + assert(g_eax == 1 && SMEM64(0x60000100) == 1000000); + printf("actual translated QueryPerformanceCounter: 8 plausible monotonic samples; frequency1000000\n"); + mac_guest_address_reset(); + free(bytes); + return 0; +} diff --git a/port/macos/tests/translated_distributed_test.c b/port/macos/tests/translated_distributed_test.c new file mode 100644 index 0000000000..c5326aece0 --- /dev/null +++ b/port/macos/tests/translated_distributed_test.c @@ -0,0 +1,49 @@ +#define RECOMP_GENERATED_CODE +#include "recomp_funcs.h" +#include "guest_image.h" +#include "guest_address.h" +#include "recomp_state.h" +#include "host_syscall.h" +#include +#include +#include +#include +#include +#include + +static void unexpected(uint32_t va) +{ + fprintf(stderr, "unexpected guest call %08x\n", va); + abort(); +} +void recomp_unsupported_instruction(uint32_t va) { unexpected(va); } +void recomp_icall_fail_log(uint32_t va) { unexpected(va); } +void recomp_icall_not_code_log(uint32_t va) { unexpected(va); } +recomp_func_t recomp_lookup_manual(uint32_t va) { (void)va; return NULL; } +recomp_func_t recomp_lookup(uint32_t va) { (void)va; return NULL; } +recomp_func_t recomp_lookup_kernel(uint32_t va) { (void)va; return NULL; } +void host_get_tp(void) { g_eax = 0x60002000; g_esp += 4; } +void host_syscall(void) +{ + int64_t result = mac_host_arm64_32_syscall_from_stack(g_esp); + g_eax = (uint32_t)result; + g_edx = (uint32_t)((uint64_t)result >> 32); + g_esp += 4; +} + +static void decode(unsigned size){mac_recomp_state_initialize_thread(0x6000f000,0x60002000);MEM32(g_esp)=0;MEM32(g_esp+4)=1;MEM32(g_esp+8)=0x60001000;MEM32(g_esp+12)=size;TEST_network_distributed_handle_message();assert(g_esp==0x6000f004);} +int main(int argc,char **argv){ +assert(argc==2);FILE *f=fopen(argv[1],"rb");assert(f);fseek(f,0,SEEK_END);long n=ftell(f);rewind(f);void *bytes=malloc(n);assert(fread(bytes,1,n,f)==(size_t)n);fclose(f); +struct mac_guest_image image;char error[256];assert(mac_guest_image_load(bytes,n,&image,error,sizeof(error))==0);assert(mac_guest_address_commit(0x60000000,0x10000,PROT_READ|PROT_WRITE)==0);assert(mac_recomp_state_set_image(image.memory_offset,image.code_lo_va,image.code_hi_va)==0); +memset((void *)XBOX_PTR(TEST_distributed_predictions),0,8704);memset((void *)XBOX_PTR(TEST_distributed_statistics),0,12);memset((void *)XBOX_PTR(TEST_distributed_received_times),0xff,8772); +/* Actual protocol4 batch:8-byte outer header,2-byte child length, child +payload excludes its2-byte generic header. Prediction entry is64 bytes. */ +uint32_t packet=0x60001000;memset((void *)XBOX_PTR(packet),0,80);MEM16(packet)=((80<<4)|8);MEM8(packet+2)=16;MEM32(packet+4)=100; +MEM16(packet+8)=70;MEM8(packet+10)=1;MEM8(packet+11)=1;MEM32(packet+12)=100;MEM8(packet+16)=1;MEM8(packet+17)=1; +MEMF(packet+32)=12.5f;MEMF(packet+36)=-3.25f;MEMF(packet+40)=0.125f; +decode(80);uint32_t prediction=TEST_distributed_predictions+68;assert(MEM8(prediction)==1);assert(MEMF(prediction+20)==12.5f && MEMF(prediction+24)==-3.25f);assert(MEM32(TEST_distributed_statistics+4)==1); +MEM8(prediction)=0;MEM32(packet+12)=99;decode(80);assert(MEM8(prediction)==0); +MEM32(packet+12)=101;MEM16(packet+8)=71;decode(80);assert(MEM8(prediction)==0); +MEM16(packet+8)=70;MEM8(packet+16)=2;decode(80);assert(MEM8(prediction)==0); +MEM8(packet+16)=1;MEM32(packet+12)=102;decode(80);assert(MEM8(prediction)==1); +puts("Actual translated protocol4 batch/prediction decoder PASS: valid position, stale tick, malformed length, machine ownership, next valid tick");return 0;} diff --git a/port/macos/tests/translated_errno_test.c b/port/macos/tests/translated_errno_test.c new file mode 100644 index 0000000000..67c9bec6f5 --- /dev/null +++ b/port/macos/tests/translated_errno_test.c @@ -0,0 +1,67 @@ +#define RECOMP_GENERATED_CODE +#include "recomp_funcs.h" +#include "guest_image.h" +#include "guest_address.h" +#include "recomp_state.h" +#include "host_syscall.h" +#include +#include +#include +#include +#include "host_services.h" +#include "host_imports.h" +#include "posix_host_imports.h" +#include "guest_errno.h" +#include +#include +#include +#include +#include + +static void unexpected(uint32_t va) +{ + fprintf(stderr, "unexpected guest call %08x\n", va); + abort(); +} +void recomp_unsupported_instruction(uint32_t va) { unexpected(va); } +void recomp_icall_fail_log(uint32_t va) { unexpected(va); } +void recomp_icall_not_code_log(uint32_t va) { unexpected(va); } +recomp_func_t recomp_lookup_manual(uint32_t va) { (void)va; return NULL; } +extern void TEST___get_tp(void); +recomp_func_t recomp_lookup(uint32_t va) { return va == VA___get_tp ? TEST___get_tp : NULL; } +recomp_func_t recomp_lookup_kernel(uint32_t va) { (void)va; return NULL; } + +int fixture_file_errors; +uint32_t mac_guest_host_import_esp(void){return g_esp;} +uint32_t mac_guest_host_import_arg32(uint32_t i){return MEM32(g_esp+4+i*4);} +uint64_t mac_guest_host_import_arg64_words(uint32_t i){return (uint64_t)mac_guest_host_import_arg32(i)|((uint64_t)mac_guest_host_import_arg32(i+1)<<32);} +void mac_guest_host_import_return32(uint32_t value){g_eax=value;g_esp+=4;} +void mac_guest_host_import_return64(int64_t value){g_edx=(uint64_t)value>>32;mac_guest_host_import_return32(value);} +void mac_guest_host_import_return_void(void){g_esp+=4;} +void mac_guest_import_dispatch_token(uint32_t token){if(token==TOKEN_host_get_tp){mac_guest_host_import_return32(mac_host_get_guest_tp());return;}if(token==TOKEN_posix_stat){mac_macos_posix_host_import_bridge("posix_stat")();return;}unexpected(token);} +static uint32_t call(void(*f)(void),uint32_t arg,unsigned popped){mac_recomp_state_initialize_thread(0x6000f000,0x60002000);MEM32(g_esp)=0;MEM32(g_esp+4)=arg;f();assert(g_esp==0x6000f004+popped);return g_eax;} +struct thread_case {uint32_t tp,stack,seed;int error;}; +static void *errno_thread(void *opaque){ + struct thread_case *c=opaque;unsigned char scratch[256]; + mac_host_set_guest_tp(c->tp);mac_recomp_state_initialize_thread(c->stack,c->tp); + memset(scratch,(int)c->seed,sizeof(scratch));memcpy((void *)XBOX_PTR(c->stack-256),scratch,sizeof(scratch)); + g_eax=c->seed;g_edx=c->seed+1;g_ebx=c->seed+2;g_fp_stack[0]=3.125;g_xmm0.q[0]=0x1122334455667788ULL;g_mm7.q=0x8877665544332211ULL; + errno=EACCES;assert(mac_guest_errno_set_native(c->error)==0); + assert(MEM32(c->tp+28)==(uint32_t)mac_host_linux_errno(c->error)); + assert(g_eax==c->seed&&g_edx==c->seed+1&&g_ebx==c->seed+2&&g_esp==c->stack&&g_fp_stack[0]==3.125&&g_xmm0.q[0]==0x1122334455667788ULL&&g_mm7.q==0x8877665544332211ULL&&errno==EACCES); + assert(!memcmp((void *)XBOX_PTR(c->stack-256),scratch,sizeof(scratch))); + for(unsigned i=0;i<100;i++){assert(mac_guest_errno_set_native(c->error)==0);assert(MEM32(c->tp+28)==(uint32_t)mac_host_linux_errno(c->error));} + return NULL; +} +int main(int argc,char **argv){assert(argc==2);FILE *f=fopen(argv[1],"rb");assert(f);fseek(f,0,SEEK_END);long n=ftell(f);rewind(f);void *bytes=malloc(n);assert(fread(bytes,1,n,f)==(size_t)n);fclose(f); +struct mac_guest_image image;char error[256];assert(mac_guest_image_load(bytes,n,&image,error,sizeof(error))==0);assert(mac_guest_address_commit(0x60000000,0x10000,PROT_READ|PROT_WRITE)==0);assert(mac_recomp_state_set_image(image.memory_offset,image.code_lo_va,image.code_hi_va)==0); +mac_host_set_guest_tp(0x60002000);uint32_t error_address=call(TEST___errno_location,0,0);assert(error_address==0x6000201c); +strcpy((char *)XBOX_PTR(0x60005000),"/tmp/halo-errno-fixture-missing-unique");memset((void *)XBOX_PTR(0x60006000),0,300);strcpy((char *)XBOX_PTR(0x60006008),(char *)XBOX_PTR(0x60005000)); +/* Reproduce stale guest errno before installing the native writer. */ +MEM32(error_address)=9;assert(call(TEST_GetFileAttributesA,0x60005000,4)==UINT32_MAX);assert(MEM32(error_address)==9);assert(call(TEST_GetLastError,0,0)==6);fixture_file_errors=0;assert((call(TEST_file_exists,0x60006000,0)&255)==0&&fixture_file_errors==1); +assert(mac_guest_errno_install(TEST___errno_location)==0);MEM32(error_address)=9;assert(call(TEST_GetFileAttributesA,0x60005000,4)==UINT32_MAX);assert(MEM32(error_address)==2);assert(call(TEST_GetLastError,0,0)==2);fixture_file_errors=0;assert((call(TEST_file_exists,0x60006000,0)&255)==0&&fixture_file_errors==0); +mac_recomp_state_initialize_thread(0x6000f000,0x60002000);g_eax=11;g_ecx=22;g_edx=33;g_ebp=44;g_seh_ebp=55;errno=EBADF;assert(mac_guest_errno_set_native(EAGAIN)==0);assert(MEM32(error_address)==11&&errno==EBADF&&g_eax==11&&g_ecx==22&&g_edx==33&&g_ebp==44&&g_seh_ebp==55&&g_esp==0x6000f000); +mac_host_set_guest_tp(0x60003000);assert(mac_guest_errno_set_native(ENOENT)==0);assert(MEM32(0x6000301c)==2&&MEM32(error_address)==11);mac_host_set_guest_tp(0);assert(mac_guest_errno_set_native(EBADF)==-1);assert(MEM32(0x6000301c)==2); +pthread_t threads[2];struct thread_case cases[]={{0x60004000,0x6000b000,71,ENOENT},{0x60007000,0x6000d000,83,ENOTDIR}}; +for(unsigned i=0;i<2;i++)assert(!pthread_create(&threads[i],NULL,errno_thread,&cases[i]));for(unsigned i=0;i<2;i++)assert(!pthread_join(threads[i],NULL));assert(MEM32(0x6000401c)==2&&MEM32(0x6000701c)==20); +puts("Actual translated FileExists/native stat errno PASS: stale9 reproduced, missing2 corrected, no spurious file_error, Darwin EAGAIN→Linux11, register/stack/native errno preserved, two concurrent guest threads/TLS switch/init guard");return 0;} diff --git a/port/macos/tests/translated_event_switch_test.c b/port/macos/tests/translated_event_switch_test.c new file mode 100644 index 0000000000..2719f593f7 --- /dev/null +++ b/port/macos/tests/translated_event_switch_test.c @@ -0,0 +1,74 @@ +#define RECOMP_GENERATED_CODE +#include "recomp_funcs.h" +#include "guest_image.h" +#include "guest_address.h" +#include "recomp_state.h" +#include "host_syscall.h" +#include +#include +#include +#include +#include + +static void unexpected(uint32_t va) +{ + fprintf(stderr, "unexpected guest call %08x\n", va); + abort(); +} +void recomp_unsupported_instruction(uint32_t va) { unexpected(va); } +void recomp_icall_fail_log(uint32_t va) { unexpected(va); } +void recomp_icall_not_code_log(uint32_t va) { unexpected(va); } +recomp_func_t recomp_lookup_manual(uint32_t va) { (void)va; return NULL; } +recomp_func_t recomp_lookup(uint32_t va) { (void)va; return NULL; } +recomp_func_t recomp_lookup_kernel(uint32_t va) { (void)va; return NULL; } +void host_get_tp(void) { g_eax = 0x60002000; g_esp += 4; } +void host_syscall(void) +{ + int64_t result = mac_host_arm64_32_syscall_from_stack(g_esp); + g_eax = (uint32_t)result; + g_edx = (uint32_t)((uint64_t)result >> 32); + g_esp += 4; +} + +int main(int argc, char **argv) +{ + assert(argc == 2); + FILE *file = fopen(argv[1], "rb"); + assert(file); + fseek(file, 0, SEEK_END); + long size = ftell(file); + rewind(file); + void *bytes = malloc((size_t)size); + assert(fread(bytes, 1, (size_t)size, file) == (size_t)size); + fclose(file); + struct mac_guest_image image; + char error[256]; + assert(mac_guest_image_load(bytes, (size_t)size, &image, error, sizeof(error)) == 0); + assert(mac_guest_address_commit(0x60000000, 0x10000, PROT_READ | PROT_WRITE) == 0); + assert(mac_recomp_state_set_image(image.memory_offset, image.code_lo_va, image.code_hi_va) == 0); + const uint16_t deltas[] = { 0, 1, 2, 256 }; + for (unsigned arm = 0; arm < 4; ++arm) + for (unsigned short_ticks = 0; short_ticks < 2; ++short_ticks) + { + mac_recomp_state_initialize_thread(0x6000f000, 0x60002000); + g_ebp = 0x13579; g_esi = 0x24680; g_edi = 0x35791; + MEM8(0x60000200) = (uint8_t)(4 + arm); /* playback_end, each time-delta arm */ + MEM16(0x60000201) = deltas[arm]; + MEM32(0x60000100) = (uint32_t)((int32_t)deltas[arm] - short_ticks); + MEM32(0x60000110) = 0x60000200; + MEM32(g_esp) = 0; MEM32(g_esp + 4) = 0; + MEM32(g_esp + 8) = 0x60000300; + MEM32(g_esp + 12) = 0x60000100; + MEM32(g_esp + 16) = 0x60000110; + HALO_EVENT_FUNCTION(); + assert((g_eax & 255) == short_ticks); + assert(g_esp == 0x6000f004 && g_ebp == 0x13579); + assert(g_esi == 0x24680 && g_edi == 0x35791); + assert(MEM32(0x60000110) == 0x60000200); + assert(MEM32(0x60000100) == (uint32_t)((int32_t)deltas[arm] - short_ticks)); + } + printf("actual event stream switch: all4arms/8cases preserve sameframe, ticks, stream, booleanresult\n"); + mac_guest_address_reset(); + free(bytes); + return 0; +} diff --git a/port/macos/tests/translated_gamepad_test.c b/port/macos/tests/translated_gamepad_test.c new file mode 100644 index 0000000000..9bc9c390f9 --- /dev/null +++ b/port/macos/tests/translated_gamepad_test.c @@ -0,0 +1,84 @@ +#define RECOMP_GENERATED_CODE +#include "recomp_funcs.h" +#include "guest_image.h" +#include "guest_address.h" +#include "recomp_state.h" +#include "host_syscall.h" +#include "host_sdl.h" +#include +#include +#include +#include +#include +#include +static void unexpected(uint32_t va){fprintf(stderr,"unexpected guest call %08x\n",va);abort();} +void recomp_unsupported_instruction(uint32_t va){unexpected(va);} +void recomp_icall_fail_log(uint32_t va){unexpected(va);} +void recomp_icall_not_code_log(uint32_t va){unexpected(va);} +recomp_func_t recomp_lookup_manual(uint32_t va){(void)va;return NULL;} +recomp_func_t recomp_lookup(uint32_t va){(void)va;return NULL;} +recomp_func_t recomp_lookup_kernel(uint32_t va){(void)va;return NULL;} +void mac_host_apply_app_icon(void){}void mac_host_activate_app(void){} +void mac_game_evidence_presented(void){} +void mac_host_bink_presented(void){}void mac_host_memory_fingerprint_presented(void){} +void fixture_pump(void){SDL_UpdateJoysticks();while(mac_host_sdl_poll_event(0x60007000)){ +#ifdef TEST_GAMEPADS_CHANGED + SDL_Event *event=mac_guest_address_resolve(0x60007000,sizeof(SDL_Event)); + if(event->type==SDL_EVENT_GAMEPAD_ADDED || event->type==SDL_EVENT_GAMEPAD_REMOVED){uint32_t saved_stack=g_esp;PUSH32(g_esp,0);TEST_GAMEPADS_CHANGED();assert(g_esp==saved_stack);} +#endif + } } +void mac_guest_import_dispatch_token(uint32_t va) +{ + uint32_t a=MEM32(g_esp+4),b=MEM32(g_esp+8),c=MEM32(g_esp+12),d=MEM32(g_esp+16); + switch(va){ + case TEST_host_sdl_get_gamepads:g_eax=mac_host_sdl_get_gamepads(a,b);break; + case TEST_host_sdl_open_gamepad:g_eax=mac_host_sdl_open_gamepad(a);break; + case TEST_host_sdl_gamepad_from_id:g_eax=mac_host_sdl_gamepad_from_id(a);break; + case TEST_host_sdl_gamepad_axis:g_eax=mac_host_sdl_gamepad_axis(a,b);break; + case TEST_host_sdl_gamepad_button:g_eax=mac_host_sdl_gamepad_button(a,b);break; + case TEST_host_sdl_gamepad_type:g_eax=mac_host_sdl_gamepad_type(a);break; + case TEST_host_sdl_rumble_gamepad:g_eax=mac_host_sdl_rumble_gamepad(a,b,c,d);break; + default:unexpected(va); + }g_esp+=4; +} +static SDL_JoystickID attach(bool sony) +{ + SDL_VirtualJoystickDesc desc;SDL_INIT_INTERFACE(&desc);desc.type=SDL_JOYSTICK_TYPE_GAMEPAD;desc.naxes=6;desc.nbuttons=SDL_GAMEPAD_BUTTON_COUNT; + desc.vendor_id=sony?0x054c:0x045e;desc.product_id=sony?0x0ce6:0x028e;desc.name=sony?"Fixture DualSense":"Fixture Xbox 360"; + desc.axis_mask=(1u<=0);return id; +} +static void neutral(SDL_Joystick *j){for(int i=0;i<6;i++)assert(SDL_SetJoystickVirtualAxis(j,i,i>=4?-32768:0));for(int i=0;i +#include +#include +#include +#include +#include +#include +#include + +static void unexpected(uint32_t va) +{ + fprintf(stderr, "unexpected guest call %08x\n", va); + abort(); +} +void recomp_unsupported_instruction(uint32_t va) { unexpected(va); } +void recomp_icall_fail_log(uint32_t va) { unexpected(va); } +void recomp_icall_not_code_log(uint32_t va) { unexpected(va); } +recomp_func_t recomp_lookup_manual(uint32_t va) { (void)va; return NULL; } +recomp_func_t recomp_lookup(uint32_t va) { (void)va; return NULL; } +recomp_func_t recomp_lookup_kernel(uint32_t va) { (void)va; return NULL; } +void host_get_tp(void) { g_eax = 0x60002000; g_esp += 4; } +void host_syscall(void) +{ + int64_t result = mac_host_arm64_32_syscall_from_stack(g_esp); + g_eax = (uint32_t)result; + g_edx = (uint32_t)((uint64_t)result >> 32); + g_esp += 4; +} + +static void prepare_call(void) +{ + mac_recomp_state_initialize_thread(0x6000f000, 0x60002000); + MEM32(g_esp) = 0; +} + +static int64_t host_ns(void) +{ + struct timespec time; assert(clock_gettime(CLOCK_MONOTONIC, &time) == 0); + return (int64_t)time.tv_sec * 1000000000 + time.tv_nsec; +} +static void caught_signal(int number) { (void)number; } +static void *interrupt(void *opaque) +{ + struct timespec delay = {0, 2000000}; nanosleep(&delay, NULL); + assert(pthread_kill(*(pthread_t *)opaque, SIGUSR1) == 0); return NULL; +} +struct musl_timespec { int64_t seconds; int32_t nanos, padding; }; +_Static_assert(sizeof(struct musl_timespec) == 16, "musl time64 layout"); +int main(int argc, char **argv) +{ + assert(argc == 2); + FILE *file = fopen(argv[1], "rb"); assert(file); + assert(fseek(file, 0, SEEK_END) == 0); long size = ftell(file); rewind(file); + void *bytes = malloc(size); assert(fread(bytes, 1, size, file) == size); fclose(file); + struct mac_guest_image image; char error[256]; + assert(mac_guest_image_load(bytes, size, &image, error, sizeof(error)) == 0); + assert(mac_guest_address_commit(0x60000000, 0x10000, PROT_READ | PROT_WRITE) == 0); + assert(mac_recomp_state_set_image(image.memory_offset, image.code_lo_va, image.code_hi_va) == 0); + int64_t before = host_ns(), target = before + 20000000; + struct musl_timespec request = {target / 1000000000, target % 1000000000, (int32_t)0xCACACACA}; + assert(mac_guest_write(0x60003000, &request, sizeof(request)) == 0); + memset((void *)XBOX_PTR(0x60003020), 0xCA, 16); + prepare_call(); MEM32(g_esp + 4) = 1; MEM32(g_esp + 8) = 1; + MEM32(g_esp + 12) = 0x60003000; MEM32(g_esp + 16) = 0x60003020; + HALO_CLOCK_NANOSLEEP(); + assert(g_eax == 0 && host_ns() >= target); + assert(MEM32(0x60003020) == UINT32_C(0xCACACACA)); + request.seconds = 0; request.nanos = 2000000; + assert(mac_guest_write(0x60003000, &request, sizeof(request)) == 0); + before = host_ns(); prepare_call(); MEM32(g_esp + 4) = 0x60003000; MEM32(g_esp + 8) = 0; + HALO_NANOSLEEP_TIME64(); assert(g_eax == 0 && host_ns() - before >= 2000000); + struct sigaction action = {0}, old; action.sa_handler = caught_signal; sigemptyset(&action.sa_mask); + assert(sigaction(SIGUSR1, &action, &old) == 0); + request.nanos = 20000000; assert(mac_guest_write(0x60003000, &request, sizeof(request)) == 0); + pthread_t self = pthread_self(), sender; assert(pthread_create(&sender, NULL, interrupt, &self) == 0); + prepare_call(); MEM32(g_esp + 4) = 1; MEM32(g_esp + 8) = 0; + MEM32(g_esp + 12) = 0x60003000; MEM32(g_esp + 16) = 0x60003020; + HALO_CLOCK_NANOSLEEP(); assert(g_eax == 4); + assert(pthread_join(sender, NULL) == 0); assert(sigaction(SIGUSR1, &old, NULL) == 0); + struct musl_timespec remaining; assert(mac_guest_read(0x60003020, &remaining, sizeof(remaining)) == 0); + assert(remaining.seconds == 0 && remaining.nanos > 0 && remaining.nanos < 20000000); + puts("actual translated absolute20ms, relative2ms and EINTR remaining pass"); + mac_guest_address_reset(); free(bytes); return 0; +} diff --git a/port/macos/tests/translated_stdio_test.c b/port/macos/tests/translated_stdio_test.c new file mode 100644 index 0000000000..93376ce5a9 --- /dev/null +++ b/port/macos/tests/translated_stdio_test.c @@ -0,0 +1,112 @@ +#define RECOMP_GENERATED_CODE +#include "recomp_funcs.h" +#include "guest_image.h" +#include "guest_address.h" +#include "recomp_state.h" +#include "host_syscall.h" +#include +#include +#include +#include +#include + +static void unexpected(uint32_t va) +{ + fprintf(stderr, "unexpected guest call %08x\n", va); + abort(); +} +void recomp_unsupported_instruction(uint32_t va) { unexpected(va); } +void recomp_icall_fail_log(uint32_t va) { unexpected(va); } +void recomp_icall_not_code_log(uint32_t va) { unexpected(va); } +recomp_func_t recomp_lookup_manual(uint32_t va) +{ + if (va == HALO_STDIO_SEEK_VA) return HALO_STDIO_SEEK; + if (va == HALO_STDIO_READ_VA) return HALO_STDIO_READ; + return NULL; +} +recomp_func_t recomp_lookup(uint32_t va) { (void)va; return NULL; } +recomp_func_t recomp_lookup_kernel(uint32_t va) { (void)va; return NULL; } +void host_get_tp(void) { g_eax = 0x60002000; g_esp += 4; } +void host_syscall(void) +{ + int64_t result = mac_host_arm64_32_syscall_from_stack(g_esp); + g_eax = (uint32_t)result; + g_edx = (uint32_t)((uint64_t)result >> 32); + g_esp += 4; +} + +static void prepare_call(void) +{ + mac_recomp_state_initialize_thread(0x6000f000, 0x60002000); + MEM32(g_esp) = 0; +} + +int main(int argc, char **argv) +{ + assert(argc == 2); + FILE *file = fopen(argv[1], "rb"); + assert(file); + fseek(file, 0, SEEK_END); + long size = ftell(file); + rewind(file); + void *bytes = malloc((size_t)size); + assert(fread(bytes, 1, (size_t)size, file) == (size_t)size); + fclose(file); + struct mac_guest_image image; + char error[256]; + assert(mac_guest_image_load(bytes, (size_t)size, &image, error, sizeof(error)) == 0); + assert(mac_guest_address_commit(0x60000000, 0x10000, PROT_READ | PROT_WRITE) == 0); + assert(mac_recomp_state_set_image(image.memory_offset, image.code_lo_va, image.code_hi_va) == 0); + const char config[] = "fullscreen=false\n"; + char path[] = "/tmp/halo-stdio-test-XXXXXX"; + int fd = mkstemp(path); + assert(fd >= 0); + assert(write(fd, config, sizeof(config) - 1) == sizeof(config) - 1); + /* Construct the real i386 musl FILE layout around a native descriptor. + * The callbacks execute actual translated __stdio_seek/__stdio_read. */ + uint32_t fp = 0x60001000; + memset((void *)XBOX_PTR(fp), 0, 128); + MEM32(fp + 60) = fd; + MEM32(fp + 40) = HALO_STDIO_SEEK_VA; + MEM32(fp + 76) = UINT32_MAX; /* unlocked FILE */ + prepare_call(); + MEM32(g_esp + 4) = fp; + MEM32(g_esp + 8) = 0; + MEM32(g_esp + 12) = SEEK_END; + HALO_FSEEK(); + assert(g_eax == 0); + prepare_call(); + MEM32(g_esp + 4) = fp; + HALO_FTELL(); + assert(g_eax == sizeof(config) - 1); + MEM32(fp + 32) = HALO_STDIO_READ_VA; + MEM32(fp + 44) = 0x60004000; + MEM32(fp + 48) = 128; /* nonzero buffering selects readv */ + prepare_call(); + MEM32(g_esp + 4) = fp; + MEM32(g_esp + 8) = 0; + MEM32(g_esp + 12) = SEEK_SET; + HALO_FSEEK(); + assert(g_eax == 0); + prepare_call(); + MEM32(g_esp + 4) = 0x60005000; + MEM32(g_esp + 8) = 1; + MEM32(g_esp + 12) = sizeof(config) - 1; + MEM32(g_esp + 16) = fp; + HALO_FREAD(); + assert(g_eax == sizeof(config) - 1); + assert(memcmp((void *)XBOX_PTR(0x60005000), config, sizeof(config) - 1) == 0); + prepare_call(); + MEM32(g_esp + 4) = 0x60005000; + MEM32(g_esp + 8) = 1; + MEM32(g_esp + 12) = 1; + MEM32(g_esp + 16) = fp; + HALO_FREAD(); + assert(g_eax == 0); /* EOF follows the exact-length read. */ + puts("actual translated fseek/ftell/fread: configuration bytes and EOF match"); + close(fd); + unlink(path); + mac_guest_address_reset(); + free(bytes); + return 0; +} diff --git a/port/macos/tests/translated_strtod_test.c b/port/macos/tests/translated_strtod_test.c new file mode 100644 index 0000000000..36c29c55ea --- /dev/null +++ b/port/macos/tests/translated_strtod_test.c @@ -0,0 +1,36 @@ +#define RECOMP_GENERATED_CODE +#include "recomp_funcs.h" +#include "guest_image.h" +#include "guest_address.h" +#include "recomp_state.h" +#include +#include +#include +#include +static void fail(uint32_t va) { fprintf(stderr,"unexpected guest call %08x\n",va); abort(); } +void recomp_unsupported_instruction(uint32_t va) { fail(va); } +void recomp_icall_fail_log(uint32_t va) { fail(va); } +void recomp_icall_not_code_log(uint32_t va) { fail(va); } +recomp_func_t recomp_lookup_manual(uint32_t va) { (void)va; return NULL; } +recomp_func_t recomp_lookup(uint32_t va) { (void)va; return NULL; } +recomp_func_t recomp_lookup_kernel(uint32_t va) { (void)va; return NULL; } +void host_get_tp(void) { g_eax=0x60002000; g_esp+=4; } +void host_syscall(void) { fail(0x70000000); } +int main(int argc,char **argv) { + assert(argc==2); FILE*f=fopen(argv[1],"rb");assert(f);fseek(f,0,SEEK_END);long size=ftell(f);rewind(f);void*bytes=malloc(size);assert(fread(bytes,1,size,f)==size);fclose(f); + struct mac_guest_image im;char err[256];if(mac_guest_image_load(bytes,size,&im,err,sizeof(err))){fprintf(stderr,"%s\n",err);return 1;} + assert(!mac_guest_address_commit(0x60000000,0x10000,PROT_READ|PROT_WRITE)); assert(!mac_recomp_state_set_image(im.memory_offset,im.code_lo_va,im.code_hi_va)); + struct { const char*text;double expected;} cases[]={{"0.0",0.0},{"1.0",1.0},{"-2.5",-2.5},{"0.1",0.1},{"123.75",123.75}}; + for(unsigned i=0;i +#include +#include +#include +#include + +static void unexpected(uint32_t va) +{ + fprintf(stderr, "unexpected guest call %08x\n", va); + abort(); +} +void recomp_unsupported_instruction(uint32_t va) { unexpected(va); } +void recomp_icall_fail_log(uint32_t va) { unexpected(va); } +void recomp_icall_not_code_log(uint32_t va) { unexpected(va); } +static void unrelated_false(void) { g_eax = 0; g_esp += 4; } +recomp_func_t recomp_lookup_manual(uint32_t va) +{ + if (va == HALO_UI_CALLBACK_VA) return HALO_UI_CALLBACK; + if (va == HALO_UI_OTHER_VA) return unrelated_false; + return NULL; +} +int profile_count, name_opened, warnings, errors; +recomp_func_t recomp_lookup(uint32_t va) { (void)va; return NULL; } +recomp_func_t recomp_lookup_kernel(uint32_t va) { (void)va; return NULL; } +void host_get_tp(void) { g_eax = 0x60002000; g_esp += 4; } +void host_syscall(void) +{ + int64_t result = mac_host_arm64_32_syscall_from_stack(g_esp); + g_eax = (uint32_t)result; + g_edx = (uint32_t)((uint64_t)result >> 32); + g_esp += 4; +} + +int main(int argc, char **argv) +{ + assert(argc == 2); + FILE *file = fopen(argv[1], "rb"); + assert(file); + fseek(file, 0, SEEK_END); + long size = ftell(file); + rewind(file); + void *bytes = malloc((size_t)size); + assert(fread(bytes, 1, (size_t)size, file) == (size_t)size); + fclose(file); + struct mac_guest_image image; + char error[256]; + assert(mac_guest_image_load(bytes, (size_t)size, &image, error, sizeof(error)) == 0); + assert(mac_guest_address_commit(0x60000000, 0x10000, PROT_READ | PROT_WRITE) == 0); + assert(mac_recomp_state_set_image(image.memory_offset, image.code_lo_va, image.code_hi_va) == 0); + const uint32_t indices[] = {0,101,102,UINT16_MAX}; + for(unsigned i=0;i<4;i++) { + mac_recomp_state_initialize_thread(0x6000f000,0x60002000); + MEM32(g_esp)=0; MEM32(g_esp+4)=indices[i]; + HALO_UI_CLASSIFIER(); + assert((g_eax&255)==(indices[i]==101)); + assert(g_esp==0x6000f004); + } + for(unsigned mode=0;mode<3;mode++) { + profile_count=mode==1;name_opened=warnings=errors=0; + mac_recomp_state_initialize_thread(0x6000f000,0x60002000); + MEM8(0x60000300)=0; + MEM32(g_esp)=0;MEM32(g_esp+4)=0x60000100;MEM32(g_esp+8)=0x60000200; + MEM32(g_esp+12)=mode==2?0:101;MEM32(g_esp+16)=0x60000300; + HALO_UI_INVOKER(); + assert((g_eax&255)==(mode==1)); + assert(name_opened==(mode==0) && warnings==(mode==2) && errors==0); + assert(MEM8(0x60000300)==0 && g_esp==0x6000f004); + } + puts("actual translated UI callback101: zero profiles opens naming/FALSE; existing profile TRUE; unrelatedFALSE warns; classifier bounds pass"); + mac_guest_address_reset(); + free(bytes); + return 0; +} diff --git a/port/macos/tooling/LICENSE b/port/macos/tooling/LICENSE new file mode 100644 index 0000000000..32fb6be8d3 --- /dev/null +++ b/port/macos/tooling/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 halo-ce-apple-silicon contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/port/macos/tooling/NOTICE.md b/port/macos/tooling/NOTICE.md new file mode 100644 index 0000000000..8b1f8928e4 --- /dev/null +++ b/port/macos/tooling/NOTICE.md @@ -0,0 +1,5 @@ +The MIT license in port/macos/LICENSE applies to independently authored macOS host/build glue, original host fixtures and Mac documentation, plus the original added macos_* and test/check/run_macos_* tools and portable guest compilation helper. It does not relicense existing source, upstream patch context, Halo data, Xbox SDK material, artwork or screenshots. Existing source files keep the root CC0 license and retained third-party notices. + +XboxRecomp is fetched at the public pin in pins.json. The runtime patch modifies its MIT-licensed translator; licenses/XboxRecomp-MIT.txt preserves the upstream copyright. Bundled compiler-rt keeps its Apache 2.0 WITH LLVM-exception license. SHA-verified musl retains licenses/musl.txt. Upstream vendors and Homebrew libraries keep their own licenses; they are not covered by this MIT file. + +The generated helmet icon and prompt are fan artwork, excluded from the original-code MIT grant. This unofficial project is not endorsed by Microsoft, Bungie or Xbox. Game images/data and generated translation are not committed or uploaded. Build from your own supported retail Xbox image; setup never downloads Halo. diff --git a/port/macos/tooling/licenses/XboxRecomp-MIT.txt b/port/macos/tooling/licenses/XboxRecomp-MIT.txt new file mode 100644 index 0000000000..78282757ba --- /dev/null +++ b/port/macos/tooling/licenses/XboxRecomp-MIT.txt @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 sp00nz + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/port/macos/tooling/licenses/compiler-rt.txt b/port/macos/tooling/licenses/compiler-rt.txt new file mode 100644 index 0000000000..5a79a1b9d5 --- /dev/null +++ b/port/macos/tooling/licenses/compiler-rt.txt @@ -0,0 +1,311 @@ +============================================================================== +The LLVM Project is under the Apache License v2.0 with LLVM Exceptions: +============================================================================== + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + +---- LLVM Exceptions to the Apache 2.0 License ---- + +As an exception, if, as a result of your compiling your source code, portions +of this Software are embedded into an Object form of such source code, you +may redistribute such embedded portions in such Object form without complying +with the conditions of Sections 4(a), 4(b) and 4(d) of the License. + +In addition, if you combine or link compiled forms of this Software with +software that is licensed under the GPLv2 ("Combined Software") and if a +court of competent jurisdiction determines that the patent provision (Section +3), the indemnity provision (Section 9) or other Section of the License +conflicts with the conditions of the GPLv2, you may retroactively and +prospectively choose to deem waived or otherwise exclude such Section(s) of +the License, but only in their entirety and only with respect to the Combined +Software. + +============================================================================== +Software from third parties included in the LLVM Project: +============================================================================== +The LLVM Project contains third party software which is under different license +terms. All such code will be identified clearly using at least one of two +mechanisms: +1) It will be in a separate directory tree with its own `LICENSE.txt` or + `LICENSE` file at the top containing the specific license and restrictions + which apply to that software, or +2) It will contain specific license and restriction terms at the top of every + file. + +============================================================================== +Legacy LLVM License (https://llvm.org/docs/DeveloperPolicy.html#legacy): +============================================================================== + +The compiler_rt library is dual licensed under both the University of Illinois +"BSD-Like" license and the MIT license. As a user of this code you may choose +to use it under either license. As a contributor, you agree to allow your code +to be used under both. + +Full text of the relevant licenses is included below. + +============================================================================== + +University of Illinois/NCSA +Open Source License + +Copyright (c) 2009-2019 by the contributors listed in CREDITS.TXT + +All rights reserved. + +Developed by: + + LLVM Team + + University of Illinois at Urbana-Champaign + + http://llvm.org + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal with +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimers. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimers in the + documentation and/or other materials provided with the distribution. + + * Neither the names of the LLVM Team, University of Illinois at + Urbana-Champaign, nor the names of its contributors may be used to + endorse or promote products derived from this Software without specific + prior written permission. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE +SOFTWARE. + +============================================================================== + +Copyright (c) 2009-2015 by the contributors listed in CREDITS.TXT + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/port/macos/tooling/licenses/musl.txt b/port/macos/tooling/licenses/musl.txt new file mode 100644 index 0000000000..c1628e9ac8 --- /dev/null +++ b/port/macos/tooling/licenses/musl.txt @@ -0,0 +1,193 @@ +musl as a whole is licensed under the following standard MIT license: + +---------------------------------------------------------------------- +Copyright © 2005-2020 Rich Felker, et al. + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +---------------------------------------------------------------------- + +Authors/contributors include: + +A. Wilcox +Ada Worcester +Alex Dowad +Alex Suykov +Alexander Monakov +Andre McCurdy +Andrew Kelley +Anthony G. Basile +Aric Belsito +Arvid Picciani +Bartosz Brachaczek +Benjamin Peterson +Bobby Bingham +Boris Brezillon +Brent Cook +Chris Spiegel +Clément Vasseur +Daniel Micay +Daniel Sabogal +Daurnimator +David Carlier +David Edelsohn +Denys Vlasenko +Dmitry Ivanov +Dmitry V. Levin +Drew DeVault +Emil Renner Berthing +Fangrui Song +Felix Fietkau +Felix Janda +Gianluca Anzolin +Hauke Mehrtens +He X +Hiltjo Posthuma +Isaac Dunham +Jaydeep Patil +Jens Gustedt +Jeremy Huntwork +Jo-Philipp Wich +Joakim Sindholt +John Spencer +Julien Ramseier +Justin Cormack +Kaarle Ritvanen +Khem Raj +Kylie McClain +Leah Neukirchen +Luca Barbato +Luka Perkov +M Farkas-Dyck (Strake) +Mahesh Bodapati +Markus Wichmann +Masanori Ogino +Michael Clark +Michael Forney +Mikhail Kremnyov +Natanael Copa +Nicholas J. Kain +orc +Pascal Cuoq +Patrick Oppenlander +Petr Hosek +Petr Skocik +Pierre Carrier +Reini Urban +Rich Felker +Richard Pennington +Ryan Fairfax +Samuel Holland +Segev Finer +Shiz +sin +Solar Designer +Stefan Kristiansson +Stefan O'Rear +Szabolcs Nagy +Timo Teräs +Trutz Behn +Valentin Ochs +Will Dietz +William Haddon +William Pitcock + +Portions of this software are derived from third-party works licensed +under terms compatible with the above MIT license: + +The TRE regular expression implementation (src/regex/reg* and +src/regex/tre*) is Copyright © 2001-2008 Ville Laurikari and licensed +under a 2-clause BSD license (license text in the source files). The +included version has been heavily modified by Rich Felker in 2012, in +the interests of size, simplicity, and namespace cleanliness. + +Much of the math library code (src/math/* and src/complex/*) is +Copyright © 1993,2004 Sun Microsystems or +Copyright © 2003-2011 David Schultz or +Copyright © 2003-2009 Steven G. Kargl or +Copyright © 2003-2009 Bruce D. Evans or +Copyright © 2008 Stephen L. Moshier or +Copyright © 2017-2018 Arm Limited +and labelled as such in comments in the individual source files. All +have been licensed under extremely permissive terms. + +The ARM memcpy code (src/string/arm/memcpy.S) is Copyright © 2008 +The Android Open Source Project and is licensed under a two-clause BSD +license. It was taken from Bionic libc, used on Android. + +The AArch64 memcpy and memset code (src/string/aarch64/*) are +Copyright © 1999-2019, Arm Limited. + +The implementation of DES for crypt (src/crypt/crypt_des.c) is +Copyright © 1994 David Burren. It is licensed under a BSD license. + +The implementation of blowfish crypt (src/crypt/crypt_blowfish.c) was +originally written by Solar Designer and placed into the public +domain. The code also comes with a fallback permissive license for use +in jurisdictions that may not recognize the public domain. + +The smoothsort implementation (src/stdlib/qsort.c) is Copyright © 2011 +Valentin Ochs and is licensed under an MIT-style license. + +The x86_64 port was written by Nicholas J. Kain and is licensed under +the standard MIT terms. + +The mips and microblaze ports were originally written by Richard +Pennington for use in the ellcc project. The original code was adapted +by Rich Felker for build system and code conventions during upstream +integration. It is licensed under the standard MIT terms. + +The mips64 port was contributed by Imagination Technologies and is +licensed under the standard MIT terms. + +The powerpc port was also originally written by Richard Pennington, +and later supplemented and integrated by John Spencer. It is licensed +under the standard MIT terms. + +All other files which have no copyright comments are original works +produced specifically for use as part of this library, written either +by Rich Felker, the main author of the library, or by one or more +contibutors listed above. Details on authorship of individual files +can be found in the git version control history of the project. The +omission of copyright and license comments in each file is in the +interest of source tree size. + +In addition, permission is hereby granted for all public header files +(include/* and arch/*/bits/*) and crt files intended to be linked into +applications (crt/*, ldso/dlstart.c, and arch/*/crt_arch.h) to omit +the copyright notice and permission notice otherwise required by the +license, and to use these files without any requirement of +attribution. These files include substantial contributions from: + +Bobby Bingham +John Spencer +Nicholas J. Kain +Rich Felker +Richard Pennington +Stefan Kristiansson +Szabolcs Nagy + +all of whom have explicitly granted such permission. + +This file previously contained text expressing a belief that most of +the files covered by the above exception were sufficiently trivial not +to be subject to copyright, resulting in confusion over whether it +negated the permissions granted in the license. In the spirit of +permissive licensing, and of not having licensing issues being an +obstacle to adoption, that text has been removed. diff --git a/port/macos/tooling/licenses/upstream-CC0.txt b/port/macos/tooling/licenses/upstream-CC0.txt new file mode 100644 index 0000000000..0e259d42c9 --- /dev/null +++ b/port/macos/tooling/licenses/upstream-CC0.txt @@ -0,0 +1,121 @@ +Creative Commons Legal Code + +CC0 1.0 Universal + + CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE + LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN + ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS + INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES + REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS + PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM + THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED + HEREUNDER. + +Statement of Purpose + +The laws of most jurisdictions throughout the world automatically confer +exclusive Copyright and Related Rights (defined below) upon the creator +and subsequent owner(s) (each and all, an "owner") of an original work of +authorship and/or a database (each, a "Work"). + +Certain owners wish to permanently relinquish those rights to a Work for +the purpose of contributing to a commons of creative, cultural and +scientific works ("Commons") that the public can reliably and without fear +of later claims of infringement build upon, modify, incorporate in other +works, reuse and redistribute as freely as possible in any form whatsoever +and for any purposes, including without limitation commercial purposes. +These owners may contribute to the Commons to promote the ideal of a free +culture and the further production of creative, cultural and scientific +works, or to gain reputation or greater distribution for their Work in +part through the use and efforts of others. + +For these and/or other purposes and motivations, and without any +expectation of additional consideration or compensation, the person +associating CC0 with a Work (the "Affirmer"), to the extent that he or she +is an owner of Copyright and Related Rights in the Work, voluntarily +elects to apply CC0 to the Work and publicly distribute the Work under its +terms, with knowledge of his or her Copyright and Related Rights in the +Work and the meaning and intended legal effect of CC0 on those rights. + +1. Copyright and Related Rights. A Work made available under CC0 may be +protected by copyright and related or neighboring rights ("Copyright and +Related Rights"). Copyright and Related Rights include, but are not +limited to, the following: + + i. the right to reproduce, adapt, distribute, perform, display, + communicate, and translate a Work; + ii. moral rights retained by the original author(s) and/or performer(s); +iii. publicity and privacy rights pertaining to a person's image or + likeness depicted in a Work; + iv. rights protecting against unfair competition in regards to a Work, + subject to the limitations in paragraph 4(a), below; + v. rights protecting the extraction, dissemination, use and reuse of data + in a Work; + vi. database rights (such as those arising under Directive 96/9/EC of the + European Parliament and of the Council of 11 March 1996 on the legal + protection of databases, and under any national implementation + thereof, including any amended or successor version of such + directive); and +vii. other similar, equivalent or corresponding rights throughout the + world based on applicable law or treaty, and any national + implementations thereof. + +2. Waiver. To the greatest extent permitted by, but not in contravention +of, applicable law, Affirmer hereby overtly, fully, permanently, +irrevocably and unconditionally waives, abandons, and surrenders all of +Affirmer's Copyright and Related Rights and associated claims and causes +of action, whether now known or unknown (including existing as well as +future claims and causes of action), in the Work (i) in all territories +worldwide, (ii) for the maximum duration provided by applicable law or +treaty (including future time extensions), (iii) in any current or future +medium and for any number of copies, and (iv) for any purpose whatsoever, +including without limitation commercial, advertising or promotional +purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each +member of the public at large and to the detriment of Affirmer's heirs and +successors, fully intending that such Waiver shall not be subject to +revocation, rescission, cancellation, termination, or any other legal or +equitable action to disrupt the quiet enjoyment of the Work by the public +as contemplated by Affirmer's express Statement of Purpose. + +3. Public License Fallback. Should any part of the Waiver for any reason +be judged legally invalid or ineffective under applicable law, then the +Waiver shall be preserved to the maximum extent permitted taking into +account Affirmer's express Statement of Purpose. In addition, to the +extent the Waiver is so judged Affirmer hereby grants to each affected +person a royalty-free, non transferable, non sublicensable, non exclusive, +irrevocable and unconditional license to exercise Affirmer's Copyright and +Related Rights in the Work (i) in all territories worldwide, (ii) for the +maximum duration provided by applicable law or treaty (including future +time extensions), (iii) in any current or future medium and for any number +of copies, and (iv) for any purpose whatsoever, including without +limitation commercial, advertising or promotional purposes (the +"License"). The License shall be deemed effective as of the date CC0 was +applied by Affirmer to the Work. Should any part of the License for any +reason be judged legally invalid or ineffective under applicable law, such +partial invalidity or ineffectiveness shall not invalidate the remainder +of the License, and in such case Affirmer hereby affirms that he or she +will not (i) exercise any of his or her remaining Copyright and Related +Rights in the Work or (ii) assert any associated claims and causes of +action with respect to the Work, in either case contrary to Affirmer's +express Statement of Purpose. + +4. Limitations and Disclaimers. + + a. No trademark or patent rights held by Affirmer are waived, abandoned, + surrendered, licensed or otherwise affected by this document. + b. Affirmer offers the Work as-is and makes no representations or + warranties of any kind concerning the Work, express, implied, + statutory or otherwise, including without limitation warranties of + title, merchantability, fitness for a particular purpose, non + infringement, or the absence of latent or other defects, accuracy, or + the present or absence of errors, whether or not discoverable, all to + the greatest extent permissible under applicable law. + c. Affirmer disclaims responsibility for clearing rights of other persons + that may apply to the Work or any use thereof, including without + limitation any person's Copyright and Related Rights in the Work. + Further, Affirmer disclaims responsibility for obtaining any necessary + consents, permissions or other rights required for any use of the + Work. + d. Affirmer understands and acknowledges that Creative Commons is not a + party to this document and has no duty or obligation with respect to + this CC0 or use of the Work. diff --git a/port/macos/tooling/patches/.gitattributes b/port/macos/tooling/patches/.gitattributes new file mode 100644 index 0000000000..24e115f5b2 --- /dev/null +++ b/port/macos/tooling/patches/.gitattributes @@ -0,0 +1 @@ +*.patch -whitespace diff --git a/port/macos/tooling/patches/xboxrecomp-runtime.patch b/port/macos/tooling/patches/xboxrecomp-runtime.patch new file mode 100644 index 0000000000..dbc85c4312 --- /dev/null +++ b/port/macos/tooling/patches/xboxrecomp-runtime.patch @@ -0,0 +1,2315 @@ +--- a/tools/recomp/lifter.py ++++ b/tools/recomp/lifter.py +@@ -254,6 +254,7 @@ + # this shape: "add reg, reg" to shift the top bit into CF, then jae on it. + CF_TRACKED = frozenset({ + "add", "sub", "adc", "sbb", "shl", "shr", "sar", ++ "rol", "ror", "rcl", "rcr", "shld", "shrd", "tzcnt", + }) + + # Additional instructions that modify EFLAGS (tracked but handled as generic) +@@ -263,6 +264,7 @@ + "bt", "bts", "btr", "btc", # Bit test sets CF + "cmpxchg", # Compare-and-exchange sets ZF + "xadd", # Exchange-and-add sets flags ++ "tzcnt", # BMI1 sets ZF from result and CF when source is zero + }) + + # Instructions with undefined/unpredictable flags (clear tracking) +@@ -278,7 +280,7 @@ + "mov", "lea", "push", "pop", "nop", "leave", "ret", + "movzx", "movsx", "xchg", "bswap", + "cdq", "cwde", "cbw", "cwd", +- "lahf", ++ "lahf", "fcmovne", + "not", # NOT does not modify flags + "call", + "int3", "int", "wait", +@@ -297,7 +299,7 @@ + "cmpss", "cmpsd", + "cmpltss", "cmpeqss", "cmpleps", "cmpneqss", + # SSE packed float +- "movaps", "movups", "movlps", "movhps", "movlhps", "movhlps", ++ "movaps", "movups", "movlps", "movhps", "movlpd", "movlhps", "movhlps", + "addps", "subps", "mulps", "divps", + "minps", "maxps", "sqrtps", "rsqrtps", "rcpps", + "shufps", "unpcklps", "unpckhps", +@@ -306,7 +308,7 @@ + "movmskps", + # SSE2 packed double + "movapd", "movupd", +- "addpd", "subpd", "mulpd", "divpd", ++ "addpd", "subpd", "mulpd", "divpd", "unpckhpd", + # SSE/MMX integer + "movd", "movq", "movntq", + "emms", +@@ -333,15 +335,42 @@ + }) + + ++def _overflow_snapshot(state): ++ """Return a proven OF expression before SAHF, or None for unknown OF.""" ++ if not state: return None ++ kind, ops = state ++ if kind == "sahf_of": return "_sahf_of" ++ if kind == "cmp" and len(ops) == 2: ++ bits = (_operand_width(ops[0]) or _operand_width(ops[1]) or 4) * 8 ++ return f"!!(((_fa ^ _fb) & (_fa ^ (_fa - _fb))) & 0x{1 << (bits - 1):X}u)" ++ if kind == "test": return "0" ++ if kind in ("rol", "ror", "rcl", "rcr", "shl", "shr", "shld", "shrd"): ++ condition = _make_condition("jo", kind, ops) ++ return condition[0] if condition else None ++ return None ++ ++ + def _make_condition(jcc, flag_setter, flag_ops): + """ + Generate a C condition expression for a jcc based on what set the flags. + Returns (cond_expr, description) or None. + """ ++ locked_setter = flag_setter.startswith("lock ") ++ if locked_setter: ++ flag_setter = flag_setter[5:] + cond_info = COND_MAP.get(jcc) + if not cond_info: + return None + cmp_macro, test_macro, desc = cond_info ++ if flag_setter in ("snapshot_zf", "snapshot_sz"): ++ if jcc in ("je", "jz"): return "_zf", desc ++ if jcc in ("jne", "jnz"): return "!_zf", desc ++ if flag_setter == "snapshot_sz": ++ if jcc in ("jl", "jnge"): return "_slt", desc ++ if jcc in ("jge", "jnl"): return "!_slt", desc ++ if jcc in ("jle", "jng"): return "(_slt || _zf)", desc ++ if jcc in ("jg", "jnle"): return "(!_slt && !_zf)", desc ++ return None # No CF, raw SF, or raw OF is established by this meet. + + # A cmp/test that is not fused with its jcc snapshots its operands into + # _fa/_fb (zero-extended) and _fas/_fbs (sign-extended) at the point the +@@ -360,10 +389,34 @@ + else: + lhs = None + rhs = None +- +- # ── FPU compare-to-EFLAGS and sahf: no standard operands ── ++ if locked_setter: ++ # Atomic RMW lifters snapshot the result at the linearization point; ++ # rereading the guest memory operand could observe a later thread's ++ # update and produce flags from a different instruction. ++ lhs = "_fa" ++ ++ if flag_setter in ("sahf", "sahf_of"): ++ cf, zf, pf, sf = (f"!!(_sahf_flags & {mask}u)" for mask in (1, 64, 4, 128)) ++ predicates = { ++ "jb": cf, "jnae": cf, "jc": cf, ++ "jae": f"!({cf})", "jnb": f"!({cf})", "jnc": f"!({cf})", ++ "jbe": f"({cf} || {zf})", "jna": f"({cf} || {zf})", ++ "ja": f"!({cf} || {zf})", "jnbe": f"!({cf} || {zf})", ++ "je": zf, "jz": zf, "jne": f"!({zf})", "jnz": f"!({zf})", ++ "jp": pf, "jnp": f"!({pf})", "js": sf, "jns": f"!({sf})", ++ } ++ if flag_setter == "sahf_of": ++ slt = f"({sf} != _sahf_of)" ++ predicates.update({"jo": "_sahf_of", "jno": "!_sahf_of", ++ "jl": slt, "jnge": slt, "jge": f"!{slt}", "jnl": f"!{slt}", ++ "jle": f"({slt} || {zf})", "jng": f"({slt} || {zf})", ++ "jg": f"!({slt} || {zf})", "jnle": f"!({slt} || {zf})"}) ++ value = predicates.get(jcc) ++ return (value, desc) if value is not None else None ++ ++ # ── FPU compare-to-EFLAGS: no standard operands ── + if flag_setter in ("fcompi", "fcomip", "fucomi", "fucompi", +- "fucomip", "fcomi", "sahf"): ++ "fucomip", "fcomi"): + fpu_cmp_map = { + "ja": ">", "jnbe": ">", + "jae": ">=", "jnb": ">=", "jnc": ">=", +@@ -383,6 +436,19 @@ + + # If no operands available for other flag-setters, can't generate condition + if lhs is None: ++ return None ++ ++ if flag_setter in ("rol", "ror", "rcl", "rcr"): ++ if jcc in ("jb", "jnae", "jc"): return "_cf", desc ++ if jcc in ("jae", "jnb", "jnc"): return "!_cf", desc ++ if (jcc in ("jo", "jno") and len(flag_ops) > 1 and ++ flag_ops[1].type == "imm" and (flag_ops[1].imm & 31) == 1): ++ bits = (_operand_width(flag_ops[0]) or 4) * 8 ++ other = ("_rotate_result" if flag_setter == "rol" else "_cf" if flag_setter == "rcl" ++ else f"(_rotate_result >> {bits - 2})") ++ overflow = f"(((_rotate_result >> {bits - 1}) ^ {other}) & 1u)" ++ return (overflow if jcc == "jo" else f"!{overflow}"), desc ++ # Rotation never establishes ZF/SF/PF. A multi-bit OF is undefined. + return None + + # ── comiss/ucomiss: float comparison, sets CF/ZF/PF ── +@@ -407,22 +473,24 @@ + desc = f"{desc} ({void_a} vs {void_b})" if desc else desc + a, b = "_fca", "_fcb" + # comiss uses unsigned condition codes (CF, ZF) ++ unordered = f"(isnan({a}) || isnan({b}))" ++ ordered = f"(!{unordered})" + if jcc in ("ja", "jnbe"): +- return f"({a} > {b})", desc ++ return f"({ordered} && {a} > {b})", desc + if jcc in ("jae", "jnb", "jnc"): +- return f"({a} >= {b})", desc ++ return f"({ordered} && {a} >= {b})", desc + if jcc in ("jb", "jnae", "jc"): +- return f"({a} < {b})", desc ++ return f"({unordered} || {a} < {b})", desc + if jcc in ("jbe", "jna"): +- return f"({a} <= {b})", desc ++ return f"({unordered} || {a} <= {b})", desc + if jcc in ("je", "jz"): + return f"({a} == {b})", desc + if jcc in ("jne", "jnz"): +- return f"({a} != {b})", desc ++ return f"({ordered} && {a} != {b})", desc + if jcc == "jp": +- return f"0 /* {jcc}: unordered/NaN */", desc ++ return f"(isnan({a}) || isnan({b}))", desc + if jcc == "jnp": +- return f"1 /* {jcc}: ordered */", desc ++ return f"(!isnan({a}) && !isnan({b}))", desc + return None + + # SF is the sign bit of the result at the OPERAND's width, not at 32 bits. +@@ -448,8 +516,23 @@ + return f"({lhs} != 0)", desc + return None + ++ if flag_setter == "tzcnt": ++ if jcc in ("je", "jz", "sete", "setz"): ++ return "(_fa == 0)", desc ++ if jcc in ("jne", "jnz", "setne", "setnz"): ++ return "(_fa != 0)", desc ++ if jcc in ("jb", "jnae", "jc"): ++ return "_cf", desc ++ if jcc in ("jae", "jnb", "jnc"): ++ return "!_cf", desc ++ return None ++ + # ── cmp: flags from (a - b), operands unchanged ── + if flag_setter == "cmp": ++ if jcc in ("jp", "jnp"): ++ result = f"((uint32_t)({lhs}) - (uint32_t)({rhs}))" ++ parity = f"RECOMP_PARITY8({result})" ++ return (parity if jcc == "jp" else f"!{parity}"), desc + if cmp_macro: + return f"{cmp_macro}({lhs}, {rhs})", desc + if jcc == "js": +@@ -484,6 +567,10 @@ + if jcc == "jnp": + return f"(!RECOMP_PARITY8(({lhs}) & ({rhs})))", desc + return None ++ ++ if flag_setter in ("shl", "shr", "shld", "shrd"): ++ # Like CMP snapshots, this survives an intervening MOV to the operand. ++ lhs = "_shift_result" + + # ── carry conditions, from the _cf the arithmetic already produced ── + # +@@ -534,6 +621,9 @@ + + # ── add: a = a + b, flags from result ── + if flag_setter == "add": ++ if jcc in ("jp", "jnp"): ++ parity = f"RECOMP_PARITY8({lhs})" ++ return (parity if jcc == "jp" else f"!{parity}"), desc + if jcc in ("je", "jz"): + return f"({lhs} == 0)", desc + if jcc in ("jne", "jnz"): +@@ -558,6 +648,35 @@ + + # ── adc/sbb: result-based (like add/sub but with carry) ── + if flag_setter in ("adc", "sbb"): ++ if flag_setter == "sbb": ++ slt = "(_sbb_sf != _sbb_of)" ++ if jcc in ("jl", "jnge"): ++ return slt, desc ++ if jcc in ("jge", "jnl"): ++ return f"!{slt}", desc ++ if jcc in ("jle", "jng"): ++ return f"({slt} || _sbb_result == 0)", desc ++ if jcc in ("jg", "jnle"): ++ return f"(!{slt} && _sbb_result != 0)", desc ++ if jcc in ("jo",): ++ return "_sbb_of", desc ++ if jcc in ("jno",): ++ return "!_sbb_of", desc ++ if jcc in ("js",): ++ return "_sbb_sf", desc ++ if jcc in ("jns",): ++ return "!_sbb_sf", desc ++ if jcc in ("je", "jz"): ++ return "(_sbb_result == 0)", desc ++ if jcc in ("jne", "jnz"): ++ return "(_sbb_result != 0)", desc ++ if jcc == "jp": ++ return "RECOMP_PARITY8(_sbb_result)", desc ++ if jcc == "jnp": ++ return "!RECOMP_PARITY8(_sbb_result)", desc ++ if jcc in ("jp", "jnp"): ++ parity = f"RECOMP_PARITY8({lhs})" ++ return (parity if jcc == "jp" else f"!{parity}"), desc + if jcc in ("je", "jz"): + return f"({lhs} == 0)", desc + if jcc in ("jne", "jnz"): +@@ -570,6 +689,9 @@ + + # ── and/or/xor: result-based, CF=0, OF=0 ── + if flag_setter in ("and", "or", "xor"): ++ if jcc in ("jp", "jnp"): ++ parity = f"RECOMP_PARITY8({lhs})" ++ return (parity if jcc == "jp" else f"!{parity}"), desc + if jcc in ("je", "jz"): + return f"({lhs} == 0)", desc + if jcc in ("jne", "jnz"): +@@ -590,6 +712,14 @@ + + # ── dec/inc: result-based, CF unchanged ── + if flag_setter in ("dec", "inc"): ++ if jcc in ("jp", "jnp"): ++ parity = f"RECOMP_PARITY8({lhs})" ++ return (parity if jcc == "jp" else f"!{parity}"), desc ++ if jcc in ("jo", "jno"): ++ width = (_operand_width(flag_ops[0]) or 4) * 8 if flag_ops else 32 ++ overflow_value = (1 << (width - 1)) if flag_setter == "inc" else ((1 << (width - 1)) - 1) ++ overflow = f"((uint{width}_t)({lhs}) == 0x{overflow_value:X}u)" ++ return (overflow if jcc == "jo" else f"!{overflow}"), desc + if jcc in ("je", "jz"): + return f"({lhs} == 0)", desc + if jcc in ("jne", "jnz"): +@@ -606,6 +736,13 @@ + + # ── neg: flags from (0 - a_orig), result is -a ── + if flag_setter == "neg": ++ if jcc in ("jp", "jnp"): ++ parity = f"RECOMP_PARITY8({lhs})" ++ return (parity if jcc == "jp" else f"!{parity}"), desc ++ if jcc in ("jo", "jno"): ++ width = (_operand_width(flag_ops[0]) or 4) * 8 if flag_ops else 32 ++ overflow = f"((uint{width}_t)({lhs}) == 0x{1 << (width - 1):X}u)" ++ return (overflow if jcc == "jo" else f"!{overflow}"), desc + if jcc in ("je", "jz"): + return f"({lhs} == 0)", desc + if jcc in ("jne", "jnz"): +@@ -630,27 +767,22 @@ + return None + + # ── shift: result-based ── +- if flag_setter in ("shl", "shr", "sar"): ++ if flag_setter in ("shl", "shr", "sar", "shld", "shrd"): + if jcc in ("je", "jz"): + return f"({lhs} == 0)", desc + if jcc in ("jne", "jnz"): + return f"({lhs} != 0)", desc + if jcc == "js": +- return f"((int32_t){lhs} < 0)", desc ++ return f"({_sf_cast}{lhs} < 0)", desc + if jcc == "jns": +- return f"((int32_t){lhs} >= 0)", desc +- return None +- +- # ── shld/shrd: double-precision shift, result-based ── +- if flag_setter in ("shld", "shrd"): +- if jcc in ("je", "jz"): +- return f"({lhs} == 0)", desc +- if jcc in ("jne", "jnz"): +- return f"({lhs} != 0)", desc +- if jcc == "js": +- return f"((int32_t){lhs} < 0)", desc +- if jcc == "jns": +- return f"((int32_t){lhs} >= 0)", desc ++ return f"({_sf_cast}{lhs} >= 0)", desc ++ if flag_setter in ("shl", "shr", "shld", "shrd"): ++ if jcc in ("jp", "jnp"): ++ condition = f"RECOMP_PARITY8({lhs})" ++ return (condition if jcc == "jp" else f"!{condition}"), desc ++ if (jcc in ("jo", "jno") and len(flag_ops) > 1 and ++ flag_ops[-1].type == "imm" and (flag_ops[-1].imm & 31) == 1): ++ return ("_shift_of" if jcc == "jo" else "!_shift_of"), desc + return None + + # ── rol/ror/rcl/rcr: rotation, only CF/OF affected ── +@@ -745,11 +877,11 @@ + # Conditional tail call: same frame bridge as the unconditional tail + # jmp in _lift_jmp, applied only on the taken path. + if target in lifter.manual_functions: +- return (f"if ({cond_expr}) {{ g_seh_ebp = ebp; " ++ return (f"if ({cond_expr}) {{ g_ebp = g_seh_ebp = ebp; " + f"RECOMP_ITAIL(0x{target:08X}u); return; }}" + f" /* {jcc}: {desc}, manual tail */") + name = lifter._call_target_name(target) +- return (f"if ({cond_expr}) {{ g_seh_ebp = ebp; {name}(); return; }}" ++ return (f"if ({cond_expr}) {{ g_ebp = g_seh_ebp = ebp; {name}(); return; }}" + f" /* {jcc}: {desc} */") + return f"if ({cond_expr}) goto loc_{target:08X}; /* {jcc}: {desc} */" + +@@ -938,8 +1070,12 @@ + self._fp_top = 0 # FPU stack top index + self.func_start = 0 # Set per-function by translator + self.func_end = 0 ++ # Per-function computed-goto destinations for register-indirect jump ++ # tables. The translator fills this only when the load/jump idiom and ++ # bounded table are both proven; values remain labels in this C frame. ++ self.local_indirect_targets = {} + self.needs_cf = False # Set per-function by translator (has adc/sbb) +- self.publishes_ebp = False # Set per-function: has a real frame ++ self.publishes_ebp = False # Set per-function: has a local architectural EBP + self.trace_exit_name = None # Set per-function when traced + # Every direct call target we emit a name for, as {addr: name}. The + # batch translator diffs this against the functions it actually defined +@@ -1045,6 +1181,8 @@ + return self._lift_sar(insn, ops) + if m in ("rol", "ror"): + return self._lift_rotate(insn, ops, m) ++ if m in ("rcl", "rcr"): ++ return self._lift_carry_rotate(insn, ops, m) + + # ── Comparison / test (standalone, not part of cmp+jcc pattern) ── + if m == "cmp": +@@ -1065,8 +1203,9 @@ + # ── String operations ── + if m.startswith("rep ") or m.startswith("repe ") or m.startswith("repne "): + return self._lift_rep_string(insn, m) +- if m in ("movsb", "movsd", "movsw", "stosb", "stosd", "stosw", +- "lodsb", "lodsd", "lodsw"): ++ if (m in ("movsb", "movsw", "stosb", "stosd", "stosw", ++ "lodsb", "lodsd", "lodsw") ++ or (m == "movsd" and nops == 0)): + return self._lift_string_op(insn, m) + if m == "wait": + return ["/* wait - FPU sync */"] +@@ -1163,7 +1302,9 @@ + if m == "lahf": + return ["/* lahf - load AH from flags (used in FPU compare idiom) */"] + if m == "sahf": +- return ["/* sahf - store AH to flags */"] ++ out = ["_sahf_flags = (eax >> 8) & 0xD5u; /* SAHF snapshots AH; preserves OF */"] ++ if getattr(self, "needs_cf", False): out.append("_cf = _sahf_flags & 1u;") ++ return out + if m == "shld": + return self._lift_shld(insn, ops) + if m == "shrd": +@@ -1172,12 +1313,9 @@ + if len(ops) >= 2: + return [f"/* bt {_fmt_operand_read(ops[0])}, {_fmt_operand_read(ops[1])} - bit test */"] + return [f"/* bt {insn.op_str} */"] +- if m == "emms": +- # A statement rather than a comment: emms genuinely has no +- # effect here (mm/x87 aliasing is not modelled), and a +- # comment-only lift is how the conformance suite reports a +- # silently dropped instruction. +- return ["(void)0; /* emms - empty MMX state */"] ++ if m in ("emms", "femms"): ++ # EMMS marks every physical x87 register empty without changing TOP. ++ return [f"g_fp_empty_mask = 0xFFu; /* {m}: all x87 registers empty */"] + if m in ("xlat", "xlatb"): + # AL indexes the byte table at EBX. With an address-size override, + # the effective offset is calculated and wrapped at 16 bits. +@@ -1188,7 +1326,8 @@ + address = "ebx + LO8(eax)" + return [f"SET_LO8(eax, MEM8({address})); /* xlatb */"] + if m in ("sete", "setne", "setb", "setae", "setbe", "seta", +- "setl", "setge", "setle", "setg", "sets", "setns"): ++ "setl", "setge", "setle", "setg", "sets", "setns", ++ "setp", "setnp", "seto", "setno"): + return self._lift_setcc(insn, ops, m) + if m in ("cmove", "cmovne", "cmovb", "cmovae", "cmovbe", "cmova", + "cmovl", "cmovge", "cmovle", "cmovg", "cmovs", "cmovns"): +@@ -1211,7 +1350,8 @@ + "emms", "femms"): + return self._lift_mmx(insn, m, ops) + +- if m in ("movss", "movsd", "movaps", "movups", "movlps", "movhps", ++ if m in ("stmxcsr", "ldmxcsr", ++ "movss", "movsd", "movaps", "movups", "movlps", "movhps", "movlpd", + "movlhps", "movhlps", "movapd", "movupd", + "addss", "subss", "mulss", "divss", "sqrtss", + "addsd", "subsd", "mulsd", "divsd", "sqrtsd", +@@ -1227,7 +1367,8 @@ + "minps", "maxps", "rsqrtss", "rcpss", + "sqrtps", "rsqrtps", "rcpps", + "cmpneqps", "cmpeqps", "cmpltps", "cmpleps", +- "movmskps", ++ "movmskps", "subpd", "punpckldq", "unpckhpd", ++ "cmpltss", "cmpeqss", "pcmpeqb", "pmovmskb", + "pand", "pandn", "por", "pxor", "pcmpgtd"): + return self._lift_sse(insn, m, ops) + +@@ -1244,7 +1385,7 @@ + # Cache hints and store fences: nothing to model on a single-threaded + # interpreter over coherent host memory. Named so they stop showing up + # in the unimplemented report as if they were missing work. +- if m.startswith("prefetch") or m in ("sfence", "lfence", "mfence"): ++ if m.startswith("prefetch") or m in ("sfence", "lfence", "mfence", "pause"): + return [f"(void)0; /* {m}: cache/ordering hint, nothing to model */"] + + if m in ("stc", "clc", "cmc"): +@@ -1287,14 +1428,27 @@ + # would leave, which is what _make_condition reads for a following jcc. + # The other flags are architecturally undefined here, so a jcc that + # reads them was already meaningless. +- if m in ("bsf", "bsr"): ++ if m in ("bsf", "bsr", "tzcnt"): + if len(ops) < 2: + self.unimplemented.setdefault(m, []).append(insn.address) +- return [f"/* TODO: {m} {insn.op_str} */"] ++ return [f"recomp_unsupported_instruction(0x{insn.address:08X}u); /* TODO: {m} {insn.op_str} */"] + dst = _fmt_operand_read(ops[0]) + src = _fmt_operand_read(ops[1]) + width = (_operand_width(ops[1]) or 4) * 8 +- if m == "bsr": ++ if m == "tzcnt": ++ if width > 32: ++ self.unimplemented.setdefault(m, []).append(insn.address) ++ return [f"recomp_unsupported_instruction(0x{insn.address:08X}u); /* unsupported wide {m} */"] ++ return [ ++ f"{{ uint32_t _tz_src = (uint32_t)({src}); uint32_t _tz_count = {width}u;", ++ f" if (_tz_src) for (uint32_t _tz_i = 0; _tz_i < {width}u; ++_tz_i)", ++ " if (_tz_src & (1u << _tz_i)) { _tz_count = _tz_i; break; }", ++ " " + _fmt_operand_write(ops[0], "_tz_count") + ";", ++ " _fa = _tz_count; _fb = _tz_src; _fas = (int32_t)_fa; _fbs = (int32_t)_fb;", ++ (" _cf = (_tz_src == 0);" if self.needs_cf else " (void)_tz_src;") ++ + " } /* tzcnt */", ++ ] ++ elif m == "bsr": + init, cond, step = f"{width - 1}", "_bs_i >= 0", "_bs_i--" + else: + init, cond, step = "0", f"_bs_i < {width}", "_bs_i++" +@@ -1327,6 +1481,28 @@ + # unlocked xadd on memory is single-CPU-safe on the console and + # costs nothing extra to make safe here. + atomic_m = m[5:] if m.startswith("lock ") else m ++ if m.startswith("lock ") and len(ops) >= 1: ++ width = _operand_width(ops[0]) or 4 ++ if width == 4 and ops[0].type == "mem": ++ address = _fmt_mem(ops[0]) ++ if atomic_m in ("inc", "dec"): ++ delta = 1 if atomic_m == "inc" else -1 ++ return [ ++ f"{{ uint32_t _old = RECOMP_ATOMIC_ADD32(XBOX_PTR({address}), {delta}u);", ++ f" uint32_t _new = _old {'+' if delta > 0 else '-'} 1u;", ++ " _fa = _new; _fb = 0; _fas = (int32_t)_new; _fbs = 0; }" ++ f" /* {m} */", ++ ] ++ if atomic_m in ("or", "and") and len(ops) >= 2: ++ value = _fmt_operand_read(ops[1]) ++ op = "|" if atomic_m == "or" else "&" ++ helper = "RECOMP_ATOMIC_OR32" if atomic_m == "or" else "RECOMP_ATOMIC_AND32" ++ return [ ++ f"{{ uint32_t _old = {helper}(XBOX_PTR({address}), {value});", ++ f" uint32_t _new = _old {op} (uint32_t)({value});", ++ " _fa = _new; _fb = 0; _fas = (int32_t)_new; _fbs = 0; }" ++ f" /* {m} */", ++ ] + if atomic_m in ("xadd", "cmpxchg") and len(ops) >= 2: + width = _operand_width(ops[0]) or 4 + if width == 4 and ops[0].type == "mem": +@@ -1359,11 +1535,14 @@ + # + # Recorded, not merely commented -- see self.unimplemented. + self.unimplemented.setdefault(m, []).append(insn.address) +- return [f"/* TODO: {m} {insn.op_str} */"] ++ return [f"recomp_unsupported_instruction(0x{insn.address:08X}u); /* TODO: {m} {insn.op_str} */"] + + # ── MOV family ── + + def _lift_mov(self, insn, ops): ++ if any(op.type == "reg" and op.reg.startswith(("dr", "cr")) for op in ops): ++ self.unimplemented.setdefault(insn.mnemonic, []).append(insn.address) ++ return [f"recomp_unsupported_instruction(0x{insn.address:08X}u); /* privileged mov */"] + if nops := len(ops) < 2: + return [f"/* mov: bad operands */"] + src = _fmt_operand_read(ops[1]) +@@ -1490,7 +1669,15 @@ + out.append(f"_cf = (int)((uint32_t)({dst}) < (uint32_t)({src}));") + else: + out.append("_cf = 0; /* logical op clears CF */") +- out.append(_fmt_operand_write(ops[0], expr)) ++ if m == "sub" and getattr(self, "needs_zf_snapshot", False): ++ # Capture the executed result once, before a later MOV or join can ++ # change the destination. Do not re-read memory to reconstruct ZF. ++ mask = self._SNAP_MASK.get(_operand_width(ops[0]) or 4, "0xFFFFFFFFu") ++ out.extend([f"{{ uint32_t _zr = ((uint32_t)({dst}) - (uint32_t)({src})) & {mask};", ++ "_zf = (_zr == 0);", ++ _fmt_operand_write(ops[0], "_zr") + " }"]) ++ else: ++ out.append(_fmt_operand_write(ops[0], expr)) + return out + + def _lift_inc_dec(self, insn, ops, m): +@@ -1499,6 +1686,11 @@ + val = _fmt_operand_read(ops[0]) + delta = "1" + op_char = "+" if m == "inc" else "-" ++ if m == "dec" and getattr(self, "needs_zf_snapshot", False): ++ mask = self._SNAP_MASK.get(_operand_width(ops[0]) or 4, "0xFFFFFFFFu") ++ return [f"{{ uint32_t _zr = ((uint32_t)({val}) - 1u) & {mask};", ++ "_zf = (_zr == 0);", ++ _fmt_operand_write(ops[0], "_zr") + " }"] + # For sub-registers (al, cl, etc.), use the SET macro instead of ++ + if ops[0].type == "reg" and ops[0].reg in ( + "eax", "ebx", "ecx", "edx", "esi", "edi", "ebp", "esp"): +@@ -1514,7 +1706,7 @@ + if preserve_carry or self.needs_cf: + # neg sets CF iff the operand was non-zero (neg/sbb sign-extract). + out.append(f"_cf = (int)(({val}) != 0);") +- out.append(_fmt_operand_write(ops[0], f"(uint32_t)(-(int32_t){val})")) ++ out.append(_fmt_operand_write(ops[0], f"(0u - (uint32_t)({val}))")) + return out + + def _lift_not(self, insn, ops): +@@ -1524,18 +1716,32 @@ + return [_fmt_operand_write(ops[0], f"~{val}")] + + def _lift_sbb(self, insn, ops): +- """SBB: subtract with borrow. Common idiom: sbb reg, reg → -CF (0 or -1).""" ++ """SBB: subtract with borrow and retain the flags consumed by later branches.""" + if len(ops) < 2: + return ["/* sbb: bad operands */"] + dst = _fmt_operand_read(ops[0]) + src = _fmt_operand_read(ops[1]) +- # sbb reg, reg is a common idiom: result is 0 or 0xFFFFFFFF depending on CF +- if ops[0].type == "reg" and ops[1].type == "reg" and ops[0].reg == ops[1].reg: +- return [_fmt_operand_write(ops[0], "_cf ? 0xFFFFFFFF : 0") + " /* sbb self (CF extend) */"] +- w = (_operand_width(ops[0]) or 4) * 8 +- return ["{ uint64_t _t = (uint64_t)(%s) - (uint64_t)(%s) - (uint64_t)_cf;" +- " _cf = (int)((_t >> %d) & 1); %s } /* sbb */" +- % (dst, src, w, _fmt_operand_write(ops[0], "(uint32_t)_t"))] ++ size = _operand_width(ops[0]) or 4 ++ bits = size * 8 ++ mask = self._SNAP_MASK.get(size, "0xFFFFFFFFu") ++ sx = self._SNAP_SX.get(size, "(int32_t)") ++ min_signed = -(1 << (bits - 1)) ++ max_signed = (1 << (bits - 1)) - 1 ++ result_mask = f"0x{(1 << bits) - 1:X}ULL" ++ sign_mask = f"0x{1 << (bits - 1):X}ULL" ++ return [ ++ "{", ++ f" _fa = (uint32_t)({dst}) & {mask}; _fb = (uint32_t)({src}) & {mask};", ++ f" _fas = (int32_t){sx}(_fa); _fbs = (int32_t){sx}(_fb);", ++ " uint32_t _sbb_borrow_in = (uint32_t)!!_cf;", ++ f" int64_t _sbb_math = (int64_t)_fas - (int64_t)_fbs - (int64_t)_sbb_borrow_in;", ++ f" _sbb_result = (uint32_t)((uint64_t)_sbb_math & {result_mask});", ++ f" _sbb_sf = !!((uint64_t)_sbb_result & {sign_mask});", ++ f" _sbb_of = (_sbb_math < (int64_t)({min_signed}) || _sbb_math > (int64_t)({max_signed}));", ++ " _cf = (int)(_fa < _fb || (_sbb_borrow_in && _fa == _fb));", ++ f" {_fmt_operand_write(ops[0], '_sbb_result')}", ++ "} /* sbb */", ++ ] + + def _lift_adc(self, insn, ops): + """ADC: add with carry.""" +@@ -1549,24 +1755,25 @@ + % (dst, src, w, _fmt_operand_write(ops[0], "(uint32_t)_t"))] + + def _lift_shld(self, insn, ops): +- """SHLD: double-precision shift left.""" ++ return self._lift_double_shift(insn, ops, False) ++ ++ def _lift_shrd(self, insn, ops): ++ return self._lift_double_shift(insn, ops, True) ++ ++ def _lift_double_shift(self, insn, ops, right): + if len(ops) < 3: +- return [f"/* shld: bad operands */"] +- dst = _fmt_operand_read(ops[0]) +- src = _fmt_operand_read(ops[1]) +- cnt = _fmt_operand_read(ops[2]) +- return [_fmt_operand_write(ops[0], +- f"({dst} << {cnt}) | ({src} >> (32 - {cnt}))") + " /* shld */"] +- +- def _lift_shrd(self, insn, ops): +- """SHRD: double-precision shift right.""" +- if len(ops) < 3: +- return [f"/* shrd: bad operands */"] +- dst = _fmt_operand_read(ops[0]) +- src = _fmt_operand_read(ops[1]) +- cnt = _fmt_operand_read(ops[2]) +- return [_fmt_operand_write(ops[0], +- f"({dst} >> {cnt}) | ({src} << (32 - {cnt}))") + " /* shrd */"] ++ return ["/* double shift: bad operands */"] ++ if ops[2].type == "imm" and (ops[2].imm & 31) == 0: ++ return ["(void)0; /* double shift: masked zero count preserves flags/result */"] ++ dst, src, cnt = [_fmt_operand_read(op) for op in ops[:3]] ++ bits = (_operand_width(ops[0]) or 4) * 8 ++ carry = "&_cf" if self.needs_cf else "NULL" ++ snapshot = getattr(self, "needs_shift_snapshot", False) ++ overflow = "&_shift_of" if snapshot else "NULL" ++ expression = f"RECOMP_DOUBLE_SHIFT({dst}, {src}, {cnt}, {bits}, {int(right)}, {carry}, {overflow})" ++ if snapshot: ++ return [f"_shift_result = {expression};", _fmt_operand_write(ops[0], "_shift_result")] ++ return [_fmt_operand_write(ops[0], expression)] + + def _lift_imul(self, insn, ops): + nops = len(ops) +@@ -1614,36 +1821,53 @@ + + def _lift_shift(self, insn, ops, c_op): + if len(ops) < 2: +- return [f"/* shift: bad operands */"] ++ return ["/* shift: bad operands */"] ++ if ops[1].type == "imm" and (ops[1].imm & 31) == 0: ++ return ["(void)0; /* logical shift: masked zero count preserves flags/result */"] + dst = _fmt_operand_read(ops[0]) + cnt = _fmt_operand_read(ops[1]) +- out = [] +- if self.needs_cf: +- w = (_operand_width(ops[0]) or 4) * 8 +- # CF is the last bit shifted out; a zero count leaves CF alone. +- bit = f"({cnt}) - 1" if c_op == ">>" else f"{w} - ({cnt})" +- out.append(f"if ({cnt}) _cf = (int)((({dst}) >> ({bit})) & 1);") +- out.append(_fmt_operand_write(ops[0], f"{dst} {c_op} {cnt}")) +- return out ++ bits = (_operand_width(ops[0]) or 4) * 8 ++ carry = "&_cf" if self.needs_cf else "NULL" ++ snapshot = getattr(self, "needs_shift_snapshot", False) ++ overflow = "&_shift_of" if snapshot else "NULL" ++ expression = f"RECOMP_SHIFT({dst}, {cnt}, {bits}, {int(c_op == '>>')}, {carry}, {overflow})" ++ if snapshot: ++ return [f"_shift_result = {expression};", _fmt_operand_write(ops[0], "_shift_result")] ++ return [_fmt_operand_write(ops[0], expression)] + + def _lift_sar(self, insn, ops): + if len(ops) < 2: + return ["/* sar: bad operands */"] + dst = _fmt_operand_read(ops[0]) + cnt = _fmt_operand_read(ops[1]) +- out = [] +- if self.needs_cf: +- out.append(f"if ({cnt}) _cf = (int)((({dst}) >> (({cnt}) - 1)) & 1);") +- out.append(_fmt_operand_write(ops[0], f"(uint32_t)((int32_t){dst} >> {cnt})")) +- return out ++ bits = (_operand_width(ops[0]) or 4) * 8 ++ carry = "&_cf" if self.needs_cf else "NULL" ++ return [_fmt_operand_write(ops[0], f"RECOMP_SAR({dst}, {cnt}, {bits}, {carry})")] + + def _lift_rotate(self, insn, ops, m): + if len(ops) < 2: + return [f"/* {m}: bad operands */"] + dst = _fmt_operand_read(ops[0]) + cnt = _fmt_operand_read(ops[1]) +- func = "ROL32" if m == "rol" else "ROR32" +- return [_fmt_operand_write(ops[0], f"{func}({dst}, {cnt})")] ++ bits = (_operand_width(ops[0]) or 4) * 8 ++ carry = "&_cf" if self.needs_cf else "NULL" ++ expression = f"RECOMP_ROTATE({dst}, {cnt}, {bits}, {int(m == 'ror')}, {carry}, NULL)" ++ if getattr(self, "needs_rotate_snapshot", False): ++ return [f"_rotate_result = {expression};", _fmt_operand_write(ops[0], "_rotate_result")] ++ return [_fmt_operand_write(ops[0], expression)] ++ ++ def _lift_carry_rotate(self, insn, ops, m): ++ if len(ops) < 2: ++ return [f"/* {m}: bad operands */"] ++ if ops[1].type == "imm" and (ops[1].imm & 31) == 0: ++ return ["(void)0; /* carry rotate: masked zero count preserves flags/result */"] ++ dst = _fmt_operand_read(ops[0]) ++ cnt = _fmt_operand_read(ops[1]) ++ bits = (_operand_width(ops[0]) or 4) * 8 ++ expression = f"RECOMP_CARRY_ROTATE({dst}, {cnt}, {bits}, {int(m == 'rcr')}, &_cf, NULL)" ++ if getattr(self, "needs_rotate_snapshot", False): ++ return [f"_rotate_result = {expression};", _fmt_operand_write(ops[0], "_rotate_result")] ++ return [_fmt_operand_write(ops[0], expression)] + + # ── Compare / Test (standalone) ── + +@@ -1681,6 +1905,14 @@ + f"_fas = (int32_t){sx}(_fa); _fbs = (int32_t){sx}(_fb);" + f" /* {kind} {lhs}, {rhs} ({size*8}-bit) */", + ] ++ if getattr(self, "needs_zf_snapshot", False): ++ out.append("_zf = (_fa == _fb);" if kind == "cmp" ++ else "_zf = ((_fa & _fb) == 0);") ++ if getattr(self, "needs_signed_snapshot", False): ++ # Signed-less is SF xor OF, not SF alone (CMP may overflow). ++ # TEST clears OF and takes SF from its width-masked AND result. ++ out.append("_slt = (_fas < _fbs);" if kind == "cmp" ++ else f"_slt = ((int32_t){sx}(_fa & _fb) < 0);") + # A cmp sets the carry flag too, and sbb/adc/setc/rcl read it directly + # rather than through _fa/_fb. Leaving CF alone here let those pick up + # whatever an earlier instruction had left in it. +@@ -1757,16 +1989,18 @@ + # value -- so writing the true address costs nothing at the return side. + ret_va = insn.end_address + if insn.call_target: ++ # GCC/musl i386 position-independent code materializes the current ++ # instruction address with `call $+5; pop reg`. The target is the ++ # call's fallthrough, so this is not a guest function boundary or ++ # a cross-function ABI call: only the architectural return-address ++ # push matters. Calling a synthetic `sub_` here makes a ++ # local label look unresolved and breaks the following PIC pop/add. ++ if insn.call_target == ret_va: ++ return [f"PUSH32(esp, 0x{ret_va:08X}u); /* call next: materialize EIP */"] + name = self._call_target_name(insn.call_target) + lines = [] +- # Re-publish this function's frame before every call, not just once +- # at `mov ebp, esp`. g_ebp is "the last frame established anywhere", +- # so a callee that sets up its own frame overwrites it and leaves it +- # stale on return. A frameless helper called afterwards then +- # inherits the wrong frame -- in Halo, sub_001E1BA0 called one +- # function, returned, then called the frameless sub_001DEC07, which +- # inherited a long-dead frame of ~0xA6 and wrote [ebp-0xa2] and +- # [ebp-0xa0] onto Xbox VA 4 and 6: exactly the fs:[4] corruption. ++ # Publish the actual local register, including nonstandard EBP ++ # arguments, before crossing a compiled-function boundary. + if self.publishes_ebp: + lines.append("g_ebp = ebp; /* frame stays current across calls */") + lines.append("g_seh_ebp = ebp;") +@@ -1856,7 +2090,8 @@ + # target -- `call dword ptr [esp+8]`, the shape MSVC gives a + # callback invoked through a stack argument -- is read four bytes + # low and dispatches through the wrong slot. +- return [f"{{ uint32_t _icall_target = {target}; " ++ prefix = "g_ebp = g_seh_ebp = ebp; " if self.publishes_ebp else "" ++ return [f"{prefix}{{ uint32_t _icall_target = {target}; " + f"PUSH32(esp, 0x{ret_va:08X}u); " + "RECOMP_ICALL_SAFE(_icall_target, _icall_esp); }" + " /* indirect call */"] +@@ -1865,11 +2100,13 @@ + def _lift_ret(self, insn, ops): + # x86 'ret' pops return address from stack. + # 'ret N' also pops N extra bytes (stdcall cleanup). +- # If this function IS __SEH_prolog or __SEH_epilog, bridge ebp +- # so the caller can read back the frame pointer. ++ # Publish the register after the original POP/LEAVE epilog. Relays ++ # without a local EBP then inherit the restored frame, not a dead child. ++ # SEH helpers deliberately return a changed frame; keep that exchange. + prefix = "" +- if self.func_start in (self.SEH_PROLOG, self.SEH_EPILOG): +- prefix = "g_seh_ebp = ebp; " ++ if (self.publishes_ebp ++ or self.func_start in (self.SEH_PROLOG, self.SEH_EPILOG)): ++ prefix = "g_ebp = g_seh_ebp = ebp; " + # Exit trace, for functions that return with a register the caller + # relied on holding something else. Entry tracing alone cannot show + # that: it tells you what went in, never what came back. Emitted at the +@@ -1958,7 +2195,7 @@ + # Bridge ebp so the target function can inherit our frame pointer. + if insn.jump_target in self.manual_functions: + tail = ( +- f"g_seh_ebp = ebp; " ++ f"g_ebp = g_seh_ebp = ebp; " + f"RECOMP_ITAIL(0x{insn.jump_target:08X}u); return; " + f"/* manual tail jmp 0x{insn.jump_target:08X} */" + ) +@@ -1970,7 +2207,7 @@ + ] + return [tail] + name = self._call_target_name(insn.jump_target) +- tail = (f"g_seh_ebp = ebp; {name}(); return; " ++ tail = (f"g_ebp = g_seh_ebp = ebp; {name}(); return; " + f"/* tail jmp 0x{insn.jump_target:08X} */") + if self.trace_exit_name: + # Tag the exit so a trace says which one was taken. A +@@ -1982,6 +2219,15 @@ + return [tail] + return [f"goto loc_{insn.jump_target:08X};"] + elif len(ops) >= 1: ++ local_targets = self.local_indirect_targets.get(insn.address, ()) ++ if local_targets: ++ target_expr = _fmt_operand_read(ops[0]) ++ unique_targets = sorted(set(local_targets)) ++ lines = [f"{{ uint32_t _jt = {target_expr}; /* recovered local computed goto */"] ++ for target in unique_targets: ++ lines.append(f"if (_jt == 0x{target:08X}u) goto loc_{target:08X};") ++ lines.append(f"g_ebp = g_seh_ebp = ebp; RECOMP_ITAIL(_jt); return; }}") ++ return lines + # Detect intra-function switch tables (computed gotos) + switch_targets = self._analyze_switch_table(ops) + if switch_targets: +@@ -1990,10 +2236,10 @@ + lines = [f"{{ uint32_t _jt = {target_expr}; /* switch: {len(switch_targets)} entries, {len(unique_targets)} targets */"] + for t in unique_targets: + lines.append(f"if (_jt == 0x{t:08X}u) goto loc_{t:08X};") +- lines.append(f"g_seh_ebp = ebp; RECOMP_ITAIL(_jt); return; }}") ++ lines.append(f"g_ebp = g_seh_ebp = ebp; RECOMP_ITAIL(_jt); return; }}") + return lines + target = _fmt_operand_read(ops[0]) +- return [f"g_seh_ebp = ebp; RECOMP_ITAIL({target}); return; /* indirect tail jmp */"] ++ return [f"g_ebp = g_seh_ebp = ebp; RECOMP_ITAIL({target}); return; /* indirect tail jmp */"] + return ["/* jmp: no target */"] + + def _lift_jcc(self, insn): +@@ -2007,7 +2253,7 @@ + if target: + if self._is_external_target(target): + name = self._call_target_name(target) +- return [f"if ({cond}) {{ g_seh_ebp = ebp; {name}(); return; }} /* {jcc} */"] ++ return [f"if ({cond}) {{ g_ebp = g_seh_ebp = ebp; {name}(); return; }} /* {jcc} */"] + return [f"if ({cond}) goto loc_{target:08X}; /* {jcc} */"] + return [f"/* {jcc} - no target */"] + +@@ -2030,7 +2276,7 @@ + if target: + if self._is_external_target(target): + name = self._call_target_name(target) +- return [f"if ({cond} /* {jcc}: {desc} */) {{ g_seh_ebp = ebp; {name}(); return; }}"] ++ return [f"if ({cond} /* {jcc}: {desc} */) {{ g_ebp = g_seh_ebp = ebp; {name}(); return; }}"] + return [f"if ({cond} /* {jcc}: {desc} */) goto loc_{target:08X};"] + return [f"/* {jcc}: {desc} - no target */"] + +@@ -2263,21 +2509,13 @@ + if not ops: + return [f"/* {m}: no operands */"] + +- # emms/femms: only the x87 tag word, which is not modelled. +- if m in ("emms", "femms"): +- # A real statement, not a comment: this instruction genuinely has +- # no effect in this model, and the conformance suite treats a +- # comment-only lift as a silently dropped instruction -- which is +- # exactly the signal that must stay meaningful for the ones that +- # really are missing. +- return [f"(void)0; /* {m}: mm/x87 aliasing is not modelled */"] + + dst = ops[0] + + if m in self._MMX_BINARY and len(ops) >= 2: + a, b = src(dst), src(ops[1]) + if a is None or b is None: +- return [f"/* TODO: {m} {insn.op_str} */"] ++ return [f"recomp_unsupported_instruction(0x{insn.address:08X}u); /* TODO: {m} {insn.op_str} */"] + if is_mm(dst): + return [f"{dst.reg} = {self._MMX_BINARY[m]}({a}, {b}); /* {m} */"] + return [f"/* TODO: {m} {insn.op_str} (dst not mm) */"] +@@ -2291,7 +2529,7 @@ + elif count.type == "mem": + cnt = f"MMX_MEM({_fmt_mem(count)}).q" + else: +- return [f"/* TODO: {m} {insn.op_str} */"] ++ return [f"recomp_unsupported_instruction(0x{insn.address:08X}u); /* TODO: {m} {insn.op_str} */"] + return [f"{dst.reg} = {self._MMX_SHIFT[m]}({dst.reg}, {cnt}); /* {m} */"] + + # cvtps2pi / cvttps2pi: two singles in, two dwords out. The source is +@@ -2306,20 +2544,20 @@ + addr = _fmt_mem(s_op) + lo, hi = f"MEMF({addr})", f"MEMF(({addr}) + 4)" + else: +- return [f"/* TODO: {m} {insn.op_str} */"] ++ return [f"recomp_unsupported_instruction(0x{insn.address:08X}u); /* TODO: {m} {insn.op_str} */"] + return [f"{dst.reg} = MMX_FROM_PS({lo}, {hi}, {trunc}); /* {m} */"] + + if m == "pshufw" and len(ops) >= 3 and is_mm(dst): + a = src(ops[1]) + if a is None: +- return [f"/* TODO: {m} {insn.op_str} */"] ++ return [f"recomp_unsupported_instruction(0x{insn.address:08X}u); /* TODO: {m} {insn.op_str} */"] + return [f"{dst.reg} = MMX_PSHUFW({a}, {ops[2].imm & 0xFF}u); /* pshufw */"] + + if m == "pinsrw" and len(ops) >= 3 and is_mm(dst): + v = (f"{ops[1].reg}" if ops[1].type == "reg" + else f"MEM16({_fmt_mem(ops[1])})" if ops[1].type == "mem" else None) + if v is None: +- return [f"/* TODO: {m} {insn.op_str} */"] ++ return [f"recomp_unsupported_instruction(0x{insn.address:08X}u); /* TODO: {m} {insn.op_str} */"] + return [f"{dst.reg} = MMX_PINSRW({dst.reg}, {v}, " + f"{ops[2].imm & 0xFF}u); /* pinsrw */"] + +@@ -2351,20 +2589,37 @@ + " /* movd */"]) + if dst.type == "reg": + return [f"{dst.reg} = {ops[1].reg}.ud[0]; /* movd */"] +- return [f"/* TODO: {m} {insn.op_str} */"] ++ return [f"recomp_unsupported_instruction(0x{insn.address:08X}u); /* TODO: {m} {insn.op_str} */"] + + # movntq: a non-temporal store. The hint is irrelevant; the store is not. + if m == "movntq" and len(ops) >= 2 and dst.type == "mem" and is_mm(ops[1]): + return [f"MMX_STORE({_fmt_mem(dst)}, {ops[1].reg}); /* movntq */"] + + self.unimplemented.setdefault(m, []).append(insn.address) +- return [f"/* TODO: {m} {insn.op_str} */"] ++ return [f"recomp_unsupported_instruction(0x{insn.address:08X}u); /* TODO: {m} {insn.op_str} */"] + + def _lift_sse(self, insn, m, ops): + """Translate SSE instructions to C float operations.""" + nops = len(ops) + if nops < 1: + return [f"/* {m}: no operands */"] ++ ++ if m in ("stmxcsr", "ldmxcsr"): ++ if len(ops) != 1 or ops[0].type != "mem": ++ return [f"recomp_unsupported_instruction(0x{insn.address:08X}u); /* {m} requires m32 */"] ++ addr = _fmt_mem(ops[0]) ++ if m == "stmxcsr": ++ return [f"MEM32({addr}) = g_mxcsr; /* stmxcsr: guest MXCSR */"] ++ # Pentium III's architecturally defined MXCSR bits (DAZ bit 6 is ++ # reserved on that target). Invalid reserved bits fault instead ++ # of being silently normalized into a different guest value. ++ return [ ++ f"{{ uint32_t _new_mxcsr = MEM32({addr});", ++ " if (_new_mxcsr & ~0x0000FFBFu)", ++ f" recomp_unsupported_instruction(0x{insn.address:08X}u);", ++ " else g_mxcsr = _new_mxcsr;", ++ "} /* ldmxcsr: guest MXCSR */", ++ ] + + def _is_xmm(op): + return op.type == "reg" and op.reg and op.reg.startswith("xmm") +@@ -2400,6 +2655,26 @@ + return f"MEMF({_fmt_mem(op)}) = {val};" + return f"/* sse_write? */;" + ++ def _sse_read_double(op): ++ if _is_xmm(op): ++ return f"{op.reg}.d[0]" ++ if op.type == "reg": ++ return op.reg ++ if op.type == "mem": ++ return f"MEMD({_fmt_mem(op)})" ++ if op.type == "imm": ++ return _fmt_imm(op.imm) ++ return f"/* sse_read_double? */" ++ ++ def _sse_write_double(op, val): ++ if _is_xmm(op): ++ return f"{op.reg}.d[0] = {val};" ++ if op.type == "reg": ++ return f"{op.reg} = {val};" ++ if op.type == "mem": ++ return f"MEMD({_fmt_mem(op)}) = {val};" ++ return f"/* sse_write_double? */;" ++ + # ── Packed (128-bit) access ── + # A whole-register read yields a RecompXmm value; a whole-register + # write is a statement. Memory goes through the guest translation. +@@ -2454,6 +2729,14 @@ + f"{ops[1].reg}); /* {m} */"] + return [f"/* {m} {insn.op_str} */"] + ++ if m == "movlpd": ++ if nops >= 2: ++ if _is_xmm(ops[0]) and ops[1].type == "mem": ++ return [f"XMM_LOAD_LOW({ops[0].reg}, {_fmt_mem(ops[1])}); /* movlpd */"] ++ if ops[0].type == "mem" and _is_xmm(ops[1]): ++ return [f"XMM_STORE_LOW({_fmt_mem(ops[0])}, {ops[1].reg}); /* movlpd */"] ++ return [f"/* movlpd {insn.op_str} */"] ++ + if m in ("movlhps", "movhlps"): + helper = ("XMM_MOVE_LOW_TO_HIGH" if m == "movlhps" + else "XMM_MOVE_HIGH_TO_LOW") +@@ -2466,6 +2749,15 @@ + # a register-to-register move leaves them untouched. + if m in ("movss", "movsd"): + if nops >= 2: ++ if m == "movsd": ++ dst, src = ops[0], ops[1] ++ if _is_xmm(dst) and src.type == "mem": ++ return [f"{dst.reg} = XMM_SCALAR_DOUBLE(MEMD({_fmt_mem(src)})); /* movsd */"] ++ if _is_xmm(dst) and _is_xmm(src): ++ return [f"{dst.reg}.q[0] = {src.reg}.q[0]; /* movsd xmm,xmm: preserve upper 64 */"] ++ if dst.type == "mem" and _is_xmm(src): ++ return [f"MEMD({_fmt_mem(dst)}) = {src.reg}.d[0]; /* movsd */"] ++ return [f"/* movsd unsupported operands: {insn.op_str} */"] + scalar = ("XMM_SCALAR" if m == "movss" + else "XMM_SCALAR_DOUBLE") + if _is_xmm(ops[0]) and ops[1].type == "mem": +@@ -2497,30 +2789,43 @@ + return [f"{_fmt_operand_write(ops[0], src)} /* movd */"] + return [f"/* movd {insn.op_str} */"] + ++ # SSE2 MOVQ transfers exactly the low 64 bits. Loads into an XMM ++ # register clear its upper 64 bits; a register copy has the same ++ # architectural zeroing behavior, and stores read only the low qword. ++ if m == "movq": ++ if nops >= 2 and _is_xmm(ops[0]): ++ src = ops[1] ++ if _is_xmm(src): ++ return [f"{ops[0].reg}.q[0] = {src.reg}.q[0]; {ops[0].reg}.q[1] = 0; /* movq xmm,xmm */"] ++ if src.type == "mem": ++ return [f"{ops[0].reg} = XMM_SCALAR_DOUBLE(MEMD({_fmt_mem(src)})); /* movq xmm,m64 */"] ++ return [f"recomp_unsupported_instruction(0x{insn.address:08X}u); /* unsupported movq source: {insn.op_str} */"] ++ if nops >= 2 and ops[0].type == "mem" and _is_xmm(ops[1]): ++ return [f"MEMD({_fmt_mem(ops[0])}) = {ops[1].reg}.d[0]; /* movq m64,xmm */"] ++ return [f"recomp_unsupported_instruction(0x{insn.address:08X}u); /* unsupported movq operands: {insn.op_str} */"] ++ + # ── Arithmetic ── +- if m in ("addss", "addsd"): +- if nops >= 2: +- return [_sse_write(ops[0], f"{_sse_read(ops[0])} + {_sse_read(ops[1])}") + f" /* {m} */"] +- if m in ("subss", "subsd"): +- if nops >= 2: +- return [_sse_write(ops[0], f"{_sse_read(ops[0])} - {_sse_read(ops[1])}") + f" /* {m} */"] +- if m in ("mulss", "mulsd"): +- if nops >= 2: +- return [_sse_write(ops[0], f"{_sse_read(ops[0])} * {_sse_read(ops[1])}") + f" /* {m} */"] +- if m in ("divss", "divsd"): +- if nops >= 2: +- return [_sse_write(ops[0], f"{_sse_read(ops[0])} / {_sse_read(ops[1])}") + f" /* {m} */"] +- if m in ("sqrtss", "sqrtsd"): +- if nops >= 2: +- return [_sse_write(ops[0], f"sqrtf({_sse_read(ops[1])})") + f" /* {m} */"] +- if m in ("minss", "minsd"): +- if nops >= 2: +- a, b = _sse_read(ops[0]), _sse_read(ops[1]) +- return [_sse_write(ops[0], f"({a} < {b} ? {a} : {b})") + f" /* {m} */"] +- if m in ("maxss", "maxsd"): +- if nops >= 2: +- a, b = _sse_read(ops[0]), _sse_read(ops[1]) +- return [_sse_write(ops[0], f"({a} > {b} ? {a} : {b})") + f" /* {m} */"] ++ if m in ("addss", "addsd", "subss", "subsd", "mulss", "mulsd", ++ "divss", "divsd") and nops >= 2: ++ double_op = m.endswith("sd") ++ read = _sse_read_double if double_op else _sse_read ++ write = _sse_write_double if double_op else _sse_write ++ operator = {"addss": "+", "addsd": "+", "subss": "-", "subsd": "-", ++ "mulss": "*", "mulsd": "*", "divss": "/", "divsd": "/"}[m] ++ return [write(ops[0], f"{read(ops[0])} {operator} {read(ops[1])}") + f" /* {m} */"] ++ if m in ("sqrtss", "sqrtsd") and nops >= 2: ++ double_op = m == "sqrtsd" ++ read = _sse_read_double if double_op else _sse_read ++ write = _sse_write_double if double_op else _sse_write ++ root = "sqrt" if double_op else "sqrtf" ++ return [write(ops[0], f"{root}({read(ops[1])})") + f" /* {m} */"] ++ if m in ("minss", "minsd", "maxss", "maxsd") and nops >= 2: ++ double_op = m.endswith("sd") ++ read = _sse_read_double if double_op else _sse_read ++ write = _sse_write_double if double_op else _sse_write ++ a, b = read(ops[0]), read(ops[1]) ++ operator = "<" if m.startswith("min") else ">" ++ return [write(ops[0], f"({a} {operator} {b} ? {a} : {b})") + f" /* {m} */"] + + # ── Packed arithmetic ── + # These used to emit a bare comment, so a matrix concatenation built +@@ -2532,6 +2837,11 @@ + if lifted is not None: + return lifted + return [f"/* {m} {insn.op_str} */"] ++ if m == "subpd": ++ lifted = _packed_binary("XMM_SUB_DOUBLE") ++ if lifted is not None: ++ return lifted ++ return [f"/* subpd {insn.op_str} */"] + + # ── Conversions ── + if m == "cvtsi2ss": +@@ -2544,16 +2854,16 @@ + if m == "cvtsi2sd": + if nops >= 2: + src = _fmt_operand_read(ops[1]) +- return [_sse_write(ops[0], f"(double)(int32_t){src}") + " /* cvtsi2sd */"] ++ return [_sse_write_double(ops[0], f"(double)(int32_t){src}") + " /* cvtsi2sd */"] + if m in ("cvtsd2si", "cvttsd2si"): + if nops >= 2: +- return [_fmt_operand_write(ops[0], f"(int32_t){_sse_read(ops[1])}") + f" /* {m} */"] ++ return [_fmt_operand_write(ops[0], f"(int32_t){_sse_read_double(ops[1])}") + f" /* {m} */"] + if m == "cvtss2sd": + if nops >= 2: +- return [_sse_write(ops[0], f"(double){_sse_read(ops[1])}") + " /* cvtss2sd */"] ++ return [_sse_write_double(ops[0], f"(double){_sse_read(ops[1])}") + " /* cvtss2sd */"] + if m == "cvtsd2ss": + if nops >= 2: +- return [_sse_write(ops[0], f"(float){_sse_read(ops[1])}") + " /* cvtsd2ss */"] ++ return [_sse_write(ops[0], f"(float){_sse_read_double(ops[1])}") + " /* cvtsd2ss */"] + + # ── Comparison ── + if m in ("comiss", "comisd", "ucomiss", "ucomisd"): +@@ -2563,8 +2873,17 @@ + # frequently a write to a register the operand address was + # built from -- so the operands have to be read now, while + # they still mean what the compare meant. +- return [f"_fca = {_sse_read(ops[0])}; _fcb = {_sse_read(ops[1])};" ++ read = _sse_read_double if m.endswith("d") else _sse_read ++ return [f"_fca = {read(ops[0])}; _fcb = {read(ops[1])};" + f" /* {m} */"] ++ ++ if m in ("cmpltss", "cmpeqss") and nops >= 2 and _is_xmm(ops[0]): ++ lhs, rhs = _sse_read(ops[0]), _sse_read(ops[1]) ++ comparison = "<" if m == "cmpltss" else "==" ++ # CMPSS writes an all-one/all-zero bit mask to the low dword and ++ # leaves the destination's upper 96 bits unchanged. Ordered ++ # predicates are false when either input is NaN. ++ return [f"{ops[0].reg}.u[0] = (!isnan({lhs}) && !isnan({rhs}) && ({lhs} {comparison} {rhs})) ? 0xFFFFFFFFu : 0u; /* {m} */"] + + # ── Bitwise ── + # Done on the integer lanes: these carry sign-mask and select idioms +@@ -2632,6 +2951,18 @@ + return lifted + return [f"/* {m} {insn.op_str} */"] + ++ if m == "pcmpeqb": ++ lifted = _packed_binary("XMM_PCMPEQB") ++ if lifted is not None: ++ return lifted ++ return [f"/* pcmpeqb {insn.op_str} */"] ++ ++ if m == "pmovmskb" and nops >= 2: ++ src = _packed_read(ops[1]) ++ if src is not None: ++ return [_fmt_operand_write(ops[0], f"XMM_PMOVMSKB({src})") + " /* pmovmskb */"] ++ return [f"/* pmovmskb {insn.op_str} */"] ++ + # ── Move mask ── + # This feeds branches, so a hardcoded 0 silently picked one side. + if m == "movmskps": +@@ -2643,9 +2974,18 @@ + return [f"/* {m} {insn.op_str} */"] + + # ── MMX / integer SIMD ── +- if m in ("pand", "pandn", "por", "pxor", "pcmpgtd"): +- if nops >= 2: +- return [f"/* {m} {insn.op_str} (MMX/SIMD integer) */"] ++ if m in ("pand", "pandn", "por", "pxor"): ++ helper = {"pand": "XMM_AND", "pandn": "XMM_ANDN", ++ "por": "XMM_OR", "pxor": "XMM_XOR"}[m] ++ lifted = _packed_binary(helper) ++ if lifted is not None: ++ return lifted ++ return [f"recomp_unsupported_instruction(0x{insn.address:08X}u); /* unsupported {m} operands: {insn.op_str} */"] ++ if m == "pcmpgtd" and nops >= 2: ++ lifted = _packed_binary("XMM_PCMPGTD") ++ if lifted is not None: ++ return lifted ++ return [f"recomp_unsupported_instruction(0x{insn.address:08X}u); /* unsupported {m} operands: {insn.op_str} */"] + + # ── Shuffle/unpack ── + # shufps is the broadcast in every matrix concatenation. +@@ -2660,15 +3000,16 @@ + if written is not None: + return [written + f" /* {m} */"] + return [f"/* {m} {insn.op_str} */"] +- if m in ("unpcklps", "unpckhps"): +- helper = ("XMM_UNPACK_LOW" if m == "unpcklps" ++ if m in ("unpcklps", "unpckhps", "punpckldq", "unpckhpd"): ++ helper = ("XMM_UNPACK_LOW" if m in ("unpcklps", "punpckldq") + else "XMM_UNPACK_HIGH") + lifted = _packed_binary(helper) + if lifted is not None: + return lifted + return [f"/* {m} {insn.op_str} */"] + +- return [f"/* SSE: {m} {insn.op_str} */"] ++ self.unimplemented.setdefault(m, []).append(insn.address) ++ return [f"recomp_unsupported_instruction(0x{insn.address:08X}u); /* unsupported SSE: {m} {insn.op_str} */"] + + # ── FPU (x87) ── + +@@ -2714,6 +3055,12 @@ + if m == "fld": + if len(ops) >= 1: + if ops[0].type == "mem": ++ if ops[0].mem_size == 10: ++ address = _fmt_mem(ops[0]) ++ va = f"0x{insn.address:08X}u" ++ return [f"{{ double _v; if (mac_guest_x87_80_load((const void *)XBOX_PTR({address}), " ++ f"(g_fp_control_word >> 10) & 3u, &_v) != 0) " ++ f"recomp_unsupported_instruction({va}); else fp_push(_v); }} /* fld tbyte */"] + if ops[0].mem_size == 4: + return [f"fp_push(MEMF({_fmt_mem(ops[0])})); /* fld float */"] + elif ops[0].mem_size == 8: +@@ -2741,6 +3088,12 @@ + do_pop = " fp_pop();" if m == "fstp" else "" + if len(ops) >= 1 and ops[0].type == "mem": + pop = " fp_pop();" if m == "fstp" else "" ++ if ops[0].mem_size == 10: ++ address = _fmt_mem(ops[0]) ++ va = f"0x{insn.address:08X}u" ++ pop_on_success = " fp_pop();" if m == "fstp" else "" ++ return [f"{{ if (mac_guest_x87_80_store((void *)XBOX_PTR({address}), fp_top()) != 0) " ++ f"recomp_unsupported_instruction({va}); else {{{pop_on_success}}} }} /* {m} tbyte */"] + if ops[0].mem_size == 4: + return [f"MEMF({_fmt_mem(ops[0])}) = (float)fp_top();{do_pop} /* {m} */"] + elif ops[0].mem_size == 8: +@@ -2758,6 +3111,10 @@ + dst = "fp_st1()" if idx == 1 else \ + f"g_fp_stack[(g_fp_top + {idx}) & 7]" + parts.append(f"{dst} = fp_top();") ++ parts.append(f"g_fp_empty_mask = (uint8_t)((g_fp_empty_mask & " ++ f"~(1u << ((g_fp_top + {idx}) & 7u))) | " ++ f"(((g_fp_empty_mask >> g_fp_top) & 1u) << " ++ f"((g_fp_top + {idx}) & 7u)));") + if m == "fstp": + parts.append("fp_pop();") + body = " ".join(parts) if parts else "(void)0;" +@@ -2776,9 +3133,32 @@ + mem_acc = _smem_accessor(size) + int_type = {2: "int16_t", 4: "int32_t", 8: "int64_t"}.get( + size, "int32_t") ++ bits = {2: 16, 4: 32, 8: 64}.get(size, 32) + pop = " fp_pop();" if m == "fistp" else "" +- return [f"{mem_acc}({_fmt_mem(ops[0])}) = " +- f"({int_type})llrint(fp_top());{pop} /* {m} */"] ++ # FLDCW changes the guest rounding control, not the host's ++ # floating-point environment. D3DX explicitly selects truncate ++ # before FISTP(value*255 + .5); host llrint rounded white to 256 ++ # and corrupted the compressed texture's color/alpha bytes. ++ # Implement nearest-even independently of host rounding too. ++ return [ ++ "{ double _fist_value = fp_top(), _fist_rounded;", ++ " switch ((g_fp_control_word >> 10) & 3u) {", ++ " case 1: _fist_rounded = floor(_fist_value); break;", ++ " case 2: _fist_rounded = ceil(_fist_value); break;", ++ " case 3: _fist_rounded = trunc(_fist_value); break;", ++ " default: { double _fist_floor = floor(_fist_value);", ++ " double _fist_fraction = _fist_value - _fist_floor;", ++ " _fist_rounded = _fist_floor + " ++ "(_fist_fraction > 0.5 || (_fist_fraction == 0.5 && " ++ "fmod(_fist_floor, 2.0) != 0.0)); break; }", ++ " }", ++ # Masked invalid conversions store the x87 integer ++ # indefinite value; avoid undefined out-of-range C casts. ++ f" {mem_acc}({_fmt_mem(ops[0])}) = " ++ f"(!isfinite(_fist_rounded) || _fist_rounded < -0x1p{bits - 1} " ++ f"|| _fist_rounded >= 0x1p{bits - 1}) ? INT{bits}_MIN : " ++ f"({int_type})_fist_rounded;{pop} }} /* {m}: guest x87 RC */", ++ ] + return [f"/* {m} {insn.op_str} */"] + + if m in ("fadd", "faddp", "fsub", "fsubp", "fsubr", "fsubrp", +@@ -2895,15 +3275,29 @@ + if m == "f2xm1": + return [f"fp_top() = exp2(fp_top()) - 1.0; /* f2xm1 */"] + if m in ("fprem", "fprem1"): +- # Both leave the remainder in st0 and clear C2 to say "complete". +- # fprem truncates toward zero, fprem1 rounds to nearest (IEEE), which +- # is the difference between fmod and remainder. +- fn = "fmod" if m == "fprem" else "remainder" +- return [f"fp_top() = {fn}(fp_top(), fp_st1()); /* {m} */"] ++ return [f"fp_top() = recomp_fp_remainder(fp_top(), fp_st1(), {int(m == 'fprem1')}, 0x{insn.address:08X}u); /* {m} */"] + if m == "fscale": + return [f"fp_top() = ldexp(fp_top(), (int)fp_st1()); /* fscale */"] + if m == "frndint": +- return [f"fp_top() = rint(fp_top()); /* frndint */"] ++ # FLDCW only updates the guest control word. Host rint therefore ++ # follows an unrelated rounding mode. Every finite binary64 value ++ # at or above2^52 is already integral; preserve NaNs/Inf and zeros. ++ return [ ++ "{ double _round_value = fp_top(), _round_result = _round_value;", ++ " if (isfinite(_round_value) && fabs(_round_value) < 0x1p52) {", ++ " switch ((g_fp_control_word >> 10) & 3u) {", ++ " case 1: _round_result = floor(_round_value); break;", ++ " case 2: _round_result = ceil(_round_value); break;", ++ " case 3: _round_result = trunc(_round_value); break;", ++ " default: { double _round_floor = floor(_round_value);", ++ " double _round_fraction = _round_value - _round_floor;", ++ " _round_result = _round_floor + (_round_fraction > 0.5 ||", ++ " (_round_fraction == 0.5 && fmod(_round_floor, 2.0) != 0.0)); break; }", ++ " }", ++ " _round_result = copysign(_round_result, _round_value);", ++ " }", ++ " fp_top() = _round_result; } /* frndint: guest x87 RC */", ++ ] + if m == "fldpi": + return [f"fp_push(3.14159265358979323846); /* fldpi */"] + if m == "fldl2e": +@@ -2915,8 +3309,15 @@ + if m == "fldln2": + return [f"fp_push(0.69314718055994530942); /* fldln2 */"] + if m == "ftst": +- return [f"g_fp_cmp = (fp_top() < 0.0) ? -1 : " +- f"(fp_top() > 0.0) ? 1 : 0; /* ftst */"] ++ return ["g_fp_cmp = RECOMP_FCMP(fp_top(), 0.0); " ++ "RECOMP_FP_COMPARE_STATUS(); /* ftst */"] ++ if m == "fxam": ++ return ["recomp_fp_examine(); /* fxam: class/sign/empty at current TOP */"] ++ if m in ("fnclex", "fclex"): ++ return ["g_fp_status_word &= (uint16_t)~0x80FFu; /* fnclex: preserve TOP/condition codes */"] ++ if m in ("fninit", "finit"): ++ return ["g_fp_control_word = 0x037Fu; g_fp_status_word = 0; " ++ "g_fp_cmp = 0; g_fp_top = 0; g_fp_empty_mask = 0xFFu; /* fninit */"] + if m == "fxch": + # fxch st(i) swaps st0 with st(i); the bare form is st(1). Was + # hardcoded to st1, so fxch st(2)/st(3)/st(4) (86/7/1 sites in Halo) +@@ -2930,7 +3331,12 @@ + i = (self._st_index(ops[-1].reg) + if (ops and ops[-1].type == "reg" and ops[-1].reg) else 1) + dst = self._st_expr(i) +- return [f"{{ double _t = fp_top(); fp_top() = {dst}; {dst} = _t; }}" ++ return [f"{{ double _t = fp_top(); fp_top() = {dst}; {dst} = _t; " ++ f"unsigned _other = (g_fp_top + {i}) & 7u; " ++ f"unsigned _different = ((g_fp_empty_mask >> g_fp_top) ^ " ++ f"(g_fp_empty_mask >> _other)) & 1u; " ++ f"g_fp_empty_mask ^= (uint8_t)((_different << g_fp_top) | " ++ f"(_different << _other)); }}" + f" /* fxch {insn.op_str} */"] + if m in ("fcom", "fcomp", "fcompp", "fucom", "fucomp", "fucompp"): + # Compare st0 against the operand, not always st1. `fcomp [mem]` +@@ -2950,7 +3356,7 @@ + npop = 2 if m.endswith("pp") else (1 if m.endswith("p") else 0) + pops = " fp_pop();" * npop + return [f"g_fp_cmp = RECOMP_FCMP(fp_top(), {rhs});" +- f"{pops} /* {m} {insn.op_str} */"] ++ f" RECOMP_FP_COMPARE_STATUS();{pops} /* {m} {insn.op_str} */"] + if m in ("fcompi", "fcomip", "fucomi", "fucompi", "fucomip", "fcomi"): + # These set EFLAGS directly (CF, ZF, PF) from FPU comparison + # fcompi/fucompi pop st(0) after comparing; fcomi/fucomi do not +@@ -2976,10 +3382,7 @@ + # `test ah, 0x44; jp` -- the standard isnan idiom -- reads exactly + # those two bits. Reporting equal for a NaN compare sends every + # float classification in a title down the wrong branch. +- status = ("(uint16_t)(((g_fp_top & 7u) << 11) |" +- " (g_fp_cmp == 2 ? 0x4500u :" +- " g_fp_cmp < 0 ? 0x0100u :" +- " g_fp_cmp > 0 ? 0x0000u : 0x4000u))") ++ status = "RECOMP_FP_STATUS()" + if insn.op_str.strip() in ("ax", "eax"): + # `fnstsw ax` writes the whole of AX, not just AH. + return [f"eax = (eax & 0xFFFF0000u) | (uint32_t){status};" +@@ -3010,12 +3413,140 @@ + f"{_fmt_operand_read(ops[0])};" + f" /* fldcw {insn.op_str} */"] + return [f"/* fldcw {insn.op_str} - no source operand */"] ++ if m in ("fnstenv", "fstenv"): ++ if not ops or ops[0].type != "mem": ++ return [f"recomp_unsupported_instruction(0x{insn.address:08X}u); /* {m} requires memory */"] ++ a = _fmt_mem(ops[0]) ++ # 32-bit FSTENV layout: 28 bytes. We model control/status/tag, ++ # TOP, and empty tags; instruction/data pointers are not tracked. ++ return [ ++ f"{{ uint32_t _env = (uint32_t)({a}); uint16_t _tag = 0;", ++ " for (unsigned _i = 0; _i < 8; ++_i) {", ++ " unsigned _physical = (g_fp_top + _i) & 7u;", ++ " if (g_fp_empty_mask & (1u << _physical)) _tag |= (uint16_t)(3u << (2u * _physical));", ++ " }", ++ " MEM16(_env + 0u) = g_fp_control_word; MEM16(_env + 2u) = 0;", ++ " MEM16(_env + 4u) = RECOMP_FP_STATUS(); MEM16(_env + 6u) = 0;", ++ " MEM16(_env + 8u) = _tag; MEM16(_env + 10u) = 0;", ++ " MEM32(_env + 12u) = 0; MEM32(_env + 16u) = 0;", ++ " MEM32(_env + 20u) = 0; MEM32(_env + 24u) = 0;", ++ " g_fp_control_word |= 0x003Fu; /* FSTENV masks x87 exceptions */", ++ f"}} /* {m}: 32-bit guest environment */", ++ ] ++ if m in ("fldenv",): ++ if not ops or ops[0].type != "mem": ++ return [f"recomp_unsupported_instruction(0x{insn.address:08X}u); /* fldenv requires memory */"] ++ a = _fmt_mem(ops[0]) ++ return [ ++ f"{{ uint32_t _env = (uint32_t)({a});", ++ " g_fp_control_word = MEM16(_env + 0u);", ++ " g_fp_status_word = MEM16(_env + 4u);", ++ " g_fp_top = (g_fp_status_word >> 11) & 7u;", ++ " { uint16_t _tag = MEM16(_env + 8u); g_fp_empty_mask = 0;", ++ " for (unsigned _i = 0; _i < 8; ++_i)", ++ " if (((_tag >> (2u * _i)) & 3u) == 3u)", ++ " g_fp_empty_mask |= (uint8_t)(1u << _i); }", ++ " g_fp_cmp = (g_fp_status_word & 0x0400u) ? 2 :", ++ " ((g_fp_status_word & 0x0100u) ? -1 :", ++ " ((g_fp_status_word & 0x4000u) ? 0 : 1));", ++ "} /* fldenv: 32-bit guest environment */", ++ ] + if m == "fldz": + return [f"fp_push(0.0); /* fldz */"] + if m == "fld1": + return [f"fp_push(1.0); /* fld1 */"] + +- return [f"/* FPU: {m} {insn.op_str} */"] ++ # Save/restore requires the original 108-byte x87 environment/register ++ # image, including exception/tag state not represented by this model. ++ # Preserve an explicit execution boundary instead of dropping the op. ++ self.unimplemented.setdefault(m, []).append(insn.address) ++ return [f"recomp_unsupported_instruction(0x{insn.address:08X}u); /* TODO: x87 {m} {insn.op_str} */"] ++ ++ ++ ++def merge_flag_states(states, allow_zf=True): ++ """Meet known predecessor states without discarding compatible snapshots. ++ ++ CMP/TEST already write runtime operand snapshots on every incoming path. ++ The operands' identities therefore need not agree, only operation and width ++ (SF conditions still select a width-specific cast). Different CMP/TEST ++ semantics meet at captured ZF and signed-less (SF xor OF). DEC/SUB only ++ supply captured result ZF here; no preserved carry is inferred. Unknown ++ states and other unlike setters that re-read operands are not merged. ++ """ ++ if not states or any(state is None for state in states): ++ return None ++ first = states[0] ++ if all(state == first for state in states[1:]): ++ return first ++ ++ if all(state[0] in ("sahf", "sahf_of") for state in states): ++ return ("sahf", []) # Every path supplies AH flags, but not necessarily OF. ++ ++ def snapshot_signature(state): ++ kind, ops = state ++ if kind not in ("cmp", "test") or len(ops) != 2: ++ return None ++ width = _operand_width(ops[0]) or _operand_width(ops[1]) or 4 ++ return kind, width ++ ++ signature = snapshot_signature(first) ++ if signature is not None and all(snapshot_signature(state) == signature ++ for state in states[1:]): ++ return first ++ # Capabilities intersect at a join. Existing same-kind operand snapshots ++ # remain valid above; mixed CMP/TEST can also supply signed comparisons. ++ if allow_zf and all(state[0] == "snapshot_sz" or snapshot_signature(state) is not None ++ for state in states): ++ return ("snapshot_sz", []) ++ def supplies_zf(state): ++ kind, ops = state ++ return (kind in ("snapshot_zf", "snapshot_sz") or ++ snapshot_signature(state) is not None or ++ (kind == "dec" and len(ops) == 1) or ++ (kind == "sub" and len(ops) == 2)) ++ if allow_zf and all(supplies_zf(state) for state in states): ++ return ("snapshot_zf", []) ++ return None ++ ++ ++def advance_flag_state(curr, state): ++ """Pure flag transfer shared by CFG analysis and instruction emission.""" ++ original_m = curr.mnemonic ++ m = original_m[5:] if original_m.startswith("lock ") else original_m ++ if (m in ("rol", "ror", "rcl", "rcr", "sar", "shl", "shr") and len(curr.operands) > 1 and ++ curr.operands[1].type == "imm" and (curr.operands[1].imm & 31) == 0): ++ return state ++ if (m in ("shld", "shrd") and len(curr.operands) > 2 and ++ curr.operands[2].type == "imm" and (curr.operands[2].imm & 31) == 0): ++ return state ++ if m == "sahf": ++ return ("sahf_of" if _overflow_snapshot(state) is not None else "sahf", []) ++ if m in FLAG_SETTERS: return (original_m, list(curr.operands)) ++ if m in _FLAGS_UNDEFINED: return None ++ if m in _EFLAGS_SETTERS: return (m, list(curr.operands)) ++ if m in _EFLAGS_PRESERVE: return state ++ if m in ("fcompi", "fcomip", "fucomi", "fucompi", "fucomip", "fcomi", "sahf"): ++ return (m, list(curr.operands)) ++ if m.startswith(("f", "cmov", "j", "set")): return state ++ if m.startswith("rep"): ++ rest = getattr(curr, "op_str", "") ++ if any(x in m or x in rest for x in ("cmpsb", "scasb")): ++ return (m, list(curr.operands)) ++ return state ++ return None ++ ++ ++def block_flag_state(bb, incoming=None, conservative_calls=True): ++ state = incoming ++ for insn in bb.instructions: ++ # New cross-block proofs cannot assume an arbitrary callee preserves ++ # flags. Local emission/legacy state remain a separately staged policy. ++ if conservative_calls and (insn.is_call or insn.mnemonic == "popfd"): ++ state = None ++ else: ++ state = advance_flag_state(insn, state) ++ return state + + + def lift_basic_block(lifter, bb, flag_state=None): +@@ -3087,7 +3618,7 @@ + + if (curr.mnemonic in ("sete", "setne", "setb", "setae", "setbe", + "seta", "setl", "setge", "setle", "setg", +- "sets", "setns") ++ "sets", "setns", "setp", "setnp", "seto", "setno") + and last_flag_setter and len(curr.operands) >= 1): + cond = _make_setcc_value( + curr.mnemonic, last_flag_setter, last_flag_ops) +@@ -3111,6 +3642,20 @@ + f"if ({cond}) " + + _fmt_operand_write(curr.operands[0], src) + + f" /* {curr.mnemonic} */") ++ i += 1 ++ continue ++ ++ if (curr.mnemonic == "fcmovne" and last_flag_setter and ++ curr.operands and curr.operands[-1].type == "reg" and ++ curr.operands[-1].reg): ++ # FCMOVcc uses EFLAGS and transfers ST(i) to ST(0). Unlike the ++ # similarly named integer CMOV, it belongs to x87 instruction ++ # dispatch, so handle it here while the preceding flag producer is ++ # still available to the block-level condition tracker. ++ cond = _make_cmovcc_cond("cmovne", last_flag_setter, last_flag_ops) ++ if cond: ++ source = lifter._st_expr(lifter._st_index(curr.operands[-1].reg)) ++ stmts.append(f"if ({cond}) fp_top() = {source}; /* fcmovne */") + i += 1 + continue + +@@ -3131,56 +3676,17 @@ + results = lifter._lift_neg( + curr, curr.operands, preserve_carry=preserve) + else: ++ if curr.mnemonic == "sahf": ++ overflow = _overflow_snapshot((last_flag_setter, last_flag_ops) ++ if last_flag_setter else None) ++ if overflow is not None: ++ stmts.append(f"_sahf_of = {overflow}; /* OF survives SAHF */") + results = lifter.lift_instruction(insns[i]) + stmts.extend(results) + +- # Track flag-setting instructions +- if curr.mnemonic in FLAG_SETTERS: +- last_flag_setter = curr.mnemonic +- last_flag_ops = list(curr.operands) +- elif curr.mnemonic in _FLAGS_UNDEFINED: +- # Flags are undefined after these - clear tracking +- last_flag_setter = None +- last_flag_ops = [] +- elif curr.mnemonic in _EFLAGS_SETTERS: +- # Additional flag-setting instructions +- last_flag_setter = curr.mnemonic +- last_flag_ops = list(curr.operands) +- elif curr.mnemonic in _EFLAGS_PRESERVE: +- pass # These don't affect EFLAGS +- elif curr.mnemonic in ("fcompi", "fcomip", "fucomi", "fucompi", +- "fucomip", "fcomi"): +- # FPU compare-to-EFLAGS: sets CF, ZF, PF directly +- last_flag_setter = curr.mnemonic +- last_flag_ops = list(curr.operands) +- elif curr.mnemonic == "sahf": +- # sahf loads AH into flags - typically after fnstsw ax +- # in the fcomp/fnstsw/sahf pattern for FPU comparisons +- last_flag_setter = "sahf" +- last_flag_ops = list(curr.operands) +- elif curr.mnemonic.startswith("f") or curr.mnemonic.startswith("cmov"): +- pass # FPU and already-handled CMOVcc +- elif curr.mnemonic.startswith("j"): +- pass # Jumps don't set flags +- elif curr.mnemonic.startswith("set"): +- pass # SETcc doesn't set flags +- elif curr.mnemonic.startswith("rep"): +- # rep movsb/movsd = data copy, preserves flags +- # repe cmpsb/repne scasb = comparison, sets flags +- rest = curr.op_str.strip() if hasattr(curr, 'op_str') else "" +- raw_m = curr.mnemonic +- if "cmpsb" in raw_m or "scasb" in raw_m: +- last_flag_setter = raw_m +- last_flag_ops = list(curr.operands) +- elif "cmpsb" in rest or "scasb" in rest: +- last_flag_setter = raw_m +- last_flag_ops = list(curr.operands) +- else: +- pass # rep movs/stos = data movement, flags preserved +- else: +- # Unknown instruction - conservatively clear flag state +- last_flag_setter = None +- last_flag_ops = [] ++ state = advance_flag_state(curr, (last_flag_setter, last_flag_ops) ++ if last_flag_setter else None) ++ last_flag_setter, last_flag_ops = state if state else (None, []) + + i += 1 + +--- a/tools/recomp/translator.py ++++ b/tools/recomp/translator.py +@@ -22,7 +22,7 @@ + from .config import va_to_file_offset, is_code_address + from . import config as _config + from .disasm import Disassembler +-from .lifter import (Lifter, lift_basic_block, detect_seh_helpers, ++from .lifter import (Lifter, lift_basic_block, merge_flag_states, block_flag_state, detect_seh_helpers, + detect_setjmp_helpers) + + +@@ -138,8 +138,10 @@ + FP_STACK_MACROS = [ + " #define fp_push(v) do { double _fp_value = (v); \\", + " g_fp_top = (g_fp_top + 7u) & 7u; \\", ++ " g_fp_empty_mask &= (uint8_t)~(1u << g_fp_top); \\", + " g_fp_stack[g_fp_top] = _fp_value; } while (0)", +- " #define fp_pop() (g_fp_top = (g_fp_top + 1u) & 7u)", ++ " #define fp_pop() (g_fp_empty_mask |= (uint8_t)(1u << g_fp_top), \\", ++ " g_fp_top = (g_fp_top + 1u) & 7u)", + " #define fp_top() g_fp_stack[g_fp_top]", + " #define fp_st(i) g_fp_stack[(g_fp_top + (i)) & 7u]", + " #define fp_st1() fp_st(1)", +@@ -506,6 +508,105 @@ + backward.reverse() + return backward + forward + ++ def _register_indirect_switch_targets(self, instructions, index, ++ function_start, function_end): ++ """Recover a guarded `mov reg,[table+index*4]; jmp reg` switch. ++ ++ Only accepts the common bounded form with an unsigned `ja` guard and ++ a preceding `sub reg, imm` that reduces the switch index. The table ++ read is bounded to imm+1 entries, and every value must be an ++ executable instruction address inside the current function. This ++ avoids treating adjacent rodata or arbitrary indirect jumps as local ++ control flow. ++ """ ++ if index <= 0 or not self.xbe_data: ++ return [] ++ jump = instructions[index] ++ load = instructions[index - 1] ++ if (jump.mnemonic != "jmp" or jump.jump_target is not None ++ or len(jump.operands) != 1 or jump.operands[0].type != "reg"): ++ return [] ++ if (load.mnemonic != "mov" or len(load.operands) != 2 ++ or load.operands[0].type != "reg" ++ or load.operands[0].reg != jump.operands[0].reg ++ or load.operands[1].type != "mem"): ++ return [] ++ mem = load.operands[1] ++ if not mem.mem_index or mem.mem_scale != 4 or mem.mem_base: ++ return [] ++ table_va = mem.mem_disp ++ if not table_va: ++ return [] ++ ++ # Find the nearest unsigned upper-bound branch and its index ++ # subtraction. Keep the scan short so an unrelated earlier compare ++ # cannot authorize an unbounded table. ++ count = None ++ lower = max(0, index - 9) ++ for branch_index in range(index - 2, lower - 1, -1): ++ branch = instructions[branch_index] ++ if branch.mnemonic not in ("ja", "jnbe"): ++ continue ++ for sub_index in range(branch_index - 1, max(lower - 1, branch_index - 4), -1): ++ sub = instructions[sub_index] ++ if (sub.mnemonic == "sub" and len(sub.operands) == 2 ++ and sub.operands[0].type == "reg" ++ and sub.operands[0].reg == mem.mem_index ++ and sub.operands[1].type == "imm"): ++ upper_bound = int(sub.operands[1].imm) ++ if 0 <= upper_bound < 256: ++ count = upper_bound + 1 ++ break ++ if count is not None: ++ break ++ if count is None: ++ # Clang may spill a masked index, compare its low byte with SUB, ++ # then reload the original index before loading the table. The ++ # byte comparison only bounds the full register when its high ++ # bits were explicitly cleared; require that proof and the exact ++ # same stack slot, rather than accepting an arbitrary register ++ # reload following an unrelated guard. ++ if index < 6: ++ return [] ++ mask, spill, compare, branch, reload = instructions[index - 6:index - 1] ++ index_reg = mem.mem_index ++ low_alias = {"eax": "al", "ebx": "bl", "ecx": "cl", "edx": "dl"} ++ ++ def same_slot(a, b): ++ return (a.type == b.type == "mem" and a.mem_base in ("ebp", "esp") ++ and not a.mem_index and not b.mem_index ++ and a.mem_base == b.mem_base and a.mem_disp == b.mem_disp ++ and a.mem_size == b.mem_size == 4) ++ ++ if not (mask.mnemonic == "and" and len(mask.operands) == 2 ++ and mask.operands[0].type == "reg" and mask.operands[0].reg == index_reg ++ and mask.operands[1].type == "imm" and 0 <= mask.operands[1].imm <= 255 ++ and spill.mnemonic == "mov" and len(spill.operands) == 2 ++ and spill.operands[1].type == "reg" and spill.operands[1].reg == index_reg ++ and compare.mnemonic in ("cmp", "sub") and len(compare.operands) == 2 ++ and compare.operands[0].type == "reg" ++ and compare.operands[0].reg in (index_reg, low_alias.get(index_reg)) ++ and compare.operands[1].type == "imm" and 0 <= compare.operands[1].imm < 256 ++ and branch.mnemonic in ("ja", "jnbe") ++ and reload.mnemonic == "mov" and len(reload.operands) == 2 ++ and reload.operands[0].type == "reg" and reload.operands[0].reg == index_reg ++ and same_slot(spill.operands[0], reload.operands[1])): ++ return [] ++ count = int(compare.operands[1].imm) + 1 ++ ++ offset = va_to_file_offset(table_va) ++ if offset is None or offset + count * 4 > len(self.xbe_data): ++ return [] ++ targets = [] ++ for entry in range(count): ++ target = struct.unpack_from("= 2 else [] ++ + def _read_func_bytes(self, start_va, end_va): + """Read raw bytes for a function from the XBE.""" + offset = va_to_file_offset(start_va) +@@ -537,7 +638,7 @@ + last_setter = None + for insn in instructions: + m = insn.mnemonic +- if m in ("adc", "sbb", "stc", "clc", "cmc"): ++ if m in ("adc", "sbb", "stc", "clc", "cmc", "rcl", "rcr"): + return True + cc = None + if m.startswith("j") and len(m) > 1: +@@ -620,9 +721,26 @@ + if not instructions: + return None + ++ # Recover bounded computed-goto tables whose final instruction is ++ # `jmp reg` rather than `jmp [table + index*4]`. Musl's printf_core ++ # and pop_arg use this form: a guarded index loads a target from a ++ # read-only table, then jumps through the register. These targets are ++ # interior labels and must stay in this C activation (locals/flags and ++ # guest stack frame are live); dispatching them as functions corrupts ++ # the formatter's frame. ++ local_indirect_targets = {} ++ for index, insn in enumerate(instructions): ++ targets = self._register_indirect_switch_targets( ++ instructions, index, start, end) ++ if targets: ++ local_indirect_targets[insn.address] = targets ++ ++ self.lifter.local_indirect_targets = local_indirect_targets ++ + # Collect switch table targets as extra block leaders + switch_leaders = set() +- for insn in instructions: ++ for index, insn in enumerate(instructions): ++ switch_leaders.update(local_indirect_targets.get(insn.address, ())) + if insn.mnemonic == "jmp" and not insn.jump_target and insn.operands: + targets = self.lifter._analyze_switch_table(insn.operands) + for t in targets: +@@ -774,35 +892,15 @@ + # and must NOT be declared locally, otherwise the local shadows + # the global and cross-function register passing breaks. + # Volatile registers (eax, ecx, edx, esp) are also global via macros. +- reg_decls = [] ++ # Even a standard prologue first PUSHes the incoming architectural EBP. ++ # Initializing only frameless functions corrupts saved guest frame bytes. + if "ebp" in used_regs: +- reg_decls.append("ebp") +- if reg_decls: +- lines.append(f" uint32_t {', '.join(reg_decls)};") +- +- # A function with no `push ebp; mov ebp, esp` prologue that still reads +- # ebp is addressing its *caller's* frame. MSVC emits these for shared +- # tails and helpers; Halo's CRT float formatting (sub_001DEC07) opens +- # with `cmp byte ptr [edx+0xe], 5` and goes straight to [ebp-0xa4]. +- # +- # ebp is a per-function local, so without this it starts as garbage and +- # every [ebp-N] store lands wherever that points. In Halo that was the +- # fake TIB at Xbox VA 0: `mov [ebp-0xa2], bx` destroyed fs:[4], and the +- # next TLS lookup faulted, thousands of calls away from the cause. +- # +- # Inherit it instead. Frame-establishing functions publish g_ebp when +- # they execute `mov ebp, esp` (see the lifter), so the value is the +- # nearest enclosing frame -- which is exactly what the hardware ebp +- # would still hold. Deliberately not the same as making ebp global: +- # that also changes save/restore, and a callee that fails to restore +- # then corrupts its caller (tried; esp underflowed inside XapiStartup). +- if "ebp" in used_regs and not self._func_has_prologue(instructions): +- lines.append(" ebp = g_ebp; /* frameless: caller's frame */") ++ lines.append(" uint32_t ebp = g_ebp;") + + # Add _flags variable if function has conditional instructions + has_conditionals = any( + insn.is_cond_jump or insn.mnemonic.startswith("set") +- or insn.mnemonic.startswith("cmov") ++ or insn.mnemonic.startswith("cmov") or insn.mnemonic == "fcmovne" + for insn in instructions) + if has_conditionals: + lines.append(f" int _flags = 0; /* fallback flag var */") +@@ -816,12 +914,17 @@ + # sub_000EEA10 in Wreckless is exactly `bsf eax, ecx; ret`. + # cmpxchg belongs here too: it snapshots the compare it performed, + # because eax may be replaced before the branch reads the result. +- if any(insn.mnemonic in ("cmp", "test", "bsf", "bsr", "cmpxchg", +- "lock cmpxchg") ++ if any(insn.mnemonic in ("cmp", "test", "sbb", "bsf", "bsr", "cmpxchg", ++ "lock cmpxchg", "lock inc", "lock dec", ++ "lock or", "lock and", "tzcnt") + for insn in instructions): + lines.append(" uint32_t _fa = 0, _fb = 0;") + lines.append(" int32_t _fas = 0, _fbs = 0;") + lines.append(" (void)_fa; (void)_fb; (void)_fas; (void)_fbs;") ++ if any(insn.mnemonic == "sbb" for insn in instructions): ++ lines.append(" uint32_t _sbb_result = 0;") ++ lines.append(" int _sbb_sf = 0, _sbb_of = 0;") ++ lines.append(" (void)_sbb_result; (void)_sbb_sf; (void)_sbb_of;") + # Flag snapshot: a cmp/test that is not fused with its jcc records + # its operands here, zero- and sign-extended to the compare's own + # width, so the branch tests what the compare saw. +@@ -851,6 +954,9 @@ + # setter a branch reads is the lifter's tracking rule, mirrored here so + # only the functions that consume CF declare it: computing it beside + # every add in the image would be a line per add in 48,000 functions. ++ if any(insn.mnemonic == "sahf" for insn in instructions): ++ lines.append(" uint32_t _sahf_flags = 0; int _sahf_of = 0;") ++ lines.append(" (void)_sahf_flags; (void)_sahf_of;") + has_carry = self._function_needs_cf(instructions) + if has_carry: + lines.append(f" int _cf = 0; /* carry flag */") +@@ -859,7 +965,14 @@ + # corrupts multi-word arithmetic (add/adc pairs) and the shr/adc + # idiom MSVC emits for odd trailing elements. + self.lifter.needs_cf = has_carry +- self.lifter.publishes_ebp = self._func_owns_a_frame(instructions) ++ self.lifter.needs_shift_snapshot = any(insn.mnemonic in ("shl", "shr", "shld", "shrd") for insn in instructions) ++ if self.lifter.needs_shift_snapshot: ++ lines.append(" uint32_t _shift_result = 0; (void)_shift_result;") ++ lines.append(" int _shift_of = 0; (void)_shift_of;") ++ self.lifter.needs_rotate_snapshot = any(insn.mnemonic in ("rol", "ror", "rcl", "rcr") for insn in instructions) ++ if self.lifter.needs_rotate_snapshot: ++ lines.append(" uint32_t _rotate_result = 0; (void)_rotate_result;") ++ self.lifter.publishes_ebp = "ebp" in used_regs + + # SSE and MMX are architectural state, declared globally by the + # runtime exactly like the GPRs and the x87 stack. Declaring either +@@ -906,6 +1019,8 @@ + switch_targets = self.lifter._analyze_switch_table(insn.operands) + for t in switch_targets: + label_addrs.add(t) ++ for t in local_indirect_targets.get(insn.address, ()): ++ label_addrs.add(t) + + # Which blocks can reach each block. Flag state has to follow control + # flow, not address order: an optimising compiler routinely lets a jcc +@@ -914,19 +1029,68 @@ + # whatever instruction happens to sit above it -- silently, and with a + # perfectly plausible-looking condition. + preds = {bb.start: set() for bb in blocks} ++ legacy_preds = {bb.start: set() for bb in blocks} + for i, bb in enumerate(blocks): + last = bb.instructions[-1] if bb.instructions else None + if last is None: + continue + if last.jump_target in preds: + preds[last.jump_target].add(bb.start) ++ legacy_preds[last.jump_target].add(bb.start) ++ elif last.mnemonic == "jmp" and last.operands: ++ destinations = local_indirect_targets.get(last.address) ++ if destinations is None: ++ destinations = self.lifter._analyze_switch_table(last.operands) ++ for destination in destinations: ++ if destination in preds: ++ preds[destination].add(bb.start) + # A conditional jump also falls through; ret and an unconditional + # jmp do not. + leaves = last.is_ret or last.mnemonic in ("jmp", "int3", "ud2", "hlt") + if not leaves and i + 1 < len(blocks): + preds[blocks[i + 1].start].add(bb.start) +- +- out_state = {} ++ legacy_preds[blocks[i + 1].start].add(bb.start) ++ ++ # Establish terminal setters independently of address order. Propagate ++ # preserving blocks to a fixed point; unknown entry/call paths stay unknown. ++ out_state = {bb.start: block_flag_state(bb) for bb in blocks} ++ incoming_state = {} ++ for _ in range(len(blocks) + 1): ++ changed = False ++ for bb in blocks: ++ sources = preds[bb.start] ++ incoming = (merge_flag_states([out_state[p] for p in sorted(sources)]) ++ if bb.start != start and sources else None) ++ incoming_state[bb.start] = incoming ++ terminal = block_flag_state(bb, incoming) ++ if terminal != out_state[bb.start]: ++ out_state[bb.start] = terminal ++ changed = True ++ if not changed: break ++ # Preserve the previous emitter's behavior where the new solver has no ++ # proof. This is deliberately NOT a new interprocedural flags model. ++ # Reconstruct the old address-order meet without mixed-ZF acceptance; ++ # otherwise a legacy call could accidentally justify a new ZF join. ++ legacy_out = {} ++ for bb in blocks: ++ sources = legacy_preds[bb.start] ++ legacy_in = (merge_flag_states([legacy_out[p] for p in sorted(sources)], ++ allow_zf=False) ++ if bb.start != start and sources and ++ all(p in legacy_out for p in sources) else None) ++ legacy_out[bb.start] = block_flag_state(bb, legacy_in, ++ conservative_calls=False) ++ if incoming_state[bb.start] is None: ++ incoming_state[bb.start] = legacy_in ++ self.lifter.needs_zf_snapshot = any( ++ state and state[0] in ("snapshot_zf", "snapshot_sz") for state in incoming_state.values()) ++ self.lifter.needs_signed_snapshot = any( ++ state and state[0] == "snapshot_sz" for state in incoming_state.values()) ++ if self.lifter.needs_zf_snapshot: ++ lines.append(" int _zf = 0; /* captured CMP/TEST/DEC/SUB zero flag */") ++ if self.lifter.needs_signed_snapshot: ++ lines.append(" int _slt = 0; /* captured CMP/TEST SF xor OF */") ++ + for bb in blocks: + # Emit label if this block is a branch target + if bb.start in label_addrs or bb.start == start: +@@ -937,25 +1101,8 @@ + # compile. The null statement costs nothing and is always valid. + lines.append(f"loc_{bb.start:08X}: ;") + +- # Inherit the flag state only when every predecessor agrees on it. +- # Blocks are walked in address order, so a back edge's predecessor +- # may not be computed yet -- treat that as unknown rather than +- # guessing, which costs a fallback condition and never a wrong one. +- sources = preds[bb.start] +- if bb.start == start or not sources: +- incoming = None +- elif all(p in out_state for p in sources): +- states = [out_state[p] for p in sources] +- incoming = states[0] +- for other in states[1:]: +- if other != incoming: +- incoming = None +- break +- else: +- incoming = None +- +- stmts, out_state[bb.start] = lift_basic_block( +- self.lifter, bb, flag_state=incoming) ++ stmts, _ = lift_basic_block( ++ self.lifter, bb, flag_state=incoming_state[bb.start]) + for stmt in stmts: + lines.append(f" {stmt}") + +@@ -964,7 +1111,7 @@ + # Continue into the next function when control runs off the bottom. + if fallthrough_target is not None: + ft_name = self.lifter._call_target_name(fallthrough_target) +- lines.append(f" g_seh_ebp = ebp; {ft_name}(); return;" ++ lines.append(f" g_ebp = g_seh_ebp = ebp; {ft_name}(); return;" + f" /* fallthrough 0x{fallthrough_target:08X} */") + lines.append(f"") + +@@ -1378,7 +1525,7 @@ + "/**", + f" * {_config.banner_name(getattr(self, 'title', None))}" + f" - Recompiled Function Declarations", +- f" * {stats['translated']} functions, auto-generated by tools/recomp", ++ " * Auto-generated by tools/recomp; native overrides share these declarations.", + " */", + "", + "#ifndef RECOMP_FUNCS_H", +@@ -1387,22 +1534,14 @@ + '#include "recomp_types.h"', + "", + ] +- for addr, name, _ in translations: ++ # Declaration order must not change when an SDK function becomes native: ++ # otherwise every game translation unit unnecessarily rebuilds. ++ declarations = {addr: name for addr, name, _ in translations} ++ declarations.update(manual_decls) ++ declarations.update(unresolved) ++ for addr, name in sorted(declarations.items()): + decl = self._make_declaration(addr, name) + header_lines.append(f"{decl};") +- +- if manual_decls: +- header_lines.append("") +- header_lines.append("/* Hand-written overrides (defined by the project) */") +- for addr in sorted(manual_decls): +- header_lines.append( +- f"void {manual_decls[addr]}(void); /* 0x{addr:08X} */") +- +- if unresolved: +- header_lines.append("") +- header_lines.append("/* Unresolved call targets (stubbed) */") +- for addr in sorted(unresolved): +- header_lines.append(f"void {unresolved[addr]}(void);") + + header_lines.extend(["", "#endif /* RECOMP_FUNCS_H */", ""]) + +@@ -1461,8 +1600,12 @@ + + # Split translations into chunks and write .c files + generated_files = [header_path] +- chunks = [translations[i:i+chunk_size] +- for i in range(0, len(translations), chunk_size)] ++ # Keep each original function's chunk stable as native overrides are ++ # introduced. An omitted body must not shift all later functions. ++ chunk_index = {addr: i // chunk_size for i, (addr, _) in enumerate(func_list)} ++ chunks = [[] for _ in range((len(func_list) + chunk_size - 1) // chunk_size)] ++ for translation in translations: ++ chunks[chunk_index[translation[0]]].append(translation) + + # Remove chunk files a previous, larger run left behind. Projects glob + # gen/*.c into their build, so a stale chunk keeps compiling: it still +@@ -1485,8 +1628,8 @@ + "/**", + f" * {_config.banner_name(getattr(self, 'title', None))}" + f" - Recompiled code chunk {ci}", +- f" * Functions: {len(chunk)} " +- f"(0x{chunk[0][0]:08X} - 0x{chunk[-1][0]:08X})", ++ f" * Functions: {len(chunk)} " + ++ (f"(0x{chunk[0][0]:08X} - 0x{chunk[-1][0]:08X})" if chunk else "(native overrides only)"), + " */", + "", + "#define RECOMP_GENERATED_CODE", +@@ -1541,7 +1684,7 @@ + popped = self.translator._stub_ret_bytes(addr) + note = (f"ret {popped}" if popped else "not detected") + stub_lines.append( +- f"void {unresolved[addr]}(void) {{ g_esp += {4 + popped}; " ++ f"void {unresolved[addr]}(void) {{ recomp_icall_fail_log(0x{addr:08X}u); " + f"/* 0x{addr:08X}: {note} */ }}" + ) + stub_lines.append("") +--- a/templates/runtime/recomp_types.h ++++ b/templates/runtime/recomp_types.h +@@ -60,6 +60,8 @@ + * CMakeLists) -- MSVC's C4013 was emitted and discarded. Same failure as the + * missing stdlib.h in kernel_bridge.c, in a hotter path. */ + #include ++/* Development builds stop when reachable code is not yet lifted. */ ++void recomp_unsupported_instruction(uint32_t xbox_va); + + /* MSVC's __forceinline -> gcc/clang equivalent on POSIX. */ + #if !defined(_MSC_VER) && !defined(__forceinline) +@@ -160,6 +162,11 @@ + extern RECOMP_TLS double g_fp_stack[8]; + extern RECOMP_TLS int g_fp_top; + ++/* MXCSR is modeled per translated thread, just like the x87 control/status ++ * words. The macOS AOT backend cannot rely on native host FP state matching ++ * guest SSE state, so translated MXCSR instructions operate on this value. */ ++extern RECOMP_TLS uint32_t g_mxcsr; ++ + /** + * SEH frame pointer bridge. + * +@@ -213,9 +220,13 @@ + #include + #define RECOMP_ATOMIC_ADD32(p, v) ((uint32_t)_InterlockedExchangeAdd((volatile long *)(p), (long)(v))) + #define RECOMP_ATOMIC_CAS32(p, cmp, val) ((uint32_t)_InterlockedCompareExchange((volatile long *)(p), (long)(val), (long)(cmp))) ++#define RECOMP_ATOMIC_OR32(p, v) ((uint32_t)_InterlockedOr((volatile long *)(p), (long)(v))) ++#define RECOMP_ATOMIC_AND32(p, v) ((uint32_t)_InterlockedAnd((volatile long *)(p), (long)(v))) + #else + #define RECOMP_ATOMIC_ADD32(p, v) ((uint32_t)__sync_fetch_and_add((volatile uint32_t *)(p), (uint32_t)(v))) + #define RECOMP_ATOMIC_CAS32(p, cmp, val) ((uint32_t)__sync_val_compare_and_swap((volatile uint32_t *)(p), (uint32_t)(cmp), (uint32_t)(val))) ++#define RECOMP_ATOMIC_OR32(p, v) ((uint32_t)__sync_fetch_and_or((volatile uint32_t *)(p), (uint32_t)(v))) ++#define RECOMP_ATOMIC_AND32(p, v) ((uint32_t)__sync_fetch_and_and((volatile uint32_t *)(p), (uint32_t)(v))) + #endif + + #include +@@ -244,6 +255,11 @@ + word has to survive a call. (g_fp_stack/g_fp_top are declared above.) */ + extern RECOMP_TLS uint16_t g_fp_control_word; + extern RECOMP_TLS int g_fp_cmp; ++/* Separate x87 status from the comparison snapshot used by EFLAGS branches. ++ * TOP comes from g_fp_top. Empty bits track physical registers, with 1=empty. ++ * This does not add 80-bit arithmetic or arithmetic exception generation. */ ++extern RECOMP_TLS uint16_t g_fp_status_word; ++extern RECOMP_TLS uint8_t g_fp_empty_mask; + + /* Result of an x87 compare, in the shape the status word wants: + * -1 less, 0 equal, 1 greater, 2 unordered (either operand is NaN). +@@ -251,6 +267,58 @@ + * followed by `test ah, 0x44; jp` is how this era's CRT asks "is this a NaN", + * and collapsing it to "equal" answers no every time. */ + #define RECOMP_FCMP(a, b) (((a) != (a) || (b) != (b)) ? 2 : (a) < (b) ? -1 : (a) > (b) ? 1 : 0) ++#define RECOMP_FP_COMPARE_STATUS() do { \ ++ g_fp_status_word = (uint16_t)((g_fp_status_word & ~0x4700u) | \ ++ (g_fp_cmp == 2 ? 0x4500u : g_fp_cmp < 0 ? 0x0100u : \ ++ g_fp_cmp > 0 ? 0u : 0x4000u)); \ ++} while (0) ++#define RECOMP_FP_STATUS() ((uint16_t)((g_fp_status_word & ~0x3800u) | \ ++ ((g_fp_top & 7u) << 11))) ++/* FPREM/FPREM1 in the existing binary64 execution model, not x87 extended ++ * precision. Each large-exponent step uses Intel's permitted N=32 reduction. ++ * Invalid/NaN inputs need unmodeled x87 exception semantics: fail explicitly. */ ++static inline double recomp_fp_remainder(double x, double y, int nearest, uint32_t address) ++{ ++ if (isnan(x) || isnan(y) || isinf(x) || y == 0.0) { ++ recomp_unsupported_instruction(address); ++ return NAN; /* diagnostic is non-returning in the runtime */ ++ } ++ if (x == 0.0 || isinf(y)) { ++ g_fp_status_word &= (uint16_t)~0x4700u; ++ return x; ++ } ++ int difference = ilogb(fabs(x)) - ilogb(fabs(y)); ++ if (difference >= 64) { ++ double scaled = scalbn(y, difference - 32); ++ /* C0/C1/C3 are undefined on an incomplete step; retain their bits. */ ++ g_fp_status_word |= 0x0400u; ++ double result = fmod(x, scaled); ++ return result == 0.0 ? copysign(0.0, x) : result; ++ } ++ int quotient; ++ double near = remquo(fabs(x), fabs(y), "ient); ++ /* The condition bits encode the quotient magnitude, not its sign. ++ * remquo chooses nearest-even; a negative remainder rounded up and must ++ * decrement the magnitude for FPREM's truncation toward zero. */ ++ unsigned low = (unsigned)quotient; ++ if (!nearest && near < 0.0) --low; ++ uint16_t condition = (uint16_t)(((low & 4u) << 6) | ++ ((low & 2u) << 13) | ((low & 1u) << 9)); ++ g_fp_status_word = (g_fp_status_word & (uint16_t)~0x4700u) | condition; ++ double result = nearest ? remainder(x, y) : fmod(x, y); ++ return result == 0.0 ? copysign(0.0, x) : result; ++} ++ ++static inline void recomp_fp_examine(void) ++{ ++ double value = g_fp_stack[g_fp_top & 7u]; ++ unsigned category = (g_fp_empty_mask & (1u << (g_fp_top & 7u))) ? 0x4100u : ++ isnan(value) ? 0x0100u : isinf(value) ? 0x0500u : value == 0 ? 0x4000u : 0x0400u; ++ /* Every nonzero finite binary64 value is normal in x87's extended range, ++ * including a binary64 subnormal loaded by FLD. C1 reports sign even empty. */ ++ g_fp_status_word = (uint16_t)((g_fp_status_word & ~0x4700u) | category | ++ (signbit(value) ? 0x0200u : 0u)); ++} + + /* ================================================================ + * ICALL trace ring buffer (for debugging indirect calls) +@@ -347,6 +415,13 @@ + /** Float/double memory access. */ + #define MEMF(addr) (*(volatile float *)XBOX_PTR(addr)) + #define MEMD(addr) (*(volatile double *)XBOX_PTR(addr)) ++ ++/* The AOT guest models x87 registers as doubles, but musl's strtod path ++ * stores/reloads an 80-bit x87 temporary. These byte-oriented helpers decode ++ * the guest binary80 representation without relying on host long double ++ * (which is binary64 on Apple Silicon). */ ++int mac_guest_x87_80_load(const void *bytes10, unsigned rounding_mode, double *out); ++int mac_guest_x87_80_store(void *bytes10, double value); + + /* ================================================================ + * SSE / XMM register state +@@ -511,6 +586,46 @@ + RecompXmm r; + r.u[0] = a.u[2]; r.u[1] = b.u[2]; r.u[2] = a.u[3]; r.u[3] = b.u[3]; + return r; ++} ++ ++/** subpd: subtract the two independent IEEE-754 binary64 lanes. */ ++static inline RecompXmm XMM_SUB_DOUBLE(RecompXmm a, RecompXmm b) { ++ RecompXmm r; ++ r.d[0] = a.d[0] - b.d[0]; ++ r.d[1] = a.d[1] - b.d[1]; ++ return r; ++} ++ ++/** pcmpeqb: all-one bytes for equality, all-zero bytes otherwise. */ ++static inline RecompXmm XMM_PCMPEQB(RecompXmm a, RecompXmm b) { ++ RecompXmm r; ++ for (unsigned i = 0; i < 4; ++i) { ++ r.u[i] = 0; ++ for (unsigned lane = 0; lane < 4; ++lane) { ++ uint32_t shift = lane * 8u; ++ uint32_t av = (a.u[i] >> shift) & 0xFFu; ++ uint32_t bv = (b.u[i] >> shift) & 0xFFu; ++ r.u[i] |= (av == bv ? 0xFFu : 0u) << shift; ++ } ++ } ++ return r; ++} ++ ++/** pcmpgtd: each signed 32-bit lane becomes all ones iff a > b. */ ++static inline RecompXmm XMM_PCMPGTD(RecompXmm a, RecompXmm b) { ++ RecompXmm r; ++ for (unsigned i = 0; i < 4; ++i) ++ r.d[i] = a.d[i] > b.d[i] ? -1 : 0; ++ return r; ++} ++ ++/** pmovmskb: collect the sign bit of each byte into a 16-bit mask. */ ++static inline uint32_t XMM_PMOVMSKB(RecompXmm a) { ++ uint32_t mask = 0; ++ for (unsigned i = 0; i < 4; ++i) ++ for (unsigned lane = 0; lane < 4; ++lane) ++ mask |= ((a.u[i] >> (lane * 8u + 7u)) & 1u) << (i * 4u + lane); ++ return mask; + } + + /* ================================================================ +@@ -573,6 +688,95 @@ + /* ================================================================ + * Rotation / shift helpers + * ================================================================ */ ++ ++/* Operand-width rotation; counts use the x86 five-bit mask. OF is defined ++ * only when that masked count is 1. Zero count leaves both output flags alone. */ ++static inline uint32_t RECOMP_ROTATE(uint32_t value, unsigned count, unsigned bits, ++ int right, int *cf, int *of) ++{ ++ uint32_t mask = bits == 32 ? UINT32_MAX : (1u << bits) - 1u; ++ value &= mask; ++ unsigned masked = count & 31u, shift = masked & (bits - 1u); ++ uint32_t result = value; ++ if (shift) result = right ? (value >> shift) | (value << (bits - shift)) : ++ (value << shift) | (value >> (bits - shift)); ++ result &= mask; ++ if (masked) { ++ unsigned carry = right ? result >> (bits - 1u) : result & 1u; ++ if (cf) *cf = (int)carry; ++ if (of && masked == 1u) ++ *of = (int)(((result >> (bits - 1u)) ^ ++ (right ? result >> (bits - 2u) : carry)) & 1u); ++ } ++ return result; ++} ++/* Rotate the operand and CF as a 9/17/33-bit ring. The count is masked to 5 ++ * bits before reducing modulo the ring width. OF is defined only at count 1. */ ++static inline uint32_t RECOMP_CARRY_ROTATE(uint32_t value, unsigned count, unsigned bits, ++ int right, int *cf, int *of) ++{ ++ uint32_t mask = bits == 32 ? UINT32_MAX : (1u << bits) - 1u; ++ unsigned masked = count & 31u, shift = masked % (bits + 1u); ++ uint64_t ring_mask = (UINT64_C(1) << (bits + 1u)) - 1u; ++ uint64_t ring = (value & mask) | ((uint64_t)(*cf & 1) << bits); ++ if (shift) ring = (right ? (ring >> shift) | (ring << (bits + 1u - shift)) : ++ (ring << shift) | (ring >> (bits + 1u - shift))) & ring_mask; ++ uint32_t result = (uint32_t)ring & mask; ++ *cf = (int)((ring >> bits) & 1u); ++ if (of && masked == 1u) ++ *of = (int)(((result >> (bits - 1u)) ^ ++ (right ? result >> (bits - 2u) : (unsigned)*cf)) & 1u); ++ return result; ++} ++ ++/* Logical shifts truncate to operand width before shifting. CF is undefined ++ * for nonzero masked counts >= width; choose 0 there. OF is defined at count 1. */ ++static inline uint32_t RECOMP_SHIFT(uint32_t value, unsigned count, unsigned bits, ++ int right, int *cf, int *of) ++{ ++ uint32_t mask = bits == 32 ? UINT32_MAX : (1u << bits) - 1u; ++ value &= mask; count &= 31u; ++ if (!count) return value; ++ unsigned carry = count < bits ? ++ (right ? (value >> (count - 1u)) & 1u : (value >> (bits - count)) & 1u) : 0; ++ uint32_t result = count >= bits ? 0 : ++ (right ? value >> count : (value << count) & mask); ++ if (cf) *cf = (int)carry; ++ if (of && count == 1u) ++ *of = right ? (int)(value >> (bits - 1u)) : ++ (int)((result >> (bits - 1u)) ^ carry); ++ return result; ++} ++/* SHLD/SHRD: counts above 16 for 16-bit operands have undefined result/flags; ++ * choose the unchanged value there. Count 0 always preserves all state. */ ++static inline uint32_t RECOMP_DOUBLE_SHIFT(uint32_t value, uint32_t source, ++ unsigned count, unsigned bits, ++ int right, int *cf, int *of) ++{ ++ uint32_t mask = bits == 32 ? UINT32_MAX : (1u << bits) - 1u; ++ value &= mask; source &= mask; count &= 31u; ++ if (!count || count > bits) return value; ++ unsigned carry = right ? (value >> (count - 1u)) & 1u : ++ (value >> (bits - count)) & 1u; ++ uint32_t result = count == bits ? source : right ? ++ (value >> count) | ((source << (bits - count)) & mask) : ++ ((value << count) & mask) | (source >> (bits - count)); ++ if (cf) *cf = (int)carry; ++ if (of && count == 1u) ++ *of = right ? (int)((value ^ result) >> (bits - 1u)) : ++ (int)((result >> (bits - 1u)) ^ carry); ++ return result; ++} ++static inline uint32_t RECOMP_SAR(uint32_t value, unsigned count, unsigned bits, int *cf) ++{ ++ uint32_t mask = bits == 32 ? UINT32_MAX : (1u << bits) - 1u; ++ value &= mask;count &= 31u; ++ if (!count) return value; ++ uint32_t sign = value >> (bits - 1u); ++ if (cf) *cf = count >= bits ? (int)sign : (int)((value >> (count - 1u)) & 1u); ++ if (count >= bits) return sign ? mask : 0; ++ return (value >> count) | (sign ? mask ^ (mask >> count) : 0); ++} + + static inline uint32_t ROL32(uint32_t val, int n) { + n &= 31; +@@ -886,9 +1090,8 @@ + * + * mm0..mm7 alias the x87 stack on real hardware. Nothing here does, and + * nothing needs to: a title that interleaves the two calls emms between, and +- * emms is a no-op for us. Modelling the aliasing would mean giving up the +- * separate x87 model that the FPU work depends on, to reproduce a hazard the +- * hardware exists to let software avoid. ++ * EMMS empties the x87 tags; the register values remain separate. MMX/x87 ++ * value aliasing and MMX writes marking x87 registers valid remain unmodeled. + * ================================================================ */ + + #ifndef RECOMP_MMX_DEFINED +@@ -981,6 +1184,9 @@ + recomp_sat_u8((int32_t)a.ub[i] + b.ub[i])) + RECOMP_MMX_BINOP(MMX_PSUBUSB, 8, ub, + recomp_sat_u8((int32_t)a.ub[i] - b.ub[i])) ++/* psubusw: unsigned packed-word subtraction, saturating each underflow to 0. */ ++RECOMP_MMX_BINOP(MMX_PSUBUSW, 4, uw, ++ (uint16_t)(a.uw[i] > b.uw[i] ? a.uw[i] - b.uw[i] : 0)) + RECOMP_MMX_BINOP(MMX_PMULLW, 4, w, (int16_t)((int32_t)a.w[i] * b.w[i])) + RECOMP_MMX_BINOP(MMX_PMULHW, 4, w, (int16_t)(((int32_t)a.w[i] * b.w[i]) >> 16)) + RECOMP_MMX_BINOP(MMX_PAVGB, 8, ub, (uint8_t)(((int32_t)a.ub[i] + b.ub[i] + 1) >> 1)) diff --git a/port/macos/tooling/pins.json b/port/macos/tooling/pins.json new file mode 100644 index 0000000000..27033d7014 --- /dev/null +++ b/port/macos/tooling/pins.json @@ -0,0 +1,14 @@ +{ + "halo": { + "url": "https://github.com/cybersecurity/halo-ce-universal.git", + "commit": "0ef2ed7dc7a96299ea7d8d02a1bc3f9fb1c231af" + }, + "xboxrecomp": { + "url": "https://github.com/sp00nznet/xboxrecomp.git", + "commit": "051a128df5ec27ef14f1ceaaead11c5457321eef" + }, + "musl": { + "url": "https://musl.libc.org/releases/musl-1.2.5.tar.gz", + "sha256": "a9a118bbe84d8764da0ea0d28b3ab3fae8477fc7e4085d90102b8596fc7c75e4" + } +} diff --git a/port/macos/tooling/scripts/build.py b/port/macos/tooling/scripts/build.py new file mode 100644 index 0000000000..50ce76952c --- /dev/null +++ b/port/macos/tooling/scripts/build.py @@ -0,0 +1,194 @@ +#!/usr/bin/env python3 +"""Fetch pinned sources and build entirely inside an isolated working directory.""" +import argparse +import hashlib +import json +import os +from pathlib import Path +import shutil +import subprocess +import sys +import tarfile +import urllib.request + +ROOT = Path(__file__).resolve().parents[1] + +def run(*args, cwd=None, env=None): + print('+', ' '.join(map(str,args)), flush=True) + subprocess.run(list(map(str,args)), cwd=cwd, env=env, check=True, stdin=subprocess.DEVNULL) + +def sha(path): + digest=hashlib.sha256() + with Path(path).open('rb') as source: + for data in iter(lambda:source.read(1024*1024),b''):digest.update(data) + return digest.hexdigest() + +def checkout(spec, dest): + if dest.exists(): + raise ValueError('Source directory already exists; choose a fresh --work-dir: '+str(dest)) + run('git','init','-q',dest) + run('git','-C',dest,'fetch','--depth=1',spec['url'],spec['commit']) + run('git','-C',dest,'checkout','--detach','FETCH_HEAD') + got=subprocess.check_output(['git','-C',str(dest),'rev-parse','HEAD'],text=True).strip() + if got != spec['commit']:raise ValueError('Fetched commit mismatch') + +def snapshot_checkout(checkout_path, destination): + """Copy indexed current bytes, retaining working/staged edits without mutating Git.""" + checkout_path=Path(checkout_path).expanduser().resolve() + destination=Path(destination).resolve() + if destination.exists() or destination.is_relative_to(checkout_path): + raise ValueError('Checkout snapshot needs a fresh destination outside the source checkout') + prefix=subprocess.check_output(['git','-C',str(checkout_path),'rev-parse','--show-toplevel'],text=True).strip() + if Path(prefix).resolve()!=checkout_path: + raise ValueError('--source-checkout must name the repository root') + commit=subprocess.check_output(['git','-C',str(checkout_path),'rev-parse','HEAD'],text=True).strip() + names=subprocess.check_output(['git','-C',str(checkout_path),'ls-files','--cached','-z']).decode().split('\0') + ignored_parts={'.git','build','assets','__pycache__','venv','.work','node_modules'} + excluded_suffixes={'.iso','.xiso','.map','.bik','.xmv','.xbe','.elf','.o','.a','.dylib','.so','.exe','.dll','.obj','.lib','.pyc'} + inventory={} + for name in sorted(set(names)-{''}): + relative=Path(name) + if relative.is_absolute() or '..' in relative.parts: + raise ValueError('Unsafe indexed source path') + if ignored_parts.intersection(relative.parts) or any(part.endswith('.app') for part in relative.parts) or relative.suffix.lower() in excluded_suffixes: + continue + if relative.parts[:2]==('tools','dependencies'): + continue + source=checkout_path/relative + if source.is_symlink(): + raise ValueError('Source snapshot requires regular indexed files: '+name) + if not source.exists(): + continue # A working-tree deletion must remain deleted in the snapshot. + if not source.is_file(): + raise ValueError('Indexed source is not a regular file: '+name) + inventory[name]=sha(source) + required={'port/macos/CMakeLists.txt','tools/macos_aot_build.py','validation/sourceport-lp64/batch_compile_guest.py'} + if not required.issubset(inventory): + raise ValueError('Current checkout needs the integrated Mac target; stage newly added source files before using --source-checkout') + destination.mkdir(parents=True) + for name,expected in inventory.items(): + target=destination/name;target.parent.mkdir(parents=True,exist_ok=True) + shutil.copy2(checkout_path/name,target) + if sha(target)!=expected: + raise ValueError('Source changed while snapshotting: '+name) + encoded=json.dumps(inventory,sort_keys=True,separators=(',',':')).encode() + return {'mode':'current-checkout','commit':commit,'files_sha256':inventory, + 'inventory_sha256':hashlib.sha256(encoded).hexdigest(), + 'new_files':'Only indexed files are included; stage new build inputs first.'} + + +def prepare_halo_source(pins, destination, source_checkout=None): + if source_checkout is not None: + # Current-checkout mode never fetches/repatches/replaces Halo sources. + return snapshot_checkout(source_checkout,destination) + checkout(pins['halo'],destination) + run('git','apply',ROOT/'patches/macos-source.patch',cwd=destination) + shutil.copytree(ROOT/'overlay',destination,dirs_exist_ok=True,ignore=shutil.ignore_patterns('__pycache__','*.pyc')) + return {'mode':'pinned-upstream','url':pins['halo']['url'],'commit':pins['halo']['commit']} + + +def main(): + parser=argparse.ArgumentParser(description=__doc__) + parser.add_argument('--work-dir',required=True,type=Path) + parser.add_argument('--assets',required=True,type=Path) + parser.add_argument('--output',required=True,type=Path) + parser.add_argument('--jobs',type=int,default=4) + parser.add_argument('--prepare-only',action='store_true') + parser.add_argument('--source-checkout',type=Path,help='Use current indexed/working source with the Mac target; skip Halo fetch, Mac patch and overlay replacement') + args=parser.parse_args() + work=args.work_dir.expanduser().resolve() + if work.is_relative_to(ROOT): + raise ValueError('Use a workspace outside the tooling repository so generated game code/data cannot enter its tree') + assets=args.assets.expanduser().resolve();out=args.output.expanduser().resolve() + if out.is_relative_to(ROOT): + raise ValueError('Keep the built app outside the tooling repository') + if args.source_checkout is not None: + current=args.source_checkout.expanduser().resolve() + if work.is_relative_to(current) or out.is_relative_to(current): + raise ValueError('Keep build workspace and app output outside the source checkout') + work.mkdir(parents=True,exist_ok=True) + pins=json.loads((ROOT/'pins.json').read_text()) + tooling_names=['pins.json','patches/xboxrecomp-runtime.patch','scripts/build.py'] + if args.source_checkout is None: + overlay_manifest=json.loads((ROOT/'overlay-manifest.json').read_text()) + actual_overlay={str(path.relative_to(ROOT)) for path in (ROOT/'overlay').rglob('*') + if path.is_file() and '__pycache__' not in path.parts and path.suffix != '.pyc'} + if actual_overlay != set(overlay_manifest['files_sha256']): + raise ValueError('Overlay file inventory differs from its reviewed manifest') + for relative,expected in overlay_manifest['files_sha256'].items(): + if sha(ROOT/relative)!=expected: + raise ValueError('Overlay manifest mismatch: '+relative) + tooling_names+=['patches/macos-source.patch','overlay-manifest.json'] + tooling_inputs={name:sha(ROOT/name) for name in tooling_names} + source=work/'source';runtime=work/'xboxrecomp' + source_provenance=prepare_halo_source(pins,source,args.source_checkout) + checkout(pins['xboxrecomp'],runtime) + (work/'source-provenance.json').write_text(json.dumps(source_provenance,indent=2)+'\n') + run('git','apply',ROOT/'patches/xboxrecomp-runtime.patch',cwd=runtime) + deps=source/'build/deps';deps.mkdir(parents=True) + archive=deps/'musl-1.2.5.tar.gz' + urllib.request.urlretrieve(pins['musl']['url'],archive) + if sha(archive)!=pins['musl']['sha256']:raise ValueError('musl download SHA256 mismatch') + with tarfile.open(archive) as tar: + # Avoid link/path escape regardless of upstream download authenticity. + for member in tar.getmembers(): + p=Path(member.name) + if p.is_absolute() or '..' in p.parts or member.issym() or member.islnk(): + raise ValueError('Unsafe musl archive member') + tar.extractall(deps) + musl=deps/'musl-1.2.5';include=deps/'musl-include';(include/'bits').mkdir(parents=True) + with (include/'bits/alltypes.h').open('wb') as sink: + subprocess.run(['sed','-f',str(musl/'tools/mkalltypes.sed'),str(musl/'arch/i386/bits/alltypes.h.in'),str(musl/'include/alltypes.h.in')],stdout=sink,check=True) + # Platform sources use the same interposed syscall numbers as libc. + import importlib.util + spec=importlib.util.spec_from_file_location('guest_compile',source/'validation/sourceport-lp64/batch_compile_guest.py') + helper=importlib.util.module_from_spec(spec);spec.loader.exec_module(helper) + helper.generate_guest_syscall_header(source/'port/android/guest/libc/arch/arm64_32/bits/syscall.h.in',include/'bits/syscall.h') + internal=deps/'musl-internal';internal.mkdir();(internal/'version.h').write_text('#define VERSION "1.2.5"\n') + aot=source/'build/macos-aot';aot.mkdir() + python=sys.executable + run(python,'tools/linux_msvc_semantics.py','--output',aot/'halo_msvc_semantics.h','--all-inlines','--tags','source','--inlines','source','--inlines','port/include/xdk',cwd=source) + run(python,'tools/linux_msvc_semantics.py','--output',aot/'platform_msvc_semantics.h','--inlines','port/include/xdk',cwd=source) + brew=subprocess.check_output(['brew','--prefix'],text=True).strip() + llvm=Path(subprocess.check_output(['brew','--prefix','llvm'],text=True).strip())/'bin' + lld=Path(subprocess.check_output(['brew','--prefix','lld'],text=True).strip())/'bin/ld.lld' + env=os.environ.copy();env['HALO_BREW_INCLUDE']=brew+'/include' + if args.prepare_only: + print('Prepared isolated source/runtime/musl tree:',work);return + for scope in ('game','platform','math','libc'): + run(python,'validation/sourceport-lp64/batch_compile_guest.py','--scope',scope,'--jobs',args.jobs,'--cc','clang',cwd=source,env=env) + link_args=['--link-cc',llvm/'clang','--lld',lld,'--llvm-nm',llvm/'llvm-nm'] + run(python,'tools/macos_aot_build.py','combine',*link_args,cwd=source) + run(python,'tools/macos_aot_build.py','link',*link_args,cwd=source) + elf=aot/'halo_guest.elf';elf_sha=sha(elf);lift=aot/'lifted-final' + run(python,'tools/macos_lift_elf.py','--elf',elf,'--output',lift,'--recomp-root',runtime,'--expected-elf-sha256',elf_sha,cwd=source) + report=json.loads((lift/'lift_report.json').read_text()) + if report.get('functions_failed') or report.get('unresolved_direct_targets'): + raise ValueError('AOT translation contains failures/unresolved direct targets') + native=source/'build/native' + run('cmake','-S','port/macos','-B',native,'-DCMAKE_BUILD_TYPE=Release','-DCMAKE_OSX_DEPLOYMENT_TARGET=14.0','-DBUILD_TESTING=ON','-DHALO_AOT_HOST_OPTIMIZATION=2','-DHALO_LIFT_DIR='+str(lift),cwd=source) + run('cmake','--build',native,'--target','halo_macos_aot','halo_posix_host_imports_test','--parallel',args.jobs,cwd=source) + run('ctest','--test-dir',native,'--output-on-failure','-R','^halo_posix_',cwd=source) + run(python,'tools/test_macos_sdl_window.py',cwd=source) + run(python,'tools/test_macos_hd_resolution.py',cwd=source) + run(python,'tools/test_macos_display_cycle.py',cwd=source) + run(python,'tools/test_macos_ui_condition.py',cwd=source) + run(python,'tools/test_macos_network_startup.py',cwd=source) + run(python,'tools/test_macos_multiplayer_harness.py',cwd=source) + run(python,'tools/test_macos_symbol_namespaces.py',cwd=source) + run(python,'tools/test_macos_gamepad.py',cwd=source) + run(python,'tools/test_macos_translated_gamepad.py','--elf',elf,'--output',aot/'gamepad-fixture','--recomp-root',runtime,cwd=source) + run(python,'tools/test_macos_translated_errno.py','--elf',elf,'--output',aot/'errno-fixture','--miniupnpc-lib',native/'libhalo_miniupnpc.a','--recomp-root',runtime,cwd=source) + binary=native/'halo_macos_aot';stamp=aot/'local-build-provenance.json' + versions={} + for key,cmd in {'macos':['sw_vers','-productVersion'],'clang':['clang','--version'],'llvm':[str(llvm/'clang'),'--version'],'brew':['brew','list','--versions','sdl3','ffmpeg','llvm','lld','cmake'],'capstone':[python,'-c','import capstone; print(capstone.__version__)']}.items(): + versions[key]=subprocess.check_output(cmd,text=True).strip() + stamp.write_text(json.dumps({'source':source_provenance,'tooling_inputs_sha256':tooling_inputs,'build_environment':versions,'binary_sha256':sha(binary),'elf_sha256':elf_sha,'pins':{name:pin for name,pin in pins.items() if name!='halo' or args.source_checkout is None},'candidate_only':True,'startup_acceptance_pending':True,'note':'Locally built; packaging checks do not assert gameplay acceptance.'},indent=2)+'\n') + run(python,'tools/macos_package_app.py','--binary',binary,'--elf',elf,'--stamp',stamp,'--assets',assets,'--output',out,cwd=source) + run(out/'Contents/MacOS/Halo Combat Evolved','--check') + print('Built:',out,'\nLaunch with Finder. Game data is bundled; move the whole app freely. Application Support holds only writable user state.') + +if __name__=='__main__': + try:main() + except (ValueError,OSError,subprocess.CalledProcessError) as error: + raise SystemExit('Build failed: '+str(error)) diff --git a/port/macos/tooling/scripts/xbox_assets.py b/port/macos/tooling/scripts/xbox_assets.py new file mode 100644 index 0000000000..ed929115c0 --- /dev/null +++ b/port/macos/tooling/scripts/xbox_assets.py @@ -0,0 +1,184 @@ +#!/usr/bin/env python3 +"""Read user-owned Xbox XDVDFS images; copy only Halo cache/movie data.""" +import argparse +import hashlib +import json +import os +from pathlib import Path +import struct + +SECTOR = 2048 +MAGIC = b'MICROSOFT*XBOX*MEDIA' +BASES = (0, 0x0FD90000, 0x02080000, 0x18300000) +REQUIRED_MAPS = set('a10 a30 a50 b30 b40 beavercreek bloodgulch boardingaction c10 c20 c40 carousel chillout d20 d40 damnation hangemhigh longest prisoner putput ratrace sidewinder ui wizard'.split()) +REQUIRED_MOVIES = set('attract1 attract2 attract3 credits intro'.split()) +BUILDS = {b'01.10.12.2276': 'NTSC retail', b'01.01.14.2342': 'PAL retail'} + +class ImageError(ValueError): + pass + +class Image: + def __init__(self, path): + self.file = open(path, 'rb') + self.size = os.fstat(self.file.fileno()).st_size + def __del__(self): + if hasattr(self, "file"): + self.file.close() + def read(self, offset, length): + if offset < 0 or length < 0 or offset + length > self.size: + raise ImageError('Truncated image or invalid file extent') + self.file.seek(offset) + data = self.file.read(length) + if len(data) != length: + raise ImageError('Truncated image') + return data + +def inventory(image): + base = None + for candidate in BASES: + offset = candidate + 32 * SECTOR + if offset + SECTOR <= image.size: + descriptor = image.read(offset, SECTOR) + if descriptor[:20] == MAGIC and descriptor[-20:] == MAGIC: + base = candidate + break + if base is None: + if image.size > 17 * SECTOR and image.read(16 * SECTOR + 1, 5) == b'CD001': + raise ImageError('PC/Mac ISO9660 installer detected. This port requires retail Xbox Halo cache version 5; PC/Custom Edition/MCC assets are incompatible.') + raise ImageError('No Xbox XDVDFS game partition found. Supply a decrypted Xbox XISO or Redump disc image, not an archive, XBE, installer, or encrypted dump.') + sector, size = struct.unpack_from(' 32 or count > 16 * 1024 * 1024 or count < 14: + raise ImageError('Invalid directory size/depth') + key = (start, count) + if key in seen_dirs: + raise ImageError('Repeated/cyclic directory extent') + seen_dirs.add(key) + table = image.read(base + start * SECTOR, count) + visited = set() + pending = [0] + while pending: + offset = pending.pop() + if offset in visited or offset + 14 > len(table): + raise ImageError('Cyclic directory tree or invalid entry offset') + visited.add(offset) + left, right, data_sector, data_size, attrs, nlen = struct.unpack_from(' len(table): + raise ImageError('Invalid directory filename length') + raw = table[offset+14:offset+14+nlen] + try: + name = raw.decode('ascii') + except UnicodeDecodeError: + raise ImageError('Non-ASCII Xbox filename') + if name in ('.', '..') or any(c in name for c in '/\\\x00:') or any(ord(c) < 32 or ord(c) == 127 for c in name): + raise ImageError('Unsafe Xbox filename') + path = '/'.join((*prefix, name)).lower() + if path in files: + raise ImageError('Duplicate/case-colliding Xbox path') + if base + data_sector * SECTOR + data_size > image.size: + raise ImageError('File extent exceeds image') + files[path] = (base + data_sector * SECTOR, data_size, bool(attrs & 0x10)) + if attrs & 0x10: + directory(data_sector, data_size, (*prefix, name), depth+1) + if left: + pending.append(left * 4) + if right: + pending.append(right * 4) + directory(sector, size, (), 0) + return base, files + +def selected_assets(image, files): + selected = {} + builds = set() + for name, (offset, size, isdir) in files.items(): + parts = name.split('/') + if isdir or len(parts) != 2: + continue + kind, leaf = parts + if kind in ('maps', 'maps_de', 'maps_fr', 'maps_es', 'maps_it') and leaf.endswith('.map'): + if size < 2048: + raise ImageError('Halo map header is truncated: ' + name) + header = image.read(offset, 2048) + # Xbox uses reversed four-character constants on little-endian hosts. + if header[:4] != b'daeh' or struct.unpack_from('&2; exit 1; } +[ "$(uname -s)" = Darwin ] && [ "$(uname -m)" = arm64 ] || fail 'Apple Silicon macOS is required.' +iso=${1:-} +[ -n "$iso" ] || fail 'Supply your local Xbox Halo ISO: bash setup.sh "/path/Halo.iso"' +[ -f "$iso" ] || fail "ISO does not exist: $iso" +shift +work=${HALO_WORK_DIR:-"$HOME/Library/Caches/halo-ce-apple-silicon/$(date +%Y%m%d-%H%M%S)-$$"} +output=${HALO_APP_OUTPUT:-"$HOME/Applications/Halo Combat Evolved.app"} +[ ! -e "$output" ] || fail "Output already exists; preserve it and set HALO_APP_OUTPUT to a new app path: $output" +xcode-select -p >/dev/null 2>&1 || fail 'Install Apple Command Line Tools with xcode-select --install, then rerun.' +if ! command -v brew >/dev/null 2>&1; then + [ ! -x /opt/homebrew/bin/brew ] || export PATH="/opt/homebrew/bin:$PATH" +fi +command -v brew >/dev/null 2>&1 || fail 'Install Homebrew from https://brew.sh first, then rerun (its administrator setup cannot prompt inside a piped script).' +# Validate the optional current-checkout locations before creating staging. +source_checkout_arg='' +need_source_path=0 +for setup_argument in "$@"; do + if [ "$need_source_path" = 1 ]; then + source_checkout_arg=$setup_argument + need_source_path=0 + continue + fi + case "$setup_argument" in + --source-checkout) need_source_path=1 ;; + --source-checkout=*) source_checkout_arg=${setup_argument#*=} ;; + esac +done +[ "$need_source_path" = 0 ] || fail '--source-checkout needs a directory argument.' +preflight_python=$(command -v python3 || true) +[ -n "$preflight_python" ] || preflight_python=/usr/bin/python3 +if [ -n "$source_checkout_arg" ]; then + "$preflight_python" - "$work" "$output" "$source_checkout_arg" <<'PY_CHECKOUT' +from pathlib import Path +import sys +work, output, checkout = [Path(value).expanduser().resolve() for value in sys.argv[1:]] +if not checkout.is_dir(): + raise SystemExit('Source checkout directory is missing.') +if work.is_relative_to(checkout) or output.is_relative_to(checkout): + raise SystemExit('Keep build workspace and app output outside the source checkout.') +PY_CHECKOUT +fi +mkdir -p "$work" "$(dirname "$output")" +# A local checkout uses its own reviewed tools; a piped script fetches the public project. +script_dir='' +if [ -n "${BASH_SOURCE[0]:-}" ] && [ -f "${BASH_SOURCE[0]}" ]; then + script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +fi +if [ -n "$script_dir" ] && [ -f "$script_dir/pins.json" ]; then + tooling=$script_dir +else + tooling="$work/tooling" + git clone --depth=1 https://github.com/pkyanam/halo-ce-apple-silicon.git "$tooling" +#include #include /* ---------- constants */ @@ -275,6 +276,17 @@ void error( 0x61, priority>=0 && prioritymessaging.button_icons.count) { struct icon_hud_element_definition const *icon; +#ifdef HALO_MACOS + wchar_t const *mac_prompt; +#endif icon_index = remap_sticks_for_local_player( icon_index, @@ -1658,7 +1665,12 @@ void hud_messaging_update( &hud_globals->messaging.button_icons, icon_index, struct icon_hud_element_definition); +#ifdef HALO_MACOS + mac_prompt = halo_macos_control_prompt(icon_index); + if (mac_prompt || TEST_FLAG(icon->flags, _hud_icon_use_text_bit)) +#else if (TEST_FLAG(icon->flags, _hud_icon_use_text_bit)) +#endif { real_argb_color text_color; wchar_t const *icon_text; @@ -1677,6 +1689,9 @@ void hud_messaging_update( icon_text = unicode_string_list_get_string( hud_globals->messaging.alternate_icon_text.index, icon->text_index); +#ifdef HALO_MACOS + if (mac_prompt) icon_text = mac_prompt; +#endif draw_string_set_indents(line_cursor.x0 - line_bounds.x0, 0); draw_unicode_string_compute_bounds( &line_bounds, diff --git a/source/interface/ui_widget.c b/source/interface/ui_widget.c index b3ee222af9..8d86e3ad2f 100644 --- a/source/interface/ui_widget.c +++ b/source/interface/ui_widget.c @@ -3162,7 +3162,11 @@ static void event_handler_dispatch( handler->function, &widget_deleted)) { - error(_error_silent, "event handler function failed"); +#ifdef HALO_MACOS + if (!ui_widget_event_handler_function_is_branch_condition(handler->function)) +#endif + error(_error_silent, "event handler function failed"); + /* FALSE still chooses conditional widgets, including new-profile naming. */ function_failed = TRUE; } else diff --git a/source/interface/ui_widget_event_handler_functions.c b/source/interface/ui_widget_event_handler_functions.c index 6655a8544f..d71aed1984 100644 --- a/source/interface/ui_widget_event_handler_functions.c +++ b/source/interface/ui_widget_event_handler_functions.c @@ -3504,6 +3504,14 @@ static boolean multiplayer_profiles_list_initialize( return TRUE; } +#ifdef HALO_MACOS +boolean ui_widget_event_handler_function_is_branch_condition(word function_index) +{ + return (short)function_index >= 0 && function_index < NUMBEROF(event_handler_function_list.functions) && + event_handler_function_list.functions[function_index] == new_campaign_if_no_custom_player_profiles_exist; +} +#endif + boolean ui_widget_event_handler_function_invoke( struct widget_instance *widget, struct event_record *event, @@ -3518,7 +3526,11 @@ boolean ui_widget_event_handler_function_invoke( if ((short)function_index >= 0 && function_index < 102) { result = event_handler_function_list.functions[(short)function_index](widget, event, widget_deleted); - if (!result) + if (!result +#ifdef HALO_MACOS + && !ui_widget_event_handler_function_is_branch_condition(function_index) +#endif + ) console_warning("event handler '%s' failed", event_handler_function_list.names[(short)function_index]); return result; } diff --git a/source/interface/ui_widget_event_handler_functions.h b/source/interface/ui_widget_event_handler_functions.h index f425ccf972..fee4d00a34 100644 --- a/source/interface/ui_widget_event_handler_functions.h +++ b/source/interface/ui_widget_event_handler_functions.h @@ -19,6 +19,11 @@ struct widget_instance; /* ---------- prototypes/UI_WIDGET_EVENT_HANDLER_FUNCTIONS.C */ +#ifdef HALO_MACOS +/* A false condition selects another UI branch; it is not a failed action. */ +boolean ui_widget_event_handler_function_is_branch_condition(word function_index); +#endif + boolean ui_widget_event_handler_function_invoke( struct widget_instance *widget, struct event_record *event, diff --git a/source/networking/network_server_message_handler.c b/source/networking/network_server_message_handler.c index a1edd83161..a0d0ec4575 100644 --- a/source/networking/network_server_message_handler.c +++ b/source/networking/network_server_message_handler.c @@ -1494,6 +1494,17 @@ boolean network_game_server_handle_datagram( struct network_game_server_client_machine *client_machine = network_game_server_get_client_machine_at_address(server, source_address->address.long_words[0]); +#ifdef HALO_MACOS + /* Opt-in local transport diagnosis; no packet or state changes. */ + { + static long traced, enabled = -1; + if (enabled < 0) enabled = getenv("HALO_NETWORK_TRACE") && strcmp(getenv("HALO_NETWORK_TRACE"), "1") == 0; + if (enabled && (++traced <= 16 || traced % 300 == 0)) + fprintf(stderr, "[mp-predecode] n=%ld source=%08lX port=%u size=%u match=%d state=%d\n", traced, + source_address->address.long_words[0], (unsigned)source_address->port, + (unsigned)datagram_size, client_machine != NULL, (int)network_game_server_get_state(server, NULL)); + } +#endif if (client_machine && network_game_server_get_state(server, NULL) == _network_game_server_state_ingame) { long machine_index; diff --git a/source/saved games/game_state_xbox.c b/source/saved games/game_state_xbox.c index 591a52b483..2ba4aced3e 100644 --- a/source/saved games/game_state_xbox.c +++ b/source/saved games/game_state_xbox.c @@ -548,6 +548,16 @@ boolean game_state_read_header_from_persistent_storage( { stored_checksum = *header_checksum; +#ifdef HALO_MACOS + /* A new profile's storage is intentionally zero-filled by + * game_state_open_persistent_storage. It has no checkpoint yet. */ + if (!stored_checksum) + { + CloseHandle(file); + return FALSE; + } +#endif + crc_new(&checksum); *header_checksum = 0; crc_checksum_buffer(&checksum, header, header_size); diff --git a/tools/check_macos_symbol_namespaces.py b/tools/check_macos_symbol_namespaces.py new file mode 100644 index 0000000000..67e402a9c9 --- /dev/null +++ b/tools/check_macos_symbol_namespaces.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python3 +"""Reject native archive definitions that can be shadowed by generated guest code. + +A static archive member need not be linked when a generated guest alias already +satisfies its symbol. Calls from a native adapter can then recursively re-enter +that adapter. Check archives before linking, including members not selected by +ld; executable-only nm cannot detect these omitted native implementations. +""" +import argparse +import re +import subprocess +import sys +from pathlib import Path + + +def guest_definitions(directory): + result = set() + for path in directory.glob('recomp_*.c'): + text = path.read_text() + result.update(re.findall(r'^\s*void\s+(\w+)\s*\(void\)\s*\{', text, re.M)) + if not result: + raise ValueError(f'no generated guest function definitions in {directory}') + return result + + +def native_definitions(output, mach_o): + result = set() + for line in output.splitlines(): + # Both Darwin and GNU nm use: [address] . Ignore + # undefined symbols (U and weak undefined w/v), archive headers/local + # symbols, but include weak definitions W/V that can also be shadowed. + match = re.search(r'(?:^|\s)([A-Z])\s+(\S+)\s*$', line) + if not match or match[1] == 'U': + continue + symbol = match[2] + if mach_o and symbol.startswith('_'): + symbol = symbol[1:] + result.add(symbol) + return result + + +def native_references(output, mach_o): + """External calls must not bind to a generated guest ABI entry point.""" + result = set() + for line in output.splitlines(): + match = re.search(r'(?:^|\s)[Uwv]\s+(\S+)\s*$', line) + if match: + symbol = match[1] + if mach_o and symbol.startswith('_'): + symbol = symbol[1:] + result.add(symbol) + return result + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--lift-dir', type=Path, required=True) + parser.add_argument('--archive', type=Path, action='append', required=True) + parser.add_argument('--nm', default='nm') + args = parser.parse_args() + guest = guest_definitions(args.lift_dir) + total = set() + references = set() + collisions = [] + for archive in args.archive: + output = subprocess.check_output([args.nm, '-g', str(archive)], text=True) + native = native_definitions(output, sys.platform == 'darwin') + external = native_references(output, sys.platform == 'darwin') + total.update(native) + references.update(external) + collisions.extend((symbol, archive) for symbol in sorted((native | external) & guest)) + if collisions: + for symbol, archive in collisions: + print(f'ERROR: guest/native symbol collision: {symbol} in {archive}', file=sys.stderr) + print('Rename the native implementation (mac_*); keep guest ABI aliases unchanged.', file=sys.stderr) + return 1 + print(f'Guest/native symbol namespace PASS: {len(guest)} guest definitions, ' + f'{len(total)} archive definitions, {len(references)} external references, ' + f'{len(args.archive)} archives') + return 0 + + +if __name__ == '__main__': + raise SystemExit(main()) diff --git a/tools/macos_aot_build.py b/tools/macos_aot_build.py new file mode 100644 index 0000000000..b236f23c9f --- /dev/null +++ b/tools/macos_aot_build.py @@ -0,0 +1,143 @@ +#!/usr/bin/env python3 +"""Build the 2342 source guest as i386 ELF for ARM64 AOT translation. + +Stages: + compile Compile the upstream game and shared platform C source manifest. + combine Preserve all section/symbol/relocation metadata in an ELF32 ET_REL. + full Run both stages. + +This is not the final translator/linker: undefined symbols in the combined +object are reported for explicit libc/platform-import resolution. +""" +from __future__ import annotations + +import argparse +import json +import subprocess +import sys +from pathlib import Path + + +ROOT = Path(__file__).resolve().parents[1] +BUILD = ROOT / "build/macos-aot" + + +def run_compile(args: argparse.Namespace, scope: str) -> None: + cmd = [sys.executable, str(ROOT / "validation/sourceport-lp64/batch_compile_guest.py"), + "--scope", scope, "--jobs", str(args.jobs), "--cc", args.guest_cc] + if args.limit: + cmd += ["--limit", str(args.limit)] + subprocess.run(cmd, cwd=ROOT, check=True) + + +def combine(args: argparse.Namespace) -> None: + # These musl units duplicate symbols already supplied by the title's + # compatibility layer. Keep the game's strnlen and its stack-check hook. + libc_objects = [ + path for path in sorted((BUILD / "libc-obj").rglob("*.o")) + if path.as_posix().endswith(( + "/src/string/strnlen.o", + "/src/env/__stack_chk_fail.o", + # i386 assembly provides the ABI-specific x87 implementations. + "/src/fenv/fenv.o", + )) is False + ] + objects = ( + sorted((BUILD / "game-obj").rglob("*.o")) + + [p for p in sorted((BUILD / "platform-obj").rglob("*.o")) + if not p.as_posix().endswith("/port/android/guest/runtime/guest_memory_watch.o")] + + sorted((BUILD / "math-obj").rglob("*.o")) + + libc_objects + ) + if not objects: + raise SystemExit("no guest objects found; run compile first") + output = BUILD / "halo_guest_combined.o" + cmd = [args.link_cc, "--target=i386-unknown-linux-musl", "-m32", + f"-fuse-ld={args.lld}", "-nostdlib", "-no-pie", "-Wl,-r", "-o", str(output), + *map(str, objects)] + subprocess.run(cmd, cwd=ROOT, check=True) + nm = subprocess.run([args.llvm_nm, "--undefined-only", str(output)], cwd=ROOT, + check=True, text=True, stdout=subprocess.PIPE) + undefined = sorted(line.strip().split()[-1] for line in nm.stdout.splitlines() if line.strip()) + (BUILD / "undefined-symbols.txt").write_text("\n".join(undefined) + "\n") + defined_count = subprocess.run([args.llvm_nm, "--defined-only", str(output)], cwd=ROOT, + check=True, text=True, stdout=subprocess.PIPE).stdout.count("\n") + unique_undefined = sorted(set(undefined)) + groups = { + "SDL3": [s for s in unique_undefined if s.startswith("SDL_")], + "POSIX_platform": [s for s in unique_undefined if s.startswith("posix_")], + "host_services": [s for s in unique_undefined if s.startswith("host_")], + "other": [s for s in unique_undefined if not s.startswith(("SDL_", "posix_", "host_"))], + } + metadata = { + "object_count": len(objects), + "combined_elf": str(output.relative_to(ROOT)), + "defined_symbol_entries": defined_count, + "undefined_symbol_references": len(undefined), + "undefined_unique": len(unique_undefined), + "undefined_groups": {name: len(items) for name, items in groups.items()}, + "undefined_symbols": unique_undefined, + } + (BUILD / "combined-symbol-report.json").write_text(json.dumps(metadata, indent=2) + "\n") + print(json.dumps({key: value for key, value in metadata.items() if key != "undefined_symbols"}, indent=2)) + + +def link(args: argparse.Namespace) -> None: + # Generate named UD2 entries for imports. The AOT runtime must dispatch + # these VAs to typed native adapters; executing an entry traps. + subprocess.run([sys.executable, str(ROOT / "tools/macos_import_stubs.py"), + "--cc", args.link_cc], cwd=ROOT, check=True) + compiler_rt_sources = ("divdi3", "moddi3", "udivdi3", "umoddi3", "udivmoddi4") + compiler_rt_dir = ROOT / "port/macos/compiler-rt" + compiler_rt_out = BUILD / "compiler-rt-obj" + compiler_rt_out.mkdir(parents=True, exist_ok=True) + compiler_rt_objects = [] + for name in compiler_rt_sources: + obj = compiler_rt_out / f"{name}.o" + subprocess.run([args.link_cc, "--target=i386-unknown-linux-musl", "-m32", + "-O2", "-ffreestanding", "-fno-builtin", "-fno-pic", "-fno-pie", + "-I", str(compiler_rt_dir), "-c", str(compiler_rt_dir / f"{name}.c"), + "-o", str(obj)], cwd=ROOT, check=True) + compiler_rt_objects.append(obj) + object_dirs = ("game-obj", "platform-obj", "math-obj", "libc-obj") + objects = [] + for directory in object_dirs: + objects.extend(sorted((BUILD / directory).rglob("*.o"))) + objects = [p for p in objects if not p.as_posix().endswith(( + "/src/string/strnlen.o", "/src/env/__stack_chk_fail.o", "/src/fenv/fenv.o", + "/port/android/guest/runtime/guest_memory_watch.o"))] + output = BUILD / "halo_guest.elf" + mapfile = BUILD / "halo_guest.map" + cmd = [args.lld, "-m", "elf_i386", "-T", str(ROOT / "port/macos/guest_aot.ld"), + "-Map", str(mapfile), "--no-undefined", "-o", str(output), + *map(str, objects), *map(str, compiler_rt_objects), + str(BUILD / "host_import_stubs.o")] + subprocess.run(cmd, cwd=ROOT, check=True) + print(f"linked {output.relative_to(ROOT)} ({output.stat().st_size} bytes)") + + +def main() -> int: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("stage", choices=("compile", "combine", "link", "full")) + parser.add_argument("--jobs", type=int, default=4) + parser.add_argument("--limit", type=int, default=0, help="compile only the first N units per scope") + parser.add_argument("--guest-cc", default="clang") + parser.add_argument("--link-cc", default="/opt/homebrew/opt/llvm/bin/clang") + parser.add_argument("--lld", default="/opt/homebrew/opt/lld/bin/ld.lld") + parser.add_argument("--llvm-nm", default="/opt/homebrew/opt/llvm/bin/llvm-nm") + parser.add_argument("--compiler-rt", default="") + args = parser.parse_args() + if args.stage in ("compile", "full"): + run_compile(args, "game") + run_compile(args, "platform") + run_compile(args, "math") + run_compile(args, "libc") + if args.stage in ("combine", "full"): + combine(args) + if args.stage == "link": + link(args) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/tools/macos_gl_bridge.py b/tools/macos_gl_bridge.py new file mode 100644 index 0000000000..68ee6d808d --- /dev/null +++ b/tools/macos_gl_bridge.py @@ -0,0 +1,295 @@ +#!/usr/bin/env python3 +"""Generate deterministic 32-bit GL procedure tokens and wrapper signatures. + +The AOT guest is i686 and cannot hold a native arm64 GL function pointer. This +tool derives the exported GL surface from port/linux/src/gl.h, reads exact host +prototypes from SDL's OpenGL headers, and emits a stable signature manifest +consumed by the typed wrapper generator and token registry. +""" + +from __future__ import annotations + +import argparse +import hashlib +import json +import re +import sys +from pathlib import Path + +TOKEN_BASE = 0xFE100000 +TOKEN_STRIDE = 16 + + +def split_arguments(text: str) -> list[tuple[str, str]]: + text = text.strip() + if not text or text == "void": + return [] + out = [] + for arg in text.split(","): + arg = " ".join(arg.split()) + match = re.fullmatch(r"(.+?\*?\s*)([A-Za-z_][A-Za-z0-9_]*)", arg) + if not match: + raise ValueError(f"cannot parse GL parameter declaration: {arg}") + ctype, name = match.groups() + out.append((ctype.strip(), name)) + return out + + +def slot_count(ctype: str) -> int: + # GLdouble/GLclampd are 64-bit values in the i686 cdecl ABI. GLsizeiptr + # and GLintptr are pointer-sized on the guest, so occupy one 32-bit slot. + return 2 if ctype in ("GLdouble", "GLclampd") else 1 + + +def generate_wrappers(entries: list[dict[str, object]]) -> str: + lines = [ + "/* Generated by tools/macos_gl_bridge.py. Do not edit. */", + '#include ', + '#include "guest_call.h"', + '#include "guest_address.h"', + '#include "gl_token_registry.h"', + '#include "recomp_types.h"', + '#include ', + '#include ', + '#include ', + '#include ', + '#include ', + 'extern void *mac_macos_gl_native_proc(const char *name);', + 'extern void *mac_macos_gl_guest_pointer(const char *name, unsigned arg_index, uint32_t guest_va, const uint32_t *raw_args, unsigned arg_count);', + 'extern uint32_t mac_macos_gl_return_guest_pointer(const char *name, const void *host_pointer, const uint32_t *raw_args, unsigned arg_count);', + 'extern void mac_macos_gl_bridge_fault(const char *name, const char *reason);', + 'typedef void (APIENTRYP mac_fbo_get_integer_fn)(GLenum, GLint *);', + 'typedef void (APIENTRYP mac_fbo_get_attachment_fn)(GLenum, GLenum, GLenum, GLint *);', + 'typedef void (APIENTRYP mac_fbo_get_tex_level_fn)(GLenum, GLint, GLenum, GLint *);', + 'typedef void (APIENTRYP mac_fbo_bind_texture_fn)(GLenum, GLuint);', + 'typedef void (APIENTRYP mac_fbo_get_renderbuffer_fn)(GLenum, GLenum, GLint *);', + 'typedef void (APIENTRYP mac_fbo_bind_renderbuffer_fn)(GLenum, GLuint);', + 'typedef GLenum (APIENTRYP mac_fbo_get_error_fn)(void);', + 'typedef void (APIENTRYP mac_tex_get_integer_fn)(GLenum, GLint *);', + 'typedef GLenum (APIENTRYP mac_tex_get_error_fn)(void);', + 'static void mac_trace_tex_image_2d(GLenum target, GLint level, GLint internal, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const void *pixels)', + '{', + '\tif (!getenv("HALO_TRACE_TEX_IMAGE")) return;', + '\tmac_tex_get_integer_fn get_integer = (mac_tex_get_integer_fn)mac_macos_gl_native_proc("glGetIntegerv");', + '\tmac_tex_get_error_fn get_error = (mac_tex_get_error_fn)mac_macos_gl_native_proc("glGetError");', + '\tGLint binding = 0;', + '\tif (get_integer && target == GL_TEXTURE_2D) get_integer(GL_TEXTURE_BINDING_2D, &binding);', + '\tfprintf(stderr, "[tex-image] target=0x%04x texture=%d level=%d internal=0x%04x size=%dx%d border=%d format=0x%04x type=0x%04x pixels=%p\\n", target, binding, level, internal, width, height, border, format, type, pixels);', + '\tif (get_error) fprintf(stderr, "[tex-image] glErrorAfterUpload=0x%04x\\n", get_error());', + '}', + 'static void mac_trace_fbo_check(GLenum target, GLenum status)', + '{', + '\tif (!getenv("HALO_TRACE_FBO")) return;', + '\tmac_fbo_get_integer_fn get_integer = (mac_fbo_get_integer_fn)mac_macos_gl_native_proc("glGetIntegerv");', + '\tmac_fbo_get_attachment_fn get_attachment = (mac_fbo_get_attachment_fn)mac_macos_gl_native_proc("glGetFramebufferAttachmentParameteriv");', + '\tmac_fbo_get_tex_level_fn get_tex_level = (mac_fbo_get_tex_level_fn)mac_macos_gl_native_proc("glGetTexLevelParameteriv");', + '\tmac_fbo_bind_texture_fn bind_texture = (mac_fbo_bind_texture_fn)mac_macos_gl_native_proc("glBindTexture");', + '\tmac_fbo_get_renderbuffer_fn get_renderbuffer = (mac_fbo_get_renderbuffer_fn)mac_macos_gl_native_proc("glGetRenderbufferParameteriv");', + '\tmac_fbo_bind_renderbuffer_fn bind_renderbuffer = (mac_fbo_bind_renderbuffer_fn)mac_macos_gl_native_proc("glBindRenderbuffer");', + '\tmac_fbo_get_error_fn get_error = (mac_fbo_get_error_fn)mac_macos_gl_native_proc("glGetError");', + '\tGLint framebuffer = 0;', + '\tGLint old_texture = 0, old_renderbuffer = 0;', + '\tif (!get_integer || !get_attachment) { fprintf(stderr, "[fbo-trace] status=0x%04x target=0x%04x metadata-procs-missing\\n", status, target); return; }', + '\t/* Local static address from the pinned 2b0c27cb guest ELF; refresh with each relink. */', + '\tuint32_t scale_x_bits = MEM32(0x005A6514u), scale_y_bits = MEM32(0x005A6518u); float scale_x, scale_y; memcpy(&scale_x, &scale_x_bits, sizeof(scale_x)); memcpy(&scale_y, &scale_y_bits, sizeof(scale_y));', + '\tfprintf(stderr, "[fbo-trace] guest-screen-scale bits={0x%08x,0x%08x} values={%.9g,%.9g}\\n", scale_x_bits, scale_y_bits, scale_x, scale_y);', + '\tget_integer(target == GL_READ_FRAMEBUFFER ? GL_READ_FRAMEBUFFER_BINDING : GL_DRAW_FRAMEBUFFER_BINDING, &framebuffer);', + '\tfprintf(stderr, "[fbo-trace] target=0x%04x fbo=%d status=0x%04x\\n", target, framebuffer, status);', + '\tconst GLenum points[] = { GL_COLOR_ATTACHMENT0, GL_DEPTH_STENCIL_ATTACHMENT };', + '\tfor (size_t i = 0; i < sizeof(points) / sizeof(points[0]); ++i) {', + '\t\tGLint type = GL_NONE, object = 0, level = 0, width = 0, height = 0, internal = 0;', + '\t\tget_attachment(target, points[i], GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &type);', + '\t\tget_attachment(target, points[i], GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &object);', + '\t\tif (type == GL_TEXTURE && object && get_integer && get_tex_level && bind_texture) {', + '\t\t\tget_attachment(target, points[i], GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL, &level);', + '\t\t\tget_integer(GL_TEXTURE_BINDING_2D, &old_texture); bind_texture(GL_TEXTURE_2D, (GLuint)object);', + '\t\t\tget_tex_level(GL_TEXTURE_2D, level, GL_TEXTURE_WIDTH, &width); get_tex_level(GL_TEXTURE_2D, level, GL_TEXTURE_HEIGHT, &height); get_tex_level(GL_TEXTURE_2D, level, GL_TEXTURE_INTERNAL_FORMAT, &internal);', + '\t\t\tbind_texture(GL_TEXTURE_2D, (GLuint)old_texture);', + '\t\t} else if (type == GL_RENDERBUFFER && object && get_integer && get_renderbuffer && bind_renderbuffer) {', + '\t\t\tget_integer(GL_RENDERBUFFER_BINDING, &old_renderbuffer); bind_renderbuffer(GL_RENDERBUFFER, (GLuint)object);', + '\t\t\tget_renderbuffer(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &width); get_renderbuffer(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &height); get_renderbuffer(GL_RENDERBUFFER, GL_RENDERBUFFER_INTERNAL_FORMAT, &internal);', + '\t\t\tbind_renderbuffer(GL_RENDERBUFFER, (GLuint)old_renderbuffer);', + '\t\t}', + '\t\tfprintf(stderr, "[fbo-trace] attachment=0x%04x type=0x%04x object=%d level=%d size=%dx%d internal=0x%04x\\n", points[i], type, object, level, width, height, internal);', + '\t}', + '\tif (get_error) fprintf(stderr, "[fbo-trace] glErrorAfterMetadata=0x%04x\\n", get_error());', + '}', + ] + registry = [] + for entry in entries: + name = str(entry["name"]) + token = int(entry["token"]) + ret = str(entry["return_type"]) + args_text = str(entry["arguments"]) + args = split_arguments(args_text) + arg_decl = ", ".join(f"{ctype} a{index}" for index, (ctype, _) in enumerate(args)) or "void" + arg_names = ", ".join(f"a{index}" for index in range(len(args))) + lines.append(f"typedef {ret} (APIENTRYP mac_fn_{name})({arg_decl});") + lines.append(f"void sub_{token:08X}(void)") + lines.append("{") + # `recomp_types.h` defines the generated-code alias `esp` as `g_esp`. + # Do not name this wrapper-local snapshot `esp`: after preprocessing, + # `uint32_t esp = g_esp` becomes a self-initializing local `g_esp`, + # so argument reads use an indeterminate stack address and the return + # silently updates the local instead of translator TLS. + lines.append("\tuint32_t guest_entry_esp = g_esp;") + words = sum(slot_count(ctype) for ctype, _ in args) + lines.append(f"\tuint32_t stack_words[{max(words, 1)}] = {{ 0 }};") + lines.append(f"\tuint32_t raw[{max(len(args), 1)}] = {{ 0 }};") + lines.append("\t(void)raw;") + lines.append("\t(void)stack_words;") + lines.append("\tuint32_t next_esp;") + if name == "glGetString": + lines.append("\tif (getenv(\"HALO_TRACE_GUEST_STACK\"))") + lines.append("\t{ uint32_t _arg_va = guest_entry_esp + 4u; uint32_t _ret = 0; (void)mac_guest_stack_return(guest_entry_esp, &_ret);") + lines.append("\t\tfprintf(stderr, \"[GL-ENTRY] name=glGetString thread=%llu entry_esp=0x%08x caller_pre_esp=0x%08x ebp=0x%08x mem_offset=%td ret=0x%08x arg_va=0x%08x arg_host=%p\\n\", (unsigned long long)(uintptr_t)pthread_self(), guest_entry_esp, guest_entry_esp + 4u, g_ebp, g_xbox_mem_offset, _ret, _arg_va, mac_guest_address_resolve(_arg_va, 4)); fflush(stderr); }\n") + word = 0 + arg_words = [] + for index, (ctype, arg_name) in enumerate(args): + nslots = slot_count(ctype) + for component in range(nslots): + lines.append(f"\tif (mac_guest_stack_arg32(guest_entry_esp, {word + component}, &stack_words[{word + component}]) != 0)") + lines.append(f"\t\tmac_macos_gl_bridge_fault(\"{name}\", \"guest argument stack is unmapped\");") + lines.append(f"\traw[{index}] = stack_words[{word}];") + arg_words.append(word) + word += nslots + locals_ = [] + for index, (ctype, arg_name) in enumerate(args): + nslots = slot_count(ctype) + word = arg_words[index] + local = f"a{index}" + if "*" in ctype: + lines.append(f"\t{ctype} {local} = ({ctype})mac_macos_gl_guest_pointer(\"{name}\", {index}, raw[{index}], raw, {len(args)});") + lines.append(f"\tif (raw[{index}] && !{local}) mac_macos_gl_bridge_fault(\"{name}\", \"guest pointer span is invalid or unsupported\");") + elif nslots == 2: + lines.append(f"\tuint64_t bits_{index} = (uint64_t)stack_words[{word}] | ((uint64_t)stack_words[{word + 1}] << 32);") + lines.append(f"\t{ctype} {local};") + lines.append(f"\tmemcpy(&{local}, &bits_{index}, sizeof({local}));") + elif ctype in ("GLfloat", "GLclampf"): + lines.append(f"\t{ctype} {local};") + lines.append(f"\tmemcpy(&{local}, &stack_words[{word}], sizeof({local}));") + elif ctype in ("GLsizeiptr", "GLintptr"): + lines.append(f"\t{ctype} {local} = ({ctype})(int32_t)stack_words[{word}];") + else: + lines.append(f"\t{ctype} {local} = ({ctype})stack_words[{word}];") + locals_.append(local) + lines.append(f"\tmac_fn_{name} fn = (mac_fn_{name})mac_macos_gl_native_proc(\"{name}\");") + lines.append(f"\tif (!fn) mac_macos_gl_bridge_fault(\"{name}\", \"native GL entry point is unavailable\");") + call = f"fn({arg_names})" if args else "fn()" + if ret == "void": + lines.append(f"\t{call};") + if name == "glTexImage2D": + lines.append("\tmac_trace_tex_image_2d(a0, a1, a2, a3, a4, a5, a6, a7, a8);") + elif "*" in ret: + lines.append(f"\t{ret} result = {call};") + lines.append(f"\tg_eax = mac_macos_gl_return_guest_pointer(\"{name}\", (const void *)result, raw, {len(args)});") + else: + lines.append(f"\t{ret} result = {call};") + if name == "glCheckFramebufferStatus": + lines.append("\tmac_trace_fbo_check(a0, (GLenum)result);") + lines.append("\tg_eax = (uint32_t)result;") + lines.append("\tif (mac_guest_stack_after_return(guest_entry_esp, 0, &next_esp) != 0)") + lines.append(f"\t\tmac_macos_gl_bridge_fault(\"{name}\", \"cannot pop guest cdecl return address\");") + lines.append("\tg_esp = next_esp;") + lines.append("}") + registry.append((name, token)) + lines.extend([ + "struct mac_macos_gl_token_entry { const char *name; uint32_t token; mac_macos_gl_guest_function wrapper; };", + "static const struct mac_macos_gl_token_entry mac_gl_tokens[] = {", + ]) + lines.extend(f'\t{{ "{name}", 0x{token:08X}u, sub_{token:08X} }},' for name, token in registry) + lines.extend([ + "};", + "uint32_t mac_macos_gl_token_for_name(const char *name)", + "{", + "\tif (!name) return 0;", + "\tfor (size_t i = 0; i < sizeof(mac_gl_tokens) / sizeof(mac_gl_tokens[0]); i++)", + "\t\tif (strcmp(name, mac_gl_tokens[i].name) == 0) return mac_gl_tokens[i].token;", + "\treturn 0;", + "}", + "mac_macos_gl_guest_function mac_macos_gl_wrapper_for_token(uint32_t token)", + "{", + "\tfor (size_t i = 0; i < sizeof(mac_gl_tokens) / sizeof(mac_gl_tokens[0]); i++)", + "\t\tif (token == mac_gl_tokens[i].token) return mac_gl_tokens[i].wrapper;", + "\treturn NULL;", + "}", + ]) + return "\n".join(lines) + "\n" + + +def gl_names(gl_header: Path) -> list[str]: + text = gl_header.read_text() + marker = "#define GL_FUNCTIONS(X)" + start = text.find(marker, text.find("#else\n#ifdef HALO_MACOS")) + if start < 0: + raise ValueError("could not find desktop GL_FUNCTIONS list") + end = text.find("\n#endif", start) + if end < 0: + raise ValueError("unterminated desktop GL_FUNCTIONS list") + block = text[start:end] + names = re.findall(r"\bX\((gl[A-Za-z0-9_]+)\)\s*\\?", block) + if not names or len(names) != len(set(names)): + raise ValueError("GL function list is empty or contains duplicates") + return names + + +def prototypes(paths: list[Path]) -> dict[str, tuple[str, str]]: + # SDL's generated headers use either `GLAPIENTRY` or `APIENTRY`; whitespace + # around pointer return types varies, so match declaration through semicolon. + pattern = re.compile( + r"\bGLAPI\s+(.+?)(?:\s*GLAPIENTRY|\s*APIENTRY)\s+" + r"(gl[A-Za-z0-9_]+)\s*\((.*?)\)\s*;", re.S + ) + found: dict[str, tuple[str, str]] = {} + for path in paths: + for match in pattern.finditer(path.read_text()): + ret, name, args = match.groups() + found.setdefault(name, (" ".join(ret.split()), " ".join(args.split()))) + return found + + +def main() -> int: + parser = argparse.ArgumentParser() + parser.add_argument("--gl-header", type=Path, required=True) + parser.add_argument("--sdl-header", type=Path, action="append", required=True) + parser.add_argument("--output", type=Path, required=True) + parser.add_argument("--c-output", type=Path) + args = parser.parse_args() + + names = gl_names(args.gl_header) + decls = prototypes(args.sdl_header) + missing = [name for name in names if name not in decls] + if missing: + raise SystemExit("missing SDL host prototypes: " + ", ".join(missing)) + + entries = [] + for index, name in enumerate(names): + ret, signature = decls[name] + split_arguments(signature) + entries.append({ + "name": name, + "token": TOKEN_BASE + index * TOKEN_STRIDE, + "return_type": ret, + "arguments": signature, + }) + payload = { + "format": 1, + "token_base": TOKEN_BASE, + "token_stride": TOKEN_STRIDE, + "source_gl_header_sha256": hashlib.sha256(args.gl_header.read_bytes()).hexdigest(), + "entries": entries, + } + args.output.parent.mkdir(parents=True, exist_ok=True) + args.output.write_text(json.dumps(payload, indent=2) + "\n") + if args.c_output: + args.c_output.parent.mkdir(parents=True, exist_ok=True) + args.c_output.write_text(generate_wrappers(entries)) + print(f"wrote {len(entries)} deterministic GL token signatures to {args.output}") + return 0 + + +if __name__ == "__main__": + try: + sys.exit(main()) + except (OSError, ValueError) as error: + raise SystemExit(str(error)) diff --git a/tools/macos_import_stubs.py b/tools/macos_import_stubs.py new file mode 100644 index 0000000000..45026f4e77 --- /dev/null +++ b/tools/macos_import_stubs.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python3 +"""Emit deterministic fail-fast i386 import entrypoints for AOT linking. + +These trap bodies exist only to give the ELF image stable symbol addresses. +The native dispatcher must intercept each VA and invoke its typed adapter; +executing a stub is always fatal (UD2), never a successful empty return. +""" +from __future__ import annotations + +import argparse +import json +import subprocess +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +BUILD = ROOT / "build/macos-aot" +BASE = 0x70000000 +STRIDE = 16 + + +def main() -> int: + parser = argparse.ArgumentParser() + parser.add_argument("--cc", default="/opt/homebrew/opt/llvm/bin/clang") + args = parser.parse_args() + names = set((BUILD / "undefined-symbols.txt").read_text().splitlines()) + imports = sorted(name for name in names if name.startswith(("host_", "posix_")) or + name in {"guest_gl_get_proc_address", "halo_gl41_copy_image_2d", + "platform_screen_mode"}) + if len(imports) != len(set(imports)): + raise SystemExit("duplicate import names") + BUILD.mkdir(parents=True, exist_ok=True) + manifest = [{"name": name, "va": BASE + STRIDE * i, "slot": i} + for i, name in enumerate(imports)] + (BUILD / "host-import-manifest.json").write_text(json.dumps({ + "abi": "i386-cdecl-entry-trap-dispatched-by-VA", + "base": BASE, "stride": STRIDE, "imports": manifest, + }, indent=2) + "\n") + header = ["/* Generated from host-import-manifest.json; do not edit. */", + "#ifndef HALO_MACOS_HOST_IMPORT_NAMES_H", + "#define HALO_MACOS_HOST_IMPORT_NAMES_H", + f"#define MAC_GUEST_HOST_IMPORT_COUNT {len(imports)}u", + "static const char *const mac_guest_host_import_names[MAC_GUEST_HOST_IMPORT_COUNT] = {"] + header.extend(f' "{name}",' for name in imports) + header += ["};", "#endif", ""] + (BUILD / "host_import_names.h").write_text("\n".join(header)) + asm = [".section .host_imports,\"ax\",@progbits", ".code32", ".balign 16"] + for item in manifest: + asm += [f".globl {item['name']}", f".type {item['name']},@function", + f"{item['name']}:", "ud2", ".fill 14,1,0x90"] + asm += [".section .rodata.host_import_names,\"a\",@progbits", ".balign 4", + ".globl __host_import_names", "__host_import_names:"] + for item in manifest: + asm += [f".asciz \"{item['name']}\""] + asm += [".section .rodata.host_import_count,\"a\",@progbits", ".balign 4", + ".globl __host_import_count", "__host_import_count:", + f".long {len(manifest)}"] + asm += [".section .rodata.host_import_table,\"a\",@progbits", ".balign 4", + ".globl __host_import_table", "__host_import_table:"] + for item in manifest: + asm += [f".long {item['name']}"] + asm_path = BUILD / "host_import_stubs.S" + asm_path.write_text("\n".join(asm) + "\n") + obj = BUILD / "host_import_stubs.o" + subprocess.run([args.cc, "--target=i386-unknown-linux-musl", "-m32", "-c", + str(asm_path), "-o", str(obj)], cwd=ROOT, check=True) + print(f"generated {len(manifest)} fail-fast stubs at 0x{BASE:08X}.." + f"0x{BASE + STRIDE * len(manifest):08X}") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/tools/macos_lift_elf.py b/tools/macos_lift_elf.py new file mode 100644 index 0000000000..3c2dead2c4 --- /dev/null +++ b/tools/macos_lift_elf.py @@ -0,0 +1,484 @@ +#!/usr/bin/env python3 +"""Lift linked i386 ELF32 functions into native recomp C shards. + +This consumes the final AOT guest ELF, not object files: all ordinary ELF +relocations have already been resolved by the linker, while the synthetic +host-import section remains a separate executable section and is mapped to +the import labels in the adjacent manifest. Generated C retains the +XboxRecomp TLS register ABI (`void sub_VA(void)`). +""" +from __future__ import annotations + +import argparse +import hashlib +import json +import os +import pathlib +import struct +import sys +import time +import traceback + + +ROOT = pathlib.Path(__file__).resolve().parents[1] +BUILD = ROOT / "build/macos-aot" +DEFAULT_ELF = BUILD / "halo_guest.elf" +DEFAULT_OUTPUT = BUILD / "lifted" +DEFAULT_RECOMP = ROOT / "tools/dependencies/xboxrecomp-runtime-overlay" + + +def digest(path: pathlib.Path) -> str: + h = hashlib.sha256() + with path.open("rb") as f: + for block in iter(lambda: f.read(1024 * 1024), b""): + h.update(block) + return h.hexdigest() + + +def _cstring(blob: bytes, offset: int) -> str: + if offset < 0 or offset >= len(blob): + return "" + end = blob.find(b"\0", offset) + if end < 0: + return "" + return blob[offset:end].decode("utf-8", errors="replace") + + +def parse_elf32(path: pathlib.Path) -> dict: + data = path.read_bytes() + if len(data) < 52 or data[:4] != b"\x7fELF" or data[4:6] != b"\x01\x01": + raise ValueError("expected little-endian ELFCLASS32") + header = struct.unpack_from("<16sHHIIIIIHHHHHH", data, 0) + e_type, e_machine, entry = header[1], header[2], header[4] + shoff, shentsize, shnum, shstrndx = header[6], header[11], header[12], header[13] + if e_type != 2 or e_machine != 3: + raise ValueError(f"expected linked i386 ET_EXEC, got type={e_type} machine={e_machine}") + if shentsize < 40 or shoff + shentsize * shnum > len(data) or shstrndx >= shnum: + raise ValueError("invalid ELF section table") + raw_sections = [struct.unpack_from(" len(data): # not SHT_NOBITS + raise ValueError(f"section {name} extends beyond ELF bytes") + sections.append(section) + if name: + by_name[name] = section + text = by_name.get(".text") + symtab = by_name.get(".symtab") + strtab = sections[symtab["link"]] if symtab and symtab["link"] < len(sections) else None + if not text or not symtab or not strtab: + raise ValueError("linked ELF must contain .text, .symtab and its string table") + strings = data[strtab["offset"]:strtab["offset"] + strtab["size"]] + symbols = [] + entsize = symtab["entsize"] or 16 + if entsize < 16: + raise ValueError("invalid ELF32 symbol entry size") + for offset in range(symtab["offset"], symtab["offset"] + symtab["size"], entsize): + if offset + 16 > len(data): + break + name_off, value, size, info, other, section_index = struct.unpack_from("> 4, + "section_index": section_index}) + return {"data": data, "entry": entry, "sections": sections, "by_name": by_name, + "text": text, "symbols": symbols} + + +def safe_ident(name: str) -> str: + return name if name and name.replace("_", "a").isalnum() else "" + + +def load_runtime(recomp_root: pathlib.Path): + sys.path.insert(0, str(recomp_root)) + from tools.recomp import config + from tools.recomp.translator import FunctionTranslator, BatchTranslator + # Reject a previously imported overlay instead of hashing one path while + # executing another. This is particularly important in reused interpreters. + for name in ("tools.recomp.config", "tools.recomp.translator", "tools.recomp.lifter"): + module_path = pathlib.Path(sys.modules[name].__file__).resolve() + if not module_path.is_relative_to(recomp_root.resolve()): + raise RuntimeError(f"runtime module {name} loaded outside selected overlay: {module_path}") + return config, FunctionTranslator, BatchTranslator + + +def build_func_db(elf: dict, config) -> tuple[dict, dict, list[dict]]: + text = elf["text"] + text_index = text["index"] + text_end = text["addr"] + text["size"] + aliases: dict[int, list[dict]] = {} + zero_size_func_symbols: dict[int, list[dict]] = {} + for symbol in elf["symbols"]: + if symbol["type"] != 2 or symbol["section_index"] != text_index: + continue + if not symbol["size"]: + if text["addr"] <= symbol["value"] < text_end: + zero_size_func_symbols.setdefault(symbol["value"], []).append(symbol) + continue + start, end = symbol["value"], symbol["value"] + symbol["size"] + if start < text["addr"] or end > text_end or end <= start: + continue + aliases.setdefault(start, []).append(symbol) + func_db = {} + for start, syms in sorted(aliases.items()): + end = min(text_end, max(s["value"] + s["size"] for s in syms)) + func_db[start] = {"_addr": start, "start": f"0x{start:08X}", "end": end, + "size": end - start, "name": f"sub_{start:08X}", + "section": ".text", "elf_aliases": sorted( + {s["name"] for s in syms if s["name"]})} + + # Some static-archive assembly helpers are exported as STT_FUNC symbols + # with st_size == 0. Calls to those exact starts otherwise become + # unresolved direct calls even though the linked ELF carries a real symbol + # and disassemblable body (notably the C fenv helpers and setjmp aliases). + # Bound such a body by the next distinct STT_FUNC address in .text. This + # does not invent an interior entry: each start is explicit ELF metadata. + # The translator still stops control flow at RET/JMP; the bound only keeps + # decoding away from the next function. + all_starts = sorted(set(aliases) | set(zero_size_func_symbols)) + next_start = {start: all_starts[i + 1] if i + 1 < len(all_starts) else text_end + for i, start in enumerate(all_starts)} + for start, syms in sorted(zero_size_func_symbols.items()): + if start in func_db: + func_db[start]["elf_aliases"] = sorted(set(func_db[start]["elf_aliases"]) | + {s["name"] for s in syms if s["name"]}) + continue + end = next_start[start] + if end <= start: + continue + func_db[start] = {"_addr": start, "start": f"0x{start:08X}", "end": end, + "size": end - start, "name": f"sub_{start:08X}", + "section": ".text", "elf_aliases": sorted( + {s["name"] for s in syms if s["name"]}), + "inferred_zero_sized_symbol_end": True} + + imports = elf["by_name"].get(".host_imports") + label_db = {} + if imports: + for symbol in elf["symbols"]: + if (symbol["type"] == 2 and symbol["section_index"] == imports["index"] + and symbol["name"] and safe_ident(symbol["name"])): + label_db[symbol["value"]] = symbol["name"] + + sections = [] + for section in elf["sections"]: + if not section["name"] or not (section["flags"] & 0x2): # SHF_ALLOC + continue + is_code = bool(section["flags"] & 0x4) # SHF_EXECINSTR + raw_size = 0 if section["type"] == 8 else section["size"] + sections.append(config.Section(section["name"], section["addr"], section["size"], + section["offset"], raw_size, is_code)) + return func_db, label_db, sections + + +def tools_provenance(recomp_root: pathlib.Path) -> dict: + git = {} + try: + import subprocess + result = subprocess.run(["git", "-C", str(recomp_root), "rev-parse", "HEAD"], + text=True, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL) + if result.returncode == 0: + git["head"] = result.stdout.strip() + status = subprocess.run(["git", "-C", str(recomp_root), "status", "--short"], + text=True, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL) + git["status"] = status.stdout.splitlines() + except Exception as error: + git["error"] = str(error) + files = {} + for relative in ("tools/recomp/translator.py", "tools/recomp/lifter.py", + "tools/recomp/disasm.py", "tools/recomp/config.py", + "templates/runtime/recomp_types.h"): + path = recomp_root / relative + if path.is_file(): + files[relative] = digest(path) + overlay_manifest_path = recomp_root / "halo-overlay-manifest.json" + overlay_manifest = None + if overlay_manifest_path.is_file(): + overlay_manifest = json.loads(overlay_manifest_path.read_text()) + files["halo-overlay-manifest.json"] = digest(overlay_manifest_path) + return {"root": str(recomp_root), "git": git, "overlay_manifest": overlay_manifest, + "effective_sha256": files, + "loaded_modules": { + name: str(pathlib.Path(sys.modules[name].__file__).resolve()) + for name in ("tools.recomp.config", "tools.recomp.translator", + "tools.recomp.lifter", "tools.recomp.disasm") + if name in sys.modules}} + + +def emit_header(output: pathlib.Path, func_db: dict, label_db: dict, unresolved: dict, + imports: list[dict]): + names = {info["name"] for info in func_db.values()} + names.update(label_db.values()) + names.update(f"sub_{item['va']:08X}" for item in imports) + names.update(unresolved.values()) + lines = ["/* Generated from linked i386 guest symbols; do not edit. */", + "#ifndef HALO_MACOS_RECOMP_FUNCS_H", "#define HALO_MACOS_RECOMP_FUNCS_H", + '#include "recomp_types.h"', ""] + lines.extend(f"void {name}(void);" for name in sorted(names)) + lines.extend(["", "#endif", ""]) + (output / "recomp_funcs.h").write_text("\n".join(lines)) + + +def emit_image_symbols(output: pathlib.Path, elf: dict, elf_sha: str) -> dict: + """Write the runtime's exact ELF entry/thread/init-array metadata.""" + wanted = ("__guest_start", "__guest_thread_start", "__guest_thread_attach", "__errno_location") + values = {} + for name in wanted: + matches = [s["value"] for s in elf["symbols"] + if s["name"] == name and s["section_index"] == elf["text"]["index"]] + if len(set(matches)) != 1: + raise ValueError(f"expected one linked .text symbol for {name}, got {matches}") + values[name] = matches[0] + arrays = {} + for name in (".init_array", ".fini_array"): + section = elf["by_name"].get(name) + if section: + raw = elf["data"][section["offset"]:section["offset"] + section["size"]] + if len(raw) % 4: + raise ValueError(f"{name} byte length is not a multiple of pointer size") + arrays[name] = {"va": section["addr"], "bytes": section["size"], + "entries": list(struct.unpack("<" + "I" * (len(raw) // 4), raw))} + else: + arrays[name] = {"va": 0, "bytes": 0, "entries": []} + init, fini = arrays[".init_array"], arrays[".fini_array"] + lines = [ + "/* Generated from the hash-pinned linked Halo ELF. */", + "#ifndef HALO_RECOMP_IMAGE_SYMBOLS_H", + "#define HALO_RECOMP_IMAGE_SYMBOLS_H", + "#include ", + f'#define HALO_RECOMP_IMAGE_SHA256 "{elf_sha}"', + f"#define HALO_RECOMP_ENTRY_VA UINT32_C(0x{elf['entry']:08X})", + f"#define HALO_RECOMP_GUEST_START_VA UINT32_C(0x{values['__guest_start']:08X})", + f"#define HALO_RECOMP_THREAD_START_VA UINT32_C(0x{values['__guest_thread_start']:08X})", + f"#define HALO_RECOMP_THREAD_ATTACH_VA UINT32_C(0x{values['__guest_thread_attach']:08X})", + f"#define HALO_RECOMP_ERRNO_LOCATION_VA UINT32_C(0x{values['__errno_location']:08X})", + f"#define HALO_RECOMP_INIT_ARRAY_VA UINT32_C(0x{init['va']:08X})", + f"#define HALO_RECOMP_INIT_ARRAY_BYTES UINT32_C(0x{init['bytes']:X})", + f"#define HALO_RECOMP_INIT_ARRAY_COUNT UINT32_C({len(init['entries'])})", + f"#define HALO_RECOMP_FINI_ARRAY_VA UINT32_C(0x{fini['va']:08X})", + f"#define HALO_RECOMP_FINI_ARRAY_BYTES UINT32_C(0x{fini['bytes']:X})", + f"#define HALO_RECOMP_FINI_ARRAY_COUNT UINT32_C({len(fini['entries'])})", + "#endif", + "", + ] + (output / "recomp_image_symbols.h").write_text("\n".join(lines)) + return {"symbols": {"entry": elf["entry"], **values}, "init_array": init, + "fini_array": fini} + + +def main() -> int: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--elf", type=pathlib.Path, default=DEFAULT_ELF) + parser.add_argument("--output", type=pathlib.Path, default=DEFAULT_OUTPUT) + parser.add_argument("--recomp-root", type=pathlib.Path, default=DEFAULT_RECOMP) + parser.add_argument("--import-manifest", type=pathlib.Path, + default=BUILD / "host-import-manifest.json") + parser.add_argument("--expected-elf-sha256", default="", + help="required unless the import manifest pins the ELF hash") + parser.add_argument("--chunk-size", type=int, default=256) + parser.add_argument("--limit", type=int, default=0, help="lift only first N functions") + parser.add_argument("--address", action="append", default=[], + help="lift selected guest VA (repeatable; accepts 0x prefix)") + parser.add_argument("--discover", action="store_true", + help="run static callback/CFG recovery before lifting") + args = parser.parse_args() + if args.chunk_size < 1: + parser.error("--chunk-size must be positive") + elf_path = args.elf.resolve() + output = args.output.resolve() + output.mkdir(parents=True, exist_ok=True) + + elf = parse_elf32(elf_path) + elf_sha = digest(elf_path) + manifest = json.loads(args.import_manifest.read_text()) + expected_sha = args.expected_elf_sha256 or manifest.get("elf_sha256", "") + if not expected_sha: + raise SystemExit("import manifest has no elf_sha256; pass --expected-elf-sha256 for this frozen image") + if elf_sha.lower() != expected_sha.lower(): + raise SystemExit(f"ELF hash mismatch: expected {expected_sha}, got {elf_sha}") + config, FunctionTranslator, BatchTranslator = load_runtime(args.recomp_root.resolve()) + func_db, label_db, sections = build_func_db(elf, config) + import_section = elf["by_name"].get(".host_imports") + if not import_section: + raise SystemExit("linked ELF has no .host_imports section") + import_symbols = {s["name"]: s["value"] for s in elf["symbols"] + if s["type"] == 2 and s["section_index"] == import_section["index"] and s["name"]} + expected_imports = manifest.get("imports", []) + for item in expected_imports: + if import_symbols.get(item["name"]) != item["va"]: + raise SystemExit(f"import manifest mismatch for {item['name']}: " + f"ELF has {import_symbols.get(item['name'])!r}, manifest has {item['va']:#x}") + if len(import_symbols) != len(expected_imports): + raise SystemExit(f"import manifest has {len(expected_imports)} entries, " + f"ELF has {len(import_symbols)} import functions") + image_symbols = emit_image_symbols(output, elf, elf_sha) + config._install(sections, elf["entry"], 0, str(elf_path)) + translator = FunctionTranslator(elf["data"], func_db, label_db, + seh_prolog=0, seh_epilog=0, + setjmp_fn=0, longjmp_fn=0) + if args.discover: + translator.discover_static_indirect_targets() + translator.discover_cfg_ownership() + func_db = translator.func_db + + selected = sorted(func_db.items()) + if args.address: + wanted = {int(value, 0) for value in args.address} + missing = sorted(wanted - set(func_db)) + if missing: + raise SystemExit("requested addresses are not lifted ELF functions: " + + ", ".join(f"0x{x:08X}" for x in missing)) + selected = [item for item in selected if item[0] in wanted] + if args.limit: + selected = selected[:args.limit] + + types_src = args.recomp_root.resolve() / "templates/runtime/recomp_types.h" + types_text = types_src.read_text() + import_base = int(manifest["base"]) + import_stride = int(manifest["stride"]) + import_limit = import_base + len(expected_imports) * import_stride + old_icall_predicate = """#define RECOMP_ICALL_IS_CODE(_va) \\ + ((_va) >= 0xFE000000u || g_xbox_code_hi == 0u || \\ + ((_va) >= g_xbox_code_lo && (_va) < g_xbox_code_hi))""" + new_icall_predicate = f"""#define RECOMP_ICALL_IS_CODE(_va) \\ + ((_va) >= 0xFE000000u || \\ + ((_va) >= 0x{import_base:08X}u && (_va) < 0x{import_limit:08X}u) || \\ + g_xbox_code_hi == 0u || \\ + ((_va) >= g_xbox_code_lo && (_va) < g_xbox_code_hi))""" + if types_text.count(old_icall_predicate) != 1: + raise SystemExit("runtime template indirect-call predicate changed; refusing unsafe token-range patch") + types_text = types_text.replace(old_icall_predicate, new_icall_predicate) + (output / "recomp_types.h").write_text(types_text) + run_started = time.time() + bodies = [] + failed = [] + unsupported = [] + function_starts = sorted(func_db) + unimplemented_by_function = {address: start for start in function_starts + for address in ()} + del unimplemented_by_function + for index, (address, info) in enumerate(selected, 1): + before = {mnemonic: set(addrs) for mnemonic, addrs in translator.lifter.unimplemented.items()} + try: + code = translator.translate_function(address, info) + if not code: + raise RuntimeError("translator returned no body") + except Exception as error: + failed.append({"va": address, "name": info["name"], + "exception": f"{type(error).__name__}: {error}", + "traceback": traceback.format_exc(limit=5)}) + code = (f"void {info['name']}(void) {{ " + f"recomp_unsupported_instruction(0x{address:08X}u); }}") + newly_unimplemented = [] + for mnemonic, addrs in translator.lifter.unimplemented.items(): + old = before.get(mnemonic, set()) + newly_unimplemented.extend((mnemonic, pc) for pc in addrs if pc not in old) + if newly_unimplemented: + unsupported.append({"va": address, "mnemonics": [ + {"mnemonic": mnemonic, "instruction_va": pc} + for mnemonic, pc in sorted(newly_unimplemented, key=lambda x: x[1])]}) + bodies.append((address, info["name"], code)) + if index % 250 == 0 or index == len(selected): + print(f"lifted {index}/{len(selected)} functions", file=sys.stderr) + + # Write each shard once; all generated function calls have declarations in + # the shared header, even when the target is in a different shard. + for shard_index in range((len(bodies) + args.chunk_size - 1) // args.chunk_size): + chunk = bodies[shard_index * args.chunk_size:(shard_index + 1) * args.chunk_size] + lines = [f"/* Generated ELF translation shard {shard_index}: {len(chunk)} functions. */", + "#define RECOMP_GENERATED_CODE", '#include "recomp_funcs.h"', + "#include ", ""] + lines.extend(code for _, _, code in chunk) + (output / f"recomp_{shard_index:04d}.c").write_text("\n\n".join(lines) + "\n") + + defined = {name for _, name, _ in bodies} + import_names = set(label_db.values()) + unresolved = {address: name for address, name in translator.lifter.referenced_calls.items() + if name not in defined and name not in import_names} + stub_lines = ["/* Fail-fast stubs for direct calls absent from linked function symbols. */", + "#define RECOMP_GENERATED_CODE", '#include "recomp_funcs.h"', ""] + for address, name in sorted(unresolved.items()): + stub_lines.append(f"void {name}(void) {{ recomp_icall_fail_log(0x{address:08X}u); }}") + (output / "recomp_stubs_unresolved.c").write_text("\n".join(stub_lines) + "\n") + + # Named imports are called directly by translated guest code, while + # indirect calls arrive at their synthetic ELF VAs. Give both paths a + # typed `void(void)` adapter that dispatches the exact manifest token. + import_lines = ["/* Generated fail-fast ABI aliases for ELF host imports. */", + "#define RECOMP_GENERATED_CODE", '#include "recomp_funcs.h"', + '#include "guest_import_registry.h"', ""] + manual_items = [(address, name) for address, name, _ in bodies] + for item in expected_imports: + address, name = item["va"], item["name"] + import_lines.extend([ + f"void sub_{address:08X}(void) {{ mac_guest_import_dispatch_token(0x{address:08X}u); }}", + f"void {name}(void) {{ sub_{address:08X}(); }}", + ]) + manual_items.append((address, f"sub_{address:08X}")) + (output / "recomp_import_aliases.c").write_text("\n".join(import_lines) + "\n") + + manual_lines = ["/* Exact VA resolver for native/manual guest functions and imports. */", + '#include "recomp_funcs.h"', "#include ", "", + "typedef struct { uint32_t va; recomp_func_t func; } manual_entry;", + "static const manual_entry entries[] = {"] + manual_lines.extend(f" {{ 0x{address:08X}u, {name} }}," + for address, name in sorted(manual_items)) + manual_lines.extend(["};", "", "recomp_func_t recomp_lookup_manual(uint32_t xbox_va)", + "{", " size_t lo = 0, hi = sizeof(entries) / sizeof(entries[0]);", + " while (lo < hi) {", " size_t mid = lo + (hi - lo) / 2;", + " if (entries[mid].va < xbox_va) lo = mid + 1;", + " else if (entries[mid].va > xbox_va) hi = mid;", + " else return entries[mid].func;", " }", " return NULL;", "}", ""]) + (output / "recomp_manual_dispatch.c").write_text("\n".join(manual_lines)) + + # Use the translator's canonical sorted VA dispatch generator so native + # runtime lookup stays on the same convention as XboxRecomp outputs. + batch = object.__new__(BatchTranslator) + batch.translator = translator + batch.output_dir = str(output) + batch.title = "Halo AOT ELF" + batch._write_dispatch_table(bodies, str(output / "recomp_dispatch.c"), "recomp_funcs.h") + emit_header(output, func_db, label_db, unresolved, expected_imports) + + report = { + "format": 1, + "input_elf": str(elf_path), + "input_elf_sha256": elf_sha, + "expected_elf_sha256": expected_sha, + "entry": f"0x{elf['entry']:08X}", + "text": {"va": f"0x{elf['text']['addr']:08X}", "size": elf["text"]["size"]}, + "function_symbols_total": len(func_db), + "functions_selected": len(selected), + "functions_translated": len(bodies) - len(failed), + "functions_failed": failed, + "functions_with_unsupported_instructions": unsupported, + "unresolved_direct_targets": [ + {"va": f"0x{address:08X}", "name": name} + for address, name in sorted(unresolved.items())], + "imports": [{"va": f"0x{address:08X}", "name": name} + for address, name in sorted(label_db.items())], + "image_symbols": image_symbols, + "shards": (len(bodies) + args.chunk_size - 1) // args.chunk_size, + "chunk_size": args.chunk_size, + "elapsed_seconds": round(time.time() - run_started, 3), + "translator_provenance": tools_provenance(args.recomp_root.resolve()), + } + (output / "lift_report.json").write_text(json.dumps(report, indent=2) + "\n") + print(json.dumps({k: v for k, v in report.items() + if k not in ("functions_failed", "functions_with_unsupported_instructions", + "unresolved_direct_targets", "imports", "translator_provenance")}, + indent=2)) + print(f"output: {output}") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/tools/macos_network_test_lock.py b/tools/macos_network_test_lock.py new file mode 100644 index 0000000000..562101e073 --- /dev/null +++ b/tools/macos_network_test_lock.py @@ -0,0 +1,18 @@ +"""Serialize original local multiplayer test harnesses, without changing ports.""" +import fcntl +import os +from pathlib import Path +import tempfile + + +def acquire_network_test_lock(): + """Hold returned file open for the entire owned engine lifecycle.""" + path = Path(tempfile.gettempdir()) / f'halo-ce-network-test-{os.getuid()}.lock' + descriptor = os.open(path, os.O_CREAT | os.O_RDWR | os.O_NOFOLLOW, 0o600) + lease = os.fdopen(descriptor, 'r+') + try: + fcntl.flock(lease.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB) + except BlockingIOError: + lease.close() + raise RuntimeError('Another local Halo multiplayer test owns the shared endpoints. Wait for it to finish; serialize multiplayer map starts.') from None + return lease diff --git a/tools/macos_package_app.py b/tools/macos_package_app.py new file mode 100644 index 0000000000..5389646efb --- /dev/null +++ b/tools/macos_package_app.py @@ -0,0 +1,364 @@ +#!/usr/bin/env python3 +"""Package a verified ARM64 Halo engine as a relocatable local macOS app. + +Python is a build-time dependency only. The resulting app uses a native launcher +and bundles its ELF and third-party dylibs. Signing is local ad-hoc signing. +""" +from __future__ import annotations + +import argparse +import ctypes +import hashlib +import json +import os +from pathlib import Path +import plistlib +import shutil +import subprocess +import struct +import tempfile + +ROOT = Path(__file__).resolve().parents[1] +RESOURCES = ROOT / "port/macos/resources" +SYSTEM_PREFIXES = ("/usr/lib/", "/System/Library/") + + +def run(*args: str | Path, capture: bool = False) -> str: + result = subprocess.run([str(arg) for arg in args], check=True, + text=True, stdout=subprocess.PIPE if capture else None) + return result.stdout if capture else "" + + +def sha256(path: Path) -> str: + digest = hashlib.sha256() + with path.open("rb") as stream: + for block in iter(lambda: stream.read(1024 * 1024), b""): + digest.update(block) + return digest.hexdigest() + + +def dependencies(path: Path) -> list[str]: + return [line.strip().split(" (", 1)[0] + for line in run("otool", "-L", path, capture=True).splitlines()[1:]] + + +def rpaths(path: Path) -> list[str]: + lines = run("otool", "-l", path, capture=True).splitlines() + result = [] + for index, line in enumerate(lines): + if line.strip() == "cmd LC_RPATH": + result.append(lines[index + 2].strip().split(" (", 1)[0].removeprefix("path ")) + return result + + +def resolve_dependency(name: str, owner: Path, engine: Path) -> Path: + if name.startswith("@loader_path/"): + result = owner.parent / name.removeprefix("@loader_path/") + elif name.startswith("@executable_path/"): + result = engine.parent / name.removeprefix("@executable_path/") + elif name.startswith("@rpath/"): + result = None + for root in rpaths(owner) + rpaths(engine): + root = root.replace("@loader_path", str(owner.parent)).replace("@executable_path", str(engine.parent)) + candidate = Path(root) / name.removeprefix("@rpath/") + if candidate.exists(): + result = candidate + break + if result is None: + raise ValueError(f"Cannot resolve {name} referenced by {owner}") + else: + result = Path(name) + if not result.exists(): + raise ValueError(f"Missing library {name} referenced by {owner}") + return result.resolve() + + +def collect_libraries(engine: Path) -> tuple[dict[Path, list[tuple[str, Path]]], set[Path]]: + graph = {} + pending = [engine] + libraries = set() + while pending: + owner = pending.pop() + if owner in graph: + continue + refs = [] + for name in dependencies(owner): + if name.startswith(SYSTEM_PREFIXES): + continue + target = resolve_dependency(name, owner, engine) + # A dylib's LC_ID_DYLIB also appears in otool -L. + if target == owner: + continue + refs.append((name, target)) + libraries.add(target) + pending.append(target) + graph[owner] = refs + return graph, libraries + + +def version_tuple(value: str) -> tuple[int, ...]: + pieces = value.split(".") + if not pieces or not all(piece.isdigit() for piece in pieces): + raise ValueError(f"Invalid Mach-O minimum macOS version: {value}") + return tuple(map(int, pieces)) + (0,) * (3 - len(pieces)) + + +def minimum_macos(path: Path) -> str: + """Read the ARM64 slice, including older LC_VERSION_MIN_MACOSX headers.""" + command = None + versions = [] + for line in run("otool", "-arch", "arm64", "-l", path, capture=True).splitlines(): + fields = line.strip().split() + if len(fields) == 2 and fields[0] == "cmd": + command = fields[1] + elif command == "LC_BUILD_VERSION" and len(fields) == 2: + if fields[0] == "platform" and fields[1].lower() not in ("1", "macos"): + raise ValueError(f"Non-macOS ARM64 platform in {path}") + if fields[0] == "minos": + versions.append(fields[1]) + elif command == "LC_VERSION_MIN_MACOSX" and len(fields) == 2 and fields[0] == "version": + versions.append(fields[1]) + if not versions: + raise ValueError(f"Missing ARM64 macOS deployment load command in {path}") + return max(versions, key=version_tuple) + + +def install_assets(source: Path, destination: Path) -> Path: + """Copy original read-only content; never replace an existing installation.""" + maps = source / "maps" + movies = source / "bink" + if not movies.is_dir(): + movies = source / "bink-source" + if not maps.is_dir() or not movies.is_dir(): + raise ValueError("Asset source must contain maps and bink or bink-source") + if destination.exists(): + if not (destination / "maps").is_dir() or not (destination / "bink").is_dir(): + raise ValueError(f"Incomplete installed assets at {destination}; refusing to replace them") + # Confirm the existing installation matches without rewriting any files. + for src, dst in ((maps, destination / "maps"), (movies, destination / "bink")): + source_files = {p.relative_to(src) for p in src.rglob("*") if p.is_file()} + installed_files = {p.relative_to(dst) for p in dst.rglob("*") if p.is_file()} + if source_files != installed_files: + raise ValueError(f"Existing assets differ from requested source: {dst}") + for relative in source_files: + if sha256(src / relative) != sha256(dst / relative): + raise ValueError(f"Existing asset differs from requested source: {dst / relative}") + return destination + destination.parent.mkdir(parents=True, exist_ok=True) + staging = Path(tempfile.mkdtemp(prefix=".halo-assets-", dir=destination.parent)) + try: + for src, name in ((maps, "maps"), (movies, "bink")): + shutil.copytree(src, staging / name) + # Content remains read-only; the engine writes only to data/save roots. + for path in staging.rglob("*"): + path.chmod(0o555 if path.is_dir() else 0o444) + staging.chmod(0o555) + staging.rename(destination) + except BaseException: + for path in staging.rglob("*"): + if path.is_dir(): path.chmod(0o755) + staging.chmod(0o755) + shutil.rmtree(staging) + raise + return destination + + +REQUIRED_MAP_NAMES = set("a10 a30 a50 b30 b40 beavercreek bloodgulch boardingaction c10 c20 c40 carousel chillout d20 d40 damnation hangemhigh longest prisoner putput ratrace sidewinder ui wizard".split()) +REQUIRED_MOVIE_NAMES = set("attract1 attract2 attract3 credits intro".split()) + + +def clone_or_copy(source: Path, destination: Path) -> None: + """APFS clone is an independent regular file; fallback never creates hardlinks.""" + try: + system = ctypes.CDLL("/usr/lib/libSystem.B.dylib", use_errno=True) + clone = system.clonefile + clone.argtypes = (ctypes.c_char_p, ctypes.c_char_p, ctypes.c_int) + clone.restype = ctypes.c_int + if clone(os.fsencode(source), os.fsencode(destination), 0) == 0: + return + except (AttributeError, OSError): + pass + shutil.copy2(source, destination) + + +def bundle_assets(source: Path, destination: Path) -> dict: + """Seal engine-consumed disc data; keep config/debug/generated state outside.""" + destination.mkdir() + asset_records = [] + builds = set() + directories = [path for path in sorted(source.iterdir()) + if path.is_dir() and (path.name == "maps" or path.name.startswith("maps_"))] + movie_source = source / "bink" + if not movie_source.is_dir(): movie_source = source / "bink-source" + directories.append(movie_source) + for directory in directories: + if directory.is_symlink(): raise ValueError(f"Asset input directory must be regular: {directory.name}") + name = "bink" if directory == movie_source else directory.name + target = destination / name + target.mkdir() + extension = ".bik" if name == "bink" else ".map" + for original in sorted(directory.glob("*" + extension)): + if not original.is_file() or original.is_symlink(): + raise ValueError(f"Asset input must be a regular file: {original.name}") + with original.open("rb") as stream: header = stream.read(2048) + record = {"path": name + "/" + original.name, "bytes": original.stat().st_size} + if extension == ".map": + if len(header) < 2048 or header[:4] != b"daeh" or struct.unpack_from(" dict: + binary, elf, stamp_path, output, assets = [path.expanduser().resolve() + for path in (args.binary, args.elf, args.stamp, args.output, args.assets)] + stamp = json.loads(stamp_path.read_text()) + for name, path in (("binary", binary), ("elf", elf)): + if stamp.get(name + "_sha256") != sha256(path): + raise ValueError(f"{name} does not match the verified input stamp") + if "arm64" not in run("lipo", "-archs", binary, capture=True).split(): + raise ValueError("This package requires a native ARM64 engine") + if output.exists() and not args.replace: + raise ValueError(f"Output exists; use --replace to rebuild {output}") + if args.install_assets: + assets = install_assets(assets, args.install_assets.expanduser().resolve()) + movies = assets / "bink" if (assets / "bink").is_dir() else assets / "bink-source" + if not (assets / "maps").is_dir() or not movies.is_dir(): + raise ValueError("Installed original maps or movies are missing") + graph, libraries = collect_libraries(binary) + names = {} + for library in sorted(libraries): + if "arm64" not in run("lipo", "-archs", library, capture=True).split(): + raise ValueError(f"Library lacks ARM64 support: {library}") + if library.name in names and names[library.name] != library: + raise ValueError(f"Dylib basename collision: {library}") + names[library.name] = library + output.parent.mkdir(parents=True, exist_ok=True) + staging_parent = Path(tempfile.mkdtemp(prefix=".halo-app-", dir=output.parent)) + app = staging_parent / output.name + try: + macos = app / "Contents/MacOS" + resources = app / "Contents/Resources" + frameworks = app / "Contents/Frameworks" + for directory in (macos, resources, frameworks): directory.mkdir(parents=True) + asset_manifest = bundle_assets(assets, resources / "GameData") + engine = macos / "halo_engine" + shutil.copy2(binary, engine) + shutil.copy2(elf, resources / "halo_guest.elf") + # A rebuild may replace input files while a package is being staged. + # Verify the copied bytes before relocating or signing anything. + for name, copied in (("binary", engine), ("elf", resources / "halo_guest.elf")): + if sha256(copied) != stamp.get(name + "_sha256"): + raise ValueError(f"{name} changed while packaging; retry with its matching input stamp") + shutil.copy2(stamp_path, resources / "verified-startup-input.json") + shutil.copy2(RESOURCES / "HaloCombatEvolved.icns", resources / "HaloCombatEvolved.icns") + shutil.copy2(RESOURCES / "master-chief-mark-v-icon.prompt.txt", resources / "icon-generation-prompt.txt") + shutil.copy2(RESOURCES / "starter-config.toml", resources / "starter-config.toml") + destinations = {binary: engine} + for library in sorted(libraries): + destinations[library] = frameworks / library.name + shutil.copy2(library, destinations[library]) + for original, packaged in destinations.items(): + packaged.chmod(0o755) + # Rewrite signed Mach-O files first, then replace their signatures. + # Removing signatures beforehand leaves a LINKEDIT layout that older + # Xcode install_name_tool versions reject. + for old, dependency in graph[original]: + run("install_name_tool", "-change", old, "@rpath/" + dependency.name, packaged) + if original != binary: + run("install_name_tool", "-id", "@rpath/" + original.name, packaged) + for old in rpaths(packaged): + if old.startswith("/"): + run("install_name_tool", "-delete_rpath", old, packaged) + desired_rpath = "@executable_path/../Frameworks" if original == binary else "@loader_path" + if desired_rpath not in rpaths(packaged): + run("install_name_tool", "-add_rpath", desired_rpath, packaged) + run("codesign", "--force", "--sign", "-", packaged) + minimum_components = {"native_icon_api": "14.0", "engine": minimum_macos(binary)} + minimum_components.update({path.name: minimum_macos(path) for path in sorted(libraries)}) + minos = max(minimum_components.values(), key=version_tuple) + launcher = macos / "Halo Combat Evolved" + run("xcrun", "clang", "-O2", "-Wall", "-Wextra", "-Werror", "-arch", "arm64", + "-mmacosx-version-min=" + minos, RESOURCES / "macos_app_launcher.m", + "-framework", "AppKit", "-o", launcher) + manifest = {"format":1, "binary_sha256":sha256(engine), "elf_sha256":sha256(resources / "halo_guest.elf"), + "assets_relative":"GameData", "asset_manifest_sha256":sha256(resources / "GameData/asset-manifest.json"), + "asset_count":asset_manifest["asset_count"], "asset_bytes":asset_manifest["bytes"], + "minimum_macos":minos, "minimum_macos_components":minimum_components, "input_stamp":stamp, + "bundled_dylibs":{path.name:sha256(destinations[path]) for path in sorted(libraries)}, + "signing":"ad-hoc", "native_launcher":True} + (resources / "launch-config.json").write_text(json.dumps(manifest, indent=2) + "\n") + plist = {"CFBundleDisplayName":"Halo: Combat Evolved", "CFBundleName":"Halo: Combat Evolved", + "CFBundleExecutable":"Halo Combat Evolved", "CFBundleIconFile":"HaloCombatEvolved.icns", + "CFBundleIdentifier":"local.halo.native-aot", "CFBundlePackageType":"APPL", + "CFBundleVersion":"0.2", "CFBundleShortVersionString":"0.2", + "LSMinimumSystemVersion":minos, "NSHighResolutionCapable":True} + with (app / "Contents/Info.plist").open("wb") as stream: plistlib.dump(plist, stream) + run("codesign", "--force", "--sign", "-", app) + run("codesign", "--verify", "--deep", "--strict", app) + for packaged in destinations.values(): + bad = [name for name in dependencies(packaged) + if name.startswith("/") and not name.startswith(SYSTEM_PREFIXES)] + if bad: raise ValueError(f"External library references remain in {packaged}: {bad}") + # This validates paths and hashes and explicitly never launches the game. + check = json.loads(run(launcher, "--check", capture=True)) + if not check.get("verified") or check.get("launches_game"): + raise ValueError("Native no-launch check did not verify the package") + backup = output.with_name(output.name + ".previous") + if output.exists(): + if backup.exists(): raise ValueError(f"Preserved backup exists: {backup}; refusing to overwrite it") + output.rename(backup) + app.rename(output) + return {"app":str(output), "engine_sha256":manifest["binary_sha256"], + "elf_sha256":manifest["elf_sha256"], "assets_bundled":True, "asset_count":asset_manifest["asset_count"], "asset_bytes":asset_manifest["bytes"], + "bundled_dylibs":len(libraries), "minimum_macos":minos, + "codesign_verified":True, "game_launched":False} + finally: + shutil.rmtree(staging_parent) + + +def main() -> None: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--binary", required=True, type=Path) + parser.add_argument("--elf", required=True, type=Path) + parser.add_argument("--stamp", required=True, type=Path) + parser.add_argument("--assets", required=True, type=Path) + parser.add_argument("--output", required=True, type=Path) + parser.add_argument("--install-assets", type=Path, help="Copy only maps and movies to a stable new installation; preserve originals") + parser.add_argument("--replace", action="store_true", help="Preserve existing output as .app.previous before replacing it") + args = parser.parse_args() + try: + print(json.dumps(build(args), indent=2)) + except (OSError, ValueError, subprocess.CalledProcessError) as error: + parser.exit(2, f"Packaging failed: {error}\n") + + +if __name__ == "__main__": + main() diff --git a/tools/run_macos_aot_bounded.py b/tools/run_macos_aot_bounded.py new file mode 100755 index 0000000000..4b7728118c --- /dev/null +++ b/tools/run_macos_aot_bounded.py @@ -0,0 +1,194 @@ +#!/usr/bin/env python3 +"""Run the native AOT guest with durable logs and process-group cleanup.""" + +from __future__ import annotations + +import argparse +import datetime as dt +import hashlib +import json +import os +import re +import signal +import subprocess +import sys +import threading +import time +from pathlib import Path + + +ROOT = Path(__file__).resolve().parents[1] + + +def cleanup_group(process: subprocess.Popen, grace_seconds: float = 2.0) -> bool: + group_id = process.pid + def group_exists() -> bool: + # Reap our child before probing its group; Darwin can reject a probe + # while the terminating child is still an unreaped process. + process.poll() + try: + os.killpg(group_id, 0) + return True + except ProcessLookupError: + return False + except PermissionError: + groups = subprocess.check_output(["ps", "-axo", "pgid="], text=True) + return any(int(value) == group_id for value in groups.split()) + if not group_exists(): + return True + try: + os.killpg(group_id, signal.SIGTERM) + except ProcessLookupError: + return True + except PermissionError: + return not group_exists() + deadline = time.monotonic() + grace_seconds + while time.monotonic() < deadline: + if not group_exists(): + return True + time.sleep(0.05) + try: + os.killpg(group_id, signal.SIGKILL) + except ProcessLookupError: + return True + except PermissionError: + return not group_exists() + deadline = time.monotonic() + 1.0 + while time.monotonic() < deadline: + if not group_exists(): + return True + time.sleep(0.05) + return False + + +def main() -> int: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--timeout", type=float, default=45.0) + parser.add_argument("--binary", type=Path, default=ROOT / "build/native/halo_macos_aot") + parser.add_argument("--elf", type=Path, default=ROOT / "build/macos-aot/halo_guest.elf") + parser.add_argument("--data-root", type=Path, required=True) + parser.add_argument("--run-id") + parser.add_argument("--native-app", type=Path, help="run the packaged native launcher with an external owned deadline") + parser.add_argument("--sample-at", type=float, + help="sample the owned game process once at this elapsed second") + args = parser.parse_args() + if args.native_app: + args.binary = args.native_app / "Contents/MacOS/halo_engine" + args.elf = args.native_app / "Contents/Resources/halo_guest.elf" + if args.timeout <= 0: + parser.error("--timeout must be positive") + if args.sample_at is not None and not 0 < args.sample_at < args.timeout: + parser.error("--sample-at must fall within the bounded run") + run_id = args.run_id or dt.datetime.now().strftime("%Y%m%d-%H%M%S") + if not re.fullmatch(r"[A-Za-z0-9_-]+", run_id): parser.error("invalid run-id") + logs = ROOT / "build/macos-aot/run-logs" + run_root = ROOT / "build/macos-aot/run-data" / run_id + if run_root.exists() or (logs / f"{run_id}.json").exists(): parser.error("run-id exists; choose a new run-id to preserve its state/logs") + save_root = run_root / "save" + data_root = run_root / "data" + data_source = args.data_root.resolve() + logs.mkdir(parents=True, exist_ok=True) + save_root.mkdir(parents=True, exist_ok=True) + data_root.mkdir(parents=True, exist_ok=True) + if not (data_source / "maps").is_dir(): + parser.error(f"--data-root must contain a maps/ directory: {data_source}") + for source in data_source.iterdir(): + if source.name == "config.toml": + continue + target = data_root / source.name + if target.exists() or target.is_symlink(): + continue + target.symlink_to(source.resolve(), target_is_directory=source.is_dir()) + log_path = logs / f"{run_id}.log" + status_path = logs / f"{run_id}.json" + env = os.environ.copy() + env.update({ + "HALO_GUEST_ELF": str(args.elf.resolve()), + "HALO_DATA_ROOT": str(data_root.resolve()), + "HALO_SAVE_ROOT": str(save_root.resolve()), + "HALO_PROJECT_ROOT": str(ROOT), + "HALO_CONFIG_ROOT": str(save_root.resolve()), + }) + for test_variable in ("HALO_TEST_START_AT_FRAME", "HALO_TEST_KEY_SEQUENCE", + "HALO_TEST_DISABLE_CULL", "HALO_TEST_INVERT_FRONTFACE"): + env.pop(test_variable, None) + started = time.monotonic() + status: dict[str, object] = { + "run_id": run_id, + "command": [str(args.binary.resolve())], + "elf": str(args.elf.resolve()), + "elf_sha256": hashlib.sha256(args.elf.read_bytes()).hexdigest(), + "binary_sha256": hashlib.sha256(args.binary.read_bytes()).hexdigest(), + "data_root": str(data_root.resolve()), + "data_source": str(data_source), + "save_root": str(save_root.resolve()), + "log": str(log_path), + "timeout_seconds": args.timeout, + } + if env.get("HALO_CAPTURE_FRAME_DIR"): + Path(env["HALO_CAPTURE_FRAME_DIR"]).mkdir(parents=True, exist_ok=True) + status["capture_frame_dir"] = env["HALO_CAPTURE_FRAME_DIR"] + if args.native_app: + log_path = run_root / "halo.log" + status["log"] = str(log_path) + status["app"] = str(args.native_app.resolve()) + status["command"] = [str((args.native_app / "Contents/MacOS/Halo Combat Evolved").resolve()), + "--state-root", str(run_root)] + if any(key.startswith(("HALO_CAPTURE_", "HALO_SDL_TEST_")) or key == "HALO_FRAME_METRICS" for key in env): + status["command"].append("--diagnostic") + with log_path.open("wb") as log: + process = subprocess.Popen(status["command"], cwd=ROOT, + stdin=subprocess.DEVNULL, stdout=log, stderr=subprocess.STDOUT, + env=env, start_new_session=True) + status["pid"] = process.pid + status["pgid"] = process.pid + stop_sample = threading.Event() + sample_thread = None + if args.sample_at is not None: + def take_sample() -> None: + if stop_sample.wait(args.sample_at) or process.poll() is not None: + return + path = logs / f"{run_id}.sample.txt" + status["sample"] = str(path) + status["sample_started_seconds"] = round(time.monotonic() - started, 3) + try: + result = subprocess.run(["/usr/bin/sample", str(process.pid), "1", "-file", str(path)], + stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, timeout=15) + status["sample_returncode"] = result.returncode + if result.returncode: + status["sample_error"] = result.stdout.strip() + except subprocess.TimeoutExpired: + status["sample_error"] = "owned-process sample exceeded 15 seconds" + sample_thread = threading.Thread(target=take_sample, daemon=True) + sample_thread.start() + try: + try: + code = process.wait(timeout=args.timeout) + status["returncode"] = code + status["timed_out"] = False + # The guest is allowed to create helper processes; the supervisor + # owns this new process group and cleans survivors on all exits. + status["process_group_clean"] = cleanup_group(process) + except subprocess.TimeoutExpired: + status["timed_out"] = True + status["process_group_clean"] = cleanup_group(process) + process.wait() + status["returncode"] = process.returncode + status["supervisor_code"] = 124 + except KeyboardInterrupt: + status["interrupted"] = True + status["process_group_clean"] = cleanup_group(process) + process.wait() + status["returncode"] = process.returncode + status["supervisor_code"] = 130 + stop_sample.set() + if sample_thread is not None: + sample_thread.join(timeout=16) + status["elapsed_seconds"] = round(time.monotonic() - started, 3) + status_path.write_text(json.dumps(status, indent=2) + "\n") + print(json.dumps(status, indent=2)) + return int(status.get("supervisor_code", status.get("returncode", 1))) + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tools/run_macos_multiplayer_pair.py b/tools/run_macos_multiplayer_pair.py new file mode 100644 index 0000000000..4076beb685 --- /dev/null +++ b/tools/run_macos_multiplayer_pair.py @@ -0,0 +1,93 @@ +#!/usr/bin/env python3 +"""Bound two actual protocol-4 engines on one Mac using an offline transport alias. + +No packet payload is synthesized or changed. Requires the original multiplayer +map data, a matching native binary/ELF and a real configured non-loopback local +IPv4 address. Logs distinguish socket transport from actual two-player gameplay. +""" +from pathlib import Path +import argparse, datetime, ipaddress, json, math, os, re, signal, socket, subprocess, sys, time +from macos_network_test_lock import acquire_network_test_lock +ROOT=Path(__file__).resolve().parents[1] +NETWORK_PORTS=(0x141e,0x141f) # Upstream protocol's server/client ports, 5150/5151. +def check_endpoints(addresses,ports=NETWORK_PORTS): + # TCP bind+listen with REUSEADDR matches the engine and permits only closed + # TIME_WAIT connections. Active listeners still fail listen; UDP never reuses. + # The shared lease excludes other owned harnesses during this preflight. + for address in addresses: + for port in ports: + for kind in (socket.SOCK_STREAM,socket.SOCK_DGRAM): + with socket.socket(socket.AF_INET,kind) as probe: + try: + if kind == socket.SOCK_STREAM:probe.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) + probe.bind((address,port)) + if kind == socket.SOCK_STREAM:probe.listen(1) + except OSError as error:raise RuntimeError(f'occupied multiplayer endpoint {address}:{port} {"TCP" if kind==socket.SOCK_STREAM else "UDP"}: {error}') from error +def evidence(log): + text=log.read_text(errors='replace') if log.exists() else '' + ticks=[line for line in text.splitlines() if 'network test: tick ' in line and ' | playing | ' in line] + two=[line for line in ticks if len(re.findall(r' player \d+: \(',line))>=2 and len(set(re.findall(r' m(\d+)',line)))>=2] + received=[int(x) for line in two for x in re.findall(r' received (\d+)',line)] + return {'hosting':'network test: hosting ' in text,'joining':'network test: joining' in text, + 'starting':'network test: starting the game' in text,'playing_samples':len(ticks), + 'two_live_players_distinct_machines':len(two),'received_packets':max(received,default=0), + 'player_position_sets':len(set(tuple(re.findall(r' player \d+: \(([^)]*)\)',line)) for line in two)), + 'last_two_player_sample':two[-1] if two else None} +def main(): + p=argparse.ArgumentParser(description=__doc__);p.add_argument('--binary',type=Path,required=True);p.add_argument('--elf',type=Path,required=True);p.add_argument('--data-root',type=Path,required=True);p.add_argument('--host-ip',required=True) + p.add_argument('--timeout',type=float,default=150);p.add_argument('--start-delay',type=float,default=25);p.add_argument('--run-id');p.add_argument('--map',default='bloodgulch');p.add_argument('--observer-role',choices=('host','client'),help='Disable scripted input on this role to isolate the opposite replication direction');p.add_argument('--input-mode',choices=('move','bot'),default='move',help='Explicit diagnostic scripted input; move disables fire/grenade/jump');p.add_argument('--resolution',choices=('640x480','1280x720','1920x1080'),default='1280x720');p.add_argument('--map-evidence',action='store_true',help='Capture requested-map state and two rendered frames per role for map-load review');p.add_argument('--check-only',action='store_true');a=p.parse_args() + host=ipaddress.IPv4Address(a.host_ip) + if host.is_loopback or host.is_multicast or host.is_unspecified:p.error('host-ip must be an existing non-loopback local IPv4 address') + if not math.isfinite(a.timeout) or not math.isfinite(a.start_delay) or a.timeout<30 or not 15<=a.start_delay=30 and start-delay >=15 within the bound') + if not re.fullmatch(r'[a-z0-9_]+',a.map):p.error('invalid map name') + for path in (a.binary,a.elf): + if not path.is_file():p.error(f'missing {path}') + if not (a.data_root/'maps'/f'{a.map}.map').is_file():p.error('original multiplayer map is missing') + # A local bind probe neither sends traffic nor creates an interface alias. + with socket.socket(socket.AF_INET,socket.SOCK_DGRAM) as probe: + try:probe.bind((str(host),0)) + except OSError as e:p.error(f'host-ip is not locally bindable: {e}') + rid=a.run_id or 'mp-pair-'+datetime.datetime.now().strftime('%Y%m%d-%H%M%S') + if not re.fullmatch(r'[A-Za-z0-9_-]+',rid):p.error('invalid run-id') + common={k:v for k,v in os.environ.items() if not k.startswith('HALO_')} + common.update(HALO_LOCAL_TEST_IPV4_ALIAS='127.0.0.2',HALO_NET_ONLINE='false',HALO_NET_ALLOW_UPNP='false',HALO_NETCODE='distributed',HALO_AUDIO_ENABLE=os.environ.get('HALO_AUDIO_ENABLE','1'),HALO_FULLSCREEN='false',HALO_WINDOW_SCALE='1',HALO_RESOLUTION=a.resolution,HALO_NETWORK_TEST_START=str(a.start_delay),HALO_NETWORK_TRACE='1') + runs={};processes=[] + for role in ('host','client'): + env=common.copy();env.update(HALO_SDL_TEST_ROLE=role,HALO_NET_ADDRESS=str(host) if role=='host' else '127.0.0.2',HALO_NET_BROADCAST='127.0.0.2' if role=='host' else str(host),HALO_NETWORK_TEST=f'host:{a.map}' if role=='host' else 'join',HALO_TEST_INPUT=a.input_mode+(':0' if role=='host' else ':1')) + if role == a.observer_role: + env['HALO_TEST_INPUT']='' + if a.map_evidence: + capture_times=','.join(str(int((a.timeout-offset)*1000)) for offset in (20,10)) + env.update(HALO_SDL_TEST_WINDOW_POSITION='50,100' if role=='host' else '800,100',HALO_SDL_SWAP_TRACE='1',HALO_INPUT_TRACE='1',HALO_MAP_EVIDENCE='1',HALO_GL_DEBUG='1',HALO_CAPTURE_FRAME_AT_MS=capture_times,HALO_CAPTURE_FRAME_DIR=str(ROOT/'build/macos-aot/run-logs'/(rid+'-'+role+'-frames')),HALO_SDL_TEST_QUIT_AT_MS=str(int((a.timeout-3)*1000))) + cmd=[sys.executable,str(ROOT/'tools/run_macos_aot_bounded.py'),'--binary',str(a.binary.resolve()),'--elf',str(a.elf.resolve()),'--data-root',str(a.data_root.resolve()),'--timeout',str(a.timeout),'--run-id',rid+'-'+role] + runs[role]={'command':cmd,'settings':{k:v for k,v in env.items() if k.startswith('HALO_')}} + if a.check_only:print(json.dumps(runs,indent=2));return 0 + try: + pair_lock=acquire_network_test_lock() + check_endpoints((str(host),'127.0.0.1')) + except RuntimeError as error:p.error(str(error)) + logs=ROOT/'build/macos-aot/run-logs';logs.mkdir(parents=True,exist_ok=True) + for role in runs: + if (logs/(rid+'-'+role+'.json')).exists():p.error('run-id already exists; choose a new ID') + try: + for role,run in runs.items(): + env=common.copy();env.update(run['settings']);out=(logs/(rid+'-'+role+'-supervisor.log')).open('wb') + child=subprocess.Popen(run['command'],env=env,stdout=out,stderr=subprocess.STDOUT,start_new_session=True);processes.append((child,out)) + deadline=time.monotonic()+a.timeout+12 + while any(child.poll() is None for child,_ in processes): + if time.monotonic()>deadline:raise TimeoutError('pair supervisors exceeded their bounds') + time.sleep(.25) + finally: + for child,out in processes: + if child.poll() is None: + child.send_signal(signal.SIGINT) # bounded child owns and cleans its native PGID + try:child.wait(timeout=8) + except subprocess.TimeoutExpired:child.kill();child.wait() + out.close() + result={'run_id':rid,'input_mode':a.input_mode,'observer_role':a.observer_role,'resolution':a.resolution,'transport':'explicit offline logical127.0.0.2/native127.0.0.1 alias; original protocol payload unchanged','roles':{}} + for role in runs: + status_path=logs/(rid+'-'+role+'.json');status=json.loads(status_path.read_text()) if status_path.exists() else {} + result['roles'][role]={'status':status,'evidence':evidence(Path(status.get('log',logs/(rid+'-'+role+'.log'))))} + result['passed']=all(r['status'].get('process_group_clean') and (r['status'].get('returncode')==0 or (r['status'].get('timed_out') and r['status'].get('returncode')==-signal.SIGTERM)) and r['evidence']['two_live_players_distinct_machines']>=3 and r['evidence']['received_packets']>0 and r['evidence']['player_position_sets']>=2 for r in result['roles'].values()) and result['roles']['client']['evidence']['joining'] and result['roles']['host']['evidence']['hosting'] + target=logs/(rid+'-pair.json');target.write_text(json.dumps(result,indent=2)+'\n');print(json.dumps(result,indent=2));return 0 if result['passed'] else 1 +if __name__=='__main__':raise SystemExit(main()) diff --git a/tools/test_macos_display_cycle.py b/tools/test_macos_display_cycle.py new file mode 100644 index 0000000000..09a0e69989 --- /dev/null +++ b/tools/test_macos_display_cycle.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 +"""Exercise actual preset cycle and persistent config writer, with Retina window stubs.""" +from pathlib import Path +import subprocess +from test_macos_hd_resolution import function +ROOT=Path(__file__).resolve().parents[1] +def main(): + out=ROOT/'build/macos-aot/display-cycle-regression';out.mkdir(exist_ok=True) + config=(ROOT/'port/linux/src/port_config.c').read_text().replace('#include "platform.h"','static void platform_log(const char *format, ...) { (void)format; }') + platform=(ROOT/'port/linux/src/sdl_platform.c').read_text() + prefix='#define HALO_MACOS 1\n#include \n#include \n' + cycle=r''' +static SDL_Window *platform_window; +static int point_width=640,point_height=360; +static bool cycle_get_size(SDL_Window *w,int *x,int *y) { (void)w;*x=point_width;*y=point_height;return true; } +static bool cycle_get_pixels(SDL_Window *w,int *x,int *y) { (void)w;*x=point_width*2;*y=point_height*2;return true; } +static bool cycle_set_size(SDL_Window *w,int x,int y) { (void)w;point_width=x;point_height=y;return true; } +#define SDL_GetWindowSize cycle_get_size +#define SDL_GetWindowSizeInPixels cycle_get_pixels +#define SDL_SetWindowSize cycle_set_size +'''+function(platform,'platform_cycle_render_resolution')+r''' +int main(void) { + char directory[]="/tmp/halo-display-cycle-XXXXXX",path[1024];assert(mkdtemp(directory)); + setenv("HALO_CONFIG_ROOT",directory,1);unsetenv("HALO_RESOLUTION"); + snprintf(path,sizeof(path),"%s/config.toml",directory); + assert(SDL_SaveFile(path,"# keep my comment\n[display]\nresolution = \"1280x720\"\nfullscreen = false\n[audio]\nvolume = 0.4\n",99)); + assert(!strcmp(config_string("display.resolution"),"1280x720")); + const char *values[]={"1920x1080","640x480","1280x720"}; + const int widths[]={960,320,640},heights[]={540,240,360}; + for(int i=0;i<3;i++) { + platform_cycle_render_resolution();assert(!strcmp(config_string("display.resolution"),values[i])); + assert(point_width==widths[i]&&point_height==heights[i]); + size_t size;char *text=config_read_file(path,&size);assert(text); + assert(strstr(text,"# keep my comment")&&strstr(text,"volume = 0.4")); + toml_result_t parsed=toml_parse(text,(int)size);assert(parsed.ok); + toml_datum_t value=toml_seek(parsed.toptab,"display.resolution");assert(value.type==TOML_STRING&&!strcmp(value.u.s,values[i])); + toml_free(parsed);free(text); + } + assert(!config_write_string("display.resolution","bad\"value")); + assert(!strcmp(config_string("display.resolution"),"1280x720")); + assert(config_write_boolean("display.fullscreen",1));assert(config_boolean("display.fullscreen")); + assert(!strcmp(config_string("display.resolution"),"1280x720")); + unlink(path);rmdir(directory);puts("Display cycle: exact Retina presets, persistent TOML/comments/unrelated settings, invalid strings and boolean compatibility PASS"); +} +''' + # Preserve the exact literal size without relying on hand-counted characters. + cycle=cycle.replace(',99));',',strlen("# keep my comment\\n[display]\\nresolution = \\\"1280x720\\\"\\nfullscreen = false\\n[audio]\\nvolume = 0.4\\n")));') + (out/'fixture.c').write_text(prefix+config+cycle) + subprocess.run(['clang','-Wall','-Wextra','-Werror','-I'+str(ROOT/'port/linux/src'),'-I'+str(ROOT/'port/third_party/tomlc17'),'-I/opt/homebrew/include',str(out/'fixture.c'),str(ROOT/'port/third_party/tomlc17/tomlc17.c'),'-L/opt/homebrew/lib','-lSDL3','-o',str(out/'fixture')],check=True) + subprocess.run([str(out/'fixture')],check=True) +if __name__=='__main__':main() diff --git a/tools/test_macos_gamepad.py b/tools/test_macos_gamepad.py new file mode 100644 index 0000000000..c7fd25921c --- /dev/null +++ b/tools/test_macos_gamepad.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python3 +"""Actual SDL virtual Xbox/DualSense → native tokens → source XInput mapping. +No game is launched; virtual devices exercise mappings without physical hardware. +""" +from pathlib import Path +import argparse, re, subprocess, tempfile, shlex +from test_macos_ui_condition import function_body +ROOT=Path(__file__).resolve().parents[1] +def main(): + p=argparse.ArgumentParser();p.parse_args() + src=ROOT/'port/linux/src/xinput_sdl.c' + host=ROOT/'port/macos/host_sdl.c' + text=src.read_text(); defs='\n'.join(x for x in (ROOT/'port/include/xdk/xdk_xbox.h').read_text().splitlines() if x.startswith('#define XINPUT_GAMEPAD_') or x.startswith('#define XDEVICE_PORT')) + pdb=(ROOT/'port/include/xdk/xdk_pdb.h').read_text(); struct=re.search(r'struct _XINPUT_GAMEPAD \{.*?\};',pdb,re.S).group() + unit='''#include +#include +#include +#include +#include +#include +#include +#include "host_sdl.h" +#include "guest_address.h" +#define HALO_MACOS 1 +#define PORT_COUNT 4 +#define TRUE 1 +#define FALSE 0 +#define SHORT_MAX 32767 +#define SHORT_MIN (-32768) +typedef int BOOL; typedef uint8_t BYTE; typedef uint16_t WORD; typedef int16_t SHORT; typedef uint32_t DWORD; +void mac_host_log(int p,const char *s){(void)p;(void)s;} +void mac_host_logf(int p,const char *s,...){(void)p;(void)s;} +void mac_host_apply_app_icon(void){} void mac_host_activate_app(void){} +void mac_game_evidence_presented(void){} +void mac_host_bink_presented(void){} void mac_host_memory_fingerprint_presented(void){} +'''+defs+'\n'+struct+'\ntypedef struct _XINPUT_GAMEPAD XINPUT_GAMEPAD;\n' + unit+='''static unsigned enumeration_calls; +static SDL_JoystickID *fixture_gamepads(int *n){enumeration_calls++;return SDL_GetGamepads(n);} +#define SDL_GetGamepads fixture_gamepads +static SDL_Gamepad *guest_from(SDL_JoystickID id){return (SDL_Gamepad *)(uintptr_t)mac_host_sdl_gamepad_from_id(id);} +static SDL_Gamepad *guest_open(SDL_JoystickID id){return (SDL_Gamepad *)(uintptr_t)mac_host_sdl_open_gamepad(id);} +static Sint16 guest_axis(SDL_Gamepad *p,SDL_GamepadAxis a){return mac_host_sdl_gamepad_axis((uint32_t)(uintptr_t)p,a);} +static bool guest_button(SDL_Gamepad *p,SDL_GamepadButton b){return mac_host_sdl_gamepad_button((uint32_t)(uintptr_t)p,b);} +#define SDL_GetGamepadFromID guest_from +#define SDL_OpenGamepad guest_open +#define SDL_GetGamepadAxis guest_axis +#define SDL_GetGamepadButton guest_button +''' + if 'gamepad_cache_lock' in text: + start=text.index('#ifdef HALO_MACOS\n/* Four fixed player slots.');end=text.index('static int sdl_gamepads',start);unit+='\n#define HALO_GAMEPAD_CACHE_TEST 1\n'+text[start:end] + for name, ret in [('sdl_gamepads','static int '),('stick','static SHORT '),('merge_button','static void '),('sdl_gamepad_state','static void '),('connected_gamepads','static DWORD ')]:unit+='\n'+ret+function_body(text,name)+'\n' + unit+='\nshort '+function_body((ROOT/'source/input/input_xbox.c').read_text(),'fix_dead_zone')+'\n' + unit+=(ROOT/'port/macos/tests/gamepad_virtual_test.inc').read_text() + flags=shlex.split(subprocess.check_output(['pkg-config','--cflags','--libs','sdl3'],text=True)) + with tempfile.TemporaryDirectory(prefix='halo-gamepad-') as tmp: + c=Path(tmp)/'test.c';exe=Path(tmp)/'test';c.write_text(unit) + subprocess.run(['clang','-std=c11','-Wall','-Wextra','-Werror','-UNDEBUG','-I'+str(ROOT/'port/macos'),str(c),str(host),str(ROOT/'port/macos/guest_address.c'),str(ROOT/'port/macos/guest_call.c'),'-framework','OpenGL',*flags,'-o',str(exe)],check=True) + subprocess.run([str(exe)],check=True) +if __name__=='__main__':main() diff --git a/tools/test_macos_hd_resolution.py b/tools/test_macos_hd_resolution.py new file mode 100644 index 0000000000..5dbdef248a --- /dev/null +++ b/tools/test_macos_hd_resolution.py @@ -0,0 +1,94 @@ +#!/usr/bin/env python3 +"""Compile actual resolution/pointer/projection source into a bounded fixture.""" +import re +import subprocess +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] + +def function(text, name): + start = re.search(r"(?:static )?(?:BOOL|void) " + name + r"\(", text).start() + brace = text.index("{", start) + depth = 1 + end = brace + 1 + while depth: + depth += (text[end] == "{") - (text[end] == "}") + end += 1 + return text[start:end] + +def main(): + platform = (ROOT / "port/linux/src/sdl_platform.c").read_text() + renderer = (ROOT / "port/linux/src/d3d8_gl.c").read_text() + camera = (ROOT / "source/render/render_cameras.c").read_text() + projection = re.search(r"field_of_view_tangent = tangent\(camera->vertical_field_of_view.*?projection_y_scale =.*?;", camera, re.S).group() + source = r''' +#include +#include +#include +#include +#define HALO_MACOS 1 +#define HALO_ANDROID 1 +#define SCREEN_HEIGHT 480 +#define SCREEN_MAXIMUM_WIDTH 1920 +#define TRUE 1 +#define FALSE 0 +typedef int BOOL; +static const char *preset; +static const char *config_string(const char *key) { (void)key; return preset; } +static long config_integer(const char *key) { (void)key; return 0; } +static BOOL platform_screen_mode(long *w,long *h) { *w=1920;*h=1200;return TRUE; } +static long logical_width; +static long halo_screen_width(void) { return logical_width; } +struct render_target_entry { struct { long width,height,gl_width,gl_height; } target; } target; +struct { int back_buffer; } device; +static struct render_target_entry *render_target_get(void *p) { (void)p;return ⌖ } +static int point_w,point_h,pixel_w,pixel_h; +static void platform_video_window_size(int *w,int *h) { *w=point_w;*h=point_h; } +static void platform_video_drawable_size(int *w,int *h) { *w=pixel_w;*h=pixel_h; } +''' + source += function(platform, "platform_render_resolution") + "\n" + source += function(renderer, "screen_mode_choose") + "\n" + source += function(renderer, "ui_point_from_window") + "\n" + source += r''' +static float projection_x(long width) { + struct { float vertical_field_of_view; } value={1.0f}, *camera=&value; + float field_of_view_tangent, projection_x_scale, projection_y_scale; + float half_bounds_width=1,half_bounds_height=1,viewport_height=480,viewport_width=(float)width; +#define tangent tanf +'''+projection+r''' + assert(fabsf(projection_y_scale-1.0f/tanf(0.5f))<0.00001f); + return projection_x_scale; +} +int main(void) { + const char *presets[]={"1280x720","1920x1080","640x480","invalid"}; + const int widths[]={1280,1920,640,1280}, heights[]={720,1080,480,720}; + for(int i=0;i<4;i++) { + long w,h;float scale[2];short x,y; + preset=presets[i];assert(platform_render_resolution(&w,&h)); + assert(w==widths[i]&&h==heights[i]); + screen_mode_choose(&logical_width,scale); + assert(lroundf(logical_width*scale[0])==w); + assert(lroundf(480*scale[1])==h); + target.target.width=logical_width;target.target.height=480; + target.target.gl_width=w;target.target.gl_height=h; + point_w=w/2;point_h=h/2;pixel_w=w;pixel_h=h; + ui_point_from_window(point_w/2.0f,point_h/2.0f,&x,&y); + assert(x==320&&y==240); + if(h!=480) assert(projection_x(logical_width) +#include +typedef unsigned char boolean; +#define TRUE 1 +#define FALSE 0 +static struct { float menu_seconds; boolean set_up; char *map_name; } network_test; +static boolean movie_active,texture_pool_borrowed; +static unsigned host_entries; +static boolean bink_playback_active(void){return movie_active;} +static void main_set_multiplayer_map_name(const char *s){assert(s);} +static void player_ui_fast_setup_network_server(void){assert(!texture_pool_borrowed);host_entries++;} +static void platform_log(const char *s,...){assert(s);} +''' + unit+='\nstatic boolean '+gate+'\nstatic void update_frontend(boolean main_menu_loaded,float seconds){\n'+timer+host+'}\n' + unit+=r''' +int main(void){ + network_test.map_name="bloodgulch";movie_active=texture_pool_borrowed=TRUE; + /* A loaded menu beneath the intro must not consume its safe-start timer. */ + for(unsigned i=0;i<60;i++)update_frontend(TRUE,1.0f/3.0f); + assert(host_entries==0&&!network_test.set_up&&network_test.menu_seconds==0); + movie_active=texture_pool_borrowed=FALSE; + update_frontend(FALSE,5);assert(host_entries==0&&network_test.menu_seconds==0); + update_frontend(TRUE,1);assert(host_entries==0);update_frontend(TRUE,1); + assert(host_entries==1&&network_test.set_up);update_frontend(TRUE,1);assert(host_entries==1); + puts("Actual network-test frontend:loaded menu during intro preserves texture ownership/timer; normal movie disposal then2s starts host once PASS"); +} +''' + with tempfile.TemporaryDirectory(prefix='halo-network-startup-') as tmp: + c=Path(tmp)/'test.c';exe=Path(tmp)/'test';c.write_text(unit) + subprocess.run(['clang','-std=c11','-Wall','-Wextra','-Werror','-UNDEBUG',str(c),'-o',str(exe)],check=True);subprocess.run([str(exe)],check=True) +if __name__=='__main__':main() diff --git a/tools/test_macos_sdl_window.py b/tools/test_macos_sdl_window.py new file mode 100755 index 0000000000..d759a46be1 --- /dev/null +++ b/tools/test_macos_sdl_window.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 +"""Compile the native SDL token/window regression against real SDL3. +The fixture opens a hidden window; it does not launch the game. Logging and +application activation leaves are isolated, while SDL and guest spans are real. +""" +from pathlib import Path +import shlex +import subprocess +import tempfile + +ROOT = Path(__file__).resolve().parents[1] + + +def main(): + flags = shlex.split(subprocess.check_output( + ['pkg-config', '--cflags', '--libs', 'sdl3'], text=True)) + with tempfile.TemporaryDirectory(prefix='halo-sdl-window-') as temporary: + support = Path(temporary) / 'support.c' + executable = Path(temporary) / 'test' + support.write_text('''void mac_host_log(int p,const char*s){(void)p;(void)s;} +void mac_host_logf(int p,const char*s,...){(void)p;(void)s;} +void mac_host_apply_app_icon(void){} +void mac_host_activate_app(void){} +void mac_game_evidence_presented(void){} +''') + native = ROOT / 'port/macos' + subprocess.run([ + 'clang', '-std=c11', '-Wall', '-Wextra', '-Werror', '-UNDEBUG', + '-I' + str(native), str(native/'tests/host_sdl_test.c'), + str(native/'host_sdl.c'), str(native/'guest_address.c'), + str(native/'guest_call.c'), str(support), '-Wl,-dead_strip', + '-framework', 'OpenGL', *flags, '-o', str(executable)], check=True) + subprocess.run([str(executable)], check=True) + + +if __name__ == '__main__': + main() diff --git a/tools/test_macos_symbol_namespaces.py b/tools/test_macos_symbol_namespaces.py new file mode 100644 index 0000000000..d623399fbb --- /dev/null +++ b/tools/test_macos_symbol_namespaces.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 +"""Reproduce a silent archive/guest alias collision and verify the build guard.""" +import subprocess +import sys +import tempfile +from pathlib import Path +from check_macos_symbol_namespaces import native_definitions, native_references + + +def main(): + checker = Path(__file__).with_name('check_macos_symbol_namespaces.py') + # Explicit platform parser tests, including weak and undefined symbols. + assert native_definitions('0000 T _helper\n U _missing\n0000 W _weak\n', True) == {'helper', 'weak'} + assert native_definitions('0000 T helper\n U missing\n w weak_missing\n0000 W weak\n', False) == {'helper', 'weak'} + assert native_references(' U _puts\n w _weak_missing\n0000 T _local\n', True) == {'puts', 'weak_missing'} + with tempfile.TemporaryDirectory(prefix='halo-symbol-test-') as temporary: + root = Path(temporary) + lift = root / 'lift' + lift.mkdir() + (lift / 'recomp_import_aliases.c').write_text('void halo_copy(void) { }\n') + (root / 'guest.c').write_text('void halo_copy(void) { }\nvoid adapter(void); int main(void) { adapter(); return 0; }\n') + (root / 'adapter.c').write_text('void halo_copy(void); void adapter(void) { halo_copy(); }\n') + (root / 'helper.c').write_text('void halo_copy(void) { }\n') + for source in ('adapter', 'helper'): + subprocess.run(['cc', '-c', str(root / f'{source}.c'), '-o', str(root / f'{source}.o')], check=True) + archive = root / 'native.a' + subprocess.run(['ar', 'rcs', str(archive), str(root / 'adapter.o'), str(root / 'helper.o')], check=True) + # Normal ld succeeds, leaving the helper archive member unselected. + subprocess.run(['cc', str(root / 'guest.c'), str(archive), '-o', str(root / 'silent-collision')], check=True) + command = [sys.executable, str(checker), '--lift-dir', str(lift), '--archive', str(archive)] + failure = subprocess.run(command, text=True, capture_output=True) + assert failure.returncode == 1 and 'halo_copy' in failure.stderr, failure + (root / 'helper.c').write_text('void mac_copy(void) { }\n') + (root / 'adapter.c').write_text('void mac_copy(void); void adapter(void) { mac_copy(); }\n') + for source in ('adapter', 'helper'): + subprocess.run(['cc', '-c', str(root / f'{source}.c'), '-o', str(root / f'{source}.o')], check=True) + subprocess.run(['ar', 'rcs', str(archive), str(root / 'adapter.o'), str(root / 'helper.o')], check=True) + subprocess.run(['cc', str(root / 'guest.c'), str(archive), '-o', str(root / 'namespaced')], check=True) + subprocess.run(command, check=True) + # A native undefined reference can instead be shadowed by a guest + # alias of a dylib API, even when no archive defines that name. + (lift / 'recomp_import_aliases.c').write_text('void halo_copy(void) { }\nvoid puts(void) { }\n') + (root / 'adapter.c').write_text('#include \nvoid adapter(void) { puts("native"); }\n') + subprocess.run(['cc', '-c', str(root / 'adapter.c'), '-o', str(root / 'adapter.o')], check=True) + subprocess.run(['ar', 'rcs', str(archive), str(root / 'adapter.o'), str(root / 'helper.o')], check=True) + failure = subprocess.run(command, text=True, capture_output=True) + assert failure.returncode == 1 and 'puts' in failure.stderr, failure + print('Silent archive shadowing regression PASS: original link succeeds but guard rejects; namespaced helper passes') + + +if __name__ == '__main__': + main() diff --git a/tools/test_macos_translated_errno.py b/tools/test_macos_translated_errno.py new file mode 100644 index 0000000000..d9c37d943e --- /dev/null +++ b/tools/test_macos_translated_errno.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 +"""Actual guest FileExists/GetFileAttributes + native stat errno regression. +Path/ref leaves provide an exact fixture path; emutls leaf provides bounded +per-guest-thread Win32 LastError storage. Musl errno accessor stays translated. +""" +import argparse,hashlib,re,subprocess,sys +from pathlib import Path +from macos_lift_elf import ROOT,parse_elf32 + +def main(): + p=argparse.ArgumentParser(description=__doc__);p.add_argument('--elf',type=Path,default=ROOT/'build/macos-aot/halo_guest.elf');p.add_argument('--output',type=Path,required=True);p.add_argument('--miniupnpc-lib',type=Path,required=True);p.add_argument('--recomp-root',type=Path,required=True);a=p.parse_args() + elf=parse_elf32(a.elf); symbols={s['name']:s['value'] for s in elf['symbols']} + # Static __get_tp appears once per musl unit; include every real symbol. + tp_addresses=[s['value'] for s in elf['symbols'] if s['name']=='__get_tp'];digest=hashlib.sha256(a.elf.read_bytes()).hexdigest() + names=['__guest_get_tp','__errno_location','__get_tp','GetFileAttributesA','platform_set_last_error_from_errno','GetLastError','file_exists'] + cmd=[sys.executable,str(ROOT/'tools/macos_lift_elf.py'),'--elf',str(a.elf),'--output',str(a.output),'--expected-elf-sha256',digest,'--recomp-root',str(a.recomp_root),'--discover'] + for n in names:cmd+=['--address',hex(symbols[n])] + for address in tp_addresses:cmd+=['--address',hex(address)] + subprocess.run(cmd,check=True,stdout=subprocess.DEVNULL) + services={'memset':'memset((void *)XBOX_PTR(MEM32(g_esp+4)),MEM32(g_esp+8),MEM32(g_esp+12));g_eax=MEM32(g_esp+4);g_esp+=4;', + 'platform_translate_path':'strcpy((char *)XBOX_PTR(MEM32(g_esp+8)),(char *)XBOX_PTR(MEM32(g_esp+4)));g_eax=MEM32(g_esp+8);g_esp+=4;', + 'file_reference_get_const_info':'g_eax=MEM32(g_esp+4);g_esp+=4;', + 'file_reference_get_info':'g_eax=MEM32(g_esp+4);g_esp+=4;', + 'file_location_get_full_path':'strcpy((char *)XBOX_PTR(MEM32(g_esp+12)),(char *)XBOX_PTR(MEM32(g_esp+8)));g_esp+=4;', + '__emutls_get_address':'g_eax=mac_host_get_guest_tp()+0x100;g_esp+=4;', + 'file_error':'fixture_file_errors++;g_esp+=4;', + 'fill_attribute_data':'MEM32(MEM32(g_esp+8))=128;g_esp+=4;'} + stubs=(a.output/'recomp_stubs_unresolved.c').read_text().replace('#include "recomp_funcs.h"','#include "recomp_funcs.h"\n#include "host_services.h"\n#include \nextern int fixture_file_errors;') + for n,body in services.items(): + if n not in symbols:continue + f=f'sub_{symbols[n]:08X}';pat=r'void '+f+r'\(void\) \{[^\n]*\}' + if re.search(pat,stubs):stubs=re.sub(pat,'',stubs)+f'\nvoid {f}(void){{{body}}}\n' + (a.output/'controlled_services.c').write_text(stubs) + native=ROOT/'port/macos';command=['clang','-O1','-UNDEBUG','-Wno-unused','-Wno-parentheses-equality',f'-I{a.output.resolve()}',f'-I{native}',f'-I{ROOT}/port/third_party/miniupnpc/include'] + for n in ('__errno_location','__get_tp','GetFileAttributesA','GetLastError','file_exists'):command+=[f'-DTEST_{n}=sub_{symbols[n]:08X}'] + command += [f'-DVA___get_tp={symbols["__get_tp"]}'] + for n in ('host_get_tp','posix_stat'):command+=[f'-DTOKEN_{n}={symbols[n]}'] + command+=[str(native/'tests/translated_errno_test.c'),str(a.output/'recomp_0000.c'),str(a.output/'controlled_services.c'),str(a.output/'recomp_import_aliases.c')] + command+=[str(native/n) for n in ('guest_image.c','guest_address.c','guest_allocator.c','guest_call.c','recomp_state.c','guest_x87_80.c','host_services.c','host_syscall.c','guest_errno.c','posix_backend_files.c','posix_backend_net.c','posix_backend_upnp.c')] + # Exercise the published native adapter and its real translated TLS accessor. + command+=[str(native/'posix_host_imports.c'),str(a.miniupnpc_lib)] + exe=a.output/'errno-test';subprocess.run(command+['-o',str(exe)],check=True) + print('actual ELF SHA256',digest,flush=True);subprocess.run([str(exe),str(a.elf)],check=True) +if __name__=='__main__':main() diff --git a/tools/test_macos_translated_gamepad.py b/tools/test_macos_translated_gamepad.py new file mode 100644 index 0000000000..7cac5ee6b6 --- /dev/null +++ b/tools/test_macos_translated_gamepad.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 +"""Run actual linked XInput + SDL guest adapter bytes with real virtual SDL pads. +Keyboard/mouse services are explicitly neutral; allocation uses a checked, +fixed fixture scratch arena. Cache mutex services are serialized fixture leaves; +source/native thread locking is tested separately. Gamepad payloads are real. +""" +import argparse, hashlib, json, re, shlex, subprocess, sys +from pathlib import Path +from macos_lift_elf import ROOT,parse_elf32 +def main(): + p=argparse.ArgumentParser(description=__doc__);p.add_argument('--elf',type=Path,default=ROOT/'build/macos-aot/halo_guest.elf');p.add_argument('--output',type=Path,required=True);p.add_argument('--recomp-root',type=Path,required=True);p.add_argument('--reuse-lift',action='store_true');a=p.parse_args() + symbols={s['name']:s['value'] for s in parse_elf32(a.elf)['symbols']};digest=hashlib.sha256(a.elf.read_bytes()).hexdigest() + names=['XInputOpen','XInputGetState','XInputSetState','XGetDeviceChanges','controller_port','sdl_gamepads','sdl_gamepad_state','stick','merge_button','connected_gamepads','SDL_GetGamepads','SDL_OpenGamepad','SDL_GetGamepadFromID','SDL_GetGamepadAxis','SDL_GetGamepadButton','SDL_GetGamepadType','SDL_RumbleGamepad'] + if 'halo_input_gamepads_changed' in symbols:names.append('halo_input_gamepads_changed') + cmd=[sys.executable,str(ROOT/'tools/macos_lift_elf.py'),'--elf',str(a.elf),'--output',str(a.output),'--expected-elf-sha256',digest,'--recomp-root',str(a.recomp_root),'--discover'] + for n in names:cmd+=['--address',hex(symbols[n])] + if not a.reuse_lift:subprocess.run(cmd,check=True) + report=json.loads((a.output/'lift_report.json').read_text());assert report['input_elf_sha256']==digest and not report['functions_failed'] + stubs=(a.output/'recomp_stubs_unresolved.c').read_text();services={ + 'memset':'g_eax=MEM32(g_esp+4);memset((void *)XBOX_PTR(g_eax),MEM32(g_esp+8),MEM32(g_esp+12));g_esp+=4;', + 'memcpy':'g_eax=MEM32(g_esp+4);memcpy((void *)XBOX_PTR(g_eax),(void *)XBOX_PTR(MEM32(g_esp+8)),MEM32(g_esp+12));g_esp+=4;', + 'memcmp':'g_eax=memcmp((void *)XBOX_PTR(MEM32(g_esp+4)),(void *)XBOX_PTR(MEM32(g_esp+8)),MEM32(g_esp+12));g_esp+=4;', + 'malloc':'assert(MEM32(g_esp+4)<=256);g_eax=0x60008000;g_esp+=4;', + 'SDL_free':'assert(MEM32(g_esp+4)==0x60008000);g_esp+=4;', + 'pthread_mutex_lock':'g_eax=0;g_esp+=4;','__pthread_mutex_lock':'g_eax=0;g_esp+=4;', + 'pthread_mutex_unlock':'g_eax=0;g_esp+=4;','__pthread_mutex_unlock':'g_eax=0;g_esp+=4;', + 'platform_pump_events':'fixture_pump();g_esp+=4;', + 'platform_input_read':'memset((void *)XBOX_PTR(MEM32(g_esp+4)),0,128);g_esp+=4;', + 'mouse_poll':'g_esp+=4;','wheel_update':'g_esp+=4;','keyboard_gamepad':'g_esp+=4;','test_input_gamepad':'g_esp+=4;', + 'console_is_active':'g_eax=0;g_esp+=4;', 'SetEvent':'g_eax=1;g_esp+=8;', 'SetLastError':'g_esp+=8;'} + stubs=stubs.replace('#include "recomp_funcs.h"','#include "recomp_funcs.h"\n#include \n#include \nextern void fixture_pump(void);') + for n,body in services.items(): + if n not in symbols:continue + f=f'sub_{symbols[n]:08X}';pat=r'void '+f+r'\(void\) \{[^\n]*\}' + if re.search(pat,stubs):stubs=re.sub(pat,'',stubs)+f'\nvoid {f}(void){{{body}}}\n' + (a.output/'controlled_services.c').write_text(stubs) + native=ROOT/'port/macos';flags=shlex.split(subprocess.check_output(['pkg-config','--cflags','--libs','sdl3'],text=True)) + cmd=['clang','-O1','-UNDEBUG','-Wno-unused','-Wno-parentheses-equality',f'-I{a.output.resolve()}',f'-I{native}'] + for n in ['XInputOpen','XInputGetState','XInputSetState','XGetDeviceChanges']: + cmd+=[f'-DTEST_{n}=sub_{symbols[n]:08X}'] + if 'halo_input_gamepads_changed' in symbols:cmd+=[f'-DTEST_GAMEPADS_CHANGED=sub_{symbols["halo_input_gamepads_changed"]:08X}'] + cmd+=[f'-DTEST_GAMEPAD_TYPE_VA={symbols["XDEVICE_TYPE_GAMEPAD_TABLE"]}'] + imports=json.loads((ROOT/'build/macos-aot/host-import-manifest.json').read_text()) + for item in imports['imports']: + if item['name'].startswith('host_sdl_') and ('gamepad' in item['name']):assert symbols[item['name']]==item['va'];cmd+=[f'-DTEST_{item["name"]}={item["va"]}'] + cmd += [str(native/'tests/translated_gamepad_test.c'),str(a.output/'recomp_0000.c'),str(a.output/'controlled_services.c'),str(a.output/'recomp_import_aliases.c')] + cmd += [str(native/n) for n in ('guest_image.c','guest_address.c','guest_allocator.c','guest_call.c','recomp_state.c','guest_x87_80.c','host_services.c','host_syscall.c','host_sdl.c')] + exe=a.output/'translated-gamepad-test';subprocess.run(cmd+flags+['-framework','OpenGL','-o',str(exe)],check=True) + print('actual ELF SHA256',digest,flush=True);subprocess.run([str(exe),str(a.elf)],check=True) +if __name__=='__main__':main() diff --git a/tools/test_macos_ui_condition.py b/tools/test_macos_ui_condition.py new file mode 100644 index 0000000000..747638444b --- /dev/null +++ b/tools/test_macos_ui_condition.py @@ -0,0 +1,95 @@ +#!/usr/bin/env python3 +"""Compile the actual UI callback/invoker bodies with controlled profile services.""" +from pathlib import Path +import subprocess +import tempfile + +ROOT = Path(__file__).resolve().parents[1] +SOURCE = ROOT / 'source/interface/ui_widget_event_handler_functions.c' + + +def function_body(text, name): + import re + matches = list(re.finditer(r'\b' + re.escape(name) + r'\s*\([^;{}]*\)\s*\{', text)) + if len(matches) != 1: + raise RuntimeError(f'expected one definition of {name}, found {len(matches)}') + start = matches[0].start() + brace = text.index('{', start) + depth = 1 + end = brace + 1 + while depth: + depth += (text[end] == '{') - (text[end] == '}') + end += 1 + return text[start:end] + + +def main(): + text = SOURCE.read_text() + callback = function_body(text, 'new_campaign_if_no_custom_player_profiles_exist') + classifier = function_body(text, 'ui_widget_event_handler_function_is_branch_condition') + invoker = function_body(text, 'ui_widget_event_handler_function_invoke') + unit = r''' +#include +#include +#include +#include +#define HALO_MACOS 1 +#define TRUE 1 +#define FALSE 0 +#define NONE -1 +#define NUMBEROF(a) (sizeof(a)/sizeof((a)[0])) +#define match_vassert(...) ((void)0) +typedef unsigned char boolean; +typedef uint16_t word; +struct widget_instance { int unused; }; +struct event_record { int unused; }; +typedef boolean (*handler)(struct widget_instance *,struct event_record *,boolean *); +static struct { handler functions[102]; const char *names[102]; } event_handler_function_list; +static int profile_count, name_opened, warnings, errors; +static void player_profiles_enumerate_available_to_local_player_index(int player, word *count, long *index, boolean defaults) +{ assert(player==NONE && *count==1 && !defaults); *count=(word)profile_count; *index=42; } +static boolean new_campaign_chosen(struct widget_instance *widget,struct event_record *event,boolean *deleted) +{ (void)widget;(void)event;(void)deleted;name_opened++;return TRUE; } +static void console_warning(const char *format,...){ assert(format);warnings++; } +static void error(int level,const char *message){ assert(level==2 && message);errors++; } +static boolean unrelated_false(struct widget_instance *w,struct event_record *e,boolean *d) +{ (void)w;(void)e;(void)d;return FALSE; } +''' + unit += '\nstatic boolean ' + callback + unit += '\nboolean ' + classifier + unit += '\nboolean ' + invoker + unit += r''' +int main(void) +{ + struct widget_instance widget={0};struct event_record event={0};boolean deleted=FALSE; + for(unsigned int i=0;i<102;i++){event_handler_function_list.functions[i]=unrelated_false;event_handler_function_list.names[i]="unrelated";} + event_handler_function_list.functions[101]=new_campaign_if_no_custom_player_profiles_exist; + event_handler_function_list.names[101]="new game if no plyr profiles"; + assert(ui_widget_event_handler_function_is_branch_condition(101)); + assert(!ui_widget_event_handler_function_is_branch_condition(0)); + assert(!ui_widget_event_handler_function_is_branch_condition(102)); + assert(!ui_widget_event_handler_function_is_branch_condition(UINT16_MAX)); + profile_count=0; + assert(!ui_widget_event_handler_function_invoke(&widget,&event,101,&deleted)); + assert(name_opened==1 && !deleted && !warnings && !errors); + profile_count=1;name_opened=0; + assert(ui_widget_event_handler_function_invoke(&widget,&event,101,&deleted)); + assert(!name_opened && !warnings && !errors); + assert(!ui_widget_event_handler_function_invoke(&widget,&event,0,&deleted)); + assert(warnings==1 && !errors); + assert(!ui_widget_event_handler_function_invoke(&widget,&event,102,&deleted)); + assert(warnings==1 && errors==1); + puts("UI condition: zero profiles opens naming and retains FALSE; existing profile TRUE; unrelated failure warning retained; bounds checked"); +} +''' + with tempfile.TemporaryDirectory(prefix='halo-ui-condition-') as directory: + source = Path(directory) / 'condition.c' + executable = Path(directory) / 'condition-test' + source.write_text(unit) + subprocess.run(['clang', '-Wall', '-Wextra', '-Werror', '-Wno-unused-parameter', + '-UNDEBUG', str(source), '-o', str(executable)], check=True) + subprocess.run([str(executable)], check=True) + + +if __name__ == '__main__': + main() diff --git a/validation/sourceport-lp64/batch_compile_guest.py b/validation/sourceport-lp64/batch_compile_guest.py new file mode 100644 index 0000000000..b7c1df0f86 --- /dev/null +++ b/validation/sourceport-lp64/batch_compile_guest.py @@ -0,0 +1,237 @@ +#!/usr/bin/env python3 +"""Compile the upstream 2342 game C units with a native-host i386 musl ABI. + +This is a compile-only feasibility stage: it does not link host services or +translate the resulting ELF objects. Results are saved under build/macos-aot. +""" +import argparse +import concurrent.futures +import json +import re +import shutil +import os +import shlex +import subprocess +import sys +import time +from pathlib import Path + + +ROOT = Path(__file__).resolve().parents[2] +sys.path.insert(0, str(ROOT)) + + +def generate_guest_syscall_header(template: Path, destination: Path) -> None: + source = template.read_text() + # Keep the existing arm64_32 dispatch IDs, adding Linux's shared time64 + # extension so musl 1.2's 64-bit time_t selects a 16-byte kernel record. + # Without this macro it passes timespec64 to legacy clock syscall 113, + # whose eight-byte result puts nanoseconds in the seconds' high word. + if not re.search(r"^#define __NR_clock_gettime64\s", source, re.M): + source += "\n#define __NR_clock_gettime64 403\n" + if not re.search(r"^#define __NR_clock_nanosleep_time64\s", source, re.M): + # Presence also enables musl's explicit time32 marshalling for small + # legacy115/101 requests; otherwise it sends a 16-byte timespec there. + source += "\n#define __NR_clock_nanosleep_time64 407\n" + aliases = [] + for line in source.splitlines(): + match = re.match(r"#define __NR_([^\s]+)(\s+.+)$", line) + if match: + aliases.append("#define SYS_" + match.group(1) + match.group(2)) + destination.parent.mkdir(parents=True, exist_ok=True) + destination.write_text(source + "\n" + "\n".join(aliases) + "\n") + + +def main() -> int: + parser = argparse.ArgumentParser() + parser.add_argument("--jobs", type=int, default=4) + parser.add_argument("--limit", type=int, default=0) + parser.add_argument("--scope", choices=("game", "platform", "libc", "math"), default="game") + parser.add_argument("--source", action="append", default=[], + help="compile only this manifest source; repeat for a selected batch") + parser.add_argument("--cc", default="clang") + parser.add_argument("--include-root", type=Path, default=ROOT / "build/deps/musl-include") + parser.add_argument("--musl-root", type=Path, default=ROOT / "build/deps/musl-1.2.5") + args = parser.parse_args() + os.chdir(ROOT) + out = Path("build/macos-aot") + objdir = out / f"{args.scope}-obj" + objdir.mkdir(parents=True, exist_ok=True) + sources = [] + cfg = json.loads(Path("config/config.json").read_text()) + excluded = set(json.loads(Path("port/linux/port.json").read_text()).get("exclude_sources", [])) + if args.scope == "game": + for proj in cfg["projects"]: + if proj["name"] != "halobetacache": + continue + for obj in proj["objects"]: + src = obj["name"] + if str(obj["status"]).lower() == "missing" or src in excluded or Path(src).suffix.lower() != ".c": + continue + sources.append(Path(src)) + break + sources.extend(sorted(Path("port/linux/game").glob("*.c"))) + elif args.scope == "platform": + sources.extend(source for source in sorted(Path("port/linux/src").glob("*.c")) + if not source.name.startswith("posix_")) + sources.extend([Path("port/third_party/tomlc17/tomlc17.c"), Path("port/third_party/kcp/ikcp.c")]) + sources.extend(source for source in sorted(Path("port/android/guest/runtime").glob("*.c")) + if source.name != "guest_memory_watch.c") + else: + if args.scope == "libc": + from tools import android_build + android_build.MUSL_DIR = args.musl_root + sources.extend(android_build._musl_sources()) + sources.extend(args.musl_root / "src" / name for name in ( + "env/__reset_tls.c", "misc/getpriority.c", "misc/setpriority.c", + "thread/pthread_sigmask.c", + "temp/__randname.c", "thread/clone.c", "thread/pthread_cleanup_push.c", + "thread/pthread_barrier_init.c", "thread/pthread_barrier_wait.c", + "thread/pthread_barrier_destroy.c", "thread/synccall.c", + )) + sources.extend(sorted((args.musl_root / "src/fenv").glob("*.c"))) + sources.extend(args.musl_root / "src" / name for name in ( + "setjmp/i386/setjmp.s", "setjmp/i386/longjmp.s", "fenv/i386/fenv.s", + )) + else: + sources.extend(sorted(Path("port/third_party/musl-math/src").glob("*.c"))) + sources = sorted(set(sources)) + if args.source: + selected = {Path(source).resolve() for source in args.source} + known = {source.resolve() for source in sources} + missing = selected - known + if missing: + parser.error("sources outside this scope's manifest: " + ", ".join(map(str, sorted(missing)))) + sources = [source for source in sources if source.resolve() in selected] + if args.limit: + sources = sources[:args.limit] + + incs = [ + args.include_root, + args.musl_root / "arch/i386", + args.musl_root / "arch/generic", + args.musl_root / "include", + Path("port/linux/include"), + Path("port/include"), + *[Path(p) for p in json.loads(Path("config/config.json").read_text())["projects"][0]["options"]["include_dirs"] if p != "xbox/include"], + ] + xdk = Path("port/include/xdk") + common = [ + args.cc, "--target=i386-unknown-linux-musl", "-m32", "-fms-extensions", "-fshort-wchar", + "-fcommon", "-fno-pic", "-fno-strict-aliasing", "-fwrapv", + "-fno-delete-null-pointer-checks", "-freg-struct-return", "-fno-omit-frame-pointer", + "-ffp-contract=off", "-O0", "-g", "-std=gnu89", "-D__STRICT_ANSI__", "-w", + "-Wno-error=incompatible-pointer-types", "-Wno-error=implicit-function-declaration", + "-Wno-error=incompatible-function-pointer-types", "-Wno-error=int-conversion", + "-Wno-error=implicit-int", "-Wno-error=return-type", "-nostdinc", + "-fno-builtin-wcslen", "-fno-builtin-wcsnlen", "-fno-builtin-wcschr", + "-fno-builtin-wcsrchr", "-fno-builtin-wcscmp", "-fno-builtin-wcsncmp", + "-fno-builtin-wcscpy", "-fno-builtin-wcsncpy", "-fno-builtin-wcscat", + "-fno-builtin-wcsncat", "-fno-builtin-wmemchr", "-fno-builtin-wmemcmp", + "-fno-builtin-wmemcpy", "-fno-builtin-wmemmove", "-fno-builtin-wmemset", + "-isystem", str(args.include_root), "-isystem", "port/macos/musl-i386", + "-isystem", str(args.musl_root / "arch/i386"), + "-isystem", str(args.musl_root / "arch/generic"), "-isystem", str(args.musl_root / "include"), + ] + # Game/platform code uses the Xbox-compatible 8-byte alignment mode. + # The i386 musl ABI itself uses 4-byte alignment for 12-byte long double. + if args.scope not in ("libc", "math"): + common.append("-malign-double") + if args.scope in ("game", "platform"): + # Android uses compiler-emulated TLS for these guest objects. Native + # i386 GS-base accesses would dereference host address zero in the + # high-arena AOT runtime, so preserve the guest TLS ABI via emutls. + common.append("-femulated-tls") + if args.scope != "libc": + common += ["-include", "port/linux/include/halo_linux_prefix.h", + "-include", "build/macos-aot/halo_msvc_semantics.h" if args.scope == "game" + else "build/macos-aot/platform_msvc_semantics.h"] + for inc in incs[4:]: + common += ["-I", str(inc)] + common += ["-idirafter", str(xdk)] + # Keep the project config's platform/game defines. + common += ["-DDEBUG", "-Dxbox", "-DHALO_MACOS=1", "-DHALO_ANDROID=1"] + if args.scope == "platform": + syscall_arch_dir = ROOT / "build/deps/syscall-arch" + syscall_arch_dir.mkdir(parents=True, exist_ok=True) + shutil.copyfile("port/android/guest/libc/arch/arm64_32/syscall_arch.h", + syscall_arch_dir / "syscall_arch.h") + common += ["-D_GNU_SOURCE", "-DHALO_LINUX_PLATFORM_LAYER=1", "-std=gnu11", + "-I", "port/linux/src", "-I", "port/third_party/tomlc17", + "-I", "port/third_party/kcp", "-I", "port/android/guest/runtime", + "-I", "port/android/include", "-I", "port/android/guest/libc/src_include", + # The platform's musl internal syscall wrappers must use + # the same host-dispatched guest syscall ABI as libc, but + # retain the i386 arch's signal/fenv layouts. + "-I", str(syscall_arch_dir), + "-I", str(ROOT / "build/deps/musl-internal"), "-I", str(args.musl_root / "src/internal"), + "-I", str(args.musl_root / "src/include"), "-I", os.environ.get("HALO_BREW_INCLUDE", "/opt/homebrew/include")] + elif args.scope == "libc": + # Keep syscall IDs aligned with Android's guest-host syscall ABI, + # while all other machine types remain the i386 ILP32 definitions. + syscall_template = Path("port/android/guest/libc/arch/arm64_32/bits/syscall.h.in") + syscall_header = args.include_root / "bits/syscall.h" + syscall_arch_dir = ROOT / "build/deps/syscall-arch" + syscall_arch_dir.mkdir(parents=True, exist_ok=True) + shutil.copyfile("port/android/guest/libc/arch/arm64_32/syscall_arch.h", + syscall_arch_dir / "syscall_arch.h") + if syscall_template.exists(): + generate_guest_syscall_header(syscall_template, syscall_header) + # Reuse Android's syscall interposer so translated musl code never + # executes raw i386 int 0x80 on Darwin. + common += ["-std=c99", "-ffreestanding", "-fno-common", "-D_XOPEN_SOURCE=700", + "-I", str(syscall_arch_dir), + "-I", "port/macos/musl-i386", + "-I", str(args.musl_root / "arch/i386"), + "-I", str(args.musl_root / "arch/generic"), + "-I", str(ROOT / "build/deps/musl-internal"), + "-I", str(args.musl_root / "src/include"), + "-I", str(args.musl_root / "src/internal")] + elif args.scope == "math": + common += ["-std=gnu11", "-w", "-I", "port/third_party/musl-math/include", + "-include", "port/third_party/musl-math/include/libm.h"] + + def compile_one(src: Path): + try: + rel_src = src.resolve().relative_to(args.musl_root.resolve()) + except ValueError: + rel_src = src.resolve().relative_to(ROOT) + obj = objdir / rel_src.with_suffix(".o") + obj.parent.mkdir(parents=True, exist_ok=True) + cmd = [*common, "-c", str(src), "-o", str(obj)] + start = time.monotonic() + p = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) + return src.as_posix(), p.returncode, p.stderr, time.monotonic() - start + + started = time.monotonic() + results = [] + with concurrent.futures.ThreadPoolExecutor(max_workers=args.jobs) as pool: + futures = [pool.submit(compile_one, src) for src in sources] + for i, fut in enumerate(concurrent.futures.as_completed(futures), 1): + result = fut.result() + results.append(result) + if i % 50 == 0 or i == len(sources): + ok = sum(r[1] == 0 for r in results) + print(f"progress {i}/{len(sources)} complete={ok} failed={i-ok}", flush=True) + + results.sort() + log_scope = args.scope + ("-selected" if args.source else "") + log = out / f"{log_scope}-compile-results.json" + log.write_text(json.dumps([{"source": s, "returncode": rc, "stderr": err, "seconds": sec} + for s, rc, err, sec in results], indent=2) + "\n") + failures = [r for r in results if r[1] != 0] + summary = [ + f"sources={len(sources)} compiled={len(sources)-len(failures)} failed={len(failures)}", + f"elapsed_seconds={time.monotonic()-started:.1f}", + f"results={log}", + ] + (out / f"{log_scope}-compile-summary.txt").write_text("\n".join(summary) + "\n") + print("\n".join(summary)) + for src, rc, stderr, _ in failures[:20]: + errors = [line for line in stderr.splitlines() if "error:" in line or "fatal error:" in line] + print(f"FAIL {src}: " + (errors[0] if errors else stderr[:300].replace("\n", " "))) + return 1 if failures else 0 + + +if __name__ == "__main__": + raise SystemExit(main())