fix: resolve OPUS_BAD_ARG on ARM64 Apple Silicon due to variadic CTL calling convention#48
Merged
SineVector241 merged 1 commit intoAvionBlock:masterfrom Apr 8, 2026
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
On ARM64 Apple Silicon (macOS and iOS), all
opus_*_ctlP/Invoke calls fail withOPUS_BAD_ARGbecause the C ABI for variadic functions differs from non-variadic functions on this platform....) 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 returnsOPUS_BAD_ARG.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— AddedEntryPointattributes to all 20 CTL P/Invoke declarations to call shim functions instead of variadic originalsOpusSharp.Core/StaticNativeOpus.cs— SameEntryPointchanges as NativeOpus.cs.github/workflows/OpusCompile.yml— Updated all platform builds to: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):
-DBUILD_SHARED_LIBS=OFF -DCMAKE_POSITION_INDEPENDENT_CODE=ON)opus_shim.cand linked with-Wl,-force_load,libopus.ainto a singleopus.dylibopussharp_*symbols are exported vianm -g opus.dylib | grep opussharposx-arm64/native/opus.dylibin the Basis Unity project and updated the C# P/Invoke entry pointsBefore fix:
OpusEncoder.Ctl(EncoderCTL.OPUS_SET_BITRATE, 32000)andOpusEncoder.Ctl(EncoderCTL.OPUS_SET_COMPLEXITY, 5)threwOPUS_BAD_ARGon 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.