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, one child mallocs randomly sized trunks of memory 12f08c3bdfSopenharmony_ci * and initializes them; the other child calls process_vm_readv with 13f08c3bdfSopenharmony_ci * the remote iovecs initialized to the original process memory 14f08c3bdfSopenharmony_ci * locations and the local iovecs initialized to randomly sized and 15f08c3bdfSopenharmony_ci * allocated local memory locations. The second child then verifies 16f08c3bdfSopenharmony_ci * that the data is copied correctly. 17f08c3bdfSopenharmony_ci */ 18f08c3bdfSopenharmony_ci 19f08c3bdfSopenharmony_ci#include <stdio.h> 20f08c3bdfSopenharmony_ci#include <stdlib.h> 21f08c3bdfSopenharmony_ci#include <sys/types.h> 22f08c3bdfSopenharmony_ci#include <sys/wait.h> 23f08c3bdfSopenharmony_ci#include "tst_test.h" 24f08c3bdfSopenharmony_ci#include "lapi/syscalls.h" 25f08c3bdfSopenharmony_ci 26f08c3bdfSopenharmony_ci#define MAX_IOVECS 1024 27f08c3bdfSopenharmony_ci 28f08c3bdfSopenharmony_cistatic struct tcase { 29f08c3bdfSopenharmony_ci int bufsize; 30f08c3bdfSopenharmony_ci int remote_iovecs; 31f08c3bdfSopenharmony_ci int local_iovecs; 32f08c3bdfSopenharmony_ci} testcases[] = { 33f08c3bdfSopenharmony_ci { .bufsize = 1024, .remote_iovecs = 1024, .local_iovecs = 8 }, 34f08c3bdfSopenharmony_ci { .bufsize = 1024, .remote_iovecs = 512, .local_iovecs = 16 }, 35f08c3bdfSopenharmony_ci { .bufsize = 1024, .remote_iovecs = 256, .local_iovecs = 32 }, 36f08c3bdfSopenharmony_ci { .bufsize = 1024, .remote_iovecs = 128, .local_iovecs = 64 }, 37f08c3bdfSopenharmony_ci { .bufsize = 1024, .remote_iovecs = 64, .local_iovecs = 128 }, 38f08c3bdfSopenharmony_ci { .bufsize = 1024, .remote_iovecs = 32, .local_iovecs = 256 }, 39f08c3bdfSopenharmony_ci { .bufsize = 1024, .remote_iovecs = 16, .local_iovecs = 512 }, 40f08c3bdfSopenharmony_ci { .bufsize = 1024, .remote_iovecs = 8, .local_iovecs = 1024 }, 41f08c3bdfSopenharmony_ci 42f08c3bdfSopenharmony_ci { .bufsize = 131072, .remote_iovecs = 1024, .local_iovecs = 8 }, 43f08c3bdfSopenharmony_ci { .bufsize = 131072, .remote_iovecs = 512, .local_iovecs = 16 }, 44f08c3bdfSopenharmony_ci { .bufsize = 131072, .remote_iovecs = 256, .local_iovecs = 32 }, 45f08c3bdfSopenharmony_ci { .bufsize = 131072, .remote_iovecs = 128, .local_iovecs = 64 }, 46f08c3bdfSopenharmony_ci { .bufsize = 131072, .remote_iovecs = 64, .local_iovecs = 128 }, 47f08c3bdfSopenharmony_ci { .bufsize = 131072, .remote_iovecs = 32, .local_iovecs = 256 }, 48f08c3bdfSopenharmony_ci { .bufsize = 131072, .remote_iovecs = 16, .local_iovecs = 512 }, 49f08c3bdfSopenharmony_ci { .bufsize = 131072, .remote_iovecs = 8, .local_iovecs = 1024 }, 50f08c3bdfSopenharmony_ci}; 51f08c3bdfSopenharmony_ci 52f08c3bdfSopenharmony_cistatic char **data_ptr; 53f08c3bdfSopenharmony_ci 54f08c3bdfSopenharmony_cistatic void create_data_size(int *arr, int arr_sz, int buffsize) 55f08c3bdfSopenharmony_ci{ 56f08c3bdfSopenharmony_ci long bufsz_left; 57f08c3bdfSopenharmony_ci int i; 58f08c3bdfSopenharmony_ci 59f08c3bdfSopenharmony_ci bufsz_left = buffsize; 60f08c3bdfSopenharmony_ci for (i = 0; i < arr_sz - 1; i++) { 61f08c3bdfSopenharmony_ci arr[i] = rand() % ((bufsz_left / 2) + 1); 62f08c3bdfSopenharmony_ci bufsz_left -= arr[i]; 63f08c3bdfSopenharmony_ci } 64f08c3bdfSopenharmony_ci 65f08c3bdfSopenharmony_ci arr[arr_sz - 1] = bufsz_left; 66f08c3bdfSopenharmony_ci} 67f08c3bdfSopenharmony_ci 68f08c3bdfSopenharmony_cistatic void child_alloc(const int *sizes, int nr_iovecs) 69f08c3bdfSopenharmony_ci{ 70f08c3bdfSopenharmony_ci int i, j; 71f08c3bdfSopenharmony_ci long count; 72f08c3bdfSopenharmony_ci 73f08c3bdfSopenharmony_ci count = 0; 74f08c3bdfSopenharmony_ci for (i = 0; i < nr_iovecs; i++) { 75f08c3bdfSopenharmony_ci data_ptr[i] = sizes[i] ? SAFE_MALLOC(sizes[i]) : NULL; 76f08c3bdfSopenharmony_ci 77f08c3bdfSopenharmony_ci for (j = 0; j < sizes[i]; j++) { 78f08c3bdfSopenharmony_ci data_ptr[i][j] = count % 256; 79f08c3bdfSopenharmony_ci count++; 80f08c3bdfSopenharmony_ci } 81f08c3bdfSopenharmony_ci } 82f08c3bdfSopenharmony_ci 83f08c3bdfSopenharmony_ci tst_res(TINFO, "child_alloc: memory allocated and initialized"); 84f08c3bdfSopenharmony_ci 85f08c3bdfSopenharmony_ci TST_CHECKPOINT_WAKE_AND_WAIT(0); 86f08c3bdfSopenharmony_ci} 87f08c3bdfSopenharmony_ci 88f08c3bdfSopenharmony_cistatic void child_read(const int *sizes, int local_iovecs, int remote_iovecs, 89f08c3bdfSopenharmony_ci pid_t pid_alloc, int buffsize) 90f08c3bdfSopenharmony_ci{ 91f08c3bdfSopenharmony_ci struct iovec local[local_iovecs]; 92f08c3bdfSopenharmony_ci struct iovec remote[remote_iovecs]; 93f08c3bdfSopenharmony_ci int i, j; 94f08c3bdfSopenharmony_ci int count; 95f08c3bdfSopenharmony_ci int nr_error; 96f08c3bdfSopenharmony_ci int local_sizes[local_iovecs]; 97f08c3bdfSopenharmony_ci unsigned char expect, actual; 98f08c3bdfSopenharmony_ci 99f08c3bdfSopenharmony_ci for (i = 0; i < remote_iovecs; i++) { 100f08c3bdfSopenharmony_ci remote[i].iov_base = (void *)data_ptr[i]; 101f08c3bdfSopenharmony_ci remote[i].iov_len = sizes[i]; 102f08c3bdfSopenharmony_ci } 103f08c3bdfSopenharmony_ci 104f08c3bdfSopenharmony_ci create_data_size(local_sizes, local_iovecs, buffsize); 105f08c3bdfSopenharmony_ci for (i = 0; i < local_iovecs; i++) { 106f08c3bdfSopenharmony_ci local[i].iov_base = SAFE_MALLOC(local_sizes[i]); 107f08c3bdfSopenharmony_ci local[i].iov_len = local_sizes[i]; 108f08c3bdfSopenharmony_ci } 109f08c3bdfSopenharmony_ci 110f08c3bdfSopenharmony_ci tst_res(TINFO, "child_read: reading string from same memory location"); 111f08c3bdfSopenharmony_ci 112f08c3bdfSopenharmony_ci TST_EXP_POSITIVE(tst_syscall(__NR_process_vm_readv, pid_alloc, local, 113f08c3bdfSopenharmony_ci local_iovecs, remote, remote_iovecs, 0UL), 114f08c3bdfSopenharmony_ci "process_vm_read()"); 115f08c3bdfSopenharmony_ci 116f08c3bdfSopenharmony_ci if (TST_RET != buffsize) { 117f08c3bdfSopenharmony_ci tst_brk(TBROK, "process_vm_readv: expected %d bytes but got %ld", 118f08c3bdfSopenharmony_ci buffsize, TST_RET); 119f08c3bdfSopenharmony_ci } 120f08c3bdfSopenharmony_ci 121f08c3bdfSopenharmony_ci count = 0; 122f08c3bdfSopenharmony_ci nr_error = 0; 123f08c3bdfSopenharmony_ci for (i = 0; i < local_iovecs; i++) { 124f08c3bdfSopenharmony_ci for (j = 0; j < (int)local[i].iov_len; j++) { 125f08c3bdfSopenharmony_ci expect = count % 256; 126f08c3bdfSopenharmony_ci actual = ((unsigned char *)local[i].iov_base)[j]; 127f08c3bdfSopenharmony_ci if (expect != actual) 128f08c3bdfSopenharmony_ci nr_error++; 129f08c3bdfSopenharmony_ci 130f08c3bdfSopenharmony_ci count++; 131f08c3bdfSopenharmony_ci } 132f08c3bdfSopenharmony_ci } 133f08c3bdfSopenharmony_ci 134f08c3bdfSopenharmony_ci if (nr_error) 135f08c3bdfSopenharmony_ci tst_brk(TFAIL, "child_read: %d incorrect bytes received", nr_error); 136f08c3bdfSopenharmony_ci else 137f08c3bdfSopenharmony_ci tst_res(TPASS, "child_read: all bytes are correctly received"); 138f08c3bdfSopenharmony_ci} 139f08c3bdfSopenharmony_ci 140f08c3bdfSopenharmony_cistatic void setup(void) 141f08c3bdfSopenharmony_ci{ 142f08c3bdfSopenharmony_ci tst_syscall(__NR_process_vm_readv, getpid(), NULL, 0UL, NULL, 0UL, 0UL); 143f08c3bdfSopenharmony_ci 144f08c3bdfSopenharmony_ci data_ptr = SAFE_MMAP(NULL, sizeof(void *) * MAX_IOVECS, PROT_READ | PROT_WRITE, 145f08c3bdfSopenharmony_ci MAP_SHARED | MAP_ANONYMOUS, -1, 0); 146f08c3bdfSopenharmony_ci} 147f08c3bdfSopenharmony_ci 148f08c3bdfSopenharmony_cistatic void cleanup(void) 149f08c3bdfSopenharmony_ci{ 150f08c3bdfSopenharmony_ci if (data_ptr) 151f08c3bdfSopenharmony_ci SAFE_MUNMAP(data_ptr, sizeof(void *) * MAX_IOVECS); 152f08c3bdfSopenharmony_ci} 153f08c3bdfSopenharmony_ci 154f08c3bdfSopenharmony_cistatic void run(unsigned int i) 155f08c3bdfSopenharmony_ci{ 156f08c3bdfSopenharmony_ci int bufsize = testcases[i].bufsize; 157f08c3bdfSopenharmony_ci int remote_iovecs = testcases[i].remote_iovecs; 158f08c3bdfSopenharmony_ci int local_iovecs = testcases[i].local_iovecs; 159f08c3bdfSopenharmony_ci pid_t pid_alloc; 160f08c3bdfSopenharmony_ci pid_t pid_read; 161f08c3bdfSopenharmony_ci int status; 162f08c3bdfSopenharmony_ci int sizes[remote_iovecs]; 163f08c3bdfSopenharmony_ci 164f08c3bdfSopenharmony_ci tst_res(TINFO, "bufsize=%d, remote_iovecs=%d, local_iovecs=%d", bufsize, 165f08c3bdfSopenharmony_ci remote_iovecs, local_iovecs); 166f08c3bdfSopenharmony_ci 167f08c3bdfSopenharmony_ci create_data_size(sizes, remote_iovecs, bufsize); 168f08c3bdfSopenharmony_ci 169f08c3bdfSopenharmony_ci pid_alloc = SAFE_FORK(); 170f08c3bdfSopenharmony_ci if (!pid_alloc) { 171f08c3bdfSopenharmony_ci child_alloc(sizes, remote_iovecs); 172f08c3bdfSopenharmony_ci return; 173f08c3bdfSopenharmony_ci } 174f08c3bdfSopenharmony_ci 175f08c3bdfSopenharmony_ci TST_CHECKPOINT_WAIT(0); 176f08c3bdfSopenharmony_ci 177f08c3bdfSopenharmony_ci pid_read = SAFE_FORK(); 178f08c3bdfSopenharmony_ci if (!pid_read) { 179f08c3bdfSopenharmony_ci child_read(sizes, local_iovecs, remote_iovecs, pid_alloc, bufsize); 180f08c3bdfSopenharmony_ci return; 181f08c3bdfSopenharmony_ci } 182f08c3bdfSopenharmony_ci 183f08c3bdfSopenharmony_ci SAFE_WAITPID(pid_read, &status, 0); 184f08c3bdfSopenharmony_ci if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) 185f08c3bdfSopenharmony_ci tst_res(TFAIL, "child_read: %s", tst_strstatus(status)); 186f08c3bdfSopenharmony_ci 187f08c3bdfSopenharmony_ci TST_CHECKPOINT_WAKE(0); 188f08c3bdfSopenharmony_ci} 189f08c3bdfSopenharmony_ci 190f08c3bdfSopenharmony_cistatic struct tst_test test = { 191f08c3bdfSopenharmony_ci .test = run, 192f08c3bdfSopenharmony_ci .setup = setup, 193f08c3bdfSopenharmony_ci .cleanup = cleanup, 194f08c3bdfSopenharmony_ci .forks_child = 1, 195f08c3bdfSopenharmony_ci .needs_checkpoints = 1, 196f08c3bdfSopenharmony_ci .tcnt = ARRAY_SIZE(testcases), 197f08c3bdfSopenharmony_ci}; 198