Skip to content

Commit 5bfcc9e

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 36785a2 commit 5bfcc9e

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
@@ -202,14 +202,6 @@ AuxiliaryProcessMain(int argc, char *argv[])
202202

203203
MyStartTime = time(NULL);
204204

205-
/*
206-
* Fire up essential subsystems: error and memory management
207-
*
208-
* If we are running under the postmaster, this is done already.
209-
*/
210-
if (!IsUnderPostmaster)
211-
MemoryContextInit();
212-
213205
/* Compute paths, if we didn't inherit them from postmaster */
214206
if (my_exec_path[0] == '\0')
215207
{

src/backend/main/main.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "postmaster/postmaster.h"
4040
#include "tcop/tcopprot.h"
4141
#include "utils/help_config.h"
42+
#include "utils/memutils.h"
4243
#include "utils/pg_locale.h"
4344
#include "utils/ps_status.h"
4445

@@ -86,6 +87,15 @@ main(int argc, char *argv[])
8687
pgwin32_install_crashdump_handler();
8788
#endif
8889

90+
/*
91+
* Fire up essential subsystems: error and memory management
92+
*
93+
* Code after this point is allowed to use elog/ereport, though
94+
* localization of messages may not work right away, and messages won't go
95+
* anywhere but stderr until GUC settings get loaded.
96+
*/
97+
MemoryContextInit();
98+
8999
/*
90100
* Set up locale information from environment. Note that LC_CTYPE and
91101
* 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
@@ -575,11 +575,6 @@ PostmasterMain(int argc, char *argv[])
575575
*/
576576
umask(S_IRWXG | S_IRWXO);
577577

578-
/*
579-
* Fire up essential subsystems: memory management
580-
*/
581-
MemoryContextInit();
582-
583578
/*
584579
* By default, palloc() requests in the postmaster will be allocated in
585580
* the PostmasterContext, which is space that can be recycled by backends.
@@ -4379,7 +4374,6 @@ SubPostmasterMain(int argc, char *argv[])
43794374
whereToSendOutput = DestNone;
43804375

43814376
/* Setup essential subsystems (to ensure elog() behaves sanely) */
4382-
MemoryContextInit();
43834377
InitializeGUCOptions();
43844378

43854379
/* Read in the variables file */

src/backend/tcop/postgres.c

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

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

35553547
/* 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
@@ -309,6 +309,18 @@ errstart(int elevel, const char *filename, int lineno,
309309
if (elevel < ERROR && !output_to_server && !output_to_client)
310310
return false;
311311

312+
/*
313+
* We need to do some actual work. Make sure that memory context
314+
* initialization has finished, else we can't do anything useful.
315+
*/
316+
if (ErrorContext == NULL)
317+
{
318+
/* Ooops, hard crash time; very little we can do safely here */
319+
write_stderr("error occurred at %s:%d before error message processing is available\n",
320+
filename ? filename : "(unknown file)", lineno);
321+
exit(2);
322+
}
323+
312324
/*
313325
* Okay, crank up a stack entry to store the info in.
314326
*/
@@ -1231,6 +1243,15 @@ elog_start(const char *filename, int lineno, const char *funcname)
12311243
{
12321244
ErrorData *edata;
12331245

1246+
/* Make sure that memory context initialization has finished */
1247+
if (ErrorContext == NULL)
1248+
{
1249+
/* Ooops, hard crash time; very little we can do safely here */
1250+
write_stderr("error occurred at %s:%d before error message processing is available\n",
1251+
filename ? filename : "(unknown file)", lineno);
1252+
exit(2);
1253+
}
1254+
12341255
if (++errordata_stack_depth >= ERRORDATA_STACK_SIZE)
12351256
{
12361257
/*

src/backend/utils/mmgr/mcxt.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ static void MemoryContextStatsInternal(MemoryContext context, int level);
7171
* In normal multi-backend operation, this is called once during
7272
* postmaster startup, and not at all by individual backend startup
7373
* (since the backends inherit an already-initialized context subsystem
74-
* by virtue of being forked off the postmaster).
74+
* by virtue of being forked off the postmaster). But in an EXEC_BACKEND
75+
* build, each process must do this for itself.
7576
*
7677
* In a standalone backend this must be called during backend startup.
7778
*/
@@ -105,6 +106,9 @@ MemoryContextInit(void)
105106
* where retained memory in a context is *essential* --- we want to be
106107
* sure ErrorContext still has some memory even if we've run out
107108
* elsewhere!
109+
*
110+
* This should be the last step in this function, as elog.c assumes memory
111+
* management works once ErrorContext is non-null.
108112
*/
109113
ErrorContext = AllocSetContextCreate(TopMemoryContext,
110114
"ErrorContext",

0 commit comments

Comments
 (0)