Skip to content
Snippets Groups Projects
Commit ae0b2286 authored by Jaswant Panchumarti (Kitware)'s avatar Jaswant Panchumarti (Kitware)
Browse files

Add option for exception catching in wasm

- Adds `VTK_WEBASSEMBLY_EXCEPTIONS` cmake setting to
    enable exceptions at compile+link time with `-fexceptions`.
- This setting will allow the C++ unit tests to print additional stack traces
    when an uncaught exception is thrown, at the expense of greater binary size and slower execution.
- The `ENABLE_EXCEPTION_CATCHING` option requires a list of functions from which exceptions may be thrown.
    This commit allows all functions to throw exception catching with `DISABLE_EXCEPTION_CATCHING=0`
    as we cannot know all possible functions that could throw exceptions.
parent 20d58903
No related branches found
No related tags found
No related merge requests found
......@@ -18,6 +18,16 @@ if(CMAKE_SYSTEM MATCHES "SunOS.*")
endif()
if (CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
if (VTK_WEBASSEMBLY_EXCEPTIONS)
# Enable exceptions because VTK and third part code rely on C++ exceptions.
# Allow C++ to catch exceptions. Emscripten disables it by default due to high overhead.
# Generate helper functions to get stack traces for uncaught exceptions
set(VTK_REQUIRED_CXX_FLAGS "${VTK_REQUIRED_CXX_FLAGS} -fexceptions -sDISABLE_EXCEPTION_CATCHING=0")
set(VTK_REQUIRED_C_FLAGS "${VTK_REQUIRED_C_FLAGS} -fexceptions -sDISABLE_EXCEPTION_CATCHING=0")
set(VTK_REQUIRED_EXE_LINKER_FLAGS "${VTK_REQUIRED_EXE_LINKER_FLAGS} -fexceptions -sDISABLE_EXCEPTION_CATCHING=0 -sEXCEPTION_STACK_TRACES=1")
set(VTK_REQUIRED_SHARED_LINKER_FLAGS "${VTK_REQUIRED_SHARED_LINKER_FLAGS} -fexceptions -sDISABLE_EXCEPTION_CATCHING=0 -sEXCEPTION_STACK_TRACES=1")
set(VTK_REQUIRED_MODULE_LINKER_FLAGS "${VTK_REQUIRED_MODULE_LINKER_FLAGS} -fexceptions -sDISABLE_EXCEPTION_CATCHING=0 -sEXCEPTION_STACK_TRACES=1")
endif ()
if (VTK_WEBASSEMBLY_THREADS)
# Remove after https://github.com/WebAssembly/design/issues/1271 is closed
# Set Wno flag globally because even though the flag is added in vtkCopmilerWarningFlags.cmake,
......
......@@ -16,7 +16,8 @@ else ()
set(CMAKE_CXX_SIZEOF_DATA_PTR 4)
endif ()
option(VTK_WEBASSEMBLY_THREADS "Enable threading support in wasm. Adds -pthread compile and link flags." ${default_wasm_threads})
option(VTK_WEBASSEMBLY_THREADS "Enable threading support in wasm. Adds -pthread compile and link flags." OFF)
option(VTK_WEBASSEMBLY_EXCEPTIONS "Enable exception support in wasm. Adds -fexceptions compile and link flags." OFF)
# wasm linking is already multithreaded. Here, we ensure targets are linked one at a time to avoid
# OOM errors and file lock contention.
......
......@@ -106,6 +106,11 @@ If everything went well then it should now be possible to compile and run the on
Head over to [Examples/Emscripten/Cxx/Cone/README.md](https://gitlab.kitware.com/vtk/vtk/-/blob/master/Examples/Emscripten/Cxx/Cone/README.md)
and test the simple Cone example.
## Exceptions
By default, emscripten disables exception catching and enables exception throwing because of the overhead in size and speed. The [wasm-exceptions proposal](https://github.com/WebAssembly/exception-handling/blob/master/proposals/exception-handling/Exceptions.md) aims to resolve this issue.
In VTK, you can configure exceptions with the CMake setting `VTK_WEBASSEMBLY_EXCEPTIONS` (default `OFF`). Please note that the WASM CI build scripts turn on exceptions for developer convenience, so that a stack trace can be obtained when a C++ unit test crashes due to an uncaught exception or `abort`.
## Multithreading
Multithreading can be enabled in VTK wasm by turning on the CMake setting `VTK_WEBASSEMBLY_THREADS`.
......
......@@ -46,9 +46,12 @@ Less common, but variables which may be of interest to some:
* `VTK_WEBASSEMBLY_64_BIT` (default `OFF`):
This option is applicable only when building with Emscripten toolchain.
Adds -sMEMORY64 compiler and linker flags.
* `VTK_WEBASSEMBLY_EXCEPTIONS` (default `OFF`):
This option is applicable only when building with Emscripten toolchain.
Adds `-fexceptions` compiler and linker flags.
* `VTK_WEBASSEMBLY_THREADS` (default `OFF`):
This option is applicable only when building with Emscripten toolchain.
Adds -pthread compiler and linker flags. When `VTK_BUILD_TESTING` is `ON`,
Adds `-pthread` compiler and linker flags. When `VTK_BUILD_TESTING` is `ON`,
this also runs unit tests in web workers, which is the only way for the tests
to reliably load data files without having to embed entire datasets inside
the test binaries.
......
# Add exception support for WebAssembly builds
- You can now enable exceptions in VTK wasm build with the `VTK_WEBASSEMBLY_EXCEPTIONS` option. Default value is `OFF`.
......@@ -28,7 +28,7 @@ list(APPEND CMAKE_MODULE_PATH "${VTK_TOP_DIR}/CMake")
# wasm32/wasm64. Import the flag selection logic from VTK source tree.
# For VTK_BUILD_TESTING, VTK_TESTING_WASM_ENGINE[,_ARGUMENTS]
include("${VTK_SOURCE_DIR}/CMake/vtkTesting.cmake")
# For VTK_WEBASSEMBLY_64_BIT, default_wasm_threads
# For VTK_WEBASSEMBLY_64_BIT, VTK_WEBASSEMBLY_THREADS and VTK_WEBASSEMBLY_EXCEPTIONS
include("${VTK_SOURCE_DIR}/CMake/vtkEmscripten.cmake")
# For -sMEMORY64=1, -pthread, etc.
include("${VTK_SOURCE_DIR}/CMake/vtkCompilerPlatformFlags.cmake")
......
......@@ -118,6 +118,13 @@
let message = `Uncaught rejection from ${event.promise}: ${event.reason}`;
console.error(message);
wasmStdOutErr.push(message);
if (event.reason !== undefined)
{
if (event.reason.stack !== undefined)
{
wasmStdOutErr.push(`Uncaught exception : ${event.reason.message}\n ${event.reason.stack}`);
}
}
finalize(1);
return true;
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment