Path: blob/main/contrib/arm-optimized-routines/string/test/memchr.c
39534 views
/*1* memchr test.2*3* Copyright (c) 2019-2020, Arm Limited.4* SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception5*/67#include <stdint.h>8#include <stdio.h>9#include <stdlib.h>10#include <string.h>11#include <limits.h>12#include "mte.h"13#include "stringlib.h"14#include "stringtest.h"1516#define F(x, mte) {#x, x, mte},1718static const struct fun19{20const char *name;21void *(*fun) (const void *s, int c, size_t n);22int test_mte;23} funtab[] = {24// clang-format off25F(memchr, 0)26#if __aarch64__27F(__memchr_aarch64, 0)28F(__memchr_aarch64_mte, 1)29# if __ARM_FEATURE_SVE30F(__memchr_aarch64_sve, 1)31# endif32#elif __arm__33F(__memchr_arm, 0)34#endif35{0, 0, 0}36// clang-format on37};38#undef F3940#define ALIGN 3241#define LEN 51242static char *sbuf;4344static void *45alignup (void *p)46{47return (void *) (((uintptr_t) p + ALIGN - 1) & -ALIGN);48}4950static void51test (const struct fun *fun, int align, size_t seekpos, size_t len,52size_t maxlen)53{54char *src = alignup (sbuf);55char *s = src + align;56char *f = seekpos < maxlen ? s + seekpos : NULL;57int seekchar = 1;58void *p;5960if (err_count >= ERR_LIMIT)61return;62if (len > LEN || seekpos > LEN || align > ALIGN)63abort ();6465for (int i = 0; src + i < s; i++)66src[i] = seekchar;67for (int i = 0; i <= ALIGN; i++)68s[len + i] = seekchar;69for (int i = 0; i < len; i++)70s[i] = 'a' + (i & 31);71s[seekpos] = seekchar;72s[((len ^ align) & 1) ? seekpos + 1 : len] = seekchar;7374int mte_len = seekpos != -1 ? seekpos + 1 : maxlen;75s = tag_buffer (s, mte_len, fun->test_mte);76p = fun->fun (s, seekchar, maxlen);77untag_buffer (s, mte_len, fun->test_mte);78p = untag_pointer (p);7980if (p != f)81{82ERR ("%s (%p, 0x%02x, %zu) returned %p, expected %p\n", fun->name, s,83seekchar, maxlen, p, f);84quote ("input", s, len);85}86}8788int89main (void)90{91sbuf = mte_mmap (LEN + 3 * ALIGN);92int r = 0;93for (int i = 0; funtab[i].name; i++)94{95err_count = 0;96for (int a = 0; a < ALIGN; a++)97for (int n = 0; n < LEN; n++)98{99for (int sp = 0; sp < LEN; sp++)100test (funtab + i, a, sp, n, n);101test (funtab + i, a, n, n, SIZE_MAX - a);102}103char *pass = funtab[i].test_mte && mte_enabled () ? "MTE PASS" : "PASS";104printf ("%s %s\n", err_count ? "FAIL" : pass, funtab[i].name);105if (err_count)106r = -1;107}108return r;109}110111112