// sizes.c: every size from 0 to 1024 at every destination alignment from 0 // to 63, with guard bytes on both sides of the buffer. // // There is no libc for the target, so this is freestanding: it talks to // Linux with svc and checks every byte with its own loops. // // clang --target=aarch64-linux-gnu -O2 -ffreestanding -nostdlib -static // -fuse-ld=lld test/sizes.c build/memcpy.o build/memset.o typedef unsigned long size_t; typedef unsigned char u8; void *memcpy_neon(void *dst, const void *src, size_t n); void *memset_neon(void *dst, int c, size_t n); #define MAX 1024 #define GUARD 64 static u8 src[MAX + 2 * GUARD]; static u8 dst[MAX + 2 * GUARD + 64]; static long sys3(long nr, long a, long b, long c) { register long x8 __asm__("x8") = nr; register long x0 __asm__("x0") = a; register long x1 __asm__("x1") = b; register long x2 __asm__("x2") = c; __asm__ volatile("svc #0" : "+r"(x0) : "r"(x8), "r"(x1), "r"(x2) : "memory"); return x0; } static void say(const char *s) { size_t n = 0; while (s[n]) n++; sys3(64, 1, (long)s, (long)n); // write(1, s, n) } static void say_number(unsigned long v) { char text[24]; int i = sizeof text; text[--i] = 0; do text[--i] = (char)('0' + v % 10); while (v /= 10); say(text + i); } static int failures; static void fail(const char *what, size_t n, size_t align, const char *how) { if (failures++ >= 8) return; say(what); say("(n="); say_number(n); say(", align="); say_number(align); say("): "); say(how); say("\n"); } static void check(const char *what, size_t n, size_t align, const u8 *expect) { size_t start = GUARD + align; for (size_t i = 0; i < start; i++) if (dst[i] != 0xEE) return fail(what, n, align, "wrote before the buffer"); for (size_t i = 0; i < n; i++) if (dst[start + i] != (expect ? expect[i] : 0x5A)) return fail(what, n, align, "wrong bytes"); for (size_t i = start + n; i < sizeof dst; i++) if (dst[i] != 0xEE) return fail(what, n, align, "wrote past the end"); } static void guard(void) { for (size_t i = 0; i < sizeof dst; i++) ((volatile u8 *)dst)[i] = 0xEE; } static int run(void) { struct { char sysname[65], nodename[65], release[65], version[65], machine[65], domain[65]; } uts; for (size_t i = 0; i < sizeof src; i++) src[i] = (u8)(i * 131 + 7); for (size_t align = 0; align < 64; align++) for (size_t n = 0; n <= MAX; n++) { guard(); memcpy_neon(dst + GUARD + align, src + GUARD, n); check("memcpy", n, align, src + GUARD); guard(); memset_neon(dst + GUARD + align, 0x5A, n); check("memset", n, align, 0); } say("sizes: "); say_number(2 * 64 * (MAX + 1)); say(" calls, "); say_number(failures); say(" failures"); if (sys3(160, (long)&uts, 0, 0) == 0) { // uname say(" on "); say(uts.machine); } say("\n"); return failures != 0; } __attribute__((noreturn)) void _start(void) { sys3(94, run(), 0, 0); // exit_group __builtin_unreachable(); }