diff options
Diffstat (limited to 'src/regress/lib/libc/sys/t_syscall.c')
| -rw-r--r-- | src/regress/lib/libc/sys/t_syscall.c | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/src/regress/lib/libc/sys/t_syscall.c b/src/regress/lib/libc/sys/t_syscall.c new file mode 100644 index 0000000000..e864970262 --- /dev/null +++ b/src/regress/lib/libc/sys/t_syscall.c | |||
| @@ -0,0 +1,122 @@ | |||
| 1 | /* $OpenBSD: t_syscall.c,v 1.1.1.1 2019/11/19 19:57:04 bluhm Exp $ */ | ||
| 2 | /* $NetBSD: t_syscall.c,v 1.3 2018/05/28 07:55:56 martin Exp $ */ | ||
| 3 | |||
| 4 | /*- | ||
| 5 | * Copyright (c) 2018 The NetBSD Foundation, Inc. | ||
| 6 | * All rights reserved. | ||
| 7 | * | ||
| 8 | * This code is derived from software contributed to The NetBSD Foundation | ||
| 9 | * by Martin Husemann. | ||
| 10 | * | ||
| 11 | * Redistribution and use in source and binary forms, with or without | ||
| 12 | * modification, are permitted provided that the following conditions | ||
| 13 | * are met: | ||
| 14 | * 1. Redistributions of source code must retain the above copyright | ||
| 15 | * notice, this list of conditions and the following disclaimer. | ||
| 16 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 17 | * notice, this list of conditions and the following disclaimer in the | ||
| 18 | * documentation and/or other materials provided with the distribution. | ||
| 19 | * | ||
| 20 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | ||
| 21 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | ||
| 22 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 23 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | ||
| 24 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 30 | * POSSIBILITY OF SUCH DAMAGE. | ||
| 31 | */ | ||
| 32 | |||
| 33 | #include "macros.h" | ||
| 34 | |||
| 35 | #include <sys/cdefs.h> | ||
| 36 | __RCSID("$NetBSD: t_syscall.c,v 1.3 2018/05/28 07:55:56 martin Exp $"); | ||
| 37 | |||
| 38 | |||
| 39 | #include "atf-c.h" | ||
| 40 | #include <stdio.h> | ||
| 41 | #include <unistd.h> | ||
| 42 | #include <fcntl.h> | ||
| 43 | #include <err.h> | ||
| 44 | #include <string.h> | ||
| 45 | #include <stdlib.h> | ||
| 46 | #include <sys/mman.h> | ||
| 47 | #include <sys/endian.h> | ||
| 48 | #include <sys/syscall.h> | ||
| 49 | |||
| 50 | #if !defined(_LP64) && BYTE_ORDER == _BIG_ENDIAN | ||
| 51 | #define __SYSCALL_TO_UINTPTR_T(V) ((uintptr_t)((V)>>32)) | ||
| 52 | #else | ||
| 53 | #define __SYSCALL_TO_UINTPTR_T(V) ((uintptr_t)(V)) | ||
| 54 | #endif | ||
| 55 | |||
| 56 | static const char secrect_data[1024] = { | ||
| 57 | "my secret key\n" | ||
| 58 | }; | ||
| 59 | |||
| 60 | #define FILE_NAME "dummy" | ||
| 61 | |||
| 62 | #ifndef _LP64 | ||
| 63 | ATF_TC(mmap_syscall); | ||
| 64 | |||
| 65 | ATF_TC_HEAD(mmap_syscall, tc) | ||
| 66 | { | ||
| 67 | atf_tc_set_md_var(tc, "descr", "Tests mmap(2) via syscall(2)"); | ||
| 68 | } | ||
| 69 | |||
| 70 | ATF_TC_BODY(mmap_syscall, tc) | ||
| 71 | { | ||
| 72 | int fd; | ||
| 73 | const char *p; | ||
| 74 | |||
| 75 | fd = open(FILE_NAME, O_RDWR|O_CREAT|O_TRUNC, 0666); | ||
| 76 | ATF_REQUIRE(fd != -1); | ||
| 77 | |||
| 78 | write(fd, secrect_data, sizeof(secrect_data)); | ||
| 79 | |||
| 80 | p = (const char *)syscall(SYS_mmap, | ||
| 81 | 0, sizeof(secrect_data), PROT_READ, MAP_PRIVATE, fd, 0, 0, 0); | ||
| 82 | ATF_REQUIRE(p != NULL); | ||
| 83 | |||
| 84 | ATF_REQUIRE(strcmp(p, secrect_data) == 0); | ||
| 85 | } | ||
| 86 | #endif | ||
| 87 | |||
| 88 | ATF_TC(mmap___syscall); | ||
| 89 | |||
| 90 | ATF_TC_HEAD(mmap___syscall, tc) | ||
| 91 | { | ||
| 92 | atf_tc_set_md_var(tc, "descr", "Tests mmap(2) via __syscall(2)"); | ||
| 93 | } | ||
| 94 | |||
| 95 | ATF_TC_BODY(mmap___syscall, tc) | ||
| 96 | { | ||
| 97 | int fd; | ||
| 98 | const char *p; | ||
| 99 | |||
| 100 | fd = open(FILE_NAME, O_RDWR|O_CREAT|O_TRUNC, 0666); | ||
| 101 | ATF_REQUIRE(fd != -1); | ||
| 102 | |||
| 103 | write(fd, secrect_data, sizeof(secrect_data)); | ||
| 104 | |||
| 105 | p = (const char *)__SYSCALL_TO_UINTPTR_T(__syscall(SYS_mmap, | ||
| 106 | 0, sizeof(secrect_data), PROT_READ, MAP_PRIVATE, fd, | ||
| 107 | /* pad*/ 0, (off_t)0)); | ||
| 108 | ATF_REQUIRE(p != NULL); | ||
| 109 | |||
| 110 | ATF_REQUIRE(strcmp(p, secrect_data) == 0); | ||
| 111 | } | ||
| 112 | |||
| 113 | ATF_TP_ADD_TCS(tp) | ||
| 114 | { | ||
| 115 | |||
| 116 | #ifndef _LP64 | ||
| 117 | ATF_TP_ADD_TC(tp, mmap_syscall); | ||
| 118 | #endif | ||
| 119 | ATF_TP_ADD_TC(tp, mmap___syscall); | ||
| 120 | |||
| 121 | return atf_no_error(); | ||
| 122 | } | ||
