kern_copyin.c revision 303975
1237651Sbschmidt/*-
2237651Sbschmidt * Copyright (c) 2015 The FreeBSD Foundation
3237651Sbschmidt * All rights reserved.
4237651Sbschmidt *
5237651Sbschmidt * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
6237651Sbschmidt * under sponsorship from the FreeBSD Foundation.
7237651Sbschmidt *
8237651Sbschmidt * Redistribution and use in source and binary forms, with or without
9237651Sbschmidt * modification, are permitted provided that the following conditions
10237651Sbschmidt * are met:
11237651Sbschmidt * 1. Redistributions of source code must retain the above copyright
12237651Sbschmidt *    notice, this list of conditions and the following disclaimer.
13237651Sbschmidt * 2. Redistributions in binary form must reproduce the above copyright
14237651Sbschmidt *    notice, this list of conditions and the following disclaimer in the
15237651Sbschmidt *    documentation and/or other materials provided with the distribution.
16237651Sbschmidt *
17237651Sbschmidt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18237651Sbschmidt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19237651Sbschmidt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20237651Sbschmidt * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21237651Sbschmidt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22237651Sbschmidt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23237651Sbschmidt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24237651Sbschmidt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25237651Sbschmidt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26237651Sbschmidt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27237651Sbschmidt * SUCH DAMAGE.
28237651Sbschmidt */
29237651Sbschmidt
30237651Sbschmidt#include <sys/cdefs.h>
31237651Sbschmidt__FBSDID("$FreeBSD: releng/11.0/tests/sys/kern/kern_copyin.c 289603 2015-10-19 20:22:17Z kib $");
32237651Sbschmidt
33237651Sbschmidt#include <sys/param.h>
34237651Sbschmidt#include <errno.h>
35237651Sbschmidt#include <limits.h>
36237651Sbschmidt#include <stdio.h>
37237651Sbschmidt#include <stdlib.h>
38237651Sbschmidt#include <unistd.h>
39237651Sbschmidt#include <atf-c.h>
40237651Sbschmidt#include <vm/vm.h>
41237651Sbschmidt#include <vm/pmap.h>
42237651Sbschmidt#include <machine/vmparam.h>
43237651Sbschmidt
44237651Sbschmidtstatic int scratch_file;
45237651Sbschmidt
46237651Sbschmidtstatic int
47237651Sbschmidtcopyin_checker(uintptr_t uaddr, size_t len)
48237651Sbschmidt{
49237651Sbschmidt	ssize_t ret;
50237651Sbschmidt
51237651Sbschmidt	ret = write(scratch_file, (const void *)uaddr, len);
52237651Sbschmidt	return (ret == -1 ? errno : 0);
53237651Sbschmidt}
54237651Sbschmidt
55237651Sbschmidt#define	FMAX	ULONG_MAX
56237651Sbschmidt
57237651SbschmidtATF_TC_WITHOUT_HEAD(kern_copyin);
58237651SbschmidtATF_TC_BODY(kern_copyin, tc)
59237651Sbschmidt{
60237651Sbschmidt	char template[] = "copyin.XXXXXX";
61237651Sbschmidt
62237651Sbschmidt	scratch_file = mkstemp(template);
63237651Sbschmidt	ATF_REQUIRE(scratch_file != -1);
64237651Sbschmidt	unlink(template);
65237651Sbschmidt
66237651Sbschmidt	ATF_CHECK(copyin_checker(0, 0) == 0);
67237651Sbschmidt	ATF_CHECK(copyin_checker(VM_MAXUSER_ADDRESS - 10, 9) == 0);
68237651Sbschmidt	ATF_CHECK(copyin_checker(VM_MAXUSER_ADDRESS - 10, 10) == 0);
69237651Sbschmidt	ATF_CHECK(copyin_checker(VM_MAXUSER_ADDRESS - 10, 11) == EFAULT);
70237651Sbschmidt	ATF_CHECK(copyin_checker(VM_MAXUSER_ADDRESS - 1, 1) == 0);
71237651Sbschmidt	ATF_CHECK(copyin_checker(VM_MAXUSER_ADDRESS, 0) == 0);
72237651Sbschmidt	ATF_CHECK(copyin_checker(VM_MAXUSER_ADDRESS, 1) == EFAULT);
73237651Sbschmidt	ATF_CHECK(copyin_checker(VM_MAXUSER_ADDRESS, 2) == EFAULT);
74237651Sbschmidt	ATF_CHECK(copyin_checker(VM_MAXUSER_ADDRESS + 1, 0) == 0);
75237651Sbschmidt	ATF_CHECK(copyin_checker(VM_MAXUSER_ADDRESS + 1, 2) == EFAULT);
76237651Sbschmidt	ATF_CHECK(copyin_checker(FMAX - 10, 9) == EFAULT);
77237651Sbschmidt	ATF_CHECK(copyin_checker(FMAX - 10, 10) == EFAULT);
78237651Sbschmidt	ATF_CHECK(copyin_checker(FMAX - 10, 11) == EFAULT);
79237651Sbschmidt}
80237651Sbschmidt
81237651SbschmidtATF_TP_ADD_TCS(tp)
82237651Sbschmidt{
83237651Sbschmidt
84237651Sbschmidt	ATF_TP_ADD_TC(tp, kern_copyin);
85237651Sbschmidt	return (atf_no_error());
86237651Sbschmidt}
87237651Sbschmidt