(root)/
strace-6.5/
tests/
process_madvise.c
       1  /*
       2   * Check decoding of process_madvise syscall.
       3   *
       4   * Copyright (c) 2020 Dmitry V. Levin <ldv@strace.io>
       5   * All rights reserved.
       6   *
       7   * SPDX-License-Identifier: GPL-2.0-or-later
       8   */
       9  
      10  #include "tests.h"
      11  #include "scno.h"
      12  #include <fcntl.h>
      13  #include <stdio.h>
      14  #include <stdlib.h>
      15  #include <unistd.h>
      16  #include <sys/mman.h>
      17  #include <sys/uio.h>
      18  
      19  #ifndef FD0_PATH
      20  # define FD0_PATH ""
      21  #endif
      22  
      23  static const char *errstr;
      24  
      25  static long
      26  k_process_madvise(const unsigned int pidfd,
      27  		  const struct iovec *const addr,
      28  		  const kernel_ulong_t len,
      29  		  const unsigned int cmd,
      30  		  const unsigned int flags)
      31  {
      32  	const kernel_ulong_t fill = (kernel_ulong_t) 0xdefaced00000000ULL;
      33  	const kernel_ulong_t bad = (kernel_ulong_t) 0xbadc0dedbadc0dedULL;
      34  	const kernel_ulong_t arg1 = fill | pidfd;
      35  	const kernel_ulong_t arg2 = (uintptr_t) addr;
      36  	const kernel_ulong_t arg3 = len;
      37  	const kernel_ulong_t arg4 = fill | cmd;
      38  	const kernel_ulong_t arg5 = fill | flags;
      39  	const long rc = syscall(__NR_process_madvise,
      40  				arg1, arg2, arg3, arg4, arg5, bad);
      41  	errstr = sprintrc(rc);
      42  	return rc;
      43  }
      44  
      45  int
      46  main(void)
      47  {
      48  #ifdef PRINT_PATHS
      49  	skip_if_unavailable("/proc/self/fd/");
      50  #endif
      51  
      52  	(void) close(0);
      53  	if (open("/dev/full", O_WRONLY))
      54  		perror_msg_and_fail("open");
      55  
      56  	struct iovec *iov = tail_alloc(2 * sizeof(*iov));
      57  	fill_memory(iov, 2 * sizeof(*iov));
      58  
      59  	k_process_madvise(0, iov, 2, 0, -1U);
      60  	printf("process_madvise(0" FD0_PATH
      61  	       ", [{iov_base=%p, iov_len=%lu}, {iov_base=%p, iov_len=%lu}]"
      62  	       ", 2, MADV_NORMAL, %#x) = %s\n",
      63  	       iov[0].iov_base, (unsigned long) iov[0].iov_len,
      64  	       iov[1].iov_base, (unsigned long) iov[1].iov_len,
      65  	       -1U, errstr);
      66  
      67  	const kernel_ulong_t bogus_len = (kernel_ulong_t) 0xdeadbeefdeadbeef;
      68  	k_process_madvise(-1, 0, bogus_len, 20, 0);
      69  	printf("process_madvise(-1, NULL, %" PRI_klu ", MADV_COLD, 0) = %s\n",
      70  	       bogus_len, errstr);
      71  
      72  	const unsigned int bogus_flags = 0xcafef00d;
      73  	k_process_madvise(0, iov + 2, 0, 21, bogus_flags);
      74  	printf("process_madvise(0" FD0_PATH ", [], 0, MADV_PAGEOUT, %#x)"
      75  	       " = %s\n", bogus_flags, errstr);
      76  
      77  	const unsigned int bogus_cmd = 0xdeadc0de;
      78  	k_process_madvise(-1, iov + 1, 2, bogus_cmd, 0);
      79  	printf("process_madvise(-1, [{iov_base=%p, iov_len=%lu}, ... /* %p */]"
      80  	       ", 2, %#x /* MADV_??? */, 0) = %s\n",
      81  	       iov[1].iov_base, (unsigned long) iov[1].iov_len,
      82  	       iov + 2, bogus_cmd, errstr);
      83  
      84  	puts("+++ exited with 0 +++");
      85  	return 0;
      86  }