xref: /aosp_15_r20/external/ltp/testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines  Corp., 2004
4  * Copyright (c) Linux Test Project, 2004-2020
5  */
6 
7 /*
8  * DESCRIPTION
9  *	hugeshmctl02 - check for EACCES, EFAULT and EINVAL errors
10  *
11  * ALGORITHM
12  *	create a large shared memory segment without read or write permissions
13  *	create a large shared memory segment with read & write permissions
14  *	loop if that option was specified
15  *	  call shmctl() using five different invalid cases
16  *	  check the errno value
17  *	    issue a PASS message if we get EACCES, EFAULT or EINVAL
18  *	  otherwise, the tests fails
19  *	    issue a FAIL message
20  *	call cleanup
21  *
22  * HISTORY
23  *	03/2001 - Written by Wayne Boyer
24  *	04/2004 - Updated by Robbie Williamson
25  */
26 
27 #include <pwd.h>
28 #include <limits.h>
29 #include "hugetlb.h"
30 #include "lapi/syscalls.h"
31 
32 static size_t shm_size;
33 static int shm_id_1 = -1;
34 static int shm_id_2 = -1;
35 static int shm_id_3 = -1;
36 static struct shmid_ds buf;
37 
38 static struct tcase {
39 	int *shmid;
40 	int cmd;
41 	struct shmid_ds *sbuf;
42 	int error;
43 } tcases[] = {
44 	/* EFAULT - IPC_SET & buf isn't valid */
45 	{&shm_id_2, IPC_SET, (struct shmid_ds *)-1, EFAULT},
46 	/* EFAULT - IPC_STAT & buf isn't valid */
47 	{&shm_id_2, IPC_STAT, (struct shmid_ds *)-1, EFAULT},
48 	/* EINVAL - the shmid is not valid */
49 	{&shm_id_3, IPC_STAT, &buf, EINVAL},
50 	/* EINVAL - the command is not valid */
51 	{&shm_id_2, -1, &buf, EINVAL},
52 };
53 
libc_shmctl(int shmid,int cmd,void * buf)54 static int libc_shmctl(int shmid, int cmd, void *buf)
55 {
56 	return shmctl(shmid, cmd, buf);
57 }
58 
sys_shmctl(int shmid,int cmd,void * buf)59 static int sys_shmctl(int shmid, int cmd, void *buf)
60 {
61 	return tst_syscall(__NR_shmctl, shmid, cmd, buf);
62 }
63 
64 static struct test_variants
65 {
66 	int (*shmctl)(int shmid, int cmd, void *buf);
67 	char *desc;
68 } variants[] = {
69 	{ .shmctl = libc_shmctl, .desc = "libc shmctl()"},
70 #if (__NR_shmctl != __LTP__NR_INVALID_SYSCALL)
71 	{ .shmctl = sys_shmctl,  .desc = "__NR_shmctl syscall"},
72 #endif
73 };
74 
test_hugeshmctl(unsigned int i)75 static void test_hugeshmctl(unsigned int i)
76 {
77 	struct test_variants *tv = &variants[tst_variant];
78 
79 	if (tcases[i].error == EFAULT && tv->shmctl == libc_shmctl) {
80 		tst_res(TCONF, "EFAULT is skipped for libc variant");
81 		return;
82 	}
83 
84 	TEST(tv->shmctl(*(tcases[i].shmid), tcases[i].cmd, tcases[i].sbuf));
85 	if (TST_RET != -1) {
86 		tst_res(TFAIL, "shmctl succeeded unexpectedly");
87 		return;
88 	}
89 
90 	if (TST_ERR == tcases[i].error) {
91 		tst_res(TPASS | TTERRNO, "shmctl failed as expected");
92 		return;
93 	}
94 
95 	tst_res(TFAIL | TTERRNO,
96 			"shmctl failed unexpectedly - expect errno = %d, got",
97 			tcases[i].error);
98 }
99 
setup(void)100 static void setup(void)
101 {
102 	struct test_variants *tv = &variants[tst_variant];
103 	long hpage_size;
104 
105 	tst_res(TINFO, "Testing variant: %s", tv->desc);
106 
107 	if (tst_hugepages == 0)
108 		tst_brk(TCONF, "No enough hugepages for testing.");
109 
110 	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
111 
112 	shm_size = hpage_size * (tst_hugepages / 2);
113 	update_shm_size(&shm_size);
114 	shmkey = getipckey();
115 
116 	/* create a shared memory segment without read or write permissions */
117 	shm_id_1 = shmget(shmkey, shm_size, SHM_HUGETLB | IPC_CREAT | IPC_EXCL);
118 	if (shm_id_1 == -1)
119 		tst_brk(TBROK | TERRNO, "shmget #1");
120 
121 	/* create a shared memory segment with read and write permissions */
122 	shm_id_2 = shmget(shmkey + 1, shm_size,
123 			  SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW);
124 	if (shm_id_2 == -1)
125 		tst_brk(TBROK | TERRNO, "shmget #2");
126 }
127 
cleanup(void)128 static void cleanup(void)
129 {
130 	rm_shm(shm_id_1);
131 	rm_shm(shm_id_2);
132 }
133 
134 static struct tst_test test = {
135 	.test = test_hugeshmctl,
136 	.test_variants = ARRAY_SIZE(variants),
137 	.tcnt = ARRAY_SIZE(tcases),
138 	.needs_root = 1,
139 	.needs_tmpdir = 1,
140 	.options = (struct tst_option[]) {
141 		{"s:", &nr_opt, "Set the number of the been allocated hugepages"},
142 		{}
143 	},
144 	.setup = setup,
145 	.cleanup = cleanup,
146 	.hugepages = {128, TST_REQUEST},
147 };
148