String: Add support for __memcmpeq() ABI on all targets

No bug.

This commit adds support for __memcmpeq() as a new ABI for all
targets. In this commit __memcmpeq() is implemented only as an alias
to the corresponding targets memcmp() implementation. __memcmpeq() is
added as a new symbol starting with GLIBC_2.35 and defined in string.h
with comments explaining its behavior. Basic tests that it is callable
and works where added in string/tester.c

As discussed in the proposal "Add new ABI '__memcmpeq()' to libc"
__memcmpeq() is essentially a reserved namespace for bcmp(). The means
is shares the same specifications as memcmp() except the return value
for non-equal byte sequences is any non-zero value. This is less
strict than memcmp()'s return value specification and can be better
optimized when a boolean return is all that is needed.

__memcmpeq() is meant to only be called by compilers if they can prove
that the return value of a memcmp() call is only used for its boolean
value.

All tests in string/tester.c passed. As well build succeeds on
x86_64-linux-gnu target.
This commit is contained in:
Noah Goldstein 2021-10-21 15:54:57 -05:00
parent 8438135d34
commit 44829b3ddb
64 changed files with 120 additions and 0 deletions

View File

@ -89,4 +89,7 @@ libc {
sigdescr_np; sigabbrev_np;
strerrordesc_np; strerrorname_np;
}
GLIBC_2.35 {
__memcmpeq;
}
}

View File

@ -359,3 +359,6 @@ libc_hidden_builtin_def(memcmp)
# undef bcmp
weak_alias (memcmp, bcmp)
#endif
#undef __memcmpeq
strong_alias (memcmp, __memcmpeq)

View File

@ -64,6 +64,22 @@ extern void *memset (void *__s, int __c, size_t __n) __THROW __nonnull ((1));
extern int memcmp (const void *__s1, const void *__s2, size_t __n)
__THROW __attribute_pure__ __nonnull ((1, 2));
/* Compare N bytes of S1 and S2. Return zero if S1 and S2 are equal.
Return some non-zero value otherwise.
Essentially __memcmpeq has the exact same semantics as memcmp
except the return value is less constrained. memcmp is always a
correct implementation of __memcmpeq. As well !!memcmp, -memcmp,
or bcmp are correct implementations.
__memcmpeq is meant to be used by compilers when memcmp return is
only used for its bolean value.
__memcmpeq is declared only for use by compilers. Programs should
continue to use memcmp. */
extern int __memcmpeq (const void *__s1, const void *__s2, size_t __n)
__THROW __attribute_pure__ __nonnull ((1, 2));
/* Search N bytes of S for C. */
#ifdef __CORRECT_ISO_CPP_STRING_H_PROTO
extern "C++"

View File

@ -1449,6 +1449,19 @@ test_bcmp (void)
check(bcmp("abc", "def", 0) == 0, 8); /* Zero count. */
}
static void
test_memcmpeq (void)
{
it = "__memcmpeq";
check (__memcmpeq ("a", "a", 1) == 0, 1); /* Identity. */
check (__memcmpeq ("abc", "abc", 3) == 0, 2); /* Multicharacter. */
check (__memcmpeq ("abcd", "abce", 4) != 0, 3); /* Honestly unequal. */
check (__memcmpeq ("abce", "abcd", 4) != 0, 4);
check (__memcmpeq ("alph", "beta", 4) != 0, 5);
check (__memcmpeq ("abce", "abcd", 3) == 0, 6); /* Count limited. */
check (__memcmpeq ("abc", "def", 0) == 0, 8); /* Zero count. */
}
static void
test_strerror (void)
{
@ -1611,6 +1624,9 @@ main (void)
/* bcmp - somewhat like memcmp. */
test_bcmp ();
/* __memcmpeq - somewhat like memcmp. */
test_memcmpeq ();
/* strndup. */
test_strndup ();

View File

@ -177,4 +177,6 @@ L(ret_0):
END (memcmp)
#undef bcmp
weak_alias (memcmp, bcmp)
#undef __memcmpeq
strong_alias (memcmp, __memcmpeq)
libc_hidden_builtin_def (memcmp)

View File

@ -138,5 +138,6 @@ ENTRY (memcmp)
br .L_s1_aligned
END (memcmp)
weak_alias (memcmp, bcmp)
strong_alias (memcmp, __memcmpeq)
libc_hidden_def (memcmp)
.weak memcmp

View File

@ -405,4 +405,6 @@ L(table_32bytes) :
#undef bcmp
weak_alias (memcmp, bcmp)
#undef __memcmpeq
strong_alias (memcmp, __memcmpeq)
libc_hidden_builtin_def (memcmp)

View File

@ -30,6 +30,9 @@
# undef weak_alias
# define weak_alias(original, alias)
# undef strong_alias
# define strong_alias(original, alias)
#endif
#include <sysdeps/i386/i686/memcmp.S>

View File

@ -29,4 +29,5 @@
libc_ifunc_redirected (__redirect_memcmp, memcmp, IFUNC_SELECTOR ());
weak_alias (memcmp, bcmp)
strong_alias (memcmp, __memcmpeq)
#endif

View File

@ -70,4 +70,6 @@ END (memcmp)
#undef bcmp
weak_alias (memcmp, bcmp)
#undef __memcmpeq
strong_alias (memcmp, __memcmpeq)
libc_hidden_builtin_def (memcmp)

View File

@ -160,4 +160,5 @@ ENTRY(memcmp)
END(memcmp)
weak_alias (memcmp, bcmp)
strong_alias (memcmp, __memcmpeq)
libc_hidden_builtin_def (memcmp)

View File

@ -2285,6 +2285,7 @@ GLIBC_2.34 res_send F
GLIBC_2.34 shm_open F
GLIBC_2.34 shm_unlink F
GLIBC_2.34 timespec_getres F
GLIBC_2.35 __memcmpeq F
GLIBC_2.4 __confstr_chk F
GLIBC_2.4 __fgets_chk F
GLIBC_2.4 __fgets_unlocked_chk F

View File

@ -126,3 +126,4 @@ L(st2):
END (memcmp)
libc_hidden_builtin_def (memcmp)
weak_alias (memcmp,bcmp)
strong_alias (memcmp, __memcmpeq)

View File

@ -1373,3 +1373,4 @@ END (memcmp)
libc_hidden_builtin_def (memcmp)
weak_alias (memcmp, bcmp)
strong_alias (memcmp, __memcmpeq)

View File

@ -38,4 +38,7 @@
#undef weak_alias
#define weak_alias(a, b)
#undef strong_alias
#define strong_alias(a, b)
#include <sysdeps/powerpc/powerpc32/power7/memcmp.S>

View File

@ -40,6 +40,10 @@
# undef weak_alias
# define weak_alias(a, b) \
.weak b ; b = __memcmp_ppc
# undef strong_alias
# define strong_alias(a, b) \
.globl b ; b = __memcmp_ppc
#endif
#include <sysdeps/powerpc/powerpc32/power4/memcmp.S>

View File

@ -1373,3 +1373,4 @@ END (memcmp)
libc_hidden_builtin_def (memcmp)
weak_alias (memcmp, bcmp)
strong_alias (memcmp, __memcmpeq)

View File

@ -177,3 +177,4 @@ L(tail8):
END (MEMCMP)
libc_hidden_builtin_def (memcmp)
weak_alias (memcmp, bcmp)
strong_alias (memcmp, __memcmpeq)

View File

@ -22,5 +22,7 @@
#define libc_hidden_builtin_def(name)
#undef weak_alias
#define weak_alias(name,alias)
#undef strong_alias
#define strong_alias(name,alias)
#include <sysdeps/powerpc/powerpc64/le/power10/memcmp.S>

View File

@ -22,5 +22,7 @@
#define libc_hidden_builtin_def(name)
#undef weak_alias
#define weak_alias(name,alias)
#undef strong_alias
#define strong_alias(name,alias)
#include <sysdeps/powerpc/powerpc64/power4/memcmp.S>

View File

@ -22,5 +22,7 @@
#define libc_hidden_builtin_def(name)
#undef weak_alias
#define weak_alias(name,alias)
#undef strong_alias
#define strong_alias(name,alias)
#include <sysdeps/powerpc/powerpc64/power7/memcmp.S>

View File

@ -22,5 +22,7 @@
#define libc_hidden_builtin_def(name)
#undef weak_alias
#define weak_alias(name,alias)
#undef strong_alias
#define strong_alias(name,alias)
#include <sysdeps/powerpc/powerpc64/power8/memcmp.S>

View File

@ -22,6 +22,10 @@
#define weak_alias(name, aliasname) \
extern __typeof (__memcmp_ppc) aliasname \
__attribute__ ((weak, alias ("__memcmp_ppc")));
#undef strong_alias
#define strong_alias(name, aliasname) \
extern __typeof (__memcmp_ppc) aliasname \
__attribute__ ((alias ("__memcmp_ppc")));
#if IS_IN (libc) && defined(SHARED)
# undef libc_hidden_builtin_def
# define libc_hidden_builtin_def(name) \

View File

@ -1374,3 +1374,4 @@ L(duzeroLength):
END (MEMCMP)
libc_hidden_builtin_def (memcmp)
weak_alias (memcmp, bcmp)
strong_alias (memcmp, __memcmpeq)

View File

@ -1059,3 +1059,4 @@ L(duzeroLength):
END (MEMCMP)
libc_hidden_builtin_def (memcmp)
weak_alias (memcmp, bcmp)
strong_alias (memcmp, __memcmpeq)

View File

@ -1442,3 +1442,4 @@ L(duzeroLength):
END (MEMCMP)
libc_hidden_builtin_def (memcmp)
weak_alias (memcmp, bcmp)
strong_alias (memcmp, __memcmpeq)

View File

@ -164,6 +164,7 @@ END(MEMCMP_Z196)
Otherwise see sysdeps/s390/memcmp.c. */
strong_alias (MEMCMP_DEFAULT, memcmp)
weak_alias (memcmp, bcmp)
strong_alias (memcmp, __memcmpeq)
#endif
#if defined SHARED && IS_IN (libc)

View File

@ -46,4 +46,5 @@ s390_libc_ifunc_expr (__redirect_memcmp, memcmp,
})
)
weak_alias (memcmp, bcmp);
strong_alias (memcmp, __memcmpeq)
#endif

View File

@ -137,4 +137,6 @@ END(memcmp)
#undef bcmp
weak_alias (memcmp, bcmp)
#undef __memcmpeq
strong_alias (memcmp, __memcmpeq)
libc_hidden_builtin_def (memcmp)

View File

@ -2612,3 +2612,4 @@ GLIBC_2.34 tss_create F
GLIBC_2.34 tss_delete F
GLIBC_2.34 tss_get F
GLIBC_2.34 tss_set F
GLIBC_2.35 __memcmpeq F

View File

@ -2709,6 +2709,7 @@ GLIBC_2.34 tss_create F
GLIBC_2.34 tss_delete F
GLIBC_2.34 tss_get F
GLIBC_2.34 tss_set F
GLIBC_2.35 __memcmpeq F
GLIBC_2.4 _IO_fprintf F
GLIBC_2.4 _IO_printf F
GLIBC_2.4 _IO_sprintf F

View File

@ -2373,3 +2373,4 @@ GLIBC_2.34 tss_create F
GLIBC_2.34 tss_delete F
GLIBC_2.34 tss_get F
GLIBC_2.34 tss_set F
GLIBC_2.35 __memcmpeq F

View File

@ -491,6 +491,7 @@ GLIBC_2.34 tss_create F
GLIBC_2.34 tss_delete F
GLIBC_2.34 tss_get F
GLIBC_2.34 tss_set F
GLIBC_2.35 __memcmpeq F
GLIBC_2.4 _Exit F
GLIBC_2.4 _IO_2_1_stderr_ D 0xa0
GLIBC_2.4 _IO_2_1_stdin_ D 0xa0

View File

@ -488,6 +488,7 @@ GLIBC_2.34 tss_create F
GLIBC_2.34 tss_delete F
GLIBC_2.34 tss_get F
GLIBC_2.34 tss_set F
GLIBC_2.35 __memcmpeq F
GLIBC_2.4 _Exit F
GLIBC_2.4 _IO_2_1_stderr_ D 0xa0
GLIBC_2.4 _IO_2_1_stdin_ D 0xa0

View File

@ -2647,3 +2647,4 @@ GLIBC_2.34 tss_create F
GLIBC_2.34 tss_delete F
GLIBC_2.34 tss_get F
GLIBC_2.34 tss_set F
GLIBC_2.35 __memcmpeq F

View File

@ -2596,6 +2596,7 @@ GLIBC_2.34 tss_create F
GLIBC_2.34 tss_delete F
GLIBC_2.34 tss_get F
GLIBC_2.34 tss_set F
GLIBC_2.35 __memcmpeq F
GLIBC_2.4 __confstr_chk F
GLIBC_2.4 __fgets_chk F
GLIBC_2.4 __fgets_unlocked_chk F

View File

@ -2780,6 +2780,7 @@ GLIBC_2.34 tss_create F
GLIBC_2.34 tss_delete F
GLIBC_2.34 tss_get F
GLIBC_2.34 tss_set F
GLIBC_2.35 __memcmpeq F
GLIBC_2.4 __confstr_chk F
GLIBC_2.4 __fgets_chk F
GLIBC_2.4 __fgets_unlocked_chk F

View File

@ -2547,6 +2547,7 @@ GLIBC_2.34 tss_create F
GLIBC_2.34 tss_delete F
GLIBC_2.34 tss_get F
GLIBC_2.34 tss_set F
GLIBC_2.35 __memcmpeq F
GLIBC_2.4 __confstr_chk F
GLIBC_2.4 __fgets_chk F
GLIBC_2.4 __fgets_unlocked_chk F

View File

@ -492,6 +492,7 @@ GLIBC_2.34 tss_create F
GLIBC_2.34 tss_delete F
GLIBC_2.34 tss_get F
GLIBC_2.34 tss_set F
GLIBC_2.35 __memcmpeq F
GLIBC_2.4 _Exit F
GLIBC_2.4 _IO_2_1_stderr_ D 0x98
GLIBC_2.4 _IO_2_1_stdin_ D 0x98

View File

@ -2723,6 +2723,7 @@ GLIBC_2.34 tss_create F
GLIBC_2.34 tss_delete F
GLIBC_2.34 tss_get F
GLIBC_2.34 tss_set F
GLIBC_2.35 __memcmpeq F
GLIBC_2.4 __confstr_chk F
GLIBC_2.4 __fgets_chk F
GLIBC_2.4 __fgets_unlocked_chk F

View File

@ -2696,3 +2696,4 @@ GLIBC_2.34 tss_create F
GLIBC_2.34 tss_delete F
GLIBC_2.34 tss_get F
GLIBC_2.34 tss_set F
GLIBC_2.35 __memcmpeq F

View File

@ -2693,3 +2693,4 @@ GLIBC_2.34 tss_create F
GLIBC_2.34 tss_delete F
GLIBC_2.34 tss_get F
GLIBC_2.34 tss_set F
GLIBC_2.35 __memcmpeq F

View File

@ -2688,6 +2688,7 @@ GLIBC_2.34 tss_create F
GLIBC_2.34 tss_delete F
GLIBC_2.34 tss_get F
GLIBC_2.34 tss_set F
GLIBC_2.35 __memcmpeq F
GLIBC_2.4 __confstr_chk F
GLIBC_2.4 __fgets_chk F
GLIBC_2.4 __fgets_unlocked_chk F

View File

@ -2686,6 +2686,7 @@ GLIBC_2.34 tss_create F
GLIBC_2.34 tss_delete F
GLIBC_2.34 tss_get F
GLIBC_2.34 tss_set F
GLIBC_2.35 __memcmpeq F
GLIBC_2.4 __confstr_chk F
GLIBC_2.4 __fgets_chk F
GLIBC_2.4 __fgets_unlocked_chk F

View File

@ -2694,6 +2694,7 @@ GLIBC_2.34 tss_create F
GLIBC_2.34 tss_delete F
GLIBC_2.34 tss_get F
GLIBC_2.34 tss_set F
GLIBC_2.35 __memcmpeq F
GLIBC_2.4 __confstr_chk F
GLIBC_2.4 __fgets_chk F
GLIBC_2.4 __fgets_unlocked_chk F

View File

@ -2598,6 +2598,7 @@ GLIBC_2.34 tss_create F
GLIBC_2.34 tss_delete F
GLIBC_2.34 tss_get F
GLIBC_2.34 tss_set F
GLIBC_2.35 __memcmpeq F
GLIBC_2.4 __confstr_chk F
GLIBC_2.4 __fgets_chk F
GLIBC_2.4 __fgets_unlocked_chk F

View File

@ -2735,3 +2735,4 @@ GLIBC_2.34 tss_create F
GLIBC_2.34 tss_delete F
GLIBC_2.34 tss_get F
GLIBC_2.34 tss_set F
GLIBC_2.35 __memcmpeq F

View File

@ -2750,6 +2750,7 @@ GLIBC_2.34 tss_create F
GLIBC_2.34 tss_delete F
GLIBC_2.34 tss_get F
GLIBC_2.34 tss_set F
GLIBC_2.35 __memcmpeq F
GLIBC_2.4 _IO_fprintf F
GLIBC_2.4 _IO_printf F
GLIBC_2.4 _IO_sprintf F

View File

@ -2783,6 +2783,7 @@ GLIBC_2.34 tss_create F
GLIBC_2.34 tss_delete F
GLIBC_2.34 tss_get F
GLIBC_2.34 tss_set F
GLIBC_2.35 __memcmpeq F
GLIBC_2.4 _IO_fprintf F
GLIBC_2.4 _IO_printf F
GLIBC_2.4 _IO_sprintf F

View File

@ -2506,6 +2506,7 @@ GLIBC_2.34 tss_create F
GLIBC_2.34 tss_delete F
GLIBC_2.34 tss_get F
GLIBC_2.34 tss_set F
GLIBC_2.35 __memcmpeq F
GLIBC_2.4 _IO_fprintf F
GLIBC_2.4 _IO_printf F
GLIBC_2.4 _IO_sprintf F

View File

@ -2808,3 +2808,4 @@ GLIBC_2.34 tss_create F
GLIBC_2.34 tss_delete F
GLIBC_2.34 tss_get F
GLIBC_2.34 tss_set F
GLIBC_2.35 __memcmpeq F

View File

@ -2375,3 +2375,4 @@ GLIBC_2.34 tss_create F
GLIBC_2.34 tss_delete F
GLIBC_2.34 tss_get F
GLIBC_2.34 tss_set F
GLIBC_2.35 __memcmpeq F

View File

@ -2575,3 +2575,4 @@ GLIBC_2.34 tss_create F
GLIBC_2.34 tss_delete F
GLIBC_2.34 tss_get F
GLIBC_2.34 tss_set F
GLIBC_2.35 __memcmpeq F

View File

@ -2748,6 +2748,7 @@ GLIBC_2.34 tss_create F
GLIBC_2.34 tss_delete F
GLIBC_2.34 tss_get F
GLIBC_2.34 tss_set F
GLIBC_2.35 __memcmpeq F
GLIBC_2.4 _IO_fprintf F
GLIBC_2.4 _IO_printf F
GLIBC_2.4 _IO_sprintf F

View File

@ -2543,6 +2543,7 @@ GLIBC_2.34 tss_create F
GLIBC_2.34 tss_delete F
GLIBC_2.34 tss_get F
GLIBC_2.34 tss_set F
GLIBC_2.35 __memcmpeq F
GLIBC_2.4 _IO_fprintf F
GLIBC_2.4 _IO_printf F
GLIBC_2.4 _IO_sprintf F

View File

@ -2603,6 +2603,7 @@ GLIBC_2.34 tss_create F
GLIBC_2.34 tss_delete F
GLIBC_2.34 tss_get F
GLIBC_2.34 tss_set F
GLIBC_2.35 __memcmpeq F
GLIBC_2.4 __confstr_chk F
GLIBC_2.4 __fgets_chk F
GLIBC_2.4 __fgets_unlocked_chk F

View File

@ -2600,6 +2600,7 @@ GLIBC_2.34 tss_create F
GLIBC_2.34 tss_delete F
GLIBC_2.34 tss_get F
GLIBC_2.34 tss_set F
GLIBC_2.35 __memcmpeq F
GLIBC_2.4 __confstr_chk F
GLIBC_2.4 __fgets_chk F
GLIBC_2.4 __fgets_unlocked_chk F

View File

@ -2743,6 +2743,7 @@ GLIBC_2.34 tss_create F
GLIBC_2.34 tss_delete F
GLIBC_2.34 tss_get F
GLIBC_2.34 tss_set F
GLIBC_2.35 __memcmpeq F
GLIBC_2.4 _IO_fprintf F
GLIBC_2.4 _IO_printf F
GLIBC_2.4 _IO_sprintf F

View File

@ -2570,6 +2570,7 @@ GLIBC_2.34 tss_create F
GLIBC_2.34 tss_delete F
GLIBC_2.34 tss_get F
GLIBC_2.34 tss_set F
GLIBC_2.35 __memcmpeq F
GLIBC_2.4 __confstr_chk F
GLIBC_2.4 __fgets_chk F
GLIBC_2.4 __fgets_unlocked_chk F

View File

@ -2521,6 +2521,7 @@ GLIBC_2.34 tss_create F
GLIBC_2.34 tss_delete F
GLIBC_2.34 tss_get F
GLIBC_2.34 tss_set F
GLIBC_2.35 __memcmpeq F
GLIBC_2.4 __confstr_chk F
GLIBC_2.4 __fgets_chk F
GLIBC_2.4 __fgets_unlocked_chk F

View File

@ -2627,3 +2627,4 @@ GLIBC_2.34 tss_create F
GLIBC_2.34 tss_delete F
GLIBC_2.34 tss_get F
GLIBC_2.34 tss_set F
GLIBC_2.35 __memcmpeq F

View File

@ -358,4 +358,6 @@ END(memcmp)
#undef bcmp
weak_alias (memcmp, bcmp)
#undef __memcmpeq
strong_alias (memcmp, __memcmpeq)
libc_hidden_builtin_def (memcmp)

View File

@ -26,6 +26,9 @@
# undef weak_alias
# define weak_alias(ignored1, ignored2)
# undef strong_alias
# define strong_alias(ignored1, ignored2)
#endif
#include <sysdeps/x86_64/memcmp.S>

View File

@ -29,6 +29,8 @@
libc_ifunc_redirected (__redirect_memcmp, memcmp, IFUNC_SELECTOR ());
# undef bcmp
weak_alias (memcmp, bcmp)
# undef __memcmpeq
strong_alias (memcmp, __memcmpeq)
# ifdef SHARED
__hidden_ver1 (memcmp, __GI_memcmp, __redirect_memcmp)