I'm aware that WASI doesn't support traditional signals. At present, when I intentionally set off an access violation, I get "RuntimeError: memory access out of bounds" and a traceback as Node.js exits.Is there a way to catch these errors and prevent Node.js exiting? Ideally, I'd be able to notify the test harness in some way that this had happened. If this involves JavaScript, please explain slowly and gently: I'm from the C world and new to web applications.
I'm porting a math library and its test harness to WebAssembly. The test harness is allowed to be specific to Node.js, but the library is not. Both are compiled from C and C++ code, which is my comfort zone. JavaScript is a language I don't know well and haven't done anything difficult in.I'm aware that WASI doesn't support traditional signals. At present, when I intentionally set off an access violation, I get "RuntimeError: memory access out of bounds" and a traceback as Node.js exits.Is there a way to catch these errors and prevent Node.js exiting? Ideally, I'd be able to notify the test harness in some way that this had happened. If this involves JavaScript, please explain slowly and gently: I'm from the C world and new to web applications.
The reason I'm asking this is that I will have to provide support to customers when the WebAssembly version of the library is released, and prefer to have my answers ready ahead of time. My employer wants to maintain their good reputation for customer service, and goes as far as having tests for deliberately-set-off runtime errors as part of routine testing, so that we can document what happens and how to handle them.Thanks very much,John
--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/emscripten-discuss/CAH1xqgnMY0%2BoOLMs51mo2x%2BBUhWdZq%3DkxFp-En7AX3OsVqcoQQ%40mail.gmail.com.
> 1. Wrap your calls in a JS try/catch and inspect the exception you caught> and then (somehow?) continue with the test suite. (This is what Brooke> suggested already)I have a couple of questions about that:When the catch gets called, is it called by the same thread as did the segmentation violation?
Is that thread's stack still intact? Has it been unwound?
If it is the same thread and the stack is intact, then I should be able to call the function within the library that does signal handling on other platforms, and have it do its longjmp()s. If those conditions don't apply, then things are going to get more difficult.
> 2. Install a global `onerror` handler that will catch all exceptions (much like the> global signal handler on linux). See https://nodejs.org/api/process.html#event-uncaughtexception.That says it's unsafe to resume, so that probably also applies to the case above?
> Is the test harness and the library-under-test designed to be compiled into the> same executable?Yes. We prefer to have the library-under-test be a shared object or Windows DLL, on platforms where that's possible, but we can have the harness and the library linked together, and that's what I'm planning to do for WebAssembly. I'm trying to avoid producing a JS wrapper for an API with hundreds of functions, hundreds of structs, and thousands of constants. It also passes lots of pointers to code and data through the interface. My customers who want a WebAssembly version of the library already have C/C++ or Swift code that calls it and want to use it that way.> i.e. on other platforms does it somehow catch and recover from sefaults?Yes.On platforms with signals, those are turned on for segmentation faults (and for some other signals, depending on the platform). The code is C, which sets regular checkpoints with setjmp() and the signal handling function longjmp()s to the latest checkpoint with a "test aborted" value. That's the basic idea, though it's rather more complicated in practice.
--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/emscripten-discuss/CAL_va29WgYahkgoafxhXGs%2BPhfpP-o6GzQSgaTA3xRGhdPKRNg%40mail.gmail.com.
To view this discussion visit https://groups.google.com/d/msgid/emscripten-discuss/CALJpS1Nzcxx0mzS%2BxHTxrETvyGiuppeOcz68PNdjwNtYaG0YcA%40mail.gmail.com.
-fwasm-exceptions
allow me to catch out-of-bounds exceptions in Emscripten-compiled C++? > OK, what I want to do isn't possible in Emscripten.It looks like it is, actually, but it's a bit complicated. Here's the scheme: am I requiring anything that doesn't exist?I have a great big math library that I want to make available in WebAssembly form, as a commercial product. It is written in C and C++, and runs on Android, iOS, Linux, macOS and Windows. Its heritage is from 1980s and 1990s technical computing, but it is still going strong as a commercial product. The immediate customers for it want to call it from C/C++ code that they already use on other platforms (mostly Linux and Windows), rather than from JavaScript.This math library has a test harness, also written in C and C++. The test cases are not written in C/C++, but in interpreted LISP, because that's quite suitable for handling the data that the library works with, plus it was easy to write our own portable implementation of the language.When you run the test harness, it looks for a LISP file to run and starts interpreting it, which usually starts by reading a lot more LISP files to set up a working environment. It then starts reading test scripts and making the calls to the math library that they request. The test harness does not exit until it has finished running everything it was given, or it hits an unrecoverable error. On its existing platforms, it can recover from SIGSEGV, SIGPFE, and other run-time errors, aborting the test that caused them and continuing with the subsequent test(s).That last part is the problem with a naive WebAssembly implementation, where the library and the test harness are statically linked together. That means there's no opportunity for a JavaScript error handler to get involved before exceptions caused by access violations come flying out of the top of the test harness. By then, the call stack is gone, and there's no way to recover from the exception and continue with the next test.That's where I'd got to when I wrote " . . . what I want to do isn't possible in Emscripten." I've had what seem like better ideas since then.WebAssembly "modules" are, as far as I understand, kind of like Windows DLLs or Linux shared libraries. There's a difference in that you can't link a module against other modules: inter-module calls have to go via JavaScript. Is that correct?I was slow to realise that I can put JavaScript error handlers into any JavaScript layer. To take advantage of that, I would link my math library as a module, and its test harness as the main program. The test harness would call JavaScript code, which would call the library from within a try/catch block, catch exceptions, call the library's "tidy up" functions, and return an error code to the test harness. That lets the test harness recover from exceptions and move on to the next test. It also means I need to create a JavaScript wrapper for the whole of the library's API, which is a bit of a big job, but makes the library more generally usable in WebAssembly code.While writing this mail,I realised a possible short-cut to let me get the test harness running more quickly, without having to generate a wrapper for hundreds of different functions, with thousands of associated data types and constants. It goes like this:The calls to the library from the test harness are generated by C macros, and I can add code to those for the WebAssembly build. So it appears I could use EM_ASM() to create JavaScript error handlers at each of those call sites, as part of the test harness source. I could then statically link the library and the test harness for running tests. Is that plausible?
To view this discussion visit https://groups.google.com/d/msgid/emscripten-discuss/CAL_va293zmaOT35XwQ6pM-Qed2ygWf_Q5SehWK0kJG2eBu9_Aw%40mail.gmail.com.
> Yes, there is nothing stopping you wrapping your function calls in JS try/catch using EM_ASM or EM_JS.> That was what I was trying to show up thread in my EM_ASM example. You don't need to do any kind of> dynamic linking to make this work.That sounds good, but I have more questions.> You could write a C function such as>> void* call_with_catch_handler( my_c_function, my_catch_handler_caller)>> and use EM_JS/EM_ASM to implement this. Any fatal errors could then be piped to your> `my_catch_handler_caller` function.Presumably, "my_catch_handler_caller" is a JavaScript function? I'm unfortunately having to deal with these issues with zero practical experience of using JavaScript for anything. My knowledge is all from the javascript.info tutorial. There are other people in the company who build and test WebAssembly stuff, but they don't even attempt to handle run-time errors. I'm porting a larger library to WebAssembly and need to do a bit better.
The practical difficulty with following your example is that I don't know how to pass the arguments into JavaScript to be used in the try block, or get values back out of JavaScript into WebAssembly. I presume this is necessary?I've found ccall() and cwrap() in the documentation: are they all there is, or are there higher-level functions? The bind.h documentation seems to indicate that it is not yet complete, and it's not immediately clear how much of it can be used for C data structures. ; Many of my C functions have arguments that are C structs, some are passed by value, and some have a mixture of types (I don't know yet if there are any that are both passed by value and have a mixture of types).
To view this discussion visit https://groups.google.com/d/msgid/emscripten-discuss/CAH1xqgkEd5E03%2BQewh0D86r2866Ro34M-nePeUFd-dTm-h7FeQ%40mail.gmail.com.