1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2018 Andrew Turner 5 * 6 * This software was developed by SRI International and the University of 7 * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237 8 * ("CTSRD"), as part of the DARPA CRASH research programme. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #ifndef DSO_LIB 34 #include <atf-c.h> 35 #endif 36 37 #include <crt.h> 38 39 typedef void (*func_ptr)(void); 40 41 extern volatile int ctors_run; 42 extern volatile int preinit_array_run; 43 extern volatile int preinit_array_state; 44 extern volatile int init_array_run; 45 extern volatile int init_array_state; 46 47 #ifndef DSO_BASE 48 volatile int ctors_run; 49 volatile int preinit_array_run; 50 volatile int preinit_array_state = -1; 51 volatile int init_array_run; 52 volatile int init_array_state = -1; 53 #endif 54 55 #ifndef DSO_BASE 56 static void 57 ctors_handler(void) 58 { 59 60 ctors_run = 1; 61 } 62 __section(".ctors") __used static func_ptr ctors_func = 63 &ctors_handler; 64 #endif 65 66 #ifndef DSO_LIB 67 ATF_TC_WITHOUT_HEAD(ctors_test); 68 ATF_TC_BODY(ctors_test, tc) 69 { 70 71 #ifdef HAVE_CTORS 72 ATF_REQUIRE_MSG(ctors_run == 1, ".ctors not run"); 73 #else 74 ATF_REQUIRE_MSG(ctors_run == 0, ".ctors run"); 75 #endif 76 } 77 #endif 78 79 #if !defined(DSO_BASE) && !defined(DSO_LIB) 80 static void 81 preinit_array_handler(void) 82 { 83 84 preinit_array_run = 1; 85 preinit_array_state = init_array_run; 86 } 87 __section(".preinit_array") __used static func_ptr preinit_array_func = 88 &preinit_array_handler; 89 #endif 90 91 #ifndef DSO_LIB 92 ATF_TC_WITHOUT_HEAD(preinit_array_test); 93 ATF_TC_BODY(preinit_array_test, tc) 94 { 95 96 #ifdef DSO_BASE 97 /* Check .preinit_array wasn't run in a DSO */ 98 ATF_REQUIRE_MSG(preinit_array_run == 0, ".preinit_array run in DSO"); 99 #else 100 ATF_REQUIRE_MSG(preinit_array_run == 1, ".preinit_array not run"); 101 ATF_REQUIRE_MSG(preinit_array_state == 0, 102 ".preinit_array was not run before .init_array"); 103 #endif 104 } 105 #endif 106 107 #ifndef DSO_BASE 108 static void 109 init_array_handler(void) 110 { 111 112 init_array_run = 1; 113 init_array_state = preinit_array_run; 114 } 115 __section(".init_array") __used static func_ptr init_array_func = 116 &init_array_handler; 117 #endif 118 119 #ifndef DSO_LIB 120 ATF_TC_WITHOUT_HEAD(init_array_test); 121 ATF_TC_BODY(init_array_test, tc) 122 { 123 124 ATF_REQUIRE_MSG(init_array_run == 1, ".init_array not run"); 125 #ifndef DSO_BASE 126 ATF_REQUIRE_MSG(init_array_state == 1, 127 ".init_array was not run after .preinit_array"); 128 #endif 129 } 130 131 ATF_TP_ADD_TCS(tp) 132 { 133 134 ATF_TP_ADD_TC(tp, ctors_test); 135 ATF_TP_ADD_TC(tp, preinit_array_test); 136 ATF_TP_ADD_TC(tp, init_array_test); 137 138 return (atf_no_error()); 139 } 140 #endif 141