Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TiGnC-0003Qt-8l for pgsql-bugs@arkaria.postgresql.org; Tue, 11 Dec 2012 03:46:02 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1TiGnB-0003BW-NC for pgsql-bugs@arkaria.postgresql.org; Tue, 11 Dec 2012 03:46:01 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TiGnB-0003BR-1P for pgsql-bugs@postgresql.org; Tue, 11 Dec 2012 03:46:01 +0000 Received: from mail-da0-f46.google.com ([209.85.210.46]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TiGn8-0006zr-VC for pgsql-bugs@postgresql.org; Tue, 11 Dec 2012 03:46:00 +0000 Received: by mail-da0-f46.google.com with SMTP id p5so1510066dak.19 for ; Mon, 10 Dec 2012 19:45:57 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=subject:from:to:cc:in-reply-to:references:content-type:date :message-id:mime-version:x-mailer:x-gm-message-state; bh=LRp4jxCDjCVVFHoGZ/xgZbI6IFPitqKMjVpanGSIENw=; b=mHz5D9PycuvGE1HdtQiT+mQjEhDzFi0S/GG9kbJMeLmCU6Y4hXGgF6PKKALpqnuyHU YQzo3WQR9NV1r+WmOb3zJaFH4dsMRjLK999E9lcyO8xp0JJ7lrFWDiRWMx9tkQxbDmc/ 4yNz0+1ogqjAEnmXnRUGlJDibWgc1YkfXzFDc35y1NSTCcDJJCVwib9vdYLMI3GeWjzj tN4Y83ol7T2MwIBX6Xr7hTPbt+am1o4Vj1N+u4oOXd726QcRGUw5UXFOzNndfj/b3CaL qnw7wtin+tSe0c8DCObVWorDrX2MF7jKjx5xdxt1afC8rQKBPViXzTwCHVOO55aoJ5Gy rWDw== Received: by 10.68.239.99 with SMTP id vr3mr44570933pbc.154.1355197557385; Mon, 10 Dec 2012 19:45:57 -0800 (PST) Received: from ?IPv6:::1? ([2600:3c01::f03c:91ff:fe93:2ae4]) by mx.google.com with ESMTPS id nw9sm7125176pbb.42.2012.12.10.19.45.55 (version=SSLv3 cipher=OTHER); Mon, 10 Dec 2012 19:45:56 -0800 (PST) Subject: Re: init_htab causes SIGFPE (or worse) due to miscalculation for large nbuckets From: Jeff Davis To: Tom Lane Cc: pgsql-bugs@postgresql.org In-Reply-To: <2050.1355192347@sss.pgh.pa.us> References: <1355079715.3896.14.camel@jdavis> <18018.1355093132@sss.pgh.pa.us> <1355185776.13316.4.camel@sussancws0025> <1036.1355189242@sss.pgh.pa.us> <1355191549.15663.10.camel@sussancws0025> <2050.1355192347@sss.pgh.pa.us> Content-Type: multipart/mixed; boundary="=-iiy55XaxBSLSXGx+bGVN" Date: Mon, 10 Dec 2012 19:45:44 -0800 Message-ID: <1355197546.15663.61.camel@sussancws0025> Mime-Version: 1.0 X-Mailer: Evolution 2.28.3 X-Gm-Message-State: ALoCoQmR74izWEYk2nQ0ETqAVIG3B0IDN+VosXN0DBpeIyVTivRpr349P5jzVnJ0VpQAYDTpQUxa X-Pg-Spam-Score: -2.6 (--) 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 --=-iiy55XaxBSLSXGx+bGVN Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7bit On Mon, 2012-12-10 at 21:19 -0500, Tom Lane wrote: > I hadn't gone any further than to code and test the functions I listed. > If you are working on a complete patch, please press on. Attached. This fixes my test case[1] and uses the functions that you wrote. I made them static because I couldn't think of a reason for something outside to call them. Regards, Jeff Davis [1]: The test case will just eat a lot of memory right now, but that's only because I set work_mem so high. So, it doesn't actually complete, but it no longer corrupts the HASHHDR. --=-iiy55XaxBSLSXGx+bGVN Content-Type: application/x-gzip; name="htab.patch.gz" Content-Disposition: attachment; filename="htab.patch.gz" Content-Transfer-Encoding: 7bit *** a/src/backend/executor/nodeHash.c --- b/src/backend/executor/nodeHash.c *************** *** 259,264 **** ExecHashTableCreate(Hash *node, List *hashOperators, bool keepNulls) --- 259,271 ---- printf("nbatch = %d, nbuckets = %d\n", nbatch, nbuckets); #endif + /* + * If nbuckets is chosen to be too large, then shifting the result of + * my_log2() can overflow. + */ + if (nbuckets > INT_MAX / 2) + nbuckets = INT_MAX / 2; + /* nbuckets must be a power of 2 */ log2_nbuckets = my_log2(nbuckets); Assert(nbuckets == (1 << log2_nbuckets)); *** a/src/backend/utils/hash/dynahash.c --- b/src/backend/utils/hash/dynahash.c *************** *** 208,213 **** static void hash_corrupted(HTAB *hashp); --- 208,215 ---- static void register_seq_scan(HTAB *hashp); static void deregister_seq_scan(HTAB *hashp); static bool has_seq_scans(HTAB *hashp); + static int next_pow2_int(long num); + static long next_pow2_long(long num); /* *************** *** 379,386 **** hash_create(const char *tabname, long nelem, HASHCTL *info, int flags) { /* Doesn't make sense to partition a local hash table */ Assert(flags & HASH_SHARED_MEM); ! /* # of partitions had better be a power of 2 */ ! Assert(info->num_partitions == (1L << my_log2(info->num_partitions))); hctl->num_partitions = info->num_partitions; } --- 381,392 ---- { /* Doesn't make sense to partition a local hash table */ Assert(flags & HASH_SHARED_MEM); ! /* ! * The number of partitions had better be a power of 2. Also, it must ! * be less than INT_MAX (see init_htab()), so call the int version of ! * next_pow2. ! */ ! Assert(info->num_partitions == next_pow2_int(info->num_partitions)); hctl->num_partitions = info->num_partitions; } *************** *** 523,529 **** init_htab(HTAB *hashp, long nelem) { HASHHDR *hctl = hashp->hctl; HASHSEGMENT *segp; - long lnbuckets; int nbuckets; int nsegs; --- 529,534 ---- *************** *** 538,546 **** init_htab(HTAB *hashp, long nelem) * number of buckets. Allocate space for the next greater power of two * number of buckets */ ! lnbuckets = (nelem - 1) / hctl->ffactor + 1; ! ! nbuckets = 1 << my_log2(lnbuckets); /* * In a partitioned table, nbuckets must be at least equal to --- 543,549 ---- * number of buckets. Allocate space for the next greater power of two * number of buckets */ ! nbuckets = next_pow2_int((nelem - 1) / hctl->ffactor + 1); /* * In a partitioned table, nbuckets must be at least equal to *************** *** 558,564 **** init_htab(HTAB *hashp, long nelem) * Figure number of directory segments needed, round up to a power of 2 */ nsegs = (nbuckets - 1) / hctl->ssize + 1; ! nsegs = 1 << my_log2(nsegs); /* * Make sure directory is big enough. If pre-allocated directory is too --- 561,567 ---- * Figure number of directory segments needed, round up to a power of 2 */ nsegs = (nbuckets - 1) / hctl->ssize + 1; ! nsegs = next_pow2_int(nsegs); /* * Make sure directory is big enough. If pre-allocated directory is too *************** *** 628,636 **** hash_estimate_size(long num_entries, Size entrysize) elementAllocCnt; /* estimate number of buckets wanted */ ! nBuckets = 1L << my_log2((num_entries - 1) / DEF_FFACTOR + 1); /* # of segments needed for nBuckets */ ! nSegments = 1L << my_log2((nBuckets - 1) / DEF_SEGSIZE + 1); /* directory entries */ nDirEntries = DEF_DIRSIZE; while (nDirEntries < nSegments) --- 631,639 ---- elementAllocCnt; /* estimate number of buckets wanted */ ! nBuckets = next_pow2_long((num_entries - 1) / DEF_FFACTOR + 1); /* # of segments needed for nBuckets */ ! nSegments = next_pow2_long((nBuckets - 1) / DEF_SEGSIZE + 1); /* directory entries */ nDirEntries = DEF_DIRSIZE; while (nDirEntries < nSegments) *************** *** 671,679 **** hash_select_dirsize(long num_entries) nDirEntries; /* estimate number of buckets wanted */ ! nBuckets = 1L << my_log2((num_entries - 1) / DEF_FFACTOR + 1); /* # of segments needed for nBuckets */ ! nSegments = 1L << my_log2((nBuckets - 1) / DEF_SEGSIZE + 1); /* directory entries */ nDirEntries = DEF_DIRSIZE; while (nDirEntries < nSegments) --- 674,682 ---- nDirEntries; /* estimate number of buckets wanted */ ! nBuckets = next_pow2_long((num_entries - 1) / DEF_FFACTOR + 1); /* # of segments needed for nBuckets */ ! nSegments = next_pow2_long((nBuckets - 1) / DEF_SEGSIZE + 1); /* directory entries */ nDirEntries = DEF_DIRSIZE; while (nDirEntries < nSegments) *************** *** 1408,1418 **** my_log2(long num) --- 1411,1442 ---- int i; long limit; + /* guard against too-large input, which would put us into infinite loop */ + if (num > LONG_MAX / 2) + num = LONG_MAX / 2; + for (i = 0, limit = 1; limit < num; i++, limit <<= 1) ; return i; } + /* calculate first power of 2 >= num, bounded to what will fit in a long */ + static long + next_pow2_long(long num) + { + /* my_log2's internal range check is sufficient */ + return 1L << my_log2(num); + } + + /* calculate first power of 2 >= num, bounded to what will fit in an int */ + static int + next_pow2_int(long num) + { + if (num > INT_MAX / 2) + num = INT_MAX / 2; + return 1 << my_log2(num); + } + /************************* SEQ SCAN TRACKING ************************/ --=-iiy55XaxBSLSXGx+bGVN 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 --=-iiy55XaxBSLSXGx+bGVN--