xref: /freebsd/lib/libc/tests/gen/dlopen_empty_test.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
1*7fd852f8SMaxim Sobolev /*-
2*7fd852f8SMaxim Sobolev  * Copyright (c) 2016 Maksym Sobolyev
3*7fd852f8SMaxim Sobolev  * All rights reserved.
4*7fd852f8SMaxim Sobolev  *
5*7fd852f8SMaxim Sobolev  * Redistribution and use in source and binary forms, with or without
6*7fd852f8SMaxim Sobolev  * modification, are permitted provided that the following conditions
7*7fd852f8SMaxim Sobolev  * are met:
8*7fd852f8SMaxim Sobolev  * 1. Redistributions of source code must retain the above copyright
9*7fd852f8SMaxim Sobolev  *    notice, this list of conditions and the following disclaimer.
10*7fd852f8SMaxim Sobolev  * 2. Redistributions in binary form must reproduce the above copyright
11*7fd852f8SMaxim Sobolev  *    notice, this list of conditions and the following disclaimer in the
12*7fd852f8SMaxim Sobolev  *    documentation and/or other materials provided with the distribution.
13*7fd852f8SMaxim Sobolev  *
14*7fd852f8SMaxim Sobolev  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*7fd852f8SMaxim Sobolev  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*7fd852f8SMaxim Sobolev  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*7fd852f8SMaxim Sobolev  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*7fd852f8SMaxim Sobolev  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*7fd852f8SMaxim Sobolev  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*7fd852f8SMaxim Sobolev  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*7fd852f8SMaxim Sobolev  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*7fd852f8SMaxim Sobolev  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*7fd852f8SMaxim Sobolev  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*7fd852f8SMaxim Sobolev  * SUCH DAMAGE.
25*7fd852f8SMaxim Sobolev  */
26*7fd852f8SMaxim Sobolev 
27*7fd852f8SMaxim Sobolev #include <sys/stat.h>
28*7fd852f8SMaxim Sobolev #include <dlfcn.h>
29*7fd852f8SMaxim Sobolev #include <errno.h>
30*7fd852f8SMaxim Sobolev #include <fcntl.h>
31*7fd852f8SMaxim Sobolev #include <signal.h>
32*7fd852f8SMaxim Sobolev #include <stdio.h>
33*7fd852f8SMaxim Sobolev #include <stdlib.h>
34*7fd852f8SMaxim Sobolev #include <unistd.h>
35*7fd852f8SMaxim Sobolev 
36*7fd852f8SMaxim Sobolev #include <atf-c.h>
37*7fd852f8SMaxim Sobolev 
38*7fd852f8SMaxim Sobolev static const char *funname;
39*7fd852f8SMaxim Sobolev static char *soname;
40*7fd852f8SMaxim Sobolev 
41*7fd852f8SMaxim Sobolev static void
sigsegv_handler(int sig __unused)42*7fd852f8SMaxim Sobolev sigsegv_handler(int sig __unused)
43*7fd852f8SMaxim Sobolev {
44*7fd852f8SMaxim Sobolev         unlink(soname);
45*7fd852f8SMaxim Sobolev         free(soname);
46*7fd852f8SMaxim Sobolev         atf_tc_fail("got SIGSEGV in the %s(3)", funname);
47*7fd852f8SMaxim Sobolev }
48*7fd852f8SMaxim Sobolev 
49*7fd852f8SMaxim Sobolev ATF_TC(dlopen_empty_test);
ATF_TC_HEAD(dlopen_empty_test,tc)50*7fd852f8SMaxim Sobolev ATF_TC_HEAD(dlopen_empty_test, tc)
51*7fd852f8SMaxim Sobolev {
52*7fd852f8SMaxim Sobolev         atf_tc_set_md_var(tc, "descr", "Tests the dlopen() of an empty file "
53*7fd852f8SMaxim Sobolev                       "returns an error");
54*7fd852f8SMaxim Sobolev }
ATF_TC_BODY(dlopen_empty_test,tc)55*7fd852f8SMaxim Sobolev ATF_TC_BODY(dlopen_empty_test, tc)
56*7fd852f8SMaxim Sobolev {
57*7fd852f8SMaxim Sobolev         char tempname[] = "/tmp/temp.XXXXXX";
58*7fd852f8SMaxim Sobolev         char *fname;
59*7fd852f8SMaxim Sobolev         int fd;
60*7fd852f8SMaxim Sobolev         void *dlh;
61*7fd852f8SMaxim Sobolev         struct sigaction act, oact;
62*7fd852f8SMaxim Sobolev 
63*7fd852f8SMaxim Sobolev         fname = mktemp(tempname);
64*7fd852f8SMaxim Sobolev         ATF_REQUIRE_MSG(fname != NULL, "mktemp failed; errno=%d", errno);
65*7fd852f8SMaxim Sobolev         asprintf(&soname, "%s.so", fname);
66*7fd852f8SMaxim Sobolev         ATF_REQUIRE_MSG(soname != NULL, "asprintf failed; errno=%d", ENOMEM);
67*7fd852f8SMaxim Sobolev         fd = open(soname, O_WRONLY | O_CREAT | O_TRUNC, DEFFILEMODE);
68*7fd852f8SMaxim Sobolev         ATF_REQUIRE_MSG(fd != -1, "open(\"%s\") failed; errno=%d", soname, errno);
69*7fd852f8SMaxim Sobolev         close(fd);
70*7fd852f8SMaxim Sobolev 
71*7fd852f8SMaxim Sobolev         act.sa_handler = sigsegv_handler;
72*7fd852f8SMaxim Sobolev         act.sa_flags = 0;
73*7fd852f8SMaxim Sobolev         sigemptyset(&act.sa_mask);
74*7fd852f8SMaxim Sobolev         ATF_CHECK_MSG(sigaction(SIGSEGV, &act, &oact) != -1,
75*7fd852f8SMaxim Sobolev             "sigaction() failed");
76*7fd852f8SMaxim Sobolev 
77*7fd852f8SMaxim Sobolev         funname = "dlopen";
78*7fd852f8SMaxim Sobolev         dlh = dlopen(soname, RTLD_LAZY);
79*7fd852f8SMaxim Sobolev         if (dlh != NULL) {
80*7fd852f8SMaxim Sobolev                 funname = "dlclose";
81*7fd852f8SMaxim Sobolev                 dlclose(dlh);
82*7fd852f8SMaxim Sobolev         }
83*7fd852f8SMaxim Sobolev         ATF_REQUIRE_MSG(dlh == NULL, "dlopen(\"%s\") did not fail", soname);
84*7fd852f8SMaxim Sobolev         unlink(soname);
85*7fd852f8SMaxim Sobolev         free(soname);
86*7fd852f8SMaxim Sobolev }
87*7fd852f8SMaxim Sobolev 
ATF_TP_ADD_TCS(tp)88*7fd852f8SMaxim Sobolev ATF_TP_ADD_TCS(tp)
89*7fd852f8SMaxim Sobolev {
90*7fd852f8SMaxim Sobolev 
91*7fd852f8SMaxim Sobolev         ATF_TP_ADD_TC(tp, dlopen_empty_test);
92*7fd852f8SMaxim Sobolev 
93*7fd852f8SMaxim Sobolev         return (atf_no_error());
94*7fd852f8SMaxim Sobolev }
95