]> granicus.if.org Git - postgresql/commitdiff
Fix dangling smgr_owner pointer when a fake relcache entry is freed.
authorHeikki Linnakangas <heikki.linnakangas@iki.fi>
Fri, 7 Mar 2014 11:25:11 +0000 (13:25 +0200)
committerHeikki Linnakangas <heikki.linnakangas@iki.fi>
Fri, 7 Mar 2014 11:29:49 +0000 (13:29 +0200)
A fake relcache entry can "own" a SmgrRelation object, like a regular
relcache entry. But when it was free'd, the owner field in SmgrRelation
was not cleared, so it was left pointing to free'd memory.

Amazingly this apparently hasn't caused crashes in practice, or we would've
heard about it earlier. Andres found this with Valgrind.

Report and fix by Andres Freund, with minor modifications by me. Backpatch
to all supported versions.

src/backend/access/transam/xlogutils.c
src/backend/storage/smgr/smgr.c
src/include/storage/smgr.h

index 17cf1cdff4d1b6db49c8bfd2cffa9964dbb88f99..3593a4e384da956774a4960743b4f4ba5aab5fa1 100644 (file)
@@ -421,6 +421,9 @@ CreateFakeRelcacheEntry(RelFileNode rnode)
 void
 FreeFakeRelcacheEntry(Relation fakerel)
 {
+       /* make sure the fakerel is not referenced by the SmgrRelation anymore */
+       if (fakerel->rd_smgr != NULL)
+               smgrclearowner(&fakerel->rd_smgr, fakerel->rd_smgr);
        pfree(fakerel);
 }
 
index a950b853235ce797eae11d64825685f851be672c..51d966300d413625118da7d598b21e27ec9276aa 100644 (file)
@@ -86,6 +86,7 @@ static SMgrRelation first_unowned_reln = NULL;
 
 /* local function prototypes */
 static void smgrshutdown(int code, Datum arg);
+static void add_to_unowned_list(SMgrRelation reln);
 static void remove_from_unowned_list(SMgrRelation reln);
 
 
@@ -176,9 +177,8 @@ smgropen(RelFileNode rnode, BackendId backend)
                for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
                        reln->md_fd[forknum] = NULL;
 
-               /* place it at head of unowned list (to make smgrsetowner cheap) */
-               reln->next_unowned_reln = first_unowned_reln;
-               first_unowned_reln = reln;
+               /* it has no owner yet */
+               add_to_unowned_list(reln);
        }
 
        return reln;
@@ -193,7 +193,7 @@ smgropen(RelFileNode rnode, BackendId backend)
 void
 smgrsetowner(SMgrRelation *owner, SMgrRelation reln)
 {
-       /* We don't currently support "disowning" an SMgrRelation here */
+       /* We don't support "disowning" an SMgrRelation here, use smgrclearowner */
        Assert(owner != NULL);
 
        /*
@@ -215,6 +215,40 @@ smgrsetowner(SMgrRelation *owner, SMgrRelation reln)
        *owner = reln;
 }
 
+/*
+ * smgrclearowner() -- Remove long-lived reference to an SMgrRelation object
+ *                                        if one exists
+ */
+void
+smgrclearowner(SMgrRelation *owner, SMgrRelation reln)
+{
+       /* Do nothing if the SMgrRelation object is not owned by the owner */
+       if (reln->smgr_owner != owner)
+               return;
+
+       /* unset the owner's reference */
+       *owner = NULL;
+
+       /* unset our reference to the owner */
+       reln->smgr_owner = NULL;
+
+       add_to_unowned_list(reln);
+}
+
+/*
+ * add_to_unowned_list -- link an SMgrRelation onto the unowned list
+ *
+ * Check remove_from_unowned_list()'s comments for performance
+ * considerations.
+ */
+static void
+add_to_unowned_list(SMgrRelation reln)
+{
+       /* place it at head of the list (to make smgrsetowner cheap) */
+       reln->next_unowned_reln = first_unowned_reln;
+       first_unowned_reln = reln;
+}
+
 /*
  * remove_from_unowned_list -- unlink an SMgrRelation from the unowned list
  *
index 9a135cb5a204f3d65b3a1e23aa505e735083993e..d2db5f6826dc08a5256254ce304a5e66780153d6 100644 (file)
@@ -82,6 +82,7 @@ extern void smgrinit(void);
 extern SMgrRelation smgropen(RelFileNode rnode, BackendId backend);
 extern bool smgrexists(SMgrRelation reln, ForkNumber forknum);
 extern void smgrsetowner(SMgrRelation *owner, SMgrRelation reln);
+extern void smgrclearowner(SMgrRelation *owner, SMgrRelation reln);
 extern void smgrclose(SMgrRelation reln);
 extern void smgrcloseall(void);
 extern void smgrclosenode(RelFileNodeBackend rnode);