liballocs icon indicating copy to clipboard operation
liballocs copied to clipboard

Optionally avoid large unreserved mappings (for Valgrind/Memcheck, coredumping, etc)

Open stephenrkell opened this issue 2 years ago • 6 comments

Since Valgrind shadows memory eagerly, it blows up when we allocate a large MAP_NORESERVE region, because it tries to eagerly shadow it.

For a long time, we used memtables everywhere and I could not see a reasonable fix. But now we don't use them much... the pageindex is our only huge unreserved mapping.

I had long thought about a special "Valgrind build" that uses a different data structure for the pageindex. However, probably something simpler and cleaner will suffice: map the pageindex lazily. We can install a SIGSEGV handler and do that (and/or how slow would an ensure_mapped() call be at pageindex manipulations?). It works only because we explicitly place all mappings, so we can ensure we never map something else in a place where we may later need to map the pageindex.

stephenrkell avatar Mar 25 '24 14:03 stephenrkell

Having said that, Valgrind itself tries to own the memory map and so we may have a tussle on our hands. Someone needs to try this... probably me, for now.

stephenrkell avatar Mar 27 '24 18:03 stephenrkell

This is also an issue for dumping core. E.g. with ecfs or other core handlers, the kernel wants to generate and pipe a full core file to the handler program. In principle this "works" but the delay of sending petabytes through a pipe is significant (mustn't exaggerate; on x86-64, the pageindex mapping is 2^37 bytes, or 128 gigabytes) and the core file handler is typically not smart enough to create sparse output on the other end.

stephenrkell avatar Sep 03 '25 15:09 stephenrkell

This change is now implemented, although Valgrind is incidentally still not able to run us. The issue seems to be its address space layout falling foul of RELF_MAYBE_ADJUST. Probably we should come up with a robust solution to that. Maybe a piece of the dynamic linker state will be enough to reveal, indirectly, what we need to know? Below is how I run it for the alloca test case.

$ valgrind --vgdb=yes --vgdb-stop-at=startup --trace-children=yes env LD_PRELOAD="/var/local/stephen/work/devel/liballocs.git/build/debug/liballocs_preload.so" ./alloca

==4977== Memcheck, a memory error detector
==4977== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==4977== Using Valgrind-3.19.0 and LibVEX; rerun with -h for copyright info
==4977== Command: env LD_PRELOAD=/var/local/stephen/work/devel/liballocs.git/build/debug/liballocs_preload.so ./alloca
==4977== 
==4977== (action at startup) vgdb me ... 
==4977== 
==4977== TO DEBUG THIS PROCESS USING GDB: start GDB like this
==4977==   /path/to/gdb env
==4977== and then give GDB the following command
==4977==   target remote | /usr/bin/vgdb --pid=4977
==4977== --pid is optional if only one valgrind process is running
==4977== 
valgrind.bin: lazily mapped a piece of pageindex at 0x410fe8600000 (idx 0x7f43)
valgrind.bin: lazily mapped a piece of pageindex at 0x410821e00000 (idx 0x410f)
valgrind.bin: lazily mapped a piece of pageindex at 0x410ab0200000 (idx 0x5581)
valgrind.bin: lazily mapped a piece of pageindex at 0x410fffe00000 (idx 0x7fff)
valgrind.bin: lazily mapped a piece of pageindex at 0x41000a000000 (idx 0x50)
valgrind.bin: Warning: mapping of /tmp/tmp.dlbind.LYTeDG could not extend preceding bigalloc
valgrind.bin: no allocsld, so assuming ld.so malloc is not instrumented
valgrind.bin: warning: no type information for ld.so link maps, so can't apply workarounds
==4977== Memcheck, a memory error detector
==4977== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==4977== Using Valgrind-3.19.0 and LibVEX; rerun with -h for copyright info
==4977== Command: ./alloca
==4977== 
==4977== (action at startup) vgdb me ... 
==4977== 
==4977== TO DEBUG THIS PROCESS USING GDB: start GDB like this
==4977==   /path/to/gdb ./alloca
==4977== and then give GDB the following command
==4977==   target remote | /usr/bin/vgdb --pid=4977
==4977== --pid is optional if only one valgrind process is running
==4977== 
allocsld: Hello from allocsld!
allocsld: We think we are not the program
allocsld: Loader's load address is 0x4000000
allocsld: AT_PHDR is 0x108040
allocsld: AT_PHENT is 0x38
allocsld: AT_PHNUM is 0xd
allocsld: AT_BASE is 0x2000000
allocsld: AT_ENTRY is 0x109340
allocsld: AT_EXECFN is 0x1fff000fee (./alloca)
allocsld: jumping to inferior entry point 0x201aa40 with rsp 0x1ffeffed20
==4977== Invalid read of size 4
==4977==    at 0x51BC415: gnu_hash_lookup (relf.h:855)
==4977==    by 0x51BC75D: symbol_lookup_in_dyn (relf.h:1086)
==4977==    by 0x51BC7DF: symbol_lookup_in_object (relf.h:1095)
==4977==    by 0x51BC92C: fake_dlsym (relf.h:1164)
==4977==    by 0x51BCA36: __liballocs_global_init (init.c:82)
==4977==    by 0x20049CD: ??? (in /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2)
==4977==    by 0x17: ???
==4977==  Address 0xd8b3f28 is not stack'd, malloc'd or (recently) free'd
==4977== 

stephenrkell avatar Sep 04 '25 14:09 stephenrkell

I wonder if we can prevent the ld.so from fixing up the .dynamic section, thereby dispensing with RELF_MAYBE_ADJUST (within liballocs; other clients using relf.h may be stuck with it).

One way would be to force the PT_DYNAMIC to lack PF_W. This is similar to what we do in libdlbind, to interfere with how a given binary is mapped. However, this time we don't want to interfere with mapping per se. Also, glibc's ld.so sometimes reads the program headers by file I/O (see _dl_map_object_from_fd in dl-load.c), perhaps more often than it reads them from a memory mapping, so just acting at mapping time isn't enough.

Making PT_DYNAMIC non-writable also messes with DT_DEBUG, although only the executable has this so it could perhaps be special-cased.

The only other option for dispensing with RELF_MAYBE_ADJUST that I can think of is to detect when l_info is set. If so, it means the adjustment code has run. However, that is a private field so requires reflecting on the ld.so and may break for a non-glibc ld.so.

RELF_MAYBE_ADJUST is only fatally flawed for objects that are loaded at an absolute address that is smaller than their highest virtual address. As long as they are, it is always unambiguous whether an address in these .dynamic fields is unrelocated or relocated. So maybe the invariant we need is to ensure that this always holds. Within liballocs, we clearly do have the means to enforce that.

stephenrkell avatar Sep 04 '25 16:09 stephenrkell

I wonder whether there's a way to estimate the highest virtual address of an object, without requiring the program headers all the time.

stephenrkell avatar Sep 04 '25 16:09 stephenrkell

We currently also still have one large unreserved mapping I was forgetting about, used as the private malloc heap. However, I have already sketched out how to eliminate it (#65) so I think I just need to do that.

stephenrkell avatar Sep 05 '25 16:09 stephenrkell