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