tests: kernel_file_system fails on macOS hosts: three checks assume Linux behavior #1772

Open
opened 2026-11-26 10:05:24 +00:00 by github-bot · 0 comments
Owner

@⁠dexter288 on GitHub · 2026-10-20 19:14 UTC · original

Summary

kernel_file_system (tests/KernelFileSystemTests.cpp) fails on a macOS host. I looked at each failure, and none of them shows broken emulator behavior. Each check expects a value or a timing that only Linux gives:

  1. PTHREAD_CANCELED. The test compares the join result with glibc's (void*)-1. Added in e87fb8c8 (2026-10-19).
  2. getsockopt(SO_BROADCAST). The test expects the option to read back as 1. macOS gives the flag bit, 0x20.
  3. Loopback UDP. The test calls a nonblocking recvfrom right after send. On macOS the datagram arrives a few tens of µs later, so the read gets EWOULDBLOCK.

#1469 already fixes (2) and (3) as part of a much larger change. As far as I can see, nothing covers (1), because that check is newer than #1469. CI doesn't run this test on any platform (#1371 was closed), which is probably why nobody has noticed.

Environment

  • KytyPS5 source build at 1a053f9e (main, 2026-10-20), Release
  • macOS 26.6.2 (25G83), Apple M5 Pro, 48 GB
  • x86_64 build running under Rosetta 2
  • Apple clang 21.0.0 (Xcode 27.1), CMake 4.4.4, Ninja

1. PTHREAD_CANCELED is compared with glibc's value

tests/KernelFileSystemTests.cpp#L225-L227

KernelFileSystemTests: failed: mutex lock defers cancellation until explicit cancellation point

I split the combined condition and printed each part. The behavior under test works. Only the value comparison fails:

join_rc=0 result=0x1 acquired=1 finished=1

The mutex lock does not act on the pending cancellation, and the thread is canceled at PthreadTestcancel(). PthreadJoin (src/kernel/pthread.cpp#L3356) passes the host's value straight through, and the host value is different on each platform:

Host PTHREAD_CANCELED
Linux (glibc) (void*)-1, the value the test expects
macOS (void*)1 (SDK pthread/pthread.h:147)
Windows (bundled winpthread) (void*)0xDEADBEEF (3rdparty/winpthread/include/pthread.h:130)

From the winpthread header, I expect this check also fails on Windows. I have not run it there.

Possible fixes:

  • Test only: compare with PTHREAD_CANCELED instead of -1. This is the patch I tested below.
  • Emulator: make PthreadJoin translate the host value into one fixed guest value, and have the test expect that value. FreeBSD defines PTHREAD_CANCELED as (void*)1. This would match what #1469 does for SO_BROADCAST. I haven't checked what real PS5 libkernel returns, so I'm leaving the choice to you.

2. getsockopt(SO_BROADCAST) reads back as 0x20 on macOS

tests/KernelFileSystemTests.cpp#L2193-L2200, added in e679486e

KernelFileSystemTests: failed: Net UDP broadcast option can be enabled and disabled

macOS reports an enabled boolean socket option as its flag bit. These values come from a plain C program on the same Mac:

Option set to 1 macOS reads back Linux reads back
SO_BROADCAST 0x20 1
TCP_NODELAY 4 1

Getsockopt (src/libs/network.cpp#L2149) copies the host value through unchanged. #1469 makes every host return FreeBSD's 0x20 and updates this check to expect it. If you prefer that approach, that part of #1469 could probably be merged on its own. If not, a smaller test-only fix is to compare on/off state: (actual != 0) == (value != 0). TCP_NODELAY behaves the same way, but no test reads it back at the moment.

3. Loopback UDP arrives asynchronously on macOS

tests/KernelFileSystemTests.cpp#L2268-L2273 and #L2279-L2285

With (1) and (2) worked around, the next failure is:

KernelFileSystemTests: failed: Net connect selects the peer used by sendto with an omitted destination

I split this check up too. connect returns 0 and send returns 5. The immediate nonblocking recvfrom returns NET_ERROR_EWOULDBLOCK (errno 35). If I retry, the same 5 bytes arrive about 220 µs after the send, so the packet is late, not lost.

The emulator's Recvfrom calls the host recv() directly, so this is how the macOS host behaves. I checked with a plain C program on the same Mac, both arm64 and x86_64 under Rosetta: in 200 out of 200 trials, a nonblocking recv immediately after a loopback UDP send found no data. The packet arrived within 40 to 90 µs every time.

The earlier check at L2268-L2273 (sendto followed by recvfrom) has the same race. It usually passes by luck, but it failed in one of my runs. The fix is to wait for the socket to become readable, using Net::Select (which the test already uses), before both reads. #1469 does this.

Reproducing

  • Failure 1 shows up on any macOS build, before any GPU work:
    ctest --test-dir _Build/macos -R '^kernel_file_system$' --output-on-failure
  • Failures 2 and 3 come after WindowContext::CreateVulkan(), so the test needs a Vulkan device that passes device selection. On macOS that currently means KosmicKrisp plus #1547 and #1669. I used the KosmicKrisp files shipped in shadPS4 0.20.0, loaded with SDL_VULKAN_LIBRARY=.../libvulkan.dylib and VK_DRIVER_FILES=.../kosmickrisp_mesa_icd.json. With stock MoltenVK, the test stops at device selection instead (#1068).

With the three test changes below applied, I got KernelFileSystemTests: all cases passed in 5 out of 5 runs, and ctest -R '^kernel_file_system$' passed.

Test-only change (against f492027b)
@@ -38,6 +38,9 @@
 #include <thread>
 #include <vector>
 
+// PthreadJoin reports cancellation with the host's PTHREAD_CANCELED.
+#include <pthread.h>
+
 #if defined(__linux__)
 #include <arpa/inet.h>
 #include <ifaddrs.h>
@@ -223,7 +226,7 @@ void TestMutexHandoff() {
   waiter.resume = 2;
   void *result = nullptr;
   Check(PthreadJoin(waiter.thread, &result) == OK &&
-            result == reinterpret_cast<void *>(-1) && waiter.acquired && waiter.finished,
+            result == PTHREAD_CANCELED && waiter.acquired && waiter.finished,
         "mutex lock defers cancellation until explicit cancellation point");
   Check(PthreadMutexLock(&mutex) == OK && PthreadMutexUnlock(&mutex) == OK &&
             PthreadMutexDestroy(&mutex) == OK && remove(30) == OK,
@@ -2195,7 +2198,7 @@ void CheckSocketWakeup() {
     uint32_t size = sizeof(actual);
     Check(net_setsockopt(datagram, 0xffff, 0x20, &value, sizeof(value)) == 0 &&
               net_getsockopt(datagram, 0xffff, 0x20, &actual, &size) == 0 &&
-              actual == value && size == sizeof(actual),
+              (actual != 0) == (value != 0) && size == sizeof(actual),
           "Net UDP broadcast option can be enabled and disabled");
   }
   int timeout = 0;
@@ -2265,6 +2268,14 @@ void CheckSocketWakeup() {
                    address.data(), address_size) == sizeof(payload) &&
             *net_errno == Libs::Posix::POSIX_EINVAL,
         "Net sendto delivers to a guest sockaddr and preserves errno on success");
+  // macOS can deliver loopback datagrams after send returns; wait before each nonblocking read.
+  const auto datagram_readable = [&] {
+    std::array<uint64_t, 16> ready {};
+    ready[datagram / 64] = uint64_t {1} << (datagram % 64);
+    const std::array<int64_t, 2> wait {1, 0};
+    return Net::Select(datagram + 1, ready.data(), nullptr, nullptr, wait.data()) == 1;
+  };
+  Check(datagram_readable(), "sent datagram arrives on loopback");
   Check(net_recvfrom(datagram, received.data(), received.size(), 0,
                      peer.data(), &peer_size) == sizeof(payload) &&
             std::memcmp(received.data(), payload, sizeof(payload)) == 0 &&
@@ -2279,9 +2290,11 @@ void CheckSocketWakeup() {
   Check(net_connect(datagram_writer, address.data(), address_size) == 0 &&
             *net_errno == Libs::Posix::POSIX_EINVAL &&
             net_sendto(datagram_writer, payload, sizeof(payload), 0, nullptr, 0) ==
-                sizeof(payload) && *net_errno == Libs::Posix::POSIX_EINVAL &&
-            net_recvfrom(datagram, received.data(), received.size(), 0, nullptr, nullptr) ==
-                sizeof(payload) && std::memcmp(received.data(), payload, sizeof(payload)) == 0,
+                sizeof(payload) && *net_errno == Libs::Posix::POSIX_EINVAL,
+        "Net connect and sendto with an omitted destination succeed");
+  Check(datagram_readable(), "connected datagram arrives on loopback");
+  Check(net_recvfrom(datagram, received.data(), received.size(), 0, nullptr, nullptr) ==
+            sizeof(payload) && std::memcmp(received.data(), payload, sizeof(payload)) == 0,
         "Net connect selects the peer used by sendto with an omitted destination");
   Check(net_connect(-1, address.data(), address_size) == Libs::Network::NET_ERROR_EBADF &&
             *net_errno == Libs::Posix::POSIX_EBADF,

I've opened a focused, test-only PR with these changes: #1770. If you would rather the emulator return a fixed guest value for (1) or (2), I can change it.


AI disclosure: I investigated this with Claude Code (an AI assistant) on my own Mac. The values above come from those runs.

**@⁠dexter288** on GitHub · 2026-10-20 19:14 UTC · [original](https://github.com/KytyPS5/KytyPS5/issues/1772) ## Summary `kernel_file_system` (`tests/KernelFileSystemTests.cpp`) fails on a macOS host. I looked at each failure, and none of them shows broken emulator behavior. Each check expects a value or a timing that only Linux gives: 1. **`PTHREAD_CANCELED`.** The test compares the join result with glibc's `(void*)-1`. Added in e87fb8c8 (2026-10-19). 2. **`getsockopt(SO_BROADCAST)`.** The test expects the option to read back as `1`. macOS gives the flag bit, `0x20`. 3. **Loopback UDP.** The test calls a nonblocking `recvfrom` right after `send`. On macOS the datagram arrives a few tens of µs later, so the read gets `EWOULDBLOCK`. #1469 already fixes (2) and (3) as part of a much larger change. As far as I can see, nothing covers (1), because that check is newer than #1469. CI doesn't run this test on any platform (#1371 was closed), which is probably why nobody has noticed. ## Environment - KytyPS5 source build at 1a053f9e (main, 2026-10-20), Release - macOS 26.6.2 (25G83), Apple M5 Pro, 48 GB - x86_64 build running under Rosetta 2 - Apple clang 21.0.0 (Xcode 27.1), CMake 4.4.4, Ninja ## 1. `PTHREAD_CANCELED` is compared with glibc's value [`tests/KernelFileSystemTests.cpp#L225-L227`](https://github.com/KytyPS5/KytyPS5/blob/1a053f9e13405bf34171a47aae9f2b9a6e7afd7d/tests/KernelFileSystemTests.cpp#L225-L227) ``` KernelFileSystemTests: failed: mutex lock defers cancellation until explicit cancellation point ``` I split the combined condition and printed each part. The behavior under test works. Only the value comparison fails: ``` join_rc=0 result=0x1 acquired=1 finished=1 ``` The mutex lock does not act on the pending cancellation, and the thread is canceled at `PthreadTestcancel()`. `PthreadJoin` ([`src/kernel/pthread.cpp#L3356`](https://github.com/KytyPS5/KytyPS5/blob/1a053f9e13405bf34171a47aae9f2b9a6e7afd7d/src/kernel/pthread.cpp#L3356)) passes the host's value straight through, and the host value is different on each platform: | Host | `PTHREAD_CANCELED` | |---|---| | Linux (glibc) | `(void*)-1`, the value the test expects | | macOS | `(void*)1` (SDK `pthread/pthread.h:147`) | | Windows (bundled winpthread) | `(void*)0xDEADBEEF` (`3rdparty/winpthread/include/pthread.h:130`) | From the winpthread header, I expect this check also fails on Windows. I have not run it there. Possible fixes: - **Test only:** compare with `PTHREAD_CANCELED` instead of `-1`. This is the patch I tested below. - **Emulator:** make `PthreadJoin` translate the host value into one fixed guest value, and have the test expect that value. FreeBSD defines `PTHREAD_CANCELED` as `(void*)1`. This would match what #1469 does for `SO_BROADCAST`. I haven't checked what real PS5 libkernel returns, so I'm leaving the choice to you. ## 2. `getsockopt(SO_BROADCAST)` reads back as `0x20` on macOS [`tests/KernelFileSystemTests.cpp#L2193-L2200`](https://github.com/KytyPS5/KytyPS5/blob/1a053f9e13405bf34171a47aae9f2b9a6e7afd7d/tests/KernelFileSystemTests.cpp#L2193-L2200), added in e679486e ``` KernelFileSystemTests: failed: Net UDP broadcast option can be enabled and disabled ``` macOS reports an enabled boolean socket option as its flag bit. These values come from a plain C program on the same Mac: | Option set to 1 | macOS reads back | Linux reads back | |---|---|---| | `SO_BROADCAST` | `0x20` | `1` | | `TCP_NODELAY` | `4` | `1` | `Getsockopt` ([`src/libs/network.cpp#L2149`](https://github.com/KytyPS5/KytyPS5/blob/1a053f9e13405bf34171a47aae9f2b9a6e7afd7d/src/libs/network.cpp#L2149)) copies the host value through unchanged. #1469 makes every host return FreeBSD's `0x20` and updates this check to expect it. If you prefer that approach, that part of #1469 could probably be merged on its own. If not, a smaller test-only fix is to compare on/off state: `(actual != 0) == (value != 0)`. `TCP_NODELAY` behaves the same way, but no test reads it back at the moment. ## 3. Loopback UDP arrives asynchronously on macOS [`tests/KernelFileSystemTests.cpp#L2268-L2273`](https://github.com/KytyPS5/KytyPS5/blob/1a053f9e13405bf34171a47aae9f2b9a6e7afd7d/tests/KernelFileSystemTests.cpp#L2268-L2273) and [`#L2279-L2285`](https://github.com/KytyPS5/KytyPS5/blob/1a053f9e13405bf34171a47aae9f2b9a6e7afd7d/tests/KernelFileSystemTests.cpp#L2279-L2285) With (1) and (2) worked around, the next failure is: ``` KernelFileSystemTests: failed: Net connect selects the peer used by sendto with an omitted destination ``` I split this check up too. `connect` returns 0 and `send` returns 5. The immediate nonblocking `recvfrom` returns `NET_ERROR_EWOULDBLOCK` (errno 35). If I retry, the same 5 bytes arrive about 220 µs after the send, so the packet is late, not lost. The emulator's `Recvfrom` calls the host `recv()` directly, so this is how the macOS host behaves. I checked with a plain C program on the same Mac, both arm64 and x86_64 under Rosetta: in 200 out of 200 trials, a nonblocking `recv` immediately after a loopback UDP `send` found no data. The packet arrived within 40 to 90 µs every time. The earlier check at L2268-L2273 (`sendto` followed by `recvfrom`) has the same race. It usually passes by luck, but it failed in one of my runs. The fix is to wait for the socket to become readable, using `Net::Select` (which the test already uses), before both reads. #1469 does this. ## Reproducing - **Failure 1** shows up on any macOS build, before any GPU work: `ctest --test-dir _Build/macos -R '^kernel_file_system$' --output-on-failure` - **Failures 2 and 3** come after `WindowContext::CreateVulkan()`, so the test needs a Vulkan device that passes device selection. On macOS that currently means KosmicKrisp plus #1547 and #1669. I used the KosmicKrisp files shipped in shadPS4 0.20.0, loaded with `SDL_VULKAN_LIBRARY=.../libvulkan.dylib` and `VK_DRIVER_FILES=.../kosmickrisp_mesa_icd.json`. With stock MoltenVK, the test stops at device selection instead (#1068). With the three test changes below applied, I got `KernelFileSystemTests: all cases passed` in 5 out of 5 runs, and `ctest -R '^kernel_file_system$'` passed. <details> <summary>Test-only change (against f492027b)</summary> ```diff @@ -38,6 +38,9 @@ #include <thread> #include <vector> +// PthreadJoin reports cancellation with the host's PTHREAD_CANCELED. +#include <pthread.h> + #if defined(__linux__) #include <arpa/inet.h> #include <ifaddrs.h> @@ -223,7 +226,7 @@ void TestMutexHandoff() { waiter.resume = 2; void *result = nullptr; Check(PthreadJoin(waiter.thread, &result) == OK && - result == reinterpret_cast<void *>(-1) && waiter.acquired && waiter.finished, + result == PTHREAD_CANCELED && waiter.acquired && waiter.finished, "mutex lock defers cancellation until explicit cancellation point"); Check(PthreadMutexLock(&mutex) == OK && PthreadMutexUnlock(&mutex) == OK && PthreadMutexDestroy(&mutex) == OK && remove(30) == OK, @@ -2195,7 +2198,7 @@ void CheckSocketWakeup() { uint32_t size = sizeof(actual); Check(net_setsockopt(datagram, 0xffff, 0x20, &value, sizeof(value)) == 0 && net_getsockopt(datagram, 0xffff, 0x20, &actual, &size) == 0 && - actual == value && size == sizeof(actual), + (actual != 0) == (value != 0) && size == sizeof(actual), "Net UDP broadcast option can be enabled and disabled"); } int timeout = 0; @@ -2265,6 +2268,14 @@ void CheckSocketWakeup() { address.data(), address_size) == sizeof(payload) && *net_errno == Libs::Posix::POSIX_EINVAL, "Net sendto delivers to a guest sockaddr and preserves errno on success"); + // macOS can deliver loopback datagrams after send returns; wait before each nonblocking read. + const auto datagram_readable = [&] { + std::array<uint64_t, 16> ready {}; + ready[datagram / 64] = uint64_t {1} << (datagram % 64); + const std::array<int64_t, 2> wait {1, 0}; + return Net::Select(datagram + 1, ready.data(), nullptr, nullptr, wait.data()) == 1; + }; + Check(datagram_readable(), "sent datagram arrives on loopback"); Check(net_recvfrom(datagram, received.data(), received.size(), 0, peer.data(), &peer_size) == sizeof(payload) && std::memcmp(received.data(), payload, sizeof(payload)) == 0 && @@ -2279,9 +2290,11 @@ void CheckSocketWakeup() { Check(net_connect(datagram_writer, address.data(), address_size) == 0 && *net_errno == Libs::Posix::POSIX_EINVAL && net_sendto(datagram_writer, payload, sizeof(payload), 0, nullptr, 0) == - sizeof(payload) && *net_errno == Libs::Posix::POSIX_EINVAL && - net_recvfrom(datagram, received.data(), received.size(), 0, nullptr, nullptr) == - sizeof(payload) && std::memcmp(received.data(), payload, sizeof(payload)) == 0, + sizeof(payload) && *net_errno == Libs::Posix::POSIX_EINVAL, + "Net connect and sendto with an omitted destination succeed"); + Check(datagram_readable(), "connected datagram arrives on loopback"); + Check(net_recvfrom(datagram, received.data(), received.size(), 0, nullptr, nullptr) == + sizeof(payload) && std::memcmp(received.data(), payload, sizeof(payload)) == 0, "Net connect selects the peer used by sendto with an omitted destination"); Check(net_connect(-1, address.data(), address_size) == Libs::Network::NET_ERROR_EBADF && *net_errno == Libs::Posix::POSIX_EBADF, ``` </details> I've opened a focused, test-only PR with these changes: #1770. If you would rather the emulator return a fixed guest value for (1) or (2), I can change it. --- _AI disclosure: I investigated this with Claude Code (an AI assistant) on my own Mac. The values above come from those runs._
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:05:24 +00:00
This discussion has been locked. Commenting is limited to contributors.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/KytyPS5--KytyPS5#1772
No description provided.