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