xref: /freebsd/lib/libc/stdlib/cxa_thread_atexit_impl.c (revision edf8578117e8844e02c0121147f45e4609b30680)
1 /*-
2  * Copyright (c) 2016 Mahdi Mokhtari <mokhi64@gmail.com>
3  * Copyright (c) 2016, 2017 The FreeBSD Foundation
4  * All rights reserved.
5  *
6  * Portions of this software were developed by Konstantin Belousov
7  * under sponsorship from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 #include <sys/queue.h>
33 #include "namespace.h"
34 #include <errno.h>
35 #include <link.h>
36 #include <pthread.h>
37 #include <stddef.h>
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include "un-namespace.h"
41 #include "libc_private.h"
42 
43 /*
44  * C++11 introduces the thread_local scope (like __thread with some
45  * additions).  As a key-feature it should support non-trivial
46  * destructors, registered with __cxa_thread_atexit() to be executed
47  * at the thread termination.
48  *
49  * The implemention keeps a _Thread_local list of destructors per each
50  * thread, and calls __cxa_thread_call_dtors() on each thread's exit
51  * to do cleanup.  For a thread calling exit(3), in particular, for
52  * the initial thread returning from main(), we call
53  * __cxa_thread_call_dtors() inside exit().
54  *
55  * It could be possible that a dynamically loaded library, use
56  * thread_local variable but is dlclose()'d before thread exit.  The
57  * destructor of this variable will then try to access the address,
58  * for calling it but it's unloaded, so it'll crash.  We're using
59  * __elf_phdr_match_addr() to detect and prevent such cases and so
60  * prevent the crash.
61  */
62 
63 #define CXA_DTORS_ITERATIONS 4
64 
65 struct cxa_thread_dtor {
66 	void *obj;
67 	void (*func)(void *);
68 	void *dso;
69 	LIST_ENTRY(cxa_thread_dtor) entry;
70 };
71 static _Thread_local LIST_HEAD(dtor_list, cxa_thread_dtor) dtors =
72     LIST_HEAD_INITIALIZER(dtors);
73 
74 int
75 __cxa_thread_atexit_impl(void (*dtor_func)(void *), void *obj,
76     void *dso_symbol)
77 {
78 
79 	return (__cxa_thread_atexit_hidden(dtor_func, obj, dso_symbol));
80 }
81 
82 int
83 __cxa_thread_atexit_hidden(void (*dtor_func)(void *), void *obj,
84     void *dso_symbol)
85 {
86 	struct cxa_thread_dtor *new_dtor;
87 
88 	new_dtor = malloc(sizeof(*new_dtor));
89 	if (new_dtor == NULL) {
90 		errno = ENOMEM; /* forcibly override malloc(3) error */
91 		return (-1);
92 	}
93 
94 	new_dtor->obj = obj;
95 	new_dtor->func = dtor_func;
96 	new_dtor->dso = dso_symbol;
97 	LIST_INSERT_HEAD(&dtors, new_dtor, entry);
98 	return (0);
99 }
100 
101 static void
102 walk_cb_call(struct cxa_thread_dtor *dtor)
103 {
104 	struct dl_phdr_info phdr_info;
105 
106 	if (_rtld_addr_phdr(dtor->dso, &phdr_info) &&
107 	    __elf_phdr_match_addr(&phdr_info, dtor->func))
108 		dtor->func(dtor->obj);
109 	else
110 		fprintf(stderr,
111 		    "__cxa_thread_call_dtors: dtr %p from unloaded dso, skipping\n",
112 		    (void *)(dtor->func));
113 }
114 
115 static void
116 walk_cb_nocall(struct cxa_thread_dtor *dtor __unused)
117 {
118 }
119 
120 static void
121 cxa_thread_walk(void (*cb)(struct cxa_thread_dtor *))
122 {
123 	struct cxa_thread_dtor *dtor, *tdtor;
124 
125 	LIST_FOREACH_SAFE(dtor, &dtors, entry, tdtor) {
126 		LIST_REMOVE(dtor, entry);
127 		cb(dtor);
128 		free(dtor);
129 	}
130 }
131 
132 /*
133  * This is the callback function we use to call destructors, once for
134  * each thread.  It is called in exit(3) in libc/stdlib/exit.c and
135  * before exit_thread() in libthr/thread/thr_exit.c.
136  */
137 void
138 __cxa_thread_call_dtors(void)
139 {
140 	int i;
141 
142 	for (i = 0; i < CXA_DTORS_ITERATIONS && !LIST_EMPTY(&dtors); i++)
143 		cxa_thread_walk(walk_cb_call);
144 
145 	if (!LIST_EMPTY(&dtors)) {
146 		fprintf(stderr, "Thread %p is exiting with more "
147 		    "thread-specific dtors created after %d iterations "
148 		    "of destructor calls\n",
149 		    _pthread_self(), i);
150 		cxa_thread_walk(walk_cb_nocall);
151 	}
152 }
153