dotnet / dotnet-wasi-sdk

An SDK for building .NET projects as standalone WASI-compliant modules
MIT License
283 stars 12 forks source link

Is it possible to disable any optimisations and preserve stacktrace while building `.wasm`? #19

Open seclerp opened 1 year ago

seclerp commented 1 year ago

I'm currently looking into using .wasm as a standalone module to run on some custom WASI-compliant runtime. But it's impossible to debug any issues with calling native (.c) compiled functions because stacktrace with symbols is trimmed.

I also receive weird WASM exceptions like wasm trap: wasm 'unreachable' instruction executed which is almost impossible to diagnose.

seclerp commented 1 year ago

Answering my own question - it is possible, but very hard to do with the current hardcoded CLang args. See https://github.com/dotnet/dotnet-wasi-sdk/blob/main/src/Wasi.Sdk/build/Wasi.Sdk.targets#L217

Example:

  1. Stacktrace of the arbitrary unreachable exception with stripped symbols:

    0: error while executing at wasm backtrace:
           0: 0x4049c9 - <unknown>!<wasm function 7624>
           1: 0x3f5f3f - <unknown>!<wasm function 7301>
           2: 0x3f69c3 - <unknown>!<wasm function 7316>
           3: 0x3f6676 - <unknown>!<wasm function 7311>
           4: 0x3f6522 - <unknown>!<wasm function 7309>
           5: 0x3f676a - <unknown>!<wasm function 7313>
           6: 0x3f67f5 - <unknown>!<wasm function 7314>
           7: 0x29ff37 - <unknown>!<wasm function 4258>
           8: 0xf9fb9 - <unknown>!<wasm function 927>
           9: 0xe22e2 - <unknown>!<wasm function 712>
          10: 0xe2ef4 - <unknown>!<wasm function 720>
          11: 0x381b - <unknown>!<wasm function 48>
          12: 0x4180dc - <unknown>!<wasm function 7908>
    1: wasm trap: wasm `unreachable` instruction executed
  2. The same stacktrace with .wasm compiled without -Wl,-s:

    0: error while executing at wasm backtrace:
           0: 0x4049c9 - <unknown>!abort
           1: 0x3f5f3f - <unknown>!monoeg_assert_abort
           2: 0x3f69c3 - <unknown>!monoeg_log_default_handler
           3: 0x3f6676 - <unknown>!monoeg_g_logstr
           4: 0x3f6522 - <unknown>!monoeg_g_logv_nofree
           5: 0x3f676a - <unknown>!monoeg_assertion_message
           6: 0x3f67f5 - <unknown>!mono_assertion_message
           7: 0x29ff37 - <unknown>!mono_alc_get_default
           8: 0xf9fb9 - <unknown>!mono_assembly_load
           9: 0xe22e2 - <unknown>!mono_wasm_assembly_load
          10: 0xe2ef4 - <unknown>!lookup_dotnet_method
          11: 0x381b - <unknown>!count_vowels
          12: 0x4180dc - <unknown>!count_vowels.command_export
       note: using the `WASMTIME_BACKTRACE_DETAILS=1` environment variable may show more debugging information
    1: wasm trap: wasm `unreachable` instruction executed
  3. Using WASMTIME_BACKTRACE_DETAILS=1 (for Wasmtime runtime users):

    0: error while executing at wasm backtrace:
           0: 0x4049c9 - <unknown>!abort
           1: 0x3f5f3f - monoeg_assert_abort
                           at /home/runner/work/dotnet-wasi-sdk/dotnet-wasi-sdk/modules/runtime/src/mono/mono/eglib/goutput.c:57:3
           2: 0x3f69c3 - monoeg_log_default_handler
                           at /home/runner/work/dotnet-wasi-sdk/dotnet-wasi-sdk/modules/runtime/src/mono/mono/eglib/goutput.c:399:3
           3: 0x3f6676 - monoeg_g_logstr
                           at /home/runner/work/dotnet-wasi-sdk/dotnet-wasi-sdk/modules/runtime/src/mono/mono/eglib/goutput.c:151:2
           4: 0x3f6522 - monoeg_g_logv_nofree
                           at /home/runner/work/dotnet-wasi-sdk/dotnet-wasi-sdk/modules/runtime/src/mono/mono/eglib/goutput.c:166:2
           5: 0x3f676a - monoeg_assertion_message
                           at /home/runner/work/dotnet-wasi-sdk/dotnet-wasi-sdk/modules/runtime/src/mono/mono/eglib/goutput.c:207:22
           6: 0x3f67f5 - mono_assertion_message
                           at /home/runner/work/dotnet-wasi-sdk/dotnet-wasi-sdk/modules/runtime/src/mono/mono/eglib/goutput.c:226:2
           7: 0x29ff37 - mono_alc_get_default
                           at /home/runner/work/dotnet-wasi-sdk/dotnet-wasi-sdk/modules/runtime/src/mono/mono/metadata/assembly-load-context.c:81:2
           8: 0xf9fb9 - mono_assembly_load
                           at /home/runner/work/dotnet-wasi-sdk/dotnet-wasi-sdk/modules/runtime/src/mono/mono/metadata/assembly.c:2853:46
           9: 0xe22e2 - mono_wasm_assembly_load
                           at /home/runner/work/dotnet-wasi-sdk/dotnet-wasi-sdk/modules/runtime/src/mono/wasi/mono-wasi-driver/driver.c:542:22
          10: 0xe2ef4 - lookup_dotnet_method
                           at /home/runner/work/dotnet-wasi-sdk/dotnet-wasi-sdk/modules/runtime/src/mono/wasi/mono-wasi-driver/driver.c:816:30
          11: 0x381b - <unknown>!count_vowels
          12: 0x4180dc - <unknown>!count_vowels.command_export
    1: wasm trap: wasm `unreachable` instruction executed
seclerp commented 1 year ago

I'll make a PR if it works.