fix(battle): serialize LocalMP air AI unsortedPlanes updates

Add the local_mp_air_ai_sync native module to serialize calls to
TeamAiManager::addToUnsortedPlanes with an exclusive SRWLOCK on LocalMP
hosts.

Root cause (verified on 2.61.0.60, 2.61.0.58, and 2.61.0.37):
Aircraft physics and AI updates run concurrently across worker threads
via threadpool::parallel_for. When bot aircraft finish their behavior
tree (such as right after an aircraft crashes), AirAi::update tail-calls
TeamAiManager::addToUnsortedPlanes, which locks the global unit table
for lookup but mutates the team's shared unsortedPlanes
dag::Vector<uint32_t> (including reserve/HeapReAlloc) without a lock.
Under 32-bot LocalMP air battles, concurrent reallocations on this
vector corrupt the Windows heap (0xC0000374).

Also guard resolve_offender_killer in local_mp_kills.c with an
install-time check that LMP_OFFENDER_STRIDE <= LMP_OFFENDER_BUF_SIZE so
future struct growth skips cleanly instead of overflowing the stack
buffer.

Checks:
- MSVC /W4 build of wto_ws.dll (0 warnings in repository C files)
- Root and @wto/launcher npm run typecheck
- 15/15 native module resolution on 2.61.0.37 (0x807be0), 2.61.0.58
  (0x80a2b0), and 2.61.0.60 (0x80a150), checkPatchSet, wtotools
  check-patches on 2.61.0.60 (14/14), and temporary copy build
  verification (build-game-copy.ts)
- Pending live multi-match air/nuclear-escalation acceptance in an
  isolated copy.
This commit is contained in:
N0VAN0M4LY committed 2026-12-05 00:03:27 +08:00
1 parent 647eff35fe
commit fe29b5fe9c
8 files changed
+93 -2

No files matched your search

+1 -1
View File
@@ -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 $_) + '"' }
+48
View File
@@ -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<uint32_t> (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);
}
+1
View File
@@ -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)
+1
View File
@@ -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);
+9 -1
View File
@@ -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;
+3
View File
@@ -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)
+2
View File
@@ -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;