[LTP] [PATCH v2 1/9] mem: shmt02: Make sure memory is no longer accessible after child exits

Ricardo B. Marlière rbm@suse.com
Mon Jun 30 15:13:08 CEST 2025


From: Ricardo B. Marlière <rbm@suse.com>

The test description states: "Verify that the shared memory segment is
accessible as long as the process is still alive". Make sure to verify that
the segment is not accessible after the child process exits.

Fixes: 1e8516790063 ("mem: Convert shmt02 to new API")
Suggested-by: Andrea Cervesato <andrea.cervesato@suse.de>
Signed-off-by: Ricardo B. Marlière <rbm@suse.com>
---
 testcases/kernel/mem/shmt/shmt02.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)

diff --git a/testcases/kernel/mem/shmt/shmt02.c b/testcases/kernel/mem/shmt/shmt02.c
index 16d99ac7a781c42cc282dd81af8895097b404435..52145af728ae357a2eae3195dbd7784a76666e11 100644
--- a/testcases/kernel/mem/shmt/shmt02.c
+++ b/testcases/kernel/mem/shmt/shmt02.c
@@ -27,21 +27,26 @@ static void run(void)
 
 	key = GETIPCKEY();
 
-	shmid = SAFE_SHMGET(key, SHMSIZE, IPC_CREAT | 0666);
+	if (!SAFE_FORK()) {
+		shmid = SAFE_SHMGET(key, SHMSIZE, IPC_CREAT | 0666);
+		cp = SAFE_SHMAT(shmid, NULL, 0);
+		memcpy(cp, tst_rand_data, SHMSIZE);
+		SAFE_SHMCTL(shmid, IPC_RMID, NULL);
+		TST_EXP_EQ_LI(memcmp(cp, tst_rand_data, SHMSIZE), 0);
+		TST_CHECKPOINT_WAKE(0);
+		_exit(0);
+	}
 
+	TST_CHECKPOINT_WAIT(0);
+	tst_reap_children();
+	shmid = SAFE_SHMGET(key, SHMSIZE, IPC_CREAT | 0666);
 	cp = SAFE_SHMAT(shmid, NULL, 0);
-	memcpy(cp, tst_rand_data, SHMSIZE);
-
+	TST_EXP_EXPR(memcmp(cp, tst_rand_data, SHMSIZE) < 0);
 	SAFE_SHMCTL(shmid, IPC_RMID, NULL);
-
-	if (memcmp(cp, tst_rand_data, SHMSIZE) != 0) {
-		tst_res(TFAIL, "Copied data changed after IPC_RMID");
-		return;
-	}
-
-	tst_res(TPASS, "Copied data has been read back");
 }
 
 static struct tst_test test = {
 	.test_all = run,
+	.needs_checkpoints = 1,
+	.forks_child = 1,
 };

-- 
2.50.0



More information about the ltp mailing list