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

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