1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci *
4f08c3bdfSopenharmony_ci *   Copyright (c) Linux Test Project, 2016
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*
8f08c3bdfSopenharmony_ci * Test Description:
9f08c3bdfSopenharmony_ci *   Verify writev() behaviour with partially valid iovec list.
10f08c3bdfSopenharmony_ci *   Kernel <4.8 used to shorten write up to first bad invalid
11f08c3bdfSopenharmony_ci *   iovec. Starting with 4.8, a writev with short data (under
12f08c3bdfSopenharmony_ci *   page size) is likely to get shorten to 0 bytes and return
13f08c3bdfSopenharmony_ci *   EFAULT.
14f08c3bdfSopenharmony_ci *
15f08c3bdfSopenharmony_ci *   This test doesn't make assumptions how much will write get
16f08c3bdfSopenharmony_ci *   shortened. It only tests that file content/offset after
17f08c3bdfSopenharmony_ci *   syscall corresponds to return value of writev().
18f08c3bdfSopenharmony_ci *
19f08c3bdfSopenharmony_ci *   See: [RFC] writev() semantics with invalid iovec in the middle
20f08c3bdfSopenharmony_ci *        https://marc.info/?l=linux-kernel&m=147388891614289&w=2
21f08c3bdfSopenharmony_ci */
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_ci#include <errno.h>
24f08c3bdfSopenharmony_ci#include <fcntl.h>
25f08c3bdfSopenharmony_ci#include <stdio.h>
26f08c3bdfSopenharmony_ci#include <sys/mman.h>
27f08c3bdfSopenharmony_ci#include <sys/stat.h>
28f08c3bdfSopenharmony_ci#include <sys/types.h>
29f08c3bdfSopenharmony_ci#include <sys/uio.h>
30f08c3bdfSopenharmony_ci#include "tst_test.h"
31f08c3bdfSopenharmony_ci
32f08c3bdfSopenharmony_ci#define TESTFILE "testfile"
33f08c3bdfSopenharmony_ci#define CHUNK 64
34f08c3bdfSopenharmony_ci#define BUFSIZE (CHUNK * 4)
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_cistatic void *bad_addr;
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_cistatic void test_partially_valid_iovec(int initial_file_offset)
39f08c3bdfSopenharmony_ci{
40f08c3bdfSopenharmony_ci	int i, fd;
41f08c3bdfSopenharmony_ci	unsigned char buffer[BUFSIZE], fpattern[BUFSIZE], tmp[BUFSIZE];
42f08c3bdfSopenharmony_ci	long off_after;
43f08c3bdfSopenharmony_ci	struct iovec wr_iovec[] = {
44f08c3bdfSopenharmony_ci		{ buffer, CHUNK },
45f08c3bdfSopenharmony_ci		{ bad_addr, CHUNK },
46f08c3bdfSopenharmony_ci		{ buffer + CHUNK, CHUNK },
47f08c3bdfSopenharmony_ci		{ buffer + CHUNK * 2, CHUNK },
48f08c3bdfSopenharmony_ci	};
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_ci	tst_res(TINFO, "starting test with initial file offset: %d ",
51f08c3bdfSopenharmony_ci		initial_file_offset);
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_ci	for (i = 0; i < BUFSIZE; i++)
54f08c3bdfSopenharmony_ci		buffer[i] = i % (CHUNK - 1);
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_ci	memset(fpattern, 0xff, BUFSIZE);
57f08c3bdfSopenharmony_ci	tst_fill_file(TESTFILE, 0xff, CHUNK, BUFSIZE / CHUNK);
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_ci	fd = SAFE_OPEN(TESTFILE, O_RDWR, 0644);
60f08c3bdfSopenharmony_ci	SAFE_LSEEK(fd, initial_file_offset, SEEK_SET);
61f08c3bdfSopenharmony_ci	TEST(writev(fd, wr_iovec, ARRAY_SIZE(wr_iovec)));
62f08c3bdfSopenharmony_ci	off_after = (long) SAFE_LSEEK(fd, 0, SEEK_CUR);
63f08c3bdfSopenharmony_ci
64f08c3bdfSopenharmony_ci	/* bad errno */
65f08c3bdfSopenharmony_ci	if (TST_RET == -1 && TST_ERR != EFAULT) {
66f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "unexpected errno");
67f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd);
68f08c3bdfSopenharmony_ci		return;
69f08c3bdfSopenharmony_ci	}
70f08c3bdfSopenharmony_ci
71f08c3bdfSopenharmony_ci	/* nothing has been written */
72f08c3bdfSopenharmony_ci	if (TST_RET == -1 && TST_ERR == EFAULT) {
73f08c3bdfSopenharmony_ci		tst_res(TINFO, "got EFAULT");
74f08c3bdfSopenharmony_ci		/* initial file content remains untouched */
75f08c3bdfSopenharmony_ci		SAFE_LSEEK(fd, 0, SEEK_SET);
76f08c3bdfSopenharmony_ci		SAFE_READ(1, fd, tmp, BUFSIZE);
77f08c3bdfSopenharmony_ci		if (memcmp(tmp, fpattern, BUFSIZE))
78f08c3bdfSopenharmony_ci			tst_res(TFAIL, "file was written to");
79f08c3bdfSopenharmony_ci		else
80f08c3bdfSopenharmony_ci			tst_res(TPASS, "file stayed untouched");
81f08c3bdfSopenharmony_ci
82f08c3bdfSopenharmony_ci		/* offset hasn't changed */
83f08c3bdfSopenharmony_ci		if (off_after == initial_file_offset)
84f08c3bdfSopenharmony_ci			tst_res(TPASS, "offset stayed unchanged");
85f08c3bdfSopenharmony_ci		else
86f08c3bdfSopenharmony_ci			tst_res(TFAIL, "offset changed to %ld",
87f08c3bdfSopenharmony_ci				off_after);
88f08c3bdfSopenharmony_ci
89f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd);
90f08c3bdfSopenharmony_ci		return;
91f08c3bdfSopenharmony_ci	}
92f08c3bdfSopenharmony_ci
93f08c3bdfSopenharmony_ci	/* writev() wrote more bytes than bytes preceding invalid iovec */
94f08c3bdfSopenharmony_ci	tst_res(TINFO, "writev() has written %ld bytes", TST_RET);
95f08c3bdfSopenharmony_ci	if (TST_RET > (long) wr_iovec[0].iov_len) {
96f08c3bdfSopenharmony_ci		tst_res(TFAIL, "writev wrote more than expected");
97f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd);
98f08c3bdfSopenharmony_ci		return;
99f08c3bdfSopenharmony_ci	}
100f08c3bdfSopenharmony_ci
101f08c3bdfSopenharmony_ci	/* file content matches written bytes */
102f08c3bdfSopenharmony_ci	SAFE_LSEEK(fd, initial_file_offset, SEEK_SET);
103f08c3bdfSopenharmony_ci	SAFE_READ(1, fd, tmp, TST_RET);
104f08c3bdfSopenharmony_ci	if (memcmp(tmp, wr_iovec[0].iov_base, TST_RET) == 0) {
105f08c3bdfSopenharmony_ci		tst_res(TPASS, "file has expected content");
106f08c3bdfSopenharmony_ci	} else {
107f08c3bdfSopenharmony_ci		tst_res(TFAIL, "file has unexpected content");
108f08c3bdfSopenharmony_ci		tst_res_hexd(TFAIL, wr_iovec[0].iov_base, TST_RET,
109f08c3bdfSopenharmony_ci				"expected:");
110f08c3bdfSopenharmony_ci		tst_res_hexd(TFAIL, tmp, TST_RET,
111f08c3bdfSopenharmony_ci				"actual file content:");
112f08c3bdfSopenharmony_ci	}
113f08c3bdfSopenharmony_ci
114f08c3bdfSopenharmony_ci	/* file offset has been updated according to written bytes */
115f08c3bdfSopenharmony_ci	if (off_after == initial_file_offset + TST_RET)
116f08c3bdfSopenharmony_ci		tst_res(TPASS, "offset at %ld as expected", off_after);
117f08c3bdfSopenharmony_ci	else
118f08c3bdfSopenharmony_ci		tst_res(TFAIL, "offset unexpected %ld", off_after);
119f08c3bdfSopenharmony_ci
120f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd);
121f08c3bdfSopenharmony_ci}
122f08c3bdfSopenharmony_ci
123f08c3bdfSopenharmony_cistatic void test_writev(void)
124f08c3bdfSopenharmony_ci{
125f08c3bdfSopenharmony_ci	test_partially_valid_iovec(0);
126f08c3bdfSopenharmony_ci	test_partially_valid_iovec(CHUNK + 1);
127f08c3bdfSopenharmony_ci	test_partially_valid_iovec(getpagesize());
128f08c3bdfSopenharmony_ci	test_partially_valid_iovec(getpagesize() + 1);
129f08c3bdfSopenharmony_ci}
130f08c3bdfSopenharmony_ci
131f08c3bdfSopenharmony_cistatic void setup(void)
132f08c3bdfSopenharmony_ci{
133f08c3bdfSopenharmony_ci	bad_addr = SAFE_MMAP(0, 1, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
134f08c3bdfSopenharmony_ci			0, 0);
135f08c3bdfSopenharmony_ci}
136f08c3bdfSopenharmony_ci
137f08c3bdfSopenharmony_cistatic struct tst_test test = {
138f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
139f08c3bdfSopenharmony_ci	.setup = setup,
140f08c3bdfSopenharmony_ci	.test_all = test_writev,
141f08c3bdfSopenharmony_ci};
142