Home | History | Annotate | Line # | Download | only in cc
      1 #	$NetBSD: t_pthread_abuse.sh,v 1.1 2025/10/06 13:11:56 riastradh Exp $
      2 #
      3 # Copyright (c) 2025 The NetBSD Foundation, Inc.
      4 # All rights reserved.
      5 #
      6 # Redistribution and use in source and binary forms, with or without
      7 # modification, are permitted provided that the following conditions
      8 # are met:
      9 # 1. Redistributions of source code must retain the above copyright
     10 #    notice, this list of conditions and the following disclaimer.
     11 # 2. Redistributions in binary form must reproduce the above copyright
     12 #    notice, this list of conditions and the following disclaimer in the
     13 #    documentation and/or other materials provided with the distribution.
     14 #
     15 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     16 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     17 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     18 # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     19 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     20 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     21 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     22 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     23 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     24 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25 # POSSIBILITY OF SUCH DAMAGE.
     26 #
     27 
     28 pthread_abuse_head()
     29 {
     30 
     31 	atf_set "descr" \
     32 	    "Test that pthread_create calls without -lpthread fail to link"
     33 	atf_set "require.progs" "cc"
     34 }
     35 pthread_abuse_body()
     36 {
     37 
     38 	cat >test.c <<'EOF'
     39 #include <err.h>
     40 #include <pthread.h>
     41 #include <stdlib.h>
     42 
     43 static void *
     44 start(void *cookie)
     45 {
     46 	return cookie;
     47 }
     48 
     49 int
     50 main(void)
     51 {
     52 	int cookie = 123;
     53 	pthread_t t;
     54 	void *result;
     55 	int error;
     56 
     57 	error = pthread_create(&t, NULL, &start, &cookie);
     58 	if (error)
     59 		errc(EXIT_FAILURE, error, "pthread_create");
     60 	error = pthread_join(t, &result);
     61 	if (error)
     62 		errc(EXIT_FAILURE, error, "pthread_join");
     63 	return (result == &cookie ? 0 : EXIT_FAILURE);
     64 }
     65 EOF
     66 	atf_check -s not-exit:0 \
     67 	    -e match:'undefined reference to.*pthread_create' \
     68 	    cc -o test test.c
     69 	atf_check cc -o test test.c -lpthread
     70 	atf_check ./test
     71 	atf_check cc -o test test.c -pthread
     72 	atf_check ./test
     73 }
     74 
     75 atf_init_test_cases()
     76 {
     77 
     78 	atf_add_test_case pthread_abuse
     79 }
     80