summaryrefslogtreecommitdiff
path: root/tests/xet_thread_area_x86.c
blob: bdc07883d71612c079814c86333901ee8d9ddc05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
 * Check decoding of set_thread_area and get_thread_area syscalls on x86
 * architecture.
 *
 * Copyright (c) 2018-2019 The strace developers.
 * All rights reserved.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "tests.h"

#include "scno.h"

#if defined __NR_get_thread_area && defined __NR_set_thread_area \
 && defined HAVE_STRUCT_USER_DESC

# include <assert.h>
# include <errno.h>
# include <stdio.h>
# include <stdint.h>
# include <string.h>
# include <unistd.h>

# include "print_user_desc.c"

long errnum;

static void
printptr(kernel_ulong_t ptr, const char *ptr_str)
{
	if (ptr_str)
		printf("%s", ptr_str);
	else
		printf("%#llx", zero_extend_signed_to_ull(ptr));
}

/**
 * Perform set_thread_area call along with printing the expected output.
 *
 * @param ptr_val Pointer to thread area argument.
 * @param ptr_str Explicit string representation of the argument.
 * @param valid   Whether argument points to the valid memory and its contents
 *                should be decoded.
 * @param entry_number_str explicit decoding of the entry_number field.
 */
static long
set_thread_area(kernel_ulong_t ptr_val, const char *ptr_str, bool valid,
		const char *entry_number_str)
{
	struct user_desc *ptr = (struct user_desc *) (uintptr_t) ptr_val;
	long rc = -1;
	int saved_errno;

	rc = syscall(__NR_set_thread_area, ptr_val);
	saved_errno = errno;
	printf("set_thread_area(");

	if (valid)
		print_user_desc(ptr, entry_number_str);
	else
		printptr(ptr_val, ptr_str);

	errno = saved_errno;
	printf(") = %s", sprintrc(rc));
	if (!rc)
		printf(" (entry_number=%u)", ptr->entry_number);

	puts("");

	return rc;
}

/**
 * Perform get_thread_are call along with printing the expected output and
 * checking the result against the argument of the previous set_thread_area
 * call, if it had place.
 *
 * @param ptr_val  Pointer to thread area argument.
 * @param ptr_str  Explicit string representation of the argument.
 * @param valid    Whether argument points to the valid memory and its contents
 *                 should be decoded.
 * @param set_rc   Return code of the previous set_thread_area call.
 * @param expected The value of the argument passed to the previous
 *                 set_thread_area call.
 */
static void
get_thread_area(kernel_ulong_t ptr_val, const char *ptr_str, bool valid,
		long set_rc, kernel_ulong_t expected)
{
	struct user_desc *ptr = (struct user_desc *) (uintptr_t) ptr_val;
	struct user_desc *expected_ptr =
		(struct user_desc *) (uintptr_t) expected;
	int saved_errno;
	long rc;

	rc = syscall(__NR_get_thread_area, ptr_val);
	saved_errno = errno;

	printf("get_thread_area(");

	if (valid && !rc) {
		if (!set_rc) {
			assert(ptr->entry_number == expected_ptr->entry_number);
			assert(ptr->base_addr    == expected_ptr->base_addr);
			assert(ptr->limit        == expected_ptr->limit);
			assert(ptr->seg_32bit    == expected_ptr->seg_32bit);
			assert(ptr->contents     == expected_ptr->contents);
			assert(ptr->read_exec_only ==
			       expected_ptr->read_exec_only);
			assert(ptr->limit_in_pages ==
			       expected_ptr->limit_in_pages);
			assert(ptr->seg_not_present ==
			       expected_ptr->seg_not_present);
			assert(ptr->useable      == expected_ptr->useable);
			/*
			 * We do not check lm as 32-bit processes ignore it, and
			 * only 32-bit processes can successfully execute
			 * get_thread_area.
			 */
		}

		print_user_desc(ptr,
				(int) ptr->entry_number == -1 ? "-1" : NULL);
	} else {
		printptr(ptr_val, ptr_str);
	}

	errno = saved_errno;
	printf(") = %s\n", sprintrc(rc));
}

int main(void)
{
	TAIL_ALLOC_OBJECT_CONST_PTR(struct user_desc, ta1);
	TAIL_ALLOC_OBJECT_CONST_PTR(struct user_desc, ta2);
	TAIL_ALLOC_OBJECT_CONST_PTR(unsigned int, bogus_entry_number);

	long set_rc = -1;

	/*
	 * Let's do some weird syscall, it will mark the beginning of our
	 * expected output.
	 */
	syscall(__NR_reboot, 0, 0, 0, 0);

	set_rc = set_thread_area((uintptr_t) ARG_STR(NULL), false, NULL);
	get_thread_area((uintptr_t) ARG_STR(NULL), false, set_rc,
			(uintptr_t) NULL);

	set_rc = set_thread_area(-1, NULL, false, NULL);
	get_thread_area(-1, NULL, false, set_rc, -1);

	fill_memory(ta1, sizeof(*ta1));
	fill_memory_ex(ta2, sizeof(*ta2), 0xA5, 0x5A);

	set_thread_area((uintptr_t) (ta1 + 1), NULL, false, NULL);

	set_thread_area((uintptr_t) bogus_entry_number, NULL, false, NULL);

	set_thread_area((uintptr_t) ta1, NULL, true, NULL);

	ta1->entry_number = -1;
	ta1->base_addr = 0;
	ta1->limit = 0;
	ta1->contents = 1;
	ta1->seg_32bit = 1;
	ta1->seg_not_present = 0;

	set_rc = set_thread_area((uintptr_t) ta1, NULL, true, "-1");

	*bogus_entry_number = 2718281828U;
	get_thread_area((uintptr_t) bogus_entry_number,
			"{entry_number=2718281828, ...}",
			false, set_rc, (uintptr_t) ta1);

	/* That one should return -EFAULT on i386 */
	*bogus_entry_number = 12;
	get_thread_area((uintptr_t) bogus_entry_number,
			"{entry_number=12, ...}",
			false, set_rc, (uintptr_t) ta1);

	ta2->entry_number = 3141592653U;
	get_thread_area((uintptr_t) ta2, "{entry_number=3141592653, ...}",
			false, set_rc, (uintptr_t) ta1);

	ta2->entry_number = -1;
	get_thread_area((uintptr_t) ta2, "{entry_number=-1, ...}",
			false, set_rc, (uintptr_t) ta1);

	ta2->entry_number = ta1->entry_number;
	assert(set_rc == 0 || (int) ta2->entry_number == -1);
	get_thread_area((uintptr_t) ta2, "{entry_number=-1, ...}",
			true, set_rc, (uintptr_t) ta1);

	puts("+++ exited with 0 +++");

	return 0;
}

#else

SKIP_MAIN_UNDEFINED("__NR_get_thread_area && __NR_set_thread_area"
		    " && HAVE_STRUCT_USER_DESC");

#endif