08c3a6
commit b2585cae2854d7d2868fb2e51e2796042c5e0679
08c3a6
Author: Szabolcs Nagy <szabolcs.nagy@arm.com>
08c3a6
Date:   Tue May 3 13:18:04 2022 +0100
08c3a6
08c3a6
    linux: Add a getauxval test [BZ #23293]
08c3a6
    
08c3a6
    This is for bug 23293 and it relies on the glibc test system running
08c3a6
    tests via explicit ld.so invokation by default.
08c3a6
    
08c3a6
    Reviewed-by: Florian Weimer <fweimer@redhat.com>
08c3a6
    Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
08c3a6
    (cherry picked from commit 9faf5262c77487c96da8a3e961b88c0b1879e186)
08c3a6
08c3a6
diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
08c3a6
index 0657f4003e7116c6..5c772f69d1b1f1f1 100644
08c3a6
--- a/sysdeps/unix/sysv/linux/Makefile
08c3a6
+++ b/sysdeps/unix/sysv/linux/Makefile
08c3a6
@@ -123,6 +123,7 @@ tests += tst-clone tst-clone2 tst-clone3 tst-fanotify tst-personality \
08c3a6
   tst-close_range \
08c3a6
   tst-prctl \
08c3a6
   tst-scm_rights \
08c3a6
+  tst-getauxval \
08c3a6
   # tests
08c3a6
 
08c3a6
 # Test for the symbol version of fcntl that was replaced in glibc 2.28.
08c3a6
diff --git a/sysdeps/unix/sysv/linux/tst-getauxval.c b/sysdeps/unix/sysv/linux/tst-getauxval.c
08c3a6
new file mode 100644
08c3a6
index 0000000000000000..c4b619574369f4c5
08c3a6
--- /dev/null
08c3a6
+++ b/sysdeps/unix/sysv/linux/tst-getauxval.c
08c3a6
@@ -0,0 +1,74 @@
08c3a6
+/* Basic test for getauxval.
08c3a6
+   Copyright (C) 2022 Free Software Foundation, Inc.
08c3a6
+   This file is part of the GNU C Library.
08c3a6
+
08c3a6
+   The GNU C Library is free software; you can redistribute it and/or
08c3a6
+   modify it under the terms of the GNU Lesser General Public
08c3a6
+   License as published by the Free Software Foundation; either
08c3a6
+   version 2.1 of the License, or (at your option) any later version.
08c3a6
+
08c3a6
+   The GNU C Library is distributed in the hope that it will be useful,
08c3a6
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
08c3a6
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
08c3a6
+   Lesser General Public License for more details.
08c3a6
+
08c3a6
+   You should have received a copy of the GNU Lesser General Public
08c3a6
+   License along with the GNU C Library; if not, see
08c3a6
+   <https://www.gnu.org/licenses/>.  */
08c3a6
+
08c3a6
+#include <unistd.h>
08c3a6
+#include <stdio.h>
08c3a6
+#include <support/check.h>
08c3a6
+#include <sys/auxv.h>
08c3a6
+
08c3a6
+static int missing;
08c3a6
+static int mismatch;
08c3a6
+
08c3a6
+static void
08c3a6
+check_nonzero (unsigned long t, const char *s)
08c3a6
+{
08c3a6
+  unsigned long v = getauxval (t);
08c3a6
+  printf ("%s: %lu (0x%lx)\n", s, v, v);
08c3a6
+  if (v == 0)
08c3a6
+    missing++;
08c3a6
+}
08c3a6
+
08c3a6
+static void
08c3a6
+check_eq (unsigned long t, const char *s, unsigned long want)
08c3a6
+{
08c3a6
+  unsigned long v = getauxval (t);
08c3a6
+  printf ("%s: %lu want: %lu\n", s, v, want);
08c3a6
+  if (v != want)
08c3a6
+    mismatch++;
08c3a6
+}
08c3a6
+
08c3a6
+#define NZ(x) check_nonzero (x, #x)
08c3a6
+#define EQ(x, want) check_eq (x, #x, want)
08c3a6
+
08c3a6
+static int
08c3a6
+do_test (void)
08c3a6
+{
08c3a6
+  /* These auxv entries should be non-zero on Linux.  */
08c3a6
+  NZ (AT_PHDR);
08c3a6
+  NZ (AT_PHENT);
08c3a6
+  NZ (AT_PHNUM);
08c3a6
+  NZ (AT_PAGESZ);
08c3a6
+  NZ (AT_ENTRY);
08c3a6
+  NZ (AT_CLKTCK);
08c3a6
+  NZ (AT_RANDOM);
08c3a6
+  NZ (AT_EXECFN);
08c3a6
+  if (missing)
08c3a6
+    FAIL_EXIT1 ("Found %d missing auxv entries.\n", missing);
08c3a6
+
08c3a6
+  /* Check against syscalls.  */
08c3a6
+  EQ (AT_UID, getuid ());
08c3a6
+  EQ (AT_EUID, geteuid ());
08c3a6
+  EQ (AT_GID, getgid ());
08c3a6
+  EQ (AT_EGID, getegid ());
08c3a6
+  if (mismatch)
08c3a6
+    FAIL_EXIT1 ("Found %d mismatching auxv entries.\n", mismatch);
08c3a6
+
08c3a6
+  return 0;
08c3a6
+}
08c3a6
+
08c3a6
+#include <support/test-driver.c>