Thread: Why is create function bringing down the Backend server?

Why is create function bringing down the Backend server?

From
Carlos Moreno
Date:
I'm trying to add additional functionality to the contrib/pgcrypto
branch  (at least for my own use, although ideally, I'd like to
make whatever additions good enough as to be accepted as part of
the PG distribution)

Anyway, I wanted to add hash functions (SHA-1 is already there,
so I'd like to add SHA-256 and SHA-512 at the very least, and
maybe also, for completeness, SHA-224 and SHA-384).

Anyway, I started with an implementation of SHA-1 that I already
have (since it is easy to test/debug, as I only have to compare it
with the already-existing sha1 function in pgcrypto).

I got it to work nicely, and I tried several millions randomly-
generated strings, and the result of my hash function matches
the result of pgcrypto's sha1 function.

The problem is, when I execute the SQL statement:

create or replace function sha1 ....  ;

for the second time (i.e., after making modifications and
recompiling), the *backend* crashes -- it then restarts
automatically, and then I run again the create or replace
statement, and it works now  (and the function seems to
work fine -- well, in its "final" version it does).

I know the list of possible causes may be nearly infinite, so
I put the modified file (I removed most of the other stuff from
the original pgcrypto.c file, and left the pg_digest function,
which is the one that computes hashes, and the one that I
used as a model to create mine):

http://www.mochima.com/tmp/pgcrypto.c

I also modified the SQL script file to include my function;
this (when I execute this script) is precisely the moment at
which the PG backend crashes  (well, it shuts down anyway):

http://www.mochima.com/tmp/pgcrypto.sql

Any ideas of what I'm doing wrong?

BTW, I compiled with the provided Makefile, then copy the .so
files to /usr/local/pgsql/lib directory, and ran /sbin/ldconfig
(that directory is included in my /etc/ld.so.conf file).  I'm
running PG 7.4.9 on a Linux FC4 on a Dual-Core Athlon64 (kernel
x86_64-smp).

Thanks for any comments/feedback!  (please by kind, as this is
my first attempt ever at creating PG functions -- but please
be tough!  Don't hold back valuable feedback just because you
don't want to hurt my baby feelings!  :-))

Carlos
--


Re: Why is create function bringing down the Backend server?

From
Tom Lane
Date:
Carlos Moreno <moreno_pg@mochima.com> writes:
> The problem is, when I execute the SQL statement:
> create or replace function sha1 ....  ;
> for the second time (i.e., after making modifications and
> recompiling), the *backend* crashes

Getting a stack trace from that core dump might be illuminating.
Better yet, attach to the backend with gdb before you execute
the crash-triggering statement, and let gdb trap the crash.

            regards, tom lane

Re: Why is create function bringing down the Backend server?

From
Marko Kreen
Date:
On 12/22/05, Carlos Moreno <moreno_pg@mochima.com> wrote:
> The problem is, when I execute the SQL statement:
>
> create or replace function sha1 ....  ;
>
> for the second time (i.e., after making modifications and
> recompiling), the *backend* crashes -- it then restarts
> automatically, and then I run again the create or replace
> statement, and it works now  (and the function seems to
> work fine -- well, in its "final" version it does).

You should see if there's something in server log.

And then indeed, try to gdb it.  You can run Postgres in non-daemon
mode with command 'postgres -D datadir database'.

The stripped pgcrypto.c you posted - your wrapper function looks fine,
only problem I see is that you deleted function find_provider that is used
by pg_digest, so there will be undefined function in final .so.

But that should not crash the server, so gdb trace could be still useful.

> Anyway, I wanted to add hash functions (SHA-1 is already there,
> so I'd like to add SHA-256 and SHA-512 at the very least, and
> maybe also, for completeness, SHA-224 and SHA-384).

For SHA2 hashes it should be enough to compile pgcrypto
against OpenSSL 0.9.8.  Or upgrade to PostgreSQL 8.1,
where they are included.

Ofcourse, that is no fun.

If you want to hack, you could try adding SHA224 to the SHA2
implementation in 8.1.  There are currently only SHA256/384/512
hashes implemented.  (AFAIR it is basically truncated SHA256
but with different init vector)

--
marko

Re: Why is create function bringing down the Backend server?

From
Carlos Moreno
Date:
Marko Kreen wrote:

>On 12/22/05, Carlos Moreno <moreno_pg@mochima.com> wrote:
>
>>The problem is, when I execute the SQL statement:
>>
>>create or replace function sha1 ....  ;
>>
>>for the second time (i.e., after making modifications and
>>recompiling), the *backend* crashes -- it then restarts
>>automatically, and then I run again the create or replace
>>statement, and it works now  (and the function seems to
>>work fine -- well, in its "final" version it does).
>>
>
>You should see if there's something in server log.
>

The only thing that does show does not seem to say much:

LOG:  server process (PID 12885) was terminated by signal 11
LOG:  terminating any other active server processes
LOG:  all server processes terminated; reinitializing

Not sure what the meaning of signal 11 is with PG (AFAIR, it's
one of the SIGUSER values, right?)

>
>And then indeed, try to gdb it.  You can run Postgres in non-daemon
>mode with command 'postgres -D datadir database'.
>
>The stripped pgcrypto.c you posted - your wrapper function looks fine,
>only problem I see is that you deleted function find_provider that is used
>by pg_digest, so there will be undefined function in final .so.
>

Oh no!!  That was only in the function I posted, so that the file
is kept as short as possible -- in the one that I compiled, I left
everything untouched, and only added my functions.

>
>But that should not crash the server, so gdb trace could be still useful.
>
>
Ok, will try to do it and post any interesting discoveries  (I
can't find any core files, so I guess I'll have to try gdbing it)

>>Anyway, I wanted to add hash functions (SHA-1 is already there,
>>so I'd like to add SHA-256 and SHA-512 at the very least, and
>>maybe also, for completeness, SHA-224 and SHA-384).
>>
>
>For SHA2 hashes it should be enough to compile pgcrypto
>against OpenSSL 0.9.8.  Or upgrade to PostgreSQL 8.1,
>where they are included.
>
>Ofcourse, that is no fun.
>

Hahahaha -- why do I keep being naive and making the same mistake
over and over!!!  :-)

As much as it is indeed no fun, it is also good to know  (and I
didn't know that OpenSSL 0.9.8 had them either, so thanks for
the double pointer!)

>
>If you want to hack, you could try adding SHA224 to the SHA2
>implementation in 8.1.
>

Sounds like a plan  :-)

Thanks,

Carlos
--


Re: Why is create function bringing down the Backend server?

From
Marko Kreen
Date:
On 12/23/05, Carlos Moreno <moreno_pg@mochima.com> wrote:
> Marko Kreen wrote:
>
> >On 12/22/05, Carlos Moreno <moreno_pg@mochima.com> wrote:
> >
> >>The problem is, when I execute the SQL statement:
> >>
> >>create or replace function sha1 ....  ;
> >>
> >>for the second time (i.e., after making modifications and
> >>recompiling), the *backend* crashes -- it then restarts
> >>automatically, and then I run again the create or replace
> >>statement, and it works now  (and the function seems to
> >>work fine -- well, in its "final" version it does).
> >>
> >
> >You should see if there's something in server log.
> >
>
> The only thing that does show does not seem to say much:
>
> LOG:  server process (PID 12885) was terminated by signal 11
> LOG:  terminating any other active server processes
> LOG:  all server processes terminated; reinitializing
>
> Not sure what the meaning of signal 11 is with PG (AFAIR, it's
> one of the SIGUSER values, right?)

Well, that does not help.  Signal 11 is SIGSEGV.

> >And then indeed, try to gdb it.  You can run Postgres in non-daemon
> >mode with command 'postgres -D datadir database'.
> >
> >The stripped pgcrypto.c you posted - your wrapper function looks fine,
> >only problem I see is that you deleted function find_provider that is used
> >by pg_digest, so there will be undefined function in final .so.
> >
>
> Oh no!!  That was only in the function I posted, so that the file
> is kept as short as possible -- in the one that I compiled, I left
> everything untouched, and only added my functions.

Ah, that's not it.  Then I'm clueless.

> >But that should not crash the server, so gdb trace could be still useful.
> >
> >
> Ok, will try to do it and post any interesting discoveries  (I
> can't find any core files, so I guess I'll have to try gdbing it)

Core files should be in data directory somewhere.
(If postmaster ulimit allows them.)

> >>Anyway, I wanted to add hash functions (SHA-1 is already there,
> >>so I'd like to add SHA-256 and SHA-512 at the very least, and
> >>maybe also, for completeness, SHA-224 and SHA-384).
> >>
> >
> >For SHA2 hashes it should be enough to compile pgcrypto
> >against OpenSSL 0.9.8.  Or upgrade to PostgreSQL 8.1,
> >where they are included.
> >
> >Ofcourse, that is no fun.
> >
>
> Hahahaha -- why do I keep being naive and making the same mistake
> over and over!!!  :-)
>
> As much as it is indeed no fun, it is also good to know  (and I
> didn't know that OpenSSL 0.9.8 had them either, so thanks for
> the double pointer!)

Hehe.  If hacking on a project, it is usually good to check out
the latest version.  pgcrypto had a major update in 8.1.

> >If you want to hack, you could try adding SHA224 to the SHA2
> >implementation in 8.1.
> >
>
> Sounds like a plan  :-)

Cool!  Look how SHA384 fits in and try to follow that.  That way
the code is in coherent style.

--
marko