Suppress warning about stack_base_ptr with late-model GCC.
authorTom Lane
Fri, 18 Feb 2022 03:45:34 +0000 (22:45 -0500)
committerTom Lane
Fri, 18 Feb 2022 03:45:34 +0000 (22:45 -0500)
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

config/c-compiler.m4
configure
configure.in
src/backend/postmaster/postmaster.c
src/backend/tcop/postgres.c
src/backend/utils/init/miscinit.c
src/include/pg_config.h.in
src/tools/msvc/Solution.pm

index 780e906eccd3a1513343959e3acc975cb582c2b4..d3562d6feee52f7e95db559c661baea182bb0b2a 100644 (file)
@@ -381,6 +381,28 @@ fi])# PGAC_CHECK_BUILTIN_FUNC
 
 
 
+# PGAC_CHECK_BUILTIN_FUNC_PTR
+# -----------------------
+# Like PGAC_CHECK_BUILTIN_FUNC, except that the function is assumed to
+# return a pointer type, and the argument(s) should be given literally.
+# This handles some cases that PGAC_CHECK_BUILTIN_FUNC doesn't.
+AC_DEFUN([PGAC_CHECK_BUILTIN_FUNC_PTR],
+[AC_CACHE_CHECK(for $1, pgac_cv$1,
+[AC_LINK_IFELSE([AC_LANG_PROGRAM([
+void *
+call$1(void)
+{
+    return $1($2);
+}], [])],
+[pgac_cv$1=yes],
+[pgac_cv$1=no])])
+if test x"${pgac_cv$1}" = xyes ; then
+AC_DEFINE_UNQUOTED(AS_TR_CPP([HAVE$1]), 1,
+                   [Define to 1 if your compiler understands $1.])
+fi])# PGAC_CHECK_BUILTIN_FUNC_PTR
+
+
+
 # PGAC_PROG_VARCC_VARFLAGS_OPT
 # ----------------------------
 # Given a compiler, variable name and a string, check if the compiler
index 8568ffccf9625bb72640fc8d698dd0b7afe94324..1c5d4db19d617f4583e4130650a5be93e686d0f0 100755 (executable)
--- a/configure
+++ b/configure
@@ -15509,6 +15509,46 @@ cat >>confdefs.h <<_ACEOF
 #define HAVE__BUILTIN_POPCOUNT 1
 _ACEOF
 
+fi
+# __builtin_frame_address may draw a diagnostic for non-constant argument,
+# so it needs a different test function.
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for __builtin_frame_address" >&5
+$as_echo_n "checking for __builtin_frame_address... " >&6; }
+if ${pgac_cv__builtin_frame_address+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+void *
+call__builtin_frame_address(void)
+{
+    return __builtin_frame_address(0);
+}
+int
+main ()
+{
+
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_link "$LINENO"; then :
+  pgac_cv__builtin_frame_address=yes
+else
+  pgac_cv__builtin_frame_address=no
+fi
+rm -f core conftest.err conftest.$ac_objext \
+    conftest$ac_exeext conftest.$ac_ext
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $pgac_cv__builtin_frame_address" >&5
+$as_echo "$pgac_cv__builtin_frame_address" >&6; }
+if test x"${pgac_cv__builtin_frame_address}" = xyes ; then
+
+cat >>confdefs.h <<_ACEOF
+#define HAVE__BUILTIN_FRAME_ADDRESS 1
+_ACEOF
+
 fi
 
 # We require 64-bit fseeko() to be available, but run this check anyway
index 22afb114b09d7eb0173d37d30378b707cea87c00..7cd1d91672fd4a736f4e764cb40a83a1cd6d9664 100644 (file)
@@ -1713,6 +1713,9 @@ PGAC_CHECK_BUILTIN_FUNC([__builtin_bswap64], [long int x])
 PGAC_CHECK_BUILTIN_FUNC([__builtin_clz], [unsigned int x])
 PGAC_CHECK_BUILTIN_FUNC([__builtin_ctz], [unsigned int x])
 PGAC_CHECK_BUILTIN_FUNC([__builtin_popcount], [unsigned int x])
+# __builtin_frame_address may draw a diagnostic for non-constant argument,
+# so it needs a different test function.
+PGAC_CHECK_BUILTIN_FUNC_PTR([__builtin_frame_address], [0])
 
 # We require 64-bit fseeko() to be available, but run this check anyway
 # in case it finds that _LARGEFILE_SOURCE has to be #define'd for that.
index 1e0936e5b48296699331ea168df2396310904e8a..1193b8e24b155a2580f96db8e67eeae5d801b471 100644 (file)
@@ -1028,7 +1028,7 @@ PostmasterMain(int argc, char *argv[])
    /*
     * Set reference point for stack-depth checking.
     */
-   set_stack_base();
+   (void) set_stack_base();
 
    /*
     * Initialize pipe (or process handle on Windows) that allows children to
index 7bc03ae4edcfb4f2f54980d0c0528a5655c9120e..3abcbb538f7573fad8b447b904ff90b80db8be99 100644 (file)
@@ -3280,7 +3280,9 @@ ia64_get_bsp(void)
 pg_stack_base_t
 set_stack_base(void)
 {
+#ifndef HAVE__BUILTIN_FRAME_ADDRESS
    char        stack_base;
+#endif
    pg_stack_base_t old;
 
 #if defined(__ia64__) || defined(__ia64)
@@ -3290,8 +3292,16 @@ set_stack_base(void)
    old = stack_base_ptr;
 #endif
 
-   /* Set up reference point for stack depth checking */
+   /*
+    * Set up reference point for stack depth checking.  On recent gcc we use
+    * __builtin_frame_address() to avoid a warning about storing a local
+    * variable's address in a long-lived variable.
+    */
+#ifdef HAVE__BUILTIN_FRAME_ADDRESS
+   stack_base_ptr = __builtin_frame_address(0);
+#else
    stack_base_ptr = &stack_base;
+#endif
 #if defined(__ia64__) || defined(__ia64)
    register_stack_base_ptr = ia64_get_bsp();
 #endif
index cca9704d2d7afb2c1012c2a107a899d09033ed0f..fafeb4b7d0b92784267cf6ba2ade69fb9e64bea2 100644 (file)
@@ -93,13 +93,12 @@ InitPostmasterChild(void)
    IsUnderPostmaster = true;   /* we are a postmaster subprocess now */
 
    /*
-    * Set reference point for stack-depth checking. We re-do that even in the
-    * !EXEC_BACKEND case, because there are some edge cases where processes
-    * are started with an alternative stack (e.g. starting bgworkers when
-    * running postgres using the rr debugger, as bgworkers are launched from
-    * signal handlers).
+    * Set reference point for stack-depth checking.  This might seem
+    * redundant in !EXEC_BACKEND builds; but it's not because the postmaster
+    * launches its children from signal handlers, so we might be running on
+    * an alternative stack.
     */
-   set_stack_base();
+   (void) set_stack_base();
 
    InitProcessGlobals();
 
index 66c53c52bbcc1f3d7944b35cd94db376e76da7ea..2711c2ce9bd8205009a911ee4213d2d7b175efa0 100644 (file)
 /* Define to 1 if your compiler understands __builtin_ctz. */
 #undef HAVE__BUILTIN_CTZ
 
+/* Define to 1 if your compiler understands __builtin_frame_address. */
+#undef HAVE__BUILTIN_FRAME_ADDRESS
+
 /* Define to 1 if your compiler understands __builtin_$op_overflow. */
 #undef HAVE__BUILTIN_OP_OVERFLOW
 
index 3c100f858c7160e2c7d70d7c257395aa61fe2fbd..243d0b64fd0009c11d6ad022c7a4a05c5090d210 100644 (file)
@@ -419,6 +419,7 @@ sub GenerateFiles
        HAVE__BUILTIN_CLZ                        => undef,
        HAVE__BUILTIN_CONSTANT_P                 => undef,
        HAVE__BUILTIN_CTZ                        => undef,
+       HAVE__BUILTIN_FRAME_ADDRESS              => undef,
        HAVE__BUILTIN_OP_OVERFLOW                => undef,
        HAVE__BUILTIN_POPCOUNT                   => undef,
        HAVE__BUILTIN_TYPES_COMPATIBLE_P         => undef,