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

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