longjmp.c 1.5 KB

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