1 /* 2 * memcpy 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(memcpy, 0) 26 #if __aarch64__ 27 F(__memcpy_aarch64, 1) 28 # if __ARM_NEON 29 F(__memcpy_aarch64_simd, 1) 30 # endif 31 # if __ARM_FEATURE_SVE 32 F(__memcpy_aarch64_sve, 1) 33 # endif 34 #elif __arm__ 35 F(__memcpy_arm, 0) 36 #endif 37 {0, 0, 0} 38 // clang-format on 39 }; 40 #undef F 41 42 #define A 32 43 #define LEN 250000 44 static unsigned char *dbuf; 45 static unsigned char *sbuf; 46 static unsigned char wbuf[LEN + 2 * A]; 47 48 static void * 49 alignup (void *p) 50 { 51 return (void *) (((uintptr_t) p + A - 1) & -A); 52 } 53 54 static void 55 test (const struct fun *fun, int dalign, int salign, int len) 56 { 57 unsigned char *src = alignup (sbuf); 58 unsigned char *dst = alignup (dbuf); 59 unsigned char *want = wbuf; 60 unsigned char *s = src + salign; 61 unsigned char *d = dst + dalign; 62 unsigned char *w = want + dalign; 63 void *p; 64 int i; 65 66 if (err_count >= ERR_LIMIT) 67 return; 68 if (len > LEN || dalign >= A || salign >= A) 69 abort (); 70 for (i = 0; i < len + A; i++) 71 { 72 src[i] = '?'; 73 want[i] = dst[i] = '*'; 74 } 75 for (i = 0; i < len; i++) 76 s[i] = w[i] = 'a' + i % 23; 77 78 s = tag_buffer (s, len, fun->test_mte); 79 d = tag_buffer (d, len, fun->test_mte); 80 p = fun->fun (d, s, len); 81 untag_buffer (s, len, fun->test_mte); 82 untag_buffer (d, len, fun->test_mte); 83 84 if (p != d) 85 ERR ("%s(%p,..) returned %p\n", fun->name, d, p); 86 for (i = 0; i < len + A; i++) 87 { 88 if (dst[i] != want[i]) 89 { 90 ERR ("%s(align %d, align %d, %d) failed\n", fun->name, dalign, salign, 91 len); 92 quoteat ("got", dst, len + A, i); 93 quoteat ("want", want, len + A, i); 94 break; 95 } 96 } 97 } 98 99 int 100 main () 101 { 102 dbuf = mte_mmap (LEN + 2 * A); 103 sbuf = mte_mmap (LEN + 2 * A); 104 int r = 0; 105 for (int i = 0; funtab[i].name; i++) 106 { 107 err_count = 0; 108 for (int d = 0; d < A; d++) 109 for (int s = 0; s < A; s++) 110 { 111 int n; 112 for (n = 0; n < 100; n++) 113 test (funtab + i, d, s, n); 114 for (; n < LEN; n *= 2) 115 test (funtab + i, d, s, n); 116 } 117 char *pass = funtab[i].test_mte && mte_enabled () ? "MTE PASS" : "PASS"; 118 printf ("%s %s\n", err_count ? "FAIL" : pass, funtab[i].name); 119 if (err_count) 120 r = -1; 121 } 122 return r; 123 } 124