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
This commit is contained in:
herman ten brugge 2020-09-14 08:24:01 +02:00
parent 8878c29c5d
commit 61c0c852b5
2 changed files with 52 additions and 12 deletions

View File

@ -169,7 +169,6 @@ static void (*free_redir) (void *);
static void *(*realloc_redir) (void *, size_t); static void *(*realloc_redir) (void *, size_t);
static unsigned int pool_index; static unsigned int pool_index;
static unsigned char __attribute__((aligned(16))) initial_pool[256]; static unsigned char __attribute__((aligned(16))) initial_pool[256];
static unsigned char use_sem;
#endif #endif
#if HAVE_MEMALIGN #if HAVE_MEMALIGN
static void *(*memalign_redir) (size_t, size_t); 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_heap;
static unsigned char print_statistic; static unsigned char print_statistic;
static unsigned char no_strdup; static unsigned char no_strdup;
static unsigned char use_sem;
static int never_fatal; static int never_fatal;
#if HAVE_TLS_FUNC #if HAVE_TLS_FUNC
#if defined(_WIN32) #if defined(_WIN32)
@ -963,14 +963,14 @@ void __bound_init(size_t *p, int mode)
dprintf(stderr, "%s, %s(): sigaction_redir %p\n", dprintf(stderr, "%s, %s(): sigaction_redir %p\n",
__FILE__, __FUNCTION__, sigaction_redir); __FILE__, __FUNCTION__, sigaction_redir);
if (sigaction_redir == NULL) if (sigaction_redir == NULL)
bound_alloc_error ("Cannot sigaction signal"); bound_alloc_error ("Cannot redirect sigaction");
#endif #endif
#if HAVE_FORK #if HAVE_FORK
*(void **) (&fork_redir) = dlsym (addr, "fork"); *(void **) (&fork_redir) = dlsym (addr, "fork");
dprintf(stderr, "%s, %s(): fork_redir %p\n", dprintf(stderr, "%s, %s(): fork_redir %p\n",
__FILE__, __FUNCTION__, fork_redir); __FILE__, __FUNCTION__, fork_redir);
if (fork_redir == NULL) if (fork_redir == NULL)
bound_alloc_error ("Cannot sigaction fork"); bound_alloc_error ("Cannot redirect fork");
#endif #endif
} }
#endif #endif
@ -1372,12 +1372,11 @@ int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
#if HAVE_FORK #if HAVE_FORK
pid_t fork(void) pid_t fork(void)
{ {
pid_t retval = (*fork_redir)(); pid_t retval;
if (retval == 0) { WAIT_SEM();
TRY_SEM(); retval = (*fork_redir)();
POST_SEM(); POST_SEM();
}
return retval; return retval;
} }
#endif #endif

View File

@ -4,12 +4,15 @@
#include <signal.h> #include <signal.h>
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h>
#include <errno.h> #include <errno.h>
#include <setjmp.h> #include <setjmp.h>
static volatile int run = 1; static volatile int run = 1;
static sem_t sem; static sem_t sem;
static sem_t sem_child;
static void static void
add (int n) add (int n)
@ -43,6 +46,29 @@ do_signal (void *unused)
return NULL; 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) static void signal_handler(int sig)
{ {
add(10); add(10);
@ -50,11 +76,18 @@ static void signal_handler(int sig)
sem_post (&sem); sem_post (&sem);
} }
static void child_handler(int sig)
{
add(10);
add(20);
sem_post (&sem_child);
}
int int
main (void) main (void)
{ {
int i; int i;
pthread_t id1, id2; pthread_t id1, id2, id3;
struct sigaction act; struct sigaction act;
sigjmp_buf sj; sigjmp_buf sj;
sigset_t m; sigset_t m;
@ -65,21 +98,29 @@ main (void)
act.sa_flags = 0; act.sa_flags = 0;
sigemptyset (&act.sa_mask); sigemptyset (&act.sa_mask);
sigaction (SIGUSR1, &act, NULL); 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(&id1, NULL, high_load, NULL);
pthread_create(&id2, NULL, do_signal, NULL); pthread_create(&id2, NULL, do_signal, NULL);
pthread_create(&id3, NULL, do_fork, NULL);
printf ("start\n");
/* sleep does not work !!! */ /* sleep does not work !!! */
end = time(NULL) + 2; end = time(NULL) + 2;
while (time(NULL) < end) ; while (time(NULL) < end) ;
run = 0; run = 0;
printf ("end\n");
printf ("end\n"); fflush(stdout);
pthread_join(id1, NULL); pthread_join(id1, NULL);
pthread_join(id2, NULL); pthread_join(id2, NULL);
pthread_join(id3, NULL);
sem_destroy (&sem); sem_destroy (&sem);
sem_destroy (&sem_child);
sigemptyset (&m); sigemptyset (&m);
sigprocmask (SIG_SETMASK, &m, NULL); sigprocmask (SIG_SETMASK, &m, NULL);