Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix exception handling (throw) in xeus-cpp-lite. #222

Open
anutosh491 opened this issue Jan 15, 2025 · 7 comments
Open

Fix exception handling (throw) in xeus-cpp-lite. #222

anutosh491 opened this issue Jan 15, 2025 · 7 comments
Labels
bug Something isn't working

Comments

@anutosh491
Copy link
Collaborator

anutosh491 commented Jan 15, 2025

I would expect this to work with what is there as of now but I see the following doesn't work

image

So out of all the steps (code -> PTU -> LLVM IR -> wasm object -> wasm binary -> loaded on top of main module using dlopen) we need to think which one is going wrong.

  1. The linking step through clang-repl is successful here and hence a shared wasm module has been created.

  2. But the dlopen step to load it on top of the main module is not working as expected. Hence the trace

image
@anutosh491 anutosh491 added the bug Something isn't working label Jan 15, 2025
@anutosh491
Copy link
Collaborator Author

The trace indicates that the loadDynamicLibrary function call fails from dlopenInternal leading to the error

        var dlopenInternal = (handle, jsflags) => {
            // void *dlopen(const char *file, int mode);
            // http://pubs.opengroup.org/onlinepubs/009695399/functions/dlopen.html
            var filename = UTF8ToString(handle + 36);
            var flags = HEAP32[(((handle) + (4)) >> 2)];
            filename = PATH.normalize(filename);
            var searchpaths = [];

            var global = Boolean(flags & 256);
            var localScope = global ? null : {};

            // We don't care about RTLD_NOW and RTLD_LAZY.
            var combinedFlags = {
                global,
                nodelete: Boolean(flags & 4096),
                loadAsync: jsflags.loadAsync,
            }

            if (jsflags.loadAsync) {
                return loadDynamicLibrary(filename, combinedFlags, localScope, handle);
            }

            try { // THIS TRY BLOCK FAILS !!!!
                return loadDynamicLibrary(filename, combinedFlags, localScope, handle)
            } catch (e) {
                err(`Error in loading dynamic library ${filename}: ${e}`);
                dlSetError(`Could not load dynamic lib: ${filename}\n${e}`);
                return 0;
            }
        }
        

@github-actions github-actions bot added the Needs triage Used in auto labelling of new issues label Jan 15, 2025
@anutosh491 anutosh491 removed the Needs triage Used in auto labelling of new issues label Jan 15, 2025
@anutosh491
Copy link
Collaborator Author

Also this appears in the console

image

@anutosh491
Copy link
Collaborator Author

Going through emscripten's flags they also provide an error like this. We might be missing the flag responsible for enabling exceptions somewhere.

Assertion failed: Exception thrown, but exception catching is not enabled. Compile with -sNO_DISABLE_EXCEPTION_CATCHING or -sEXCEPTION_CATCHING_ALLOWED=[..] to catch. (note: in dynamic linking, if a side module wants exceptions, the main module must be built with that support))

@mcbarton
Copy link
Collaborator

@anutosh491 As far as I can tell from the linked repo exception handling isn't currently supported by WebAssembly https://github.com/WebAssembly/exception-handling/tree/master , but the repo contains a proposal on how they hope to add support for it.

@anutosh491
Copy link
Collaborator Author

Hmm, weird. That shouldn't be the case cause as I wrote about the linking step is fine which means we are creating a wasm module out of the "throw" command. It's the loading that is not working as expected. So I am guessing we should be able to do this !

@anutosh491
Copy link
Collaborator Author

This is the wasm module being generated for throw("error")

(module $incr_module_2.wasm
  (memory $env.memory (;0;) (import "env" "memory") 1)
  (table $env.__indirect_function_table (;0;) (import "env" "__indirect_function_table") 0 funcref)
  (global $__memory_base (;0;) (import "env" "__memory_base") i32)
  (global $__table_base (;1;) (import "env" "__table_base") i32)
  (func $__cxa_allocate_exception (;0;) (import "env" "__cxa_allocate_exception") (param i32) (result i32))
  (func $__cxa_throw (;1;) (import "env" "__cxa_throw") (param i32 i32 i32))
  (global $typeinfo for char const* (;2;) (import "GOT.mem" "_ZTIPKc") (mut i32))
  (func $__wasm_call_ctors (;2;) (export "__wasm_call_ctors")
    call $_GLOBAL__sub_I_incr_module_2
  )
  (func $__stmts__0 (;3;)
    (local $var0 i32)
    (local $var1 i32)
    global.get $__memory_base
    local.set $var0
    i32.const 4
    call $__cxa_allocate_exception
    local.tee $var1
    local.get $var0
    i32.const 0
    i32.add
    i32.store
    local.get $var1
    global.get $typeinfo for char const*
    i32.const 0
    call $__cxa_throw
    unreachable
  )
  (func $_GLOBAL__sub_I_incr_module_2 (;4;)
    call $__stmts__0
  )
  (data (global.get $__memory_base) "error\00")
)

@mcbarton
Copy link
Collaborator

@anutosh491 The -fexceptions and -fwasm-exceptions flags may be relevant this PR (saw them in this issue emscripten-core/emscripten#22587 )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants