xref: /freebsd/libexec/rtld-elf/tests/rtld_deepbind/rtld_deepbind.c (revision d9c543b6b0cabea6e6679d70b4e701018e7eab80)
1*d9c543b6SKyle Evans /*-
2*d9c543b6SKyle Evans  *
3*d9c543b6SKyle Evans  * Copyright (C) 2023 NetApp, Inc.
4*d9c543b6SKyle Evans  *
5*d9c543b6SKyle Evans  * SPDX-License-Identifier: BSD-2-Clause
6*d9c543b6SKyle Evans  *
7*d9c543b6SKyle Evans  */
8*d9c543b6SKyle Evans 
9*d9c543b6SKyle Evans #include <dlfcn.h>
10*d9c543b6SKyle Evans 
11*d9c543b6SKyle Evans #include <atf-c.h>
12*d9c543b6SKyle Evans 
13*d9c543b6SKyle Evans int get_value(void);
14*d9c543b6SKyle Evans void set_value(int);
15*d9c543b6SKyle Evans 
16*d9c543b6SKyle Evans #define	APP_VALUE	5
17*d9c543b6SKyle Evans #define	LIB_VALUE	20
18*d9c543b6SKyle Evans 
19*d9c543b6SKyle Evans ATF_TC_WITHOUT_HEAD(deepbind_simple);
ATF_TC_BODY(deepbind_simple,tc)20*d9c543b6SKyle Evans ATF_TC_BODY(deepbind_simple, tc)
21*d9c543b6SKyle Evans {
22*d9c543b6SKyle Evans 	void *hdl;
23*d9c543b6SKyle Evans 	void (*proxy_set_value)(int);
24*d9c543b6SKyle Evans 	int (*proxy_get_value)(void);
25*d9c543b6SKyle Evans 	int app_value, lib_value;
26*d9c543b6SKyle Evans 
27*d9c543b6SKyle Evans 	set_value(APP_VALUE);
28*d9c543b6SKyle Evans 
29*d9c543b6SKyle Evans 	/*
30*d9c543b6SKyle Evans 	 * libdeep has a dependency on libval2.so, which is a rebuild of
31*d9c543b6SKyle Evans 	 * libval.so that provides get_value() and set_value() for both us and
32*d9c543b6SKyle Evans 	 * the lib.  The lib's get_value() and set_value() should bind to the
33*d9c543b6SKyle Evans 	 * versions in libval2 instead of libval with RTLD_DEEPBIND.
34*d9c543b6SKyle Evans 	 */
35*d9c543b6SKyle Evans 	hdl = dlopen("$ORIGIN/libdeep.so", RTLD_LAZY | RTLD_DEEPBIND);
36*d9c543b6SKyle Evans 	ATF_REQUIRE(hdl != NULL);
37*d9c543b6SKyle Evans 
38*d9c543b6SKyle Evans 	proxy_set_value = dlsym(hdl, "proxy_set_value");
39*d9c543b6SKyle Evans 	ATF_REQUIRE(proxy_set_value != NULL);
40*d9c543b6SKyle Evans 
41*d9c543b6SKyle Evans 	proxy_get_value = dlsym(hdl, "proxy_get_value");
42*d9c543b6SKyle Evans 	ATF_REQUIRE(proxy_get_value != NULL);
43*d9c543b6SKyle Evans 
44*d9c543b6SKyle Evans 	(*proxy_set_value)(LIB_VALUE);
45*d9c543b6SKyle Evans 
46*d9c543b6SKyle Evans 	lib_value = (*proxy_get_value)();
47*d9c543b6SKyle Evans 	app_value = get_value();
48*d9c543b6SKyle Evans 
49*d9c543b6SKyle Evans 	/*
50*d9c543b6SKyle Evans 	 * In the initial implementation or if libdeep.so is *not* linked
51*d9c543b6SKyle Evans 	 * against its own libval2, then these both return the later set
52*d9c543b6SKyle Evans 	 * LIB_VALUE (20) as they bind to the symbol provided by libval and
53*d9c543b6SKyle Evans 	 * use its .bss val.
54*d9c543b6SKyle Evans 	 */
55*d9c543b6SKyle Evans 	ATF_REQUIRE_INTEQ(lib_value, LIB_VALUE);
56*d9c543b6SKyle Evans 	ATF_REQUIRE_INTEQ(app_value, APP_VALUE);
57*d9c543b6SKyle Evans }
58*d9c543b6SKyle Evans 
ATF_TP_ADD_TCS(tp)59*d9c543b6SKyle Evans ATF_TP_ADD_TCS(tp)
60*d9c543b6SKyle Evans {
61*d9c543b6SKyle Evans 
62*d9c543b6SKyle Evans 	ATF_TP_ADD_TC(tp, deepbind_simple);
63*d9c543b6SKyle Evans 
64*d9c543b6SKyle Evans 	return atf_no_error();
65*d9c543b6SKyle Evans }
66