From 778e766f6b6342ad50047c2e947fb9e6ee5b0240 Mon Sep 17 00:00:00 2001 From: "Justin C. Miller" Date: Wed, 12 Jul 2023 19:46:02 -0700 Subject: [PATCH] [libj6] Fix a memcpy return address bug My `REP.MOVSB` `memcpy` implementation had marked its C++ variable constraints as output instead of input, causing the compiler to emit code to copy the values of `$rsi` and `$rdi` back into the `src` and `dst` pointers, so after the copy `dst` pointed to the memory just beyond what had been copied. Very few places actually used the return value from `memcpy`, so this went unnoticed for a bit.. --- src/libraries/j6/memutils.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/libraries/j6/memutils.cpp b/src/libraries/j6/memutils.cpp index 65b49ec..ff4fd1f 100644 --- a/src/libraries/j6/memutils.cpp +++ b/src/libraries/j6/memutils.cpp @@ -7,9 +7,12 @@ using namespace j6; using namespace __j6libc; -void *memcpy(void * restrict s1, const void * restrict s2, size_t n) { - asm volatile ("rep movsb" : "+D"(s1), "+S"(s2), "+c"(n) :: "memory"); - return s1; +void *memcpy(void * restrict dst, const void * restrict src, size_t n) { + asm volatile ("rep movsb" + : + : "D"(dst), "S"(src), "c"(n) + : "memory"); + return dst; } static void memmove_dispatch(char *s1, const char *s2, size_t n) {