076f82
commit 02c6a3d35316d360ae08623f617b1873d2f6159a
076f82
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
076f82
Date:   Wed Jun 30 15:51:31 2021 -0300
076f82
076f82
    elf: Add audit tests for modules with TLSDESC
076f82
    
076f82
    Checked on x86_64-linux-gnu, i686-linux-gnu, and aarch64-linux-gnu.
076f82
    
076f82
    Reviewed-by: Florian Weimer <fweimer@redhat.com>
076f82
    (cherry picked from commit d1b38173c9255b1a4ae00018ad9b35404a7c74d0)
076f82
076f82
diff --git a/elf/Makefile b/elf/Makefile
076f82
index eab9d46b6165e6be..29f545d2272bf6e2 100644
076f82
--- a/elf/Makefile
076f82
+++ b/elf/Makefile
076f82
@@ -807,6 +807,22 @@ modules-names += tst-gnu2-tls1mod
076f82
 $(objpfx)tst-gnu2-tls1: $(objpfx)tst-gnu2-tls1mod.so
076f82
 tst-gnu2-tls1mod.so-no-z-defs = yes
076f82
 CFLAGS-tst-gnu2-tls1mod.c += -mtls-dialect=gnu2
076f82
+
076f82
+tests += tst-audit-tlsdesc tst-audit-tlsdesc-dlopen
076f82
+modules-names += tst-audit-tlsdesc-mod1 tst-audit-tlsdesc-mod2 tst-auditmod-tlsdesc
076f82
+$(objpfx)tst-audit-tlsdesc: $(objpfx)tst-audit-tlsdesc-mod1.so \
076f82
+			    $(objpfx)tst-audit-tlsdesc-mod2.so \
076f82
+			    $(shared-thread-library)
076f82
+CFLAGS-tst-audit-tlsdesc-mod1.c += -mtls-dialect=gnu2
076f82
+CFLAGS-tst-audit-tlsdesc-mod2.c += -mtls-dialect=gnu2
076f82
+$(objpfx)tst-audit-tlsdesc-dlopen: $(shared-thread-library)
076f82
+$(objpfx)tst-audit-tlsdesc-dlopen.out: $(objpfx)tst-audit-tlsdesc-mod1.so \
076f82
+				       $(objpfx)tst-audit-tlsdesc-mod2.so
076f82
+$(objpfx)tst-audit-tlsdesc-mod1.so: $(objpfx)tst-audit-tlsdesc-mod2.so
076f82
+$(objpfx)tst-audit-tlsdesc.out: $(objpfx)tst-auditmod-tlsdesc.so
076f82
+tst-audit-tlsdesc-ENV = LD_AUDIT=$(objpfx)tst-auditmod-tlsdesc.so
076f82
+$(objpfx)tst-audit-tlsdesc-dlopen.out: $(objpfx)tst-auditmod-tlsdesc.so
076f82
+tst-audit-tlsdesc-dlopen-ENV = LD_AUDIT=$(objpfx)tst-auditmod-tlsdesc.so
076f82
 endif
076f82
 ifeq (yes,$(have-protected-data))
076f82
 modules-names += tst-protected1moda tst-protected1modb
076f82
diff --git a/elf/tst-audit-tlsdesc-dlopen.c b/elf/tst-audit-tlsdesc-dlopen.c
076f82
new file mode 100644
076f82
index 0000000000000000..9c16bb087aca1b77
076f82
--- /dev/null
076f82
+++ b/elf/tst-audit-tlsdesc-dlopen.c
076f82
@@ -0,0 +1,67 @@
076f82
+/* DT_AUDIT with modules with TLSDESC.
076f82
+   Copyright (C) 2021 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 <support/check.h>
076f82
+#include <support/xthread.h>
076f82
+#include <support/xdlfcn.h>
076f82
+
076f82
+static void *
076f82
+thr_func (void *mod)
076f82
+{
076f82
+  int* (*get_global1)(void) = xdlsym (mod, "get_global1");
076f82
+  int* (*get_global2)(void) = xdlsym (mod, "get_global2");
076f82
+  void (*set_global2)(int) = xdlsym (mod, "set_global2");
076f82
+  int* (*get_local1)(void) = xdlsym (mod, "get_local1");
076f82
+  int* (*get_local2)(void) = xdlsym (mod, "get_local2");
076f82
+
076f82
+  int *global1 = get_global1 ();
076f82
+  TEST_COMPARE (*global1, 0);
076f82
+  ++*global1;
076f82
+
076f82
+  int *global2 = get_global2 ();
076f82
+  TEST_COMPARE (*global2, 0);
076f82
+  ++*global2;
076f82
+  TEST_COMPARE (*global2, 1);
076f82
+
076f82
+  set_global2 (10);
076f82
+  TEST_COMPARE (*global2, 10);
076f82
+
076f82
+  int *local1 = get_local1 ();
076f82
+  TEST_COMPARE (*local1, 0);
076f82
+  ++*local1;
076f82
+
076f82
+  int *local2 = get_local2 ();
076f82
+  TEST_COMPARE (*local2, 0);
076f82
+  ++*local2;
076f82
+
076f82
+  return 0;
076f82
+}
076f82
+
076f82
+static int
076f82
+do_test (void)
076f82
+{
076f82
+  void *mod = xdlopen ("tst-audit-tlsdesc-mod1.so", RTLD_LAZY);
076f82
+
076f82
+  pthread_t thr = xpthread_create (NULL, thr_func, mod);
076f82
+  void *r = xpthread_join (thr);
076f82
+  TEST_VERIFY (r == NULL);
076f82
+
076f82
+  return 0;
076f82
+}
076f82
+
076f82
+#include <support/test-driver.c>
076f82
diff --git a/elf/tst-audit-tlsdesc-mod1.c b/elf/tst-audit-tlsdesc-mod1.c
076f82
new file mode 100644
076f82
index 0000000000000000..61c7dd99a2fb5e28
076f82
--- /dev/null
076f82
+++ b/elf/tst-audit-tlsdesc-mod1.c
076f82
@@ -0,0 +1,41 @@
076f82
+/* DT_AUDIT with modules with TLSDESC.
076f82
+   Copyright (C) 2021 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
+__thread int global1;
076f82
+
076f82
+int *
076f82
+get_global1 (void)
076f82
+{
076f82
+  return &global1;
076f82
+}
076f82
+
076f82
+static __thread int local1;
076f82
+
076f82
+void *
076f82
+get_local1 (void)
076f82
+{
076f82
+  return &local1;
076f82
+}
076f82
+
076f82
+extern __thread int global2;
076f82
+
076f82
+void
076f82
+set_global2 (int v)
076f82
+{
076f82
+  global2 = v;
076f82
+}
076f82
diff --git a/elf/tst-audit-tlsdesc-mod2.c b/elf/tst-audit-tlsdesc-mod2.c
076f82
new file mode 100644
076f82
index 0000000000000000..28aef635f688ee03
076f82
--- /dev/null
076f82
+++ b/elf/tst-audit-tlsdesc-mod2.c
076f82
@@ -0,0 +1,33 @@
076f82
+/* DT_AUDIT with modules with TLSDESC.
076f82
+   Copyright (C) 2021 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
+__thread int global2;
076f82
+
076f82
+int *
076f82
+get_global2 (void)
076f82
+{
076f82
+  return &global2;
076f82
+}
076f82
+
076f82
+static __thread int local2;
076f82
+
076f82
+void *
076f82
+get_local2 (void)
076f82
+{
076f82
+  return &local2;
076f82
+}
076f82
diff --git a/elf/tst-audit-tlsdesc.c b/elf/tst-audit-tlsdesc.c
076f82
new file mode 100644
076f82
index 0000000000000000..3c8be81c95528f47
076f82
--- /dev/null
076f82
+++ b/elf/tst-audit-tlsdesc.c
076f82
@@ -0,0 +1,60 @@
076f82
+/* DT_AUDIT with modules with TLSDESC.
076f82
+   Copyright (C) 2021 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 <support/check.h>
076f82
+#include <support/xthread.h>
076f82
+
076f82
+extern __thread int global1;
076f82
+extern __thread int global2;
076f82
+void *get_local1 (void);
076f82
+void set_global2 (int v);
076f82
+void *get_local2 (void);
076f82
+
076f82
+static void *
076f82
+thr_func (void *clousure)
076f82
+{
076f82
+  TEST_COMPARE (global1, 0);
076f82
+  ++global1;
076f82
+  TEST_COMPARE (global2, 0);
076f82
+  ++global2;
076f82
+  TEST_COMPARE (global2, 1);
076f82
+
076f82
+  set_global2 (10);
076f82
+  TEST_COMPARE (global2, 10);
076f82
+
076f82
+  int *local1 = get_local1 ();
076f82
+  TEST_COMPARE (*local1, 0);
076f82
+  ++*local1;
076f82
+
076f82
+  int *local2 = get_local2 ();
076f82
+  TEST_COMPARE (*local2, 0);
076f82
+  ++*local2;
076f82
+
076f82
+  return 0;
076f82
+}
076f82
+
076f82
+static int
076f82
+do_test (void)
076f82
+{
076f82
+  pthread_t thr = xpthread_create (NULL, thr_func, NULL);
076f82
+  void *r = xpthread_join (thr);
076f82
+  TEST_VERIFY (r == NULL);
076f82
+  return 0;
076f82
+}
076f82
+
076f82
+#include <support/test-driver.c>
076f82
diff --git a/elf/tst-auditmod-tlsdesc.c b/elf/tst-auditmod-tlsdesc.c
076f82
new file mode 100644
076f82
index 0000000000000000..e4b835d1f1fb6f73
076f82
--- /dev/null
076f82
+++ b/elf/tst-auditmod-tlsdesc.c
076f82
@@ -0,0 +1,25 @@
076f82
+/* DT_AUDIT with modules with TLSDESC.
076f82
+   Copyright (C) 2021 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 <link.h>
076f82
+
076f82
+unsigned int
076f82
+la_version (unsigned int version)
076f82
+{
076f82
+  return LAV_CURRENT;
076f82
+}