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

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