[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Is malloc signal-"tolerant"?
From: |
Stefan Hoffmeister |
Subject: |
Is malloc signal-"tolerant"? |
Date: |
Sun, 23 Dec 2001 00:33:44 +0100 |
Hi,
it is a well-documented fact that malloc is not a signal-safe function -
i.e. it cannot be called from the context of a signal handler.
But is malloc signal-"tolerant" in that its internal data structures
survive a longjmp() (or a siglongjmp()) out of a signal handler?
Given this program, for instance, which basically exercises malloc and
free in a very tight loop,
***************************
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <setjmp.h>
#include <string.h>
sigjmp_buf JumpBuffer;
int Loop = 1;
void SignalIntHandler(int SignalNumber)
{
siglongjmp(JumpBuffer, 1);
}
void SignalQuitHandler(int SignalNumber)
{
Loop = 0;
}
void LoopWithFun()
{
sigsetjmp(JumpBuffer, 1);
while (Loop)
{
int MemSize = rand();
free(malloc(MemSize));
}
}
int main(int argc, char *argv[])
{
struct sigaction sa;
printf("Starting the fun\r\n");
memset(&sa, sizeof(sa), 0);
sa.sa_handler = SignalIntHandler;
sa.sa_flags = SA_RESTART;
sigaction(SIGINT, &sa, 0);
sa.sa_handler = SignalQuitHandler;
sa.sa_flags = SA_RESTART;
sigaction(SIGQUIT, &sa, 0);
LoopWithFun();
printf("Done\n");
return 0;
}
***************************
and a shell command on a separate terminal
while true; do killall -INT TheAboveProgram; done
that essentially keeps signalling to the test program - should this
survive?
[This would imply that the for the duration of a malloc operation,
certain signals are blocked - I cannot imagine any other way to
guarantee signal-atomic operations.]
Currently the above test scenario terminates with a SIGSEGV after a
while (glibc 2.2.4, kernel 2.4.17), presumably because either malloc or
free were disturbed by the siglongjmp() while updating some rather
important data structure.
TIA!
Stefan
- Is malloc signal-"tolerant"?,
Stefan Hoffmeister <=