1 /* 2 * memmove test. 3 * 4 * Copyright (c) 2019-2023, Arm Limited. 5 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception 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 void *(*fun) (void *, const void *, size_t); 22 int test_mte; 23 } funtab[] = { 24 // clang-format off 25 F(memmove, 0) 26 #if __aarch64__ 27 F(__memmove_aarch64, 1) 28 F(__memmove_aarch64_simd, 1) 29 # if __ARM_FEATURE_SVE 30 F(__memmove_aarch64_sve, 1) 31 # endif 32 # if WANT_MOPS 33 F(__memmove_aarch64_mops, 1) 34 # endif 35 #endif 36 {0, 0, 0} 37 // clang-format on 38 }; 39 #undef F 40 41 #define A 32 42 #define LEN 250000 43 static unsigned char *dbuf; 44 static unsigned char *sbuf; 45 static unsigned char wbuf[LEN + 2 * A]; 46 47 static void * 48 alignup (void *p) 49 { 50 return (void *) (((uintptr_t) p + A - 1) & -A); 51 } 52 53 static void 54 test (const struct fun *fun, int dalign, int salign, int len) 55 { 56 unsigned char *src = alignup (sbuf); 57 unsigned char *dst = alignup (dbuf); 58 unsigned char *want = wbuf; 59 unsigned char *s = src + salign; 60 unsigned char *d = dst + dalign; 61 unsigned char *w = want + dalign; 62 void *p; 63 int i; 64 65 if (err_count >= ERR_LIMIT) 66 return; 67 if (len > LEN || dalign >= A || salign >= A) 68 abort (); 69 for (i = 0; i < len + A; i++) 70 { 71 src[i] = '?'; 72 want[i] = dst[i] = '*'; 73 } 74 for (i = 0; i < len; i++) 75 s[i] = w[i] = 'a' + i % 23; 76 77 p = fun->fun (d, s, len); 78 if (p != d) 79 ERR ("%s(%p,..) returned %p\n", fun->name, d, p); 80 for (i = 0; i < len + A; i++) 81 { 82 if (dst[i] != want[i]) 83 { 84 ERR ("%s(align %d, align %d, %d) failed\n", fun->name, dalign, salign, 85 len); 86 quoteat ("got", dst, len + A, i); 87 quoteat ("want", want, len + A, i); 88 break; 89 } 90 } 91 } 92 93 static void 94 test_overlap (const struct fun *fun, int dalign, int salign, int len) 95 { 96 unsigned char *src = alignup (sbuf); 97 unsigned char *dst = src; 98 unsigned char *want = wbuf; 99 unsigned char *s = src + salign; 100 unsigned char *d = dst + dalign; 101 unsigned char *w = wbuf + dalign; 102 void *p; 103 104 if (err_count >= ERR_LIMIT) 105 return; 106 if (len > LEN || dalign >= A || salign >= A) 107 abort (); 108 109 for (int i = 0; i < len + A; i++) 110 src[i] = want[i] = '?'; 111 112 for (int i = 0; i < len; i++) 113 s[i] = want[salign + i] = 'a' + i % 23; 114 for (int i = 0; i < len; i++) 115 w[i] = s[i]; 116 117 s = tag_buffer (s, len, fun->test_mte); 118 d = tag_buffer (d, len, fun->test_mte); 119 p = fun->fun (d, s, len); 120 untag_buffer (s, len, fun->test_mte); 121 untag_buffer (d, len, fun->test_mte); 122 123 if (p != d) 124 ERR ("%s(%p,..) returned %p\n", fun->name, d, p); 125 for (int i = 0; i < len + A; i++) 126 { 127 if (dst[i] != want[i]) 128 { 129 ERR ("%s(align %d, align %d, %d) failed\n", fun->name, dalign, salign, 130 len); 131 quoteat ("got", dst, len + A, i); 132 quoteat ("want", want, len + A, i); 133 break; 134 } 135 } 136 } 137 138 int 139 main () 140 { 141 dbuf = mte_mmap (LEN + 2 * A); 142 sbuf = mte_mmap (LEN + 2 * A); 143 int r = 0; 144 for (int i = 0; funtab[i].name; i++) 145 { 146 err_count = 0; 147 for (int d = 0; d < A; d++) 148 for (int s = 0; s < A; s++) 149 { 150 int n; 151 for (n = 0; n < 100; n++) 152 { 153 test (funtab + i, d, s, n); 154 test_overlap (funtab + i, d, s, n); 155 } 156 for (; n < LEN; n *= 2) 157 { 158 test (funtab + i, d, s, n); 159 test_overlap (funtab + i, d, s, n); 160 } 161 } 162 char *pass = funtab[i].test_mte && mte_enabled () ? "MTE PASS" : "PASS"; 163 printf ("%s %s\n", err_count ? "FAIL" : pass, funtab[i].name); 164 if (err_count) 165 r = -1; 166 } 167 return r; 168 } 169