Skip to content

Fix VM_Call crash in DLL mode when nargs is 0#66

Open
742617000027 wants to merge 1 commit intoDefrag-racing:mainfrom
742617000027:fix/vm-call-nargs-zero
Open

Fix VM_Call crash in DLL mode when nargs is 0#66
742617000027 wants to merge 1 commit intoDefrag-racing:mainfrom
742617000027:fix/vm-call-nargs-zero

Conversation

@742617000027
Copy link
Copy Markdown

Summary

Fixes a crash (SIGBUS) on ARM64 when using native DLL cgame modules (e.g. cgame-proxymod).

On ARM64 with -O2, the compiler transforms the for loop in VM_Call's DLL path into a do-while loop without a zero-trip guard. When nargs is 0 (used by CG_CONSOLE_COMMAND and CG_SHUTDOWN), the va_arg copy loop never terminates and reads past the stack into the guard page, causing a SIGBUS crash.

The VM interpreter path already handles this correctly — the compiled code includes a cmp w1, #1; b.lt skip guard before its copy loop.

Fix

Zero-initialize the args array with memset. This gives the compiler a separate initialization path that prevents it from eliminating the loop guard during optimization.

Reproduction

  1. Build for macOS ARM64 (Apple Silicon)
  2. Load a native cgame DLL (e.g. cgame-proxymod with vm_cgame 0)
  3. Enter a map and trigger any console command routed to cgame (e.g. weapon switch)
  4. Engine crashes with Received signal 10 (SIGBUS)

See also: Jelvan1/cgame-proxymod#14

On ARM64, the compiler transforms the va_arg copy loop into a do-while
(check at bottom) without a zero-trip guard. When nargs is 0, the loop
never terminates and reads past the stack into the guard page, causing
SIGBUS.

This affects any VM_Call with nargs=0 in DLL mode, such as
CG_CONSOLE_COMMAND and CG_SHUTDOWN. The VM interpreter path already
handles this correctly with a `cmp w1, Defrag-racing#1; b.lt skip` guard.

Zero-initialize the args array so the memset gives the compiler a
separate initialization path, preventing it from folding the guard
into the loop and eliminating it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant