| Message ID | ffa4261e-e0e7-474c-a497-3808928681e2@ya2.so-net.ne.jp |
|---|---|
| State | New |
| Headers |
Return-Path: <newlib-bounces~patchwork=sourceware.org@sourceware.org> X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id 7C38A4BA9005 for <patchwork@sourceware.org>; Sat, 28 Mar 2026 01:36:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7C38A4BA9005 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1774661778; bh=b2AF0jKIUoowzCQ6SuTHktbEbWFt1TwmRYywL5sXnK0=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=nhM5ok61bmo0yqkfjA6eVXI8G/LNFKkicNltr/mEA9vrkKZOu2jmoXWWLBzC+zX0p vjkOfvAE2dKOj5HfV9AWfYkjZaSq4c9/VOPLU4UqV4Zy+5SUDwN/F1DMn3AybVs9U4 DPm+Eikopw+i6nmJsxpsSQRB5af3Ijj0QeQyiZIY= X-Original-To: newlib@sourceware.org Delivered-To: newlib@sourceware.org Received: from hsmtpd-def-c1101.xspmail.jp (hsmtpd-def-c1101.xspmail.jp [220.100.193.65]) by sourceware.org (Postfix) with ESMTPS id 63D704BA23D6 for <newlib@sourceware.org>; Sat, 28 Mar 2026 01:35:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 63D704BA23D6 ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 63D704BA23D6 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1774661743; cv=none; b=gVErVYz9pkryxq3fueKbQUYw6khrcdEPm0VTdVpbPU8uzdRjO35ZLLFNiZuzrDlXuVjiiCTe4jSaAJm4aDq6fGi8/fqpiGm18cAXW2mXhYCvpCthX8cif5b0DBJ2hWzDzfq4nGUI5OLLRV9IGifmqIiL9OOWgMWc2UcJr8PAMvQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1774661743; c=relaxed/simple; bh=x2PQSVhSukZrpJABZwuKPRQHBV8rlaWGFfZpElFrMHg=; h=DKIM-Signature:Message-ID:Date:MIME-Version:To:From:Subject; b=LJKnvwx2Ux21CuHUo36yOV5DFXPI5JG1spXnUgg3AprSTIc4tCUUKusbcxbLM3wOsYYc9Bv023b5Wtv1ZjJ0DCqrVI8oIUE1jkmj8XGwGtefrfaHJrocQH88hs1oHut5i7qDeJ0SDG12NsDArLgjDaIPx4JCXaCb9vt8mGAdU+Y= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 63D704BA23D6 X-Country-Code: JP Received: from [192.168.1.59] (fp76f100cd.tkyc628.ap.nuro.jp [118.241.0.205]) by hsmtpd-out-0.sonet.c1101.xspmail.jp (Halon) with ESMTPSA (TLSv1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 id efcc073d-5f6c-46ee-ae01-313f0efa3fcf; Sat, 28 Mar 2026 10:35:40 +0900 (JST) Message-ID: <ffa4261e-e0e7-474c-a497-3808928681e2@ya2.so-net.ne.jp> Date: Sat, 28 Mar 2026 10:35:38 +0900 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: newlib@sourceware.org Subject: Bug in memcpy() for m68k Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-13.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_PASS, SPF_PASS, TXREP, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: newlib@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Newlib mailing list <newlib.sourceware.org> List-Unsubscribe: <https://sourceware.org/mailman/options/newlib>, <mailto:newlib-request@sourceware.org?subject=unsubscribe> List-Archive: <https://sourceware.org/pipermail/newlib/> List-Post: <mailto:newlib@sourceware.org> List-Help: <mailto:newlib-request@sourceware.org?subject=help> List-Subscribe: <https://sourceware.org/mailman/listinfo/newlib>, <mailto:newlib-request@sourceware.org?subject=subscribe> From: Yuichi Nakamura via Newlib <newlib@sourceware.org> Reply-To: Yuichi Nakamura <yunk@ya2.so-net.ne.jp> Errors-To: newlib-bounces~patchwork=sourceware.org@sourceware.org |
| Series |
Bug in memcpy() for m68k
|
|
Commit Message
Yuichi Nakamura
March 28, 2026, 1:35 a.m. UTC
Hi all,
I found the following issues in the m68k version of memcpy(). In
particular, issue #1 is critical because it causes incorrect behavior on
the 68000.
1. On CPUs that do not support misaligned access (MISALIGNED_OK=0), such
as the 68000, memcpy() does not correctly copy regions larger than 64 KB
when the destination is not long-word aligned.
2. The 68020 supports misaligned access, but this is not currently
implemented.
3. The 68000 can access long-word data at even addresses, but alignment
is checked as if the address must be a multiple of 4.
4. Because the loop count is checked using signed comparison, memcpy()
fails for data sizes larger than 2 GB.
The following patch fixes these issues.
Best regards,
Yuichi Nakamura
#else
# define MISALIGNED_OK 0
@@ -49,10 +49,10 @@ SYM(memcpy):
#if !MISALIGNED_OK
/* Goto .Lresidue if either dest or src is not 4-byte aligned */
move.l a0,d0
- and.l #3,d0
+ and.l #1,d0
bne .Lresidue
move.l a1,d0
- and.l #3,d0
+ and.l #1,d0
bne .Lresidue
#else /* MISALIGNED_OK */
/* align dest */
@@ -95,7 +95,7 @@ SYM(memcpy):
#else
subq.l #1,d0
#endif
- bpl 1b
+ bcc 1b
bra .Lresidue
1:
@@ -104,9 +104,13 @@ SYM(memcpy):
.Lresidue:
#if !defined (__mcoldfire__)
dbra d1,1b | loop until done
+#if !MISALIGNED_OK
+ sub.l #0x10000,d1
+ bcc 1b
+#endif /* !MISALIGNED_OK */
#else
subq.l #1,d1
- bpl 1b
+ bcc 1b
#endif
move.l 4(sp),d0 | return value
rts
Comments
Hi Yuichi, unfortunately your patch doesn't apply cleanly due to whitespace issues. Can you please create a patch with `git format-patch' and send it via `git send-email' to this list? Alternatively, append the patch created with git format-patch as attachment to your mail. This should avoid whitespace issue entirely. Thanks, Corinna On Mar 28 10:35, Yuichi Nakamura via Newlib wrote: > Hi all, > > I found the following issues in the m68k version of memcpy(). In particular, > issue #1 is critical because it causes incorrect behavior on the 68000. > > 1. On CPUs that do not support misaligned access (MISALIGNED_OK=0), such as > the 68000, memcpy() does not correctly copy regions larger than 64 KB when > the destination is not long-word aligned. > 2. The 68020 supports misaligned access, but this is not currently > implemented. > 3. The 68000 can access long-word data at even addresses, but alignment is > checked as if the address must be a multiple of 4. > 4. Because the loop count is checked using signed comparison, memcpy() fails > for data sizes larger than 2 GB. > > The following patch fixes these issues. > > Best regards, > Yuichi Nakamura > > > diff --git a/newlib/libc/machine/m68k/memcpy.S > b/newlib/libc/machine/m68k/memcpy.S > index 464da95ef..ecf1da611 100644 > --- a/newlib/libc/machine/m68k/memcpy.S > +++ b/newlib/libc/machine/m68k/memcpy.S > @@ -15,7 +15,7 @@ > > #include "m68kasm.h" > > -#if defined (__mcoldfire__) || defined (__mc68030__) || defined > (__mc68040__) || defined (__mc68060__) > +#if defined (__mcoldfire__) || defined (__mc68020__) || defined > (__mc68030__) || defined (__mc68040__) || defined (__mc68060__) > # define MISALIGNED_OK 1 > #else > # define MISALIGNED_OK 0 > @@ -49,10 +49,10 @@ SYM(memcpy): > #if !MISALIGNED_OK > /* Goto .Lresidue if either dest or src is not 4-byte aligned */ > move.l a0,d0 > - and.l #3,d0 > + and.l #1,d0 > bne .Lresidue > move.l a1,d0 > - and.l #3,d0 > + and.l #1,d0 > bne .Lresidue > #else /* MISALIGNED_OK */ > /* align dest */ > @@ -95,7 +95,7 @@ SYM(memcpy): > #else > subq.l #1,d0 > #endif > - bpl 1b > + bcc 1b > bra .Lresidue > > 1: > @@ -104,9 +104,13 @@ SYM(memcpy): > .Lresidue: > #if !defined (__mcoldfire__) > dbra d1,1b | loop until done > +#if !MISALIGNED_OK > + sub.l #0x10000,d1 > + bcc 1b > +#endif /* !MISALIGNED_OK */ > #else > subq.l #1,d1 > - bpl 1b > + bcc 1b > #endif > move.l 4(sp),d0 | return value > rts
Hi Corinna, Thank you for the advice. I have attached the patch to this email. Best regards, Yuichi Nakamura On 2026/03/30 17:47, Corinna Vinschen wrote: > Hi Yuichi, > > unfortunately your patch doesn't apply cleanly due to whitespace issues. > > Can you please create a patch with `git format-patch' and send it via > `git send-email' to this list? Alternatively, append the patch created > with git format-patch as attachment to your mail. This should avoid > whitespace issue entirely. > > > Thanks, > Corinna > > > On Mar 28 10:35, Yuichi Nakamura via Newlib wrote: >> Hi all, >> >> I found the following issues in the m68k version of memcpy(). In particular, >> issue #1 is critical because it causes incorrect behavior on the 68000. >> >> 1. On CPUs that do not support misaligned access (MISALIGNED_OK=0), such as >> the 68000, memcpy() does not correctly copy regions larger than 64 KB when >> the destination is not long-word aligned. >> 2. The 68020 supports misaligned access, but this is not currently >> implemented. >> 3. The 68000 can access long-word data at even addresses, but alignment is >> checked as if the address must be a multiple of 4. >> 4. Because the loop count is checked using signed comparison, memcpy() fails >> for data sizes larger than 2 GB. >> >> The following patch fixes these issues. >> >> Best regards, >> Yuichi Nakamura >> >> >> diff --git a/newlib/libc/machine/m68k/memcpy.S >> b/newlib/libc/machine/m68k/memcpy.S >> index 464da95ef..ecf1da611 100644 >> --- a/newlib/libc/machine/m68k/memcpy.S >> +++ b/newlib/libc/machine/m68k/memcpy.S >> @@ -15,7 +15,7 @@ >> >> #include "m68kasm.h" >> >> -#if defined (__mcoldfire__) || defined (__mc68030__) || defined >> (__mc68040__) || defined (__mc68060__) >> +#if defined (__mcoldfire__) || defined (__mc68020__) || defined >> (__mc68030__) || defined (__mc68040__) || defined (__mc68060__) >> # define MISALIGNED_OK 1 >> #else >> # define MISALIGNED_OK 0 >> @@ -49,10 +49,10 @@ SYM(memcpy): >> #if !MISALIGNED_OK >> /* Goto .Lresidue if either dest or src is not 4-byte aligned */ >> move.l a0,d0 >> - and.l #3,d0 >> + and.l #1,d0 >> bne .Lresidue >> move.l a1,d0 >> - and.l #3,d0 >> + and.l #1,d0 >> bne .Lresidue >> #else /* MISALIGNED_OK */ >> /* align dest */ >> @@ -95,7 +95,7 @@ SYM(memcpy): >> #else >> subq.l #1,d0 >> #endif >> - bpl 1b >> + bcc 1b >> bra .Lresidue >> >> 1: >> @@ -104,9 +104,13 @@ SYM(memcpy): >> .Lresidue: >> #if !defined (__mcoldfire__) >> dbra d1,1b | loop until done >> +#if !MISALIGNED_OK >> + sub.l #0x10000,d1 >> + bcc 1b >> +#endif /* !MISALIGNED_OK */ >> #else >> subq.l #1,d1 >> - bpl 1b >> + bcc 1b >> #endif >> move.l 4(sp),d0 | return value >> rts From 1bb0e3e1e05fe97974f8e967814c7ff70046dfbf Mon Sep 17 00:00:00 2001 From: Yuichi Nakamura <y.512.nakamura@gmail.com> Date: Mon, 30 Mar 2026 22:55:33 +0900 Subject: [PATCH] Fix memcpy alignment problems for m68k architecture --- newlib/libc/machine/m68k/memcpy.S | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/newlib/libc/machine/m68k/memcpy.S b/newlib/libc/machine/m68k/memcpy.S index 464da95ef..ecf1da611 100644 --- a/newlib/libc/machine/m68k/memcpy.S +++ b/newlib/libc/machine/m68k/memcpy.S @@ -15,7 +15,7 @@ #include "m68kasm.h" -#if defined (__mcoldfire__) || defined (__mc68030__) || defined (__mc68040__) || defined (__mc68060__) +#if defined (__mcoldfire__) || defined (__mc68020__) || defined (__mc68030__) || defined (__mc68040__) || defined (__mc68060__) # define MISALIGNED_OK 1 #else # define MISALIGNED_OK 0 @@ -49,10 +49,10 @@ SYM(memcpy): #if !MISALIGNED_OK /* Goto .Lresidue if either dest or src is not 4-byte aligned */ move.l a0,d0 - and.l #3,d0 + and.l #1,d0 bne .Lresidue move.l a1,d0 - and.l #3,d0 + and.l #1,d0 bne .Lresidue #else /* MISALIGNED_OK */ /* align dest */ @@ -95,7 +95,7 @@ SYM(memcpy): #else subq.l #1,d0 #endif - bpl 1b + bcc 1b bra .Lresidue 1: @@ -104,9 +104,13 @@ SYM(memcpy): .Lresidue: #if !defined (__mcoldfire__) dbra d1,1b | loop until done +#if !MISALIGNED_OK + sub.l #0x10000,d1 + bcc 1b +#endif /* !MISALIGNED_OK */ #else subq.l #1,d1 - bpl 1b + bcc 1b #endif move.l 4(sp),d0 | return value rts
Hi Yuichi, Since you didn't add any explainng commit message text, I took the liberty to add your original description verbatim to your patches commit message. Pushed. Thanks, Corinna On Mar 30 23:02, Yuichi Nakamura via Newlib wrote: > Hi Corinna, > > Thank you for the advice. > I have attached the patch to this email. > > Best regards, > Yuichi Nakamura > > > On 2026/03/30 17:47, Corinna Vinschen wrote: > > Hi Yuichi, > > > > unfortunately your patch doesn't apply cleanly due to whitespace issues. > > > > Can you please create a patch with `git format-patch' and send it via > > `git send-email' to this list? Alternatively, append the patch created > > with git format-patch as attachment to your mail. This should avoid > > whitespace issue entirely. > > > > > > Thanks, > > Corinna > > > > > > On Mar 28 10:35, Yuichi Nakamura via Newlib wrote: > > > Hi all, > > > > > > I found the following issues in the m68k version of memcpy(). In particular, > > > issue #1 is critical because it causes incorrect behavior on the 68000. > > > > > > 1. On CPUs that do not support misaligned access (MISALIGNED_OK=0), such as > > > the 68000, memcpy() does not correctly copy regions larger than 64 KB when > > > the destination is not long-word aligned. > > > 2. The 68020 supports misaligned access, but this is not currently > > > implemented. > > > 3. The 68000 can access long-word data at even addresses, but alignment is > > > checked as if the address must be a multiple of 4. > > > 4. Because the loop count is checked using signed comparison, memcpy() fails > > > for data sizes larger than 2 GB. > > > > > > The following patch fixes these issues. > > > > > > Best regards, > > > Yuichi Nakamura > > > > > > > > > diff --git a/newlib/libc/machine/m68k/memcpy.S > > > b/newlib/libc/machine/m68k/memcpy.S > > > index 464da95ef..ecf1da611 100644 > > > --- a/newlib/libc/machine/m68k/memcpy.S > > > +++ b/newlib/libc/machine/m68k/memcpy.S > > > @@ -15,7 +15,7 @@ > > > > > > #include "m68kasm.h" > > > > > > -#if defined (__mcoldfire__) || defined (__mc68030__) || defined > > > (__mc68040__) || defined (__mc68060__) > > > +#if defined (__mcoldfire__) || defined (__mc68020__) || defined > > > (__mc68030__) || defined (__mc68040__) || defined (__mc68060__) > > > # define MISALIGNED_OK 1 > > > #else > > > # define MISALIGNED_OK 0 > > > @@ -49,10 +49,10 @@ SYM(memcpy): > > > #if !MISALIGNED_OK > > > /* Goto .Lresidue if either dest or src is not 4-byte aligned */ > > > move.l a0,d0 > > > - and.l #3,d0 > > > + and.l #1,d0 > > > bne .Lresidue > > > move.l a1,d0 > > > - and.l #3,d0 > > > + and.l #1,d0 > > > bne .Lresidue > > > #else /* MISALIGNED_OK */ > > > /* align dest */ > > > @@ -95,7 +95,7 @@ SYM(memcpy): > > > #else > > > subq.l #1,d0 > > > #endif > > > - bpl 1b > > > + bcc 1b > > > bra .Lresidue > > > > > > 1: > > > @@ -104,9 +104,13 @@ SYM(memcpy): > > > .Lresidue: > > > #if !defined (__mcoldfire__) > > > dbra d1,1b | loop until done > > > +#if !MISALIGNED_OK > > > + sub.l #0x10000,d1 > > > + bcc 1b > > > +#endif /* !MISALIGNED_OK */ > > > #else > > > subq.l #1,d1 > > > - bpl 1b > > > + bcc 1b > > > #endif > > > move.l 4(sp),d0 | return value > > > rts > From 1bb0e3e1e05fe97974f8e967814c7ff70046dfbf Mon Sep 17 00:00:00 2001 > From: Yuichi Nakamura <y.512.nakamura@gmail.com> > Date: Mon, 30 Mar 2026 22:55:33 +0900 > Subject: [PATCH] Fix memcpy alignment problems for m68k architecture > > --- > newlib/libc/machine/m68k/memcpy.S | 14 +++++++++----- > 1 file changed, 9 insertions(+), 5 deletions(-) > > diff --git a/newlib/libc/machine/m68k/memcpy.S b/newlib/libc/machine/m68k/memcpy.S > index 464da95ef..ecf1da611 100644 > --- a/newlib/libc/machine/m68k/memcpy.S > +++ b/newlib/libc/machine/m68k/memcpy.S > @@ -15,7 +15,7 @@ > > #include "m68kasm.h" > > -#if defined (__mcoldfire__) || defined (__mc68030__) || defined (__mc68040__) || defined (__mc68060__) > +#if defined (__mcoldfire__) || defined (__mc68020__) || defined (__mc68030__) || defined (__mc68040__) || defined (__mc68060__) > # define MISALIGNED_OK 1 > #else > # define MISALIGNED_OK 0 > @@ -49,10 +49,10 @@ SYM(memcpy): > #if !MISALIGNED_OK > /* Goto .Lresidue if either dest or src is not 4-byte aligned */ > move.l a0,d0 > - and.l #3,d0 > + and.l #1,d0 > bne .Lresidue > move.l a1,d0 > - and.l #3,d0 > + and.l #1,d0 > bne .Lresidue > #else /* MISALIGNED_OK */ > /* align dest */ > @@ -95,7 +95,7 @@ SYM(memcpy): > #else > subq.l #1,d0 > #endif > - bpl 1b > + bcc 1b > bra .Lresidue > > 1: > @@ -104,9 +104,13 @@ SYM(memcpy): > .Lresidue: > #if !defined (__mcoldfire__) > dbra d1,1b | loop until done > +#if !MISALIGNED_OK > + sub.l #0x10000,d1 > + bcc 1b > +#endif /* !MISALIGNED_OK */ > #else > subq.l #1,d1 > - bpl 1b > + bcc 1b > #endif > move.l 4(sp),d0 | return value > rts > -- > 2.43.0 >
diff --git a/newlib/libc/machine/m68k/memcpy.S b/newlib/libc/machine/m68k/memcpy.S index 464da95ef..ecf1da611 100644 --- a/newlib/libc/machine/m68k/memcpy.S +++ b/newlib/libc/machine/m68k/memcpy.S @@ -15,7 +15,7 @@ #include "m68kasm.h" -#if defined (__mcoldfire__) || defined (__mc68030__) || defined (__mc68040__) || defined (__mc68060__) +#if defined (__mcoldfire__) || defined (__mc68020__) || defined (__mc68030__) || defined (__mc68040__) || defined (__mc68060__) # define MISALIGNED_OK 1