Bug 289084 - lib/libc/string/memcmp_test:{diff,neq} tests failing in CI on arm64
Summary: lib/libc/string/memcmp_test:{diff,neq} tests failing in CI on arm64
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: tests (show other bugs)
Version: 16.0-CURRENT
Hardware: arm64 Any
: --- Affects Only Me
Assignee: Ed Maste
URL:
Keywords: regression
Depends on:
Blocks:
 
Reported: 2025-08-24 18:16 UTC by Siva Mahadevan
Modified: 2025-09-16 17:41 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Siva Mahadevan freebsd_committer freebsd_triage 2025-08-24 18:16:37 UTC
lib/libc/tests/string/memcmp_test.c has the following comment:

> On FreeBSD we demand that memcmp returns the difference between the characters at the first site of mismatch.  However, ISO/IEC 9899:1990 only specifies that a number greater than, equal to, or less than zero shall be returned.  If a unit test for this less strict behaviour is desired, define RES(x) to be (((x) > 0) - ((x) < 0)).

On arm64, the optimized memcmp assembly does not have this behaviour and instead returns the standard-specified <0 or >0 number (which happens to be -1 and 1 in this case).

This is causing the 'lib/libc/string/memcmp_test:diff' and 'lib/libc/string/memcmp_test:neq' testcases to fail in CI as seen here: https://ci.freebsd.org/view/Test/job/FreeBSD-main-aarch64-test/1748/testReport

@fuz could you help triage this?
Comment 1 Robert Clausecker freebsd_committer freebsd_triage 2025-08-24 19:54:15 UTC
On aarch64, we use ARM's optimised string routines.  These do not follow this extra condition and have never done so.  I was aware of this when I pushed this unit test.  Eventually, we'll need to either relax the test or write our own memcmp() that follows the stricter behaviour.
Comment 2 Ed Maste freebsd_committer freebsd_triage 2025-08-24 20:03:56 UTC
It is a bug that we expect memcmp to return the difference in the byte values. The compiler is free to inline memcmp() with an implementation that does not do this, so we may also not have the expected behaviour on architectures other than arm64.

On my amd64 laptop `printf("%d\n", memcmp("a", "c", 1));` prints -1.
Comment 3 Ed Maste freebsd_committer freebsd_triage 2025-09-12 16:25:39 UTC
Test update: https://reviews.freebsd.org/D52502
Comment 4 commit-hook freebsd_committer freebsd_triage 2025-09-13 19:30:47 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=e0eaabb80d1724acf88f04acbc2ca13d42270863

commit e0eaabb80d1724acf88f04acbc2ca13d42270863
Author:     Ed Maste <emaste@FreeBSD.org>
AuthorDate: 2024-11-20 16:47:35 +0000
Commit:     Ed Maste <emaste@FreeBSD.org>
CommitDate: 2025-09-13 19:30:31 +0000

    libc: Have memcmp test what the standard requires

    libc's C memcmp currently returns the difference in byte values rather
    than just -1/0/1 as the AArch64 assembly implementation, many non-
    FreeBSD implementations, and compiler built-in optimizations do.

    It is a bug for a user to expect memcmp to return the difference in the
    byte values as the compiler is free to inline memcmp() with an
    implementation that does not do this.  Change the test to validate only
    what the standard requires.

    PR:             289084
    Reviewed by:    markj, fuz
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D52502

 lib/libc/tests/string/memcmp_test.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
Comment 5 commit-hook freebsd_committer freebsd_triage 2025-09-16 17:35:51 UTC
A commit in branch stable/15 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=79e4dfe2a5f7079608b92d2d1fe38838e7601323

commit 79e4dfe2a5f7079608b92d2d1fe38838e7601323
Author:     Ed Maste <emaste@FreeBSD.org>
AuthorDate: 2024-11-20 16:47:35 +0000
Commit:     Ed Maste <emaste@FreeBSD.org>
CommitDate: 2025-09-16 17:34:32 +0000

    libc: Have memcmp test what the standard requires

    libc's C memcmp currently returns the difference in byte values rather
    than just -1/0/1 as the AArch64 assembly implementation, many non-
    FreeBSD implementations, and compiler built-in optimizations do.

    It is a bug for a user to expect memcmp to return the difference in the
    byte values as the compiler is free to inline memcmp() with an
    implementation that does not do this.  Change the test to validate only
    what the standard requires.

    PR:             289084
    Reviewed by:    markj, fuz
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D52502

    (cherry picked from commit e0eaabb80d1724acf88f04acbc2ca13d42270863)

 lib/libc/tests/string/memcmp_test.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)