131914882SAlex Richardson /* 231914882SAlex Richardson * memmove test. 331914882SAlex Richardson * 431914882SAlex Richardson * Copyright (c) 2019-2020, Arm Limited. 531914882SAlex Richardson * SPDX-License-Identifier: MIT 631914882SAlex Richardson */ 731914882SAlex Richardson 831914882SAlex Richardson #include <stdint.h> 931914882SAlex Richardson #include <stdio.h> 1031914882SAlex Richardson #include <stdlib.h> 1131914882SAlex Richardson #include <string.h> 1231914882SAlex Richardson #include "mte.h" 1331914882SAlex Richardson #include "stringlib.h" 1431914882SAlex Richardson #include "stringtest.h" 1531914882SAlex Richardson 1631914882SAlex Richardson #define F(x, mte) {#x, x, mte}, 1731914882SAlex Richardson 1831914882SAlex Richardson static const struct fun 1931914882SAlex Richardson { 2031914882SAlex Richardson const char *name; 2131914882SAlex Richardson void *(*fun) (void *, const void *, size_t); 2231914882SAlex Richardson int test_mte; 2331914882SAlex Richardson } funtab[] = { 2431914882SAlex Richardson // clang-format off 2531914882SAlex Richardson F(memmove, 0) 2631914882SAlex Richardson #if __aarch64__ 2731914882SAlex Richardson F(__memmove_aarch64, 1) 2831914882SAlex Richardson # if __ARM_NEON 2931914882SAlex Richardson F(__memmove_aarch64_simd, 1) 3031914882SAlex Richardson # endif 31*d49ad206SAndrew Turner # if __ARM_FEATURE_SVE 32*d49ad206SAndrew Turner F(__memmove_aarch64_sve, 1) 33*d49ad206SAndrew Turner # endif 3431914882SAlex Richardson #endif 3531914882SAlex Richardson {0, 0, 0} 3631914882SAlex Richardson // clang-format on 3731914882SAlex Richardson }; 3831914882SAlex Richardson #undef F 3931914882SAlex Richardson 4031914882SAlex Richardson #define A 32 4131914882SAlex Richardson #define LEN 250000 4231914882SAlex Richardson static unsigned char *dbuf; 4331914882SAlex Richardson static unsigned char *sbuf; 4431914882SAlex Richardson static unsigned char wbuf[LEN + 2 * A]; 4531914882SAlex Richardson 4631914882SAlex Richardson static void * 4731914882SAlex Richardson alignup (void *p) 4831914882SAlex Richardson { 4931914882SAlex Richardson return (void *) (((uintptr_t) p + A - 1) & -A); 5031914882SAlex Richardson } 5131914882SAlex Richardson 5231914882SAlex Richardson static void 5331914882SAlex Richardson test (const struct fun *fun, int dalign, int salign, int len) 5431914882SAlex Richardson { 5531914882SAlex Richardson unsigned char *src = alignup (sbuf); 5631914882SAlex Richardson unsigned char *dst = alignup (dbuf); 5731914882SAlex Richardson unsigned char *want = wbuf; 5831914882SAlex Richardson unsigned char *s = src + salign; 5931914882SAlex Richardson unsigned char *d = dst + dalign; 6031914882SAlex Richardson unsigned char *w = want + dalign; 6131914882SAlex Richardson void *p; 6231914882SAlex Richardson int i; 6331914882SAlex Richardson 6431914882SAlex Richardson if (err_count >= ERR_LIMIT) 6531914882SAlex Richardson return; 6631914882SAlex Richardson if (len > LEN || dalign >= A || salign >= A) 6731914882SAlex Richardson abort (); 6831914882SAlex Richardson for (i = 0; i < len + A; i++) 6931914882SAlex Richardson { 7031914882SAlex Richardson src[i] = '?'; 7131914882SAlex Richardson want[i] = dst[i] = '*'; 7231914882SAlex Richardson } 7331914882SAlex Richardson for (i = 0; i < len; i++) 7431914882SAlex Richardson s[i] = w[i] = 'a' + i % 23; 7531914882SAlex Richardson 7631914882SAlex Richardson p = fun->fun (d, s, len); 7731914882SAlex Richardson if (p != d) 7831914882SAlex Richardson ERR ("%s(%p,..) returned %p\n", fun->name, d, p); 7931914882SAlex Richardson for (i = 0; i < len + A; i++) 8031914882SAlex Richardson { 8131914882SAlex Richardson if (dst[i] != want[i]) 8231914882SAlex Richardson { 8331914882SAlex Richardson ERR ("%s(align %d, align %d, %d) failed\n", fun->name, dalign, salign, 8431914882SAlex Richardson len); 8531914882SAlex Richardson quoteat ("got", dst, len + A, i); 8631914882SAlex Richardson quoteat ("want", want, len + A, i); 8731914882SAlex Richardson break; 8831914882SAlex Richardson } 8931914882SAlex Richardson } 9031914882SAlex Richardson } 9131914882SAlex Richardson 9231914882SAlex Richardson static void 9331914882SAlex Richardson test_overlap (const struct fun *fun, int dalign, int salign, int len) 9431914882SAlex Richardson { 9531914882SAlex Richardson unsigned char *src = alignup (sbuf); 9631914882SAlex Richardson unsigned char *dst = src; 9731914882SAlex Richardson unsigned char *want = wbuf; 9831914882SAlex Richardson unsigned char *s = src + salign; 9931914882SAlex Richardson unsigned char *d = dst + dalign; 10031914882SAlex Richardson unsigned char *w = wbuf + dalign; 10131914882SAlex Richardson void *p; 10231914882SAlex Richardson 10331914882SAlex Richardson if (err_count >= ERR_LIMIT) 10431914882SAlex Richardson return; 10531914882SAlex Richardson if (len > LEN || dalign >= A || salign >= A) 10631914882SAlex Richardson abort (); 10731914882SAlex Richardson 10831914882SAlex Richardson for (int i = 0; i < len + A; i++) 10931914882SAlex Richardson src[i] = want[i] = '?'; 11031914882SAlex Richardson 11131914882SAlex Richardson for (int i = 0; i < len; i++) 11231914882SAlex Richardson s[i] = want[salign + i] = 'a' + i % 23; 11331914882SAlex Richardson for (int i = 0; i < len; i++) 11431914882SAlex Richardson w[i] = s[i]; 11531914882SAlex Richardson 11631914882SAlex Richardson s = tag_buffer (s, len, fun->test_mte); 11731914882SAlex Richardson d = tag_buffer (d, len, fun->test_mte); 11831914882SAlex Richardson p = fun->fun (d, s, len); 11931914882SAlex Richardson untag_buffer (s, len, fun->test_mte); 12031914882SAlex Richardson untag_buffer (d, len, fun->test_mte); 12131914882SAlex Richardson 12231914882SAlex Richardson if (p != d) 12331914882SAlex Richardson ERR ("%s(%p,..) returned %p\n", fun->name, d, p); 12431914882SAlex Richardson for (int i = 0; i < len + A; i++) 12531914882SAlex Richardson { 12631914882SAlex Richardson if (dst[i] != want[i]) 12731914882SAlex Richardson { 12831914882SAlex Richardson ERR ("%s(align %d, align %d, %d) failed\n", fun->name, dalign, salign, 12931914882SAlex Richardson len); 13031914882SAlex Richardson quoteat ("got", dst, len + A, i); 13131914882SAlex Richardson quoteat ("want", want, len + A, i); 13231914882SAlex Richardson break; 13331914882SAlex Richardson } 13431914882SAlex Richardson } 13531914882SAlex Richardson } 13631914882SAlex Richardson 13731914882SAlex Richardson int 13831914882SAlex Richardson main () 13931914882SAlex Richardson { 14031914882SAlex Richardson dbuf = mte_mmap (LEN + 2 * A); 14131914882SAlex Richardson sbuf = mte_mmap (LEN + 2 * A); 14231914882SAlex Richardson int r = 0; 14331914882SAlex Richardson for (int i = 0; funtab[i].name; i++) 14431914882SAlex Richardson { 14531914882SAlex Richardson err_count = 0; 14631914882SAlex Richardson for (int d = 0; d < A; d++) 14731914882SAlex Richardson for (int s = 0; s < A; s++) 14831914882SAlex Richardson { 14931914882SAlex Richardson int n; 15031914882SAlex Richardson for (n = 0; n < 100; n++) 15131914882SAlex Richardson { 15231914882SAlex Richardson test (funtab + i, d, s, n); 15331914882SAlex Richardson test_overlap (funtab + i, d, s, n); 15431914882SAlex Richardson } 15531914882SAlex Richardson for (; n < LEN; n *= 2) 15631914882SAlex Richardson { 15731914882SAlex Richardson test (funtab + i, d, s, n); 15831914882SAlex Richardson test_overlap (funtab + i, d, s, n); 15931914882SAlex Richardson } 16031914882SAlex Richardson } 16131914882SAlex Richardson char *pass = funtab[i].test_mte && mte_enabled () ? "MTE PASS" : "PASS"; 16231914882SAlex Richardson printf ("%s %s\n", err_count ? "FAIL" : pass, funtab[i].name); 16331914882SAlex Richardson if (err_count) 16431914882SAlex Richardson r = -1; 16531914882SAlex Richardson } 16631914882SAlex Richardson return r; 16731914882SAlex Richardson } 168