[PATCH v8 1/3] String: Add support for __memcmpeq() ABI on all targets

Noah Goldstein goldstein.w.n@gmail.com
Fri Oct 22 04:04:33 GMT 2021


On Thu, Oct 21, 2021 at 9:38 PM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> On Thu, Oct 21, 2021 at 4:42 PM Noah Goldstein <goldstein.w.n@gmail.com> wrote:
> >
> > On Thu, Oct 21, 2021 at 5:50 PM H.J. Lu <hjl.tools@gmail.com> wrote:
> > >
> > > On Thu, Oct 21, 2021 at 3:38 PM Noah Goldstein <goldstein.w.n@gmail.com> wrote:
> > > >
> > > > 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.
> > > > ---
> > > >  string/Versions                                  |  3 +++
> > > >  string/memcmp.c                                  |  2 ++
> > > >  string/string.h                                  | 16 ++++++++++++++++
> > > >  string/tester.c                                  | 16 ++++++++++++++++
> > > >  sysdeps/aarch64/memcmp.S                         |  2 ++
> > > >  sysdeps/csky/abiv2/memcmp.S                      |  1 +
> > > >  sysdeps/i386/i686/memcmp.S                       |  2 ++
> > > >  sysdeps/i386/i686/multiarch/memcmp.c             |  1 +
> > > >  sysdeps/i386/memcmp.S                            |  2 ++
> > > >  sysdeps/ia64/memcmp.S                            |  1 +
> > > >  sysdeps/mach/hurd/i386/libc.abilist              |  1 +
> > > >  sysdeps/powerpc/powerpc32/405/memcmp.S           |  1 +
> > > >  sysdeps/powerpc/powerpc32/power4/memcmp.S        |  1 +
> > > >  sysdeps/powerpc/powerpc32/power7/memcmp.S        |  1 +
> > > >  sysdeps/powerpc/powerpc64/le/power10/memcmp.S    |  1 +
> > > >  sysdeps/powerpc/powerpc64/power4/memcmp.S        |  1 +
> > > >  sysdeps/powerpc/powerpc64/power7/memcmp.S        |  1 +
> > > >  sysdeps/powerpc/powerpc64/power8/memcmp.S        |  1 +
> > > >  sysdeps/s390/memcmp-z900.S                       |  1 +
> > > >  sysdeps/s390/memcmp.c                            |  1 +
> > > >  sysdeps/sparc/sparc64/memcmp.S                   |  2 ++
> > > >  sysdeps/unix/sysv/linux/aarch64/libc.abilist     |  1 +
> > > >  sysdeps/unix/sysv/linux/alpha/libc.abilist       |  1 +
> > > >  sysdeps/unix/sysv/linux/arc/libc.abilist         |  1 +
> > > >  sysdeps/unix/sysv/linux/arm/be/libc.abilist      |  1 +
> > > >  sysdeps/unix/sysv/linux/arm/le/libc.abilist      |  1 +
> > > >  sysdeps/unix/sysv/linux/csky/libc.abilist        |  1 +
> > > >  sysdeps/unix/sysv/linux/hppa/libc.abilist        |  1 +
> > > >  sysdeps/unix/sysv/linux/i386/libc.abilist        |  1 +
> > > >  sysdeps/unix/sysv/linux/ia64/libc.abilist        |  1 +
> > > >  .../unix/sysv/linux/m68k/coldfire/libc.abilist   |  1 +
> > > >  sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist |  1 +
> > > >  .../unix/sysv/linux/microblaze/be/libc.abilist   |  1 +
> > > >  .../unix/sysv/linux/microblaze/le/libc.abilist   |  1 +
> > > >  .../unix/sysv/linux/mips/mips32/fpu/libc.abilist |  1 +
> > > >  .../sysv/linux/mips/mips32/nofpu/libc.abilist    |  1 +
> > > >  .../unix/sysv/linux/mips/mips64/n32/libc.abilist |  1 +
> > > >  .../unix/sysv/linux/mips/mips64/n64/libc.abilist |  1 +
> > > >  sysdeps/unix/sysv/linux/nios2/libc.abilist       |  1 +
> > > >  .../linux/powerpc/powerpc32/fpu/libc.abilist     |  1 +
> > > >  .../linux/powerpc/powerpc32/nofpu/libc.abilist   |  1 +
> > > >  .../sysv/linux/powerpc/powerpc64/be/libc.abilist |  1 +
> > > >  .../sysv/linux/powerpc/powerpc64/le/libc.abilist |  1 +
> > > >  sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist  |  1 +
> > > >  sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist  |  1 +
> > > >  .../unix/sysv/linux/s390/s390-32/libc.abilist    |  1 +
> > > >  .../unix/sysv/linux/s390/s390-64/libc.abilist    |  1 +
> > > >  sysdeps/unix/sysv/linux/sh/be/libc.abilist       |  1 +
> > > >  sysdeps/unix/sysv/linux/sh/le/libc.abilist       |  1 +
> > > >  .../unix/sysv/linux/sparc/sparc32/libc.abilist   |  1 +
> > > >  .../unix/sysv/linux/sparc/sparc64/libc.abilist   |  1 +
> > > >  sysdeps/unix/sysv/linux/x86_64/64/libc.abilist   |  1 +
> > > >  sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist  |  1 +
> > > >  sysdeps/x86_64/memcmp.S                          |  2 ++
> > > >  sysdeps/x86_64/multiarch/memcmp.c                |  2 ++
> > > >  55 files changed, 94 insertions(+)
> > > >
> > > > diff --git a/string/Versions b/string/Versions
> > > > index 298ecd401a..864c4cf7a4 100644
> > > > --- a/string/Versions
> > > > +++ b/string/Versions
> > > > @@ -89,4 +89,7 @@ libc {
> > > >      sigdescr_np; sigabbrev_np;
> > > >      strerrordesc_np; strerrorname_np;
> > > >    }
> > > > +  GLIBC_2.35 {
> > > > +    __memcmpeq;
> > > > +  }
> > > >  }
> > > > diff --git a/string/memcmp.c b/string/memcmp.c
> > > > index 9b46d7a905..5020be00e0 100644
> > > > --- a/string/memcmp.c
> > > > +++ b/string/memcmp.c
> > > > @@ -358,4 +358,6 @@ libc_hidden_builtin_def(memcmp)
> > > >  #ifdef weak_alias
> > > >  # undef bcmp
> > > >  weak_alias (memcmp, bcmp)
> > > > +# undef __memcmpeq
> > > > +weak_alias (memcmp, __memcmpeq)
> > > >  #endif
> > > > diff --git a/string/string.h b/string/string.h
> > > > index b1b083edce..e70feeaeea 100644
> > > > --- a/string/string.h
> > > > +++ b/string/string.h
> > > > @@ -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++"
> > > > diff --git a/string/tester.c b/string/tester.c
> > > > index 778160ae6e..605b3f00f9 100644
> > > > --- a/string/tester.c
> > > > +++ b/string/tester.c
> > > > @@ -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 ();
> > > >
> > > > diff --git a/sysdeps/aarch64/memcmp.S b/sysdeps/aarch64/memcmp.S
> > > > index c1937f6f5c..bc932eff2a 100644
> > > > --- a/sysdeps/aarch64/memcmp.S
> > > > +++ b/sysdeps/aarch64/memcmp.S
> > > > @@ -177,4 +177,6 @@ L(ret_0):
> > > >  END (memcmp)
> > > >  #undef bcmp
> > > >  weak_alias (memcmp, bcmp)
> > > > +#undef __memcmpeq
> > > > +weak_alias (memcmp, __memcmpeq)
> > >
> > > Can we use strong_alias for __memcmpeq?
> >
> > Think we can but not sure if its needed or we should.
>
> __memcmpeq shouldn't have STB_WEAK binding since it
> shouldn't be preempted.

Got it. Note for x86_64 the patch defined an alias from
memcmp -> memcmpeq in two places.

Onces in sysdeps/x86_64/memcmp.S and once
in sysdeps/x86_64/multiarch/memcmp.c. The one in
memcmp.c is the one that will get the IFUNC selection
so I think that's the one we want to keep but will there
be an issue with ld.so usage of __memcmpeq if there
is no alias in memcmp.S? Should the one in memcmp.S
stay weak maybe?
>
>
> --
> H.J.


More information about the Libc-alpha mailing list