1# Copyright 2017-2024 Free Software Foundation, Inc.
2
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 3 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16# This file is part of the gdb testsuite.
17
18# Test that GDB tolerates being started with libSegFault.so preloaded
19# with LD_PRELOAD, and that GDB warns about a custom SIGSEGV custom
20# handler.  See PR gdb/18653
21# <https://sourceware.org/bugzilla/show_bug.cgi?id=18653#c7>.
22
23# We cannot expect remote hosts to see environment variables set on
24# the local machine.
25require {!is_remote host}
26
27# Spawn GDB with LIB preloaded with LD_PRELOAD.  CMDLINE_OPTS are
28# command line options passed to GDB.
29
30proc gdb_spawn_with_ld_preload {lib cmdline_opts} {
31    global env
32
33    save_vars { env(LD_PRELOAD) env(ASAN_OPTIONS)} {
34	if { ![info exists env(LD_PRELOAD) ]
35	     || $env(LD_PRELOAD) == "" } {
36	    set env(LD_PRELOAD) "$lib"
37	} else {
38	    append env(LD_PRELOAD) ":$lib"
39	}
40
41	# Prevent address sanitizer error:
42	# ASan runtime does not come first in initial library list; you should
43	# either link runtime to your application or manually preload it with
44	# LD_PRELOAD.
45	set_sanitizer_default ASAN_OPTIONS verify_asan_link_order 0
46
47	gdb_spawn_with_cmdline_opts $cmdline_opts
48    }
49}
50
51proc test_libsegfault {} {
52    global gdb_prompt
53
54    set libsegfault "libSegFault.so"
55
56    # When started normally, if libSegFault.so is preloaded, GDB
57    # should warn about not being able to propagate the signal
58    # disposition of SIGSEGV.
59    gdb_exit
60    gdb_spawn_with_ld_preload $libsegfault ""
61
62    set test "gdb emits custom handler warning"
63    gdb_test_multiple "" $test {
64	-re "cannot be preloaded.*\r\n$gdb_prompt $" {
65	    # Glibc 2.22 outputs:
66	    # ERROR: ld.so: object 'libSegFault.so' from LD_PRELOAD cannot be preloaded (cannot open shared object file): ignored.
67	    untested "cannot preload libSegFault.so"
68	    return
69	}
70	-re "Found custom handler.*won't be propagated.*\r\n$gdb_prompt $" {
71	    pass $test
72	}
73    }
74
75    # "-q" should disable the warning, though.
76    gdb_exit
77    gdb_spawn_with_ld_preload $libsegfault "-q"
78
79    set test "quiet suppresses custom handler warning"
80    gdb_test_multiple "" $test {
81	-re "^$gdb_prompt $" {
82	    pass $test
83	}
84    }
85}
86
87save_vars { ::INTERNAL_GDBFLAGS } {
88    set ::INTERNAL_GDBFLAGS [string map {"-q" ""} $::INTERNAL_GDBFLAGS]
89    test_libsegfault
90}
91