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