[LTP] [PATCH v3] Rewrite process_vm_readv03.c test with new LTP API

Cyril Hrubis chrubis@suse.cz
Tue Feb 15 16:16:30 CET 2022


Hi!
Pushed with minor changes, thanks.

The most important change was that fix for a memory leak in the run()
function that would cause problems with large enough -i parameter.

I also hardcoded the maximal number of iovecs instead of using whatever
is the system limit.

Full diff:

diff --git a/testcases/kernel/syscalls/cma/process_vm_readv03.c b/testcases/kernel/syscalls/cma/process_vm_readv03.c
index d6945fb7d..4caafe867 100644
--- a/testcases/kernel/syscalls/cma/process_vm_readv03.c
+++ b/testcases/kernel/syscalls/cma/process_vm_readv03.c
@@ -13,7 +13,7 @@
  * the remote iovecs initialized to the original process memory
  * locations and the local iovecs initialized to randomly sized and
  * allocated local memory locations. The second child then verifies
- * that the data copied is correct.
+ * that the data is copied correctly.
  */
 
 #include <stdio.h>
@@ -23,6 +23,8 @@
 #include "tst_test.h"
 #include "lapi/syscalls.h"
 
+#define MAX_IOVECS 1024
+
 static struct tcase {
 	int bufsize;
 	int remote_iovecs;
@@ -48,18 +50,16 @@ static struct tcase {
 };
 
 static char **data_ptr;
-static long max_iovec;
 
 static void create_data_size(int *arr, int arr_sz, int buffsize)
 {
-	long bufsz_left, bufsz_single;
+	long bufsz_left;
 	int i;
 
 	bufsz_left = buffsize;
 	for (i = 0; i < arr_sz - 1; i++) {
-		bufsz_single = rand() % ((bufsz_left / 2) + 1);
-		arr[i] = bufsz_single;
-		bufsz_left -= bufsz_single;
+		arr[i] = rand() % ((bufsz_left / 2) + 1);
+		bufsz_left -= arr[i];
 	}
 
 	arr[arr_sz - 1] = bufsz_left;
@@ -80,12 +80,12 @@ static void child_alloc(const int *sizes, int nr_iovecs)
 		}
 	}
 
-	tst_res(TINFO, "child 0: memory allocated and initialized");
+	tst_res(TINFO, "child_alloc: memory allocated and initialized");
 
 	TST_CHECKPOINT_WAKE_AND_WAIT(0);
 }
 
-static void child_invoke(const int *sizes, int local_iovecs, int remote_iovecs,
+static void child_read(const int *sizes, int local_iovecs, int remote_iovecs,
 			 pid_t pid_alloc, int buffsize)
 {
 	struct iovec local[local_iovecs];
@@ -93,7 +93,7 @@ static void child_invoke(const int *sizes, int local_iovecs, int remote_iovecs,
 	int i, j;
 	int count;
 	int nr_error;
-	int *local_sizes;
+	int local_sizes[local_iovecs];
 	unsigned char expect, actual;
 
 	for (i = 0; i < remote_iovecs; i++) {
@@ -101,17 +101,17 @@ static void child_invoke(const int *sizes, int local_iovecs, int remote_iovecs,
 		remote[i].iov_len = sizes[i];
 	}
 
-	local_sizes = SAFE_MALLOC(local_iovecs * sizeof(int));
 	create_data_size(local_sizes, local_iovecs, buffsize);
 	for (i = 0; i < local_iovecs; i++) {
 		local[i].iov_base = SAFE_MALLOC(local_sizes[i]);
 		local[i].iov_len = local_sizes[i];
 	}
 
-	tst_res(TINFO, "child 1: reading string from same memory location");
+	tst_res(TINFO, "child_read: reading string from same memory location");
 
 	TST_EXP_POSITIVE(tst_syscall(__NR_process_vm_readv, pid_alloc, local,
-				     local_iovecs, remote, remote_iovecs, 0UL));
+				     local_iovecs, remote, remote_iovecs, 0UL),
+			 "process_vm_read()");
 
 	if (TST_RET != buffsize) {
 		tst_brk(TBROK, "process_vm_readv: expected %d bytes but got %ld",
@@ -132,24 +132,23 @@ static void child_invoke(const int *sizes, int local_iovecs, int remote_iovecs,
 	}
 
 	if (nr_error)
-		tst_brk(TFAIL, "child 1: %d incorrect bytes received", nr_error);
+		tst_brk(TFAIL, "child_read: %d incorrect bytes received", nr_error);
 	else
-		tst_res(TPASS, "child 1: all bytes are correctly received");
+		tst_res(TPASS, "child_read: all bytes are correctly received");
 }
 
 static void setup(void)
 {
 	tst_syscall(__NR_process_vm_readv, getpid(), NULL, 0UL, NULL, 0UL, 0UL);
 
-	max_iovec = sysconf(_SC_IOV_MAX);
-	data_ptr = SAFE_MMAP(NULL, sizeof(void *) * max_iovec, PROT_READ | PROT_WRITE,
+	data_ptr = SAFE_MMAP(NULL, sizeof(void *) * MAX_IOVECS, PROT_READ | PROT_WRITE,
 			     MAP_SHARED | MAP_ANONYMOUS, -1, 0);
 }
 
 static void cleanup(void)
 {
 	if (data_ptr)
-		SAFE_MUNMAP(data_ptr, sizeof(void *) * max_iovec);
+		SAFE_MUNMAP(data_ptr, sizeof(void *) * MAX_IOVECS);
 }
 
 static void run(unsigned int i)
@@ -158,14 +157,13 @@ static void run(unsigned int i)
 	int remote_iovecs = testcases[i].remote_iovecs;
 	int local_iovecs = testcases[i].local_iovecs;
 	pid_t pid_alloc;
-	pid_t pid_invoke;
+	pid_t pid_read;
 	int status;
-	int *sizes;
+	int sizes[remote_iovecs];
 
 	tst_res(TINFO, "bufsize=%d, remote_iovecs=%d, local_iovecs=%d", bufsize,
 		remote_iovecs, local_iovecs);
 
-	sizes = SAFE_MALLOC(remote_iovecs * sizeof(int));
 	create_data_size(sizes, remote_iovecs, bufsize);
 
 	pid_alloc = SAFE_FORK();
@@ -176,19 +174,17 @@ static void run(unsigned int i)
 
 	TST_CHECKPOINT_WAIT(0);
 
-	pid_invoke = SAFE_FORK();
-	if (!pid_invoke) {
-		child_invoke(sizes, local_iovecs, remote_iovecs, pid_alloc, bufsize);
+	pid_read = SAFE_FORK();
+	if (!pid_read) {
+		child_read(sizes, local_iovecs, remote_iovecs, pid_alloc, bufsize);
 		return;
 	}
 
-	SAFE_WAITPID(pid_invoke, &status, 0);
+	SAFE_WAITPID(pid_read, &status, 0);
 	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
-		tst_res(TFAIL, "child 1: returns %s", tst_strstatus(status));
+		tst_res(TFAIL, "child_read: %s", tst_strstatus(status));
 
 	TST_CHECKPOINT_WAKE(0);
-
-	SAFE_WAITPID(pid_alloc, &status, 0);
 }
 
 static struct tst_test test = {
@@ -198,5 +194,4 @@ static struct tst_test test = {
 	.forks_child = 1,
 	.needs_checkpoints = 1,
 	.tcnt = ARRAY_SIZE(testcases),
-	.min_kver = "2.0",
 };

-- 
Cyril Hrubis
chrubis@suse.cz


More information about the ltp mailing list