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

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