xref: /freebsd/contrib/netbsd-tests/net/bpf/t_div-by-zero.c (revision 62cfcf62f627e5093fb37026a6d8c98e4d2ef04c)
1 #include <sys/types.h>
2 #include <sys/ioctl.h>
3 
4 #include <net/bpf.h>
5 
6 #include <atf-c.h>
7 #include <fcntl.h>
8 
9 #include <rump/rump.h>
10 #include <rump/rump_syscalls.h>
11 
12 ATF_TC(div_by_zero);
13 ATF_TC_HEAD(div_by_zero, tc)
14 {
15 
16 	atf_tc_set_md_var(tc, "descr", "Check that BPF rejects a filter "
17 	    "which divides by 0");
18 }
19 
20 ATF_TC_BODY(div_by_zero, tc)
21 {
22 	struct bpf_program bp;
23 	int fd;
24 
25 	/*
26 	 * Source code for following program:
27 	 * link[0:4]/0 = 2
28 	 */
29 	struct bpf_insn bins[] = {
30 	    { 0x20, 0, 0, 0x00000000 },
31 	    { 0x34, 0, 0, 0x00000000 },
32 	    { 0x15, 0, 1, 0x00000002 },
33 	    { 0x6, 0, 0, 0x00000060 },
34 	    { 0x6, 0, 0, 0x00000000 },
35 	};
36 
37 	bp.bf_len = __arraycount(bins);
38 	bp.bf_insns = bins;
39 
40 	rump_init();
41 	fd = rump_sys_open("/dev/bpf", O_RDWR);
42 	ATF_CHECK(fd != -1);
43 	ATF_REQUIRE_EQ_MSG(rump_sys_ioctl(fd, BIOCSETF, &bp), -1,
44 	    "bpf accepted program with division by zero");
45 }
46 
47 ATF_TP_ADD_TCS(tp)
48 {
49 
50 	ATF_TP_ADD_TC(tp, div_by_zero);
51 	return atf_no_error();
52 }
53