1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
4f08c3bdfSopenharmony_ci */
5f08c3bdfSopenharmony_ci
6f08c3bdfSopenharmony_ci/*\
7f08c3bdfSopenharmony_ci * [Description]
8f08c3bdfSopenharmony_ci *
9f08c3bdfSopenharmony_ci * This test verifies EINVAL for futex_waitv syscall.
10f08c3bdfSopenharmony_ci */
11f08c3bdfSopenharmony_ci
12f08c3bdfSopenharmony_ci#include <time.h>
13f08c3bdfSopenharmony_ci#include <stdlib.h>
14f08c3bdfSopenharmony_ci#include "tst_test.h"
15f08c3bdfSopenharmony_ci#include "lapi/futex.h"
16f08c3bdfSopenharmony_ci#include "futex2test.h"
17f08c3bdfSopenharmony_ci#include "tst_safe_clocks.h"
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_cistatic uint32_t *futex;
20f08c3bdfSopenharmony_cistatic struct futex_waitv *waitv;
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_cistatic void setup(void)
23f08c3bdfSopenharmony_ci{
24f08c3bdfSopenharmony_ci	futex = SAFE_MALLOC(sizeof(uint32_t));
25f08c3bdfSopenharmony_ci	*futex = FUTEX_INITIALIZER;
26f08c3bdfSopenharmony_ci}
27f08c3bdfSopenharmony_ci
28f08c3bdfSopenharmony_cistatic void init_timeout(struct timespec *to)
29f08c3bdfSopenharmony_ci{
30f08c3bdfSopenharmony_ci	SAFE_CLOCK_GETTIME(CLOCK_MONOTONIC, to);
31f08c3bdfSopenharmony_ci	to->tv_sec++;
32f08c3bdfSopenharmony_ci}
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_cistatic void init_waitv(void)
35f08c3bdfSopenharmony_ci{
36f08c3bdfSopenharmony_ci	waitv->uaddr = (uintptr_t)futex;
37f08c3bdfSopenharmony_ci	waitv->flags = FUTEX_32 | FUTEX_PRIVATE_FLAG;
38f08c3bdfSopenharmony_ci	waitv->val = 0;
39f08c3bdfSopenharmony_ci}
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_cistatic void test_invalid_flags(void)
42f08c3bdfSopenharmony_ci{
43f08c3bdfSopenharmony_ci	struct timespec to;
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_ci	init_waitv();
46f08c3bdfSopenharmony_ci	init_timeout(&to);
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_ci	/* Testing a waiter without FUTEX_32 flag */
49f08c3bdfSopenharmony_ci	waitv->flags = FUTEX_PRIVATE_FLAG;
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_ci	TST_EXP_FAIL(futex_waitv(waitv, 1, 0, &to, CLOCK_MONOTONIC), EINVAL,
52f08c3bdfSopenharmony_ci		     "futex_waitv with invalid flags");
53f08c3bdfSopenharmony_ci}
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_cistatic void test_unaligned_address(void)
56f08c3bdfSopenharmony_ci{
57f08c3bdfSopenharmony_ci	struct timespec to;
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_ci	init_waitv();
60f08c3bdfSopenharmony_ci	init_timeout(&to);
61f08c3bdfSopenharmony_ci
62f08c3bdfSopenharmony_ci	waitv->uaddr = 1;
63f08c3bdfSopenharmony_ci
64f08c3bdfSopenharmony_ci	TST_EXP_FAIL(futex_waitv(waitv, 1, 0, &to, CLOCK_MONOTONIC), EINVAL,
65f08c3bdfSopenharmony_ci		     "futex_waitv with unligned address");
66f08c3bdfSopenharmony_ci}
67f08c3bdfSopenharmony_ci
68f08c3bdfSopenharmony_cistatic void test_null_address(void)
69f08c3bdfSopenharmony_ci{
70f08c3bdfSopenharmony_ci	struct timespec to;
71f08c3bdfSopenharmony_ci
72f08c3bdfSopenharmony_ci	init_waitv();
73f08c3bdfSopenharmony_ci	init_timeout(&to);
74f08c3bdfSopenharmony_ci
75f08c3bdfSopenharmony_ci	waitv->uaddr = 0x00000000;
76f08c3bdfSopenharmony_ci
77f08c3bdfSopenharmony_ci	TST_EXP_FAIL(futex_waitv(waitv, 1, 0, &to, CLOCK_MONOTONIC), EFAULT,
78f08c3bdfSopenharmony_ci		     "futex_waitv address is NULL");
79f08c3bdfSopenharmony_ci}
80f08c3bdfSopenharmony_ci
81f08c3bdfSopenharmony_cistatic void test_null_waiters(void)
82f08c3bdfSopenharmony_ci{
83f08c3bdfSopenharmony_ci	struct timespec to;
84f08c3bdfSopenharmony_ci
85f08c3bdfSopenharmony_ci	init_timeout(&to);
86f08c3bdfSopenharmony_ci
87f08c3bdfSopenharmony_ci	TST_EXP_FAIL(futex_waitv(NULL, 1, 0, &to, CLOCK_MONOTONIC), EINVAL,
88f08c3bdfSopenharmony_ci		     "futex_waitv waiters are NULL");
89f08c3bdfSopenharmony_ci}
90f08c3bdfSopenharmony_ci
91f08c3bdfSopenharmony_cistatic void test_invalid_clockid(void)
92f08c3bdfSopenharmony_ci{
93f08c3bdfSopenharmony_ci	struct timespec to;
94f08c3bdfSopenharmony_ci
95f08c3bdfSopenharmony_ci	init_waitv();
96f08c3bdfSopenharmony_ci	init_timeout(&to);
97f08c3bdfSopenharmony_ci
98f08c3bdfSopenharmony_ci	TST_EXP_FAIL(futex_waitv(waitv, 1, 0, &to, CLOCK_TAI), EINVAL,
99f08c3bdfSopenharmony_ci		     "futex_waitv invalid clockid");
100f08c3bdfSopenharmony_ci}
101f08c3bdfSopenharmony_ci
102f08c3bdfSopenharmony_cistatic void test_invalid_nr_futexes(void)
103f08c3bdfSopenharmony_ci{
104f08c3bdfSopenharmony_ci	struct timespec to;
105f08c3bdfSopenharmony_ci
106f08c3bdfSopenharmony_ci	init_waitv();
107f08c3bdfSopenharmony_ci	init_timeout(&to);
108f08c3bdfSopenharmony_ci
109f08c3bdfSopenharmony_ci	/* Valid nr_futexes is [1, 128] */
110f08c3bdfSopenharmony_ci	TST_EXP_FAIL(futex_waitv(waitv, 129, 0, &to, CLOCK_MONOTONIC), EINVAL,
111f08c3bdfSopenharmony_ci		     "futex_waitv invalid nr_futexes");
112f08c3bdfSopenharmony_ci	TST_EXP_FAIL(futex_waitv(waitv, 0, 0, &to, CLOCK_MONOTONIC), EINVAL,
113f08c3bdfSopenharmony_ci		     "futex_waitv invalid nr_futexes");
114f08c3bdfSopenharmony_ci}
115f08c3bdfSopenharmony_ci
116f08c3bdfSopenharmony_cistatic void test_mismatch_between_uaddr_and_val(void)
117f08c3bdfSopenharmony_ci{
118f08c3bdfSopenharmony_ci	struct timespec to;
119f08c3bdfSopenharmony_ci
120f08c3bdfSopenharmony_ci	waitv->uaddr = (uintptr_t)futex;
121f08c3bdfSopenharmony_ci	waitv->flags = FUTEX_32 | FUTEX_PRIVATE_FLAG;
122f08c3bdfSopenharmony_ci	waitv->val = 1;
123f08c3bdfSopenharmony_ci
124f08c3bdfSopenharmony_ci	init_timeout(&to);
125f08c3bdfSopenharmony_ci
126f08c3bdfSopenharmony_ci	TST_EXP_FAIL(futex_waitv(waitv, 1, 0, &to, CLOCK_MONOTONIC), EAGAIN,
127f08c3bdfSopenharmony_ci		     "futex_waitv mismatch between value of uaddr and val");
128f08c3bdfSopenharmony_ci}
129f08c3bdfSopenharmony_ci
130f08c3bdfSopenharmony_cistatic void test_timeout(void)
131f08c3bdfSopenharmony_ci{
132f08c3bdfSopenharmony_ci	struct timespec to;
133f08c3bdfSopenharmony_ci
134f08c3bdfSopenharmony_ci	waitv->uaddr = (uintptr_t)futex;
135f08c3bdfSopenharmony_ci	waitv->flags = FUTEX_32 | FUTEX_PRIVATE_FLAG;
136f08c3bdfSopenharmony_ci	waitv->val = 0;
137f08c3bdfSopenharmony_ci
138f08c3bdfSopenharmony_ci	SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &to);
139f08c3bdfSopenharmony_ci	to = tst_timespec_add_us(to, 10000);
140f08c3bdfSopenharmony_ci
141f08c3bdfSopenharmony_ci	TST_EXP_FAIL(futex_waitv(waitv, 1, 0, &to, CLOCK_REALTIME), ETIMEDOUT,
142f08c3bdfSopenharmony_ci		     "futex_waitv timeout");
143f08c3bdfSopenharmony_ci}
144f08c3bdfSopenharmony_ci
145f08c3bdfSopenharmony_cistatic void cleanup(void)
146f08c3bdfSopenharmony_ci{
147f08c3bdfSopenharmony_ci	free(futex);
148f08c3bdfSopenharmony_ci}
149f08c3bdfSopenharmony_ci
150f08c3bdfSopenharmony_cistatic void run(void)
151f08c3bdfSopenharmony_ci{
152f08c3bdfSopenharmony_ci	test_invalid_flags();
153f08c3bdfSopenharmony_ci	test_unaligned_address();
154f08c3bdfSopenharmony_ci	test_null_address();
155f08c3bdfSopenharmony_ci	test_null_waiters();
156f08c3bdfSopenharmony_ci	test_invalid_clockid();
157f08c3bdfSopenharmony_ci	test_invalid_nr_futexes();
158f08c3bdfSopenharmony_ci	test_mismatch_between_uaddr_and_val();
159f08c3bdfSopenharmony_ci	test_timeout();
160f08c3bdfSopenharmony_ci}
161f08c3bdfSopenharmony_ci
162f08c3bdfSopenharmony_cistatic struct tst_test test = {
163f08c3bdfSopenharmony_ci	.test_all = run,
164f08c3bdfSopenharmony_ci	.setup = setup,
165f08c3bdfSopenharmony_ci	.cleanup = cleanup,
166f08c3bdfSopenharmony_ci	.min_kver = "5.16",
167f08c3bdfSopenharmony_ci	.bufs =
168f08c3bdfSopenharmony_ci		(struct tst_buffers[]){
169f08c3bdfSopenharmony_ci			{ &waitv, .size = sizeof(struct futex_waitv) },
170f08c3bdfSopenharmony_ci			{},
171f08c3bdfSopenharmony_ci		},
172f08c3bdfSopenharmony_ci};
173