1 /* $OpenBSD: atexit.c,v 1.14 2007/09/05 20:47:47 chl Exp $ */
2 /*
3 * Copyright (c) 2002 Daniel Hartmeier
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 *
30 */
31
32 #include <sys/types.h>
33 #include <sys/mman.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include "atexit.h"
38 #include "thread_private.h"
39
40 int __atexit_invalid = 1;
41 struct atexit *__atexit;
42
43 /*
44 * Function pointers are stored in a linked list of pages. The list
45 * is initially empty, and pages are allocated on demand. The first
46 * function pointer in the first allocated page (the last one in
47 * the linked list) was reserved for the cleanup function.
48 * TODO: switch to the regular FreeBSD/NetBSD atexit implementation.
49 *
50 * Outside the following functions, all pages are mprotect()'ed
51 * to prevent unintentional/malicious corruption.
52 */
53
54 /*
55 * Register a function to be performed at exit or when a shared object
56 * with the given dso handle is unloaded dynamically. Also used as
57 * the backend for atexit(). For more info on this API, see:
58 *
59 * http://www.codesourcery.com/cxx-abi/abi.html#dso-dtor
60 */
61 int
__cxa_atexit(void (* func)(void *),void * arg,void * dso)62 __cxa_atexit(void (*func)(void *), void *arg, void *dso)
63 {
64 struct atexit *p = __atexit;
65 struct atexit_fn *fnp;
66 int pgsize = getpagesize();
67 int ret = -1;
68
69 if (pgsize < (int)sizeof(*p))
70 return (-1);
71 _ATEXIT_LOCK();
72 p = __atexit;
73 if (p != NULL) {
74 if (p->ind + 1 >= p->max)
75 p = NULL;
76 else if (mprotect(p, pgsize, PROT_READ | PROT_WRITE))
77 goto unlock;
78 }
79 if (p == NULL) {
80 p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE,
81 MAP_ANON | MAP_PRIVATE, -1, 0);
82 if (p == MAP_FAILED)
83 goto unlock;
84 if (__atexit == NULL) {
85 memset(&p->fns[0], 0, sizeof(p->fns[0]));
86 p->ind = 1;
87 } else
88 p->ind = 0;
89 p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) /
90 sizeof(p->fns[0]);
91 p->next = __atexit;
92 __atexit = p;
93 if (__atexit_invalid)
94 __atexit_invalid = 0;
95 }
96 fnp = &p->fns[p->ind++];
97 fnp->fn_ptr.cxa_func = func;
98 fnp->fn_arg = arg;
99 fnp->fn_dso = dso;
100 if (mprotect(p, pgsize, PROT_READ))
101 goto unlock;
102 ret = 0;
103 unlock:
104 _ATEXIT_UNLOCK();
105 return (ret);
106 }
107
108 /*
109 * Call all handlers registered with __cxa_atexit() for the shared
110 * object owning 'dso'.
111 * Note: if 'dso' is NULL, then all remaining handlers are called.
112 */
113 void
__cxa_finalize(void * dso)114 __cxa_finalize(void *dso)
115 {
116 struct atexit *p, *q;
117 struct atexit_fn fn;
118 int n, pgsize = getpagesize();
119 static int call_depth;
120
121 if (__atexit_invalid)
122 return;
123
124 _ATEXIT_LOCK();
125 call_depth++;
126
127 for (p = __atexit; p != NULL; p = p->next) {
128 for (n = p->ind; --n >= 0;) {
129 if (p->fns[n].fn_ptr.cxa_func == NULL)
130 continue; /* already called */
131 if (dso != NULL && dso != p->fns[n].fn_dso)
132 continue; /* wrong DSO */
133
134 /*
135 * Mark handler as having been already called to avoid
136 * dupes and loops, then call the appropriate function.
137 */
138 fn = p->fns[n];
139 if (mprotect(p, pgsize, PROT_READ | PROT_WRITE) == 0) {
140 p->fns[n].fn_ptr.cxa_func = NULL;
141 mprotect(p, pgsize, PROT_READ);
142 }
143 _ATEXIT_UNLOCK();
144 #if ANDROID
145 /* it looks like we should always call the function
146 * with an argument, even if dso is not NULL. Otherwise
147 * static destructors will not be called properly on
148 * the ARM.
149 */
150 (*fn.fn_ptr.cxa_func)(fn.fn_arg);
151 #else /* !ANDROID */
152 if (dso != NULL)
153 (*fn.fn_ptr.cxa_func)(fn.fn_arg);
154 else
155 (*fn.fn_ptr.std_func)();
156 #endif /* !ANDROID */
157 _ATEXIT_LOCK();
158 }
159 }
160
161 /*
162 * If called via exit(), unmap the pages since we have now run
163 * all the handlers. We defer this until calldepth == 0 so that
164 * we don't unmap things prematurely if called recursively.
165 */
166 if (dso == NULL && --call_depth == 0) {
167 for (p = __atexit; p != NULL; ) {
168 q = p;
169 p = p->next;
170 munmap(q, pgsize);
171 }
172 __atexit = NULL;
173 }
174 _ATEXIT_UNLOCK();
175 }
176