1#!/bin/sh
2# Trigger a bug that would cause 'sort' to reference stale thread stack memory.
3
4# Copyright (C) 2010-2023 Free Software Foundation, Inc.
5
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
19# written by Jim Meyering and Paul Eggert
20
21. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
22print_ver_ sort
23
24very_expensive_
25require_valgrind_
26
27grep '^#define HAVE_PTHREAD_T 1' "$CONFIG_HEADER" > /dev/null ||
28  skip_ 'requires pthreads'
29
30# gensort output seems to trigger the failure more often,
31# so prefer gensort if it is available.
32(gensort -a 10000 in) 2>/dev/null ||
33  seq -f %-98f 10000 | shuf > in ||
34  framework_failure_
35
36# On Fedora-17-beta (valgrind-3.7.0-2.fc17.x86_64), this evokes two complaints
37# that a conditional jump or move depends on uninitialized values,
38# each originating from _dl_start.
39valgrind --quiet --error-exitcode=3 sort --version > /dev/null ||
40  framework_failure_ 'valgrind fails for trivial sort invocation'
41
42# With the bug, 'sort' would fail under valgrind about half the time,
43# on some circa-2010 multicore Linux platforms.  Run the test 100 times
44# so that the probability of missing the bug should be about 1 in
45# 2**100 on these hosts.
46for i in $(seq 100); do
47  valgrind --quiet --error-exitcode=3 \
48      sort -S 100K --parallel=2 in > /dev/null ||
49    { fail=$?; echo iteration $i failed; Exit $fail; }
50done
51
52Exit $fail
53