Changeset: 18e949a6e0bb for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/18e949a6e0bb
Modified Files:
        gdk/ChangeLog.Jan2022
        gdk/gdk_bat.c
        gdk/gdk_batop.c
        sql/ChangeLog.Jan2022
Branch: Jan2022
Log Message:

Merge with Jul2021 branch.


diffs (199 lines):

diff --git a/gdk/ChangeLog.Jan2022 b/gdk/ChangeLog.Jan2022
--- a/gdk/ChangeLog.Jan2022
+++ b/gdk/ChangeLog.Jan2022
@@ -1,6 +1,12 @@
 # ChangeLog file for GDK
 # This file is updated with Maddlog
 
+* Wed Dec 14 2022 Sjoerd Mullender <[email protected]>
+- Fixed a race condition where a hash could have been created on a
+  bat using the old bat count while in another thread the bat count
+  got updated.  This would make the hash be based on too small a size,
+  causing failures later on.
+
 * Thu Dec  8 2022 Sjoerd Mullender <[email protected]>
 - When extending a bat failed, the capacity had been updated already and
   was therefore too large.  This could then later cause a crash.  This has
diff --git a/gdk/gdk_bat.c b/gdk/gdk_bat.c
--- a/gdk/gdk_bat.c
+++ b/gdk/gdk_bat.c
@@ -1157,6 +1157,7 @@ BUNappendmulti(BAT *b, const void *value
                b->tsorted = b->trevsorted = b->tkey = false;
        }
        MT_lock_unset(&b->theaplock);
+       MT_rwlock_wrlock(&b->thashlock);
        if (values && b->ttype) {
                int (*atomcmp) (const void *, const void *) = 
ATOMcompare(b->ttype);
                const void *atomnil = ATOMnilptr(b->ttype);
@@ -1172,6 +1173,7 @@ BUNappendmulti(BAT *b, const void *value
                                t = ((void **) values)[i];
                                gdk_return rc = tfastins_nocheckVAR(b, p, t);
                                if (rc != GDK_SUCCEED) {
+                                       MT_rwlock_wrunlock(&b->thashlock);
                                        return rc;
                                }
                                if (vbase != b->tvheap->base) {
@@ -1212,7 +1214,6 @@ BUNappendmulti(BAT *b, const void *value
                                }
                                p++;
                        }
-                       MT_rwlock_wrlock(&b->thashlock);
                        if (b->thash) {
                                p -= count;
                                for (BUN i = 0; i < count; i++) {
@@ -1221,7 +1222,6 @@ BUNappendmulti(BAT *b, const void *value
                                        p++;
                                }
                        }
-                       MT_rwlock_wrunlock(&b->thashlock);
                } else if (ATOMstorage(b->ttype) == TYPE_msk) {
                        bi.minpos = bi.maxpos = BUN_NONE;
                        minvalp = maxvalp = NULL;
@@ -1235,7 +1235,6 @@ BUNappendmulti(BAT *b, const void *value
                                minvalp = BUNtloc(bi, bi.minpos);
                        if (bi.maxpos != BUN_NONE)
                                maxvalp = BUNtloc(bi, bi.maxpos);
-                       MT_rwlock_wrlock(&b->thashlock);
                        for (BUN i = 0; i < count; i++) {
                                t = (void *) ((char *) values + (i << 
b->tshift));
                                gdk_return rc = tfastins_nocheckFIX(b, p, t);
@@ -1265,14 +1264,12 @@ BUNappendmulti(BAT *b, const void *value
                                }
                                p++;
                        }
-                       MT_rwlock_wrunlock(&b->thashlock);
                }
                MT_lock_set(&b->theaplock);
                b->tminpos = bi.minpos;
                b->tmaxpos = bi.maxpos;
                MT_lock_unset(&b->theaplock);
        } else {
-               MT_rwlock_wrlock(&b->thashlock);
                for (BUN i = 0; i < count; i++) {
                        gdk_return rc = tfastins_nocheck(b, p, t);
                        if (rc != GDK_SUCCEED) {
@@ -1284,11 +1281,11 @@ BUNappendmulti(BAT *b, const void *value
                        }
                        p++;
                }
-               MT_rwlock_wrunlock(&b->thashlock);
        }
        MT_lock_set(&b->theaplock);
        BATsetcount(b, p);
        MT_lock_unset(&b->theaplock);
+       MT_rwlock_wrunlock(&b->thashlock);
 
        IMPSdestroy(b); /* no support for inserts in imprints yet */
        OIDXdestroy(b);
@@ -1555,14 +1552,11 @@ BUNinplacemulti(BAT *b, const oid *posit
                        }
                        if (b->twidth < SIZEOF_VAR_T &&
                            (b->twidth <= 2 ? _d - GDK_VAROFFSET : _d) >= 
((size_t) 1 << (8 << b->tshift))) {
-                               /* doesn't fit in current heap, upgrade
-                                * it, can't keep hashlock while doing
-                                * so */
-                               MT_rwlock_wrunlock(&b->thashlock);
+                               /* doesn't fit in current heap, upgrade it */
                                if (GDKupgradevarheap(b, _d, 0, bi.count) != 
GDK_SUCCEED) {
+                                       MT_rwlock_wrunlock(&b->thashlock);
                                        return GDK_FAIL;
                                }
-                               MT_rwlock_wrlock(&b->thashlock);
                        }
                        /* reinitialize iterator after possible heap upgrade */
                        {
diff --git a/gdk/gdk_batop.c b/gdk/gdk_batop.c
--- a/gdk/gdk_batop.c
+++ b/gdk/gdk_batop.c
@@ -328,12 +328,12 @@ insert_string_bat(BAT *b, BAT *n, struct
        }
        bat_iterator_end(&ni);
        TIMEOUT_CHECK(timeoffset, TIMEOUT_HANDLER(GDK_FAIL));
+       MT_rwlock_wrlock(&b->thashlock);
        MT_lock_set(&b->theaplock);
        BATsetcount(b, oldcnt + ci->ncand);
        assert(b->batCapacity >= b->batCount);
        MT_lock_unset(&b->theaplock);
        /* maintain hash */
-       MT_rwlock_wrlock(&b->thashlock);
        for (r = oldcnt, cnt = BATcount(b); b->thash && r < cnt; r++) {
                HASHappend_locked(b, r, b->tvheap->base + VarHeapVal(Tloc(b, 
0), r, b->twidth));
        }
@@ -403,11 +403,11 @@ append_varsized_bat(BAT *b, BAT *n, stru
                                *dst++ = src[canditer_next(ci) - hseq];
                        }
                }
+               MT_rwlock_wrlock(&b->thashlock);
                MT_lock_set(&b->theaplock);
                BATsetcount(b, BATcount(b) + ci->ncand);
                MT_lock_unset(&b->theaplock);
                /* maintain hash table */
-               MT_rwlock_wrlock(&b->thashlock);
                for (BUN i = BATcount(b) - ci->ncand;
                     b->thash && i < BATcount(b);
                     i++) {
@@ -494,10 +494,10 @@ append_varsized_bat(BAT *b, BAT *n, stru
                        r++;
                }
        }
-       MT_rwlock_wrunlock(&b->thashlock);
        MT_lock_set(&b->theaplock);
        BATsetcount(b, r);
        MT_lock_unset(&b->theaplock);
+       MT_rwlock_wrunlock(&b->thashlock);
        bat_iterator_end(&ni);
        return GDK_SUCCEED;
 }
@@ -922,10 +922,10 @@ BATappend2(BAT *b, BAT *n, BAT *s, bool 
                        }
                        TIMEOUT_CHECK(timeoffset, 
GOTO_LABEL_TIMEOUT_HANDLER(bailout));
                }
-               MT_rwlock_wrunlock(&b->thashlock);
                MT_lock_set(&b->theaplock);
                BATsetcount(b, b->batCount + ci.ncand);
                MT_lock_unset(&b->theaplock);
+               MT_rwlock_wrunlock(&b->thashlock);
        }
 
   doreturn:
@@ -1318,16 +1318,10 @@ BATappend_or_update(BAT *b, BAT *p, cons
                        }
                        if (b->twidth < SIZEOF_VAR_T &&
                            (b->twidth <= 2 ? d - GDK_VAROFFSET : d) >= 
((size_t) 1 << (8 << b->tshift))) {
-                               /* doesn't fit in current heap, upgrade
-                                * it, can't keep hashlock while doing
-                                * so */
-                               MT_rwlock_wrunlock(&b->thashlock);
-                               locked = false;
+                               /* doesn't fit in current heap, upgrade it */
                                if (GDKupgradevarheap(b, d, 0, MAX(updid, 
b->batCount)) != GDK_SUCCEED) {
                                        goto bailout;
                                }
-                               MT_rwlock_wrlock(&b->thashlock);
-                               locked = true;
                        }
                        /* in case ATOMreplaceVAR and/or
                         * GDKupgradevarheap replaces a heap, we need to
@@ -1367,6 +1361,7 @@ BATappend_or_update(BAT *b, BAT *p, cons
                b->tvheap->dirty = true;
                MT_lock_unset(&b->theaplock);
        } else if (ATOMstorage(b->ttype) == TYPE_msk) {
+               assert(b->thash == NULL);
                HASHdestroy(b); /* hash doesn't make sense for msk */
                for (BUN i = 0; i < ni.count; i++) {
                        oid updid;
diff --git a/sql/ChangeLog.Jan2022 b/sql/ChangeLog.Jan2022
--- a/sql/ChangeLog.Jan2022
+++ b/sql/ChangeLog.Jan2022
@@ -1,6 +1,11 @@
 # ChangeLog file for sql
 # This file is updated with Maddlog
 
+* Wed Dec 14 2022 Sjoerd Mullender <[email protected]>
+- Fixed cleanup after a failed allocation where the data being cleaned
+  up was unitialized but still used as pointers to memory that also had
+  to be freed.
+
 * Wed Dec  7 2022 Sjoerd Mullender <[email protected]>
 - If there was an error in one of the special commands to the server
   (e.g. setting the reply size for result sets), the server could get
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to