1 /* 2 * strlen 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 <sys/mman.h> 13 #include <limits.h> 14 #include "mte.h" 15 #include "stringlib.h" 16 #include "stringtest.h" 17 18 #define F(x, mte) {#x, x, mte}, 19 20 static const struct fun 21 { 22 const char *name; 23 size_t (*fun) (const char *s); 24 int test_mte; 25 } funtab[] = { 26 // clang-format off 27 F(strlen, 0) 28 #if __aarch64__ 29 F(__strlen_aarch64, 0) 30 F(__strlen_aarch64_mte, 1) 31 # if __ARM_FEATURE_SVE 32 F(__strlen_aarch64_sve, 1) 33 # endif 34 #elif __arm__ 35 # if __ARM_ARCH >= 6 && __ARM_ARCH_ISA_THUMB == 2 36 F(__strlen_armv6t2, 0) 37 # endif 38 #endif 39 {0, 0, 0} 40 // clang-format on 41 }; 42 #undef F 43 44 #define ALIGN 32 45 #define LEN 512 46 static char *sbuf; 47 48 static void * 49 alignup (void *p) 50 { 51 return (void *) (((uintptr_t) p + ALIGN - 1) & -ALIGN); 52 } 53 54 static void 55 test (const struct fun *fun, int align, int len) 56 { 57 char *src = alignup (sbuf); 58 char *s = src + align; 59 size_t r; 60 61 if (err_count >= ERR_LIMIT) 62 return; 63 if (len > LEN || align >= ALIGN) 64 abort (); 65 66 for (int i = 0; src + i < s; i++) 67 src[i] = 0; 68 for (int i = 1; i <= ALIGN; i++) 69 s[len + i] = (len + align) & 1 ? 1 : 0; 70 for (int i = 0; i < len; i++) 71 s[i] = 'a' + (i & 31); 72 s[len] = '\0'; 73 74 s = tag_buffer (s, len + 1, fun->test_mte); 75 r = fun->fun (s); 76 untag_buffer (s, len + 1, fun->test_mte); 77 78 if (r != len) 79 { 80 ERR ("%s (%p) returned %zu expected %d\n", fun->name, s, r, len); 81 quote ("input", src, len); 82 } 83 } 84 85 int 86 main (void) 87 { 88 sbuf = mte_mmap (LEN + 3 * ALIGN); 89 int r = 0; 90 for (int i = 0; funtab[i].name; i++) 91 { 92 err_count = 0; 93 for (int a = 0; a < ALIGN; a++) 94 for (int n = 0; n < LEN; n++) 95 test (funtab + i, a, n); 96 97 char *pass = funtab[i].test_mte && mte_enabled () ? "MTE PASS" : "PASS"; 98 printf ("%s %s\n", err_count ? "FAIL" : pass, funtab[i].name); 99 if (err_count) 100 r = -1; 101 } 102 return r; 103 } 104