summaryrefslogtreecommitdiff
path: root/gdb-rhbz1156192-recursive-dlopen-test.patch
blob: 96b93ae585f704aa796ab01a91a838d341519a9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
From: Fedora GDB patches <invalid@email.com>
Date: Fri, 27 Oct 2017 21:07:50 +0200
Subject: gdb-rhbz1156192-recursive-dlopen-test.patch

;; Testcase for '[SAP] Recursive dlopen causes SAP HANA installer to
;; crash.' (RH BZ 1156192).
;;=fedoratest

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