blob: a26bee4ff510de18e29fe4028082fc4ae0f54730 [file] [log] [blame]
Dmitriy Ivanov53c3c272014-07-11 12:59:16 -07001/* $OpenBSD: atexit.c,v 1.20 2014/07/11 09:51:37 kettenis 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
Elliott Hughes468efc82018-07-10 14:39:49 -070032#include "atexit.h"
33
34#include <pthread.h>
Dmitriy Ivanov53c3c272014-07-11 12:59:16 -070035#include <stdlib.h>
36#include <string.h>
37#include <unistd.h>
Elliott Hughes468efc82018-07-10 14:39:49 -070038#include <sys/mman.h>
39#include <sys/types.h>
Dmitriy Ivanov53c3c272014-07-11 12:59:16 -070040
Daniel Micayf0a73472015-07-25 15:40:14 -040041/* BEGIN android-changed */
42#include "private/bionic_prctl.h"
43/* END android-changed */
44
Elliott Hughes468efc82018-07-10 14:39:49 -070045static pthread_mutex_t g_atexit_lock = PTHREAD_MUTEX_INITIALIZER;
46#define _ATEXIT_LOCK() pthread_mutex_lock(&g_atexit_lock)
47#define _ATEXIT_UNLOCK() pthread_mutex_unlock(&g_atexit_lock)
48
Dmitriy Ivanov9cf6fc32015-06-04 14:33:18 -070049struct atexit {
50 struct atexit *next; /* next in list */
51 int ind; /* next index in this table */
52 int max; /* max entries >= ATEXIT_SIZE */
53 struct atexit_fn {
54 void (*fn_ptr)(void *);
55 void *fn_arg; /* argument for CXA callback */
56 void *fn_dso; /* shared module handle */
57 } fns[1]; /* the table itself */
58};
59
60static struct atexit *__atexit;
Dmitriy Ivanov53c3c272014-07-11 12:59:16 -070061static int restartloop;
62
Dmitriy Ivanovea295f62014-11-20 20:47:02 -080063/* BEGIN android-changed: __unregister_atfork is used by __cxa_finalize */
64extern void __unregister_atfork(void* dso);
65/* END android-changed */
66
Dmitriy Ivanov53c3c272014-07-11 12:59:16 -070067/*
68 * Function pointers are stored in a linked list of pages. The list
69 * is initially empty, and pages are allocated on demand. The first
70 * function pointer in the first allocated page (the last one in
71 * the linked list) is reserved for the cleanup function.
72 *
73 * Outside the following functions, all pages are mprotect()'ed
74 * to prevent unintentional/malicious corruption.
75 */
76
77/*
78 * Register a function to be performed at exit or when a shared object
79 * with the given dso handle is unloaded dynamically. Also used as
80 * the backend for atexit(). For more info on this API, see:
81 *
82 * http://www.codesourcery.com/cxx-abi/abi.html#dso-dtor
83 */
84int
85__cxa_atexit(void (*func)(void *), void *arg, void *dso)
86{
Dmitriy Ivanov53c3c272014-07-11 12:59:16 -070087 struct atexit_fn *fnp;
Dmitriy Ivanovea295f62014-11-20 20:47:02 -080088 size_t pgsize = getpagesize();
Dmitriy Ivanov53c3c272014-07-11 12:59:16 -070089 int ret = -1;
90
Elliott Hughes468efc82018-07-10 14:39:49 -070091 if (pgsize < sizeof(struct atexit))
Dmitriy Ivanov53c3c272014-07-11 12:59:16 -070092 return (-1);
93 _ATEXIT_LOCK();
Elliott Hughes468efc82018-07-10 14:39:49 -070094 struct atexit *p = __atexit;
Dmitriy Ivanov53c3c272014-07-11 12:59:16 -070095 if (p != NULL) {
96 if (p->ind + 1 >= p->max)
97 p = NULL;
98 else if (mprotect(p, pgsize, PROT_READ | PROT_WRITE))
99 goto unlock;
100 }
101 if (p == NULL) {
102 p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE,
103 MAP_ANON | MAP_PRIVATE, -1, 0);
104 if (p == MAP_FAILED)
105 goto unlock;
Daniel Micayf0a73472015-07-25 15:40:14 -0400106/* BEGIN android-changed */
107 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, p, pgsize,
108 "atexit handlers");
109/* END android-changed */
Dmitriy Ivanov53c3c272014-07-11 12:59:16 -0700110 if (__atexit == NULL) {
111 memset(&p->fns[0], 0, sizeof(p->fns[0]));
112 p->ind = 1;
113 } else
114 p->ind = 0;
115 p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) /
116 sizeof(p->fns[0]);
117 p->next = __atexit;
118 __atexit = p;
119 }
120 fnp = &p->fns[p->ind++];
121 fnp->fn_ptr = func;
122 fnp->fn_arg = arg;
123 fnp->fn_dso = dso;
124 if (mprotect(p, pgsize, PROT_READ))
125 goto unlock;
126 restartloop = 1;
127 ret = 0;
128unlock:
129 _ATEXIT_UNLOCK();
130 return (ret);
131}
132
133/*
134 * Call all handlers registered with __cxa_atexit() for the shared
135 * object owning 'dso'.
136 * Note: if 'dso' is NULL, then all remaining handlers are called.
137 */
138void
139__cxa_finalize(void *dso)
140{
141 struct atexit *p, *q;
142 struct atexit_fn fn;
143 int n, pgsize = getpagesize();
144 static int call_depth;
145
146 _ATEXIT_LOCK();
147 call_depth++;
148
149restart:
150 restartloop = 0;
151 for (p = __atexit; p != NULL; p = p->next) {
152 for (n = p->ind; --n >= 0;) {
153 if (p->fns[n].fn_ptr == NULL)
154 continue; /* already called */
155 if (dso != NULL && dso != p->fns[n].fn_dso)
156 continue; /* wrong DSO */
157
158 /*
159 * Mark handler as having been already called to avoid
160 * dupes and loops, then call the appropriate function.
161 */
162 fn = p->fns[n];
163 if (mprotect(p, pgsize, PROT_READ | PROT_WRITE) == 0) {
164 p->fns[n].fn_ptr = NULL;
165 mprotect(p, pgsize, PROT_READ);
166 }
167 _ATEXIT_UNLOCK();
168 (*fn.fn_ptr)(fn.fn_arg);
169 _ATEXIT_LOCK();
170 if (restartloop)
171 goto restart;
172 }
173 }
174
175 call_depth--;
176
177 /*
178 * If called via exit(), unmap the pages since we have now run
179 * all the handlers. We defer this until calldepth == 0 so that
180 * we don't unmap things prematurely if called recursively.
181 */
182 if (dso == NULL && call_depth == 0) {
183 for (p = __atexit; p != NULL; ) {
184 q = p;
185 p = p->next;
186 munmap(q, pgsize);
187 }
188 __atexit = NULL;
189 }
190 _ATEXIT_UNLOCK();
Dmitriy Ivanovea295f62014-11-20 20:47:02 -0800191
Elliott Hughes468efc82018-07-10 14:39:49 -0700192 fflush(NULL);
Elliott Hughesbb46afd2015-12-04 18:03:12 -0800193
Dmitriy Ivanovea295f62014-11-20 20:47:02 -0800194 /* BEGIN android-changed: call __unregister_atfork if dso is not null */
195 if (dso != NULL) {
196 __unregister_atfork(dso);
197 }
198 /* END android-changed */
Dmitriy Ivanov53c3c272014-07-11 12:59:16 -0700199}