1295059Ssobomax/*- 2295059Ssobomax * Copyright (c) 2016 Maksym Sobolyev 3295059Ssobomax * All rights reserved. 4295059Ssobomax * 5295059Ssobomax * Redistribution and use in source and binary forms, with or without 6295059Ssobomax * modification, are permitted provided that the following conditions 7295059Ssobomax * are met: 8295059Ssobomax * 1. Redistributions of source code must retain the above copyright 9295059Ssobomax * notice, this list of conditions and the following disclaimer. 10295059Ssobomax * 2. Redistributions in binary form must reproduce the above copyright 11295059Ssobomax * notice, this list of conditions and the following disclaimer in the 12295059Ssobomax * documentation and/or other materials provided with the distribution. 13295059Ssobomax * 14295059Ssobomax * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15295059Ssobomax * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16295059Ssobomax * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17295059Ssobomax * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18295059Ssobomax * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19295059Ssobomax * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20295059Ssobomax * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21295059Ssobomax * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22295059Ssobomax * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23295059Ssobomax * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24295059Ssobomax * SUCH DAMAGE. 25295059Ssobomax */ 26295059Ssobomax 27295059Ssobomax#include <sys/cdefs.h> 28295059Ssobomax__FBSDID("$FreeBSD: releng/11.0/lib/libc/tests/gen/dlopen_empty_test.c 295059 2016-01-30 04:16:05Z sobomax $"); 29295059Ssobomax 30295059Ssobomax#include <sys/stat.h> 31295059Ssobomax#include <dlfcn.h> 32295059Ssobomax#include <errno.h> 33295059Ssobomax#include <fcntl.h> 34295059Ssobomax#include <signal.h> 35295059Ssobomax#include <stdio.h> 36295059Ssobomax#include <stdlib.h> 37295059Ssobomax#include <unistd.h> 38295059Ssobomax 39295059Ssobomax#include <atf-c.h> 40295059Ssobomax 41295059Ssobomaxstatic const char *funname; 42295059Ssobomaxstatic char *soname; 43295059Ssobomax 44295059Ssobomaxstatic void 45295059Ssobomaxsigsegv_handler(int sig __unused) 46295059Ssobomax{ 47295059Ssobomax unlink(soname); 48295059Ssobomax free(soname); 49295059Ssobomax atf_tc_fail("got SIGSEGV in the %s(3)", funname); 50295059Ssobomax} 51295059Ssobomax 52295059SsobomaxATF_TC(dlopen_empty_test); 53295059SsobomaxATF_TC_HEAD(dlopen_empty_test, tc) 54295059Ssobomax{ 55295059Ssobomax atf_tc_set_md_var(tc, "descr", "Tests the dlopen() of an empty file " 56295059Ssobomax "returns an error"); 57295059Ssobomax} 58295059SsobomaxATF_TC_BODY(dlopen_empty_test, tc) 59295059Ssobomax{ 60295059Ssobomax char tempname[] = "/tmp/temp.XXXXXX"; 61295059Ssobomax char *fname; 62295059Ssobomax int fd; 63295059Ssobomax void *dlh; 64295059Ssobomax struct sigaction act, oact; 65295059Ssobomax 66295059Ssobomax fname = mktemp(tempname); 67295059Ssobomax ATF_REQUIRE_MSG(fname != NULL, "mktemp failed; errno=%d", errno); 68295059Ssobomax asprintf(&soname, "%s.so", fname); 69295059Ssobomax ATF_REQUIRE_MSG(soname != NULL, "asprintf failed; errno=%d", ENOMEM); 70295059Ssobomax fd = open(soname, O_WRONLY | O_CREAT | O_TRUNC, DEFFILEMODE); 71295059Ssobomax ATF_REQUIRE_MSG(fd != -1, "open(\"%s\") failed; errno=%d", soname, errno); 72295059Ssobomax close(fd); 73295059Ssobomax 74295059Ssobomax act.sa_handler = sigsegv_handler; 75295059Ssobomax act.sa_flags = 0; 76295059Ssobomax sigemptyset(&act.sa_mask); 77295059Ssobomax ATF_CHECK_MSG(sigaction(SIGSEGV, &act, &oact) != -1, 78295059Ssobomax "sigaction() failed"); 79295059Ssobomax 80295059Ssobomax funname = "dlopen"; 81295059Ssobomax dlh = dlopen(soname, RTLD_LAZY); 82295059Ssobomax if (dlh != NULL) { 83295059Ssobomax funname = "dlclose"; 84295059Ssobomax dlclose(dlh); 85295059Ssobomax } 86295059Ssobomax ATF_REQUIRE_MSG(dlh == NULL, "dlopen(\"%s\") did not fail", soname); 87295059Ssobomax unlink(soname); 88295059Ssobomax free(soname); 89295059Ssobomax} 90295059Ssobomax 91295059SsobomaxATF_TP_ADD_TCS(tp) 92295059Ssobomax{ 93295059Ssobomax 94295059Ssobomax ATF_TP_ADD_TC(tp, dlopen_empty_test); 95295059Ssobomax 96295059Ssobomax return (atf_no_error()); 97295059Ssobomax} 98