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