Merged
Conversation
v1.6.5 Update (patch)
…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.
fix: resolve OPUS_BAD_ARG on ARM64 Apple Silicon due to variadic CTL calling convention
- Windows: map CMake arch names (Win32, x64, ARM64) to vcvarsall equivalents (x86, amd64, amd64_arm64) for cross-compilation - Linux x86: pass -m32 flag to gcc so the shim is compiled as 32-bit to match the i386 libopus.a static archive
Add explicit CRT libraries (ucrt.lib, vcruntime.lib, msvcrt.lib) to the Windows link step. The x86 SEH handler symbol __except_handler4_common lives in vcruntime.lib and isn't automatically pulled in when manually invoking link.exe with a static opus.lib built by CMake with /MD.
fix: correct OpusCompile workflow for shim linking on Windows and Linux x86
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.
No description provided.