From patchwork Mon Jul 20 16:36:36 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 497788 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 50135140D55 for ; Tue, 21 Jul 2015 02:36:56 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b=WlqLo7f2; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=mABFnFYBsnCsShJTauKu0BRRVrP8pOB YcbQB9ADqJoya5APFvVQSBkt/kiFc5pB3IwlxYNRSC2YJoYmiEl6ZbedIyI/TMqq yYwn5SVjP+UDEZj/5ONeCRggiROLQvT9FYozYkRJ1I72u0ukWsWGbTpbm9bL9H6B ayCKdIIk1tag= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references; s=default; bh=lEgwPjjMKIpg4Mfg6+TgJtICuF0=; b=WlqLo 7f2q7pRDiwoocshUN2uUG0WvCZTwiT9tjJZPxJBJPWzluo3etesuXVMqeBL45MIR oRWnSiaPaVUsjRTU5IV728lPsyWgC22NiL27K/pdIZ9aXnjsPhfEoMEltRfXTqbT P4D5JzJUw3pchKo5MPQvZNYa1iFKwmEJAzYZI0= Received: (qmail 123333 invoked by alias); 20 Jul 2015 16:36:50 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 123319 invoked by uid 89); 20 Jul 2015 16:36:49 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.7 required=5.0 tests=AWL, BAYES_00, KAM_LAZY_DOMAIN_SECURITY, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 X-HELO: mx1.redhat.com From: "Siddhesh Poyarekar" To: libc-alpha@sourceware.org Cc: roland@hack.frob.com, carlos@redhat.com Subject: [PATCH v6] Remove Linuxism from tst-tls-atexit Date: Mon, 20 Jul 2015 22:06:36 +0530 Message-Id: <1437410196-24683-1-git-send-email-siddhesh@redhat.com> In-Reply-To: <55ACF201.6010505@redhat.com> References: <55ACF201.6010505@redhat.com> The tst-tls-atexit test case searches for its module in /proc/PID/maps to verify that it is unloaded, which is a Linux-specific test. This patch makes the test generic by trying to call foo (the symbol obtained from dlsym) and traps SIGSEGV momentarily to see if the crash occurred. Verified that the test continues to succeed on x86_64. There is a bug in the test case where it calls dlclose once again, which is actually incorrect but still manages to unload the DSO thanks to an existing bug in __tls_call_dtors. This will be fixed in a later patch which also fixes up the __cxa_thread_atexit_impl implementation. I have added a FIXME comment to that call momentarily, which I will remove when I fix the problem. * stdlib/tst-tls-atexit-lib.c (do_foo): Rename to reg_dtor. * stdlib/tst-tls-atexit.c: (is_loaded): New function. (load): Rename to reg_dtor_and_close. Move dlopen to... (do_test): ... here. Use IS_LOADED to test for its availability. --- stdlib/tst-tls-atexit-lib.c | 2 +- stdlib/tst-tls-atexit.c | 84 +++++++++++++++++++++++---------------------- 2 files changed, 44 insertions(+), 42 deletions(-) diff --git a/stdlib/tst-tls-atexit-lib.c b/stdlib/tst-tls-atexit-lib.c index 2945379..2478d80 100644 --- a/stdlib/tst-tls-atexit-lib.c +++ b/stdlib/tst-tls-atexit-lib.c @@ -31,7 +31,7 @@ void A_dtor (void *obj) ((A *)obj)->val = obj; } -void do_foo (void) +void reg_dtor (void) { static __thread A b; __cxa_thread_atexit_impl (A_dtor, &b, __dso_handle); diff --git a/stdlib/tst-tls-atexit.c b/stdlib/tst-tls-atexit.c index 0c6c499..1947048 100644 --- a/stdlib/tst-tls-atexit.c +++ b/stdlib/tst-tls-atexit.c @@ -16,10 +16,11 @@ License along with the GNU C Library; if not, see . */ -/* There are two tests in this test case. The first is implicit where it is +/* There are two parts in this test case. The first is implicit where it is assumed that the destructor call on exit of the LOAD function does not segfault. The other is a verification that after the thread has exited, a - dlclose will unload the DSO. */ + dlclose will unload the DSO, which is done by calling into the DSO and + expecting that call to segfault. */ #include #include @@ -27,32 +28,41 @@ #include #include #include +#include -void *handle; +#define DSO_NAME "$ORIGIN/tst-tls-atexit-lib.so" -void * -load (void *u) +/* Walk through the map in the _r_debug structure to see if our lib is still + loaded. */ +static bool +is_loaded (void) { - handle = dlopen ("$ORIGIN/tst-tls-atexit-lib.so", RTLD_LAZY); - if (handle == NULL) - { - printf ("Unable to load DSO: %s\n", dlerror ()); - return (void *) (uintptr_t) 1; - } + struct link_map *lm = (struct link_map *) _r_debug.r_map; + + for (; lm; lm = lm->l_next) + if (lm->l_type == lt_loaded && lm->l_name + && strcmp (basename (DSO_NAME), basename (lm->l_name)) == 0) + return true; + return false; +} - void (*foo) (void) = (void (*) (void)) dlsym (handle, "do_foo"); +/* Accept a valid handle returned by DLOPEN, load the reg_dtor symbol to + register a destructor and then call dlclose on the handle. The dlclose + should not unload the DSO since the destructor has not been called yet. */ +static void * +reg_dtor_and_close (void *h) +{ + void (*reg_dtor) (void) = (void (*) (void)) dlsym (h, "reg_dtor"); - if (foo == NULL) + if (reg_dtor == NULL) { printf ("Unable to find symbol: %s\n", dlerror ()); - exit (1); + return (void *) (uintptr_t) 1; } - foo (); + reg_dtor (); - /* This should not unload the DSO. If it does, then the thread exit will - result in a segfault. */ - dlclose (handle); + dlclose (h); return NULL; } @@ -64,7 +74,15 @@ do_test (void) int ret; void *thr_ret; - if ((ret = pthread_create (&t, NULL, load, NULL)) != 0) + /* Load the DSO. */ + void *handle = dlopen (DSO_NAME, RTLD_LAZY); + if (handle == NULL) + { + printf ("Unable to load DSO: %s\n", dlerror ()); + return 1; + } + + if ((ret = pthread_create (&t, NULL, reg_dtor_and_close, handle)) != 0) { printf ("pthread_create failed: %s\n", strerror (ret)); return 1; @@ -79,30 +97,14 @@ do_test (void) if (thr_ret != NULL) return 1; - /* Now this should unload the DSO. */ + /* Now this should unload the DSO. FIXME: This is a bug, calling dlclose + like this is actually wrong, but it works because cxa_thread_atexit_impl + has a bug which results in dlclose allowing this to work. */ dlclose (handle); - /* Run through our maps and ensure that the DSO is unloaded. */ - FILE *f = fopen ("/proc/self/maps", "r"); - - if (f == NULL) - { - perror ("Failed to open /proc/self/maps"); - fprintf (stderr, "Skipping verification of DSO unload\n"); - return 0; - } - - char *line = NULL; - size_t s = 0; - while (getline (&line, &s, f) > 0) - { - if (strstr (line, "tst-tls-atexit-lib.so")) - { - printf ("DSO not unloaded yet:\n%s", line); - return 1; - } - } - free (line); + /* Check link maps to ensure that the DSO has unloaded. */ + if (is_loaded ()) + return 1; return 0; }