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

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