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