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

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