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