aboutsummaryrefslogtreecommitdiff
path: root/opcodes/cgen-opc.c
diff options
context:
space:
mode:
authorNick Alcock <nick.alcock@oracle.com>2024-04-26 18:19:15 +0100
committerNick Alcock <nick.alcock@oracle.com>2024-04-26 21:07:21 +0100
commitd8afb3b974afad68d22e382148c1b3cb990adc46 (patch)
tree6f970dba76d220d97775cd41f7502be6f0b8fa1d /opcodes/cgen-opc.c
parent1df3d1ea79e3024553192ad2845c2e001ee21a23 (diff)
libctf: fix leak of entire dict when dict opening failsusers/nalcock/libctf-open-error-leak
Ever since commit 1fa7a0c24e78e7f ("libctf: sort out potential refcount loops") ctf_dict_close has only freed anything if the refcount on entry to the function is precisely 1. >1 obviously just decrements the refcount, but the linker machinery can sometimes cause freeing to recurse from a dict to another dict and then back to the first dict again, so we interpret a refcount of 0 as an indication that this is a recursive call and we should just return, because a caller is already freeing this dict. Unfortunately there is one situation in which this is not true: the bad: codepath in ctf_bufopen entered when opening fails. Because the refcount is bumped only at the very end of ctf_bufopen, any failure causes ctf_dict_close to be entered with a refcount of zero, and it frees nothing and we leak the entire dict. The solution is to bump the refcount to 1 right before freeing... but this codepath is clearly delicate enough that we need to properly validate it, so we add a test that uses malloc interposition to count allocations and frees, creates a dict, writes it out, intentionally corrupts it (by setting a bunch of bytes after the header to a value high enough that it is definitely not a valid CTF type kind), then tries to open it again and counts the malloc/free pairs to make sure they're matched. (Test run only on *-linux-gnu, because malloc interposition is not a thing you can rely upon working everywhere, and this test is not arch-dependent so if it passes on one arch it can be assumed to pass on all of them.) libctf/ * ctf-open.c (ctf_bufopen): Bump the refcount on failure. * testsuite/libctf-regression/open-error-free.*: New test.
Diffstat (limited to 'opcodes/cgen-opc.c')
0 files changed, 0 insertions, 0 deletions