1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2001
4f08c3bdfSopenharmony_ci */
5f08c3bdfSopenharmony_ci
6f08c3bdfSopenharmony_ci/*
7f08c3bdfSopenharmony_ci * DESCRIPTION
8f08c3bdfSopenharmony_ci *
9f08c3bdfSopenharmony_ci * 1) shmat() chooses a suitable (unused) address when shmaddr is NULL.
10f08c3bdfSopenharmony_ci * 2) shmat() attaches shm segment to the shmaddr when shmaddr is a
11f08c3bdfSopenharmony_ci *    page-aligned address.
12f08c3bdfSopenharmony_ci * 3) shmat() attaches shm segment to the address equal to shmaddr rounded
13f08c3bdfSopenharmony_ci *    down to the nearest multiple of SHMLBA when shmaddr is a page-unaligned
14f08c3bdfSopenharmony_ci *    address and shmflg is set to SHM_RND.
15f08c3bdfSopenharmony_ci * 4) shmat() attaches shm segment to the shmaddr for reading when shmflg
16f08c3bdfSopenharmony_ci *    is set to SHM_RDONLY.
17f08c3bdfSopenharmony_ci */
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_ci#include <errno.h>
20f08c3bdfSopenharmony_ci#include <sys/types.h>
21f08c3bdfSopenharmony_ci#include <sys/ipc.h>
22f08c3bdfSopenharmony_ci#include <sys/shm.h>
23f08c3bdfSopenharmony_ci#include <sys/wait.h>
24f08c3bdfSopenharmony_ci#include <stdlib.h>
25f08c3bdfSopenharmony_ci#include <stdint.h>
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_ci#include "tst_test.h"
28f08c3bdfSopenharmony_ci#include "tst_safe_sysv_ipc.h"
29f08c3bdfSopenharmony_ci#include "libnewipc.h"
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_ci#define ALIGN_DOWN(in_addr) ((void *)(((uintptr_t)in_addr / SHMLBA) * SHMLBA))
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_cistatic int shm_id = -1;
34f08c3bdfSopenharmony_cistatic key_t shm_key;
35f08c3bdfSopenharmony_cistatic void *null_addr;
36f08c3bdfSopenharmony_cistatic void *aligned_addr;
37f08c3bdfSopenharmony_cistatic void *unaligned_addr;
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_cistatic struct test_case_t {
40f08c3bdfSopenharmony_ci	void **shmaddr;
41f08c3bdfSopenharmony_ci	int flag;
42f08c3bdfSopenharmony_ci	int exp_status;
43f08c3bdfSopenharmony_ci	char *desp;
44f08c3bdfSopenharmony_ci} tcases[] = {
45f08c3bdfSopenharmony_ci	{&null_addr, 0, 0, "NULL address"},
46f08c3bdfSopenharmony_ci	{&aligned_addr, 0, 0, "aligned address"},
47f08c3bdfSopenharmony_ci	{&unaligned_addr, SHM_RND, 0, "unaligned address with SHM_RND"},
48f08c3bdfSopenharmony_ci	{&aligned_addr, SHM_RDONLY, SIGSEGV,
49f08c3bdfSopenharmony_ci	"aligned address with SHM_READONLY, and got SIGSEGV on write"}
50f08c3bdfSopenharmony_ci};
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_cistatic void *expected_addr(void *in_addr, void *out_addr)
53f08c3bdfSopenharmony_ci{
54f08c3bdfSopenharmony_ci	if (!in_addr)
55f08c3bdfSopenharmony_ci		return out_addr;
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ci	return ALIGN_DOWN(in_addr);
58f08c3bdfSopenharmony_ci}
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_cistatic void do_child(int *in_addr, int expect_crash)
61f08c3bdfSopenharmony_ci{
62f08c3bdfSopenharmony_ci	if (expect_crash)
63f08c3bdfSopenharmony_ci		tst_no_corefile(1);
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_ci	*in_addr = 10;
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_ci	exit(0);
68f08c3bdfSopenharmony_ci}
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_cistatic int expected_status(int status, int exp_status)
71f08c3bdfSopenharmony_ci{
72f08c3bdfSopenharmony_ci	if (!exp_status && WIFEXITED(status))
73f08c3bdfSopenharmony_ci		return 0;
74f08c3bdfSopenharmony_ci
75f08c3bdfSopenharmony_ci	if (exp_status && WIFSIGNALED(status) && WTERMSIG(status) == exp_status)
76f08c3bdfSopenharmony_ci		return 0;
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_ci	return 1;
79f08c3bdfSopenharmony_ci}
80f08c3bdfSopenharmony_ci
81f08c3bdfSopenharmony_cistatic void verify_shmat(unsigned int n)
82f08c3bdfSopenharmony_ci{
83f08c3bdfSopenharmony_ci	int *addr;
84f08c3bdfSopenharmony_ci	pid_t pid;
85f08c3bdfSopenharmony_ci	int status = 0;
86f08c3bdfSopenharmony_ci	struct shmid_ds buf;
87f08c3bdfSopenharmony_ci
88f08c3bdfSopenharmony_ci	struct test_case_t *tc = &tcases[n];
89f08c3bdfSopenharmony_ci
90f08c3bdfSopenharmony_ci	addr = shmat(shm_id, *tc->shmaddr, tc->flag);
91f08c3bdfSopenharmony_ci	if (addr == (void *)-1) {
92f08c3bdfSopenharmony_ci		tst_res(TFAIL | TERRNO, "shmat() failed");
93f08c3bdfSopenharmony_ci		return;
94f08c3bdfSopenharmony_ci	}
95f08c3bdfSopenharmony_ci
96f08c3bdfSopenharmony_ci	SAFE_SHMCTL(shm_id, IPC_STAT, &buf);
97f08c3bdfSopenharmony_ci
98f08c3bdfSopenharmony_ci	if (buf.shm_nattch != 1) {
99f08c3bdfSopenharmony_ci		tst_res(TFAIL, "number of attaches was incorrect");
100f08c3bdfSopenharmony_ci		goto end;
101f08c3bdfSopenharmony_ci	}
102f08c3bdfSopenharmony_ci
103f08c3bdfSopenharmony_ci	if (buf.shm_segsz != INT_SIZE) {
104f08c3bdfSopenharmony_ci		tst_res(TFAIL, "segment size was incorrect");
105f08c3bdfSopenharmony_ci		goto end;
106f08c3bdfSopenharmony_ci	}
107f08c3bdfSopenharmony_ci
108f08c3bdfSopenharmony_ci	if (expected_addr(*tc->shmaddr, addr) != addr) {
109f08c3bdfSopenharmony_ci		tst_res(TFAIL,
110f08c3bdfSopenharmony_ci			"shared memory address %p is not correct, expected %p",
111f08c3bdfSopenharmony_ci			addr, expected_addr(*tc->shmaddr, addr));
112f08c3bdfSopenharmony_ci		goto end;
113f08c3bdfSopenharmony_ci	}
114f08c3bdfSopenharmony_ci
115f08c3bdfSopenharmony_ci	pid = SAFE_FORK();
116f08c3bdfSopenharmony_ci	if (!pid)
117f08c3bdfSopenharmony_ci		do_child(addr, tc->exp_status == SIGSEGV);
118f08c3bdfSopenharmony_ci	else
119f08c3bdfSopenharmony_ci		SAFE_WAITPID(pid, &status, 0);
120f08c3bdfSopenharmony_ci
121f08c3bdfSopenharmony_ci	if (expected_status(status, tc->exp_status))
122f08c3bdfSopenharmony_ci		tst_res(TFAIL, "shmat() failed to attach %s", tc->desp);
123f08c3bdfSopenharmony_ci	else
124f08c3bdfSopenharmony_ci		tst_res(TPASS, "shmat() succeeded to attach %s", tc->desp);
125f08c3bdfSopenharmony_ci
126f08c3bdfSopenharmony_ciend:
127f08c3bdfSopenharmony_ci	SAFE_SHMDT(addr);
128f08c3bdfSopenharmony_ci}
129f08c3bdfSopenharmony_ci
130f08c3bdfSopenharmony_cistatic void setup(void)
131f08c3bdfSopenharmony_ci{
132f08c3bdfSopenharmony_ci	aligned_addr = PROBE_FREE_ADDR();
133f08c3bdfSopenharmony_ci	unaligned_addr = aligned_addr + SHMLBA - 1;
134f08c3bdfSopenharmony_ci
135f08c3bdfSopenharmony_ci	shm_key = GETIPCKEY();
136f08c3bdfSopenharmony_ci
137f08c3bdfSopenharmony_ci	shm_id = SAFE_SHMGET(shm_key, INT_SIZE, SHM_RW | IPC_CREAT | IPC_EXCL);
138f08c3bdfSopenharmony_ci}
139f08c3bdfSopenharmony_ci
140f08c3bdfSopenharmony_cistatic void cleanup(void)
141f08c3bdfSopenharmony_ci{
142f08c3bdfSopenharmony_ci	if (shm_id != -1)
143f08c3bdfSopenharmony_ci		SAFE_SHMCTL(shm_id, IPC_RMID, NULL);
144f08c3bdfSopenharmony_ci}
145f08c3bdfSopenharmony_ci
146f08c3bdfSopenharmony_cistatic struct tst_test test = {
147f08c3bdfSopenharmony_ci	.needs_root = 1,
148f08c3bdfSopenharmony_ci	.forks_child = 1,
149f08c3bdfSopenharmony_ci	.setup = setup,
150f08c3bdfSopenharmony_ci	.cleanup = cleanup,
151f08c3bdfSopenharmony_ci	.test = verify_shmat,
152f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases)
153f08c3bdfSopenharmony_ci};
154