Path: blob/main/lib/libc/tests/stdlib/cxa_thread_atexit_nothr_test.cc
39530 views
/*-1* Copyright (c) 2016 Mahdi Mokhtari <[email protected]>2* Copyright (c) 2016 The FreeBSD Foundation3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#include <dlfcn.h>28#include <atf-c++.hpp>29#include <cstdio>30#include <cstdlib>3132static FILE *output = NULL;3334struct Foo {35Foo() { ATF_REQUIRE(fprintf(output, "Created\n") > 0); }36~Foo() { ATF_REQUIRE(fprintf(output, "Destroyed\n") > 0); }37void use() { ATF_REQUIRE(fprintf(output, "Used\n") > 0); }38};3940static thread_local Foo f;4142/*43* This test must not be linked to libpthread.44*/45ATF_TEST_CASE_WITHOUT_HEAD(cxx__nothr);46ATF_TEST_CASE_BODY(cxx__nothr)47{48void *libthr_handle;4950/* Avoid coredump during f construction. */51output = stderr;5253libthr_handle = dlopen("libthr.so.3", RTLD_LAZY | RTLD_GLOBAL |54RTLD_NOLOAD);55ATF_REQUIRE(libthr_handle == NULL);56}5758static void59check_local_main(void)60{61static const char out_log[] = "Created\nUsed\nDestroyed\n";6263fflush(output);64ATF_REQUIRE(atf::utils::compare_file("test_main.txt", out_log));65}6667ATF_TEST_CASE_WITHOUT_HEAD(cxx__thread_local_main);68ATF_TEST_CASE_BODY(cxx__thread_local_main)69{7071ATF_REQUIRE((output = fopen("test_main.txt", "w")) != NULL);72f.use();73atexit(check_local_main);74}7576extern "C" int __cxa_thread_atexit(void (*)(void *), void *, void *);7778static void79again(void *arg)80{8182__cxa_thread_atexit(again, arg, &output);83}8485ATF_TEST_CASE_WITHOUT_HEAD(cxx__thread_inf_dtors);86ATF_TEST_CASE_BODY(cxx__thread_inf_dtors)87{8889again(NULL);90}9192ATF_INIT_TEST_CASES(tcs)93{9495ATF_ADD_TEST_CASE(tcs, cxx__nothr);96ATF_ADD_TEST_CASE(tcs, cxx__thread_local_main);97ATF_ADD_TEST_CASE(tcs, cxx__thread_inf_dtors);98}99100101