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

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