Home | History | Annotate | Download | only in tests-mx32
      1 /*
      2  * Check decoding of memfd_create syscall.
      3  *
      4  * Copyright (c) 2015-2017 Dmitry V. Levin <ldv (at) altlinux.org>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 #include "tests.h"
     31 #include <asm/unistd.h>
     32 #include "scno.h"
     33 
     34 #ifdef __NR_memfd_create
     35 
     36 # include <stdio.h>
     37 # include <stdint.h>
     38 # include <unistd.h>
     39 
     40 # ifdef HAVE_LINUX_MEMFD_H
     41 #  include <linux/memfd.h>
     42 # endif
     43 
     44 # ifndef MFD_HUGE_SHIFT
     45 #  define MFD_HUGE_SHIFT 26
     46 # endif
     47 
     48 # ifndef MFD_HUGE_MASK
     49 #  define MFD_HUGE_MASK 0x3f
     50 # endif
     51 
     52 static const char *errstr;
     53 
     54 static long
     55 k_memfd_create(const kernel_ulong_t name, const kernel_ulong_t flags)
     56 {
     57 	const long rc = syscall(__NR_memfd_create, name, flags);
     58 	errstr = sprintrc(rc);
     59 	return rc;
     60 }
     61 
     62 int
     63 main(void)
     64 {
     65 	const size_t size = 255 - (sizeof("memfd:") - 1) + 1;
     66 	char *pattern = tail_alloc(size);
     67 	fill_memory_ex(pattern, size, '0', 10);
     68 
     69 	k_memfd_create((uintptr_t) pattern, 0);
     70 	printf("memfd_create(\"%.*s\"..., 0) = %s\n",
     71 	       (int) size - 1, pattern, errstr);
     72 
     73 	kernel_ulong_t flags = (kernel_ulong_t) 0xfacefeed00000007ULL;
     74 	k_memfd_create((uintptr_t) pattern, flags);
     75 	printf("memfd_create(\"%.*s\"..., %s) = %s\n",
     76 	       (int) size - 1, pattern,
     77 	       "MFD_CLOEXEC|MFD_ALLOW_SEALING|MFD_HUGETLB",
     78 	       errstr);
     79 
     80 	pattern[size - 1] = '\0';
     81 	flags = 30 << MFD_HUGE_SHIFT;
     82 	k_memfd_create((uintptr_t) pattern, flags);
     83 	printf("memfd_create(\"%s\", 30<<MFD_HUGE_SHIFT) = %s\n",
     84 	       pattern, errstr);
     85 
     86 	pattern += size - 1;
     87 	flags = (kernel_ulong_t) -1ULL;
     88 	k_memfd_create(0, flags);
     89 	flags = -1U & ~(7 | (MFD_HUGE_MASK << MFD_HUGE_SHIFT));
     90 	printf("memfd_create(NULL, MFD_CLOEXEC|MFD_ALLOW_SEALING|MFD_HUGETLB"
     91 	       "|%#x|%u<<MFD_HUGE_SHIFT) = %s\n",
     92 	       (unsigned int) flags, MFD_HUGE_MASK, errstr);
     93 
     94 	puts("+++ exited with 0 +++");
     95 	return 0;
     96 }
     97 
     98 #else
     99 
    100 SKIP_MAIN_UNDEFINED("__NR_memfd_create")
    101 
    102 #endif
    103