]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/kern/kern_copyin.c
Fix Coverity warnings about mkstemp in tests
[FreeBSD/FreeBSD.git] / tests / sys / kern / kern_copyin.c
1 /*-
2  * Copyright (c) 2015 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
6  * under sponsorship from the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <sys/stat.h>
35 #include <errno.h>
36 #include <limits.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <atf-c.h>
41 #include <vm/vm.h>
42 #include <vm/pmap.h>
43 #include <machine/vmparam.h>
44
45 static int scratch_file;
46
47 static int
48 copyin_checker(uintptr_t uaddr, size_t len)
49 {
50         ssize_t ret;
51
52         ret = write(scratch_file, (const void *)uaddr, len);
53         return (ret == -1 ? errno : 0);
54 }
55
56 #define FMAX    ULONG_MAX
57
58 ATF_TC_WITHOUT_HEAD(kern_copyin);
59 ATF_TC_BODY(kern_copyin, tc)
60 {
61         char template[] = "copyin.XXXXXX";
62
63 #ifdef __mips__
64         /*
65          * MIPS has different VM layout: the UVA map on mips ends the
66          * highest mapped entry at the VM_MAXUSER_ADDRESS - PAGE_SIZE,
67          * while all other arches map either stack or shared page up
68          * to the VM_MAXUSER_ADDRESS.
69          */
70         atf_tc_skip("Platform is not supported.");
71 #endif
72
73         umask(0077);
74         scratch_file = mkstemp(template);
75         ATF_REQUIRE(scratch_file != -1);
76         unlink(template);
77
78         ATF_CHECK(copyin_checker(0, 0) == 0);
79         ATF_CHECK(copyin_checker(VM_MAXUSER_ADDRESS - 10, 9) == 0);
80         ATF_CHECK(copyin_checker(VM_MAXUSER_ADDRESS - 10, 10) == 0);
81         ATF_CHECK(copyin_checker(VM_MAXUSER_ADDRESS - 10, 11) == EFAULT);
82         ATF_CHECK(copyin_checker(VM_MAXUSER_ADDRESS - 1, 1) == 0);
83         ATF_CHECK(copyin_checker(VM_MAXUSER_ADDRESS, 0) == 0);
84         ATF_CHECK(copyin_checker(VM_MAXUSER_ADDRESS, 1) == EFAULT);
85         ATF_CHECK(copyin_checker(VM_MAXUSER_ADDRESS, 2) == EFAULT);
86         ATF_CHECK(copyin_checker(VM_MAXUSER_ADDRESS + 1, 0) == 0);
87         ATF_CHECK(copyin_checker(VM_MAXUSER_ADDRESS + 1, 2) == EFAULT);
88         ATF_CHECK(copyin_checker(FMAX - 10, 9) == EFAULT);
89         ATF_CHECK(copyin_checker(FMAX - 10, 10) == EFAULT);
90         ATF_CHECK(copyin_checker(FMAX - 10, 11) == EFAULT);
91 }
92
93 ATF_TP_ADD_TCS(tp)
94 {
95
96         ATF_TP_ADD_TC(tp, kern_copyin);
97         return (atf_no_error());
98 }