xref: /freebsd/contrib/netbsd-tests/rump/rumpkern/t_kern.c (revision 1a36faad54665288ed4eb839d2a4699ae2ead45e)
1*63d1fd59SEnji Cooper /*	$NetBSD: t_kern.c,v 1.4 2017/01/13 21:30:43 christos Exp $	*/
257718be8SEnji Cooper 
357718be8SEnji Cooper /*-
457718be8SEnji Cooper  * Copyright (c) 2011 The NetBSD Foundation, Inc.
557718be8SEnji Cooper  * All rights reserved.
657718be8SEnji Cooper  *
757718be8SEnji Cooper  * Redistribution and use in source and binary forms, with or without
857718be8SEnji Cooper  * modification, are permitted provided that the following conditions
957718be8SEnji Cooper  * are met:
1057718be8SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
1157718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
1257718be8SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
1357718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
1457718be8SEnji Cooper  *    documentation and/or other materials provided with the distribution.
1557718be8SEnji Cooper  *
1657718be8SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
1757718be8SEnji Cooper  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
1857718be8SEnji Cooper  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1957718be8SEnji Cooper  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2057718be8SEnji Cooper  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
2157718be8SEnji Cooper  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2257718be8SEnji Cooper  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2357718be8SEnji Cooper  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2457718be8SEnji Cooper  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
2557718be8SEnji Cooper  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2657718be8SEnji Cooper  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
2757718be8SEnji Cooper  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2857718be8SEnji Cooper  */
2957718be8SEnji Cooper 
3057718be8SEnji Cooper #include <sys/types.h>
3157718be8SEnji Cooper #include <sys/signal.h>
3257718be8SEnji Cooper #include <sys/wait.h>
3357718be8SEnji Cooper 
3457718be8SEnji Cooper #include <rump/rump.h>
3557718be8SEnji Cooper 
3657718be8SEnji Cooper #include <atf-c.h>
3757718be8SEnji Cooper #include <stdio.h>
3857718be8SEnji Cooper #include <stdlib.h>
3957718be8SEnji Cooper #include <unistd.h>
4057718be8SEnji Cooper 
41*63d1fd59SEnji Cooper #include "h_macros.h"
4257718be8SEnji Cooper #include "../kernspace/kernspace.h"
4357718be8SEnji Cooper 
4457718be8SEnji Cooper #define LOCKFUN(_name_, _descr_,_needld_, _expect_)			\
4557718be8SEnji Cooper 	ATF_TC(lockme_##_name_);					\
4657718be8SEnji Cooper 	ATF_TC_HEAD(lockme_##_name_, tc) {				\
4757718be8SEnji Cooper 		atf_tc_set_md_var(tc, "descr", _descr_);		\
4857718be8SEnji Cooper 	}								\
4957718be8SEnji Cooper 	ATF_TC_BODY(lockme_##_name_, tc) {				\
5057718be8SEnji Cooper 		locktest(tc, LOCKME_##_name_, _needld_, _expect_);	\
5157718be8SEnji Cooper 	}
5257718be8SEnji Cooper 
5357718be8SEnji Cooper static void
locktest(const atf_tc_t * tc,enum locktest lt,int needld,const char * expect)5457718be8SEnji Cooper locktest(const atf_tc_t *tc, enum locktest lt, int needld, const char *expect)
5557718be8SEnji Cooper {
5657718be8SEnji Cooper 	extern const int rump_lockdebug;
5757718be8SEnji Cooper 	int pipetti[2];
5857718be8SEnji Cooper 	int status;
5957718be8SEnji Cooper 
6057718be8SEnji Cooper 	if (needld && !rump_lockdebug)
6157718be8SEnji Cooper 		atf_tc_skip("test requires LOCKDEBUG kernel");
6257718be8SEnji Cooper 	RL(pipe(pipetti));
6357718be8SEnji Cooper 
6457718be8SEnji Cooper 	switch (fork()) {
6557718be8SEnji Cooper 	case 0:
6657718be8SEnji Cooper 		RL(dup2(pipetti[1], STDOUT_FILENO));
6757718be8SEnji Cooper 		RL(dup2(pipetti[1], STDOUT_FILENO));
6857718be8SEnji Cooper 		rump_init();
6957718be8SEnji Cooper 		rump_schedule();
7057718be8SEnji Cooper 		rumptest_lockme(lt);
7157718be8SEnji Cooper 		rump_unschedule();
7257718be8SEnji Cooper 		break;
7357718be8SEnji Cooper 	default:
7457718be8SEnji Cooper 		RL(wait(&status));
7557718be8SEnji Cooper 		ATF_REQUIRE(WIFSIGNALED(status) && WTERMSIG(status) == SIGABRT);
7657718be8SEnji Cooper 		if (rump_lockdebug) {
7757718be8SEnji Cooper 			char buf[8192];
7857718be8SEnji Cooper 
7957718be8SEnji Cooper 			ATF_REQUIRE(read(pipetti[0], buf, sizeof(buf)) > 0);
8057718be8SEnji Cooper 			if (strncmp(buf, expect, strlen(expect)) != 0)
8157718be8SEnji Cooper 				atf_tc_fail("unexpected output");
8257718be8SEnji Cooper 		}
8357718be8SEnji Cooper 		break;
8457718be8SEnji Cooper 	case -1:
8557718be8SEnji Cooper 		atf_tc_fail("fork");
8657718be8SEnji Cooper 	}
8757718be8SEnji Cooper }
8857718be8SEnji Cooper 
8957718be8SEnji Cooper LOCKFUN(DESTROYHELD, "destroy lock while held", 0,
9057718be8SEnji Cooper     "mutex error: lockdebug_free: is locked or in use");
9157718be8SEnji Cooper LOCKFUN(DOUBLEFREE, "free lock twice", 0,
9257718be8SEnji Cooper     "panic: lockdebug_lookup: uninitialized lock");
9357718be8SEnji Cooper LOCKFUN(DOUBLEINIT, "init lock twice", 1,
9457718be8SEnji Cooper     "mutex error: lockdebug_alloc: already initialized");
9557718be8SEnji Cooper LOCKFUN(MEMFREE, "free memory active lock is in", 1,
9657718be8SEnji Cooper     "mutex error: kmem_intr_free: allocation contains active lock");
9757718be8SEnji Cooper LOCKFUN(MTX, "locking-against-self mutex", 0,
9857718be8SEnji Cooper     "mutex error: lockdebug_wantlock: locking against myself");
9957718be8SEnji Cooper LOCKFUN(RWDOUBLEX, "locking-against-self exclusive rwlock", 0,
10057718be8SEnji Cooper     "rwlock error: lockdebug_wantlock: locking against myself");
10157718be8SEnji Cooper LOCKFUN(RWRX, "rw: first shared, then exclusive", 1,
10257718be8SEnji Cooper     "rwlock error: lockdebug_wantlock: locking against myself");
10357718be8SEnji Cooper LOCKFUN(RWXR, "rw: first execusive, then shared", 0,
10457718be8SEnji Cooper     "rwlock error: lockdebug_wantlock: locking against myself");
10557718be8SEnji Cooper 
ATF_TP_ADD_TCS(tp)10657718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
10757718be8SEnji Cooper {
10857718be8SEnji Cooper 
10957718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, lockme_MTX);
11057718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, lockme_RWDOUBLEX);
11157718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, lockme_RWRX);
11257718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, lockme_RWXR);
11357718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, lockme_DOUBLEINIT);
11457718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, lockme_DOUBLEFREE);
11557718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, lockme_DESTROYHELD);
11657718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, lockme_MEMFREE);
11757718be8SEnji Cooper 
11857718be8SEnji Cooper 	return atf_no_error();
11957718be8SEnji Cooper }
120