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

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