aboutsummaryrefslogtreecommitdiff
path: root/tests/sys/sound/mmap.c
blob: 6ebfd2f7dec549fa27a516f9f1366f353dacbda3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*-
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2026 The FreeBSD Foundation
 */

#include <sys/param.h>
#include <sys/mman.h>
#include <sys/soundcard.h>

#include <atf-c.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>

#define	FMT_ERR(s)	s ": %s", strerror(errno)

ATF_TC(mmap_offset_overflow);
ATF_TC_HEAD(mmap_offset_overflow, tc)
{
	atf_tc_set_md_var(tc, "descr", "mmap offset overflow test");
	atf_tc_set_md_var(tc, "require.kmods", "snd_dummy");
}

ATF_TC_BODY(mmap_offset_overflow, tc)
{
	uint8_t *buf;
	off_t off;
	size_t len;
	int fd;

	fd = open("/dev/dsp.dummy", O_RDWR);
	ATF_REQUIRE_MSG(fd >= 0, FMT_ERR("open"));

	/* off + len will overflow and wrap back to 0. */
	off = 0xfffffffffffff000;
	len = 0x1000;

	buf = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, off);
	ATF_REQUIRE_MSG(buf == MAP_FAILED, FMT_ERR("mmap"));

	munmap(buf, len);

	close(fd);
}

/*
 * Verify that a MAP_SHARED mapping of a DSP device's software buffer remains
 * valid after the file descriptor is closed.
 */
ATF_TC(mmap_buffer_lifetime);
ATF_TC_HEAD(mmap_buffer_lifetime, tc)
{
	atf_tc_set_md_var(tc, "descr", "mmap data survives close()");
	atf_tc_set_md_var(tc, "require.kmods", "snd_dummy");
}
ATF_TC_BODY(mmap_buffer_lifetime, tc)
{
	audio_buf_info abi;
	uint8_t *buf;
	size_t len;
	int fd, arg;

	fd = open("/dev/dsp.dummy", O_RDWR);
	ATF_REQUIRE_MSG(fd >= 0, FMT_ERR("open"));

	arg = (2 << 16) | 14; /* 2*16KB */
	ATF_REQUIRE_MSG(ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &arg) == 0,
	    FMT_ERR("SNDCTL_DSP_SETFRAGMENT"));
	ATF_REQUIRE_MSG(ioctl(fd, SNDCTL_DSP_GETOSPACE, &abi) == 0,
	    FMT_ERR("SNDCTL_DSP_GETOSPACE"));

	len = abi.bytes;
	ATF_REQUIRE_MSG(len >= PAGE_SIZE, "buffer too small: %zu", len);

	buf = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	ATF_REQUIRE_MSG(buf != MAP_FAILED, FMT_ERR("mmap"));

	for (size_t i = 0; i < len; i++) {
		ATF_REQUIRE_MSG(buf[i] == 0,
		    "mmap data corrupted at offset %zu: want 0 got 0x%02x",
		    i, buf[i]);
	}

	memset(buf, 0xa5, len);
	for (size_t i = 0; i < len; i++) {
		ATF_REQUIRE_MSG(buf[i] == 0xa5,
		    "mmap data corrupted at offset %zu: want 0xa5 got 0x%02x",
		    i, buf[i]);
	}

	ATF_REQUIRE(close(fd) == 0);

	/* Closing the device causes the buffer to be reset. */
	for (size_t i = 0; i < len; i++) {
		ATF_REQUIRE_MSG(buf[i] == 0 || buf[i] == 0xa5,
		    "mmap data corrupted at offset %zu: got 0x%02x", i, buf[i]);
	}
	memset(buf, 0xa5, len);

	ATF_REQUIRE(munmap(buf, len) == 0);
}

ATF_TC(mmap_reject_prot_exec);
ATF_TC_HEAD(mmap_reject_prot_exec, tc)
{
	atf_tc_set_md_var(tc, "descr", "mmap PROT_EXEC rejection test");
	atf_tc_set_md_var(tc, "require.kmods", "snd_dummy");
}

ATF_TC_BODY(mmap_reject_prot_exec, tc)
{
	uint8_t *buf;
	size_t len;
	int fd;

	fd = open("/dev/dsp.dummy", O_RDWR);
	ATF_REQUIRE_MSG(fd >= 0, FMT_ERR("open"));

	len = 8;

	buf = mmap(NULL, len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_SHARED,
	    fd, 0);
	ATF_REQUIRE_MSG(buf == MAP_FAILED, FMT_ERR("mmap"));

	munmap(buf, len);

	close(fd);
}

ATF_TP_ADD_TCS(tp)
{
	ATF_TP_ADD_TC(tp, mmap_offset_overflow);
	ATF_TP_ADD_TC(tp, mmap_buffer_lifetime);
	ATF_TP_ADD_TC(tp, mmap_reject_prot_exec);

	return (atf_no_error());
}