Merge pull request #15884 from zaryabzip/fix/kernel-ampr-event-bad-queue

fix(libkernel): return EBADF from sceKernelAddAmprEvent for an equeue that is not live
This commit is contained in:
Dean authored and GitHub committed 2026-11-08 12:04:03 +13:00
commit 9e47f0c3ac
3 files changed
+4 -6

No files matched your search

+1 -6
View File
@@ -842,9 +842,6 @@ int APS5_VABI sceKernelDeleteTimerEvent(KernelEqueue eq, int id) {
}
int APS5_VABI sceKernelAddAmprEvent(KernelEqueue eq, int id, void* udata) {
if (eq == 0) {
return EQUEUE_OK;
}
KernelEqueueEvent event{};
event.event.ident = static_cast<uintptr_t>(id);
event.event.filter = EVFILT_AMPR;
@@ -867,9 +864,7 @@ int APS5_VABI sceKernelAddAmprEvent(KernelEqueue eq, int id, void* udata) {
e->event.data = 0;
}
};
EqueueAddEvent_nid_postfix(eq, event);
return EQUEUE_OK;
return EqueueAddEvent_nid_postfix(eq, event);
}
int APS5_VABI sceKernelAddAmprSystemEvent(KernelEqueue eq, int id, void* udata) {
+2
View File
@@ -244,6 +244,8 @@ static void VerifyRepeatedAmprCompletionsAreNotLost() {
Require(sceKernelWaitEqueue(eq, events, 4, &count, &poll) == SCE_KERNEL_ERROR_ETIMEDOUT);
Require(sceKernelDeleteAmprEvent(eq, 3) == SCE_OK);
Require(sceKernelDeleteEqueue(eq) == SCE_OK);
Require(sceKernelAddAmprEvent(eq, 3, &udata) == SCE_KERNEL_ERROR_EBADF);
Require(sceKernelAddAmprEvent(0, 3, &udata) == SCE_KERNEL_ERROR_EBADF);
}
static void VerifyUndrainedTriggersStayBounded() {
+1
View File
@@ -190,6 +190,7 @@
- [sceKernelWaitSema and sceKernelSignalSema](../../../core/libs/prx/libkernel/Semaphore/src/Semaphore.cpp) (libkernel) queue waiters in arrival order, or with `SCE_KERNEL_SEMA_ATTR_TH_PRIO` (0x02) by the priority the waiting thread has when it starts to wait, lower values first and equal ones in arrival order. `sceKernelSignalSema` hands its tokens to them in that order, passing over one that needs more than is left, and a `sceKernelWaitSema` or `sceKernelPollSema` that finds enough tokens takes them without queueing, as shadPS4's [semaphore](https://github.com/shadps4-emu/shadPS4/blob/06e813ff8c8e2a6d81b02cabfcc9543c3debf78e/src/core/libraries/kernel/threads/semaphore.cpp#L159-L206) does. A priority change while a thread waits does not move it in the queue, an attribute of 0 queues in arrival order, and the console's order and its rule for a waiter that needs more than is left are unverified
- [sem_setname](../../../core/libs/prx/libkernel/Pthread/Posix/Sem.cpp) (libkernel) takes `(sem, const char* name)` and registers the name once through the kernel's `namedobj_create`, which is not modelled here: only the one-time registration is kept, as a flag on the host semaphore, and the name is not stored or read back. The export is in `libkernel` and the first dword of the semaphore holds `0xffff736d` before the call and `(id << 16) | 0x736d` after; a second call, and a call on a zeroed semaphore, return -1 with `EAGAIN` and no syscall, all taken from BunnyFl's firmware 13.42 disassembly and emulator run on #11267. A destroyed semaphore (zeroed here) was not measured on its own and answers as a zeroed one. A null semaphore faults in the module and a null name is passed unchecked to the kernel, so both throw here, as does a failing `namedobj_create`, whose errors are not known.
- [kevent measurements](../../../core/libs/prx/libkernel/Equeue/src/Kqueue.cpp) (libkernel) - in payload processes on a PS5 (firmware 12.70), re-adding an existing periodic `EVFILT_TIMER` with `EV_ADD`, `fflags` 0 and a positive period kept the armed expiry and the expirations already counted and applied the new period after that expiry, as FreeBSD 11.0 does, where 11.4's `filt_timertouch` restarts the timer: a 50 ms timer re-added with 10 ms fired first 46 ms after the re-add and then every 10 ms, a 10 ms timer re-added with 100 ms fired first 5 ms after the re-add and then every 100 ms, the 12 expirations counted before a re-add were reported after it, and `EV_ONESHOT` in the re-add left the timer periodic. Here such a re-add does the same; on firmware 13.60 a re-add with a unit in `fflags` (`NOTE_MSECONDS`, `NOTE_NSECONDS`) also applied the new period after the armed expiry, and re-adding a disabled timer returned 0 and left it disabled, as here; re-adding an armed one-shot timer, a period of 0 or less, a period too large to add to the armed expiry, other `fflags` or flags other than `EV_ONESHOT` were not measured and throw here, and the other changes to an existing timer except `EV_DELETE` throw here too. There `EV_ADD` failed with `EPERM` for `EVFILT_AIO`, `EVFILT_LIO` and `EVFILT_SENDFILE` at every ident tried (0, 5, 999, 2^40 and an open pipe) and, at ident 5, also with `EV_CLEAR`, `EV_ONESHOT` or `EV_DISABLE` added, and `EV_DELETE` and `EV_ENABLE` without `EV_ADD` failed with `ENOENT`. `EVFILT_PROCDESC` failed with `EBADF` on a descriptor that is not open and on an ident above `INT_MAX`, and with `EINVAL` on every open descriptor tried: Unix-domain stream and datagram sockets, descriptor 0 (a socket), an `AF_INET` UDP socket, both ends of a pipe, a regular file, a directory, another kqueue and the kqueue itself; `EV_DELETE` on a socket failed with `ENOENT`. Here these four filters answer the same way. `EV_ADD` of `EVFILT_PROCDESC` fails with `EINVAL` on the kinds of descriptor tried, since there are no process descriptors here, and also on shared memory and JIT shared memory, which are host files here: FreeBSD 11.0 gives `shm_open` objects [`invfo_kqfilter`](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/sys/kern/uipc_shm.c#L136), which answers `EINVAL`, but neither was measured. It throws on any other kind, such as `/dev/random` and `/dev/urandom`, and on descriptors 0 to 2 that are not sockets or kqueues, whatever they hold: the host's standard streams, a pipe or file that `dup2` put on one of them, or a descriptor opened on a number that was free when libkernel loaded. A change of any of the four without `EV_ADD` fails with `ENOENT`, for `EVFILT_PROCDESC` after the descriptor check, as `kqueue_register` finds no knote. When `EV_ADD` of one of the four fails with `EPERM`, or with `EINVAL` on a descriptor other than the kqueue itself, the error event has `fflags` 0 and the errno in `data`, as FreeBSD 11.0's [`kqueue_register`](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/sys/kern/kern_event.c#L1302-L1316) clears `fflags` and `data` before the attach and [`kqueue_kevent`](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/sys/kern/kern_event.c#L1005-L1006) then stores the error in `data`; a descriptor that is not open, the kqueue itself and `EV_ENABLE` with `EV_DISABLE` fail before that and keep their `fflags`. That error event was not measured. `EVFILT_PROC` on the process itself with `NOTE_EXIT` was accepted there and is accepted here as FreeBSD 11.4's [`filt_procattach`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_event.c) accepts it: the knote gets `EV_CLEAR` and never fires while the process runs; another process and notes other than `NOTE_EXIT` still throw. `EVFILT_FS` at ident 0, also accepted there, is accepted here (see the kqueue and kevent entry). `EVFILT_SIGNAL` for `SIGUSR1` was accepted there and is accepted here; its `data` counts only the signals that `raise`, `kill`, `killpg` and `pthread_kill` send and the `ITIMER_REAL` expiry, and the counts the PS5 reports were not measured. Also measured there on Unix-domain socketpairs, and still throwing here: a write mark of 2 on the other end of that stream socketpair gave an event (`data` 8192), and re-adding that knote with 2^30 gave none; a mark of 5 on a datagram socket gave no read event after one 1-byte datagram and a read event (`data` 11) after a second, 10-byte datagram. `EVFILT_READ` on another kqueue reported the number of events pending in it there (1, then 2) and throws here; `EVFILT_WRITE` on a kqueue and a kqueue registered on itself failed with `EINVAL` there, as they do here
- [sceKernelAddAmprEvent and sceKernelAddAmprSystemEvent](../../../core/libs/prx/libkernel/Equeue/Equeue.cpp) (libkernel) - `sceKernelAddAmprEvent` returns `EBADF` (0x80020009) for an equeue that is not live, a null one included, as [obSCEne's census](https://github.com/project-oops/obSCEne/blob/de5efc406319ade18df00654025494804405a7fb/reports/hardware/report-20261013-172400.log#L25680-L25694) measured in its own title on a PS5 with system software 12.40, as `sceKernelAddTimerEvent` already does here. `sceKernelAddAmprSystemEvent` forwards to it and so returns the same; that one was not probed
- [tcsendbreak, tcdrain, tcflush, tcflow, tcgetpgrp, tcsetpgrp](../../../core/libs/prx/libkernel/File/src/Terminal.cpp) (libkernel) follow FreeBSD 11.4's [`termios.c`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/libc/gen/termios.c): `tcflush` answers `EINVAL` for a queue other than `TCIFLUSH`, `TCOFLUSH` or `TCIOFLUSH` and `tcflow` for an action outside `TCOOFF`..`TCION` before the descriptor is looked at, and every other call fails as its terminal ioctl does on a descriptor that is not a terminal, with the errors `tcgetattr` gives, so `tcsendbreak` never reaches its 0.4 s pause. FreeBSD's `pipe_ioctl` answers `TIOCGPGRP` and `TIOCSPGRP` on a pipe or FIFO with the descriptor's SIGIO owner, which is not modelled, so `tcgetpgrp` and `tcsetpgrp` throw on a host pipe or FIFO above descriptor 2; descriptors 0 to 2 are taken as non-terminals that are not pipes, whatever the host attaches to them, so a pipe the guest creates on a freed descriptor 0 to 2 gets `ENOTTY` there. The PS5 `libkernel` is assumed to use this FreeBSD code, and `tcdrain` is not a cancellation point here
- [Windows file timestamps](../../../core/libs/prx/libkernel/File/include/WindowsFileTime.hpp) use the host filesystem's timestamp range and resolution. `utimes`, `futimes`, and `lutimes` reject timestamps outside nonnegative signed 64-bit Windows file-time ticks with `EINVAL`, including exactly 1601-01-01 00:00:00 UTC, whose zero value means "leave unchanged" to `SetFileTime`. Directory timestamp updates resolve the current path of the opened handle. Filesystems with coarser timestamp resolution cannot retain every guest microsecond. Birth-time clamping is not atomic with concurrent host timestamp updates and has not been measured on a console
- [EVFILT_VNODE on macOS](../../../core/libs/prx/libkernel/Equeue/src/FileEvent.cpp) (libkernel) registers `EVFILT_VNODE` on a duplicate of the descriptor in a host kqueue, whose notes have FreeBSD's values, and drops the `NOTE_LINK` that XNU adds to `NOTE_DELETE` when the watched file or directory is removed, since FreeBSD's `vop_remove_post` and `vop_rmdir_post` report `NOTE_DELETE` alone. As on Windows, a directory registered with `NOTE_EXTEND` is accepted but never receives it: XNU reports a move between directories as `NOTE_WRITE` alone, with `NOTE_LINK` for a subdirectory