xref: /freebsd/contrib/arm-optimized-routines/string/test/strchrnul.c (revision 5956d97f4b3204318ceb6aa9c77bd0bc6ea87a41)
1 /*
2  * strchrnul test.
3  *
4  * Copyright (c) 2019-2020, Arm Limited.
5  * SPDX-License-Identifier: MIT
6  */
7 
8 #ifndef _GNU_SOURCE
9 #define _GNU_SOURCE
10 #endif
11 
12 #include <stdint.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <limits.h>
17 #include "mte.h"
18 #include "stringlib.h"
19 #include "stringtest.h"
20 
21 #define F(x, mte) {#x, x, mte},
22 
23 static const struct fun
24 {
25   const char *name;
26   char *(*fun) (const char *s, int c);
27   int test_mte;
28 } funtab[] = {
29   // clang-format off
30   F(strchrnul, 0)
31 #if __aarch64__
32   F(__strchrnul_aarch64, 0)
33   F(__strchrnul_aarch64_mte, 1)
34 # if __ARM_FEATURE_SVE
35   F(__strchrnul_aarch64_sve, 1)
36 # endif
37 #endif
38   {0, 0, 0}
39   // clang-format on
40 };
41 #undef F
42 
43 #define ALIGN 32
44 #define LEN 512
45 static char *sbuf;
46 
47 static void *
48 alignup (void *p)
49 {
50   return (void *) (((uintptr_t) p + ALIGN - 1) & -ALIGN);
51 }
52 
53 static void
54 test (const struct fun *fun, int align, int seekpos, int len)
55 {
56   char *src = alignup (sbuf);
57   char *s = src + align;
58   char *f = seekpos != -1 ? s + seekpos : s + len;
59   int seekchar = 0x1;
60   void *p;
61 
62   if (err_count >= ERR_LIMIT)
63     return;
64   if (len > LEN || seekpos >= len || align >= ALIGN)
65     abort ();
66 
67   for (int i = 0; src + i < s; i++)
68     src[i] = (i + len) & 1 ? seekchar : 0;
69   for (int i = 1; i <= ALIGN; i++)
70     s[len + i] = (i + len) & 1 ? seekchar : 0;
71   for (int i = 0; i < len; i++)
72     s[i] = 'a' + (i & 31);
73   if (seekpos != -1)
74     s[seekpos] = seekchar;
75   if (seekpos != -1 && (len + align) & 1)
76     s[seekpos + 1] = seekchar;
77   s[len] = '\0';
78 
79   int mte_len = seekpos != -1 ? seekpos + 1 : len + 1;
80   s = tag_buffer (s, mte_len, fun->test_mte);
81   p = fun->fun (s, seekchar);
82   untag_buffer (s, mte_len, fun->test_mte);
83   p = untag_pointer (p);
84 
85   if (p != f)
86     {
87       ERR ("%s (%p, 0x%02x) len %d returned %p, expected %p pos %d\n",
88 	   fun->name, s, seekchar, len, p, f, seekpos);
89       quote ("input", s, len);
90     }
91 
92   s = tag_buffer (s, len + 1, fun->test_mte);
93   p = fun->fun (s, 0);
94   untag_buffer (s, len + 1, fun->test_mte);
95 
96   if (p != s + len)
97     {
98       ERR ("%s (%p, 0x%02x) len %d returned %p, expected %p pos %d\n",
99 	   fun->name, s, 0, len, p, f, len);
100       quote ("input", s, len);
101     }
102 }
103 
104 int
105 main (void)
106 {
107   sbuf = mte_mmap (LEN + 3 * ALIGN);
108   int r = 0;
109   for (int i = 0; funtab[i].name; i++)
110     {
111       err_count = 0;
112       for (int a = 0; a < ALIGN; a++)
113 	for (int n = 0; n < LEN; n++)
114 	  {
115 	    for (int sp = 0; sp < n; sp++)
116 	      test (funtab + i, a, sp, n);
117 	    test (funtab + i, a, -1, n);
118 	  }
119 
120       char *pass = funtab[i].test_mte && mte_enabled () ? "MTE PASS" : "PASS";
121       printf ("%s %s\n", err_count ? "FAIL" : pass, funtab[i].name);
122       if (err_count)
123 	r = -1;
124     }
125   return r;
126 }
127