Home | History | Annotate | Download | only in tests-mx32
      1 /*
      2  * Check decoding of "old mmap" edition of mmap syscall.
      3  *
      4  * Copyright (c) 2016 Dmitry V. Levin <ldv (at) altlinux.org>
      5  * Copyright (c) 2016-2018 The strace developers.
      6  * All rights reserved.
      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  * 3. The name of the author may not be used to endorse or promote products
     17  *    derived from this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "tests.h"
     32 #include <asm/unistd.h>
     33 
     34 /*
     35  * On s390x and m68k, this is the mmap syscall used by glibc, so,
     36  * from one side, it's already covered by another test, and, from another side,
     37  * it would require additional efforts to filter out mmap calls made by glibc.
     38  */
     39 
     40 #if defined __NR_mmap \
     41 	&& (defined __arm__ || defined __i386__ || defined __m68k__ \
     42 		|| defined __s390__ || defined __s390x__) \
     43 	&& (defined PATH_TRACING || !(defined __s390x__ || defined __m68k__))
     44 
     45 # include <stdio.h>
     46 # include <string.h>
     47 # include <sys/mman.h>
     48 # include <unistd.h>
     49 
     50 # ifndef TEST_FD
     51 #  define TEST_FD -2LU
     52 # endif
     53 
     54 int
     55 main(void)
     56 {
     57 	long rc = syscall(__NR_mmap, 0);
     58 # ifndef PATH_TRACING
     59 	printf("mmap(NULL) = %ld %s (%m)\n", rc, errno2name());
     60 # endif
     61 
     62 	const unsigned long args1_c[6] = {
     63 		(unsigned long) 0xbadc0deddeadbeefULL,	/* addr */
     64 		(unsigned long) 0xdeefacedfacefeedULL,	/* len */
     65 		PROT_READ|PROT_EXEC,	/* prot */
     66 		MAP_FILE|MAP_FIXED,	/* flags */
     67 		TEST_FD,		/* fd */
     68 		(unsigned long) 0xdecaffedbadc0dedULL	/* offset */
     69 	};
     70 	const unsigned long page_size = get_page_size();
     71 	const unsigned long args2_c[6] = {
     72 		0,
     73 		page_size,
     74 		PROT_READ|PROT_WRITE,
     75 		MAP_PRIVATE|MAP_ANONYMOUS,
     76 		-1LU,
     77 		(unsigned long) 0xda7a1057faced000ULL & -page_size
     78 	};
     79 	void *args = tail_memdup(args1_c, sizeof(args1_c));
     80 
     81 	rc = syscall(__NR_mmap, args);
     82 	printf("mmap(%#lx, %lu, PROT_READ|PROT_EXEC, MAP_FILE|MAP_FIXED"
     83 	       ", %d, %#lx) = %ld %s (%m)\n",
     84 	       args1_c[0], args1_c[1], (int) args1_c[4], args1_c[5],
     85 	       rc, errno2name());
     86 
     87 	memcpy(args, args2_c, sizeof(args2_c));
     88 	rc = syscall(__NR_mmap, args);
     89 # ifndef PATH_TRACING
     90 	printf("mmap(NULL, %lu, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS"
     91 	       ", %d, %#lx) = %#lx\n",
     92 	       args2_c[1], (int) args2_c[4], args2_c[5], rc);
     93 # endif
     94 
     95 	void *addr = (void *) rc;
     96 	if (mprotect(addr, page_size, PROT_NONE))
     97 		perror_msg_and_fail("mprotect(%p, %lu, PROT_NONE)",
     98 				    addr, page_size);
     99 
    100 	puts("+++ exited with 0 +++");
    101 	return 0;
    102 }
    103 
    104 #else
    105 
    106 SKIP_MAIN_UNDEFINED("defined __NR_mmap "
    107 	"&& (defined __arm__ || defined __i386__ || defined __m68k__ "
    108 		"|| defined __s390__ || defined __s390x__) "
    109 	"&& (defined PATH_TRACING || !(defined __s390x__ || defined __m68k__))")
    110 
    111 #endif
    112