1 /* $OpenBSD: atexit.c,v 1.16 2013/06/02 21:08:36 matthew 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 struct atexit *__atexit; 41 42 /* 43 * Function pointers are stored in a linked list of pages. The list 44 * is initially empty, and pages are allocated on demand. The first 45 * function pointer in the first allocated page (the last one in 46 * the linked list) is reserved for the cleanup function. 47 * 48 * Outside the following functions, all pages are mprotect()'ed 49 * to prevent unintentional/malicious corruption. 50 */ 51 52 /* 53 * Register a function to be performed at exit or when a shared object 54 * with the given dso handle is unloaded dynamically. Also used as 55 * the backend for atexit(). For more info on this API, see: 56 * 57 * http://www.codesourcery.com/cxx-abi/abi.html#dso-dtor 58 */ 59 int 60 __cxa_atexit(void (*func)(void *), void *arg, void *dso) 61 { 62 struct atexit *p = __atexit; 63 struct atexit_fn *fnp; 64 int pgsize = getpagesize(); 65 int ret = -1; 66 67 if (pgsize < sizeof(*p)) 68 return (-1); 69 _ATEXIT_LOCK(); 70 p = __atexit; 71 if (p != NULL) { 72 if (p->ind + 1 >= p->max) 73 p = NULL; 74 else if (mprotect(p, pgsize, PROT_READ | PROT_WRITE)) 75 goto unlock; 76 } 77 if (p == NULL) { 78 p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE, 79 MAP_ANON | MAP_PRIVATE, -1, 0); 80 if (p == MAP_FAILED) 81 goto unlock; 82 if (__atexit == NULL) { 83 memset(&p->fns[0], 0, sizeof(p->fns[0])); 84 p->ind = 1; 85 } else 86 p->ind = 0; 87 p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) / 88 sizeof(p->fns[0]); 89 p->next = __atexit; 90 __atexit = p; 91 } 92 fnp = &p->fns[p->ind++]; 93 fnp->fn_ptr.cxa_func = func; 94 fnp->fn_arg = arg; 95 fnp->fn_dso = dso; 96 if (mprotect(p, pgsize, PROT_READ)) 97 goto unlock; 98 ret = 0; 99 unlock: 100 _ATEXIT_UNLOCK(); 101 return (ret); 102 } 103 104 /* 105 * Register a function to be performed at exit. 106 */ 107 int 108 atexit(void (*func)(void)) 109 { 110 return (__cxa_atexit((void (*)(void *))func, NULL, NULL)); 111 } 112 113 /* 114 * Call all handlers registered with __cxa_atexit() for the shared 115 * object owning 'dso'. 116 * Note: if 'dso' is NULL, then all remaining handlers are called. 117 */ 118 void 119 __cxa_finalize(void *dso) 120 { 121 struct atexit *p, *q; 122 struct atexit_fn fn; 123 int n, pgsize = getpagesize(); 124 static int call_depth; 125 126 call_depth++; 127 128 for (p = __atexit; p != NULL; p = p->next) { 129 for (n = p->ind; --n >= 0;) { 130 if (p->fns[n].fn_ptr.cxa_func == NULL) 131 continue; /* already called */ 132 if (dso != NULL && dso != p->fns[n].fn_dso) 133 continue; /* wrong DSO */ 134 135 /* 136 * Mark handler as having been already called to avoid 137 * dupes and loops, then call the appropriate function. 138 */ 139 fn = p->fns[n]; 140 if (mprotect(p, pgsize, PROT_READ | PROT_WRITE) == 0) { 141 p->fns[n].fn_ptr.cxa_func = NULL; 142 mprotect(p, pgsize, PROT_READ); 143 } 144 if (fn.fn_dso != NULL) 145 (*fn.fn_ptr.cxa_func)(fn.fn_arg); 146 else 147 (*fn.fn_ptr.std_func)(); 148 } 149 } 150 151 call_depth--; 152 153 /* 154 * If called via exit(), unmap the pages since we have now run 155 * all the handlers. We defer this until calldepth == 0 so that 156 * we don't unmap things prematurely if called recursively. 157 */ 158 if (dso == NULL && call_depth == 0) { 159 for (p = __atexit; p != NULL; ) { 160 q = p; 161 p = p->next; 162 munmap(q, pgsize); 163 } 164 __atexit = NULL; 165 } 166 } 167 168 /* 169 * Register the cleanup function 170 */ 171 void 172 __atexit_register_cleanup(void (*func)(void)) 173 { 174 struct atexit *p; 175 int pgsize = getpagesize(); 176 177 if (pgsize < sizeof(*p)) 178 return; 179 _ATEXIT_LOCK(); 180 p = __atexit; 181 while (p != NULL && p->next != NULL) 182 p = p->next; 183 if (p == NULL) { 184 p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE, 185 MAP_ANON | MAP_PRIVATE, -1, 0); 186 if (p == MAP_FAILED) 187 goto unlock; 188 p->ind = 1; 189 p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) / 190 sizeof(p->fns[0]); 191 p->next = NULL; 192 __atexit = p; 193 } else { 194 if (mprotect(p, pgsize, PROT_READ | PROT_WRITE)) 195 goto unlock; 196 } 197 p->fns[0].fn_ptr.std_func = func; 198 p->fns[0].fn_arg = NULL; 199 p->fns[0].fn_dso = NULL; 200 mprotect(p, pgsize, PROT_READ); 201 unlock: 202 _ATEXIT_UNLOCK(); 203 } 204