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