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

Introduce signal handling #157

Merged
merged 5 commits into from
Aug 6, 2023
Merged

Introduce signal handling #157

merged 5 commits into from
Aug 6, 2023

Conversation

jow-
Copy link
Owner

@jow- jow- commented Jul 11, 2023

This PR introduces the infrastructure for handling UNIX signals inside ucode.

@jow- jow- force-pushed the signal-handling branch 3 times, most recently from ad3b802 to 2e5e01e Compare July 12, 2023 09:48
jow- added 5 commits July 27, 2023 13:59
Introduce the basic facilities to implement UNIX process signal handling
in ucode. The VM structure is extended by a bitmap keeping track of
received signal numbers, a sigaction structure which holds a generic
signal handler to update the bitmap, a pipe where the generic signal
handler will send received signal numbers to and an array of managed
signal handler functions, indexed by signal number.

Additionally, three new C API functions are added to control signal
delivery in the VM instance:

 - `uc_vm_signal_dispatch()`
   This function invokes signal handler callbacks for each received
   signal number, clears the bitmap and empties the pipe. The VM itself
   will invoke this function after each executed bytecode instruction.
   In some cases however it is useful for C code driving the VM to
   force immediate signal delivery, e.g. from within long running C
   functions that do not return to ucode (to the next bytecode
   instruction) in a timely manner.

 - `uc_vm_signal_raise()`
   This function will deliver the given signal number, so that it is
   picked up by the next call to `uc_vm_signal_dispatch()`. It is used
   by the generic C signal handler function to forward received signals
   to the VM instance.

 - `uc_vm_signal_notifyfd()`
   This functions returns the read end of the internal signal pipe.
   It is mainly useful for integration into select or poll based event
   loops, in order to be notified when there's pending signals for
   delivery into the VM instance.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
In order to prevent a premature release of the managed ucode signal handler
callbacks, ensure to treat the containing array as GC root to mark the
function values as reachable.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Enable signal dispatching by default for standalone ucode programs.

Also adjust the `gc()` testcase output as the default number of
allocations with enabled signal dispatching changes slightly.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
The `signal()` standard library function provides functionality for
registering signal handler callbacks, restoring default signal behaviour
or ignoring specific signals.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
When the VM instance loading the uloop module has signal dispatching
enabled, implicitly register the signal pipe as file descriptor in the
global uloop and dispatch received signals to the VM instance.

This ensures that the respective managed ucode signal handlers are
invoked immediately and not just after `uloop_run()` returns.

Also end the uloop in case any invoked signal handler threw an
exception, to match the expected behaviour with other kinds of
callbacks.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
@jow- jow- force-pushed the signal-handling branch from 2e5e01e to 2593270 Compare July 27, 2023 12:48
@jow- jow- marked this pull request as ready for review August 6, 2023 23:00
@jow- jow- merged commit a383154 into master Aug 6, 2023
@jow- jow- deleted the signal-handling branch August 6, 2023 23:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant