Path: blob/main/contrib/arm-optimized-routines/string/test/strchrnul.c
39535 views
/*1* strchrnul test.2*3* Copyright (c) 2019-2020, Arm Limited.4* SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception5*/67#ifndef _GNU_SOURCE8#define _GNU_SOURCE9#endif1011#include <stdint.h>12#include <stdio.h>13#include <stdlib.h>14#include <string.h>15#include <limits.h>16#include "mte.h"17#include "stringlib.h"18#include "stringtest.h"1920#define F(x, mte) {#x, x, mte},2122static const struct fun23{24const char *name;25char *(*fun) (const char *s, int c);26int test_mte;27} funtab[] = {28// clang-format off29F(strchrnul, 0)30#if __aarch64__31F(__strchrnul_aarch64, 0)32F(__strchrnul_aarch64_mte, 1)33# if __ARM_FEATURE_SVE34F(__strchrnul_aarch64_sve, 1)35# endif36#endif37{0, 0, 0}38// clang-format on39};40#undef F4142#define ALIGN 3243#define LEN 51244static char *sbuf;4546static void *47alignup (void *p)48{49return (void *) (((uintptr_t) p + ALIGN - 1) & -ALIGN);50}5152static void53test (const struct fun *fun, int align, int seekpos, int len)54{55char *src = alignup (sbuf);56char *s = src + align;57char *f = seekpos != -1 ? s + seekpos : s + len;58int seekchar = 0x1;59void *p;6061if (err_count >= ERR_LIMIT)62return;63if (len > LEN || seekpos >= len || align >= ALIGN)64abort ();6566for (int i = 0; src + i < s; i++)67src[i] = (i + len) & 1 ? seekchar : 0;68for (int i = 1; i <= ALIGN; i++)69s[len + i] = (i + len) & 1 ? seekchar : 0;70for (int i = 0; i < len; i++)71s[i] = 'a' + (i & 31);72if (seekpos != -1)73s[seekpos] = seekchar;74if (seekpos != -1 && (len + align) & 1)75s[seekpos + 1] = seekchar;76s[len] = '\0';7778int mte_len = seekpos != -1 ? seekpos + 1 : len + 1;79s = tag_buffer (s, mte_len, fun->test_mte);80p = fun->fun (s, seekchar);81untag_buffer (s, mte_len, fun->test_mte);82p = untag_pointer (p);8384if (p != f)85{86ERR ("%s (%p, 0x%02x) len %d returned %p, expected %p pos %d\n",87fun->name, s, seekchar, len, p, f, seekpos);88quote ("input", s, len);89}9091s = tag_buffer (s, len + 1, fun->test_mte);92p = fun->fun (s, 0);93untag_buffer (s, len + 1, fun->test_mte);9495if (p != s + len)96{97ERR ("%s (%p, 0x%02x) len %d returned %p, expected %p pos %d\n",98fun->name, s, 0, len, p, f, len);99quote ("input", s, len);100}101}102103int104main (void)105{106sbuf = mte_mmap (LEN + 3 * ALIGN);107int r = 0;108for (int i = 0; funtab[i].name; i++)109{110err_count = 0;111for (int a = 0; a < ALIGN; a++)112for (int n = 0; n < LEN; n++)113{114for (int sp = 0; sp < n; sp++)115test (funtab + i, a, sp, n);116test (funtab + i, a, -1, n);117}118119char *pass = funtab[i].test_mte && mte_enabled () ? "MTE PASS" : "PASS";120printf ("%s %s\n", err_count ? "FAIL" : pass, funtab[i].name);121if (err_count)122r = -1;123}124return r;125}126127128