2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2018 Andrew Turner
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.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
41 typedef void (*func_ptr)(void);
43 extern volatile int jcr_run;
44 extern const func_ptr *jcr_ptr;
45 extern const void *jcr_func_ptr;
46 extern volatile int ctors_run;
47 extern volatile int preinit_array_run;
48 extern volatile int preinit_array_state;
49 extern volatile int init_array_run;
50 extern volatile int init_array_state;
54 const func_ptr *jcr_ptr;
55 volatile int ctors_run;
56 volatile int preinit_array_run;
57 volatile int preinit_array_state = -1;
58 volatile int init_array_run;
59 volatile int init_array_state = -1;
61 void _Jv_RegisterClasses(const func_ptr *);
63 __section(".jcr") __used static func_ptr jcr_func = (func_ptr)1;
64 const void *jcr_func_ptr = &jcr_func;
67 _Jv_RegisterClasses(const func_ptr *jcr)
76 ATF_TC_WITHOUT_HEAD(jcr_test);
77 ATF_TC_BODY(jcr_test, tc)
80 ATF_REQUIRE_MSG(jcr_run == 1, ".jcr not run");
81 ATF_REQUIRE_MSG(jcr_ptr == jcr_func_ptr,
82 "Incorrect pointer passed to _Jv_RegisterClasses");
93 __section(".ctors") __used static func_ptr ctors_func =
98 ATF_TC_WITHOUT_HEAD(ctors_test);
99 ATF_TC_BODY(ctors_test, tc)
103 ATF_REQUIRE_MSG(ctors_run == 1, ".ctors not run");
105 ATF_REQUIRE_MSG(ctors_run == 0, ".ctors run");
110 #if !defined(DSO_BASE) && !defined(DSO_LIB)
112 preinit_array_handler(void)
115 preinit_array_run = 1;
116 preinit_array_state = init_array_run;
118 __section(".preinit_array") __used static func_ptr preinit_array_func =
119 &preinit_array_handler;
123 ATF_TC_WITHOUT_HEAD(preinit_array_test);
124 ATF_TC_BODY(preinit_array_test, tc)
128 /* Check .preinit_array wasn't run in a DSO */
129 ATF_REQUIRE_MSG(preinit_array_run == 0, ".preinit_array run in DSO");
131 ATF_REQUIRE_MSG(preinit_array_run == 1, ".preinit_array not run");
132 ATF_REQUIRE_MSG(preinit_array_state == 0,
133 ".preinit_array was not run before .init_array");
140 init_array_handler(void)
144 init_array_state = preinit_array_run;
146 __section(".init_array") __used static func_ptr init_array_func =
151 ATF_TC_WITHOUT_HEAD(init_array_test);
152 ATF_TC_BODY(init_array_test, tc)
155 ATF_REQUIRE_MSG(init_array_run == 1, ".init_array not run");
157 ATF_REQUIRE_MSG(init_array_state == 1,
158 ".init_array was not run after .preinit_array");
165 ATF_TP_ADD_TC(tp, jcr_test);
166 ATF_TP_ADD_TC(tp, ctors_test);
167 ATF_TP_ADD_TC(tp, preinit_array_test);
168 ATF_TP_ADD_TC(tp, init_array_test);
170 return (atf_no_error());