]> granicus.if.org Git - postgresql/commitdiff
Fix possible crashes due to using elog/ereport too early in startup.
authorTom Lane <tgl@sss.pgh.pa.us>
Sat, 11 Jan 2014 21:35:37 +0000 (16:35 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Sat, 11 Jan 2014 21:35:37 +0000 (16:35 -0500)
Per reports from Andres Freund and Luke Campbell, a server failure during
set_pglocale_pgservice results in a segfault rather than a useful error
message, because the infrastructure needed to use ereport hasn't been
initialized; specifically, MemoryContextInit hasn't been called.
One known cause of this is starting the server in a directory it
doesn't have permission to read.

We could try to prevent set_pglocale_pgservice from using anything that
depends on palloc or elog, but that would be messy, and the odds of future
breakage seem high.  Moreover there are other things being called in main.c
that look likely to use palloc or elog too --- perhaps those things
shouldn't be there, but they are there today.  The best solution seems to
be to move the call of MemoryContextInit to very early in the backend's
real main() function.  I've verified that an elog or ereport occurring
immediately after that is now capable of sending something useful to
stderr.

I also added code to elog.c to print something intelligible rather than
just crashing if MemoryContextInit hasn't created the ErrorContext.
This could happen if MemoryContextInit itself fails (due to malloc
failure), and provides some future-proofing against someone trying to
sneak in new code even earlier in server startup.

Back-patch to all supported branches.  Since we've only heard reports of
this type of failure recently, it may be that some recent change has made
it more likely to see a crash of this kind; but it sure looks like it's
broken all the way back.

src/backend/bootstrap/bootstrap.c
src/backend/main/main.c
src/backend/postmaster/postmaster.c
src/backend/tcop/postgres.c
src/backend/utils/error/elog.c
src/backend/utils/mmgr/mcxt.c

index fc093ccc4b9aa339f0fa093e1d6d24cb40ff2699..40ed7ec5112917d5da0c84ccdf3cce3148adc898 100644 (file)
@@ -199,14 +199,6 @@ AuxiliaryProcessMain(int argc, char *argv[])
 
        MyStartTime = time(NULL);
 
-       /*
-        * Fire up essential subsystems: error and memory management
-        *
-        * If we are running under the postmaster, this is done already.
-        */
-       if (!IsUnderPostmaster)
-               MemoryContextInit();
-
        /* Compute paths, if we didn't inherit them from postmaster */
        if (my_exec_path[0] == '\0')
        {
index 2eee3ef72aeeaf4ed1b0a6b23b90a73321a857e5..420c395a61c0ef9ee003a32b2e8e1e48abb89c75 100644 (file)
@@ -39,6 +39,7 @@
 #include "postmaster/postmaster.h"
 #include "tcop/tcopprot.h"
 #include "utils/help_config.h"
+#include "utils/memutils.h"
 #include "utils/pg_locale.h"
 #include "utils/ps_status.h"
 #ifdef WIN32
@@ -89,6 +90,15 @@ main(int argc, char *argv[])
        pgwin32_install_crashdump_handler();
 #endif
 
+       /*
+        * Fire up essential subsystems: error and memory management
+        *
+        * Code after this point is allowed to use elog/ereport, though
+        * localization of messages may not work right away, and messages won't go
+        * anywhere but stderr until GUC settings get loaded.
+        */
+       MemoryContextInit();
+
        /*
         * Set up locale information from environment.  Note that LC_CTYPE and
         * LC_COLLATE will be overridden later from pg_control if we are in an
index 873ae4a2c4ac72041c5ea8fb0cc39f73ae7981e4..7746b614647388250514ffb51bd36a7d512045c2 100644 (file)
@@ -498,11 +498,6 @@ PostmasterMain(int argc, char *argv[])
         */
        umask(S_IRWXG | S_IRWXO);
 
-       /*
-        * Fire up essential subsystems: memory management
-        */
-       MemoryContextInit();
-
        /*
         * By default, palloc() requests in the postmaster will be allocated in
         * the PostmasterContext, which is space that can be recycled by backends.
@@ -3989,7 +3984,6 @@ SubPostmasterMain(int argc, char *argv[])
        whereToSendOutput = DestNone;
 
        /* Setup essential subsystems (to ensure elog() behaves sanely) */
-       MemoryContextInit();
        InitializeGUCOptions();
 
        /* Read in the variables file */
index c7f36486ff76be319d96d6e450890f276df468a0..a37fa26a21ba6223b12243c0e796dfca27e9c9fe 100644 (file)
@@ -3531,14 +3531,6 @@ PostgresMain(int argc, char *argv[],
                MyStartTime = time(NULL);
        }
 
-       /*
-        * Fire up essential subsystems: error and memory management
-        *
-        * If we are running under the postmaster, this is done already.
-        */
-       if (!IsUnderPostmaster)
-               MemoryContextInit();
-
        SetProcessingMode(InitProcessing);
 
        /* Compute paths, if we didn't inherit them from postmaster */
index 986b312046b6f126e60ced057262b72ef41cf275..ec2197477291a0d5274ecbcac7a7d817a3a993c7 100644 (file)
@@ -301,6 +301,18 @@ errstart(int elevel, const char *filename, int lineno,
        if (elevel < ERROR && !output_to_server && !output_to_client)
                return false;
 
+       /*
+        * We need to do some actual work.  Make sure that memory context
+        * initialization has finished, else we can't do anything useful.
+        */
+       if (ErrorContext == NULL)
+       {
+               /* Ooops, hard crash time; very little we can do safely here */
+               write_stderr("error occurred at %s:%d before error message processing is available\n",
+                                        filename ? filename : "(unknown file)", lineno);
+               exit(2);
+       }
+
        /*
         * Okay, crank up a stack entry to store the info in.
         */
@@ -1137,6 +1149,15 @@ elog_start(const char *filename, int lineno, const char *funcname)
 {
        ErrorData  *edata;
 
+       /* Make sure that memory context initialization has finished */
+       if (ErrorContext == NULL)
+       {
+               /* Ooops, hard crash time; very little we can do safely here */
+               write_stderr("error occurred at %s:%d before error message processing is available\n",
+                                        filename ? filename : "(unknown file)", lineno);
+               exit(2);
+       }
+
        if (++errordata_stack_depth >= ERRORDATA_STACK_SIZE)
        {
                /*
index 7ed35d5bdf123ed6b1725edf27d32e94c20aebb4..0e2d151a3ee2416869b179cae9d9e66671095627 100644 (file)
@@ -68,7 +68,8 @@ static void MemoryContextStatsInternal(MemoryContext context, int level);
  * In normal multi-backend operation, this is called once during
  * postmaster startup, and not at all by individual backend startup
  * (since the backends inherit an already-initialized context subsystem
- * by virtue of being forked off the postmaster).
+ * by virtue of being forked off the postmaster).  But in an EXEC_BACKEND
+ * build, each process must do this for itself.
  *
  * In a standalone backend this must be called during backend startup.
  */
@@ -102,6 +103,9 @@ MemoryContextInit(void)
         * where retained memory in a context is *essential* --- we want to be
         * sure ErrorContext still has some memory even if we've run out
         * elsewhere!
+        *
+        * This should be the last step in this function, as elog.c assumes memory
+        * management works once ErrorContext is non-null.
         */
        ErrorContext = AllocSetContextCreate(TopMemoryContext,
                                                                                 "ErrorContext",