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

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