2007-10-26 23:59:43 +02:00
|
|
|
#!/bin/sh
|
|
|
|
|
# Detect printf(3) failure even when it doesn't set stream error indicator
|
|
|
|
|
|
2025-01-01 09:14:56 +00:00
|
|
|
# Copyright (C) 2007-2025 Free Software Foundation, Inc.
|
2007-10-26 23:59:43 +02:00
|
|
|
|
|
|
|
|
# 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
|
2017-09-19 01:13:23 -07:00
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2007-10-26 23:59:43 +02:00
|
|
|
|
2008-05-12 08:49:58 +02:00
|
|
|
prog=printf
|
2007-10-26 23:59:43 +02:00
|
|
|
|
2012-09-02 21:55:12 +02:00
|
|
|
. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
|
2010-11-17 21:42:13 +01:00
|
|
|
print_ver_ printf
|
2007-10-26 23:59:43 +02:00
|
|
|
|
maint: use adaptive approach for `ulimit -v` based tests
When configured with either 'symlinks' or 'shebangs' as value for
the --enable-single-binary option, tests based on `ulimit -v` are
skipped. The reason is that the multicall 'coreutils' binary requires
much more memory due to shared libraries being loaded, and the size of
the 'date' binary (~290KiB) compared to the multicall binary (~5MiB),
of course. Finally, in the case of 'shebangs', the starting shell
requires more memory, too
Instead of using hard-coded values for the memory limit, use an
adaptive approach: first determine the amount of memory for a similar,
yet more trivial invocation of the command, and then do the real test
run using that limit (plus some buffer in some cases).
* init.cfg (require_ulimit_v_): Remove function.
(get_min_ulimit_v_): Add function to determine the minimum memory limit
required for a given command in an adaptive way.
* cfg.mk (sc_prohibit_test_ulimit_without_require_): Change the name
of the above function in the syntax-check rule.
* tests/cp/link-heap.sh: Use the above function to determine the
minimum memory required to run a command simpler than in the real test
run. Use that limit plus a buffer there. While at it, change to list
of commands in the subshell to fail also if the beginning `ulimit -v`
fails.
* tests/dd/no-allocate.sh: Likewise.
* tests/misc/csplit-heap.sh: Likewise.
* tests/misc/cut-huge-range.sh: Likewise.
* tests/misc/head-c.sh: Likewise.
* tests/misc/printf-surprise.sh: Likewise.
* tests/split/line-bytes.sh: Likewise.
* tests/rm/many-dir-entries-vs-OOM.sh: Likewise - doing it separately
for each program under test.
2015-09-22 23:23:26 +02:00
|
|
|
vm=$(get_min_ulimit_v_ env $prog %20f 0) \
|
2024-01-17 23:49:52 +00:00
|
|
|
|| skip_ 'shell lacks ulimit, or ASAN enabled'
|
2007-10-26 23:59:43 +02:00
|
|
|
|
2007-11-01 12:06:11 +01:00
|
|
|
# Up to coreutils-6.9, "printf %.Nf 0" would encounter an ENOMEM internal
|
|
|
|
|
# error from glibc's printf(3) function whenever N was large relative to
|
|
|
|
|
# the size of available memory. As of Oct 2007, that internal stream-
|
|
|
|
|
# related failure was not reflected (for any libc I know of) in the usual
|
|
|
|
|
# stream error indicator that is tested by ferror. The result was that
|
|
|
|
|
# while the printf command obviously failed (generated no output),
|
|
|
|
|
# it mistakenly exited successfully (exit status of 0).
|
|
|
|
|
|
|
|
|
|
# Testing it is tricky, because there is so much variance
|
|
|
|
|
# in quality for this corner of printf(3) implementations.
|
|
|
|
|
# Most implementations do attempt to allocate N bytes of storage.
|
2008-05-16 22:55:27 +02:00
|
|
|
# Using the maximum value for N (2^31-1) causes glibc-2.7 to try to
|
2007-11-01 12:06:11 +01:00
|
|
|
# allocate almost 2^64 bytes, while freeBSD 6.1's implementation
|
|
|
|
|
# correctly outputs almost 2GB worth of 0's, which takes too long.
|
|
|
|
|
# We want to test implementations that allocate N bytes, but without
|
|
|
|
|
# triggering the above extremes.
|
|
|
|
|
|
2008-05-16 22:55:27 +02:00
|
|
|
# Some other versions of glibc-2.7 have a snprintf function that segfaults
|
|
|
|
|
# when an internal (technically unnecessary!) memory allocation fails.
|
|
|
|
|
|
2007-11-01 12:06:11 +01:00
|
|
|
# The compromise is to limit virtual memory to something reasonable,
|
|
|
|
|
# and to make an N-byte-allocating-printf require more than that, thus
|
|
|
|
|
# triggering the printf(3) misbehavior -- which, btw, is required by ISO C99.
|
|
|
|
|
|
2011-05-10 08:14:16 +01:00
|
|
|
mkfifo_or_skip_ fifo
|
2019-01-20 00:13:15 -08:00
|
|
|
trap_sigpipe_or_skip_
|
2017-08-12 23:53:33 -07:00
|
|
|
|
2008-11-16 09:53:48 +01:00
|
|
|
# Disable MALLOC_PERTURB_, to avoid triggering this bug
|
2017-01-14 22:57:51 -08:00
|
|
|
# https://bugs.debian.org/481543#77
|
2008-11-16 09:53:48 +01:00
|
|
|
export MALLOC_PERTURB_=0
|
|
|
|
|
|
2015-05-01 05:26:38 +01:00
|
|
|
# Terminate any background process
|
|
|
|
|
cleanup_() { kill $pid 2>/dev/null && wait $pid; }
|
|
|
|
|
|
|
|
|
|
head -c 10 fifo > out & pid=$!
|
2012-08-05 16:10:09 +02:00
|
|
|
|
2015-10-08 03:41:07 +01:00
|
|
|
# Trigger large mem allocation failure
|
2025-01-15 01:36:13 +00:00
|
|
|
( trap '' PIPE && ulimit -v $(($vm+4000)) &&
|
|
|
|
|
env $prog %20000000f 0 2>err-msg > fifo )
|
2008-05-16 22:55:27 +02:00
|
|
|
exit=$?
|
2007-11-01 12:06:11 +01:00
|
|
|
|
|
|
|
|
# Map this longer, and rarer, diagnostic to the common one.
|
2017-08-12 23:53:33 -07:00
|
|
|
# printf: cannot perform formatted output: Cannot allocate memory"
|
2008-05-16 22:55:27 +02:00
|
|
|
sed 's/cannot perform .*/write error/' err-msg > k && mv k err-msg
|
2015-01-10 21:12:38 +00:00
|
|
|
err_msg=$(tr '\n' : < err-msg)
|
2008-09-07 20:50:49 +02:00
|
|
|
|
|
|
|
|
# By some bug, on Solaris 11 (5.11 snv_86), err_msg ends up
|
|
|
|
|
# containing '1> fifo:printf: write error:'. Recognize that, too.
|
|
|
|
|
|
2008-05-16 22:55:27 +02:00
|
|
|
case $err_msg in
|
2011-09-01 14:45:51 +02:00
|
|
|
"$prog: write error:"*) diagnostic=y ;;
|
2008-09-07 20:50:49 +02:00
|
|
|
"1> fifo:$prog: write error:") diagnostic=y ;;
|
2007-11-01 12:06:11 +01:00
|
|
|
'') diagnostic=n ;;
|
|
|
|
|
*) diagnostic=unexpected ;;
|
|
|
|
|
esac
|
|
|
|
|
n_out=$(wc -c < out)
|
|
|
|
|
|
2008-05-16 22:55:27 +02:00
|
|
|
case $n_out:$diagnostic:$exit in
|
|
|
|
|
10:n:0) ;; # ok, succeeds w/no diagnostic: FreeBSD 6.1
|
2017-08-12 23:53:33 -07:00
|
|
|
10:y:1) ;; # ok, fails with EPIPE diagnostic: musl libc
|
2008-05-16 22:55:27 +02:00
|
|
|
0:y:1) ;; # ok, glibc-2.8 and newer, when printf(3) fails with ENOMEM
|
|
|
|
|
|
2008-11-16 09:53:48 +01:00
|
|
|
# With MALLOC_PERTURB_=0, this no longer happens.
|
|
|
|
|
# *:139) # segfault; known bug at least in debian unstable's libc6 2.7-11
|
|
|
|
|
# echo 1>&2 "$0: bug in snprintf causes low-mem use of printf to segfault"
|
|
|
|
|
# fail=77;;
|
2007-11-01 12:06:11 +01:00
|
|
|
|
|
|
|
|
# 10:y) ;; # Fail: doesn't happen: nobody succeeds with a diagnostic
|
|
|
|
|
# 0:n) ;; # Fail pre-patch: no output, no diag
|
2008-05-16 22:55:27 +02:00
|
|
|
*) fail=1;;
|
2007-11-01 12:06:11 +01:00
|
|
|
esac
|
2007-10-26 23:59:43 +02:00
|
|
|
|
2008-09-07 10:31:27 +02:00
|
|
|
Exit $fail
|