Memory leak reported by address sanitizer in ECPGconnect/CRYPTO_zalloc

From: Madars Vitolins <madars(dot)vitolins(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Memory leak reported by address sanitizer in ECPGconnect/CRYPTO_zalloc
Date: 2019-10-17 10:12:03
Message-ID: 34f0f7fa-2977-615d-d690-9f47c22c88dd@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Folks,

I am doing some development for Enduro/X distributed transaction
middleware XA driver emulation, so that it would be suitable for ecpg
apps too, and so the program opens the connection with help of
ECPGconnect() with assigned connection id, .e.g:

/**
 * Perform connect
 * @param conndata parsed connection data
 * @param connname connection name
 * @return NULL (connection failed) or connection object
 */
expublic PGconn * ndrx_pg_connect(ndrx_pgconnect_t *conndata, char
*connname)
{
    PGconn *ret = NULL;

    NDRX_LOG(log_debug, "Establishing ECPG connection: [%s]", conndata);

    /* OK, try to open, with out autocommit please!
     */
    if (!ECPGconnect (__LINE__, conndata->c, conndata->url, conndata->user,
            conndata->password, connname, EXFALSE))
    {
        NDRX_LOG(log_error, "ECPGconnect failed, code %ld state: [%s]: %s",
                (long)sqlca.sqlcode, sqlca.sqlstate,
sqlca.sqlerrm.sqlerrmc);
        ret = NULL;
        goto out;
    }

    ret = ECPGget_PGconn(connname);
    if (NULL==ret)
    {
        NDRX_LOG(log_error, "Postgres error: failed to get PQ
connection!");
        ret = NULL;
        goto out;
    }

out:
  ...

And connection at the end of the program are closed by:

/**
 * disconnect from postgres
 * @param conn current connection object
 * @param connname connection name
 * @return EXSUCCEED/EXFAIL
 */
expublic int ndrx_pg_disconnect(PGconn *conn, char *connname)
{
    int ret = EXSUCCEED;

    NDRX_LOG(log_debug, "Closing ECPG connection: [%s]", connname);

    if (!ECPGdisconnect(__LINE__, connname))
    {
        NDRX_LOG(log_error, "ECPGdisconnect failed: %s",
                PQerrorMessage(conn));
        EXFAIL_OUT(ret);
    }
out:
    return ret;
}

I logs I have:

N:NDRX:5:b86e6a53:
3940:7f4ccc218300:012:20191017:091547294:a_open_entry:tmi/xa.c:0365:atmi_xa_open_entry
RMID=1
N:NDRX:5:b86e6a53:
3940:7f4ccc218300:012:20191017:091547294:a_open_entry:switch.c:0295:Connection
name: [20191017-91547294-11]
N:NDRX:5:b86e6a53:
3940:7f4ccc218300:012:20191017:091547294:x_pg_connect:s/ecpg.c:0067:Establishing
ECPG connection: []
N:NDRX:5:b86e6a53: 3940:7f4cdab329c0:001:20191017:091547296:_tpcontinue
:return.c:0631:Long jumping to continue!

...

N:NDRX:5:b86e6a53:
3940:7f4cdab329c0:001:20191017:091548579:_close_entry:tmi/xa.c:0404:atmi_xa_close_entry
N:NDRX:5:b86e6a53:
3940:7f4cdab329c0:001:20191017:091548579:g_disconnect:s/ecpg.c:0102:Closing
ECPG connection: [20191017-91546256-0]
N:NDRX:4:b86e6a53:
3940:7f4cdab329c0:001:20191017:091548580:_close_entry:switch.c:0341:Connection
closed

But the problem is that at times I get following leaks:

==3940==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 716 byte(s) in 3 object(s) allocated from:
    #0 0x7f4cd9a42b50 in __interceptor_malloc
(/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb50)
    #1 0x7f4cd43a4e58 in CRYPTO_zalloc
(/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1+0x17ae58)

Direct leak of 584 byte(s) in 1 object(s) allocated from:
    #0 0x7f4cd9a42b50 in __interceptor_malloc
(/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb50)
    #1 0x7f4cd43a4e58 in CRYPTO_zalloc
(/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1+0x17ae58)
    #2 0x7f4cd4ba1b5f in PQconnectPoll
(/usr/lib/x86_64-linux-gnu/libpq.so.5+0xeb5f)
    #3 0x7f4cd4ba29de (/usr/lib/x86_64-linux-gnu/libpq.so.5+0xf9de)
    #4 0x7f4cd4ba5666 in PQconnectdbParams
(/usr/lib/x86_64-linux-gnu/libpq.so.5+0x12666)
    #5 0x7f4cd4de89c2 in ECPGconnect
(/usr/lib/x86_64-linux-gnu/libecpg.so.6+0xc9c2)
    #6 0x7f4cd50f9d73 in ndrx_pg_connect
/home/user1/endurox/xadrv/postgres/ecpg.c:71
    #7 0x7f4cd50f6ce7 in xa_open_entry
/home/user1/endurox/xadrv/postgres/pgswitch.c:297
    #8 0x7f4cd50f6ce7 in xa_open_entry_stat
/home/user1/endurox/xadrv/postgres/pgswitch.c:785
    #9 0x7f4cd9403ef0 in atmi_xa_open_entry
/home/user1/endurox/libatmi/xa.c:373
    #10 0x7f4cd940bcce in ndrx_tpopen /home/user1/endurox/libatmi/xa.c:1335
    #11 0x7f4cd93c27e2 in tpopen /home/user1/endurox/libatmi/atmi.c:468
    #12 0x55ae20f4e0f9 in tm_thread_init
/home/user1/endurox/tmsrv/tmsrv.c:115
    #13 0x55ae20f4f3b8 in TPTMSRV_TH /home/user1/endurox/tmsrv/tmsrv.c:161
    #14 0x55ae20f6ca63 in poolthread_do
/home/user1/endurox/libexthpool/thpool.c:370
    #15 0x7f4cd8bf36da in start_thread
(/lib/x86_64-linux-gnu/libpthread.so.0+0x76da)

Indirect leak of 1728 byte(s) in 8 object(s) allocated from:
    #0 0x7f4cd9a42b50 in __interceptor_malloc
(/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb50)
    #1 0x7f4cd43a4e58 in CRYPTO_zalloc
(/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1+0x17ae58)

Can anybody give a hint where could be a problem?

program links libecpg and libpq.

$ psql --version
psql (PostgreSQL) 10.10 (Ubuntu 10.10-0ubuntu0.18.04.1)

Thanks a lot in advance,

Madars

Browse pgsql-hackers by date

  From Date Subject
Next Message 曾文旌 (义从) 2019-10-17 10:18:34 Re: [Proposal] Global temporary tables
Previous Message Alvaro Herrera 2019-10-17 09:58:27 Re: Remaining calls of heap_close/heap_open in the tree