5811 Commits

Author SHA1 Message Date
Paul Floyd
d320fc123b FreeBSD: clang-tidy corrections 2023-01-08 17:52:48 +01:00
Philippe Waroquiers
39a063513c Implement front end GDB commands for Valgrind gdbserver monitor commands.
This commit implements in python a set of GDB commands corresponding to the
Valgrind gdbserver monitor commands.

Basically, the idea is that one GDB command is defined for each valgrind
gdbserver subcommand and will generate and send a monitor command to valgrind.

The python code is auto-loaded by GDB as soon as GDB observes that the valgrind
preload core shared lib is loaded (e.g. vgpreload_core-amd64-linux.so).
This automatic loading is done thanks to the .debug_gdb_scripts section
added in vg_preloaded.c file.

Sadly, the auto-load only happens once valgrind has started to execute the code
of ld that loads this vg_preload file.

I have tried 2 approaches to have the python code auto-loaded when attaching at
startup to valgrind:
  * have valgrind gdbserver reporting first to GDB that the executable file is
    the tool executable (with a .debug_gdb_scripts section) and then reporting
    the real (guest) executable file.
    The drawback of this approach is that it triggers a warning/question in GDB
    according to the GDB setting 'set exec-file-mismatch'.
  * have valgrind gdbserver pretending to be multiprocess enabled, and report
    a fake process using the tool executable with a .debug_gdb_scripts section.
    The drawback of this is that this always creates a second inferior in GDB,
    which will be confusing.

Possibly, we might complete the below message :
  ==2984378== (action at startup) vgdb me ...
  ==2984378==
  ==2984378== TO DEBUG THIS PROCESS USING GDB: start GDB like this
  ==2984378==   /path/to/gdb /home/philippe/valgrind/littleprogs/some_mem
  ==2984378== and then give GDB the following command
  ==2984378==   target remote | /home/philippe/valgrind/git/improve/Inst/libexec/valgrind/../../bin/vgdb --pid=2984378
  ==2984378== --pid is optional if only one valgrind process is running

with:
  ==2984378== GDB valgrind python specific commands will be auto-loaded when execution begins.
  ==2984378== Alternatively, you might load it before with the GDB command:
  ==2984378==   source /abs/path/to/valgrind/install/libexec/valgrind/valgrind-monitor.py

The following GDB setting traces the monitor commands sent by a GDB valgrind
command to the valgrind gdbserver:
  set debug valgrind-execute-monitor on

How to use the new GDB valgrind commands?
-----------------------------------------

The usage of the GDB front end commands is compatible with the
monitor command as accepted today by Valgrind.

For example, the memcheck monitor command "xb' has the following usage:
 xb <addr> [<len>]

With some piece of code:
   'char some_mem [5];'
xb can be used the following way:
  (gdb) print &some_mem
  (gdb) $2 = (char (*)[5]) 0x1ffefffe8b
  (gdb) monitor xb 0x1ffefffe8b 5
  		    ff	  ff	  ff	  ff	  ff
  0x4A43040:	  0x00	0x00	0x00	0x00	0x00
  (gdb)

The same action can be done with the new GDB 'memcheck xb' command:
  (gdb) memcheck xb 0x1ffefffe8b 5
  		    ff	  ff	  ff	  ff	  ff
  0x1FFEFFFE8B:	  0x00	0x00	0x00	0x00	0x00
  (gdb)

At this point, you might ask yourself: "what is the interest ?".

Using GDB valgrind commands provides several advantages compared to
the valgrind gdbserver monitor commands.

Evaluation of arguments by GDB:
-------------------------------
For relevant arguments, the GDB command will evaluate its arguments using
the usual GDB evaluation logic, for example, instead of printing/copying
the address and size of 'some_mem', the following will work:
  (gdb) memcheck xb &some_mem sizeof(some_mem)
  		    ff	  ff	  ff	  ff	  ff
  0x1FFEFFFE8B:	  0x00	0x00	0x00	0x00	0x00
  (gdb)

or:
  (gdb) p some_mem
  $4 = "\000\000\000\000"
  (gdb) memcheck xb &$4
  		    ff	  ff	  ff	  ff	  ff
  0x1FFEFFFE8B:	  0x00	0x00	0x00	0x00	0x00
  (gdb)

This is both easier to use interactively and easier to use in GDB scripts,
as you can directly use variable names in the GDB valgrind commands.

Command completion by GDB:
--------------------------
The usual command completion in GDB will work for the GDB valgrind commands.
For example, typing TAB after the letter 'l' in:
  (gdb) valgrind v.info l
will show the 2 "valgrind v.info" subcommands:
  last_error  location
  (gdb) valgrind v.info l

Note that as usual, GDB will recognise a command as soon as it is unambiguous.

Usual help and apropos support by GDB:
--------------------------------------
The Valgrind gdbserver provides an online help using:
  (gdb) monitor help
However, this gives the help for all monitor commands, and is not searchable.
GDB provides a better help and documentation search.
For example, the following commands can be used to get various help
or search the GDB Valgrind command online documentation:
   help valgrind
   help memcheck
   help helgrind
   help callgrind
   help massif
to get help about the general valgrind commands or the tool specific commands.

Examples of searching the online documentation:
  apropos valgrind.*location
  apropos -v validity
  apropos -v leak

User can define aliases for the valgrind commands:
--------------------------------------------------
The following aliases are predefined:
  v and vg for valgrind
  mc for memcheck
  hg for helgrind
  cg for callgrind
  ms for massif

So, the following will be equivalent:
   (gdb) valgrind v.info location &some_mem
   (gdb) v v.i lo &some_mem
   (gdb) alias Vl = valgrind v.info location
   (gdb) Vl &some_mem

Thanks to Hassan El Karouni for the help in factorising the python
code common to all valgrind python commands using a decorator.
2023-01-08 16:00:57 +01:00
Paul Floyd
2c93baf53a Bug 433873 - openat2 syscall unimplemented on Linux
This syscall is not exposed via glibc, so there is only an
addition to the scalar test.
2023-01-06 21:27:01 +01:00
Julian Seward
1b8d0cbc81 Fix 64-bit uncleanness in VG_(get_bbs_translated)/VG_(get_bbs_discarded_or_dumped) ..
.. and some debug printing associated with them.  I don't think this affects
anything apart from debug printing.  Noticed when running a x86 (32-bit)
Firefox build.
2023-01-04 17:12:21 +01:00
Philippe Waroquiers
c8bb6a62ca Add clo option -scheduling-quantum=<number> to control scheduler time slice.
This option can be useful when tracking race conditions which are sensitive
to thread scheduling.
2022-12-30 16:28:23 +01:00
Paul Floyd
227fa1d53b Cleanup for clangd, mostly FreeBSD
Consistently use braces. Make some bitwise expressions use unsigned.
Use some named variables for magic numbers.
2022-12-25 18:36:20 +01:00
Paul Floyd
d8cc70f6d5 FreeBSD only: fix 445743
Restart 3 of the umtx_op mutex operations if they are interrupted.
2022-12-25 10:18:51 +01:00
Paul Floyd
29cfa77b23 FreeBSD: enable PERF_FAST_LOADV for x86 and cleanup for clangd
clangd doesn't like asm, prefers __asm__
some hicpp-braces-around-statements fixes
2022-12-25 09:25:01 +01:00
Paul Floyd
2de91d914c Bug 444488 - Use glibc.pthread.stack_cache_size tunable
Try to use GLIBC_TUNABLES to disable the pthread stack
cache.
2022-12-23 16:51:15 +01:00
Philippe Waroquiers
7e03a15d8d Pass a dummy process_option_state for dynamic options
The process_option_state is functionally needed during initial parsing
of CLO options.  When later changing them, only changing the CLO
itself is good enough.
But the processing of option needs to have a state.
2022-12-23 16:15:23 +01:00
Paul Floyd
8b0105ba63 Improve FreeBSD preadv/pwritev error messages
Also correct a copy paste error in generic readv.
2022-12-22 23:38:54 +01:00
Paul Floyd
5b52408406 Bug 462830 - WARNING: unhandled amd64-freebsd syscall: 474
Add syscall wrappers for sctp_generic_recvmsg and sctp_generic_sendmsg
on FreeBSD.
2022-12-22 23:08:45 +01:00
Paul Floyd
35bb01dd45 FreeBSD: make amd64 aspace max mem 128Gbytes 2022-12-22 09:30:45 +01:00
Alexandra Petlanova Hajkova
ea91997394 vgdb: allow address reuse to avoid "address already in use" errors
https://bugs.kde.org/show_bug.cgi?id=459476
2022-11-12 13:03:46 +01:00
Paul Floyd
f2550057e1 Bug 170510 - Don't warn about ioctl of size 0 without direction hint
Apply this to generic and update the message on all platforms.
2022-11-10 22:31:07 +01:00
Paul Floyd
4ff2185f45 FreeBSD: remove dependency on elf header and make VKI_ copies of AT defines
Also prepare NEWS and configure.ac for 3.21.0
2022-10-28 16:52:50 +02:00
Paul Floyd
328ece8463 Fix DRD and Helgrind on Solaris.
It seems as though Solaris RW sections can also have the
execute flag set. Checking for RW and !X was causing the
debuginfo reading to fail. That meant that the helgrind and
drd preload shared libraries weren't processed, and also
the rtld bind function pointers not setup. Without the rtld bind
function an assert fires and Helgrind and DRD abort.
2022-10-23 15:29:40 +02:00
Paul Floyd
0ea3746e97 Fix build on macOS
A while back when I added support for split RW PT_LOAD sections
one instance in the macho code didn't get updated. Also
update the comment that refers to the old struct member that
got renamed.
2022-10-20 23:15:00 +02:00
Paul Floyd
12d2a3c85d Auxv user stack for FreeBSD, last part
Clean up, didn't need an extra func for user stack limit.
2022-10-19 00:51:05 +02:00
Paul Floyd
802f2d21d9 Auxv user stack for FreeBSD, part 2
Try to set the stack limit.
2022-10-18 22:11:32 +02:00
Paul Floyd
121374b287 Add auxv entry for usrstack on FreeBSD 14, part 1
Previously the user stack was obtained using the kern.usrstack
sysctl. This has been moved to auxv in FreeBSD 14. Without
this change all programs linked with libthr fail with a panic
when they fail to get a valid user stack address.

Note also in FreeBSD 14 ASLR has been enabled. This means that
there is now some extra difference between the address layout of
a standalone executable and the same executable under valgrind.

Pre-FreeBSD 14 and under valgrind:
lib rtld is loaded after the executable (though a much smaller gap
inder valgrind)
user stack starts at 0x7ffffffff000

FreeBSD 14
lib rtld is loaded at a much higher address, around 0xeeeecc15000
user stack is at a much lower address, around 0x82073d000

This means that valgrind behaves somewhat as thogh the
kern.elf(64|32).aslr.stack sysctl were set to 0.

Some more work will be needed for the stack size.
There are no plans at the moment to match the FreeBSD 14 memory
layout.
2022-10-18 23:38:36 +02:00
Paul Floyd
b31ff3321e Improve FreeBSD handling of W^X setting.
FreeBSD has a sysctl that prevents mmapping regions
that are both Write and Exec. This setting prevents
Valgrind from workig correctly.

This change uses ELF tags to turn off W^X control,
and also removes a sysctl check for the same flag.

Patch contributor:
0a3a794143
2022-10-16 18:58:52 +02:00
Philippe Waroquiers
348775f34b Remove register cache to fix 458915 gdbserver causes wrong syscall return
The valgrind gdbserver inheritated a register cache from the original
GDBserver implementation.
The objective of this register cache was to improve the performance
of GDB-> gdbserver -> inferior by avoiding the gdbserver having to
do ptrace system calls each time GDB wants to read or write a register
when the inferior is stopped.

This register cache is however not useful for the valgrind gdbserver:
As the valgrind gdbserver being co-located with the inferior, it
can directly and efficiently read and write registers from/to the VEX
state.

This commit ensures the valgrind GDBserver directly reads from
VEX state instead of fetching the registers from the VEX state and
copying them to the gdbserver regcache.

Similarly, when GDB wants to modify a register, the valgrind GDB server now
directly writes into the VEX state instead of writing the registers
in the regcache and having the regcache flushed to the VEX state
when execution is resumed.

The files regcache.h and regcache.c are still useful as they provide
a translation between a register number, a register name on one side
and the offset in an array of bytes in the format expected by GDB.
The regcache now is only used to create this array of bytes, which is
itself only used temporarily when GDB reads or writes the complete
set of registers instead of reading/writing one register at a time.

Removing the usage of this regcache avoids the bug 458915.
The regcache was causing the bug in the following circumstances:
We have a thread executing code, while we have a bunch of threads
that are blocked in a syscall.
When a thread is blocked in a syscall, the VEX rax register is set to the
syscall nr.
A thread executing code will check from time to time if GDB tries to
attach.
When GDB attaches to the valgrind gdbserver , the thread executing code
will copy the registers from all the threads to the thread gdbserver regcache.
However, the threads blocked in a system call can be unblocked e.g.
because the epoll_wait timeout expires. In such a case, the thread will
still execute the few instructions that follow the syscall instructions
till the thread is blocked trying to acquire the scheduler lock.
These instructions are extracting the syscall return code from the host
register and copies it to the valgrind VEX state.
However, this assembly code is not aware that there is a gdbserver cache.
When the unblocked thread is on the acquire lock statement,
the GDB server regcache is now inconsistent (i.e. different from) the
real VEX state.
When finally GDB tells GDB server to continue execution, the GDB server
wrongly detected that its regcache was modified compared to the VEX state:
the regcache still contains e.g. for the rax register the syscall number
while the unblocked thread has put the syscall return code in the VEX
rax register. GDBserver then flushed the regcache rax (containing the
syscall number) to the VEX rax.
And that led to the detected bug that the syscall return code seen by
the guest application was the syscall number.

Removing the regcache ensures that GDB directly reads the values
from VEX and directly writes to VEX state.

Note that we could still have GDB reading from VEX a register value
that will be changed a few instructions later.
GDB will then show some (slightly) old/obsolete values
for some registers to the user.
This should have no consequence as long as GDB does not try to modify
the registers to execute an inferior call.

The bug did not happen systematically as most of the time, when threads are
blocked in syscalls, vgdb attaches using ptrace to the valgrind process.
When vgdb attaches with ptrace, it stops all the threads using linux syscall.
When vgdb stops the threads, the threads blocked in a syscall will not
execute the instructions between the syscall instruction and the lock
acquire, and so the problem of desynchronisation between the VEX state
and the register cache could not happen.

This commit touches architecture specific files of the gdbserver,
it has been tested on amd64/debian, on pcc64/centos and on arm64/ubuntu.
Possibly, some untested arch might not compile but the fix should be trivial.
2022-10-16 00:44:40 +02:00
Paul Floyd
2dde5c405b Bug 131186 - writev reports error in (vector[...])
Use the index rather than ...
Also done for readv.
2022-10-12 08:34:51 +02:00
Philippe Waroquiers
3c57204534 Fix 459477 missing \n in XERROR and ERROR calls
Based on a patch by Alexandra Hajkova.
2022-09-25 13:13:31 +02:00
Philippe Waroquiers
e489f31974 Add abexit in --vgdb-stopat. fix 459031 --error-exitcode doc. Add lwpid in thread_wrapper tracing.
Note that this modifies files on darwin/solaris/bsd but I only did a linux
build so possibly this commit might cause a compilation error, that should
then be trivial to fix.

Also added memmem test in the list of ignored files.
2022-09-17 22:54:05 +02:00
Paul Floyd
498b5c4286 Fix reading dwarf info for arrays with lower bound and count
There was an out-by-one error, the upper bound was being set to
count whereas it should be count - 1.

This was causing drd/tests/atomic_var to fail because the g_dummy
array seemed to overlap the following s_y variable.

This seems only to have been caused by clang, not GCC, which
presumably supplies lower and upper bound rather than lower
bound and count.
2022-07-05 12:53:34 +02:00
Paul Floyd
332946eac4 Remove a hack for read dwarf var size on FreeBSD
The need for this are lost in the mists of time. It no longer
has any effect.
2022-07-05 12:51:44 +02:00
Paul Floyd
5d058e6332 Backout synthesizing AT_BSDFLAGS auxv entry
This was causing a crash on several FreeBSD 13.1 testcases
(but not 13.0). Probably related to "sig fastblock".
2022-07-03 21:28:57 +02:00
Paul Floyd
db0bfcc6a5 Fix a couple of compiler warnings because of some missed size_t -> SizeT changes. 2022-07-03 16:25:10 +02:00
Paul Floyd
b29a1e1cf5 Improve FreeBSD sysctl kern.usrstack
This was handled by sysctl but not sysctlbyname.
The value returned was wrong.
Added a regtest.
2022-07-03 15:12:20 +02:00
Paul Floyd
9f27d8fbc7 Bug-456171 [PATCH] FreeBSD: Don't record address errors when accessing the 'kern.ps_strings' sysctl struct
There is quite a lot of stuff here.

The problem is that setproctitle and kern.ps_strings were using the Valgrind host auxv
rather than the guest. The proposed patch would have just ignored those memory ranges.

I've gone a fair bit further than that
1. refactored the initimg code for building the client auxv. Previously we were
   simply ignoring any non-scalar entries. Now we copy most of thse as well.
   That means that 'strtab' built on the client stack no longet only contains
   strings, at can also now contain binary structures. Note I was a bit
   concerned that there may be some alignment issues, but I haven't seen any
   problems so far.
2. Added intercepts to sysctl and sysctlbyname for kern.ps_strings, then find
   AT_PS_STRINGS from the client auxv that is now usable from step 1.
3. Some refactoring of sysctl and sysctlbyname syscall wrappers. More to do
   there!
4. Added a setproctitle testcase (that also tests the sysctls).
5. Updated the auxv testcase now that more AT_* entries are handled.
2022-07-03 13:05:54 +02:00
Paul Floyd
3f5fcd59da Fix a few issues with reallocf and add a FreeBSD amd64 regtest
1. new_size is size_t (unsigned) and can't be negative
2. NULL not returned when the size is 0 and the memory freed
3. set ENOMEM if the allocation fails (but this time NULL does
   get returned)
2022-06-17 13:52:45 +02:00
Paul Floyd
a108669a18 Implement vgdb invoker on FreeBSD
This is a translation of the Linux vgdb-invoker-ptrace.c
to the FreeBSD ptrace dialect. It seems to be basically
functional (3 out of 4 of the regression tests pass,
and for the 4th one it seems to be a limitation of
ptrace on FreeBSD that it can cause syscalls to be
interrupted).
2022-06-14 22:39:31 +02:00
Luboš Luňák
026cda6c81 support DW_FORM_addrx3 and DW_FORM_strx3
Apparently these may get used after all with large enough binaries,
despite being somewhat tricky with regard to endianess.
2022-06-14 21:11:35 +02:00
Luboš Luňák
4bb0164e6b implement support for missing DW_LLE_* and DW_RLE_* values 2022-06-14 20:47:02 +02:00
Luboš Luňák
e95904b99c treat DW_TAG_skeleton_unit like DW_TAG_compile_unit
It's basically the same, except for being split-dwarf. Handling
it is required e.g. for reading line info.
2022-06-13 22:07:40 +02:00
Luboš Luňák
d19bbdf120 read dwarf5 DW_FORM_rnglistx and DW_FORM_loclistx
The .debug_rnglists/.debug_loclists sections first have a list of offsets
and then a list of the actual data that those offsets point to.
2022-06-13 19:06:10 +02:00
Luboš Luňák
383f36462f avoid warning about missing DW_AT_*_base in skip_DIE()
Similarly to setup_cu_bases(), DW_FORM_addrx etc. may depend
on DW_AT_addr_base etc. that have not been read yet.
2022-06-13 18:12:14 +02:00
Luboš Luňák
61dfba4232 read dwarf5 DW_FORM_addrx* and DW_FORM_strx* as generated by Clang14
DW_FORM_addrx* are offsets into .debug_addr containing addresses.
DW_FORM_strx* are offsets into .debug_str_offsets, which contain
offsets into .debug_str. Support for these also requires reading
DW_AT_addr_base and DW_AT_str_offsets_base before any other field
in the abbrev table entry, as those may use this form.
2022-06-13 16:47:16 +02:00
Paul Floyd
145fb72dbf Fix some compiler warnings on FreeBSD
Includes one serious issue of a switch case fallthrough
2022-06-12 15:48:32 +02:00
Mark Wielaard
3aee8a2944 syswrap-linux.c (sys_execveat): Define path as const HChar *
Like buf, path (ARG2) is a const HChar *
Prevents a gcc warning: assignment discards 'const' qualifier from
                        pointer target type [-Wdiscarded-qualifiers]
13328 |                        path = buf;
      |                             ^
2022-06-09 23:10:30 +02:00
Mark Wielaard
438d5e956d syswrap-linux.c (sys_bpf): Compare raw_tracepoint.name against 0, not NULL
raw_tracepoint.name is a __vki_u64 (even on 32bit arches), so compare
against zero, not NULL, to avoid a gcc warning.
2022-06-09 22:55:23 +02:00
Luboš Luňák
61ddbc1fc3 read properly unit headers depending on dwarf5 unit_type
There may be additional fields that need to be skipped over, otherwise
further reading will interpret these incorrectly.
2022-06-09 22:36:30 +02:00
Paul Floyd
7844752299 Bug 452802 Handle lld 9+ split RW PT_LOAD segments correctly
Many changes mostly related to modifying VG_(di_notify_mmap)( Addr a, Bool allow_SkFileV, Int use_fd )
so that instead of triggering debuginfo reading after seeing one RX PT_LOAD and 1 RW PT_LOAD it
can handle either 1 or 2 RW PT_LOADs.
2022-06-09 22:03:04 +02:00
Paul Floyd
26cfb342f1 A little bit in indentation 2022-06-07 20:38:17 +02:00
Paul Floyd
3ce7673720 Add syscall wrappers for FreeBSD funlinkat and copy_file_range
Aslo add a smoketest for /bin/cp, which uses copy_file_range
2022-06-06 22:37:47 +02:00
Paul Floyd
6cc2d94d93 Use a different way to tell where the syscall handler was interrupted on FreeBSD and macOS
I was using a global variable. This would be set to '1' just before
calling the function to save cflags and cleared just after, then
using the variable to fill in the 'outside_rnage_ condition
in VG_(fixup_guest_state_after_syscall_interrupted)

Even though I haven't experienced any isseus with that, the comments just before
do_syscall_for_client made me want to try an alternative.

This code is very ugly and won't please the language lawyers.
Functions aren't guaranteed to have an address and there is no
guarantee that the binary layout will reflect the source layout.
Sadly C doesn't have something like "sizeof(*function)" to give
the size of a function in bytes. The next best that I could
manage was to use dummy 'marker' functions just after the
ones I want the end address of and then use the address of
'marker - 1'

I did think of one other way to do this. That would be to
generate a C file containing the function sizes. This would
require

1. "put_flag_size.c" would depend on the VEX guest_(x86|amd64)_helpers
   object files
2. Extract the sizes, for instance

echo -n "const size_t x86_put_eflag_c_size = 0x" > put_flag_size.c
nm -F sysv libvex_x86_freebsd_a-guest_x86_helpers.o | awk -F\| '/LibVEX_GuestX86_put_eflag_c/{print $5}' >> put_flag_size.c
echo ";" >> put_flag_size.c

That seems fairly difficult to do in automake and I'm not sure if
it would be robust.
2022-06-06 15:18:29 +02:00
Paul Floyd
eeaf547e6c Fix FreeBSD x86 allocation of GDT for threads on x86
Also update README.freebsd
This is about the last significant failure that is x86 FreeBSD specific.
2022-06-06 11:20:39 +02:00
Paul Floyd
7b0cd3ac28 Add code that makes 1st page after brk inaccessible.
Also remove FreeBSD expected.

Note: this code is also mssing from Solaris and macOS initimg's.
2022-06-02 20:31:12 +02:00