Blame SOURCES/gdb-rhbz1156192-recursive-dlopen-test.patch

a8223e
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
a8223e
From: Fedora GDB patches <invalid@email.com>
a8223e
Date: Fri, 27 Oct 2017 21:07:50 +0200
a8223e
Subject: gdb-rhbz1156192-recursive-dlopen-test.patch
a8223e
a8223e
;; Testcase for '[SAP] Recursive dlopen causes SAP HANA installer to
a8223e
;; crash.' (RH BZ 1156192).
a8223e
;;=fedoratest
a8223e
a8223e
diff --git a/gdb/testsuite/gdb.base/gdb-rhbz1156192-recursive-dlopen-libbar.c b/gdb/testsuite/gdb.base/gdb-rhbz1156192-recursive-dlopen-libbar.c
a8223e
new file mode 100644
a8223e
--- /dev/null
a8223e
+++ b/gdb/testsuite/gdb.base/gdb-rhbz1156192-recursive-dlopen-libbar.c
a8223e
@@ -0,0 +1,30 @@
a8223e
+/* Testcase for recursive dlopen calls.
a8223e
+
a8223e
+   Copyright (C) 2014 Free Software Foundation, Inc.
a8223e
+
a8223e
+   This file is part of GDB.
a8223e
+
a8223e
+   This program is free software; you can redistribute it and/or modify
a8223e
+   it under the terms of the GNU General Public License as published by
a8223e
+   the Free Software Foundation; either version 3 of the License, or
a8223e
+   (at your option) any later version.
a8223e
+
a8223e
+   This program is distributed in the hope that it will be useful,
a8223e
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
a8223e
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
a8223e
+   GNU General Public License for more details.
a8223e
+
a8223e
+   You should have received a copy of the GNU General Public License
a8223e
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
a8223e
+
a8223e
+/* This test was copied from glibc's testcase called
a8223e
+   <dlfcn/tst-rec-dlopen.c> and related files.  */
a8223e
+
a8223e
+#include <stdio.h>
a8223e
+#include <stdlib.h>
a8223e
+
a8223e
+void
a8223e
+bar (void)
a8223e
+{
a8223e
+  printf ("Called bar.\n");
a8223e
+}
a8223e
diff --git a/gdb/testsuite/gdb.base/gdb-rhbz1156192-recursive-dlopen-libfoo.c b/gdb/testsuite/gdb.base/gdb-rhbz1156192-recursive-dlopen-libfoo.c
a8223e
new file mode 100644
a8223e
--- /dev/null
a8223e
+++ b/gdb/testsuite/gdb.base/gdb-rhbz1156192-recursive-dlopen-libfoo.c
a8223e
@@ -0,0 +1,30 @@
a8223e
+/* Testcase for recursive dlopen calls.
a8223e
+
a8223e
+   Copyright (C) 2014 Free Software Foundation, Inc.
a8223e
+
a8223e
+   This file is part of GDB.
a8223e
+
a8223e
+   This program is free software; you can redistribute it and/or modify
a8223e
+   it under the terms of the GNU General Public License as published by
a8223e
+   the Free Software Foundation; either version 3 of the License, or
a8223e
+   (at your option) any later version.
a8223e
+
a8223e
+   This program is distributed in the hope that it will be useful,
a8223e
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
a8223e
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
a8223e
+   GNU General Public License for more details.
a8223e
+
a8223e
+   You should have received a copy of the GNU General Public License
a8223e
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
a8223e
+
a8223e
+/* This test was copied from glibc's testcase called
a8223e
+   <dlfcn/tst-rec-dlopen.c> and related files.  */
a8223e
+
a8223e
+#include <stdio.h>
a8223e
+#include <stdlib.h>
a8223e
+
a8223e
+void
a8223e
+foo (void)
a8223e
+{
a8223e
+  printf ("Called foo.\n");
a8223e
+}
a8223e
diff --git a/gdb/testsuite/gdb.base/gdb-rhbz1156192-recursive-dlopen.c b/gdb/testsuite/gdb.base/gdb-rhbz1156192-recursive-dlopen.c
a8223e
new file mode 100644
a8223e
--- /dev/null
a8223e
+++ b/gdb/testsuite/gdb.base/gdb-rhbz1156192-recursive-dlopen.c
a8223e
@@ -0,0 +1,125 @@
a8223e
+/* Testcase for recursive dlopen calls.
a8223e
+
a8223e
+   Copyright (C) 2014 Free Software Foundation, Inc.
a8223e
+
a8223e
+   This file is part of GDB.
a8223e
+
a8223e
+   This program is free software; you can redistribute it and/or modify
a8223e
+   it under the terms of the GNU General Public License as published by
a8223e
+   the Free Software Foundation; either version 3 of the License, or
a8223e
+   (at your option) any later version.
a8223e
+
a8223e
+   This program is distributed in the hope that it will be useful,
a8223e
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
a8223e
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
a8223e
+   GNU General Public License for more details.
a8223e
+
a8223e
+   You should have received a copy of the GNU General Public License
a8223e
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
a8223e
+
a8223e
+/* This test was copied from glibc's testcase called
a8223e
+   <dlfcn/tst-rec-dlopen.c> and related files.  */
a8223e
+
a8223e
+#include <stdio.h>
a8223e
+#include <stdlib.h>
a8223e
+#include <malloc.h>
a8223e
+#include <dlfcn.h>
a8223e
+
a8223e
+#define DSO "gdb-rhbz1156192-recursive-dlopen-libfoo.so"
a8223e
+#define FUNC "foo"
a8223e
+
a8223e
+#define DSO1 "gdb-rhbz1156192-recursive-dlopen-libbar.so"
a8223e
+#define FUNC1 "bar"
a8223e
+
a8223e
+/* Prototype for my hook.  */
a8223e
+void *custom_malloc_hook (size_t, const void *);
a8223e
+
a8223e
+/* Pointer to old malloc hooks.  */
a8223e
+void *(*old_malloc_hook) (size_t, const void *);
a8223e
+
a8223e
+/* Call function func_name in DSO dso_name via dlopen.  */
a8223e
+void
a8223e
+call_func (const char *dso_name, const char *func_name)
a8223e
+{
a8223e
+  int ret;
a8223e
+  void *dso;
a8223e
+  void (*func) (void);
a8223e
+  char *err;
a8223e
+
a8223e
+  /* Open the DSO.  */
a8223e
+  dso = dlopen (dso_name, RTLD_NOW|RTLD_GLOBAL);
a8223e
+  if (dso == NULL)
a8223e
+    {
a8223e
+      err = dlerror ();
a8223e
+      fprintf (stderr, "%s\n", err);
a8223e
+      exit (1);
a8223e
+    }
a8223e
+  /* Clear any errors.  */
a8223e
+  dlerror ();
a8223e
+
a8223e
+  /* Lookup func.  */
a8223e
+  *(void **) (&func) = dlsym (dso, func_name);
a8223e
+  if (func == NULL)
a8223e
+    {
a8223e
+      err = dlerror ();
a8223e
+      if (err != NULL)
a8223e
+        {
a8223e
+  fprintf (stderr, "%s\n", err);
a8223e
+  exit (1);
a8223e
+        }
a8223e
+    }
a8223e
+  /* Call func twice.  */
a8223e
+  (*func) ();
a8223e
+
a8223e
+  /* Close the library and look for errors too.  */
a8223e
+  ret = dlclose (dso);
a8223e
+  if (ret != 0)
a8223e
+    {
a8223e
+      err = dlerror ();
a8223e
+      fprintf (stderr, "%s\n", err);
a8223e
+      exit (1);
a8223e
+    }
a8223e
+
a8223e
+}
a8223e
+
a8223e
+/* Empty hook that does nothing.  */
a8223e
+void *
a8223e
+custom_malloc_hook (size_t size, const void *caller)
a8223e
+{
a8223e
+  void *result;
a8223e
+  /* Restore old hooks.  */
a8223e
+  __malloc_hook = old_malloc_hook;
a8223e
+  /* First call a function in another library via dlopen.  */
a8223e
+  call_func (DSO1, FUNC1);
a8223e
+  /* Called recursively.  */
a8223e
+  result = malloc (size);
a8223e
+  /* Restore new hooks.  */
a8223e
+  old_malloc_hook = __malloc_hook;
a8223e
+  __malloc_hook = custom_malloc_hook;
a8223e
+  return result;
a8223e
+}
a8223e
+
a8223e
+int
a8223e
+main (void)
a8223e
+{
a8223e
+
a8223e
+  /* Save old hook.  */
a8223e
+  old_malloc_hook = __malloc_hook;
a8223e
+  /* Install new hook.  */
a8223e
+  __malloc_hook = custom_malloc_hook;
a8223e
+
a8223e
+  /* Attempt to dlopen a shared library. This dlopen will
a8223e
+     trigger an access to the ld.so.cache, and that in turn
a8223e
+     will require a malloc to duplicate data in the cache.
a8223e
+     The malloc will call our malloc hook which calls dlopen
a8223e
+     recursively, and upon return of this dlopen the non-ref
a8223e
+     counted ld.so.cache mapping will be unmapped. We will
a8223e
+     return to the original dlopen and crash trying to access
a8223e
+     dlopened data.  */
a8223e
+  call_func (DSO, FUNC);
a8223e
+
a8223e
+  /* Restore old hook.  */
a8223e
+  __malloc_hook = old_malloc_hook;
a8223e
+
a8223e
+  return 0;
a8223e
+}
a8223e
diff --git a/gdb/testsuite/gdb.base/gdb-rhbz1156192-recursive-dlopen.exp b/gdb/testsuite/gdb.base/gdb-rhbz1156192-recursive-dlopen.exp
a8223e
new file mode 100644
a8223e
--- /dev/null
a8223e
+++ b/gdb/testsuite/gdb.base/gdb-rhbz1156192-recursive-dlopen.exp
a8223e
@@ -0,0 +1,157 @@
a8223e
+# Copyright 2014 Free Software Foundation, Inc.
a8223e
+#
a8223e
+# This program is free software; you can redistribute it and/or modify
a8223e
+# it under the terms of the GNU General Public License as published by
a8223e
+# the Free Software Foundation; either version 3 of the License, or
a8223e
+# (at your option) any later version.
a8223e
+#
a8223e
+# This program is distributed in the hope that it will be useful,
a8223e
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
a8223e
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
a8223e
+# GNU General Public License for more details.
a8223e
+#
a8223e
+# You should have received a copy of the GNU General Public License
a8223e
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
a8223e
+
a8223e
+if {[skip_shlib_tests]} {
a8223e
+    untested "skipping shlib tests"
a8223e
+    return 0
a8223e
+} elseif {[use_gdb_stub]} {
a8223e
+    untested "skipping tests because of stub"
a8223e
+    return 0
a8223e
+}
a8223e
+
a8223e
+# Library foo
a8223e
+set libname1 "gdb-rhbz1156192-recursive-dlopen-libfoo"
a8223e
+set srcfile_lib1 ${srcdir}/${subdir}/${libname1}.c
a8223e
+set binfile_lib1 [standard_output_file ${libname1}.so]
a8223e
+# Library bar
a8223e
+set libname2 "gdb-rhbz1156192-recursive-dlopen-libbar"
a8223e
+set srcfile_lib2 ${srcdir}/${subdir}/${libname2}.c
a8223e
+set binfile_lib2 [standard_output_file ${libname2}.so]
a8223e
+
a8223e
+set testfile "gdb-rhbz1156192-recursive-dlopen"
a8223e
+set srcfile ${testfile}.c
a8223e
+set executable ${testfile}
a8223e
+set binfile [standard_output_file ${executable}]
a8223e
+
a8223e
+if { [gdb_compile_shlib ${srcfile_lib1} ${binfile_lib1} \
a8223e
+	{ debug "additional_flags=-fPIC" }] != "" } {
a8223e
+    untested "Could not compile ${binfile_lib1}"
a8223e
+    return -1
a8223e
+}
a8223e
+
a8223e
+if { [gdb_compile_shlib ${srcfile_lib2} ${binfile_lib2} \
a8223e
+	{ debug "additional_flags=-fPIC" }] != "" } {
a8223e
+    untested "Could not compile ${binfile_lib2}"
a8223e
+    return -1
a8223e
+}
a8223e
+
a8223e
+if { [prepare_for_testing ${testfile}.exp ${executable} ${srcfile} \
a8223e
+	[ list debug shlib_load "additional_flags=-Wno-deprecated-declarations" ]] } {
a8223e
+    untested "Could not compile ${executable}"
a8223e
+    return -1
a8223e
+}
a8223e
+
a8223e
+set supported 0
a8223e
+gdb_test_multiple "run" "initial trial run" {
a8223e
+    -re -wrap "exited normally.*" {
a8223e
+	set supported 1
a8223e
+	pass $gdb_test_name
a8223e
+    }
a8223e
+    -re -wrap "exited with code.*" {
a8223e
+	untested "failed at $gdb_test_name"
a8223e
+    }
a8223e
+}
a8223e
+
a8223e
+if { $supported == 0 } {
a8223e
+    return -1
a8223e
+}
a8223e
+
a8223e
+proc do_test { has_libfoo has_libbar } {
a8223e
+  global hex binfile_lib2 binfile_lib1 gdb_prompt
a8223e
+  set libbar_match "[string_to_regexp $binfile_lib2]"
a8223e
+  set libfoo_match "[string_to_regexp $binfile_lib1]"
a8223e
+
a8223e
+  gdb_test_multiple "info shared" "info shared" {
a8223e
+    -re ".*$libfoo_match\r\n.*$libbar_match\(\r\n.*Shared library is missing\)?.*\r\n${gdb_prompt} $" {
a8223e
+      if { $has_libfoo && $has_libbar } {
a8223e
+	pass "matched libfoo and libbar"
a8223e
+      } else {
a8223e
+	fail "matched libfoo and libbar (has_libfoo = $has_libfoo, has_libbar = $has_libbar)"
a8223e
+      }
a8223e
+    }
a8223e
+    -re ".*$libfoo_match\(\r\n.*Shared library is missing\)?.*\r\n${gdb_prompt} $" {
a8223e
+      if { $has_libfoo && !$has_libbar } {
a8223e
+	pass "matched libfoo"
a8223e
+      } else {
a8223e
+	fail "matched libfoo (has_libfoo = $has_libfoo, has_libbar = $has_libbar)"
a8223e
+      }
a8223e
+    }
a8223e
+    -re ".*$libbar_match\(\r\n.*Shared library is missing\)?.*\r\n${gdb_prompt} $" {
a8223e
+      if { $has_libbar && !$has_libfoo } {
a8223e
+	pass "matched libbar"
a8223e
+      } else {
a8223e
+	fail "matched libbar (has_libfoo = $has_libfoo, has_libbar = $has_libbar)"
a8223e
+      }
a8223e
+    }
a8223e
+    "\r\n${gdb_prompt} $" {
a8223e
+      if { !$has_libfoo && !$has_libbar } {
a8223e
+	pass "did not match libfoo nor libbar"
a8223e
+      } else {
a8223e
+	fail "did not match libfoo nor libbar (has_libfoo = $has_libfoo, has_libbar = $has_libbar)"
a8223e
+      }
a8223e
+    }
a8223e
+  }
a8223e
+}
a8223e
+
a8223e
+proc test_stop_on_solib_events { } {
a8223e
+  set pass 0
a8223e
+  # This variable holds the information about whether libfoo and
a8223e
+  # libbar (respectively) are expected in the "info shared" output.
a8223e
+  set solib_event_order { { 0 0 } { 0 0   } { 0 0   } { 0 1 } \
a8223e
+			  { 0 1 } { 0 0   } { 0 0   } { 0 1 } \
a8223e
+			  { 0 1 } { 0 0   } { 0 0   } { 0 1 } \
a8223e
+			  { 0 1 } { 0 0   } { 0 0 1 } { 1 1 } \
a8223e
+			  { 1 1 } { 1 0   } { 1 0   } { 1 1 } \
a8223e
+			  { 1 1 } { 1 0 1 } { 1 0   } { 1 0 } }
a8223e
+
a8223e
+  with_test_prefix "stop-on-solib-events" {
a8223e
+    gdb_test_no_output "set stop-on-solib-events 1" "setting stop-on-solib-events"
a8223e
+
a8223e
+    gdb_run_cmd
a8223e
+    gdb_test "" "Wait for first prompt"
a8223e
+    foreach l $solib_event_order {
a8223e
+      incr pass
a8223e
+      with_test_prefix "pass #$pass" {
a8223e
+	set should_be_corrupted [expr 0+0[lindex $l 2]]
a8223e
+	do_test [lindex $l 0] [lindex $l 1]
a8223e
+	set test "continue"
a8223e
+	global gdb_prompt
a8223e
+	gdb_test_multiple $test $test {
a8223e
+	  -re "\r\nwarning: Corrupted shared library list:.*\r\nStopped due to shared library event.*\r\n$gdb_prompt $" {
a8223e
+	    set corrupted 1
a8223e
+	    pass $test
a8223e
+	  }
a8223e
+	  -re "\r\nStopped due to shared library event.*\r\n$gdb_prompt $" {
a8223e
+	    set corrupted 0
a8223e
+	    pass $test
a8223e
+	  }
a8223e
+	}
a8223e
+	set test "corrupted=$corrupted but should_be_corrupted=$should_be_corrupted"
a8223e
+	if {$corrupted == $should_be_corrupted} {
a8223e
+	  pass $test
a8223e
+	} else {
a8223e
+	  fail $test
a8223e
+	}
a8223e
+      }
a8223e
+    }
a8223e
+    # In the last pass we do not expect to see libfoo or libbar.
a8223e
+    incr pass
a8223e
+    with_test_prefix "pass #$pass" {
a8223e
+      do_test 0 0
a8223e
+    }
a8223e
+  }
a8223e
+}
a8223e
+
a8223e
+test_stop_on_solib_events