postgresql/src/backend
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
..
access Fix comment in xlogprefetcher.c 2022-10-06 20:25:02 +09:00
backup Rename shadowed local variables 2022-10-05 21:01:41 +13:00
bootstrap meson: Add initial version of meson based build system 2022-09-21 22:37:17 -07:00
catalog Rename shadowed local variables 2022-10-05 21:01:41 +13:00
commands Rename shadowed local variables 2022-10-05 21:01:41 +13:00
executor Remove uses of MemoryContextContains in nodeAgg.c and nodeWindowAgg.c. 2022-10-06 13:27:34 -04:00
foreign meson: Add initial version of meson based build system 2022-09-21 22:37:17 -07:00
jit meson: Add windows resource files 2022-10-05 09:56:05 -07:00
lib meson: Add initial version of meson based build system 2022-09-21 22:37:17 -07:00
libpq meson: Add initial version of meson based build system 2022-09-21 22:37:17 -07:00
main meson: Add initial version of meson based build system 2022-09-21 22:37:17 -07:00
nodes Revert 56-bit relfilenode change and follow-up commits. 2022-09-28 09:55:28 -04:00
optimizer Rename shadowed local variables 2022-10-05 21:01:41 +13:00
parser Rename shadowed local variables 2022-10-05 21:01:41 +13:00
partitioning Rename shadowed local variables 2022-10-05 21:01:41 +13:00
po meson: Add initial version of meson based build system 2022-09-21 22:37:17 -07:00
port meson: Add initial version of meson based build system 2022-09-21 22:37:17 -07:00
postmaster Message style improvements 2022-09-24 18:41:25 -04:00
regex Remove uses of register due to incompatibility with C++17 and up 2022-09-24 12:08:37 -07:00
replication meson: Add windows resource files 2022-10-05 09:56:05 -07:00
rewrite meson: Add initial version of meson based build system 2022-09-21 22:37:17 -07:00
snowball meson: Add support for building with precompiled headers 2022-10-06 17:19:30 -07:00
statistics Rename shadowed local variables 2022-10-05 21:01:41 +13:00
storage Take care to de-duplicate entries in standby.c's table of locks. 2022-10-06 12:27:36 -04:00
tcop Add optional parameter to PG_TRY() macros 2022-10-06 10:08:31 +13:00
tsearch Introduce t_isalnum() to replace t_isalpha() || t_isdigit() tests. 2022-10-06 11:08:56 -04:00
utils Improve our ability to detect bogus pointers passed to pfree et al. 2022-10-06 21:24:00 -04:00
.gitignore
Makefile Move gramparse.h to src/backend/parser 2022-09-14 10:57:13 +07:00
common.mk Blind attempt to fix LLVM dependency in the backend 2022-09-15 10:53:48 +07:00
meson.build meson: Add support for building with precompiled headers 2022-10-06 17:19:30 -07:00
nls.mk NLS: Put list of available languages into LINGUAS files 2022-07-13 08:19:17 +02:00