diff --git a/native/wto-net/build.ps1 b/native/wto-net/build.ps1 index 4e45c3a..3d8d259 100644 --- a/native/wto-net/build.ps1 +++ b/native/wto-net/build.ps1 @@ -23,7 +23,7 @@ New-Item -ItemType Directory -Force $obj | Out-Null $sources = @( "$src\dll_main.c", "$src\hook_config.c", "$src\redirect_rules.c", "$src\resolver_hooks.c", "$src\connection_hooks.c", "$src\local_mp_core.c", "$src\local_mp_values.c", "$src\local_mp_players.c", "$src\local_mp_loadout.c", "$src\local_mp_actions.c", "$src\local_mp_awards.c", "$src\local_mp_kills.c", "$src\local_mp_events.c", "$src\local_mp_scores.c", "$src\local_mp_nuke.c", "$src\local_mp_support.c", - "$src\local_mp_damage_notifications.c", "$src\local_mp_air_events.c", "$src\local_mp_ground_ai.c", "$src\local_mp_zones.c", + "$src\local_mp_damage_notifications.c", "$src\local_mp_air_events.c", "$src\local_mp_ground_ai.c", "$src\local_mp_zones.c", "$src\local_mp_air_ai_sync.c", "$mh\src\hook.c", "$mh\src\buffer.c", "$mh\src\trampoline.c", "$mh\src\hde\hde64.c" ) | ForEach-Object { '"' + (Resolve-Path $_) + '"' } diff --git a/native/wto-net/src/local_mp_air_ai_sync.c b/native/wto-net/src/local_mp_air_ai_sync.c new file mode 100644 index 0000000..0879459 --- /dev/null +++ b/native/wto-net/src/local_mp_air_ai_sync.c @@ -0,0 +1,48 @@ +/* + * Thread synchronization for the shared air-AI `unsortedPlanes` vector on a LocalMP host. + * + * Aircraft physics and AI updates run across worker threads via `threadpool::parallel_for` + * (2.61.0.60: 0x373e51 -> 0x39f5d0 -> 0xdf52b0 -> 0xe48740 -> 0xe42db0 -> 0x31a23e0 -> + * 0xdb07e0 -> 0x7fba00). When a bot plane finishes its behavior tree, AirAi::update tail-calls + * TeamAiManager::addToUnsortedPlanes (0x8007b0 -> 0x80a150 on 2.61.0.60). That function locks + * the global unit table while resolving the unit, then mutates the team's shared + * `unsortedPlanes` dag::Vector (including reserve/HeapReAlloc) without any lock. + * + * Online clients skip this path (isHost == 0), and retail matches rarely run 31 bot aircraft on + * one host. In LocalMP air battles, multiple same-team bots finishing tasks right after an + * aircraft crash race on the vector and corrupt the Windows heap (0xC0000374). + * + * Wrapping the function with an exclusive SRWLOCK on LocalMP hosts keeps parallel physics intact + * while serializing mutations of `unsortedPlanes`. The target performs no callbacks or re-entrant + * calls and acquires only the global unit-table lock after ours, so lock ordering is fixed. + */ +#include "hook_config.h" +#include "local_mp_internal.h" +#include "local_mp_values.h" + +typedef void (__fastcall *AddUnsortedPlaneFn)(void *team_mgr, uint32_t unit_no); + +static AddUnsortedPlaneFn orig_add_unsorted_plane; +static SRWLOCK g_unsorted_planes_lock = SRWLOCK_INIT; + +static void __fastcall hook_add_unsorted_plane(void *team_mgr, uint32_t unit_no) +{ + if (!local_mp_is_host()) { + orig_add_unsorted_plane(team_mgr, unit_no); + return; + } + AcquireSRWLockExclusive(&g_unsorted_planes_lock); + orig_add_unsorted_plane(team_mgr, unit_no); + ReleaseSRWLockExclusive(&g_unsorted_planes_lock); +} + +int install_local_mp_air_ai_sync_hooks(void) +{ + const LocalMpHookSpec hook = { + "air_ai_add_unsorted_plane", + g_lmp.air_ai_add_unsorted_plane, + (void *)&hook_add_unsorted_plane, + (void **)&orig_add_unsorted_plane, + }; + return local_mp_queue_hooks("local_mp_air_ai_sync", &hook, 1); +} diff --git a/native/wto-net/src/local_mp_core.c b/native/wto-net/src/local_mp_core.c index 8b47fba..f114b56 100644 --- a/native/wto-net/src/local_mp_core.c +++ b/native/wto-net/src/local_mp_core.c @@ -145,6 +145,7 @@ static const LocalMpModule MODULES[] = { { "local_mp_damage_notifications", install_local_mp_damage_notification_hooks }, { "local_mp_air_events", install_local_mp_air_event_hooks }, { "local_mp_ground_ai", install_local_mp_ground_ai_hooks }, + { "local_mp_air_ai_sync", install_local_mp_air_ai_sync_hooks }, }; static void use_core_values(void) diff --git a/native/wto-net/src/local_mp_internal.h b/native/wto-net/src/local_mp_internal.h index a0c84b6..9ec8859 100644 --- a/native/wto-net/src/local_mp_internal.h +++ b/native/wto-net/src/local_mp_internal.h @@ -112,3 +112,4 @@ void local_mp_zones_reset(void); int install_local_mp_air_event_hooks(void); int install_local_mp_ground_ai_hooks(void); void local_mp_ground_ai_update(float dt); +int install_local_mp_air_ai_sync_hooks(void); diff --git a/native/wto-net/src/local_mp_kills.c b/native/wto-net/src/local_mp_kills.c index b6259b4..3ced280 100644 --- a/native/wto-net/src/local_mp_kills.c +++ b/native/wto-net/src/local_mp_kills.c @@ -147,9 +147,12 @@ static void process_unit_death(uint8_t *victim, uint8_t *killer_mp, int32_t kill } } +/* Stack buffer for Unit::chooseOffender; stock writes 0x124 bytes on 2.61.0.37/.58/.60. */ +#define LMP_OFFENDER_BUF_SIZE 0x180u + static uint8_t *resolve_offender_killer(uint8_t *victim, int32_t *out_army) { - __declspec(align(16)) uint8_t offender[0x180]; + __declspec(align(16)) uint8_t offender[LMP_OFFENDER_BUF_SIZE]; uintptr_t vtbl = victim ? *(uintptr_t *)victim : 0; ChooseOffenderVfn choose_fn; @@ -209,6 +212,11 @@ int install_local_mp_kill_hooks(void) { "on_unit_dead_once", g_lmp.on_unit_dead_once, (void *)&hook_on_unit_dead_once, (void **)&orig_on_unit_dead_once }, }; + if (LMP_OFFENDER_STRIDE > LMP_OFFENDER_BUF_SIZE) { + log_line("local_mp: Offender stride 0x%zx exceeds the 0x%x-byte stack buffer", + LMP_OFFENDER_STRIDE, LMP_OFFENDER_BUF_SIZE); + return 0; + } fn_mplayer_add_score = (MPlayerAddScoreFn)g_lmp.mplayer_add_score; fn_net_mark_dirty = (NetMarkDirtyFn)g_lmp.net_mark_dirty; diff --git a/native/wto-net/src/local_mp_values.c b/native/wto-net/src/local_mp_values.c index 2fd244a..f9c0fba 100644 --- a/native/wto-net/src/local_mp_values.c +++ b/native/wto-net/src/local_mp_values.c @@ -43,6 +43,7 @@ typedef struct LmpValueSpec { #define DAMAGE_NOTICES "local_mp_damage_notifications" #define AIR_EVENTS "local_mp_air_events" #define GROUND_AI "local_mp_ground_ai" +#define AIR_AI_SYNC "local_mp_air_ai_sync" static const LmpValueSpec SPECS[] = { A(ZONES, "apply_zone_damage", apply_zone_damage), @@ -200,6 +201,8 @@ static const LmpValueSpec SPECS[] = { A(GROUND_AI, "ground_ai_drive_to_pos_update", ground_ai_drive_to_pos_update), O(GROUND_AI, "ground_ai_move_properties.move_system", ground_ai_move_system), O(GROUND_AI, "ground_ai_move_annotation.inactive", ground_ai_move_inactive), + + A(AIR_AI_SYNC, "air_ai_add_unsorted_plane", air_ai_add_unsorted_plane), }; static void store(const LmpValueSpec *spec, int64_t value, uint8_t *base) diff --git a/native/wto-net/src/local_mp_values.h b/native/wto-net/src/local_mp_values.h index 2c79ad3..9dc8f25 100644 --- a/native/wto-net/src/local_mp_values.h +++ b/native/wto-net/src/local_mp_values.h @@ -95,6 +95,8 @@ typedef struct LmpValues { /* local_mp_ground_ai */ uint8_t *ground_ai_mission_action, *ground_ai_as_ground_model, *ground_ai_drive_to_pos_update; size_t ground_ai_move_system, ground_ai_move_inactive; + /* local_mp_air_ai_sync */ + uint8_t *air_ai_add_unsorted_plane; } LmpValues; extern LmpValues g_lmp; diff --git a/patches/native/local_mp_air_ai_sync.jsonc b/patches/native/local_mp_air_ai_sync.jsonc new file mode 100644 index 0000000..8b4b9e5 --- /dev/null +++ b/patches/native/local_mp_air_ai_sync.jsonc @@ -0,0 +1,28 @@ +// Serialize concurrent updates to the team AI unsortedPlanes list on a LocalMP host. +// The local server cannot add locking inside the client's parallel aircraft physics threads. +// Without it: concurrent HeapReAlloc calls on the shared vector corrupt the heap (0xC0000374). +{ + "module": "local_mp_air_ai_sync", + "features": [ + "K-AI opponents and teammates" + ], + "requires": [ + "local_mp_core" + ], + "sites": { + // TeamAiManager::addToUnsortedPlanes; unique reference to "unsortedPlanes". + // Verified at 2.61.0.37 0x807be0, 2.61.0.58 0x80a2b0, 2.61.0.60 0x80a150. + "air_ai_add_unsorted_plane": { + "variants": [ + { + "written_for": "2.61.0.37", + "locate": { + "method": "string_ref", + "string": "unsortedPlanes", + "target": "function" + } + } + ] + } + } +}