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

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