Re: BUG #13571: sql-only extension segfaults backend during creation

From: Oleksandr Shulgin <oleksandr(dot)shulgin(at)zalando(dot)de>
To: feikesteenbergen(at)gmail(dot)com
Cc: pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #13571: sql-only extension segfaults backend during creation
Date: 2015-08-14 13:08:01
Message-ID: 87k2sy6mry.fsf@ashulgin01.corp.ad.zalando.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

feikesteenbergen(at)gmail(dot)com writes:

> The following bug has been logged on the website:
>
> Bug reference: 13571
> Logged by: Feike Steenbergen
> Email address: feikesteenbergen(at)gmail(dot)com
> PostgreSQL version: 9.5alpha2
> Operating system: Debian jessie x86_64
> Description:
>
> When installing a sql-only extension, my backend crashes taking the cluster
> with it.
>
> After some debugging we have created a reproducable testcase.
>
> postgres(at)postgres=# create extension castbug;
> server closed the connection unexpectedly
>
> castbug.control:
>
> relocatable = false
> superuser = true
> comment = 'This triggers a segfault on 9.5alpha2'
> default_version = unstable
>
> castbug--unstable.sql:
>
> DO
> $$
> DECLARE
> roles text[] := '{"job_scheduler","job_monitor"}';
> BEGIN
> END;
> $$;
>
> SELECT count(*)
> FROM pg_class
> CROSS JOIN generate_series(1,100);
>
> DO
> $$
> DECLARE
> something text[] := '{"jb_scheduler","job_monitr"}';
> BEGIN
> END;
> $$;

I did a quick debugging session on this and I believe that the cast_hash
logic in pl_exec.c is to blame.

The first time get_cast_hashentry() tries to find a cast, it creates the
global hash table and inserts an entry for the cast in it, using the
current memory context, which is of the first DO block. When the first
DO block ends, the memory context is freed, but the cast_hash with all
its content stays. So the next time we're trying to find an appropriate
cast function in the second DO block, we are getting the old one, but it
is now pointing to the deallocated memory.

I'm not sure what would be the proper fix for this.

--
Alex

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2015-08-14 13:58:08 Re: BUG #13571: sql-only extension segfaults backend during creation
Previous Message Feike Steenbergen 2015-08-14 13:04:35 Re: BUG #13571: sql-only extension segfaults backend during creation