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