public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Adhemerval Zanella <azanella@sourceware.org>
To: glibc-cvs@sourceware.org
Subject: [glibc/azanella/clang] elf: Do not assume relocation ordering to check for output
Date: Fri,  3 Jun 2022 14:12:42 +0000 (GMT)	[thread overview]
Message-ID: <20220603141242.2D44C3850856@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=864814901127d04c70e42e864587c8a834d5134d

commit 864814901127d04c70e42e864587c8a834d5134d
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Thu May 12 14:37:36 2022 -0300

    elf: Do not assume relocation ordering to check for output
    
    The static linker might not create the PLT relocation in the ordering
    expected by the tests.  For instance, on x86_64 binutils shows:
    
      $ readelf -aW elf/tst-audit25a | grep tst_audit25mod[12]_func
      000000000000b0b0  0000001b00000007 R_X86_64_JUMP_SLOT 0000000000000000 tst_audit25mod1_func1 + 0
      000000000000b128  0000002a00000007 R_X86_64_JUMP_SLOT 0000000000000000 tst_audit25mod2_func1 + 0
      000000000000b1b8  0000004300000007 R_X86_64_JUMP_SLOT 0000000000000000 tst_audit25mod1_func2 + 0
      000000000000b228  0000005200000007 R_X86_64_JUMP_SLOT 0000000000000000 tst_audit25mod2_func2 + 0
      [...]
    
    While lld shows:
    
      $ readelf -aW elf/tst-audit25a | grep tst_audit25mod[12]_func
      000000000000a488  0000000900000007 R_X86_64_JUMP_SLOT 0000000000000000 tst_audit25mod1_func1 + 0
      000000000000a490  0000000a00000007 R_X86_64_JUMP_SLOT 0000000000000000 tst_audit25mod1_func2 + 0
      000000000000a498  0000000b00000007 R_X86_64_JUMP_SLOT 0000000000000000 tst_audit25mod2_func1 + 0
      000000000000a4a0  0000000c00000007 R_X86_64_JUMP_SLOT 0000000000000000 tst_audit25mod2_func2 + 0
      [...]
    
    Instead, check each line against the expected set.

Diff:
---
 elf/Makefile       |  4 +---
 elf/tst-audit25.h  | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 elf/tst-audit25a.c | 46 ++++++++++++++++++-------------------
 elf/tst-audit25b.c | 48 ++++++++++++++++++++-------------------
 4 files changed, 115 insertions(+), 49 deletions(-)

diff --git a/elf/Makefile b/elf/Makefile
index 40cc2a19a2..baaffe01ed 100644
--- a/elf/Makefile
+++ b/elf/Makefile
@@ -2343,9 +2343,7 @@ LDFLAGS-tst-audit24d = -Wl,-z,lazy
 
 $(objpfx)tst-audit25a.out: $(objpfx)tst-auditmod25.so
 $(objpfx)tst-audit25a: $(objpfx)tst-audit25mod1.so \
-		       $(objpfx)tst-audit25mod2.so \
-		       $(objpfx)tst-audit25mod3.so \
-		       $(objpfx)tst-audit25mod4.so
+		       $(objpfx)tst-audit25mod2.so
 LDFLAGS-tst-audit25a = -Wl,-z,lazy
 $(objpfx)tst-audit25mod1.so: $(objpfx)tst-audit25mod3.so
 LDFLAGS-tst-audit25mod1.so = -Wl,-z,now
diff --git a/elf/tst-audit25.h b/elf/tst-audit25.h
new file mode 100644
index 0000000000..489d0de519
--- /dev/null
+++ b/elf/tst-audit25.h
@@ -0,0 +1,66 @@
+/* Check LD_AUDIT and LD_BIND_NOW.
+   Copyright (C) 2022 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _TEST_AUDIT25_H
+#define _TEST_AUDIT25_H
+
+#include <string.h>
+#include <support/check.h>
+#include <support/xstdio.h>
+
+/* Check if every line in EXPECTED is presented only once on BUFFER
+   with size of LEN.  */
+static void
+check_output (char *buffer, size_t len, const char *expected[],
+	      size_t expected_len)
+{
+  FILE *f = fmemopen (buffer, len, "r");
+  TEST_VERIFY (f != NULL);
+
+  bool found[expected_len];
+  for (size_t i = 0; i < expected_len; i++)
+    found[i] = false;
+
+  char *line = NULL;
+  size_t linelen = 0;
+  while (xgetline (&line, &linelen, f))
+    {
+      for (size_t i = 0; i < expected_len; i++)
+	if (strcmp (line, expected[i]) == 0)
+	  {
+	    if (found[i] != false)
+	      {
+		support_record_failure ();
+		printf ("error: duplicated line %s\n", expected[i]);
+	      }
+	    TEST_COMPARE (found[i], false);
+	    found[i] = true;
+	  }
+    }
+
+  for (size_t i = 0; i < expected_len; i++)
+    if (found[i] == false)
+      {
+	support_record_failure ();
+	printf ("error: %s not present in output\n", expected[i]);
+      }
+
+  xfclose (f);
+}
+
+#endif
diff --git a/elf/tst-audit25a.c b/elf/tst-audit25a.c
index c2cff8541b..51097b5c9c 100644
--- a/elf/tst-audit25a.c
+++ b/elf/tst-audit25a.c
@@ -17,17 +17,11 @@
    <https://www.gnu.org/licenses/>.  */
 
 #include <array_length.h>
-#include <errno.h>
 #include <getopt.h>
-#include <limits.h>
-#include <inttypes.h>
-#include <string.h>
 #include <stdlib.h>
 #include <support/capture_subprocess.h>
-#include <support/check.h>
-#include <support/xstdio.h>
 #include <support/support.h>
-#include <sys/auxv.h>
+#include <tst-audit25.h>
 
 static int restart;
 #define CMDLINE_OPTIONS \
@@ -82,13 +76,17 @@ do_test (int argc, char *argv[])
     /* tst-audit25a is build with -Wl,-z,lazy and tst-audit25mod1 with
        -Wl,-z,now; so only tst_audit25mod3_func1 should be expected to
        have LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT.  */
-    TEST_COMPARE_STRING (result.err.buffer,
-			 "la_symbind: tst_audit25mod3_func1 1\n"
-			 "la_symbind: tst_audit25mod1_func1 0\n"
-			 "la_symbind: tst_audit25mod1_func2 0\n"
-			 "la_symbind: tst_audit25mod2_func1 0\n"
-			 "la_symbind: tst_audit25mod4_func1 0\n"
-			 "la_symbind: tst_audit25mod2_func2 0\n");
+    const char *expected[] = {
+      "la_symbind: tst_audit25mod3_func1 1\n",
+      "la_symbind: tst_audit25mod1_func1 0\n",
+      "la_symbind: tst_audit25mod1_func2 0\n",
+      "la_symbind: tst_audit25mod2_func1 0\n",
+      "la_symbind: tst_audit25mod4_func1 0\n",
+      "la_symbind: tst_audit25mod2_func2 0\n"
+    };
+
+    check_output (result.err.buffer, result.err.length,
+		  expected, array_length (expected));
 
     support_capture_subprocess_free (&result);
   }
@@ -101,15 +99,17 @@ do_test (int argc, char *argv[])
 				      sc_allow_stderr);
 
     /* With LD_BIND_NOW all symbols are expected to have
-       LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT.  Also the resolution
-       order is done in breadth-first order.  */
-    TEST_COMPARE_STRING (result.err.buffer,
-			 "la_symbind: tst_audit25mod4_func1 1\n"
-			 "la_symbind: tst_audit25mod3_func1 1\n"
-			 "la_symbind: tst_audit25mod1_func1 1\n"
-			 "la_symbind: tst_audit25mod2_func1 1\n"
-			 "la_symbind: tst_audit25mod1_func2 1\n"
-			 "la_symbind: tst_audit25mod2_func2 1\n");
+       LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT.  */
+    const char *expected[] = {
+      "la_symbind: tst_audit25mod4_func1 1\n",
+      "la_symbind: tst_audit25mod3_func1 1\n",
+      "la_symbind: tst_audit25mod1_func1 1\n",
+      "la_symbind: tst_audit25mod2_func1 1\n",
+      "la_symbind: tst_audit25mod1_func2 1\n",
+      "la_symbind: tst_audit25mod2_func2 1\n"
+    };
+    check_output (result.err.buffer, result.err.length,
+		  expected, array_length (expected));
 
     support_capture_subprocess_free (&result);
   }
diff --git a/elf/tst-audit25b.c b/elf/tst-audit25b.c
index 46391770fd..807ff53b3e 100644
--- a/elf/tst-audit25b.c
+++ b/elf/tst-audit25b.c
@@ -16,17 +16,12 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <errno.h>
+#include <array_length.h>
 #include <getopt.h>
-#include <limits.h>
-#include <inttypes.h>
-#include <string.h>
 #include <stdlib.h>
 #include <support/capture_subprocess.h>
-#include <support/check.h>
-#include <support/xstdio.h>
 #include <support/support.h>
-#include <sys/auxv.h>
+#include <tst-audit25.h>
 
 static int restart;
 #define CMDLINE_OPTIONS \
@@ -81,13 +76,17 @@ do_test (int argc, char *argv[])
        tst-audit25mod2 is built with -Wl,-z,lazy.  So only
        tst_audit25mod4_func1 (called by tst_audit25mod2_func1) should not
        have LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT.  */
-    TEST_COMPARE_STRING (result.err.buffer,
-			 "la_symbind: tst_audit25mod3_func1 1\n"
-			 "la_symbind: tst_audit25mod1_func1 1\n"
-			 "la_symbind: tst_audit25mod2_func1 1\n"
-			 "la_symbind: tst_audit25mod1_func2 1\n"
-			 "la_symbind: tst_audit25mod2_func2 1\n"
-			 "la_symbind: tst_audit25mod4_func1 0\n");
+    const char *expected[] = {
+       "la_symbind: tst_audit25mod3_func1 1\n",
+       "la_symbind: tst_audit25mod1_func1 1\n",
+       "la_symbind: tst_audit25mod2_func1 1\n",
+       "la_symbind: tst_audit25mod1_func2 1\n",
+       "la_symbind: tst_audit25mod2_func2 1\n",
+       "la_symbind: tst_audit25mod4_func1 0\n"
+    };
+
+    check_output (result.err.buffer, result.err.length,
+		  expected, array_length (expected));
 
     support_capture_subprocess_free (&result);
   }
@@ -100,15 +99,18 @@ do_test (int argc, char *argv[])
 				      sc_allow_stderr);
 
     /* With LD_BIND_NOW all symbols are expected to have
-       LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT.  Also the resolution
-       order is done in breadth-first order.  */
-    TEST_COMPARE_STRING (result.err.buffer,
-			 "la_symbind: tst_audit25mod4_func1 1\n"
-			 "la_symbind: tst_audit25mod3_func1 1\n"
-			 "la_symbind: tst_audit25mod1_func1 1\n"
-			 "la_symbind: tst_audit25mod2_func1 1\n"
-			 "la_symbind: tst_audit25mod1_func2 1\n"
-			 "la_symbind: tst_audit25mod2_func2 1\n");
+       LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT.  */
+    const char *expected[] = {
+      "la_symbind: tst_audit25mod4_func1 1\n",
+      "la_symbind: tst_audit25mod3_func1 1\n",
+      "la_symbind: tst_audit25mod1_func1 1\n",
+      "la_symbind: tst_audit25mod2_func1 1\n",
+      "la_symbind: tst_audit25mod1_func2 1\n",
+      "la_symbind: tst_audit25mod2_func2 1\n"
+    };
+
+    check_output (result.err.buffer, result.err.length,
+		  expected, array_length (expected));
 
     support_capture_subprocess_free (&result);
   }


             reply	other threads:[~2022-06-03 14:12 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-03 14:12 Adhemerval Zanella [this message]
  -- strict thread matches above, loose matches on Subject: below --
2022-06-09 21:26 Adhemerval Zanella
2022-06-09 13:23 Adhemerval Zanella
2022-05-13 14:26 Adhemerval Zanella
2022-05-12 19:40 Adhemerval Zanella

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220603141242.2D44C3850856@sourceware.org \
    --to=azanella@sourceware.org \
    --cc=glibc-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).