Path: blob/main/contrib/arm-optimized-routines/string/test/strcpy.c
39491 views
/*1* strcpy test.2*3* Copyright (c) 2019-2022, 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 "mte.h"12#include "stringlib.h"13#include "stringtest.h"1415#define F(x, mte) {#x, x, mte},1617static const struct fun18{19const char *name;20char *(*fun) (char *dest, const char *src);21int test_mte;22} funtab[] = {23// clang-format off24F(strcpy, 0)25#if __aarch64__26F(__strcpy_aarch64, 1)27# if __ARM_FEATURE_SVE28F(__strcpy_aarch64_sve, 1)29# endif30#elif __arm__ && defined (__thumb2__) && !defined (__thumb__)31F(__strcpy_arm, 0)32#endif33{0, 0, 0}34// clang-format on35};36#undef F3738#define ALIGN 3239#define LEN 51240static char *dbuf;41static char *sbuf;42static char wbuf[LEN + 3 * ALIGN];4344static void *45alignup (void *p)46{47return (void *) (((uintptr_t) p + ALIGN - 1) & -ALIGN);48}4950static void51test (const struct fun *fun, int dalign, int salign, int len)52{53char *src = alignup (sbuf);54char *dst = alignup (dbuf);55char *want = wbuf;56char *s = src + salign;57char *d = dst + dalign;58char *w = want + dalign;59void *p;60int i;6162if (err_count >= ERR_LIMIT)63return;64if (len > LEN || dalign >= ALIGN || salign >= ALIGN)65abort ();66for (i = 0; i < len + ALIGN; i++)67{68src[i] = '?';69want[i] = dst[i] = '*';70}71for (int i = 0; src + i < s; i++)72src[i] = 0;73for (int i = 1; i <= ALIGN; i++)74s[len + i] = (len + salign) & 1 ? 1 : 0;75for (i = 0; i < len; i++)76s[i] = w[i] = 'a' + (i & 31);77s[len] = w[len] = '\0';7879s = tag_buffer (s, len + 1, fun->test_mte);80d = tag_buffer (d, len + 1, fun->test_mte);81p = fun->fun (d, s);82untag_buffer (s, len + 1, fun->test_mte);83untag_buffer (d, len + 1, fun->test_mte);8485if (p != d)86ERR ("%s (%p,..) returned %p\n", fun->name, d, p);8788for (i = 0; i < len + ALIGN; i++)89{90if (dst[i] != want[i])91{92ERR ("%s (align %d, align %d, %d) failed\n",93fun->name, dalign, salign, len);94quoteat ("got", dst, len + ALIGN, i);95quoteat ("want", want, len + ALIGN, i);96break;97}98}99}100101int102main (void)103{104sbuf = mte_mmap (LEN + 3 * ALIGN);105dbuf = mte_mmap (LEN + 3 * ALIGN);106int r = 0;107for (int i = 0; funtab[i].name; i++)108{109err_count = 0;110for (int d = 0; d < ALIGN; d++)111for (int s = 0; s < ALIGN; s++)112for (int n = 0; n < LEN; n++)113test (funtab + i, d, s, n);114115char *pass = funtab[i].test_mte && mte_enabled () ? "MTE PASS" : "PASS";116printf ("%s %s\n", err_count ? "FAIL" : pass, funtab[i].name);117if (err_count)118r = -1;119}120return r;121}122123124