| Dmitriy Ivanov | 53c3c27 | 2014-07-11 12:59:16 -0700 | [diff] [blame] | 1 | /*	$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 Hughes | 468efc8 | 2018-07-10 14:39:49 -0700 | [diff] [blame] | 32 | #include "atexit.h" | 
|  | 33 |  | 
|  | 34 | #include <pthread.h> | 
| Dmitriy Ivanov | 53c3c27 | 2014-07-11 12:59:16 -0700 | [diff] [blame] | 35 | #include <stdlib.h> | 
|  | 36 | #include <string.h> | 
|  | 37 | #include <unistd.h> | 
| Elliott Hughes | 468efc8 | 2018-07-10 14:39:49 -0700 | [diff] [blame] | 38 | #include <sys/mman.h> | 
| Elliott Hughes | 99d5465 | 2018-08-22 10:36:23 -0700 | [diff] [blame] | 39 | #include <sys/prctl.h> | 
| Elliott Hughes | 468efc8 | 2018-07-10 14:39:49 -0700 | [diff] [blame] | 40 | #include <sys/types.h> | 
| Dmitriy Ivanov | 53c3c27 | 2014-07-11 12:59:16 -0700 | [diff] [blame] | 41 |  | 
| Elliott Hughes | 468efc8 | 2018-07-10 14:39:49 -0700 | [diff] [blame] | 42 | static 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 Ivanov | 9cf6fc3 | 2015-06-04 14:33:18 -0700 | [diff] [blame] | 46 | struct 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 |  | 
|  | 57 | static struct atexit *__atexit; | 
| Dmitriy Ivanov | 53c3c27 | 2014-07-11 12:59:16 -0700 | [diff] [blame] | 58 | static int restartloop; | 
|  | 59 |  | 
| Dmitriy Ivanov | ea295f6 | 2014-11-20 20:47:02 -0800 | [diff] [blame] | 60 | /* BEGIN android-changed: __unregister_atfork is used by __cxa_finalize */ | 
|  | 61 | extern void __unregister_atfork(void* dso); | 
|  | 62 | /* END android-changed */ | 
|  | 63 |  | 
| Dmitriy Ivanov | 53c3c27 | 2014-07-11 12:59:16 -0700 | [diff] [blame] | 64 | /* | 
|  | 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 | */ | 
|  | 81 | int | 
|  | 82 | __cxa_atexit(void (*func)(void *), void *arg, void *dso) | 
|  | 83 | { | 
| Dmitriy Ivanov | 53c3c27 | 2014-07-11 12:59:16 -0700 | [diff] [blame] | 84 | struct atexit_fn *fnp; | 
| Dmitriy Ivanov | ea295f6 | 2014-11-20 20:47:02 -0800 | [diff] [blame] | 85 | size_t pgsize = getpagesize(); | 
| Dmitriy Ivanov | 53c3c27 | 2014-07-11 12:59:16 -0700 | [diff] [blame] | 86 | int ret = -1; | 
|  | 87 |  | 
| Elliott Hughes | 468efc8 | 2018-07-10 14:39:49 -0700 | [diff] [blame] | 88 | if (pgsize < sizeof(struct atexit)) | 
| Dmitriy Ivanov | 53c3c27 | 2014-07-11 12:59:16 -0700 | [diff] [blame] | 89 | return (-1); | 
|  | 90 | _ATEXIT_LOCK(); | 
| Elliott Hughes | 468efc8 | 2018-07-10 14:39:49 -0700 | [diff] [blame] | 91 | struct atexit *p = __atexit; | 
| Dmitriy Ivanov | 53c3c27 | 2014-07-11 12:59:16 -0700 | [diff] [blame] | 92 | 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 Micay | f0a7347 | 2015-07-25 15:40:14 -0400 | [diff] [blame] | 103 | /* BEGIN android-changed */ | 
|  | 104 | prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, p, pgsize, | 
|  | 105 | "atexit handlers"); | 
|  | 106 | /* END android-changed */ | 
| Dmitriy Ivanov | 53c3c27 | 2014-07-11 12:59:16 -0700 | [diff] [blame] | 107 | 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; | 
|  | 125 | unlock: | 
|  | 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 | */ | 
|  | 135 | void | 
|  | 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 |  | 
|  | 146 | restart: | 
|  | 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 Ivanov | ea295f6 | 2014-11-20 20:47:02 -0800 | [diff] [blame] | 188 |  | 
| Ryan Prichard | c5d8c6c | 2019-04-24 14:14:47 -0700 | [diff] [blame] | 189 | /* If called via exit(), flush output of all open files. */ | 
|  | 190 | if (dso == NULL) { | 
| Ryan Prichard | c485cdb | 2019-04-30 14:47:34 -0700 | [diff] [blame] | 191 | extern void __libc_stdio_cleanup(void); | 
|  | 192 | __libc_stdio_cleanup(); | 
| Ryan Prichard | c5d8c6c | 2019-04-24 14:14:47 -0700 | [diff] [blame] | 193 | } | 
| Elliott Hughes | bb46afd | 2015-12-04 18:03:12 -0800 | [diff] [blame] | 194 |  | 
| Dmitriy Ivanov | ea295f6 | 2014-11-20 20:47:02 -0800 | [diff] [blame] | 195 | /* BEGIN android-changed: call __unregister_atfork if dso is not null */ | 
|  | 196 | if (dso != NULL) { | 
|  | 197 | __unregister_atfork(dso); | 
|  | 198 | } | 
|  | 199 | /* END android-changed */ | 
| Dmitriy Ivanov | 53c3c27 | 2014-07-11 12:59:16 -0700 | [diff] [blame] | 200 | } |