1*2c53affbSjmc /* $OpenBSD: atexit.c,v 1.29 2022/12/27 17:10:06 jmc Exp $ */
22762c2edSdhartmei /*
32762c2edSdhartmei * Copyright (c) 2002 Daniel Hartmeier
4df930be7Sderaadt * All rights reserved.
5df930be7Sderaadt *
6df930be7Sderaadt * Redistribution and use in source and binary forms, with or without
7df930be7Sderaadt * modification, are permitted provided that the following conditions
8df930be7Sderaadt * are met:
9f8c1f0d0Sdhartmei *
102762c2edSdhartmei * - Redistributions of source code must retain the above copyright
112762c2edSdhartmei * notice, this list of conditions and the following disclaimer.
122762c2edSdhartmei * - Redistributions in binary form must reproduce the above
132762c2edSdhartmei * copyright notice, this list of conditions and the following
142762c2edSdhartmei * disclaimer in the documentation and/or other materials provided
152762c2edSdhartmei * with the distribution.
162762c2edSdhartmei *
172762c2edSdhartmei * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
182762c2edSdhartmei * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
192762c2edSdhartmei * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
202762c2edSdhartmei * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
212762c2edSdhartmei * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
222762c2edSdhartmei * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
232762c2edSdhartmei * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
242762c2edSdhartmei * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
252762c2edSdhartmei * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
262762c2edSdhartmei * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
272762c2edSdhartmei * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
282762c2edSdhartmei * POSSIBILITY OF SUCH DAMAGE.
292762c2edSdhartmei *
30df930be7Sderaadt */
31df930be7Sderaadt
322762c2edSdhartmei #include <sys/types.h>
332762c2edSdhartmei #include <sys/mman.h>
34df930be7Sderaadt #include <stdlib.h>
35be457116Schl #include <string.h>
36720afc0eSguenther #include <tib.h>
37197ba3e9Shenning #include <unistd.h>
38720afc0eSguenther
39df930be7Sderaadt #include "atexit.h"
40514a545fSguenther #include "atfork.h"
41882a3f06Sotto #include "thread_private.h"
42df930be7Sderaadt
43df930be7Sderaadt struct atexit *__atexit;
44884342deStedu static int restartloop;
45df930be7Sderaadt
460b0bfcdbSguenther /* define and initialize the list */
470b0bfcdbSguenther struct atfork_listhead _atfork_list = TAILQ_HEAD_INITIALIZER(_atfork_list);
480b0bfcdbSguenther
490b0bfcdbSguenther
50df930be7Sderaadt /*
518d62ee40Sdhartmei * Function pointers are stored in a linked list of pages. The list
528d62ee40Sdhartmei * is initially empty, and pages are allocated on demand. The first
538d62ee40Sdhartmei * function pointer in the first allocated page (the last one in
548d62ee40Sdhartmei * the linked list) is reserved for the cleanup function.
558d62ee40Sdhartmei *
565198527cSmillert * Outside the following functions, all pages are mprotect()'ed
578d62ee40Sdhartmei * to prevent unintentional/malicious corruption.
588d62ee40Sdhartmei */
598d62ee40Sdhartmei
608d62ee40Sdhartmei /*
615198527cSmillert * Register a function to be performed at exit or when a shared object
625198527cSmillert * with the given dso handle is unloaded dynamically. Also used as
635198527cSmillert * the backend for atexit(). For more info on this API, see:
645198527cSmillert *
655198527cSmillert * http://www.codesourcery.com/cxx-abi/abi.html#dso-dtor
66df930be7Sderaadt */
67df930be7Sderaadt int
__cxa_atexit(void (* func)(void *),void * arg,void * dso)685198527cSmillert __cxa_atexit(void (*func)(void *), void *arg, void *dso)
69df930be7Sderaadt {
7060b91d5aSderaadt struct atexit *p;
715198527cSmillert struct atexit_fn *fnp;
72d8bc04e4Spat int pgsize = getpagesize();
73882a3f06Sotto int ret = -1;
74df930be7Sderaadt
752762c2edSdhartmei if (pgsize < sizeof(*p))
762762c2edSdhartmei return (-1);
77882a3f06Sotto _ATEXIT_LOCK();
78882a3f06Sotto p = __atexit;
792762c2edSdhartmei if (p != NULL) {
802762c2edSdhartmei if (p->ind + 1 >= p->max)
812762c2edSdhartmei p = NULL;
822762c2edSdhartmei else if (mprotect(p, pgsize, PROT_READ | PROT_WRITE))
83882a3f06Sotto goto unlock;
842762c2edSdhartmei }
852762c2edSdhartmei if (p == NULL) {
862762c2edSdhartmei p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE,
872762c2edSdhartmei MAP_ANON | MAP_PRIVATE, -1, 0);
882762c2edSdhartmei if (p == MAP_FAILED)
89882a3f06Sotto goto unlock;
908d62ee40Sdhartmei if (__atexit == NULL) {
915198527cSmillert memset(&p->fns[0], 0, sizeof(p->fns[0]));
928d62ee40Sdhartmei p->ind = 1;
938d62ee40Sdhartmei } else
94df930be7Sderaadt p->ind = 0;
952762c2edSdhartmei p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) /
962762c2edSdhartmei sizeof(p->fns[0]);
97df930be7Sderaadt p->next = __atexit;
98df930be7Sderaadt __atexit = p;
99df930be7Sderaadt }
1005198527cSmillert fnp = &p->fns[p->ind++];
1015537d3e0Skettenis fnp->fn_ptr = func;
1025198527cSmillert fnp->fn_arg = arg;
1035198527cSmillert fnp->fn_dso = dso;
1042762c2edSdhartmei if (mprotect(p, pgsize, PROT_READ))
105882a3f06Sotto goto unlock;
106884342deStedu restartloop = 1;
107882a3f06Sotto ret = 0;
108882a3f06Sotto unlock:
109882a3f06Sotto _ATEXIT_UNLOCK();
110882a3f06Sotto return (ret);
111df930be7Sderaadt }
11257b9636dSguenther DEF_STRONG(__cxa_atexit);
1138d62ee40Sdhartmei
1148d62ee40Sdhartmei /*
115*2c53affbSjmc * Copy of atexit() used by libc and anything statically linked into the
1166897476fSguenther * executable. This passes NULL for the dso, so the callbacks are only
1176897476fSguenther * invoked by exit() and not dlclose()
1186897476fSguenther */
1196897476fSguenther int
atexit(void (* fn)(void))1206897476fSguenther atexit(void (*fn)(void))
1216897476fSguenther {
1226897476fSguenther return (__cxa_atexit((void (*)(void *))fn, NULL, NULL));
1236897476fSguenther }
1246897476fSguenther DEF_STRONG(atexit);
1256897476fSguenther
1260afcdedaSkettenis void
_thread_finalize(void)1270afcdedaSkettenis _thread_finalize(void)
1280afcdedaSkettenis {
1290afcdedaSkettenis struct tib *tib = TIB_GET();
1300afcdedaSkettenis
1310afcdedaSkettenis while (tib->tib_atexit) {
1320afcdedaSkettenis struct thread_atexit_fn *fnp = tib->tib_atexit;
1330afcdedaSkettenis tib->tib_atexit = fnp->next;
1340afcdedaSkettenis fnp->func(fnp->arg);
1350afcdedaSkettenis free(fnp);
1360afcdedaSkettenis }
1370afcdedaSkettenis }
1380afcdedaSkettenis
1396897476fSguenther /*
1405198527cSmillert * Call all handlers registered with __cxa_atexit() for the shared
1415198527cSmillert * object owning 'dso'.
1425198527cSmillert * Note: if 'dso' is NULL, then all remaining handlers are called.
1435198527cSmillert */
1445198527cSmillert void
__cxa_finalize(void * dso)1455198527cSmillert __cxa_finalize(void *dso)
1465198527cSmillert {
1475198527cSmillert struct atexit *p, *q;
1485198527cSmillert struct atexit_fn fn;
1495198527cSmillert int n, pgsize = getpagesize();
1505198527cSmillert static int call_depth;
1515198527cSmillert
1520afcdedaSkettenis if (dso == NULL)
1530afcdedaSkettenis _thread_finalize();
1540afcdedaSkettenis
15543de99abSkettenis _ATEXIT_LOCK();
1565198527cSmillert call_depth++;
1575198527cSmillert
158884342deStedu restart:
159884342deStedu restartloop = 0;
1605198527cSmillert for (p = __atexit; p != NULL; p = p->next) {
1615198527cSmillert for (n = p->ind; --n >= 0;) {
1625537d3e0Skettenis if (p->fns[n].fn_ptr == NULL)
1635198527cSmillert continue; /* already called */
1645198527cSmillert if (dso != NULL && dso != p->fns[n].fn_dso)
1655198527cSmillert continue; /* wrong DSO */
1665198527cSmillert
1675198527cSmillert /*
1685198527cSmillert * Mark handler as having been already called to avoid
1695198527cSmillert * dupes and loops, then call the appropriate function.
1705198527cSmillert */
1715198527cSmillert fn = p->fns[n];
1725198527cSmillert if (mprotect(p, pgsize, PROT_READ | PROT_WRITE) == 0) {
1735537d3e0Skettenis p->fns[n].fn_ptr = NULL;
1745198527cSmillert mprotect(p, pgsize, PROT_READ);
1755198527cSmillert }
17643de99abSkettenis _ATEXIT_UNLOCK();
1775537d3e0Skettenis (*fn.fn_ptr)(fn.fn_arg);
17843de99abSkettenis _ATEXIT_LOCK();
179884342deStedu if (restartloop)
180884342deStedu goto restart;
1815198527cSmillert }
1825198527cSmillert }
1835198527cSmillert
1847d23ac2aSmatthew call_depth--;
1857d23ac2aSmatthew
1865198527cSmillert /*
1875198527cSmillert * If called via exit(), unmap the pages since we have now run
1885198527cSmillert * all the handlers. We defer this until calldepth == 0 so that
1895198527cSmillert * we don't unmap things prematurely if called recursively.
1905198527cSmillert */
1917d23ac2aSmatthew if (dso == NULL && call_depth == 0) {
1925198527cSmillert for (p = __atexit; p != NULL; ) {
1935198527cSmillert q = p;
1945198527cSmillert p = p->next;
1955198527cSmillert munmap(q, pgsize);
1965198527cSmillert }
1975198527cSmillert __atexit = NULL;
1985198527cSmillert }
19943de99abSkettenis _ATEXIT_UNLOCK();
200514a545fSguenther
201514a545fSguenther /*
202514a545fSguenther * If unloading a DSO, unregister any atfork handlers registered
203514a545fSguenther * by it. Skip the locking if the list is currently empty.
204514a545fSguenther */
205514a545fSguenther if (dso != NULL && TAILQ_FIRST(&_atfork_list) != NULL) {
206514a545fSguenther struct atfork_fn *af, *afnext;
207514a545fSguenther
208514a545fSguenther _ATFORK_LOCK();
209514a545fSguenther TAILQ_FOREACH_SAFE(af, &_atfork_list, fn_next, afnext)
210514a545fSguenther if (af->fn_dso == dso) {
211514a545fSguenther TAILQ_REMOVE(&_atfork_list, af, fn_next);
212514a545fSguenther free(af);
213514a545fSguenther }
214514a545fSguenther _ATFORK_UNLOCK();
215514a545fSguenther
216514a545fSguenther }
2175198527cSmillert }
21857b9636dSguenther DEF_STRONG(__cxa_finalize);
2195198527cSmillert
2205198527cSmillert /*
2218d62ee40Sdhartmei * Register the cleanup function
2228d62ee40Sdhartmei */
2238d62ee40Sdhartmei void
__atexit_register_cleanup(void (* func)(void))2245198527cSmillert __atexit_register_cleanup(void (*func)(void))
2258d62ee40Sdhartmei {
226882a3f06Sotto struct atexit *p;
227d8bc04e4Spat int pgsize = getpagesize();
2288d62ee40Sdhartmei
2298d62ee40Sdhartmei if (pgsize < sizeof(*p))
2308d62ee40Sdhartmei return;
231882a3f06Sotto _ATEXIT_LOCK();
232882a3f06Sotto p = __atexit;
2338d62ee40Sdhartmei while (p != NULL && p->next != NULL)
2348d62ee40Sdhartmei p = p->next;
2358d62ee40Sdhartmei if (p == NULL) {
2368d62ee40Sdhartmei p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE,
2378d62ee40Sdhartmei MAP_ANON | MAP_PRIVATE, -1, 0);
2388d62ee40Sdhartmei if (p == MAP_FAILED)
239882a3f06Sotto goto unlock;
2408d62ee40Sdhartmei p->ind = 1;
2418d62ee40Sdhartmei p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) /
2428d62ee40Sdhartmei sizeof(p->fns[0]);
2438d62ee40Sdhartmei p->next = NULL;
2448d62ee40Sdhartmei __atexit = p;
2458d62ee40Sdhartmei } else {
2468d62ee40Sdhartmei if (mprotect(p, pgsize, PROT_READ | PROT_WRITE))
247882a3f06Sotto goto unlock;
2488d62ee40Sdhartmei }
2495537d3e0Skettenis p->fns[0].fn_ptr = (void (*)(void *))func;
2505198527cSmillert p->fns[0].fn_arg = NULL;
2515198527cSmillert p->fns[0].fn_dso = NULL;
2528d62ee40Sdhartmei mprotect(p, pgsize, PROT_READ);
253884342deStedu restartloop = 1;
254882a3f06Sotto unlock:
255882a3f06Sotto _ATEXIT_UNLOCK();
2568d62ee40Sdhartmei }
257