tana / Wasm3DotNet

A .NET binding for wasm3
MIT License
19 stars 2 forks source link

Unity project with wasm3 x64 DLL causes crashed in Unity. #2

Open ku6ryo opened 3 years ago

ku6ryo commented 3 years ago

As I wrote in an issue, I'm trying to use this lib in Unity. https://github.com/tana/Wasm3DotNet/issues/1

By the change by @tana (thanks) , it became that DLL x64 build is successfully created. However, I faced crashes when I run a Unity project.

Condition

Steps

  1. built x64 DLL
  2. copied it to Unity Asset folder
  3. Run project on Unity Editor

My Code

            using (var environment = new Wasm3DotNet.Wrapper.Environment())
            {
                runtime = new Runtime(environment);
                var module = runtime.ParseModule(wasm);

                module.LinkRawFunction("externals", "print_add", "i(ii)", LogInt);

                runtime.LoadModule(module);
                var func = runtime.FindFunction("test");
                func.Call(10);
            }

Log


[MODES] ModeService[default].RefreshMenus
[MODES] ModeService[default].UpdateModeMenus
Stacktrace:

  at <unknown> <0xffffffff>
  at (wrapper managed-to-native) Wasm3DotNet.NativeFunctions.m3_LinkRawFunction (Wasm3DotNet.IM3Module,string,string,string,Wasm3DotNet.M3RawCall) [0x00057] in <e1fd07aa3797446eb17278fcde37f707>:0
  at Wasm3DotNet.Wrapper.Module.LinkRawFunction (string,string,string,Wasm3DotNet.M3RawCall) [0x00000] in C:\Users\ryokuro\Development\UnityScriptingWASM\Unity\Assets\Wasm3DotNet\Wasm3DotNet\Wrapper\Module.cs:18
  at ConnectorWasm3/<>c__DisplayClass7_0.<Load>b__0 (UnityEngine.AsyncOperation) [0x0005e] in C:\Users\ryokuro\Development\UnityScriptingWASM\Unity\Assets\Sandbox\ConnectorWasm3.cs:51
  at UnityEngine.AsyncOperation.InvokeCompletionEvent () [0x00016] in <c7864a0eaeb24b2a999fb177623d54b4>:0
  at (wrapper runtime-invoke) object.runtime_invoke_void__this__ (object,intptr,intptr,intptr) [0x00020] in <695d1cc93cca45069c528c15c9fdd749>:0

=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

Received signal SIGSEGV
Stack trace:
0x00007ff95c2ab325 (wasm3) [m3_env.c:1000] AcquireCodePageWithCapacity 
0x00007ff95c278031 (wasm3) [m3_bind.c:134] LinkRawFunction 
0x00007ff95c277f18 (wasm3) [m3_bind.c:174] FindAndLinkFunction 
0x00007ff95c27860b (wasm3) [m3_bind.c:200] m3_LinkRawFunction 
0x00000180b10ff206 (Mono JIT Code) (wrapper managed-to-native) Wasm3DotNet.NativeFunctions:m3_LinkRawFunction (Wasm3DotNet.IM3Module,string,string,string,Wasm3DotNet.M3RawCall)
0x00000180b10ff043 (Mono JIT Code) [Module.cs:18] Wasm3DotNet.Wrapper.Module:LinkRawFunction (string,string,string,Wasm3DotNet.M3RawCall) 
0x00000180b10fe13b (Mono JIT Code) [ConnectorWasm3.cs:53] ConnectorWasm3/<>c__DisplayClass7_0:<Load>b__0 (UnityEngine.AsyncOperation) 
0x00000180b10fdf0b (Mono JIT Code) UnityEngine.AsyncOperation:InvokeCompletionEvent ()
0x00000180afd1d760 (Mono JIT Code) (wrapper runtime-invoke) object:runtime_invoke_void__this__ (object,intptr,intptr,intptr)
0x00007ff955a2e630 (mono-2.0-bdwgc) [mini-runtime.c:2812] mono_jit_runtime_invoke 
0x00007ff9559b2ac2 (mono-2.0-bdwgc) [object.c:2921] do_runtime_invoke 
0x00007ff9559bbb1f (mono-2.0-bdwgc) [object.c:2968] mono_runtime_invoke 
0x00007ff66cbbe844 (Unity) scripting_method_invoke
0x00007ff66cbb7195 (Unity) ScriptingInvocation::Invoke
0x00007ff66c482efb (Unity) AsyncOperation::InvokeCoroutine
0x00007ff66d4b7b2c (Unity) UnityWebRequestAsyncOperation::InvokeCoroutine
0x00007ff66d4b7d11 (Unity) UnityWebRequestProto<UnityWebRequestTransport,AtomicRefCounter,RedirectHelper,ResponseHelper,DownloadHandler,UploadHandler,CertificateHandler,HeaderHelper,AsyncOperation>::Job_InvokeCoroutine
0x00007ff66c37242a (Unity) BackgroundJobQueue::ExecuteMainThreadJobs
0x00007ff66c4b9189 (Unity) `InitPlayerLoopCallbacks'::`2'::EarlyUpdateExecuteMainThreadJobsRegistrator::Forward
0x00007ff66c49b41c (Unity) ExecutePlayerLoop
0x00007ff66c49b4f3 (Unity) ExecutePlayerLoop
0x00007ff66c4a23a9 (Unity) PlayerLoop
0x00007ff66d8cc841 (Unity) PlayerLoopController::UpdateScene
0x00007ff66d8ca4d6 (Unity) Application::TickTimer
0x00007ff66e23a711 (Unity) MainMessageLoop
0x00007ff66e23e751 (Unity) WinMain
0x00007ff6700c67b6 (Unity) __scrt_common_main_seh
0x00007ffa1b7f54e0 (KERNEL32) BaseThreadInitThunk
0x00007ffa1bdc485b (ntdll) RtlUserThreadStart

UnityWebRequestProto<UnityWebRequestTransport,AtomicRefCounter,RedirectHelper,ResponseHelper,DownloadHandler,UploadHandler,CertificateHandler,HeaderHelper,AsyncOperation>::Job_InvokeCoroutine
0x00007ff66c37242a (Unity) BackgroundJobQueue::ExecuteMainThreadJobs
0x00007ff66c4b9189 (Unity) `InitPlayerLoopCallbacks'::`2'::EarlyUpdateExecuteMainThreadJobsRegistrator::Forward
0x00007ff66c49b41c (Unity) ExecutePlayerLoop
0x00007ff66c49b4f3 (Unity) ExecutePlayerLoop
0x00007ff66c4a23a9 (Unity) PlayerLoop
0x00007ff66d8cc841 (Unity) PlayerLoopController::UpdateScene
0x00007ff66d8ca4d6 (Unity) Application::TickTimer
0x00007ff66e23a711 (Unity) MainMessageLoop
0x00007ff66e23e751 (Unity) WinMain
0x00007ff6700c67b6 (Unity) __scrt_common_main_seh
0x00007ffa1b7f54e0 (KERNEL32) BaseThreadInitThunk
0x00007ffa1bdc485b (ntdll) RtlUserThreadStart
tana commented 3 years ago

I tried running Wasm3DotNet on Unity 2020.3.0f1, but could not reproduce the problem.

I think this crash happened because version of managed and native library were not matched. Could you build both wasm3.dll (native) and Wasm3DotNet.dll (managed) using same version of this repository, then copy both DLLs into Unity?

tana commented 3 years ago

By the way, the Environment object has to be retained during using any functionalities of wasm3. So it is better to move the variable environment in your code from an using statement to a field. For example:

public class Foo : MonoBehaviour
{
    Wasm3DotNet.Wrapper.Environment environment;
    Runtime runtime;
    public void Start()
    {
        environment = new Wasm3DotNet.Wrapper.Environment();
        runtime = new Runtime(environment);
        var module = runtime.ParseModule(wasm);

        module.LinkRawFunction("externals", "print_add", "i(ii)", LogInt);

        runtime.LoadModule(module);
        var func = runtime.FindFunction("test");
        func.Call(10);
    }
}
ku6ryo commented 3 years ago

OK, thank you for checking. I will try again.

ku6ryo commented 3 years ago

I created DLL of wasm3 and Wasm3DotNet for x64 and tried to run your test.wasm in this repo but sees the same crash log. Your environment and my environment might be a bit different and it can be the cause.

tana commented 3 years ago

Maybe wasm3_dll/wasm3 submodule in your local copy was not updated. Could you try git pull and git submodule update then build DLLs again?

If it does not work, could you try the following ZIP file? This is a copy of entire Unity project I made to test Wasm3DotNet on Unity. It contains DLLs built on my environment. https://1drv.ms/u/s!AvizOnmg-QO6hqhXRqBddisbG_NB5w?e=bccZTa

ku6ryo commented 3 years ago

Cool thanks. I will try !