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

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