Path: blob/master/tools/testing/selftests/arm64/signal/testcases/testcases.h
26295 views
/* SPDX-License-Identifier: GPL-2.0 */1/* Copyright (C) 2019 ARM Limited */2#ifndef __TESTCASES_H__3#define __TESTCASES_H__45#include <stddef.h>6#include <stdio.h>7#include <stdbool.h>8#include <stdint.h>9#include <stdlib.h>10#include <ucontext.h>11#include <signal.h>1213/* Architecture specific sigframe definitions */14#include <asm/sigcontext.h>1516#define FPSIMD_CTX (1 << 0)17#define SVE_CTX (1 << 1)18#define ZA_CTX (1 << 2)19#define EXTRA_CTX (1 << 3)20#define ZT_CTX (1 << 4)21#define FPMR_CTX (1 << 5)22#define GCS_CTX (1 << 6)2324#define KSFT_BAD_MAGIC 0xdeadbeef2526#define HDR_SZ \27sizeof(struct _aarch64_ctx)2829#define GET_UC_RESV_HEAD(uc) \30(struct _aarch64_ctx *)(&(uc->uc_mcontext.__reserved))3132#define GET_SF_RESV_HEAD(sf) \33(struct _aarch64_ctx *)(&(sf).uc.uc_mcontext.__reserved)3435#define GET_SF_RESV_SIZE(sf) \36sizeof((sf).uc.uc_mcontext.__reserved)3738#define GET_BUF_RESV_HEAD(buf) \39(struct _aarch64_ctx *)(&(buf).uc.uc_mcontext.__reserved)4041#define GET_BUF_RESV_SIZE(buf) \42(sizeof(buf) - sizeof(buf.uc) + \43sizeof((buf).uc.uc_mcontext.__reserved))4445#define GET_UCP_RESV_SIZE(ucp) \46sizeof((ucp)->uc_mcontext.__reserved)4748#define ASSERT_BAD_CONTEXT(uc) do { \49char *err = NULL; \50if (!validate_reserved((uc), GET_UCP_RESV_SIZE((uc)), &err)) { \51if (err) \52fprintf(stderr, \53"Using badly built context - ERR: %s\n",\54err); \55} else { \56abort(); \57} \58} while (0)5960#define ASSERT_GOOD_CONTEXT(uc) do { \61char *err = NULL; \62if (!validate_reserved((uc), GET_UCP_RESV_SIZE((uc)), &err)) { \63if (err) \64fprintf(stderr, \65"Detected BAD context - ERR: %s\n", err);\66abort(); \67} else { \68fprintf(stderr, "uc context validated.\n"); \69} \70} while (0)7172/*73* A simple record-walker for __reserved area: it walks through assuming74* only to find a proper struct __aarch64_ctx header descriptor.75*76* Instead it makes no assumptions on the content and ordering of the77* records, any needed bounds checking must be enforced by the caller78* if wanted: this way can be used by caller on any maliciously built bad79* contexts.80*81* head->size accounts both for payload and header _aarch64_ctx size !82*/83#define GET_RESV_NEXT_HEAD(h) \84(struct _aarch64_ctx *)((char *)(h) + (h)->size)8586struct fake_sigframe {87siginfo_t info;88ucontext_t uc;89};909192bool validate_reserved(ucontext_t *uc, size_t resv_sz, char **err);9394static inline struct _aarch64_ctx *get_header(struct _aarch64_ctx *head, uint32_t magic,95size_t resv_sz, size_t *offset)96{97size_t offs = 0;98struct _aarch64_ctx *found = NULL;99100if (!head || resv_sz < HDR_SZ)101return found;102103while (offs <= resv_sz - HDR_SZ &&104head->magic != magic && head->magic) {105offs += head->size;106head = GET_RESV_NEXT_HEAD(head);107}108if (head->magic == magic) {109found = head;110if (offset)111*offset = offs;112}113114return found;115}116117118static inline struct _aarch64_ctx *get_terminator(struct _aarch64_ctx *head,119size_t resv_sz,120size_t *offset)121{122return get_header(head, 0, resv_sz, offset);123}124125static inline void write_terminator_record(struct _aarch64_ctx *tail)126{127if (tail) {128tail->magic = 0;129tail->size = 0;130}131}132133struct _aarch64_ctx *get_starting_head(struct _aarch64_ctx *shead,134size_t need_sz, size_t resv_sz,135size_t *offset);136#endif137138139