glibc/gmon/tst-mcount-overflow.c
Simon Kissane 9f81b8fa65 gmon: improve mcount overflow handling [BZ# 27576]
When mcount overflows, no gmon.out file is generated, but no message is printed
to the user, leaving the user with no idea why, and thinking maybe there is
some bug - which is how BZ 27576 ended up being logged. Print a message to
stderr in this case so the user knows what is going on.

As a comment in sys/gmon.h acknowledges, the hardcoded MAXARCS value is too
small for some large applications, including the test case in that BZ. Rather
than increase it, add tunables to enable MINARCS and MAXARCS to be overridden
at runtime (glibc.gmon.minarcs and glibc.gmon.maxarcs). So if a user gets the
mcount overflow error, they can try increasing maxarcs (they might need to
increase minarcs too if the heuristic is wrong in their case.)

Note setting minarcs/maxarcs too large can cause monstartup to fail with an
out of memory error. If you set them large enough, it can cause an integer
overflow in calculating the buffer size. I haven't done anything to defend
against that - it would not generally be a security vulnerability, since these
tunables will be ignored in suid/sgid programs (due to the SXID_ERASE default),
and if you can set GLIBC_TUNABLES in the environment of a process, you can take
it over anyway (LD_PRELOAD, LD_LIBRARY_PATH, etc). I thought about modifying
the code of monstartup to defend against integer overflows, but doing so is
complicated, and I realise the existing code is susceptible to them even prior
to this change (e.g. try passing a pathologically large highpc argument to
monstartup), so I decided just to leave that possibility in-place.

Add a test case which demonstrates mcount overflow and the tunables.

Document the new tunables in the manual.

Signed-off-by: Simon Kissane <skissane@gmail.com>
Reviewed-by: DJ Delorie <dj@redhat.com>
(cherry picked from commit 31be941e43)
2023-04-28 16:05:29 +02:00

73 lines
2.1 KiB
C

/* Test program to trigger mcount overflow in profiling collection.
Copyright (C) 2017-2023 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
/* Program with sufficiently complex, yet pointless, call graph
that it will trigger an mcount overflow, when you set the
minarcs/maxarcs tunables to very low values. */
#define PREVENT_TAIL_CALL asm volatile ("")
/* Calls REP(n) macro 16 times, for n=0..15.
* You need to define REP(n) before using this.
*/
#define REPS \
REP(0) REP(1) REP(2) REP(3) REP(4) REP(5) REP(6) REP(7) \
REP(8) REP(9) REP(10) REP(11) REP(12) REP(13) REP(14) REP(15)
/* Defines 16 leaf functions named f1_0 to f1_15 */
#define REP(n) \
__attribute__ ((noinline, noclone, weak)) void f1_##n (void) {};
REPS
#undef REP
/* Calls all 16 leaf functions f1_* in succession */
__attribute__ ((noinline, noclone, weak)) void
f2 (void)
{
# define REP(n) f1_##n();
REPS
# undef REP
PREVENT_TAIL_CALL;
}
/* Defines 16 functions named f2_0 to f2_15, which all just call f2 */
#define REP(n) \
__attribute__ ((noinline, noclone, weak)) void \
f2_##n (void) { f2(); PREVENT_TAIL_CALL; };
REPS
#undef REP
__attribute__ ((noinline, noclone, weak)) void
f3 (int count)
{
for (int i = 0; i < count; ++i)
{
/* Calls f1_0(), f2_0(), f1_1(), f2_1(), f3_0(), etc */
# define REP(n) f1_##n(); f2_##n();
REPS
# undef REP
}
}
int
main (void)
{
f3 (1000);
return 0;
}