Re: BUG #18374: Printing memory contexts on OOM condition might lead to segmentation fault

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Alexander Lakhin <exclusion(at)gmail(dot)com>
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #18374: Printing memory contexts on OOM condition might lead to segmentation fault
Date: 2024-03-03 21:39:29
Message-ID: 3399097.1709501969@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

I wrote:
> I find this in [1]:
>
> The C language stack growth does an implicit mremap. If you want absolute
> guarantees and run close to the edge you MUST mmap your stack for the
> largest size you think you will need. For typical stack usage this does
> not matter much but it's a corner case if you really really care
>
> Seems like we need to do some more work at startup to enforce that
> we have the amount of stack we think we do, if we're on Linux.

After thinking about that some more, I'm really quite unenthused about
trying to remap the stack for ourselves. It'd be both platform- and
architecture-dependent, and I'm afraid it'd introduce as many failure
modes as it removes. (Notably, I'm not sure we could guarantee
there's a guard page below the stack.) Since we've not seen reports
of this failure from the wild, I doubt it's worth the trouble.

I do think it's probably worth reducing MemoryContextDelete's stack
usage to O(1), just to ensure we can't get into stack trouble during
transaction abort. That's not hard at all, as attached.

I tried to make MemoryContextResetChildren work similarly, but that
doesn't work because if we're not removing child contexts then we
need extra state to tell which ones we've done already. For the
same reason my idea for bounding the stack space needed by
MemoryContextStats doesn't seem to work. We could possibly make it
work if we were willing to add a temporary-use pointer field to all
MemoryContext headers, but I'm unconvinced that'd be a good tradeoff.

regards, tom lane

Attachment Content-Type Size
memory-context-delete-with-fixed-stack-space.patch text/x-diff 1.4 KB

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Thomas Munro 2024-03-03 22:12:11 Re: BUG #18349: ERROR: invalid DSA memory alloc request size 1811939328, CONTEXT: parallel worker
Previous Message Tom Lane 2024-03-02 19:58:39 Re: BUG #18374: Printing memory contexts on OOM condition might lead to segmentation fault