xref: /netbsd-src/tests/rump/rumpkern/t_kern.c (revision 18c37829c4ccfd6e316f7a3ac4fe906fca62ffcb)
1*18c37829Smartin /*	$NetBSD: t_kern.c,v 1.6 2020/08/28 19:29:58 martin Exp $	*/
2051cd856Spooka 
3051cd856Spooka /*-
4051cd856Spooka  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5051cd856Spooka  * All rights reserved.
6051cd856Spooka  *
7051cd856Spooka  * Redistribution and use in source and binary forms, with or without
8051cd856Spooka  * modification, are permitted provided that the following conditions
9051cd856Spooka  * are met:
10051cd856Spooka  * 1. Redistributions of source code must retain the above copyright
11051cd856Spooka  *    notice, this list of conditions and the following disclaimer.
12051cd856Spooka  * 2. Redistributions in binary form must reproduce the above copyright
13051cd856Spooka  *    notice, this list of conditions and the following disclaimer in the
14051cd856Spooka  *    documentation and/or other materials provided with the distribution.
15051cd856Spooka  *
16051cd856Spooka  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17051cd856Spooka  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18051cd856Spooka  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19051cd856Spooka  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20051cd856Spooka  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21051cd856Spooka  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22051cd856Spooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23051cd856Spooka  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24051cd856Spooka  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25051cd856Spooka  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26051cd856Spooka  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27051cd856Spooka  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28051cd856Spooka  */
29051cd856Spooka 
30051cd856Spooka #include <sys/types.h>
31051cd856Spooka #include <sys/signal.h>
32051cd856Spooka #include <sys/wait.h>
33051cd856Spooka 
34051cd856Spooka #include <rump/rump.h>
35051cd856Spooka 
36051cd856Spooka #include <atf-c.h>
37051cd856Spooka #include <stdio.h>
38051cd856Spooka #include <stdlib.h>
39051cd856Spooka #include <unistd.h>
4045d3394eSpgoyette #include <regex.h>
41051cd856Spooka 
42c54cb811Schristos #include "h_macros.h"
43051cd856Spooka #include "../kernspace/kernspace.h"
44051cd856Spooka 
45964d2049Spooka #define LOCKFUN(_name_, _descr_,_needld_, _expect_)			\
46051cd856Spooka 	ATF_TC(lockme_##_name_);					\
47051cd856Spooka 	ATF_TC_HEAD(lockme_##_name_, tc) {				\
48051cd856Spooka 		atf_tc_set_md_var(tc, "descr", _descr_);		\
49051cd856Spooka 	}								\
50051cd856Spooka 	ATF_TC_BODY(lockme_##_name_, tc) {				\
51964d2049Spooka 		locktest(tc, LOCKME_##_name_, _needld_, _expect_);	\
52051cd856Spooka 	}
53051cd856Spooka 
54051cd856Spooka static void
locktest(const atf_tc_t * tc,enum locktest lt,int needld,const char * expect)55964d2049Spooka locktest(const atf_tc_t *tc, enum locktest lt, int needld, const char *expect)
56051cd856Spooka {
57051cd856Spooka 	extern const int rump_lockdebug;
58964d2049Spooka 	int pipetti[2];
59051cd856Spooka 	int status;
6045d3394eSpgoyette 	ssize_t len;
6145d3394eSpgoyette 	regex_t preg;
62051cd856Spooka 
63051cd856Spooka 	if (needld && !rump_lockdebug)
64051cd856Spooka 		atf_tc_skip("test requires LOCKDEBUG kernel");
65964d2049Spooka 	RL(pipe(pipetti));
66051cd856Spooka 
67051cd856Spooka 	switch (fork()) {
68051cd856Spooka 	case 0:
69964d2049Spooka 		RL(dup2(pipetti[1], STDOUT_FILENO));
70964d2049Spooka 		RL(dup2(pipetti[1], STDOUT_FILENO));
71051cd856Spooka 		rump_init();
72051cd856Spooka 		rump_schedule();
73051cd856Spooka 		rumptest_lockme(lt);
74051cd856Spooka 		rump_unschedule();
75051cd856Spooka 		break;
76051cd856Spooka 	default:
77051cd856Spooka 		RL(wait(&status));
78051cd856Spooka 		ATF_REQUIRE(WIFSIGNALED(status) && WTERMSIG(status) == SIGABRT);
79964d2049Spooka 		if (rump_lockdebug) {
80964d2049Spooka 			char buf[8192];
81964d2049Spooka 
8245d3394eSpgoyette 			len = read(pipetti[0], buf, sizeof(buf) - 1);
8345d3394eSpgoyette 			ATF_REQUIRE(len > 0);
8445d3394eSpgoyette 			buf[len] = '\0';
8545d3394eSpgoyette 			/*
8645d3394eSpgoyette 			 * We use regex matching here, since the rump
8745d3394eSpgoyette 			 * kernel messages include routine names and line
8845d3394eSpgoyette 			 * numbers which may not remain constant.
8945d3394eSpgoyette 			 */
9045d3394eSpgoyette 			if ((status = regcomp(&preg, expect, REG_BASIC)) != 0) {
9145d3394eSpgoyette 				regerror(status, &preg, buf, sizeof(buf));
9245d3394eSpgoyette 				printf("regcomp error: %s\n", buf);
9345d3394eSpgoyette 				atf_tc_fail("regcomp failed");
9445d3394eSpgoyette 			}
9545d3394eSpgoyette 			if ((status = regexec(&preg, buf, 0, NULL, 0)) != 0) {
9645d3394eSpgoyette 				printf("expected: \"%s\"\n", expect);
9745d3394eSpgoyette 				printf("received: \"%s\"\n", buf);
9845d3394eSpgoyette 				regerror(status, &preg, buf, sizeof(buf));
9945d3394eSpgoyette 				printf("regexec error: %s\n", buf);
100964d2049Spooka 				atf_tc_fail("unexpected output");
101964d2049Spooka 			}
10245d3394eSpgoyette 			regfree(&preg);
10345d3394eSpgoyette 		}
104051cd856Spooka 		break;
105051cd856Spooka 	case -1:
106051cd856Spooka 		atf_tc_fail("fork");
107051cd856Spooka 	}
108051cd856Spooka }
109051cd856Spooka 
110964d2049Spooka LOCKFUN(DESTROYHELD, "destroy lock while held", 0,
11145d3394eSpgoyette     "mutex error: mutex_destroy,.*: is locked or in use");
112964d2049Spooka LOCKFUN(DOUBLEFREE, "free lock twice", 0,
11345d3394eSpgoyette     "panic: mutex_destroy,.*: uninitialized lock");
114964d2049Spooka LOCKFUN(DOUBLEINIT, "init lock twice", 1,
115*18c37829Smartin     "mutex error: .*mutex_init,.*: already initialized");
116964d2049Spooka LOCKFUN(MEMFREE, "free memory active lock is in", 1,
11745d3394eSpgoyette     "mutex error: kmem_intr_free,.*: allocation contains active lock");
118964d2049Spooka LOCKFUN(MTX, "locking-against-self mutex", 0,
11945d3394eSpgoyette     "mutex error: mutex_enter,.*: locking against myself");
120964d2049Spooka LOCKFUN(RWDOUBLEX, "locking-against-self exclusive rwlock", 0,
12145d3394eSpgoyette     "rwlock error: rw_enter,.*: locking against myself");
122964d2049Spooka LOCKFUN(RWRX, "rw: first shared, then exclusive", 1,
12345d3394eSpgoyette     "rwlock error: rw_enter,.*: locking against myself");
124964d2049Spooka LOCKFUN(RWXR, "rw: first execusive, then shared", 0,
12545d3394eSpgoyette     "rwlock error: rw_enter,.*: locking against myself");
126051cd856Spooka 
ATF_TP_ADD_TCS(tp)127051cd856Spooka ATF_TP_ADD_TCS(tp)
128051cd856Spooka {
129051cd856Spooka 
130051cd856Spooka 	ATF_TP_ADD_TC(tp, lockme_MTX);
131051cd856Spooka 	ATF_TP_ADD_TC(tp, lockme_RWDOUBLEX);
132051cd856Spooka 	ATF_TP_ADD_TC(tp, lockme_RWRX);
133051cd856Spooka 	ATF_TP_ADD_TC(tp, lockme_RWXR);
134051cd856Spooka 	ATF_TP_ADD_TC(tp, lockme_DOUBLEINIT);
135051cd856Spooka 	ATF_TP_ADD_TC(tp, lockme_DOUBLEFREE);
136051cd856Spooka 	ATF_TP_ADD_TC(tp, lockme_DESTROYHELD);
137051cd856Spooka 	ATF_TP_ADD_TC(tp, lockme_MEMFREE);
138051cd856Spooka 
139051cd856Spooka 	return atf_no_error();
140051cd856Spooka }
141