summaryrefslogtreecommitdiff
path: root/tests/process_vm_readv_writev.c
blob: 9309135a6b50394cd8049c452a5d2925a4a355b4 (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
 * Check decoding of process_vm_readv/process_vm_writev syscall.
 *
 * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr@gmail.com>
 * Copyright (c) 2016-2018 The strace developers.
 * All rights reserved.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include <inttypes.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/uio.h>

#if OP_WR
# define in_iovec  rmt_iovec
# define out_iovec lcl_iovec
# define in_iov    rmt_iov
# define out_iov   lcl_iov
#else
# define in_iovec  lcl_iovec
# define out_iovec rmt_iovec
# define in_iov    lcl_iov
# define out_iov   rmt_iov
#endif

typedef void (*iov_print_fn)(const struct iovec *, const void *, long);

enum { MAX_SEGM_COUNT = 2, MAX_STR_LEN = 5 };

struct print_iov_arg {
	uint32_t count;
	uint32_t valid    :1,
		 string   :1,
		 addr_term:1,
		 check_rc :1;
	uint32_t str_segms;
	uint8_t  str_base[MAX_SEGM_COUNT];
	uint8_t  str_size[MAX_SEGM_COUNT];
};

static void
print_iov(const struct iovec *iov, const void *arg_ptr, long rc)
{
	const struct print_iov_arg *arg = arg_ptr;
	uint32_t i;
	uint32_t num_segm = 0;
	uint64_t segm_offs = 0;

	if (!arg || !arg->valid) {
		if (iov)
			printf("%p", iov);
		else
			printf("NULL");

		return;
	}

	printf("[");

	for (i = 0; i < arg->count; i++) {
		if (i)
			printf(", ");

		if (i >= MAX_STR_LEN) {
			printf("...");
			break;
		}

		printf("{iov_base=");
		if (arg->string && (!arg->check_rc || (rc != -1))) {
			uint64_t str_left = iov[i].iov_len;
			uint64_t pr_count = 0;

			printf("\"");

			while (str_left--) {
				static const char oct_str[] = "01234567";
				uint8_t c = arg->str_base[num_segm] + segm_offs;

				if ((num_segm >= arg->str_segms) ||
				    (num_segm >= MAX_SEGM_COUNT))
					error_msg_and_fail("print_iov: segment "
							   "count overrun");

				if (pr_count++ < MAX_STR_LEN)
					printf("\\%.1s%.1s%d",
					       (c >> 6) ?
					       oct_str + (c >> 6) : "",
					       (c >> 3) ?
					       oct_str + ((c >> 3) & 7) : "",
					       c & 7);

				segm_offs++;

				if (segm_offs >= arg->str_size[num_segm]) {
					num_segm++;
					segm_offs = 0;
				}
			}

			printf("\"");

			if (pr_count > MAX_STR_LEN)
				printf("...");
		} else {
			if (iov[i].iov_base)
				printf("%p", iov[i].iov_base);
			else
				printf("NULL");
		}

		printf(", iov_len=%zu}", iov[i].iov_len);
	}

	if (arg->addr_term)
		printf(", ... /* %p */", iov + arg->count);

	printf("]");
}

static void
do_call(kernel_ulong_t pid,
	kernel_ulong_t local_iov, const char *local_arg,
	kernel_ulong_t liovcnt,
	kernel_ulong_t remote_iov, const char *remote_arg,
	kernel_ulong_t riovcnt,
	kernel_ulong_t flags, iov_print_fn pr_iov)
{
	long rc;
	const char *errstr;

	rc = syscall(OP_NR, pid, local_iov, liovcnt, remote_iov, riovcnt,
		flags);
	errstr = sprintrc(rc);

	printf("%s(%d, ", OP_STR, (int) pid);

	if (pr_iov)
		pr_iov((const struct iovec *) (uintptr_t) local_iov, local_arg,
			rc);
	else
		printf("%s", local_arg);

	printf(", %lu, ", (unsigned long) liovcnt);

	if (pr_iov)
		pr_iov((const struct iovec *) (uintptr_t) remote_iov,
		       remote_arg, rc);
	else
		printf("%s", remote_arg);

	printf(", %lu, %lu) = %s\n", (unsigned long) riovcnt,
		(unsigned long) flags, errstr);
}

kernel_ulong_t
ptr_cast(void *ptr)
{
	return (kernel_ulong_t) (uintptr_t) ptr;
}

int
main(void)
{
	enum {
		SIZE_11 = 2,
		SIZE_12 = 3,
		SIZE_13 = 4,
		SIZE_1 = SIZE_11 + SIZE_12 + SIZE_13,
		SIZE_21 = 5,
		SIZE_22 = 6,
		SIZE_23 = 7,
		SIZE_2 = SIZE_21 + SIZE_22 + SIZE_23,
	};

	enum {
		SEGM1_BASE = 0x80,
		SEGM2_BASE = 0xA0,
	};

	static const kernel_ulong_t bogus_pid =
		(kernel_ulong_t) 0xbadfaceddeadca57ULL;
	static const kernel_ulong_t bogus_iovcnt1 =
		(kernel_ulong_t) 0xdec0ded1defaced2ULL;
	static const kernel_ulong_t bogus_iovcnt2 =
		(kernel_ulong_t) 0xdec0ded3defaced4ULL;
	static const kernel_ulong_t bogus_flags =
		(kernel_ulong_t) 0xdeadc0deda7adeadULL;

	pid_t my_pid = getpid();
	char *data1_out = tail_alloc(SIZE_1);
	char *data2_out = tail_alloc(SIZE_2);
	char *data1_in  = tail_alloc(SIZE_2);
	char *data2_in  = tail_alloc(SIZE_1);

	struct iovec bogus_iovec[] = {
		{ data1_out + SIZE_1, (size_t) 0xdeadfaceca57beefULL },
		{ data1_in  + SIZE_2, (size_t) 0xbadc0dedda7adeadULL },
		{ data2_out + SIZE_2, (size_t) 0xf157facedec0ded1ULL },
		{ data2_in  + SIZE_1, (size_t) 0xdefaced2bea7be57ULL },
	};

	struct iovec out_iovec[] = {
		{ data1_out,  SIZE_11 },
		{ data1_out + SIZE_11,  SIZE_12 },
		{ data1_out + SIZE_11 + SIZE_12,  SIZE_13 },
		{ data2_out,  SIZE_21 },
		{ data2_out + SIZE_21,  SIZE_22 },
		{ data2_out + SIZE_21 + SIZE_22,  SIZE_23 },
	};
	struct iovec in_iovec[] = {
		{ data1_in,  SIZE_23 },
		{ data1_in + SIZE_23,  SIZE_22 },
		{ data1_in + SIZE_23 + SIZE_22,  SIZE_21 },
		{ data2_in,  SIZE_13 },
		{ data2_in + SIZE_13,  SIZE_12 },
		{ data2_in + SIZE_13 + SIZE_12,  SIZE_11 },
	};

	struct iovec *bogus_iov = tail_memdup(bogus_iovec, sizeof(bogus_iovec));
	struct iovec *lcl_iov   = tail_memdup(lcl_iovec,   sizeof(lcl_iovec));
	struct iovec *rmt_iov   = tail_memdup(rmt_iovec,   sizeof(rmt_iovec));

	struct print_iov_arg bogus_arg   = { ARRAY_SIZE(bogus_iovec), 1 };
	struct print_iov_arg lcl_arg     = { ARRAY_SIZE(lcl_iovec), 1, 1, 0, 0,
		2, {SEGM1_BASE, SEGM2_BASE}, {SIZE_1, SIZE_2} };
	struct print_iov_arg rmt_arg     = { ARRAY_SIZE(rmt_iovec), 1 };

	struct print_iov_arg bogus_arg_cut = {
		ARRAY_SIZE(bogus_iovec) - 2, 1, 0, 1
	};
	struct print_iov_arg lcl_arg_cut = {
		ARRAY_SIZE(lcl_iovec) - 2, 1, 1, 1, 0, 2,
		{ SEGM1_BASE + SIZE_11 + SIZE_12, SEGM2_BASE },
		{SIZE_13, SIZE_2}
	};
	struct print_iov_arg rmt_arg_cut = { ARRAY_SIZE(rmt_iovec) - 2, 1 };


	fill_memory_ex(data1_out, SIZE_1, SEGM1_BASE, SIZE_1);
	fill_memory_ex(data2_out, SIZE_2, SEGM2_BASE, SIZE_2);


	do_call(bogus_pid, (kernel_ulong_t) (uintptr_t) ARG_STR(NULL),
		bogus_iovcnt1, (kernel_ulong_t) (uintptr_t) ARG_STR(NULL),
		bogus_iovcnt2, bogus_flags, NULL);

	do_call(my_pid, ptr_cast(bogus_iov + ARRAY_SIZE(bogus_iovec)),
		"[]", 0, ptr_cast(in_iov + ARRAY_SIZE(in_iovec)), "[]",
		0, 0, NULL);
	do_call(my_pid, ptr_cast(bogus_iov + ARRAY_SIZE(bogus_iovec)), NULL,
		bogus_iovcnt1, ptr_cast(in_iov + ARRAY_SIZE(in_iovec)), NULL,
		bogus_iovcnt2, 0, print_iov);

	do_call(my_pid, ptr_cast(bogus_iov), (char *) &bogus_arg,
		ARRAY_SIZE(bogus_iovec), ptr_cast(rmt_iov + 2),
		(char *) &rmt_arg_cut, ARRAY_SIZE(rmt_iovec) - 2, 0, print_iov);

#if !OP_WR
	lcl_arg.check_rc = 1;
	lcl_arg_cut.check_rc = 1;
#endif

	do_call(my_pid, ptr_cast(lcl_iov + 2), (char *) &lcl_arg_cut,
		ARRAY_SIZE(lcl_iovec) - 1, ptr_cast(bogus_iov + 2),
		(char *) &bogus_arg_cut, ARRAY_SIZE(bogus_iovec) - 1, 0,
		print_iov);

	lcl_arg_cut.addr_term = 0;

	rmt_arg_cut.addr_term = 1;
	rmt_arg_cut.count = 5;

	do_call(my_pid, ptr_cast(lcl_iov + 2), (char *) &lcl_arg_cut,
		ARRAY_SIZE(lcl_iovec) - 2, ptr_cast(rmt_iov + 1),
		(char *) &rmt_arg_cut, ARRAY_SIZE(rmt_iovec), 0, print_iov);

	/* Correct call */
	do_call(my_pid, ptr_cast(lcl_iov), (char *) &lcl_arg,
		ARRAY_SIZE(lcl_iovec), ptr_cast(rmt_iov), (char *) &rmt_arg,
		ARRAY_SIZE(rmt_iovec), 0, print_iov);

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

	return 0;
}