xref: /openbsd-src/regress/lib/libc/malloc/malloc_threaderr/malloc_threaderr.c (revision 92c6079fadf96e1babcde0781194afc3f678ff01)
1162f7d2fSotto /*
2162f7d2fSotto  * Copyright (c) 2018 Otto Moerbeek <otto@drijf.net>
3162f7d2fSotto  *
4162f7d2fSotto  * Permission to use, copy, modify, and distribute this software for any
5162f7d2fSotto  * purpose with or without fee is hereby granted, provided that the above
6162f7d2fSotto  * copyright notice and this permission notice appear in all copies.
7162f7d2fSotto  *
8162f7d2fSotto  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9162f7d2fSotto  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10162f7d2fSotto  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11162f7d2fSotto  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12162f7d2fSotto  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13162f7d2fSotto  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14162f7d2fSotto  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15162f7d2fSotto  */
16162f7d2fSotto 
17162f7d2fSotto #include <err.h>
18*92c6079fSotto #include <pthread.h>
19*92c6079fSotto #include <signal.h>
20162f7d2fSotto #include <stdio.h>
21162f7d2fSotto #include <stdlib.h>
22*92c6079fSotto #include <unistd.h>
23*92c6079fSotto #include <sys/resource.h>
24162f7d2fSotto 
25162f7d2fSotto pthread_cond_t cond;
26162f7d2fSotto pthread_mutex_t mutex;
27162f7d2fSotto 
28162f7d2fSotto void *p;
29162f7d2fSotto 
m(void * arg)30162f7d2fSotto void *m(void *arg)
31162f7d2fSotto {
32162f7d2fSotto 	p = malloc(100000);
33162f7d2fSotto 	if (p == NULL)
34162f7d2fSotto 		err(1, NULL);
35162f7d2fSotto 	return NULL;
36162f7d2fSotto }
37162f7d2fSotto 
f(void * arg)38162f7d2fSotto void *f(void *arg)
39162f7d2fSotto {
40162f7d2fSotto 	free(p);
41162f7d2fSotto 	free(p);
42162f7d2fSotto 	return NULL;
43162f7d2fSotto }
44162f7d2fSotto 
45*92c6079fSotto void
catch(int x)46*92c6079fSotto catch(int x)
47*92c6079fSotto {
48*92c6079fSotto 	_exit(0);
49*92c6079fSotto }
50*92c6079fSotto 
51162f7d2fSotto int
main(void)52162f7d2fSotto main(void)
53162f7d2fSotto {
54*92c6079fSotto 	const struct rlimit lim = {0, 0};
55162f7d2fSotto 	pthread_t t1, t2;
56162f7d2fSotto 
57*92c6079fSotto 	/* prevent coredumps */
58*92c6079fSotto 	setrlimit(RLIMIT_CORE, &lim);
59*92c6079fSotto 	printf("This test is supposed to print a malloc error\n");
60*92c6079fSotto 
61*92c6079fSotto 	signal(SIGABRT, catch);
62162f7d2fSotto 
63162f7d2fSotto 	if (pthread_create(&t1, NULL, m, NULL))
64162f7d2fSotto 		err(1, "pthread_create");
65162f7d2fSotto 	pthread_join(t1, NULL);
66162f7d2fSotto 
67162f7d2fSotto 	if (pthread_create(&t2, NULL, f, NULL))
68162f7d2fSotto 		err(1, "pthread_create");
69162f7d2fSotto 	pthread_join(t2, NULL);
70162f7d2fSotto 
71*92c6079fSotto 	return 1;
72162f7d2fSotto }
73