public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Paul Pluzhnikov <ppluzhnikov@google.com>
To: GLIBC Devel <libc-alpha@sourceware.org>
Subject: [patch] Add tests for atexit/on_exit firing order
Date: Mon, 10 Jul 2017 15:01:00 -0000	[thread overview]
Message-ID: <CALoOobMo+n2qfftAYG6Tyibg7Y7iCDn0PEMuBhvJyzwT4vVxZA@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 499 bytes --]

Greetings,

While working a on patch for bz14333, I discovered that there are no
tests for ordering of functions registered with atexit/on_exit, and in
particular the case where such function itself registers new exit
handlers.

This patch adds such test. I am using on_exit here because it
conveniently allows passing an argument.


2017-07-10  Paul Pluzhnikov  <ppluzhnikov@google.com>

        * stdlib/Makefile (tests): Add tst-on_exit
        * stdlib/tst-on_exit.c: New.


-- 
Paul Pluzhnikov

[-- Attachment #2: glibc-on_exit-20170710.txt --]
[-- Type: text/plain, Size: 2120 bytes --]

diff --git a/stdlib/Makefile b/stdlib/Makefile
index 0314d5926b..cc9f9215e4 100644
--- a/stdlib/Makefile
+++ b/stdlib/Makefile
@@ -80,7 +80,7 @@ tests		:= tst-strtol tst-strtod testmb testrand testsort testdiv   \
 		   tst-strtol-locale tst-strtod-nan-locale tst-strfmon_l    \
 		   tst-quick_exit tst-thread-quick_exit tst-width	    \
 		   tst-width-stdint tst-strfrom tst-strfrom-locale	    \
-		   tst-getrandom
+		   tst-getrandom tst-on_exit
 tests-internal	:= tst-strtod1i tst-strtod3 tst-strtod4 tst-strtod5i \
 		   tst-tls-atexit tst-tls-atexit-nodelete
 tests-static	:= tst-secure-getenv
diff --git a/stdlib/tst-on_exit.c b/stdlib/tst-on_exit.c
new file mode 100644
index 0000000000..0de3b68525
--- /dev/null
+++ b/stdlib/tst-on_exit.c
@@ -0,0 +1,68 @@
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define MAX_ON_EXIT 10
+static int expected[MAX_ON_EXIT];
+static int next_slot = 0;
+static int next_expected = 0;
+
+static void my_on_exit (void (*fn) (int status, void *));
+
+static void
+fn1 (int status, void *arg)
+{
+  intptr_t k = (intptr_t) arg;
+
+  printf ("fn1:\t\t%p %d\n", fn1, (int) k);
+  if (next_slot < 1 || expected[--next_slot] != k)
+    _exit (1);
+}
+
+static void
+fn2 (int status, void *arg)
+{
+  intptr_t k = (intptr_t) arg;
+
+  printf ("fn2:\t\t%p %d\n", fn2, (int) k);
+  if (next_slot < 1 || expected[--next_slot] != k)
+    _exit (1);
+  my_on_exit (fn1);
+}
+
+static void
+fn3 (int status, void *arg)
+{
+  intptr_t k = (intptr_t) arg;
+
+  printf ("fn3:\t\t%p %d\n", fn3, (int) k);
+  if (next_slot < 1 || expected[--next_slot] != k)
+    _exit (1);
+  my_on_exit (fn2);
+}
+
+static void
+my_on_exit (void (*fn) (int, void *))
+{
+  intptr_t k = ++next_expected;
+
+  printf ("on_exit:\t%p %d\n", fn, (int) k);
+  on_exit (fn, (void *) k);
+  assert (next_slot < MAX_ON_EXIT);
+  expected[next_slot++] = k;
+}
+
+static int
+do_test (void)
+{
+  my_on_exit (fn2);
+  my_on_exit (fn1);
+  my_on_exit (fn2);
+  my_on_exit (fn3);
+  return 0;
+}
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"

             reply	other threads:[~2017-07-10 15:01 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-10 15:01 Paul Pluzhnikov [this message]
2017-07-10 15:09 ` Carlos O'Donell
2017-07-10 15:18 ` Joseph Myers
2017-07-10 15:39   ` Paul Pluzhnikov
2017-07-10 15:45     ` Szabolcs Nagy
2017-07-10 15:56       ` Joseph Myers
2017-07-24 15:50         ` Paul Pluzhnikov
2017-07-24 17:05           ` Paul Pluzhnikov
2017-07-31 18:05             ` Paul Pluzhnikov
2017-08-07 14:54               ` Paul Pluzhnikov
2017-08-16 16:36             ` Carlos O'Donell
2017-08-28  2:16               ` Paul Pluzhnikov
2017-07-10 15:55     ` Joseph Myers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CALoOobMo+n2qfftAYG6Tyibg7Y7iCDn0PEMuBhvJyzwT4vVxZA@mail.gmail.com \
    --to=ppluzhnikov@google.com \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).