From 61c0c852b58bddedb71c9a6a42813b45684dc7c9 Mon Sep 17 00:00:00 2001 From: herman ten brugge Date: Mon, 14 Sep 2020 08:24:01 +0200 Subject: [PATCH] Update boundschecking for fork bcheck.c: - Fix fork function. - Move use_sem - Fix bound_alloc_error text tests/tests2/114_bound_signal.c: - Add test for fork --- lib/bcheck.c | 15 +++++----- tests/tests2/114_bound_signal.c | 49 ++++++++++++++++++++++++++++++--- 2 files changed, 52 insertions(+), 12 deletions(-) diff --git a/lib/bcheck.c b/lib/bcheck.c index 54ef5a79..e96b7ab3 100644 --- a/lib/bcheck.c +++ b/lib/bcheck.c @@ -169,7 +169,6 @@ static void (*free_redir) (void *); static void *(*realloc_redir) (void *, size_t); static unsigned int pool_index; static unsigned char __attribute__((aligned(16))) initial_pool[256]; -static unsigned char use_sem; #endif #if HAVE_MEMALIGN static void *(*memalign_redir) (size_t, size_t); @@ -329,6 +328,7 @@ static unsigned char print_calls; static unsigned char print_heap; static unsigned char print_statistic; static unsigned char no_strdup; +static unsigned char use_sem; static int never_fatal; #if HAVE_TLS_FUNC #if defined(_WIN32) @@ -963,14 +963,14 @@ void __bound_init(size_t *p, int mode) dprintf(stderr, "%s, %s(): sigaction_redir %p\n", __FILE__, __FUNCTION__, sigaction_redir); if (sigaction_redir == NULL) - bound_alloc_error ("Cannot sigaction signal"); + bound_alloc_error ("Cannot redirect sigaction"); #endif #if HAVE_FORK *(void **) (&fork_redir) = dlsym (addr, "fork"); dprintf(stderr, "%s, %s(): fork_redir %p\n", __FILE__, __FUNCTION__, fork_redir); if (fork_redir == NULL) - bound_alloc_error ("Cannot sigaction fork"); + bound_alloc_error ("Cannot redirect fork"); #endif } #endif @@ -1372,12 +1372,11 @@ int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact) #if HAVE_FORK pid_t fork(void) { - pid_t retval = (*fork_redir)(); + pid_t retval; - if (retval == 0) { - TRY_SEM(); - POST_SEM(); - } + WAIT_SEM(); + retval = (*fork_redir)(); + POST_SEM(); return retval; } #endif diff --git a/tests/tests2/114_bound_signal.c b/tests/tests2/114_bound_signal.c index f167a219..87f44ca8 100644 --- a/tests/tests2/114_bound_signal.c +++ b/tests/tests2/114_bound_signal.c @@ -4,12 +4,15 @@ #include #include #include +#include #include +#include #include #include static volatile int run = 1; static sem_t sem; +static sem_t sem_child; static void add (int n) @@ -43,6 +46,29 @@ do_signal (void *unused) return NULL; } +static void * +do_fork (void *unused) +{ + pid_t pid; + + while (run) { + switch ((pid = fork())) { + case 0: + add(1000); + add(2000); + exit(0); + break; + case -1: + return NULL; + default: + while (sem_wait(&sem_child) < 0 && errno == EINTR); + wait(NULL); + break; + } + } + return NULL; +} + static void signal_handler(int sig) { add(10); @@ -50,11 +76,18 @@ static void signal_handler(int sig) sem_post (&sem); } +static void child_handler(int sig) +{ + add(10); + add(20); + sem_post (&sem_child); +} + int main (void) { int i; - pthread_t id1, id2; + pthread_t id1, id2, id3; struct sigaction act; sigjmp_buf sj; sigset_t m; @@ -65,21 +98,29 @@ main (void) act.sa_flags = 0; sigemptyset (&act.sa_mask); sigaction (SIGUSR1, &act, NULL); + act.sa_handler = child_handler; + sigaction (SIGCHLD, &act, NULL); - sem_init (&sem, 1, 0); + printf ("start\n"); fflush(stdout); + + sem_init (&sem, 0, 0); + sem_init (&sem_child, 0, 0); pthread_create(&id1, NULL, high_load, NULL); pthread_create(&id2, NULL, do_signal, NULL); + pthread_create(&id3, NULL, do_fork, NULL); - printf ("start\n"); /* sleep does not work !!! */ end = time(NULL) + 2; while (time(NULL) < end) ; run = 0; - printf ("end\n"); + + printf ("end\n"); fflush(stdout); pthread_join(id1, NULL); pthread_join(id2, NULL); + pthread_join(id3, NULL); sem_destroy (&sem); + sem_destroy (&sem_child); sigemptyset (&m); sigprocmask (SIG_SETMASK, &m, NULL);