1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines  Corp., 2012
4f08c3bdfSopenharmony_ci * Copyright (c) Linux Test Project, 2012
5f08c3bdfSopenharmony_ci * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
6f08c3bdfSopenharmony_ci */
7f08c3bdfSopenharmony_ci
8f08c3bdfSopenharmony_ci/*\
9f08c3bdfSopenharmony_ci * [Description]
10f08c3bdfSopenharmony_ci *
11f08c3bdfSopenharmony_ci * Fork two children, the first one allocates a chunk of memory and the
12f08c3bdfSopenharmony_ci * other one call process_vm_writev to write known data into the first
13f08c3bdfSopenharmony_ci * child. Then first child verifies that the data is as expected.
14f08c3bdfSopenharmony_ci */
15f08c3bdfSopenharmony_ci
16f08c3bdfSopenharmony_ci#include <stdlib.h>
17f08c3bdfSopenharmony_ci#include <sys/types.h>
18f08c3bdfSopenharmony_ci#include <sys/uio.h>
19f08c3bdfSopenharmony_ci#include "tst_test.h"
20f08c3bdfSopenharmony_ci#include "lapi/syscalls.h"
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_cistatic uintptr_t *data_ptr;
23f08c3bdfSopenharmony_cistatic char *str_buffsize;
24f08c3bdfSopenharmony_cistatic int bufsize = 100000;
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_cistatic void child_alloc_and_verify(int buffsize)
27f08c3bdfSopenharmony_ci{
28f08c3bdfSopenharmony_ci	char foo[buffsize];
29f08c3bdfSopenharmony_ci	int i;
30f08c3bdfSopenharmony_ci	int err;
31f08c3bdfSopenharmony_ci
32f08c3bdfSopenharmony_ci	tst_res(TINFO, "child 0: allocate memory");
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_ci	memset(foo, 'a', buffsize);
35f08c3bdfSopenharmony_ci	*data_ptr = (uintptr_t)foo;
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_ci	TST_CHECKPOINT_WAKE_AND_WAIT(0);
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_ci	err = 0;
40f08c3bdfSopenharmony_ci	for (i = 0; i < buffsize; i++)
41f08c3bdfSopenharmony_ci		if (foo[i] != 'w')
42f08c3bdfSopenharmony_ci			err++;
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_ci	if (err)
45f08c3bdfSopenharmony_ci		tst_res(TFAIL, "child 0: found %d differences from expected data", err);
46f08c3bdfSopenharmony_ci	else
47f08c3bdfSopenharmony_ci		tst_res(TPASS, "child 0: read back expected data");
48f08c3bdfSopenharmony_ci}
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_cistatic void child_write(int buffsize, pid_t pid_alloc)
51f08c3bdfSopenharmony_ci{
52f08c3bdfSopenharmony_ci	char lp[buffsize];
53f08c3bdfSopenharmony_ci	struct iovec local, remote;
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_ci	tst_res(TINFO, "child 1: write to the same memory location");
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ci	memset(lp, 'w', buffsize);
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_ci	local.iov_base = lp;
60f08c3bdfSopenharmony_ci	local.iov_len = buffsize;
61f08c3bdfSopenharmony_ci	remote.iov_base = (void *)*data_ptr;
62f08c3bdfSopenharmony_ci	remote.iov_len = buffsize;
63f08c3bdfSopenharmony_ci
64f08c3bdfSopenharmony_ci	TST_EXP_POSITIVE(tst_syscall(__NR_process_vm_writev, pid_alloc, &local,
65f08c3bdfSopenharmony_ci				     1UL, &remote, 1UL, 0UL));
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_ci	if (TST_RET != buffsize) {
68f08c3bdfSopenharmony_ci		tst_brk(TBROK, "process_vm_writev: expected %d bytes but got %ld",
69f08c3bdfSopenharmony_ci			buffsize, TST_RET);
70f08c3bdfSopenharmony_ci	}
71f08c3bdfSopenharmony_ci}
72f08c3bdfSopenharmony_ci
73f08c3bdfSopenharmony_cistatic void setup(void)
74f08c3bdfSopenharmony_ci{
75f08c3bdfSopenharmony_ci	tst_syscall(__NR_process_vm_writev, getpid(), NULL, 0UL, NULL, 0UL, 0UL);
76f08c3bdfSopenharmony_ci
77f08c3bdfSopenharmony_ci	if (tst_parse_int(str_buffsize, &bufsize, 1, INT_MAX))
78f08c3bdfSopenharmony_ci		tst_brk(TBROK, "Invalid buffer size '%s'", str_buffsize);
79f08c3bdfSopenharmony_ci
80f08c3bdfSopenharmony_ci	data_ptr = SAFE_MMAP(NULL, sizeof(uintptr_t), PROT_READ | PROT_WRITE,
81f08c3bdfSopenharmony_ci			     MAP_SHARED | MAP_ANONYMOUS, -1, 0);
82f08c3bdfSopenharmony_ci}
83f08c3bdfSopenharmony_ci
84f08c3bdfSopenharmony_cistatic void cleanup(void)
85f08c3bdfSopenharmony_ci{
86f08c3bdfSopenharmony_ci	if (data_ptr)
87f08c3bdfSopenharmony_ci		SAFE_MUNMAP(data_ptr, sizeof(uintptr_t));
88f08c3bdfSopenharmony_ci}
89f08c3bdfSopenharmony_ci
90f08c3bdfSopenharmony_cistatic void run(void)
91f08c3bdfSopenharmony_ci{
92f08c3bdfSopenharmony_ci	pid_t pid_alloc;
93f08c3bdfSopenharmony_ci	pid_t pid_write;
94f08c3bdfSopenharmony_ci	int status;
95f08c3bdfSopenharmony_ci
96f08c3bdfSopenharmony_ci	pid_alloc = SAFE_FORK();
97f08c3bdfSopenharmony_ci	if (!pid_alloc) {
98f08c3bdfSopenharmony_ci		child_alloc_and_verify(bufsize);
99f08c3bdfSopenharmony_ci		return;
100f08c3bdfSopenharmony_ci	}
101f08c3bdfSopenharmony_ci
102f08c3bdfSopenharmony_ci	TST_CHECKPOINT_WAIT(0);
103f08c3bdfSopenharmony_ci
104f08c3bdfSopenharmony_ci	pid_write = SAFE_FORK();
105f08c3bdfSopenharmony_ci	if (!pid_write) {
106f08c3bdfSopenharmony_ci		child_write(bufsize, pid_alloc);
107f08c3bdfSopenharmony_ci		return;
108f08c3bdfSopenharmony_ci	}
109f08c3bdfSopenharmony_ci
110f08c3bdfSopenharmony_ci	SAFE_WAITPID(pid_write, &status, 0);
111f08c3bdfSopenharmony_ci	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
112f08c3bdfSopenharmony_ci		tst_res(TFAIL, "write child: %s", tst_strstatus(status));
113f08c3bdfSopenharmony_ci
114f08c3bdfSopenharmony_ci	TST_CHECKPOINT_WAKE(0);
115f08c3bdfSopenharmony_ci}
116f08c3bdfSopenharmony_ci
117f08c3bdfSopenharmony_cistatic struct tst_test test = {
118f08c3bdfSopenharmony_ci	.test_all = run,
119f08c3bdfSopenharmony_ci	.setup = setup,
120f08c3bdfSopenharmony_ci	.cleanup = cleanup,
121f08c3bdfSopenharmony_ci	.forks_child = 1,
122f08c3bdfSopenharmony_ci	.needs_checkpoints = 1,
123f08c3bdfSopenharmony_ci	.options =
124f08c3bdfSopenharmony_ci		(struct tst_option[]){
125f08c3bdfSopenharmony_ci			{ "s:", &str_buffsize, "Total buffer size (default 100000)" },
126f08c3bdfSopenharmony_ci			{},
127f08c3bdfSopenharmony_ci		},
128f08c3bdfSopenharmony_ci};
129