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