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