• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Check decoding of set_mempolicy syscall.
3  *
4  * Copyright (c) 2016 Dmitry V. Levin <ldv@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 #ifdef __NR_set_mempolicy
35 
36 # include <errno.h>
37 # include <stdio.h>
38 # include <string.h>
39 # include <unistd.h>
40 
41 # include "xlat.h"
42 # include "xlat/policies.h"
43 
44 # define MAX_STRLEN 3
45 # define NLONGS(n) ((n + 8 * sizeof(long) - 2) \
46 		      / (8 * sizeof(long)))
47 
48 static void
print_nodes(const unsigned long maxnode,unsigned int offset)49 print_nodes(const unsigned long maxnode, unsigned int offset)
50 {
51 	unsigned int nlongs = NLONGS(maxnode);
52 	if (nlongs <= offset)
53 		nlongs = 0;
54 	else
55 		nlongs -= offset;
56 	const unsigned int size = nlongs * sizeof(long);
57 	unsigned long *const nodemask =
58 		tail_alloc(size ? size : (offset ? 1 : 0));
59 	memset(nodemask, 0, size);
60 
61 	long rc = syscall(__NR_set_mempolicy, 0, nodemask, maxnode);
62 	const char *errstr = sprintrc(rc);
63 
64 	fputs("set_mempolicy(MPOL_DEFAULT, ", stdout);
65 
66 	if (nlongs) {
67 		putc('[', stdout);
68 		unsigned int i;
69 		for (i = 0; i < nlongs + offset; ++i) {
70 			if (i)
71 				fputs(", ", stdout);
72 			if (i < nlongs) {
73 				if (i >= MAX_STRLEN) {
74 					fputs("...", stdout);
75 					break;
76 				}
77 				printf("%#0*lx", (int) sizeof(long) * 2 + 2,
78 				       nodemask[i]);
79 			} else {
80 				printf("... /* %p */", nodemask + i);
81 				break;
82 			}
83 		}
84 		putc(']', stdout);
85 	} else {
86 		if (maxnode)
87 			printf("%p", nodemask);
88 		else
89 			printf("[]");
90 	}
91 
92 	printf(", %lu) = %s\n", maxnode, errstr);
93 }
94 
95 static void
test_offset(const unsigned int offset)96 test_offset(const unsigned int offset)
97 {
98 	unsigned long maxnode = get_page_size() * 8;
99 
100 	print_nodes(maxnode, offset);
101 	print_nodes(maxnode + 1, offset);
102 	print_nodes(maxnode + 2, offset);
103 
104 	maxnode = sizeof(long) * 8;
105 	print_nodes(0, offset);
106 	print_nodes(1, offset);
107 	print_nodes(2, offset);
108 	print_nodes(maxnode - 1, offset);
109 	print_nodes(maxnode    , offset);
110 	print_nodes(maxnode + 1, offset);
111 	print_nodes(maxnode + 2, offset);
112 	print_nodes(maxnode * 2 - 1, offset);
113 	print_nodes(maxnode * 2    , offset);
114 	print_nodes(maxnode * 2 + 1, offset);
115 	print_nodes(maxnode * 2 + 2, offset);
116 	print_nodes(maxnode * 3 - 1, offset);
117 	print_nodes(maxnode * 3    , offset);
118 	print_nodes(maxnode * 3 + 1, offset);
119 	print_nodes(maxnode * 3 + 2, offset);
120 	print_nodes(maxnode * 4 + 2, offset);
121 }
122 
123 int
main(void)124 main(void)
125 {
126 	if (syscall(__NR_set_mempolicy, 0, 0, 0))
127 		perror_msg_and_skip("set_mempolicy");
128 	puts("set_mempolicy(MPOL_DEFAULT, NULL, 0) = 0");
129 
130 	const unsigned long *nodemask = (void *) 0xfacefeedfffffffeULL;
131 	const unsigned long maxnode = (unsigned long) 0xcafef00dbadc0dedULL;
132 	long rc = syscall(__NR_set_mempolicy, 1, nodemask, maxnode);
133 	printf("set_mempolicy(MPOL_PREFERRED, %p, %lu) = %s\n",
134 	       nodemask, maxnode, sprintrc(rc));
135 
136 	test_offset(0);
137 	test_offset(1);
138 
139 	puts("+++ exited with 0 +++");
140 	return 0;
141 }
142 
143 #else
144 
145 SKIP_MAIN_UNDEFINED("__NR_set_mempolicy")
146 
147 #endif
148