Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XtFyH-0000SE-Rm for pgsql-bugs@arkaria.postgresql.org; Tue, 25 Nov 2014 13:15:57 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XtFyG-0004bN-Gv for pgsql-bugs@arkaria.postgresql.org; Tue, 25 Nov 2014 13:15:56 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XtFyF-0004bE-DC for pgsql-bugs@postgresql.org; Tue, 25 Nov 2014 13:15:55 +0000 Received: from smtp-outbound-1.vmware.com ([208.91.2.12]) by makus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1XtFyA-0003Gm-Nh for pgsql-bugs@postgresql.org; Tue, 25 Nov 2014 13:15:52 +0000 Received: from sc9-mailhost3.vmware.com (sc9-mailhost3.vmware.com [10.113.161.73]) by smtp-outbound-1.vmware.com (Postfix) with ESMTP id 67BB62878B for ; Tue, 25 Nov 2014 05:15:48 -0800 (PST) Received: from EX13-CAS-009.vmware.com (EX13-CAS-009.vmware.com [10.113.191.61]) by sc9-mailhost3.vmware.com (Postfix) with ESMTP id 54E7D41298 for ; Tue, 25 Nov 2014 05:15:48 -0800 (PST) Received: from EX13-MBX-024.vmware.com (10.113.191.44) by EX13-MBX-006.vmware.com (10.113.191.26) with Microsoft SMTP Server (TLS) id 15.0.913.22; Tue, 25 Nov 2014 05:15:43 -0800 Received: from [192.168.1.90] (10.113.160.246) by EX13-MBX-024.vmware.com (10.113.191.44) with Microsoft SMTP Server (TLS) id 15.0.913.22; Tue, 25 Nov 2014 05:15:31 -0800 Message-ID: <547480DE.4040408@vmware.com> Date: Tue, 25 Nov 2014 15:15:10 +0200 From: Heikki Linnakangas Organization: VMware User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.2.0 MIME-Version: 1.0 To: Pg Bugs Subject: PQexec() hangs on OOM Content-Type: multipart/mixed; boundary="------------090103020807050203090209" X-Originating-IP: [10.113.160.246] X-ClientProxiedBy: EX13-CAS-013.vmware.com (10.113.191.65) To EX13-MBX-024.vmware.com (10.113.191.44) X-Pg-Spam-Score: -6.9 (------) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --------------090103020807050203090209 Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 7bit 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 --------------090103020807050203090209 Content-Type: text/x-csrc; name="mallocfail.c" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="mallocfail.c" /* * 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 #include #include #include #include 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"); } --------------090103020807050203090209 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --------------090103020807050203090209--