postgresql/src
Tom Lane 80ef926758 Improve our ability to detect bogus pointers passed to pfree et al.
Commit c6e0fe1f2 was a shade too trusting that any pointer passed
to pfree, repalloc, etc will point at a valid chunk.  Notably,
passing a pointer that was actually obtained from malloc tended
to result in obscure assertion failures, if not worse.  (On FreeBSD
I've seen such mistakes take down the entire cluster, seemingly as
a result of clobbering shared memory.)

To improve matters, extend the mcxt_methods[] array so that it
has entries for every possible MemoryContextMethodID bit-pattern,
with the currently unassigned ID codes pointing to error-reporting
functions.  Then, fiddle with the ID assignments so that patterns
likely to be associated with bad pointers aren't valid ID codes.
In particular, we should avoid assigning bit patterns 000 (zeroed
memory) and 111 (wipe_mem'd memory).

It turns out that on glibc (Linux), malloc uses chunk headers that
have flag bits in the same place we keep MemoryContextMethodID,
and that the bit patterns 000, 001, 010 are the only ones we'll
see as long as the backend isn't threaded.  So we can have very
robust detection of pfree'ing a malloc-assigned block on that
platform, at least so long as we can refrain from using up those
ID codes.  On other platforms, we don't have such a good guarantee,
but keeping 000 reserved will be enough to catch many such cases.

While here, make GetMemoryChunkMethodID() local to mcxt.c, as there
seems no need for it to be exposed even in memutils_internal.h.

Patch by me, with suggestions from Andres Freund and David Rowley.

Discussion: https://postgr.es/m/2910981.1665080361@sss.pgh.pa.us
2022-10-06 21:24:00 -04:00
..
backend Improve our ability to detect bogus pointers passed to pfree et al. 2022-10-06 21:24:00 -04:00
bin meson: Add support for building with precompiled headers 2022-10-06 17:19:30 -07:00
common meson: Add support for building with precompiled headers 2022-10-06 17:19:30 -07:00
fe_utils meson: Add support for building with precompiled headers 2022-10-06 17:19:30 -07:00
include Improve our ability to detect bogus pointers passed to pfree et al. 2022-10-06 21:24:00 -04:00
interfaces meson: Add support for building with precompiled headers 2022-10-06 17:19:30 -07:00
makefiles windows: remove date from version number in win32ver.rc 2022-09-26 11:38:02 -07:00
pl meson: Add support for building with precompiled headers 2022-10-06 17:19:30 -07:00
port meson: Add support for building with precompiled headers 2022-10-06 17:19:30 -07:00
template Move darwin sysroot determination into separate file 2022-09-01 16:54:19 -07:00
test Create subscription stats entry at CREATE SUBSCRIPTION time 2022-10-06 17:17:16 -07:00
timezone meson: Add windows resource files 2022-10-05 09:56:05 -07:00
tools windows: Adjust FD_SETSIZE via commandline define 2022-10-06 13:09:57 -07:00
tutorial Update copyright for 2022 2022-01-07 19:04:57 -05:00
.gitignore
DEVELOPERS
Makefile
Makefile.global.in Split TESTDIR into TESTLOGDIR and TESTDATADIR 2022-09-19 18:03:17 -07:00
Makefile.shlib aix: No need to use mkldexport when we want to export all symbols 2022-09-09 19:11:49 -07:00
meson.build meson: Add initial version of meson based build system 2022-09-21 22:37:17 -07:00
nls-global.mk Fix for make unportability 2022-07-13 09:15:01 +02:00