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

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