Path: blob/main/lib/libc/tests/stdlib/dynthr_test.c
39530 views
/*1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (C) 2019 Andrew Gierth4*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*26* Though this file is initially distributed under the 2-clause BSD license,27* the author grants permission for its redistribution under alternative28* licenses as set forth at <https://rhodiumtoad.github.io/RELICENSE.txt>.29* This paragraph and the RELICENSE.txt file are not part of the license and30* may be omitted in redistributions.31*/3233#include <stdio.h>34#include <stdlib.h>35#include <stdarg.h>36#include <string.h>37#include <unistd.h>38#include <dlfcn.h>3940#include <atf-c.h>4142typedef void (modfunc_t)(int op);4344/*45* Minimal test case for PR 235158; mutual dependencies between jemalloc and46* libthr causing issues in thread creation. Specifically to this case, libthr47* uses calloc to initialize pthread mutexes, and jemalloc uses pthread mutexes.48*49* Deferred initialization provided by jemalloc proved to be fragile, causing50* issues like in the referenced PR where thread creation in a shared object51* loaded via dlopen(3) would stall unless the calling application also linked52* against pthread.53*/54ATF_TC(maintc);55ATF_TC_HEAD(maintc, tc)56{5758atf_tc_set_md_var(tc, "timeout", "3");59}6061ATF_TC_BODY(maintc, tc)62{63char *libpath;64modfunc_t *func;65void *mod_handle;66const char *srcdir;67dlfunc_t rawfunc;6869srcdir = atf_tc_get_config_var(tc, "srcdir");70if (asprintf(&libpath, "%s/dynthr_mod.so", srcdir) < 0)71atf_tc_fail("failed to construct path to libthr");72mod_handle = dlopen(libpath, RTLD_LOCAL);73free(libpath);74if (mod_handle == NULL)75atf_tc_fail("failed to open dynthr_mod.so: %s", dlerror());76rawfunc = dlfunc(mod_handle, "mod_main");77if (rawfunc == NULL)78atf_tc_fail("failed to resolve function mod_main");79func = (modfunc_t *)rawfunc;80func(1);81func(0);82}8384ATF_TP_ADD_TCS(tp)85{8687ATF_TP_ADD_TC(tp, maintc);88return (atf_no_error());89}909192