1 /* $OpenBSD: atexit_test.c,v 1.8 2015/10/25 04:11:00 guenther Exp $ */ 2 3 /* 4 * Copyright (c) 2002 Daniel Hartmeier 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * - Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * - Redistributions in binary form must reproduce the above 14 * copyright notice, this list of conditions and the following 15 * disclaimer in the documentation and/or other materials provided 16 * with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 22 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 * 31 * Effort sponsored in part by the Defense Advanced Research Projects 32 * Agency (DARPA) and Air Force Research Laboratory, Air Force 33 * Materiel Command, USAF, under agreement number F30602-01-2-0537. 34 * 35 */ 36 37 #include <stdio.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include <signal.h> 41 #include <unistd.h> 42 #include "stdlib/atexit.h" 43 44 void handle_first(void); 45 void handle_middle(void); 46 void handle_last(void); 47 void handle_invalid(void *); 48 void handle_cleanup(void); 49 void handle_signal(int); 50 51 static int counter; 52 53 int 54 main(int argc, char *argv[]) 55 { 56 int i; 57 58 if (argc != 2 || (strcmp(argv[1], "-valid") && 59 strcmp(argv[1], "-invalid-atexit") && 60 strcmp(argv[1], "-invalid-cleanup"))) { 61 fprintf(stderr, "%s -valid/-invalid-atexit/-invalid-cleanup\n", 62 argv[0]); 63 return (1); 64 } 65 fprintf(stderr, "main()\n"); 66 if (atexit(handle_last)) { 67 perror("atexit(handle_last) failed"); 68 return (1); 69 } 70 for (i = 0; i < 65535; ++i) { 71 if (atexit(handle_middle)) { 72 perror("atexit(handle_middle) failed"); 73 return (1); 74 } 75 } 76 if (atexit(handle_first)) { 77 perror("atexit(handle_first) failed"); 78 return (1); 79 } 80 /* this is supposed to segfault */ 81 if (!strcmp(argv[1], "-invalid-atexit")) { 82 signal(SIGSEGV, handle_signal); 83 __atexit->fns[0].fn_ptr = handle_invalid; 84 } else if (!strcmp(argv[1], "-invalid-cleanup")) { 85 struct atexit *p = __atexit; 86 87 signal(SIGSEGV, handle_signal); 88 while (p != NULL && p->next != NULL) 89 p = p->next; 90 if (p == NULL) 91 fprintf(stderr, "p == NULL, no page found\n"); 92 p->fns[0].fn_ptr = handle_invalid; 93 } 94 __atexit_register_cleanup(handle_cleanup); 95 counter = 0; 96 fprintf(stderr, "main() returns\n"); 97 return (0); 98 } 99 100 void 101 handle_first(void) 102 { 103 fprintf(stderr, "handle_first() counter == %i\n", counter); 104 } 105 106 void 107 handle_middle(void) 108 { 109 counter++; 110 } 111 112 void 113 handle_last(void) 114 { 115 fprintf(stderr, "handle_last() counter == %i\n", counter); 116 } 117 118 void 119 handle_cleanup(void) 120 { 121 fprintf(stderr, "handle_cleanup()\n"); 122 } 123 124 void 125 handle_invalid(void *arg) 126 { 127 fprintf(stderr, "handle_invalid() THIS SHOULD HAVE SEGFAULTED INSTEAD!\n"); 128 } 129 130 void 131 handle_signal(int sigraised) 132 { 133 switch (sigraised) { 134 case SIGSEGV: 135 dprintf(STDERR_FILENO, "SIGSEGV\n"); 136 exit(0); 137 default: 138 dprintf(STDERR_FILENO, "unexpected signal caught\n"); 139 exit(1); 140 } 141 } 142