1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Test setjmp(), longjmp()
4  *
5  * Copyright (c) 2021, Heinrich Schuchardt <xypron.glpk@gmx.de>
6  */
7 
8 #include <setjmp.h>
9 #include <test/lib.h>
10 #include <test/test.h>
11 #include <test/ut.h>
12 
13 struct test_jmp_buf {
14 	jmp_buf env;
15 	int val;
16 };
17 
18 /**
19  * test_longjmp() - test longjmp function
20  *
21  * @i is passed to longjmp.
22  * @i << 8 is set in the environment structure.
23  *
24  * @env:	environment
25  * @i:		value passed to longjmp()
26  */
test_longjmp(struct test_jmp_buf * env,int i)27 static noinline void test_longjmp(struct test_jmp_buf *env, int i)
28 {
29 	env->val = i << 8;
30 	longjmp(env->env, i);
31 }
32 
33 /**
34  * test_setjmp() - test setjmp function
35  *
36  * setjmp() will return the value @i passed to longjmp() if @i is non-zero.
37  * For @i == 0 we expect return value 1.
38  *
39  * @i << 8 will be set by test_longjmp in the environment structure.
40  * This value can be used to check that the stack frame is restored.
41  *
42  * We return the XORed values to allow simply check both at once.
43  *
44  * @i:		value passed to longjmp()
45  * Return:	values return by longjmp()
46  */
test_setjmp(int i)47 static int test_setjmp(int i)
48 {
49 	struct test_jmp_buf env;
50 	int ret;
51 
52 	env.val = -1;
53 	ret = setjmp(env.env);
54 	if (ret)
55 		return ret ^ env.val;
56 	test_longjmp(&env, i);
57 	/* We should not arrive here */
58 	return 0x1000;
59 }
60 
lib_test_longjmp(struct unit_test_state * uts)61 static int lib_test_longjmp(struct unit_test_state *uts)
62 {
63 	int i;
64 
65 	for (i = -3; i < 0; ++i)
66 		ut_asserteq(i ^ (i << 8), test_setjmp(i));
67 	ut_asserteq(1, test_setjmp(0));
68 	for (i = 1; i < 4; ++i)
69 		ut_asserteq(i ^ (i << 8), test_setjmp(i));
70 	return 0;
71 }
72 LIB_TEST(lib_test_longjmp, 0);
73