blob: 9c2948909f4f3c436ec755055912169b1ba11229 [file] [log] [blame]
Dmitry V. Levind10a9552016-04-27 23:43:24 +00001/*
2 * Check decoding of set_mempolicy syscall.
3 *
4 * Copyright (c) 2016 Dmitry V. Levin <ldv@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 <sys/syscall.h>
32
33#ifdef __NR_set_mempolicy
34
35# include <errno.h>
36# include <stdio.h>
37# include <string.h>
38# include <unistd.h>
39
40# include "xlat.h"
41# include "xlat/policies.h"
42
43# define MAX_STRLEN 3
44# define NLONGS(n) ((n + 8 * sizeof(long) - 2) \
45 / (8 * sizeof(long)))
46
47static void
48print_nodes(const unsigned long maxnode, unsigned int offset)
49{
50 unsigned int nlongs = NLONGS(maxnode);
51 if (nlongs <= offset)
52 nlongs = 0;
53 else
54 nlongs -= offset;
55 const unsigned int size = nlongs * sizeof(long);
56 unsigned long *const nodemask =
57 tail_alloc(size ? size : (offset ? 1 : 0));
58 memset(nodemask, 0, size);
59 (void) tail_alloc(1);
60
61 long rc = syscall(__NR_set_mempolicy, 0, nodemask, maxnode);
62 int saved_errno = errno;
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 printf("%p", nodemask);
87 }
88
89 printf(", %lu) = ", maxnode);
90 if (rc) {
91 errno = saved_errno;
92 printf("%ld %s (%m)\n", rc, errno2name());
93 } else {
94 puts("0");
95 }
96}
97
98static void
99test_offset(const unsigned int offset)
100{
101 unsigned long maxnode = get_page_size() * 8;
102
103 print_nodes(maxnode, offset);
104 print_nodes(maxnode + 1, offset);
105 print_nodes(maxnode + 2, offset);
106
107 maxnode = sizeof(long) * 8;
108 print_nodes(0, offset);
109 print_nodes(1, offset);
110 print_nodes(2, offset);
111 print_nodes(maxnode - 1, offset);
112 print_nodes(maxnode , offset);
113 print_nodes(maxnode + 1, offset);
114 print_nodes(maxnode + 2, offset);
115 print_nodes(maxnode * 2 - 1, offset);
116 print_nodes(maxnode * 2 , offset);
117 print_nodes(maxnode * 2 + 1, offset);
118 print_nodes(maxnode * 2 + 2, offset);
119 print_nodes(maxnode * 3 - 1, offset);
120 print_nodes(maxnode * 3 , offset);
121 print_nodes(maxnode * 3 + 1, offset);
122 print_nodes(maxnode * 3 + 2, offset);
123 print_nodes(maxnode * 4 + 2, offset);
124}
125
126int
127main(void)
128{
129 if (syscall(__NR_set_mempolicy, 0, 0, 0))
130 perror_msg_and_skip("set_mempolicy");
131 puts("set_mempolicy(MPOL_DEFAULT, NULL, 0) = 0");
132
133 const unsigned long *nodemask = (void *) 0xfacefeedfffffffe;
134 const unsigned long maxnode = (unsigned long) 0xcafef00dbadc0ded;
135 long rc = syscall(__NR_set_mempolicy, 1, nodemask, maxnode);
136 printf("set_mempolicy(MPOL_PREFERRED, %p, %lu) = %ld %s (%m)\n",
137 nodemask, maxnode, rc, errno2name());
138
139 test_offset(0);
140 test_offset(1);
141
142 puts("+++ exited with 0 +++");
143 return 0;
144}
145
146#else
147
148SKIP_MAIN_UNDEFINED("__NR_set_mempolicy")
149
150#endif