Path: blob/main/lib/libc/tests/gen/dlopen_empty_test.c
39500 views
/*-1* Copyright (c) 2016 Maksym Sobolyev2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526#include <sys/stat.h>27#include <dlfcn.h>28#include <errno.h>29#include <fcntl.h>30#include <signal.h>31#include <stdio.h>32#include <stdlib.h>33#include <unistd.h>3435#include <atf-c.h>3637static const char *funname;38static char *soname;3940static void41sigsegv_handler(int sig __unused)42{43unlink(soname);44free(soname);45atf_tc_fail("got SIGSEGV in the %s(3)", funname);46}4748ATF_TC(dlopen_empty_test);49ATF_TC_HEAD(dlopen_empty_test, tc)50{51atf_tc_set_md_var(tc, "descr", "Tests the dlopen() of an empty file "52"returns an error");53}54ATF_TC_BODY(dlopen_empty_test, tc)55{56char tempname[] = "/tmp/temp.XXXXXX";57char *fname;58int fd;59void *dlh;60struct sigaction act, oact;6162fname = mktemp(tempname);63ATF_REQUIRE_MSG(fname != NULL, "mktemp failed; errno=%d", errno);64asprintf(&soname, "%s.so", fname);65ATF_REQUIRE_MSG(soname != NULL, "asprintf failed; errno=%d", ENOMEM);66fd = open(soname, O_WRONLY | O_CREAT | O_TRUNC, DEFFILEMODE);67ATF_REQUIRE_MSG(fd != -1, "open(\"%s\") failed; errno=%d", soname, errno);68close(fd);6970act.sa_handler = sigsegv_handler;71act.sa_flags = 0;72sigemptyset(&act.sa_mask);73ATF_CHECK_MSG(sigaction(SIGSEGV, &act, &oact) != -1,74"sigaction() failed");7576funname = "dlopen";77dlh = dlopen(soname, RTLD_LAZY);78if (dlh != NULL) {79funname = "dlclose";80dlclose(dlh);81}82ATF_REQUIRE_MSG(dlh == NULL, "dlopen(\"%s\") did not fail", soname);83unlink(soname);84free(soname);85}8687ATF_TP_ADD_TCS(tp)88{8990ATF_TP_ADD_TC(tp, dlopen_empty_test);9192return (atf_no_error());93}949596