rodrigodzf / Jack-Audio-For-Unity

Jack Audio support for Unity3d
MIT License
30 stars 7 forks source link

Dependency to libjack64.dll crashes Unity 2019 #15

Closed Schroedingers-Cat closed 3 years ago

Schroedingers-Cat commented 4 years ago

Opening the sample project included within this repository, Unity 2019 instantly crashes during project loading while accessing the dependency libjack64.dll. This is reproducible on https://github.com/rodrigodzf/Jack-Audio-For-Unity/commit/9d3a32c1a59ca282b4271bed8cd4d6f41d4fcbde but also all working previous commits are affected. The plugin from commit https://github.com/rodrigodzf/Jack-Audio-For-Unity/commit/9d3a32c1a59ca282b4271bed8cd4d6f41d4fcbde has been built using Jack 1.9.11 libraries and the exact version of this library is installed on the systems where Unity 2019 crashes. This rules out incompatible jack library versions. Unity 5-2018 are not affected by this behaviour. The platform is Windows 10 x64 1809 and 1903.

How to reproduce:

  1. Check out this repo at 9d3a32c1a59ca282b4271bed8cd4d6f41d4fcbde (current master)
  2. Install Jack 1.9.11 from here
  3. Open the included sample project with Unity 5-2018
  4. The Unity editor should load and open up
  5. Close the Unity editor
  6. Open the sample project with Unity 2019
  7. During project upgrade/loading, the editor will silently crash

Side note: It is also possible to delete any asset from the "Asset" directory except for the "Plugins" directory, ruling out any C# scripts as cause for the crash.

This project already uses the latest native audio SDK from Unity, so that cannot cause the problem.

Checking the editor log from "%localappdata%\Unity\Editor\Editor.log", the crash happens just after Unity found all compatible native plugins:

Refreshing native plugins compatible for Editor in 2009.80 ms, found 3 plugins.

When creating a debug version of the plugin with VS 2019 and setting up breakpoints to all available functions or constructors and starting the Unity 2019 editor with VS in debug mode, it seems that Unity actually never get's to run any line of code from the plugin. VS will instead report a crash in the external dependency of this plugin "libjack64.dll".

Removing any dependency to jack.h (like done here https://github.com/Schroedingers-Cat/Jack-Audio-For-Unity/commit/9aa82f0c698aedb4cf264b30dbe6b791a6fbe31d) fixes the crash for Unity 2019 (but also removes any functionality of this plugin).

Schroedingers-Cat commented 4 years ago

This also affects builds made with Unity. However, the latest version producing working builds with Jack is Unity 2017.1.5. It also happens with a minimal Unity project simply marshalling the libjack64.dll and calling jack_client_open() from a C# script/MonoBehaviour. In that case, the crash occurs when going into playmode. So:

I've created a minimal console application utilising the Jack 1.9.11 library on Windows 10 1903 x64 and Visual Studio 2019 (v142 platform toolset and Windows SDK 10.0.18362). That one crashes before the "hello world" line, too. MinimalJack-Console.zip
Building with the VS2017 toolchain (v141 platform toolset and Windows SDK 10.0.17763) and VS2015 toolchain (v140 platform toolset and Windows SDK 8.1) also crashes the console app instantly. I wonder what Unity 2018 and 2017 do differently to access the library without crashing.

Schroedingers-Cat commented 4 years ago

Disabling the address space layout randomization feature in the linker setting fixes the crash within the minimal jack console application:
grafik

That makes it likely that a sudden change in Unity's build system is causing this problem in affected versions. The way I see it, this is a problem with the jack library itself. I'll try to contact the jack developers.

Schroedingers-Cat commented 4 years ago

So it seems that the jack developers have been informed about this problem, but didn't do anything until this point: https://github.com/jackaudio/jack2/issues/332 Part of the problem might be that none of the current maintainers is able to build Jack on Windows.

For the time being, there is an insecure workaround on Windows 10:

  1. Open Windows Defender Security Center
  2. Select App & browser control from the sidebar
  3. Scroll down to and click on settings for exploit protection
  4. Disable Randomize memory allocations (Bottom-up ASLR)
  5. Restart Windows

Note: this option can also be set per-application, which might be desirable.

On Windows 7, the equivalent of this workaround seems to be setting the registry key HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management\MoveImages to 0 like explained here: https://stackoverflow.com/questions/9560993/how-do-you-disable-aslr-address-space-layout-randomization-on-windows-7-x64

dburongarcia commented 4 years ago

Hello, @Schroedingers-Cat I tried your workaround but Unity is still crashing whenever I try to enter in play mode with the test scenes. Unity just closes and it also closes Jack server. I tried with both the master and dev branch, and also just importing the JackAudioForUnity and copying the AudioPlugin-JackAudioForUnity.dll from Plugins in the repo. I'm using Windows 10 and Unity 2019.4.0f1. Any tips? Thanks

Schroedingers-Cat commented 4 years ago

This issue is about the crashes upon accessing the jack dll when opening a Unity project. Your problem seems to be related to crashing when running a scene, so it's likely a different issue. I've answered your question on the Unity forum.

dburongarcia commented 4 years ago

Hi, I will follow in Unity forum then. Thanks https://forum.unity.com/threads/multiple-audio-channels-to-multiple-audio-outputs.522738/

Schroedingers-Cat commented 3 years ago

So the solution is either https://github.com/rodrigodzf/Jack-Audio-For-Unity/issues/15#issuecomment-527897023 or to build Jack on Windows from Source Code with a more recent version of VS which has the ASLR feature implicitly enabled by default.

alfonso73 commented 3 years ago

Note: this option can also be set per-application, which might be desirable.

@Schroedingers-Cat thanks for the hint. Should this option be set for scsynth.exe, sclang.exe, qjackctl.exe and jack.exe? Or only on the SC side or Jack side? thanks

oops i realized the post is about Unity. But on SuperCollider Win10 64x there's the same exact problem. How did you change the option on an app base? On the Unity side or on the Jack side or both?

Schroedingers-Cat commented 3 years ago

You'll need to set the option for every executable that has a dependency to JACK. You can use a tool like this to find what libraries are necessary for a given software: https://github.com/lucasg/Dependencies

alfonso73 commented 3 years ago

Many thanks @Schroedingers-Cat !

alfonso73 commented 3 years ago

Should i set this option for .exe only or for .dll too?

Schroedingers-Cat commented 3 years ago

Should i set this option for .exe only or for .dll too?

I had to set the option only for the program loading the dll.

dyfer commented 3 years ago

FYI this has been fixed in Jack 1.9.17 - JackRouter has been recompiled and shouldn't cause this crash anymore, AFAIU.

Schroedingers-Cat commented 3 years ago

So great to see the devs have had the time to work on windows releases.