xref: /freebsd/tests/sys/vm/stack/stack_dlopen_exec_test.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
1 /*-
2  * Copyright (c) 2023 Dmitry Chagin <dchagin@FreeBSD.org>
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  */
6 
7 #include <sys/systm.h>
8 #include <vm/vm_param.h>
9 
10 #include <atf-c.h>
11 #include <dlfcn.h>
12 
13 static int jumpstack0(void) __noinline;
14 static int jumpstack1(void) __noinline;
15 
16 static int (*socheckstack)(void) = NULL;
17 
18 static int
19 checkstack(void)
20 {
21 	void *fh;
22 
23 	if (socheckstack == NULL) {
24 		fh = dlopen("libsoxstack.so", RTLD_LAZY);
25 		ATF_REQUIRE(fh != NULL);
26 		socheckstack = dlsym(fh, "checkstack");
27 		ATF_REQUIRE(socheckstack != NULL);
28 	}
29 	return (socheckstack());
30 }
31 
32 static int
33 jumpstack0(void)
34 {
35 	char stack[SGROWSIZ];
36 
37 	explicit_bzero(stack, sizeof(stack));
38 	return (checkstack());
39 }
40 
41 static int
42 jumpstack1(void)
43 {
44 	char stack[SGROWSIZ * 2];
45 
46 	explicit_bzero(stack, sizeof(stack));
47 	return (checkstack());
48 }
49 
50 ATF_TC_WITHOUT_HEAD(dlopen_test);
51 ATF_TC_BODY(dlopen_test, tc)
52 {
53 
54 	ATF_REQUIRE(jumpstack0() == 0);
55 	ATF_REQUIRE(jumpstack1() == 0);
56 }
57 
58 ATF_TP_ADD_TCS(tp)
59 {
60 
61 	ATF_TP_ADD_TC(tp, dlopen_test);
62 
63 	return (atf_no_error());
64 }
65