1279 Commits

Author SHA1 Message Date
Paul Floyd
cb7adf4a9e Bug 449309 - Missing loopback device ioctl(s)
Patch provided by:
    Faheem Sheikh <faheem.sheikh@bmw.de>

Note that the testcase provided with this report hasn't
been added as it requires privileges.
2023-03-05 08:42:42 +01:00
Paul Floyd
94a41e050c Bug 445743 - "The impossible happened: mutex is locked simultaneously by two threads" while using mutexes with priority inheritance and signals
Been using the same fix in FreeBSD for quite a while, so I think
it's safe to apply to Linux and close the bugzi item.
2023-02-28 22:17:25 +01:00
Paul Floyd
f6f7cae623 FreeBSD mknodat syscall dev arg is 64bits so the syscall needs splitting into amd64 and x86 versions 2023-02-25 16:09:41 +01:00
Paul Floyd
b861458d3d FreeBSD: make rfork() fail more gracefully
rfork() is barely used in base FreeBSD. The main use
is in posix_spawn(). If rfork() fails with EINVAL
then it falls back to using vfork(). This is preferable
to Valgrind bombing.

ksh93 uses posix_spawn. I tested bash and csh and they had
no problems.

Also add 'hello world" smoke tests for bash csh and ksh
2023-02-25 15:16:35 +01:00
Paul Floyd
b1aba91160 FreeBSD: cleanup and refactor syscalls readlink and readlinkat
There was some code to handle /proc/curproc/file (a symlink to
the exe that wee need to bodge as it refers to the tool exe).
But it was neither tested nor working.

Can't use the same technique as Linux and Solaris which have more
complete /proc filesystems where each pid has symlinks for
each open file, which we use for the guest. Instead need to
copy the path ourselves. So move sys_readlink out of generic.

Simplify the handling of the resolved guest exe name - store it in
a global like VG_(args_the_exename).
2023-01-31 21:52:36 +01:00
Paul Floyd
5830ece5cd FreeBSD: update comment about __sysctlbyname and kern.proc.pathname 2023-01-25 08:31:09 +01:00
Paul Floyd
c15993dcb4 FreeBSD: start adding more checks for /proc
Also add a couple of expecteds.
2023-01-22 21:48:51 +01:00
Paul Floyd
a526bbd051 FreeBSD: Fix auxv AT_EXECPATH
This was being copied from the host. Now it's synthesized for
the guest. Also improve the none/freebsd/auxv test to
print a few of the strings in auxv (but not the envp ones).
2023-01-21 20:49:30 +01:00
Paul Floyd
5d38764204 Bug 464476 - Firefox fails to start under Valgrind
On FreeBSD, Firefox uses the kern.proc.pathname.PID sysctl
to get the binary path (where PID can be the actual pid
or -1). The user path is /usr/local/bin/firefox which is
a symlink to /usr/local/lib/firefox/firefox.

This was failing because we were not handling this MIB.
That meant that the sysctl returned the path for the
binary of the running tool (e.g.,
/home/paulf/scratch/valgrind/memcheck/memcheck-amd64-freebsd).
Firefox looks for files in the same directory.
Since it was the wrong directory it failed to find them and
exited.

I also noticed a lot of _umtx_op errors. On analysis they
are spurious. The wake ops take an "obj" argument, a pointer
to a variable. They only use the address as a key for
lookups and don't read the contents.
2023-01-21 17:55:09 +01:00
Paul Floyd
cae2118d95 FreeBSD: some _umtx_op corrections
mutex trylock doesn't user timer arguments
plain mutex lock doesn't need restart

still not sure about mutex trylock
2023-01-15 09:05:55 +01:00
Paul Floyd
d320fc123b FreeBSD: clang-tidy corrections 2023-01-08 17:52:48 +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
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
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
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
802f2d21d9 Auxv user stack for FreeBSD, part 2
Try to set the stack limit.
2022-10-18 22:11:32 +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
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
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
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
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
2b370744e5 Indent and add more comments for FreeBSD syscall code
After working on an issue that turns out to seem to be with the
FreeBSD kernel sched_uler I played a lot with the Valgrind
syscall and scheduler code. I've kept the comments and the
reformatting.
2022-05-30 22:57:34 +02:00
Paul Floyd
034e5d2242 Fixes for FreeBSD pdkill syscall wrapper 2022-05-24 23:40:42 +02:00
Paul Floyd
6cb8e52c34 More changes for FreeBSD 13.1
These concern auxv, swapoff and fcntl F_KINFO

I wanted to use the new fcntl K_INFO to replace the existing
horrible implementation of resolve_filename, but it seems to
have change the behaviour for redirected files. Several
fdleak regtests fail because stdout resolves to an empty
string.
2022-05-14 16:46:03 +02:00
Paul Floyd
d99b369c09 FreeBSD shm_open2 arg5 seems to be optional
This syscall doesn't have a libc interface and manpage so
the documentation is skimpy.
2022-05-09 11:58:45 +02:00
Paul Floyd
3a883a6c1d Add FreeBSD shm_open2 syscall wrapper 2022-05-09 11:05:49 +02:00
Paul Floyd
e23f5b6a14 FreeBSD x86 small logging changes and add comments explaining why none/tests/manythreads fails 2022-05-07 17:30:23 +02:00
Di Chen
fca4a3d8e5 Support new memfd_secret linux syscall (447)
memfd_secret is a new syscall in linux 5.14. memfd_secret() is
disabled by default and a command-line option needs to be added to
enable it at boot time.

$ cat /proc/cmdline
[...] secretmem.enable=y

https://bugs.kde.org/451878
https://lwn.net/Articles/865256/
2022-04-19 11:59:36 +02:00
Paul Floyd
60dd674e71 Missing defines for FreeBSD 14 swapoff
+1 typo
2022-04-13 23:32:55 +02:00
Paul Floyd
cdd98111c7 Updates to FreeBSD syscalls for FreeBSD 14
3 new syscalls, and swapoff has a COMPAT13 version.
2022-04-13 23:00:44 +02:00
Paul Floyd
3e7774ea5a Bug 452274 memcheck crashes with Assertion 'sci->status.what == SsIdle' failed
FreeBSD (and Darwin) use the carry flag for syscall syscall status.
That means that in the assembler for do_syscall_for_client_WRK
they have a call to LibVEX_GuestAMD64_put_rflag_c (amd64) or
LibVEX_GuestX86_put_eflag_c (x86). These also call WRK functions.

The problem is that do_syscall_for_client_WRK has carefully crafted
labels correspinding to IP addresses. If a signal interrupts
processdings, IP can be compared to these addresses so that
VG_(fixup_guest_state_after_syscall_interrupted) can work
out how to resume the syscall. But if IP is in the save
carry flag functions, the address is not recognized and
VG_(fixup_guest_state_after_syscall_interrupted) fails.

The crash in the title happens because the interrupted
syscall does not reset its status, and on the next syscall
it is expected that the status be idle.

To fix this I added global variables that get set to 1
just before calling the save carry flag functions, and cleared
just after. VG_(fixup_guest_state_after_syscall_interrupted)
can then check this and work out which section we are in
and resume the syscall correctly.

Also:

Start a new NEWS section for 3.20

Add a regtest for this and also a similar one for Bug 445032
(x86-freebsd only, new subdir).

I saw that this problem also probably exists with macOS, so I made
the same changes there (not yet tested)
2022-04-12 23:50:48 +02:00
Paul Floyd
2b33a318d8 Update Solaris execve with checks for NULL argv
Also requires 2 expected to be updated
2022-04-10 23:02:13 +02:00
Paul Floyd
5f83395616 Fix a crash handling fexecve.
Found this by testing the Solaris execx (the bits that are
Linux-cmpatible) test. That was giving

--28286-- VALGRIND INTERNAL ERROR: Valgrind received a signal 11 (SIGSEGV) - exiting
--28286-- si_code=2;  Faulting address: 0x4A0095A;  sp: 0x1002ca9c88

valgrind: the 'impossible' happened:
   Killed by fatal signal

host stacktrace:
==28286==    at 0x5803DE54: vgPlain_strcpy (m_libcbase.c:309)
==28286==    by 0x5810A9B3: vgSysWrap_linux_sys_execveat_before (syswrap-linux.c:13310)
==28286==    by 0x580953C9: vgPlain_client_syscall (syswrap-main.c:2234)

It's a mistake to copy the path obtained with VG_(resolve_filename) to
the client ARG2, it's unlikely to have space for the path.

Instead just copy the pointer.
2022-04-10 20:55:28 +02:00
Mark Wielaard
8eb547054a Warn for execve syscall with argv or argv[0] being NULL.
For execve valgrind would silently fail when argv was NULL or
unadressable. Make sure that this produces a warning under memcheck.

The linux kernel accepts argv[0] being NULL, but most other kernels
don't since posix says it should be non-NULL and it causes argc to
be zero which is unexpected and might cause security issues.

This adjusts some testcases so they don't rely on execve succeeding
when argv is NULL and expect warnings about argv or argv[0] being
NULL or unaddressable.

https://bugs.kde.org/show_bug.cgi?id=450437
2022-04-06 22:48:45 +02:00
Paul Floyd
f540c79937 Missed macro when adding FreeBSD 13.1
Also add the names of some new FreeBSD 14 syscalls (not yet
inplemented in Valgrind).
2022-04-06 20:54:19 +02:00
Paul Floyd
6c91d2d4ee Extend FreeBSD procctl
4 new types added in FreeBSD 13.1
2022-04-03 18:27:17 +02:00
Paul Floyd
bbc3bcab0a Bug 451843 - valgrind fails to start on a FreeBSD system which enforces W^X
Also add FreeBSD 13.1 to configure.ac
2022-04-03 15:50:38 +02:00
Mark Wielaard
957339db27 bpf attr->raw_tracepoint.name may be NULL for BPF_RAW_TRACEPOINT_OPEN.
For BPF_RAW_TRACEPOINT_OPEN attr->raw_tracepoint.name may be NULL.
Otherwise it should point to a valid (max 128 char) string. Only
raw_tracepoint.prog_fd needs to be set.

https://bugs.kde.org/show_bug.cgi?id=451626
2022-03-19 01:06:40 +01:00
Andreas Arnez
03a8b24ae3 Bug 444552 - s390x: Fix sys_ipc semtimedop syscall
On s390x Linux platforms the sys_ipc semtimedop call has four instead of
five parameters, where the timeout is passed in the third instead of the
fifth.

Reflect this difference in the handling of VKI_SEMTIMEDOP.
2022-02-18 19:46:47 +01:00
Mark Wielaard
c90561e20f Do not try to record fd name for io_uring_setup
In POST(sys_io_uring_setup) we tried to use record_fd_open_with_given_name
with ARG1 as name. But ARG1 isn't a char pointer. So this might crash with
--track-fds=yes. Since no (file) name is associated with the fd returned by
io_uring_setup use record_fd_open_nameless instead.

https://bugs.kde.org/show_bug.cgi?id=449838
2022-02-09 23:37:53 +01:00
Mark Wielaard
1024237358 Implement linux rseq syscall as ENOSYS
This implements rseq for amd64, arm, arm64, ppc32, ppc64,
s390x and x86 linux as ENOSYS (without warning).

glibc will start using rseq to accelerate sched_getcpu, if
available. This would cause a warning from valgrind every
time a new thread is started.

Real rseq (restartable sequences) support is pretty hard, so
for now just explicitly return ENOSYS (just like we do for clone3).

https://sourceware.org/pipermail/libc-alpha/2021-December/133656.html
2021-12-15 12:15:06 +01:00