Skip to content

Commit 2e30d77

Browse files
committed
Suppress warning about stack_base_ptr with late-model GCC.
GCC 12 complains that set_stack_base is storing the address of a local variable in a long-lived pointer. This is an entirely reasonable warning (indeed, it just helped us find a bug); but that behavior is intentional here. We can work around it by using __builtin_frame_address(0) instead of a specific local variable; that produces an address a dozen or so bytes different, in my testing, but we don't care about such a small difference. Maybe someday a compiler lacking that function will start to issue a similar warning, but we'll worry about that when it happens. Patch by me, per a suggestion from Andres Freund. Back-patch to v12, which is as far back as the patch will go without some pain. (Recently-established project policy would permit a back-patch as far as 9.2, but I'm disinclined to expend the work until GCC 12 is much more widespread.) Discussion: https://postgr.es/m/3773792.1645141467@sss.pgh.pa.us
1 parent a9e186d commit 2e30d77

File tree

8 files changed

+86
-8
lines changed

8 files changed

+86
-8
lines changed

config/c-compiler.m4

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,28 @@ fi])# PGAC_CHECK_BUILTIN_FUNC
381381

382382

383383

384+
# PGAC_CHECK_BUILTIN_FUNC_PTR
385+
# -----------------------
386+
# Like PGAC_CHECK_BUILTIN_FUNC, except that the function is assumed to
387+
# return a pointer type, and the argument(s) should be given literally.
388+
# This handles some cases that PGAC_CHECK_BUILTIN_FUNC doesn't.
389+
AC_DEFUN([PGAC_CHECK_BUILTIN_FUNC_PTR],
390+
[AC_CACHE_CHECK(for $1, pgac_cv$1,
391+
[AC_LINK_IFELSE([AC_LANG_PROGRAM([
392+
void *
393+
call$1(void)
394+
{
395+
return $1($2);
396+
}], [])],
397+
[pgac_cv$1=yes],
398+
[pgac_cv$1=no])])
399+
if test x"${pgac_cv$1}" = xyes ; then
400+
AC_DEFINE_UNQUOTED(AS_TR_CPP([HAVE$1]), 1,
401+
[Define to 1 if your compiler understands $1.])
402+
fi])# PGAC_CHECK_BUILTIN_FUNC_PTR
403+
404+
405+
384406
# PGAC_PROG_VARCC_VARFLAGS_OPT
385407
# ----------------------------
386408
# Given a compiler, variable name and a string, check if the compiler

configure

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15944,6 +15944,46 @@ cat >>confdefs.h <<_ACEOF
1594415944
#define HAVE__BUILTIN_POPCOUNT 1
1594515945
_ACEOF
1594615946

15947+
fi
15948+
# __builtin_frame_address may draw a diagnostic for non-constant argument,
15949+
# so it needs a different test function.
15950+
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for __builtin_frame_address" >&5
15951+
$as_echo_n "checking for __builtin_frame_address... " >&6; }
15952+
if ${pgac_cv__builtin_frame_address+:} false; then :
15953+
$as_echo_n "(cached) " >&6
15954+
else
15955+
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
15956+
/* end confdefs.h. */
15957+
15958+
void *
15959+
call__builtin_frame_address(void)
15960+
{
15961+
return __builtin_frame_address(0);
15962+
}
15963+
int
15964+
main ()
15965+
{
15966+
15967+
;
15968+
return 0;
15969+
}
15970+
_ACEOF
15971+
if ac_fn_c_try_link "$LINENO"; then :
15972+
pgac_cv__builtin_frame_address=yes
15973+
else
15974+
pgac_cv__builtin_frame_address=no
15975+
fi
15976+
rm -f core conftest.err conftest.$ac_objext \
15977+
conftest$ac_exeext conftest.$ac_ext
15978+
fi
15979+
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $pgac_cv__builtin_frame_address" >&5
15980+
$as_echo "$pgac_cv__builtin_frame_address" >&6; }
15981+
if test x"${pgac_cv__builtin_frame_address}" = xyes ; then
15982+
15983+
cat >>confdefs.h <<_ACEOF
15984+
#define HAVE__BUILTIN_FRAME_ADDRESS 1
15985+
_ACEOF
15986+
1594715987
fi
1594815988

1594915989
# We require 64-bit fseeko() to be available, but run this check anyway

configure.ac

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,6 +1769,9 @@ PGAC_CHECK_BUILTIN_FUNC([__builtin_bswap64], [long int x])
17691769
PGAC_CHECK_BUILTIN_FUNC([__builtin_clz], [unsigned int x])
17701770
PGAC_CHECK_BUILTIN_FUNC([__builtin_ctz], [unsigned int x])
17711771
PGAC_CHECK_BUILTIN_FUNC([__builtin_popcount], [unsigned int x])
1772+
# __builtin_frame_address may draw a diagnostic for non-constant argument,
1773+
# so it needs a different test function.
1774+
PGAC_CHECK_BUILTIN_FUNC_PTR([__builtin_frame_address], [0])
17721775

17731776
# We require 64-bit fseeko() to be available, but run this check anyway
17741777
# in case it finds that _LARGEFILE_SOURCE has to be #define'd for that.

src/backend/postmaster/postmaster.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,7 @@ PostmasterMain(int argc, char *argv[])
10331033
/*
10341034
* Set reference point for stack-depth checking.
10351035
*/
1036-
set_stack_base();
1036+
(void) set_stack_base();
10371037

10381038
/*
10391039
* Initialize pipe (or process handle on Windows) that allows children to

src/backend/tcop/postgres.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3415,7 +3415,9 @@ ia64_get_bsp(void)
34153415
pg_stack_base_t
34163416
set_stack_base(void)
34173417
{
3418+
#ifndef HAVE__BUILTIN_FRAME_ADDRESS
34183419
char stack_base;
3420+
#endif
34193421
pg_stack_base_t old;
34203422

34213423
#if defined(__ia64__) || defined(__ia64)
@@ -3425,8 +3427,16 @@ set_stack_base(void)
34253427
old = stack_base_ptr;
34263428
#endif
34273429

3428-
/* Set up reference point for stack depth checking */
3430+
/*
3431+
* Set up reference point for stack depth checking. On recent gcc we use
3432+
* __builtin_frame_address() to avoid a warning about storing a local
3433+
* variable's address in a long-lived variable.
3434+
*/
3435+
#ifdef HAVE__BUILTIN_FRAME_ADDRESS
3436+
stack_base_ptr = __builtin_frame_address(0);
3437+
#else
34293438
stack_base_ptr = &stack_base;
3439+
#endif
34303440
#if defined(__ia64__) || defined(__ia64)
34313441
register_stack_base_ptr = ia64_get_bsp();
34323442
#endif

src/backend/utils/init/miscinit.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,12 @@ InitPostmasterChild(void)
9595
IsUnderPostmaster = true; /* we are a postmaster subprocess now */
9696

9797
/*
98-
* Set reference point for stack-depth checking. We re-do that even in the
99-
* !EXEC_BACKEND case, because there are some edge cases where processes
100-
* are started with an alternative stack (e.g. starting bgworkers when
101-
* running postgres using the rr debugger, as bgworkers are launched from
102-
* signal handlers).
98+
* Set reference point for stack-depth checking. This might seem
99+
* redundant in !EXEC_BACKEND builds; but it's not because the postmaster
100+
* launches its children from signal handlers, so we might be running on
101+
* an alternative stack.
103102
*/
104-
set_stack_base();
103+
(void) set_stack_base();
105104

106105
InitProcessGlobals();
107106

src/include/pg_config.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,9 @@
745745
/* Define to 1 if your compiler understands __builtin_ctz. */
746746
#undef HAVE__BUILTIN_CTZ
747747

748+
/* Define to 1 if your compiler understands __builtin_frame_address. */
749+
#undef HAVE__BUILTIN_FRAME_ADDRESS
750+
748751
/* Define to 1 if your compiler understands __builtin_$op_overflow. */
749752
#undef HAVE__BUILTIN_OP_OVERFLOW
750753

src/tools/msvc/Solution.pm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ sub GenerateFiles
441441
HAVE__BUILTIN_CLZ => undef,
442442
HAVE__BUILTIN_CONSTANT_P => undef,
443443
HAVE__BUILTIN_CTZ => undef,
444+
HAVE__BUILTIN_FRAME_ADDRESS => undef,
444445
HAVE__BUILTIN_OP_OVERFLOW => undef,
445446
HAVE__BUILTIN_POPCOUNT => undef,
446447
HAVE__BUILTIN_TYPES_COMPATIBLE_P => undef,

0 commit comments

Comments
 (0)