1 /* 2 * strncmp test. 3 * 4 * Copyright (c) 2019-2020, Arm Limited. 5 * SPDX-License-Identifier: MIT 6 */ 7 8 #include <stdint.h> 9 #include <stdio.h> 10 #include <stdlib.h> 11 #include <string.h> 12 #include "mte.h" 13 #include "stringlib.h" 14 #include "stringtest.h" 15 16 #define F(x, mte) {#x, x, mte}, 17 18 static const struct fun 19 { 20 const char *name; 21 int (*fun) (const char *, const char *, size_t); 22 int test_mte; 23 } funtab[] = { 24 // clang-format off 25 F(strncmp, 0) 26 #if __aarch64__ 27 F(__strncmp_aarch64, 0) 28 F(__strncmp_aarch64_mte, 1) 29 # if __ARM_FEATURE_SVE 30 F(__strncmp_aarch64_sve, 1) 31 # endif 32 #endif 33 {0, 0, 0} 34 // clang-format on 35 }; 36 #undef F 37 38 #define A 32 39 #define LEN 250000 40 static char *s1buf; 41 static char *s2buf; 42 43 static void * 44 alignup (void *p) 45 { 46 return (void *) (((uintptr_t) p + A - 1) & -A); 47 } 48 49 static void 50 test (const struct fun *fun, int s1align, int s2align, int maxlen, int diffpos, 51 int len, int delta) 52 { 53 char *src1 = alignup (s1buf); 54 char *src2 = alignup (s2buf); 55 char *s1 = src1 + s1align; 56 char *s2 = src2 + s2align; 57 int r; 58 59 if (err_count >= ERR_LIMIT) 60 return; 61 if (len > LEN || s1align >= A || s2align >= A) 62 abort (); 63 if (diffpos >= len) 64 abort (); 65 if ((diffpos < 0) != (delta == 0)) 66 abort (); 67 68 for (int i = 0; i < len + A; i++) 69 src1[i] = src2[i] = '?'; 70 for (int i = 0; i < len; i++) 71 s1[i] = s2[i] = 'a' + i % 23; 72 if (delta) 73 s1[diffpos] += delta; 74 s1[len] = s2[len] = '\0'; 75 76 size_t mte_len = maxlen < len + 1 ? maxlen : len + 1; 77 s1 = tag_buffer (s1, mte_len, fun->test_mte); 78 s2 = tag_buffer (s2, mte_len, fun->test_mte); 79 r = fun->fun (s1, s2, maxlen); 80 untag_buffer (s1, mte_len, fun->test_mte); 81 untag_buffer (s2, mte_len, fun->test_mte); 82 83 if (diffpos >= maxlen) 84 { 85 diffpos = -1; 86 delta = 0; 87 } 88 if ((delta == 0 && r != 0) || (delta > 0 && r <= 0) || (delta < 0 && r >= 0)) 89 { 90 ERR ( 91 "%s(align %d, align %d, %d) (len=%d, diffpos=%d) failed, returned %d\n", 92 fun->name, s1align, s2align, maxlen, len, diffpos, r); 93 quoteat ("src1", src1, len + A, diffpos); 94 quoteat ("src2", src2, len + A, diffpos); 95 } 96 } 97 98 int 99 main () 100 { 101 s1buf = mte_mmap (LEN + 2 * A + 1); 102 s2buf = mte_mmap (LEN + 2 * A + 1); 103 int r = 0; 104 for (int i = 0; funtab[i].name; i++) 105 { 106 err_count = 0; 107 for (int d = 0; d < A; d++) 108 for (int s = 0; s < A; s++) 109 { 110 int n; 111 test (funtab + i, d, s, 0, -1, 0, 0); 112 test (funtab + i, d, s, 1, -1, 0, 0); 113 test (funtab + i, d, s, 0, -1, 1, 0); 114 test (funtab + i, d, s, 1, -1, 1, 0); 115 test (funtab + i, d, s, 2, -1, 1, 0); 116 test (funtab + i, d, s, 1, 0, 1, 1); 117 test (funtab + i, d, s, 1, 0, 1, -1); 118 for (n = 2; n < 100; n++) 119 { 120 test (funtab + i, d, s, n, -1, n, 0); 121 test (funtab + i, d, s, n, n / 2, n, 1); 122 test (funtab + i, d, s, n / 2, -1, n, 0); 123 test (funtab + i, d, s, n / 2, n / 2, n, -1); 124 } 125 for (; n < LEN; n *= 2) 126 { 127 test (funtab + i, d, s, n, -1, n, 0); 128 test (funtab + i, d, s, n, n / 2, n, -1); 129 test (funtab + i, d, s, n / 2, -1, n, 0); 130 test (funtab + i, d, s, n / 2, n / 2, n, 1); 131 } 132 } 133 char *pass = funtab[i].test_mte && mte_enabled () ? "MTE PASS" : "PASS"; 134 printf ("%s %s\n", err_count ? "FAIL" : pass, funtab[i].name); 135 if (err_count) 136 r = -1; 137 } 138 return r; 139 } 140