Placeholder-Software / Dissonance

Unity Voice Chat Asset
69 stars 5 forks source link

[bug] libopus crashes on iOS #277

Closed zenorbi closed 8 months ago

zenorbi commented 10 months ago

Context

I have a working project that runs on Windows, Linux, macOS both with Mono and IL2CPP. However when I target iOS Dissonance crashes even on a minimal (basically empty) project.

Expected behaviour

The iOS build doesn't crash.

Actual behaviour

The iOS build crashes as soon as Dissonance wants to encode the audio with opus.

Steps to Reproduce

  1. Open the attached minimal project
  2. Copy the contents of Dissonance 9.0.2 into the Dissonance folder
  3. Build and run the project for iOS

Your Environment

Technical info

* thread #40, stop reason = EXC_BAD_ACCESS (code=2, address=0x130000001b)
  * frame #0: 0x000000010734831c UnityFramework`ec_tell_frac
    frame #1: 0x0000000107345508 UnityFramework`quant_all_bands + 644
    frame #2: 0x00000001074d3b00 UnityFramework`celt_encode_with_ec + 10860
    frame #3: 0x00000001074fd9e4 UnityFramework`opus_encode_native + 11416
    frame #4: 0x00000001074fdd8c UnityFramework`encode_multiframe_packet + 464
    frame #5: 0x00000001074fbef8 UnityFramework`opus_encode_native + 4524
    frame #6: 0x00000001074fe690 UnityFramework`opus_encode_float + 328
    frame #7: 0x0000000107644814 UnityFramework`OpusEncoder_EncodeFloats_m86F9FB4A7D1FBB13BC2C67F7667AC28DC9074804 + 392
    frame #8: 0x0000000107646d38 UnityFramework`OpusEncoder_Encode_m0E7D45F63A87331B1392E901CFAE254AC36F6915 + 312
    frame #9: 0x0000000107650b04 UnityFramework`EncoderPipeline_EncodeFrames_mBF24715DBAB2BBC03591571163254932C41B5C5D + 652
    frame #10: 0x000000010765067c UnityFramework`EncoderPipeline_ReceiveMicrophoneData_m8BD2C15CB580FF91E7E20DBA9566844AC60FA5EA + 696
    frame #11: 0x000000010764a3dc UnityFramework`BasePreprocessingPipeline_SendSamplesToSubscribers_m69DA014DAF339F9E6178A596B0E8B3C9FCC23B0E + 796
    frame #12: 0x0000000107651fc0 UnityFramework`WebRtcPreprocessingPipeline_PreprocessAudioFrame_mC5F2F37091E3980383F4186D3612454B4D10A035 + 344
    frame #13: 0x0000000107649664 UnityFramework`BasePreprocessingPipeline_ThreadEntry_m4A4E94CF006A5B2E4428175FDEF1AEE53A3F64CD + 628
    frame #14: 0x0000000107f83d78 UnityFramework`ExecutionContext_RunInternal_mC37E85D6FC972DF219A00FE1EE34329D1D63CC94 + 408
    frame #15: 0x00000001075cb514 UnityFramework`il2cpp::vm::Runtime::InvokeWithThrow(MethodInfo const*, void*, void**) + 100
    frame #16: 0x00000001075cb45c UnityFramework`il2cpp::vm::Runtime::Invoke(MethodInfo const*, void*, void**, Il2CppException**) + 84
    frame #17: 0x000000010757fef4 UnityFramework`il2cpp::icalls::mscorlib::System::Threading::ThreadStart(void*) + 96
    frame #18: 0x000000010758ed68 UnityFramework`il2cpp::os::Thread::RunWrapper(void*) + 84
    frame #19: 0x0000000107591d3c UnityFramework`il2cpp::os::ThreadImpl::ThreadStartWrapper(void*) + 40
    frame #20: 0x00000001dbb72338 libsystem_pthread.dylib`_pthread_start + 116
martindevans commented 10 months ago

Thanks for the detailed bug report and reproduction project. @TomGillen is going to try it out on an iOS device to see if we can reproduce it.

the9an commented 10 months ago

@martindevans I have just purchased an asset on the Unity Asset Store. It runs well on Android, but when I tried running it on iPhone 14, it also crashes. Same above problem. I feel very disappointed. Please quickly fix this bug, as I am currently unable to use this asset. 🙏 🙏 🙏 🙏 🙏 🙏 🙏 🙏 🙏 🙏 🙏 🙏 🙏 🙏

martindevans commented 10 months ago

Tom tried but couldn't reproduce the problem (using an iPad), we're going to be trying to reproduce it again tomorrow (using an iPhone this time).

@the9an could you tell me some more details about your setup, to improve our chances of reproducing the issue?

the9an commented 10 months ago

@martindevans

Error trace ``` OS Version: iPhone OS 16.3.1 Release Type: User Exception Type: EXC_BREAKPOINT (SIGTRAP) Exception Codes: 0x0000000000000001, 0x0000000105bdb144 Termination Reason: SIGNAL 5 Trace/BPT trap: 5 Terminating Process: exc handler [34064] Triggered by Thread: 49 Thread 0 name: Dispatch queue: com.apple.main-thread Thread 0: 0 libsystem_kernel.dylib 0x2125b4da8 mach_msg2_trap + 8 1 libsystem_kernel.dylib 0x2125c7a1c mach_msg2_internal + 80 2 libsystem_kernel.dylib 0x2125c7c5c mach_msg_overwrite + 388 3 libsystem_kernel.dylib 0x2125b52ec mach_msg + 24 4 CoreFoundation 0x1d4b11ac4 __CFRunLoopServiceMachPort + 160 5 CoreFoundation 0x1d4b12d08 __CFRunLoopRun + 1232 6 CoreFoundation 0x1d4b17eb0 CFRunLoopRunSpecific + 612 7 GraphicsServices 0x20ed0d368 GSEventRunModal + 164 8 UIKitCore 0x1d700d668 -[UIApplication _run] + 888 9 UIKitCore 0x1d700d2cc UIApplicationMain + 340 10 UnityFramework 0x104cf5478 -[UnityFramework runUIApplicationMainWithArgc:argv:] + 87160 (main.mm:96) 11 voiceChat 0x10094fccc main + 31948 (main.mm:28) 12 dyld 0x1f3410960 start + 2528 <...> Thread 48 name: AURemoteIO::IOThread Thread 48: 0 libsystem_kernel.dylib 0x2125b4da8 mach_msg2_trap + 8 1 libsystem_kernel.dylib 0x2125c7a1c mach_msg2_internal + 80 2 libsystem_kernel.dylib 0x2125c7c5c mach_msg_overwrite + 388 3 libsystem_kernel.dylib 0x2125b52ec mach_msg + 24 4 libEmbeddedSystemAUs.dylib 0x23ca5c24c void* caulk::thread_proxy > >(void*) + 556 5 libsystem_pthread.dylib 0x222d196cc _pthread_start + 148 6 libsystem_pthread.dylib 0x222d18ba4 thread_start + 8 Thread 49 Crashed: 0 UnityFramework 0x105bdb144 webrtc::AudioProcessingImpl::~AudioProcessingImpl() + 0 1 UnityFramework 0x105b7ec68 webrtc::AudioProcessing::Create() + 68 2 UnityFramework 0x105b6f684 Dissonance_CreatePreprocessor + 64 3 UnityFramework 0x105c668a4 WebRtcPreprocessor_CreatePreprocessor_m14802D2D94C5B948D2B6F05AA5BC85B14433AEEC + 16279716 (DissonanceVoip2.cpp:27520) 4 UnityFramework 0x105c64750 WebRtcPreprocessor_Reset_m1095248B939AA0AED8B99521853EB4F1E48C52FC + 16271184 (DissonanceVoip2.cpp:27372) 5 UnityFramework 0x105c645c4 WebRtcPreprocessingPipeline_ApplyReset_mF42DB01F2380959DE081E33C4048651D47583B1D + 16270788 (DissonanceVoip2.cpp:26393) 6 UnityFramework 0x105c5b95c BasePreprocessingPipeline_ThreadEntry_m4A4E94CF006A5B2E4428175FDEF1AEE53A3F64CD + 16234844 (DissonanceVoip2.cpp:0) 7 UnityFramework 0x1069b2880 ExecutionContext_RunInternal_mFA922C000D78E36A9A994FFEA9493DA20240BB64 + 30222464 (mscorlib8.cpp:25100) 8 UnityFramework 0x105b0a688 il2cpp::vm::Runtime::InvokeWithThrow(MethodInfo const*, void*, void**) + 14853768 (Runtime.cpp:0) 9 UnityFramework 0x105b0a4d0 il2cpp::vm::Runtime::Invoke(MethodInfo const*, void*, void**, Il2CppException**) + 14853328 (Runtime.cpp:570) 10 UnityFramework 0x105ab97c0 il2cpp::icalls::mscorlib::System::Threading::ThreadStart(void*) + 14522304 (Thread.cpp:66) 11 UnityFramework 0x105ac9fc4 il2cpp::os::Thread::RunWrapper(void*) + 14589892 (Thread.cpp:0) 12 UnityFramework 0x105acccac il2cpp::os::ThreadImpl::ThreadStartWrapper(void*) + 14601388 (ThreadImpl.cpp:127) 13 libsystem_pthread.dylib 0x222d196cc _pthread_start + 148 14 libsystem_pthread.dylib 0x222d18ba4 thread_start + 8 Thread 49 crashed with ARM Thread State (64-bit): x0: 0x0000000112f09330 x1: 0x0000000000000000 x2: 0x0000000000000000 x3: 0x0000000000000000 x4: 0x0000000000000000 x5: 0x0000000000000000 x6: 0x0000000000000000 x7: 0x0000000000000000 x8: 0x0000000105bdb144 x9: 0x000000004d555458 x10: 0x0000000000000000 x11: 0x0000000000000000 x12: 0x0000000000000000 x13: 0x0000000000000000 x14: 0x0000000000000000 x15: 0x0000000000000000 x16: 0x000000030ec20f14 x17: 0x000000017133f000 x18: 0x0000000000000000 x19: 0x0000000112f09330 x20: 0x0000000000000003 x21: 0x0000000000000002 x22: 0x0000000000000001 x23: 0x0000000000000001 x24: 0x0000000000000001 x25: 0x00000000ffffffff x26: 0x0000000000000001 x27: 0x0000000000000001 x28: 0x0000000000000001 fp: 0x000000017133ec30 lr: 0x0000000105b7ec68 sp: 0x000000017133ec00 pc: 0x0000000105bdb144 cpsr: 0x80001800 far: 0x0000000105bbd33c esr: 0xf2000001 (Breakpoint) brk 1 Binary Images: 0x2125b4000 - 0x2125ebfeb libsystem_kernel.dylib arm64e ```
martindevans commented 10 months ago

Thanks for the extra details. That will help a lot tomorrow.

One thing that I noticed is that you have Photon Voice installed. In the past we have had issues on iOS caused by Dissonance and Photon Voice interfering with each other. So it might fix your issue if you uninstall Photon Voice.

the9an commented 10 months ago

@martindevans After I uninstalled Photon Voice, there were no more crashes on iOS. Thank you for the support.

martindevans commented 10 months ago

@zenorbi Just checking this isn't the same issue for you? As far as I can see you don't have Photon Voice installed in your project, so there's probably still an issue to be resolved.

zenorbi commented 10 months ago

@martindevans My issue is different from that, the example I have attached has no libraries besides the Unity base and Dissonance 9.0.2. I have just checked the project on an iPad Pro (11-inch) (4th generation) with iOS 16.6 installed. The iPad is freshly installed, not jailbroken and I just installed the repro project. As soon as I clicked Ok on the Microphone permission popup, the app crashed at the exact same place (ec_tell_frac, EXC_BAD_ACCESS) with the exact same call stack. If I then close the app (and detach the debugger), open it again from the iPad interface, once Unity loads the app crashes immediately.

We also tested this on an iPhone Pro (14th gen) with iOS 17.0 beta (21A5312c). Same exact behavior as with the iPad.

Let me know if I can help you in any way to reproduce the issue. I can also send you the complete unity folder with the generated xcode project as well, but that would include dissonance which you probably don't want to make public :D

martindevans commented 10 months ago

We weren't able to reproduce the issue today, but we think we may have an idea what the problem is. Assuming this is the problem we've rebuilt the libopus file with a potential fix. To keep the download link private I've created an issue in your private reproduction repo with the link.

zenorbi commented 10 months ago

I have tested both the repro project and our big project with the new libopus.a and I can happily report that this fixed the crashing. I am impressed how you managed to fix this without being able to reproduce it. Thank you for the support! Will this be published as a new version in the Unity Asset Store or should I mark this library version as custom made for us?

martindevans commented 10 months ago

That's good to hear, thanks for all the detailed. We'll be publishing this in the next version of Dissonance, so you don't need to maintain a fork with this file :)

the9an commented 10 months ago

@martindevans

In the past we have had issues on iOS caused by Dissonance and Photon Voice interfering with each other

For certain specific purposes, I want to retain Photon Voice 2 in my project, is there any way to prevent it from crashing?

martindevans commented 10 months ago

Unfortunately for now I don't think it's possible on iOS, the static linking makes things difficult. We might be able to apply the same fix to the preprocessor as we did for the encoder, but it's a lot more complex.

martindevans commented 10 months ago

@the9an We've compiled a version of the preprocessor which might fix the problem with Dissonance and PUN2 on iOS. Could you please email me (martin@placeholder-software.co.uk) and I'll send you back a version for testing.

Ago251 commented 8 months ago

Hi @martindevans, I have the same error but I use mirror network instead PUN2. The crash happens when I unmute mic.

Information: Unity: 2021.3.1f1 Dissonance: 8.3.1 Dissonance for Mirror Networking: 8.3.0 iOS: 16.6

martindevans commented 8 months ago

@Ago251 The fixes for the9an were included in the 9.0.3 release of Dissonance. does upgrading to that fix the issue for you?

Ago251 commented 8 months ago

The error with 9.0.3 version is fixed. Thank you!

Dioinecail commented 6 months ago

Hello, not sure if it's related but libopus crashes on iOS if you build an app with xcode 15+ I've had the exact same crash message EXC_BAD_ACCESS (code=2, address=0x130000001b) with callstack leading to opus_encode_native For some reason the app does not crash if built on xcode 14.3.1 Haven't tried upgrading Dissonance to the latest version because literally piled up with tasks right now :(

Info: Unity 2021.3.16f1 | 2021.3.21f1 xcode 15.0.1 Dissonance 9.0.2 macOS Sonoma 14.1.2

martindevans commented 6 months ago

The upgrade from 9.0.2 to 9.0.3 should be completely painless. I'd suggest trying that first since it is known to fix a pretty similar error.

If it doesn't work please open up a new issue with as much detail as possible and I'll have to investigate it after Christmas :)

Dioinecail commented 3 months ago

Finally was able to scrap some time to test libopus crash on the latest dissonance update and i can confirm that it works on xcode 15+ 👍