Using Valgrind to detect faulty buffer accesses (no pin or buffer content lock held)
Peter Geoghegan <pg@bowt.ie>
From: Peter Geoghegan <pg@bowt.ie>
To: Andres Freund <andres@anarazel.de>,
Alvaro Herrera <alvherre@2ndquadrant.com>, Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Date: 2020-04-25T01:37:44Z
Lists: pgsql-hackers
Attachments
- v1-0001-Add-Valgrind-buffer-access-instrumentation.patch (application/octet-stream) patch v1-0001
- v1-0002-Add-nbtree-Valgrind-buffer-lock-checks.patch (application/octet-stream) patch v1-0002
- v1-0003-Fix-second-page-deletion-bug.patch (application/octet-stream) patch v1-0003
- v1-0004-Fix-third-page-deletion-bug.patch (application/octet-stream) patch v1-0004
I recently expressed an interest in using Valgrind memcheck to detect access to pages whose buffers do not have a pin held in the backend, or do not have a buffer lock held (the latter check makes sense for pages owned by index access methods). I came up with a quick and dirty patch, that I confirmed found a bug in nbtree VACUUM that I spotted randomly: https://postgr.es/m/CAH2-Wz=WRu6NMWtit2weDnuGxdsWeNyFygeBP_zZ2Sso0YAGFg@mail.gmail.com (This is a bug in commit 857f9c36cda.) Alvaro wrote a similar patch back in 2015, that I'd forgotten about but was reminded of today: https://postgr.es/m/20150723195349.GW5596@postgresql.org I took his version (which was better than my rough original) and rebased it -- that's attached as the first patch. The second patch is something that takes the general idea further by having nbtree mark pages whose buffers lack a buffer lock (that may or may not have a buffer pin) as NOACCESS in a similar way. This second patch detected two more bugs in nbtree page deletion by running the regression tests with Valgrind memcheck. These additional bugs are probably of lower severity than the first one, since we at least have a buffer pin (we just don't have buffer locks). All three bugs are very similar, though: they all involve dereferencing a pointer to the special area of a page at a point where the underlying buffer is no longer safe to access. The final two patches fix the two newly discovered bugs -- I don't have a fix for the first bug yet, since that one is more complicated (and probably more serious). The regression tests run with Valgrind will complain about all three bugs if you just apply the first two patches (though you only need the first patch to see a complaint about the first, more serious bug when the tests are run). -- Peter Geoghegan
Commits
-
Add nbtree Valgrind buffer lock checks.
- 4a70f829d86c 14.0 landed
-
Add Valgrind buffer access instrumentation.
- 1e0dfd166b3f 14.0 landed
-
Fix bug in nbtree VACUUM "skip full scan" feature.
- b0229f26da75 13.0 cited
-
Fix another minor page deletion buffer lock issue.
- 7154aa16a64d 13.0 landed
-
Fix minor nbtree page deletion buffer lock issue.
- fa7ff642c22c 13.0 landed
-
Skip full index scan during cleanup of B-tree indexes when possible
- 857f9c36cda5 11.0 cited