Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Date: Thu, 14 Jun 2012 06:01:45 -0500
From: Richard Pennington <rich@...nware.com>
To: musl@...ts.openwall.com
Subject: Timing of destructors?

Hi,

I have a small test program:
#include <stdio.h>
#include <stdlib.h>

void before(void) __attribute__((constructor));
void before(void)
{
    printf("before\n");
}

void after(void) __attribute__((destructor));
void after(void)
{
    printf("after\n");
}

int main()
{
    printf("hello world\n");
}

which doesn't print "after". This is because stdio is cleaned up before 
destructors are called in exit.c. Using stdio in destructors can be handy for 
debugging (if nothing else). Would it be evil to modify exit.c to look like 
this?:

void exit(int code)
{
        static int lock;

        /* If more than one thread calls exit, hang until _Exit ends it all */
        while (a_swap(&lock, 1)) __syscall(SYS_pause);

        /* Destructor s**t is kept separate from atexit to avoid bloat */
        if (libc.fini) libc.fini();
        if (libc.ldso_fini) libc.ldso_fini();

        /* Only do atexit & stdio flush if they were actually used */
        __funcs_on_exit();
        __fflush_on_exit();

        _Exit(code);
        for(;;);
}

The change is to move the *_on_exit() calls to after the destructors have been 
called.

-Rich

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.