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

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