683572
From f793042f2bac2ace9a5c0030b47b41c4db561a5b Mon Sep 17 00:00:00 2001
683572
From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= <ppisar@redhat.com>
683572
Date: Fri, 6 Jun 2014 14:31:59 +0200
683572
Subject: [PATCH] Destroy {GDBM,NDBM,ODBM,SDBM}_File objects only from original
683572
 thread context
683572
MIME-Version: 1.0
683572
Content-Type: text/plain; charset=UTF-8
683572
Content-Transfer-Encoding: 8bit
683572
683572
This patch fixes a crash when destroing a hash tied to a *_File
683572
database after spawning a thread:
683572
683572
use Fcntl;
683572
use SDBM_File;
683572
use threads;
683572
tie(my %dbtest, 'SDBM_File', "test.db", O_RDWR|O_CREAT, 0666);
683572
threads->new(sub {})->join;
683572
683572
This crashed or paniced depending on how perl was configured.
683572
683572
Closes RT#61912.
683572
683572
Signed-off-by: Petr Písař <ppisar@redhat.com>
683572
---
683572
 ext/GDBM_File/GDBM_File.xs | 16 ++++++++++------
683572
 ext/NDBM_File/NDBM_File.xs | 16 ++++++++++------
683572
 ext/ODBM_File/ODBM_File.xs | 18 +++++++++++-------
683572
 ext/SDBM_File/SDBM_File.xs |  4 +++-
683572
 t/lib/dbmt_common.pl       | 35 +++++++++++++++++++++++++++++++++++
683572
 5 files changed, 69 insertions(+), 20 deletions(-)
683572
683572
diff --git a/ext/GDBM_File/GDBM_File.xs b/ext/GDBM_File/GDBM_File.xs
683572
index 33e08e2..7160f54 100644
683572
--- a/ext/GDBM_File/GDBM_File.xs
683572
+++ b/ext/GDBM_File/GDBM_File.xs
683572
@@ -13,6 +13,7 @@
683572
 #define store_value 3
683572
 
683572
 typedef struct {
683572
+	tTHX    owner;
683572
 	GDBM_FILE 	dbp ;
683572
 	SV *    filter[4];
683572
 	int     filtering ;
683572
@@ -98,6 +99,7 @@ gdbm_TIEHASH(dbtype, name, read_write, m
683572
 	}
683572
 	if (dbp) {
683572
 	    RETVAL = (GDBM_File)safecalloc(1, sizeof(GDBM_File_type));
683572
+	    RETVAL->owner = aTHX;
683572
 	    RETVAL->dbp = dbp;
683572
 	} else {
683572
 	    RETVAL = NULL;
683572
@@ -118,12 +120,14 @@ gdbm_DESTROY(db)
683572
 	PREINIT:
683572
 	int i = store_value;
683572
 	CODE:
683572
-	gdbm_close(db);
683572
-	do {
683572
-	    if (db->filter[i])
683572
-		SvREFCNT_dec(db->filter[i]);
683572
-	} while (i-- > 0);
683572
-	safefree(db);
683572
+	if (db && db->owner == aTHX) {
683572
+	    gdbm_close(db);
683572
+	    do {
683572
+		if (db->filter[i])
683572
+		    SvREFCNT_dec(db->filter[i]);
683572
+	    } while (i-- > 0);
683572
+	    safefree(db);
683572
+	}
683572
 
683572
 #define gdbm_FETCH(db,key)			gdbm_fetch(db->dbp,key)
683572
 datum_value
683572
diff --git a/ext/NDBM_File/NDBM_File.xs b/ext/NDBM_File/NDBM_File.xs
683572
index 52e60fc..af223e5 100644
683572
--- a/ext/NDBM_File/NDBM_File.xs
683572
+++ b/ext/NDBM_File/NDBM_File.xs
683572
@@ -33,6 +33,7 @@ END_EXTERN_C
683572
 #define store_value 3
683572
 
683572
 typedef struct {
683572
+	tTHX    owner;
683572
 	DBM * 	dbp ;
683572
 	SV *    filter[4];
683572
 	int     filtering ;
683572
@@ -71,6 +72,7 @@ ndbm_TIEHASH(dbtype, filename, flags, mode)
683572
 	    RETVAL = NULL ;
683572
 	    if ((dbp =  dbm_open(filename, flags, mode))) {
683572
 	        RETVAL = (NDBM_File)safecalloc(1, sizeof(NDBM_File_type));
683572
+		RETVAL->owner = aTHX;
683572
 		RETVAL->dbp = dbp ;
683572
 	    }
683572
 	    
683572
@@ -84,12 +86,14 @@ ndbm_DESTROY(db)
683572
 	PREINIT:
683572
 	int i = store_value;
683572
 	CODE:
683572
-	dbm_close(db->dbp);
683572
-	do {
683572
-	    if (db->filter[i])
683572
-		SvREFCNT_dec(db->filter[i]);
683572
-	} while (i-- > 0);
683572
-	safefree(db);
683572
+	if (db && db->owner == aTHX) {
683572
+	    dbm_close(db->dbp);
683572
+	    do {
683572
+		if (db->filter[i])
683572
+		    SvREFCNT_dec(db->filter[i]);
683572
+	    } while (i-- > 0);
683572
+	    safefree(db);
683572
+	}
683572
 
683572
 #define ndbm_FETCH(db,key)			dbm_fetch(db->dbp,key)
683572
 datum_value
683572
diff --git a/ext/ODBM_File/ODBM_File.xs b/ext/ODBM_File/ODBM_File.xs
683572
index d1ece7f..f7e00a0 100644
683572
--- a/ext/ODBM_File/ODBM_File.xs
683572
+++ b/ext/ODBM_File/ODBM_File.xs
683572
@@ -49,6 +49,7 @@ datum	nextkey(datum key);
683572
 #define store_value 3
683572
 
683572
 typedef struct {
683572
+	tTHX    owner;
683572
 	void * 	dbp ;
683572
 	SV *    filter[4];
683572
 	int     filtering ;
683572
@@ -137,6 +138,7 @@ odbm_TIEHASH(dbtype, filename, flags, mode)
683572
 	    }
683572
 	    dbp = (void*)(dbminit(filename) >= 0 ? &dbmrefcnt : 0);
683572
 	    RETVAL = (ODBM_File)safecalloc(1, sizeof(ODBM_File_type));
683572
+	    RETVAL->owner = aTHX;
683572
 	    RETVAL->dbp = dbp ;
683572
 	}
683572
 	OUTPUT:
683572
@@ -149,13 +151,15 @@ DESTROY(db)
683572
 	dMY_CXT;
683572
 	int i = store_value;
683572
 	CODE:
683572
-	dbmrefcnt--;
683572
-	dbmclose();
683572
-	do {
683572
-	    if (db->filter[i])
683572
-		SvREFCNT_dec(db->filter[i]);
683572
-	} while (i-- > 0);
683572
-	safefree(db);
683572
+	if (db && db->owner == aTHX) {
683572
+	    dbmrefcnt--;
683572
+	    dbmclose();
683572
+	    do {
683572
+		if (db->filter[i])
683572
+		    SvREFCNT_dec(db->filter[i]);
683572
+	    } while (i-- > 0);
683572
+	    safefree(db);
683572
+	}
683572
 
683572
 datum_value
683572
 odbm_FETCH(db, key)
683572
diff --git a/ext/SDBM_File/SDBM_File.xs b/ext/SDBM_File/SDBM_File.xs
683572
index 291e41b..0bdae9a 100644
683572
--- a/ext/SDBM_File/SDBM_File.xs
683572
+++ b/ext/SDBM_File/SDBM_File.xs
683572
@@ -10,6 +10,7 @@
683572
 #define store_value 3
683572
 
683572
 typedef struct {
683572
+	tTHX    owner;
683572
 	DBM * 	dbp ;
683572
 	SV *    filter[4];
683572
 	int     filtering ;
683572
@@ -51,6 +52,7 @@ sdbm_TIEHASH(dbtype, filename, flags, mode)
683572
 	    }
683572
 	    if (dbp) {
683572
 	        RETVAL = (SDBM_File)safecalloc(1, sizeof(SDBM_File_type));
683572
+		RETVAL->owner = aTHX;
683572
 		RETVAL->dbp = dbp ;
683572
 	    }
683572
 	    
683572
@@ -62,7 +64,7 @@ void
683572
 sdbm_DESTROY(db)
683572
 	SDBM_File	db
683572
 	CODE:
683572
-	if (db) {
683572
+	if (db && db->owner == aTHX) {
683572
 	    int i = store_value;
683572
 	    sdbm_close(db->dbp);
683572
 	    do {
683572
diff --git a/t/lib/dbmt_common.pl b/t/lib/dbmt_common.pl
683572
index 5d4098c..a0a4d52 100644
683572
--- a/t/lib/dbmt_common.pl
683572
+++ b/t/lib/dbmt_common.pl
683572
@@ -510,5 +510,40 @@ unlink <Op_dbmx*>, $Dfile;
683572
    unlink <Op1_dbmx*>;
683572
 }
683572
 
683572
+{
683572
+   # Check DBM back-ends do not destroy objects from then-spawned threads.
683572
+   # RT#61912.
683572
+   SKIP: {
683572
+      my $threads_count = 2;
683572
+      skip 'Threads are disabled', 3 + 2 * $threads_count
683572
+        unless $Config{usethreads};
683572
+      use_ok('threads');
683572
+
683572
+      my %h;
683572
+      unlink <Op1_dbmx*>;
683572
+
683572
+      my $db = tie %h, $DBM_Class, 'Op1_dbmx', $create, 0640;
683572
+      isa_ok($db, $DBM_Class);
683572
+
683572
+      for (1 .. 2) {
683572
+         ok(threads->create(
683572
+            sub {
683572
+               $SIG{'__WARN__'} = sub { fail(shift) }; # debugging perl panics
683572
+                        # report it by spurious TAP line
683572
+               1;
683572
+            }), "Thread $_ created");
683572
+      }
683572
+      for (threads->list) {
683572
+         is($_->join, 1, "A thread exited successfully");
683572
+      }
683572
+
683572
+      pass("Tied object survived exiting threads");
683572
+
683572
+      undef $db;
683572
+      untie %h;
683572
+      unlink <Op1_dbmx*>;
683572
+   }
683572
+}
683572
+
683572
 done_testing();
683572
 1;
683572
-- 
683572
1.9.3
683572