Path: blob/main/lib/libc/tests/sys/swapcontext_test.c
39530 views
/*-1* Copyright (c) 2025 Raptor Computing Systems, LLC2*3* SPDX-License-Identifier: BSD-2-Clause4*/56#include <stdio.h>7#include <stdlib.h>8#include <ucontext.h>9#include <errno.h>1011#include <atf-c.h>1213#define STACK_SIZE (64ull << 10)1415static volatile int callback_reached = 0;1617static ucontext_t uctx_save, uctx_switch;1819static void swapcontext_callback()20{21// Increment callback reached variable22// If this is called multiple times, we will fail the test23// If this is not called at all, we will fail the test24callback_reached++;25}2627ATF_TC(swapcontext_basic);28ATF_TC_HEAD(swapcontext_basic, tc)29{30atf_tc_set_md_var(tc, "descr",31"Verify basic functionality of swapcontext");32}3334ATF_TC_BODY(swapcontext_basic, tc)35{36char *stack;37int res;3839stack = malloc(STACK_SIZE);40ATF_REQUIRE_MSG(stack != NULL, "malloc failed: %s", strerror(errno));41res = getcontext(&uctx_switch);42ATF_REQUIRE_MSG(res == 0, "getcontext failed: %s", strerror(errno));4344uctx_switch.uc_stack.ss_sp = stack;45uctx_switch.uc_stack.ss_size = STACK_SIZE;46uctx_switch.uc_link = &uctx_save;47makecontext(&uctx_switch, swapcontext_callback, 0);4849res = swapcontext(&uctx_save, &uctx_switch);5051ATF_REQUIRE_MSG(res == 0, "swapcontext failed: %s", strerror(errno));52ATF_REQUIRE_MSG(callback_reached == 1,53"callback failed, reached %d times", callback_reached);54}5556ATF_TP_ADD_TCS(tp)57{58ATF_TP_ADD_TC(tp, swapcontext_basic);5960return (atf_no_error());61}62636465