From patchwork Sat Mar 28 01:35:38 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yuichi Nakamura X-Patchwork-Id: 132394 Return-Path: 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 ; 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 ; 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: 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 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Yuichi Nakamura via Newlib From: Yuichi Nakamura Reply-To: Yuichi Nakamura Errors-To: newlib-bounces~patchwork=sourceware.org@sourceware.org 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 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