xref: /openbsd-src/regress/lib/libc/malloc/malloc_threaderr/malloc_threaderr.c (revision 92c6079fadf96e1babcde0781194afc3f678ff01)
1 /*
2  * Copyright (c) 2018 Otto Moerbeek <otto@drijf.net>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include <err.h>
18 #include <pthread.h>
19 #include <signal.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <sys/resource.h>
24 
25 pthread_cond_t cond;
26 pthread_mutex_t mutex;
27 
28 void *p;
29 
m(void * arg)30 void *m(void *arg)
31 {
32 	p = malloc(100000);
33 	if (p == NULL)
34 		err(1, NULL);
35 	return NULL;
36 }
37 
f(void * arg)38 void *f(void *arg)
39 {
40 	free(p);
41 	free(p);
42 	return NULL;
43 }
44 
45 void
catch(int x)46 catch(int x)
47 {
48 	_exit(0);
49 }
50 
51 int
main(void)52 main(void)
53 {
54 	const struct rlimit lim = {0, 0};
55 	pthread_t t1, t2;
56 
57 	/* prevent coredumps */
58 	setrlimit(RLIMIT_CORE, &lim);
59 	printf("This test is supposed to print a malloc error\n");
60 
61 	signal(SIGABRT, catch);
62 
63 	if (pthread_create(&t1, NULL, m, NULL))
64 		err(1, "pthread_create");
65 	pthread_join(t1, NULL);
66 
67 	if (pthread_create(&t2, NULL, f, NULL))
68 		err(1, "pthread_create");
69 	pthread_join(t2, NULL);
70 
71 	return 1;
72 }
73