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

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