Skip to content

Commit 3953a1d

Browse files
zx2c4shuahkh
authored andcommitted
selftests: vDSO: improve getrandom and chacha error messages
Improve the error and skip condition messages to let the developer know precisely where a test has failed. Also make better use of the ksft api for this. Signed-off-by: Jason A. Donenfeld <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent fe6305c commit 3953a1d

File tree

2 files changed

+49
-53
lines changed

2 files changed

+49
-53
lines changed

tools/testing/selftests/vDSO/vdso_test_chacha.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,8 @@ int main(int argc, char *argv[])
9191
ksft_set_plan(1);
9292

9393
for (unsigned int trial = 0; trial < TRIALS; ++trial) {
94-
if (getrandom(key, sizeof(key), 0) != sizeof(key)) {
95-
printf("getrandom() failed!\n");
96-
return KSFT_SKIP;
97-
}
94+
if (getrandom(key, sizeof(key), 0) != sizeof(key))
95+
ksft_exit_skip("getrandom() failed unexpectedly\n");
9896
memset(counter1, 0, sizeof(counter1));
9997
reference_chacha20_blocks(output1, key, counter1, BLOCKS);
10098
for (unsigned int split = 0; split < BLOCKS; ++split) {
@@ -103,8 +101,10 @@ int main(int argc, char *argv[])
103101
if (split)
104102
__arch_chacha20_blocks_nostack(output2, key, counter2, split);
105103
__arch_chacha20_blocks_nostack(output2 + split * BLOCK_SIZE, key, counter2, BLOCKS - split);
106-
if (memcmp(output1, output2, sizeof(output1)) || memcmp(counter1, counter2, sizeof(counter1)))
107-
return KSFT_FAIL;
104+
if (memcmp(output1, output2, sizeof(output1)))
105+
ksft_exit_fail_msg("Main loop outputs do not match on trial %u, split %u\n", trial, split);
106+
if (memcmp(counter1, counter2, sizeof(counter1)))
107+
ksft_exit_fail_msg("Main loop counters do not match on trial %u, split %u\n", trial, split);
108108
}
109109
}
110110
memset(counter1, 0, sizeof(counter1));
@@ -114,14 +114,19 @@ int main(int argc, char *argv[])
114114

115115
reference_chacha20_blocks(output1, key, counter1, BLOCKS);
116116
__arch_chacha20_blocks_nostack(output2, key, counter2, BLOCKS);
117-
if (memcmp(output1, output2, sizeof(output1)) || memcmp(counter1, counter2, sizeof(counter1)))
118-
return KSFT_FAIL;
117+
if (memcmp(output1, output2, sizeof(output1)))
118+
ksft_exit_fail_msg("Block limit outputs do not match after first round\n");
119+
if (memcmp(counter1, counter2, sizeof(counter1)))
120+
ksft_exit_fail_msg("Block limit counters do not match after first round\n");
119121

120122
reference_chacha20_blocks(output1, key, counter1, BLOCKS);
121123
__arch_chacha20_blocks_nostack(output2, key, counter2, BLOCKS);
122-
if (memcmp(output1, output2, sizeof(output1)) || memcmp(counter1, counter2, sizeof(counter1)))
123-
return KSFT_FAIL;
124+
if (memcmp(output1, output2, sizeof(output1)))
125+
ksft_exit_fail_msg("Block limit outputs do not match after second round\n");
126+
if (memcmp(counter1, counter2, sizeof(counter1)))
127+
ksft_exit_fail_msg("Block limit counters do not match after second round\n");
124128

125129
ksft_test_result_pass("chacha: PASS\n");
126-
return KSFT_PASS;
130+
ksft_exit_pass();
131+
return 0;
127132
}

tools/testing/selftests/vDSO/vdso_test_getrandom.c

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
} while (0)
4141
#endif
4242

43+
#define ksft_assert(condition) \
44+
do { if (!(condition)) ksft_exit_fail_msg("Assertion failed: %s\n", #condition); } while (0)
45+
4346
static struct {
4447
pthread_mutex_t lock;
4548
void **states;
@@ -111,26 +114,19 @@ static void vgetrandom_init(void)
111114
const char *version = versions[VDSO_VERSION];
112115
const char *name = names[VDSO_NAMES][6];
113116
unsigned long sysinfo_ehdr = getauxval(AT_SYSINFO_EHDR);
114-
size_t ret;
117+
ssize_t ret;
115118

116-
if (!sysinfo_ehdr) {
117-
printf("AT_SYSINFO_EHDR is not present!\n");
118-
exit(KSFT_SKIP);
119-
}
119+
if (!sysinfo_ehdr)
120+
ksft_exit_skip("AT_SYSINFO_EHDR is not present\n");
120121
vdso_init_from_sysinfo_ehdr(sysinfo_ehdr);
121122
vgrnd.fn = (__typeof__(vgrnd.fn))vdso_sym(version, name);
122-
if (!vgrnd.fn) {
123-
printf("%s is missing!\n", name);
124-
exit(KSFT_SKIP);
125-
}
123+
if (!vgrnd.fn)
124+
ksft_exit_skip("%s@%s symbol is missing from vDSO\n", name, version);
126125
ret = VDSO_CALL(vgrnd.fn, 5, NULL, 0, 0, &vgrnd.params, ~0UL);
127-
if (ret == -ENOSYS) {
128-
printf("unsupported architecture\n");
129-
exit(KSFT_SKIP);
130-
} else if (ret) {
131-
printf("failed to fetch vgetrandom params!\n");
132-
exit(KSFT_FAIL);
133-
}
126+
if (ret == -ENOSYS)
127+
ksft_exit_skip("CPU does not have runtime support\n");
128+
else if (ret)
129+
ksft_exit_fail_msg("Failed to fetch vgetrandom params: %zd\n", ret);
134130
}
135131

136132
static ssize_t vgetrandom(void *buf, size_t len, unsigned long flags)
@@ -139,10 +135,7 @@ static ssize_t vgetrandom(void *buf, size_t len, unsigned long flags)
139135

140136
if (!state) {
141137
state = vgetrandom_get_state();
142-
if (!state) {
143-
printf("vgetrandom_get_state failed!\n");
144-
exit(KSFT_FAIL);
145-
}
138+
ksft_assert(state);
146139
}
147140
return VDSO_CALL(vgrnd.fn, 5, buf, len, flags, state, vgrnd.params.size_of_opaque_state);
148141
}
@@ -154,7 +147,7 @@ static void *test_vdso_getrandom(void *ctx)
154147
for (size_t i = 0; i < TRIALS; ++i) {
155148
unsigned int val;
156149
ssize_t ret = vgetrandom(&val, sizeof(val), 0);
157-
assert(ret == sizeof(val));
150+
ksft_assert(ret == sizeof(val));
158151
}
159152
return NULL;
160153
}
@@ -164,7 +157,7 @@ static void *test_libc_getrandom(void *ctx)
164157
for (size_t i = 0; i < TRIALS; ++i) {
165158
unsigned int val;
166159
ssize_t ret = getrandom(&val, sizeof(val), 0);
167-
assert(ret == sizeof(val));
160+
ksft_assert(ret == sizeof(val));
168161
}
169162
return NULL;
170163
}
@@ -174,7 +167,7 @@ static void *test_syscall_getrandom(void *ctx)
174167
for (size_t i = 0; i < TRIALS; ++i) {
175168
unsigned int val;
176169
ssize_t ret = syscall(__NR_getrandom, &val, sizeof(val), 0);
177-
assert(ret == sizeof(val));
170+
ksft_assert(ret == sizeof(val));
178171
}
179172
return NULL;
180173
}
@@ -209,7 +202,7 @@ static void bench_multi(void)
209202

210203
clock_gettime(CLOCK_MONOTONIC, &start);
211204
for (size_t i = 0; i < THREADS; ++i)
212-
assert(pthread_create(&threads[i], NULL, test_vdso_getrandom, NULL) == 0);
205+
ksft_assert(pthread_create(&threads[i], NULL, test_vdso_getrandom, NULL) == 0);
213206
for (size_t i = 0; i < THREADS; ++i)
214207
pthread_join(threads[i], NULL);
215208
clock_gettime(CLOCK_MONOTONIC, &end);
@@ -218,7 +211,7 @@ static void bench_multi(void)
218211

219212
clock_gettime(CLOCK_MONOTONIC, &start);
220213
for (size_t i = 0; i < THREADS; ++i)
221-
assert(pthread_create(&threads[i], NULL, test_libc_getrandom, NULL) == 0);
214+
ksft_assert(pthread_create(&threads[i], NULL, test_libc_getrandom, NULL) == 0);
222215
for (size_t i = 0; i < THREADS; ++i)
223216
pthread_join(threads[i], NULL);
224217
clock_gettime(CLOCK_MONOTONIC, &end);
@@ -227,7 +220,7 @@ static void bench_multi(void)
227220

228221
clock_gettime(CLOCK_MONOTONIC, &start);
229222
for (size_t i = 0; i < THREADS; ++i)
230-
assert(pthread_create(&threads[i], NULL, test_syscall_getrandom, NULL) == 0);
223+
ksft_assert(pthread_create(&threads[i], NULL, test_syscall_getrandom, NULL) == 0);
231224
for (size_t i = 0; i < THREADS; ++i)
232225
pthread_join(threads[i], NULL);
233226
clock_gettime(CLOCK_MONOTONIC, &end);
@@ -252,48 +245,46 @@ static void kselftest(void)
252245

253246
for (size_t i = 0; i < 1000; ++i) {
254247
ssize_t ret = vgetrandom(weird_size, sizeof(weird_size), 0);
255-
if (ret != sizeof(weird_size))
256-
exit(KSFT_FAIL);
248+
ksft_assert(ret == sizeof(weird_size));
257249
}
258250

259251
ksft_test_result_pass("getrandom: PASS\n");
260252

261253
unshare(CLONE_NEWUSER);
262-
assert(unshare(CLONE_NEWTIME) == 0);
254+
ksft_assert(unshare(CLONE_NEWTIME) == 0);
263255
child = fork();
264-
assert(child >= 0);
256+
ksft_assert(child >= 0);
265257
if (!child) {
266258
vgetrandom_init();
267259
child = getpid();
268-
assert(ptrace(PTRACE_TRACEME, 0, NULL, NULL) == 0);
269-
assert(kill(child, SIGSTOP) == 0);
270-
assert(vgetrandom(weird_size, sizeof(weird_size), 0) == sizeof(weird_size));
260+
ksft_assert(ptrace(PTRACE_TRACEME, 0, NULL, NULL) == 0);
261+
ksft_assert(kill(child, SIGSTOP) == 0);
262+
ksft_assert(vgetrandom(weird_size, sizeof(weird_size), 0) == sizeof(weird_size));
271263
_exit(0);
272264
}
273265
for (;;) {
274266
struct ptrace_syscall_info info = { 0 };
275267
int status, ret;
276-
assert(waitpid(child, &status, 0) >= 0);
268+
ksft_assert(waitpid(child, &status, 0) >= 0);
277269
if (WIFEXITED(status)) {
278-
if (WEXITSTATUS(status) != 0)
279-
exit(KSFT_FAIL);
270+
ksft_assert(WEXITSTATUS(status) == 0);
280271
break;
281272
}
282-
assert(WIFSTOPPED(status));
273+
ksft_assert(WIFSTOPPED(status));
283274
if (WSTOPSIG(status) == SIGSTOP)
284-
assert(ptrace(PTRACE_SETOPTIONS, child, 0, PTRACE_O_TRACESYSGOOD) == 0);
275+
ksft_assert(ptrace(PTRACE_SETOPTIONS, child, 0, PTRACE_O_TRACESYSGOOD) == 0);
285276
else if (WSTOPSIG(status) == (SIGTRAP | 0x80)) {
286-
assert(ptrace(PTRACE_GET_SYSCALL_INFO, child, sizeof(info), &info) > 0);
277+
ksft_assert(ptrace(PTRACE_GET_SYSCALL_INFO, child, sizeof(info), &info) > 0);
287278
if (info.op == PTRACE_SYSCALL_INFO_ENTRY && info.entry.nr == __NR_getrandom &&
288279
info.entry.args[0] == (uintptr_t)weird_size && info.entry.args[1] == sizeof(weird_size))
289-
exit(KSFT_FAIL);
280+
ksft_exit_fail_msg("vgetrandom passed buffer to syscall getrandom unexpectedly\n");
290281
}
291-
assert(ptrace(PTRACE_SYSCALL, child, 0, 0) == 0);
282+
ksft_assert(ptrace(PTRACE_SYSCALL, child, 0, 0) == 0);
292283
}
293284

294285
ksft_test_result_pass("getrandom timens: PASS\n");
295286

296-
exit(KSFT_PASS);
287+
ksft_exit_pass();
297288
}
298289

299290
static void usage(const char *argv0)

0 commit comments

Comments
 (0)