Skip to content

Commit 46b738f

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 0dc0284 commit 46b738f

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
@@ -15509,6 +15509,46 @@ cat >>confdefs.h <<_ACEOF
1550915509
#define HAVE__BUILTIN_POPCOUNT 1
1551015510
_ACEOF
1551115511

15512+
fi
15513+
# __builtin_frame_address may draw a diagnostic for non-constant argument,
15514+
# so it needs a different test function.
15515+
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for __builtin_frame_address" >&5
15516+
$as_echo_n "checking for __builtin_frame_address... " >&6; }
15517+
if ${pgac_cv__builtin_frame_address+:} false; then :
15518+
$as_echo_n "(cached) " >&6
15519+
else
15520+
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
15521+
/* end confdefs.h. */
15522+
15523+
void *
15524+
call__builtin_frame_address(void)
15525+
{
15526+
return __builtin_frame_address(0);
15527+
}
15528+
int
15529+
main ()
15530+
{
15531+
15532+
;
15533+
return 0;
15534+
}
15535+
_ACEOF
15536+
if ac_fn_c_try_link "$LINENO"; then :
15537+
pgac_cv__builtin_frame_address=yes
15538+
else
15539+
pgac_cv__builtin_frame_address=no
15540+
fi
15541+
rm -f core conftest.err conftest.$ac_objext \
15542+
conftest$ac_exeext conftest.$ac_ext
15543+
fi
15544+
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $pgac_cv__builtin_frame_address" >&5
15545+
$as_echo "$pgac_cv__builtin_frame_address" >&6; }
15546+
if test x"${pgac_cv__builtin_frame_address}" = xyes ; then
15547+
15548+
cat >>confdefs.h <<_ACEOF
15549+
#define HAVE__BUILTIN_FRAME_ADDRESS 1
15550+
_ACEOF
15551+
1551215552
fi
1551315553

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

configure.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,6 +1713,9 @@ PGAC_CHECK_BUILTIN_FUNC([__builtin_bswap64], [long int x])
17131713
PGAC_CHECK_BUILTIN_FUNC([__builtin_clz], [unsigned int x])
17141714
PGAC_CHECK_BUILTIN_FUNC([__builtin_ctz], [unsigned int x])
17151715
PGAC_CHECK_BUILTIN_FUNC([__builtin_popcount], [unsigned int x])
1716+
# __builtin_frame_address may draw a diagnostic for non-constant argument,
1717+
# so it needs a different test function.
1718+
PGAC_CHECK_BUILTIN_FUNC_PTR([__builtin_frame_address], [0])
17161719

17171720
# We require 64-bit fseeko() to be available, but run this check anyway
17181721
# 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
@@ -1028,7 +1028,7 @@ PostmasterMain(int argc, char *argv[])
10281028
/*
10291029
* Set reference point for stack-depth checking.
10301030
*/
1031-
set_stack_base();
1031+
(void) set_stack_base();
10321032

10331033
/*
10341034
* 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
@@ -3280,7 +3280,9 @@ ia64_get_bsp(void)
32803280
pg_stack_base_t
32813281
set_stack_base(void)
32823282
{
3283+
#ifndef HAVE__BUILTIN_FRAME_ADDRESS
32833284
char stack_base;
3285+
#endif
32843286
pg_stack_base_t old;
32853287

32863288
#if defined(__ia64__) || defined(__ia64)
@@ -3290,8 +3292,16 @@ set_stack_base(void)
32903292
old = stack_base_ptr;
32913293
#endif
32923294

3293-
/* Set up reference point for stack depth checking */
3295+
/*
3296+
* Set up reference point for stack depth checking. On recent gcc we use
3297+
* __builtin_frame_address() to avoid a warning about storing a local
3298+
* variable's address in a long-lived variable.
3299+
*/
3300+
#ifdef HAVE__BUILTIN_FRAME_ADDRESS
3301+
stack_base_ptr = __builtin_frame_address(0);
3302+
#else
32943303
stack_base_ptr = &stack_base;
3304+
#endif
32953305
#if defined(__ia64__) || defined(__ia64)
32963306
register_stack_base_ptr = ia64_get_bsp();
32973307
#endif

src/backend/utils/init/miscinit.c

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

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

104103
InitProcessGlobals();
105104

src/include/pg_config.h.in

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

710+
/* Define to 1 if your compiler understands __builtin_frame_address. */
711+
#undef HAVE__BUILTIN_FRAME_ADDRESS
712+
710713
/* Define to 1 if your compiler understands __builtin_$op_overflow. */
711714
#undef HAVE__BUILTIN_OP_OVERFLOW
712715

src/tools/msvc/Solution.pm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ sub GenerateFiles
419419
HAVE__BUILTIN_CLZ => undef,
420420
HAVE__BUILTIN_CONSTANT_P => undef,
421421
HAVE__BUILTIN_CTZ => undef,
422+
HAVE__BUILTIN_FRAME_ADDRESS => undef,
422423
HAVE__BUILTIN_OP_OVERFLOW => undef,
423424
HAVE__BUILTIN_POPCOUNT => undef,
424425
HAVE__BUILTIN_TYPES_COMPATIBLE_P => undef,

0 commit comments

Comments
 (0)