From: Heikki Linnakangas <hlinnakangas@vmware.com>
To: Pg Bugs <pgsql-bugs@postgresql.org>
Subject: PQexec() hangs on OOM
Date: Tue, 25 Nov 2014 15:15:10 +0200
Message-ID: <547480DE.4040408@vmware.com> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
Alex Shulgin's find of a missing NULL check after strdup()
(87tx1or3cc.fsf@commandprompt.com) prompted me to do some testing of
libpq, when malloc/strdup returns NULL. To simulate running out of
memory, I wrote a little LD_PRELOAD library that allows causing malloc()
to return NULL, after a particular number of calls. and ran
src/test/examples/testlibpq with that hack.
After fixing all the missing NULL-checks, I found that testlibpq
sometimes just hangs. It happens when this malloc() call in
PQmakeEmptyPGResult() fails:
#3 0x00007f6dc86495c0 in malloc () from /home/heikki/mallocfail.so
#4 0x00007f6dc8423b6e in PQmakeEmptyPGresult (conn=0x1bea040,
status=PGRES_COMMAND_OK) at fe-exec.c:144
#5 0x00007f6dc8430b27 in pqParseInput3 (conn=0x1bea040) at
fe-protocol3.c:204
#6 0x00007f6dc8426468 in parseInput (conn=0x1bea040) at fe-exec.c:1652
#7 0x00007f6dc8426583 in PQgetResult (conn=0x1bea040) at fe-exec.c:1727
#8 0x00007f6dc8426c76 in PQexecFinish (conn=0x1bea040) at fe-exec.c:2000
#9 0x00007f6dc84268ad in PQexec (conn=0x1bea040, query=0x400e32
"BEGIN") at fe-exec.c:1834
#10 0x0000000000400b18 in main (argc=1, argv=0x7fffc9b6a568) at
testlibpq.c:59
When that malloc() returns NULL, parseInput returns without reading any
input. PQgetResult() takes that as a sign that it needs to read more
input from the server, before calling parseInput() again, and that read
never returns because there is no more data coming from the server.
I don't have any immediate plans to fix this, or to continue testing
this. There might well be more cases like this. Patches are welcome.
Attached is the little wrapper library I used to test this. testlibpq
hangs when run with MALLOC_FAIL_AT=110. It's really quick & dirty, sorry
about that. I'm sure there are more sophisticated tools to do similar
testing out there somewhere..
- Heikki
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugsAttachments:mallocfail.c (text/x-csrc, 971B) download
/*
* To compile:
* gcc mallocfail.c -c -fPIC
* ld -ldl mallocfail.o -o mallocfail.so -shared
*
* To run:
* MALLOC_FAIL_AT=110 LD_PRELOAD=mallocfail.so ./testlibpq
*/
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <execinfo.h>
#include <unistd.h>
static int counter = 0;
static int fail_at = -1;
void dump_core(void)
{
if (!fork())
abort();
}
void *(*orig_malloc)(size_t size);
void *malloc(size_t size)
{
counter++;
if (counter % 10 == 0)
printf("malloc called: %d!\n", counter);
if (counter == fail_at)
{
printf("malloc failing: %d!\n", counter);
create_dump();
return NULL;
}
return orig_malloc(size);
}
void
_init(void)
{
char *fail_at_str = getenv("MALLOC_FAIL_AT");
if (!fail_at_str)
printf("FAIL_AT not set!\n");
else
{
fail_at = atoi(fail_at_str);
printf("FAIL_AT: %d\n", fail_at);
}
printf("loading malloc imposter.\n");
orig_malloc = dlsym(RTLD_NEXT, "malloc");
}
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: hlinnakangas@vmware.com
Subject: Re: PQexec() hangs on OOM
In-Reply-To: <547480DE.4040408@vmware.com>
References: <547480DE.4040408@vmware.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox