Skip to content

Commit d9c4442

Browse files
committed
Fix possible crashes due to using elog/ereport too early in startup.
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.
1 parent 2d76d75 commit d9c4442

File tree

6 files changed

+36
-23
lines changed

6 files changed

+36
-23
lines changed

src/backend/bootstrap/bootstrap.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,6 @@ AuxiliaryProcessMain(int argc, char *argv[])
194194

195195
MyStartTime = time(NULL);
196196

197-
/*
198-
* Fire up essential subsystems: error and memory management
199-
*
200-
* If we are running under the postmaster, this is done already.
201-
*/
202-
if (!IsUnderPostmaster)
203-
MemoryContextInit();
204-
205197
/* Compute paths, if we didn't inherit them from postmaster */
206198
if (my_exec_path[0] == '\0')
207199
{

src/backend/main/main.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "postmaster/postmaster.h"
3939
#include "tcop/tcopprot.h"
4040
#include "utils/help_config.h"
41+
#include "utils/memutils.h"
4142
#include "utils/pg_locale.h"
4243
#include "utils/ps_status.h"
4344
#ifdef WIN32
@@ -78,6 +79,15 @@ main(int argc, char *argv[])
7879
*/
7980
argv = save_ps_display_args(argc, argv);
8081

82+
/*
83+
* Fire up essential subsystems: error and memory management
84+
*
85+
* Code after this point is allowed to use elog/ereport, though
86+
* localization of messages may not work right away, and messages won't go
87+
* anywhere but stderr until GUC settings get loaded.
88+
*/
89+
MemoryContextInit();
90+
8191
/*
8292
* Set up locale information from environment. Note that LC_CTYPE and
8393
* LC_COLLATE will be overridden later from pg_control if we are in an

src/backend/postmaster/postmaster.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -496,11 +496,6 @@ PostmasterMain(int argc, char *argv[])
496496
*/
497497
umask((mode_t) 0077);
498498

499-
/*
500-
* Fire up essential subsystems: memory management
501-
*/
502-
MemoryContextInit();
503-
504499
/*
505500
* By default, palloc() requests in the postmaster will be allocated in
506501
* the PostmasterContext, which is space that can be recycled by backends.
@@ -3943,7 +3938,6 @@ SubPostmasterMain(int argc, char *argv[])
39433938
whereToSendOutput = DestNone;
39443939

39453940
/* Setup essential subsystems (to ensure elog() behaves sanely) */
3946-
MemoryContextInit();
39473941
InitializeGUCOptions();
39483942

39493943
/* Read in the variables file */

src/backend/tcop/postgres.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3540,14 +3540,6 @@ PostgresMain(int argc, char *argv[],
35403540
MyStartTime = time(NULL);
35413541
}
35423542

3543-
/*
3544-
* Fire up essential subsystems: error and memory management
3545-
*
3546-
* If we are running under the postmaster, this is done already.
3547-
*/
3548-
if (!IsUnderPostmaster)
3549-
MemoryContextInit();
3550-
35513543
SetProcessingMode(InitProcessing);
35523544

35533545
/* Compute paths, if we didn't inherit them from postmaster */

src/backend/utils/error/elog.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,18 @@ errstart(int elevel, const char *filename, int lineno,
294294
if (elevel < ERROR && !output_to_server && !output_to_client)
295295
return false;
296296

297+
/*
298+
* We need to do some actual work. Make sure that memory context
299+
* initialization has finished, else we can't do anything useful.
300+
*/
301+
if (ErrorContext == NULL)
302+
{
303+
/* Ooops, hard crash time; very little we can do safely here */
304+
write_stderr("error occurred at %s:%d before error message processing is available\n",
305+
filename ? filename : "(unknown file)", lineno);
306+
exit(2);
307+
}
308+
297309
/*
298310
* Okay, crank up a stack entry to store the info in.
299311
*/
@@ -1099,6 +1111,15 @@ elog_start(const char *filename, int lineno, const char *funcname)
10991111
{
11001112
ErrorData *edata;
11011113

1114+
/* Make sure that memory context initialization has finished */
1115+
if (ErrorContext == NULL)
1116+
{
1117+
/* Ooops, hard crash time; very little we can do safely here */
1118+
write_stderr("error occurred at %s:%d before error message processing is available\n",
1119+
filename ? filename : "(unknown file)", lineno);
1120+
exit(2);
1121+
}
1122+
11021123
if (++errordata_stack_depth >= ERRORDATA_STACK_SIZE)
11031124
{
11041125
/*

src/backend/utils/mmgr/mcxt.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ static void MemoryContextStatsInternal(MemoryContext context, int level);
6868
* In normal multi-backend operation, this is called once during
6969
* postmaster startup, and not at all by individual backend startup
7070
* (since the backends inherit an already-initialized context subsystem
71-
* by virtue of being forked off the postmaster).
71+
* by virtue of being forked off the postmaster). But in an EXEC_BACKEND
72+
* build, each process must do this for itself.
7273
*
7374
* In a standalone backend this must be called during backend startup.
7475
*/
@@ -102,6 +103,9 @@ MemoryContextInit(void)
102103
* where retained memory in a context is *essential* --- we want to be
103104
* sure ErrorContext still has some memory even if we've run out
104105
* elsewhere!
106+
*
107+
* This should be the last step in this function, as elog.c assumes memory
108+
* management works once ErrorContext is non-null.
105109
*/
106110
ErrorContext = AllocSetContextCreate(TopMemoryContext,
107111
"ErrorContext",

0 commit comments

Comments
 (0)