> Hi, > > is it possible to use UFFI and avoid crashing the image when the called > code segfaults? > > Yes its possible. When I was exploring the means to live code with C++ I came to specific implementations of live coding for C++. One of them explored a feature called "Hardware exceptions" .Hardware exceptions are very similar to regular exception with the big difference that are handled by the CPU and not the language or the OS.
Crashes is a standard behavior that is imposed by the OS kernel in the case that an app tries to access memory is not allowed to access or access it in the way it is not allowed to access it. An exception will capture this behavior it wont allow the application to crash. Hardware exceptions are a standard feature for most CPUs and obviously they are not implemented at language level. For example you can find documentation about Hardware exceptions on Windows at https://msdn.microsoft.com/en-us/library/windows/desktop/ms680657(v=vs.85).aspx This means the C code you will be calling with UFFI will have to be wrapped inside a Hardware exception. When a crash is suppose to happen inside code that is wrapped with a hardware exception, the error (seffault in your example) will be displayed in the console but the application wont crash it will continue to execute depending on how you handle the exception. Generally hardware exception are to be avoided because they can hide bugs because the OS not always reports and can crash an app without an error in that you will be oblivious to what went wrong, when and how as the crash will not happen. > In other words, can I somehow wrap the call and throw in-image > Exception/Error on failure instead of the whole thing crashing? > > Its possible if you make a C function that contains a hardware exception take as argument a pointer to a specific function and then calls it inside the exception. > Or is the only way to do that have the called thing running as a separate > app (e.g. via OSSubProcess) and communicate via pipes or sockets? > It wont make a diffirence if the crash is caused by UFFI it will still crash Pharo that applies even if you use hardware exceptions , so you need always to be careful. Plus Pharo VM at any point can crash for its own reason, that is completely unrelated to what you are doing Thanks, > Peter > So yes its possible, but you still need to be careful.