1 /* $OpenBSD: atexit.c,v 1.17 2013/12/28 18:38:42 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 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 * Call all handlers registered with __cxa_atexit() for the shared 106 * object owning 'dso'. 107 * Note: if 'dso' is NULL, then all remaining handlers are called. 108 */ 109 void 110 __cxa_finalize(void *dso) 111 { 112 struct atexit *p, *q; 113 struct atexit_fn fn; 114 int n, pgsize = getpagesize(); 115 static int call_depth; 116 117 call_depth++; 118 119 for (p = __atexit; p != NULL; p = p->next) { 120 for (n = p->ind; --n >= 0;) { 121 if (p->fns[n].fn_ptr.cxa_func == NULL) 122 continue; /* already called */ 123 if (dso != NULL && dso != p->fns[n].fn_dso) 124 continue; /* wrong DSO */ 125 126 /* 127 * Mark handler as having been already called to avoid 128 * dupes and loops, then call the appropriate function. 129 */ 130 fn = p->fns[n]; 131 if (mprotect(p, pgsize, PROT_READ | PROT_WRITE) == 0) { 132 p->fns[n].fn_ptr.cxa_func = NULL; 133 mprotect(p, pgsize, PROT_READ); 134 } 135 if (fn.fn_dso != NULL) 136 (*fn.fn_ptr.cxa_func)(fn.fn_arg); 137 else 138 (*fn.fn_ptr.std_func)(); 139 } 140 } 141 142 call_depth--; 143 144 /* 145 * If called via exit(), unmap the pages since we have now run 146 * all the handlers. We defer this until calldepth == 0 so that 147 * we don't unmap things prematurely if called recursively. 148 */ 149 if (dso == NULL && call_depth == 0) { 150 for (p = __atexit; p != NULL; ) { 151 q = p; 152 p = p->next; 153 munmap(q, pgsize); 154 } 155 __atexit = NULL; 156 } 157 } 158 159 /* 160 * Register the cleanup function 161 */ 162 void 163 __atexit_register_cleanup(void (*func)(void)) 164 { 165 struct atexit *p; 166 int pgsize = getpagesize(); 167 168 if (pgsize < sizeof(*p)) 169 return; 170 _ATEXIT_LOCK(); 171 p = __atexit; 172 while (p != NULL && p->next != NULL) 173 p = p->next; 174 if (p == NULL) { 175 p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE, 176 MAP_ANON | MAP_PRIVATE, -1, 0); 177 if (p == MAP_FAILED) 178 goto unlock; 179 p->ind = 1; 180 p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) / 181 sizeof(p->fns[0]); 182 p->next = NULL; 183 __atexit = p; 184 } else { 185 if (mprotect(p, pgsize, PROT_READ | PROT_WRITE)) 186 goto unlock; 187 } 188 p->fns[0].fn_ptr.std_func = func; 189 p->fns[0].fn_arg = NULL; 190 p->fns[0].fn_dso = NULL; 191 mprotect(p, pgsize, PROT_READ); 192 unlock: 193 _ATEXIT_UNLOCK(); 194 } 195