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

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