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