a8db42b
commit 02c6a3d35316d360ae08623f617b1873d2f6159a
a8db42b
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
a8db42b
Date:   Wed Jun 30 15:51:31 2021 -0300
a8db42b
a8db42b
    elf: Add audit tests for modules with TLSDESC
a8db42b
    
a8db42b
    Checked on x86_64-linux-gnu, i686-linux-gnu, and aarch64-linux-gnu.
a8db42b
    
a8db42b
    Reviewed-by: Florian Weimer <fweimer@redhat.com>
a8db42b
    (cherry picked from commit d1b38173c9255b1a4ae00018ad9b35404a7c74d0)
a8db42b
a8db42b
diff --git a/elf/Makefile b/elf/Makefile
a8db42b
index eab9d46b6165e6be..29f545d2272bf6e2 100644
a8db42b
--- a/elf/Makefile
a8db42b
+++ b/elf/Makefile
a8db42b
@@ -807,6 +807,22 @@ modules-names += tst-gnu2-tls1mod
a8db42b
 $(objpfx)tst-gnu2-tls1: $(objpfx)tst-gnu2-tls1mod.so
a8db42b
 tst-gnu2-tls1mod.so-no-z-defs = yes
a8db42b
 CFLAGS-tst-gnu2-tls1mod.c += -mtls-dialect=gnu2
a8db42b
+
a8db42b
+tests += tst-audit-tlsdesc tst-audit-tlsdesc-dlopen
a8db42b
+modules-names += tst-audit-tlsdesc-mod1 tst-audit-tlsdesc-mod2 tst-auditmod-tlsdesc
a8db42b
+$(objpfx)tst-audit-tlsdesc: $(objpfx)tst-audit-tlsdesc-mod1.so \
a8db42b
+			    $(objpfx)tst-audit-tlsdesc-mod2.so \
a8db42b
+			    $(shared-thread-library)
a8db42b
+CFLAGS-tst-audit-tlsdesc-mod1.c += -mtls-dialect=gnu2
a8db42b
+CFLAGS-tst-audit-tlsdesc-mod2.c += -mtls-dialect=gnu2
a8db42b
+$(objpfx)tst-audit-tlsdesc-dlopen: $(shared-thread-library)
a8db42b
+$(objpfx)tst-audit-tlsdesc-dlopen.out: $(objpfx)tst-audit-tlsdesc-mod1.so \
a8db42b
+				       $(objpfx)tst-audit-tlsdesc-mod2.so
a8db42b
+$(objpfx)tst-audit-tlsdesc-mod1.so: $(objpfx)tst-audit-tlsdesc-mod2.so
a8db42b
+$(objpfx)tst-audit-tlsdesc.out: $(objpfx)tst-auditmod-tlsdesc.so
a8db42b
+tst-audit-tlsdesc-ENV = LD_AUDIT=$(objpfx)tst-auditmod-tlsdesc.so
a8db42b
+$(objpfx)tst-audit-tlsdesc-dlopen.out: $(objpfx)tst-auditmod-tlsdesc.so
a8db42b
+tst-audit-tlsdesc-dlopen-ENV = LD_AUDIT=$(objpfx)tst-auditmod-tlsdesc.so
a8db42b
 endif
a8db42b
 ifeq (yes,$(have-protected-data))
a8db42b
 modules-names += tst-protected1moda tst-protected1modb
a8db42b
diff --git a/elf/tst-audit-tlsdesc-dlopen.c b/elf/tst-audit-tlsdesc-dlopen.c
a8db42b
new file mode 100644
a8db42b
index 0000000000000000..9c16bb087aca1b77
a8db42b
--- /dev/null
a8db42b
+++ b/elf/tst-audit-tlsdesc-dlopen.c
a8db42b
@@ -0,0 +1,67 @@
a8db42b
+/* DT_AUDIT with modules with TLSDESC.
a8db42b
+   Copyright (C) 2021 Free Software Foundation, Inc.
a8db42b
+   This file is part of the GNU C Library.
a8db42b
+
a8db42b
+   The GNU C Library is free software; you can redistribute it and/or
a8db42b
+   modify it under the terms of the GNU Lesser General Public
a8db42b
+   License as published by the Free Software Foundation; either
a8db42b
+   version 2.1 of the License, or (at your option) any later version.
a8db42b
+
a8db42b
+   The GNU C Library is distributed in the hope that it will be useful,
a8db42b
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
a8db42b
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
a8db42b
+   Lesser General Public License for more details.
a8db42b
+
a8db42b
+   You should have received a copy of the GNU Lesser General Public
a8db42b
+   License along with the GNU C Library; if not, see
a8db42b
+   <https://www.gnu.org/licenses/>.  */
a8db42b
+
a8db42b
+#include <support/check.h>
a8db42b
+#include <support/xthread.h>
a8db42b
+#include <support/xdlfcn.h>
a8db42b
+
a8db42b
+static void *
a8db42b
+thr_func (void *mod)
a8db42b
+{
a8db42b
+  int* (*get_global1)(void) = xdlsym (mod, "get_global1");
a8db42b
+  int* (*get_global2)(void) = xdlsym (mod, "get_global2");
a8db42b
+  void (*set_global2)(int) = xdlsym (mod, "set_global2");
a8db42b
+  int* (*get_local1)(void) = xdlsym (mod, "get_local1");
a8db42b
+  int* (*get_local2)(void) = xdlsym (mod, "get_local2");
a8db42b
+
a8db42b
+  int *global1 = get_global1 ();
a8db42b
+  TEST_COMPARE (*global1, 0);
a8db42b
+  ++*global1;
a8db42b
+
a8db42b
+  int *global2 = get_global2 ();
a8db42b
+  TEST_COMPARE (*global2, 0);
a8db42b
+  ++*global2;
a8db42b
+  TEST_COMPARE (*global2, 1);
a8db42b
+
a8db42b
+  set_global2 (10);
a8db42b
+  TEST_COMPARE (*global2, 10);
a8db42b
+
a8db42b
+  int *local1 = get_local1 ();
a8db42b
+  TEST_COMPARE (*local1, 0);
a8db42b
+  ++*local1;
a8db42b
+
a8db42b
+  int *local2 = get_local2 ();
a8db42b
+  TEST_COMPARE (*local2, 0);
a8db42b
+  ++*local2;
a8db42b
+
a8db42b
+  return 0;
a8db42b
+}
a8db42b
+
a8db42b
+static int
a8db42b
+do_test (void)
a8db42b
+{
a8db42b
+  void *mod = xdlopen ("tst-audit-tlsdesc-mod1.so", RTLD_LAZY);
a8db42b
+
a8db42b
+  pthread_t thr = xpthread_create (NULL, thr_func, mod);
a8db42b
+  void *r = xpthread_join (thr);
a8db42b
+  TEST_VERIFY (r == NULL);
a8db42b
+
a8db42b
+  return 0;
a8db42b
+}
a8db42b
+
a8db42b
+#include <support/test-driver.c>
a8db42b
diff --git a/elf/tst-audit-tlsdesc-mod1.c b/elf/tst-audit-tlsdesc-mod1.c
a8db42b
new file mode 100644
a8db42b
index 0000000000000000..61c7dd99a2fb5e28
a8db42b
--- /dev/null
a8db42b
+++ b/elf/tst-audit-tlsdesc-mod1.c
a8db42b
@@ -0,0 +1,41 @@
a8db42b
+/* DT_AUDIT with modules with TLSDESC.
a8db42b
+   Copyright (C) 2021 Free Software Foundation, Inc.
a8db42b
+   This file is part of the GNU C Library.
a8db42b
+
a8db42b
+   The GNU C Library is free software; you can redistribute it and/or
a8db42b
+   modify it under the terms of the GNU Lesser General Public
a8db42b
+   License as published by the Free Software Foundation; either
a8db42b
+   version 2.1 of the License, or (at your option) any later version.
a8db42b
+
a8db42b
+   The GNU C Library is distributed in the hope that it will be useful,
a8db42b
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
a8db42b
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
a8db42b
+   Lesser General Public License for more details.
a8db42b
+
a8db42b
+   You should have received a copy of the GNU Lesser General Public
a8db42b
+   License along with the GNU C Library; if not, see
a8db42b
+   <https://www.gnu.org/licenses/>.  */
a8db42b
+
a8db42b
+__thread int global1;
a8db42b
+
a8db42b
+int *
a8db42b
+get_global1 (void)
a8db42b
+{
a8db42b
+  return &global1;
a8db42b
+}
a8db42b
+
a8db42b
+static __thread int local1;
a8db42b
+
a8db42b
+void *
a8db42b
+get_local1 (void)
a8db42b
+{
a8db42b
+  return &local1;
a8db42b
+}
a8db42b
+
a8db42b
+extern __thread int global2;
a8db42b
+
a8db42b
+void
a8db42b
+set_global2 (int v)
a8db42b
+{
a8db42b
+  global2 = v;
a8db42b
+}
a8db42b
diff --git a/elf/tst-audit-tlsdesc-mod2.c b/elf/tst-audit-tlsdesc-mod2.c
a8db42b
new file mode 100644
a8db42b
index 0000000000000000..28aef635f688ee03
a8db42b
--- /dev/null
a8db42b
+++ b/elf/tst-audit-tlsdesc-mod2.c
a8db42b
@@ -0,0 +1,33 @@
a8db42b
+/* DT_AUDIT with modules with TLSDESC.
a8db42b
+   Copyright (C) 2021 Free Software Foundation, Inc.
a8db42b
+   This file is part of the GNU C Library.
a8db42b
+
a8db42b
+   The GNU C Library is free software; you can redistribute it and/or
a8db42b
+   modify it under the terms of the GNU Lesser General Public
a8db42b
+   License as published by the Free Software Foundation; either
a8db42b
+   version 2.1 of the License, or (at your option) any later version.
a8db42b
+
a8db42b
+   The GNU C Library is distributed in the hope that it will be useful,
a8db42b
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
a8db42b
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
a8db42b
+   Lesser General Public License for more details.
a8db42b
+
a8db42b
+   You should have received a copy of the GNU Lesser General Public
a8db42b
+   License along with the GNU C Library; if not, see
a8db42b
+   <https://www.gnu.org/licenses/>.  */
a8db42b
+
a8db42b
+__thread int global2;
a8db42b
+
a8db42b
+int *
a8db42b
+get_global2 (void)
a8db42b
+{
a8db42b
+  return &global2;
a8db42b
+}
a8db42b
+
a8db42b
+static __thread int local2;
a8db42b
+
a8db42b
+void *
a8db42b
+get_local2 (void)
a8db42b
+{
a8db42b
+  return &local2;
a8db42b
+}
a8db42b
diff --git a/elf/tst-audit-tlsdesc.c b/elf/tst-audit-tlsdesc.c
a8db42b
new file mode 100644
a8db42b
index 0000000000000000..3c8be81c95528f47
a8db42b
--- /dev/null
a8db42b
+++ b/elf/tst-audit-tlsdesc.c
a8db42b
@@ -0,0 +1,60 @@
a8db42b
+/* DT_AUDIT with modules with TLSDESC.
a8db42b
+   Copyright (C) 2021 Free Software Foundation, Inc.
a8db42b
+   This file is part of the GNU C Library.
a8db42b
+
a8db42b
+   The GNU C Library is free software; you can redistribute it and/or
a8db42b
+   modify it under the terms of the GNU Lesser General Public
a8db42b
+   License as published by the Free Software Foundation; either
a8db42b
+   version 2.1 of the License, or (at your option) any later version.
a8db42b
+
a8db42b
+   The GNU C Library is distributed in the hope that it will be useful,
a8db42b
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
a8db42b
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
a8db42b
+   Lesser General Public License for more details.
a8db42b
+
a8db42b
+   You should have received a copy of the GNU Lesser General Public
a8db42b
+   License along with the GNU C Library; if not, see
a8db42b
+   <https://www.gnu.org/licenses/>.  */
a8db42b
+
a8db42b
+#include <support/check.h>
a8db42b
+#include <support/xthread.h>
a8db42b
+
a8db42b
+extern __thread int global1;
a8db42b
+extern __thread int global2;
a8db42b
+void *get_local1 (void);
a8db42b
+void set_global2 (int v);
a8db42b
+void *get_local2 (void);
a8db42b
+
a8db42b
+static void *
a8db42b
+thr_func (void *clousure)
a8db42b
+{
a8db42b
+  TEST_COMPARE (global1, 0);
a8db42b
+  ++global1;
a8db42b
+  TEST_COMPARE (global2, 0);
a8db42b
+  ++global2;
a8db42b
+  TEST_COMPARE (global2, 1);
a8db42b
+
a8db42b
+  set_global2 (10);
a8db42b
+  TEST_COMPARE (global2, 10);
a8db42b
+
a8db42b
+  int *local1 = get_local1 ();
a8db42b
+  TEST_COMPARE (*local1, 0);
a8db42b
+  ++*local1;
a8db42b
+
a8db42b
+  int *local2 = get_local2 ();
a8db42b
+  TEST_COMPARE (*local2, 0);
a8db42b
+  ++*local2;
a8db42b
+
a8db42b
+  return 0;
a8db42b
+}
a8db42b
+
a8db42b
+static int
a8db42b
+do_test (void)
a8db42b
+{
a8db42b
+  pthread_t thr = xpthread_create (NULL, thr_func, NULL);
a8db42b
+  void *r = xpthread_join (thr);
a8db42b
+  TEST_VERIFY (r == NULL);
a8db42b
+  return 0;
a8db42b
+}
a8db42b
+
a8db42b
+#include <support/test-driver.c>
a8db42b
diff --git a/elf/tst-auditmod-tlsdesc.c b/elf/tst-auditmod-tlsdesc.c
a8db42b
new file mode 100644
a8db42b
index 0000000000000000..e4b835d1f1fb6f73
a8db42b
--- /dev/null
a8db42b
+++ b/elf/tst-auditmod-tlsdesc.c
a8db42b
@@ -0,0 +1,25 @@
a8db42b
+/* DT_AUDIT with modules with TLSDESC.
a8db42b
+   Copyright (C) 2021 Free Software Foundation, Inc.
a8db42b
+   This file is part of the GNU C Library.
a8db42b
+
a8db42b
+   The GNU C Library is free software; you can redistribute it and/or
a8db42b
+   modify it under the terms of the GNU Lesser General Public
a8db42b
+   License as published by the Free Software Foundation; either
a8db42b
+   version 2.1 of the License, or (at your option) any later version.
a8db42b
+
a8db42b
+   The GNU C Library is distributed in the hope that it will be useful,
a8db42b
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
a8db42b
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
a8db42b
+   Lesser General Public License for more details.
a8db42b
+
a8db42b
+   You should have received a copy of the GNU Lesser General Public
a8db42b
+   License along with the GNU C Library; if not, see
a8db42b
+   <https://www.gnu.org/licenses/>.  */
a8db42b
+
a8db42b
+#include <link.h>
a8db42b
+
a8db42b
+unsigned int
a8db42b
+la_version (unsigned int version)
a8db42b
+{
a8db42b
+  return LAV_CURRENT;
a8db42b
+}