t_kern.c revision 314817
1/*	$NetBSD: t_kern.c,v 1.4 2017/01/13 21:30:43 christos Exp $	*/
2
3/*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <sys/types.h>
31#include <sys/signal.h>
32#include <sys/wait.h>
33
34#include <rump/rump.h>
35
36#include <atf-c.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <unistd.h>
40
41#include "h_macros.h"
42#include "../kernspace/kernspace.h"
43
44#define LOCKFUN(_name_, _descr_,_needld_, _expect_)			\
45	ATF_TC(lockme_##_name_);					\
46	ATF_TC_HEAD(lockme_##_name_, tc) {				\
47		atf_tc_set_md_var(tc, "descr", _descr_);		\
48	}								\
49	ATF_TC_BODY(lockme_##_name_, tc) {				\
50		locktest(tc, LOCKME_##_name_, _needld_, _expect_);	\
51	}
52
53static void
54locktest(const atf_tc_t *tc, enum locktest lt, int needld, const char *expect)
55{
56	extern const int rump_lockdebug;
57	int pipetti[2];
58	int status;
59
60	if (needld && !rump_lockdebug)
61		atf_tc_skip("test requires LOCKDEBUG kernel");
62	RL(pipe(pipetti));
63
64	switch (fork()) {
65	case 0:
66		RL(dup2(pipetti[1], STDOUT_FILENO));
67		RL(dup2(pipetti[1], STDOUT_FILENO));
68		rump_init();
69		rump_schedule();
70		rumptest_lockme(lt);
71		rump_unschedule();
72		break;
73	default:
74		RL(wait(&status));
75		ATF_REQUIRE(WIFSIGNALED(status) && WTERMSIG(status) == SIGABRT);
76		if (rump_lockdebug) {
77			char buf[8192];
78
79			ATF_REQUIRE(read(pipetti[0], buf, sizeof(buf)) > 0);
80			if (strncmp(buf, expect, strlen(expect)) != 0)
81				atf_tc_fail("unexpected output");
82		}
83		break;
84	case -1:
85		atf_tc_fail("fork");
86	}
87}
88
89LOCKFUN(DESTROYHELD, "destroy lock while held", 0,
90    "mutex error: lockdebug_free: is locked or in use");
91LOCKFUN(DOUBLEFREE, "free lock twice", 0,
92    "panic: lockdebug_lookup: uninitialized lock");
93LOCKFUN(DOUBLEINIT, "init lock twice", 1,
94    "mutex error: lockdebug_alloc: already initialized");
95LOCKFUN(MEMFREE, "free memory active lock is in", 1,
96    "mutex error: kmem_intr_free: allocation contains active lock");
97LOCKFUN(MTX, "locking-against-self mutex", 0,
98    "mutex error: lockdebug_wantlock: locking against myself");
99LOCKFUN(RWDOUBLEX, "locking-against-self exclusive rwlock", 0,
100    "rwlock error: lockdebug_wantlock: locking against myself");
101LOCKFUN(RWRX, "rw: first shared, then exclusive", 1,
102    "rwlock error: lockdebug_wantlock: locking against myself");
103LOCKFUN(RWXR, "rw: first execusive, then shared", 0,
104    "rwlock error: lockdebug_wantlock: locking against myself");
105
106ATF_TP_ADD_TCS(tp)
107{
108
109	ATF_TP_ADD_TC(tp, lockme_MTX);
110	ATF_TP_ADD_TC(tp, lockme_RWDOUBLEX);
111	ATF_TP_ADD_TC(tp, lockme_RWRX);
112	ATF_TP_ADD_TC(tp, lockme_RWXR);
113	ATF_TP_ADD_TC(tp, lockme_DOUBLEINIT);
114	ATF_TP_ADD_TC(tp, lockme_DOUBLEFREE);
115	ATF_TP_ADD_TC(tp, lockme_DESTROYHELD);
116	ATF_TP_ADD_TC(tp, lockme_MEMFREE);
117
118	return atf_no_error();
119}
120