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