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