xref: /freebsd-src/contrib/arm-optimized-routines/string/test/strnlen.c (revision 072a4ba82a01476eaee33781ccd241033eefcf0b)
131914882SAlex Richardson /*
231914882SAlex Richardson  * strnlen test.
331914882SAlex Richardson  *
431914882SAlex Richardson  * Copyright (c) 2019-2020, Arm Limited.
5*072a4ba8SAndrew Turner  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
631914882SAlex Richardson  */
731914882SAlex Richardson 
831914882SAlex Richardson #ifndef _GNU_SOURCE
931914882SAlex Richardson #define _GNU_SOURCE
1031914882SAlex Richardson #endif
1131914882SAlex Richardson 
1231914882SAlex Richardson #include <stdint.h>
1331914882SAlex Richardson #include <stdio.h>
1431914882SAlex Richardson #include <stdlib.h>
1531914882SAlex Richardson #include <string.h>
1631914882SAlex Richardson #include <limits.h>
1731914882SAlex Richardson #include "mte.h"
1831914882SAlex Richardson #include "stringlib.h"
1931914882SAlex Richardson #include "stringtest.h"
2031914882SAlex Richardson 
2131914882SAlex Richardson #define F(x, mte) {#x, x, mte},
2231914882SAlex Richardson 
2331914882SAlex Richardson static const struct fun
2431914882SAlex Richardson {
2531914882SAlex Richardson   const char *name;
2631914882SAlex Richardson   size_t (*fun) (const char *s, size_t m);
2731914882SAlex Richardson   int test_mte;
2831914882SAlex Richardson } funtab[] = {
2931914882SAlex Richardson   // clang-format off
3031914882SAlex Richardson   F(strnlen, 0)
3131914882SAlex Richardson #if __aarch64__
3231914882SAlex Richardson   F(__strnlen_aarch64, 1)
3331914882SAlex Richardson # if __ARM_FEATURE_SVE
3431914882SAlex Richardson   F(__strnlen_aarch64_sve, 1)
3531914882SAlex Richardson # endif
3631914882SAlex Richardson #endif
3731914882SAlex Richardson   {0, 0, 0}
3831914882SAlex Richardson   // clang-format on
3931914882SAlex Richardson };
4031914882SAlex Richardson #undef F
4131914882SAlex Richardson 
4231914882SAlex Richardson #define ALIGN 32
4331914882SAlex Richardson #define LEN 512
4431914882SAlex Richardson static char *sbuf;
4531914882SAlex Richardson 
4631914882SAlex Richardson static void *
alignup(void * p)4731914882SAlex Richardson alignup (void *p)
4831914882SAlex Richardson {
4931914882SAlex Richardson   return (void *) (((uintptr_t) p + ALIGN - 1) & -ALIGN);
5031914882SAlex Richardson }
5131914882SAlex Richardson 
5231914882SAlex Richardson static void
test(const struct fun * fun,int align,size_t maxlen,size_t len)5331914882SAlex Richardson test (const struct fun *fun, int align, size_t maxlen, size_t len)
5431914882SAlex Richardson {
5531914882SAlex Richardson   char *src = alignup (sbuf);
5631914882SAlex Richardson   char *s = src + align;
5731914882SAlex Richardson   size_t r;
5831914882SAlex Richardson   size_t e = maxlen < len ? maxlen : len;
5931914882SAlex Richardson 
6031914882SAlex Richardson   if (err_count >= ERR_LIMIT)
6131914882SAlex Richardson     return;
6231914882SAlex Richardson   if (len > LEN || align >= ALIGN)
6331914882SAlex Richardson     abort ();
6431914882SAlex Richardson 
6531914882SAlex Richardson   for (int i = 0; src + i < s; i++)
6631914882SAlex Richardson     src[i] = 0;
6731914882SAlex Richardson   for (int i = 1; i <= ALIGN; i++)
6831914882SAlex Richardson     s[len + i] = (len + align) & 1 ? 1 : 0;
6931914882SAlex Richardson   for (int i = 0; i < len; i++)
7031914882SAlex Richardson     s[i] = 'a' + (i & 31);
7131914882SAlex Richardson   s[len] = 0;
7231914882SAlex Richardson   if ((len + align) & 1)
7331914882SAlex Richardson     s[e + 1] = 0;
7431914882SAlex Richardson 
7531914882SAlex Richardson   size_t mte_len = maxlen < len + 1 ? maxlen : len + 1;
7631914882SAlex Richardson   s = tag_buffer (s, mte_len, fun->test_mte);
7731914882SAlex Richardson   r = fun->fun (s, maxlen);
7831914882SAlex Richardson   untag_buffer (s, mte_len, fun->test_mte);
7931914882SAlex Richardson 
8031914882SAlex Richardson   if (r != e)
8131914882SAlex Richardson     {
8231914882SAlex Richardson       ERR ("%s (%p, %zu) len %zu returned %zu, expected %zu\n",
8331914882SAlex Richardson 	   fun->name, s, maxlen, len, r, e);
8431914882SAlex Richardson       quote ("input", s, len);
8531914882SAlex Richardson     }
8631914882SAlex Richardson }
8731914882SAlex Richardson 
8831914882SAlex Richardson int
main(void)8931914882SAlex Richardson main (void)
9031914882SAlex Richardson {
9131914882SAlex Richardson   sbuf = mte_mmap (LEN + 3 * ALIGN);
9231914882SAlex Richardson   int r = 0;
9331914882SAlex Richardson   for (int i = 0; funtab[i].name; i++)
9431914882SAlex Richardson     {
9531914882SAlex Richardson       err_count = 0;
9631914882SAlex Richardson       for (int a = 0; a < ALIGN; a++)
9731914882SAlex Richardson 	for (int n = 0; n < LEN; n++)
9831914882SAlex Richardson 	  {
9931914882SAlex Richardson 	    for (int maxlen = 0; maxlen < LEN; maxlen++)
10031914882SAlex Richardson 	      test (funtab + i, a, maxlen, n);
10131914882SAlex Richardson 	    test (funtab + i, a, SIZE_MAX - a, n);
10231914882SAlex Richardson 	  }
10331914882SAlex Richardson       char *pass = funtab[i].test_mte && mte_enabled () ? "MTE PASS" : "PASS";
10431914882SAlex Richardson       printf ("%s %s\n", err_count ? "FAIL" : pass, funtab[i].name);
10531914882SAlex Richardson       if (err_count)
10631914882SAlex Richardson 	r = -1;
10731914882SAlex Richardson     }
10831914882SAlex Richardson   return r;
10931914882SAlex Richardson }
110