(root)/
strace-6.5/
tests/
get_mempolicy.c
       1  /*
       2   * Check decoding of get_mempolicy syscall.
       3   *
       4   * Copyright (c) 2016-2022 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  
      13  #include <stdio.h>
      14  #include <unistd.h>
      15  
      16  #include "xlat.h"
      17  #include "xlat/mpol_modes.h"
      18  
      19  #define MAX_STRLEN 3
      20  #define NLONGS(n) ((n + 8 * sizeof(long) - 2) \
      21  		     / (8 * sizeof(long)))
      22  
      23  static void
      24  print_nodes(unsigned long maxnode)
      25  {
      26  	unsigned long *const nodemask =
      27  		tail_alloc(sizeof(*nodemask) * NLONGS(maxnode));
      28  
      29  	if (syscall(__NR_get_mempolicy, 0, nodemask, maxnode, 0, 0)) {
      30  		printf("get_mempolicy(NULL, %p, %lu, NULL, 0) = -1 %s (%m)\n",
      31  		       nodemask, maxnode, errno2name());
      32  		return;
      33  	}
      34  
      35  	printf("get_mempolicy(NULL, [");
      36  
      37  	unsigned int nlongs = NLONGS(maxnode);
      38  	for (unsigned int i = 0; i < nlongs; ++i) {
      39  		if (i)
      40  			fputs(", ", stdout);
      41  		if (i >= MAX_STRLEN) {
      42  			fputs("...", stdout);
      43  			break;
      44  		}
      45  		printf("%#0*lx", (int) sizeof(*nodemask) * 2, nodemask[i]);
      46  	}
      47  
      48  	printf("], %lu, NULL, 0) = 0\n", maxnode);
      49  }
      50  
      51  int
      52  main(void)
      53  {
      54  	long rc;
      55  
      56  	if (syscall(__NR_get_mempolicy, 0, 0, 0, 0, 0))
      57  		perror_msg_and_skip("get_mempolicy");
      58  	puts("get_mempolicy(NULL, NULL, 0, NULL, 0) = 0");
      59  
      60  	int *mode = (void *) 0xdefaced1baddeed2;
      61  	unsigned long maxnode = (unsigned long) 0xcafef00dbadc0dedULL;
      62  	const unsigned long *nodemask = (void *) 0xfacedad3bebefed4ULL;
      63  	const unsigned long addr = (unsigned long) 0xfacefeeddeadbeefULL;
      64  	const unsigned long flags = -1U;
      65  	rc = syscall(__NR_get_mempolicy, mode, nodemask, maxnode, addr, flags);
      66  	printf("get_mempolicy(%p, %p, %lu, %#lx, %s|%#lx) = %ld %s (%m)\n",
      67  	       mode, nodemask, maxnode, addr,
      68  	       "MPOL_F_NODE|MPOL_F_ADDR|MPOL_F_MEMS_ALLOWED",
      69  	       flags & ~7, rc, errno2name());
      70  
      71  	mode = tail_alloc(sizeof(*mode));
      72  
      73  	rc = syscall(__NR_get_mempolicy, mode, 0, 0, 0, 0);
      74  	printf("get_mempolicy([");
      75  	printxval(mpol_modes, (unsigned) *mode, "MPOL_???");
      76  	printf("], NULL, 0, NULL, 0) = %ld\n", rc);
      77  
      78  	*mode = -1;
      79  	rc = syscall(__NR_get_mempolicy, mode, 0, 0, mode - 1, 2);
      80  	printf("get_mempolicy([");
      81  	printxval(mpol_modes, (unsigned) *mode, "MPOL_???");
      82  	printf("], NULL, 0, %p, MPOL_F_ADDR) = %ld\n", mode - 1, rc);
      83  
      84  	maxnode = get_page_size() * 8;
      85  
      86  	print_nodes(maxnode);
      87  	print_nodes(maxnode + 1);
      88  	print_nodes(maxnode + 2);
      89  
      90  	maxnode = sizeof(*nodemask) * 8;
      91  	print_nodes(maxnode - 1);
      92  	print_nodes(maxnode    );
      93  	print_nodes(maxnode + 1);
      94  	print_nodes(maxnode + 2);
      95  	print_nodes(maxnode * 2 - 1);
      96  	print_nodes(maxnode * 2    );
      97  	print_nodes(maxnode * 2 + 1);
      98  	print_nodes(maxnode * 2 + 2);
      99  	print_nodes(maxnode * 3 - 1);
     100  	print_nodes(maxnode * 3    );
     101  	print_nodes(maxnode * 3 + 1);
     102  	print_nodes(maxnode * 3 + 2);
     103  
     104  	puts("+++ exited with 0 +++");
     105  	return 0;
     106  }