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