11.6Smartin/*	$NetBSD: t_kern.c,v 1.6 2020/08/28 19:29:58 martin Exp $	*/
21.1Spooka
31.1Spooka/*-
41.1Spooka * Copyright (c) 2011 The NetBSD Foundation, Inc.
51.1Spooka * All rights reserved.
61.1Spooka *
71.1Spooka * Redistribution and use in source and binary forms, with or without
81.1Spooka * modification, are permitted provided that the following conditions
91.1Spooka * are met:
101.1Spooka * 1. Redistributions of source code must retain the above copyright
111.1Spooka *    notice, this list of conditions and the following disclaimer.
121.1Spooka * 2. Redistributions in binary form must reproduce the above copyright
131.1Spooka *    notice, this list of conditions and the following disclaimer in the
141.1Spooka *    documentation and/or other materials provided with the distribution.
151.1Spooka *
161.1Spooka * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
171.1Spooka * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
181.1Spooka * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
191.1Spooka * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
201.1Spooka * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
211.1Spooka * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221.1Spooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
231.1Spooka * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
241.1Spooka * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
251.1Spooka * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
261.1Spooka * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
271.1Spooka * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
281.1Spooka */
291.1Spooka
301.1Spooka#include <sys/types.h>
311.1Spooka#include <sys/signal.h>
321.1Spooka#include <sys/wait.h>
331.1Spooka
341.1Spooka#include <rump/rump.h>
351.1Spooka
361.1Spooka#include <atf-c.h>
371.1Spooka#include <stdio.h>
381.1Spooka#include <stdlib.h>
391.1Spooka#include <unistd.h>
401.5Spgoyette#include <regex.h>
411.1Spooka
421.4Schristos#include "h_macros.h"
431.1Spooka#include "../kernspace/kernspace.h"
441.1Spooka
451.2Spooka#define LOCKFUN(_name_, _descr_,_needld_, _expect_)			\
461.1Spooka	ATF_TC(lockme_##_name_);					\
471.1Spooka	ATF_TC_HEAD(lockme_##_name_, tc) {				\
481.1Spooka		atf_tc_set_md_var(tc, "descr", _descr_);		\
491.1Spooka	}								\
501.1Spooka	ATF_TC_BODY(lockme_##_name_, tc) {				\
511.2Spooka		locktest(tc, LOCKME_##_name_, _needld_, _expect_);	\
521.1Spooka	}
531.1Spooka
541.1Spookastatic void
551.2Spookalocktest(const atf_tc_t *tc, enum locktest lt, int needld, const char *expect)
561.1Spooka{
571.1Spooka	extern const int rump_lockdebug;
581.2Spooka	int pipetti[2];
591.1Spooka	int status;
601.5Spgoyette	ssize_t len;
611.5Spgoyette	regex_t preg;
621.1Spooka
631.1Spooka	if (needld && !rump_lockdebug)
641.1Spooka		atf_tc_skip("test requires LOCKDEBUG kernel");
651.2Spooka	RL(pipe(pipetti));
661.1Spooka
671.1Spooka	switch (fork()) {
681.1Spooka	case 0:
691.2Spooka		RL(dup2(pipetti[1], STDOUT_FILENO));
701.2Spooka		RL(dup2(pipetti[1], STDOUT_FILENO));
711.1Spooka		rump_init();
721.1Spooka		rump_schedule();
731.1Spooka		rumptest_lockme(lt);
741.1Spooka		rump_unschedule();
751.1Spooka		break;
761.1Spooka	default:
771.1Spooka		RL(wait(&status));
781.1Spooka		ATF_REQUIRE(WIFSIGNALED(status) && WTERMSIG(status) == SIGABRT);
791.2Spooka		if (rump_lockdebug) {
801.2Spooka			char buf[8192];
811.2Spooka
821.5Spgoyette			len = read(pipetti[0], buf, sizeof(buf) - 1);
831.5Spgoyette			ATF_REQUIRE(len > 0);
841.5Spgoyette			buf[len] = '\0';
851.5Spgoyette			/*
861.5Spgoyette			 * We use regex matching here, since the rump
871.5Spgoyette			 * kernel messages include routine names and line
881.5Spgoyette			 * numbers which may not remain constant.
891.5Spgoyette			 */
901.5Spgoyette			if ((status = regcomp(&preg, expect, REG_BASIC)) != 0) {
911.5Spgoyette				regerror(status, &preg, buf, sizeof(buf));
921.5Spgoyette				printf("regcomp error: %s\n", buf);
931.5Spgoyette				atf_tc_fail("regcomp failed");
941.5Spgoyette			}
951.5Spgoyette			if ((status = regexec(&preg, buf, 0, NULL, 0)) != 0) {
961.5Spgoyette				printf("expected: \"%s\"\n", expect);
971.5Spgoyette				printf("received: \"%s\"\n", buf);
981.5Spgoyette				regerror(status, &preg, buf, sizeof(buf));
991.5Spgoyette				printf("regexec error: %s\n", buf);
1001.2Spooka				atf_tc_fail("unexpected output");
1011.5Spgoyette			}
1021.5Spgoyette			regfree(&preg);
1031.2Spooka		}
1041.1Spooka		break;
1051.1Spooka	case -1:
1061.1Spooka		atf_tc_fail("fork");
1071.1Spooka	}
1081.1Spooka}
1091.1Spooka
1101.2SpookaLOCKFUN(DESTROYHELD, "destroy lock while held", 0,
1111.5Spgoyette    "mutex error: mutex_destroy,.*: is locked or in use");
1121.2SpookaLOCKFUN(DOUBLEFREE, "free lock twice", 0,
1131.5Spgoyette    "panic: mutex_destroy,.*: uninitialized lock");
1141.2SpookaLOCKFUN(DOUBLEINIT, "init lock twice", 1,
1151.6Smartin    "mutex error: .*mutex_init,.*: already initialized");
1161.2SpookaLOCKFUN(MEMFREE, "free memory active lock is in", 1,
1171.5Spgoyette    "mutex error: kmem_intr_free,.*: allocation contains active lock");
1181.2SpookaLOCKFUN(MTX, "locking-against-self mutex", 0,
1191.5Spgoyette    "mutex error: mutex_enter,.*: locking against myself");
1201.2SpookaLOCKFUN(RWDOUBLEX, "locking-against-self exclusive rwlock", 0,
1211.5Spgoyette    "rwlock error: rw_enter,.*: locking against myself");
1221.2SpookaLOCKFUN(RWRX, "rw: first shared, then exclusive", 1,
1231.5Spgoyette    "rwlock error: rw_enter,.*: locking against myself");
1241.2SpookaLOCKFUN(RWXR, "rw: first execusive, then shared", 0,
1251.5Spgoyette    "rwlock error: rw_enter,.*: locking against myself");
1261.1Spooka
1271.1SpookaATF_TP_ADD_TCS(tp)
1281.1Spooka{
1291.1Spooka
1301.1Spooka	ATF_TP_ADD_TC(tp, lockme_MTX);
1311.1Spooka	ATF_TP_ADD_TC(tp, lockme_RWDOUBLEX);
1321.1Spooka	ATF_TP_ADD_TC(tp, lockme_RWRX);
1331.1Spooka	ATF_TP_ADD_TC(tp, lockme_RWXR);
1341.1Spooka	ATF_TP_ADD_TC(tp, lockme_DOUBLEINIT);
1351.1Spooka	ATF_TP_ADD_TC(tp, lockme_DOUBLEFREE);
1361.1Spooka	ATF_TP_ADD_TC(tp, lockme_DESTROYHELD);
1371.1Spooka	ATF_TP_ADD_TC(tp, lockme_MEMFREE);
1381.1Spooka
1391.1Spooka	return atf_no_error();
1401.1Spooka}
141