Re: BUG #16329: Valgrind detects an invalid read when building a gist index with buffering
Tom Lane <tgl@sss.pgh.pa.us>
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Alexander Lakhin <exclusion@gmail.com>
Cc: pgsql-bugs@lists.postgresql.org
Date: 2020-05-05T18:59:17Z
Lists: pgsql-bugs
Alexander Lakhin <exclusion@gmail.com> writes: >> Please look at the patch that modifies the gist regression test to make >> the issue visible and fixes it by avoiding access to the memory context >> that can be reset in gistProcessEmptyingQueue(). > Could you please let me know if the fix is incorrect (or not elaborated > enough to be included in the upcoming releases) or this issue is false > positive? I took a look at this. I see the hazard, but I'm not sure that I like your proposed quick-fix. Essentially, the problem is that gistBuildCallback is supposing that the tuple it creates will survive the execution of its subroutines, which in fact it won't always. But that means that at some point the tuple pointer it's passed down to those subroutines becomes a dangling pointer. What guarantee would we have that the subroutines don't access the tuple after that point? I think the real issue here is confusion about which level of function "owns" the tempCxt and gets to decide when to reset it. We can't have both gistBuildCallback and gistProcessEmptyingQueue doing that. Maybe we need more than one temporary context, or maybe we could just skip the context reset in gistProcessEmptyingQueue and let the storage accumulate till we get back to gistBuildCallback. But in any case it's going to require more analysis. Or, if we do just hack it as you suggest, there had better be a couple of fat comments in gistBufferingBuildInsert warning about the hazards. I was somewhat dismayed to realize from looking at the code coverage report that we have exactly zero test coverage of the gist buffering build code paths today. That's just awful; how did the feature get committed with no test coverage? Your proposed test additions raise the coverage in gistbuild.c and gistbuildbuffers.c to something respectable, at least. But it looks like there are still some potentially-delicate paths that aren't tested, notably the "have to stop buffering" business. Can we do better at reasonable cost, or are those paths just never reached without huge data volume? (Could we make them more reachable by turning down maintenance_work_mem or some other setting?) regards, tom lane
Commits
-
Fix dereference of dangling pointer in GiST index buffering build.
- d2a1d4b190ec 12.15 landed
- b5c6776c1127 11.20 landed
- a1904c9ce56a 14.8 landed
- 8e5eef50c5b4 16.0 landed
- 2dc77adc768e 15.3 landed
- 2adb6adad5f8 13.11 landed
-
Fix GiST buffering build to work when there are included columns.
- 962ab473ec3d 13.1 landed
- 371668a8389d 14.0 landed
- 12945874ebd6 12.5 landed
-
Re-allow testing of GiST buffered builds.
- 78c0b6ed273a 14.0 landed
-
Add support for building GiST index by sorting.
- 16fa9b2b30a3 14.0 cited