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