Repository navigation
Description
After @Keno 's patch finally went in some form into Wine I found there still happen diversions.
So I tried to bisect and found Wine was changing how a shared memory between wineserver and the gui processes get mapped. Unfortunately there were a few more commits making this one not alone revertable.
But I could strip it down to this modification to wine to make replaying work again by using the previous mapping flag MAP_SHARED instead of MAP_PRIVATE:
--- a/dlls/ntdll/unix/virtual.c
+++ b/dlls/ntdll/unix/virtual.c
@@ -2351,7 +2351,13 @@ static NTSTATUS map_file_into_view( struct file_view *view, int fd, size_t start
/* changes to the file are not guaranteed to be visible in read-only MAP_PRIVATE mappings,
* but they are on Linux so we take advantage of it */
#ifdef __linux__
+ if (vprot & SEC_FILE) {
flags |= MAP_PRIVATE;
+ } else {
+ flags |= MAP_SHARED;
+ prot &= ~PROT_WRITE;
+ //ERR("fd=%d flags=0x%x prot=0x%x vprot=0x%x:%d\n", fd, flags, prot, vprot, __LINE__);
+ }
#else
flags |= MAP_SHARED;
prot &= ~PROT_WRITE;This is what I got, but there was no relation to a shared memory visible:
[FATAL src/ReplaySession.cc:1278:check_ticks_consistency()]
(task 1131280 (rec:1131219) at time 151589)
-> Assertion `ticks_now == trace_ticks' failed to hold. ticks mismatch for 'SYSCALL: read'; expected 24071991, got 24071980
So I tried prove this shared memory being the issue by spraying such calls syscall(-1, 0, 0); in the functions shown by the emergency shell and reached that way function shared_object_get_id in Wine, with a better to follow register mismatch:
[FATAL src/ReplayTask.cc:130:validate_regs()]
(task 1123897 (rec:1123824) at time 170377)
-> Assertion `!comparison.mismatch_count' failed to hold. Mismatched registers, replay vs rec: r12 (nil) != 0x18
104 lock.id = object->id;
0x00007f7e41638440 <+127>: mov 0x8(%rbx),%r12
0x00007f7e41638444 <+131>: mov %r12,-0x40(%rbp)
There the memory in question looks like this:
(gdb) print/x *(const shared_object_t *) 0x7ffffe9e0ef8
$5 = {seq = 0x6, id = 0x0, shm = {desktop = {flags = 0xfefefefe, cursor = {x = 0xfefefefe, y = 0xfefefefe, last_change = 0xfefefefe, clip = {left = 0xfefefefe, top = 0xfefefefe, right = 0xfefefefe,
bottom = 0xfefefefe}}, keystate = {0xfe <repeats 256 times>}, monitor_serial = 0xfefefefefefefefe}, queue = {hooks_count = {0xfefefefe <repeats 17 times>}, wake_mask = 0xfefefefe, wake_bits = 0xfefefefe,
changed_mask = 0xfefefefe, changed_bits = 0xfefefefe}, input = {foreground = 0xfefefefe, active = 0xfefefefe, focus = 0xfefefefe, capture = 0xfefefefe, menu_owner = 0xfefefefe, move_size = 0xfefefefe,
caret = 0xfefefefe, caret_rect = {left = 0xfefefefe, top = 0xfefefefe, right = 0xfefefefe, bottom = 0xfefefefe}, cursor = 0xfefefefe, cursor_count = 0xfefefefe, keystate = {0xfe <repeats 256 times>},
keystate_lock = 0xfefefefe}}}
When I replay the wineserver to short before the diversion I see this state of the mapping:
(rr) print/x *(const shared_object_t *)0x7f872e6d0ef8
$1 = {seq = 0xe, id = 0x18, shm = {desktop = {flags = 0x0, cursor = {x = 0x0, y = 0x0, last_change = 0x0, clip = {left = 0x0, top = 0x0, right = 0x0, bottom = 0x0}}, keystate = {0x0 <repeats 41 times>, 0x80, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x80, 0x0, 0x0, 0x55 <repeats 204 times>}, monitor_serial = 0x5555555555555555}, queue = {hooks_count = {0x0 <repeats 17 times>}, wake_mask = 0x0, wake_bits = 0x8000,
changed_mask = 0x0, changed_bits = 0x8000}, input = {foreground = 0x0, active = 0x0, focus = 0x0, capture = 0x0, menu_owner = 0x0, move_size = 0x0, caret = 0x0, caret_rect = {left = 0x0, top = 0x0,
right = 0x0, bottom = 0x0}, cursor = 0x0, cursor_count = 0x0, keystate = {0x0 <repeats 21 times>, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x80, 0x0, 0x0, 0x55 <repeats 224 times>},
keystate_lock = 0x55555555}}}
So it looks like this mapping was not propagating changes to the memory in wineserver to the wine process, which seems to show some initial state of it.
To make things more complicated this shared memory is created by wineserver by creating a temporary file and sending the fd through a socket to the wine process.
I tried to replicate this in a rr testcase, but unfortunately this is not showing the issue, so there is maybe more to it:
mmap_private_from_fd_via_socket.c.txt
I had made some tests a while ago for this issue and was seeing it at Intel hardware too, so it seems no AMD related thing.
Is this probably a known issue in rr?
Any hints how to continue from here?
PS.: To speed up things I am just recording wineserver and the wine process I am interested in. But the issue appears also when recording the whole tree.
rr record bash -c "wineserver -k; wineserver -p60; rr record --nested=release wine wineconsole cmd /c \"timeout /t 10\" & sleep 8s; echo start; WINEDLLOVERRIDES=winemenubuilder.exe=n WINEDEBUG=+pid,+timestamp wine winemine; sleep 1s; wineserver -k; sleep 1s"```
Activity
rocallahan commentedon May 31, 2025
This comment looks suspicious:
https://gitlab.winehq.org/wine/wine/-/blob/master/dlls/ntdll/unix/virtual.c?ref_type=heads#L2351
In rr, changes to the file behind a
MAP_PRIVATEmapping will generally not be reflected in aMAP_PRIVATEmapping even if it's read-only. ForMAP_PRIVATEmappings we generally make some kind of copy of the file data at mapping time. As the comment hints, applications aren't really supposed to depend on readonlyMAP_PRIVATEmappings tracking updates to the underlying file.rocallahan commentedon May 31, 2025
I don't know what that commit was really trying to do.
rr has
MmappedFileMonitorto track file writes that should be echoed toMAP_SHAREDmappings. Extending it toMAP_PRIVATEas well might work but would be pretty expensive :-(.bernhardu commentedon May 31, 2025
I mixed up the flags when I tried to create a rr test. This version shows a similar assertion as Wine:
mmap_private_from_fd_via_socket.c.txt
[-]Replaying a Wine recording triggers `Assertion 'ticks_now == trace_ticks'`[/-][+]Replaying a Wine recording triggers `Assertion 'ticks_now == trace_ticks'`, mmap with MAP_PRIVATE[/+]bernhardu commentedon Jun 1, 2025
Thanks for the hints. Unfortunately all my tests to get into the MmappedFileMonitor path did still show a register diversion up to now.
rocallahan commentedon Jun 2, 2025
MmappedFileMonitorwon't help us here. The testcase doesMAP_PRIVATEmapping of itMAP_SHAREDmapping of it(The socket stuff is not relevant.)
During replay, when rr creates the private mapping, we create anonymous pages and copy the recorded initial contents of the mapping into those pages. So those pages aren't connected to the shared mapping.
We can probably fix this by making non-anonymous private mappings use the shared-mapping path, i.e. we create a sharable file for the mapped pages, and create a private mapping for it during replay just like we do during recording.
rocallahan commentedon Jun 2, 2025
Unfortunately, making MAP_PRIVATE behave more like MAP_SHARED means making replay a lot slower in general because when we clone sessions we have to replicate the structure of shared mappings in the cloned processes :-(. That seems like a sad tradeoff to support Wine depending on behaviour that Linux doesn't even guarantee.
bernhardu commentedon Jun 2, 2025
In this case, would it be something to give a warning about this private mapping which may cause issues?
Can this situation safe enough detected?
So this is then maybe the point to involve Wine developer, if this is really the intended behaviour?
Maybe instead of slowing down rr in general, it might be possible to get a "switch" into Wine to avoid this,
something like in my opening comment?
Thanks for you time.
rocallahan commentedon Jun 2, 2025
If you can figure out which file(s) this matters for and why they are being treated this way, that would be very useful.
It's not obvious to me why you would want to do this instead of just mapping them shared but readonly, like Wine used to do.
bernhardu commentedon Jun 3, 2025
Will try to get some more infos.
About the files - if I understand it right, this situation is currently just for one mapping between wineserver and a .exe process, when it creates some "shared session block". And for this the file is just the vehicle to have both processes having the same memory mapped - the content of the file is after the mapping not of interest.
(Would it be easier to lookup for the private mapping, if that file is mapped in some other process and leave the file with the inital content, but "connect" the two memory areas?)
rocallahan commentedon Jun 3, 2025
Ok.
The commit message of https://gitlab.winehq.org/wine/wine/-/commit/07160a23fa2139c2dc9cad92158ddf0b64a3e75e says "So that PAGE_WRITECOPY can be set after the fact". That makes sense. But I would have expected that you can switch the mapping from shared to private by re-mapping the file object, although that's more complicated and probably a bit slower.
rocallahan commentedon Jun 3, 2025
I guess a possible workaround might be to have
map_shared_session_blockuse mmap directly instead ofNtMapViewOfSection?bernhardu commentedon Jun 3, 2025
As a workaround the modification from my initial comment is working for me, which is more or less just for this single mapping at .exe side using map shared instead map private, like the compile switch would do for all non-linux systems.
rocallahan commentedon Jun 4, 2025
Do you want to try to get that upstream?
bernhardu commentedon Jun 5, 2025
I am not sure, I tried to bring it to Wine developers attention:
https://bugs.winehq.org/show_bug.cgi?id=58332
Let's see where this gets us.
bernhardu commentedon Jun 14, 2026
Sorry for the delay, I am afraid following workaround was not much liked in Wine upstream:
https://gitlab.winehq.org/wine/wine/-/merge_requests/10925
rocallahan commentedon Jun 15, 2026
Let's try to hack around it.
In the WINE case, does the wineserver always map the file
MAP_SHAREDbefore the application process maps itMAP_PRIVATE?bernhardu commentedon Jun 15, 2026
For convenience putting a few links here, adding to the backtraces of the comment above:
So I would say yes, that file gets always mapped
MAP_SHAREDby wineserver, later the fd gets transferred to the .exe process and there mappedMAP_PRIVATE.bernhardu commentedon Jun 17, 2026
I understood it probably wrong, if the question was if wineserver maps once for each application process. Then I think no, wineserver maps once
MAP_SHAREDand each application process maps then multiple timesMAP_PRIVATE.rocallahan commentedon Jul 19, 2026
I think maybe what we can do is, if we see a readonly
MAP_PRIVATEof an fd that isMAP_SHAREDelsewhere, just treat it as shared.