xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/mmap/mmap11.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (C) 2010  Red Hat, Inc.
3*49cdfc7eSAndroid Build Coastguard Worker  *
4*49cdfc7eSAndroid Build Coastguard Worker  * This program is free software; you can redistribute it and/or modify
5*49cdfc7eSAndroid Build Coastguard Worker  * it under the terms of version 2 of the GNU General Public License as
6*49cdfc7eSAndroid Build Coastguard Worker  * published by the Free Software Foundation.
7*49cdfc7eSAndroid Build Coastguard Worker  *
8*49cdfc7eSAndroid Build Coastguard Worker  * This program is distributed in the hope that it would be useful, but
9*49cdfc7eSAndroid Build Coastguard Worker  * WITHOUT ANY WARRANTY; without even the implied warranty of
10*49cdfc7eSAndroid Build Coastguard Worker  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * Further, this software is distributed without any warranty that it is
13*49cdfc7eSAndroid Build Coastguard Worker  * free of the rightful claim of any third person regarding infringement
14*49cdfc7eSAndroid Build Coastguard Worker  * or the like.  Any license provided herein, whether implied or
15*49cdfc7eSAndroid Build Coastguard Worker  * otherwise, applies only to this software file.  Patent licenses, if
16*49cdfc7eSAndroid Build Coastguard Worker  * any, provided herein do not apply to combinations of this program
17*49cdfc7eSAndroid Build Coastguard Worker  * with other software, or any other product whatsoever.
18*49cdfc7eSAndroid Build Coastguard Worker  *
19*49cdfc7eSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License
20*49cdfc7eSAndroid Build Coastguard Worker  * along with this program; if not, write the Free Software Foundation,
21*49cdfc7eSAndroid Build Coastguard Worker  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22*49cdfc7eSAndroid Build Coastguard Worker  */
23*49cdfc7eSAndroid Build Coastguard Worker /*
24*49cdfc7eSAndroid Build Coastguard Worker  * munmap() don't check sysctl_max_mapcount
25*49cdfc7eSAndroid Build Coastguard Worker  *
26*49cdfc7eSAndroid Build Coastguard Worker  * From http://lkml.org/lkml/2009/10/2/85:
27*49cdfc7eSAndroid Build Coastguard Worker  *
28*49cdfc7eSAndroid Build Coastguard Worker  * On ia64, the following test program exit abnormally, because glibc
29*49cdfc7eSAndroid Build Coastguard Worker  * thread library called abort().
30*49cdfc7eSAndroid Build Coastguard Worker  *
31*49cdfc7eSAndroid Build Coastguard Worker  *  ========================================================
32*49cdfc7eSAndroid Build Coastguard Worker  *  (gdb) bt
33*49cdfc7eSAndroid Build Coastguard Worker  *  #0  0xa000000000010620 in __kernel_syscall_via_break ()
34*49cdfc7eSAndroid Build Coastguard Worker  *  #1  0x20000000003208e0 in raise () from /lib/libc.so.6.1
35*49cdfc7eSAndroid Build Coastguard Worker  *  #2  0x2000000000324090 in abort () from /lib/libc.so.6.1
36*49cdfc7eSAndroid Build Coastguard Worker  *  #3  0x200000000027c3e0 in __deallocate_stack () from
37*49cdfc7eSAndroid Build Coastguard Worker  *      /lib/libpthread.so.0
38*49cdfc7eSAndroid Build Coastguard Worker  *  #4  0x200000000027f7c0 in start_thread () from /lib/libpthread.so.0
39*49cdfc7eSAndroid Build Coastguard Worker  *  #5  0x200000000047ef60 in __clone2 () from /lib/libc.so.6.1
40*49cdfc7eSAndroid Build Coastguard Worker  *  ========================================================
41*49cdfc7eSAndroid Build Coastguard Worker  * The fact is, glibc call munmap() when thread exitng time for freeing
42*49cdfc7eSAndroid Build Coastguard Worker  * stack, and it assume munlock() never fail. However, munmap() often
43*49cdfc7eSAndroid Build Coastguard Worker  * make vma splitting and it with many mapcount make -ENOMEM.
44*49cdfc7eSAndroid Build Coastguard Worker  *
45*49cdfc7eSAndroid Build Coastguard Worker  * Oh well, stack unfreeing is not reasonable option. Also munlock() via
46*49cdfc7eSAndroid Build Coastguard Worker  * free() shouldn't failed.
47*49cdfc7eSAndroid Build Coastguard Worker  *
48*49cdfc7eSAndroid Build Coastguard Worker  * Thus, munmap() shoudn't check max-mapcount.
49*49cdfc7eSAndroid Build Coastguard Worker  */
50*49cdfc7eSAndroid Build Coastguard Worker #include<stdio.h>
51*49cdfc7eSAndroid Build Coastguard Worker #include<stdlib.h>
52*49cdfc7eSAndroid Build Coastguard Worker #include<string.h>
53*49cdfc7eSAndroid Build Coastguard Worker #include<pthread.h>
54*49cdfc7eSAndroid Build Coastguard Worker #include<errno.h>
55*49cdfc7eSAndroid Build Coastguard Worker #include<unistd.h>
56*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
57*49cdfc7eSAndroid Build Coastguard Worker 
58*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "mmap11";
59*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;
60*49cdfc7eSAndroid Build Coastguard Worker 
61*49cdfc7eSAndroid Build Coastguard Worker #define MAL_SIZE (100*1024)
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker static void *wait_thread(void *args);
64*49cdfc7eSAndroid Build Coastguard Worker static void *wait_thread2(void *args);
65*49cdfc7eSAndroid Build Coastguard Worker static void setup(void);
66*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void);
67*49cdfc7eSAndroid Build Coastguard Worker static void check(void);
68*49cdfc7eSAndroid Build Coastguard Worker 
main(int argc,char * argv[])69*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char *argv[])
70*49cdfc7eSAndroid Build Coastguard Worker {
71*49cdfc7eSAndroid Build Coastguard Worker 
72*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(argc, argv, NULL, NULL);
73*49cdfc7eSAndroid Build Coastguard Worker 	setup();
74*49cdfc7eSAndroid Build Coastguard Worker 	check();
75*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
76*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
77*49cdfc7eSAndroid Build Coastguard Worker }
78*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)79*49cdfc7eSAndroid Build Coastguard Worker void setup(void)
80*49cdfc7eSAndroid Build Coastguard Worker {
81*49cdfc7eSAndroid Build Coastguard Worker 	tst_require_root();
82*49cdfc7eSAndroid Build Coastguard Worker 
83*49cdfc7eSAndroid Build Coastguard Worker 	tst_sig(FORK, DEF_HANDLER, cleanup);
84*49cdfc7eSAndroid Build Coastguard Worker 	TEST_PAUSE;
85*49cdfc7eSAndroid Build Coastguard Worker }
86*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)87*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void)
88*49cdfc7eSAndroid Build Coastguard Worker {
89*49cdfc7eSAndroid Build Coastguard Worker }
90*49cdfc7eSAndroid Build Coastguard Worker 
check(void)91*49cdfc7eSAndroid Build Coastguard Worker void check(void)
92*49cdfc7eSAndroid Build Coastguard Worker {
93*49cdfc7eSAndroid Build Coastguard Worker 	int lc;
94*49cdfc7eSAndroid Build Coastguard Worker 	pthread_t *thread, th;
95*49cdfc7eSAndroid Build Coastguard Worker 	int ret, count = 0;
96*49cdfc7eSAndroid Build Coastguard Worker 	pthread_attr_t attr;
97*49cdfc7eSAndroid Build Coastguard Worker 
98*49cdfc7eSAndroid Build Coastguard Worker 	ret = pthread_attr_init(&attr);
99*49cdfc7eSAndroid Build Coastguard Worker 	if (ret)
100*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK | TERRNO, cleanup, "pthread_attr_init");
101*49cdfc7eSAndroid Build Coastguard Worker 	ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
102*49cdfc7eSAndroid Build Coastguard Worker 	if (ret)
103*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK | TERRNO, cleanup,
104*49cdfc7eSAndroid Build Coastguard Worker 			 "pthread_attr_setdetachstate");
105*49cdfc7eSAndroid Build Coastguard Worker 	thread = malloc(STD_LOOP_COUNT * sizeof(pthread_t));
106*49cdfc7eSAndroid Build Coastguard Worker 	if (thread == NULL)
107*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK | TERRNO, cleanup, "malloc");
108*49cdfc7eSAndroid Build Coastguard Worker 
109*49cdfc7eSAndroid Build Coastguard Worker 	for (lc = 0; TEST_LOOPING(lc); lc++) {
110*49cdfc7eSAndroid Build Coastguard Worker 		tst_count = 0;
111*49cdfc7eSAndroid Build Coastguard Worker 		ret = pthread_create(&th, &attr, wait_thread, NULL);
112*49cdfc7eSAndroid Build Coastguard Worker 		if (ret) {
113*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TINFO, "[%d] ", count);
114*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK | TERRNO, cleanup, "pthread_create");
115*49cdfc7eSAndroid Build Coastguard Worker 		}
116*49cdfc7eSAndroid Build Coastguard Worker 		count++;
117*49cdfc7eSAndroid Build Coastguard Worker 		ret = pthread_create(&thread[lc], &attr, wait_thread2, NULL);
118*49cdfc7eSAndroid Build Coastguard Worker 		if (ret) {
119*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TINFO, "[%d] ", count);
120*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK | TERRNO, cleanup, "pthread_create");
121*49cdfc7eSAndroid Build Coastguard Worker 		}
122*49cdfc7eSAndroid Build Coastguard Worker 		count++;
123*49cdfc7eSAndroid Build Coastguard Worker 	}
124*49cdfc7eSAndroid Build Coastguard Worker 	tst_resm(TPASS, "test completed.");
125*49cdfc7eSAndroid Build Coastguard Worker 	free(thread);
126*49cdfc7eSAndroid Build Coastguard Worker }
127*49cdfc7eSAndroid Build Coastguard Worker 
wait_thread(void * args)128*49cdfc7eSAndroid Build Coastguard Worker void *wait_thread(void *args)
129*49cdfc7eSAndroid Build Coastguard Worker {
130*49cdfc7eSAndroid Build Coastguard Worker 	void *addr;
131*49cdfc7eSAndroid Build Coastguard Worker 
132*49cdfc7eSAndroid Build Coastguard Worker 	addr = malloc(MAL_SIZE);
133*49cdfc7eSAndroid Build Coastguard Worker 	if (addr)
134*49cdfc7eSAndroid Build Coastguard Worker 		memset(addr, 1, MAL_SIZE);
135*49cdfc7eSAndroid Build Coastguard Worker 	sleep(1);
136*49cdfc7eSAndroid Build Coastguard Worker 	return NULL;
137*49cdfc7eSAndroid Build Coastguard Worker }
138*49cdfc7eSAndroid Build Coastguard Worker 
wait_thread2(void * args)139*49cdfc7eSAndroid Build Coastguard Worker void *wait_thread2(void *args)
140*49cdfc7eSAndroid Build Coastguard Worker {
141*49cdfc7eSAndroid Build Coastguard Worker 	return NULL;
142*49cdfc7eSAndroid Build Coastguard Worker }
143