Re: [PATCH] Make gram.y use palloc/pfree for memory management

Lists: pgsql-hackers
From: Marko Kreen <markokr(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: [PATCH] Make gram.y use palloc/pfree for memory management
Date: 2008-08-31 22:17:27
Message-ID: 20080831221727.18573.11996.stgit@lapp
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Currently gram.y uses malloc/free for memory management,
but all pointers are local to function. Unlike flex-based
scan.l which has static references to buffers and reuses them
across calls.

This means gram.y can leak memory if error is throws in
the middle of parsing.

The patch redefines malloc/free to palloc/pfree to fix the leak.

I did not use YYSTACK_ALLOC / YYSTACK_FREE that exists in newer bison,
because bison 1.875 does not support those.

Attachment Content-Type Size
gram-memleak-fix.patch text/plain 849 bytes

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Marko Kreen <markokr(at)gmail(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: [PATCH] Make gram.y use palloc/pfree for memory management
Date: 2008-09-01 02:44:50
Message-ID: 2719.1220237090@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Marko Kreen <markokr(at)gmail(dot)com> writes:
> This means gram.y can leak memory if error is throws in
> the middle of parsing.

Please offer some evidence for that claim.

regards, tom lane


From: "Marko Kreen" <markokr(at)gmail(dot)com>
To: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: [PATCH] Make gram.y use palloc/pfree for memory management
Date: 2008-09-01 13:33:53
Message-ID: e51f66da0809010633q5d0f999br72fe9dedf3b47760@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

First a correction, overriding malloc/free seems dangerous they
seems to leak out, so correct would be to use YYMALLOC/YYFREE.
This leaves 1.875 potentially leaking, but danger seems small.

On 9/1/08, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> Marko Kreen <markokr(at)gmail(dot)com> writes:
> > This means gram.y can leak memory if error is throws in
> > the middle of parsing.
>
> Please offer some evidence for that claim.

The leak occurs when
1. bison does allocation.
2. error is thrown.

Now, normally bison does not do allocation as it has initially 200-item
stack allocated in stack. When this is full it does allocate.

But I'm not familial enough with bison internals and Postgres parser
structure, on how cause the stack fill up. It may be that Postgres
parser avoids recursive stack allocations, thus in practice the
leak cannot occur.

--
marko


From: "Marko Kreen" <markokr(at)gmail(dot)com>
To: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: [PATCH] Make gram.y use palloc/pfree for memory management
Date: 2008-09-02 10:29:58
Message-ID: e51f66da0809020329x2eb85e55g70e8b5d1cf7ed577@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 9/1/08, Marko Kreen <markokr(at)gmail(dot)com> wrote:
> First a correction, overriding malloc/free seems dangerous they
> seems to leak out, so correct would be to use YYMALLOC/YYFREE.
> This leaves 1.875 potentially leaking, but danger seems small.

Here is the safer patch. As the chance for the leak is rare,
leaving 1.875 vulnerable should not be problem.

--
marko

Attachment Content-Type Size
bison.palloc.diff text/x-diff 477 bytes

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Marko Kreen" <markokr(at)gmail(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: [PATCH] Make gram.y use palloc/pfree for memory management
Date: 2008-09-02 20:43:13
Message-ID: 5621.1220388193@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

"Marko Kreen" <markokr(at)gmail(dot)com> writes:
> On 9/1/08, Marko Kreen <markokr(at)gmail(dot)com> wrote:
>> First a correction, overriding malloc/free seems dangerous they
>> seems to leak out, so correct would be to use YYMALLOC/YYFREE.
>> This leaves 1.875 potentially leaking, but danger seems small.

> Here is the safer patch. As the chance for the leak is rare,
> leaving 1.875 vulnerable should not be problem.

Actually, 1.875 appears to default to using alloca() when available,
so the issue doesn't even arise if you're building with gcc. (This
is probably why I'd never noticed a problem in this area.)

I thought that we might have an issue with flex as well, but so far as
I can tell there's no real problem given our current usage style for
the lexers.

Applied to all the backend .l files ...

regards, tom lane