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

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