[PATCH 1/2] setjmp: Use BSD sematic as default for setjmp
Adhemerval Zanella Netto
adhemerval.zanella@linaro.org
Wed Aug 2 14:56:04 GMT 2023
On 02/08/23 11:43, Florian Weimer wrote:
> * Adhemerval Zanella Netto:
>
>>> Ahh, you mean because use removed the signal unblocking from abort?
>>>
>>> If the signal is blocked, it is not delivered before it is unblocked.
>>> This means that the handler will not observe it blocked.
>>>
>>> But POSIX says this:
>>>
>>> | The abort() function shall override blocking or ignoring the SIGABRT
>>> | signal.
>>>
>>> It also says:
>>>
>>> | The SIGABRT signal shall be sent to the calling process as if by means
>>> | of raise() with the argument SIGABRT.
>>>
>>> Strictly speaking, it is impossible to comply with both requirements,
>>> but I think the handler is expected to run even if SIGABRT is blocked.
>>> As far as I understand it, the new code terminates the process in this
>>> case, without ever running the handler.
>>
>> The later has been changed with a new clarification [1]:
>>
>> The SIGABRT signal shall be sent to the calling [CX]thread[/CX] as if by
>> means of raise() with the argument SIGABRT. [CX]If this signal does not
>> terminate the process (for example, if the signal is caught and the handler
>> returns), abort() may change the disposition of SIGABRT to SIG_DFL and send
>> the signal (in the same way) again. If a second signal is sent and it does
>> not terminate the process, the behavior is unspecified, except that the
>> abort() call shall not return.[/CX]
>>
>> [1] https://austingroupbugs.net/view.php?id=906#c5851
>
> Okay, I missed that change. So removing the unblocking should be okay
> after this specification change. I still don't see how the removal of
> unblocking changes the signal mask observed by the signal handler,
> though.
It is not by the signal handler, but rather after a SIGABRT handler issues
longjmp. With default flags (0), SIGABRT will continue to be blocked:
$ cat test.c
#include <assert.h>
#include <setjmp.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
static jmp_buf jb;
static void
sigabrt_handler (int sig)
{
longjmp (jb, 1);
}
int main (int argc, char *argv[])
{
sigset_t set;
assert (sigprocmask (SIG_BLOCK, NULL, &set) == 0);
printf ("SIGABRT blocked: %d\n", sigismember (&set, SIGABRT));
struct sigaction sa = { .sa_handler = sigabrt_handler, .sa_flags = 0 };
sigemptyset (&sa.sa_mask);
assert (sigaction (SIGABRT, &sa, 0) == 0);
if (setjmp (jb) == 0)
abort ();
printf ("first abort did not terminated\n");
assert (sigprocmask (SIG_BLOCK, NULL, &set) == 0);
printf ("SIGABRT blocked: %d\n", sigismember (&set, SIGABRT));
if (setjmp (jb) == 0)
abort ();
printf ("second abort did not terminated\n");
return 0;
}
$ gcc test.c -o test && ./test
SIGABRT blocked: 0
first abort did not terminated
SIGABRT blocked: 1
second abort did not terminated
By continuing to use _setjmp as 'setjmp' and not unblocking SIGABRT on abort
call, the process will be aborted regardless. This is due the difference
historically between BSD and SysV regarding whether setjmp/longjmp should
save/restore the signal mask (POSIX current allows both semantics).
More information about the Libc-alpha
mailing list