Skip to content

fix: resolve OPUS_BAD_ARG on ARM64 Apple Silicon due to variadic CTL calling convention#48

Merged
SineVector241 merged 1 commit intoAvionBlock:masterfrom
katruud:fix/arm64-apple-variadic-ctl
Apr 8, 2026
Merged

fix: resolve OPUS_BAD_ARG on ARM64 Apple Silicon due to variadic CTL calling convention#48
SineVector241 merged 1 commit intoAvionBlock:masterfrom
katruud:fix/arm64-apple-variadic-ctl

Conversation

@katruud
Copy link
Copy Markdown
Contributor

@katruud katruud commented Apr 8, 2026

Summary

On ARM64 Apple Silicon (macOS and iOS), all opus_*_ctl P/Invoke calls fail with OPUS_BAD_ARG because the C ABI for variadic functions differs from non-variadic functions on this platform.

  • Root cause: Apple's ARM64 ABI passes variadic arguments (those after ...) on the stack, while fixed arguments go in registers. C# P/Invoke marshals all parameters as fixed arguments in registers. The native opus library expects variadic args on the stack, reads garbage, and returns OPUS_BAD_ARG.
  • Scope: This affects only Apple ARM64 platforms (macOS Apple Silicon, iOS). On x86-64, Linux ARM64, and Android ARM64, the variadic and non-variadic calling conventions happen to be identical, so P/Invoke works correctly by coincidence.
  • Fix: Add a native C shim (opus_shim.c) with 20 non-variadic wrapper functions that correctly forward to the variadic opus CTL functions. The shim is compiled into the same native binary on all platforms, eliminating conditional compilation in C#.

Changes

  • OpusSharp.Natives/opus_shim.c (new) — Non-variadic wrappers for all CTL overloads:
    • opus_encoder_ctl (6 overloads)
    • opus_decoder_ctl (3 overloads)
    • opus_dred_decoder_ctl (2 overloads)
    • opus_multistream_encoder_ctl (6 overloads)
    • opus_multistream_decoder_ctl (3 overloads)
  • OpusSharp.Core/NativeOpus.cs — Added EntryPoint attributes to all 20 CTL P/Invoke declarations to call shim functions instead of variadic originals
  • OpusSharp.Core/StaticNativeOpus.cs — Same EntryPoint changes as NativeOpus.cs
  • .github/workflows/OpusCompile.yml — Updated all platform builds to:
    1. Build opus as a static library
    2. Compile the shim
    3. Link both into the final shared library (or add to static archive for iOS/Wasm)

Why all platforms and not just Apple ARM64?

Using the shim everywhere eliminates platform-specific conditional compilation in C#. The shim adds negligible overhead (a single function call indirection) and works correctly on every platform. This also future-proofs against any other platform that may adopt distinct variadic calling conventions.

Backwards compatibility

The C# method names and signatures are completely unchanged — only the native entry points they resolve to are different. No downstream calling code requires modification.

Tested locally on macOS ARM64

Built and tested locally against the Basis VR framework on macOS ARM64 (Apple Silicon):

  1. Built opus from source as a static library (-DBUILD_SHARED_LIBS=OFF -DCMAKE_POSITION_INDEPENDENT_CODE=ON)
  2. Compiled opus_shim.c and linked with -Wl,-force_load,libopus.a into a single opus.dylib
  3. Verified all 20 opussharp_* symbols are exported via nm -g opus.dylib | grep opussharp
  4. Replaced the osx-arm64/native/opus.dylib in the Basis Unity project and updated the C# P/Invoke entry points
  5. Entered Unity play mode and connected to a networking server

Before fix: OpusEncoder.Ctl(EncoderCTL.OPUS_SET_BITRATE, 32000) and OpusEncoder.Ctl(EncoderCTL.OPUS_SET_COMPLEXITY, 5) threw OPUS_BAD_ARG on every connection attempt, completely preventing voice audio initialization.

After fix: Encoder CTL calls succeed, audio transmission initializes without errors, and voice networking operates normally on macOS ARM64.

…calling convention

On ARM64 Apple Silicon (macOS and iOS), the C ABI passes variadic
arguments (those after `...`) on the stack, while fixed arguments are
passed in registers. The opus `_ctl` functions (opus_encoder_ctl,
opus_decoder_ctl, opus_multistream_encoder_ctl, etc.) are all variadic
C functions. When C# P/Invoke calls these functions, it marshals all
parameters as fixed arguments in registers. The native opus library
expects the variadic arguments on the stack, reads uninitialized memory
instead, and returns OPUS_BAD_ARG.

This issue only affects Apple ARM64 platforms. On x86-64, Linux ARM64,
and Android ARM64, the variadic and non-variadic calling conventions
are identical, so P/Invoke works correctly by coincidence.

The fix introduces a small C shim library (opus_shim.c) containing 20
non-variadic wrapper functions — one for each P/Invoke CTL overload.
Since the shim is compiled natively, the compiler correctly handles the
variadic forwarding to the underlying opus CTL functions. The shim is
compiled into the same native binary (opus.dylib, opus.dll, opus.so,
libopus.a) on all platforms, avoiding conditional compilation in C# and
ensuring consistent behavior everywhere.

Changes:
- Add OpusSharp.Natives/opus_shim.c with non-variadic wrappers for all
  opus_encoder_ctl, opus_decoder_ctl, opus_dred_decoder_ctl,
  opus_multistream_encoder_ctl, and opus_multistream_decoder_ctl
  overloads (20 functions total)
- Update NativeOpus.cs and StaticNativeOpus.cs to use EntryPoint
  attributes pointing to the shim function names (40 declarations each)
- Update OpusCompile.yml to build opus as a static library first, then
  compile the shim and link both into a single shared library for each
  platform (or add the shim object to the static archive for iOS/Wasm)

The C# method names and signatures are unchanged, so no calling code
in downstream projects requires modification.
Copy link
Copy Markdown
Member

@SineVector241 SineVector241 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@SineVector241 SineVector241 merged commit 607202b into AvionBlock:master Apr 8, 2026
4 checks passed
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.

2 participants