Summary: | /proc/self/exe is not set properly when valgrind is involved | ||
---|---|---|---|
Product: | [Developer tools] valgrind | Reporter: | smile <smilenglitter> |
Component: | general | Assignee: | Julian Seward <jseward> |
Status: | RESOLVED FIXED | ||
Severity: | normal | CC: | frank.wallingford, rjwalsh |
Priority: | NOR | ||
Version: | 2.1.2 | ||
Target Milestone: | --- | ||
Platform: | unspecified | ||
OS: | Linux | ||
Latest Commit: | Version Fixed In: | ||
Sentry Crash Report: | |||
Attachments: | Patch to make readlink on /proc/self/exe work |
Description
smile
2004-08-10 16:20:48 UTC
I suspect this will be very hard to fix. The problem is that as far as the kernel is concerned, the executable is /usr/bin/valgrind, but that has actually been unmapped once stage2 is loaded. What proc_exe_link() in fs/proc/base.c in the kernel does is look through the target process's memory mappings for one with the VM_EXECUTABLE flag set and then return the file from which that memory is mapped as the target of the link. Because /usr/bin/valgrind has been unmapped it doesn't find such a mapping so returns ENOENT instead. It's possible that making valgrind's ELF loader set MAP_EXECUTABLE when it maps the target program will fix this... Otherwise what we would probably have to do to fix this is to special case the readlink system call to spot these attempts and return a suitable value. It seems that mmap deliberately clears MAP_EXECUTABLE from the passed flags so it is impossible for a user program to cause VM_EXECUTABLE to be set on a mapping. Hence the only solution would be a hack in the readlink system call. Would read() have to be wrapped as well? And readv(), and any similar syscalls? Smile/Madhan, did you cause a problem for you in a real program? (I'm trying to gauge how important it is.) We wouldn't need to wrap read because if you open a symbol link and read from it you are actually reading from the file that is the target of the link. It's only readlink that allows you to see where there link is pointing. This is part of Oracle's routines to get the executable path/name. Later ELF parsing is done to get routine names from PC address for getting stack trace and other routines. Alternative ofcourse is to resort back to the usual way of looking at CWD and PATH and finding the executable. I have used readlink() in the sample program, in the real scenario a direct open() is used. *** Bug 89637 has been marked as a duplicate of this bug. *** Created attachment 7671 [details]
Patch to make readlink on /proc/self/exe work
This (fairly horrible) patch makes readlink on /proc/self/exe (and
/proc/<client-pid>/exe) work by special casing it in the readlink system call
wrapper.
The patch fixes the problem with readlink. Can you also extend this to a direct fopen. Arrgh. I was wrong of course in my previous comment about open because although open does open the target of the link it can only do that if the kernel can manage to resolve the link. Unfortunately I'm not sure that it's going to be sensible to perform the file name mapping done here for all the system calls that work with filenames as it would just get silly. We may just have to leave this as something that can't be simulated fully. I agree with you. The readlink is a usable workaround. Thanks. If it makes sense can we move this to the wishlist. I think Robert fixed this one. readlink returns useful info for /proc/self/exe. I also made open on /proc/self/exe work. Both are hacks, not general solutions, but they'll do for now... |